@agentskit/chat-server 0.1.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AgentsKit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @agentskit/chat-server
2
+
3
+ Web-standard request handler for AgentsKit Chat definitions. It composes the canonical AgentsKit controller and memory with AgentsKit Chat protocol/session contracts.
4
+
5
+ ```ts
6
+ const POST = createChatHandler({
7
+ authenticate: async request => ({ ok: true, context: await authenticate(request) }),
8
+ resolveDefinition: context => chats.forTenant(context.tenantId),
9
+ sessionStorage: context => sessions.forTenant(context.tenantId),
10
+ })
11
+ ```
12
+
13
+ The returned function accepts a standard `Request` and returns a standard streaming `Response`.
package/dist/index.cjs ADDED
@@ -0,0 +1,249 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ChatHandlerError: () => ChatHandlerError,
24
+ createChatHandler: () => createChatHandler
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_core = require("@agentskit/core");
28
+ var import_chat = require("@agentskit/chat");
29
+ var import_chat_protocol = require("@agentskit/chat-protocol");
30
+ var ChatHandlerError = class extends Error {
31
+ status;
32
+ code;
33
+ retryable;
34
+ constructor(options) {
35
+ super(options.message);
36
+ this.name = "ChatHandlerError";
37
+ this.status = options.status;
38
+ this.code = options.code;
39
+ this.retryable = options.retryable ?? false;
40
+ }
41
+ };
42
+ var encoder = new TextEncoder();
43
+ var decoder = new TextDecoder();
44
+ var json = (diagnostic, status) => new Response(JSON.stringify({ error: diagnostic }), {
45
+ status,
46
+ headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }
47
+ });
48
+ var safeError = (error) => error instanceof ChatHandlerError ? { status: error.status, diagnostic: { version: 1, code: error.code, message: error.message, retryable: error.retryable } } : error instanceof import_chat.SessionConflictError ? { status: 409, diagnostic: { version: 1, code: "SESSION_CONFLICT", message: "Another turn is active for this session.", retryable: true } } : { status: 500, diagnostic: { version: 1, code: "SERVER_INTERNAL", message: "The chat request failed.", retryable: true } };
49
+ var fail = (status, code, message, retryable = false) => {
50
+ throw new ChatHandlerError({ status, code, message, retryable });
51
+ };
52
+ var withSignal = async (operation, signal) => {
53
+ if (signal.aborted) throw signal.reason;
54
+ let rejectAbort;
55
+ const abort = () => rejectAbort?.(signal.reason);
56
+ const aborted = new Promise((_resolve, reject) => {
57
+ rejectAbort = reject;
58
+ });
59
+ signal.addEventListener("abort", abort, { once: true });
60
+ try {
61
+ return await Promise.race([Promise.resolve(operation), aborted]);
62
+ } finally {
63
+ signal.removeEventListener("abort", abort);
64
+ }
65
+ };
66
+ var readBody = async (request, maxBodyBytes, signal) => {
67
+ const declared = Number(request.headers.get("content-length"));
68
+ if (Number.isFinite(declared) && declared > maxBodyBytes) fail(413, "REQUEST_TOO_LARGE", "Request body is too large.");
69
+ const reader = request.body?.getReader();
70
+ if (!reader) return fail(400, "REQUEST_INVALID_JSON", "Request body is not valid JSON.");
71
+ const chunks = [];
72
+ let size = 0;
73
+ try {
74
+ while (true) {
75
+ const result = await withSignal(reader.read(), signal);
76
+ if (result.done) break;
77
+ size += result.value.byteLength;
78
+ if (size > maxBodyBytes) {
79
+ await reader.cancel();
80
+ return fail(413, "REQUEST_TOO_LARGE", "Request body is too large.");
81
+ }
82
+ chunks.push(result.value);
83
+ }
84
+ } finally {
85
+ reader.releaseLock();
86
+ }
87
+ const bytes = new Uint8Array(size);
88
+ let offset = 0;
89
+ for (const chunk of chunks) {
90
+ bytes.set(chunk, offset);
91
+ offset += chunk.byteLength;
92
+ }
93
+ try {
94
+ return JSON.parse(decoder.decode(bytes));
95
+ } catch {
96
+ return fail(400, "REQUEST_INVALID_JSON", "Request body is not valid JSON.");
97
+ }
98
+ };
99
+ var snapshotStatus = (state) => state.status === "error" ? "error" : state.status === "streaming" ? "streaming" : state.messages.length === 0 ? "idle" : "complete";
100
+ var createChatHandler = (options) => {
101
+ const timeoutMs = options.timeoutMs ?? 3e4;
102
+ const cleanupTimeoutMs = options.cleanupTimeoutMs ?? 5e3;
103
+ const maxBodyBytes = options.maxBodyBytes ?? 64 * 1024;
104
+ if (![timeoutMs, cleanupTimeoutMs, maxBodyBytes].every((value) => Number.isSafeInteger(value) && value > 0)) fail(500, "SERVER_INVALID_CONFIG", "Chat handler configuration is invalid.");
105
+ const leaseMs = timeoutMs + 3 * cleanupTimeoutMs;
106
+ if (!Number.isSafeInteger(leaseMs)) fail(500, "SERVER_INVALID_CONFIG", "Chat handler configuration is invalid.");
107
+ const createId = options.createId ?? (() => crypto.randomUUID());
108
+ return async (request) => {
109
+ const deadline = AbortSignal.timeout(timeoutMs);
110
+ const signal = AbortSignal.any([request.signal, deadline]);
111
+ try {
112
+ if (request.method !== "POST") fail(405, "REQUEST_METHOD_NOT_ALLOWED", "Only POST is supported.");
113
+ if (!request.headers.get("content-type")?.toLowerCase().startsWith("application/json")) fail(415, "REQUEST_UNSUPPORTED_MEDIA_TYPE", "Content-Type must be application/json.");
114
+ let context;
115
+ if (options.authenticate) {
116
+ const authenticated = await withSignal(options.authenticate(request, signal), signal);
117
+ if (!authenticated.ok) return authenticated.response;
118
+ context = authenticated.context;
119
+ }
120
+ const decoded = (0, import_chat_protocol.decodeTurnEvent)(await readBody(request, maxBodyBytes, signal));
121
+ if (!decoded.ok || decoded.event.event !== "client.turn.submit") return fail(400, "REQUEST_INVALID_EVENT", "Request body must be a valid turn submission.");
122
+ const submission = decoded.event;
123
+ const definition = await withSignal(options.resolveDefinition(context, submission.sessionId, signal), signal);
124
+ const storage = options.sessionStorage(context, signal);
125
+ const session = await withSignal((0, import_chat.resumeChatSession)(definition, { sessionId: submission.sessionId, storage, signal, ...options.now ? { now: options.now } : {} }), signal);
126
+ if (!await withSignal(session.claimTurn(submission.turnId, leaseMs, signal), signal)) return json({ version: 1, code: "SESSION_BUSY", message: "Another turn is active for this session.", retryable: true }, 409);
127
+ const memory = definition.chat.memory;
128
+ const loaded = memory ? await withSignal(memory.load({ signal }), signal) : definition.chat.initialMessages ?? [];
129
+ const messages = loaded.length > 0 ? loaded : definition.chat.initialMessages ?? [];
130
+ const { memory: _memory, ...chat } = definition.chat;
131
+ const controller = (0, import_core.createChatController)(session.updateChat({ ...chat, initialMessages: [...messages] }));
132
+ let pending;
133
+ let wake;
134
+ let done = false;
135
+ let closed = false;
136
+ const push = () => {
137
+ const state = controller.getState();
138
+ pending = { ...state, messages: [...state.messages], usage: { ...state.usage } };
139
+ wake?.();
140
+ wake = void 0;
141
+ };
142
+ const unsubscribe = controller.subscribe(push);
143
+ let cleanup = async () => void 0;
144
+ const abort = () => {
145
+ controller.stop();
146
+ void cleanup(true).catch(() => void 0);
147
+ };
148
+ signal.addEventListener("abort", abort, { once: true });
149
+ const send = controller.send(submission.payload.input).finally(() => {
150
+ done = true;
151
+ wake?.();
152
+ wake = void 0;
153
+ });
154
+ cleanup = async (stop) => {
155
+ if (closed) return;
156
+ closed = true;
157
+ unsubscribe();
158
+ signal.removeEventListener("abort", abort);
159
+ if (stop && !signal.aborted) controller.stop();
160
+ const settleSignal = AbortSignal.timeout(cleanupTimeoutMs);
161
+ await withSignal(send.catch(() => void 0), settleSignal).catch(() => void 0);
162
+ const saveSignal = AbortSignal.timeout(cleanupTimeoutMs);
163
+ let outcome = "completed";
164
+ try {
165
+ await withSignal(memory?.save(controller.getState().messages, { signal: saveSignal }), saveSignal);
166
+ } catch (error) {
167
+ outcome = "indeterminate";
168
+ throw error;
169
+ } finally {
170
+ const releaseSignal = AbortSignal.timeout(cleanupTimeoutMs);
171
+ await withSignal(session.releaseTurn(submission.turnId, outcome, releaseSignal), releaseSignal);
172
+ }
173
+ };
174
+ const diagnosticLine = (code, message) => {
175
+ const event = import_chat_protocol.TurnEventSchema.parse({
176
+ protocol: "agentskit.chat.turn",
177
+ version: 1,
178
+ eventId: createId(),
179
+ sessionId: submission.sessionId,
180
+ turnId: submission.turnId,
181
+ sequence: session.getCursor() + 1,
182
+ emittedAt: (options.now?.() ?? /* @__PURE__ */ new Date()).toISOString(),
183
+ event: "server.turn.diagnostic",
184
+ payload: { version: 1, code, message, retryable: true }
185
+ });
186
+ return encoder.encode(`${(0, import_chat_protocol.encodeTurnEvent)(event)}
187
+ `);
188
+ };
189
+ const body = new ReadableStream({
190
+ async pull(stream) {
191
+ try {
192
+ while (!pending && !done && !signal.aborted) await new Promise((resolve) => {
193
+ wake = resolve;
194
+ });
195
+ if (signal.aborted) {
196
+ stream.enqueue(diagnosticLine(deadline.aborted ? "SERVER_TIMEOUT" : "REQUEST_CANCELLED", deadline.aborted ? "The chat request timed out." : "The chat request was cancelled."));
197
+ await cleanup(true);
198
+ stream.close();
199
+ return;
200
+ }
201
+ if (pending) {
202
+ const state = pending;
203
+ pending = void 0;
204
+ await withSignal(session.persist(signal), signal);
205
+ const event = (0, import_chat_protocol.createSnapshotEvent)({
206
+ eventId: createId(),
207
+ sessionId: submission.sessionId,
208
+ turnId: submission.turnId,
209
+ sequence: session.getCursor(),
210
+ emittedAt: (options.now?.() ?? /* @__PURE__ */ new Date()).toISOString(),
211
+ messages: state.messages,
212
+ status: snapshotStatus(state),
213
+ usage: state.usage,
214
+ lineage: { operation: "submit" },
215
+ ...state.error ? { error: { version: 1, code: "CHAT_TURN_FAILED", message: "The chat turn failed.", retryable: true } } : {}
216
+ });
217
+ stream.enqueue(encoder.encode(`${(0, import_chat_protocol.encodeTurnEvent)(event)}
218
+ `));
219
+ return;
220
+ }
221
+ await cleanup(false);
222
+ stream.close();
223
+ } catch (error) {
224
+ const safe = safeError(error);
225
+ try {
226
+ stream.enqueue(diagnosticLine(safe.diagnostic.code, safe.diagnostic.message));
227
+ } catch {
228
+ }
229
+ await cleanup(true).catch(() => void 0);
230
+ stream.close();
231
+ }
232
+ },
233
+ async cancel() {
234
+ await cleanup(true).catch(() => void 0);
235
+ }
236
+ });
237
+ return new Response(body, { status: 200, headers: { "content-type": "application/x-ndjson; charset=utf-8", "cache-control": "no-store", "x-content-type-options": "nosniff" } });
238
+ } catch (error) {
239
+ if (signal.aborted) return json({ version: 1, code: deadline.aborted ? "SERVER_TIMEOUT" : "REQUEST_CANCELLED", message: deadline.aborted ? "The chat request timed out." : "The chat request was cancelled.", retryable: true }, deadline.aborted ? 408 : 499);
240
+ const safe = safeError(error);
241
+ return json(safe.diagnostic, safe.status);
242
+ }
243
+ };
244
+ };
245
+ // Annotate the CommonJS export names for ESM import in node:
246
+ 0 && (module.exports = {
247
+ ChatHandlerError,
248
+ createChatHandler
249
+ });
@@ -0,0 +1,34 @@
1
+ import { ChatDefinition, SessionStorage } from '@agentskit/chat';
2
+
3
+ type ChatHandler = (request: Request) => Promise<Response>;
4
+ type AuthenticationResult<TContext> = {
5
+ readonly ok: true;
6
+ readonly context: TContext;
7
+ } | {
8
+ readonly ok: false;
9
+ readonly response: Response;
10
+ };
11
+ interface ChatHandlerOptions<TContext = undefined> {
12
+ readonly authenticate?: (request: Request, signal: AbortSignal) => AuthenticationResult<TContext> | Promise<AuthenticationResult<TContext>>;
13
+ readonly resolveDefinition: (context: TContext | undefined, sessionId: string, signal: AbortSignal) => ChatDefinition | Promise<ChatDefinition>;
14
+ readonly sessionStorage: (context: TContext | undefined, signal: AbortSignal) => SessionStorage;
15
+ readonly timeoutMs?: number;
16
+ readonly cleanupTimeoutMs?: number;
17
+ readonly maxBodyBytes?: number;
18
+ readonly now?: () => Date;
19
+ readonly createId?: () => string;
20
+ }
21
+ declare class ChatHandlerError extends Error {
22
+ readonly status: number;
23
+ readonly code: string;
24
+ readonly retryable: boolean;
25
+ constructor(options: {
26
+ readonly status: number;
27
+ readonly code: string;
28
+ readonly message: string;
29
+ readonly retryable?: boolean;
30
+ });
31
+ }
32
+ declare const createChatHandler: <TContext = undefined>(options: ChatHandlerOptions<TContext>) => ChatHandler;
33
+
34
+ export { type AuthenticationResult, type ChatHandler, ChatHandlerError, type ChatHandlerOptions, createChatHandler };
@@ -0,0 +1,34 @@
1
+ import { ChatDefinition, SessionStorage } from '@agentskit/chat';
2
+
3
+ type ChatHandler = (request: Request) => Promise<Response>;
4
+ type AuthenticationResult<TContext> = {
5
+ readonly ok: true;
6
+ readonly context: TContext;
7
+ } | {
8
+ readonly ok: false;
9
+ readonly response: Response;
10
+ };
11
+ interface ChatHandlerOptions<TContext = undefined> {
12
+ readonly authenticate?: (request: Request, signal: AbortSignal) => AuthenticationResult<TContext> | Promise<AuthenticationResult<TContext>>;
13
+ readonly resolveDefinition: (context: TContext | undefined, sessionId: string, signal: AbortSignal) => ChatDefinition | Promise<ChatDefinition>;
14
+ readonly sessionStorage: (context: TContext | undefined, signal: AbortSignal) => SessionStorage;
15
+ readonly timeoutMs?: number;
16
+ readonly cleanupTimeoutMs?: number;
17
+ readonly maxBodyBytes?: number;
18
+ readonly now?: () => Date;
19
+ readonly createId?: () => string;
20
+ }
21
+ declare class ChatHandlerError extends Error {
22
+ readonly status: number;
23
+ readonly code: string;
24
+ readonly retryable: boolean;
25
+ constructor(options: {
26
+ readonly status: number;
27
+ readonly code: string;
28
+ readonly message: string;
29
+ readonly retryable?: boolean;
30
+ });
31
+ }
32
+ declare const createChatHandler: <TContext = undefined>(options: ChatHandlerOptions<TContext>) => ChatHandler;
33
+
34
+ export { type AuthenticationResult, type ChatHandler, ChatHandlerError, type ChatHandlerOptions, createChatHandler };
package/dist/index.js ADDED
@@ -0,0 +1,223 @@
1
+ // src/index.ts
2
+ import { createChatController } from "@agentskit/core";
3
+ import { resumeChatSession, SessionConflictError } from "@agentskit/chat";
4
+ import { createSnapshotEvent, decodeTurnEvent, encodeTurnEvent, TurnEventSchema } from "@agentskit/chat-protocol";
5
+ var ChatHandlerError = class extends Error {
6
+ status;
7
+ code;
8
+ retryable;
9
+ constructor(options) {
10
+ super(options.message);
11
+ this.name = "ChatHandlerError";
12
+ this.status = options.status;
13
+ this.code = options.code;
14
+ this.retryable = options.retryable ?? false;
15
+ }
16
+ };
17
+ var encoder = new TextEncoder();
18
+ var decoder = new TextDecoder();
19
+ var json = (diagnostic, status) => new Response(JSON.stringify({ error: diagnostic }), {
20
+ status,
21
+ headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }
22
+ });
23
+ var safeError = (error) => error instanceof ChatHandlerError ? { status: error.status, diagnostic: { version: 1, code: error.code, message: error.message, retryable: error.retryable } } : error instanceof SessionConflictError ? { status: 409, diagnostic: { version: 1, code: "SESSION_CONFLICT", message: "Another turn is active for this session.", retryable: true } } : { status: 500, diagnostic: { version: 1, code: "SERVER_INTERNAL", message: "The chat request failed.", retryable: true } };
24
+ var fail = (status, code, message, retryable = false) => {
25
+ throw new ChatHandlerError({ status, code, message, retryable });
26
+ };
27
+ var withSignal = async (operation, signal) => {
28
+ if (signal.aborted) throw signal.reason;
29
+ let rejectAbort;
30
+ const abort = () => rejectAbort?.(signal.reason);
31
+ const aborted = new Promise((_resolve, reject) => {
32
+ rejectAbort = reject;
33
+ });
34
+ signal.addEventListener("abort", abort, { once: true });
35
+ try {
36
+ return await Promise.race([Promise.resolve(operation), aborted]);
37
+ } finally {
38
+ signal.removeEventListener("abort", abort);
39
+ }
40
+ };
41
+ var readBody = async (request, maxBodyBytes, signal) => {
42
+ const declared = Number(request.headers.get("content-length"));
43
+ if (Number.isFinite(declared) && declared > maxBodyBytes) fail(413, "REQUEST_TOO_LARGE", "Request body is too large.");
44
+ const reader = request.body?.getReader();
45
+ if (!reader) return fail(400, "REQUEST_INVALID_JSON", "Request body is not valid JSON.");
46
+ const chunks = [];
47
+ let size = 0;
48
+ try {
49
+ while (true) {
50
+ const result = await withSignal(reader.read(), signal);
51
+ if (result.done) break;
52
+ size += result.value.byteLength;
53
+ if (size > maxBodyBytes) {
54
+ await reader.cancel();
55
+ return fail(413, "REQUEST_TOO_LARGE", "Request body is too large.");
56
+ }
57
+ chunks.push(result.value);
58
+ }
59
+ } finally {
60
+ reader.releaseLock();
61
+ }
62
+ const bytes = new Uint8Array(size);
63
+ let offset = 0;
64
+ for (const chunk of chunks) {
65
+ bytes.set(chunk, offset);
66
+ offset += chunk.byteLength;
67
+ }
68
+ try {
69
+ return JSON.parse(decoder.decode(bytes));
70
+ } catch {
71
+ return fail(400, "REQUEST_INVALID_JSON", "Request body is not valid JSON.");
72
+ }
73
+ };
74
+ var snapshotStatus = (state) => state.status === "error" ? "error" : state.status === "streaming" ? "streaming" : state.messages.length === 0 ? "idle" : "complete";
75
+ var createChatHandler = (options) => {
76
+ const timeoutMs = options.timeoutMs ?? 3e4;
77
+ const cleanupTimeoutMs = options.cleanupTimeoutMs ?? 5e3;
78
+ const maxBodyBytes = options.maxBodyBytes ?? 64 * 1024;
79
+ if (![timeoutMs, cleanupTimeoutMs, maxBodyBytes].every((value) => Number.isSafeInteger(value) && value > 0)) fail(500, "SERVER_INVALID_CONFIG", "Chat handler configuration is invalid.");
80
+ const leaseMs = timeoutMs + 3 * cleanupTimeoutMs;
81
+ if (!Number.isSafeInteger(leaseMs)) fail(500, "SERVER_INVALID_CONFIG", "Chat handler configuration is invalid.");
82
+ const createId = options.createId ?? (() => crypto.randomUUID());
83
+ return async (request) => {
84
+ const deadline = AbortSignal.timeout(timeoutMs);
85
+ const signal = AbortSignal.any([request.signal, deadline]);
86
+ try {
87
+ if (request.method !== "POST") fail(405, "REQUEST_METHOD_NOT_ALLOWED", "Only POST is supported.");
88
+ if (!request.headers.get("content-type")?.toLowerCase().startsWith("application/json")) fail(415, "REQUEST_UNSUPPORTED_MEDIA_TYPE", "Content-Type must be application/json.");
89
+ let context;
90
+ if (options.authenticate) {
91
+ const authenticated = await withSignal(options.authenticate(request, signal), signal);
92
+ if (!authenticated.ok) return authenticated.response;
93
+ context = authenticated.context;
94
+ }
95
+ const decoded = decodeTurnEvent(await readBody(request, maxBodyBytes, signal));
96
+ if (!decoded.ok || decoded.event.event !== "client.turn.submit") return fail(400, "REQUEST_INVALID_EVENT", "Request body must be a valid turn submission.");
97
+ const submission = decoded.event;
98
+ const definition = await withSignal(options.resolveDefinition(context, submission.sessionId, signal), signal);
99
+ const storage = options.sessionStorage(context, signal);
100
+ const session = await withSignal(resumeChatSession(definition, { sessionId: submission.sessionId, storage, signal, ...options.now ? { now: options.now } : {} }), signal);
101
+ if (!await withSignal(session.claimTurn(submission.turnId, leaseMs, signal), signal)) return json({ version: 1, code: "SESSION_BUSY", message: "Another turn is active for this session.", retryable: true }, 409);
102
+ const memory = definition.chat.memory;
103
+ const loaded = memory ? await withSignal(memory.load({ signal }), signal) : definition.chat.initialMessages ?? [];
104
+ const messages = loaded.length > 0 ? loaded : definition.chat.initialMessages ?? [];
105
+ const { memory: _memory, ...chat } = definition.chat;
106
+ const controller = createChatController(session.updateChat({ ...chat, initialMessages: [...messages] }));
107
+ let pending;
108
+ let wake;
109
+ let done = false;
110
+ let closed = false;
111
+ const push = () => {
112
+ const state = controller.getState();
113
+ pending = { ...state, messages: [...state.messages], usage: { ...state.usage } };
114
+ wake?.();
115
+ wake = void 0;
116
+ };
117
+ const unsubscribe = controller.subscribe(push);
118
+ let cleanup = async () => void 0;
119
+ const abort = () => {
120
+ controller.stop();
121
+ void cleanup(true).catch(() => void 0);
122
+ };
123
+ signal.addEventListener("abort", abort, { once: true });
124
+ const send = controller.send(submission.payload.input).finally(() => {
125
+ done = true;
126
+ wake?.();
127
+ wake = void 0;
128
+ });
129
+ cleanup = async (stop) => {
130
+ if (closed) return;
131
+ closed = true;
132
+ unsubscribe();
133
+ signal.removeEventListener("abort", abort);
134
+ if (stop && !signal.aborted) controller.stop();
135
+ const settleSignal = AbortSignal.timeout(cleanupTimeoutMs);
136
+ await withSignal(send.catch(() => void 0), settleSignal).catch(() => void 0);
137
+ const saveSignal = AbortSignal.timeout(cleanupTimeoutMs);
138
+ let outcome = "completed";
139
+ try {
140
+ await withSignal(memory?.save(controller.getState().messages, { signal: saveSignal }), saveSignal);
141
+ } catch (error) {
142
+ outcome = "indeterminate";
143
+ throw error;
144
+ } finally {
145
+ const releaseSignal = AbortSignal.timeout(cleanupTimeoutMs);
146
+ await withSignal(session.releaseTurn(submission.turnId, outcome, releaseSignal), releaseSignal);
147
+ }
148
+ };
149
+ const diagnosticLine = (code, message) => {
150
+ const event = TurnEventSchema.parse({
151
+ protocol: "agentskit.chat.turn",
152
+ version: 1,
153
+ eventId: createId(),
154
+ sessionId: submission.sessionId,
155
+ turnId: submission.turnId,
156
+ sequence: session.getCursor() + 1,
157
+ emittedAt: (options.now?.() ?? /* @__PURE__ */ new Date()).toISOString(),
158
+ event: "server.turn.diagnostic",
159
+ payload: { version: 1, code, message, retryable: true }
160
+ });
161
+ return encoder.encode(`${encodeTurnEvent(event)}
162
+ `);
163
+ };
164
+ const body = new ReadableStream({
165
+ async pull(stream) {
166
+ try {
167
+ while (!pending && !done && !signal.aborted) await new Promise((resolve) => {
168
+ wake = resolve;
169
+ });
170
+ if (signal.aborted) {
171
+ stream.enqueue(diagnosticLine(deadline.aborted ? "SERVER_TIMEOUT" : "REQUEST_CANCELLED", deadline.aborted ? "The chat request timed out." : "The chat request was cancelled."));
172
+ await cleanup(true);
173
+ stream.close();
174
+ return;
175
+ }
176
+ if (pending) {
177
+ const state = pending;
178
+ pending = void 0;
179
+ await withSignal(session.persist(signal), signal);
180
+ const event = createSnapshotEvent({
181
+ eventId: createId(),
182
+ sessionId: submission.sessionId,
183
+ turnId: submission.turnId,
184
+ sequence: session.getCursor(),
185
+ emittedAt: (options.now?.() ?? /* @__PURE__ */ new Date()).toISOString(),
186
+ messages: state.messages,
187
+ status: snapshotStatus(state),
188
+ usage: state.usage,
189
+ lineage: { operation: "submit" },
190
+ ...state.error ? { error: { version: 1, code: "CHAT_TURN_FAILED", message: "The chat turn failed.", retryable: true } } : {}
191
+ });
192
+ stream.enqueue(encoder.encode(`${encodeTurnEvent(event)}
193
+ `));
194
+ return;
195
+ }
196
+ await cleanup(false);
197
+ stream.close();
198
+ } catch (error) {
199
+ const safe = safeError(error);
200
+ try {
201
+ stream.enqueue(diagnosticLine(safe.diagnostic.code, safe.diagnostic.message));
202
+ } catch {
203
+ }
204
+ await cleanup(true).catch(() => void 0);
205
+ stream.close();
206
+ }
207
+ },
208
+ async cancel() {
209
+ await cleanup(true).catch(() => void 0);
210
+ }
211
+ });
212
+ return new Response(body, { status: 200, headers: { "content-type": "application/x-ndjson; charset=utf-8", "cache-control": "no-store", "x-content-type-options": "nosniff" } });
213
+ } catch (error) {
214
+ if (signal.aborted) return json({ version: 1, code: deadline.aborted ? "SERVER_TIMEOUT" : "REQUEST_CANCELLED", message: deadline.aborted ? "The chat request timed out." : "The chat request was cancelled.", retryable: true }, deadline.aborted ? 408 : 499);
215
+ const safe = safeError(error);
216
+ return json(safe.diagnostic, safe.status);
217
+ }
218
+ };
219
+ };
220
+ export {
221
+ ChatHandlerError,
222
+ createChatHandler
223
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@agentskit/chat-server",
3
+ "version": "0.1.0",
4
+ "description": "Web-standard server handler for AgentsKit Chat applications.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/AgentsKit-io/agentskit-chat.git",
9
+ "directory": "packages/server"
10
+ },
11
+ "homepage": "https://github.com/AgentsKit-io/agentskit-chat#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/AgentsKit-io/agentskit-chat/issues"
14
+ },
15
+ "type": "module",
16
+ "main": "./dist/index.cjs",
17
+ "module": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js",
23
+ "require": "./dist/index.cjs"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "dependencies": {
30
+ "@agentskit/chat": "0.1.0",
31
+ "@agentskit/chat-protocol": "0.1.0"
32
+ },
33
+ "peerDependencies": {
34
+ "@agentskit/core": "^1.12.2"
35
+ },
36
+ "devDependencies": {
37
+ "@agentskit/core": "^1.12.2",
38
+ "tsup": "^8.5.1"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public",
42
+ "provenance": true
43
+ },
44
+ "scripts": {
45
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
46
+ "lint": "tsc --noEmit",
47
+ "test": "pnpm --filter @agentskit/chat build && pnpm --filter @agentskit/chat-protocol build && vitest run --coverage"
48
+ }
49
+ }