openbot 0.3.3 → 0.3.5
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 +1 -1
- package/dist/app/cli.js +1 -1
- package/dist/app/server.js +39 -21
- package/dist/assets/icon.svg +4 -0
- package/dist/bus/services.js +6 -5
- package/dist/harness/dispatcher.js +267 -0
- package/dist/harness/orchestrator.js +76 -0
- package/dist/harness/queue-processor.js +26 -0
- package/dist/services/storage.js +39 -5
- package/docs/architecture.md +5 -8
- package/package.json +7 -7
- package/src/app/cli.ts +1 -1
- package/src/app/server.ts +49 -27
- package/src/app/types.ts +37 -0
- package/src/bus/services.ts +6 -5
- package/src/bus/types.ts +4 -0
- package/src/harness/dispatcher.ts +379 -0
- package/src/services/storage.ts +41 -4
- package/src/harness/agent-harness.ts +0 -58
- package/src/harness/event-normalizer.ts +0 -82
- package/src/harness/orchestrator.ts +0 -104
- package/src/harness/queue-processor.ts +0 -220
- package/src/harness/types.ts +0 -34
package/README.md
CHANGED
|
@@ -120,7 +120,7 @@ Shared plugins can be placed in `~/.openbot/plugins` and referenced by agents.
|
|
|
120
120
|
## Project Layout
|
|
121
121
|
|
|
122
122
|
- `src/app`: CLI, server, event types, and app config.
|
|
123
|
-
- `src/harness`:
|
|
123
|
+
- `src/harness`: orchestration, process, and MCP runtime helpers.
|
|
124
124
|
- `src/plugins`: built-in plugin implementations.
|
|
125
125
|
- `src/services`: local storage service.
|
|
126
126
|
- `src/registry`: plugin registry.
|
package/dist/app/cli.js
CHANGED
|
@@ -16,7 +16,7 @@ function checkNodeVersion() {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
checkNodeVersion();
|
|
19
|
-
program.name('openbot').description('OpenBot CLI').version('0.3.
|
|
19
|
+
program.name('openbot').description('OpenBot CLI').version('0.3.4');
|
|
20
20
|
program
|
|
21
21
|
.command('start')
|
|
22
22
|
.description('Start the OpenBot harness')
|
package/dist/app/server.js
CHANGED
|
@@ -11,7 +11,7 @@ import { generateId } from 'melony';
|
|
|
11
11
|
import { DEFAULT_BASE_DIR, loadConfig, resolvePath } from '../app/config.js';
|
|
12
12
|
import { processService } from '../harness/process.js';
|
|
13
13
|
import { storageService } from '../services/storage.js';
|
|
14
|
-
import {
|
|
14
|
+
import { dispatch } from '../harness/dispatcher.js';
|
|
15
15
|
import { initPlugins } from '../registry/plugins.js';
|
|
16
16
|
import { ensureEventId, openBotEventFromQuery } from './utils.js';
|
|
17
17
|
export async function startServer(options = {}) {
|
|
@@ -69,25 +69,39 @@ export async function startServer(options = {}) {
|
|
|
69
69
|
});
|
|
70
70
|
};
|
|
71
71
|
const buildActiveRunsSnapshot = () => {
|
|
72
|
-
const
|
|
72
|
+
const byBucket = new Map();
|
|
73
73
|
for (const run of activeRuns.values()) {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
const threadId = run.threadId || undefined;
|
|
75
|
+
const key = JSON.stringify([run.channelId, threadId ?? null]);
|
|
76
|
+
let bucket = byBucket.get(key);
|
|
77
|
+
if (!bucket) {
|
|
78
|
+
bucket = { channelId: run.channelId, threadId, activeCount: 0, agentIds: new Set() };
|
|
79
|
+
byBucket.set(key, bucket);
|
|
80
|
+
}
|
|
81
|
+
bucket.activeCount += 1;
|
|
82
|
+
bucket.agentIds.add(run.agentId);
|
|
81
83
|
}
|
|
84
|
+
const channels = Array.from(byBucket.values())
|
|
85
|
+
.sort((a, b) => {
|
|
86
|
+
const c = a.channelId.localeCompare(b.channelId);
|
|
87
|
+
if (c !== 0)
|
|
88
|
+
return c;
|
|
89
|
+
return (a.threadId ?? '').localeCompare(b.threadId ?? '');
|
|
90
|
+
})
|
|
91
|
+
.map(({ channelId, threadId, activeCount, agentIds }) => {
|
|
92
|
+
const row = {
|
|
93
|
+
channelId,
|
|
94
|
+
activeCount,
|
|
95
|
+
agentIds: Array.from(agentIds),
|
|
96
|
+
};
|
|
97
|
+
if (threadId !== undefined) {
|
|
98
|
+
row.threadId = threadId;
|
|
99
|
+
}
|
|
100
|
+
return row;
|
|
101
|
+
});
|
|
82
102
|
return {
|
|
83
103
|
type: 'agent:active-runs:snapshot',
|
|
84
|
-
data: {
|
|
85
|
-
channels: Array.from(byChannel.entries()).map(([channelId, value]) => ({
|
|
86
|
-
channelId,
|
|
87
|
-
activeCount: value.activeCount,
|
|
88
|
-
agentIds: Array.from(value.agentIds),
|
|
89
|
-
})),
|
|
90
|
-
},
|
|
104
|
+
data: { channels },
|
|
91
105
|
};
|
|
92
106
|
};
|
|
93
107
|
app.use(cors());
|
|
@@ -178,19 +192,22 @@ export async function startServer(options = {}) {
|
|
|
178
192
|
event: chunk,
|
|
179
193
|
});
|
|
180
194
|
sendToClientKey(targetClientKey, chunk);
|
|
181
|
-
if (chunk.type === 'agent:run:start' ||
|
|
195
|
+
if (chunk.type === 'agent:run:start' ||
|
|
196
|
+
chunk.type === 'agent:run:end' ||
|
|
197
|
+
chunk.type === 'agent:run:stopped') {
|
|
182
198
|
sendToClientKey(GLOBAL_CHANNEL_ID, chunk);
|
|
183
199
|
}
|
|
184
200
|
};
|
|
185
201
|
try {
|
|
186
|
-
|
|
202
|
+
ensureEventId(event);
|
|
203
|
+
await dispatch({
|
|
187
204
|
runId,
|
|
188
205
|
agentId: agentId || 'system',
|
|
206
|
+
event,
|
|
189
207
|
channelId,
|
|
190
208
|
threadId,
|
|
191
209
|
onEvent,
|
|
192
210
|
});
|
|
193
|
-
await harness.dispatch(event);
|
|
194
211
|
res.sendStatus(200);
|
|
195
212
|
}
|
|
196
213
|
catch (error) {
|
|
@@ -220,14 +237,15 @@ export async function startServer(options = {}) {
|
|
|
220
237
|
events.push(chunk);
|
|
221
238
|
};
|
|
222
239
|
try {
|
|
223
|
-
|
|
240
|
+
ensureEventId(event);
|
|
241
|
+
await dispatch({
|
|
224
242
|
runId,
|
|
225
243
|
agentId: agentId || 'system',
|
|
244
|
+
event,
|
|
226
245
|
channelId,
|
|
227
246
|
threadId,
|
|
228
247
|
onEvent,
|
|
229
248
|
});
|
|
230
|
-
await harness.dispatch(event);
|
|
231
249
|
res.json({ events });
|
|
232
250
|
}
|
|
233
251
|
catch (error) {
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<svg width="60.2" height="58.1" viewBox="262.8 360.5 84.2 82.1" fill="#9333EA" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<path d="M301.841 380.896C302.069 380.893 302.297 380.889 302.531 380.885C304.636 380.853 306.741 380.836 308.845 380.822C310.26 380.813 311.675 380.8 313.09 380.775C314.456 380.752 315.822 380.739 317.189 380.735C317.71 380.731 318.231 380.724 318.752 380.712C322.824 380.626 322.824 380.626 323.909 381.43C324.315 382.025 324.561 382.569 324.8 383.246C324.965 383.518 325.13 383.789 325.301 384.068C325.519 384.445 325.519 384.445 325.741 384.83C325.909 385.118 326.077 385.407 326.249 385.704C326.336 385.854 326.423 386.004 326.513 386.159C328.304 389.25 330.137 392.314 332.004 395.361C332.482 396.144 332.957 396.929 333.431 397.715C333.631 398.035 333.631 398.035 333.835 398.361C334.546 399.544 334.969 400.367 334.7 401.777C334.24 402.838 333.683 403.834 333.113 404.839C332.867 405.285 332.621 405.73 332.375 406.176C332.125 406.626 331.874 407.075 331.622 407.525C330.952 408.724 330.296 409.931 329.639 411.137C329.518 411.359 329.397 411.58 329.272 411.807C328.303 413.581 327.349 415.362 326.398 417.144C324.53 420.642 322.621 424.107 320.611 427.525C320.282 428.088 319.962 428.655 319.644 429.224C319.215 429.954 319.215 429.954 318.708 430.462C317.978 430.557 317.978 430.557 317.185 430.462C316.049 429.578 315.42 428.258 314.71 427.035C313.685 425.291 312.637 423.6 311.454 421.96C311.024 421.203 310.906 420.668 310.838 419.8C311.092 419.081 311.092 419.081 311.491 418.343C311.707 417.937 311.707 417.937 311.927 417.522C312.081 417.242 312.235 416.963 312.393 416.675C312.615 416.26 312.615 416.26 312.841 415.836C314.003 413.671 314.003 413.671 314.549 412.797C315.269 411.676 315.269 411.676 315.308 410.387C314.801 409.466 314.268 408.575 313.694 407.695C313.466 407.335 313.238 406.976 313.011 406.616C312.845 406.353 312.845 406.353 312.676 406.085C312.247 405.402 311.829 404.713 311.425 404.014C311.304 403.806 311.183 403.598 311.058 403.384C310.786 402.65 310.848 402.26 311.092 401.523C311.6 401.015 311.6 401.015 312.57 400.967C312.988 400.97 313.407 400.975 313.826 400.982C314.281 400.981 314.737 400.981 315.192 400.98C315.911 400.98 316.629 400.984 317.348 400.995C318.041 401.005 318.733 401.002 319.427 400.998C319.641 401.004 319.854 401.011 320.075 401.017C320.695 401.007 321.171 400.974 321.754 400.762C322.329 400.154 322.665 399.481 323.023 398.731C323.171 398.49 323.318 398.248 323.47 398C323.899 397.287 324.326 396.573 324.752 395.859C324.904 395.608 325.056 395.357 325.212 395.099C325.355 394.859 325.498 394.618 325.646 394.371C325.841 394.045 325.841 394.045 326.04 393.713C326.38 393.138 326.38 393.138 326.323 392.385C326.167 392.384 326.011 392.383 325.85 392.383C322.054 392.368 318.257 392.349 314.461 392.325C312.625 392.313 310.789 392.303 308.953 392.297C307.352 392.291 305.752 392.282 304.152 392.271C303.305 392.265 302.458 392.26 301.61 392.258C300.664 392.256 299.719 392.249 298.773 392.241C298.351 392.241 298.351 392.241 297.921 392.242C297.663 392.238 297.406 392.235 297.14 392.232C296.916 392.231 296.693 392.23 296.462 392.228C295.73 392.109 295.379 391.879 294.846 391.369C294.591 389.978 295.187 388.999 295.837 387.818C295.938 387.63 296.038 387.441 296.142 387.246C296.464 386.646 296.789 386.049 297.115 385.451C297.333 385.045 297.551 384.638 297.769 384.231C299.545 380.925 299.545 380.925 301.841 380.896Z" fill="#9333EA"/>
|
|
3
|
+
<path d="M291.476 372.547C292.795 373.009 293.243 374.055 293.942 375.218C294.23 375.684 294.519 376.15 294.807 376.615C294.945 376.84 295.082 377.065 295.223 377.297C295.544 377.813 295.875 378.314 296.225 378.811C296.388 379.044 296.551 379.277 296.718 379.518C296.856 379.711 296.994 379.903 297.137 380.102C297.492 380.97 297.407 381.615 297.131 382.485C296.704 383.459 296.209 384.39 295.703 385.325C295.497 385.714 295.497 385.714 295.287 386.111C294.44 387.702 293.559 389.255 292.565 390.759C292.217 391.585 292.351 392.026 292.562 392.892C292.955 393.665 292.955 393.665 293.466 394.431C293.555 394.572 293.644 394.713 293.736 394.858C294.019 395.304 294.305 395.748 294.592 396.192C294.971 396.778 295.346 397.365 295.719 397.953C295.886 398.212 296.053 398.471 296.225 398.738C296.646 399.535 296.848 400.115 296.877 401.015C296.559 401.435 296.559 401.435 296.115 401.777C295.364 401.849 294.663 401.877 293.911 401.871C293.467 401.876 293.023 401.88 292.579 401.885C291.879 401.887 291.18 401.888 290.48 401.888C289.804 401.888 289.129 401.895 288.453 401.903C288.244 401.901 288.036 401.899 287.82 401.897C286.637 401.91 286.637 401.91 285.616 402.466C284.844 403.543 284.229 404.69 283.598 405.854C283.455 406.11 283.312 406.365 283.165 406.628C283.031 406.873 282.897 407.118 282.76 407.371C282.637 407.594 282.515 407.818 282.389 408.048C282.071 408.656 282.071 408.656 282.408 409.392C282.559 409.393 282.71 409.394 282.865 409.395C286.539 409.416 290.213 409.439 293.886 409.465C295.663 409.478 297.439 409.49 299.216 409.499C300.929 409.509 302.642 409.521 304.356 409.534C305.011 409.539 305.665 409.543 306.32 409.546C307.235 409.55 308.15 409.557 309.064 409.565C309.473 409.566 309.473 409.566 309.89 409.567C310.139 409.57 310.388 409.573 310.644 409.576C310.861 409.577 311.077 409.578 311.3 409.58C311.854 409.646 311.854 409.646 312.615 410.154C312.797 411.581 312.41 412.577 311.745 413.808C311.656 413.979 311.567 414.15 311.475 414.326C311.288 414.685 311.099 415.042 310.908 415.399C310.616 415.946 310.329 416.495 310.042 417.045C309.859 417.394 309.676 417.743 309.492 418.092C309.363 418.337 309.363 418.337 309.231 418.588C308.881 419.242 308.574 419.78 308.046 420.308C307.364 420.361 306.707 420.382 306.024 420.382C305.815 420.383 305.606 420.385 305.391 420.387C304.698 420.392 304.005 420.392 303.312 420.393C302.832 420.395 302.351 420.396 301.871 420.398C300.862 420.401 299.854 420.402 298.845 420.402C297.552 420.402 296.259 420.409 294.966 420.417C293.973 420.423 292.98 420.424 291.987 420.424C291.51 420.424 291.034 420.427 290.557 420.431C289.89 420.436 289.224 420.434 288.557 420.431C288.36 420.434 288.163 420.437 287.959 420.44C287.061 420.429 286.569 420.386 285.852 419.824C285.47 419.314 285.137 418.817 284.831 418.259C284.718 418.056 284.605 417.852 284.488 417.643C284.367 417.423 284.246 417.203 284.121 416.976C282.907 414.808 281.638 412.679 280.329 410.566C280.156 410.286 279.982 410.006 279.803 409.717C279.469 409.176 279.134 408.636 278.799 408.096C277.871 406.592 276.959 405.077 276.062 403.554C275.968 403.409 275.874 403.264 275.777 403.114C275.143 402.097 274.834 401.442 275.046 400.254C275.425 399.337 275.889 398.475 276.363 397.604C276.568 397.22 276.568 397.22 276.777 396.828C278.005 394.541 279.284 392.283 280.569 390.029C282.224 387.119 283.868 384.197 285.327 381.184C289.509 372.654 289.509 372.654 291.476 372.547Z" fill="#9333EA"/>
|
|
4
|
+
</svg>
|
package/dist/bus/services.js
CHANGED
|
@@ -455,8 +455,8 @@ export const busServicesPlugin = (options) => (builder) => {
|
|
|
455
455
|
});
|
|
456
456
|
builder.on('action:storage:create-agent', async function* (event) {
|
|
457
457
|
try {
|
|
458
|
-
const { agentId, name, description, instructions, plugins } = event.data;
|
|
459
|
-
await storage.createAgent({ agentId, name, description, instructions, plugins });
|
|
458
|
+
const { agentId, name, description, image, instructions, plugins } = event.data;
|
|
459
|
+
await storage.createAgent({ agentId, name, description, image, instructions, plugins });
|
|
460
460
|
yield { type: 'action:storage:create-agent-result', data: { success: true } };
|
|
461
461
|
}
|
|
462
462
|
catch (error) {
|
|
@@ -471,8 +471,8 @@ export const busServicesPlugin = (options) => (builder) => {
|
|
|
471
471
|
});
|
|
472
472
|
builder.on('action:storage:update-agent', async function* (event) {
|
|
473
473
|
try {
|
|
474
|
-
const { agentId, name, description, instructions, plugins } = event.data;
|
|
475
|
-
await storage.updateAgent({ agentId, name, description, instructions, plugins });
|
|
474
|
+
const { agentId, name, description, image, instructions, plugins } = event.data;
|
|
475
|
+
await storage.updateAgent({ agentId, name, description, image, instructions, plugins });
|
|
476
476
|
yield { type: 'action:storage:update-agent-result', data: { success: true } };
|
|
477
477
|
}
|
|
478
478
|
catch (error) {
|
|
@@ -758,7 +758,7 @@ export const busServicesPlugin = (options) => (builder) => {
|
|
|
758
758
|
});
|
|
759
759
|
builder.on('action:agent:install', async function* (event) {
|
|
760
760
|
try {
|
|
761
|
-
const { agentId, name, description, instructions, plugins } = event.data;
|
|
761
|
+
const { agentId, name, description, image, instructions, plugins } = event.data;
|
|
762
762
|
// Ensure each plugin is available locally. Built-in ids resolve
|
|
763
763
|
// immediately; npm-name ids are fetched on demand.
|
|
764
764
|
for (const ref of plugins) {
|
|
@@ -780,6 +780,7 @@ export const busServicesPlugin = (options) => (builder) => {
|
|
|
780
780
|
agentId,
|
|
781
781
|
name,
|
|
782
782
|
description,
|
|
783
|
+
image,
|
|
783
784
|
instructions,
|
|
784
785
|
plugins,
|
|
785
786
|
});
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { ensureEventId } from '../app/utils.js';
|
|
2
|
+
import { storageService } from '../services/storage.js';
|
|
3
|
+
import { createAgentRuntime } from './runtime-factory.js';
|
|
4
|
+
import { advanceAfterRun } from './todo-advance.js';
|
|
5
|
+
const MAX_CHAIN_DEPTH = 20;
|
|
6
|
+
const stopRequests = [];
|
|
7
|
+
const STOP_REQUEST_TTL_MS = 30 * 60 * 1000;
|
|
8
|
+
const pruneStopRequests = () => {
|
|
9
|
+
const now = Date.now();
|
|
10
|
+
for (let i = stopRequests.length - 1; i >= 0; i -= 1) {
|
|
11
|
+
if (now - stopRequests[i].requestedAt > STOP_REQUEST_TTL_MS) {
|
|
12
|
+
stopRequests.splice(i, 1);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const findStopRequest = (target) => {
|
|
17
|
+
pruneStopRequests();
|
|
18
|
+
return stopRequests.find((r) => {
|
|
19
|
+
if (r.runId !== target.runId)
|
|
20
|
+
return false;
|
|
21
|
+
if (r.agentId && r.agentId !== target.agentId)
|
|
22
|
+
return false;
|
|
23
|
+
if (r.channelId && r.channelId !== target.channelId)
|
|
24
|
+
return false;
|
|
25
|
+
if (r.threadId && r.threadId !== target.threadId)
|
|
26
|
+
return false;
|
|
27
|
+
return true;
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
// Public API
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
export async function dispatch(options) {
|
|
34
|
+
const { event } = options;
|
|
35
|
+
ensureEventId(event);
|
|
36
|
+
if (event.type === 'action:agent_run_stop') {
|
|
37
|
+
await handleStop(event, options);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const ctx = {
|
|
41
|
+
runId: options.runId,
|
|
42
|
+
channelId: options.channelId,
|
|
43
|
+
threadId: options.threadId,
|
|
44
|
+
onEvent: options.onEvent,
|
|
45
|
+
};
|
|
46
|
+
if (event.type === 'user:input' || event.type === 'agent:invoke') {
|
|
47
|
+
const invoke = await normalizeUserInput(event, ctx);
|
|
48
|
+
await runStep({ agentId: options.agentId || 'system', event: invoke }, ctx, 0);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// Bus pass-through: route to the targeted agent's runtime once. No agent step,
|
|
52
|
+
// no advance, no follow-ups. Keeps queries (`/api/state`) cheap and idempotent.
|
|
53
|
+
await runBusEvent(event, options.agentId || 'system', ctx);
|
|
54
|
+
}
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// Agent step: run:start -> runtime -> run:end -> advance -> chain
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
async function runStep(step, ctx, depth) {
|
|
59
|
+
if (depth >= MAX_CHAIN_DEPTH) {
|
|
60
|
+
console.warn(`[dispatcher] Reached MAX_CHAIN_DEPTH (${MAX_CHAIN_DEPTH}); stopping chain.`);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const target = {
|
|
64
|
+
runId: ctx.runId,
|
|
65
|
+
agentId: step.agentId,
|
|
66
|
+
channelId: ctx.channelId,
|
|
67
|
+
threadId: ctx.threadId,
|
|
68
|
+
};
|
|
69
|
+
const preStop = findStopRequest(target);
|
|
70
|
+
if (preStop) {
|
|
71
|
+
const state = await storageService.getOpenBotState({ ...target, event: step.event });
|
|
72
|
+
await ctx.onEvent({ type: 'agent:run:stopped', data: { ...target, reason: preStop.reason } }, state);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
let state;
|
|
76
|
+
try {
|
|
77
|
+
state = await storageService.getOpenBotState({ ...target, event: step.event });
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (error.code === 'AGENT_NOT_FOUND') {
|
|
81
|
+
const fallback = await storageService.getOpenBotState({
|
|
82
|
+
...target,
|
|
83
|
+
agentId: 'system',
|
|
84
|
+
event: step.event,
|
|
85
|
+
});
|
|
86
|
+
await ctx.onEvent({
|
|
87
|
+
type: 'agent:output',
|
|
88
|
+
data: { content: `⚠️ Agent **${step.agentId}** does not exist. Please check the agent ID and try again.` },
|
|
89
|
+
meta: { agentId: 'system', threadId: ctx.threadId },
|
|
90
|
+
}, fallback);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
await ctx.onEvent({ type: 'agent:run:start', data: { ...target } }, state);
|
|
96
|
+
const followUps = [];
|
|
97
|
+
const queuedAgentIds = new Set();
|
|
98
|
+
let lastAgentOutput;
|
|
99
|
+
try {
|
|
100
|
+
const runtime = await createAgentRuntime(state);
|
|
101
|
+
for await (const chunk of runtime.run(step.event, { state, runId: ctx.runId })) {
|
|
102
|
+
const stop = findStopRequest(target);
|
|
103
|
+
if (stop) {
|
|
104
|
+
await ctx.onEvent({ type: 'agent:run:stopped', data: { ...target, reason: stop.reason } }, state);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
if (chunk.id === step.event.id && chunk.type === step.event.type)
|
|
108
|
+
continue;
|
|
109
|
+
if (chunk.type === 'action:create_thread:result' && chunk.data.success) {
|
|
110
|
+
ctx.threadId = chunk.data.threadId || ctx.threadId;
|
|
111
|
+
}
|
|
112
|
+
if (chunk.type === 'agent:output' &&
|
|
113
|
+
chunk.meta?.agentId === step.agentId) {
|
|
114
|
+
const content = chunk.data?.content;
|
|
115
|
+
if (typeof content === 'string' && content.trim())
|
|
116
|
+
lastAgentOutput = content.trim();
|
|
117
|
+
}
|
|
118
|
+
// Handoff requests are internal: queue a follow-up step instead of forwarding.
|
|
119
|
+
if (chunk.type === 'handoff:request') {
|
|
120
|
+
const req = chunk;
|
|
121
|
+
const targetAgent = req.data?.agentId;
|
|
122
|
+
if (targetAgent && targetAgent !== step.agentId && !queuedAgentIds.has(targetAgent)) {
|
|
123
|
+
queuedAgentIds.add(targetAgent);
|
|
124
|
+
followUps.push({
|
|
125
|
+
agentId: targetAgent,
|
|
126
|
+
event: makeInvoke(req.data.content, ctx.threadId, req.meta),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
chunk.meta = { ...chunk.meta, agentId: step.agentId };
|
|
132
|
+
await ctx.onEvent(chunk, state);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
console.error(`[dispatcher] Agent run failed: ${step.agentId}`, error);
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
const endState = await storageService.getOpenBotState({ ...target, event: step.event });
|
|
140
|
+
await ctx.onEvent({ type: 'agent:run:end', data: { ...target } }, endState);
|
|
141
|
+
}
|
|
142
|
+
// Autonomous todo advance: single trigger point, runs once per `agent:run:end`.
|
|
143
|
+
try {
|
|
144
|
+
const handoff = await advanceAfterRun({
|
|
145
|
+
storage: storageService,
|
|
146
|
+
channelId: ctx.channelId,
|
|
147
|
+
threadId: ctx.threadId,
|
|
148
|
+
endedAgentId: step.agentId,
|
|
149
|
+
lastAgentOutput,
|
|
150
|
+
});
|
|
151
|
+
if (handoff && !queuedAgentIds.has(handoff.agentId)) {
|
|
152
|
+
queuedAgentIds.add(handoff.agentId);
|
|
153
|
+
followUps.push({
|
|
154
|
+
agentId: handoff.agentId,
|
|
155
|
+
event: makeInvoke(handoff.content, ctx.threadId),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
console.warn('[dispatcher] todo advance failed', error);
|
|
161
|
+
}
|
|
162
|
+
for (const next of followUps) {
|
|
163
|
+
await runStep(next, ctx, depth + 1);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
// Bus pass-through: run an event through the targeted agent's runtime, forward
|
|
168
|
+
// chunks. No run:start/end, no advance, no follow-ups.
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
async function runBusEvent(event, agentId, ctx) {
|
|
171
|
+
let state;
|
|
172
|
+
try {
|
|
173
|
+
state = await storageService.getOpenBotState({
|
|
174
|
+
runId: ctx.runId,
|
|
175
|
+
agentId,
|
|
176
|
+
channelId: ctx.channelId,
|
|
177
|
+
threadId: ctx.threadId,
|
|
178
|
+
event,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
if (error.code === 'AGENT_NOT_FOUND') {
|
|
183
|
+
// Silently drop: bus pass-through has no UI surface to warn into.
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const runtime = await createAgentRuntime(state);
|
|
190
|
+
for await (const chunk of runtime.run(event, { state, runId: ctx.runId })) {
|
|
191
|
+
if (chunk.id === event.id && chunk.type === event.type)
|
|
192
|
+
continue;
|
|
193
|
+
chunk.meta = { ...chunk.meta, agentId };
|
|
194
|
+
await ctx.onEvent(chunk, state);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
console.error(`[dispatcher] Bus event failed: ${event.type} (${agentId})`, error);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
// Helpers
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
async function normalizeUserInput(event, ctx) {
|
|
205
|
+
const rawContent = event.data?.content || '';
|
|
206
|
+
// The user-facing copy stored/streamed for the UI.
|
|
207
|
+
const userFacing = {
|
|
208
|
+
type: 'agent:invoke',
|
|
209
|
+
id: event.id,
|
|
210
|
+
data: { content: rawContent, role: 'user' },
|
|
211
|
+
meta: {
|
|
212
|
+
agentId: 'system',
|
|
213
|
+
userId: event.meta?.userId,
|
|
214
|
+
userName: event.meta?.userName,
|
|
215
|
+
userAvatarUrl: event.meta?.userAvatarUrl,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
const initialState = await storageService.getOpenBotState({
|
|
219
|
+
runId: ctx.runId,
|
|
220
|
+
agentId: 'system',
|
|
221
|
+
channelId: ctx.channelId,
|
|
222
|
+
threadId: ctx.threadId,
|
|
223
|
+
event: userFacing,
|
|
224
|
+
});
|
|
225
|
+
await ctx.onEvent(userFacing, initialState);
|
|
226
|
+
// The event actually fed to the target agent. Carries the input threadId (or the
|
|
227
|
+
// message id, used as the anchor for Slack-style new threads).
|
|
228
|
+
return {
|
|
229
|
+
...event,
|
|
230
|
+
type: 'agent:invoke',
|
|
231
|
+
data: { ...(event.data || {}), content: rawContent, role: 'user' },
|
|
232
|
+
meta: {
|
|
233
|
+
...(event.meta || {}),
|
|
234
|
+
threadId: ctx.threadId || event.id,
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function makeInvoke(content, threadId, baseMeta) {
|
|
239
|
+
return ensureEventId({
|
|
240
|
+
type: 'agent:invoke',
|
|
241
|
+
data: { role: 'user', content },
|
|
242
|
+
meta: { ...(baseMeta || {}), threadId },
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
async function handleStop(stopEvent, options) {
|
|
246
|
+
const { runId, channelId, threadId, onEvent } = options;
|
|
247
|
+
stopRequests.push({
|
|
248
|
+
runId: stopEvent.data.runId,
|
|
249
|
+
agentId: stopEvent.data.agentId,
|
|
250
|
+
channelId: stopEvent.data.channelId || channelId,
|
|
251
|
+
threadId: stopEvent.data.threadId || threadId,
|
|
252
|
+
reason: stopEvent.data.reason,
|
|
253
|
+
requestedAt: Date.now(),
|
|
254
|
+
});
|
|
255
|
+
const state = await storageService.getOpenBotState({
|
|
256
|
+
runId,
|
|
257
|
+
agentId: options.agentId || 'system',
|
|
258
|
+
channelId,
|
|
259
|
+
threadId,
|
|
260
|
+
event: stopEvent,
|
|
261
|
+
});
|
|
262
|
+
await onEvent({
|
|
263
|
+
type: 'action:agent_run_stop:result',
|
|
264
|
+
data: { success: true, message: `Stop requested for run ${stopEvent.data.runId}.` },
|
|
265
|
+
meta: stopEvent.meta,
|
|
266
|
+
}, state);
|
|
267
|
+
}
|
|
@@ -2,6 +2,30 @@ import { storageService } from '../services/storage.js';
|
|
|
2
2
|
import { createAgentRuntime } from './runtime-factory.js';
|
|
3
3
|
import { EventNormalizer } from './event-normalizer.js';
|
|
4
4
|
import { QueueProcessor } from './queue-processor.js';
|
|
5
|
+
const stopRequests = [];
|
|
6
|
+
const STOP_REQUEST_TTL_MS = 30 * 60 * 1000;
|
|
7
|
+
const pruneStopRequests = () => {
|
|
8
|
+
const now = Date.now();
|
|
9
|
+
for (let i = stopRequests.length - 1; i >= 0; i -= 1) {
|
|
10
|
+
if (now - stopRequests[i].requestedAt > STOP_REQUEST_TTL_MS) {
|
|
11
|
+
stopRequests.splice(i, 1);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const findStopRequest = (options) => {
|
|
16
|
+
pruneStopRequests();
|
|
17
|
+
return stopRequests.find((request) => {
|
|
18
|
+
if (request.runId !== options.runId)
|
|
19
|
+
return false;
|
|
20
|
+
if (request.agentId && request.agentId !== options.agentId)
|
|
21
|
+
return false;
|
|
22
|
+
if (request.channelId && request.channelId !== options.channelId)
|
|
23
|
+
return false;
|
|
24
|
+
if (request.threadId && request.threadId !== options.threadId)
|
|
25
|
+
return false;
|
|
26
|
+
return true;
|
|
27
|
+
});
|
|
28
|
+
};
|
|
5
29
|
export const orchestratorService = {
|
|
6
30
|
/**
|
|
7
31
|
* The primary entry point for all events coming into the system (e.g. from the API).
|
|
@@ -9,6 +33,33 @@ export const orchestratorService = {
|
|
|
9
33
|
*/
|
|
10
34
|
dispatch: async (options) => {
|
|
11
35
|
const { runId, channelId, threadId, onEvent } = options;
|
|
36
|
+
if (options.event.type === 'action:agent_run_stop') {
|
|
37
|
+
const stopEvent = options.event;
|
|
38
|
+
stopRequests.push({
|
|
39
|
+
runId: stopEvent.data.runId,
|
|
40
|
+
agentId: stopEvent.data.agentId,
|
|
41
|
+
channelId: stopEvent.data.channelId || channelId,
|
|
42
|
+
threadId: stopEvent.data.threadId || threadId,
|
|
43
|
+
reason: stopEvent.data.reason,
|
|
44
|
+
requestedAt: Date.now(),
|
|
45
|
+
});
|
|
46
|
+
const state = await storageService.getOpenBotState({
|
|
47
|
+
runId,
|
|
48
|
+
agentId: options.agentId || 'system',
|
|
49
|
+
channelId,
|
|
50
|
+
threadId,
|
|
51
|
+
event: options.event,
|
|
52
|
+
});
|
|
53
|
+
await onEvent({
|
|
54
|
+
type: 'action:agent_run_stop:result',
|
|
55
|
+
data: {
|
|
56
|
+
success: true,
|
|
57
|
+
message: `Stop requested for run ${stopEvent.data.runId}.`,
|
|
58
|
+
},
|
|
59
|
+
meta: options.event.meta,
|
|
60
|
+
}, state);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
12
63
|
// 1. Normalize incoming event
|
|
13
64
|
const { finalEvent, finalAgentId } = await EventNormalizer.normalize(options.event, {
|
|
14
65
|
runId,
|
|
@@ -24,6 +75,7 @@ export const orchestratorService = {
|
|
|
24
75
|
threadId,
|
|
25
76
|
onEvent,
|
|
26
77
|
executeAgent: orchestratorService.executeAgent,
|
|
78
|
+
shouldStopRun: orchestratorService.shouldStopRun,
|
|
27
79
|
});
|
|
28
80
|
// 3. Enqueue initial event
|
|
29
81
|
processor.enqueue({ agentId: finalAgentId, event: finalEvent });
|
|
@@ -59,9 +111,29 @@ export const orchestratorService = {
|
|
|
59
111
|
throw error;
|
|
60
112
|
}
|
|
61
113
|
const agentRuntime = await createAgentRuntime(agentState);
|
|
114
|
+
const stopInfo = {
|
|
115
|
+
runId,
|
|
116
|
+
agentId,
|
|
117
|
+
channelId,
|
|
118
|
+
threadId,
|
|
119
|
+
};
|
|
62
120
|
try {
|
|
63
121
|
// RUN the agent runtime
|
|
64
122
|
for await (const chunk of agentRuntime.run(event, { state: agentState, runId })) {
|
|
123
|
+
const stopRequest = findStopRequest(stopInfo);
|
|
124
|
+
if (stopRequest) {
|
|
125
|
+
await onEvent({
|
|
126
|
+
type: 'agent:run:stopped',
|
|
127
|
+
data: {
|
|
128
|
+
runId,
|
|
129
|
+
agentId,
|
|
130
|
+
channelId,
|
|
131
|
+
threadId,
|
|
132
|
+
reason: stopRequest.reason,
|
|
133
|
+
},
|
|
134
|
+
}, agentState);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
65
137
|
chunk.meta = { ...chunk.meta, agentId };
|
|
66
138
|
await onEvent(chunk, agentState);
|
|
67
139
|
}
|
|
@@ -70,4 +142,8 @@ export const orchestratorService = {
|
|
|
70
142
|
console.error(`[orchestrator] Agent run failed: ${agentId}`, error);
|
|
71
143
|
}
|
|
72
144
|
},
|
|
145
|
+
shouldStopRun: (options) => {
|
|
146
|
+
const request = findStopRequest(options);
|
|
147
|
+
return request ? { shouldStop: true, reason: request.reason } : { shouldStop: false };
|
|
148
|
+
},
|
|
73
149
|
};
|
|
@@ -28,6 +28,32 @@ export class QueueProcessor {
|
|
|
28
28
|
// Run items for the SAME agent sequentially to preserve event order and state consistency.
|
|
29
29
|
for (const item of items) {
|
|
30
30
|
const { event: currentEvent } = item;
|
|
31
|
+
const stopCheck = this.options.shouldStopRun?.({
|
|
32
|
+
runId: this.options.runId,
|
|
33
|
+
agentId,
|
|
34
|
+
channelId: this.options.channelId,
|
|
35
|
+
threadId: this.currentThreadId,
|
|
36
|
+
});
|
|
37
|
+
if (stopCheck?.shouldStop) {
|
|
38
|
+
const stoppedState = await storageService.getOpenBotState({
|
|
39
|
+
runId: this.options.runId,
|
|
40
|
+
agentId,
|
|
41
|
+
channelId: this.options.channelId,
|
|
42
|
+
threadId: this.currentThreadId,
|
|
43
|
+
event: currentEvent,
|
|
44
|
+
});
|
|
45
|
+
await this.options.onEvent({
|
|
46
|
+
type: 'agent:run:stopped',
|
|
47
|
+
data: {
|
|
48
|
+
runId: this.options.runId,
|
|
49
|
+
agentId,
|
|
50
|
+
channelId: this.options.channelId,
|
|
51
|
+
threadId: this.currentThreadId,
|
|
52
|
+
reason: stopCheck.reason,
|
|
53
|
+
},
|
|
54
|
+
}, stoppedState);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
31
57
|
// Track handoff requests queued in this step to avoid accidental duplicates.
|
|
32
58
|
const queuedRequestKeys = new Set();
|
|
33
59
|
const queuedItems = [];
|