openbot 0.5.4 → 0.5.6
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/plugins/service.js +5 -1
- package/dist/services/todo/types.js +1 -0
- package/docs/plugins.md +14 -12
- 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 +4 -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 +13 -12
- 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,323 +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 type { Storage } from '../../services/plugins/domain.js';
|
|
7
|
-
import { OpenBotEvent, OpenBotState } from '../../app/types.js';
|
|
8
|
-
|
|
9
|
-
const TUNNEL_URL_PATTERN = /https:\/\/[a-z0-9-]+\.trycloudflare\.com/i;
|
|
10
|
-
const TUNNEL_READY_TIMEOUT_MS = 60_000;
|
|
11
|
-
const MAX_LOG_CHARS = 8_000;
|
|
12
|
-
|
|
13
|
-
const previewToolDefinitions = {
|
|
14
|
-
expose_port: {
|
|
15
|
-
description:
|
|
16
|
-
'Expose a local dev server port via a temporary public Cloudflare quick tunnel. Returns a previewUrl stored on the channel. Dev servers must listen on 0.0.0.0 or 127.0.0.1. Call after shell_exec when the server is ready.',
|
|
17
|
-
inputSchema: z.object({
|
|
18
|
-
port: z
|
|
19
|
-
.number()
|
|
20
|
-
.int()
|
|
21
|
-
.min(1024)
|
|
22
|
-
.max(65535)
|
|
23
|
-
.describe('Local port of the running dev server (e.g. 5173).'),
|
|
24
|
-
}),
|
|
25
|
-
},
|
|
26
|
-
unexpose_port: {
|
|
27
|
-
description:
|
|
28
|
-
'Stop the active Cloudflare preview tunnel for this channel and clear previewUrl from channel state.',
|
|
29
|
-
inputSchema: z.object({}),
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
interface PreviewTunnel {
|
|
34
|
-
id: string;
|
|
35
|
-
channelId: string;
|
|
36
|
-
port: number;
|
|
37
|
-
url: string;
|
|
38
|
-
process: ChildProcess;
|
|
39
|
-
startedAt: number;
|
|
40
|
-
logs: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const tunnels = new Map<string, PreviewTunnel>();
|
|
44
|
-
const tunnelByChannel = new Map<string, string>();
|
|
45
|
-
|
|
46
|
-
const blockedPorts = (): Set<number> => {
|
|
47
|
-
const openbotPort = Number(process.env.PORT ?? 4132);
|
|
48
|
-
return new Set([22, 80, 443, openbotPort]);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const appendLog = (tunnel: PreviewTunnel, chunk: string) => {
|
|
52
|
-
tunnel.logs += chunk;
|
|
53
|
-
if (tunnel.logs.length > MAX_LOG_CHARS) {
|
|
54
|
-
tunnel.logs = tunnel.logs.slice(-MAX_LOG_CHARS);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const killTunnelProcess = (tunnel: PreviewTunnel) => {
|
|
59
|
-
const { process: child } = tunnel;
|
|
60
|
-
if (!child.pid) {
|
|
61
|
-
try {
|
|
62
|
-
child.kill();
|
|
63
|
-
} catch {
|
|
64
|
-
/* ignore */
|
|
65
|
-
}
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
child.kill('SIGTERM');
|
|
71
|
-
} catch {
|
|
72
|
-
try {
|
|
73
|
-
child.kill();
|
|
74
|
-
} catch {
|
|
75
|
-
/* ignore */
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const removeTunnel = (tunnelId: string) => {
|
|
81
|
-
const tunnel = tunnels.get(tunnelId);
|
|
82
|
-
if (!tunnel) return;
|
|
83
|
-
killTunnelProcess(tunnel);
|
|
84
|
-
tunnels.delete(tunnelId);
|
|
85
|
-
if (tunnelByChannel.get(tunnel.channelId) === tunnelId) {
|
|
86
|
-
tunnelByChannel.delete(tunnel.channelId);
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export const stopPreviewForChannel = (channelId: string) => {
|
|
91
|
-
const tunnelId = tunnelByChannel.get(channelId);
|
|
92
|
-
if (tunnelId) {
|
|
93
|
-
removeTunnel(tunnelId);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const waitForTunnelUrl = (child: ChildProcess, timeoutMs: number): Promise<string> =>
|
|
98
|
-
new Promise((resolve, reject) => {
|
|
99
|
-
let buffer = '';
|
|
100
|
-
let settled = false;
|
|
101
|
-
|
|
102
|
-
const cleanup = () => {
|
|
103
|
-
clearTimeout(timer);
|
|
104
|
-
child.stdout?.off('data', onData);
|
|
105
|
-
child.stderr?.off('data', onData);
|
|
106
|
-
child.off('exit', onExit);
|
|
107
|
-
child.off('error', onError);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const tryParse = () => {
|
|
111
|
-
const match = buffer.match(TUNNEL_URL_PATTERN);
|
|
112
|
-
if (match) {
|
|
113
|
-
settled = true;
|
|
114
|
-
cleanup();
|
|
115
|
-
resolve(match[0]);
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
const onData = (chunk: Buffer) => {
|
|
120
|
-
buffer += chunk.toString();
|
|
121
|
-
if (buffer.length > 16_000) {
|
|
122
|
-
buffer = buffer.slice(-16_000);
|
|
123
|
-
}
|
|
124
|
-
tryParse();
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
const onExit = (code: number | null) => {
|
|
128
|
-
if (settled) return;
|
|
129
|
-
settled = true;
|
|
130
|
-
cleanup();
|
|
131
|
-
reject(new Error(`cloudflared exited before providing a tunnel URL (code ${code ?? 'unknown'})`));
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const onError = (err: Error) => {
|
|
135
|
-
if (settled) return;
|
|
136
|
-
settled = true;
|
|
137
|
-
cleanup();
|
|
138
|
-
reject(err);
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const timer = setTimeout(() => {
|
|
142
|
-
if (settled) return;
|
|
143
|
-
settled = true;
|
|
144
|
-
cleanup();
|
|
145
|
-
reject(new Error('Timed out waiting for Cloudflare tunnel URL'));
|
|
146
|
-
}, timeoutMs);
|
|
147
|
-
|
|
148
|
-
child.stdout?.on('data', onData);
|
|
149
|
-
child.stderr?.on('data', onData);
|
|
150
|
-
child.on('exit', onExit);
|
|
151
|
-
child.on('error', onError);
|
|
152
|
-
tryParse();
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
const startCloudflaredTunnel = async (channelId: string, port: number): Promise<PreviewTunnel> => {
|
|
156
|
-
const child = spawn(
|
|
157
|
-
'cloudflared',
|
|
158
|
-
['tunnel', '--url', `http://127.0.0.1:${port}`, '--no-autoupdate'],
|
|
159
|
-
{
|
|
160
|
-
env: process.env,
|
|
161
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
162
|
-
},
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
const tunnel: PreviewTunnel = {
|
|
166
|
-
id: randomUUID(),
|
|
167
|
-
channelId,
|
|
168
|
-
port,
|
|
169
|
-
url: '',
|
|
170
|
-
process: child,
|
|
171
|
-
startedAt: Date.now(),
|
|
172
|
-
logs: '',
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
child.stdout?.on('data', (data: Buffer) => appendLog(tunnel, data.toString()));
|
|
176
|
-
child.stderr?.on('data', (data: Buffer) => appendLog(tunnel, data.toString()));
|
|
177
|
-
|
|
178
|
-
child.on('exit', () => {
|
|
179
|
-
tunnels.delete(tunnel.id);
|
|
180
|
-
if (tunnelByChannel.get(channelId) === tunnel.id) {
|
|
181
|
-
tunnelByChannel.delete(channelId);
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const url = await waitForTunnelUrl(child, TUNNEL_READY_TIMEOUT_MS);
|
|
186
|
-
tunnel.url = url;
|
|
187
|
-
tunnels.set(tunnel.id, tunnel);
|
|
188
|
-
tunnelByChannel.set(channelId, tunnel.id);
|
|
189
|
-
return tunnel;
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const clearPreviewChannelState = async (storage: Storage, channelId: string) => {
|
|
193
|
-
await storage.patchChannelState({
|
|
194
|
-
channelId,
|
|
195
|
-
state: {
|
|
196
|
-
previewUrl: null,
|
|
197
|
-
previewPort: null,
|
|
198
|
-
previewExposedAt: null,
|
|
199
|
-
},
|
|
200
|
-
});
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
const previewPluginRuntime =
|
|
204
|
-
(storage: Storage): MelonyPlugin<OpenBotState, OpenBotEvent> =>
|
|
205
|
-
(builder) => {
|
|
206
|
-
builder.on('action:expose_port', async function* (event, context) {
|
|
207
|
-
const channelId = context.state.channelId;
|
|
208
|
-
const port = event.data?.port;
|
|
209
|
-
|
|
210
|
-
if (!Number.isInteger(port)) {
|
|
211
|
-
yield {
|
|
212
|
-
type: 'action:expose_port:result',
|
|
213
|
-
data: {
|
|
214
|
-
success: false,
|
|
215
|
-
output: 'port must be an integer between 1024 and 65535.',
|
|
216
|
-
},
|
|
217
|
-
meta: event.meta,
|
|
218
|
-
} as OpenBotEvent;
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (blockedPorts().has(port)) {
|
|
223
|
-
yield {
|
|
224
|
-
type: 'action:expose_port:result',
|
|
225
|
-
data: {
|
|
226
|
-
success: false,
|
|
227
|
-
output: `Port ${port} is reserved and cannot be exposed.`,
|
|
228
|
-
},
|
|
229
|
-
meta: event.meta,
|
|
230
|
-
} as OpenBotEvent;
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const existingTunnelId = tunnelByChannel.get(channelId);
|
|
235
|
-
if (existingTunnelId) {
|
|
236
|
-
removeTunnel(existingTunnelId);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
try {
|
|
240
|
-
const tunnel = await startCloudflaredTunnel(channelId, port);
|
|
241
|
-
|
|
242
|
-
await storage.patchChannelState({
|
|
243
|
-
channelId,
|
|
244
|
-
state: {
|
|
245
|
-
previewUrl: tunnel.url,
|
|
246
|
-
previewPort: port,
|
|
247
|
-
previewExposedAt: tunnel.startedAt,
|
|
248
|
-
},
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
if (context.state.channelDetails) {
|
|
252
|
-
context.state.channelDetails = await storage.getChannelDetails({ channelId });
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
yield {
|
|
256
|
-
type: 'action:expose_port:result',
|
|
257
|
-
data: {
|
|
258
|
-
success: true,
|
|
259
|
-
previewUrl: tunnel.url,
|
|
260
|
-
port,
|
|
261
|
-
temporary: true,
|
|
262
|
-
output: `Preview available at ${tunnel.url} (temporary Cloudflare quick tunnel).`,
|
|
263
|
-
},
|
|
264
|
-
meta: event.meta,
|
|
265
|
-
} as OpenBotEvent;
|
|
266
|
-
} catch (error) {
|
|
267
|
-
const message =
|
|
268
|
-
error instanceof Error ? error.message : 'Failed to start Cloudflare tunnel';
|
|
269
|
-
const needsCloudflared =
|
|
270
|
-
message.includes('ENOENT') || message.toLowerCase().includes('cloudflared');
|
|
271
|
-
const hint = needsCloudflared
|
|
272
|
-
? ' Install cloudflared and ensure it is on PATH (https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/downloads/).'
|
|
273
|
-
: '';
|
|
274
|
-
|
|
275
|
-
yield {
|
|
276
|
-
type: 'action:expose_port:result',
|
|
277
|
-
data: {
|
|
278
|
-
success: false,
|
|
279
|
-
error: message,
|
|
280
|
-
output: `${message}${hint}`,
|
|
281
|
-
},
|
|
282
|
-
meta: event.meta,
|
|
283
|
-
} as OpenBotEvent;
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
builder.on('action:unexpose_port', async function* (event, context) {
|
|
288
|
-
const channelId = context.state.channelId;
|
|
289
|
-
|
|
290
|
-
stopPreviewForChannel(channelId);
|
|
291
|
-
await clearPreviewChannelState(storage, channelId);
|
|
292
|
-
|
|
293
|
-
if (context.state.channelDetails) {
|
|
294
|
-
context.state.channelDetails = await storage.getChannelDetails({ channelId });
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
yield {
|
|
298
|
-
type: 'action:unexpose_port:result',
|
|
299
|
-
data: {
|
|
300
|
-
success: true,
|
|
301
|
-
output: 'Preview tunnel stopped and previewUrl cleared from channel state.',
|
|
302
|
-
},
|
|
303
|
-
meta: event.meta,
|
|
304
|
-
} as OpenBotEvent;
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
builder.on('action:delete_channel', async function* (event) {
|
|
308
|
-
const channelId = (event.data as { channelId?: string })?.channelId;
|
|
309
|
-
if (channelId) {
|
|
310
|
-
stopPreviewForChannel(channelId);
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
export const previewPlugin: Plugin = {
|
|
316
|
-
id: 'preview',
|
|
317
|
-
name: 'Preview',
|
|
318
|
-
description: 'Temporary public preview URLs via Cloudflare quick tunnels.',
|
|
319
|
-
toolDefinitions: previewToolDefinitions,
|
|
320
|
-
factory: ({ storage }) => previewPluginRuntime(storage),
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
export default previewPlugin;
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import z from 'zod';
|
|
2
|
-
import type { Plugin } from '../../services/plugins/types.js';
|
|
3
|
-
import type { OpenBotEvent, UIWidgetListItem } from '../../app/types.js';
|
|
4
|
-
import { todoService, type TodoItem, type TodoList, type TodoStatus } from './service.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* `todo` — thread-scoped task checklist for multi-step agent work.
|
|
8
|
-
*
|
|
9
|
-
* Persisted at `~/.openbot/channels/<channelId>/threads/<threadId>/todos.json`.
|
|
10
|
-
* The agent replaces the full list via `todo_write`; the runtime injects the
|
|
11
|
-
* current list into context each turn.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** Map todo statuses onto UI list-item status label + variant. */
|
|
15
|
-
function toWidgetItemStatus(
|
|
16
|
-
status: TodoStatus,
|
|
17
|
-
): Pick<UIWidgetListItem, 'status' | 'statusVariant'> {
|
|
18
|
-
switch (status) {
|
|
19
|
-
case 'pending':
|
|
20
|
-
return { status: 'Pending', statusVariant: 'default' };
|
|
21
|
-
case 'in_progress':
|
|
22
|
-
return { status: 'In progress', statusVariant: 'info' };
|
|
23
|
-
case 'completed':
|
|
24
|
-
return { status: 'Done', statusVariant: 'success' };
|
|
25
|
-
case 'cancelled':
|
|
26
|
-
return { status: 'Cancelled', statusVariant: 'danger' };
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function formatTodoOutput(list: TodoList): string {
|
|
31
|
-
if (list.items.length === 0) return 'Todo list is empty.';
|
|
32
|
-
const lines = list.items.map((t) => `- [${t.status}] (${t.id}) ${t.content}`);
|
|
33
|
-
return `Todos (${list.items.length}):\n${lines.join('\n')}`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function todoListWidget(args: {
|
|
37
|
-
runId: string;
|
|
38
|
-
list: TodoList;
|
|
39
|
-
}): OpenBotEvent {
|
|
40
|
-
const items: UIWidgetListItem[] = args.list.items.map((t: TodoItem) => ({
|
|
41
|
-
id: t.id,
|
|
42
|
-
label: t.content,
|
|
43
|
-
...toWidgetItemStatus(t.status),
|
|
44
|
-
}));
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
type: 'client:ui:widget',
|
|
48
|
-
data: {
|
|
49
|
-
// One widget per agent run — repeated todo_write calls update in place.
|
|
50
|
-
widgetId: `todos:${args.runId}`,
|
|
51
|
-
kind: 'list',
|
|
52
|
-
title: 'Todos',
|
|
53
|
-
description:
|
|
54
|
-
args.list.items.length === 0
|
|
55
|
-
? 'No todos'
|
|
56
|
-
: `${args.list.items.filter((t) => t.status === 'completed').length}/${args.list.items.length} completed`,
|
|
57
|
-
items,
|
|
58
|
-
display: 'expanded',
|
|
59
|
-
state: 'open',
|
|
60
|
-
},
|
|
61
|
-
meta: {},
|
|
62
|
-
} as OpenBotEvent;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const todoItemSchema = z.object({
|
|
66
|
-
id: z.string().min(1).describe('Stable todo id (e.g. "1", "setup-repo").'),
|
|
67
|
-
content: z.string().min(1).describe('Short imperative description of the step.'),
|
|
68
|
-
status: z
|
|
69
|
-
.enum(['pending', 'in_progress', 'completed', 'cancelled'])
|
|
70
|
-
.describe('Todo status. At most one item may be in_progress.'),
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
const todoToolDefinitions = {
|
|
74
|
-
todo_write: {
|
|
75
|
-
description:
|
|
76
|
-
'Replace the current thread todo list with the provided items. Use for multi-step tasks: write the plan first, keep exactly one item in_progress while working, mark items completed immediately after finishing, and when done leave all items as completed (do not clear the list). Pass the full intended list each call (not a partial patch).',
|
|
77
|
-
inputSchema: z.object({
|
|
78
|
-
items: z
|
|
79
|
-
.array(todoItemSchema)
|
|
80
|
-
.max(20)
|
|
81
|
-
.describe('Full todo list for this thread (max 20). Replaces any previous list.'),
|
|
82
|
-
}),
|
|
83
|
-
},
|
|
84
|
-
todo_read: {
|
|
85
|
-
description:
|
|
86
|
-
'Read the current thread todo list. Usually unnecessary — the list is already injected into context each turn. Use only if you need an explicit refresh after an external change.',
|
|
87
|
-
inputSchema: z.object({}),
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
export const todoPlugin: Plugin = {
|
|
92
|
-
id: 'todo',
|
|
93
|
-
name: 'Todo',
|
|
94
|
-
description:
|
|
95
|
-
'Thread-scoped todo checklist for multi-step task tracking (todo_write / todo_read).',
|
|
96
|
-
toolDefinitions: todoToolDefinitions,
|
|
97
|
-
factory: () => (builder) => {
|
|
98
|
-
builder.on('action:todo_write', async function* (event, context) {
|
|
99
|
-
const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
|
|
100
|
-
try {
|
|
101
|
-
const channelId = context.state.channelId;
|
|
102
|
-
const threadId = context.state.threadId;
|
|
103
|
-
if (!channelId || !threadId) {
|
|
104
|
-
throw new Error('Missing channelId or threadId for todo_write');
|
|
105
|
-
}
|
|
106
|
-
const items = (event.data as { items: TodoItem[] }).items;
|
|
107
|
-
const list = await todoService.writeTodos({ channelId, threadId, items });
|
|
108
|
-
const runId = context.state.runId;
|
|
109
|
-
if (!runId) {
|
|
110
|
-
throw new Error('Missing runId for todo_write widget');
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const widget = todoListWidget({ runId, list });
|
|
114
|
-
widget.meta = { ...resultMeta, threadId, runId };
|
|
115
|
-
yield widget;
|
|
116
|
-
|
|
117
|
-
yield {
|
|
118
|
-
type: 'action:todo_write:result',
|
|
119
|
-
data: { success: true, list, output: formatTodoOutput(list) },
|
|
120
|
-
meta: resultMeta,
|
|
121
|
-
} as OpenBotEvent;
|
|
122
|
-
} catch (error) {
|
|
123
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
124
|
-
yield {
|
|
125
|
-
type: 'action:todo_write:result',
|
|
126
|
-
data: {
|
|
127
|
-
success: false,
|
|
128
|
-
error: message,
|
|
129
|
-
output: message,
|
|
130
|
-
},
|
|
131
|
-
meta: resultMeta,
|
|
132
|
-
} as OpenBotEvent;
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
builder.on('action:todo_read', async function* (event, context) {
|
|
137
|
-
const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
|
|
138
|
-
try {
|
|
139
|
-
const channelId = context.state.channelId;
|
|
140
|
-
const threadId = context.state.threadId;
|
|
141
|
-
if (!channelId || !threadId) {
|
|
142
|
-
throw new Error('Missing channelId or threadId for todo_read');
|
|
143
|
-
}
|
|
144
|
-
const list = await todoService.getTodos({ channelId, threadId });
|
|
145
|
-
yield {
|
|
146
|
-
type: 'action:todo_read:result',
|
|
147
|
-
data: { success: true, list, output: formatTodoOutput(list) },
|
|
148
|
-
meta: resultMeta,
|
|
149
|
-
} as OpenBotEvent;
|
|
150
|
-
} catch (error) {
|
|
151
|
-
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
152
|
-
yield {
|
|
153
|
-
type: 'action:todo_read:result',
|
|
154
|
-
data: {
|
|
155
|
-
success: false,
|
|
156
|
-
error: message,
|
|
157
|
-
output: message,
|
|
158
|
-
},
|
|
159
|
-
meta: resultMeta,
|
|
160
|
-
} as OpenBotEvent;
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
},
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
export default todoPlugin;
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { DEFAULT_CHANNELS_DIR, getBaseDir, resolvePath } from '../../app/config.js';
|
|
4
|
-
|
|
5
|
-
export type TodoStatus = 'pending' | 'in_progress' | 'completed' | 'cancelled';
|
|
6
|
-
|
|
7
|
-
export type TodoItem = {
|
|
8
|
-
id: string;
|
|
9
|
-
content: string;
|
|
10
|
-
status: TodoStatus;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type TodoList = {
|
|
14
|
-
items: TodoItem[];
|
|
15
|
-
updatedAt: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export const MAX_TODO_ITEMS = 20;
|
|
19
|
-
|
|
20
|
-
const TODO_STATUSES: ReadonlySet<string> = new Set([
|
|
21
|
-
'pending',
|
|
22
|
-
'in_progress',
|
|
23
|
-
'completed',
|
|
24
|
-
'cancelled',
|
|
25
|
-
]);
|
|
26
|
-
|
|
27
|
-
const getThreadDir = (channelId: string, threadId: string): string => {
|
|
28
|
-
const base = resolvePath(path.join(getBaseDir(), DEFAULT_CHANNELS_DIR, channelId));
|
|
29
|
-
return path.join(base, 'threads', threadId);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const getTodosPath = (channelId: string, threadId: string): string =>
|
|
33
|
-
path.join(getThreadDir(channelId, threadId), 'todos.json');
|
|
34
|
-
|
|
35
|
-
const emptyList = (): TodoList => ({
|
|
36
|
-
items: [],
|
|
37
|
-
updatedAt: new Date().toISOString(),
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Validate and normalize a todo list write. Throws on invalid input.
|
|
42
|
-
* Enforces: non-empty content, known statuses, unique ids, max size,
|
|
43
|
-
* and at most one `in_progress` item.
|
|
44
|
-
*/
|
|
45
|
-
export function validateTodoItems(items: TodoItem[]): TodoItem[] {
|
|
46
|
-
if (!Array.isArray(items)) {
|
|
47
|
-
throw new Error('todos must be an array');
|
|
48
|
-
}
|
|
49
|
-
if (items.length > MAX_TODO_ITEMS) {
|
|
50
|
-
throw new Error(`At most ${MAX_TODO_ITEMS} todos allowed`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const seen = new Set<string>();
|
|
54
|
-
let inProgressCount = 0;
|
|
55
|
-
const normalized: TodoItem[] = [];
|
|
56
|
-
|
|
57
|
-
for (const raw of items) {
|
|
58
|
-
if (!raw || typeof raw !== 'object') {
|
|
59
|
-
throw new Error('Each todo must be an object');
|
|
60
|
-
}
|
|
61
|
-
const id = typeof raw.id === 'string' ? raw.id.trim() : '';
|
|
62
|
-
const content = typeof raw.content === 'string' ? raw.content.trim() : '';
|
|
63
|
-
const status = raw.status;
|
|
64
|
-
|
|
65
|
-
if (!id) throw new Error('Each todo requires a non-empty id');
|
|
66
|
-
if (!content) throw new Error(`Todo "${id}" requires non-empty content`);
|
|
67
|
-
if (!TODO_STATUSES.has(status)) {
|
|
68
|
-
throw new Error(
|
|
69
|
-
`Todo "${id}" has invalid status "${String(status)}"; expected pending|in_progress|completed|cancelled`,
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
if (seen.has(id)) throw new Error(`Duplicate todo id "${id}"`);
|
|
73
|
-
seen.add(id);
|
|
74
|
-
|
|
75
|
-
if (status === 'in_progress') inProgressCount += 1;
|
|
76
|
-
|
|
77
|
-
normalized.push({ id, content, status: status as TodoStatus });
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (inProgressCount > 1) {
|
|
81
|
-
throw new Error('At most one todo may be in_progress');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return normalized;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export const todoService = {
|
|
88
|
-
getTodos: async (args: { channelId: string; threadId: string }): Promise<TodoList> => {
|
|
89
|
-
const filePath = getTodosPath(args.channelId, args.threadId);
|
|
90
|
-
try {
|
|
91
|
-
const raw = await fs.readFile(filePath, 'utf-8');
|
|
92
|
-
const parsed = JSON.parse(raw) as Partial<TodoList>;
|
|
93
|
-
const items = Array.isArray(parsed.items) ? (parsed.items as TodoItem[]) : [];
|
|
94
|
-
return {
|
|
95
|
-
items,
|
|
96
|
-
updatedAt:
|
|
97
|
-
typeof parsed.updatedAt === 'string' ? parsed.updatedAt : new Date().toISOString(),
|
|
98
|
-
};
|
|
99
|
-
} catch (e: unknown) {
|
|
100
|
-
if ((e as { code?: string })?.code === 'ENOENT') return emptyList();
|
|
101
|
-
throw e;
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
|
|
105
|
-
writeTodos: async (args: {
|
|
106
|
-
channelId: string;
|
|
107
|
-
threadId: string;
|
|
108
|
-
items: TodoItem[];
|
|
109
|
-
}): Promise<TodoList> => {
|
|
110
|
-
const items = validateTodoItems(args.items);
|
|
111
|
-
const list: TodoList = {
|
|
112
|
-
items,
|
|
113
|
-
updatedAt: new Date().toISOString(),
|
|
114
|
-
};
|
|
115
|
-
const threadDir = getThreadDir(args.channelId, args.threadId);
|
|
116
|
-
await fs.mkdir(threadDir, { recursive: true });
|
|
117
|
-
const filePath = getTodosPath(args.channelId, args.threadId);
|
|
118
|
-
const tmp = `${filePath}.tmp`;
|
|
119
|
-
await fs.writeFile(tmp, `${JSON.stringify(list, null, 2)}\n`, 'utf-8');
|
|
120
|
-
await fs.rename(tmp, filePath);
|
|
121
|
-
return list;
|
|
122
|
-
},
|
|
123
|
-
};
|