openbot 0.3.5 → 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 (98) 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/assets/icon.svg +9 -3
  7. package/dist/bus/services.js +78 -132
  8. package/dist/harness/agent-invoke-run.js +44 -0
  9. package/dist/harness/agent-turn.js +99 -0
  10. package/dist/harness/channel-participants.js +40 -0
  11. package/dist/harness/constants.js +2 -0
  12. package/dist/harness/context-meter.js +97 -0
  13. package/dist/harness/context.js +98 -45
  14. package/dist/harness/dispatch.js +144 -0
  15. package/dist/harness/dispatcher.js +45 -156
  16. package/dist/harness/history.js +177 -0
  17. package/dist/harness/index.js +91 -0
  18. package/dist/harness/orchestration.js +88 -0
  19. package/dist/harness/participants.js +22 -0
  20. package/dist/harness/run-harness.js +154 -0
  21. package/dist/harness/run.js +98 -0
  22. package/dist/harness/runtime-factory.js +0 -34
  23. package/dist/harness/runtime.js +57 -0
  24. package/dist/harness/todo-dispatch.js +51 -0
  25. package/dist/harness/todos.js +5 -0
  26. package/dist/harness/turn.js +79 -0
  27. package/dist/plugins/approval/index.js +105 -149
  28. package/dist/plugins/delegation/index.js +119 -32
  29. package/dist/plugins/memory/index.js +103 -14
  30. package/dist/plugins/memory/service.js +152 -0
  31. package/dist/plugins/openbot/context.js +80 -0
  32. package/dist/plugins/openbot/history.js +98 -0
  33. package/dist/plugins/openbot/index.js +31 -0
  34. package/dist/plugins/openbot/runtime.js +317 -0
  35. package/dist/plugins/openbot/system-prompt.js +5 -0
  36. package/dist/plugins/plugin-manager/index.js +105 -0
  37. package/dist/plugins/storage/index.js +573 -0
  38. package/dist/plugins/storage/service.js +1159 -0
  39. package/dist/plugins/storage-tools/index.js +2 -2
  40. package/dist/plugins/thread-namer/index.js +72 -0
  41. package/dist/plugins/thread-naming/generate-title.js +44 -0
  42. package/dist/plugins/thread-naming/index.js +103 -0
  43. package/dist/plugins/threads/index.js +114 -0
  44. package/dist/plugins/todo/index.js +24 -25
  45. package/dist/plugins/ui/index.js +2 -32
  46. package/dist/registry/plugins.js +3 -9
  47. package/dist/services/plugins/domain.js +1 -0
  48. package/dist/services/plugins/plugin-cache.js +9 -0
  49. package/dist/services/plugins/registry.js +110 -0
  50. package/dist/services/plugins/service.js +177 -0
  51. package/dist/services/plugins/types.js +1 -0
  52. package/dist/services/process.js +29 -0
  53. package/dist/services/storage.js +41 -15
  54. package/dist/services/thread-naming.js +81 -0
  55. package/docs/agents.md +16 -10
  56. package/docs/architecture.md +2 -2
  57. package/docs/plugins.md +6 -15
  58. package/docs/templates/AGENT.example.md +7 -13
  59. package/package.json +1 -2
  60. package/src/app/agent-ids.ts +5 -0
  61. package/src/app/cli.ts +1 -1
  62. package/src/app/config.ts +1 -31
  63. package/src/app/server.ts +8 -16
  64. package/src/app/types.ts +70 -190
  65. package/src/assets/icon.svg +9 -3
  66. package/src/harness/index.ts +145 -0
  67. package/src/plugins/approval/index.ts +91 -189
  68. package/src/plugins/delegation/index.ts +136 -39
  69. package/src/plugins/memory/index.ts +112 -15
  70. package/src/{services/memory.ts → plugins/memory/service.ts} +1 -1
  71. package/src/plugins/openbot/context.ts +91 -0
  72. package/src/plugins/openbot/history.ts +107 -0
  73. package/src/plugins/openbot/index.ts +37 -0
  74. package/src/plugins/openbot/runtime.ts +384 -0
  75. package/src/plugins/openbot/system-prompt.ts +7 -0
  76. package/src/plugins/plugin-manager/index.ts +122 -0
  77. package/src/plugins/shell/index.ts +1 -1
  78. package/src/plugins/storage/index.ts +633 -0
  79. package/src/{services/storage.ts → plugins/storage/service.ts} +257 -72
  80. package/src/{bus/types.ts → services/plugins/domain.ts} +20 -7
  81. package/src/services/plugins/plugin-cache.ts +13 -0
  82. package/src/{registry/plugins.ts → services/plugins/registry.ts} +25 -27
  83. package/src/services/{plugins.ts → plugins/service.ts} +96 -2
  84. package/src/{bus/plugin.ts → services/plugins/types.ts} +3 -3
  85. package/src/bus/services.ts +0 -908
  86. package/src/harness/context.ts +0 -356
  87. package/src/harness/dispatcher.ts +0 -379
  88. package/src/harness/mcp.ts +0 -78
  89. package/src/harness/runtime-factory.ts +0 -129
  90. package/src/harness/todo-advance.ts +0 -128
  91. package/src/plugins/ai-sdk/index.ts +0 -41
  92. package/src/plugins/ai-sdk/runtime.ts +0 -468
  93. package/src/plugins/ai-sdk/system-prompt.ts +0 -18
  94. package/src/plugins/mcp/index.ts +0 -128
  95. package/src/plugins/storage-tools/index.ts +0 -90
  96. package/src/plugins/todo/index.ts +0 -64
  97. package/src/plugins/ui/index.ts +0 -227
  98. /package/src/{harness → services}/process.ts +0 -0
