openbot 0.3.6 → 0.4.0

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.
Files changed (96) hide show
  1. package/README.md +15 -16
  2. package/dist/app/agent-ids.js +4 -0
  3. package/dist/app/cli.js +1 -1
  4. package/dist/app/config.js +0 -19
  5. package/dist/app/server.js +8 -14
  6. package/dist/bus/services.js +34 -124
  7. package/dist/harness/agent-invoke-run.js +44 -0
  8. package/dist/harness/agent-turn.js +99 -0
  9. package/dist/harness/channel-participants.js +40 -0
  10. package/dist/harness/constants.js +2 -0
  11. package/dist/harness/context-meter.js +97 -0
  12. package/dist/harness/context.js +95 -47
  13. package/dist/harness/dispatch.js +144 -0
  14. package/dist/harness/dispatcher.js +45 -156
  15. package/dist/harness/history.js +177 -0
  16. package/dist/harness/index.js +91 -0
  17. package/dist/harness/orchestration.js +88 -0
  18. package/dist/harness/participants.js +22 -0
  19. package/dist/harness/run-harness.js +154 -0
  20. package/dist/harness/run.js +98 -0
  21. package/dist/harness/runtime-factory.js +0 -34
  22. package/dist/harness/runtime.js +57 -0
  23. package/dist/harness/todo-dispatch.js +51 -0
  24. package/dist/harness/todos.js +5 -0
  25. package/dist/harness/turn.js +79 -0
  26. package/dist/plugins/approval/index.js +105 -149
  27. package/dist/plugins/delegation/index.js +119 -32
  28. package/dist/plugins/memory/index.js +103 -14
  29. package/dist/plugins/memory/service.js +152 -0
  30. package/dist/plugins/openbot/context.js +80 -0
  31. package/dist/plugins/openbot/history.js +98 -0
  32. package/dist/plugins/openbot/index.js +31 -0
  33. package/dist/plugins/openbot/runtime.js +317 -0
  34. package/dist/plugins/openbot/system-prompt.js +5 -0
  35. package/dist/plugins/plugin-manager/index.js +105 -0
  36. package/dist/plugins/storage/index.js +573 -0
  37. package/dist/plugins/storage/service.js +1159 -0
  38. package/dist/plugins/storage-tools/index.js +2 -2
  39. package/dist/plugins/thread-namer/index.js +72 -0
  40. package/dist/plugins/thread-naming/generate-title.js +44 -0
  41. package/dist/plugins/thread-naming/index.js +103 -0
  42. package/dist/plugins/threads/index.js +114 -0
  43. package/dist/plugins/todo/index.js +24 -25
  44. package/dist/plugins/ui/index.js +2 -32
  45. package/dist/registry/plugins.js +3 -9
  46. package/dist/services/plugins/domain.js +1 -0
  47. package/dist/services/plugins/plugin-cache.js +9 -0
  48. package/dist/services/plugins/registry.js +110 -0
  49. package/dist/services/plugins/service.js +177 -0
  50. package/dist/services/plugins/types.js +1 -0
  51. package/dist/services/process.js +29 -0
  52. package/dist/services/storage.js +11 -10
  53. package/dist/services/thread-naming.js +81 -0
  54. package/docs/agents.md +16 -10
  55. package/docs/architecture.md +2 -2
  56. package/docs/plugins.md +6 -15
  57. package/docs/templates/AGENT.example.md +7 -13
  58. package/package.json +1 -2
  59. package/src/app/agent-ids.ts +5 -0
  60. package/src/app/cli.ts +1 -1
  61. package/src/app/config.ts +1 -31
  62. package/src/app/server.ts +8 -16
  63. package/src/app/types.ts +63 -189
  64. package/src/harness/index.ts +145 -0
  65. package/src/plugins/approval/index.ts +91 -189
  66. package/src/plugins/delegation/index.ts +136 -39
  67. package/src/plugins/memory/index.ts +112 -15
  68. package/src/{services/memory.ts → plugins/memory/service.ts} +1 -1
  69. package/src/plugins/openbot/context.ts +91 -0
  70. package/src/plugins/openbot/history.ts +107 -0
  71. package/src/plugins/openbot/index.ts +37 -0
  72. package/src/plugins/openbot/runtime.ts +384 -0
  73. package/src/plugins/openbot/system-prompt.ts +7 -0
  74. package/src/plugins/plugin-manager/index.ts +122 -0
  75. package/src/plugins/shell/index.ts +1 -1
  76. package/src/plugins/storage/index.ts +633 -0
  77. package/src/{services/storage.ts → plugins/storage/service.ts} +224 -67
  78. package/src/{bus/types.ts → services/plugins/domain.ts} +16 -7
  79. package/src/services/plugins/plugin-cache.ts +13 -0
  80. package/src/{registry/plugins.ts → services/plugins/registry.ts} +25 -27
  81. package/src/services/{plugins.ts → plugins/service.ts} +96 -2
  82. package/src/{bus/plugin.ts → services/plugins/types.ts} +3 -3
  83. package/src/bus/services.ts +0 -954
  84. package/src/harness/context.ts +0 -365
  85. package/src/harness/dispatcher.ts +0 -379
  86. package/src/harness/mcp.ts +0 -78
  87. package/src/harness/runtime-factory.ts +0 -129
  88. package/src/harness/todo-advance.ts +0 -128
  89. package/src/plugins/ai-sdk/index.ts +0 -41
  90. package/src/plugins/ai-sdk/runtime.ts +0 -468
  91. package/src/plugins/ai-sdk/system-prompt.ts +0 -18
  92. package/src/plugins/mcp/index.ts +0 -128
  93. package/src/plugins/storage-tools/index.ts +0 -90
  94. package/src/plugins/todo/index.ts +0 -64
  95. package/src/plugins/ui/index.ts +0 -227
  96. /package/src/{harness → services}/process.ts +0 -0
