@cogitator-ai/koa 0.1.13 → 0.2.1
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/README.md +49 -25
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +1 -4
- package/dist/app.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +8 -11
- package/dist/middleware/auth.js.map +1 -1
- package/dist/middleware/body-parser.d.ts +5 -1
- package/dist/middleware/body-parser.d.ts.map +1 -1
- package/dist/middleware/body-parser.js +74 -14
- package/dist/middleware/body-parser.js.map +1 -1
- package/dist/middleware/error-handler.d.ts.map +1 -1
- package/dist/middleware/error-handler.js +23 -7
- package/dist/middleware/error-handler.js.map +1 -1
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/agents.js +38 -34
- package/dist/routes/agents.js.map +1 -1
- package/dist/routes/health.d.ts.map +1 -1
- package/dist/routes/health.js +1 -2
- package/dist/routes/health.js.map +1 -1
- package/dist/routes/swarms.d.ts.map +1 -1
- package/dist/routes/swarms.js +51 -51
- package/dist/routes/swarms.js.map +1 -1
- package/dist/routes/threads.d.ts.map +1 -1
- package/dist/routes/threads.js +33 -37
- package/dist/routes/threads.js.map +1 -1
- package/dist/routes/tools.d.ts.map +1 -1
- package/dist/routes/tools.js +11 -17
- package/dist/routes/tools.js.map +1 -1
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/workflows.js +57 -26
- package/dist/routes/workflows.js.map +1 -1
- package/dist/swagger/index.d.ts.map +1 -1
- package/dist/swagger/index.js +6 -10
- package/dist/swagger/index.js.map +1 -1
- package/dist/types.d.ts +11 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/errors.d.ts +8 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +21 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/lookup.d.ts +2 -0
- package/dist/utils/lookup.d.ts.map +1 -0
- package/dist/utils/lookup.js +4 -0
- package/dist/utils/lookup.js.map +1 -0
- package/dist/utils/request.d.ts +4 -0
- package/dist/utils/request.d.ts.map +1 -0
- package/dist/utils/request.js +11 -0
- package/dist/utils/request.js.map +1 -0
- package/dist/utils/results.d.ts +22 -0
- package/dist/utils/results.d.ts.map +1 -0
- package/dist/utils/results.js +37 -0
- package/dist/utils/results.js.map +1 -0
- package/dist/utils/validation.d.ts +14 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +103 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/websocket/handler.d.ts +1 -1
- package/dist/websocket/handler.d.ts.map +1 -1
- package/dist/websocket/handler.js +271 -152
- package/dist/websocket/handler.js.map +1 -1
- package/package.json +9 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../src/routes/health.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../src/routes/health.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,aAAa,CAAC;AAElE,wBAAgB,kBAAkB,IAAI,MAAM,CAAC,cAAc,CAAC,CAiB3D"}
|
package/dist/routes/health.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
|
-
const startTime = Date.now();
|
|
3
2
|
export function createHealthRoutes() {
|
|
4
3
|
const router = new Router();
|
|
5
4
|
router.get('/health', (ctx) => {
|
|
6
5
|
const response = {
|
|
7
6
|
status: 'ok',
|
|
8
|
-
uptime:
|
|
7
|
+
uptime: Math.round(process.uptime() * 1000),
|
|
9
8
|
timestamp: Date.now(),
|
|
10
9
|
};
|
|
11
10
|
ctx.body = response;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.js","sourceRoot":"","sources":["../../src/routes/health.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,MAAM,
|
|
1
|
+
{"version":3,"file":"health.js","sourceRoot":"","sources":["../../src/routes/health.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,MAAM,UAAU,kBAAkB;IAChC,MAAM,MAAM,GAAG,IAAI,MAAM,EAAkB,CAAC;IAE5C,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,EAAE;QAC5B,MAAM,QAAQ,GAAmB;YAC/B,MAAM,EAAE,IAAI;YACZ,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC;YAC3C,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC;QACF,GAAG,CAAC,IAAI,GAAG,QAAQ,CAAC;IACtB,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE;QAC3B,GAAG,CAAC,IAAI,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swarms.d.ts","sourceRoot":"","sources":["../../src/routes/swarms.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"swarms.d.ts","sourceRoot":"","sources":["../../src/routes/swarms.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,EAAE,cAAc,EAAyC,MAAM,aAAa,CAAC;AAUzF,wBAAgB,iBAAiB,IAAI,MAAM,CAAC,cAAc,CAAC,CAqL1D"}
|
package/dist/routes/swarms.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
2
|
import { KoaStreamWriter, setupSSEHeaders } from '../streaming/index.js';
|
|
3
3
|
import { generateId } from '@cogitator-ai/server-shared';
|
|
4
|
+
import { getOwn } from '../utils/lookup.js';
|
|
5
|
+
import { isModuleNotFoundError, resolveError } from '../utils/errors.js';
|
|
6
|
+
import { getRequestBody, onClientDisconnect } from '../utils/request.js';
|
|
7
|
+
import { serializeSwarmUsage, toSwarmRunResponse } from '../utils/results.js';
|
|
8
|
+
import { parseSwarmRunRequest } from '../utils/validation.js';
|
|
4
9
|
export function createSwarmRoutes() {
|
|
5
10
|
const router = new Router();
|
|
6
11
|
router.get('/swarms', (ctx) => {
|
|
@@ -27,90 +32,82 @@ export function createSwarmRoutes() {
|
|
|
27
32
|
router.post('/swarms/:name/run', async (ctx) => {
|
|
28
33
|
const { swarms, runtime } = ctx.state.cogitator;
|
|
29
34
|
const { name } = ctx.params;
|
|
30
|
-
const swarmConfig = swarms
|
|
35
|
+
const swarmConfig = getOwn(swarms, name);
|
|
31
36
|
if (!swarmConfig) {
|
|
32
37
|
ctx.status = 404;
|
|
33
38
|
ctx.body = { error: { message: `Swarm '${name}' not found`, code: 'NOT_FOUND' } };
|
|
34
39
|
return;
|
|
35
40
|
}
|
|
36
|
-
const
|
|
37
|
-
if (!
|
|
41
|
+
const parsed = parseSwarmRunRequest(getRequestBody(ctx));
|
|
42
|
+
if (!parsed.ok) {
|
|
38
43
|
ctx.status = 400;
|
|
39
|
-
ctx.body = { error: { message:
|
|
44
|
+
ctx.body = { error: { message: parsed.message, code: 'INVALID_INPUT' } };
|
|
40
45
|
return;
|
|
41
46
|
}
|
|
47
|
+
let aborted = false;
|
|
48
|
+
let abortSwarm;
|
|
49
|
+
onClientDisconnect(ctx, () => {
|
|
50
|
+
aborted = true;
|
|
51
|
+
abortSwarm?.();
|
|
52
|
+
});
|
|
42
53
|
try {
|
|
43
54
|
const { Swarm } = await import('@cogitator-ai/swarms');
|
|
44
55
|
const swarm = new Swarm(runtime, swarmConfig);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
for (const [agentName, agentResult] of result.agentResults.entries()) {
|
|
53
|
-
agentResults[agentName] = {
|
|
54
|
-
output: agentResult.output,
|
|
55
|
-
usage: agentResult.usage,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
const resourceUsage = swarm.getResourceUsage();
|
|
59
|
-
const response = {
|
|
60
|
-
swarmId: swarm.id,
|
|
61
|
-
swarmName: swarm.name,
|
|
62
|
-
strategy: swarm.strategyType,
|
|
63
|
-
output: result.output,
|
|
64
|
-
agentResults,
|
|
65
|
-
usage: {
|
|
66
|
-
totalTokens: resourceUsage.totalTokens,
|
|
67
|
-
totalCost: resourceUsage.totalCost,
|
|
68
|
-
elapsedTime: resourceUsage.elapsedTime,
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
ctx.body = response;
|
|
56
|
+
abortSwarm = () => swarm.abort();
|
|
57
|
+
if (aborted)
|
|
58
|
+
return;
|
|
59
|
+
const result = await swarm.run(parsed.value);
|
|
60
|
+
if (aborted)
|
|
61
|
+
return;
|
|
62
|
+
ctx.body = toSwarmRunResponse(swarm, result);
|
|
72
63
|
}
|
|
73
64
|
catch (error) {
|
|
74
|
-
if (
|
|
65
|
+
if (aborted)
|
|
66
|
+
return;
|
|
67
|
+
if (isModuleNotFoundError(error)) {
|
|
75
68
|
ctx.status = 501;
|
|
76
69
|
ctx.body = { error: { message: 'Swarms package not installed', code: 'UNIMPLEMENTED' } };
|
|
77
70
|
return;
|
|
78
71
|
}
|
|
79
|
-
const
|
|
80
|
-
ctx.status =
|
|
81
|
-
ctx.body =
|
|
72
|
+
const { status, body } = resolveError(error, 'Swarm run error');
|
|
73
|
+
ctx.status = status;
|
|
74
|
+
ctx.body = body;
|
|
82
75
|
}
|
|
83
76
|
});
|
|
84
77
|
router.post('/swarms/:name/stream', async (ctx) => {
|
|
85
78
|
const { swarms, runtime } = ctx.state.cogitator;
|
|
86
79
|
const { name } = ctx.params;
|
|
87
|
-
const swarmConfig = swarms
|
|
80
|
+
const swarmConfig = getOwn(swarms, name);
|
|
88
81
|
if (!swarmConfig) {
|
|
89
82
|
ctx.status = 404;
|
|
90
83
|
ctx.body = { error: { message: `Swarm '${name}' not found`, code: 'NOT_FOUND' } };
|
|
91
84
|
return;
|
|
92
85
|
}
|
|
93
|
-
const
|
|
94
|
-
if (!
|
|
86
|
+
const parsed = parseSwarmRunRequest(getRequestBody(ctx));
|
|
87
|
+
if (!parsed.ok) {
|
|
95
88
|
ctx.status = 400;
|
|
96
|
-
ctx.body = { error: { message:
|
|
89
|
+
ctx.body = { error: { message: parsed.message, code: 'INVALID_INPUT' } };
|
|
97
90
|
return;
|
|
98
91
|
}
|
|
99
92
|
setupSSEHeaders(ctx);
|
|
100
93
|
const writer = new KoaStreamWriter(ctx);
|
|
101
94
|
const messageId = generateId('swarm');
|
|
102
|
-
|
|
95
|
+
let aborted = false;
|
|
96
|
+
let abortSwarm;
|
|
97
|
+
onClientDisconnect(ctx, () => {
|
|
98
|
+
aborted = true;
|
|
103
99
|
writer.close();
|
|
100
|
+
abortSwarm?.();
|
|
104
101
|
});
|
|
105
102
|
try {
|
|
106
103
|
const { Swarm } = await import('@cogitator-ai/swarms');
|
|
107
104
|
const swarm = new Swarm(runtime, swarmConfig);
|
|
105
|
+
abortSwarm = () => swarm.abort();
|
|
106
|
+
if (aborted)
|
|
107
|
+
return;
|
|
108
108
|
writer.start(messageId);
|
|
109
109
|
const result = await swarm.run({
|
|
110
|
-
|
|
111
|
-
context: body.context,
|
|
112
|
-
threadId: body.threadId,
|
|
113
|
-
timeout: body.timeout,
|
|
110
|
+
...parsed.value,
|
|
114
111
|
onAgentStart: (agentName) => {
|
|
115
112
|
writer.swarmEvent('agent_start', { agentName, timestamp: Date.now() });
|
|
116
113
|
},
|
|
@@ -131,21 +128,24 @@ export function createSwarmRoutes() {
|
|
|
131
128
|
writer.swarmEvent(event.type, event.data);
|
|
132
129
|
},
|
|
133
130
|
});
|
|
134
|
-
|
|
131
|
+
if (aborted)
|
|
132
|
+
return;
|
|
135
133
|
writer.swarmEvent('swarm_completed', {
|
|
136
134
|
swarmId: swarm.id,
|
|
137
135
|
output: result.output,
|
|
138
|
-
usage:
|
|
136
|
+
usage: serializeSwarmUsage(swarm.getResourceUsage()),
|
|
139
137
|
});
|
|
140
138
|
writer.finish(messageId);
|
|
141
139
|
}
|
|
142
140
|
catch (error) {
|
|
143
|
-
if (
|
|
141
|
+
if (aborted)
|
|
142
|
+
return;
|
|
143
|
+
if (isModuleNotFoundError(error)) {
|
|
144
144
|
writer.error('Swarms package not installed', 'UNIMPLEMENTED');
|
|
145
145
|
}
|
|
146
146
|
else {
|
|
147
|
-
const
|
|
148
|
-
writer.error(message,
|
|
147
|
+
const { body } = resolveError(error, 'Swarm stream error');
|
|
148
|
+
writer.error(body.error.message, body.error.code);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
finally {
|
|
@@ -155,7 +155,7 @@ export function createSwarmRoutes() {
|
|
|
155
155
|
router.get('/swarms/:name/blackboard', (ctx) => {
|
|
156
156
|
const { swarms } = ctx.state.cogitator;
|
|
157
157
|
const { name } = ctx.params;
|
|
158
|
-
const swarmConfig = swarms
|
|
158
|
+
const swarmConfig = getOwn(swarms, name);
|
|
159
159
|
if (!swarmConfig) {
|
|
160
160
|
ctx.status = 404;
|
|
161
161
|
ctx.body = { error: { message: `Swarm '${name}' not found`, code: 'NOT_FOUND' } };
|
|
@@ -169,7 +169,7 @@ export function createSwarmRoutes() {
|
|
|
169
169
|
return;
|
|
170
170
|
}
|
|
171
171
|
const response = {
|
|
172
|
-
sections: swarmConfig.blackboard.sections
|
|
172
|
+
sections: swarmConfig.blackboard.sections ?? {},
|
|
173
173
|
};
|
|
174
174
|
ctx.body = response;
|
|
175
175
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swarms.js","sourceRoot":"","sources":["../../src/routes/swarms.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"swarms.js","sourceRoot":"","sources":["../../src/routes/swarms.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAEzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D,MAAM,UAAU,iBAAiB;IAC/B,MAAM,MAAM,GAAG,IAAI,MAAM,EAAkB,CAAC;IAE5C,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,EAAE;QAC5B,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC;QACvC,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE;YAC9D,MAAM,MAAM,GAAa,EAAE,CAAC;YAC5B,IAAI,MAAM,CAAC,UAAU;gBAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,U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C;QAC5B,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAEzC,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;YACjB,GAAG,CAAC,IAAI,GAAG,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,UAAU,IAAI,aAAa,EAAE,IAAI,EAAE,WAAW,EAAE,EAAE,CAAC;YAClF,OAAO;QACT,CAAC;QAED,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC;YACrC,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;YACjB,GAAG,CAAC,IAAI,GAAG;gBACT,KAAK,EAAE,EAAE,OAAO,EAAE,uCAAuC,EAAE,IAAI,EAAE,eAAe,EAAE;aACnF,CAAC;YACF,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,GAAuB;YACnC,QAAQ,EAAE,WAAW,CAAC,UAAU,CAAC,QAAQ,IAAI,EAAE;SAChD,CAAC;QAEF,GAAG,CAAC,IAAI,GAAG,QAAQ,CAAC;IACtB,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"threads.d.ts","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"threads.d.ts","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,aAAa,CAAC;AAkBlE,wBAAgB,kBAAkB,IAAI,MAAM,CAAC,cAAc,CAAC,CA4F3D"}
|
package/dist/routes/threads.js
CHANGED
|
@@ -1,99 +1,95 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
|
+
import { countMessageTokens } from '@cogitator-ai/memory';
|
|
3
|
+
import { resolveError } from '../utils/errors.js';
|
|
4
|
+
import { getRequestBody } from '../utils/request.js';
|
|
5
|
+
import { parseAddMessageRequest } from '../utils/validation.js';
|
|
6
|
+
function respondWithError(ctx, error, label) {
|
|
7
|
+
const { status, body } = resolveError(error, label);
|
|
8
|
+
ctx.status = status;
|
|
9
|
+
ctx.body = body;
|
|
10
|
+
}
|
|
11
|
+
function respondMemoryUnavailable(ctx) {
|
|
12
|
+
ctx.status = 503;
|
|
13
|
+
ctx.body = { error: { message: 'Memory not configured', code: 'UNAVAILABLE' } };
|
|
14
|
+
}
|
|
2
15
|
export function createThreadRoutes() {
|
|
3
16
|
const router = new Router();
|
|
4
17
|
router.get('/threads/:id', async (ctx) => {
|
|
5
18
|
const memory = ctx.state.cogitator.runtime.memory;
|
|
6
19
|
if (!memory) {
|
|
7
|
-
ctx
|
|
8
|
-
ctx.body = { error: { message: 'Memory not configured', code: 'UNAVAILABLE' } };
|
|
20
|
+
respondMemoryUnavailable(ctx);
|
|
9
21
|
return;
|
|
10
22
|
}
|
|
11
23
|
const { id } = ctx.params;
|
|
12
24
|
try {
|
|
13
25
|
const result = await memory.getEntries({ threadId: id });
|
|
14
26
|
if (!result.success) {
|
|
15
|
-
ctx.
|
|
16
|
-
ctx.body = { error: { message: result.error, code: 'INTERNAL' } };
|
|
27
|
+
respondWithError(ctx, new Error(result.error), 'Thread get error');
|
|
17
28
|
return;
|
|
18
29
|
}
|
|
19
30
|
const entries = result.data;
|
|
20
|
-
const
|
|
21
|
-
const createdAt = entries.length > 0 ? entries[0].createdAt.getTime() : Date.now();
|
|
22
|
-
const updatedAt = entries.length > 0 ? entries[entries.length - 1].createdAt.getTime() : Date.now();
|
|
31
|
+
const now = Date.now();
|
|
23
32
|
const response = {
|
|
24
33
|
id,
|
|
25
|
-
messages,
|
|
26
|
-
createdAt,
|
|
27
|
-
updatedAt,
|
|
34
|
+
messages: entries.map((entry) => entry.message),
|
|
35
|
+
createdAt: entries.length > 0 ? entries[0].createdAt.getTime() : now,
|
|
36
|
+
updatedAt: entries.length > 0 ? entries[entries.length - 1].createdAt.getTime() : now,
|
|
28
37
|
};
|
|
29
38
|
ctx.body = response;
|
|
30
39
|
}
|
|
31
40
|
catch (error) {
|
|
32
|
-
|
|
33
|
-
ctx.status = 500;
|
|
34
|
-
ctx.body = { error: { message, code: 'INTERNAL' } };
|
|
41
|
+
respondWithError(ctx, error, 'Thread get error');
|
|
35
42
|
}
|
|
36
43
|
});
|
|
37
44
|
router.post('/threads/:id/messages', async (ctx) => {
|
|
38
45
|
const memory = ctx.state.cogitator.runtime.memory;
|
|
39
46
|
if (!memory) {
|
|
40
|
-
ctx
|
|
41
|
-
ctx.body = { error: { message: 'Memory not configured', code: 'UNAVAILABLE' } };
|
|
47
|
+
respondMemoryUnavailable(ctx);
|
|
42
48
|
return;
|
|
43
49
|
}
|
|
44
50
|
const { id } = ctx.params;
|
|
45
|
-
const
|
|
46
|
-
if (!
|
|
51
|
+
const parsed = parseAddMessageRequest(getRequestBody(ctx));
|
|
52
|
+
if (!parsed.ok) {
|
|
47
53
|
ctx.status = 400;
|
|
48
|
-
ctx.body = {
|
|
49
|
-
error: { message: 'Missing required fields: role, content', code: 'INVALID_INPUT' },
|
|
50
|
-
};
|
|
54
|
+
ctx.body = { error: { message: parsed.message, code: 'INVALID_INPUT' } };
|
|
51
55
|
return;
|
|
52
56
|
}
|
|
57
|
+
const message = { role: parsed.value.role, content: parsed.value.content };
|
|
53
58
|
try {
|
|
54
59
|
const result = await memory.addEntry({
|
|
55
60
|
threadId: id,
|
|
56
|
-
message
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
},
|
|
60
|
-
tokenCount: 0,
|
|
61
|
+
message,
|
|
62
|
+
tokenCount: countMessageTokens(message),
|
|
63
|
+
...(parsed.value.metadata && { metadata: parsed.value.metadata }),
|
|
61
64
|
});
|
|
62
65
|
if (!result.success) {
|
|
63
|
-
ctx.
|
|
64
|
-
ctx.body = { error: { message: result.error, code: 'INTERNAL' } };
|
|
66
|
+
respondWithError(ctx, new Error(result.error), 'Thread add message error');
|
|
65
67
|
return;
|
|
66
68
|
}
|
|
67
69
|
ctx.status = 201;
|
|
68
70
|
ctx.body = { success: true };
|
|
69
71
|
}
|
|
70
72
|
catch (error) {
|
|
71
|
-
|
|
72
|
-
ctx.status = 500;
|
|
73
|
-
ctx.body = { error: { message, code: 'INTERNAL' } };
|
|
73
|
+
respondWithError(ctx, error, 'Thread add message error');
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
76
|
router.delete('/threads/:id', async (ctx) => {
|
|
77
77
|
const memory = ctx.state.cogitator.runtime.memory;
|
|
78
78
|
if (!memory) {
|
|
79
|
-
ctx
|
|
80
|
-
ctx.body = { error: { message: 'Memory not configured', code: 'UNAVAILABLE' } };
|
|
79
|
+
respondMemoryUnavailable(ctx);
|
|
81
80
|
return;
|
|
82
81
|
}
|
|
83
82
|
const { id } = ctx.params;
|
|
84
83
|
try {
|
|
85
84
|
const result = await memory.clearThread(id);
|
|
86
85
|
if (!result.success) {
|
|
87
|
-
ctx.
|
|
88
|
-
ctx.body = { error: { message: result.error, code: 'INTERNAL' } };
|
|
86
|
+
respondWithError(ctx, new Error(result.error), 'Thread delete error');
|
|
89
87
|
return;
|
|
90
88
|
}
|
|
91
89
|
ctx.status = 204;
|
|
92
90
|
}
|
|
93
91
|
catch (error) {
|
|
94
|
-
|
|
95
|
-
ctx.status = 500;
|
|
96
|
-
ctx.body = { error: { message, code: 'INTERNAL' } };
|
|
92
|
+
respondWithError(ctx, error, 'Thread delete error');
|
|
97
93
|
}
|
|
98
94
|
});
|
|
99
95
|
return router;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"threads.js","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,
|
|
1
|
+
{"version":3,"file":"threads.js","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[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MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC;QAClD,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,wBAAwB,CAAC,GAAG,CAAC,CAAC;YAC9B,OAAO;QACT,CAAC;QAED,MAAM,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;QAE1B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;YAC5C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACpB,gBAAgB,CAAC,GAAG,EAAE,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,qBAAqB,CAAC,CAAC;gBACtE,OAAO;YACT,CAAC;YACD,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;QACnB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,gBAAgB,CAAC,GAAG,EAAE,KAAK,EAAE,qBAAqB,CAAC,CAAC;QACtD,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/routes/tools.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,EAAE,cAAc,EAAoB,MAAM,aAAa,CAAC;AAEpE,wBAAgB,gBAAgB,IAAI,MAAM,CAAC,cAAc,CAAC,
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/routes/tools.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,EAAE,cAAc,EAAoB,MAAM,aAAa,CAAC;AAEpE,wBAAgB,gBAAgB,IAAI,MAAM,CAAC,cAAc,CAAC,CAwBzD"}
|
package/dist/routes/tools.js
CHANGED
|
@@ -3,26 +3,20 @@ export function createToolRoutes() {
|
|
|
3
3
|
const router = new Router();
|
|
4
4
|
router.get('/tools', (ctx) => {
|
|
5
5
|
const { agents } = ctx.state.cogitator;
|
|
6
|
-
const
|
|
6
|
+
const tools = new Map();
|
|
7
7
|
for (const agent of Object.values(agents)) {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
8
|
+
for (const tool of agent.config.tools ?? []) {
|
|
9
|
+
if (tools.has(tool.name))
|
|
10
|
+
continue;
|
|
11
|
+
const schema = tool.toJSON();
|
|
12
|
+
tools.set(tool.name, {
|
|
13
|
+
name: schema.name,
|
|
14
|
+
description: schema.description,
|
|
15
|
+
parameters: schema.parameters,
|
|
16
|
+
});
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
const response = {
|
|
20
|
-
tools: Array.from(toolsSet.values()).map((t) => ({
|
|
21
|
-
name: t.name,
|
|
22
|
-
description: t.description,
|
|
23
|
-
parameters: t.parameters,
|
|
24
|
-
})),
|
|
25
|
-
};
|
|
19
|
+
const response = { tools: Array.from(tools.values()) };
|
|
26
20
|
ctx.body = response;
|
|
27
21
|
});
|
|
28
22
|
return router;
|
package/dist/routes/tools.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/routes/tools.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,MAAM,UAAU,gBAAgB;IAC9B,MAAM,MAAM,GAAG,IAAI,MAAM,EAAkB,CAAC;IAE5C,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE;QAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC;QACvC,MAAM,
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/routes/tools.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,MAAM,UAAU,gBAAgB;IAC9B,MAAM,MAAM,GAAG,IAAI,MAAM,EAAkB,CAAC;IAE5C,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE;QAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC;QACvC,MAAM,KAAK,GAAG,IAAI,GAAG,EAA6C,CAAC;QAEnE,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;YAC1C,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE,EAAE,CAAC;gBAC5C,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;oBAAE,SAAS;gBACnC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;gBAC7B,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE;oBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,WAAW,EAAE,MAAM,CAAC,WAAW;oBAC/B,UAAU,EAAE,MAAM,CAAC,UAAU;iBAC9B,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,MAAM,QAAQ,GAAqB,EAAE,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;QACzE,GAAG,CAAC,IAAI,GAAG,QAAQ,CAAC;IACtB,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workflows.d.ts","sourceRoot":"","sources":["../../src/routes/workflows.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"workflows.d.ts","sourceRoot":"","sources":["../../src/routes/workflows.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,EAAE,cAAc,EAAwB,MAAM,aAAa,CAAC;AASxE,wBAAgB,oBAAoB,IAAI,MAAM,CAAC,cAAc,CAAC,CAmJ7D"}
|
package/dist/routes/workflows.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
2
|
import { KoaStreamWriter, setupSSEHeaders } from '../streaming/index.js';
|
|
3
3
|
import { generateId } from '@cogitator-ai/server-shared';
|
|
4
|
+
import { getOwn } from '../utils/lookup.js';
|
|
5
|
+
import { isModuleNotFoundError, resolveError } from '../utils/errors.js';
|
|
6
|
+
import { getRequestBody, onClientDisconnect } from '../utils/request.js';
|
|
7
|
+
import { toWorkflowRunResponse } from '../utils/results.js';
|
|
8
|
+
import { parseWorkflowRunRequest } from '../utils/validation.js';
|
|
4
9
|
export function createWorkflowRoutes() {
|
|
5
10
|
const router = new Router();
|
|
6
11
|
router.get('/workflows', (ctx) => {
|
|
@@ -16,63 +21,80 @@ export function createWorkflowRoutes() {
|
|
|
16
21
|
router.post('/workflows/:name/run', async (ctx) => {
|
|
17
22
|
const { workflows, runtime } = ctx.state.cogitator;
|
|
18
23
|
const { name } = ctx.params;
|
|
19
|
-
const workflow = workflows
|
|
24
|
+
const workflow = getOwn(workflows, name);
|
|
20
25
|
if (!workflow) {
|
|
21
26
|
ctx.status = 404;
|
|
22
27
|
ctx.body = { error: { message: `Workflow '${name}' not found`, code: 'NOT_FOUND' } };
|
|
23
28
|
return;
|
|
24
29
|
}
|
|
25
|
-
const
|
|
30
|
+
const parsed = parseWorkflowRunRequest(getRequestBody(ctx));
|
|
31
|
+
if (!parsed.ok) {
|
|
32
|
+
ctx.status = 400;
|
|
33
|
+
ctx.body = { error: { message: parsed.message, code: 'INVALID_INPUT' } };
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const abortController = new AbortController();
|
|
37
|
+
onClientDisconnect(ctx, () => abortController.abort());
|
|
26
38
|
try {
|
|
27
39
|
const { WorkflowExecutor } = await import('@cogitator-ai/workflows');
|
|
28
40
|
const executor = new WorkflowExecutor(runtime);
|
|
29
|
-
const result = await executor.execute(workflow,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
41
|
+
const result = await executor.execute(workflow, parsed.value.input, {
|
|
42
|
+
...parsed.value.options,
|
|
43
|
+
signal: abortController.signal,
|
|
44
|
+
});
|
|
45
|
+
if (abortController.signal.aborted)
|
|
46
|
+
return;
|
|
47
|
+
if (result.error) {
|
|
48
|
+
const { status, body } = resolveError(result.error, 'Workflow run error');
|
|
49
|
+
ctx.status = status;
|
|
50
|
+
ctx.body = body;
|
|
51
|
+
return;
|
|
33
52
|
}
|
|
34
|
-
|
|
35
|
-
workflowId: result.workflowId,
|
|
36
|
-
workflowName: result.workflowName,
|
|
37
|
-
state: result.state,
|
|
38
|
-
duration: result.duration,
|
|
39
|
-
nodeResults,
|
|
40
|
-
};
|
|
41
|
-
ctx.body = response;
|
|
53
|
+
ctx.body = toWorkflowRunResponse(result);
|
|
42
54
|
}
|
|
43
55
|
catch (error) {
|
|
44
|
-
if (
|
|
56
|
+
if (abortController.signal.aborted)
|
|
57
|
+
return;
|
|
58
|
+
if (isModuleNotFoundError(error)) {
|
|
45
59
|
ctx.status = 501;
|
|
46
60
|
ctx.body = { error: { message: 'Workflows package not installed', code: 'UNIMPLEMENTED' } };
|
|
47
61
|
return;
|
|
48
62
|
}
|
|
49
|
-
const
|
|
50
|
-
ctx.status =
|
|
51
|
-
ctx.body =
|
|
63
|
+
const { status, body } = resolveError(error, 'Workflow run error');
|
|
64
|
+
ctx.status = status;
|
|
65
|
+
ctx.body = body;
|
|
52
66
|
}
|
|
53
67
|
});
|
|
54
68
|
router.post('/workflows/:name/stream', async (ctx) => {
|
|
55
69
|
const { workflows, runtime } = ctx.state.cogitator;
|
|
56
70
|
const { name } = ctx.params;
|
|
57
|
-
const workflow = workflows
|
|
71
|
+
const workflow = getOwn(workflows, name);
|
|
58
72
|
if (!workflow) {
|
|
59
73
|
ctx.status = 404;
|
|
60
74
|
ctx.body = { error: { message: `Workflow '${name}' not found`, code: 'NOT_FOUND' } };
|
|
61
75
|
return;
|
|
62
76
|
}
|
|
63
|
-
const
|
|
77
|
+
const parsed = parseWorkflowRunRequest(getRequestBody(ctx));
|
|
78
|
+
if (!parsed.ok) {
|
|
79
|
+
ctx.status = 400;
|
|
80
|
+
ctx.body = { error: { message: parsed.message, code: 'INVALID_INPUT' } };
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
64
83
|
setupSSEHeaders(ctx);
|
|
65
84
|
const writer = new KoaStreamWriter(ctx);
|
|
66
85
|
const messageId = generateId('wf');
|
|
67
|
-
|
|
86
|
+
const abortController = new AbortController();
|
|
87
|
+
onClientDisconnect(ctx, () => {
|
|
68
88
|
writer.close();
|
|
89
|
+
abortController.abort();
|
|
69
90
|
});
|
|
70
91
|
try {
|
|
71
92
|
const { WorkflowExecutor } = await import('@cogitator-ai/workflows');
|
|
72
93
|
const executor = new WorkflowExecutor(runtime);
|
|
73
94
|
writer.start(messageId);
|
|
74
|
-
const result = await executor.execute(workflow,
|
|
75
|
-
...
|
|
95
|
+
const result = await executor.execute(workflow, parsed.value.input, {
|
|
96
|
+
...parsed.value.options,
|
|
97
|
+
signal: abortController.signal,
|
|
76
98
|
onNodeStart: (node) => {
|
|
77
99
|
writer.workflowEvent('node_started', { nodeName: node, timestamp: Date.now() });
|
|
78
100
|
},
|
|
@@ -86,6 +108,13 @@ export function createWorkflowRoutes() {
|
|
|
86
108
|
writer.workflowEvent('node_progress', { nodeName: node, progress });
|
|
87
109
|
},
|
|
88
110
|
});
|
|
111
|
+
if (abortController.signal.aborted)
|
|
112
|
+
return;
|
|
113
|
+
if (result.error) {
|
|
114
|
+
const { body } = resolveError(result.error, 'Workflow stream error');
|
|
115
|
+
writer.error(body.error.message, body.error.code);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
89
118
|
writer.workflowEvent('workflow_completed', {
|
|
90
119
|
workflowId: result.workflowId,
|
|
91
120
|
duration: result.duration,
|
|
@@ -93,12 +122,14 @@ export function createWorkflowRoutes() {
|
|
|
93
122
|
writer.finish(messageId);
|
|
94
123
|
}
|
|
95
124
|
catch (error) {
|
|
96
|
-
if (
|
|
125
|
+
if (abortController.signal.aborted)
|
|
126
|
+
return;
|
|
127
|
+
if (isModuleNotFoundError(error)) {
|
|
97
128
|
writer.error('Workflows package not installed', 'UNIMPLEMENTED');
|
|
98
129
|
}
|
|
99
130
|
else {
|
|
100
|
-
const
|
|
101
|
-
writer.error(message,
|
|
131
|
+
const { body } = resolveError(error, 'Workflow stream error');
|
|
132
|
+
writer.error(body.error.message, body.error.code);
|
|
102
133
|
}
|
|
103
134
|
}
|
|
104
135
|
finally {
|