@@ -1,159 +1,115 @@
1
- import { storageService } from '../../services/storage.js';
2
- export const DEFAULT_APPROVAL_RULES = [
3
- {
4
- action: 'action:shell_exec',
5
- denyEvent: 'action:shell_exec:result',
6
- message: 'The agent wants to run a terminal command.',
7
- detailKeys: ['command', 'cwd', 'shell', 'timeoutMs'],
8
- hiddenKeys: ['env'],
9
- denyData: {
10
- exitCode: null,
11
- stdout: '',
12
- stderr: 'Command execution was denied by the user.',
13
- timedOut: false,
14
- },
15
- },
16
- ];
17
- const asRecord = (value) => value && typeof value === 'object' && !Array.isArray(value)
18
- ? value
19
- : {};
20
- const getApprovalsFromState = (state) => {
21
- const source = state.threadDetails?.state ?? state.channelDetails?.state;
22
- const stateRecord = asRecord(source);
23
- return asRecord(stateRecord.approvals);
24
- };
25
- const persistApprovals = async (state, approvals) => {
26
- if (state.threadId) {
27
- await storageService.patchThreadState({
28
- channelId: state.channelId,
29
- threadId: state.threadId,
30
- state: { approvals },
31
- });
32
- return;
33
- }
34
- await storageService.patchChannelState({
35
- channelId: state.channelId,
36
- state: { approvals },
37
- });
38
- };
39
- const buildApprovalPlugin = (rules) => (builder) => {
40
- for (const rule of rules) {
41
- builder.on(rule.action, async function* (event, context) {
42
- const meta = asRecord(event.meta);
43
- if (meta.approvalStatus === 'approved')
1
+ import { randomUUID } from 'node:crypto';
2
+ /**
3
+ * `approval` — gates protected tool calls behind a UI confirmation widget.
4
+ *
5
+ * This is a simplified version that intercepts specified actions (default: shell_exec)
6
+ * and requires user approval before they are allowed to proceed.
7
+ */
8
+ // In-memory tracking for pending approval IDs with TTL (shared across plugin instances)
9
+ const pendingApprovals = new Map();
10
+ const TTL_MS = 4 * 60 * 60 * 1000; // 4 hours
11
+ export const approvalPlugin = {
12
+ id: 'approval',
13
+ name: 'Approval',
14
+ description: 'Gate protected tool calls behind a UI confirmation widget.',
15
+ factory: ({ config }) => (builder) => {
16
+ // Actions that require approval. Defaults to shell_exec.
17
+ const actionsToApprove = config.actions || ['action:shell_exec'];
18
+ for (const action of actionsToApprove) {
19
+ builder.intercept(action, (event, context) => {
20
+ // If already approved in this flow, let it pass to the actual handler
21
+ if (event.meta?.approvalStatus === 'approved')
22
+ return event;
23
+ // Otherwise, intercept and ask for approval via a UI widget
24
+ const displayData = action === 'action:shell_exec'
25
+ ? `\`\`\`bash\n${event.data.command}\n\`\`\``
26
+ : `\`\`\`json\n${JSON.stringify(event.data, null, 2)}\n\`\`\``;
27
+ const widgetId = randomUUID();
28
+ pendingApprovals.set(widgetId, Date.now());
29
+ context.suspend({
30
+ type: 'client:ui:widget',
31
+ data: {
32
+ widgetId,
33
+ kind: 'message',
34
+ title: `The agent wants to perform \`${action}\``,
35
+ body: displayData,
36
+ metadata: {
37
+ type: 'approval:request',
38
+ originalEvent: event,
39
+ },
40
+ actions: [
41
+ { id: 'approve', label: 'Approve', variant: 'primary' },
42
+ { id: 'deny', label: 'Deny', variant: 'danger' },
43
+ ],
44
+ },
45
+ meta: { agentId: context.state.agentId, threadId: context.state.threadId },
46
+ });
47
+ });
48
+ }
49
+ // Handle the user's response from the UI widget
50
+ builder.on('client:ui:widget:response', async function* (event, context) {
51
+ const { widgetId, actionId } = event.data;
52
+ const metadata = event.data?.metadata;
53
+ if (metadata?.type !== 'approval:request')
44
54
  return;
45
- const eventData = asRecord(event.data);
46
- const eventMeta = meta;
47
- const approvalId = `approval_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
48
- const widgetId = `widget_${approvalId}`;
49
- const executeEvent = rule.executeEvent || rule.action;
50
- const denyEvent = rule.denyEvent || `${rule.action}:result`;
51
- const denyData = rule.denyData || {};
52
- const hiddenKeys = new Set(rule.hiddenKeys || []);
53
- const detailKeys = rule.detailKeys || Object.keys(eventData);
54
- const details = detailKeys
55
- .filter((key) => !hiddenKeys.has(key))
56
- .map((key) => `- ${key}: ${String(eventData[key] ?? '')}`)
57
- .join('\n');
58
- const pendingApprovals = getApprovalsFromState(context.state);
59
- pendingApprovals[approvalId] = {
60
- id: approvalId,
61
- action: rule.action,
62
- executeEvent,
63
- denyEvent,
64
- denyData,
65
- payload: eventData,
66
- meta: eventMeta,
67
- message: rule.message || `Approval required for ${rule.action}.`,
68
- createdAt: new Date().toISOString(),
69
- status: 'pending',
70
- };
71
- await persistApprovals(context.state, pendingApprovals);
55
+ // Verify the widget is still pending and hasn't expired
56
+ if (!widgetId || !pendingApprovals.has(widgetId)) {
57
+ console.warn(`[approval] Received response for unknown or already handled widget: ${widgetId}`);
58
+ return;
59
+ }
60
+ const timestamp = pendingApprovals.get(widgetId);
61
+ if (Date.now() - timestamp > TTL_MS) {
62
+ pendingApprovals.delete(widgetId);
63
+ console.warn(`[approval] Received response for expired widget: ${widgetId}`);
64
+ return;
65
+ }
66
+ // Mark as handled
67
+ pendingApprovals.delete(widgetId);
68
+ const originalEvent = metadata.originalEvent;
69
+ const approved = actionId === 'approve';
70
+ // Yield a "responded" widget update to the UI
72
71
  yield {
73
72
  type: 'client:ui:widget',
74
73
  data: {
75
- kind: 'choice',
76
74
  widgetId,
77
- title: 'Approval Required',
78
- body: `${rule.message || 'A protected action requires approval.'}${details ? `\n\n${details}` : ''}`,
79
- metadata: { type: 'approval:request', approvalId, action: rule.action },
80
- actions: [
81
- { id: 'approve', label: 'Approve', variant: 'primary' },
82
- { id: 'deny', label: 'Deny', variant: 'danger' },
83
- ],
75
+ kind: 'message',
76
+ title: `Action ${approved ? 'Approved' : 'Denied'}`,
77
+ body: `The request for \`${originalEvent.type}\` was ${approved ? 'approved' : 'denied'}.`,
78
+ state: approved ? 'submitted' : 'cancelled',
79
+ display: 'collapsed',
80
+ disabled: true,
81
+ actions: [], // Clear actions to disable buttons in UI
84
82
  },
85
- meta: { ...(event.meta || {}), agentId: context.state.agentId },
86
- };
87
- yield {
88
- type: 'agent:output',
89
- data: { content: `Waiting for approval before running \`${rule.action}\`.` },
90
- meta: { ...(event.meta || {}), agentId: context.state.agentId },
83
+ meta: { agentId: context.state.agentId, threadId: context.state.threadId },
91
84
  };
92
- context.suspend();
85
+ if (approved) {
86
+ // Re-emit the original event with approved status so the actual handler can run
87
+ yield {
88
+ ...originalEvent,
89
+ meta: {
90
+ ...(originalEvent.meta || {}),
91
+ approvalStatus: 'approved',
92
+ },
93
+ };
94
+ }
95
+ else {
96
+ // Emit a failure result event for the denied action
97
+ // yield {
98
+ // type: `${originalEvent.type}:result` as OpenBotEvent['type'],
99
+ // data: {
100
+ // success: false,
101
+ // error: 'Action denied by user.',
102
+ // stderr: 'Action denied by user.',
103
+ // },
104
+ // meta: originalEvent.meta,
105
+ // } as OpenBotEvent;
106
+ yield {
107
+ type: 'agent:output',
108
+ data: { content: `Action \`${originalEvent.type}\` was denied.` },
109
+ meta: { agentId: context.state.agentId },
110
+ };
111
+ }
93
112
  });
94
- }
95
- builder.on('client:ui:widget:response', async function* (event, context) {
96
- const metadata = asRecord(event.data?.metadata);
97
- if (metadata.type !== 'approval:request')
98
- return;
99
- const approvalId = String(metadata.approvalId || '');
100
- if (!approvalId)
101
- return;
102
- const approvals = getApprovalsFromState(context.state);
103
- const approval = approvals[approvalId];
104
- if (!approval || approval.status !== 'pending') {
105
- yield {
106
- type: 'agent:output',
107
- data: { content: 'Approval request not found or already resolved.' },
108
- meta: { ...(event.meta || {}), agentId: context.state.agentId },
109
- };
110
- return;
111
- }
112
- const approved = event.data.actionId === 'approve';
113
- approvals[approvalId] = {
114
- ...approval,
115
- status: approved ? 'approved' : 'denied',
116
- };
117
- await persistApprovals(context.state, approvals);
118
- if (approved) {
119
- yield {
120
- type: approval.executeEvent,
121
- data: approval.payload,
122
- meta: {
123
- ...(approval.meta || {}),
124
- approvalId,
125
- approvalStatus: 'approved',
126
- },
127
- };
128
- return;
129
- }
130
- yield {
131
- type: approval.denyEvent,
132
- data: {
133
- success: false,
134
- approved: false,
135
- error: 'Action denied by user approval.',
136
- ...approval.denyData,
137
- },
138
- meta: { ...(approval.meta || {}), approvalId },
139
- };
140
- yield {
141
- type: 'agent:output',
142
- data: { content: 'Action denied by user approval.' },
143
- meta: { ...(event.meta || {}), agentId: context.state.agentId },
144
- };
145
- });
146
- };
147
- const readRules = (config) => {
148
- const raw = config.rules;
149
- if (!Array.isArray(raw))
150
- return DEFAULT_APPROVAL_RULES;
151
- return raw.filter((entry) => !!entry && typeof entry === 'object' && typeof entry.action === 'string');
152
- };
153
- export const approvalPlugin = {
154
- id: 'approval',
155
- name: 'Approval',
156
- description: 'Gate protected tool calls (e.g. shell_exec) behind a UI confirmation prompt.',
157
- factory: ({ config }) => buildApprovalPlugin(readRules(config)),
113
+ },
158
114
  };
159
115
  export default approvalPlugin;
@@ -1,40 +1,127 @@
1
- import z from 'zod';
2
- const handoffToolDefinitions = {
3
- handoff: {
4
- description: 'Transfer control to another agent. The target agent continues the task in this thread.',
1
+ import { z } from 'zod';
2
+ import { generateId } from 'melony';
3
+ import { runAgent } from '../../harness/index.js';
4
+ /**
5
+ * `delegation` — allows agents to delegate tasks to other agents.
6
+ *
7
+ * Only the 'system' agent is allowed to delegate by default.
8
+ * It uses runAgent to execute the delegated agent in its own isolated runtime,
9
+ * bridging events back to the caller's stream.
10
+ */
11
+ const delegationToolDefinitions = {
12
+ delegate_task: {
13
+ description: 'Delegate a specific task or question to another specialized agent.',
5
14
  inputSchema: z.object({
6
- agentId: z.string().describe('The ID of the target agent.'),
7
- content: z.string().describe('The message or task to hand off.'),
15
+ agentId: z.string().describe('The ID of the agent to delegate to (e.g., "researcher", "coder").'),
16
+ prompt: z.string().describe('The instructions or question for the delegated agent.'),
8
17
  }),
9
18
  },
10
19
  };
11
- const handoffPluginRuntime = () => (builder) => {
12
- builder.on('action:handoff', async function* (event, context) {
13
- const { agentId, content } = event.data;
14
- yield {
15
- type: 'agent:output',
16
- data: { content: `Handing off to **${agentId}**: ${content}` },
17
- meta: { ...(event.meta || {}), agentId: context.state.agentId },
18
- };
19
- yield {
20
- type: 'handoff:request',
21
- data: { agentId, content },
22
- meta: { ...(event.meta || {}), agentId: context.state.agentId },
23
- };
24
- if (event.meta?.toolCallId) {
25
- yield {
26
- type: 'action:handoff:result',
27
- data: { success: true, agentId, accepted: true },
28
- meta: { ...(event.meta || {}), agentId: context.state.agentId },
29
- };
30
- }
31
- });
32
- };
33
20
  export const delegationPlugin = {
34
21
  id: 'delegation',
35
- name: 'Handoff',
36
- description: 'Hand off tasks to other agents on the bus.',
37
- toolDefinitions: handoffToolDefinitions,
38
- factory: () => handoffPluginRuntime(),
22
+ name: 'Delegation',
23
+ description: 'Allows agents to call upon other agents to solve sub-tasks.',
24
+ toolDefinitions: delegationToolDefinitions,
25
+ factory: () => (builder) => {
26
+ // Handle the tool execution
27
+ builder.on('action:delegate_task', async function* (event, context) {
28
+ const delegateEvent = event;
29
+ // POLICY: Only the 'system' agent can delegate
30
+ if (context.state.agentId !== 'system') {
31
+ yield {
32
+ type: 'action:delegate_task:result',
33
+ data: {
34
+ success: false,
35
+ error: 'Only the system agent can delegate.'
36
+ },
37
+ meta: delegateEvent.meta,
38
+ };
39
+ return;
40
+ }
41
+ const { agentId, prompt } = delegateEvent.data;
42
+ const toolCallId = delegateEvent.meta?.toolCallId;
43
+ if (!toolCallId)
44
+ return;
45
+ const runId = `dg_${generateId()}`;
46
+ let lastAgentOutput = '';
47
+ // Queue to bridge the async onEvent callback to this generator
48
+ const eventQueue = [];
49
+ let resolveNext = null;
50
+ let isFinished = false;
51
+ // Start the delegated agent in its own runtime.
52
+ // We don't await this immediately so we can yield events as they arrive.
53
+ const runPromise = runAgent({
54
+ runId,
55
+ agentId,
56
+ event: {
57
+ type: 'agent:invoke',
58
+ data: {
59
+ role: 'user',
60
+ content: prompt,
61
+ agentId: agentId,
62
+ },
63
+ meta: {
64
+ threadId: context.state.threadId,
65
+ parentAgentId: context.state.agentId,
66
+ parentToolCallId: toolCallId,
67
+ },
68
+ },
69
+ channelId: context.state.channelId,
70
+ threadId: context.state.threadId,
71
+ onEvent: async (outEvent) => {
72
+ // Enrich events with parent metadata so the UI can track the hierarchy
73
+ const enrichedEvent = {
74
+ ...outEvent,
75
+ meta: {
76
+ ...outEvent.meta,
77
+ parentAgentId: context.state.agentId,
78
+ parentToolCallId: toolCallId,
79
+ }
80
+ };
81
+ eventQueue.push(enrichedEvent);
82
+ if (outEvent.type === 'agent:output') {
83
+ lastAgentOutput = outEvent.data.content;
84
+ }
85
+ // Wake up the generator loop if it's waiting
86
+ if (resolveNext) {
87
+ resolveNext();
88
+ resolveNext = null;
89
+ }
90
+ }
91
+ }).catch(error => {
92
+ console.error(`[delegation] Error in delegated run ${runId}:`, error);
93
+ }).finally(() => {
94
+ isFinished = true;
95
+ if (resolveNext) {
96
+ resolveNext();
97
+ resolveNext = null;
98
+ }
99
+ });
100
+ // Yield events from the delegated agent as they arrive
101
+ while (!isFinished || eventQueue.length > 0) {
102
+ if (eventQueue.length === 0) {
103
+ await new Promise(r => { resolveNext = r; });
104
+ }
105
+ while (eventQueue.length > 0) {
106
+ yield eventQueue.shift();
107
+ }
108
+ }
109
+ // Ensure the run is fully complete (though isFinished already implies this)
110
+ await runPromise;
111
+ // Yield the result back to our own LLM runtime.
112
+ yield {
113
+ type: 'action:delegate_task:result',
114
+ data: {
115
+ success: true,
116
+ output: lastAgentOutput,
117
+ },
118
+ meta: {
119
+ ...delegateEvent.meta,
120
+ agentId: context.state.agentId,
121
+ toolCallId: toolCallId,
122
+ },
123
+ };
124
+ });
125
+ },
39
126
  };
40
127
  export default delegationPlugin;
@@ -1,17 +1,31 @@
1
1
  import z from 'zod';
2
2
  /**
3
- * `memory` exposes the global memory store as agent tools.
4
- *
5
- * The actual handlers live in `bus/services.ts` because memory is platform
6
- * infrastructure (shared across every agent on the bus); this plugin only
7
- * contributes the tool definitions so a runtime plugin (e.g. `ai-sdk`) can
8
- * surface them to the LLM.
9
- *
10
- * Scopes
11
- * ------
12
- * - `global` (default) — visible to every agent and channel.
13
- * - `agent` — visible only to the agent that wrote it.
14
- * - `channel` — visible only inside the active channel.
3
+ * Resolve a scope alias to a concrete scope string. Aliases let tools accept
4
+ * `agent`/`channel`/`global` without knowing the active ids; the bus rewrites
5
+ * them using `context.state`.
6
+ */
7
+ function resolveMemoryScope(alias, state) {
8
+ switch (alias) {
9
+ case 'agent':
10
+ return `agent:${state.agentId}`;
11
+ case 'channel':
12
+ return `channel:${state.channelId}`;
13
+ case 'global':
14
+ case undefined:
15
+ return 'global';
16
+ default:
17
+ return 'global';
18
+ }
19
+ }
20
+ function resolveMemoryScopeFilter(alias, state) {
21
+ if (alias === 'all' || alias === undefined) {
22
+ return ['global', `agent:${state.agentId}`, `channel:${state.channelId}`];
23
+ }
24
+ return [resolveMemoryScope(alias, state)];
25
+ }
26
+ /**
27
+ * `memory` — exposes the global memory store as agent tools and provides
28
+ * platform-level memory handlers.
15
29
  */
16
30
  const memoryToolDefinitions = {
17
31
  remember: {
@@ -64,8 +78,83 @@ export const memoryPlugin = {
64
78
  name: 'Memory',
65
79
  description: 'Global long-term memory: remember/recall/forget facts across runs and agents.',
66
80
  toolDefinitions: memoryToolDefinitions,
67
- factory: () => () => {
68
- // Handlers live in bus/services.ts; this plugin only contributes tool definitions.
81
+ factory: ({ storage }) => (builder) => {
82
+ builder.on('action:remember', async function* (event, context) {
83
+ const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
84
+ try {
85
+ const { content, scope, tags } = event.data;
86
+ const record = await storage.appendMemory({
87
+ scope: resolveMemoryScope(scope, context.state),
88
+ content,
89
+ tags,
90
+ });
91
+ yield {
92
+ type: 'action:remember:result',
93
+ data: { success: true, record },
94
+ meta: resultMeta,
95
+ };
96
+ }
97
+ catch (error) {
98
+ yield {
99
+ type: 'action:remember:result',
100
+ data: {
101
+ success: false,
102
+ error: error instanceof Error ? error.message : 'Unknown error',
103
+ },
104
+ meta: resultMeta,
105
+ };
106
+ }
107
+ });
108
+ builder.on('action:recall', async function* (event, context) {
109
+ const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
110
+ try {
111
+ const { query, tag, scope, limit } = event.data;
112
+ const records = await storage.listMemories({
113
+ scopes: resolveMemoryScopeFilter(scope, context.state),
114
+ query,
115
+ tag,
116
+ limit,
117
+ });
118
+ yield {
119
+ type: 'action:recall:result',
120
+ data: { success: true, records },
121
+ meta: resultMeta,
122
+ };
123
+ }
124
+ catch (error) {
125
+ yield {
126
+ type: 'action:recall:result',
127
+ data: {
128
+ success: false,
129
+ records: [],
130
+ error: error instanceof Error ? error.message : 'Unknown error',
131
+ },
132
+ meta: resultMeta,
133
+ };
134
+ }
135
+ });
136
+ builder.on('action:forget', async function* (event, context) {
137
+ const resultMeta = { ...(event.meta || {}), agentId: context.state.agentId };
138
+ try {
139
+ const deleted = await storage.deleteMemory({ id: event.data.id });
140
+ yield {
141
+ type: 'action:forget:result',
142
+ data: { success: true, deleted },
143
+ meta: resultMeta,
144
+ };
145
+ }
146
+ catch (error) {
147
+ yield {
148
+ type: 'action:forget:result',
149
+ data: {
150
+ success: false,
151
+ deleted: false,
152
+ error: error instanceof Error ? error.message : 'Unknown error',
153
+ },
154
+ meta: resultMeta,
155
+ };
156
+ }
157
+ });
69
158
  },
70
159
  };
71
160
  export default memoryPlugin;