@@ -1,954 +0,0 @@
1
- import { MelonyPlugin } from 'melony';
2
- import { DEFAULT_MARKETPLACE_REGISTRY_URL, loadConfig } from '../app/config.js';
3
- import {
4
- OpenBotEvent,
5
- OpenBotState,
6
- MemoryScopeAlias,
7
- TodoItem,
8
- TodoStatus,
9
- TodoWriteInput,
10
- } from '../app/types.js';
11
- import type { PluginRef } from './plugin.js';
12
- import { Storage } from './types.js';
13
- import { storageService } from '../services/storage.js';
14
- import { pluginService } from '../services/plugins.js';
15
-
16
- const readTodos = (state: OpenBotState): TodoItem[] => {
17
- const raw = (state.threadDetails?.state as Record<string, unknown> | undefined)?.todos;
18
- return Array.isArray(raw) ? (raw as TodoItem[]) : [];
19
- };
20
-
21
- let todoCounter = 0;
22
- const newTodoId = (now: number, idx: number): string =>
23
- `todo_${now.toString(36)}_${(todoCounter++).toString(36)}_${idx}`;
24
-
25
- async function persistTodos(
26
- storage: Storage,
27
- state: OpenBotState,
28
- todos: TodoItem[],
29
- ): Promise<void> {
30
- if (!state.threadId) throw new Error('No active thread');
31
- await storage.patchThreadState({
32
- channelId: state.channelId,
33
- threadId: state.threadId,
34
- state: { todos },
35
- });
36
- state.threadDetails = await storage.getThreadDetails({
37
- channelId: state.channelId,
38
- threadId: state.threadId,
39
- });
40
- }
41
-
42
-
43
- /**
44
- * Resolve a scope alias to a concrete scope string. Aliases let tools accept
45
- * `agent`/`channel`/`global` without knowing the active ids; the bus rewrites
46
- * them using `context.state`.
47
- */
48
- function resolveMemoryScope(
49
- alias: MemoryScopeAlias | undefined,
50
- state: OpenBotState,
51
- ): string {
52
- switch (alias) {
53
- case 'agent':
54
- return `agent:${state.agentId}`;
55
- case 'channel':
56
- return `channel:${state.channelId}`;
57
- case 'global':
58
- case undefined:
59
- return 'global';
60
- default:
61
- return 'global';
62
- }
63
- }
64
-
65
- function resolveMemoryScopeFilter(
66
- alias: MemoryScopeAlias | 'all' | undefined,
67
- state: OpenBotState,
68
- ): string[] | undefined {
69
- if (alias === 'all' || alias === undefined) {
70
- return ['global', `agent:${state.agentId}`, `channel:${state.channelId}`];
71
- }
72
- return [resolveMemoryScope(alias, state)];
73
- }
74
-
75
- /** One marketplace entry; matches `action:marketplace:list:result` agent shape. */
76
- export type MarketplaceAgentListing = {
77
- id: string;
78
- name: string;
79
- description: string;
80
- image?: string;
81
- instructions: string;
82
- plugins: PluginRef[];
83
- };
84
-
85
- const DEFAULT_MARKETPLACE_AGENTS: MarketplaceAgentListing[] = [
86
- {
87
- id: 'researcher',
88
- name: 'Researcher',
89
- description: 'Specialized in web research and information synthesis.',
90
- instructions:
91
- 'You are a research assistant. Use available tools to find information.',
92
- plugins: [
93
- { id: 'ai-sdk', config: { model: 'openai/gpt-4o' } },
94
- { id: 'mcp' },
95
- { id: 'shell' },
96
- ],
97
- },
98
- {
99
- id: 'coder',
100
- name: 'Coder',
101
- description: 'Expert in multiple programming languages and software architecture.',
102
- instructions: 'You are an expert software engineer. Help the user with coding tasks.',
103
- plugins: [{ id: 'claude-code' }],
104
- },
105
- ];
106
-
107
- function isRecord(value: unknown): value is Record<string, unknown> {
108
- return typeof value === 'object' && value !== null && !Array.isArray(value);
109
- }
110
-
111
- /**
112
- * Parses JSON from a remote registry file. Supports either
113
- * `{ "agents": [ ... ] }` or a top-level array.
114
- */
115
- export function parseMarketplaceRegistryJson(data: unknown): MarketplaceAgentListing[] {
116
- const rawAgents: unknown =
117
- Array.isArray(data) ? data : isRecord(data) && Array.isArray(data.agents) ? data.agents : null;
118
- if (!Array.isArray(rawAgents)) {
119
- throw new Error('Registry JSON must be an array or an object with an "agents" array');
120
- }
121
- return rawAgents.map((item, i) => {
122
- if (!isRecord(item)) {
123
- throw new Error(`agents[${i}]: expected object`);
124
- }
125
- const id = item.id;
126
- const name = item.name;
127
- const description = item.description;
128
- const instructions = item.instructions;
129
- const pluginsRaw = item.plugins;
130
- if (typeof id !== 'string' || !id) throw new Error(`agents[${i}].id must be a non-empty string`);
131
- if (typeof name !== 'string') throw new Error(`agents[${i}].name must be a string`);
132
- if (typeof description !== 'string') throw new Error(`agents[${i}].description must be a string`);
133
- if (typeof instructions !== 'string') {
134
- throw new Error(`agents[${i}].instructions must be a string`);
135
- }
136
- if (!Array.isArray(pluginsRaw)) throw new Error(`agents[${i}].plugins must be an array`);
137
- const plugins: PluginRef[] = pluginsRaw.map((p, j) => {
138
- if (!isRecord(p) || typeof p.id !== 'string' || !p.id) {
139
- throw new Error(`agents[${i}].plugins[${j}]: expected { "id": string, "config"?: object }`);
140
- }
141
- const ref: PluginRef = { id: p.id };
142
- if (p.config !== undefined) {
143
- if (!isRecord(p.config)) throw new Error(`agents[${i}].plugins[${j}].config must be an object`);
144
- ref.config = p.config;
145
- }
146
- return ref;
147
- });
148
- const listing: MarketplaceAgentListing = { id, name, description, instructions, plugins };
149
- if (item.image !== undefined) {
150
- if (typeof item.image !== 'string') throw new Error(`agents[${i}].image must be a string`);
151
- listing.image = item.image;
152
- }
153
- return listing;
154
- });
155
- }
156
-
157
- async function fetchMarketplaceAgentsFromUrl(url: string): Promise<MarketplaceAgentListing[]> {
158
- const res = await fetch(url, {
159
- headers: { Accept: 'application/json' },
160
- signal: AbortSignal.timeout(15_000),
161
- });
162
- if (!res.ok) {
163
- throw new Error(`Registry HTTP ${res.status} ${res.statusText}`);
164
- }
165
- const json: unknown = await res.json();
166
- return parseMarketplaceRegistryJson(json);
167
- }
168
-
169
- /**
170
- * Bus-level service plugin.
171
- *
172
- * This handler bundle is registered once on every agent run and exposes the
173
- * platform's shared services (storage CRUD, channel/thread management, agent
174
- * registry, agent-package install/marketplace) over the event bus.
175
- *
176
- * Any agent (first-party OpenBot or community-built) can call into these
177
- * services purely by emitting `action:*` events with no per-agent wiring.
178
- */
179
- export interface BusServicesOptions {
180
- storage: Storage;
181
- }
182
-
183
- export const busServicesPlugin =
184
- (options: BusServicesOptions): MelonyPlugin<OpenBotState, OpenBotEvent> =>
185
- (builder) => {
186
- const { storage } = options;
187
-
188
- builder.on('action:create_thread', async function* (event, context) {
189
- const threadId = event.meta?.threadId;
190
- const channelId = context.state.channelId;
191
- const { threadTitle, initialState } = (event as any).data;
192
-
193
- if (!threadId) {
194
- console.warn('[bus] Cannot create thread: meta.threadId is missing');
195
- return;
196
- }
197
-
198
- context.state.threadId = threadId;
199
-
200
- if (channelId) {
201
- try {
202
- await storage.createThread({
203
- channelId,
204
- threadId,
205
- threadTitle,
206
- initialState: (initialState as Record<string, unknown>) || {},
207
- });
208
-
209
- context.state.threadDetails = await storage.getThreadDetails({
210
- channelId,
211
- threadId,
212
- });
213
- } catch (error) {
214
- console.warn(
215
- `[bus] Failed to initialize thread for channel ${channelId} thread ${threadId}`,
216
- error,
217
- );
218
- }
219
- }
220
-
221
- yield {
222
- type: 'action:create_thread:result',
223
- data: { success: true, threadId, threadTitle },
224
- meta: { ...(event.meta || {}), threadId, agentId: context.state.agentId },
225
- } as OpenBotEvent;
226
- });
227
-
228
- builder.on('action:create_channel', async function* (event, context) {
229
- const { channelId, spec, initialState, cwd, participants } = (event as any).data;
230
- const rawChannelId = (channelId || '').trim();
231
- const channelSpec = typeof spec === 'string' ? spec : '';
232
-
233
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
234
-
235
- if (!rawChannelId) {
236
- yield {
237
- type: 'action:create_channel:result',
238
- data: { success: false, channelId: '', channelUrl: '' },
239
- meta: resultMeta,
240
- } as OpenBotEvent;
241
- return;
242
- }
243
-
244
- const channelUrl = `/channels/${rawChannelId}`;
245
-
246
- const mergedInitial: Record<string, unknown> = { ...(initialState || {}) };
247
- if (participants !== undefined) {
248
- const normalized = Array.isArray(participants)
249
- ? participants
250
- .filter((x: unknown): x is string => typeof x === 'string')
251
- .map((s: string) => s.trim())
252
- .filter(Boolean)
253
- : [];
254
- mergedInitial.participants = normalized;
255
- }
256
-
257
- try {
258
- await storage.createChannel({
259
- channelId: rawChannelId,
260
- spec: channelSpec,
261
- initialState: mergedInitial,
262
- cwd,
263
- });
264
-
265
- yield {
266
- type: 'action:create_channel:result',
267
- data: { success: true, channelId: rawChannelId, channelUrl },
268
- meta: resultMeta,
269
- } as OpenBotEvent;
270
-
271
- yield {
272
- type: 'agent:output',
273
- data: { content: `Created channel \`${rawChannelId}\`.` },
274
- meta: resultMeta,
275
- } as OpenBotEvent;
276
- } catch {
277
- yield {
278
- type: 'action:create_channel:result',
279
- data: { success: false, channelId: rawChannelId, channelUrl },
280
- meta: resultMeta,
281
- } as OpenBotEvent;
282
- }
283
- });
284
-
285
- builder.on('action:update_channel', async function* (event, context) {
286
- const data = (event.data || {}) as {
287
- channelId?: string;
288
- name?: string;
289
- cwd?: string;
290
- participants?: string[];
291
- };
292
- const targetChannelId = (data.channelId || context.state.channelId || '').trim();
293
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
294
-
295
- if (!targetChannelId) {
296
- yield {
297
- type: 'action:update_channel:result',
298
- data: { success: false, channelId: '', updatedFields: [] as string[] },
299
- meta: resultMeta,
300
- } as OpenBotEvent;
301
- return;
302
- }
303
-
304
- const patch: Record<string, unknown> = {};
305
- const updatedFields: string[] = [];
306
-
307
- if (typeof data.name === 'string' && data.name.trim()) {
308
- patch.name = data.name.trim();
309
- updatedFields.push('name');
310
- }
311
- if (typeof data.cwd === 'string' && data.cwd.trim()) {
312
- patch.cwd = data.cwd.trim();
313
- updatedFields.push('cwd');
314
- }
315
- if (data.participants !== undefined) {
316
- if (Array.isArray(data.participants)) {
317
- patch.participants = data.participants
318
- .filter((x): x is string => typeof x === 'string')
319
- .map((s) => s.trim())
320
- .filter(Boolean);
321
- } else {
322
- patch.participants = [];
323
- }
324
- updatedFields.push('participants');
325
- }
326
-
327
- try {
328
- if (updatedFields.length > 0) {
329
- await storage.patchChannelState({ channelId: targetChannelId, state: patch });
330
- }
331
-
332
- if (targetChannelId === context.state.channelId) {
333
- context.state.channelDetails = await storage.getChannelDetails({
334
- channelId: context.state.channelId,
335
- });
336
- }
337
-
338
- yield {
339
- type: 'action:update_channel:result',
340
- data: { success: true, channelId: targetChannelId, updatedFields },
341
- meta: resultMeta,
342
- } as OpenBotEvent;
343
- } catch {
344
- yield {
345
- type: 'action:update_channel:result',
346
- data: { success: false, channelId: targetChannelId, updatedFields },
347
- meta: resultMeta,
348
- } as OpenBotEvent;
349
- }
350
- });
351
-
352
- builder.on('action:patch_channel_details', async function* (event, context) {
353
- const updatedFields: ('state' | 'spec' | 'cwd' | 'participants')[] = [];
354
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
355
- const data = (event.data || {}) as {
356
- state?: Record<string, unknown>;
357
- spec?: string;
358
- cwd?: string;
359
- participants?: string[];
360
- };
361
- try {
362
- if (data.state !== undefined) {
363
- await storage.patchChannelState({
364
- channelId: context.state.channelId,
365
- state: data.state,
366
- });
367
- updatedFields.push('state');
368
- }
369
- if (typeof data.spec === 'string') {
370
- await storage.patchChannelSpec({
371
- channelId: context.state.channelId,
372
- spec: data.spec,
373
- });
374
- updatedFields.push('spec');
375
- }
376
- if (typeof data.cwd === 'string') {
377
- await storage.patchChannelState({
378
- channelId: context.state.channelId,
379
- state: { cwd: data.cwd },
380
- });
381
- updatedFields.push('cwd');
382
- }
383
- if (data.participants !== undefined) {
384
- const normalized = Array.isArray(data.participants)
385
- ? data.participants
386
- .filter((x): x is string => typeof x === 'string')
387
- .map((s) => s.trim())
388
- .filter(Boolean)
389
- : [];
390
- await storage.patchChannelState({
391
- channelId: context.state.channelId,
392
- state: { participants: normalized },
393
- });
394
- updatedFields.push('participants');
395
- }
396
-
397
- context.state.channelDetails = await storage.getChannelDetails({
398
- channelId: context.state.channelId,
399
- });
400
-
401
- yield {
402
- type: 'action:patch_channel_details:result',
403
- data: { success: true, updatedFields },
404
- meta: resultMeta,
405
- };
406
- } catch {
407
- yield {
408
- type: 'action:patch_channel_details:result',
409
- data: { success: false, updatedFields },
410
- meta: resultMeta,
411
- };
412
- }
413
- });
414
-
415
- builder.on('action:patch_thread_details', async function* (event, context) {
416
- const updatedFields: ('state')[] = [];
417
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
418
- try {
419
- if (!context.state.threadId) {
420
- throw new Error('Missing threadId in state for patch_thread_details');
421
- }
422
- if ((event.data as any).state !== undefined) {
423
- await storage.patchThreadState({
424
- channelId: context.state.channelId,
425
- threadId: context.state.threadId,
426
- state: (event.data as any).state,
427
- });
428
- updatedFields.push('state');
429
- }
430
-
431
- context.state.threadDetails = await storage.getThreadDetails({
432
- channelId: context.state.channelId,
433
- threadId: context.state.threadId,
434
- });
435
-
436
- yield {
437
- type: 'action:patch_thread_details:result',
438
- data: { success: true, updatedFields },
439
- meta: resultMeta,
440
- };
441
- } catch {
442
- yield {
443
- type: 'action:patch_thread_details:result',
444
- data: { success: false, updatedFields },
445
- meta: resultMeta,
446
- };
447
- }
448
- });
449
-
450
- builder.on('action:todo_write', async function* (event, context) {
451
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
452
- try {
453
- if (!context.state.threadId) {
454
- throw new Error('todo_write requires an active thread');
455
- }
456
- const existing = readTodos(context.state);
457
- const byId = new Map(existing.map((t) => [t.id, t]));
458
- const now = Date.now();
459
- const author = context.state.agentId || 'system';
460
-
461
- const inputs = (event.data as { todos: TodoWriteInput[] }).todos || [];
462
- const next: TodoItem[] = inputs.map((raw, idx) => {
463
- const prior = raw.id ? byId.get(raw.id) : undefined;
464
- return {
465
- id: prior?.id || raw.id || newTodoId(now, idx),
466
- content: raw.content,
467
- status: raw.status || prior?.status || 'pending',
468
- assignee: raw.assignee ?? prior?.assignee,
469
- createdBy: prior?.createdBy || author,
470
- createdAt: prior?.createdAt || now,
471
- updatedAt: now,
472
- ...(prior?.result !== undefined ? { result: prior.result } : {}),
473
- };
474
- });
475
-
476
- await persistTodos(storage, context.state, next);
477
-
478
- yield {
479
- type: 'action:todo_write:result',
480
- data: { success: true, todos: next },
481
- meta: resultMeta,
482
- } as OpenBotEvent;
483
- } catch (error) {
484
- yield {
485
- type: 'action:todo_write:result',
486
- data: {
487
- success: false,
488
- todos: readTodos(context.state),
489
- error: error instanceof Error ? error.message : 'Unknown error',
490
- },
491
- meta: resultMeta,
492
- } as OpenBotEvent;
493
- }
494
- });
495
-
496
- builder.on('action:todo_update', async function* (event, context) {
497
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
498
- const patch = event.data as {
499
- id: string;
500
- status?: TodoStatus;
501
- content?: string;
502
- assignee?: string;
503
- };
504
- try {
505
- if (!context.state.threadId) {
506
- throw new Error('todo_update requires an active thread');
507
- }
508
- const existing = readTodos(context.state);
509
- const idx = existing.findIndex((t) => t.id === patch.id);
510
- if (idx === -1) {
511
- throw new Error(`Todo "${patch.id}" not found`);
512
- }
513
- const now = Date.now();
514
- const updated: TodoItem = {
515
- ...existing[idx],
516
- ...(patch.content !== undefined ? { content: patch.content } : {}),
517
- ...(patch.status !== undefined ? { status: patch.status } : {}),
518
- ...(patch.assignee !== undefined
519
- ? { assignee: patch.assignee === '' ? undefined : patch.assignee }
520
- : {}),
521
- updatedAt: now,
522
- };
523
- const next = [...existing];
524
- next[idx] = updated;
525
-
526
- await persistTodos(storage, context.state, next);
527
-
528
- yield {
529
- type: 'action:todo_update:result',
530
- data: { success: true, todo: updated, todos: next },
531
- meta: resultMeta,
532
- } as OpenBotEvent;
533
- } catch (error) {
534
- yield {
535
- type: 'action:todo_update:result',
536
- data: {
537
- success: false,
538
- todos: readTodos(context.state),
539
- error: error instanceof Error ? error.message : 'Unknown error',
540
- },
541
- meta: resultMeta,
542
- } as OpenBotEvent;
543
- }
544
- });
545
-
546
- builder.on('action:storage:get-channels', async function* () {
547
- const channels = await storage.getChannels();
548
- yield { type: 'action:storage:get-channels-result', data: { channels } };
549
- });
550
-
551
- builder.on('action:storage:get-threads', async function* (event) {
552
- const threads = await storage.getThreads({ channelId: event.data.channelId });
553
- yield { type: 'action:storage:get-threads-result', data: { threads } };
554
- });
555
-
556
- builder.on('action:storage:get-channel-details', async function* (_, state) {
557
- const channelDetails = await storage.getChannelDetails({
558
- channelId: state.state.channelId,
559
- });
560
- yield { type: 'action:storage:get-channel-details-result', data: { channelDetails } };
561
- });
562
-
563
- builder.on('action:storage:get-thread-details', async function* (_, state) {
564
- const threadId = state.state.threadId;
565
- const threadDetails = threadId
566
- ? await storage.getThreadDetails({ channelId: state.state.channelId, threadId })
567
- : null;
568
- yield { type: 'action:storage:get-thread-details-result', data: { threadDetails } };
569
- });
570
-
571
- builder.on('action:storage:get-agents', async function* () {
572
- const agents = await storage.getAgents();
573
- yield { type: 'action:storage:get-agents-result', data: { agents } };
574
- });
575
-
576
- builder.on('action:storage:get-plugins', async function* () {
577
- const plugins = await storage.getPlugins();
578
- yield { type: 'action:storage:get-plugins-result', data: { plugins } };
579
- });
580
-
581
- builder.on('action:storage:get-agent-details', async function* (event) {
582
- try {
583
- const agentDetails = await storage.getAgentDetails({ agentId: event.data.agentId });
584
- yield { type: 'action:storage:get-agent-details-result', data: { agentDetails } };
585
- } catch (error) {
586
- console.error(`[bus] Failed to get agent details for ${event.data.agentId}`, error);
587
- yield {
588
- type: 'action:storage:get-agent-details-result',
589
- data: {
590
- agentDetails: null as any,
591
- error: error instanceof Error ? error.message : 'Unknown error',
592
- },
593
- };
594
- }
595
- });
596
-
597
- builder.on('action:storage:create-agent', async function* (event) {
598
- try {
599
- const { agentId, name, description, image, instructions, plugins } = event.data;
600
- await storage.createAgent({ agentId, name, description, image, instructions, plugins });
601
- yield { type: 'action:storage:create-agent-result', data: { success: true } };
602
- } catch (error) {
603
- yield {
604
- type: 'action:storage:create-agent-result',
605
- data: {
606
- success: false,
607
- error: error instanceof Error ? error.message : 'Unknown error',
608
- },
609
- };
610
- }
611
- });
612
-
613
- builder.on('action:storage:update-agent', async function* (event) {
614
- try {
615
- const { agentId, name, description, image, instructions, plugins } = event.data;
616
- await storage.updateAgent({ agentId, name, description, image, instructions, plugins });
617
- yield { type: 'action:storage:update-agent-result', data: { success: true } };
618
- } catch (error) {
619
- yield {
620
- type: 'action:storage:update-agent-result',
621
- data: {
622
- success: false,
623
- error: error instanceof Error ? error.message : 'Unknown error',
624
- },
625
- };
626
- }
627
- });
628
-
629
- builder.on('action:storage:delete-agent', async function* (event) {
630
- try {
631
- await storage.deleteAgent({ agentId: event.data.agentId });
632
- yield { type: 'action:storage:delete-agent-result', data: { success: true } };
633
- } catch (error) {
634
- yield {
635
- type: 'action:storage:delete-agent-result',
636
- data: {
637
- success: false,
638
- error: error instanceof Error ? error.message : 'Unknown error',
639
- },
640
- };
641
- }
642
- });
643
-
644
- builder.on('action:storage:get-events', async function* (_, state) {
645
- const events = await storage.getEvents(state.state);
646
- if (!state.state.threadId && events.length > 0) {
647
- const lastId = events[events.length - 1]?.id;
648
- if (lastId) {
649
- await storageService.setLastReadForChannel({
650
- channelId: state.state.channelId,
651
- lastReadEventId: lastId,
652
- });
653
- }
654
- }
655
- yield { type: 'action:storage:get-events-result', data: { events } };
656
- });
657
-
658
- builder.on('action:storage:get-variables', async function* () {
659
- const variables = await storage.getVariables();
660
- const masked: Record<string, string> = {};
661
- for (const [key, val] of Object.entries(variables)) {
662
- if (typeof val === 'object' && val !== null && val.secret) {
663
- masked[key] = '********';
664
- } else {
665
- masked[key] = typeof val === 'string' ? val : val.value;
666
- }
667
- }
668
- yield {
669
- type: 'action:storage:get-variables-result',
670
- data: { variables: masked },
671
- } as OpenBotEvent;
672
- });
673
-
674
- builder.on('action:storage:create-variable', async function* (event) {
675
- try {
676
- const { key, value, secret } = event.data;
677
- await storage.createVariable({ key, value, secret });
678
- yield { type: 'action:storage:create-variable-result', data: { success: true } };
679
- } catch (error) {
680
- yield {
681
- type: 'action:storage:create-variable-result',
682
- data: {
683
- success: false,
684
- error: error instanceof Error ? error.message : 'Unknown error',
685
- },
686
- };
687
- }
688
- });
689
-
690
- builder.on('action:storage:delete-variable', async function* (event) {
691
- try {
692
- await storage.deleteVariable({ key: event.data.key });
693
- yield { type: 'action:storage:delete-variable-result', data: { success: true } };
694
- } catch (error) {
695
- yield {
696
- type: 'action:storage:delete-variable-result',
697
- data: {
698
- success: false,
699
- error: error instanceof Error ? error.message : 'Unknown error',
700
- },
701
- };
702
- }
703
- });
704
-
705
- builder.on('action:storage:patch-channel-state', async function* (event, state) {
706
- try {
707
- await storage.patchChannelState({
708
- channelId: state.state.channelId,
709
- state: event.data.state,
710
- });
711
- yield { type: 'action:storage:patch-channel-state-result', data: { success: true } };
712
- } catch {
713
- yield { type: 'action:storage:patch-channel-state-result', data: { success: false } };
714
- }
715
- });
716
-
717
- builder.on('action:storage:patch-thread-state', async function* (event, state) {
718
- try {
719
- if (!state.state.threadId) {
720
- throw new Error('Missing threadId in state for patch-thread-state');
721
- }
722
- await storage.patchThreadState({
723
- channelId: state.state.channelId,
724
- threadId: state.state.threadId,
725
- state: event.data.state,
726
- });
727
- yield { type: 'action:storage:patch-thread-state-result', data: { success: true } };
728
- } catch {
729
- yield { type: 'action:storage:patch-thread-state-result', data: { success: false } };
730
- }
731
- });
732
-
733
- builder.on('action:storage:list-files', async function* (event, context) {
734
- const channelId = context.state.channelId;
735
- const subPath = (event.data as any)?.path || '';
736
- try {
737
- const files = await storage.listFiles({ channelId, path: subPath });
738
- yield {
739
- type: 'action:storage:list-files:result',
740
- data: { success: true, files },
741
- };
742
- } catch (error) {
743
- yield {
744
- type: 'action:storage:list-files:result',
745
- data: {
746
- success: false,
747
- files: [],
748
- error: error instanceof Error ? error.message : 'Unknown error',
749
- },
750
- };
751
- }
752
- });
753
-
754
- builder.on('action:storage:read-file', async function* (event, context) {
755
- const channelId = context.state.channelId;
756
- const filePath = (event.data as any)?.path;
757
- if (!filePath) {
758
- yield {
759
- type: 'action:storage:read-file:result',
760
- data: { success: false, path: '', error: 'Path is required' },
761
- };
762
- return;
763
- }
764
- try {
765
- const content = await storage.readFile({ channelId, path: filePath });
766
- yield {
767
- type: 'action:storage:read-file:result',
768
- data: { success: true, content, path: filePath },
769
- };
770
- } catch (error) {
771
- yield {
772
- type: 'action:storage:read-file:result',
773
- data: {
774
- success: false,
775
- path: filePath,
776
- error: error instanceof Error ? error.message : 'Unknown error',
777
- },
778
- };
779
- }
780
- });
781
-
782
- builder.on('action:plugin:install', async function* (event) {
783
- try {
784
- const { name, version } = event.data;
785
- const result = await pluginService.install({ packageName: name, version });
786
- yield {
787
- type: 'action:plugin:install:result',
788
- data: { success: true, plugin: result },
789
- } as OpenBotEvent;
790
- } catch (error) {
791
- yield {
792
- type: 'action:plugin:install:result',
793
- data: { success: false, error: (error as Error).message },
794
- } as OpenBotEvent;
795
- }
796
- });
797
-
798
- builder.on('action:plugin:uninstall', async function* (event) {
799
- try {
800
- await pluginService.uninstall(event.data.id);
801
- yield { type: 'action:plugin:uninstall:result', data: { success: true } };
802
- } catch (error) {
803
- yield {
804
- type: 'action:plugin:uninstall:result',
805
- data: { success: false, error: (error as Error).message },
806
- };
807
- }
808
- });
809
-
810
- builder.on('action:marketplace:list', async function* () {
811
- const { marketplaceRegistryUrl } = loadConfig();
812
- const registryUrl =
813
- marketplaceRegistryUrl?.trim() || DEFAULT_MARKETPLACE_REGISTRY_URL;
814
- let agents = DEFAULT_MARKETPLACE_AGENTS;
815
- try {
816
- agents = await fetchMarketplaceAgentsFromUrl(registryUrl);
817
- } catch (err) {
818
- console.warn(
819
- `[bus] marketplace registry fetch failed (${registryUrl}), using built-in list:`,
820
- err instanceof Error ? err.message : err,
821
- );
822
- }
823
- yield {
824
- type: 'action:marketplace:list:result',
825
- data: { success: true, agents },
826
- } as OpenBotEvent;
827
- });
828
-
829
- builder.on('action:remember', async function* (event, context) {
830
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
831
- try {
832
- const { content, scope, tags } = event.data;
833
- const record = await storage.appendMemory({
834
- scope: resolveMemoryScope(scope, context.state),
835
- content,
836
- tags,
837
- });
838
- yield {
839
- type: 'action:remember:result',
840
- data: { success: true, record },
841
- meta: resultMeta,
842
- } as OpenBotEvent;
843
- } catch (error) {
844
- yield {
845
- type: 'action:remember:result',
846
- data: {
847
- success: false,
848
- error: error instanceof Error ? error.message : 'Unknown error',
849
- },
850
- meta: resultMeta,
851
- } as OpenBotEvent;
852
- }
853
- });
854
-
855
- builder.on('action:recall', async function* (event, context) {
856
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
857
- try {
858
- const { query, tag, scope, limit } = event.data;
859
- const records = await storage.listMemories({
860
- scopes: resolveMemoryScopeFilter(scope, context.state),
861
- query,
862
- tag,
863
- limit,
864
- });
865
- yield {
866
- type: 'action:recall:result',
867
- data: { success: true, records },
868
- meta: resultMeta,
869
- } as OpenBotEvent;
870
- } catch (error) {
871
- yield {
872
- type: 'action:recall:result',
873
- data: {
874
- success: false,
875
- records: [],
876
- error: error instanceof Error ? error.message : 'Unknown error',
877
- },
878
- meta: resultMeta,
879
- } as OpenBotEvent;
880
- }
881
- });
882
-
883
- builder.on('action:forget', async function* (event, context) {
884
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
885
- try {
886
- const deleted = await storage.deleteMemory({ id: event.data.id });
887
- yield {
888
- type: 'action:forget:result',
889
- data: { success: true, deleted },
890
- meta: resultMeta,
891
- } as OpenBotEvent;
892
- } catch (error) {
893
- yield {
894
- type: 'action:forget:result',
895
- data: {
896
- success: false,
897
- deleted: false,
898
- error: error instanceof Error ? error.message : 'Unknown error',
899
- },
900
- meta: resultMeta,
901
- } as OpenBotEvent;
902
- }
903
- });
904
-
905
- builder.on('action:agent:install', async function* (event) {
906
- try {
907
- const { agentId, name, description, image, instructions, plugins } = event.data;
908
-
909
- // Ensure each plugin is available locally. Built-in ids resolve
910
- // immediately; npm-name ids are fetched on demand.
911
- for (const ref of plugins) {
912
- const installed = await pluginService.isInstalled(ref.id);
913
- if (!installed && ref.id.includes('/') === false && ref.id.includes('-plugin-') === false) {
914
- // Treat ids without a hyphen+slash signature as built-ins; skip install.
915
- continue;
916
- }
917
- if (!installed) {
918
- try {
919
- await pluginService.install({ packageName: ref.id });
920
- } catch (err) {
921
- console.warn(`[bus] Failed to pre-install plugin ${ref.id}`, err);
922
- }
923
- }
924
- }
925
-
926
- await storage.createAgent({
927
- agentId,
928
- name,
929
- description,
930
- image,
931
- instructions,
932
- plugins,
933
- });
934
- yield {
935
- type: 'action:agent:install:result',
936
- data: { success: true, agentId },
937
- } as OpenBotEvent;
938
- yield {
939
- type: 'agent:output',
940
- data: { content: `Successfully installed agent **${name}** (${agentId}) from marketplace.` },
941
- meta: { agentId: 'system' },
942
- } as OpenBotEvent;
943
- } catch (error) {
944
- yield {
945
- type: 'action:agent:install:result',
946
- data: {
947
- success: false,
948
- agentId: event.data.agentId,
949
- error: error instanceof Error ? error.message : 'Unknown error',
950
- },
951
- } as OpenBotEvent;
952
- }
953
- });
954
- };