@witqq/agent-sdk 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{types-CqvUAYxt.d.ts → agent-C6H2CgJA.d.cts} +139 -102
- package/dist/{types-CqvUAYxt.d.cts → agent-F7oB6eKp.d.ts} +139 -102
- package/dist/auth/index.cjs +72 -1
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +21 -154
- package/dist/auth/index.d.ts +21 -154
- package/dist/auth/index.js +72 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/backends/claude.cjs +480 -261
- package/dist/backends/claude.cjs.map +1 -1
- package/dist/backends/claude.d.cts +3 -1
- package/dist/backends/claude.d.ts +3 -1
- package/dist/backends/claude.js +480 -261
- package/dist/backends/claude.js.map +1 -1
- package/dist/backends/copilot.cjs +337 -112
- package/dist/backends/copilot.cjs.map +1 -1
- package/dist/backends/copilot.d.cts +12 -4
- package/dist/backends/copilot.d.ts +12 -4
- package/dist/backends/copilot.js +337 -112
- package/dist/backends/copilot.js.map +1 -1
- package/dist/backends/mock-llm.cjs +719 -0
- package/dist/backends/mock-llm.cjs.map +1 -0
- package/dist/backends/mock-llm.d.cts +37 -0
- package/dist/backends/mock-llm.d.ts +37 -0
- package/dist/backends/mock-llm.js +717 -0
- package/dist/backends/mock-llm.js.map +1 -0
- package/dist/backends/vercel-ai.cjs +301 -61
- package/dist/backends/vercel-ai.cjs.map +1 -1
- package/dist/backends/vercel-ai.d.cts +3 -1
- package/dist/backends/vercel-ai.d.ts +3 -1
- package/dist/backends/vercel-ai.js +301 -61
- package/dist/backends/vercel-ai.js.map +1 -1
- package/dist/backends-Cno0gZjy.d.cts +114 -0
- package/dist/backends-Cno0gZjy.d.ts +114 -0
- package/dist/chat/accumulator.cjs +1 -1
- package/dist/chat/accumulator.cjs.map +1 -1
- package/dist/chat/accumulator.d.cts +5 -2
- package/dist/chat/accumulator.d.ts +5 -2
- package/dist/chat/accumulator.js +1 -1
- package/dist/chat/accumulator.js.map +1 -1
- package/dist/chat/backends.cjs +1084 -821
- package/dist/chat/backends.cjs.map +1 -1
- package/dist/chat/backends.d.cts +10 -6
- package/dist/chat/backends.d.ts +10 -6
- package/dist/chat/backends.js +1082 -800
- package/dist/chat/backends.js.map +1 -1
- package/dist/chat/context.cjs +50 -0
- package/dist/chat/context.cjs.map +1 -1
- package/dist/chat/context.d.cts +27 -3
- package/dist/chat/context.d.ts +27 -3
- package/dist/chat/context.js +50 -0
- package/dist/chat/context.js.map +1 -1
- package/dist/chat/core.cjs +60 -27
- package/dist/chat/core.cjs.map +1 -1
- package/dist/chat/core.d.cts +41 -382
- package/dist/chat/core.d.ts +41 -382
- package/dist/chat/core.js +58 -28
- package/dist/chat/core.js.map +1 -1
- package/dist/chat/errors.cjs +48 -26
- package/dist/chat/errors.cjs.map +1 -1
- package/dist/chat/errors.d.cts +6 -31
- package/dist/chat/errors.d.ts +6 -31
- package/dist/chat/errors.js +48 -25
- package/dist/chat/errors.js.map +1 -1
- package/dist/chat/events.cjs.map +1 -1
- package/dist/chat/events.d.cts +6 -2
- package/dist/chat/events.d.ts +6 -2
- package/dist/chat/events.js.map +1 -1
- package/dist/chat/index.cjs +1612 -1125
- package/dist/chat/index.cjs.map +1 -1
- package/dist/chat/index.d.cts +35 -10
- package/dist/chat/index.d.ts +35 -10
- package/dist/chat/index.js +1600 -1097
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/react/theme.css +2517 -0
- package/dist/chat/react.cjs +2212 -1158
- package/dist/chat/react.cjs.map +1 -1
- package/dist/chat/react.d.cts +665 -122
- package/dist/chat/react.d.ts +665 -122
- package/dist/chat/react.js +2191 -1156
- package/dist/chat/react.js.map +1 -1
- package/dist/chat/runtime.cjs +405 -186
- package/dist/chat/runtime.cjs.map +1 -1
- package/dist/chat/runtime.d.cts +92 -28
- package/dist/chat/runtime.d.ts +92 -28
- package/dist/chat/runtime.js +405 -186
- package/dist/chat/runtime.js.map +1 -1
- package/dist/chat/server.cjs +2247 -212
- package/dist/chat/server.cjs.map +1 -1
- package/dist/chat/server.d.cts +451 -90
- package/dist/chat/server.d.ts +451 -90
- package/dist/chat/server.js +2234 -213
- package/dist/chat/server.js.map +1 -1
- package/dist/chat/sessions.cjs +64 -66
- package/dist/chat/sessions.cjs.map +1 -1
- package/dist/chat/sessions.d.cts +37 -118
- package/dist/chat/sessions.d.ts +37 -118
- package/dist/chat/sessions.js +65 -67
- package/dist/chat/sessions.js.map +1 -1
- package/dist/chat/sqlite.cjs +536 -0
- package/dist/chat/sqlite.cjs.map +1 -0
- package/dist/chat/sqlite.d.cts +164 -0
- package/dist/chat/sqlite.d.ts +164 -0
- package/dist/chat/sqlite.js +527 -0
- package/dist/chat/sqlite.js.map +1 -0
- package/dist/chat/state.cjs +14 -1
- package/dist/chat/state.cjs.map +1 -1
- package/dist/chat/state.d.cts +5 -2
- package/dist/chat/state.d.ts +5 -2
- package/dist/chat/state.js +14 -1
- package/dist/chat/state.js.map +1 -1
- package/dist/chat/storage.cjs +58 -33
- package/dist/chat/storage.cjs.map +1 -1
- package/dist/chat/storage.d.cts +18 -8
- package/dist/chat/storage.d.ts +18 -8
- package/dist/chat/storage.js +59 -34
- package/dist/chat/storage.js.map +1 -1
- package/dist/errors-C-so0M4t.d.cts +33 -0
- package/dist/errors-C-so0M4t.d.ts +33 -0
- package/dist/errors-CmVvczxZ.d.cts +28 -0
- package/dist/errors-CmVvczxZ.d.ts +28 -0
- package/dist/{in-process-transport-C2oPTYs6.d.ts → in-process-transport-7EIit9Xk.d.ts} +72 -33
- package/dist/{in-process-transport-DG-w5G6k.d.cts → in-process-transport-Ct9YcX8I.d.cts} +72 -33
- package/dist/index.cjs +354 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +294 -123
- package/dist/index.d.ts +294 -123
- package/dist/index.js +347 -60
- package/dist/index.js.map +1 -1
- package/dist/provider-types-PTSlRPNB.d.cts +39 -0
- package/dist/provider-types-PTSlRPNB.d.ts +39 -0
- package/dist/refresh-manager-B81PpYBr.d.cts +153 -0
- package/dist/refresh-manager-Dlv_iNZi.d.ts +153 -0
- package/dist/testing.cjs +1107 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +144 -0
- package/dist/testing.d.ts +144 -0
- package/dist/testing.js +1101 -0
- package/dist/testing.js.map +1 -0
- package/dist/token-store-CSUBgYwn.d.ts +48 -0
- package/dist/token-store-CuC4hB9Z.d.cts +48 -0
- package/dist/{transport-DX1Nhm4N.d.cts → transport-DLWCN18G.d.cts} +5 -4
- package/dist/{transport-D1OaUgRk.d.ts → transport-DsuS-GeM.d.ts} +5 -4
- package/dist/{types-CGF7AEX1.d.cts → types-4vbcmPTp.d.cts} +4 -2
- package/dist/{types-Bh5AhqD-.d.ts → types-BxggH0Yh.d.ts} +4 -2
- package/dist/types-DgtI1hzh.d.ts +364 -0
- package/dist/types-DkSXALKg.d.cts +364 -0
- package/package.json +41 -5
- package/LICENSE +0 -21
- package/README.md +0 -948
- package/dist/errors-BDLbNu9w.d.cts +0 -13
- package/dist/errors-BDLbNu9w.d.ts +0 -13
- package/dist/types-DLZzlJxt.d.ts +0 -39
- package/dist/types-tE0CXwBl.d.cts +0 -39
package/dist/chat/server.js
CHANGED
|
@@ -1,8 +1,104 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
1
2
|
import * as fs from 'fs';
|
|
2
3
|
import { mkdirSync, writeFileSync, readFileSync, unlinkSync, existsSync, readdirSync } from 'fs';
|
|
3
4
|
import * as path from 'path';
|
|
4
5
|
import { join } from 'path';
|
|
5
6
|
|
|
7
|
+
// src/chat/server/utils.ts
|
|
8
|
+
var BodyParseError = class extends Error {
|
|
9
|
+
statusCode;
|
|
10
|
+
constructor(message, statusCode) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "BodyParseError";
|
|
13
|
+
this.statusCode = statusCode;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function readBody(req, maxSize = 1048576) {
|
|
17
|
+
return new Promise((resolve2, reject) => {
|
|
18
|
+
let body = "";
|
|
19
|
+
let size = 0;
|
|
20
|
+
let exceeded = false;
|
|
21
|
+
req.on("data", (chunk) => {
|
|
22
|
+
if (exceeded) return;
|
|
23
|
+
const str = chunk.toString();
|
|
24
|
+
size += Buffer.byteLength(str);
|
|
25
|
+
if (size > maxSize) {
|
|
26
|
+
exceeded = true;
|
|
27
|
+
reject(new BodyParseError("Request body too large", 413));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
body += str;
|
|
31
|
+
});
|
|
32
|
+
req.on("end", () => {
|
|
33
|
+
if (exceeded) return;
|
|
34
|
+
try {
|
|
35
|
+
resolve2(JSON.parse(body || "{}"));
|
|
36
|
+
} catch {
|
|
37
|
+
reject(new BodyParseError("Invalid JSON in request body", 400));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
if ("once" in req && typeof req.once === "function") {
|
|
41
|
+
req.once(
|
|
42
|
+
"error",
|
|
43
|
+
() => reject(new BodyParseError("Request error", 500))
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function json(res, data, status = 200) {
|
|
49
|
+
res.writeHead(status, { "Content-Type": "application/json" });
|
|
50
|
+
res.end(JSON.stringify(data));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/chat/server/routes/sessions.ts
|
|
54
|
+
var sessionRoutes = async (method, path2, req, res, ctx) => {
|
|
55
|
+
const { runtime, maxBodySize } = ctx;
|
|
56
|
+
const sessionMatch = path2.match(/^\/sessions\/([^/]+)$/);
|
|
57
|
+
const contextStatsMatch = path2.match(/^\/sessions\/([^/]+)\/context-stats$/);
|
|
58
|
+
if (method === "POST" && path2 === "/sessions/create") {
|
|
59
|
+
const body = await readBody(req, maxBodySize);
|
|
60
|
+
const session = await runtime.createSession({
|
|
61
|
+
title: body.title || `Chat ${(/* @__PURE__ */ new Date()).toLocaleTimeString()}`,
|
|
62
|
+
config: body.config || {
|
|
63
|
+
model: "",
|
|
64
|
+
backend: ""
|
|
65
|
+
},
|
|
66
|
+
...body.tags ? { tags: body.tags } : {},
|
|
67
|
+
...body.custom ? { custom: body.custom } : {}
|
|
68
|
+
});
|
|
69
|
+
json(res, session);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (method === "GET" && contextStatsMatch) {
|
|
73
|
+
const id = decodeURIComponent(contextStatsMatch[1]);
|
|
74
|
+
const stats = await runtime.getContextStats(id);
|
|
75
|
+
json(res, stats ?? null);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
if (method === "GET" && sessionMatch) {
|
|
79
|
+
const id = decodeURIComponent(sessionMatch[1]);
|
|
80
|
+
const session = await runtime.getSession(id);
|
|
81
|
+
if (!session) {
|
|
82
|
+
json(res, { error: "Not found" }, 404);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
json(res, session);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (method === "DELETE" && sessionMatch) {
|
|
89
|
+
const id = decodeURIComponent(sessionMatch[1]);
|
|
90
|
+
await runtime.deleteSession(id);
|
|
91
|
+
json(res, { ok: true });
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
if (method === "GET" && path2 === "/sessions") {
|
|
95
|
+
const sessions = await runtime.listSessions();
|
|
96
|
+
json(res, sessions);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
};
|
|
101
|
+
|
|
6
102
|
// src/chat/backends/transport.ts
|
|
7
103
|
var SSEChatTransport = class {
|
|
8
104
|
res;
|
|
@@ -78,16 +174,22 @@ var SSEChatTransport = class {
|
|
|
78
174
|
};
|
|
79
175
|
async function streamToTransport(events, transport) {
|
|
80
176
|
try {
|
|
81
|
-
|
|
177
|
+
const textChunks = [];
|
|
178
|
+
let finishReason;
|
|
82
179
|
for await (const event of events) {
|
|
83
180
|
if (!transport.isOpen) break;
|
|
181
|
+
if (event.type === "done") {
|
|
182
|
+
finishReason = event.finishReason;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
84
185
|
transport.send(event);
|
|
85
186
|
if (event.type === "message:delta") {
|
|
86
|
-
|
|
187
|
+
textChunks.push(event.text);
|
|
87
188
|
}
|
|
88
189
|
}
|
|
89
190
|
if (transport.isOpen) {
|
|
90
|
-
|
|
191
|
+
const finalOutput = textChunks.length > 0 ? textChunks.join("") : void 0;
|
|
192
|
+
transport.send({ type: "done", finalOutput, finishReason });
|
|
91
193
|
}
|
|
92
194
|
transport.close();
|
|
93
195
|
} catch (err) {
|
|
@@ -95,115 +197,332 @@ async function streamToTransport(events, transport) {
|
|
|
95
197
|
}
|
|
96
198
|
}
|
|
97
199
|
|
|
98
|
-
// src/
|
|
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
|
-
|
|
200
|
+
// src/errors.ts
|
|
201
|
+
var AgentSDKError = class extends Error {
|
|
202
|
+
/** @internal Marker for cross-bundle identity checks */
|
|
203
|
+
_agentSDKError = true;
|
|
204
|
+
/** Machine-readable error code. Prefer values from the ErrorCode enum. */
|
|
205
|
+
code;
|
|
206
|
+
/** Whether this error is safe to retry */
|
|
207
|
+
retryable;
|
|
208
|
+
/** HTTP status code hint for error classification */
|
|
209
|
+
httpStatus;
|
|
210
|
+
constructor(message, options) {
|
|
211
|
+
super(message, options);
|
|
212
|
+
this.name = "AgentSDKError";
|
|
213
|
+
this.code = options?.code;
|
|
214
|
+
this.retryable = options?.retryable ?? false;
|
|
215
|
+
this.httpStatus = options?.httpStatus;
|
|
216
|
+
}
|
|
217
|
+
/** Check if an error is an AgentSDKError (works across bundled copies) */
|
|
218
|
+
static is(error) {
|
|
219
|
+
return error instanceof Error && "_agentSDKError" in error && error._agentSDKError === true;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// src/chat/errors.ts
|
|
224
|
+
var ChatError = class extends AgentSDKError {
|
|
225
|
+
code;
|
|
226
|
+
retryable;
|
|
227
|
+
retryAfter;
|
|
228
|
+
timestamp;
|
|
229
|
+
constructor(message, options) {
|
|
230
|
+
super(message, {
|
|
231
|
+
cause: options.cause,
|
|
232
|
+
code: options.code,
|
|
233
|
+
retryable: options.retryable
|
|
234
|
+
});
|
|
235
|
+
this.name = "ChatError";
|
|
236
|
+
this.code = options.code;
|
|
237
|
+
this.retryable = options.retryable ?? false;
|
|
238
|
+
this.retryAfter = options.retryAfter;
|
|
239
|
+
this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// src/chat/server/request-context.ts
|
|
244
|
+
async function resolveRequestContext(providerId, deps) {
|
|
245
|
+
const provider = await deps.providerStore.get(providerId);
|
|
246
|
+
if (!provider) {
|
|
247
|
+
throw new ChatError(`Provider "${providerId}" not found`, {
|
|
248
|
+
code: "PROVIDER_NOT_FOUND" /* PROVIDER_NOT_FOUND */
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
const credentials = await deps.tokenStore.load(provider.backend);
|
|
252
|
+
if (!credentials) {
|
|
253
|
+
throw new ChatError(
|
|
254
|
+
`Authentication required for backend "${provider.backend}"`,
|
|
255
|
+
{
|
|
256
|
+
code: "AUTH_REQUIRED" /* AUTH_REQUIRED */
|
|
124
257
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return {
|
|
261
|
+
backend: provider.backend,
|
|
262
|
+
credentials,
|
|
263
|
+
model: provider.model,
|
|
264
|
+
provider
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/chat/server/routes/messages.ts
|
|
269
|
+
var messageRoutes = async (method, path2, req, res, ctx) => {
|
|
270
|
+
const { runtime, maxBodySize, heartbeatMs, hooks, transportFactory } = ctx;
|
|
271
|
+
if (method === "POST" && path2 === "/send") {
|
|
272
|
+
const body = await readBody(req, maxBodySize);
|
|
273
|
+
const sessionId = body.sessionId;
|
|
274
|
+
const message = body.message || body.content;
|
|
275
|
+
if (!sessionId || !message) {
|
|
276
|
+
json(res, { error: "sessionId and message are required" }, 400);
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
let model;
|
|
280
|
+
let reqBackend;
|
|
281
|
+
let reqCredentials;
|
|
282
|
+
const hasProviderInfra = !!(ctx.providerStore && ctx.tokenStore);
|
|
283
|
+
if (hasProviderInfra) {
|
|
284
|
+
const providerId = body.providerId;
|
|
285
|
+
if (!providerId || typeof providerId !== "string") {
|
|
286
|
+
json(res, { error: "providerId is required" }, 400);
|
|
287
|
+
return true;
|
|
130
288
|
}
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
289
|
+
try {
|
|
290
|
+
const reqCtx = await resolveRequestContext(providerId, {
|
|
291
|
+
providerStore: ctx.providerStore,
|
|
292
|
+
tokenStore: ctx.tokenStore
|
|
293
|
+
});
|
|
294
|
+
model = reqCtx.model;
|
|
295
|
+
reqBackend = reqCtx.backend;
|
|
296
|
+
reqCredentials = reqCtx.credentials;
|
|
297
|
+
} catch (err) {
|
|
298
|
+
if (err instanceof ChatError && err.code === "PROVIDER_NOT_FOUND" /* PROVIDER_NOT_FOUND */) {
|
|
299
|
+
json(res, { error: err.message }, 404);
|
|
300
|
+
return true;
|
|
137
301
|
}
|
|
138
|
-
|
|
139
|
-
|
|
302
|
+
if (err instanceof ChatError && err.code === "AUTH_REQUIRED" /* AUTH_REQUIRED */) {
|
|
303
|
+
json(res, { error: err.message }, 401);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
throw err;
|
|
140
307
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
308
|
+
}
|
|
309
|
+
const bodyModel = body.model;
|
|
310
|
+
if (hooks?.onModelSwitch && bodyModel && typeof bodyModel === "string") {
|
|
311
|
+
try {
|
|
312
|
+
await hooks.onModelSwitch(bodyModel);
|
|
313
|
+
} catch (err) {
|
|
314
|
+
json(res, { error: err instanceof Error ? err.message : String(err) }, 403);
|
|
315
|
+
return true;
|
|
146
316
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
317
|
+
}
|
|
318
|
+
if (hooks?.onBeforeSend) {
|
|
319
|
+
try {
|
|
320
|
+
await hooks.onBeforeSend(sessionId, message);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
json(res, { error: err instanceof Error ? err.message : String(err) }, 403);
|
|
323
|
+
return true;
|
|
151
324
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (body.model) opts.model = body.model;
|
|
167
|
-
const stream = runtime.send(
|
|
168
|
-
sessionId,
|
|
169
|
-
message,
|
|
170
|
-
Object.keys(opts).length > 0 ? opts : void 0
|
|
171
|
-
);
|
|
172
|
-
await streamToTransport(stream, transport);
|
|
173
|
-
} catch (err) {
|
|
174
|
-
transport.error(err instanceof Error ? err : new Error(String(err)));
|
|
175
|
-
}
|
|
176
|
-
return;
|
|
325
|
+
}
|
|
326
|
+
model = bodyModel || model;
|
|
327
|
+
if (!model) {
|
|
328
|
+
json(res, { error: "model is required (via body.model or providerId)" }, 400);
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
const transport = transportFactory ? transportFactory(req, res) : new SSEChatTransport(res, {
|
|
332
|
+
heartbeatMs,
|
|
333
|
+
request: req
|
|
334
|
+
});
|
|
335
|
+
try {
|
|
336
|
+
if (!reqBackend || !reqCredentials) {
|
|
337
|
+
json(res, { error: "backend and credentials are required (configure providerStore + tokenStore)" }, 400);
|
|
338
|
+
return true;
|
|
177
339
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
340
|
+
const opts = { model, backend: reqBackend, credentials: reqCredentials };
|
|
341
|
+
const stream = runtime.send(sessionId, message, opts);
|
|
342
|
+
await streamToTransport(stream, transport);
|
|
343
|
+
} catch (err) {
|
|
344
|
+
transport.error(err instanceof Error ? err : new Error(String(err)));
|
|
345
|
+
}
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
if (method === "POST" && path2 === "/abort") {
|
|
349
|
+
runtime.abort();
|
|
350
|
+
json(res, { ok: true });
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
return false;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
// src/chat/server/routes/config.ts
|
|
357
|
+
var configRoutes = async (method, path2, req, res, ctx) => {
|
|
358
|
+
const { runtime, maxBodySize, hooks, providerStore } = ctx;
|
|
359
|
+
if (method === "GET" && path2 === "/models") {
|
|
360
|
+
let models = await runtime.listModels();
|
|
361
|
+
if (models.length === 0 && providerStore && ctx.tokenStore) {
|
|
362
|
+
const providers = await providerStore.list();
|
|
363
|
+
for (const p of providers) {
|
|
364
|
+
const token = await ctx.tokenStore.load(p.backend);
|
|
365
|
+
if (token) {
|
|
366
|
+
models = await runtime.listModels({ backend: p.backend, credentials: token });
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
182
369
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
370
|
+
}
|
|
371
|
+
if (hooks?.filterModels) models = hooks.filterModels(models);
|
|
372
|
+
json(res, models);
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
if (method === "GET" && path2 === "/backends") {
|
|
376
|
+
const backends = await runtime.listBackends();
|
|
377
|
+
json(res, backends);
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
if (method === "POST" && path2 === "/model/switch") {
|
|
381
|
+
const body = await readBody(req, maxBodySize);
|
|
382
|
+
if (!body.model || typeof body.model !== "string") {
|
|
383
|
+
json(res, { error: "model is required" }, 400);
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
if (hooks?.onModelSwitch) {
|
|
387
|
+
try {
|
|
388
|
+
await hooks.onModelSwitch(body.model);
|
|
389
|
+
} catch (err) {
|
|
390
|
+
json(res, { error: err instanceof Error ? err.message : String(err) }, 403);
|
|
391
|
+
return true;
|
|
187
392
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
393
|
+
}
|
|
394
|
+
json(res, { ok: true });
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
if (method === "POST" && path2 === "/provider/switch") {
|
|
398
|
+
const body = await readBody(req, maxBodySize);
|
|
399
|
+
if (!body.providerId || typeof body.providerId !== "string") {
|
|
400
|
+
json(res, { error: "providerId is required" }, 400);
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
if (!providerStore) {
|
|
404
|
+
json(res, { error: "No provider store configured" }, 400);
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
const provider = await providerStore.get(body.providerId);
|
|
408
|
+
if (!provider) {
|
|
409
|
+
json(res, { error: `Provider "${body.providerId}" not found` }, 404);
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
if (hooks?.onProviderSwitch) {
|
|
413
|
+
try {
|
|
414
|
+
await hooks.onProviderSwitch({ providerId: body.providerId, backend: provider.backend });
|
|
415
|
+
} catch (err) {
|
|
416
|
+
json(res, { error: err instanceof Error ? err.message : String(err) }, 400);
|
|
417
|
+
return true;
|
|
197
418
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
419
|
+
}
|
|
420
|
+
json(res, { ok: true });
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
return false;
|
|
424
|
+
};
|
|
425
|
+
var providerRoutes = async (method, path2, req, res, ctx) => {
|
|
426
|
+
const { providerStore, maxBodySize } = ctx;
|
|
427
|
+
if (!providerStore) return false;
|
|
428
|
+
const idMatch = path2.match(/^\/providers\/([^/]+)$/);
|
|
429
|
+
if (method === "GET" && path2 === "/providers") {
|
|
430
|
+
const providers = await providerStore.list();
|
|
431
|
+
json(res, providers);
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
if (method === "GET" && idMatch) {
|
|
435
|
+
const id = decodeURIComponent(idMatch[1]);
|
|
436
|
+
const provider = await providerStore.get(id);
|
|
437
|
+
if (!provider) {
|
|
438
|
+
json(res, { error: "Provider not found" }, 404);
|
|
439
|
+
return true;
|
|
440
|
+
}
|
|
441
|
+
json(res, provider);
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
if (method === "POST" && path2 === "/providers") {
|
|
445
|
+
const body = await readBody(req, maxBodySize);
|
|
446
|
+
const backend = body.backend;
|
|
447
|
+
const model = body.model;
|
|
448
|
+
const label = body.label;
|
|
449
|
+
if (!backend || typeof backend !== "string") {
|
|
450
|
+
json(res, { error: "backend is required" }, 400);
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
if (!model || typeof model !== "string") {
|
|
454
|
+
json(res, { error: "model is required" }, 400);
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
if (!label || typeof label !== "string") {
|
|
458
|
+
json(res, { error: "label is required" }, 400);
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
const config = {
|
|
462
|
+
id: randomUUID(),
|
|
463
|
+
backend,
|
|
464
|
+
model,
|
|
465
|
+
label,
|
|
466
|
+
createdAt: Date.now()
|
|
467
|
+
};
|
|
468
|
+
await providerStore.create(config);
|
|
469
|
+
json(res, config, 201);
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
if (method === "PUT" && idMatch) {
|
|
473
|
+
const id = decodeURIComponent(idMatch[1]);
|
|
474
|
+
const existing = await providerStore.get(id);
|
|
475
|
+
if (!existing) {
|
|
476
|
+
json(res, { error: "Provider not found" }, 404);
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
const body = await readBody(req, maxBodySize);
|
|
480
|
+
const changes = {};
|
|
481
|
+
if (body.backend && typeof body.backend === "string") changes.backend = body.backend;
|
|
482
|
+
if (body.model && typeof body.model === "string") changes.model = body.model;
|
|
483
|
+
if (body.label && typeof body.label === "string") changes.label = body.label;
|
|
484
|
+
await providerStore.update(id, changes);
|
|
485
|
+
const updated = await providerStore.get(id);
|
|
486
|
+
json(res, updated);
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
if (method === "DELETE" && idMatch) {
|
|
490
|
+
const id = decodeURIComponent(idMatch[1]);
|
|
491
|
+
await providerStore.delete(id);
|
|
492
|
+
json(res, { ok: true });
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
return false;
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
// src/chat/server/handler.ts
|
|
499
|
+
var ROUTE_PIPELINE = [
|
|
500
|
+
sessionRoutes,
|
|
501
|
+
messageRoutes,
|
|
502
|
+
configRoutes,
|
|
503
|
+
providerRoutes
|
|
504
|
+
];
|
|
505
|
+
function createChatHandler(runtime, options) {
|
|
506
|
+
const prefix = options?.prefix ?? "";
|
|
507
|
+
const state = {};
|
|
508
|
+
const ctx = {
|
|
509
|
+
runtime,
|
|
510
|
+
maxBodySize: options?.maxBodySize ?? 1048576,
|
|
511
|
+
heartbeatMs: options?.heartbeatMs,
|
|
512
|
+
hooks: options?.hooks,
|
|
513
|
+
providerStore: options?.providerStore,
|
|
514
|
+
tokenStore: options?.tokenStore,
|
|
515
|
+
transportFactory: options?.transportFactory,
|
|
516
|
+
state
|
|
517
|
+
};
|
|
518
|
+
return async (req, res) => {
|
|
519
|
+
const url = req.url || "";
|
|
520
|
+
const method = req.method || "GET";
|
|
521
|
+
const rawPath = prefix ? url.slice(prefix.length) : url;
|
|
522
|
+
const path2 = rawPath.split("?")[0];
|
|
523
|
+
try {
|
|
524
|
+
for (const route of ROUTE_PIPELINE) {
|
|
525
|
+
if (await route(method, path2, req, res, ctx)) return;
|
|
207
526
|
}
|
|
208
527
|
json(res, { error: "Not found" }, 404);
|
|
209
528
|
} catch (err) {
|
|
@@ -211,55 +530,14 @@ function createChatHandler(runtime, options) {
|
|
|
211
530
|
json(res, { error: err.message }, err.statusCode);
|
|
212
531
|
} else {
|
|
213
532
|
const message = err instanceof Error ? err.message : String(err);
|
|
533
|
+
if (ctx.hooks?.onError) {
|
|
534
|
+
ctx.hooks.onError(err instanceof Error ? err : new Error(message), { route: path2, method });
|
|
535
|
+
}
|
|
214
536
|
json(res, { error: message }, 500);
|
|
215
537
|
}
|
|
216
538
|
}
|
|
217
539
|
};
|
|
218
540
|
}
|
|
219
|
-
var BodyParseError = class extends Error {
|
|
220
|
-
statusCode;
|
|
221
|
-
constructor(message, statusCode) {
|
|
222
|
-
super(message);
|
|
223
|
-
this.name = "BodyParseError";
|
|
224
|
-
this.statusCode = statusCode;
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
function readBody(req, maxSize) {
|
|
228
|
-
return new Promise((resolve2, reject) => {
|
|
229
|
-
let body = "";
|
|
230
|
-
let size = 0;
|
|
231
|
-
let exceeded = false;
|
|
232
|
-
req.on("data", (chunk) => {
|
|
233
|
-
if (exceeded) return;
|
|
234
|
-
const str = chunk.toString();
|
|
235
|
-
size += Buffer.byteLength(str);
|
|
236
|
-
if (size > maxSize) {
|
|
237
|
-
exceeded = true;
|
|
238
|
-
reject(new BodyParseError("Request body too large", 413));
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
body += str;
|
|
242
|
-
});
|
|
243
|
-
req.on("end", () => {
|
|
244
|
-
if (exceeded) return;
|
|
245
|
-
try {
|
|
246
|
-
resolve2(JSON.parse(body || "{}"));
|
|
247
|
-
} catch {
|
|
248
|
-
reject(new BodyParseError("Invalid JSON in request body", 400));
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
if ("once" in req && typeof req.once === "function") {
|
|
252
|
-
req.once(
|
|
253
|
-
"error",
|
|
254
|
-
() => reject(new BodyParseError("Request error", 500))
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
function json(res, data, status = 200) {
|
|
260
|
-
res.writeHead(status, { "Content-Type": "application/json" });
|
|
261
|
-
res.end(JSON.stringify(data));
|
|
262
|
-
}
|
|
263
541
|
|
|
264
542
|
// src/chat/server/auth-handler.ts
|
|
265
543
|
function createAuthHandler(options) {
|
|
@@ -275,74 +553,74 @@ function createAuthHandler(options) {
|
|
|
275
553
|
const path2 = rawPath.split("?")[0];
|
|
276
554
|
try {
|
|
277
555
|
if (method === "POST" && path2 === "/auth/start") {
|
|
278
|
-
const body = await
|
|
556
|
+
const body = await readBody(req, maxBodySize);
|
|
279
557
|
const provider = body.provider;
|
|
280
558
|
if (!provider || !isValidProvider(provider)) {
|
|
281
|
-
|
|
559
|
+
json(res, { error: "provider is required (copilot, claude, vercel-ai)" }, 400);
|
|
282
560
|
return;
|
|
283
561
|
}
|
|
284
562
|
pendingCopilot = null;
|
|
285
563
|
pendingClaude = null;
|
|
286
564
|
if (provider === "copilot") {
|
|
287
565
|
if (!options.createCopilotAuth) {
|
|
288
|
-
|
|
566
|
+
json(res, { error: "Copilot auth not configured" }, 400);
|
|
289
567
|
return;
|
|
290
568
|
}
|
|
291
569
|
const auth = options.createCopilotAuth();
|
|
292
570
|
const flow = await auth.startDeviceFlow();
|
|
293
571
|
pendingCopilot = { waitForToken: flow.waitForToken };
|
|
294
|
-
|
|
572
|
+
json(res, { userCode: flow.userCode, verificationUrl: flow.verificationUrl });
|
|
295
573
|
return;
|
|
296
574
|
}
|
|
297
575
|
if (provider === "claude") {
|
|
298
576
|
if (!options.createClaudeAuth) {
|
|
299
|
-
|
|
577
|
+
json(res, { error: "Claude auth not configured" }, 400);
|
|
300
578
|
return;
|
|
301
579
|
}
|
|
302
580
|
const auth = options.createClaudeAuth();
|
|
303
581
|
const flow = auth.startOAuthFlow();
|
|
304
582
|
pendingClaude = { completeAuth: flow.completeAuth };
|
|
305
|
-
|
|
583
|
+
json(res, { authorizeUrl: flow.authorizeUrl });
|
|
306
584
|
return;
|
|
307
585
|
}
|
|
308
|
-
|
|
586
|
+
json(res, { ready: true });
|
|
309
587
|
return;
|
|
310
588
|
}
|
|
311
589
|
if (method === "POST" && path2 === "/auth/copilot/poll") {
|
|
312
590
|
if (!pendingCopilot) {
|
|
313
|
-
|
|
591
|
+
json(res, { error: "No active Copilot flow" }, 400);
|
|
314
592
|
return;
|
|
315
593
|
}
|
|
316
594
|
const token = await pendingCopilot.waitForToken();
|
|
317
595
|
pendingCopilot = null;
|
|
318
596
|
await tokenStore.save("copilot", token);
|
|
319
597
|
if (onAuth) await onAuth("copilot", token);
|
|
320
|
-
|
|
598
|
+
json(res, { ok: true, login: token.login });
|
|
321
599
|
return;
|
|
322
600
|
}
|
|
323
601
|
if (method === "POST" && path2 === "/auth/claude/complete") {
|
|
324
602
|
if (!pendingClaude) {
|
|
325
|
-
|
|
603
|
+
json(res, { error: "No active Claude flow" }, 400);
|
|
326
604
|
return;
|
|
327
605
|
}
|
|
328
|
-
const body = await
|
|
606
|
+
const body = await readBody(req, maxBodySize);
|
|
329
607
|
const code = body.code;
|
|
330
608
|
if (!code || typeof code !== "string") {
|
|
331
|
-
|
|
609
|
+
json(res, { error: "code is required" }, 400);
|
|
332
610
|
return;
|
|
333
611
|
}
|
|
334
612
|
const token = await pendingClaude.completeAuth(code);
|
|
335
613
|
pendingClaude = null;
|
|
336
614
|
await tokenStore.save("claude", token);
|
|
337
615
|
if (onAuth) await onAuth("claude", token);
|
|
338
|
-
|
|
616
|
+
json(res, { ok: true });
|
|
339
617
|
return;
|
|
340
618
|
}
|
|
341
619
|
if (method === "POST" && path2 === "/auth/vercel/complete") {
|
|
342
|
-
const body = await
|
|
620
|
+
const body = await readBody(req, maxBodySize);
|
|
343
621
|
const apiKey = body.apiKey;
|
|
344
622
|
if (!apiKey || typeof apiKey !== "string") {
|
|
345
|
-
|
|
623
|
+
json(res, { error: "apiKey is required" }, 400);
|
|
346
624
|
return;
|
|
347
625
|
}
|
|
348
626
|
const token = {
|
|
@@ -353,86 +631,57 @@ function createAuthHandler(options) {
|
|
|
353
631
|
const storeToken = body.baseUrl ? { ...token, baseUrl: body.baseUrl } : token;
|
|
354
632
|
await tokenStore.save("vercel-ai", storeToken);
|
|
355
633
|
if (onAuth) await onAuth("vercel-ai", storeToken);
|
|
356
|
-
|
|
634
|
+
json(res, { ok: true });
|
|
357
635
|
return;
|
|
358
636
|
}
|
|
359
637
|
if (method === "GET" && path2 === "/tokens/saved") {
|
|
360
638
|
const saved = await tokenStore.list();
|
|
361
|
-
|
|
639
|
+
json(res, { saved });
|
|
362
640
|
return;
|
|
363
641
|
}
|
|
364
642
|
if (method === "POST" && path2 === "/tokens/use") {
|
|
365
|
-
const body = await
|
|
643
|
+
const body = await readBody(req, maxBodySize);
|
|
366
644
|
const provider = body.provider;
|
|
367
645
|
if (!provider || !isValidProvider(provider)) {
|
|
368
|
-
|
|
646
|
+
json(res, { error: "provider is required (copilot, claude, vercel-ai)" }, 400);
|
|
369
647
|
return;
|
|
370
648
|
}
|
|
371
649
|
const token = await tokenStore.load(provider);
|
|
372
650
|
if (!token) {
|
|
373
|
-
|
|
651
|
+
json(res, { error: `No saved token for ${provider}` }, 404);
|
|
374
652
|
return;
|
|
375
653
|
}
|
|
376
654
|
if (onAuth) await onAuth(provider, token);
|
|
377
|
-
|
|
655
|
+
json(res, { ok: true, provider });
|
|
378
656
|
return;
|
|
379
657
|
}
|
|
380
658
|
if (method === "POST" && path2 === "/tokens/clear") {
|
|
381
659
|
await tokenStore.clearAll();
|
|
382
660
|
if (options.onLogout) await options.onLogout();
|
|
383
|
-
|
|
661
|
+
json(res, { ok: true });
|
|
384
662
|
return;
|
|
385
663
|
}
|
|
386
664
|
if (method === "POST" && path2 === "/auth/dispose") {
|
|
387
665
|
pendingCopilot = null;
|
|
388
666
|
pendingClaude = null;
|
|
389
667
|
if (options.onLogout) await options.onLogout();
|
|
390
|
-
|
|
668
|
+
json(res, { ok: true });
|
|
391
669
|
return;
|
|
392
670
|
}
|
|
393
|
-
|
|
671
|
+
json(res, { error: "Not found" }, 404);
|
|
394
672
|
} catch (err) {
|
|
395
|
-
|
|
396
|
-
|
|
673
|
+
if (err instanceof BodyParseError) {
|
|
674
|
+
json(res, { error: err.message }, err.statusCode);
|
|
675
|
+
} else {
|
|
676
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
677
|
+
json(res, { error: message }, 500);
|
|
678
|
+
}
|
|
397
679
|
}
|
|
398
680
|
};
|
|
399
681
|
}
|
|
400
682
|
function isValidProvider(p) {
|
|
401
683
|
return p === "copilot" || p === "claude" || p === "vercel-ai";
|
|
402
684
|
}
|
|
403
|
-
function readBody2(req, maxSize) {
|
|
404
|
-
return new Promise((resolve2) => {
|
|
405
|
-
let body = "";
|
|
406
|
-
let size = 0;
|
|
407
|
-
let exceeded = false;
|
|
408
|
-
req.on("data", (chunk) => {
|
|
409
|
-
if (exceeded) return;
|
|
410
|
-
const str = chunk.toString();
|
|
411
|
-
size += Buffer.byteLength(str);
|
|
412
|
-
if (size > maxSize) {
|
|
413
|
-
exceeded = true;
|
|
414
|
-
resolve2({});
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
body += str;
|
|
418
|
-
});
|
|
419
|
-
req.on("end", () => {
|
|
420
|
-
if (exceeded) return;
|
|
421
|
-
try {
|
|
422
|
-
resolve2(JSON.parse(body || "{}"));
|
|
423
|
-
} catch {
|
|
424
|
-
resolve2({});
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
if ("once" in req && typeof req.once === "function") {
|
|
428
|
-
req.once("error", () => resolve2({}));
|
|
429
|
-
}
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
function json2(res, data, status = 200) {
|
|
433
|
-
res.writeHead(status, { "Content-Type": "application/json" });
|
|
434
|
-
res.end(JSON.stringify(data));
|
|
435
|
-
}
|
|
436
685
|
|
|
437
686
|
// src/chat/server/cors.ts
|
|
438
687
|
function corsMiddleware(options) {
|
|
@@ -463,6 +712,1212 @@ function corsMiddleware(options) {
|
|
|
463
712
|
return false;
|
|
464
713
|
};
|
|
465
714
|
}
|
|
715
|
+
|
|
716
|
+
// src/chat/types.ts
|
|
717
|
+
function createChatId() {
|
|
718
|
+
return crypto.randomUUID();
|
|
719
|
+
}
|
|
720
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
721
|
+
function toChatId(value) {
|
|
722
|
+
if (!UUID_RE.test(value)) {
|
|
723
|
+
throw new TypeError(`Invalid ChatId: "${value}" is not a valid UUID`);
|
|
724
|
+
}
|
|
725
|
+
return value;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// src/chat/bridge.ts
|
|
729
|
+
function chatEventToAgentEvent(event) {
|
|
730
|
+
switch (event.type) {
|
|
731
|
+
case "message:delta":
|
|
732
|
+
return { type: "text_delta", text: event.text };
|
|
733
|
+
case "thinking:start":
|
|
734
|
+
return { type: "thinking_start" };
|
|
735
|
+
case "thinking:delta":
|
|
736
|
+
return { type: "thinking_delta", text: event.text };
|
|
737
|
+
case "thinking:end":
|
|
738
|
+
return { type: "thinking_end" };
|
|
739
|
+
case "tool:start":
|
|
740
|
+
return {
|
|
741
|
+
type: "tool_call_start",
|
|
742
|
+
toolCallId: event.toolCallId,
|
|
743
|
+
toolName: event.toolName,
|
|
744
|
+
args: event.args
|
|
745
|
+
};
|
|
746
|
+
case "tool:complete":
|
|
747
|
+
return {
|
|
748
|
+
type: "tool_call_end",
|
|
749
|
+
toolCallId: event.toolCallId,
|
|
750
|
+
toolName: event.toolName,
|
|
751
|
+
result: event.result
|
|
752
|
+
};
|
|
753
|
+
case "error":
|
|
754
|
+
return { type: "error", error: event.error, recoverable: event.recoverable, code: event.code };
|
|
755
|
+
default:
|
|
756
|
+
return null;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// src/chat/context.ts
|
|
761
|
+
function estimateTokens(message, options) {
|
|
762
|
+
const ratio = options?.charsPerToken ?? 4;
|
|
763
|
+
let charCount = 0;
|
|
764
|
+
charCount += message.role.length + 4;
|
|
765
|
+
for (const part of message.parts) {
|
|
766
|
+
charCount += estimatePartChars(part);
|
|
767
|
+
}
|
|
768
|
+
return Math.ceil(charCount / ratio);
|
|
769
|
+
}
|
|
770
|
+
function estimatePartChars(part) {
|
|
771
|
+
switch (part.type) {
|
|
772
|
+
case "text":
|
|
773
|
+
return part.text.length;
|
|
774
|
+
case "reasoning":
|
|
775
|
+
return part.text.length;
|
|
776
|
+
case "tool_call":
|
|
777
|
+
return JSON.stringify(part.args).length + part.name.length + 20 + (part.result !== void 0 ? JSON.stringify(part.result).length : 0);
|
|
778
|
+
case "source":
|
|
779
|
+
return (part.title?.length ?? 0) + part.url.length + 10;
|
|
780
|
+
case "file":
|
|
781
|
+
return part.name.length + part.data.length + 20;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
var ContextWindowManager = class {
|
|
785
|
+
config;
|
|
786
|
+
constructor(config) {
|
|
787
|
+
this.config = {
|
|
788
|
+
maxTokens: config.maxTokens,
|
|
789
|
+
reservedTokens: config.reservedTokens ?? 0,
|
|
790
|
+
strategy: config.strategy ?? "truncate-oldest",
|
|
791
|
+
estimation: config.estimation,
|
|
792
|
+
summarizer: config.summarizer
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
/** Available token budget after reserving tokens */
|
|
796
|
+
get availableBudget() {
|
|
797
|
+
return Math.max(0, this.config.maxTokens - this.config.reservedTokens);
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Estimate tokens for a single message.
|
|
801
|
+
* @param message - Message to estimate
|
|
802
|
+
* @returns Estimated token count
|
|
803
|
+
*/
|
|
804
|
+
estimateMessageTokens(message) {
|
|
805
|
+
return estimateTokens(message, this.config.estimation);
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Fit messages within the token budget using the configured strategy.
|
|
809
|
+
* @param messages - All messages to consider
|
|
810
|
+
* @returns Result with fitted messages and metadata
|
|
811
|
+
*/
|
|
812
|
+
fitMessages(messages) {
|
|
813
|
+
if (messages.length === 0) {
|
|
814
|
+
return { messages: [], totalTokens: 0, removedCount: 0, wasTruncated: false };
|
|
815
|
+
}
|
|
816
|
+
const budget = this.availableBudget;
|
|
817
|
+
const tokenCounts = messages.map((m) => this.estimateMessageTokens(m));
|
|
818
|
+
const totalTokens = tokenCounts.reduce((a, b) => a + b, 0);
|
|
819
|
+
if (totalTokens <= budget) {
|
|
820
|
+
return {
|
|
821
|
+
messages: [...messages],
|
|
822
|
+
totalTokens,
|
|
823
|
+
removedCount: 0,
|
|
824
|
+
wasTruncated: false
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
switch (this.config.strategy) {
|
|
828
|
+
case "truncate-oldest":
|
|
829
|
+
return this.truncateOldest(messages, tokenCounts, budget);
|
|
830
|
+
case "sliding-window":
|
|
831
|
+
return this.slidingWindow(messages, tokenCounts, budget);
|
|
832
|
+
case "summarize-placeholder":
|
|
833
|
+
return this.summarizePlaceholder(messages, tokenCounts, budget);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Async variant of fitMessages that supports async summarization.
|
|
838
|
+
* When strategy is "summarize-placeholder" and a summarizer is configured,
|
|
839
|
+
* calls the summarizer with removed messages and replaces the placeholder text.
|
|
840
|
+
* Falls back to static placeholder if summarizer throws.
|
|
841
|
+
* For other strategies, behaves identically to fitMessages().
|
|
842
|
+
*/
|
|
843
|
+
async fitMessagesAsync(messages) {
|
|
844
|
+
const result = this.fitMessages(messages);
|
|
845
|
+
if (this.config.strategy !== "summarize-placeholder" || !result.wasTruncated || !this.config.summarizer) {
|
|
846
|
+
return result;
|
|
847
|
+
}
|
|
848
|
+
const keptIds = new Set(result.messages.map((m) => m.id));
|
|
849
|
+
const removed = messages.filter((m) => !keptIds.has(m.id));
|
|
850
|
+
if (removed.length === 0) return result;
|
|
851
|
+
let summaryText;
|
|
852
|
+
try {
|
|
853
|
+
summaryText = await this.config.summarizer(removed);
|
|
854
|
+
} catch {
|
|
855
|
+
return result;
|
|
856
|
+
}
|
|
857
|
+
const updatedMessages = result.messages.map((m) => {
|
|
858
|
+
if (m.metadata?.isSummary === true) {
|
|
859
|
+
return {
|
|
860
|
+
...m,
|
|
861
|
+
parts: [{ type: "text", text: summaryText, status: "complete" }]
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
return m;
|
|
865
|
+
});
|
|
866
|
+
return { ...result, messages: updatedMessages };
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Trim messages using real token usage data from the previous API call.
|
|
870
|
+
* Uses average-based algorithm: `avgTokensPerMessage = lastPromptTokens / messageCount`.
|
|
871
|
+
* Removes oldest non-system messages until freed budget brings usage under modelContextWindow.
|
|
872
|
+
*
|
|
873
|
+
* @param messages - All messages in the session
|
|
874
|
+
* @param lastPromptTokens - Real prompt tokens from the last API response
|
|
875
|
+
* @param modelContextWindow - Model's total context window size in tokens
|
|
876
|
+
* @returns Result with fitted messages and metadata
|
|
877
|
+
*/
|
|
878
|
+
fitMessagesWithUsage(messages, lastPromptTokens, modelContextWindow) {
|
|
879
|
+
if (messages.length === 0) {
|
|
880
|
+
return { messages: [], totalTokens: 0, removedCount: 0, wasTruncated: false };
|
|
881
|
+
}
|
|
882
|
+
const budget = modelContextWindow - this.config.reservedTokens;
|
|
883
|
+
if (budget <= 0 || lastPromptTokens <= budget) {
|
|
884
|
+
return {
|
|
885
|
+
messages: [...messages],
|
|
886
|
+
totalTokens: lastPromptTokens,
|
|
887
|
+
removedCount: 0,
|
|
888
|
+
wasTruncated: false
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
const avgTokensPerMessage = lastPromptTokens / messages.length;
|
|
892
|
+
const tokensToFree = lastPromptTokens - budget;
|
|
893
|
+
const messagesToRemove = Math.ceil(tokensToFree / avgTokensPerMessage);
|
|
894
|
+
const nonSystemIndices = [];
|
|
895
|
+
for (let i = 0; i < messages.length; i++) {
|
|
896
|
+
if (messages[i].role === "system") ; else {
|
|
897
|
+
nonSystemIndices.push(i);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
const removableCount = Math.min(messagesToRemove, nonSystemIndices.length);
|
|
901
|
+
const removedIndices = new Set(nonSystemIndices.slice(0, removableCount));
|
|
902
|
+
const result = [];
|
|
903
|
+
for (let i = 0; i < messages.length; i++) {
|
|
904
|
+
if (!removedIndices.has(i)) {
|
|
905
|
+
result.push(messages[i]);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
const estimatedTokens = Math.round(
|
|
909
|
+
lastPromptTokens * (result.length / messages.length)
|
|
910
|
+
);
|
|
911
|
+
return {
|
|
912
|
+
messages: result,
|
|
913
|
+
totalTokens: estimatedTokens,
|
|
914
|
+
removedCount: removableCount,
|
|
915
|
+
wasTruncated: removableCount > 0
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Truncate oldest: keeps system messages, removes oldest non-system messages first.
|
|
920
|
+
* Always keeps the most recent user message.
|
|
921
|
+
*/
|
|
922
|
+
truncateOldest(messages, tokenCounts, budget) {
|
|
923
|
+
const systemIndices = [];
|
|
924
|
+
const nonSystemIndices = [];
|
|
925
|
+
for (let i = 0; i < messages.length; i++) {
|
|
926
|
+
if (messages[i].role === "system") {
|
|
927
|
+
systemIndices.push(i);
|
|
928
|
+
} else {
|
|
929
|
+
nonSystemIndices.push(i);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
let usedTokens = systemIndices.reduce(
|
|
933
|
+
(sum, i) => sum + tokenCounts[i],
|
|
934
|
+
0
|
|
935
|
+
);
|
|
936
|
+
const includedNonSystem = [];
|
|
937
|
+
for (let i = nonSystemIndices.length - 1; i >= 0; i--) {
|
|
938
|
+
const idx = nonSystemIndices[i];
|
|
939
|
+
if (usedTokens + tokenCounts[idx] <= budget) {
|
|
940
|
+
includedNonSystem.unshift(idx);
|
|
941
|
+
usedTokens += tokenCounts[idx];
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
const includedSet = /* @__PURE__ */ new Set([...systemIndices, ...includedNonSystem]);
|
|
945
|
+
const result = [];
|
|
946
|
+
let resultTokens = 0;
|
|
947
|
+
for (let i = 0; i < messages.length; i++) {
|
|
948
|
+
if (includedSet.has(i)) {
|
|
949
|
+
result.push(messages[i]);
|
|
950
|
+
resultTokens += tokenCounts[i];
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
return {
|
|
954
|
+
messages: result,
|
|
955
|
+
totalTokens: resultTokens,
|
|
956
|
+
removedCount: messages.length - result.length,
|
|
957
|
+
wasTruncated: true
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Sliding window: keeps the most recent messages that fit within budget.
|
|
962
|
+
*/
|
|
963
|
+
slidingWindow(messages, tokenCounts, budget) {
|
|
964
|
+
const result = [];
|
|
965
|
+
let usedTokens = 0;
|
|
966
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
967
|
+
if (usedTokens + tokenCounts[i] <= budget) {
|
|
968
|
+
result.unshift(messages[i]);
|
|
969
|
+
usedTokens += tokenCounts[i];
|
|
970
|
+
} else {
|
|
971
|
+
break;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
return {
|
|
975
|
+
messages: result,
|
|
976
|
+
totalTokens: usedTokens,
|
|
977
|
+
removedCount: messages.length - result.length,
|
|
978
|
+
wasTruncated: true
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Summarize placeholder: replaces truncated messages with a placeholder,
|
|
983
|
+
* preserving system messages and recent context.
|
|
984
|
+
*/
|
|
985
|
+
summarizePlaceholder(messages, tokenCounts, budget) {
|
|
986
|
+
const systemMessages = [];
|
|
987
|
+
const nonSystem = [];
|
|
988
|
+
for (let i = 0; i < messages.length; i++) {
|
|
989
|
+
if (messages[i].role === "system") {
|
|
990
|
+
systemMessages.push({ msg: messages[i], tokens: tokenCounts[i] });
|
|
991
|
+
} else {
|
|
992
|
+
nonSystem.push({ msg: messages[i], tokens: tokenCounts[i], idx: i });
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
let usedTokens = systemMessages.reduce((s, m) => s + m.tokens, 0);
|
|
996
|
+
const placeholderTokens = 20;
|
|
997
|
+
usedTokens += placeholderTokens;
|
|
998
|
+
const recentKept = [];
|
|
999
|
+
for (let i = nonSystem.length - 1; i >= 0; i--) {
|
|
1000
|
+
if (usedTokens + nonSystem[i].tokens <= budget) {
|
|
1001
|
+
recentKept.unshift(nonSystem[i]);
|
|
1002
|
+
usedTokens += nonSystem[i].tokens;
|
|
1003
|
+
} else {
|
|
1004
|
+
break;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
const removedCount = messages.length - systemMessages.length - recentKept.length;
|
|
1008
|
+
const result = [];
|
|
1009
|
+
for (const sm of systemMessages) {
|
|
1010
|
+
result.push(sm.msg);
|
|
1011
|
+
}
|
|
1012
|
+
if (removedCount > 0) {
|
|
1013
|
+
result.push({
|
|
1014
|
+
id: "context-placeholder",
|
|
1015
|
+
role: "system",
|
|
1016
|
+
parts: [{ type: "text", text: `[${removedCount} earlier message${removedCount === 1 ? "" : "s"} omitted for context window]`, status: "complete" }],
|
|
1017
|
+
metadata: { isSummary: true },
|
|
1018
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1019
|
+
status: "complete"
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
for (const m of recentKept) {
|
|
1023
|
+
result.push(m.msg);
|
|
1024
|
+
}
|
|
1025
|
+
return {
|
|
1026
|
+
messages: result,
|
|
1027
|
+
totalTokens: usedTokens,
|
|
1028
|
+
removedCount,
|
|
1029
|
+
wasTruncated: true
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
// src/chat/state.ts
|
|
1035
|
+
var StateMachine = class {
|
|
1036
|
+
constructor(initial, transitions) {
|
|
1037
|
+
this.initial = initial;
|
|
1038
|
+
this.transitions = transitions;
|
|
1039
|
+
this._current = initial;
|
|
1040
|
+
}
|
|
1041
|
+
_current;
|
|
1042
|
+
/** Current state */
|
|
1043
|
+
get current() {
|
|
1044
|
+
return this._current;
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Check whether transitioning to `next` is allowed from current state
|
|
1048
|
+
* @param next - Target state to check
|
|
1049
|
+
* @returns True if transition is allowed
|
|
1050
|
+
*/
|
|
1051
|
+
canTransition(next) {
|
|
1052
|
+
const allowed = this.transitions[this._current];
|
|
1053
|
+
return allowed !== void 0 && allowed.includes(next);
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Transition to `next` state.
|
|
1057
|
+
* @throws ChatError(INVALID_TRANSITION) if the transition is not allowed
|
|
1058
|
+
*/
|
|
1059
|
+
transition(next) {
|
|
1060
|
+
if (!this.canTransition(next)) {
|
|
1061
|
+
throw new ChatError(
|
|
1062
|
+
`Invalid transition: ${this._current} \u2192 ${next}`,
|
|
1063
|
+
{ code: "INVALID_TRANSITION" /* INVALID_TRANSITION */ }
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
this._current = next;
|
|
1067
|
+
}
|
|
1068
|
+
/** Reset to initial state */
|
|
1069
|
+
reset() {
|
|
1070
|
+
this._current = this.initial;
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
var RUNTIME_TRANSITIONS = {
|
|
1074
|
+
idle: ["streaming", "disposed"],
|
|
1075
|
+
streaming: ["idle", "error", "disposed"],
|
|
1076
|
+
error: ["idle", "disposed"],
|
|
1077
|
+
disposed: []
|
|
1078
|
+
};
|
|
1079
|
+
var ChatReentrancyGuard = class {
|
|
1080
|
+
_acquired = false;
|
|
1081
|
+
/** Whether the guard is currently held */
|
|
1082
|
+
get isAcquired() {
|
|
1083
|
+
return this._acquired;
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Acquire the guard. Throws if already acquired.
|
|
1087
|
+
* @throws ChatError with code REENTRANCY
|
|
1088
|
+
*/
|
|
1089
|
+
acquire() {
|
|
1090
|
+
if (this._acquired) {
|
|
1091
|
+
throw new ChatError(
|
|
1092
|
+
"Concurrent operation detected: a send is already in progress",
|
|
1093
|
+
{ code: "REENTRANCY" /* REENTRANCY */ }
|
|
1094
|
+
);
|
|
1095
|
+
}
|
|
1096
|
+
this._acquired = true;
|
|
1097
|
+
}
|
|
1098
|
+
/** Release the guard. Safe to call even if not acquired. */
|
|
1099
|
+
release() {
|
|
1100
|
+
this._acquired = false;
|
|
1101
|
+
}
|
|
1102
|
+
};
|
|
1103
|
+
var ChatAbortController = class {
|
|
1104
|
+
_controller;
|
|
1105
|
+
_onExternalAbort;
|
|
1106
|
+
_externalSignal;
|
|
1107
|
+
constructor(externalSignal) {
|
|
1108
|
+
this._controller = new AbortController();
|
|
1109
|
+
this._externalSignal = externalSignal;
|
|
1110
|
+
if (externalSignal) {
|
|
1111
|
+
if (externalSignal.aborted) {
|
|
1112
|
+
this._controller.abort(externalSignal.reason);
|
|
1113
|
+
} else {
|
|
1114
|
+
this._onExternalAbort = () => {
|
|
1115
|
+
this._controller.abort(externalSignal.reason);
|
|
1116
|
+
};
|
|
1117
|
+
externalSignal.addEventListener("abort", this._onExternalAbort, { once: true });
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
/** The AbortSignal for this controller */
|
|
1122
|
+
get signal() {
|
|
1123
|
+
return this._controller.signal;
|
|
1124
|
+
}
|
|
1125
|
+
/** Whether the operation has been aborted */
|
|
1126
|
+
get isAborted() {
|
|
1127
|
+
return this._controller.signal.aborted;
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Abort the operation.
|
|
1131
|
+
* @param reason - Optional abort reason
|
|
1132
|
+
*/
|
|
1133
|
+
abort(reason) {
|
|
1134
|
+
this._controller.abort(reason);
|
|
1135
|
+
}
|
|
1136
|
+
/** Clean up external signal listener to prevent memory leaks */
|
|
1137
|
+
dispose() {
|
|
1138
|
+
if (this._onExternalAbort && this._externalSignal) {
|
|
1139
|
+
this._externalSignal.removeEventListener("abort", this._onExternalAbort);
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
// src/chat/accumulator.ts
|
|
1145
|
+
var MessageAccumulator = class {
|
|
1146
|
+
messageId;
|
|
1147
|
+
parts = [];
|
|
1148
|
+
status = "pending";
|
|
1149
|
+
currentTextPart = null;
|
|
1150
|
+
currentReasoningPart = null;
|
|
1151
|
+
toolCallParts = /* @__PURE__ */ new Map();
|
|
1152
|
+
_finalized = false;
|
|
1153
|
+
constructor(messageId) {
|
|
1154
|
+
this.messageId = messageId ?? createChatId();
|
|
1155
|
+
}
|
|
1156
|
+
/** Get current message ID */
|
|
1157
|
+
get id() {
|
|
1158
|
+
return this.messageId;
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Apply an AgentEvent to accumulate into the message
|
|
1162
|
+
* @param event - AgentEvent to process
|
|
1163
|
+
* @throws Error if accumulator is already finalized
|
|
1164
|
+
*/
|
|
1165
|
+
apply(event) {
|
|
1166
|
+
if (this._finalized) throw new Error("Cannot apply events to finalized accumulator");
|
|
1167
|
+
if (this.status === "pending") {
|
|
1168
|
+
this.status = "streaming";
|
|
1169
|
+
}
|
|
1170
|
+
switch (event.type) {
|
|
1171
|
+
case "text_delta":
|
|
1172
|
+
this.handleTextDelta(event.text);
|
|
1173
|
+
break;
|
|
1174
|
+
case "thinking_start":
|
|
1175
|
+
this.finalizeCurrentText();
|
|
1176
|
+
this.currentReasoningPart = { type: "reasoning", text: "", status: "streaming" };
|
|
1177
|
+
this.parts.push(this.currentReasoningPart);
|
|
1178
|
+
break;
|
|
1179
|
+
case "thinking_delta":
|
|
1180
|
+
if (this.currentReasoningPart) {
|
|
1181
|
+
this.currentReasoningPart.text += event.text;
|
|
1182
|
+
}
|
|
1183
|
+
break;
|
|
1184
|
+
case "thinking_end":
|
|
1185
|
+
if (this.currentReasoningPart) {
|
|
1186
|
+
this.currentReasoningPart.status = "complete";
|
|
1187
|
+
this.currentReasoningPart = null;
|
|
1188
|
+
}
|
|
1189
|
+
break;
|
|
1190
|
+
case "tool_call_start": {
|
|
1191
|
+
this.finalizeCurrentText();
|
|
1192
|
+
const toolPart = {
|
|
1193
|
+
type: "tool_call",
|
|
1194
|
+
toolCallId: event.toolCallId,
|
|
1195
|
+
name: event.toolName,
|
|
1196
|
+
args: event.args,
|
|
1197
|
+
status: "running"
|
|
1198
|
+
};
|
|
1199
|
+
this.toolCallParts.set(event.toolCallId, toolPart);
|
|
1200
|
+
this.parts.push(toolPart);
|
|
1201
|
+
break;
|
|
1202
|
+
}
|
|
1203
|
+
case "tool_call_end": {
|
|
1204
|
+
const existing = this.toolCallParts.get(event.toolCallId);
|
|
1205
|
+
if (existing) {
|
|
1206
|
+
existing.result = event.result;
|
|
1207
|
+
existing.status = "complete";
|
|
1208
|
+
}
|
|
1209
|
+
break;
|
|
1210
|
+
}
|
|
1211
|
+
case "error":
|
|
1212
|
+
this.status = "error";
|
|
1213
|
+
break;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
handleTextDelta(text) {
|
|
1217
|
+
if (!this.currentTextPart) {
|
|
1218
|
+
this.currentTextPart = { type: "text", text: "", status: "streaming" };
|
|
1219
|
+
this.parts.push(this.currentTextPart);
|
|
1220
|
+
}
|
|
1221
|
+
this.currentTextPart.text += text;
|
|
1222
|
+
}
|
|
1223
|
+
finalizeCurrentText() {
|
|
1224
|
+
if (this.currentTextPart) {
|
|
1225
|
+
this.currentTextPart.status = "complete";
|
|
1226
|
+
this.currentTextPart = null;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Get a snapshot of the current accumulated message (for streaming UI)
|
|
1231
|
+
* @returns ChatMessage with current parts and "streaming" status
|
|
1232
|
+
*/
|
|
1233
|
+
snapshot() {
|
|
1234
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1235
|
+
return {
|
|
1236
|
+
id: this.messageId,
|
|
1237
|
+
role: "assistant",
|
|
1238
|
+
parts: this.parts.map((p) => ({ ...p })),
|
|
1239
|
+
status: this.status === "pending" ? "pending" : "streaming",
|
|
1240
|
+
createdAt: now,
|
|
1241
|
+
updatedAt: now
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* Finalize the accumulator and return the complete ChatMessage
|
|
1246
|
+
* @returns Completed ChatMessage with all parts finalized
|
|
1247
|
+
* @throws Error if accumulator is already finalized
|
|
1248
|
+
*/
|
|
1249
|
+
finalize() {
|
|
1250
|
+
if (this._finalized) throw new Error("Accumulator already finalized");
|
|
1251
|
+
this._finalized = true;
|
|
1252
|
+
this.finalizeCurrentText();
|
|
1253
|
+
if (this.currentReasoningPart) {
|
|
1254
|
+
this.currentReasoningPart.status = "complete";
|
|
1255
|
+
this.currentReasoningPart = null;
|
|
1256
|
+
}
|
|
1257
|
+
for (const [, toolPart] of this.toolCallParts) {
|
|
1258
|
+
if (toolPart.status === "running" || toolPart.status === "pending") {
|
|
1259
|
+
toolPart.status = "error";
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
if (this.status !== "error" && this.status !== "cancelled") {
|
|
1263
|
+
this.status = "complete";
|
|
1264
|
+
}
|
|
1265
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1266
|
+
return {
|
|
1267
|
+
id: this.messageId,
|
|
1268
|
+
role: "assistant",
|
|
1269
|
+
parts: this.parts,
|
|
1270
|
+
status: this.status,
|
|
1271
|
+
createdAt: now,
|
|
1272
|
+
updatedAt: now
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
/** Check if the accumulator has been finalized */
|
|
1276
|
+
get finalized() {
|
|
1277
|
+
return this._finalized;
|
|
1278
|
+
}
|
|
1279
|
+
};
|
|
1280
|
+
|
|
1281
|
+
// src/chat/watchdog.ts
|
|
1282
|
+
async function* withStreamWatchdog(source, config) {
|
|
1283
|
+
const { timeoutMs, signal } = config;
|
|
1284
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
1285
|
+
let aborted = false;
|
|
1286
|
+
if (signal?.aborted) {
|
|
1287
|
+
iterator.return?.();
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
const onAbort = () => {
|
|
1291
|
+
aborted = true;
|
|
1292
|
+
iterator.return?.();
|
|
1293
|
+
};
|
|
1294
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1295
|
+
try {
|
|
1296
|
+
while (true) {
|
|
1297
|
+
if (aborted) break;
|
|
1298
|
+
const timeout = new CancellableTimeout(timeoutMs);
|
|
1299
|
+
try {
|
|
1300
|
+
const result = await Promise.race([
|
|
1301
|
+
iterator.next(),
|
|
1302
|
+
timeout.promise
|
|
1303
|
+
]);
|
|
1304
|
+
timeout.cancel();
|
|
1305
|
+
if (result.done) break;
|
|
1306
|
+
yield result.value;
|
|
1307
|
+
} catch (err) {
|
|
1308
|
+
timeout.cancel();
|
|
1309
|
+
throw err;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
} finally {
|
|
1313
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1314
|
+
iterator.return?.();
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
var CancellableTimeout = class {
|
|
1318
|
+
promise;
|
|
1319
|
+
_timer;
|
|
1320
|
+
_cancelled = false;
|
|
1321
|
+
constructor(ms) {
|
|
1322
|
+
this.promise = new Promise((_, reject) => {
|
|
1323
|
+
this._timer = setTimeout(() => {
|
|
1324
|
+
if (!this._cancelled) {
|
|
1325
|
+
reject(
|
|
1326
|
+
new ChatError(
|
|
1327
|
+
`Stream timed out after ${ms}ms of inactivity`,
|
|
1328
|
+
{ code: "TIMEOUT" /* TIMEOUT */ }
|
|
1329
|
+
)
|
|
1330
|
+
);
|
|
1331
|
+
}
|
|
1332
|
+
}, ms);
|
|
1333
|
+
});
|
|
1334
|
+
this.promise.catch(() => {
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
cancel() {
|
|
1338
|
+
this._cancelled = true;
|
|
1339
|
+
if (this._timer !== void 0) {
|
|
1340
|
+
clearTimeout(this._timer);
|
|
1341
|
+
this._timer = void 0;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
|
|
1346
|
+
// src/chat/listener-set.ts
|
|
1347
|
+
var ListenerSet = class {
|
|
1348
|
+
_listeners = /* @__PURE__ */ new Set();
|
|
1349
|
+
/** Add a listener. Returns an unsubscribe function. */
|
|
1350
|
+
add(callback) {
|
|
1351
|
+
this._listeners.add(callback);
|
|
1352
|
+
return () => {
|
|
1353
|
+
this._listeners.delete(callback);
|
|
1354
|
+
};
|
|
1355
|
+
}
|
|
1356
|
+
/** Notify all listeners with the given arguments. Errors are isolated per listener. */
|
|
1357
|
+
notify(...args) {
|
|
1358
|
+
for (const cb of this._listeners) {
|
|
1359
|
+
try {
|
|
1360
|
+
cb(...args);
|
|
1361
|
+
} catch {
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
/** Remove all listeners. */
|
|
1366
|
+
clear() {
|
|
1367
|
+
this._listeners.clear();
|
|
1368
|
+
}
|
|
1369
|
+
/** Current number of listeners. */
|
|
1370
|
+
get size() {
|
|
1371
|
+
return this._listeners.size;
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
1374
|
+
|
|
1375
|
+
// src/chat/runtime.ts
|
|
1376
|
+
var ChatRuntime = class {
|
|
1377
|
+
_state;
|
|
1378
|
+
_guard;
|
|
1379
|
+
_backends;
|
|
1380
|
+
_sessionStore;
|
|
1381
|
+
_contextConfig;
|
|
1382
|
+
_ctxManager;
|
|
1383
|
+
_middleware;
|
|
1384
|
+
_tools = /* @__PURE__ */ new Map();
|
|
1385
|
+
_retryConfig;
|
|
1386
|
+
_contextStats = /* @__PURE__ */ new Map();
|
|
1387
|
+
_sessionUsage = /* @__PURE__ */ new Map();
|
|
1388
|
+
_modelContextWindows = /* @__PURE__ */ new Map();
|
|
1389
|
+
_onContextTrimmed;
|
|
1390
|
+
_streamTimeoutMs;
|
|
1391
|
+
_sessionListeners = new ListenerSet();
|
|
1392
|
+
_adapterPool = /* @__PURE__ */ new Map();
|
|
1393
|
+
_defaultBackend;
|
|
1394
|
+
_abortController = null;
|
|
1395
|
+
constructor(options) {
|
|
1396
|
+
this._state = new StateMachine("idle", RUNTIME_TRANSITIONS);
|
|
1397
|
+
this._guard = new ChatReentrancyGuard();
|
|
1398
|
+
this._backends = options.backends;
|
|
1399
|
+
this._defaultBackend = options.defaultBackend;
|
|
1400
|
+
this._sessionStore = options.sessionStore;
|
|
1401
|
+
this._contextConfig = options.context;
|
|
1402
|
+
if (this._contextConfig) {
|
|
1403
|
+
this._ctxManager = new ContextWindowManager(this._contextConfig);
|
|
1404
|
+
}
|
|
1405
|
+
this._middleware = [...options.middleware ?? []];
|
|
1406
|
+
this._retryConfig = options.retryConfig;
|
|
1407
|
+
this._onContextTrimmed = options.onContextTrimmed;
|
|
1408
|
+
this._streamTimeoutMs = options.streamTimeoutMs;
|
|
1409
|
+
if (!options.backends[options.defaultBackend]) {
|
|
1410
|
+
throw new ChatError(
|
|
1411
|
+
`Default backend "${options.defaultBackend}" not found in backends map`,
|
|
1412
|
+
{ code: "INVALID_INPUT" /* INVALID_INPUT */ }
|
|
1413
|
+
);
|
|
1414
|
+
}
|
|
1415
|
+
if (options.tools) {
|
|
1416
|
+
for (const tool of options.tools) {
|
|
1417
|
+
this._tools.set(tool.name, tool);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
// ── Lifecycle ──────────────────────────────────────────────
|
|
1422
|
+
get status() {
|
|
1423
|
+
return this._state.current;
|
|
1424
|
+
}
|
|
1425
|
+
async dispose() {
|
|
1426
|
+
if (this._state.current === "disposed") return;
|
|
1427
|
+
this._abortController?.abort("Runtime disposed");
|
|
1428
|
+
this._abortController?.dispose();
|
|
1429
|
+
this._abortController = null;
|
|
1430
|
+
this._state.transition("disposed");
|
|
1431
|
+
for (const adapter of this._adapterPool.values()) {
|
|
1432
|
+
try {
|
|
1433
|
+
await adapter.dispose();
|
|
1434
|
+
} catch {
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
this._adapterPool.clear();
|
|
1438
|
+
}
|
|
1439
|
+
// ── Sessions ───────────────────────────────────────────────
|
|
1440
|
+
async createSession(options) {
|
|
1441
|
+
this.assertNotDisposed();
|
|
1442
|
+
const config = {
|
|
1443
|
+
model: options.config?.model ?? "",
|
|
1444
|
+
backend: options.config?.backend ?? this._defaultBackend,
|
|
1445
|
+
...options.config
|
|
1446
|
+
};
|
|
1447
|
+
const session = await this._sessionStore.createSession({ ...options, config });
|
|
1448
|
+
this._notifySessionChange();
|
|
1449
|
+
return session;
|
|
1450
|
+
}
|
|
1451
|
+
async getSession(id) {
|
|
1452
|
+
this.assertNotDisposed();
|
|
1453
|
+
const cid = toChatId(id);
|
|
1454
|
+
return this._sessionStore.getSession(cid);
|
|
1455
|
+
}
|
|
1456
|
+
async listSessions(options) {
|
|
1457
|
+
this.assertNotDisposed();
|
|
1458
|
+
return this._sessionStore.listSessions(options);
|
|
1459
|
+
}
|
|
1460
|
+
async deleteSession(id) {
|
|
1461
|
+
this.assertNotDisposed();
|
|
1462
|
+
const cid = toChatId(id);
|
|
1463
|
+
const session = await this._sessionStore.getSession(cid);
|
|
1464
|
+
if (!session) return;
|
|
1465
|
+
await this._sessionStore.deleteSession(cid);
|
|
1466
|
+
this._contextStats.delete(cid);
|
|
1467
|
+
this._sessionUsage.delete(cid);
|
|
1468
|
+
this._notifySessionChange();
|
|
1469
|
+
}
|
|
1470
|
+
// ── Messaging ──────────────────────────────────────────────
|
|
1471
|
+
async *send(sessionId, message, options) {
|
|
1472
|
+
this.validateSendInput(message, options);
|
|
1473
|
+
this._guard.acquire();
|
|
1474
|
+
const cid = toChatId(sessionId);
|
|
1475
|
+
this._abortController = new ChatAbortController(options?.signal);
|
|
1476
|
+
try {
|
|
1477
|
+
if (this._state.current === "error") {
|
|
1478
|
+
this._state.transition("idle");
|
|
1479
|
+
}
|
|
1480
|
+
this._state.transition("streaming");
|
|
1481
|
+
await this.loadSession(cid);
|
|
1482
|
+
const mwCtx = {
|
|
1483
|
+
sessionId: cid,
|
|
1484
|
+
signal: this._abortController.signal
|
|
1485
|
+
};
|
|
1486
|
+
const userMessage = await this.applyBeforeSendMiddleware(
|
|
1487
|
+
this.createUserMessage(message),
|
|
1488
|
+
mwCtx
|
|
1489
|
+
);
|
|
1490
|
+
if (userMessage === null) {
|
|
1491
|
+
this._state.transition("idle");
|
|
1492
|
+
return;
|
|
1493
|
+
}
|
|
1494
|
+
const updatedSession = await this.persistAndReload(cid, userMessage);
|
|
1495
|
+
const sessionForAdapter = await this.trimSessionContext(cid, updatedSession, options.model);
|
|
1496
|
+
const stream = await this.prepareEventStream(
|
|
1497
|
+
cid,
|
|
1498
|
+
sessionForAdapter,
|
|
1499
|
+
updatedSession,
|
|
1500
|
+
message,
|
|
1501
|
+
options
|
|
1502
|
+
);
|
|
1503
|
+
const accumulator = new MessageAccumulator();
|
|
1504
|
+
const eventSource = this._streamTimeoutMs ? withStreamWatchdog(stream, { timeoutMs: this._streamTimeoutMs, signal: this._abortController.signal }) : stream;
|
|
1505
|
+
for await (const event of eventSource) {
|
|
1506
|
+
if (this._abortController.isAborted) break;
|
|
1507
|
+
this.feedAccumulator(accumulator, event);
|
|
1508
|
+
if (event.type === "usage") {
|
|
1509
|
+
this._sessionUsage.set(cid, {
|
|
1510
|
+
promptTokens: event.promptTokens,
|
|
1511
|
+
completionTokens: event.completionTokens
|
|
1512
|
+
});
|
|
1513
|
+
this.updateContextStatsWithUsage(cid, event.promptTokens, event.completionTokens, options);
|
|
1514
|
+
}
|
|
1515
|
+
const processed = await this.applyOnEventMiddleware(event, mwCtx);
|
|
1516
|
+
if (processed) yield processed;
|
|
1517
|
+
}
|
|
1518
|
+
if (this._state.current === "disposed") return;
|
|
1519
|
+
await this.finalizeAssistantMessage(cid, accumulator, mwCtx);
|
|
1520
|
+
this._state.transition("idle");
|
|
1521
|
+
} catch (error) {
|
|
1522
|
+
const result = await this.handleSendError(error, cid);
|
|
1523
|
+
if (result !== null) throw result;
|
|
1524
|
+
} finally {
|
|
1525
|
+
this._guard.release();
|
|
1526
|
+
this._abortController?.dispose();
|
|
1527
|
+
this._abortController = null;
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
// ── Send Pipeline Stages ──────────────────────────────────────
|
|
1531
|
+
/** Stage 1: Validate send inputs (message content + required fields). */
|
|
1532
|
+
validateSendInput(message, options) {
|
|
1533
|
+
this.assertNotDisposed();
|
|
1534
|
+
if (!message || message.trim().length === 0) {
|
|
1535
|
+
throw new ChatError("Message cannot be empty", { code: "INVALID_INPUT" /* INVALID_INPUT */ });
|
|
1536
|
+
}
|
|
1537
|
+
if (!options.model) {
|
|
1538
|
+
throw new ChatError(
|
|
1539
|
+
"options.model is required \u2014 caller must specify which model to use",
|
|
1540
|
+
{ code: "INVALID_INPUT" /* INVALID_INPUT */ }
|
|
1541
|
+
);
|
|
1542
|
+
}
|
|
1543
|
+
if (!options.backend) {
|
|
1544
|
+
throw new ChatError(
|
|
1545
|
+
"options.backend is required \u2014 caller must specify which backend to use",
|
|
1546
|
+
{ code: "INVALID_INPUT" /* INVALID_INPUT */ }
|
|
1547
|
+
);
|
|
1548
|
+
}
|
|
1549
|
+
if (!options.credentials) {
|
|
1550
|
+
throw new ChatError(
|
|
1551
|
+
"options.credentials is required \u2014 caller must provide authentication credentials",
|
|
1552
|
+
{ code: "INVALID_INPUT" /* INVALID_INPUT */ }
|
|
1553
|
+
);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
/** Stage 2: Load session from store. */
|
|
1557
|
+
async loadSession(cid) {
|
|
1558
|
+
const session = await this._sessionStore.getSession(cid);
|
|
1559
|
+
if (!session) {
|
|
1560
|
+
throw new ChatError(
|
|
1561
|
+
`Session "${cid}" not found`,
|
|
1562
|
+
{ code: "SESSION_NOT_FOUND" /* SESSION_NOT_FOUND */ }
|
|
1563
|
+
);
|
|
1564
|
+
}
|
|
1565
|
+
return session;
|
|
1566
|
+
}
|
|
1567
|
+
/** Stage 3: Apply onBeforeSend middleware pipeline. Returns null if middleware rejected the send. */
|
|
1568
|
+
async applyBeforeSendMiddleware(userMessage, ctx) {
|
|
1569
|
+
let msg = userMessage;
|
|
1570
|
+
for (const mw of this._middleware) {
|
|
1571
|
+
if (mw.onBeforeSend && msg) {
|
|
1572
|
+
msg = await mw.onBeforeSend(msg, ctx);
|
|
1573
|
+
if (msg === null) return null;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
return msg;
|
|
1577
|
+
}
|
|
1578
|
+
/** Stage 4: Persist user message and reload session with full history. */
|
|
1579
|
+
async persistAndReload(cid, userMessage) {
|
|
1580
|
+
await this._sessionStore.appendMessage(cid, userMessage);
|
|
1581
|
+
return await this._sessionStore.getSession(cid);
|
|
1582
|
+
}
|
|
1583
|
+
/** Stage 5: Auto-trim context window if configured. Returns session snapshot for adapter. */
|
|
1584
|
+
async trimSessionContext(cid, session, model) {
|
|
1585
|
+
if (!this._ctxManager) return session;
|
|
1586
|
+
const ctxManager = this._ctxManager;
|
|
1587
|
+
const lastUsage = this._sessionUsage.get(cid);
|
|
1588
|
+
const modelContextWindow = model ? this._modelContextWindows.get(model) : void 0;
|
|
1589
|
+
if (lastUsage && modelContextWindow) {
|
|
1590
|
+
const result2 = ctxManager.fitMessagesWithUsage(
|
|
1591
|
+
session.messages,
|
|
1592
|
+
lastUsage.promptTokens,
|
|
1593
|
+
modelContextWindow
|
|
1594
|
+
);
|
|
1595
|
+
this._contextStats.set(cid, {
|
|
1596
|
+
totalTokens: result2.totalTokens,
|
|
1597
|
+
removedCount: result2.removedCount,
|
|
1598
|
+
wasTruncated: result2.wasTruncated,
|
|
1599
|
+
availableBudget: Math.max(0, modelContextWindow - result2.totalTokens),
|
|
1600
|
+
realPromptTokens: lastUsage.promptTokens,
|
|
1601
|
+
realCompletionTokens: lastUsage.completionTokens,
|
|
1602
|
+
modelContextWindow
|
|
1603
|
+
});
|
|
1604
|
+
if (result2.wasTruncated && this._onContextTrimmed) {
|
|
1605
|
+
const keptIds = new Set(result2.messages.map((m) => m.id));
|
|
1606
|
+
const removed = session.messages.filter((m) => !keptIds.has(m.id));
|
|
1607
|
+
if (removed.length > 0) {
|
|
1608
|
+
try {
|
|
1609
|
+
this._onContextTrimmed(cid, removed);
|
|
1610
|
+
} catch {
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
return { ...session, messages: result2.messages };
|
|
1615
|
+
}
|
|
1616
|
+
const result = await ctxManager.fitMessagesAsync(session.messages);
|
|
1617
|
+
this._contextStats.set(cid, {
|
|
1618
|
+
totalTokens: result.totalTokens,
|
|
1619
|
+
removedCount: result.removedCount,
|
|
1620
|
+
wasTruncated: result.wasTruncated,
|
|
1621
|
+
availableBudget: ctxManager.availableBudget,
|
|
1622
|
+
modelContextWindow
|
|
1623
|
+
});
|
|
1624
|
+
if (result.wasTruncated && this._onContextTrimmed) {
|
|
1625
|
+
const keptIds = new Set(result.messages.map((m) => m.id));
|
|
1626
|
+
const removed = session.messages.filter((m) => !keptIds.has(m.id));
|
|
1627
|
+
if (removed.length > 0) {
|
|
1628
|
+
try {
|
|
1629
|
+
this._onContextTrimmed(cid, removed);
|
|
1630
|
+
} catch {
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
return { ...session, messages: result.messages };
|
|
1635
|
+
}
|
|
1636
|
+
/** Update context stats with real usage data from a usage event. */
|
|
1637
|
+
updateContextStatsWithUsage(cid, promptTokens, completionTokens, options) {
|
|
1638
|
+
const modelContextWindow = options.model ? this._modelContextWindows.get(options.model) : void 0;
|
|
1639
|
+
const existing = this._contextStats.get(cid);
|
|
1640
|
+
this._contextStats.set(cid, {
|
|
1641
|
+
totalTokens: promptTokens,
|
|
1642
|
+
removedCount: existing?.removedCount ?? 0,
|
|
1643
|
+
wasTruncated: existing?.wasTruncated ?? false,
|
|
1644
|
+
availableBudget: modelContextWindow ? Math.max(0, modelContextWindow - promptTokens) : existing?.availableBudget ?? 0,
|
|
1645
|
+
realPromptTokens: promptTokens,
|
|
1646
|
+
realCompletionTokens: completionTokens,
|
|
1647
|
+
modelContextWindow
|
|
1648
|
+
});
|
|
1649
|
+
}
|
|
1650
|
+
/** Stage 6: Prepare event stream — adapter with retry, tool injection. */
|
|
1651
|
+
async prepareEventStream(cid, sessionForAdapter, fullSession, message, options) {
|
|
1652
|
+
const adapter = await this.getOrCreateAdapterWithRetry(options.backend, options.credentials);
|
|
1653
|
+
const runtimeTools = this._tools.size > 0 ? this.injectToolContext([...this._tools.values()], {
|
|
1654
|
+
sessionId: cid,
|
|
1655
|
+
custom: fullSession.metadata?.custom
|
|
1656
|
+
}) : void 0;
|
|
1657
|
+
const streamOptions = {
|
|
1658
|
+
signal: this._abortController.signal,
|
|
1659
|
+
model: options.model,
|
|
1660
|
+
systemPrompt: options.systemPrompt,
|
|
1661
|
+
tools: runtimeTools
|
|
1662
|
+
};
|
|
1663
|
+
return this.createStreamWithRetry(
|
|
1664
|
+
adapter,
|
|
1665
|
+
sessionForAdapter,
|
|
1666
|
+
message,
|
|
1667
|
+
streamOptions,
|
|
1668
|
+
options.backend,
|
|
1669
|
+
options.credentials
|
|
1670
|
+
);
|
|
1671
|
+
}
|
|
1672
|
+
/** Stage 7: Apply onEvent middleware pipeline (sequential transform/suppress). */
|
|
1673
|
+
async applyOnEventMiddleware(event, ctx) {
|
|
1674
|
+
let processed = event;
|
|
1675
|
+
for (const mw of this._middleware) {
|
|
1676
|
+
if (mw.onEvent && processed) {
|
|
1677
|
+
processed = await mw.onEvent(processed, ctx);
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
return processed;
|
|
1681
|
+
}
|
|
1682
|
+
/** Stage 8: Finalize accumulator, apply afterReceive middleware, persist assistant message. */
|
|
1683
|
+
async finalizeAssistantMessage(cid, accumulator, ctx) {
|
|
1684
|
+
let assistantMessage = accumulator.finalize();
|
|
1685
|
+
for (const mw of this._middleware) {
|
|
1686
|
+
if (mw.onAfterReceive) {
|
|
1687
|
+
assistantMessage = await mw.onAfterReceive(assistantMessage, ctx);
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
await this._sessionStore.appendMessage(cid, assistantMessage);
|
|
1691
|
+
this._notifySessionChange();
|
|
1692
|
+
}
|
|
1693
|
+
/** Stage 9: Error handling — apply onError middleware, transition state. Returns null if suppressed. */
|
|
1694
|
+
async handleSendError(error, cid) {
|
|
1695
|
+
let processedError = error instanceof Error ? error : new Error(String(error));
|
|
1696
|
+
const ctx = {
|
|
1697
|
+
sessionId: cid,
|
|
1698
|
+
signal: this._abortController?.signal ?? new AbortController().signal
|
|
1699
|
+
};
|
|
1700
|
+
for (const mw of this._middleware) {
|
|
1701
|
+
if (mw.onError) {
|
|
1702
|
+
const result = await mw.onError(processedError, ctx);
|
|
1703
|
+
if (result === null) {
|
|
1704
|
+
if (this._state.canTransition("idle")) {
|
|
1705
|
+
this._state.transition("idle");
|
|
1706
|
+
}
|
|
1707
|
+
return null;
|
|
1708
|
+
}
|
|
1709
|
+
processedError = result;
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
if (this._state.canTransition("error")) {
|
|
1713
|
+
this._state.transition("error");
|
|
1714
|
+
}
|
|
1715
|
+
return processedError;
|
|
1716
|
+
}
|
|
1717
|
+
abort() {
|
|
1718
|
+
this._abortController?.abort("User abort");
|
|
1719
|
+
}
|
|
1720
|
+
// ── Backend / Model ────────────────────────────────────────
|
|
1721
|
+
async listModels(options) {
|
|
1722
|
+
this.assertNotDisposed();
|
|
1723
|
+
let models = [];
|
|
1724
|
+
const firstAdapter = [...this._adapterPool.values()][0];
|
|
1725
|
+
if (firstAdapter) {
|
|
1726
|
+
try {
|
|
1727
|
+
models = await firstAdapter.listModels();
|
|
1728
|
+
} catch {
|
|
1729
|
+
return [];
|
|
1730
|
+
}
|
|
1731
|
+
} else if (options?.backend && options?.credentials) {
|
|
1732
|
+
try {
|
|
1733
|
+
const adapter = await this.getOrCreateAdapter(options.backend, options.credentials);
|
|
1734
|
+
models = await adapter.listModels();
|
|
1735
|
+
} catch {
|
|
1736
|
+
return [];
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
for (const model of models) {
|
|
1740
|
+
if (model.contextWindow != null) {
|
|
1741
|
+
this._modelContextWindows.set(model.id, model.contextWindow);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
return models;
|
|
1745
|
+
}
|
|
1746
|
+
async listBackends() {
|
|
1747
|
+
this.assertNotDisposed();
|
|
1748
|
+
return Object.keys(this._backends).map((name) => ({ name }));
|
|
1749
|
+
}
|
|
1750
|
+
// ── Tools ──────────────────────────────────────────────────
|
|
1751
|
+
get registeredTools() {
|
|
1752
|
+
return this._tools;
|
|
1753
|
+
}
|
|
1754
|
+
registerTool(tool) {
|
|
1755
|
+
this.assertNotDisposed();
|
|
1756
|
+
this._tools.set(tool.name, tool);
|
|
1757
|
+
}
|
|
1758
|
+
removeTool(name) {
|
|
1759
|
+
this.assertNotDisposed();
|
|
1760
|
+
this._tools.delete(name);
|
|
1761
|
+
}
|
|
1762
|
+
// ── Middleware ──────────────────────────────────────────────
|
|
1763
|
+
use(middleware) {
|
|
1764
|
+
this.assertNotDisposed();
|
|
1765
|
+
this._middleware.push(middleware);
|
|
1766
|
+
}
|
|
1767
|
+
removeMiddleware(middleware) {
|
|
1768
|
+
this.assertNotDisposed();
|
|
1769
|
+
const idx = this._middleware.indexOf(middleware);
|
|
1770
|
+
if (idx >= 0) this._middleware.splice(idx, 1);
|
|
1771
|
+
}
|
|
1772
|
+
// ── Context Stats ─────────────────────────────────────────
|
|
1773
|
+
async getContextStats(sessionId) {
|
|
1774
|
+
const cid = toChatId(sessionId);
|
|
1775
|
+
return this._contextStats.get(cid) ?? null;
|
|
1776
|
+
}
|
|
1777
|
+
// ── Session Subscription ──────────────────────────────────
|
|
1778
|
+
onSessionChange(callback) {
|
|
1779
|
+
return this._sessionListeners.add(callback);
|
|
1780
|
+
}
|
|
1781
|
+
_notifySessionChange() {
|
|
1782
|
+
this._sessionListeners.notify();
|
|
1783
|
+
}
|
|
1784
|
+
// ── Private Helpers ────────────────────────────────────────
|
|
1785
|
+
async getOrCreateAdapter(backend, credentials) {
|
|
1786
|
+
const key = this.getPoolKey(backend, credentials);
|
|
1787
|
+
const existing = this._adapterPool.get(key);
|
|
1788
|
+
if (existing) return existing;
|
|
1789
|
+
for (const [oldKey, oldAdapter] of this._adapterPool) {
|
|
1790
|
+
if (oldKey.startsWith(backend + ":")) {
|
|
1791
|
+
try {
|
|
1792
|
+
await oldAdapter.dispose();
|
|
1793
|
+
} catch {
|
|
1794
|
+
}
|
|
1795
|
+
this._adapterPool.delete(oldKey);
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
const factory = this._backends[backend];
|
|
1799
|
+
if (!factory) {
|
|
1800
|
+
throw new ChatError(
|
|
1801
|
+
`Backend "${backend}" not found`,
|
|
1802
|
+
{ code: "INVALID_INPUT" /* INVALID_INPUT */ }
|
|
1803
|
+
);
|
|
1804
|
+
}
|
|
1805
|
+
const adapter = await factory(credentials);
|
|
1806
|
+
this._adapterPool.set(key, adapter);
|
|
1807
|
+
return adapter;
|
|
1808
|
+
}
|
|
1809
|
+
getPoolKey(backend, credentials) {
|
|
1810
|
+
const token = credentials.accessToken;
|
|
1811
|
+
const hash = token.length > 16 ? token.slice(0, 8) + token.slice(-8) : token;
|
|
1812
|
+
return `${backend}:${hash}`;
|
|
1813
|
+
}
|
|
1814
|
+
/** Wrap each tool's execute to inject ToolContext as 2nd argument */
|
|
1815
|
+
injectToolContext(tools, context) {
|
|
1816
|
+
return tools.map((tool) => ({
|
|
1817
|
+
...tool,
|
|
1818
|
+
execute: (params) => tool.execute(params, context)
|
|
1819
|
+
}));
|
|
1820
|
+
}
|
|
1821
|
+
/** Map ChatEvent to AgentEvent for MessageAccumulator */
|
|
1822
|
+
feedAccumulator(acc, event) {
|
|
1823
|
+
const agentEvent = chatEventToAgentEvent(event);
|
|
1824
|
+
if (agentEvent) acc.apply(agentEvent);
|
|
1825
|
+
}
|
|
1826
|
+
createUserMessage(text) {
|
|
1827
|
+
return {
|
|
1828
|
+
id: createChatId(),
|
|
1829
|
+
role: "user",
|
|
1830
|
+
parts: [{ type: "text", text, status: "complete" }],
|
|
1831
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1832
|
+
status: "complete"
|
|
1833
|
+
};
|
|
1834
|
+
}
|
|
1835
|
+
assertNotDisposed() {
|
|
1836
|
+
if (this._state.current === "disposed") {
|
|
1837
|
+
throw new ChatError(
|
|
1838
|
+
"Runtime is disposed",
|
|
1839
|
+
{ code: "DISPOSED" /* DISPOSED */ }
|
|
1840
|
+
);
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
/** Get or create adapter with retry on connection errors */
|
|
1844
|
+
async getOrCreateAdapterWithRetry(backend, credentials) {
|
|
1845
|
+
const maxAttempts = this._retryConfig?.maxAttempts ?? 1;
|
|
1846
|
+
const delayMs = this._retryConfig?.delayMs ?? 0;
|
|
1847
|
+
let lastError;
|
|
1848
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
1849
|
+
try {
|
|
1850
|
+
return await this.getOrCreateAdapter(backend, credentials);
|
|
1851
|
+
} catch (err) {
|
|
1852
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
1853
|
+
if (attempt < maxAttempts) {
|
|
1854
|
+
const key = this.getPoolKey(backend, credentials);
|
|
1855
|
+
const old = this._adapterPool.get(key);
|
|
1856
|
+
if (old) {
|
|
1857
|
+
try {
|
|
1858
|
+
await old.dispose();
|
|
1859
|
+
} catch {
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
this._adapterPool.delete(key);
|
|
1863
|
+
await delay(delayMs);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
throw lastError;
|
|
1868
|
+
}
|
|
1869
|
+
/**
|
|
1870
|
+
* Create stream with retry for pre-stream connection errors.
|
|
1871
|
+
* Tries to get the first event from the stream; if that fails,
|
|
1872
|
+
* retries with a fresh adapter. Once first event is received,
|
|
1873
|
+
* the stream is committed (no more retries).
|
|
1874
|
+
*/
|
|
1875
|
+
async createStreamWithRetry(adapter, session, message, options, backend, credentials) {
|
|
1876
|
+
const maxAttempts = this._retryConfig?.maxAttempts ?? 1;
|
|
1877
|
+
const delayMs = this._retryConfig?.delayMs ?? 0;
|
|
1878
|
+
let lastError;
|
|
1879
|
+
let currentAdapter = adapter;
|
|
1880
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
1881
|
+
try {
|
|
1882
|
+
const stream = currentAdapter.streamMessage(session, message, options);
|
|
1883
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
1884
|
+
const first = await iterator.next();
|
|
1885
|
+
return (async function* () {
|
|
1886
|
+
if (!first.done) yield first.value;
|
|
1887
|
+
while (true) {
|
|
1888
|
+
const next = await iterator.next();
|
|
1889
|
+
if (next.done) break;
|
|
1890
|
+
yield next.value;
|
|
1891
|
+
}
|
|
1892
|
+
})();
|
|
1893
|
+
} catch (err) {
|
|
1894
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
1895
|
+
if (attempt < maxAttempts) {
|
|
1896
|
+
try {
|
|
1897
|
+
await currentAdapter.dispose();
|
|
1898
|
+
} catch {
|
|
1899
|
+
}
|
|
1900
|
+
const key = this.getPoolKey(backend, credentials);
|
|
1901
|
+
this._adapterPool.delete(key);
|
|
1902
|
+
await delay(delayMs);
|
|
1903
|
+
currentAdapter = await this.getOrCreateAdapter(backend, credentials);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
throw lastError;
|
|
1908
|
+
}
|
|
1909
|
+
};
|
|
1910
|
+
function delay(ms) {
|
|
1911
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
1912
|
+
}
|
|
1913
|
+
function createChatRuntime(options) {
|
|
1914
|
+
return new ChatRuntime(options);
|
|
1915
|
+
}
|
|
1916
|
+
var DEFAULT_PROVIDER_MODELS = {
|
|
1917
|
+
copilot: "gpt-5-mini",
|
|
1918
|
+
claude: "claude-sonnet-4-5-20250514",
|
|
1919
|
+
"vercel-ai": "gpt-4.1-mini"
|
|
1920
|
+
};
|
|
466
1921
|
var MIME_TYPES = {
|
|
467
1922
|
".html": "text/html",
|
|
468
1923
|
".css": "text/css",
|
|
@@ -482,15 +1937,25 @@ var MIME_TYPES = {
|
|
|
482
1937
|
".map": "application/json"
|
|
483
1938
|
};
|
|
484
1939
|
function createChatServer(options) {
|
|
1940
|
+
const runtime = options.runtime ?? (options.runtimeConfig ? createChatRuntime(options.runtimeConfig) : (() => {
|
|
1941
|
+
throw new Error("Either `runtime` or `runtimeConfig` must be provided to createChatServer");
|
|
1942
|
+
})());
|
|
485
1943
|
const chatPrefix = options.chatPrefix ?? "/api/chat";
|
|
486
1944
|
const authPrefix = options.authPrefix ?? "/api/auth";
|
|
487
1945
|
const staticPrefix = options.staticPrefix ?? "/";
|
|
488
1946
|
const staticDir = options.staticDir ? path.resolve(options.staticDir) : void 0;
|
|
489
|
-
const
|
|
1947
|
+
const healthPath = options.healthPath !== false ? options.healthPath ?? "/api/health" : void 0;
|
|
1948
|
+
const authOptions = wrapAuthWithServiceManager(
|
|
1949
|
+
wrapAuthWithAutoProviders(options),
|
|
1950
|
+
options.serviceManager
|
|
1951
|
+
);
|
|
1952
|
+
const chatHandler = createChatHandler(runtime, {
|
|
490
1953
|
prefix: chatPrefix,
|
|
1954
|
+
providerStore: options.providers?.providerStore,
|
|
1955
|
+
hooks: options.hooks,
|
|
491
1956
|
...options.chatHandlerOptions
|
|
492
1957
|
});
|
|
493
|
-
const authHandler =
|
|
1958
|
+
const authHandler = authOptions ? createAuthHandler(authOptions) : void 0;
|
|
494
1959
|
const cors = options.cors !== false ? corsMiddleware(options.cors) : void 0;
|
|
495
1960
|
return async (req, res) => {
|
|
496
1961
|
const url = req.url || "/";
|
|
@@ -506,6 +1971,10 @@ function createChatServer(options) {
|
|
|
506
1971
|
return;
|
|
507
1972
|
}
|
|
508
1973
|
}
|
|
1974
|
+
if (healthPath && urlPath === healthPath) {
|
|
1975
|
+
json(res, { ok: true }, 200);
|
|
1976
|
+
return;
|
|
1977
|
+
}
|
|
509
1978
|
if (urlPath.startsWith(chatPrefix + "/") || urlPath === chatPrefix) {
|
|
510
1979
|
await chatHandler(req, res);
|
|
511
1980
|
return;
|
|
@@ -542,13 +2011,51 @@ function createChatServer(options) {
|
|
|
542
2011
|
} catch {
|
|
543
2012
|
}
|
|
544
2013
|
}
|
|
545
|
-
|
|
2014
|
+
json(res, { error: "Not found" }, 404);
|
|
546
2015
|
};
|
|
547
2016
|
}
|
|
548
|
-
function
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
2017
|
+
function wrapAuthWithAutoProviders(options) {
|
|
2018
|
+
if (!options.auth) return void 0;
|
|
2019
|
+
if (!options.autoCreateProviders || !options.providers) return options.auth;
|
|
2020
|
+
const providerStore = options.providers.providerStore;
|
|
2021
|
+
const modelMap = typeof options.autoCreateProviders === "object" ? options.autoCreateProviders : DEFAULT_PROVIDER_MODELS;
|
|
2022
|
+
const userOnAuth = options.auth.onAuth;
|
|
2023
|
+
const wrappedOnAuth = async (backend, token) => {
|
|
2024
|
+
if (userOnAuth) await userOnAuth(backend, token);
|
|
2025
|
+
try {
|
|
2026
|
+
const existing = await providerStore.list();
|
|
2027
|
+
const hasBackend = existing.some((p) => p.backend === backend);
|
|
2028
|
+
if (!hasBackend) {
|
|
2029
|
+
const model = modelMap[backend] ?? "default";
|
|
2030
|
+
const label = `${backend.charAt(0).toUpperCase() + backend.slice(1)} ${model}`;
|
|
2031
|
+
await providerStore.create({
|
|
2032
|
+
id: crypto.randomUUID(),
|
|
2033
|
+
backend,
|
|
2034
|
+
model,
|
|
2035
|
+
label,
|
|
2036
|
+
createdAt: Date.now()
|
|
2037
|
+
});
|
|
2038
|
+
}
|
|
2039
|
+
} catch {
|
|
2040
|
+
}
|
|
2041
|
+
};
|
|
2042
|
+
return { ...options.auth, onAuth: wrappedOnAuth };
|
|
2043
|
+
}
|
|
2044
|
+
function wrapAuthWithServiceManager(authOptions, serviceManager) {
|
|
2045
|
+
if (!authOptions || !serviceManager) return authOptions;
|
|
2046
|
+
const userOnAuth = authOptions.onAuth;
|
|
2047
|
+
const userOnLogout = authOptions.onLogout;
|
|
2048
|
+
return {
|
|
2049
|
+
...authOptions,
|
|
2050
|
+
onAuth: async (backend, token) => {
|
|
2051
|
+
if (userOnAuth) await userOnAuth(backend, token);
|
|
2052
|
+
await serviceManager.handleAuth(backend, token);
|
|
2053
|
+
},
|
|
2054
|
+
onLogout: async () => {
|
|
2055
|
+
if (userOnLogout) await userOnLogout();
|
|
2056
|
+
await serviceManager.handleLogout();
|
|
2057
|
+
}
|
|
2058
|
+
};
|
|
552
2059
|
}
|
|
553
2060
|
var InMemoryTokenStore = class {
|
|
554
2061
|
tokens = /* @__PURE__ */ new Map();
|
|
@@ -611,7 +2118,521 @@ var FileTokenStore = class {
|
|
|
611
2118
|
return join(this.dir, `${provider}-token.json`);
|
|
612
2119
|
}
|
|
613
2120
|
};
|
|
2121
|
+
function createProviderHandler(options) {
|
|
2122
|
+
const { providerStore } = options;
|
|
2123
|
+
return async (req, res) => {
|
|
2124
|
+
const url = req.url || "";
|
|
2125
|
+
const method = req.method || "GET";
|
|
2126
|
+
const path2 = url.split("?")[0];
|
|
2127
|
+
const idMatch = path2.match(/^\/providers\/([^/]+)$/);
|
|
2128
|
+
try {
|
|
2129
|
+
if (method === "GET" && path2 === "/providers") {
|
|
2130
|
+
const providers = await providerStore.list();
|
|
2131
|
+
json(res, providers);
|
|
2132
|
+
return;
|
|
2133
|
+
}
|
|
2134
|
+
if (method === "GET" && idMatch) {
|
|
2135
|
+
const id = decodeURIComponent(idMatch[1]);
|
|
2136
|
+
const provider = await providerStore.get(id);
|
|
2137
|
+
if (!provider) {
|
|
2138
|
+
json(res, { error: "Provider not found" }, 404);
|
|
2139
|
+
return;
|
|
2140
|
+
}
|
|
2141
|
+
json(res, provider);
|
|
2142
|
+
return;
|
|
2143
|
+
}
|
|
2144
|
+
if (method === "POST" && path2 === "/providers") {
|
|
2145
|
+
const body = await readBody(req);
|
|
2146
|
+
const backend = body.backend;
|
|
2147
|
+
const model = body.model;
|
|
2148
|
+
const label = body.label;
|
|
2149
|
+
if (!backend || typeof backend !== "string") {
|
|
2150
|
+
json(res, { error: "backend is required" }, 400);
|
|
2151
|
+
return;
|
|
2152
|
+
}
|
|
2153
|
+
if (!model || typeof model !== "string") {
|
|
2154
|
+
json(res, { error: "model is required" }, 400);
|
|
2155
|
+
return;
|
|
2156
|
+
}
|
|
2157
|
+
if (!label || typeof label !== "string") {
|
|
2158
|
+
json(res, { error: "label is required" }, 400);
|
|
2159
|
+
return;
|
|
2160
|
+
}
|
|
2161
|
+
const config = {
|
|
2162
|
+
id: randomUUID(),
|
|
2163
|
+
backend,
|
|
2164
|
+
model,
|
|
2165
|
+
label,
|
|
2166
|
+
createdAt: Date.now()
|
|
2167
|
+
};
|
|
2168
|
+
await providerStore.create(config);
|
|
2169
|
+
json(res, config, 201);
|
|
2170
|
+
return;
|
|
2171
|
+
}
|
|
2172
|
+
if (method === "PUT" && idMatch) {
|
|
2173
|
+
const id = decodeURIComponent(idMatch[1]);
|
|
2174
|
+
const existing = await providerStore.get(id);
|
|
2175
|
+
if (!existing) {
|
|
2176
|
+
json(res, { error: "Provider not found" }, 404);
|
|
2177
|
+
return;
|
|
2178
|
+
}
|
|
2179
|
+
const body = await readBody(req);
|
|
2180
|
+
const changes = {};
|
|
2181
|
+
if (body.backend && typeof body.backend === "string") changes.backend = body.backend;
|
|
2182
|
+
if (body.model && typeof body.model === "string") changes.model = body.model;
|
|
2183
|
+
if (body.label && typeof body.label === "string") changes.label = body.label;
|
|
2184
|
+
await providerStore.update(id, changes);
|
|
2185
|
+
const updated = await providerStore.get(id);
|
|
2186
|
+
json(res, updated);
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
if (method === "DELETE" && idMatch) {
|
|
2190
|
+
const id = decodeURIComponent(idMatch[1]);
|
|
2191
|
+
await providerStore.delete(id);
|
|
2192
|
+
json(res, { ok: true });
|
|
2193
|
+
return;
|
|
2194
|
+
}
|
|
2195
|
+
json(res, { error: "Not found" }, 404);
|
|
2196
|
+
} catch (err) {
|
|
2197
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
2198
|
+
json(res, { error: message }, 500);
|
|
2199
|
+
}
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
var InMemoryProviderStore = class {
|
|
2203
|
+
providers = /* @__PURE__ */ new Map();
|
|
2204
|
+
async create(config) {
|
|
2205
|
+
const id = config.id || randomUUID();
|
|
2206
|
+
this.providers.set(id, { ...config, id });
|
|
2207
|
+
}
|
|
2208
|
+
async get(id) {
|
|
2209
|
+
const p = this.providers.get(id);
|
|
2210
|
+
return p ? { ...p } : null;
|
|
2211
|
+
}
|
|
2212
|
+
async update(id, changes) {
|
|
2213
|
+
const existing = this.providers.get(id);
|
|
2214
|
+
if (!existing) {
|
|
2215
|
+
throw new Error(`Provider "${id}" not found`);
|
|
2216
|
+
}
|
|
2217
|
+
this.providers.set(id, { ...existing, ...changes, id: existing.id, createdAt: existing.createdAt });
|
|
2218
|
+
}
|
|
2219
|
+
async delete(id) {
|
|
2220
|
+
this.providers.delete(id);
|
|
2221
|
+
}
|
|
2222
|
+
async list() {
|
|
2223
|
+
return [...this.providers.values()].map((p) => ({ ...p }));
|
|
2224
|
+
}
|
|
2225
|
+
};
|
|
2226
|
+
var FileProviderStore = class {
|
|
2227
|
+
dir;
|
|
2228
|
+
constructor(options) {
|
|
2229
|
+
this.dir = options.directory;
|
|
2230
|
+
}
|
|
2231
|
+
async create(config) {
|
|
2232
|
+
const id = config.id || randomUUID();
|
|
2233
|
+
const data = { ...config, id };
|
|
2234
|
+
mkdirSync(this.dir, { recursive: true });
|
|
2235
|
+
writeFileSync(this.filePath(id), JSON.stringify(data));
|
|
2236
|
+
}
|
|
2237
|
+
async get(id) {
|
|
2238
|
+
try {
|
|
2239
|
+
const data = readFileSync(this.filePath(id), "utf-8");
|
|
2240
|
+
return JSON.parse(data);
|
|
2241
|
+
} catch {
|
|
2242
|
+
return null;
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
async update(id, changes) {
|
|
2246
|
+
const existing = await this.get(id);
|
|
2247
|
+
if (!existing) {
|
|
2248
|
+
throw new Error(`Provider "${id}" not found`);
|
|
2249
|
+
}
|
|
2250
|
+
const updated = { ...existing, ...changes, id: existing.id, createdAt: existing.createdAt };
|
|
2251
|
+
writeFileSync(this.filePath(id), JSON.stringify(updated));
|
|
2252
|
+
}
|
|
2253
|
+
async delete(id) {
|
|
2254
|
+
try {
|
|
2255
|
+
unlinkSync(this.filePath(id));
|
|
2256
|
+
} catch {
|
|
2257
|
+
}
|
|
2258
|
+
}
|
|
2259
|
+
async list() {
|
|
2260
|
+
if (!existsSync(this.dir)) return [];
|
|
2261
|
+
return readdirSync(this.dir).filter((f) => f.endsWith("-provider.json")).map((f) => {
|
|
2262
|
+
try {
|
|
2263
|
+
const data = readFileSync(join(this.dir, f), "utf-8");
|
|
2264
|
+
return JSON.parse(data);
|
|
2265
|
+
} catch {
|
|
2266
|
+
return null;
|
|
2267
|
+
}
|
|
2268
|
+
}).filter((p) => p !== null);
|
|
2269
|
+
}
|
|
2270
|
+
filePath(id) {
|
|
2271
|
+
return join(this.dir, `${id}-provider.json`);
|
|
2272
|
+
}
|
|
2273
|
+
};
|
|
2274
|
+
|
|
2275
|
+
// src/auth/refresh-manager.ts
|
|
2276
|
+
var TokenRefreshManager = class {
|
|
2277
|
+
currentToken;
|
|
2278
|
+
refreshFn;
|
|
2279
|
+
threshold;
|
|
2280
|
+
maxRetries;
|
|
2281
|
+
retryDelayMs;
|
|
2282
|
+
minDelayMs;
|
|
2283
|
+
timerId = null;
|
|
2284
|
+
running = false;
|
|
2285
|
+
disposed = false;
|
|
2286
|
+
listeners = {
|
|
2287
|
+
refreshed: /* @__PURE__ */ new Set(),
|
|
2288
|
+
error: /* @__PURE__ */ new Set(),
|
|
2289
|
+
expired: /* @__PURE__ */ new Set(),
|
|
2290
|
+
disposed: /* @__PURE__ */ new Set()
|
|
2291
|
+
};
|
|
2292
|
+
constructor(options) {
|
|
2293
|
+
this.currentToken = { ...options.token };
|
|
2294
|
+
this.refreshFn = options.refresh;
|
|
2295
|
+
this.threshold = options.refreshThreshold ?? 0.8;
|
|
2296
|
+
this.maxRetries = options.maxRetries ?? 3;
|
|
2297
|
+
this.retryDelayMs = options.retryDelayMs ?? 1e3;
|
|
2298
|
+
this.minDelayMs = options.minDelayMs ?? 1e3;
|
|
2299
|
+
}
|
|
2300
|
+
/** Register an event listener */
|
|
2301
|
+
on(event, listener) {
|
|
2302
|
+
this.listeners[event].add(listener);
|
|
2303
|
+
return this;
|
|
2304
|
+
}
|
|
2305
|
+
/** Remove an event listener */
|
|
2306
|
+
off(event, listener) {
|
|
2307
|
+
this.listeners[event].delete(listener);
|
|
2308
|
+
return this;
|
|
2309
|
+
}
|
|
2310
|
+
/** Current token managed by this instance */
|
|
2311
|
+
get token() {
|
|
2312
|
+
return { ...this.currentToken };
|
|
2313
|
+
}
|
|
2314
|
+
/** Whether the manager is currently running */
|
|
2315
|
+
get isRunning() {
|
|
2316
|
+
return this.running;
|
|
2317
|
+
}
|
|
2318
|
+
/** Whether the manager has been disposed */
|
|
2319
|
+
get isDisposed() {
|
|
2320
|
+
return this.disposed;
|
|
2321
|
+
}
|
|
2322
|
+
/**
|
|
2323
|
+
* Start automatic refresh scheduling.
|
|
2324
|
+
* If the token is already expired, emits "expired" immediately.
|
|
2325
|
+
* If the token has no expiresIn, does nothing (long-lived token).
|
|
2326
|
+
*/
|
|
2327
|
+
start() {
|
|
2328
|
+
if (this.disposed) return;
|
|
2329
|
+
if (this.running) return;
|
|
2330
|
+
this.running = true;
|
|
2331
|
+
this.schedule();
|
|
2332
|
+
}
|
|
2333
|
+
/** Stop automatic refresh (can be restarted with start()) */
|
|
2334
|
+
stop() {
|
|
2335
|
+
this.running = false;
|
|
2336
|
+
this.clearTimer();
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* Update the managed token (e.g. after manual refresh).
|
|
2340
|
+
* Reschedules automatic refresh if running.
|
|
2341
|
+
*/
|
|
2342
|
+
updateToken(token) {
|
|
2343
|
+
if (this.disposed) return;
|
|
2344
|
+
this.currentToken = { ...token };
|
|
2345
|
+
if (this.running) {
|
|
2346
|
+
this.clearTimer();
|
|
2347
|
+
this.schedule();
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
/** Stop and clean up all resources */
|
|
2351
|
+
dispose() {
|
|
2352
|
+
if (this.disposed) return;
|
|
2353
|
+
this.stop();
|
|
2354
|
+
this.disposed = true;
|
|
2355
|
+
this.emit("disposed");
|
|
2356
|
+
for (const set of Object.values(this.listeners)) {
|
|
2357
|
+
set.clear();
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
// ─── Private ──────────────────────────────────────────────────
|
|
2361
|
+
schedule() {
|
|
2362
|
+
if (!this.running || this.disposed) return;
|
|
2363
|
+
const delayMs = this.computeRefreshDelay();
|
|
2364
|
+
if (delayMs === null) {
|
|
2365
|
+
return;
|
|
2366
|
+
}
|
|
2367
|
+
if (delayMs <= 0) {
|
|
2368
|
+
this.timerId = setTimeout(() => {
|
|
2369
|
+
this.timerId = null;
|
|
2370
|
+
if (!this.running || this.disposed) return;
|
|
2371
|
+
void this.performRefresh();
|
|
2372
|
+
}, 0);
|
|
2373
|
+
return;
|
|
2374
|
+
}
|
|
2375
|
+
this.timerId = setTimeout(() => {
|
|
2376
|
+
this.timerId = null;
|
|
2377
|
+
if (!this.running || this.disposed) return;
|
|
2378
|
+
void this.performRefresh();
|
|
2379
|
+
}, Math.max(delayMs, this.minDelayMs));
|
|
2380
|
+
}
|
|
2381
|
+
async performRefresh(attempt = 1) {
|
|
2382
|
+
if (!this.running || this.disposed) return;
|
|
2383
|
+
try {
|
|
2384
|
+
const newToken = await this.refreshFn(this.currentToken);
|
|
2385
|
+
if (!this.running || this.disposed) return;
|
|
2386
|
+
this.currentToken = { ...newToken };
|
|
2387
|
+
this.emit("refreshed", newToken);
|
|
2388
|
+
this.schedule();
|
|
2389
|
+
} catch (err) {
|
|
2390
|
+
if (!this.running || this.disposed) return;
|
|
2391
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
2392
|
+
this.emit("error", error, attempt);
|
|
2393
|
+
if (attempt < this.maxRetries) {
|
|
2394
|
+
const delay2 = this.retryDelayMs * Math.pow(2, attempt - 1);
|
|
2395
|
+
this.timerId = setTimeout(() => {
|
|
2396
|
+
this.timerId = null;
|
|
2397
|
+
if (!this.running || this.disposed) return;
|
|
2398
|
+
void this.performRefresh(attempt + 1);
|
|
2399
|
+
}, delay2);
|
|
2400
|
+
} else {
|
|
2401
|
+
if (this.isTokenExpired()) {
|
|
2402
|
+
this.running = false;
|
|
2403
|
+
this.emit("expired");
|
|
2404
|
+
} else {
|
|
2405
|
+
const expiresIn = this.currentToken.expiresIn;
|
|
2406
|
+
if (expiresIn == null) return;
|
|
2407
|
+
const expiresAt = this.currentToken.obtainedAt + expiresIn * 1e3;
|
|
2408
|
+
const waitMs = Math.max(expiresAt - Date.now(), this.minDelayMs);
|
|
2409
|
+
this.timerId = setTimeout(() => {
|
|
2410
|
+
this.timerId = null;
|
|
2411
|
+
if (!this.running || this.disposed) return;
|
|
2412
|
+
void this.performRefresh();
|
|
2413
|
+
}, waitMs);
|
|
2414
|
+
}
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
computeRefreshDelay() {
|
|
2419
|
+
if (this.currentToken.expiresIn == null) return null;
|
|
2420
|
+
const lifetimeMs = this.currentToken.expiresIn * 1e3;
|
|
2421
|
+
const refreshAtMs = this.currentToken.obtainedAt + lifetimeMs * this.threshold;
|
|
2422
|
+
const now = Date.now();
|
|
2423
|
+
const delay2 = refreshAtMs - now;
|
|
2424
|
+
return delay2;
|
|
2425
|
+
}
|
|
2426
|
+
isTokenExpired() {
|
|
2427
|
+
if (this.currentToken.expiresIn == null) return false;
|
|
2428
|
+
const expiresAt = this.currentToken.obtainedAt + this.currentToken.expiresIn * 1e3;
|
|
2429
|
+
return Date.now() >= expiresAt;
|
|
2430
|
+
}
|
|
2431
|
+
clearTimer() {
|
|
2432
|
+
if (this.timerId !== null) {
|
|
2433
|
+
clearTimeout(this.timerId);
|
|
2434
|
+
this.timerId = null;
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
emit(event, ...args) {
|
|
2438
|
+
for (const listener of this.listeners[event]) {
|
|
2439
|
+
try {
|
|
2440
|
+
listener(...args);
|
|
2441
|
+
} catch {
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
};
|
|
2446
|
+
|
|
2447
|
+
// src/chat/server/service-manager.ts
|
|
2448
|
+
var ServiceManager = class {
|
|
2449
|
+
_services = /* @__PURE__ */ new Map();
|
|
2450
|
+
_refreshManagers = /* @__PURE__ */ new Map();
|
|
2451
|
+
_options;
|
|
2452
|
+
constructor(options) {
|
|
2453
|
+
this._options = options;
|
|
2454
|
+
}
|
|
2455
|
+
/**
|
|
2456
|
+
* Handle auth event: dispose old service (if any) and create new one.
|
|
2457
|
+
* If the token is refreshable and refreshFactory is configured, starts a
|
|
2458
|
+
* TokenRefreshManager that auto-refreshes and recreates the service.
|
|
2459
|
+
*/
|
|
2460
|
+
async handleAuth(backend, token) {
|
|
2461
|
+
this._stopRefreshManager(backend);
|
|
2462
|
+
const old = this._services.get(backend);
|
|
2463
|
+
if (old) {
|
|
2464
|
+
try {
|
|
2465
|
+
await old.dispose();
|
|
2466
|
+
} catch {
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
const service = await this._options.createService(backend, token);
|
|
2470
|
+
this._services.set(backend, service);
|
|
2471
|
+
this._startRefreshManager(backend, token);
|
|
2472
|
+
return service;
|
|
2473
|
+
}
|
|
2474
|
+
/**
|
|
2475
|
+
* Handle logout: dispose all services, stop all refresh managers, clear cache.
|
|
2476
|
+
*/
|
|
2477
|
+
async handleLogout() {
|
|
2478
|
+
for (const backend of [...this._refreshManagers.keys()]) {
|
|
2479
|
+
this._stopRefreshManager(backend);
|
|
2480
|
+
}
|
|
2481
|
+
for (const [, service] of this._services) {
|
|
2482
|
+
try {
|
|
2483
|
+
await service.dispose();
|
|
2484
|
+
} catch {
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
this._services.clear();
|
|
2488
|
+
}
|
|
2489
|
+
/**
|
|
2490
|
+
* Dispose the ServiceManager — stops all refresh managers and disposes all services.
|
|
2491
|
+
*/
|
|
2492
|
+
async dispose() {
|
|
2493
|
+
await this.handleLogout();
|
|
2494
|
+
}
|
|
2495
|
+
/** Get cached service for a backend (undefined if not authenticated). */
|
|
2496
|
+
getService(backend) {
|
|
2497
|
+
return this._services.get(backend);
|
|
2498
|
+
}
|
|
2499
|
+
/** Check if a service exists for the given backend. */
|
|
2500
|
+
hasService(backend) {
|
|
2501
|
+
return this._services.has(backend);
|
|
2502
|
+
}
|
|
2503
|
+
/** Get all backend names with active services. */
|
|
2504
|
+
get activeBackends() {
|
|
2505
|
+
return [...this._services.keys()];
|
|
2506
|
+
}
|
|
2507
|
+
/** Get active refresh manager for a backend (for testing/introspection). */
|
|
2508
|
+
getRefreshManager(backend) {
|
|
2509
|
+
return this._refreshManagers.get(backend);
|
|
2510
|
+
}
|
|
2511
|
+
// ── Private ─────────────────────────────────────────────────
|
|
2512
|
+
_startRefreshManager(backend, token) {
|
|
2513
|
+
if (!this._options.refreshFactory) return;
|
|
2514
|
+
if (token.expiresIn == null) return;
|
|
2515
|
+
const refreshFn = this._options.refreshFactory(backend);
|
|
2516
|
+
if (!refreshFn) return;
|
|
2517
|
+
const manager = new TokenRefreshManager({
|
|
2518
|
+
token,
|
|
2519
|
+
refresh: refreshFn,
|
|
2520
|
+
refreshThreshold: this._options.refreshOptions?.refreshThreshold,
|
|
2521
|
+
maxRetries: this._options.refreshOptions?.maxRetries,
|
|
2522
|
+
retryDelayMs: this._options.refreshOptions?.retryDelayMs
|
|
2523
|
+
});
|
|
2524
|
+
manager.on("refreshed", (newToken) => {
|
|
2525
|
+
void this._recreateService(backend, newToken);
|
|
2526
|
+
});
|
|
2527
|
+
manager.on("expired", () => {
|
|
2528
|
+
this._options.onTokenExpired?.(backend);
|
|
2529
|
+
void this._logoutBackend(backend);
|
|
2530
|
+
});
|
|
2531
|
+
this._refreshManagers.set(backend, manager);
|
|
2532
|
+
manager.start();
|
|
2533
|
+
}
|
|
2534
|
+
_stopRefreshManager(backend) {
|
|
2535
|
+
const manager = this._refreshManagers.get(backend);
|
|
2536
|
+
if (manager) {
|
|
2537
|
+
manager.dispose();
|
|
2538
|
+
this._refreshManagers.delete(backend);
|
|
2539
|
+
}
|
|
2540
|
+
}
|
|
2541
|
+
async _recreateService(backend, token) {
|
|
2542
|
+
const old = this._services.get(backend);
|
|
2543
|
+
if (old) {
|
|
2544
|
+
try {
|
|
2545
|
+
await old.dispose();
|
|
2546
|
+
} catch {
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
try {
|
|
2550
|
+
const service = await this._options.createService(backend, token);
|
|
2551
|
+
this._services.set(backend, service);
|
|
2552
|
+
} catch {
|
|
2553
|
+
this._services.delete(backend);
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
async _logoutBackend(backend) {
|
|
2557
|
+
this._stopRefreshManager(backend);
|
|
2558
|
+
const service = this._services.get(backend);
|
|
2559
|
+
if (service) {
|
|
2560
|
+
try {
|
|
2561
|
+
await service.dispose();
|
|
2562
|
+
} catch {
|
|
2563
|
+
}
|
|
2564
|
+
this._services.delete(backend);
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
};
|
|
2568
|
+
|
|
2569
|
+
// src/chat/server/adapter-pool.ts
|
|
2570
|
+
var AdapterPool = class {
|
|
2571
|
+
_cached = /* @__PURE__ */ new Map();
|
|
2572
|
+
_pending = /* @__PURE__ */ new Map();
|
|
2573
|
+
_factory;
|
|
2574
|
+
_disposed = false;
|
|
2575
|
+
constructor(options) {
|
|
2576
|
+
this._factory = options.factory;
|
|
2577
|
+
}
|
|
2578
|
+
/**
|
|
2579
|
+
* Get or create an adapter for the given backend.
|
|
2580
|
+
* Concurrent calls for the same backend share one creation promise.
|
|
2581
|
+
* Failed creations are NOT cached — next call retries.
|
|
2582
|
+
*/
|
|
2583
|
+
async getAdapter(backend) {
|
|
2584
|
+
if (this._disposed) {
|
|
2585
|
+
throw new Error("AdapterPool is disposed");
|
|
2586
|
+
}
|
|
2587
|
+
const cached = this._cached.get(backend);
|
|
2588
|
+
if (cached) return cached;
|
|
2589
|
+
const pending = this._pending.get(backend);
|
|
2590
|
+
if (pending) return pending;
|
|
2591
|
+
const promise = this._create(backend);
|
|
2592
|
+
this._pending.set(backend, promise);
|
|
2593
|
+
try {
|
|
2594
|
+
const adapter = await promise;
|
|
2595
|
+
this._cached.set(backend, adapter);
|
|
2596
|
+
return adapter;
|
|
2597
|
+
} finally {
|
|
2598
|
+
this._pending.delete(backend);
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
/**
|
|
2602
|
+
* Evict (dispose and remove) the cached adapter for a backend.
|
|
2603
|
+
* Use after token rotation to force re-creation on next getAdapter().
|
|
2604
|
+
*/
|
|
2605
|
+
async evict(backend) {
|
|
2606
|
+
const cached = this._cached.get(backend);
|
|
2607
|
+
if (cached) {
|
|
2608
|
+
this._cached.delete(backend);
|
|
2609
|
+
try {
|
|
2610
|
+
await cached.dispose();
|
|
2611
|
+
} catch {
|
|
2612
|
+
}
|
|
2613
|
+
}
|
|
2614
|
+
}
|
|
2615
|
+
/** Check if a backend has a cached adapter. */
|
|
2616
|
+
has(backend) {
|
|
2617
|
+
return this._cached.has(backend);
|
|
2618
|
+
}
|
|
2619
|
+
/** Get all backend names with cached adapters. */
|
|
2620
|
+
get activeBackends() {
|
|
2621
|
+
return [...this._cached.keys()];
|
|
2622
|
+
}
|
|
2623
|
+
/** Dispose all cached adapters and mark pool as unusable. */
|
|
2624
|
+
async dispose() {
|
|
2625
|
+
this._disposed = true;
|
|
2626
|
+
const backends = [...this._cached.keys()];
|
|
2627
|
+
for (const backend of backends) {
|
|
2628
|
+
await this.evict(backend);
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
async _create(backend) {
|
|
2632
|
+
return this._factory(backend);
|
|
2633
|
+
}
|
|
2634
|
+
};
|
|
614
2635
|
|
|
615
|
-
export { FileTokenStore, InMemoryTokenStore, corsMiddleware, createAuthHandler, createChatHandler, createChatServer };
|
|
2636
|
+
export { AdapterPool, BodyParseError, DEFAULT_PROVIDER_MODELS, FileProviderStore, FileTokenStore, InMemoryProviderStore, InMemoryTokenStore, ServiceManager, configRoutes, corsMiddleware, createAuthHandler, createChatHandler, createChatServer, createProviderHandler, json, messageRoutes, providerRoutes, readBody, resolveRequestContext, sessionRoutes };
|
|
616
2637
|
//# sourceMappingURL=server.js.map
|
|
617
2638
|
//# sourceMappingURL=server.js.map
|