@copilotkit/runtime 1.55.2-next.1 → 1.55.2
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/CHANGELOG.md +7 -0
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/v2/runtime/core/fetch-handler.cjs +43 -3
- package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.d.cts.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.d.mts.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.mjs +43 -3
- package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -1
- package/dist/v2/runtime/core/fetch-router.cjs +26 -0
- package/dist/v2/runtime/core/fetch-router.cjs.map +1 -1
- package/dist/v2/runtime/core/fetch-router.mjs +26 -0
- package/dist/v2/runtime/core/fetch-router.mjs.map +1 -1
- package/dist/v2/runtime/core/hooks.cjs.map +1 -1
- package/dist/v2/runtime/core/hooks.d.cts +13 -0
- package/dist/v2/runtime/core/hooks.d.cts.map +1 -1
- package/dist/v2/runtime/core/hooks.d.mts +13 -0
- package/dist/v2/runtime/core/hooks.d.mts.map +1 -1
- package/dist/v2/runtime/core/hooks.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/threads.cjs +179 -0
- package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/threads.mjs +173 -0
- package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -0
- package/package.json +2 -2
- package/src/v2/runtime/__tests__/fetch-router.test.ts +76 -0
- package/src/v2/runtime/core/fetch-handler.ts +55 -4
- package/src/v2/runtime/core/fetch-router.ts +48 -0
- package/src/v2/runtime/core/hooks.ts +6 -1
- package/src/v2/runtime/handlers/handle-threads.ts +1 -0
- package/src/v2/runtime/handlers/intelligence/threads.ts +28 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
require("reflect-metadata");
|
|
2
|
+
const require_runtime = require('../../../../_virtual/_rolldown/runtime.cjs');
|
|
3
|
+
const require_runtime$1 = require('../../core/runtime.cjs');
|
|
4
|
+
const require_json_response = require('../shared/json-response.cjs');
|
|
5
|
+
const require_intelligence_utils = require('../shared/intelligence-utils.cjs');
|
|
6
|
+
const require_resolve_intelligence_user = require('../shared/resolve-intelligence-user.cjs');
|
|
7
|
+
let _copilotkit_shared = require("@copilotkit/shared");
|
|
8
|
+
|
|
9
|
+
//#region src/v2/runtime/handlers/intelligence/threads.ts
|
|
10
|
+
async function parseJsonBody(request) {
|
|
11
|
+
try {
|
|
12
|
+
return await request.json();
|
|
13
|
+
} catch (error) {
|
|
14
|
+
_copilotkit_shared.logger.error({ err: error }, "Malformed JSON in request body");
|
|
15
|
+
return require_json_response.errorResponse("Invalid request body", 400);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function requireIntelligenceRuntime(runtime) {
|
|
19
|
+
if (!require_runtime$1.isIntelligenceRuntime(runtime)) return require_json_response.errorResponse("Missing CopilotKitIntelligence configuration. Thread operations require a CopilotKitIntelligence instance to be provided in CopilotRuntime options.", 422);
|
|
20
|
+
return runtime;
|
|
21
|
+
}
|
|
22
|
+
async function resolveThreadMutationContext(runtime, request) {
|
|
23
|
+
const body = await parseJsonBody(request);
|
|
24
|
+
if (require_json_response.isHandlerResponse(body)) return body;
|
|
25
|
+
const user = await require_resolve_intelligence_user.resolveIntelligenceUser({
|
|
26
|
+
runtime,
|
|
27
|
+
request
|
|
28
|
+
});
|
|
29
|
+
if (require_json_response.isHandlerResponse(user)) return user;
|
|
30
|
+
const agentId = body.agentId;
|
|
31
|
+
if (!require_intelligence_utils.isValidIdentifier(agentId)) return require_json_response.errorResponse("Valid agentId is required", 400);
|
|
32
|
+
return {
|
|
33
|
+
body,
|
|
34
|
+
userId: user.id,
|
|
35
|
+
agentId
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async function handleListThreads({ runtime, request }) {
|
|
39
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
40
|
+
if (require_json_response.isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
41
|
+
try {
|
|
42
|
+
const url = new URL(request.url);
|
|
43
|
+
const agentId = url.searchParams.get("agentId");
|
|
44
|
+
const includeArchived = url.searchParams.get("includeArchived") === "true";
|
|
45
|
+
const limitParam = url.searchParams.get("limit");
|
|
46
|
+
const cursor = url.searchParams.get("cursor");
|
|
47
|
+
const user = await require_resolve_intelligence_user.resolveIntelligenceUser({
|
|
48
|
+
runtime: intelligenceRuntime,
|
|
49
|
+
request
|
|
50
|
+
});
|
|
51
|
+
if (require_json_response.isHandlerResponse(user)) return user;
|
|
52
|
+
if (!require_intelligence_utils.isValidIdentifier(agentId)) return require_json_response.errorResponse("Valid agentId query param is required", 400);
|
|
53
|
+
const data = await intelligenceRuntime.intelligence.listThreads({
|
|
54
|
+
userId: user.id,
|
|
55
|
+
agentId,
|
|
56
|
+
...includeArchived ? { includeArchived: true } : {},
|
|
57
|
+
...limitParam ? { limit: Number(limitParam) } : {},
|
|
58
|
+
...cursor ? { cursor } : {}
|
|
59
|
+
});
|
|
60
|
+
return Response.json(data);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
_copilotkit_shared.logger.error({ err: error }, "Error listing threads");
|
|
63
|
+
return require_json_response.errorResponse("Failed to list threads", 500);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function handleUpdateThread({ runtime, request, threadId }) {
|
|
67
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
68
|
+
if (require_json_response.isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
69
|
+
try {
|
|
70
|
+
const mutation = await resolveThreadMutationContext(intelligenceRuntime, request);
|
|
71
|
+
if (require_json_response.isHandlerResponse(mutation)) return mutation;
|
|
72
|
+
const updates = { ...mutation.body };
|
|
73
|
+
delete updates.agentId;
|
|
74
|
+
delete updates.userId;
|
|
75
|
+
const thread = await intelligenceRuntime.intelligence.updateThread({
|
|
76
|
+
threadId,
|
|
77
|
+
userId: mutation.userId,
|
|
78
|
+
agentId: mutation.agentId,
|
|
79
|
+
updates
|
|
80
|
+
});
|
|
81
|
+
return Response.json(thread);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
_copilotkit_shared.logger.error({
|
|
84
|
+
err: error,
|
|
85
|
+
threadId
|
|
86
|
+
}, "Error updating thread");
|
|
87
|
+
return require_json_response.errorResponse("Failed to update thread", 500);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function handleSubscribeToThreads({ runtime, request }) {
|
|
91
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
92
|
+
if (require_json_response.isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
93
|
+
try {
|
|
94
|
+
const user = await require_resolve_intelligence_user.resolveIntelligenceUser({
|
|
95
|
+
runtime: intelligenceRuntime,
|
|
96
|
+
request
|
|
97
|
+
});
|
|
98
|
+
if (require_json_response.isHandlerResponse(user)) return user;
|
|
99
|
+
const credentials = await intelligenceRuntime.intelligence.ɵsubscribeToThreads({ userId: user.id });
|
|
100
|
+
return Response.json({ joinToken: credentials.joinToken });
|
|
101
|
+
} catch (error) {
|
|
102
|
+
_copilotkit_shared.logger.error({ err: error }, "Error subscribing to threads");
|
|
103
|
+
return require_json_response.errorResponse("Failed to subscribe to threads", 500);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function handleArchiveThread({ runtime, request, threadId }) {
|
|
107
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
108
|
+
if (require_json_response.isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
109
|
+
try {
|
|
110
|
+
const mutation = await resolveThreadMutationContext(intelligenceRuntime, request);
|
|
111
|
+
if (require_json_response.isHandlerResponse(mutation)) return mutation;
|
|
112
|
+
await intelligenceRuntime.intelligence.archiveThread({
|
|
113
|
+
threadId,
|
|
114
|
+
userId: mutation.userId,
|
|
115
|
+
agentId: mutation.agentId
|
|
116
|
+
});
|
|
117
|
+
return Response.json({
|
|
118
|
+
threadId,
|
|
119
|
+
archived: true
|
|
120
|
+
});
|
|
121
|
+
} catch (error) {
|
|
122
|
+
_copilotkit_shared.logger.error({
|
|
123
|
+
err: error,
|
|
124
|
+
threadId
|
|
125
|
+
}, "Error archiving thread");
|
|
126
|
+
return require_json_response.errorResponse("Failed to archive thread", 500);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async function handleDeleteThread({ runtime, request, threadId }) {
|
|
130
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
131
|
+
if (require_json_response.isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
132
|
+
try {
|
|
133
|
+
const mutation = await resolveThreadMutationContext(intelligenceRuntime, request);
|
|
134
|
+
if (require_json_response.isHandlerResponse(mutation)) return mutation;
|
|
135
|
+
await intelligenceRuntime.intelligence.deleteThread({
|
|
136
|
+
threadId,
|
|
137
|
+
userId: mutation.userId,
|
|
138
|
+
agentId: mutation.agentId
|
|
139
|
+
});
|
|
140
|
+
return Response.json({
|
|
141
|
+
threadId,
|
|
142
|
+
deleted: true
|
|
143
|
+
});
|
|
144
|
+
} catch (error) {
|
|
145
|
+
_copilotkit_shared.logger.error({
|
|
146
|
+
err: error,
|
|
147
|
+
threadId
|
|
148
|
+
}, "Error deleting thread");
|
|
149
|
+
return require_json_response.errorResponse("Failed to delete thread", 500);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function handleGetThreadMessages({ runtime, request, threadId }) {
|
|
153
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
154
|
+
if (require_json_response.isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
155
|
+
try {
|
|
156
|
+
const user = await require_resolve_intelligence_user.resolveIntelligenceUser({
|
|
157
|
+
runtime: intelligenceRuntime,
|
|
158
|
+
request
|
|
159
|
+
});
|
|
160
|
+
if (require_json_response.isHandlerResponse(user)) return user;
|
|
161
|
+
const data = await intelligenceRuntime.intelligence.getThreadMessages({ threadId });
|
|
162
|
+
return Response.json(data);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
_copilotkit_shared.logger.error({
|
|
165
|
+
err: error,
|
|
166
|
+
threadId
|
|
167
|
+
}, "Error getting thread messages");
|
|
168
|
+
return require_json_response.errorResponse("Failed to get thread messages", 500);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
exports.handleArchiveThread = handleArchiveThread;
|
|
174
|
+
exports.handleDeleteThread = handleDeleteThread;
|
|
175
|
+
exports.handleGetThreadMessages = handleGetThreadMessages;
|
|
176
|
+
exports.handleListThreads = handleListThreads;
|
|
177
|
+
exports.handleSubscribeToThreads = handleSubscribeToThreads;
|
|
178
|
+
exports.handleUpdateThread = handleUpdateThread;
|
|
179
|
+
//# sourceMappingURL=threads.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"threads.cjs","names":["errorResponse","isIntelligenceRuntime","isHandlerResponse","resolveIntelligenceUser","isValidIdentifier"],"sources":["../../../../../src/v2/runtime/handlers/intelligence/threads.ts"],"sourcesContent":["import {\n CopilotIntelligenceRuntimeLike,\n CopilotRuntimeLike,\n isIntelligenceRuntime,\n} from \"../../core/runtime\";\nimport { logger } from \"@copilotkit/shared\";\nimport { errorResponse, isHandlerResponse } from \"../shared/json-response\";\nimport { isValidIdentifier } from \"../shared/intelligence-utils\";\nimport { resolveIntelligenceUser } from \"../shared/resolve-intelligence-user\";\n\ninterface ThreadsHandlerParams {\n runtime: CopilotRuntimeLike;\n request: Request;\n}\n\ninterface ThreadMutationParams extends ThreadsHandlerParams {\n threadId: string;\n}\n\ninterface ThreadMutationContext {\n userId: string;\n agentId: string;\n body: Record<string, unknown>;\n}\n\nasync function parseJsonBody(\n request: Request,\n): Promise<Record<string, unknown> | Response> {\n try {\n return (await request.json()) as Record<string, unknown>;\n } catch (error) {\n logger.error({ err: error }, \"Malformed JSON in request body\");\n return errorResponse(\"Invalid request body\", 400);\n }\n}\n\nfunction requireIntelligenceRuntime(\n runtime: CopilotRuntimeLike,\n): CopilotIntelligenceRuntimeLike | Response {\n if (!isIntelligenceRuntime(runtime)) {\n return errorResponse(\n \"Missing CopilotKitIntelligence configuration. Thread operations require a CopilotKitIntelligence instance to be provided in CopilotRuntime options.\",\n 422,\n );\n }\n\n return runtime;\n}\n\nasync function resolveThreadMutationContext(\n runtime: CopilotIntelligenceRuntimeLike,\n request: Request,\n): Promise<ThreadMutationContext | Response> {\n const body = await parseJsonBody(request);\n if (isHandlerResponse(body)) return body;\n\n const user = await resolveIntelligenceUser({ runtime, request });\n if (isHandlerResponse(user)) return user;\n\n const agentId = body.agentId;\n if (!isValidIdentifier(agentId)) {\n return errorResponse(\"Valid agentId is required\", 400);\n }\n\n return {\n body,\n userId: user.id,\n agentId,\n };\n}\n\nexport async function handleListThreads({\n runtime,\n request,\n}: ThreadsHandlerParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const url = new URL(request.url);\n const agentId = url.searchParams.get(\"agentId\");\n const includeArchived = url.searchParams.get(\"includeArchived\") === \"true\";\n const limitParam = url.searchParams.get(\"limit\");\n const cursor = url.searchParams.get(\"cursor\");\n const user = await resolveIntelligenceUser({\n runtime: intelligenceRuntime,\n request,\n });\n if (isHandlerResponse(user)) return user;\n\n if (!isValidIdentifier(agentId)) {\n return errorResponse(\"Valid agentId query param is required\", 400);\n }\n\n const data = await intelligenceRuntime.intelligence.listThreads({\n userId: user.id,\n agentId,\n ...(includeArchived ? { includeArchived: true } : {}),\n ...(limitParam ? { limit: Number(limitParam) } : {}),\n ...(cursor ? { cursor } : {}),\n });\n\n return Response.json(data);\n } catch (error) {\n logger.error({ err: error }, \"Error listing threads\");\n return errorResponse(\"Failed to list threads\", 500);\n }\n}\n\nexport async function handleUpdateThread({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const mutation = await resolveThreadMutationContext(\n intelligenceRuntime,\n request,\n );\n if (isHandlerResponse(mutation)) return mutation;\n\n const updates = { ...mutation.body };\n delete updates.agentId;\n delete updates.userId;\n\n const thread = await intelligenceRuntime.intelligence.updateThread({\n threadId,\n userId: mutation.userId,\n agentId: mutation.agentId,\n updates,\n });\n\n return Response.json(thread);\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error updating thread\");\n return errorResponse(\"Failed to update thread\", 500);\n }\n}\n\nexport async function handleSubscribeToThreads({\n runtime,\n request,\n}: ThreadsHandlerParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const user = await resolveIntelligenceUser({\n runtime: intelligenceRuntime,\n request,\n });\n if (isHandlerResponse(user)) return user;\n\n const credentials =\n await intelligenceRuntime.intelligence.ɵsubscribeToThreads({\n userId: user.id,\n });\n\n return Response.json({ joinToken: credentials.joinToken });\n } catch (error) {\n logger.error({ err: error }, \"Error subscribing to threads\");\n return errorResponse(\"Failed to subscribe to threads\", 500);\n }\n}\n\nexport async function handleArchiveThread({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const mutation = await resolveThreadMutationContext(\n intelligenceRuntime,\n request,\n );\n if (isHandlerResponse(mutation)) return mutation;\n\n await intelligenceRuntime.intelligence.archiveThread({\n threadId,\n userId: mutation.userId,\n agentId: mutation.agentId,\n });\n\n return Response.json({ threadId, archived: true });\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error archiving thread\");\n return errorResponse(\"Failed to archive thread\", 500);\n }\n}\n\nexport async function handleDeleteThread({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const mutation = await resolveThreadMutationContext(\n intelligenceRuntime,\n request,\n );\n if (isHandlerResponse(mutation)) return mutation;\n\n await intelligenceRuntime.intelligence.deleteThread({\n threadId,\n userId: mutation.userId,\n agentId: mutation.agentId,\n });\n\n return Response.json({ threadId, deleted: true });\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error deleting thread\");\n return errorResponse(\"Failed to delete thread\", 500);\n }\n}\n\nexport async function handleGetThreadMessages({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const user = await resolveIntelligenceUser({\n runtime: intelligenceRuntime,\n request,\n });\n if (isHandlerResponse(user)) return user;\n\n const data = await intelligenceRuntime.intelligence.getThreadMessages({\n threadId,\n });\n\n return Response.json(data);\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error getting thread messages\");\n return errorResponse(\"Failed to get thread messages\", 500);\n }\n}\n"],"mappings":";;;;;;;;;AAyBA,eAAe,cACb,SAC6C;AAC7C,KAAI;AACF,SAAQ,MAAM,QAAQ,MAAM;UACrB,OAAO;AACd,4BAAO,MAAM,EAAE,KAAK,OAAO,EAAE,iCAAiC;AAC9D,SAAOA,oCAAc,wBAAwB,IAAI;;;AAIrD,SAAS,2BACP,SAC2C;AAC3C,KAAI,CAACC,wCAAsB,QAAQ,CACjC,QAAOD,oCACL,uJACA,IACD;AAGH,QAAO;;AAGT,eAAe,6BACb,SACA,SAC2C;CAC3C,MAAM,OAAO,MAAM,cAAc,QAAQ;AACzC,KAAIE,wC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,CAAC;AAEF,SAAO,SAAS,KAAK;GAAE;GAAU,UAAU;GAAM,CAAC;UAC3C,OAAO;AACd,4BAAO,MAAM;GAAE,KAAK;GAAO;GAAU,EAAE,yBAAyB;AAChE,SAAOF,oCAAc,4BAA4B,IAAI;;;AAIzD,eAAsB,mBAAmB,EACvC,SACA,SACA,YAC0C;CAC1C,MAAM,sBAAsB,2BAA2B,QAAQ;AAC/D,KAAIE,wCAAkB,oBAAoB,CACxC,QAAO;AAGT,KAAI;EACF,MAAM,WAAW,MAAM,6BACrB,qBACA,QACD;AACD,MAAIA,wCAAkB,SAAS,CAAE,QAAO;AAExC,QAAM,oBAAoB,aAAa,aAAa;GAClD;GACA,QAAQ,SAAS;GACjB,SAAS,SAAS;GACnB,CAAC;AAEF,SAAO,SAAS,KAAK;GAAE;GAAU,SAAS;GAAM,CAAC;UAC1C,OAAO;AACd,4BAAO,MAAM;GAAE,KAAK;GAAO;GAAU,EAAE,wBAAwB;AAC/D,SAAOF,oCAAc,2BAA2B,IAAI;;;AAIxD,eAAsB,wBAAwB,EAC5C,SACA,SACA,YAC0C;CAC1C,MAAM,sBAAsB,2BAA2B,QAAQ;AAC/D,KAAIE,wCAAkB,oBAAoB,CACxC,QAAO;AAGT,KAAI;EACF,MAAM,OAAO,MAAMC,0DAAwB;GACzC,SAAS;GACT;GACD,CAAC;AACF,MAAID,wCAAkB,KAAK,CAAE,QAAO;EAEpC,MAAM,OAAO,MAAM,oBAAoB,aAAa,kBAAkB,EACpE,UACD,CAAC;AAEF,SAAO,SAAS,KAAK,KAAK;UACnB,OAAO;AACd,4BAAO,MAAM;GAAE,KAAK;GAAO;GAAU,EAAE,gCAAgC;AACvE,SAAOF,oCAAc,iCAAiC,IAAI"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { isIntelligenceRuntime } from "../../core/runtime.mjs";
|
|
3
|
+
import { errorResponse, isHandlerResponse } from "../shared/json-response.mjs";
|
|
4
|
+
import { isValidIdentifier } from "../shared/intelligence-utils.mjs";
|
|
5
|
+
import { resolveIntelligenceUser } from "../shared/resolve-intelligence-user.mjs";
|
|
6
|
+
import { logger } from "@copilotkit/shared";
|
|
7
|
+
|
|
8
|
+
//#region src/v2/runtime/handlers/intelligence/threads.ts
|
|
9
|
+
async function parseJsonBody(request) {
|
|
10
|
+
try {
|
|
11
|
+
return await request.json();
|
|
12
|
+
} catch (error) {
|
|
13
|
+
logger.error({ err: error }, "Malformed JSON in request body");
|
|
14
|
+
return errorResponse("Invalid request body", 400);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function requireIntelligenceRuntime(runtime) {
|
|
18
|
+
if (!isIntelligenceRuntime(runtime)) return errorResponse("Missing CopilotKitIntelligence configuration. Thread operations require a CopilotKitIntelligence instance to be provided in CopilotRuntime options.", 422);
|
|
19
|
+
return runtime;
|
|
20
|
+
}
|
|
21
|
+
async function resolveThreadMutationContext(runtime, request) {
|
|
22
|
+
const body = await parseJsonBody(request);
|
|
23
|
+
if (isHandlerResponse(body)) return body;
|
|
24
|
+
const user = await resolveIntelligenceUser({
|
|
25
|
+
runtime,
|
|
26
|
+
request
|
|
27
|
+
});
|
|
28
|
+
if (isHandlerResponse(user)) return user;
|
|
29
|
+
const agentId = body.agentId;
|
|
30
|
+
if (!isValidIdentifier(agentId)) return errorResponse("Valid agentId is required", 400);
|
|
31
|
+
return {
|
|
32
|
+
body,
|
|
33
|
+
userId: user.id,
|
|
34
|
+
agentId
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
async function handleListThreads({ runtime, request }) {
|
|
38
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
39
|
+
if (isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
40
|
+
try {
|
|
41
|
+
const url = new URL(request.url);
|
|
42
|
+
const agentId = url.searchParams.get("agentId");
|
|
43
|
+
const includeArchived = url.searchParams.get("includeArchived") === "true";
|
|
44
|
+
const limitParam = url.searchParams.get("limit");
|
|
45
|
+
const cursor = url.searchParams.get("cursor");
|
|
46
|
+
const user = await resolveIntelligenceUser({
|
|
47
|
+
runtime: intelligenceRuntime,
|
|
48
|
+
request
|
|
49
|
+
});
|
|
50
|
+
if (isHandlerResponse(user)) return user;
|
|
51
|
+
if (!isValidIdentifier(agentId)) return errorResponse("Valid agentId query param is required", 400);
|
|
52
|
+
const data = await intelligenceRuntime.intelligence.listThreads({
|
|
53
|
+
userId: user.id,
|
|
54
|
+
agentId,
|
|
55
|
+
...includeArchived ? { includeArchived: true } : {},
|
|
56
|
+
...limitParam ? { limit: Number(limitParam) } : {},
|
|
57
|
+
...cursor ? { cursor } : {}
|
|
58
|
+
});
|
|
59
|
+
return Response.json(data);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
logger.error({ err: error }, "Error listing threads");
|
|
62
|
+
return errorResponse("Failed to list threads", 500);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function handleUpdateThread({ runtime, request, threadId }) {
|
|
66
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
67
|
+
if (isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
68
|
+
try {
|
|
69
|
+
const mutation = await resolveThreadMutationContext(intelligenceRuntime, request);
|
|
70
|
+
if (isHandlerResponse(mutation)) return mutation;
|
|
71
|
+
const updates = { ...mutation.body };
|
|
72
|
+
delete updates.agentId;
|
|
73
|
+
delete updates.userId;
|
|
74
|
+
const thread = await intelligenceRuntime.intelligence.updateThread({
|
|
75
|
+
threadId,
|
|
76
|
+
userId: mutation.userId,
|
|
77
|
+
agentId: mutation.agentId,
|
|
78
|
+
updates
|
|
79
|
+
});
|
|
80
|
+
return Response.json(thread);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
logger.error({
|
|
83
|
+
err: error,
|
|
84
|
+
threadId
|
|
85
|
+
}, "Error updating thread");
|
|
86
|
+
return errorResponse("Failed to update thread", 500);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async function handleSubscribeToThreads({ runtime, request }) {
|
|
90
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
91
|
+
if (isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
92
|
+
try {
|
|
93
|
+
const user = await resolveIntelligenceUser({
|
|
94
|
+
runtime: intelligenceRuntime,
|
|
95
|
+
request
|
|
96
|
+
});
|
|
97
|
+
if (isHandlerResponse(user)) return user;
|
|
98
|
+
const credentials = await intelligenceRuntime.intelligence.ɵsubscribeToThreads({ userId: user.id });
|
|
99
|
+
return Response.json({ joinToken: credentials.joinToken });
|
|
100
|
+
} catch (error) {
|
|
101
|
+
logger.error({ err: error }, "Error subscribing to threads");
|
|
102
|
+
return errorResponse("Failed to subscribe to threads", 500);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async function handleArchiveThread({ runtime, request, threadId }) {
|
|
106
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
107
|
+
if (isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
108
|
+
try {
|
|
109
|
+
const mutation = await resolveThreadMutationContext(intelligenceRuntime, request);
|
|
110
|
+
if (isHandlerResponse(mutation)) return mutation;
|
|
111
|
+
await intelligenceRuntime.intelligence.archiveThread({
|
|
112
|
+
threadId,
|
|
113
|
+
userId: mutation.userId,
|
|
114
|
+
agentId: mutation.agentId
|
|
115
|
+
});
|
|
116
|
+
return Response.json({
|
|
117
|
+
threadId,
|
|
118
|
+
archived: true
|
|
119
|
+
});
|
|
120
|
+
} catch (error) {
|
|
121
|
+
logger.error({
|
|
122
|
+
err: error,
|
|
123
|
+
threadId
|
|
124
|
+
}, "Error archiving thread");
|
|
125
|
+
return errorResponse("Failed to archive thread", 500);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function handleDeleteThread({ runtime, request, threadId }) {
|
|
129
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
130
|
+
if (isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
131
|
+
try {
|
|
132
|
+
const mutation = await resolveThreadMutationContext(intelligenceRuntime, request);
|
|
133
|
+
if (isHandlerResponse(mutation)) return mutation;
|
|
134
|
+
await intelligenceRuntime.intelligence.deleteThread({
|
|
135
|
+
threadId,
|
|
136
|
+
userId: mutation.userId,
|
|
137
|
+
agentId: mutation.agentId
|
|
138
|
+
});
|
|
139
|
+
return Response.json({
|
|
140
|
+
threadId,
|
|
141
|
+
deleted: true
|
|
142
|
+
});
|
|
143
|
+
} catch (error) {
|
|
144
|
+
logger.error({
|
|
145
|
+
err: error,
|
|
146
|
+
threadId
|
|
147
|
+
}, "Error deleting thread");
|
|
148
|
+
return errorResponse("Failed to delete thread", 500);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async function handleGetThreadMessages({ runtime, request, threadId }) {
|
|
152
|
+
const intelligenceRuntime = requireIntelligenceRuntime(runtime);
|
|
153
|
+
if (isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime;
|
|
154
|
+
try {
|
|
155
|
+
const user = await resolveIntelligenceUser({
|
|
156
|
+
runtime: intelligenceRuntime,
|
|
157
|
+
request
|
|
158
|
+
});
|
|
159
|
+
if (isHandlerResponse(user)) return user;
|
|
160
|
+
const data = await intelligenceRuntime.intelligence.getThreadMessages({ threadId });
|
|
161
|
+
return Response.json(data);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
logger.error({
|
|
164
|
+
err: error,
|
|
165
|
+
threadId
|
|
166
|
+
}, "Error getting thread messages");
|
|
167
|
+
return errorResponse("Failed to get thread messages", 500);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
export { handleArchiveThread, handleDeleteThread, handleGetThreadMessages, handleListThreads, handleSubscribeToThreads, handleUpdateThread };
|
|
173
|
+
//# sourceMappingURL=threads.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"threads.mjs","names":[],"sources":["../../../../../src/v2/runtime/handlers/intelligence/threads.ts"],"sourcesContent":["import {\n CopilotIntelligenceRuntimeLike,\n CopilotRuntimeLike,\n isIntelligenceRuntime,\n} from \"../../core/runtime\";\nimport { logger } from \"@copilotkit/shared\";\nimport { errorResponse, isHandlerResponse } from \"../shared/json-response\";\nimport { isValidIdentifier } from \"../shared/intelligence-utils\";\nimport { resolveIntelligenceUser } from \"../shared/resolve-intelligence-user\";\n\ninterface ThreadsHandlerParams {\n runtime: CopilotRuntimeLike;\n request: Request;\n}\n\ninterface ThreadMutationParams extends ThreadsHandlerParams {\n threadId: string;\n}\n\ninterface ThreadMutationContext {\n userId: string;\n agentId: string;\n body: Record<string, unknown>;\n}\n\nasync function parseJsonBody(\n request: Request,\n): Promise<Record<string, unknown> | Response> {\n try {\n return (await request.json()) as Record<string, unknown>;\n } catch (error) {\n logger.error({ err: error }, \"Malformed JSON in request body\");\n return errorResponse(\"Invalid request body\", 400);\n }\n}\n\nfunction requireIntelligenceRuntime(\n runtime: CopilotRuntimeLike,\n): CopilotIntelligenceRuntimeLike | Response {\n if (!isIntelligenceRuntime(runtime)) {\n return errorResponse(\n \"Missing CopilotKitIntelligence configuration. Thread operations require a CopilotKitIntelligence instance to be provided in CopilotRuntime options.\",\n 422,\n );\n }\n\n return runtime;\n}\n\nasync function resolveThreadMutationContext(\n runtime: CopilotIntelligenceRuntimeLike,\n request: Request,\n): Promise<ThreadMutationContext | Response> {\n const body = await parseJsonBody(request);\n if (isHandlerResponse(body)) return body;\n\n const user = await resolveIntelligenceUser({ runtime, request });\n if (isHandlerResponse(user)) return user;\n\n const agentId = body.agentId;\n if (!isValidIdentifier(agentId)) {\n return errorResponse(\"Valid agentId is required\", 400);\n }\n\n return {\n body,\n userId: user.id,\n agentId,\n };\n}\n\nexport async function handleListThreads({\n runtime,\n request,\n}: ThreadsHandlerParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const url = new URL(request.url);\n const agentId = url.searchParams.get(\"agentId\");\n const includeArchived = url.searchParams.get(\"includeArchived\") === \"true\";\n const limitParam = url.searchParams.get(\"limit\");\n const cursor = url.searchParams.get(\"cursor\");\n const user = await resolveIntelligenceUser({\n runtime: intelligenceRuntime,\n request,\n });\n if (isHandlerResponse(user)) return user;\n\n if (!isValidIdentifier(agentId)) {\n return errorResponse(\"Valid agentId query param is required\", 400);\n }\n\n const data = await intelligenceRuntime.intelligence.listThreads({\n userId: user.id,\n agentId,\n ...(includeArchived ? { includeArchived: true } : {}),\n ...(limitParam ? { limit: Number(limitParam) } : {}),\n ...(cursor ? { cursor } : {}),\n });\n\n return Response.json(data);\n } catch (error) {\n logger.error({ err: error }, \"Error listing threads\");\n return errorResponse(\"Failed to list threads\", 500);\n }\n}\n\nexport async function handleUpdateThread({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const mutation = await resolveThreadMutationContext(\n intelligenceRuntime,\n request,\n );\n if (isHandlerResponse(mutation)) return mutation;\n\n const updates = { ...mutation.body };\n delete updates.agentId;\n delete updates.userId;\n\n const thread = await intelligenceRuntime.intelligence.updateThread({\n threadId,\n userId: mutation.userId,\n agentId: mutation.agentId,\n updates,\n });\n\n return Response.json(thread);\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error updating thread\");\n return errorResponse(\"Failed to update thread\", 500);\n }\n}\n\nexport async function handleSubscribeToThreads({\n runtime,\n request,\n}: ThreadsHandlerParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const user = await resolveIntelligenceUser({\n runtime: intelligenceRuntime,\n request,\n });\n if (isHandlerResponse(user)) return user;\n\n const credentials =\n await intelligenceRuntime.intelligence.ɵsubscribeToThreads({\n userId: user.id,\n });\n\n return Response.json({ joinToken: credentials.joinToken });\n } catch (error) {\n logger.error({ err: error }, \"Error subscribing to threads\");\n return errorResponse(\"Failed to subscribe to threads\", 500);\n }\n}\n\nexport async function handleArchiveThread({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const mutation = await resolveThreadMutationContext(\n intelligenceRuntime,\n request,\n );\n if (isHandlerResponse(mutation)) return mutation;\n\n await intelligenceRuntime.intelligence.archiveThread({\n threadId,\n userId: mutation.userId,\n agentId: mutation.agentId,\n });\n\n return Response.json({ threadId, archived: true });\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error archiving thread\");\n return errorResponse(\"Failed to archive thread\", 500);\n }\n}\n\nexport async function handleDeleteThread({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const mutation = await resolveThreadMutationContext(\n intelligenceRuntime,\n request,\n );\n if (isHandlerResponse(mutation)) return mutation;\n\n await intelligenceRuntime.intelligence.deleteThread({\n threadId,\n userId: mutation.userId,\n agentId: mutation.agentId,\n });\n\n return Response.json({ threadId, deleted: true });\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error deleting thread\");\n return errorResponse(\"Failed to delete thread\", 500);\n }\n}\n\nexport async function handleGetThreadMessages({\n runtime,\n request,\n threadId,\n}: ThreadMutationParams): Promise<Response> {\n const intelligenceRuntime = requireIntelligenceRuntime(runtime);\n if (isHandlerResponse(intelligenceRuntime)) {\n return intelligenceRuntime;\n }\n\n try {\n const user = await resolveIntelligenceUser({\n runtime: intelligenceRuntime,\n request,\n });\n if (isHandlerResponse(user)) return user;\n\n const data = await intelligenceRuntime.intelligence.getThreadMessages({\n threadId,\n });\n\n return Response.json(data);\n } catch (error) {\n logger.error({ err: error, threadId }, \"Error getting thread messages\");\n return errorResponse(\"Failed to get thread messages\", 500);\n }\n}\n"],"mappings":";;;;;;;;AAyBA,eAAe,cACb,SAC6C;AAC7C,KAAI;AACF,SAAQ,MAAM,QAAQ,MAAM;UACrB,OAAO;AACd,SAAO,MAAM,EAAE,KAAK,OAAO,EAAE,iCAAiC;AAC9D,SAAO,cAAc,wBAAwB,IAAI;;;AAIrD,SAAS,2BACP,SAC2C;AAC3C,KAAI,CAAC,sBAAsB,QAAQ,CACjC,QAAO,cACL,uJACA,IACD;AAGH,QAAO;;AAGT,eAAe,6BACb,SACA,SAC2C;CAC3C,MAAM,OAAO,MAAM,cAAc,QAAQ;AACzC,KAAI,kBAAkB,KAAK,CAAE,QAAO;CAEpC,MAAM,OAAO,MAAM,wBAAwB;EAAE;EAAS;EAAS,CAAC;AAChE,KAAI,kBAAkB,KAAK,CAAE,QAAO;CAEpC,MAAM,UAAU,KAAK;AACrB,KAAI,CAAC,kBAAkB,QAAQ,CAC7B,QAAO,cAAc,6BAA6B,IAAI;AAGxD,QAAO;EACL;EACA,QAAQ,KAAK;EACb;EACD;;AAGH,eAAsB,kBAAkB,EACtC,SACA,WAC0C;CAC1C,MAAM,sBAAsB,2BAA2B,QAAQ;AAC/D,KAAI,kBAAkB,oBAAoB,CACxC,QAAO;AAGT,KAAI;EACF,MAAM,MAAM,IAAI,IAAI,QAAQ,IAAI;EAChC,MAAM,UAAU,IAAI,aAAa,IAAI,UAAU;EAC/C,MAAM,kBAAkB,IAAI,aAAa,IAAI,kBAAkB,KAAK;EACpE,MAAM,aAAa,IAAI,aAAa,IAAI,QAAQ;EAChD,MAAM,SAAS,IAAI,aAAa,IAAI,SAAS;EAC7C,MAAM,OAAO,MAAM,wBAAwB;GACzC,SAAS;GACT;GACD,CAAC;AACF,MAAI,kBAAkB,KAAK,CAAE,QAAO;AAEpC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AO;AAGT,KAAI;EACF,MAAM,OAAO,MAAM,wBAAwB;GACzC,SAAS;GACT;GACD,CAAC;AACF,MAAI,kBAAkB,KAAK,CAAE,QAAO;EAEpC,MAAM,OAAO,MAAM,oBAAoB,aAAa,kBAAkB,EACpE,UACD,CAAC;AAEF,SAAO,SAAS,KAAK,KAAK;UACnB,OAAO;AACd,SAAO,MAAM;GAAE,KAAK;GAAO;GAAU,EAAE,gCAAgC;AACvE,SAAO,cAAc,iCAAiC,IAAI"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/runtime",
|
|
3
|
-
"version": "1.55.2
|
|
3
|
+
"version": "1.55.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
"uuid": "^10.0.0",
|
|
116
116
|
"ws": "^8.18.0",
|
|
117
117
|
"zod": "^3.23.3",
|
|
118
|
-
"@copilotkit/shared": "1.55.2
|
|
118
|
+
"@copilotkit/shared": "1.55.2"
|
|
119
119
|
},
|
|
120
120
|
"devDependencies": {
|
|
121
121
|
"@copilotkit/aimock": "^1.10.0",
|
|
@@ -50,6 +50,57 @@ describe("fetch-router", () => {
|
|
|
50
50
|
expect(result).toBeNull();
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
+
it("matches GET /threads", () => {
|
|
54
|
+
const result = matchRoute("/api/copilotkit/threads", basePath);
|
|
55
|
+
expect(result).toEqual({ method: "threads/list" });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("matches POST /threads/subscribe", () => {
|
|
59
|
+
const result = matchRoute("/api/copilotkit/threads/subscribe", basePath);
|
|
60
|
+
expect(result).toEqual({ method: "threads/subscribe" });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("matches PATCH /threads/:threadId", () => {
|
|
64
|
+
const result = matchRoute("/api/copilotkit/threads/thread-abc", basePath);
|
|
65
|
+
expect(result).toEqual({
|
|
66
|
+
method: "threads/update",
|
|
67
|
+
threadId: "thread-abc",
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("matches POST /threads/:threadId/archive", () => {
|
|
72
|
+
const result = matchRoute(
|
|
73
|
+
"/api/copilotkit/threads/thread-abc/archive",
|
|
74
|
+
basePath,
|
|
75
|
+
);
|
|
76
|
+
expect(result).toEqual({
|
|
77
|
+
method: "threads/archive",
|
|
78
|
+
threadId: "thread-abc",
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("matches GET /threads/:threadId/messages", () => {
|
|
83
|
+
const result = matchRoute(
|
|
84
|
+
"/api/copilotkit/threads/thread-abc/messages",
|
|
85
|
+
basePath,
|
|
86
|
+
);
|
|
87
|
+
expect(result).toEqual({
|
|
88
|
+
method: "threads/messages",
|
|
89
|
+
threadId: "thread-abc",
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("handles URL-encoded threadId in thread routes", () => {
|
|
94
|
+
const result = matchRoute(
|
|
95
|
+
"/api/copilotkit/threads/thread%2F123",
|
|
96
|
+
basePath,
|
|
97
|
+
);
|
|
98
|
+
expect(result).toEqual({
|
|
99
|
+
method: "threads/update",
|
|
100
|
+
threadId: "thread/123",
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
53
104
|
it("returns null when basePath is a prefix but not a segment boundary", () => {
|
|
54
105
|
const result = matchRoute("/api/copilotkitextra/info", basePath);
|
|
55
106
|
expect(result).toBeNull();
|
|
@@ -124,6 +175,31 @@ describe("fetch-router", () => {
|
|
|
124
175
|
expect(result).toBeNull();
|
|
125
176
|
});
|
|
126
177
|
|
|
178
|
+
it("matches /threads suffix", () => {
|
|
179
|
+
const result = matchRoute("/anything/threads");
|
|
180
|
+
expect(result).toEqual({ method: "threads/list" });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("matches /threads/subscribe suffix", () => {
|
|
184
|
+
const result = matchRoute("/anything/threads/subscribe");
|
|
185
|
+
expect(result).toEqual({ method: "threads/subscribe" });
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("matches /threads/:threadId suffix", () => {
|
|
189
|
+
const result = matchRoute("/anything/threads/t1");
|
|
190
|
+
expect(result).toEqual({ method: "threads/update", threadId: "t1" });
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("matches /threads/:threadId/archive suffix", () => {
|
|
194
|
+
const result = matchRoute("/anything/threads/t1/archive");
|
|
195
|
+
expect(result).toEqual({ method: "threads/archive", threadId: "t1" });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("matches /threads/:threadId/messages suffix", () => {
|
|
199
|
+
const result = matchRoute("/anything/threads/t1/messages");
|
|
200
|
+
expect(result).toEqual({ method: "threads/messages", threadId: "t1" });
|
|
201
|
+
});
|
|
202
|
+
|
|
127
203
|
it("works with deeply nested mount prefix", () => {
|
|
128
204
|
const result = matchRoute("/api/v2/copilotkit/agent/a1/run");
|
|
129
205
|
expect(result).toEqual({ method: "agent/run", agentId: "a1" });
|
|
@@ -46,6 +46,14 @@ import { handleConnectAgent } from "../handlers/handle-connect";
|
|
|
46
46
|
import { handleStopAgent } from "../handlers/handle-stop";
|
|
47
47
|
import { handleGetRuntimeInfo } from "../handlers/get-runtime-info";
|
|
48
48
|
import { handleTranscribe } from "../handlers/handle-transcribe";
|
|
49
|
+
import {
|
|
50
|
+
handleListThreads,
|
|
51
|
+
handleSubscribeToThreads,
|
|
52
|
+
handleUpdateThread,
|
|
53
|
+
handleArchiveThread,
|
|
54
|
+
handleDeleteThread,
|
|
55
|
+
handleGetThreadMessages,
|
|
56
|
+
} from "../handlers/handle-threads";
|
|
49
57
|
import {
|
|
50
58
|
parseMethodCall,
|
|
51
59
|
createJsonRequest,
|
|
@@ -306,6 +314,31 @@ function dispatchRoute(
|
|
|
306
314
|
return handleGetRuntimeInfo({ runtime, request });
|
|
307
315
|
case "transcribe":
|
|
308
316
|
return handleTranscribe({ runtime, request });
|
|
317
|
+
case "threads/list":
|
|
318
|
+
return handleListThreads({ runtime, request });
|
|
319
|
+
case "threads/subscribe":
|
|
320
|
+
return handleSubscribeToThreads({ runtime, request });
|
|
321
|
+
case "threads/update":
|
|
322
|
+
if (request.method.toUpperCase() === "DELETE") {
|
|
323
|
+
return handleDeleteThread({
|
|
324
|
+
runtime,
|
|
325
|
+
request,
|
|
326
|
+
threadId: route.threadId,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
return handleUpdateThread({ runtime, request, threadId: route.threadId });
|
|
330
|
+
case "threads/archive":
|
|
331
|
+
return handleArchiveThread({
|
|
332
|
+
runtime,
|
|
333
|
+
request,
|
|
334
|
+
threadId: route.threadId,
|
|
335
|
+
});
|
|
336
|
+
case "threads/messages":
|
|
337
|
+
return handleGetThreadMessages({
|
|
338
|
+
runtime,
|
|
339
|
+
request,
|
|
340
|
+
threadId: route.threadId,
|
|
341
|
+
});
|
|
309
342
|
}
|
|
310
343
|
}
|
|
311
344
|
|
|
@@ -376,10 +409,28 @@ function validateHttpMethod(
|
|
|
376
409
|
route: RouteInfo,
|
|
377
410
|
): Response | null {
|
|
378
411
|
const method = httpMethod.toUpperCase();
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
412
|
+
|
|
413
|
+
switch (route.method) {
|
|
414
|
+
case "info":
|
|
415
|
+
case "threads/list":
|
|
416
|
+
case "threads/messages":
|
|
417
|
+
if (method === "GET") return null;
|
|
418
|
+
return jsonResponse({ error: "Method not allowed" }, 405, {
|
|
419
|
+
Allow: "GET",
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
case "threads/update":
|
|
423
|
+
if (method === "PATCH" || method === "DELETE") return null;
|
|
424
|
+
return jsonResponse({ error: "Method not allowed" }, 405, {
|
|
425
|
+
Allow: "PATCH, DELETE",
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
default:
|
|
429
|
+
if (method === "POST") return null;
|
|
430
|
+
return jsonResponse({ error: "Method not allowed" }, 405, {
|
|
431
|
+
Allow: "POST",
|
|
432
|
+
});
|
|
433
|
+
}
|
|
383
434
|
}
|
|
384
435
|
|
|
385
436
|
/* ------------------------------------------------------------------------------------------------
|
|
@@ -108,5 +108,53 @@ function matchSegments(path: string): RouteInfo | null {
|
|
|
108
108
|
return { method: "agent/stop", agentId, threadId };
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
// /threads/subscribe (2 segments)
|
|
112
|
+
if (
|
|
113
|
+
len >= 2 &&
|
|
114
|
+
segments[len - 2] === "threads" &&
|
|
115
|
+
segments[len - 1] === "subscribe"
|
|
116
|
+
) {
|
|
117
|
+
return { method: "threads/subscribe" };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// /threads/:threadId/messages (3 segments)
|
|
121
|
+
if (
|
|
122
|
+
len >= 3 &&
|
|
123
|
+
segments[len - 3] === "threads" &&
|
|
124
|
+
segments[len - 1] === "messages"
|
|
125
|
+
) {
|
|
126
|
+
const threadId = safeDecodeURIComponent(segments[len - 2]!);
|
|
127
|
+
if (!threadId) return null;
|
|
128
|
+
return { method: "threads/messages", threadId };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// /threads/:threadId/archive (3 segments)
|
|
132
|
+
if (
|
|
133
|
+
len >= 3 &&
|
|
134
|
+
segments[len - 3] === "threads" &&
|
|
135
|
+
segments[len - 1] === "archive"
|
|
136
|
+
) {
|
|
137
|
+
const threadId = safeDecodeURIComponent(segments[len - 2]!);
|
|
138
|
+
if (!threadId) return null;
|
|
139
|
+
return { method: "threads/archive", threadId };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// /threads/:threadId (2 segments) — update or delete
|
|
143
|
+
if (
|
|
144
|
+
len >= 2 &&
|
|
145
|
+
segments[len - 2] === "threads" &&
|
|
146
|
+
segments[len - 1] !== "subscribe"
|
|
147
|
+
) {
|
|
148
|
+
const threadId = safeDecodeURIComponent(segments[len - 1]!);
|
|
149
|
+
if (!threadId) return null;
|
|
150
|
+
// Disambiguated by HTTP method in the handler
|
|
151
|
+
return { method: "threads/update", threadId };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// /threads (1 segment) — list
|
|
155
|
+
if (len >= 1 && segments[len - 1] === "threads") {
|
|
156
|
+
return { method: "threads/list" };
|
|
157
|
+
}
|
|
158
|
+
|
|
111
159
|
return null;
|
|
112
160
|
}
|
|
@@ -38,7 +38,12 @@ export type RouteInfo =
|
|
|
38
38
|
| { method: "agent/connect"; agentId: string }
|
|
39
39
|
| { method: "agent/stop"; agentId: string; threadId: string }
|
|
40
40
|
| { method: "info" }
|
|
41
|
-
| { method: "transcribe" }
|
|
41
|
+
| { method: "transcribe" }
|
|
42
|
+
| { method: "threads/list" }
|
|
43
|
+
| { method: "threads/subscribe" }
|
|
44
|
+
| { method: "threads/update"; threadId: string }
|
|
45
|
+
| { method: "threads/archive"; threadId: string }
|
|
46
|
+
| { method: "threads/messages"; threadId: string };
|
|
42
47
|
|
|
43
48
|
/* ------------------------------------------------------------------------------------------------
|
|
44
49
|
* Hook contexts
|