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,356 +0,0 @@
1
- import { OpenBotEvent, OpenBotState, TodoItem } from '../app/types.js';
2
- import { Storage } from '../bus/types.js';
3
-
4
- /**
5
- * Represents a piece of context that can be used in a prompt.
6
- *
7
- * Items flow through the engine in two phases:
8
- * 1. Each registered `ContextProvider` emits zero or more items.
9
- * 2. Each registered `ContextProcessor` may transform / drop / re-rank
10
- * items (e.g. token-budget enforcement).
11
- *
12
- * Higher `priority` items appear first in the assembled prompt and are the
13
- * last to be dropped under budget pressure.
14
- */
15
- export interface ContextItem {
16
- id: string;
17
- type: string;
18
- priority: number;
19
- content: string;
20
- metadata?: Record<string, any>;
21
- }
22
-
23
- export interface ContextProvider {
24
- name: string;
25
- provide(state: OpenBotState, storage?: Storage): Promise<ContextItem[]>;
26
- }
27
-
28
- export interface ContextProcessor {
29
- name: string;
30
- process(items: ContextItem[], state: OpenBotState): Promise<ContextItem[]>;
31
- }
32
-
33
- /**
34
- * Cheap, dependency-free token estimator. Roughly char/4 — fine for budget
35
- * enforcement; can be swapped for a tokenizer-backed implementation later
36
- * without touching providers.
37
- */
38
- export const estimateTokens = (text: string): number =>
39
- Math.ceil((text?.length ?? 0) / 4);
40
-
41
- /**
42
- * Hard cap (in characters) on a single context item. Keeps any one provider
43
- * — typically the recent-events feed — from monopolising the prompt budget.
44
- */
45
- const ITEM_HARD_CHAR_CAP = 6000;
46
-
47
- const truncate = (text: string, maxChars: number): string =>
48
- text.length <= maxChars ? text : `${text.slice(0, maxChars)}\n…[truncated]`;
49
-
50
- export class ContextEngine {
51
- private providers: ContextProvider[] = [];
52
- private processors: ContextProcessor[] = [];
53
-
54
- registerProvider(provider: ContextProvider) {
55
- this.providers.push(provider);
56
- }
57
-
58
- registerProcessor(processor: ContextProcessor) {
59
- this.processors.push(processor);
60
- }
61
-
62
- async buildContext(state: OpenBotState, storage?: Storage): Promise<string> {
63
- let items: ContextItem[] = [];
64
- for (const provider of this.providers) {
65
- try {
66
- const providedItems = await provider.provide(state, storage);
67
- for (const item of providedItems) {
68
- items.push({ ...item, content: truncate(item.content, ITEM_HARD_CHAR_CAP) });
69
- }
70
- } catch (error) {
71
- console.warn(`[ContextEngine] Provider ${provider.name} failed:`, error);
72
- }
73
- }
74
-
75
- for (const processor of this.processors) {
76
- try {
77
- items = await processor.process(items, state);
78
- } catch (error) {
79
- console.warn(`[ContextEngine] Processor ${processor.name} failed:`, error);
80
- }
81
- }
82
-
83
- return items
84
- .sort((a, b) => b.priority - a.priority)
85
- .map((item) => item.content)
86
- .join('\n\n');
87
- }
88
- }
89
-
90
- /**
91
- * Default context engine. Order of providers is by emit order; final ordering
92
- * in the prompt is determined by `priority`. The token-budget processor runs
93
- * last so dropping happens after every provider has contributed.
94
- */
95
- export function createDefaultContextEngine(): ContextEngine {
96
- const engine = new ContextEngine();
97
-
98
- engine.registerProvider(new AgentDetailsProvider());
99
- engine.registerProvider(new ChannelDetailsProvider());
100
- engine.registerProvider(new ThreadDetailsProvider());
101
- engine.registerProvider(new TodoProvider());
102
- engine.registerProvider(new MemoryProvider());
103
- engine.registerProvider(new RecentEventsProvider());
104
-
105
- engine.registerProcessor(new TokenBudgetProcessor());
106
-
107
- return engine;
108
- }
109
-
110
- class AgentDetailsProvider implements ContextProvider {
111
- name = 'agent-details';
112
- async provide(state: OpenBotState): Promise<ContextItem[]> {
113
- if (!state.agentDetails) return [];
114
- const instructions = state.agentDetails.instructions?.trim();
115
- if (!instructions) return [];
116
-
117
- return [{
118
- id: 'agent-details',
119
- type: 'agent',
120
- priority: 100,
121
- content: `# ${state.agentDetails.name}\n\n${instructions}`,
122
- }];
123
- }
124
- }
125
-
126
- class ChannelDetailsProvider implements ContextProvider {
127
- name = 'channel-details';
128
- async provide(state: OpenBotState): Promise<ContextItem[]> {
129
- if (!state.channelDetails) return [];
130
- const spec = state.channelDetails.spec?.trim();
131
- if (!spec) return [];
132
-
133
- return [{
134
- id: 'channel-details',
135
- type: 'channel',
136
- priority: 80,
137
- content: `# Channel you are in: ${state.channelDetails.name}\n\n Channel Specification: ${spec}`,
138
- }];
139
- }
140
- }
141
-
142
- class ThreadDetailsProvider implements ContextProvider {
143
- name = 'thread-details';
144
- async provide(state: OpenBotState): Promise<ContextItem[]> {
145
- if (!state.threadDetails) return [];
146
-
147
- // For now, this provider is a placeholder for future state-based assembly.
148
- // It currently only surfaces the thread name to provide basic context.
149
- return [
150
- {
151
- id: 'thread-details',
152
- type: 'thread',
153
- priority: 90,
154
- content: `# Thread you are in: ${state.threadDetails.name}`,
155
- },
156
- ];
157
- }
158
- }
159
-
160
- /**
161
- * Surfaces the shared per-thread todo list. The list lives in
162
- * `threadDetails.state.todos` and is owned by bus services — every agent in
163
- * the thread reads from the same canonical source, which is how multi-agent
164
- * autonomous flows stay coordinated.
165
- */
166
- class TodoProvider implements ContextProvider {
167
- name = 'todos';
168
- async provide(state: OpenBotState): Promise<ContextItem[]> {
169
- const raw = (state.threadDetails?.state as Record<string, unknown> | undefined)?.todos;
170
- const todos: TodoItem[] = Array.isArray(raw) ? (raw as TodoItem[]) : [];
171
- if (todos.length === 0) return [];
172
-
173
- const DISPLAY_RESULT_CAP = 2500;
174
-
175
- const marker: Record<TodoItem['status'], string> = {
176
- pending: '[ ]',
177
- in_progress: '[~]',
178
- done: '[x]',
179
- cancelled: '[-]',
180
- };
181
- const formatted = todos
182
- .map((t) => {
183
- const assignee = t.assignee ? ` @${t.assignee}` : '';
184
- let line = `- ${marker[t.status]} (${t.id})${assignee} ${t.content}`;
185
- if (t.status === 'done' && t.result?.trim()) {
186
- let snippet = t.result.trim();
187
- if (snippet.length > DISPLAY_RESULT_CAP) {
188
- snippet = `${snippet.slice(0, DISPLAY_RESULT_CAP)}…[truncated]`;
189
- }
190
- line += `\n Result: ${snippet}`;
191
- }
192
- return line;
193
- })
194
- .join('\n');
195
-
196
- return [
197
- {
198
- id: 'todos',
199
- type: 'todos',
200
- priority: 92,
201
- content:
202
- `## Shared todo plan (thread state)\n` +
203
- `Orchestrator authors with \`todo_write\`; assignees run one step at a time. ` +
204
- `When an item is \`done\`, its captured output appears below so every agent can see prior steps without relying on merged chat history.\n\n` +
205
- `${formatted}`,
206
- },
207
- ];
208
- }
209
- }
210
-
211
- /**
212
- * Fetches relevant memories (global + active agent + active channel) and
213
- * surfaces them at high priority so the LLM treats them as ground truth
214
- * rather than chat history.
215
- */
216
- class MemoryProvider implements ContextProvider {
217
- name = 'memory';
218
- async provide(state: OpenBotState, storage?: Storage): Promise<ContextItem[]> {
219
- if (!storage?.listMemories) return [];
220
-
221
- try {
222
- const scopes = ['global', `agent:${state.agentId}`];
223
- if (state.channelId) scopes.push(`channel:${state.channelId}`);
224
-
225
- const records = await storage.listMemories({ scopes, limit: 50 });
226
- if (records.length === 0) return [];
227
-
228
- const formatted = records
229
- .map((r) => {
230
- const tags = r.tags?.length ? ` [${r.tags.join(', ')}]` : '';
231
- const scopeLabel = r.scope === 'global' ? 'global' : r.scope;
232
- return `- (${scopeLabel}${tags}) ${r.content}`;
233
- })
234
- .join('\n');
235
-
236
- return [
237
- {
238
- id: 'memory',
239
- type: 'memory',
240
- priority: 95,
241
- content: `## Remembered facts\nTrust these unless the user contradicts them. Use \`forget\` to remove stale ones.\n\n${formatted}`,
242
- },
243
- ];
244
- } catch (error) {
245
- console.warn('[ContextEngine] MemoryProvider failed:', error);
246
- return [];
247
- }
248
- }
249
- }
250
-
251
- /**
252
- * Event types we omit from the recent-events context block. They duplicate
253
- * information already in the conversation history, are infrastructural
254
- * noise, or are too large to be useful as a tail summary.
255
- */
256
- const NOISY_EVENT_PREFIXES = [
257
- 'agent:invoke',
258
- 'agent:output',
259
- 'agent:run',
260
- 'agent:active-runs',
261
- 'client:ui',
262
- 'stream:',
263
- 'action:storage:get-',
264
- 'action:storage:patch-',
265
- ];
266
-
267
- const MAX_RECENT_EVENTS = 20;
268
- const MAX_EVENT_DATA_CHARS = 300;
269
-
270
- const isNoisyEvent = (event: OpenBotEvent): boolean =>
271
- NOISY_EVENT_PREFIXES.some((prefix) => event.type.startsWith(prefix));
272
-
273
- const summarizeEvent = (event: OpenBotEvent): string => {
274
- const data = (event as { data?: unknown }).data;
275
- if (data === undefined) return `- ${event.type}`;
276
- let payload: string;
277
- try {
278
- payload = typeof data === 'string' ? data : JSON.stringify(data);
279
- } catch {
280
- payload = '[unserialisable]';
281
- }
282
- if (payload.length > MAX_EVENT_DATA_CHARS) {
283
- payload = `${payload.slice(0, MAX_EVENT_DATA_CHARS)}…`;
284
- }
285
- return `- ${event.type}: ${payload}`;
286
- };
287
-
288
- class RecentEventsProvider implements ContextProvider {
289
- name = 'recent-events';
290
- async provide(state: OpenBotState, storage?: Storage): Promise<ContextItem[]> {
291
- if (!storage) return [];
292
-
293
- const channelId = state.channelId;
294
- const threadId = state.threadId;
295
-
296
- try {
297
- const events = await storage.getEvents({ channelId, threadId });
298
- const filtered = events.filter((e) => !isNoisyEvent(e));
299
- if (filtered.length === 0) return [];
300
-
301
- const formatted = filtered.slice(-MAX_RECENT_EVENTS).map(summarizeEvent).join('\n');
302
-
303
- return [
304
- {
305
- id: threadId ? 'thread-events' : 'channel-events',
306
- type: 'events',
307
- priority: 70,
308
- content: `## ${threadId ? 'THREAD' : 'CHANNEL'} RECENT ACTIVITIES (events)\n${formatted}`,
309
- },
310
- ];
311
- } catch (error) {
312
- console.warn('[ContextEngine] Failed to fetch events:', error);
313
- return [];
314
- }
315
- }
316
- }
317
-
318
- /**
319
- * Drops the lowest-priority items until the assembled prompt fits within the
320
- * token budget. The first item with priority >= `keepFloor` is always kept,
321
- * so the agent's own instructions can never be evicted. Stable on ties:
322
- * later-emitted items go first.
323
- */
324
- export class TokenBudgetProcessor implements ContextProcessor {
325
- name = 'token-budget';
326
- /** Soft prompt budget in tokens (matches gpt-4o-mini's reasonable system slice). */
327
- static DEFAULT_BUDGET = 8000;
328
- /** Items at or above this priority are never dropped. */
329
- static KEEP_FLOOR = 100;
330
-
331
- constructor(
332
- private budget: number = TokenBudgetProcessor.DEFAULT_BUDGET,
333
- private keepFloor: number = TokenBudgetProcessor.KEEP_FLOOR,
334
- ) {}
335
-
336
- async process(items: ContextItem[]): Promise<ContextItem[]> {
337
- const sorted = [...items].sort((a, b) => b.priority - a.priority);
338
- const out: ContextItem[] = [];
339
- let used = 0;
340
-
341
- for (const item of sorted) {
342
- const cost = estimateTokens(item.content);
343
- if (item.priority >= this.keepFloor) {
344
- out.push(item);
345
- used += cost;
346
- continue;
347
- }
348
- if (used + cost <= this.budget) {
349
- out.push(item);
350
- used += cost;
351
- }
352
- }
353
-
354
- return out;
355
- }
356
- }