openbot 0.5.4 → 0.5.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/CLAUDE.md +5 -4
- package/deploy/README.md +2 -4
- package/dist/app/cli.js +3 -1
- package/dist/app/cloud-mode.js +5 -16
- package/dist/app/ensure-default-stack.js +54 -0
- package/dist/app/openbot-plugin.js +4 -0
- package/dist/app/server.js +2 -3
- package/dist/harness/index.js +3 -0
- package/dist/plugins/openbot/index.js +6 -5
- package/dist/plugins/openbot/model.js +2 -41
- package/dist/plugins/openbot/runtime.js +9 -4
- package/dist/plugins/storage/index.js +3 -325
- package/dist/plugins/storage/service.js +18 -21
- package/dist/services/plugins/host.js +21 -0
- package/dist/services/plugins/model-registry.js +34 -9
- package/dist/services/plugins/registry.js +26 -42
- package/dist/services/todo/types.js +1 -0
- package/docs/templates/AGENT.example.md +8 -14
- package/package.json +2 -2
- package/src/app/cli.ts +3 -1
- package/src/app/cloud-mode.ts +7 -22
- package/src/app/ensure-default-stack.ts +63 -0
- package/src/app/openbot-plugin.ts +5 -0
- package/src/app/server.ts +2 -5
- package/src/app/types.ts +3 -8
- package/src/harness/index.ts +4 -0
- package/src/plugins/storage/index.ts +3 -368
- package/src/plugins/storage/service.ts +17 -22
- package/src/services/plugins/host.ts +36 -0
- package/src/services/plugins/model-registry.ts +41 -9
- package/src/services/plugins/registry.ts +28 -43
- package/src/services/plugins/service.ts +6 -11
- package/src/services/plugins/types.ts +36 -2
- package/src/services/todo/types.ts +12 -0
- package/src/plugins/approval/index.ts +0 -147
- package/src/plugins/bash/index.ts +0 -545
- package/src/plugins/delegation/index.ts +0 -153
- package/src/plugins/memory/index.ts +0 -182
- package/src/plugins/openbot/context.ts +0 -137
- package/src/plugins/openbot/history.ts +0 -158
- package/src/plugins/openbot/index.ts +0 -95
- package/src/plugins/openbot/model.ts +0 -76
- package/src/plugins/openbot/runtime.ts +0 -504
- package/src/plugins/openbot/system-prompt.ts +0 -57
- package/src/plugins/preview/index.ts +0 -323
- package/src/plugins/todo/index.ts +0 -166
- package/src/plugins/todo/service.ts +0 -123
- package/src/plugins/ui/index.ts +0 -130
|
@@ -1,545 +0,0 @@
|
|
|
1
|
-
import { MelonyPlugin } from 'melony';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { spawn, type ChildProcess } from 'node:child_process';
|
|
4
|
-
import { randomUUID } from 'node:crypto';
|
|
5
|
-
import type { Plugin } from '../../services/plugins/types.js';
|
|
6
|
-
import { OpenBotEvent, OpenBotState } from '../../app/types.js';
|
|
7
|
-
import { resolvePath } from '../../app/config.js';
|
|
8
|
-
|
|
9
|
-
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
10
|
-
const FOREGROUND_DEV_TIMEOUT_MS = 5_000;
|
|
11
|
-
const TIMEOUT_EXIT_CODE = 124;
|
|
12
|
-
const MAX_LOG_CHARS = 32_000;
|
|
13
|
-
const DEFAULT_SESSION_ID = 'default';
|
|
14
|
-
|
|
15
|
-
const DEV_SERVER_READY_PATTERNS = [
|
|
16
|
-
/\bready in \d+/i,
|
|
17
|
-
/\blocal\s+https?:\/\//i,
|
|
18
|
-
/\blistening on\b/i,
|
|
19
|
-
/\bstarted server on\b/i,
|
|
20
|
-
/\bwatching for file changes\b/i,
|
|
21
|
-
];
|
|
22
|
-
|
|
23
|
-
const isBackgroundedCommand = (command: string): boolean => {
|
|
24
|
-
const trimmed = command.trim();
|
|
25
|
-
return /\s&\s*$/.test(trimmed) || /\bnohup\b/.test(trimmed) || /\bdisown\b/.test(trimmed);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const looksLikeForegroundDevServer = (command: string): boolean => {
|
|
29
|
-
const trimmed = command.trim();
|
|
30
|
-
if (isBackgroundedCommand(trimmed)) return false;
|
|
31
|
-
return (
|
|
32
|
-
/\b(pnpm|npm|yarn|bun)\s+(run\s+)?dev\b/.test(trimmed) ||
|
|
33
|
-
/\b(astro|vite|next|nuxt|remix)\s+dev\b/.test(trimmed) ||
|
|
34
|
-
/\bpnpm\s+start\b/.test(trimmed) ||
|
|
35
|
-
/\bnpm\s+run\s+start\b/.test(trimmed)
|
|
36
|
-
);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const resolveExecTimeoutMs = (command: string): number =>
|
|
40
|
-
looksLikeForegroundDevServer(command) ? FOREGROUND_DEV_TIMEOUT_MS : DEFAULT_TIMEOUT_MS;
|
|
41
|
-
|
|
42
|
-
const isDevServerReady = (output: string): boolean =>
|
|
43
|
-
DEV_SERVER_READY_PATTERNS.some((pattern) => pattern.test(output));
|
|
44
|
-
|
|
45
|
-
const formatTimeoutOutput = (partialOutput: string, timeoutMs: number, ready: boolean): string => {
|
|
46
|
-
const seconds = Math.round(timeoutMs / 1000);
|
|
47
|
-
const statusLine = ready
|
|
48
|
-
? `[shell_exec timed out after ${seconds}s — process is still running and appears ready. Use shell_view to confirm the URL/port. Do not start a duplicate server.]`
|
|
49
|
-
: `[shell_exec timed out after ${seconds}s — process may still be starting. Poll with shell_wait then shell_view until ready. Do not start a duplicate server.]`;
|
|
50
|
-
const body = partialOutput.trim();
|
|
51
|
-
return body ? `${body}\n\n${statusLine}` : statusLine;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
type ExecResult = {
|
|
55
|
-
exitCode: number;
|
|
56
|
-
output: string;
|
|
57
|
-
timedOut?: boolean;
|
|
58
|
-
stillRunning?: boolean;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const shellToolDefinitions = {
|
|
62
|
-
shell_exec: {
|
|
63
|
-
description:
|
|
64
|
-
'Execute a command in a stateful shell session. Blocks until the command exits. Foreground dev servers (e.g. `pnpm dev` without `&`) return after ~15s with output so far — poll with shell_wait/shell_view until ready. Prefer `pnpm dev &` to return immediately.',
|
|
65
|
-
inputSchema: z.object({
|
|
66
|
-
id: z
|
|
67
|
-
.string()
|
|
68
|
-
.describe('Shell session identifier (e.g. "default", "server"). Reuse ids to keep state.'),
|
|
69
|
-
exec_dir: z
|
|
70
|
-
.string()
|
|
71
|
-
.describe('Working directory for this command (absolute path).'),
|
|
72
|
-
command: z.string().describe('Shell command to execute.'),
|
|
73
|
-
}),
|
|
74
|
-
},
|
|
75
|
-
shell_view: {
|
|
76
|
-
description:
|
|
77
|
-
'View recent output from a shell session. Use to poll dev-server logs after shell_exec times out or after backgrounding with `&`.',
|
|
78
|
-
inputSchema: z.object({
|
|
79
|
-
id: z.string().describe('Shell session identifier.'),
|
|
80
|
-
}),
|
|
81
|
-
},
|
|
82
|
-
shell_wait: {
|
|
83
|
-
description:
|
|
84
|
-
'Wait N seconds, then return recent shell output. Use with shell_view to poll dev-server startup after shell_exec times out (e.g. shell_wait 3s, then shell_view, repeat until ready).',
|
|
85
|
-
inputSchema: z.object({
|
|
86
|
-
id: z.string().describe('Shell session identifier.'),
|
|
87
|
-
seconds: z.number().int().min(1).max(300).describe('Seconds to wait.'),
|
|
88
|
-
}),
|
|
89
|
-
},
|
|
90
|
-
shell_write_to_process: {
|
|
91
|
-
description:
|
|
92
|
-
'Write input to a running process in a shell session. Use to answer interactive prompts.',
|
|
93
|
-
inputSchema: z.object({
|
|
94
|
-
id: z.string().describe('Shell session identifier.'),
|
|
95
|
-
input: z.string().describe('Input to send to the process.'),
|
|
96
|
-
press_enter: z
|
|
97
|
-
.boolean()
|
|
98
|
-
.describe('Whether to press Enter after the input.'),
|
|
99
|
-
}),
|
|
100
|
-
},
|
|
101
|
-
shell_kill_process: {
|
|
102
|
-
description:
|
|
103
|
-
'Send interrupt to the active process in a shell session (e.g. stop a dev server).',
|
|
104
|
-
inputSchema: z.object({
|
|
105
|
-
id: z.string().describe('Shell session identifier.'),
|
|
106
|
-
}),
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const shellQuote = (value: string): string => `'${value.replace(/'/g, `'\\''`)}'`;
|
|
111
|
-
|
|
112
|
-
const resolveCwd = (context: { state: OpenBotState }, execDir?: string): string => {
|
|
113
|
-
const raw =
|
|
114
|
-
(typeof execDir === 'string' && execDir.trim()) ||
|
|
115
|
-
context.state.channelDetails?.cwd ||
|
|
116
|
-
process.cwd();
|
|
117
|
-
return resolvePath(raw);
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const sessionKey = (channelId: string, id: string) => `${channelId}:${id}`;
|
|
121
|
-
|
|
122
|
-
class ShellSession {
|
|
123
|
-
private output = '';
|
|
124
|
-
private process: ChildProcess | null = null;
|
|
125
|
-
private execQueue: Promise<unknown> = Promise.resolve();
|
|
126
|
-
private pending:
|
|
127
|
-
| {
|
|
128
|
-
marker: string;
|
|
129
|
-
startLen: number;
|
|
130
|
-
timeoutMs: number;
|
|
131
|
-
resolve: (result: ExecResult) => void;
|
|
132
|
-
reject: (error: Error) => void;
|
|
133
|
-
timer: NodeJS.Timeout;
|
|
134
|
-
}
|
|
135
|
-
| undefined;
|
|
136
|
-
|
|
137
|
-
constructor(
|
|
138
|
-
readonly channelId: string,
|
|
139
|
-
readonly id: string,
|
|
140
|
-
readonly cwd: string,
|
|
141
|
-
) {
|
|
142
|
-
this.spawn();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private spawn() {
|
|
146
|
-
this.process = spawn('bash', [], {
|
|
147
|
-
cwd: this.cwd,
|
|
148
|
-
env: process.env,
|
|
149
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
this.process.stdout?.on('data', (chunk: Buffer) => this.append(chunk.toString()));
|
|
153
|
-
this.process.stderr?.on('data', (chunk: Buffer) => this.append(chunk.toString()));
|
|
154
|
-
this.process.on('exit', () => {
|
|
155
|
-
this.process = null;
|
|
156
|
-
this.rejectPending(new Error('Shell session exited unexpectedly'));
|
|
157
|
-
});
|
|
158
|
-
this.process.on('error', (error) => {
|
|
159
|
-
this.rejectPending(error);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
private rejectPending(error: Error) {
|
|
164
|
-
if (!this.pending) return;
|
|
165
|
-
clearTimeout(this.pending.timer);
|
|
166
|
-
this.pending.reject(error);
|
|
167
|
-
this.pending = undefined;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
private append(chunk: string) {
|
|
171
|
-
this.output += chunk;
|
|
172
|
-
if (this.output.length > MAX_LOG_CHARS) {
|
|
173
|
-
this.output = this.output.slice(-MAX_LOG_CHARS);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (!this.pending) return;
|
|
177
|
-
const tail = this.output.slice(this.pending.startLen);
|
|
178
|
-
const markerIndex = tail.indexOf(this.pending.marker);
|
|
179
|
-
if (markerIndex === -1) return;
|
|
180
|
-
|
|
181
|
-
const afterMarker = tail.slice(markerIndex + this.pending.marker.length);
|
|
182
|
-
const match = afterMarker.match(/^:(\d+)/);
|
|
183
|
-
const exitCode = match ? Number.parseInt(match[1], 10) : 0;
|
|
184
|
-
const output = tail.slice(0, markerIndex).trimEnd();
|
|
185
|
-
|
|
186
|
-
clearTimeout(this.pending.timer);
|
|
187
|
-
this.pending.resolve({ exitCode, output });
|
|
188
|
-
this.pending = undefined;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
private ensureProcess() {
|
|
192
|
-
if (!this.process?.stdin) {
|
|
193
|
-
this.spawn();
|
|
194
|
-
}
|
|
195
|
-
if (!this.process?.stdin) {
|
|
196
|
-
throw new Error('Failed to start shell session');
|
|
197
|
-
}
|
|
198
|
-
return this.process;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
private enqueue<T>(fn: () => Promise<T>): Promise<T> {
|
|
202
|
-
const next = this.execQueue.then(fn, fn);
|
|
203
|
-
this.execQueue = next.catch(() => undefined);
|
|
204
|
-
return next;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
view(): string {
|
|
208
|
-
return this.output.slice(-8000);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
async exec(command: string, execDir: string): Promise<ExecResult> {
|
|
212
|
-
return this.enqueue(() => this.execInternal(command, execDir));
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
private execInternal(command: string, execDir: string): Promise<ExecResult> {
|
|
216
|
-
const process = this.ensureProcess();
|
|
217
|
-
const marker = `__OPENBOT_${randomUUID().replace(/-/g, '')}__`;
|
|
218
|
-
const script = `cd ${shellQuote(execDir)} && ${command}; printf '\\n${marker}:%s\\n' "$?"`;
|
|
219
|
-
const timeoutMs = resolveExecTimeoutMs(command);
|
|
220
|
-
|
|
221
|
-
return new Promise((resolve, reject) => {
|
|
222
|
-
if (this.pending) {
|
|
223
|
-
reject(new Error('Shell session is busy'));
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const startLen = this.output.length;
|
|
228
|
-
const timer = setTimeout(() => {
|
|
229
|
-
if (!this.pending) return;
|
|
230
|
-
const { startLen: pendingStartLen, timeoutMs: pendingTimeoutMs, resolve: pendingResolve } =
|
|
231
|
-
this.pending;
|
|
232
|
-
clearTimeout(this.pending.timer);
|
|
233
|
-
this.pending = undefined;
|
|
234
|
-
|
|
235
|
-
const partial = this.output.slice(pendingStartLen).trimEnd();
|
|
236
|
-
const ready = isDevServerReady(partial);
|
|
237
|
-
pendingResolve({
|
|
238
|
-
exitCode: TIMEOUT_EXIT_CODE,
|
|
239
|
-
output: formatTimeoutOutput(partial, pendingTimeoutMs, ready),
|
|
240
|
-
timedOut: true,
|
|
241
|
-
stillRunning: true,
|
|
242
|
-
});
|
|
243
|
-
}, timeoutMs);
|
|
244
|
-
|
|
245
|
-
this.pending = { marker, startLen, timeoutMs, resolve, reject, timer };
|
|
246
|
-
|
|
247
|
-
try {
|
|
248
|
-
process.stdin!.write(`${script}\n`);
|
|
249
|
-
} catch (error) {
|
|
250
|
-
clearTimeout(timer);
|
|
251
|
-
this.pending = undefined;
|
|
252
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async wait(seconds: number): Promise<string> {
|
|
258
|
-
await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
|
|
259
|
-
return this.view();
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
write(input: string, pressEnter: boolean) {
|
|
263
|
-
const process = this.ensureProcess();
|
|
264
|
-
process.stdin!.write(pressEnter ? `${input}\n` : input);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
kill() {
|
|
268
|
-
const process = this.ensureProcess();
|
|
269
|
-
process.stdin!.write('\x03');
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
destroy() {
|
|
273
|
-
this.rejectPending(new Error('Shell session closed'));
|
|
274
|
-
if (!this.process) return;
|
|
275
|
-
try {
|
|
276
|
-
this.process.kill('SIGTERM');
|
|
277
|
-
} catch {
|
|
278
|
-
// ignore
|
|
279
|
-
}
|
|
280
|
-
this.process = null;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const sessions = new Map<string, ShellSession>();
|
|
285
|
-
|
|
286
|
-
const getSession = (
|
|
287
|
-
channelId: string,
|
|
288
|
-
id: string,
|
|
289
|
-
defaultCwd: string,
|
|
290
|
-
): ShellSession => {
|
|
291
|
-
const key = sessionKey(channelId, id);
|
|
292
|
-
const existing = sessions.get(key);
|
|
293
|
-
if (existing) return existing;
|
|
294
|
-
|
|
295
|
-
const session = new ShellSession(channelId, id, defaultCwd);
|
|
296
|
-
sessions.set(key, session);
|
|
297
|
-
return session;
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
const destroySessionsForChannel = (channelId: string) => {
|
|
301
|
-
for (const [key, session] of sessions.entries()) {
|
|
302
|
-
if (!key.startsWith(`${channelId}:`)) continue;
|
|
303
|
-
session.destroy();
|
|
304
|
-
sessions.delete(key);
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
const formatResult = (output: string, extra?: Record<string, unknown>) => ({
|
|
309
|
-
success: true,
|
|
310
|
-
output: output.trim() || '(no output)',
|
|
311
|
-
...extra,
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
const resolveShellWidgetId = (event: OpenBotEvent): string =>
|
|
315
|
-
event.meta?.toolCallId || randomUUID();
|
|
316
|
-
|
|
317
|
-
const formatShellWidgetBody = (
|
|
318
|
-
input: Record<string, unknown>,
|
|
319
|
-
output: string,
|
|
320
|
-
): string => {
|
|
321
|
-
const inputText = JSON.stringify(input, null, 2);
|
|
322
|
-
const outputText = output.trim() || '(no output)';
|
|
323
|
-
return `Input:\n${inputText}\n\nOutput:\n${outputText}`;
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
function* emitShellWidgetPending(
|
|
327
|
-
event: OpenBotEvent,
|
|
328
|
-
context: { state: OpenBotState },
|
|
329
|
-
tool: string,
|
|
330
|
-
input: Record<string, unknown>,
|
|
331
|
-
widgetId: string,
|
|
332
|
-
): Generator<OpenBotEvent> {
|
|
333
|
-
const threadId = event.meta?.threadId || context.state.threadId;
|
|
334
|
-
|
|
335
|
-
yield {
|
|
336
|
-
type: 'client:ui:widget',
|
|
337
|
-
data: {
|
|
338
|
-
widgetId,
|
|
339
|
-
kind: 'message',
|
|
340
|
-
title: tool,
|
|
341
|
-
body: formatShellWidgetBody(input, '(running...)'),
|
|
342
|
-
display: 'collapsed',
|
|
343
|
-
metadata: {
|
|
344
|
-
type: 'shell:tool',
|
|
345
|
-
tool,
|
|
346
|
-
input,
|
|
347
|
-
status: 'running',
|
|
348
|
-
},
|
|
349
|
-
},
|
|
350
|
-
meta: { agentId: context.state.agentId, threadId },
|
|
351
|
-
} as OpenBotEvent;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
function* emitShellToolResult(
|
|
355
|
-
event: OpenBotEvent,
|
|
356
|
-
context: { state: OpenBotState },
|
|
357
|
-
tool: string,
|
|
358
|
-
input: Record<string, unknown>,
|
|
359
|
-
result: { success: boolean; output: string; [key: string]: unknown },
|
|
360
|
-
widgetId: string,
|
|
361
|
-
): Generator<OpenBotEvent> {
|
|
362
|
-
const threadId = event.meta?.threadId || context.state.threadId;
|
|
363
|
-
const output = String(result.output ?? '');
|
|
364
|
-
|
|
365
|
-
yield {
|
|
366
|
-
type: 'client:ui:widget',
|
|
367
|
-
data: {
|
|
368
|
-
widgetId,
|
|
369
|
-
kind: 'message',
|
|
370
|
-
title: tool,
|
|
371
|
-
body: formatShellWidgetBody(input, output),
|
|
372
|
-
state: result.success ? 'submitted' : 'error',
|
|
373
|
-
display: 'collapsed',
|
|
374
|
-
metadata: {
|
|
375
|
-
type: 'shell:tool',
|
|
376
|
-
tool,
|
|
377
|
-
input,
|
|
378
|
-
output,
|
|
379
|
-
success: result.success,
|
|
380
|
-
status: result.success ? 'done' : 'error',
|
|
381
|
-
},
|
|
382
|
-
},
|
|
383
|
-
meta: { agentId: context.state.agentId, threadId },
|
|
384
|
-
} as OpenBotEvent;
|
|
385
|
-
|
|
386
|
-
const { output: _output, ...resultData } = result;
|
|
387
|
-
yield {
|
|
388
|
-
type: `action:${tool}:result` as OpenBotEvent['type'],
|
|
389
|
-
data: { ...resultData, output },
|
|
390
|
-
meta: event.meta,
|
|
391
|
-
} as OpenBotEvent;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
async function* runShellTool(
|
|
395
|
-
event: OpenBotEvent,
|
|
396
|
-
context: { state: OpenBotState },
|
|
397
|
-
tool: string,
|
|
398
|
-
input: Record<string, unknown>,
|
|
399
|
-
execute: () => Promise<{ success: boolean; output: string; [key: string]: unknown }>,
|
|
400
|
-
): AsyncGenerator<OpenBotEvent> {
|
|
401
|
-
const widgetId = resolveShellWidgetId(event);
|
|
402
|
-
yield* emitShellWidgetPending(event, context, tool, input, widgetId);
|
|
403
|
-
|
|
404
|
-
try {
|
|
405
|
-
const result = await execute();
|
|
406
|
-
yield* emitShellToolResult(event, context, tool, input, result, widgetId);
|
|
407
|
-
} catch (error) {
|
|
408
|
-
const message = error instanceof Error ? error.message : 'Unknown shell error';
|
|
409
|
-
yield* emitShellToolResult(
|
|
410
|
-
event,
|
|
411
|
-
context,
|
|
412
|
-
tool,
|
|
413
|
-
input,
|
|
414
|
-
{ success: false, output: message, error: message },
|
|
415
|
-
widgetId,
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
const shellPluginRuntime = (): MelonyPlugin<OpenBotState, OpenBotEvent> => (builder) => {
|
|
421
|
-
builder.on('action:shell_exec', async function* (event, context) {
|
|
422
|
-
const { id, exec_dir, command } = event.data as {
|
|
423
|
-
id?: string;
|
|
424
|
-
exec_dir?: string;
|
|
425
|
-
command?: string;
|
|
426
|
-
};
|
|
427
|
-
const sessionId = (id || DEFAULT_SESSION_ID).trim() || DEFAULT_SESSION_ID;
|
|
428
|
-
const channelId = context.state.channelId;
|
|
429
|
-
|
|
430
|
-
const input = {
|
|
431
|
-
id: sessionId,
|
|
432
|
-
exec_dir: exec_dir ?? resolveCwd(context),
|
|
433
|
-
command: command ?? '',
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
if (!command?.trim()) {
|
|
437
|
-
yield* runShellTool(event, context, 'shell_exec', input, async () => ({
|
|
438
|
-
success: false,
|
|
439
|
-
output: 'command is required',
|
|
440
|
-
}));
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
yield* runShellTool(event, context, 'shell_exec', input, async () => {
|
|
445
|
-
const execDir = resolveCwd(context, exec_dir);
|
|
446
|
-
const session = getSession(channelId, sessionId, execDir);
|
|
447
|
-
const result = await session.exec(command, execDir);
|
|
448
|
-
const success = result.timedOut ? isDevServerReady(result.output) : result.exitCode === 0;
|
|
449
|
-
return {
|
|
450
|
-
success,
|
|
451
|
-
exitCode: result.exitCode,
|
|
452
|
-
output: result.output.trim() || '(no output)',
|
|
453
|
-
...(result.timedOut && { timedOut: true, stillRunning: result.stillRunning }),
|
|
454
|
-
};
|
|
455
|
-
});
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
builder.on('action:shell_view', async function* (event, context) {
|
|
459
|
-
const sessionId = ((event.data as { id?: string })?.id || DEFAULT_SESSION_ID).trim();
|
|
460
|
-
const channelId = context.state.channelId;
|
|
461
|
-
const defaultCwd = resolveCwd(context);
|
|
462
|
-
const input = { id: sessionId };
|
|
463
|
-
|
|
464
|
-
yield* runShellTool(event, context, 'shell_view', input, async () => {
|
|
465
|
-
const session = getSession(channelId, sessionId, defaultCwd);
|
|
466
|
-
return formatResult(session.view());
|
|
467
|
-
});
|
|
468
|
-
});
|
|
469
|
-
|
|
470
|
-
builder.on('action:shell_wait', async function* (event, context) {
|
|
471
|
-
const { id, seconds } = event.data as { id?: string; seconds?: number };
|
|
472
|
-
const sessionId = (id || DEFAULT_SESSION_ID).trim();
|
|
473
|
-
const channelId = context.state.channelId;
|
|
474
|
-
const defaultCwd = resolveCwd(context);
|
|
475
|
-
const waitedSeconds = seconds ?? 5;
|
|
476
|
-
const input = { id: sessionId, seconds: waitedSeconds };
|
|
477
|
-
|
|
478
|
-
yield* runShellTool(event, context, 'shell_wait', input, async () => {
|
|
479
|
-
const session = getSession(channelId, sessionId, defaultCwd);
|
|
480
|
-
const output = await session.wait(waitedSeconds);
|
|
481
|
-
return formatResult(output, { waitedSeconds });
|
|
482
|
-
});
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
builder.on('action:shell_write_to_process', async function* (event, context) {
|
|
486
|
-
const { id, input: textInput, press_enter } = event.data as {
|
|
487
|
-
id?: string;
|
|
488
|
-
input?: string;
|
|
489
|
-
press_enter?: boolean;
|
|
490
|
-
};
|
|
491
|
-
const sessionId = (id || DEFAULT_SESSION_ID).trim();
|
|
492
|
-
const channelId = context.state.channelId;
|
|
493
|
-
const defaultCwd = resolveCwd(context);
|
|
494
|
-
|
|
495
|
-
const toolInput = {
|
|
496
|
-
id: sessionId,
|
|
497
|
-
input: textInput ?? '',
|
|
498
|
-
press_enter: press_enter ?? true,
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
if (!textInput?.trim()) {
|
|
502
|
-
yield* runShellTool(event, context, 'shell_write_to_process', toolInput, async () => ({
|
|
503
|
-
success: false,
|
|
504
|
-
output: 'input is required',
|
|
505
|
-
}));
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
yield* runShellTool(event, context, 'shell_write_to_process', toolInput, async () => {
|
|
510
|
-
const session = getSession(channelId, sessionId, defaultCwd);
|
|
511
|
-
session.write(textInput, toolInput.press_enter);
|
|
512
|
-
return { success: true, output: 'Input sent to shell session.' };
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
|
|
516
|
-
builder.on('action:shell_kill_process', async function* (event, context) {
|
|
517
|
-
const sessionId = ((event.data as { id?: string })?.id || DEFAULT_SESSION_ID).trim();
|
|
518
|
-
const channelId = context.state.channelId;
|
|
519
|
-
const defaultCwd = resolveCwd(context);
|
|
520
|
-
const input = { id: sessionId };
|
|
521
|
-
|
|
522
|
-
yield* runShellTool(event, context, 'shell_kill_process', input, async () => {
|
|
523
|
-
const session = getSession(channelId, sessionId, defaultCwd);
|
|
524
|
-
session.kill();
|
|
525
|
-
return { success: true, output: 'Interrupt sent to shell session.' };
|
|
526
|
-
});
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
builder.on('action:delete_channel', async function* (event) {
|
|
530
|
-
const channelId = (event.data as { channelId?: string })?.channelId;
|
|
531
|
-
if (channelId) {
|
|
532
|
-
destroySessionsForChannel(channelId);
|
|
533
|
-
}
|
|
534
|
-
});
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
export const bashPlugin: Plugin = {
|
|
538
|
-
id: 'bash',
|
|
539
|
-
name: 'Shell',
|
|
540
|
-
description: 'Stateful shell sessions for the channel workspace (Manus-style).',
|
|
541
|
-
toolDefinitions: shellToolDefinitions,
|
|
542
|
-
factory: () => shellPluginRuntime(),
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
export default bashPlugin;
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { generateId } from 'melony';
|
|
3
|
-
import type { Plugin } from '../../services/plugins/types.js';
|
|
4
|
-
import {
|
|
5
|
-
OpenBotEvent,
|
|
6
|
-
DelegateTaskEvent,
|
|
7
|
-
} from '../../app/types.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* `delegation` — allows agents to delegate tasks to other agents.
|
|
11
|
-
*
|
|
12
|
-
* Only the 'system' agent is allowed to delegate by default.
|
|
13
|
-
* It uses runAgent to execute the delegated agent in its own isolated runtime,
|
|
14
|
-
* bridging events back to the caller's stream.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
const delegationToolDefinitions = {
|
|
18
|
-
delegate_task: {
|
|
19
|
-
description: 'Delegate a specific task or question to another specialized agent.',
|
|
20
|
-
inputSchema: z.object({
|
|
21
|
-
agentId: z.string().describe('The ID of the agent to delegate to (e.g., "researcher", "coder").'),
|
|
22
|
-
prompt: z.string().describe('The instructions or question for the delegated agent.'),
|
|
23
|
-
}),
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const delegationPlugin: Plugin = {
|
|
28
|
-
id: 'delegation',
|
|
29
|
-
name: 'Delegation',
|
|
30
|
-
description: 'Allows agents to call upon other agents to solve sub-tasks.',
|
|
31
|
-
toolDefinitions: delegationToolDefinitions,
|
|
32
|
-
factory: (pluginContext) => (builder) => {
|
|
33
|
-
|
|
34
|
-
// Handle the tool execution
|
|
35
|
-
builder.on('action:delegate_task', async function* (event, context) {
|
|
36
|
-
const delegateEvent = event as DelegateTaskEvent;
|
|
37
|
-
|
|
38
|
-
// POLICY: Only the 'system' agent can delegate
|
|
39
|
-
if (context.state.agentId !== 'system') {
|
|
40
|
-
yield {
|
|
41
|
-
type: 'action:delegate_task:result',
|
|
42
|
-
data: {
|
|
43
|
-
success: false,
|
|
44
|
-
error: 'Only the system agent can delegate.'
|
|
45
|
-
},
|
|
46
|
-
meta: delegateEvent.meta,
|
|
47
|
-
} as OpenBotEvent;
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const { agentId, prompt } = delegateEvent.data;
|
|
52
|
-
const toolCallId = delegateEvent.meta?.toolCallId;
|
|
53
|
-
|
|
54
|
-
if (!toolCallId) return;
|
|
55
|
-
|
|
56
|
-
// Break circular dependency by dynamic import
|
|
57
|
-
const { runAgent } = await import('../../harness/index.js');
|
|
58
|
-
|
|
59
|
-
const runId = `dg_${generateId()}`;
|
|
60
|
-
let lastAgentOutput = '';
|
|
61
|
-
|
|
62
|
-
// Queue to bridge the async onEvent callback to this generator
|
|
63
|
-
const eventQueue: OpenBotEvent[] = [];
|
|
64
|
-
let resolveNext: (() => void) | null = null;
|
|
65
|
-
let isFinished = false;
|
|
66
|
-
|
|
67
|
-
// Start the delegated agent in its own runtime.
|
|
68
|
-
// We don't await this immediately so we can yield events as they arrive.
|
|
69
|
-
const runPromise = runAgent({
|
|
70
|
-
runId,
|
|
71
|
-
agentId,
|
|
72
|
-
event: {
|
|
73
|
-
type: 'agent:invoke',
|
|
74
|
-
data: {
|
|
75
|
-
role: 'user',
|
|
76
|
-
content: prompt,
|
|
77
|
-
agentId: agentId,
|
|
78
|
-
},
|
|
79
|
-
meta: {
|
|
80
|
-
threadId: context.state.threadId,
|
|
81
|
-
parentAgentId: context.state.agentId,
|
|
82
|
-
parentToolCallId: toolCallId,
|
|
83
|
-
},
|
|
84
|
-
} as OpenBotEvent,
|
|
85
|
-
channelId: context.state.channelId,
|
|
86
|
-
threadId: context.state.threadId,
|
|
87
|
-
publicBaseUrl: pluginContext.publicBaseUrl,
|
|
88
|
-
// Child events are re-yielded to the parent harness, which persists them once.
|
|
89
|
-
persistEvents: false,
|
|
90
|
-
onEvent: async (outEvent) => {
|
|
91
|
-
// Enrich events with parent metadata so the UI can track the hierarchy
|
|
92
|
-
const enrichedEvent = {
|
|
93
|
-
...outEvent,
|
|
94
|
-
meta: {
|
|
95
|
-
...outEvent.meta,
|
|
96
|
-
parentAgentId: context.state.agentId,
|
|
97
|
-
parentToolCallId: toolCallId,
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
eventQueue.push(enrichedEvent);
|
|
102
|
-
|
|
103
|
-
if (outEvent.type === 'agent:output') {
|
|
104
|
-
lastAgentOutput = outEvent.data.content;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Wake up the generator loop if it's waiting
|
|
108
|
-
if (resolveNext) {
|
|
109
|
-
resolveNext();
|
|
110
|
-
resolveNext = null;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}).catch(error => {
|
|
114
|
-
console.error(`[delegation] Error in delegated run ${runId}:`, error);
|
|
115
|
-
}).finally(() => {
|
|
116
|
-
isFinished = true;
|
|
117
|
-
if (resolveNext) {
|
|
118
|
-
resolveNext();
|
|
119
|
-
resolveNext = null;
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// Yield events from the delegated agent as they arrive
|
|
124
|
-
while (!isFinished || eventQueue.length > 0) {
|
|
125
|
-
if (eventQueue.length === 0) {
|
|
126
|
-
await new Promise<void>(r => { resolveNext = r; });
|
|
127
|
-
}
|
|
128
|
-
while (eventQueue.length > 0) {
|
|
129
|
-
yield eventQueue.shift()!;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Ensure the run is fully complete (though isFinished already implies this)
|
|
134
|
-
await runPromise;
|
|
135
|
-
|
|
136
|
-
// Yield the result back to our own LLM runtime.
|
|
137
|
-
yield {
|
|
138
|
-
type: 'action:delegate_task:result',
|
|
139
|
-
data: {
|
|
140
|
-
success: true,
|
|
141
|
-
output: lastAgentOutput,
|
|
142
|
-
},
|
|
143
|
-
meta: {
|
|
144
|
-
...delegateEvent.meta,
|
|
145
|
-
agentId: context.state.agentId,
|
|
146
|
-
toolCallId: toolCallId,
|
|
147
|
-
},
|
|
148
|
-
} as OpenBotEvent;
|
|
149
|
-
});
|
|
150
|
-
},
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
export default delegationPlugin;
|