openbot 0.4.6 → 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.
Files changed (132) hide show
  1. package/.claude/agents/runtime-security-reviewer.md +32 -0
  2. package/.claude/hooks/format.sh +18 -0
  3. package/.claude/hooks/protect-secrets.sh +23 -0
  4. package/.claude/settings.json +26 -0
  5. package/.claude/skills/deploy/SKILL.md +26 -0
  6. package/.claude/skills/new-plugin/SKILL.md +46 -0
  7. package/.dockerignore +9 -0
  8. package/.mcp.json +8 -0
  9. package/AGENTS.md +2 -2
  10. package/CLAUDE.md +50 -0
  11. package/Dockerfile +41 -0
  12. package/deploy/README.md +61 -0
  13. package/deploy/entrypoint.sh +6 -0
  14. package/deploy/fly.toml +36 -0
  15. package/dist/app/bootstrap.js +29 -0
  16. package/dist/app/cli.js +3 -1
  17. package/dist/app/cloud-mode.js +26 -0
  18. package/dist/app/config.js +61 -16
  19. package/dist/app/responding-agent.js +32 -0
  20. package/dist/app/server.js +47 -9
  21. package/dist/plugins/approval/index.js +1 -1
  22. package/dist/plugins/bash/index.js +386 -164
  23. package/dist/plugins/memory/service.js +2 -5
  24. package/dist/plugins/openbot/context.js +17 -1
  25. package/dist/plugins/openbot/index.js +25 -9
  26. package/dist/plugins/openbot/model.js +59 -0
  27. package/dist/plugins/openbot/runtime.js +39 -56
  28. package/dist/plugins/openbot/system-prompt.js +44 -9
  29. package/dist/plugins/preview/index.js +267 -0
  30. package/dist/plugins/storage/index.js +3 -3
  31. package/dist/plugins/storage/service.js +58 -12
  32. package/dist/plugins/todo/index.js +131 -38
  33. package/dist/plugins/todo/service.js +93 -0
  34. package/dist/plugins/ui/index.js +8 -1
  35. package/dist/services/plugins/model-registry.js +101 -0
  36. package/dist/services/plugins/registry.js +6 -4
  37. package/dist/services/plugins/service.js +2 -6
  38. package/docs/agents.md +20 -2
  39. package/docs/architecture.md +1 -1
  40. package/docs/plugins.md +13 -3
  41. package/docs/templates/AGENT.example.md +1 -1
  42. package/package.json +5 -2
  43. package/pnpm-workspace.yaml +2 -0
  44. package/src/app/bootstrap.ts +43 -0
  45. package/src/app/cli.ts +3 -1
  46. package/src/app/cloud-mode.ts +41 -0
  47. package/src/app/config.ts +71 -19
  48. package/src/app/responding-agent.ts +46 -0
  49. package/src/app/server.ts +55 -9
  50. package/src/app/types.ts +167 -31
  51. package/src/plugins/approval/index.ts +1 -1
  52. package/src/plugins/bash/index.ts +498 -185
  53. package/src/plugins/memory/service.ts +2 -5
  54. package/src/plugins/openbot/context.ts +17 -1
  55. package/src/plugins/openbot/index.ts +27 -11
  56. package/src/plugins/openbot/model.ts +76 -0
  57. package/src/plugins/openbot/runtime.ts +47 -69
  58. package/src/plugins/openbot/system-prompt.ts +44 -9
  59. package/src/plugins/preview/index.ts +323 -0
  60. package/src/plugins/storage/index.ts +5 -8
  61. package/src/plugins/storage/service.ts +79 -15
  62. package/src/plugins/todo/index.ts +166 -0
  63. package/src/plugins/todo/service.ts +123 -0
  64. package/src/plugins/ui/index.ts +8 -1
  65. package/src/services/plugins/domain.ts +2 -0
  66. package/src/services/plugins/model-registry.ts +146 -0
  67. package/src/services/plugins/registry.ts +6 -4
  68. package/src/services/plugins/service.ts +2 -6
  69. package/dist/agents/openbot/index.js +0 -76
  70. package/dist/agents/openbot/middleware/approval.js +0 -132
  71. package/dist/agents/openbot/runtime.js +0 -289
  72. package/dist/agents/openbot/system-prompt.js +0 -32
  73. package/dist/agents/openbot/tools/delegation.js +0 -78
  74. package/dist/agents/openbot/tools/mcp.js +0 -99
  75. package/dist/agents/openbot/tools/shell.js +0 -91
  76. package/dist/agents/openbot/tools/storage.js +0 -75
  77. package/dist/agents/openbot/tools/ui.js +0 -176
  78. package/dist/agents/system.js +0 -33
  79. package/dist/bus/agent-package.js +0 -1
  80. package/dist/bus/plugin.js +0 -1
  81. package/dist/bus/services.js +0 -754
  82. package/dist/bus/types.js +0 -1
  83. package/dist/harness/agent-harness.js +0 -45
  84. package/dist/harness/agent-invoke-run.js +0 -44
  85. package/dist/harness/agent-turn.js +0 -99
  86. package/dist/harness/channel-participants.js +0 -40
  87. package/dist/harness/constants.js +0 -2
  88. package/dist/harness/context-meter.js +0 -97
  89. package/dist/harness/context.js +0 -363
  90. package/dist/harness/dispatch.js +0 -144
  91. package/dist/harness/dispatcher.js +0 -156
  92. package/dist/harness/event-normalizer.js +0 -59
  93. package/dist/harness/history.js +0 -177
  94. package/dist/harness/mcp.js +0 -61
  95. package/dist/harness/orchestration.js +0 -88
  96. package/dist/harness/orchestrator.js +0 -149
  97. package/dist/harness/participants.js +0 -22
  98. package/dist/harness/process.js +0 -29
  99. package/dist/harness/queue-processor.js +0 -187
  100. package/dist/harness/run-harness.js +0 -154
  101. package/dist/harness/run.js +0 -98
  102. package/dist/harness/runtime-factory.js +0 -73
  103. package/dist/harness/runtime.js +0 -57
  104. package/dist/harness/todo-advance.js +0 -93
  105. package/dist/harness/todo-dispatch.js +0 -51
  106. package/dist/harness/todos.js +0 -5
  107. package/dist/harness/turn.js +0 -79
  108. package/dist/harness/types.js +0 -1
  109. package/dist/plugins/ai-sdk/index.js +0 -34
  110. package/dist/plugins/ai-sdk/runtime.js +0 -395
  111. package/dist/plugins/ai-sdk/system-prompt.js +0 -18
  112. package/dist/plugins/ai-sdk.js +0 -331
  113. package/dist/plugins/approval.js +0 -163
  114. package/dist/plugins/delegation.js +0 -108
  115. package/dist/plugins/mcp/index.js +0 -108
  116. package/dist/plugins/mcp.js +0 -140
  117. package/dist/plugins/shell/index.js +0 -100
  118. package/dist/plugins/shell.js +0 -123
  119. package/dist/plugins/storage-tools/index.js +0 -76
  120. package/dist/plugins/storage.js +0 -737
  121. package/dist/plugins/thread-namer/index.js +0 -72
  122. package/dist/plugins/threads/index.js +0 -114
  123. package/dist/plugins/ui.js +0 -211
  124. package/dist/plugins/workflow/index.js +0 -65
  125. package/dist/registry/agents.js +0 -138
  126. package/dist/registry/plugins.js +0 -110
  127. package/dist/services/agent-packages.js +0 -103
  128. package/dist/services/memory.js +0 -152
  129. package/dist/services/plugins.js +0 -98
  130. package/dist/services/storage.js +0 -1028
  131. package/dist/workflow/service.js +0 -106
  132. package/dist/workflow/types.js +0 -3
