-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathchat-routes.ts
More file actions
211 lines (200 loc) Β· 6.7 KB
/
chat-routes.ts
File metadata and controls
211 lines (200 loc) Β· 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { type OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { sessionResponseSchema } from "@nexu/shared";
import type { ControllerContainer } from "../app/container.js";
import { ChatService } from "../services/chat-service.js";
import type { ControllerBindings } from "../types.js";
// ~10 MB base64 cap β raw file β 7.5 MB; prevents OOM/body-size attacks
const MAX_ATTACHMENT_CONTENT_BYTES = 10_000_000;
const chatAttachmentSchema = z.object({
// Images go through OpenClaw's chat.send attachments pipeline unchanged.
// Files (PDF / text-readable) are extracted on the controller and folded
// into message text as <file>β¦</file> blocks before forwarding; OpenClaw's
// gateway RPC itself only carries images over the wire.
type: z.enum(["image", "file"]),
content: z.string().max(MAX_ATTACHMENT_CONTENT_BYTES),
metadata: z
.object({
mimeType: z.string().optional(),
filename: z.string().optional(),
size: z.number().optional(),
})
.optional(),
});
const localChatMessageInputSchema = z.object({
type: z.enum(["text", "image", "video", "audio", "file"]),
content: z.string().max(MAX_ATTACHMENT_CONTENT_BYTES),
metadata: z
.object({
width: z.number().optional(),
height: z.number().optional(),
duration: z.number().optional(),
mimeType: z.string().optional(),
filename: z.string().optional(),
size: z.number().optional(),
})
.optional(),
/** Optional additional attachments for multipart (text + images/files) messages */
attachments: z.array(chatAttachmentSchema).optional(),
});
const localChatMessageOutputSchema = z.object({
id: z.string(),
role: z.string(),
type: z.string(),
content: z.unknown(),
timestamp: z.number().nullable(),
createdAt: z.string().nullable(),
});
export function registerChatRoutes(
app: OpenAPIHono<ControllerBindings>,
container: ControllerContainer,
): void {
const chatService = new ChatService(
container.gatewayService,
container.attachmentStore,
);
// GET /api/v1/chat/session - Resolve a named sessionKey to a real session
app.openapi(
createRoute({
method: "get",
path: "/api/v1/chat/session",
tags: ["Chat"],
request: {
query: z.object({
botId: z.string(),
sessionKey: z.string(),
}),
},
responses: {
200: {
description: "Session resolved from sessionKey",
content: {
"application/json": {
schema: z.object({
session: sessionResponseSchema.nullable(),
}),
},
},
},
},
}),
async (c) => {
const { botId, sessionKey } = c.req.valid("query");
const session = await container.sessionService.getSessionBySessionKey(
botId,
sessionKey,
);
return c.json({ session });
},
);
// POST /api/v1/chat/local - Send local chat message (direct to main session)
app.openapi(
createRoute({
method: "post",
path: "/api/v1/chat/local",
tags: ["Chat"],
request: {
body: {
content: {
"application/json": {
schema: z.object({
botId: z.string(),
sessionKey: z.string(),
message: localChatMessageInputSchema,
}),
},
},
},
},
responses: {
200: {
description: "Local chat message sent",
content: {
"application/json": {
schema: z.object({
session: sessionResponseSchema.nullable(),
message: localChatMessageOutputSchema,
}),
},
},
},
},
}),
async (c) => {
const { botId, message } = c.req.valid("json");
// Send message to agent main session β do NOT pre-create the session
// here. Pre-creating writes an empty key-based .jsonl file that
// appears as a ghost entry in the sessions list. OpenClaw will create
// the real UUID-named JSONL and register it in sessions.json as part of
// processing chat.send, so we look it up afterwards.
const result = await chatService.sendLocalMessage(botId, message);
// Best-effort session lookup immediately after send. sessions.json is
// flushed asynchronously by OpenClaw, so this may return null on the
// very first message. The frontend handles that case with its own
// 3-second discovery retry loop β no server-side sleep needed here.
//
// Always look up via the main session key β chat.send always targets
// agent:{botId}:main regardless of which channel key the frontend
// sends in the body (the body's sessionKey is informational only).
const mainSessionKey = `agent:${botId}:main`;
const session = await container.sessionService.getSessionBySessionKey(
botId,
mainSessionKey,
);
return c.json({
session,
message: result,
});
},
);
// GET /api/v1/chat/history - Full aggregated message history across all
// compacted sessions for a bot's main webchat conversation.
// When OpenClaw performs context compaction it creates a new UUID-named JSONL
// and leaves previous session files orphaned on disk. This endpoint collects
// all such orphaned files plus the current main session file, sorts them
// chronologically, and returns the concatenated message list β giving the
// frontend a single continuous timeline regardless of how many compactions
// have occurred.
app.openapi(
createRoute({
method: "get",
path: "/api/v1/chat/history",
tags: ["Chat"],
request: {
query: z.object({
botId: z.string(),
limit: z.coerce.number().int().min(1).max(2000).optional(),
}),
},
responses: {
200: {
description:
"Full conversation history aggregated across all compacted sessions",
content: {
"application/json": {
schema: z.object({
messages: z.array(
z.object({
id: z.string(),
role: z.enum(["user", "assistant"]),
content: z.unknown(),
timestamp: z.number().nullable(),
createdAt: z.string().nullable(),
}),
),
sessionCount: z.number(),
}),
},
},
},
},
}),
async (c) => {
const { botId, limit } = c.req.valid("query");
const result = await container.sessionService.getFullMainChatHistory(
botId,
limit,
);
return c.json(result);
},
);
}