@cogitator-ai/tetsu 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 +21 -0
- package/README.md +268 -0
- package/dist/auth.d.ts +25 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +30 -0
- package/dist/auth.js.map +1 -0
- package/dist/controller.d.ts +804 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/controller.js +476 -0
- package/dist/controller.js.map +1 -0
- package/dist/errors.d.ts +32 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +70 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/operations.d.ts +41 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/operations.js +162 -0
- package/dist/operations.js.map +1 -0
- package/dist/schemas.d.ts +305 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +185 -0
- package/dist/schemas.js.map +1 -0
- package/dist/socket.d.ts +40 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +120 -0
- package/dist/socket.js.map +1 -0
- package/dist/streaming.d.ts +16 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +82 -0
- package/dist/streaming.js.map +1 -0
- package/dist/types.d.ts +84 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +72 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EACV,OAAO,EAMR,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA6C7B,OAAO,KAAK,EAAe,aAAa,EAAE,MAAM,YAAY,CAAC;AAsC7D;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgV9B,CAAC"}
|
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import { controller, route } from '@tetsujs/core';
|
|
2
|
+
import { sse } from '@tetsujs/sse';
|
|
3
|
+
import { countMessageTokens } from '@cogitator-ai/memory';
|
|
4
|
+
import { createErrorEvent, createFinishEvent, createStartEvent, createSwarmEvent, createTextDeltaEvent, createTextEndEvent, createTextStartEvent, createToolCallDeltaEvent, createToolCallEndEvent, createToolCallStartEvent, createToolResultEvent, createWorkflowEvent, generateId, } from '@cogitator-ai/server-shared';
|
|
5
|
+
import { httpError } from '@tetsujs/core';
|
|
6
|
+
import { resolveCaller } from './auth.js';
|
|
7
|
+
import { clientClosedRequest, cogitatorErrors, describeError } from './errors.js';
|
|
8
|
+
import { checkThreadAccess, executeSwarm, executeWorkflow, findAgent, findSwarm, findWorkflow, listAgents, listSwarms, listTools, listWorkflows, runAgent, serializeSwarmUsage, toAgentRunResponse, toSwarmRunResponse, toWorkflowRunResponse, } from './operations.js';
|
|
9
|
+
import { AddMessageBody, AddMessageResponse, AgentListResponse, AgentRunResponse, BlackboardResponse, errorEnvelope, failureEnvelope, HealthResponse, NameParams, ReadyResponse, RUN_FAILURES, RunBody, SwarmListResponse, SwarmRunBody, SwarmRunResponse, ThreadParams, ThreadResponse, ToolListResponse, WorkflowListResponse, WorkflowRunBody, WorkflowRunResponse, } from './schemas.js';
|
|
10
|
+
import { cogitatorSocket } from './socket.js';
|
|
11
|
+
import { DONE_EVENT, eventsOf, resolveSignal, sseEvent } from './streaming.js';
|
|
12
|
+
const AgentNotFound = errorEnvelope('AGENT_NOT_FOUND', 'No agent is registered under this name');
|
|
13
|
+
const WorkflowNotFound = errorEnvelope('WORKFLOW_NOT_FOUND', 'No workflow is registered under this name');
|
|
14
|
+
const SwarmNotFound = errorEnvelope('SWARM_NOT_FOUND', 'No swarm is registered under this name');
|
|
15
|
+
const RunNotFound = failureEnvelope(404, 'No agent, workflow or swarm is registered under this name, or the agent called a tool it does not have', ['SWARM_NOT_FOUND']);
|
|
16
|
+
const NotImplemented = failureEnvelope(501, 'The optional package this endpoint needs is not installed', ['PACKAGE_NOT_INSTALLED']);
|
|
17
|
+
const ThreadForbidden = errorEnvelope('THREAD_FORBIDDEN', 'The caller may not use this memory thread');
|
|
18
|
+
const MemoryNotConfigured = errorEnvelope('MEMORY_NOT_CONFIGURED', 'The Cogitator runtime has no memory adapter');
|
|
19
|
+
const BlackboardDisabled = errorEnvelope('BLACKBOARD_DISABLED', 'The blackboard is not enabled for this swarm');
|
|
20
|
+
const STREAM_DESCRIPTION = 'Server-sent events in the Cogitator stream protocol, one JSON event per `data:` line, ending with `data: [DONE]`.';
|
|
21
|
+
/**
|
|
22
|
+
* The Cogitator HTTP API as a Tetsu controller.
|
|
23
|
+
*
|
|
24
|
+
* Mount it like any controller, usually under a group:
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const app = createApp({
|
|
29
|
+
* routes: [
|
|
30
|
+
* group('/cogitator', { children: [cogitatorController({ cogitator, agents: { chat } })] }),
|
|
31
|
+
* docs({ info: { title: 'Agents', version: '1.0.0' } }),
|
|
32
|
+
* ],
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* Bun.serve({ ...app, port: 3000 });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const cogitatorController = controller('Cogitator', (deps) => {
|
|
39
|
+
const builtAt = Date.now();
|
|
40
|
+
const caller = resolveCaller(deps.auth);
|
|
41
|
+
const errors = cogitatorErrors();
|
|
42
|
+
const until = () => resolveSignal(deps.until);
|
|
43
|
+
const memoryOf = () => {
|
|
44
|
+
const memory = deps.cogitator.memory;
|
|
45
|
+
if (!memory) {
|
|
46
|
+
throw httpError(503, 'MEMORY_NOT_CONFIGURED', 'The Cogitator runtime has no memory adapter');
|
|
47
|
+
}
|
|
48
|
+
return memory;
|
|
49
|
+
};
|
|
50
|
+
const routes = {
|
|
51
|
+
health: route({
|
|
52
|
+
method: 'GET',
|
|
53
|
+
path: '/health',
|
|
54
|
+
schema: { response: HealthResponse },
|
|
55
|
+
hooks: { onError: [errors] },
|
|
56
|
+
docs: { summary: 'Liveness check', tags: ['health'] },
|
|
57
|
+
handler: () => ({
|
|
58
|
+
status: 'ok',
|
|
59
|
+
uptime: Date.now() - builtAt,
|
|
60
|
+
timestamp: Date.now(),
|
|
61
|
+
}),
|
|
62
|
+
}),
|
|
63
|
+
ready: route({
|
|
64
|
+
method: 'GET',
|
|
65
|
+
path: '/ready',
|
|
66
|
+
schema: { response: ReadyResponse },
|
|
67
|
+
hooks: { onError: [errors] },
|
|
68
|
+
docs: { summary: 'Readiness check', tags: ['health'] },
|
|
69
|
+
handler: () => ({ status: 'ok' }),
|
|
70
|
+
}),
|
|
71
|
+
listAgents: route({
|
|
72
|
+
method: 'GET',
|
|
73
|
+
path: '/agents',
|
|
74
|
+
schema: { response: AgentListResponse },
|
|
75
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
76
|
+
docs: { summary: 'List agents', tags: ['agents'] },
|
|
77
|
+
handler: () => listAgents(deps),
|
|
78
|
+
}),
|
|
79
|
+
runAgent: route({
|
|
80
|
+
method: 'POST',
|
|
81
|
+
path: '/agents/:name/run',
|
|
82
|
+
schema: {
|
|
83
|
+
params: NameParams,
|
|
84
|
+
body: RunBody,
|
|
85
|
+
response: {
|
|
86
|
+
...RUN_FAILURES,
|
|
87
|
+
200: AgentRunResponse,
|
|
88
|
+
403: ThreadForbidden,
|
|
89
|
+
404: RunNotFound,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
93
|
+
docs: { summary: 'Run an agent and wait for its answer', tags: ['agents'] },
|
|
94
|
+
handler: async (ctx) => {
|
|
95
|
+
const agent = findAgent(deps, ctx.params.name);
|
|
96
|
+
await checkThreadAccess(deps, ctx.cogitatorAuth, ctx.body.threadId);
|
|
97
|
+
const signal = ctx.req.signal;
|
|
98
|
+
try {
|
|
99
|
+
const result = await runAgent(deps, agent, { ...ctx.body, signal }, ctx.cogitatorAuth);
|
|
100
|
+
return toAgentRunResponse(result);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (signal.aborted)
|
|
104
|
+
throw clientClosedRequest();
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
}),
|
|
109
|
+
streamAgent: route({
|
|
110
|
+
method: 'POST',
|
|
111
|
+
path: '/agents/:name/stream',
|
|
112
|
+
schema: {
|
|
113
|
+
params: NameParams,
|
|
114
|
+
body: RunBody,
|
|
115
|
+
response: { 403: ThreadForbidden, 404: AgentNotFound },
|
|
116
|
+
},
|
|
117
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
118
|
+
docs: {
|
|
119
|
+
summary: 'Run an agent and stream tokens, tool calls and results',
|
|
120
|
+
description: STREAM_DESCRIPTION,
|
|
121
|
+
tags: ['agents'],
|
|
122
|
+
},
|
|
123
|
+
handler: async (ctx) => {
|
|
124
|
+
const agent = findAgent(deps, ctx.params.name);
|
|
125
|
+
const auth = ctx.cogitatorAuth;
|
|
126
|
+
const body = ctx.body;
|
|
127
|
+
await checkThreadAccess(deps, auth, body.threadId);
|
|
128
|
+
return sse(ctx, (signal) => agentEvents(deps, agent, body, auth, signal), {
|
|
129
|
+
until: until(),
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
getThread: route({
|
|
134
|
+
method: 'GET',
|
|
135
|
+
path: '/threads/:id',
|
|
136
|
+
schema: {
|
|
137
|
+
params: ThreadParams,
|
|
138
|
+
response: { 200: ThreadResponse, 403: ThreadForbidden, 503: MemoryNotConfigured },
|
|
139
|
+
},
|
|
140
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
141
|
+
docs: { summary: 'Read the messages of a memory thread', tags: ['threads'] },
|
|
142
|
+
handler: async (ctx) => {
|
|
143
|
+
const memory = memoryOf();
|
|
144
|
+
const id = ctx.params.id;
|
|
145
|
+
await checkThreadAccess(deps, ctx.cogitatorAuth, id);
|
|
146
|
+
const result = await memory.getEntries({ threadId: id });
|
|
147
|
+
if (!result.success)
|
|
148
|
+
throw new Error(result.error);
|
|
149
|
+
const entries = result.data;
|
|
150
|
+
const now = Date.now();
|
|
151
|
+
return {
|
|
152
|
+
id,
|
|
153
|
+
messages: entries.map((entry) => entry.message),
|
|
154
|
+
createdAt: entries.at(0)?.createdAt.getTime() ?? now,
|
|
155
|
+
updatedAt: entries.at(-1)?.createdAt.getTime() ?? now,
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
addThreadMessage: route({
|
|
160
|
+
method: 'POST',
|
|
161
|
+
path: '/threads/:id/messages',
|
|
162
|
+
schema: {
|
|
163
|
+
params: ThreadParams,
|
|
164
|
+
body: AddMessageBody,
|
|
165
|
+
response: { 201: AddMessageResponse, 403: ThreadForbidden, 503: MemoryNotConfigured },
|
|
166
|
+
},
|
|
167
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
168
|
+
docs: { summary: 'Append a message to a memory thread', tags: ['threads'] },
|
|
169
|
+
handler: async (ctx) => {
|
|
170
|
+
const memory = memoryOf();
|
|
171
|
+
const id = ctx.params.id;
|
|
172
|
+
await checkThreadAccess(deps, ctx.cogitatorAuth, id);
|
|
173
|
+
const message = { role: ctx.body.role, content: ctx.body.content };
|
|
174
|
+
const result = await memory.addEntry({
|
|
175
|
+
threadId: id,
|
|
176
|
+
message,
|
|
177
|
+
tokenCount: countMessageTokens(message),
|
|
178
|
+
...(ctx.body.metadata && { metadata: ctx.body.metadata }),
|
|
179
|
+
});
|
|
180
|
+
if (!result.success)
|
|
181
|
+
throw new Error(result.error);
|
|
182
|
+
ctx.out.status = 201;
|
|
183
|
+
return { success: true };
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
deleteThread: route({
|
|
187
|
+
method: 'DELETE',
|
|
188
|
+
path: '/threads/:id',
|
|
189
|
+
schema: {
|
|
190
|
+
params: ThreadParams,
|
|
191
|
+
response: { 204: null, 403: ThreadForbidden, 503: MemoryNotConfigured },
|
|
192
|
+
},
|
|
193
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
194
|
+
docs: { summary: 'Delete a memory thread', tags: ['threads'] },
|
|
195
|
+
handler: async (ctx) => {
|
|
196
|
+
const memory = memoryOf();
|
|
197
|
+
await checkThreadAccess(deps, ctx.cogitatorAuth, ctx.params.id);
|
|
198
|
+
const result = await memory.clearThread(ctx.params.id);
|
|
199
|
+
if (!result.success)
|
|
200
|
+
throw new Error(result.error);
|
|
201
|
+
},
|
|
202
|
+
}),
|
|
203
|
+
listTools: route({
|
|
204
|
+
method: 'GET',
|
|
205
|
+
path: '/tools',
|
|
206
|
+
schema: { response: ToolListResponse },
|
|
207
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
208
|
+
docs: { summary: 'List the tools of every agent', tags: ['agents'] },
|
|
209
|
+
handler: () => listTools(deps),
|
|
210
|
+
}),
|
|
211
|
+
listWorkflows: route({
|
|
212
|
+
method: 'GET',
|
|
213
|
+
path: '/workflows',
|
|
214
|
+
schema: { response: WorkflowListResponse },
|
|
215
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
216
|
+
docs: { summary: 'List workflows', tags: ['workflows'] },
|
|
217
|
+
handler: () => listWorkflows(deps),
|
|
218
|
+
}),
|
|
219
|
+
runWorkflow: route({
|
|
220
|
+
method: 'POST',
|
|
221
|
+
path: '/workflows/:name/run',
|
|
222
|
+
schema: {
|
|
223
|
+
params: NameParams,
|
|
224
|
+
body: WorkflowRunBody,
|
|
225
|
+
response: {
|
|
226
|
+
...RUN_FAILURES,
|
|
227
|
+
200: WorkflowRunResponse,
|
|
228
|
+
404: RunNotFound,
|
|
229
|
+
501: NotImplemented,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
233
|
+
docs: { summary: 'Run a workflow and wait for its final state', tags: ['workflows'] },
|
|
234
|
+
handler: async (ctx) => {
|
|
235
|
+
const workflow = findWorkflow(deps, ctx.params.name);
|
|
236
|
+
const signal = ctx.req.signal;
|
|
237
|
+
try {
|
|
238
|
+
const result = await executeWorkflow(deps, workflow, ctx.body.input, ctx.body.options ?? {}, signal);
|
|
239
|
+
return toWorkflowRunResponse(result);
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
if (signal.aborted)
|
|
243
|
+
throw clientClosedRequest();
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
}),
|
|
248
|
+
streamWorkflow: route({
|
|
249
|
+
method: 'POST',
|
|
250
|
+
path: '/workflows/:name/stream',
|
|
251
|
+
schema: {
|
|
252
|
+
params: NameParams,
|
|
253
|
+
body: WorkflowRunBody,
|
|
254
|
+
response: { 404: WorkflowNotFound },
|
|
255
|
+
},
|
|
256
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
257
|
+
docs: {
|
|
258
|
+
summary: 'Run a workflow and stream node events',
|
|
259
|
+
description: STREAM_DESCRIPTION,
|
|
260
|
+
tags: ['workflows'],
|
|
261
|
+
},
|
|
262
|
+
handler: (ctx) => {
|
|
263
|
+
const workflow = findWorkflow(deps, ctx.params.name);
|
|
264
|
+
const body = ctx.body;
|
|
265
|
+
return sse(ctx, (signal) => workflowEvents(deps, workflow, body, signal), {
|
|
266
|
+
until: until(),
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
}),
|
|
270
|
+
listSwarms: route({
|
|
271
|
+
method: 'GET',
|
|
272
|
+
path: '/swarms',
|
|
273
|
+
schema: { response: SwarmListResponse },
|
|
274
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
275
|
+
docs: { summary: 'List swarms', tags: ['swarms'] },
|
|
276
|
+
handler: () => listSwarms(deps),
|
|
277
|
+
}),
|
|
278
|
+
runSwarm: route({
|
|
279
|
+
method: 'POST',
|
|
280
|
+
path: '/swarms/:name/run',
|
|
281
|
+
schema: {
|
|
282
|
+
params: NameParams,
|
|
283
|
+
body: SwarmRunBody,
|
|
284
|
+
response: {
|
|
285
|
+
...RUN_FAILURES,
|
|
286
|
+
200: SwarmRunResponse,
|
|
287
|
+
403: ThreadForbidden,
|
|
288
|
+
404: RunNotFound,
|
|
289
|
+
501: NotImplemented,
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
293
|
+
docs: { summary: 'Run a swarm and wait for its result', tags: ['swarms'] },
|
|
294
|
+
handler: async (ctx) => {
|
|
295
|
+
const config = findSwarm(deps, ctx.params.name);
|
|
296
|
+
await checkThreadAccess(deps, ctx.cogitatorAuth, ctx.body.threadId);
|
|
297
|
+
const signal = ctx.req.signal;
|
|
298
|
+
try {
|
|
299
|
+
const { swarm, result } = await executeSwarm(deps, config, ctx.body, signal);
|
|
300
|
+
if (signal.aborted)
|
|
301
|
+
throw clientClosedRequest();
|
|
302
|
+
return toSwarmRunResponse(swarm, result);
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
if (signal.aborted)
|
|
306
|
+
throw clientClosedRequest();
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
}),
|
|
311
|
+
streamSwarm: route({
|
|
312
|
+
method: 'POST',
|
|
313
|
+
path: '/swarms/:name/stream',
|
|
314
|
+
schema: {
|
|
315
|
+
params: NameParams,
|
|
316
|
+
body: SwarmRunBody,
|
|
317
|
+
response: { 403: ThreadForbidden, 404: SwarmNotFound },
|
|
318
|
+
},
|
|
319
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
320
|
+
docs: {
|
|
321
|
+
summary: 'Run a swarm and stream agent events',
|
|
322
|
+
description: STREAM_DESCRIPTION,
|
|
323
|
+
tags: ['swarms'],
|
|
324
|
+
},
|
|
325
|
+
handler: async (ctx) => {
|
|
326
|
+
const config = findSwarm(deps, ctx.params.name);
|
|
327
|
+
const body = ctx.body;
|
|
328
|
+
await checkThreadAccess(deps, ctx.cogitatorAuth, body.threadId);
|
|
329
|
+
return sse(ctx, (signal) => swarmEvents(deps, config, body, signal), {
|
|
330
|
+
until: until(),
|
|
331
|
+
});
|
|
332
|
+
},
|
|
333
|
+
}),
|
|
334
|
+
getBlackboard: route({
|
|
335
|
+
method: 'GET',
|
|
336
|
+
path: '/swarms/:name/blackboard',
|
|
337
|
+
schema: {
|
|
338
|
+
params: NameParams,
|
|
339
|
+
response: { 200: BlackboardResponse, 404: SwarmNotFound, 409: BlackboardDisabled },
|
|
340
|
+
},
|
|
341
|
+
hooks: { beforeParse: [caller], onError: [errors] },
|
|
342
|
+
docs: { summary: 'Read the configured blackboard sections of a swarm', tags: ['swarms'] },
|
|
343
|
+
handler: (ctx) => {
|
|
344
|
+
const config = findSwarm(deps, ctx.params.name);
|
|
345
|
+
if (!config.blackboard?.enabled) {
|
|
346
|
+
throw httpError(409, 'BLACKBOARD_DISABLED', 'The blackboard is not enabled for this swarm');
|
|
347
|
+
}
|
|
348
|
+
return { sections: config.blackboard.sections ?? {} };
|
|
349
|
+
},
|
|
350
|
+
}),
|
|
351
|
+
};
|
|
352
|
+
const socket = deps.websocket
|
|
353
|
+
? cogitatorSocket(deps, caller, deps.websocket === true ? undefined : deps.websocket.path)
|
|
354
|
+
: undefined;
|
|
355
|
+
return { ...routes, ...(socket && { socket }) };
|
|
356
|
+
});
|
|
357
|
+
function* failure(error) {
|
|
358
|
+
const described = describeError(error);
|
|
359
|
+
yield sseEvent(createErrorEvent(described.message, described.code));
|
|
360
|
+
if (described.unexpected)
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
async function* agentEvents(deps, agent, body, auth, signal) {
|
|
364
|
+
const messageId = generateId('msg');
|
|
365
|
+
const textId = generateId('txt');
|
|
366
|
+
let result;
|
|
367
|
+
yield sseEvent(createStartEvent(messageId));
|
|
368
|
+
yield sseEvent(createTextStartEvent(textId));
|
|
369
|
+
try {
|
|
370
|
+
yield* eventsOf(signal, async (emit) => {
|
|
371
|
+
result = await runAgent(deps, agent, {
|
|
372
|
+
...body,
|
|
373
|
+
stream: true,
|
|
374
|
+
signal,
|
|
375
|
+
onToken: (token) => {
|
|
376
|
+
if (token)
|
|
377
|
+
emit(sseEvent(createTextDeltaEvent(textId, token)));
|
|
378
|
+
},
|
|
379
|
+
onToolCall: (call) => {
|
|
380
|
+
emit(sseEvent(createToolCallStartEvent(call.id, call.name)));
|
|
381
|
+
emit(sseEvent(createToolCallDeltaEvent(call.id, JSON.stringify(call.arguments))));
|
|
382
|
+
emit(sseEvent(createToolCallEndEvent(call.id)));
|
|
383
|
+
},
|
|
384
|
+
onToolResult: (toolResult) => {
|
|
385
|
+
emit(sseEvent(createToolResultEvent(generateId('res'), toolResult.callId, toolResult.result)));
|
|
386
|
+
},
|
|
387
|
+
}, auth);
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
catch (error) {
|
|
391
|
+
if (signal.aborted)
|
|
392
|
+
return;
|
|
393
|
+
yield sseEvent(createTextEndEvent(textId));
|
|
394
|
+
yield* failure(error);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (signal.aborted || !result)
|
|
398
|
+
return;
|
|
399
|
+
yield sseEvent(createTextEndEvent(textId));
|
|
400
|
+
yield sseEvent(createFinishEvent(messageId, {
|
|
401
|
+
inputTokens: result.usage.inputTokens,
|
|
402
|
+
outputTokens: result.usage.outputTokens,
|
|
403
|
+
totalTokens: result.usage.totalTokens,
|
|
404
|
+
}));
|
|
405
|
+
yield DONE_EVENT;
|
|
406
|
+
}
|
|
407
|
+
async function* workflowEvents(deps, workflow, body, signal) {
|
|
408
|
+
const messageId = generateId('wf');
|
|
409
|
+
let result;
|
|
410
|
+
yield sseEvent(createStartEvent(messageId));
|
|
411
|
+
try {
|
|
412
|
+
yield* eventsOf(signal, async (emit) => {
|
|
413
|
+
const event = (name, data) => emit(sseEvent(createWorkflowEvent(name, data)));
|
|
414
|
+
result = await executeWorkflow(deps, workflow, body.input, {
|
|
415
|
+
...body.options,
|
|
416
|
+
onNodeStart: (nodeName) => event('node_started', { nodeName, timestamp: Date.now() }),
|
|
417
|
+
onNodeComplete: (nodeName, output, duration) => event('node_completed', { nodeName, output, duration }),
|
|
418
|
+
onNodeError: (nodeName, error) => event('node_error', { nodeName, error: error.message }),
|
|
419
|
+
onNodeProgress: (nodeName, progress) => event('node_progress', { nodeName, progress }),
|
|
420
|
+
}, signal);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
catch (error) {
|
|
424
|
+
if (signal.aborted)
|
|
425
|
+
return;
|
|
426
|
+
yield* failure(error);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (signal.aborted || !result)
|
|
430
|
+
return;
|
|
431
|
+
yield sseEvent(createWorkflowEvent('workflow_completed', {
|
|
432
|
+
workflowId: result.workflowId,
|
|
433
|
+
duration: result.duration,
|
|
434
|
+
}));
|
|
435
|
+
yield sseEvent(createFinishEvent(messageId));
|
|
436
|
+
yield DONE_EVENT;
|
|
437
|
+
}
|
|
438
|
+
async function* swarmEvents(deps, config, body, signal) {
|
|
439
|
+
const messageId = generateId('swarm');
|
|
440
|
+
let completed;
|
|
441
|
+
yield sseEvent(createStartEvent(messageId));
|
|
442
|
+
try {
|
|
443
|
+
yield* eventsOf(signal, async (emit) => {
|
|
444
|
+
const event = (name, data) => emit(sseEvent(createSwarmEvent(name, data)));
|
|
445
|
+
completed = await executeSwarm(deps, config, {
|
|
446
|
+
...body,
|
|
447
|
+
onAgentStart: (agentName) => event('agent_start', { agentName, timestamp: Date.now() }),
|
|
448
|
+
onAgentComplete: (agentName, agentResult) => event('agent_complete', {
|
|
449
|
+
agentName,
|
|
450
|
+
output: agentResult.output,
|
|
451
|
+
timestamp: Date.now(),
|
|
452
|
+
}),
|
|
453
|
+
onAgentError: (agentName, error) => event('agent_error', { agentName, error: error.message }),
|
|
454
|
+
onMessage: (message) => event('message', message),
|
|
455
|
+
onEvent: (swarmEvent) => event(swarmEvent.type, swarmEvent.data),
|
|
456
|
+
}, signal);
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
catch (error) {
|
|
460
|
+
if (signal.aborted)
|
|
461
|
+
return;
|
|
462
|
+
yield* failure(error);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (signal.aborted || !completed)
|
|
466
|
+
return;
|
|
467
|
+
const { swarm, result } = completed;
|
|
468
|
+
yield sseEvent(createSwarmEvent('swarm_completed', {
|
|
469
|
+
swarmId: swarm.id,
|
|
470
|
+
output: result.output,
|
|
471
|
+
usage: serializeSwarmUsage(swarm.getResourceUsage()),
|
|
472
|
+
}));
|
|
473
|
+
yield sseEvent(createFinishEvent(messageId));
|
|
474
|
+
yield DONE_EVENT;
|
|
475
|
+
}
|
|
476
|
+
//# sourceMappingURL=controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACpB,wBAAwB,EACxB,sBAAsB,EACtB,wBAAwB,EACxB,qBAAqB,EACrB,mBAAmB,EACnB,UAAU,GACX,MAAM,6BAA6B,CAAC;AAUrC,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EACL,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,SAAS,EACT,SAAS,EACT,YAAY,EACZ,UAAU,EACV,UAAU,EACV,SAAS,EACT,aAAa,EACb,QAAQ,EACR,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,iBAAiB,EACjB,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CAAC,SAAS,EAAE,KAAK,EAAE,EAAE,CACjC,KAAK,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;gBAC3D,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC;gBACjD,OAAO,EAAE,CAAC,UAAU,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC;aACjE,EACD,MAAM,CACP,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,MAAM,CAAC,OAAO;YAAE,OAAO;QAC3B,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACtB,OAAO;IACT,CAAC;IAED,IAAI,MAAM,CAAC,OAAO,IAAI,CAAC,SAAS;QAAE,OAAO;IACzC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;IACpC,MAAM,QAAQ,CACZ,gBAAgB,CAAC,iBAAiB,EAAE;QAClC,OAAO,EAAE,KAAK,CAAC,EAAE;QACjB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,KAAK,EAAE,mBAAmB,CAAC,KAAK,CAAC,gBAAgB,EAAE,CAAC;KACrD,CAAC,CACH,CAAC;IACF,MAAM,QAAQ,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,CAAC;IAC7C,MAAM,UAAU,CAAC;AACnB,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { HttpError } from '@tetsujs/core';
|
|
2
|
+
import { CogitatorError } from '@cogitator-ai/types';
|
|
3
|
+
/** The status a request is answered with when its client left before the answer was ready. */
|
|
4
|
+
export declare const CLIENT_CLOSED_REQUEST = 499;
|
|
5
|
+
/** The HTTP status a `CogitatorError` maps to: its own, the one of its code, or `500`. */
|
|
6
|
+
export declare function cogitatorErrorStatus(error: CogitatorError): number;
|
|
7
|
+
/**
|
|
8
|
+
* Answers a `CogitatorError` in the Tetsu envelope `{ status, message, error }`,
|
|
9
|
+
* with `Retry-After` when the error says when to retry.
|
|
10
|
+
*/
|
|
11
|
+
export declare function cogitatorErrorResponse(error: CogitatorError): Response;
|
|
12
|
+
/**
|
|
13
|
+
* An `onError` hook that answers every `CogitatorError` with its status and code.
|
|
14
|
+
*
|
|
15
|
+
* `cogitatorController()` mounts one on each of its routes. Mount another on the
|
|
16
|
+
* application to map errors thrown by your own routes the same way.
|
|
17
|
+
*/
|
|
18
|
+
export declare function cogitatorErrors(): import("@tetsujs/core").Hook<"onError", import("@tetsujs/core").BaseCtx & {
|
|
19
|
+
readonly error: unknown;
|
|
20
|
+
}, unknown>;
|
|
21
|
+
export interface DescribedError {
|
|
22
|
+
message: string;
|
|
23
|
+
code: string;
|
|
24
|
+
/** Whether the error is a failure of the server rather than an answer. */
|
|
25
|
+
unexpected: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** What a stream or a socket tells its client about an error. */
|
|
28
|
+
export declare function describeError(error: unknown): DescribedError;
|
|
29
|
+
export declare function isModuleNotFoundError(error: unknown): boolean;
|
|
30
|
+
export declare function importOptional<T>(load: () => Promise<T>, packageName: string): Promise<T>;
|
|
31
|
+
export declare function clientClosedRequest(): HttpError;
|
|
32
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,SAAS,EAAa,MAAM,eAAe,CAAC;AAEtE,OAAO,EAAE,cAAc,EAAsB,MAAM,qBAAqB,CAAC;AAEzE,8FAA8F;AAC9F,eAAO,MAAM,qBAAqB,MAAM,CAAC;AAMzC,0FAA0F;AAC1F,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,cAAc,GAAG,MAAM,CAGlE;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,cAAc,GAAG,QAAQ,CAOtE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe;;YAI9B;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,CAgB5D;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAG7D;AAED,wBAAsB,cAAc,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAa/F;AAED,wBAAgB,mBAAmB,IAAI,SAAS,CAE/C"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { errorBody, hook, HttpError, httpError } from '@tetsujs/core';
|
|
2
|
+
import { CogitatorError, ERROR_STATUS_CODES } from '@cogitator-ai/types';
|
|
3
|
+
/** The status a request is answered with when its client left before the answer was ready. */
|
|
4
|
+
export const CLIENT_CLOSED_REQUEST = 499;
|
|
5
|
+
function isErrorStatus(status) {
|
|
6
|
+
return Number.isInteger(status) && status >= 400 && status <= 599;
|
|
7
|
+
}
|
|
8
|
+
/** The HTTP status a `CogitatorError` maps to: its own, the one of its code, or `500`. */
|
|
9
|
+
export function cogitatorErrorStatus(error) {
|
|
10
|
+
const status = error.statusCode ?? ERROR_STATUS_CODES[error.code] ?? 500;
|
|
11
|
+
return isErrorStatus(status) ? status : 500;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Answers a `CogitatorError` in the Tetsu envelope `{ status, message, error }`,
|
|
15
|
+
* with `Retry-After` when the error says when to retry.
|
|
16
|
+
*/
|
|
17
|
+
export function cogitatorErrorResponse(error) {
|
|
18
|
+
const status = cogitatorErrorStatus(error);
|
|
19
|
+
const headers = new Headers();
|
|
20
|
+
if (error.retryAfter !== undefined && error.retryAfter > 0) {
|
|
21
|
+
headers.set('retry-after', String(Math.ceil(error.retryAfter / 1000)));
|
|
22
|
+
}
|
|
23
|
+
return Response.json(errorBody(status, error.code, error.message), { status, headers });
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* An `onError` hook that answers every `CogitatorError` with its status and code.
|
|
27
|
+
*
|
|
28
|
+
* `cogitatorController()` mounts one on each of its routes. Mount another on the
|
|
29
|
+
* application to map errors thrown by your own routes the same way.
|
|
30
|
+
*/
|
|
31
|
+
export function cogitatorErrors() {
|
|
32
|
+
return hook.onError((ctx) => CogitatorError.isCogitatorError(ctx.error) ? cogitatorErrorResponse(ctx.error) : undefined);
|
|
33
|
+
}
|
|
34
|
+
/** What a stream or a socket tells its client about an error. */
|
|
35
|
+
export function describeError(error) {
|
|
36
|
+
if (CogitatorError.isCogitatorError(error)) {
|
|
37
|
+
return { message: error.message, code: error.code, unexpected: false };
|
|
38
|
+
}
|
|
39
|
+
if (error instanceof HttpError) {
|
|
40
|
+
const fallback = errorBody(error.status);
|
|
41
|
+
const body = typeof error.body === 'object' && error.body !== null ? error.body : {};
|
|
42
|
+
const message = typeof error.body === 'string' ? error.body : body.message;
|
|
43
|
+
return {
|
|
44
|
+
message: typeof message === 'string' ? message : fallback.message,
|
|
45
|
+
code: typeof body.error === 'string' ? body.error : fallback.error,
|
|
46
|
+
unexpected: false,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return { message: 'Internal server error', code: 'INTERNAL_SERVER_ERROR', unexpected: true };
|
|
50
|
+
}
|
|
51
|
+
export function isModuleNotFoundError(error) {
|
|
52
|
+
if (typeof error !== 'object' || error === null || !('code' in error))
|
|
53
|
+
return false;
|
|
54
|
+
return error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND';
|
|
55
|
+
}
|
|
56
|
+
export async function importOptional(load, packageName) {
|
|
57
|
+
try {
|
|
58
|
+
return await load();
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (isModuleNotFoundError(error)) {
|
|
62
|
+
throw httpError(501, 'PACKAGE_NOT_INSTALLED', `${packageName} is not installed: add it to use this endpoint`);
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function clientClosedRequest() {
|
|
68
|
+
return httpError(CLIENT_CLOSED_REQUEST, 'CLIENT_CLOSED_REQUEST', 'Client closed request');
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAEtE,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzE,8FAA8F;AAC9F,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAEzC,SAAS,aAAa,CAAC,MAAc;IACnC,OAAO,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,IAAI,GAAG,CAAC;AACpE,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,oBAAoB,CAAC,KAAqB;IACxD,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,IAAI,kBAAkB,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC;IACzE,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC;AAC9C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAqB;IAC1D,MAAM,MAAM,GAAG,oBA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AS,CAAC,qBAAqB,EAAE,uBAAuB,EAAE,uBAAuB,CAAC,CAAC;AAC5F,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { cogitatorController } from './controller.js';
|
|
2
|
+
export { callerHook } from './auth.js';
|
|
3
|
+
export type { CallerFields, CallerHook } from './auth.js';
|
|
4
|
+
export { cogitatorErrors, cogitatorErrorResponse, cogitatorErrorStatus, describeError, CLIENT_CLOSED_REQUEST, } from './errors.js';
|
|
5
|
+
export type { DescribedError } from './errors.js';
|
|
6
|
+
export { RunBody, SwarmRunBody, WorkflowRunBody, AddMessageBody, SocketMessage, AgentRunResponse, AgentListResponse, ThreadResponse, ToolListResponse, WorkflowListResponse, WorkflowRunResponse, SwarmListResponse, SwarmRunResponse, BlackboardResponse, HealthResponse, } from './schemas.js';
|
|
7
|
+
export type { AuthContext, Authenticate, AuthorizeThread, CogitatorDeps, ShutdownSignal, WebSocketOptions, AgentRunRequest, SwarmRunRequest, WorkflowRunRequest, AddMessageRequest, HealthResponseBody, AgentListResponseBody, AgentRunResponseBody, ThreadResponseBody, ToolListResponseBody, WorkflowListResponseBody, WorkflowRunResponseBody, SwarmListResponseBody, SwarmRunResponseBody, BlackboardResponseBody, WebSocketClientMessage, WebSocketServerMessage, } from './types.js';
|
|
8
|
+
export type { StreamEvent, Usage } from '@cogitator-ai/server-shared';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC1D,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,oBAAoB,EACpB,aAAa,EACb,qBAAqB,GACtB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,OAAO,EACL,OAAO,EACP,YAAY,EACZ,eAAe,EACf,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,GACf,MAAM,cAAc,CAAC;AAEtB,YAAY,EACV,WAAW,EACX,YAAY,EACZ,eAAe,EACf,aAAa,EACb,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACpB,wBAAwB,EACxB,uBAAuB,EACvB,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAEpB,YAAY,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { cogitatorController } from './controller.js';
|
|
2
|
+
export { callerHook } from './auth.js';
|
|
3
|
+
export { cogitatorErrors, cogitatorErrorResponse, cogitatorErrorStatus, describeError, CLIENT_CLOSED_REQUEST, } from './errors.js';
|
|
4
|
+
export { RunBody, SwarmRunBody, WorkflowRunBody, AddMessageBody, SocketMessage, AgentRunResponse, AgentListResponse, ThreadResponse, ToolListResponse, WorkflowListResponse, WorkflowRunResponse, SwarmListResponse, SwarmRunResponse, BlackboardResponse, HealthResponse, } from './schemas.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|