@@ -1,754 +0,0 @@
1
- import { DEFAULT_MARKETPLACE_REGISTRY_URL, loadConfig } from '../app/config.js';
2
- import { storageService } from '../services/storage.js';
3
- import { pluginService } from '../services/plugins.js';
4
- /**
5
- * Resolve a scope alias to a concrete scope string. Aliases let tools accept
6
- * `agent`/`channel`/`global` without knowing the active ids; the bus rewrites
7
- * them using `context.state`.
8
- */
9
- function resolveMemoryScope(alias, state) {
10
- switch (alias) {
11
- case 'agent':
12
- return `agent:${state.agentId}`;
13
- case 'channel':
14
- return `channel:${state.channelId}`;
15
- case 'global':
16
- case undefined:
17
- return 'global';
18
- default:
19
- return 'global';
20
- }
21
- }
22
- function resolveMemoryScopeFilter(alias, state) {
23
- if (alias === 'all' || alias === undefined) {
24
- return ['global', `agent:${state.agentId}`, `channel:${state.channelId}`];
25
- }
26
- return [resolveMemoryScope(alias, state)];
27
- }
28
- const DEFAULT_MARKETPLACE_AGENTS = [];
29
- function isRecord(value) {
30
- return typeof value === 'object' && value !== null && !Array.isArray(value);
31
- }
32
- /**
33
- * Parses JSON from a remote registry file. Supports either
34
- * `{ "agents": [ ... ] }` or a top-level array.
35
- */
36
- export function parseMarketplaceRegistryJson(data) {
37
- const rawAgents = Array.isArray(data) ? data : isRecord(data) && Array.isArray(data.agents) ? data.agents : null;
38
- if (!Array.isArray(rawAgents)) {
39
- throw new Error('Registry JSON must be an array or an object with an "agents" array');
40
- }
41
- return rawAgents.map((item, i) => {
42
- if (!isRecord(item)) {
43
- throw new Error(`agents[${i}]: expected object`);
44
- }
45
- const id = item.id;
46
- const name = item.name;
47
- const description = item.description;
48
- const instructions = item.instructions;
49
- const pluginsRaw = item.plugins;
50
- if (typeof id !== 'string' || !id)
51
- throw new Error(`agents[${i}].id must be a non-empty string`);
52
- if (typeof name !== 'string')
53
- throw new Error(`agents[${i}].name must be a string`);
54
- if (typeof description !== 'string')
55
- throw new Error(`agents[${i}].description must be a string`);
56
- if (typeof instructions !== 'string') {
57
- throw new Error(`agents[${i}].instructions must be a string`);
58
- }
59
- if (!Array.isArray(pluginsRaw))
60
- throw new Error(`agents[${i}].plugins must be an array`);
61
- const plugins = pluginsRaw.map((p, j) => {
62
- if (!isRecord(p) || typeof p.id !== 'string' || !p.id) {
63
- throw new Error(`agents[${i}].plugins[${j}]: expected { "id": string, "config"?: object }`);
64
- }
65
- const ref = { id: p.id };
66
- if (p.config !== undefined) {
67
- if (!isRecord(p.config))
68
- throw new Error(`agents[${i}].plugins[${j}].config must be an object`);
69
- ref.config = p.config;
70
- }
71
- return ref;
72
- });
73
- const listing = { id, name, description, instructions, plugins };
74
- if (item.image !== undefined) {
75
- if (typeof item.image !== 'string')
76
- throw new Error(`agents[${i}].image must be a string`);
77
- listing.image = item.image;
78
- }
79
- return listing;
80
- });
81
- }
82
- async function fetchMarketplaceAgentsFromUrl(url) {
83
- const res = await fetch(url, {
84
- headers: { Accept: 'application/json' },
85
- signal: AbortSignal.timeout(15000),
86
- });
87
- if (!res.ok) {
88
- throw new Error(`Registry HTTP ${res.status} ${res.statusText}`);
89
- }
90
- const json = await res.json();
91
- return parseMarketplaceRegistryJson(json);
92
- }
93
- export const busServicesPlugin = (options) => (builder) => {
94
- const { storage } = options;
95
- builder.on('action:create_thread', async function* (event, context) {
96
- const threadId = event.meta?.threadId;
97
- const channelId = context.state.channelId;
98
- const { threadTitle, initialState } = event.data;
99
- if (!threadId) {
100
- console.warn('[bus] Cannot create thread: meta.threadId is missing');
101
- return;
102
- }
103
- context.state.threadId = threadId;
104
- if (channelId) {
105
- try {
106
- await storage.createThread({
107
- channelId,
108
- threadId,
109
- threadTitle,
110
- initialState: initialState || {},
111
- });
112
- context.state.threadDetails = await storage.getThreadDetails({
113
- channelId,
114
- threadId,
115
- });
116
- }
117
- catch (error) {
118
- console.warn(`[bus] Failed to initialize thread for channel ${channelId} thread ${threadId}`, error);
119
- }
120
- }
121
- yield {
122
- type: 'action:create_thread:result',
123
- data: { success: true, threadId, threadTitle },
124
- meta: { ...(event.meta || {}), threadId, agentId: context.state.agentId },
125
- };
126
- });
127
- builder.on('action:create_channel', async function* (event, context) {
128
- const { channelId, spec, initialState, cwd, participants } = event.data;
129
- const rawChannelId = (channelId || '').trim();
130
- const channelSpec = typeof spec === 'string' ? spec : '';
131
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
132
- if (!rawChannelId) {
133
- yield {
134
- type: 'action:create_channel:result',
135
- data: { success: false, channelId: '', channelUrl: '' },
136
- meta: resultMeta,
137
- };
138
- return;
139
- }
140
- const channelUrl = `/channels/${rawChannelId}`;
141
- const mergedInitial = { ...(initialState || {}) };
142
- if (participants !== undefined) {
143
- const normalized = Array.isArray(participants)
144
- ? participants
145
- .filter((x) => typeof x === 'string')
146
- .map((s) => s.trim())
147
- .filter(Boolean)
148
- : [];
149
- mergedInitial.participants = normalized;
150
- }
151
- try {
152
- await storage.createChannel({
153
- channelId: rawChannelId,
154
- spec: channelSpec,
155
- initialState: mergedInitial,
156
- cwd,
157
- });
158
- yield {
159
- type: 'action:create_channel:result',
160
- data: { success: true, channelId: rawChannelId, channelUrl },
161
- meta: resultMeta,
162
- };
163
- yield {
164
- type: 'agent:output',
165
- data: { content: `Created channel \`${rawChannelId}\`.` },
166
- meta: resultMeta,
167
- };
168
- }
169
- catch {
170
- yield {
171
- type: 'action:create_channel:result',
172
- data: { success: false, channelId: rawChannelId, channelUrl },
173
- meta: resultMeta,
174
- };
175
- }
176
- });
177
- builder.on('action:update_channel', async function* (event, context) {
178
- const data = (event.data || {});
179
- const targetChannelId = (data.channelId || context.state.channelId || '').trim();
180
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
181
- if (!targetChannelId) {
182
- yield {
183
- type: 'action:update_channel:result',
184
- data: { success: false, channelId: '', updatedFields: [] },
185
- meta: resultMeta,
186
- };
187
- return;
188
- }
189
- const patch = {};
190
- const updatedFields = [];
191
- if (typeof data.name === 'string' && data.name.trim()) {
192
- patch.name = data.name.trim();
193
- updatedFields.push('name');
194
- }
195
- if (typeof data.cwd === 'string' && data.cwd.trim()) {
196
- patch.cwd = data.cwd.trim();
197
- updatedFields.push('cwd');
198
- }
199
- if (data.participants !== undefined) {
200
- if (Array.isArray(data.participants)) {
201
- patch.participants = data.participants
202
- .filter((x) => typeof x === 'string')
203
- .map((s) => s.trim())
204
- .filter(Boolean);
205
- }
206
- else {
207
- patch.participants = [];
208
- }
209
- updatedFields.push('participants');
210
- }
211
- try {
212
- if (updatedFields.length > 0) {
213
- await storage.patchChannelState({ channelId: targetChannelId, state: patch });
214
- }
215
- if (targetChannelId === context.state.channelId) {
216
- context.state.channelDetails = await storage.getChannelDetails({
217
- channelId: context.state.channelId,
218
- });
219
- }
220
- yield {
221
- type: 'action:update_channel:result',
222
- data: { success: true, channelId: targetChannelId, updatedFields },
223
- meta: resultMeta,
224
- };
225
- }
226
- catch {
227
- yield {
228
- type: 'action:update_channel:result',
229
- data: { success: false, channelId: targetChannelId, updatedFields },
230
- meta: resultMeta,
231
- };
232
- }
233
- });
234
- builder.on('action:patch_channel_details', async function* (event, context) {
235
- const updatedFields = [];
236
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
237
- const data = (event.data || {});
238
- try {
239
- if (data.state !== undefined) {
240
- await storage.patchChannelState({
241
- channelId: context.state.channelId,
242
- state: data.state,
243
- });
244
- updatedFields.push('state');
245
- }
246
- if (typeof data.spec === 'string') {
247
- await storage.patchChannelSpec({
248
- channelId: context.state.channelId,
249
- spec: data.spec,
250
- });
251
- updatedFields.push('spec');
252
- }
253
- if (typeof data.cwd === 'string') {
254
- await storage.patchChannelState({
255
- channelId: context.state.channelId,
256
- state: { cwd: data.cwd },
257
- });
258
- updatedFields.push('cwd');
259
- }
260
- if (data.participants !== undefined) {
261
- const normalized = Array.isArray(data.participants)
262
- ? data.participants
263
- .filter((x) => typeof x === 'string')
264
- .map((s) => s.trim())
265
- .filter(Boolean)
266
- : [];
267
- await storage.patchChannelState({
268
- channelId: context.state.channelId,
269
- state: { participants: normalized },
270
- });
271
- updatedFields.push('participants');
272
- }
273
- context.state.channelDetails = await storage.getChannelDetails({
274
- channelId: context.state.channelId,
275
- });
276
- yield {
277
- type: "client:ui:widget",
278
- data: {
279
- kind: "message",
280
- title: "Channel details updated.",
281
- body: "The channel details have been updated.",
282
- },
283
- meta: resultMeta,
284
- };
285
- yield {
286
- type: 'action:patch_channel_details:result',
287
- data: { success: true, updatedFields },
288
- meta: resultMeta,
289
- };
290
- }
291
- catch {
292
- yield {
293
- type: 'action:patch_channel_details:result',
294
- data: { success: false, updatedFields },
295
- meta: resultMeta,
296
- };
297
- }
298
- });
299
- builder.on('action:patch_thread_details', async function* (event, context) {
300
- const updatedFields = [];
301
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
302
- try {
303
- if (!context.state.threadId) {
304
- throw new Error('Missing threadId in state for patch_thread_details');
305
- }
306
- if (event.data.state !== undefined) {
307
- await storage.patchThreadState({
308
- channelId: context.state.channelId,
309
- threadId: context.state.threadId,
310
- state: event.data.state,
311
- });
312
- updatedFields.push('state');
313
- }
314
- context.state.threadDetails = await storage.getThreadDetails({
315
- channelId: context.state.channelId,
316
- threadId: context.state.threadId,
317
- });
318
- yield {
319
- type: 'action:patch_thread_details:result',
320
- data: { success: true, updatedFields },
321
- meta: resultMeta,
322
- };
323
- }
324
- catch {
325
- yield {
326
- type: 'action:patch_thread_details:result',
327
- data: { success: false, updatedFields },
328
- meta: resultMeta,
329
- };
330
- }
331
- });
332
- builder.on('agent:usage', async function* (event, context) {
333
- const { usage } = event.data;
334
- if (!context.state.threadId)
335
- return;
336
- const currentState = context.state.threadDetails?.state || {};
337
- const currentUsage = currentState.usage || {
338
- promptTokens: 0,
339
- completionTokens: 0,
340
- totalTokens: 0,
341
- };
342
- const nextUsage = {
343
- promptTokens: (currentUsage.promptTokens || 0) + usage.promptTokens,
344
- completionTokens: (currentUsage.completionTokens || 0) + usage.completionTokens,
345
- totalTokens: (currentUsage.totalTokens || 0) + usage.totalTokens,
346
- };
347
- await storage.patchThreadState({
348
- channelId: context.state.channelId,
349
- threadId: context.state.threadId,
350
- state: { usage: nextUsage },
351
- });
352
- context.state.threadDetails = await storage.getThreadDetails({
353
- channelId: context.state.channelId,
354
- threadId: context.state.threadId,
355
- });
356
- });
357
- builder.on('action:storage:get-channels', async function* () {
358
- const channels = await storage.getChannels();
359
- yield { type: 'action:storage:get-channels-result', data: { channels } };
360
- });
361
- builder.on('action:storage:get-threads', async function* (event) {
362
- const threads = await storage.getThreads({ channelId: event.data.channelId });
363
- yield { type: 'action:storage:get-threads-result', data: { threads } };
364
- });
365
- builder.on('action:storage:get-channel-details', async function* (_, state) {
366
- const channelDetails = await storage.getChannelDetails({
367
- channelId: state.state.channelId,
368
- });
369
- yield { type: 'action:storage:get-channel-details-result', data: { channelDetails } };
370
- });
371
- builder.on('action:storage:get-thread-details', async function* (_, state) {
372
- const threadId = state.state.threadId;
373
- const threadDetails = threadId
374
- ? await storage.getThreadDetails({ channelId: state.state.channelId, threadId })
375
- : null;
376
- yield { type: 'action:storage:get-thread-details-result', data: { threadDetails } };
377
- });
378
- builder.on('action:storage:get-agents', async function* () {
379
- const agents = await storage.getAgents();
380
- yield { type: 'action:storage:get-agents-result', data: { agents } };
381
- });
382
- builder.on('action:storage:get-plugins', async function* () {
383
- const plugins = await storage.getPlugins();
384
- yield { type: 'action:storage:get-plugins-result', data: { plugins } };
385
- });
386
- builder.on('action:storage:get-agent-details', async function* (event) {
387
- try {
388
- const agentDetails = await storage.getAgentDetails({ agentId: event.data.agentId });
389
- yield { type: 'action:storage:get-agent-details-result', data: { agentDetails } };
390
- }
391
- catch (error) {
392
- console.error(`[bus] Failed to get agent details for ${event.data.agentId}`, error);
393
- yield {
394
- type: 'action:storage:get-agent-details-result',
395
- data: {
396
- agentDetails: null,
397
- error: error instanceof Error ? error.message : 'Unknown error',
398
- },
399
- };
400
- }
401
- });
402
- builder.on('action:storage:create-agent', async function* (event) {
403
- try {
404
- const { agentId, name, description, image, instructions, plugins } = event.data;
405
- await storage.createAgent({ agentId, name, description, image, instructions, plugins });
406
- yield { type: 'action:storage:create-agent-result', data: { success: true } };
407
- }
408
- catch (error) {
409
- yield {
410
- type: 'action:storage:create-agent-result',
411
- data: {
412
- success: false,
413
- error: error instanceof Error ? error.message : 'Unknown error',
414
- },
415
- };
416
- }
417
- });
418
- builder.on('action:storage:update-agent', async function* (event) {
419
- try {
420
- const { agentId, name, description, image, instructions, plugins } = event.data;
421
- await storage.updateAgent({ agentId, name, description, image, instructions, plugins });
422
- yield { type: 'action:storage:update-agent-result', data: { success: true } };
423
- }
424
- catch (error) {
425
- yield {
426
- type: 'action:storage:update-agent-result',
427
- data: {
428
- success: false,
429
- error: error instanceof Error ? error.message : 'Unknown error',
430
- },
431
- };
432
- }
433
- });
434
- builder.on('action:storage:delete-agent', async function* (event) {
435
- try {
436
- await storage.deleteAgent({ agentId: event.data.agentId });
437
- yield { type: 'action:storage:delete-agent-result', data: { success: true } };
438
- }
439
- catch (error) {
440
- yield {
441
- type: 'action:storage:delete-agent-result',
442
- data: {
443
- success: false,
444
- error: error instanceof Error ? error.message : 'Unknown error',
445
- },
446
- };
447
- }
448
- });
449
- builder.on('action:storage:get-events', async function* (_, state) {
450
- const events = await storage.getEvents(state.state);
451
- if (!state.state.threadId && events.length > 0) {
452
- const lastId = events[events.length - 1]?.id;
453
- if (lastId) {
454
- await storageService.setLastReadForChannel({
455
- channelId: state.state.channelId,
456
- lastReadEventId: lastId,
457
- });
458
- }
459
- }
460
- yield { type: 'action:storage:get-events-result', data: { events } };
461
- });
462
- builder.on('action:storage:get-variables', async function* () {
463
- const variables = await storage.getVariables();
464
- const masked = {};
465
- for (const [key, val] of Object.entries(variables)) {
466
- if (typeof val === 'object' && val !== null && val.secret) {
467
- masked[key] = '********';
468
- }
469
- else {
470
- masked[key] = typeof val === 'string' ? val : val.value;
471
- }
472
- }
473
- yield {
474
- type: 'action:storage:get-variables-result',
475
- data: { variables: masked },
476
- };
477
- });
478
- builder.on('action:storage:create-variable', async function* (event) {
479
- try {
480
- const { key, value, secret } = event.data;
481
- await storage.createVariable({ key, value, secret });
482
- yield { type: 'action:storage:create-variable-result', data: { success: true } };
483
- }
484
- catch (error) {
485
- yield {
486
- type: 'action:storage:create-variable-result',
487
- data: {
488
- success: false,
489
- error: error instanceof Error ? error.message : 'Unknown error',
490
- },
491
- };
492
- }
493
- });
494
- builder.on('action:storage:delete-variable', async function* (event) {
495
- try {
496
- await storage.deleteVariable({ key: event.data.key });
497
- yield { type: 'action:storage:delete-variable-result', data: { success: true } };
498
- }
499
- catch (error) {
500
- yield {
501
- type: 'action:storage:delete-variable-result',
502
- data: {
503
- success: false,
504
- error: error instanceof Error ? error.message : 'Unknown error',
505
- },
506
- };
507
- }
508
- });
509
- builder.on('action:storage:patch-channel-state', async function* (event, state) {
510
- try {
511
- await storage.patchChannelState({
512
- channelId: state.state.channelId,
513
- state: event.data.state,
514
- });
515
- yield { type: 'action:storage:patch-channel-state-result', data: { success: true } };
516
- }
517
- catch {
518
- yield { type: 'action:storage:patch-channel-state-result', data: { success: false } };
519
- }
520
- });
521
- builder.on('action:storage:patch-thread-state', async function* (event, state) {
522
- try {
523
- if (!state.state.threadId) {
524
- throw new Error('Missing threadId in state for patch-thread-state');
525
- }
526
- await storage.patchThreadState({
527
- channelId: state.state.channelId,
528
- threadId: state.state.threadId,
529
- state: event.data.state,
530
- });
531
- yield { type: 'action:storage:patch-thread-state-result', data: { success: true } };
532
- }
533
- catch {
534
- yield { type: 'action:storage:patch-thread-state-result', data: { success: false } };
535
- }
536
- });
537
- builder.on('action:storage:list-files', async function* (event, context) {
538
- const channelId = context.state.channelId;
539
- const subPath = event.data?.path || '';
540
- try {
541
- const files = await storage.listFiles({ channelId, path: subPath });
542
- yield {
543
- type: 'action:storage:list-files:result',
544
- data: { success: true, files },
545
- };
546
- }
547
- catch (error) {
548
- yield {
549
- type: 'action:storage:list-files:result',
550
- data: {
551
- success: false,
552
- files: [],
553
- error: error instanceof Error ? error.message : 'Unknown error',
554
- },
555
- };
556
- }
557
- });
558
- builder.on('action:storage:read-file', async function* (event, context) {
559
- const channelId = context.state.channelId;
560
- const filePath = event.data?.path;
561
- if (!filePath) {
562
- yield {
563
- type: 'action:storage:read-file:result',
564
- data: { success: false, path: '', error: 'Path is required' },
565
- };
566
- return;
567
- }
568
- try {
569
- const content = await storage.readFile({ channelId, path: filePath });
570
- yield {
571
- type: 'action:storage:read-file:result',
572
- data: { success: true, content, path: filePath },
573
- };
574
- }
575
- catch (error) {
576
- yield {
577
- type: 'action:storage:read-file:result',
578
- data: {
579
- success: false,
580
- path: filePath,
581
- error: error instanceof Error ? error.message : 'Unknown error',
582
- },
583
- };
584
- }
585
- });
586
- builder.on('action:plugin:install', async function* (event) {
587
- try {
588
- const { name, version } = event.data;
589
- const result = await pluginService.install({ packageName: name, version });
590
- yield {
591
- type: 'action:plugin:install:result',
592
- data: { success: true, plugin: result },
593
- };
594
- }
595
- catch (error) {
596
- yield {
597
- type: 'action:plugin:install:result',
598
- data: { success: false, error: error.message },
599
- };
600
- }
601
- });
602
- builder.on('action:plugin:uninstall', async function* (event) {
603
- try {
604
- await pluginService.uninstall(event.data.id);
605
- yield { type: 'action:plugin:uninstall:result', data: { success: true } };
606
- }
607
- catch (error) {
608
- yield {
609
- type: 'action:plugin:uninstall:result',
610
- data: { success: false, error: error.message },
611
- };
612
- }
613
- });
614
- builder.on('action:marketplace:list', async function* () {
615
- const { marketplaceRegistryUrl } = loadConfig();
616
- const registryUrl = marketplaceRegistryUrl?.trim() || DEFAULT_MARKETPLACE_REGISTRY_URL;
617
- let agents = DEFAULT_MARKETPLACE_AGENTS;
618
- try {
619
- agents = await fetchMarketplaceAgentsFromUrl(registryUrl);
620
- }
621
- catch (err) {
622
- console.warn(`[bus] marketplace registry fetch failed (${registryUrl}), using built-in list:`, err instanceof Error ? err.message : err);
623
- }
624
- yield {
625
- type: 'action:marketplace:list:result',
626
- data: { success: true, agents },
627
- };
628
- });
629
- builder.on('action:remember', async function* (event, context) {
630
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
631
- try {
632
- const { content, scope, tags } = event.data;
633
- const record = await storage.appendMemory({
634
- scope: resolveMemoryScope(scope, context.state),
635
- content,
636
- tags,
637
- });
638
- yield {
639
- type: 'action:remember:result',
640
- data: { success: true, record },
641
- meta: resultMeta,
642
- };
643
- }
644
- catch (error) {
645
- yield {
646
- type: 'action:remember:result',
647
- data: {
648
- success: false,
649
- error: error instanceof Error ? error.message : 'Unknown error',
650
- },
651
- meta: resultMeta,
652
- };
653
- }
654
- });
655
- builder.on('action:recall', async function* (event, context) {
656
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
657
- try {
658
- const { query, tag, scope, limit } = event.data;
659
- const records = await storage.listMemories({
660
- scopes: resolveMemoryScopeFilter(scope, context.state),
661
- query,
662
- tag,
663
- limit,
664
- });
665
- yield {
666
- type: 'action:recall:result',
667
- data: { success: true, records },
668
- meta: resultMeta,
669
- };
670
- }
671
- catch (error) {
672
- yield {
673
- type: 'action:recall:result',
674
- data: {
675
- success: false,
676
- records: [],
677
- error: error instanceof Error ? error.message : 'Unknown error',
678
- },
679
- meta: resultMeta,
680
- };
681
- }
682
- });
683
- builder.on('action:forget', async function* (event, context) {
684
- const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
685
- try {
686
- const deleted = await storage.deleteMemory({ id: event.data.id });
687
- yield {
688
- type: 'action:forget:result',
689
- data: { success: true, deleted },
690
- meta: resultMeta,
691
- };
692
- }
693
- catch (error) {
694
- yield {
695
- type: 'action:forget:result',
696
- data: {
697
- success: false,
698
- deleted: false,
699
- error: error instanceof Error ? error.message : 'Unknown error',
700
- },
701
- meta: resultMeta,
702
- };
703
- }
704
- });
705
- builder.on('action:agent:install', async function* (event) {
706
- try {
707
- const { agentId, name, description, image, instructions, plugins } = event.data;
708
- // Ensure each plugin is available locally. Built-in ids resolve
709
- // immediately; npm-name ids are fetched on demand.
710
- for (const ref of plugins) {
711
- const installed = await pluginService.isInstalled(ref.id);
712
- if (!installed && ref.id.includes('/') === false && ref.id.includes('-plugin-') === false) {
713
- // Treat ids without a hyphen+slash signature as built-ins; skip install.
714
- continue;
715
- }
716
- if (!installed) {
717
- try {
718
- await pluginService.install({ packageName: ref.id });
719
- }
720
- catch (err) {
721
- console.warn(`[bus] Failed to pre-install plugin ${ref.id}`, err);
722
- }
723
- }
724
- }
725
- await storage.createAgent({
726
- agentId,
727
- name,
728
- description,
729
- image,
730
- instructions,
731
- plugins,
732
- });
733
- yield {
734
- type: 'action:agent:install:result',
735
- data: { success: true, agentId },
736
- };
737
- yield {
738
- type: 'agent:output',
739
- data: { content: `Successfully installed agent **${name}** (${agentId}) from marketplace.` },
740
- meta: { agentId: 'system' },
741
- };
742
- }
743
- catch (error) {
744
- yield {
745
- type: 'action:agent:install:result',
746
- data: {
747
- success: false,
748
- agentId: event.data.agentId,
749
- error: error instanceof Error ? error.message : 'Unknown error',
750
- },
751
- };
752
- }
753
- });
754
- };