openbot 0.3.6 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -16
- package/dist/app/agent-ids.js +4 -0
- package/dist/app/cli.js +1 -1
- package/dist/app/config.js +10 -19
- package/dist/app/server.js +208 -17
- package/dist/bus/services.js +34 -124
- package/dist/harness/agent-invoke-run.js +44 -0
- package/dist/harness/agent-turn.js +99 -0
- package/dist/harness/channel-participants.js +40 -0
- package/dist/harness/constants.js +2 -0
- package/dist/harness/context-meter.js +97 -0
- package/dist/harness/context.js +95 -47
- package/dist/harness/dispatch.js +144 -0
- package/dist/harness/dispatcher.js +45 -156
- package/dist/harness/history.js +177 -0
- package/dist/harness/index.js +109 -0
- package/dist/harness/orchestration.js +88 -0
- package/dist/harness/participants.js +22 -0
- package/dist/harness/run-harness.js +154 -0
- package/dist/harness/run.js +98 -0
- package/dist/harness/runtime-factory.js +0 -34
- package/dist/harness/runtime.js +57 -0
- package/dist/harness/todo-dispatch.js +51 -0
- package/dist/harness/todos.js +5 -0
- package/dist/harness/turn.js +79 -0
- package/dist/plugins/approval/index.js +120 -149
- package/dist/plugins/bash/index.js +195 -0
- package/dist/plugins/delegation/index.js +121 -32
- package/dist/plugins/memory/index.js +103 -14
- package/dist/plugins/memory/service.js +152 -0
- package/dist/plugins/openbot/context.js +125 -0
- package/dist/plugins/openbot/history.js +144 -0
- package/dist/plugins/openbot/index.js +71 -0
- package/dist/plugins/openbot/runtime.js +381 -0
- package/dist/plugins/openbot/system-prompt.js +25 -0
- package/dist/plugins/plugin-manager/index.js +189 -0
- package/dist/plugins/shell/index.js +2 -1
- package/dist/plugins/storage/files.js +67 -0
- package/dist/plugins/storage/index.js +750 -0
- package/dist/plugins/storage/service.js +1316 -0
- package/dist/plugins/storage-tools/index.js +2 -2
- package/dist/plugins/thread-namer/index.js +72 -0
- package/dist/plugins/thread-naming/generate-title.js +44 -0
- package/dist/plugins/thread-naming/index.js +103 -0
- package/dist/plugins/threads/index.js +114 -0
- package/dist/plugins/todo/index.js +24 -25
- package/dist/plugins/ui/index.js +109 -180
- package/dist/registry/plugins.js +3 -9
- package/dist/services/abort.js +43 -0
- package/dist/services/plugins/domain.js +1 -0
- package/dist/services/plugins/plugin-cache.js +9 -0
- package/dist/services/plugins/registry.js +112 -0
- package/dist/services/plugins/service.js +232 -0
- package/dist/services/plugins/types.js +1 -0
- package/dist/services/process.js +29 -0
- package/dist/services/storage.js +11 -10
- package/dist/services/thread-naming.js +81 -0
- package/docs/agents.md +15 -12
- package/docs/architecture.md +2 -2
- package/docs/plugins.md +29 -17
- package/docs/templates/AGENT.example.md +8 -14
- package/package.json +1 -2
- package/src/app/agent-ids.ts +5 -0
- package/src/app/cli.ts +1 -1
- package/src/app/config.ts +14 -31
- package/src/app/server.ts +243 -19
- package/src/app/types.ts +331 -187
- package/src/harness/index.ts +166 -0
- package/src/plugins/approval/index.ts +107 -188
- package/src/plugins/bash/index.ts +232 -0
- package/src/plugins/delegation/index.ts +139 -39
- package/src/plugins/memory/index.ts +112 -15
- package/src/{services/memory.ts → plugins/memory/service.ts} +1 -1
- package/src/plugins/openbot/context.ts +140 -0
- package/src/plugins/openbot/history.ts +158 -0
- package/src/plugins/openbot/index.ts +79 -0
- package/src/plugins/openbot/runtime.ts +478 -0
- package/src/plugins/openbot/system-prompt.ts +27 -0
- package/src/plugins/plugin-manager/index.ts +224 -0
- package/src/plugins/storage/files.ts +81 -0
- package/src/plugins/storage/index.ts +823 -0
- package/src/{services/storage.ts → plugins/storage/service.ts} +485 -105
- package/src/plugins/ui/index.ts +117 -221
- package/src/services/abort.ts +46 -0
- package/src/{bus/types.ts → services/plugins/domain.ts} +50 -8
- package/src/services/plugins/plugin-cache.ts +13 -0
- package/src/{registry/plugins.ts → services/plugins/registry.ts} +28 -28
- package/src/services/plugins/service.ts +318 -0
- package/src/{bus/plugin.ts → services/plugins/types.ts} +7 -3
- package/src/bus/services.ts +0 -954
- package/src/harness/context.ts +0 -365
- package/src/harness/dispatcher.ts +0 -379
- package/src/harness/mcp.ts +0 -78
- package/src/harness/runtime-factory.ts +0 -129
- package/src/harness/todo-advance.ts +0 -128
- package/src/plugins/ai-sdk/index.ts +0 -41
- package/src/plugins/ai-sdk/runtime.ts +0 -468
- package/src/plugins/ai-sdk/system-prompt.ts +0 -18
- package/src/plugins/mcp/index.ts +0 -128
- package/src/plugins/shell/index.ts +0 -123
- package/src/plugins/storage-tools/index.ts +0 -90
- package/src/plugins/todo/index.ts +0 -64
- package/src/services/plugins.ts +0 -133
- /package/src/{harness → services}/process.ts +0 -0
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
import z from 'zod';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
import { DEFAULT_BASE_DIR, loadConfig, resolvePath } from '../../app/config.js';
|
|
5
|
+
const DEFAULT_LIMIT = 50;
|
|
6
|
+
const MAX_LIMIT = 500;
|
|
7
|
+
const getMemoryDir = () => {
|
|
8
|
+
const config = loadConfig();
|
|
9
|
+
return path.join(resolvePath(config.baseDir || DEFAULT_BASE_DIR), 'memory');
|
|
10
|
+
};
|
|
11
|
+
const getLogPath = () => path.join(getMemoryDir(), 'log.jsonl');
|
|
12
|
+
const ensureDir = async () => {
|
|
13
|
+
await fs.mkdir(getMemoryDir(), { recursive: true });
|
|
14
|
+
};
|
|
15
|
+
const readLog = async () => {
|
|
16
|
+
try {
|
|
17
|
+
const raw = await fs.readFile(getLogPath(), 'utf-8');
|
|
18
|
+
return raw
|
|
19
|
+
.split(/\r?\n/)
|
|
20
|
+
.map((line) => line.trim())
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.map((line) => {
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(line);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
.filter((e) => !!e);
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
if (e?.code === 'ENOENT')
|
|
34
|
+
return [];
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const replay = (entries) => {
|
|
39
|
+
const out = new Map();
|
|
40
|
+
for (const entry of entries) {
|
|
41
|
+
if (entry.op === 'add') {
|
|
42
|
+
out.set(entry.record.id, entry.record);
|
|
43
|
+
}
|
|
44
|
+
else if (entry.op === 'delete') {
|
|
45
|
+
out.delete(entry.id);
|
|
46
|
+
}
|
|
47
|
+
else if (entry.op === 'update') {
|
|
48
|
+
const existing = out.get(entry.id);
|
|
49
|
+
if (!existing)
|
|
50
|
+
continue;
|
|
51
|
+
out.set(entry.id, {
|
|
52
|
+
...existing,
|
|
53
|
+
...entry.patch,
|
|
54
|
+
id: existing.id,
|
|
55
|
+
updatedAt: entry.at,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
};
|
|
61
|
+
const appendEntry = async (entry) => {
|
|
62
|
+
await ensureDir();
|
|
63
|
+
await fs.appendFile(getLogPath(), `${JSON.stringify(entry)}\n`, 'utf-8');
|
|
64
|
+
};
|
|
65
|
+
const matchesQuery = (record, query, tag) => {
|
|
66
|
+
if (tag) {
|
|
67
|
+
if (!record.tags || !record.tags.includes(tag))
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (query) {
|
|
71
|
+
const q = query.toLowerCase();
|
|
72
|
+
if (!record.content.toLowerCase().includes(q))
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
};
|
|
77
|
+
export const memoryService = {
|
|
78
|
+
appendMemory: async (args) => {
|
|
79
|
+
const now = new Date().toISOString();
|
|
80
|
+
const record = {
|
|
81
|
+
id: crypto.randomUUID(),
|
|
82
|
+
scope: args.scope,
|
|
83
|
+
content: args.content,
|
|
84
|
+
tags: args.tags?.length ? args.tags : undefined,
|
|
85
|
+
createdAt: now,
|
|
86
|
+
updatedAt: now,
|
|
87
|
+
};
|
|
88
|
+
await appendEntry({ op: 'add', record });
|
|
89
|
+
return record;
|
|
90
|
+
},
|
|
91
|
+
updateMemory: async (args) => {
|
|
92
|
+
const entries = await readLog();
|
|
93
|
+
const map = replay(entries);
|
|
94
|
+
if (!map.has(args.id))
|
|
95
|
+
return false;
|
|
96
|
+
const at = new Date().toISOString();
|
|
97
|
+
const patch = {};
|
|
98
|
+
if (args.content !== undefined)
|
|
99
|
+
patch.content = args.content;
|
|
100
|
+
if (args.tags !== undefined)
|
|
101
|
+
patch.tags = args.tags.length ? args.tags : undefined;
|
|
102
|
+
if (Object.keys(patch).length === 0)
|
|
103
|
+
return true;
|
|
104
|
+
await appendEntry({ op: 'update', id: args.id, patch, at });
|
|
105
|
+
return true;
|
|
106
|
+
},
|
|
107
|
+
deleteMemory: async (args) => {
|
|
108
|
+
const entries = await readLog();
|
|
109
|
+
const map = replay(entries);
|
|
110
|
+
if (!map.has(args.id))
|
|
111
|
+
return false;
|
|
112
|
+
await appendEntry({ op: 'delete', id: args.id, at: new Date().toISOString() });
|
|
113
|
+
return true;
|
|
114
|
+
},
|
|
115
|
+
listMemories: async (args = {}) => {
|
|
116
|
+
const entries = await readLog();
|
|
117
|
+
const map = replay(entries);
|
|
118
|
+
const limit = Math.min(Math.max(args.limit ?? DEFAULT_LIMIT, 1), MAX_LIMIT);
|
|
119
|
+
const scopeSet = (() => {
|
|
120
|
+
if (args.scope)
|
|
121
|
+
return new Set([args.scope]);
|
|
122
|
+
if (args.scopes && args.scopes.length > 0)
|
|
123
|
+
return new Set(args.scopes);
|
|
124
|
+
return null;
|
|
125
|
+
})();
|
|
126
|
+
const filtered = [];
|
|
127
|
+
for (const record of map.values()) {
|
|
128
|
+
if (scopeSet && !scopeSet.has(record.scope))
|
|
129
|
+
continue;
|
|
130
|
+
if (!matchesQuery(record, args.query, args.tag))
|
|
131
|
+
continue;
|
|
132
|
+
filtered.push(record);
|
|
133
|
+
}
|
|
134
|
+
filtered.sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : -1));
|
|
135
|
+
return filtered.slice(0, limit);
|
|
136
|
+
},
|
|
137
|
+
/**
|
|
138
|
+
* Compact the log into a single `add` per surviving record. Cheap to call
|
|
139
|
+
* occasionally; not required for correctness.
|
|
140
|
+
*/
|
|
141
|
+
compact: async () => {
|
|
142
|
+
const entries = await readLog();
|
|
143
|
+
const map = replay(entries);
|
|
144
|
+
const surviving = Array.from(map.values());
|
|
145
|
+
await ensureDir();
|
|
146
|
+
const tmp = `${getLogPath()}.tmp`;
|
|
147
|
+
const body = surviving.map((record) => JSON.stringify({ op: 'add', record })).join('\n');
|
|
148
|
+
await fs.writeFile(tmp, body ? `${body}\n` : '', 'utf-8');
|
|
149
|
+
await fs.rename(tmp, getLogPath());
|
|
150
|
+
return surviving.length;
|
|
151
|
+
},
|
|
152
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { OPENBOT_SYSTEM_PROMPT } from './system-prompt.js';
|
|
2
|
+
export const DEFAULT_CONTEXT_BUDGET = 8000;
|
|
3
|
+
export const MAX_CONTEXT_FILES = 50;
|
|
4
|
+
/**
|
|
5
|
+
* Returns the known context window budget (in tokens) for a given model string.
|
|
6
|
+
*/
|
|
7
|
+
export const getContextBudgetForModel = (modelString) => {
|
|
8
|
+
const budgets = {
|
|
9
|
+
'openai/gpt-4o': 128000,
|
|
10
|
+
'openai/gpt-4o-mini': 128000,
|
|
11
|
+
'openai/o1-preview': 128000,
|
|
12
|
+
'openai/o1-mini': 128000,
|
|
13
|
+
'anthropic/claude-3-5-sonnet-20240620': 200000,
|
|
14
|
+
'anthropic/claude-3-5-sonnet-latest': 200000,
|
|
15
|
+
'anthropic/claude-3-opus-20240229': 200000,
|
|
16
|
+
'anthropic/claude-3-sonnet-20240229': 200000,
|
|
17
|
+
'anthropic/claude-3-haiku-20240307': 200000,
|
|
18
|
+
};
|
|
19
|
+
return budgets[modelString] || DEFAULT_CONTEXT_BUDGET;
|
|
20
|
+
};
|
|
21
|
+
/** Built-in orchestrator agent id. */
|
|
22
|
+
export const ORCHESTRATOR_AGENT_ID = 'system';
|
|
23
|
+
/**
|
|
24
|
+
* Check if a channel is a solo DM (only the agent is present).
|
|
25
|
+
*/
|
|
26
|
+
export function isDmSoloChannel(participants, agentId) {
|
|
27
|
+
return participants.length === 0 || (participants.length === 1 && participants[0] === agentId);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Simplified context builder for MVP.
|
|
31
|
+
*/
|
|
32
|
+
export async function buildContext(state, storage) {
|
|
33
|
+
const { channelId, threadId, channelDetails, agentId, threadDetails, agentDetails } = state;
|
|
34
|
+
const participants = channelDetails?.participants || [];
|
|
35
|
+
const isDm = isDmSoloChannel(participants, agentId);
|
|
36
|
+
const sections = [];
|
|
37
|
+
// Fetch agents once if storage is available
|
|
38
|
+
const allAgents = storage?.getAgents ? await storage.getAgents().catch(() => []) : [];
|
|
39
|
+
// 1. User
|
|
40
|
+
if (state.currentUser?.userName) {
|
|
41
|
+
sections.push(`## HUMAN\n- Name: ${state.currentUser.userName}`);
|
|
42
|
+
}
|
|
43
|
+
// 2. Environment
|
|
44
|
+
let env = '## ENVIRONMENT\n';
|
|
45
|
+
if (isDm) {
|
|
46
|
+
env += '- Mode: Direct Message (Solo)\n';
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const channelName = channelDetails?.name || channelId;
|
|
50
|
+
env += `- Mode: Channel (#${channelName})\n`;
|
|
51
|
+
if (channelDetails?.cwd) {
|
|
52
|
+
env += `- Workspace: ${channelDetails.cwd}\n`;
|
|
53
|
+
}
|
|
54
|
+
if (threadId) {
|
|
55
|
+
env += `- Thread: ${threadDetails?.name || threadId}\n`;
|
|
56
|
+
}
|
|
57
|
+
const peerIds = participants.filter((id) => id !== agentId);
|
|
58
|
+
const participantLabels = peerIds.map((id) => {
|
|
59
|
+
const agent = allAgents.find((a) => a.id === id);
|
|
60
|
+
return agent ? `${agent.name} (${id})` : id;
|
|
61
|
+
});
|
|
62
|
+
env += `- Participants: ${participantLabels.length > 0 ? participantLabels.join(', ') : 'None'}\n`;
|
|
63
|
+
}
|
|
64
|
+
sections.push(env);
|
|
65
|
+
// 2.5 Installed Agents
|
|
66
|
+
if (allAgents.length > 0) {
|
|
67
|
+
const formatted = allAgents
|
|
68
|
+
.map((a) => `- ${a.id}: ${a.name}${a.description ? ` - ${a.description}` : ''}`)
|
|
69
|
+
.join('\n');
|
|
70
|
+
sections.push(`## INSTALLED AGENTS\n${formatted}`);
|
|
71
|
+
}
|
|
72
|
+
// 3. Channel Spec
|
|
73
|
+
const spec = channelDetails?.spec?.trim();
|
|
74
|
+
if (spec) {
|
|
75
|
+
sections.push(`## CHANNEL SPECIFICATION\n${spec}`);
|
|
76
|
+
}
|
|
77
|
+
// 4. Files
|
|
78
|
+
if (storage?.listFiles && channelId && channelDetails?.cwd) {
|
|
79
|
+
try {
|
|
80
|
+
const files = await storage.listFiles({ channelId });
|
|
81
|
+
if (files.length > 0) {
|
|
82
|
+
const limited = files.slice(0, MAX_CONTEXT_FILES);
|
|
83
|
+
const formatted = limited
|
|
84
|
+
.map((f) => `- ${f.name}${f.isDirectory ? '/' : ''}`)
|
|
85
|
+
.join('\n');
|
|
86
|
+
let fileSection = `## FILES\n${formatted}`;
|
|
87
|
+
if (files.length > MAX_CONTEXT_FILES) {
|
|
88
|
+
fileSection += `\n- ... and ${files.length - MAX_CONTEXT_FILES} more files`;
|
|
89
|
+
}
|
|
90
|
+
sections.push(fileSection);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
sections.push('## FILES\n- (No files in workspace)');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.warn('[context] Failed to fetch files:', error);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// 5. Agent Instructions
|
|
101
|
+
const rawInstructions = agentDetails?.instructions?.trim();
|
|
102
|
+
if (rawInstructions &&
|
|
103
|
+
rawInstructions !== OPENBOT_SYSTEM_PROMPT.trim()) {
|
|
104
|
+
sections.push(`## Instructions\n${rawInstructions}`);
|
|
105
|
+
}
|
|
106
|
+
// 6. Memories
|
|
107
|
+
if (storage?.listMemories) {
|
|
108
|
+
try {
|
|
109
|
+
const scopes = ['global', `agent:${agentId}`];
|
|
110
|
+
if (channelId)
|
|
111
|
+
scopes.push(`channel:${channelId}`);
|
|
112
|
+
const records = await storage.listMemories({ scopes, limit: 20 });
|
|
113
|
+
if (records.length > 0) {
|
|
114
|
+
const formatted = records
|
|
115
|
+
.map((r) => `- (${r.scope}) ${r.content}`)
|
|
116
|
+
.join('\n');
|
|
117
|
+
sections.push(`## MEMORIES\n${formatted}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
console.warn('[context] Failed to fetch memories:', error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return sections.join('\n\n');
|
|
125
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensures every tool-call has a matching tool-result before calling the LLM.
|
|
3
|
+
* Orphaned calls (interrupted run, missing :result event, etc.) get an empty
|
|
4
|
+
* result so the conversation can resume instead of failing validation.
|
|
5
|
+
*/
|
|
6
|
+
function fillMissingToolResults(messages) {
|
|
7
|
+
const filled = [];
|
|
8
|
+
const pending = new Map();
|
|
9
|
+
const flushPending = () => {
|
|
10
|
+
if (pending.size === 0)
|
|
11
|
+
return;
|
|
12
|
+
filled.push({
|
|
13
|
+
role: 'tool',
|
|
14
|
+
content: [...pending.entries()].map(([toolCallId, toolName]) => ({
|
|
15
|
+
type: 'tool-result',
|
|
16
|
+
toolCallId,
|
|
17
|
+
toolName,
|
|
18
|
+
output: { type: 'text', value: '' },
|
|
19
|
+
})),
|
|
20
|
+
});
|
|
21
|
+
pending.clear();
|
|
22
|
+
};
|
|
23
|
+
for (const message of messages) {
|
|
24
|
+
if (message.role === 'tool' && Array.isArray(message.content)) {
|
|
25
|
+
filled.push(message);
|
|
26
|
+
for (const part of message.content) {
|
|
27
|
+
if (part.type === 'tool-result') {
|
|
28
|
+
pending.delete(part.toolCallId);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
flushPending();
|
|
34
|
+
filled.push(message);
|
|
35
|
+
if (message.role === 'assistant' && Array.isArray(message.content)) {
|
|
36
|
+
for (const part of message.content) {
|
|
37
|
+
if (part.type === 'tool-call') {
|
|
38
|
+
const toolCall = part;
|
|
39
|
+
pending.set(toolCall.toolCallId, toolCall.toolName);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
flushPending();
|
|
45
|
+
return filled;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Converts a raw event log into a valid chain of ModelMessages for the AI SDK.
|
|
49
|
+
*
|
|
50
|
+
* This is a basic implementation that maps events to messages and filters out
|
|
51
|
+
* events from sub-processes (delegation) to avoid duplication in history.
|
|
52
|
+
*/
|
|
53
|
+
export function eventsToModelMessages(events) {
|
|
54
|
+
const messages = [];
|
|
55
|
+
for (const event of events) {
|
|
56
|
+
// Skip events that belong to a sub-process (like delegation)
|
|
57
|
+
// so they don't pollute the main conversation history.
|
|
58
|
+
if (event.meta?.parentToolCallId) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
switch (event.type) {
|
|
62
|
+
case 'agent:output': {
|
|
63
|
+
const last = messages[messages.length - 1];
|
|
64
|
+
if (last && last.role === 'assistant' && typeof last.content === 'string') {
|
|
65
|
+
last.content += event.data.content;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
messages.push({ role: 'assistant', content: event.data.content });
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 'agent:invoke': {
|
|
73
|
+
const invokeEvent = event;
|
|
74
|
+
if (invokeEvent.data?.content && invokeEvent.data?.role) {
|
|
75
|
+
messages.push({
|
|
76
|
+
role: invokeEvent.data.role,
|
|
77
|
+
content: invokeEvent.data.content
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
default:
|
|
83
|
+
// Handle tool calls (action:*)
|
|
84
|
+
if (event.type.startsWith('action:') && !event.type.endsWith(':result')) {
|
|
85
|
+
const toolName = event.type.slice(7);
|
|
86
|
+
const toolCallId = event.meta?.toolCallId;
|
|
87
|
+
if (!toolCallId)
|
|
88
|
+
break;
|
|
89
|
+
const toolCall = {
|
|
90
|
+
type: 'tool-call',
|
|
91
|
+
toolCallId,
|
|
92
|
+
toolName,
|
|
93
|
+
input: event.data,
|
|
94
|
+
};
|
|
95
|
+
const last = messages[messages.length - 1];
|
|
96
|
+
if (last && last.role === 'assistant') {
|
|
97
|
+
if (typeof last.content === 'string') {
|
|
98
|
+
last.content = [
|
|
99
|
+
{ type: 'text', text: last.content },
|
|
100
|
+
toolCall,
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
else if (Array.isArray(last.content)) {
|
|
104
|
+
last.content.push(toolCall);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
messages.push({
|
|
109
|
+
role: 'assistant',
|
|
110
|
+
content: [toolCall],
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// Handle tool results (action:*:result)
|
|
115
|
+
else if (event.type.startsWith('action:') && event.type.endsWith(':result')) {
|
|
116
|
+
const toolName = event.type.slice(7, -7);
|
|
117
|
+
const toolCallId = event.meta?.toolCallId;
|
|
118
|
+
if (!toolCallId)
|
|
119
|
+
break;
|
|
120
|
+
const toolResult = {
|
|
121
|
+
type: 'tool-result',
|
|
122
|
+
toolCallId,
|
|
123
|
+
toolName,
|
|
124
|
+
output: {
|
|
125
|
+
type: 'text',
|
|
126
|
+
value: event?.data?.output || "No output", // ?.output is from delegation result
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
const last = messages[messages.length - 1];
|
|
130
|
+
if (last && last.role === 'tool' && Array.isArray(last.content)) {
|
|
131
|
+
last.content.push(toolResult);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
messages.push({
|
|
135
|
+
role: 'tool',
|
|
136
|
+
content: [toolResult],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return fillMissingToolResults(messages);
|
|
144
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { openbotRuntime } from './runtime.js';
|
|
2
|
+
import { bashPlugin } from '../bash/index.js';
|
|
3
|
+
import { memoryPlugin } from '../memory/index.js';
|
|
4
|
+
import { approvalPlugin } from '../approval/index.js';
|
|
5
|
+
import { storagePlugin } from '../storage/index.js';
|
|
6
|
+
import { delegationPlugin } from '../delegation/index.js';
|
|
7
|
+
import { uiPlugin } from '../ui/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* `openbot` — the standard, opinionated OpenBot agent runtime.
|
|
10
|
+
*
|
|
11
|
+
* This is the canonical execution loop for OpenBot agents. It handles
|
|
12
|
+
* `agent:invoke`, manages short-term memory, assembles context, and
|
|
13
|
+
* orchestrates tool calls.
|
|
14
|
+
*
|
|
15
|
+
* It comes with a "batteries-included" set of inbuilt tools: bash, memory,
|
|
16
|
+
* storage, delegation, and approval.
|
|
17
|
+
*/
|
|
18
|
+
export const openbotPlugin = {
|
|
19
|
+
id: 'openbot',
|
|
20
|
+
name: 'OpenBot Agent',
|
|
21
|
+
description: 'The standard OpenBot agent runtime with inbuilt tools (bash, memory, storage, delegation, and approval).',
|
|
22
|
+
configSchema: {
|
|
23
|
+
type: 'object',
|
|
24
|
+
properties: {
|
|
25
|
+
model: {
|
|
26
|
+
type: 'string',
|
|
27
|
+
description: 'Provider model string, e.g. openai/gpt-4o-mini, anthropic/claude-3-5-sonnet-20240620',
|
|
28
|
+
default: 'openai/gpt-4o-mini',
|
|
29
|
+
},
|
|
30
|
+
approval: {
|
|
31
|
+
type: 'object',
|
|
32
|
+
description: 'Configuration for the inbuilt approval plugin.',
|
|
33
|
+
properties: {
|
|
34
|
+
actions: {
|
|
35
|
+
type: 'array',
|
|
36
|
+
items: { type: 'string' },
|
|
37
|
+
description: 'List of actions that require manual approval.',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
toolDefinitions: {
|
|
44
|
+
...bashPlugin.toolDefinitions,
|
|
45
|
+
...memoryPlugin.toolDefinitions,
|
|
46
|
+
...storagePlugin.toolDefinitions,
|
|
47
|
+
...delegationPlugin.toolDefinitions,
|
|
48
|
+
...uiPlugin.toolDefinitions,
|
|
49
|
+
},
|
|
50
|
+
factory: (context) => (builder) => {
|
|
51
|
+
const { config, storage, tools, abortSignal } = context;
|
|
52
|
+
// Register inbuilt plugins
|
|
53
|
+
bashPlugin.factory(context)(builder);
|
|
54
|
+
memoryPlugin.factory(context)(builder);
|
|
55
|
+
storagePlugin.factory(context)(builder);
|
|
56
|
+
delegationPlugin.factory(context)(builder);
|
|
57
|
+
uiPlugin.factory(context)(builder);
|
|
58
|
+
// Approval plugin configuration
|
|
59
|
+
const approvalConfig = config?.approval || {
|
|
60
|
+
actions: ['action:bash', 'action:create_channel', 'action:delete_channel'],
|
|
61
|
+
};
|
|
62
|
+
approvalPlugin.factory({ ...context, config: approvalConfig })(builder);
|
|
63
|
+
return openbotRuntime({
|
|
64
|
+
model: config?.model,
|
|
65
|
+
storage,
|
|
66
|
+
toolDefinitions: tools,
|
|
67
|
+
abortSignal,
|
|
68
|
+
})(builder);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
export default openbotPlugin;
|