openbot 0.4.7 → 0.5.4
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/agents/runtime-security-reviewer.md +32 -0
- package/.claude/hooks/format.sh +18 -0
- package/.claude/hooks/protect-secrets.sh +23 -0
- package/.claude/settings.json +26 -0
- package/.claude/skills/deploy/SKILL.md +26 -0
- package/.claude/skills/new-plugin/SKILL.md +46 -0
- package/.dockerignore +9 -0
- package/.mcp.json +8 -0
- package/AGENTS.md +2 -2
- package/CLAUDE.md +50 -0
- package/Dockerfile +41 -0
- package/deploy/README.md +61 -0
- package/deploy/entrypoint.sh +6 -0
- package/deploy/fly.toml +36 -0
- package/dist/app/bootstrap.js +29 -0
- package/dist/app/cli.js +3 -1
- package/dist/app/cloud-mode.js +26 -0
- package/dist/app/config.js +61 -16
- package/dist/app/server.js +38 -6
- package/dist/plugins/approval/index.js +1 -1
- package/dist/plugins/bash/index.js +386 -164
- package/dist/plugins/memory/service.js +2 -5
- package/dist/plugins/openbot/context.js +17 -1
- package/dist/plugins/openbot/index.js +23 -8
- package/dist/plugins/openbot/model.js +59 -0
- package/dist/plugins/openbot/runtime.js +39 -56
- package/dist/plugins/openbot/system-prompt.js +43 -8
- package/dist/plugins/preview/index.js +267 -0
- package/dist/plugins/storage/index.js +3 -3
- package/dist/plugins/storage/service.js +58 -12
- package/dist/plugins/todo/index.js +131 -38
- package/dist/plugins/todo/service.js +93 -0
- package/dist/plugins/ui/index.js +8 -1
- package/dist/services/plugins/model-registry.js +101 -0
- package/dist/services/plugins/registry.js +6 -4
- package/dist/services/plugins/service.js +2 -6
- package/docs/agents.md +20 -2
- package/docs/architecture.md +1 -1
- package/docs/plugins.md +13 -3
- package/docs/templates/AGENT.example.md +1 -1
- package/package.json +5 -2
- package/pnpm-workspace.yaml +2 -0
- package/src/app/bootstrap.ts +43 -0
- package/src/app/cli.ts +3 -1
- package/src/app/cloud-mode.ts +41 -0
- package/src/app/config.ts +71 -19
- package/src/app/server.ts +45 -6
- package/src/app/types.ts +162 -31
- package/src/plugins/approval/index.ts +1 -1
- package/src/plugins/bash/index.ts +498 -185
- package/src/plugins/memory/service.ts +2 -5
- package/src/plugins/openbot/context.ts +17 -1
- package/src/plugins/openbot/index.ts +25 -10
- package/src/plugins/openbot/model.ts +76 -0
- package/src/plugins/openbot/runtime.ts +47 -69
- package/src/plugins/openbot/system-prompt.ts +43 -8
- package/src/plugins/preview/index.ts +323 -0
- package/src/plugins/storage/index.ts +5 -8
- package/src/plugins/storage/service.ts +79 -15
- package/src/plugins/todo/index.ts +166 -0
- package/src/plugins/todo/service.ts +123 -0
- package/src/plugins/ui/index.ts +8 -1
- package/src/services/plugins/domain.ts +2 -0
- package/src/services/plugins/model-registry.ts +146 -0
- package/src/services/plugins/registry.ts +6 -4
- package/src/services/plugins/service.ts +2 -6
- package/dist/agents/openbot/index.js +0 -76
- package/dist/agents/openbot/middleware/approval.js +0 -132
- package/dist/agents/openbot/runtime.js +0 -289
- package/dist/agents/openbot/system-prompt.js +0 -32
- package/dist/agents/openbot/tools/delegation.js +0 -78
- package/dist/agents/openbot/tools/mcp.js +0 -99
- package/dist/agents/openbot/tools/shell.js +0 -91
- package/dist/agents/openbot/tools/storage.js +0 -75
- package/dist/agents/openbot/tools/ui.js +0 -176
- package/dist/agents/system.js +0 -33
- package/dist/bus/agent-package.js +0 -1
- package/dist/bus/plugin.js +0 -1
- package/dist/bus/services.js +0 -754
- package/dist/bus/types.js +0 -1
- package/dist/harness/agent-harness.js +0 -45
- package/dist/harness/agent-invoke-run.js +0 -44
- package/dist/harness/agent-turn.js +0 -99
- package/dist/harness/channel-participants.js +0 -40
- package/dist/harness/constants.js +0 -2
- package/dist/harness/context-meter.js +0 -97
- package/dist/harness/context.js +0 -363
- package/dist/harness/dispatch.js +0 -144
- package/dist/harness/dispatcher.js +0 -156
- package/dist/harness/event-normalizer.js +0 -59
- package/dist/harness/history.js +0 -177
- package/dist/harness/mcp.js +0 -61
- package/dist/harness/orchestration.js +0 -88
- package/dist/harness/orchestrator.js +0 -149
- package/dist/harness/participants.js +0 -22
- package/dist/harness/process.js +0 -29
- package/dist/harness/queue-processor.js +0 -187
- package/dist/harness/run-harness.js +0 -154
- package/dist/harness/run.js +0 -98
- package/dist/harness/runtime-factory.js +0 -73
- package/dist/harness/runtime.js +0 -57
- package/dist/harness/todo-advance.js +0 -93
- package/dist/harness/todo-dispatch.js +0 -51
- package/dist/harness/todos.js +0 -5
- package/dist/harness/turn.js +0 -79
- package/dist/harness/types.js +0 -1
- package/dist/plugins/ai-sdk/index.js +0 -34
- package/dist/plugins/ai-sdk/runtime.js +0 -395
- package/dist/plugins/ai-sdk/system-prompt.js +0 -18
- package/dist/plugins/ai-sdk.js +0 -331
- package/dist/plugins/approval.js +0 -163
- package/dist/plugins/delegation.js +0 -108
- package/dist/plugins/mcp/index.js +0 -108
- package/dist/plugins/mcp.js +0 -140
- package/dist/plugins/shell/index.js +0 -100
- package/dist/plugins/shell.js +0 -123
- package/dist/plugins/storage-tools/index.js +0 -76
- package/dist/plugins/storage.js +0 -737
- package/dist/plugins/thread-namer/index.js +0 -72
- package/dist/plugins/threads/index.js +0 -114
- package/dist/plugins/ui.js +0 -211
- package/dist/plugins/workflow/index.js +0 -65
- package/dist/registry/agents.js +0 -138
- package/dist/registry/plugins.js +0 -110
- package/dist/services/agent-packages.js +0 -103
- package/dist/services/memory.js +0 -152
- package/dist/services/plugins.js +0 -98
- package/dist/services/storage.js +0 -1028
- package/dist/workflow/service.js +0 -106
- package/dist/workflow/types.js +0 -3
package/dist/app/server.js
CHANGED
|
@@ -8,7 +8,8 @@ import { createRequire } from 'module';
|
|
|
8
8
|
const require = createRequire(import.meta.url);
|
|
9
9
|
const pkg = require('../../package.json');
|
|
10
10
|
import { generateId } from 'melony';
|
|
11
|
-
import {
|
|
11
|
+
import { getBaseDir, loadConfig } from '../app/config.js';
|
|
12
|
+
import { isCloudMode, getCloudIntegrationsConfig } from './cloud-mode.js';
|
|
12
13
|
import { processService } from '../services/process.js';
|
|
13
14
|
import { runAgent, STATE_AGENT_ID, ORCHESTRATOR_AGENT_ID } from '../harness/index.js';
|
|
14
15
|
import { initPlugins } from '../services/plugins/registry.js';
|
|
@@ -29,8 +30,11 @@ export async function startServer(options = {}) {
|
|
|
29
30
|
.passthrough();
|
|
30
31
|
const config = loadConfig();
|
|
31
32
|
processService.syncWorkspaceVariablesToProcessEnv();
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
if (isCloudMode()) {
|
|
34
|
+
const integrations = getCloudIntegrationsConfig();
|
|
35
|
+
console.log(`[server] Cloud mode enabled${integrations ? '' : ' (integrations proxy env not set)'}`);
|
|
36
|
+
}
|
|
37
|
+
const openBotDir = getBaseDir();
|
|
34
38
|
const PORT = Number(options.port ?? config.port ?? process.env.PORT ?? 4132);
|
|
35
39
|
const app = express();
|
|
36
40
|
const clients = new Map();
|
|
@@ -194,6 +198,21 @@ export async function startServer(options = {}) {
|
|
|
194
198
|
next();
|
|
195
199
|
});
|
|
196
200
|
app.use(cors());
|
|
201
|
+
const gatewayToken = process.env.OPENBOT_GATEWAY_TOKEN?.trim();
|
|
202
|
+
if (gatewayToken) {
|
|
203
|
+
app.use((req, res, next) => {
|
|
204
|
+
if (req.path === '/api/health' || req.method === 'OPTIONS') {
|
|
205
|
+
next();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const got = req.get('x-openbot-gateway-token');
|
|
209
|
+
if (got !== gatewayToken) {
|
|
210
|
+
res.status(401).json({ error: 'unauthorized' });
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
next();
|
|
214
|
+
});
|
|
215
|
+
}
|
|
197
216
|
const resolvePublicBaseUrl = () => getPublicBaseUrl(PORT, config.publicUrl);
|
|
198
217
|
app.use((req, res, next) => {
|
|
199
218
|
const isWorkspaceUpload = req.method === 'POST' &&
|
|
@@ -206,7 +225,7 @@ export async function startServer(options = {}) {
|
|
|
206
225
|
express.json({ limit: '20mb' })(req, res, next);
|
|
207
226
|
});
|
|
208
227
|
app.get('/api/health', (req, res) => {
|
|
209
|
-
res.json({ status: 'ok', version: pkg.version });
|
|
228
|
+
res.json({ status: 'ok', version: pkg.version, apiVersion: 1 });
|
|
210
229
|
});
|
|
211
230
|
app.get('/api/events', (req, res) => {
|
|
212
231
|
const { channelId, threadId } = getContext(req);
|
|
@@ -567,8 +586,21 @@ export async function startServer(options = {}) {
|
|
|
567
586
|
res.status(500).json({ error: 'Failed to process state request' });
|
|
568
587
|
}
|
|
569
588
|
});
|
|
570
|
-
|
|
571
|
-
|
|
589
|
+
const HOST = process.env.HOST || '0.0.0.0';
|
|
590
|
+
const server = app.listen(PORT, HOST, () => {
|
|
591
|
+
console.log(`\x1b[32mOpenBot server listening at http://${HOST}:${PORT}\x1b[0m`);
|
|
572
592
|
console.log(`🌐 Visit \x1b[96m\x1b[1mhttps://openbot.one\x1b[0m to connect to this runtime and manage everything from there. ✨`);
|
|
573
593
|
});
|
|
594
|
+
const shutdown = (signal) => {
|
|
595
|
+
console.log(`\n[server] Received ${signal}, shutting down...`);
|
|
596
|
+
server.close(() => {
|
|
597
|
+
process.exit(0);
|
|
598
|
+
});
|
|
599
|
+
setTimeout(() => {
|
|
600
|
+
console.error('[server] Forced shutdown after timeout');
|
|
601
|
+
process.exit(1);
|
|
602
|
+
}, 10000).unref();
|
|
603
|
+
};
|
|
604
|
+
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
|
605
|
+
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
574
606
|
}
|
|
@@ -14,7 +14,7 @@ export const approvalPlugin = {
|
|
|
14
14
|
description: 'Gate protected tool calls behind a UI confirmation widget.',
|
|
15
15
|
factory: ({ config, storage }) => (builder) => {
|
|
16
16
|
// Actions that require approval. Defaults to bash.
|
|
17
|
-
const actionsToApprove = config.actions || ['action:
|
|
17
|
+
const actionsToApprove = config.actions || ['action:shell_exec'];
|
|
18
18
|
for (const action of actionsToApprove) {
|
|
19
19
|
builder.intercept(action, (event, context) => {
|
|
20
20
|
// If already approved in this flow, let it pass to the actual handler
|
|
@@ -2,194 +2,416 @@ import { z } from 'zod';
|
|
|
2
2
|
import { spawn } from 'node:child_process';
|
|
3
3
|
import { randomUUID } from 'node:crypto';
|
|
4
4
|
import { resolvePath } from '../../app/config.js';
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
const DEFAULT_TIMEOUT_MS = 120000;
|
|
6
|
+
const FOREGROUND_DEV_TIMEOUT_MS = 5000;
|
|
7
|
+
const TIMEOUT_EXIT_CODE = 124;
|
|
8
|
+
const MAX_LOG_CHARS = 32000;
|
|
9
|
+
const DEFAULT_SESSION_ID = 'default';
|
|
10
|
+
const DEV_SERVER_READY_PATTERNS = [
|
|
11
|
+
/\bready in \d+/i,
|
|
12
|
+
/\blocal\s+https?:\/\//i,
|
|
13
|
+
/\blistening on\b/i,
|
|
14
|
+
/\bstarted server on\b/i,
|
|
15
|
+
/\bwatching for file changes\b/i,
|
|
16
|
+
];
|
|
17
|
+
const isBackgroundedCommand = (command) => {
|
|
18
|
+
const trimmed = command.trim();
|
|
19
|
+
return /\s&\s*$/.test(trimmed) || /\bnohup\b/.test(trimmed) || /\bdisown\b/.test(trimmed);
|
|
20
|
+
};
|
|
21
|
+
const looksLikeForegroundDevServer = (command) => {
|
|
22
|
+
const trimmed = command.trim();
|
|
23
|
+
if (isBackgroundedCommand(trimmed))
|
|
24
|
+
return false;
|
|
25
|
+
return (/\b(pnpm|npm|yarn|bun)\s+(run\s+)?dev\b/.test(trimmed) ||
|
|
26
|
+
/\b(astro|vite|next|nuxt|remix)\s+dev\b/.test(trimmed) ||
|
|
27
|
+
/\bpnpm\s+start\b/.test(trimmed) ||
|
|
28
|
+
/\bnpm\s+run\s+start\b/.test(trimmed));
|
|
29
|
+
};
|
|
30
|
+
const resolveExecTimeoutMs = (command) => looksLikeForegroundDevServer(command) ? FOREGROUND_DEV_TIMEOUT_MS : DEFAULT_TIMEOUT_MS;
|
|
31
|
+
const isDevServerReady = (output) => DEV_SERVER_READY_PATTERNS.some((pattern) => pattern.test(output));
|
|
32
|
+
const formatTimeoutOutput = (partialOutput, timeoutMs, ready) => {
|
|
33
|
+
const seconds = Math.round(timeoutMs / 1000);
|
|
34
|
+
const statusLine = ready
|
|
35
|
+
? `[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.]`
|
|
36
|
+
: `[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.]`;
|
|
37
|
+
const body = partialOutput.trim();
|
|
38
|
+
return body ? `${body}\n\n${statusLine}` : statusLine;
|
|
39
|
+
};
|
|
40
|
+
const shellToolDefinitions = {
|
|
41
|
+
shell_exec: {
|
|
42
|
+
description: '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.',
|
|
8
43
|
inputSchema: z.object({
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
.
|
|
12
|
-
|
|
13
|
-
.
|
|
44
|
+
id: z
|
|
45
|
+
.string()
|
|
46
|
+
.describe('Shell session identifier (e.g. "default", "server"). Reuse ids to keep state.'),
|
|
47
|
+
exec_dir: z
|
|
48
|
+
.string()
|
|
49
|
+
.describe('Working directory for this command (absolute path).'),
|
|
50
|
+
command: z.string().describe('Shell command to execute.'),
|
|
51
|
+
}),
|
|
52
|
+
},
|
|
53
|
+
shell_view: {
|
|
54
|
+
description: 'View recent output from a shell session. Use to poll dev-server logs after shell_exec times out or after backgrounding with `&`.',
|
|
55
|
+
inputSchema: z.object({
|
|
56
|
+
id: z.string().describe('Shell session identifier.'),
|
|
57
|
+
}),
|
|
58
|
+
},
|
|
59
|
+
shell_wait: {
|
|
60
|
+
description: '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).',
|
|
61
|
+
inputSchema: z.object({
|
|
62
|
+
id: z.string().describe('Shell session identifier.'),
|
|
63
|
+
seconds: z.number().int().min(1).max(300).describe('Seconds to wait.'),
|
|
14
64
|
}),
|
|
15
65
|
},
|
|
16
|
-
|
|
17
|
-
description: '
|
|
66
|
+
shell_write_to_process: {
|
|
67
|
+
description: 'Write input to a running process in a shell session. Use to answer interactive prompts.',
|
|
18
68
|
inputSchema: z.object({
|
|
19
|
-
|
|
69
|
+
id: z.string().describe('Shell session identifier.'),
|
|
70
|
+
input: z.string().describe('Input to send to the process.'),
|
|
71
|
+
press_enter: z
|
|
72
|
+
.boolean()
|
|
73
|
+
.describe('Whether to press Enter after the input.'),
|
|
20
74
|
}),
|
|
21
75
|
},
|
|
22
|
-
|
|
23
|
-
description: '
|
|
24
|
-
inputSchema: z.object({
|
|
76
|
+
shell_kill_process: {
|
|
77
|
+
description: 'Send interrupt to the active process in a shell session (e.g. stop a dev server).',
|
|
78
|
+
inputSchema: z.object({
|
|
79
|
+
id: z.string().describe('Shell session identifier.'),
|
|
80
|
+
}),
|
|
25
81
|
},
|
|
26
82
|
};
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
83
|
+
const shellQuote = (value) => `'${value.replace(/'/g, `'\\''`)}'`;
|
|
84
|
+
const resolveCwd = (context, execDir) => {
|
|
85
|
+
const raw = (typeof execDir === 'string' && execDir.trim()) ||
|
|
86
|
+
context.state.channelDetails?.cwd ||
|
|
87
|
+
process.cwd();
|
|
88
|
+
return resolvePath(raw);
|
|
89
|
+
};
|
|
90
|
+
const sessionKey = (channelId, id) => `${channelId}:${id}`;
|
|
91
|
+
class ShellSession {
|
|
92
|
+
constructor(channelId, id, cwd) {
|
|
93
|
+
this.channelId = channelId;
|
|
94
|
+
this.id = id;
|
|
95
|
+
this.cwd = cwd;
|
|
96
|
+
this.output = '';
|
|
97
|
+
this.process = null;
|
|
98
|
+
this.execQueue = Promise.resolve();
|
|
99
|
+
this.spawn();
|
|
100
|
+
}
|
|
101
|
+
spawn() {
|
|
102
|
+
this.process = spawn('bash', [], {
|
|
103
|
+
cwd: this.cwd,
|
|
104
|
+
env: process.env,
|
|
34
105
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
35
106
|
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
console.error(`[bash] Session error for channel ${channelId}:`, err);
|
|
45
|
-
sessions.delete(channelId);
|
|
107
|
+
this.process.stdout?.on('data', (chunk) => this.append(chunk.toString()));
|
|
108
|
+
this.process.stderr?.on('data', (chunk) => this.append(chunk.toString()));
|
|
109
|
+
this.process.on('exit', () => {
|
|
110
|
+
this.process = null;
|
|
111
|
+
this.rejectPending(new Error('Shell session exited unexpectedly'));
|
|
112
|
+
});
|
|
113
|
+
this.process.on('error', (error) => {
|
|
114
|
+
this.rejectPending(error);
|
|
46
115
|
});
|
|
47
|
-
|
|
48
|
-
|
|
116
|
+
}
|
|
117
|
+
rejectPending(error) {
|
|
118
|
+
if (!this.pending)
|
|
119
|
+
return;
|
|
120
|
+
clearTimeout(this.pending.timer);
|
|
121
|
+
this.pending.reject(error);
|
|
122
|
+
this.pending = undefined;
|
|
123
|
+
}
|
|
124
|
+
append(chunk) {
|
|
125
|
+
this.output += chunk;
|
|
126
|
+
if (this.output.length > MAX_LOG_CHARS) {
|
|
127
|
+
this.output = this.output.slice(-MAX_LOG_CHARS);
|
|
128
|
+
}
|
|
129
|
+
if (!this.pending)
|
|
130
|
+
return;
|
|
131
|
+
const tail = this.output.slice(this.pending.startLen);
|
|
132
|
+
const markerIndex = tail.indexOf(this.pending.marker);
|
|
133
|
+
if (markerIndex === -1)
|
|
134
|
+
return;
|
|
135
|
+
const afterMarker = tail.slice(markerIndex + this.pending.marker.length);
|
|
136
|
+
const match = afterMarker.match(/^:(\d+)/);
|
|
137
|
+
const exitCode = match ? Number.parseInt(match[1], 10) : 0;
|
|
138
|
+
const output = tail.slice(0, markerIndex).trimEnd();
|
|
139
|
+
clearTimeout(this.pending.timer);
|
|
140
|
+
this.pending.resolve({ exitCode, output });
|
|
141
|
+
this.pending = undefined;
|
|
142
|
+
}
|
|
143
|
+
ensureProcess() {
|
|
144
|
+
if (!this.process?.stdin) {
|
|
145
|
+
this.spawn();
|
|
146
|
+
}
|
|
147
|
+
if (!this.process?.stdin) {
|
|
148
|
+
throw new Error('Failed to start shell session');
|
|
149
|
+
}
|
|
150
|
+
return this.process;
|
|
151
|
+
}
|
|
152
|
+
enqueue(fn) {
|
|
153
|
+
const next = this.execQueue.then(fn, fn);
|
|
154
|
+
this.execQueue = next.catch(() => undefined);
|
|
155
|
+
return next;
|
|
156
|
+
}
|
|
157
|
+
view() {
|
|
158
|
+
return this.output.slice(-8000);
|
|
159
|
+
}
|
|
160
|
+
async exec(command, execDir) {
|
|
161
|
+
return this.enqueue(() => this.execInternal(command, execDir));
|
|
162
|
+
}
|
|
163
|
+
execInternal(command, execDir) {
|
|
164
|
+
const process = this.ensureProcess();
|
|
165
|
+
const marker = `__OPENBOT_${randomUUID().replace(/-/g, '')}__`;
|
|
166
|
+
const script = `cd ${shellQuote(execDir)} && ${command}; printf '\\n${marker}:%s\\n' "$?"`;
|
|
167
|
+
const timeoutMs = resolveExecTimeoutMs(command);
|
|
168
|
+
return new Promise((resolve, reject) => {
|
|
169
|
+
if (this.pending) {
|
|
170
|
+
reject(new Error('Shell session is busy'));
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const startLen = this.output.length;
|
|
174
|
+
const timer = setTimeout(() => {
|
|
175
|
+
if (!this.pending)
|
|
176
|
+
return;
|
|
177
|
+
const { startLen: pendingStartLen, timeoutMs: pendingTimeoutMs, resolve: pendingResolve } = this.pending;
|
|
178
|
+
clearTimeout(this.pending.timer);
|
|
179
|
+
this.pending = undefined;
|
|
180
|
+
const partial = this.output.slice(pendingStartLen).trimEnd();
|
|
181
|
+
const ready = isDevServerReady(partial);
|
|
182
|
+
pendingResolve({
|
|
183
|
+
exitCode: TIMEOUT_EXIT_CODE,
|
|
184
|
+
output: formatTimeoutOutput(partial, pendingTimeoutMs, ready),
|
|
185
|
+
timedOut: true,
|
|
186
|
+
stillRunning: true,
|
|
187
|
+
});
|
|
188
|
+
}, timeoutMs);
|
|
189
|
+
this.pending = { marker, startLen, timeoutMs, resolve, reject, timer };
|
|
190
|
+
try {
|
|
191
|
+
process.stdin.write(`${script}\n`);
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
clearTimeout(timer);
|
|
195
|
+
this.pending = undefined;
|
|
196
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
197
|
+
}
|
|
49
198
|
});
|
|
50
199
|
}
|
|
200
|
+
async wait(seconds) {
|
|
201
|
+
await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
|
|
202
|
+
return this.view();
|
|
203
|
+
}
|
|
204
|
+
write(input, pressEnter) {
|
|
205
|
+
const process = this.ensureProcess();
|
|
206
|
+
process.stdin.write(pressEnter ? `${input}\n` : input);
|
|
207
|
+
}
|
|
208
|
+
kill() {
|
|
209
|
+
const process = this.ensureProcess();
|
|
210
|
+
process.stdin.write('\x03');
|
|
211
|
+
}
|
|
212
|
+
destroy() {
|
|
213
|
+
this.rejectPending(new Error('Shell session closed'));
|
|
214
|
+
if (!this.process)
|
|
215
|
+
return;
|
|
216
|
+
try {
|
|
217
|
+
this.process.kill('SIGTERM');
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
// ignore
|
|
221
|
+
}
|
|
222
|
+
this.process = null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const sessions = new Map();
|
|
226
|
+
const getSession = (channelId, id, defaultCwd) => {
|
|
227
|
+
const key = sessionKey(channelId, id);
|
|
228
|
+
const existing = sessions.get(key);
|
|
229
|
+
if (existing)
|
|
230
|
+
return existing;
|
|
231
|
+
const session = new ShellSession(channelId, id, defaultCwd);
|
|
232
|
+
sessions.set(key, session);
|
|
51
233
|
return session;
|
|
52
234
|
};
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
235
|
+
const destroySessionsForChannel = (channelId) => {
|
|
236
|
+
for (const [key, session] of sessions.entries()) {
|
|
237
|
+
if (!key.startsWith(`${channelId}:`))
|
|
238
|
+
continue;
|
|
239
|
+
session.destroy();
|
|
240
|
+
sessions.delete(key);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
const formatResult = (output, extra) => ({
|
|
244
|
+
success: true,
|
|
245
|
+
output: output.trim() || '(no output)',
|
|
246
|
+
...extra,
|
|
247
|
+
});
|
|
248
|
+
const resolveShellWidgetId = (event) => event.meta?.toolCallId || randomUUID();
|
|
249
|
+
const formatShellWidgetBody = (input, output) => {
|
|
250
|
+
const inputText = JSON.stringify(input, null, 2);
|
|
251
|
+
const outputText = output.trim() || '(no output)';
|
|
252
|
+
return `Input:\n${inputText}\n\nOutput:\n${outputText}`;
|
|
253
|
+
};
|
|
254
|
+
function* emitShellWidgetPending(event, context, tool, input, widgetId) {
|
|
255
|
+
const threadId = event.meta?.threadId || context.state.threadId;
|
|
256
|
+
yield {
|
|
257
|
+
type: 'client:ui:widget',
|
|
258
|
+
data: {
|
|
259
|
+
widgetId,
|
|
260
|
+
kind: 'message',
|
|
261
|
+
title: tool,
|
|
262
|
+
body: formatShellWidgetBody(input, '(running...)'),
|
|
263
|
+
display: 'collapsed',
|
|
264
|
+
metadata: {
|
|
265
|
+
type: 'shell:tool',
|
|
266
|
+
tool,
|
|
267
|
+
input,
|
|
268
|
+
status: 'running',
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
meta: { agentId: context.state.agentId, threadId },
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function* emitShellToolResult(event, context, tool, input, result, widgetId) {
|
|
275
|
+
const threadId = event.meta?.threadId || context.state.threadId;
|
|
276
|
+
const output = String(result.output ?? '');
|
|
277
|
+
yield {
|
|
278
|
+
type: 'client:ui:widget',
|
|
279
|
+
data: {
|
|
280
|
+
widgetId,
|
|
281
|
+
kind: 'message',
|
|
282
|
+
title: tool,
|
|
283
|
+
body: formatShellWidgetBody(input, output),
|
|
284
|
+
state: result.success ? 'submitted' : 'error',
|
|
285
|
+
display: 'collapsed',
|
|
286
|
+
metadata: {
|
|
287
|
+
type: 'shell:tool',
|
|
288
|
+
tool,
|
|
289
|
+
input,
|
|
290
|
+
output,
|
|
291
|
+
success: result.success,
|
|
292
|
+
status: result.success ? 'done' : 'error',
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
meta: { agentId: context.state.agentId, threadId },
|
|
296
|
+
};
|
|
297
|
+
const { output: _output, ...resultData } = result;
|
|
298
|
+
yield {
|
|
299
|
+
type: `action:${tool}:result`,
|
|
300
|
+
data: { ...resultData, output },
|
|
301
|
+
meta: event.meta,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
async function* runShellTool(event, context, tool, input, execute) {
|
|
305
|
+
const widgetId = resolveShellWidgetId(event);
|
|
306
|
+
yield* emitShellWidgetPending(event, context, tool, input, widgetId);
|
|
307
|
+
try {
|
|
308
|
+
const result = await execute();
|
|
309
|
+
yield* emitShellToolResult(event, context, tool, input, result, widgetId);
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
const message = error instanceof Error ? error.message : 'Unknown shell error';
|
|
313
|
+
yield* emitShellToolResult(event, context, tool, input, { success: false, output: message, error: message }, widgetId);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const shellPluginRuntime = () => (builder) => {
|
|
317
|
+
builder.on('action:shell_exec', async function* (event, context) {
|
|
318
|
+
const { id, exec_dir, command } = event.data;
|
|
319
|
+
const sessionId = (id || DEFAULT_SESSION_ID).trim() || DEFAULT_SESSION_ID;
|
|
56
320
|
const channelId = context.state.channelId;
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const result = await new Promise((resolve) => {
|
|
69
|
-
let stdout = '';
|
|
70
|
-
let stderr = '';
|
|
71
|
-
let timedOut = false;
|
|
72
|
-
const sentinel = `__OPENBOT_BASH_DONE_${Math.random().toString(36).substring(7)}__`;
|
|
73
|
-
const timeoutMs = 60000; // 1 minute timeout for tool calls
|
|
74
|
-
const timer = setTimeout(() => {
|
|
75
|
-
timedOut = true;
|
|
76
|
-
// We don't kill the session on timeout, just return what we have
|
|
77
|
-
resolve({ exitCode: null, stdout, stderr, timedOut });
|
|
78
|
-
}, timeoutMs);
|
|
79
|
-
const onStdout = (data) => {
|
|
80
|
-
const str = data.toString();
|
|
81
|
-
if (str.includes(sentinel)) {
|
|
82
|
-
const parts = str.split(sentinel);
|
|
83
|
-
stdout += parts[0];
|
|
84
|
-
const exitCodeMatch = parts[1].match(/EXIT:(\d+)/);
|
|
85
|
-
const exitCode = exitCodeMatch ? parseInt(exitCodeMatch[1], 10) : 0;
|
|
86
|
-
cleanup();
|
|
87
|
-
resolve({ exitCode, stdout, stderr, timedOut: false });
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
stdout += str;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const onStderr = (data) => {
|
|
94
|
-
stderr += data.toString();
|
|
95
|
-
};
|
|
96
|
-
const cleanup = () => {
|
|
97
|
-
clearTimeout(timer);
|
|
98
|
-
session.process.stdout?.removeListener('data', onStdout);
|
|
99
|
-
session.process.stderr?.removeListener('data', onStderr);
|
|
100
|
-
};
|
|
101
|
-
session.process.stdout?.on('data', onStdout);
|
|
102
|
-
session.process.stderr?.on('data', onStderr);
|
|
103
|
-
// Execute command and then echo the sentinel with exit code
|
|
104
|
-
session.process.stdin?.write(`${command}\necho "${sentinel}EXIT:$?"\n`);
|
|
105
|
-
});
|
|
106
|
-
yield {
|
|
107
|
-
type: 'action:bash:result',
|
|
108
|
-
data: {
|
|
109
|
-
success: result.exitCode === 0 && !result.timedOut,
|
|
110
|
-
exitCode: result.exitCode,
|
|
111
|
-
stdout: result.stdout.trim(),
|
|
112
|
-
stderr: result.stderr.trim(),
|
|
113
|
-
timedOut: result.timedOut,
|
|
114
|
-
output: result.stderr.trim() ? result.stderr.trim() : result.stdout.trim(),
|
|
115
|
-
},
|
|
116
|
-
meta: event.meta,
|
|
117
|
-
};
|
|
321
|
+
const input = {
|
|
322
|
+
id: sessionId,
|
|
323
|
+
exec_dir: exec_dir ?? resolveCwd(context),
|
|
324
|
+
command: command ?? '',
|
|
325
|
+
};
|
|
326
|
+
if (!command?.trim()) {
|
|
327
|
+
yield* runShellTool(event, context, 'shell_exec', input, async () => ({
|
|
328
|
+
success: false,
|
|
329
|
+
output: 'command is required',
|
|
330
|
+
}));
|
|
331
|
+
return;
|
|
118
332
|
}
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
error: message,
|
|
130
|
-
output: message,
|
|
131
|
-
},
|
|
132
|
-
meta: event.meta,
|
|
333
|
+
yield* runShellTool(event, context, 'shell_exec', input, async () => {
|
|
334
|
+
const execDir = resolveCwd(context, exec_dir);
|
|
335
|
+
const session = getSession(channelId, sessionId, execDir);
|
|
336
|
+
const result = await session.exec(command, execDir);
|
|
337
|
+
const success = result.timedOut ? isDevServerReady(result.output) : result.exitCode === 0;
|
|
338
|
+
return {
|
|
339
|
+
success,
|
|
340
|
+
exitCode: result.exitCode,
|
|
341
|
+
output: result.output.trim() || '(no output)',
|
|
342
|
+
...(result.timedOut && { timedOut: true, stillRunning: result.stillRunning }),
|
|
133
343
|
};
|
|
134
|
-
}
|
|
344
|
+
});
|
|
135
345
|
});
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const channelId =
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
type: 'action:bash_stop:result',
|
|
146
|
-
data: { success: true, output: `Bash session for channel ${channelId} stopped.` },
|
|
147
|
-
meta: event.meta,
|
|
148
|
-
};
|
|
346
|
+
builder.on('action:shell_view', async function* (event, context) {
|
|
347
|
+
const sessionId = (event.data?.id || DEFAULT_SESSION_ID).trim();
|
|
348
|
+
const channelId = context.state.channelId;
|
|
349
|
+
const defaultCwd = resolveCwd(context);
|
|
350
|
+
const input = { id: sessionId };
|
|
351
|
+
yield* runShellTool(event, context, 'shell_view', input, async () => {
|
|
352
|
+
const session = getSession(channelId, sessionId, defaultCwd);
|
|
353
|
+
return formatResult(session.view());
|
|
354
|
+
});
|
|
149
355
|
});
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
yield {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
},
|
|
173
|
-
})),
|
|
174
|
-
},
|
|
175
|
-
meta: event.meta,
|
|
176
|
-
};
|
|
177
|
-
yield {
|
|
178
|
-
type: 'action:bash_list_sessions:result',
|
|
179
|
-
data: {
|
|
180
|
-
success: true,
|
|
181
|
-
sessions: activeSessions,
|
|
182
|
-
output: JSON.stringify(activeSessions),
|
|
183
|
-
},
|
|
184
|
-
meta: event.meta,
|
|
356
|
+
builder.on('action:shell_wait', async function* (event, context) {
|
|
357
|
+
const { id, seconds } = event.data;
|
|
358
|
+
const sessionId = (id || DEFAULT_SESSION_ID).trim();
|
|
359
|
+
const channelId = context.state.channelId;
|
|
360
|
+
const defaultCwd = resolveCwd(context);
|
|
361
|
+
const waitedSeconds = seconds ?? 5;
|
|
362
|
+
const input = { id: sessionId, seconds: waitedSeconds };
|
|
363
|
+
yield* runShellTool(event, context, 'shell_wait', input, async () => {
|
|
364
|
+
const session = getSession(channelId, sessionId, defaultCwd);
|
|
365
|
+
const output = await session.wait(waitedSeconds);
|
|
366
|
+
return formatResult(output, { waitedSeconds });
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
builder.on('action:shell_write_to_process', async function* (event, context) {
|
|
370
|
+
const { id, input: textInput, press_enter } = event.data;
|
|
371
|
+
const sessionId = (id || DEFAULT_SESSION_ID).trim();
|
|
372
|
+
const channelId = context.state.channelId;
|
|
373
|
+
const defaultCwd = resolveCwd(context);
|
|
374
|
+
const toolInput = {
|
|
375
|
+
id: sessionId,
|
|
376
|
+
input: textInput ?? '',
|
|
377
|
+
press_enter: press_enter ?? true,
|
|
185
378
|
};
|
|
379
|
+
if (!textInput?.trim()) {
|
|
380
|
+
yield* runShellTool(event, context, 'shell_write_to_process', toolInput, async () => ({
|
|
381
|
+
success: false,
|
|
382
|
+
output: 'input is required',
|
|
383
|
+
}));
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
yield* runShellTool(event, context, 'shell_write_to_process', toolInput, async () => {
|
|
387
|
+
const session = getSession(channelId, sessionId, defaultCwd);
|
|
388
|
+
session.write(textInput, toolInput.press_enter);
|
|
389
|
+
return { success: true, output: 'Input sent to shell session.' };
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
builder.on('action:shell_kill_process', async function* (event, context) {
|
|
393
|
+
const sessionId = (event.data?.id || DEFAULT_SESSION_ID).trim();
|
|
394
|
+
const channelId = context.state.channelId;
|
|
395
|
+
const defaultCwd = resolveCwd(context);
|
|
396
|
+
const input = { id: sessionId };
|
|
397
|
+
yield* runShellTool(event, context, 'shell_kill_process', input, async () => {
|
|
398
|
+
const session = getSession(channelId, sessionId, defaultCwd);
|
|
399
|
+
session.kill();
|
|
400
|
+
return { success: true, output: 'Interrupt sent to shell session.' };
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
builder.on('action:delete_channel', async function* (event) {
|
|
404
|
+
const channelId = event.data?.channelId;
|
|
405
|
+
if (channelId) {
|
|
406
|
+
destroySessionsForChannel(channelId);
|
|
407
|
+
}
|
|
186
408
|
});
|
|
187
409
|
};
|
|
188
410
|
export const bashPlugin = {
|
|
189
411
|
id: 'bash',
|
|
190
|
-
name: '
|
|
191
|
-
description: 'Stateful
|
|
192
|
-
toolDefinitions:
|
|
193
|
-
factory: () =>
|
|
412
|
+
name: 'Shell',
|
|
413
|
+
description: 'Stateful shell sessions for the channel workspace (Manus-style).',
|
|
414
|
+
toolDefinitions: shellToolDefinitions,
|
|
415
|
+
factory: () => shellPluginRuntime(),
|
|
194
416
|
};
|
|
195
417
|
export default bashPlugin;
|