openbot 0.2.14 → 0.3.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.
- package/dist/agents/openbot/index.js +76 -0
- package/dist/agents/openbot/middleware/approval.js +132 -0
- package/dist/agents/openbot/runtime.js +289 -0
- package/dist/agents/openbot/system-prompt.js +32 -0
- package/dist/agents/openbot/tools/delegation.js +78 -0
- package/dist/agents/openbot/tools/mcp.js +99 -0
- package/dist/agents/openbot/tools/shell.js +91 -0
- package/dist/agents/openbot/tools/storage.js +75 -0
- package/dist/agents/openbot/tools/ui.js +176 -0
- package/dist/agents/system.js +20 -93
- package/dist/app/cli.js +0 -0
- package/dist/app/config.js +4 -1
- package/dist/app/server.js +15 -8
- package/dist/bus/agent-package.js +1 -0
- package/dist/bus/plugin.js +1 -0
- package/dist/bus/services.js +600 -0
- package/dist/bus/types.js +1 -0
- package/dist/harness/context.js +131 -0
- package/dist/harness/event-normalizer.js +59 -0
- package/dist/harness/orchestrator.js +27 -227
- package/dist/harness/process.js +25 -3
- package/dist/harness/queue-processor.js +227 -0
- package/dist/harness/runtime-factory.js +103 -0
- package/dist/plugins/ai-sdk/index.js +37 -0
- package/dist/plugins/ai-sdk/runtime.js +330 -0
- package/dist/plugins/ai-sdk/system-prompt.js +3 -0
- package/dist/plugins/ai-sdk.js +277 -87
- package/dist/plugins/approval/index.js +159 -0
- package/dist/plugins/approval.js +163 -0
- package/dist/plugins/delegation/index.js +79 -0
- package/dist/plugins/delegation.js +67 -11
- package/dist/plugins/mcp/index.js +108 -0
- package/dist/plugins/shell/index.js +99 -0
- package/dist/plugins/shell.js +123 -0
- package/dist/plugins/storage-tools/index.js +85 -0
- package/dist/plugins/storage.js +240 -5
- package/dist/plugins/ui/index.js +184 -0
- package/dist/plugins/ui.js +185 -21
- package/dist/registry/agents.js +138 -0
- package/dist/registry/plugins.js +91 -50
- package/dist/services/agent-packages.js +103 -0
- package/dist/services/plugins.js +98 -0
- package/dist/services/storage.js +360 -94
- package/docs/agents.md +39 -66
- package/docs/architecture.md +1 -1
- package/docs/plugins.md +70 -58
- package/docs/templates/AGENT.example.md +57 -0
- package/package.json +8 -7
- package/src/app/cli.ts +1 -1
- package/src/app/config.ts +14 -4
- package/src/app/server.ts +23 -10
- package/src/app/types.ts +385 -16
- package/src/assets/icon.svg +4 -1
- package/src/bus/plugin.ts +67 -0
- package/src/bus/services.ts +666 -0
- package/src/bus/types.ts +147 -0
- package/src/harness/context.ts +160 -0
- package/src/harness/event-normalizer.ts +82 -0
- package/src/harness/orchestrator.ts +35 -273
- package/src/harness/process.ts +28 -4
- package/src/harness/queue-processor.ts +309 -0
- package/src/harness/runtime-factory.ts +125 -0
- package/src/plugins/ai-sdk/index.ts +44 -0
- package/src/plugins/ai-sdk/runtime.ts +410 -0
- package/src/plugins/ai-sdk/system-prompt.ts +4 -0
- package/src/plugins/approval/index.ts +228 -0
- package/src/plugins/delegation/index.ts +94 -0
- package/src/plugins/mcp/index.ts +128 -0
- package/src/plugins/shell/index.ts +123 -0
- package/src/plugins/storage-tools/index.ts +101 -0
- package/src/plugins/ui/index.ts +227 -0
- package/src/registry/plugins.ts +106 -55
- package/src/services/plugins.ts +133 -0
- package/src/services/storage.ts +465 -137
- package/src/agents/system.ts +0 -112
- package/src/plugins/ai-sdk.ts +0 -197
- package/src/plugins/delegation.ts +0 -60
- package/src/plugins/mcp.ts +0 -154
- package/src/plugins/storage.ts +0 -725
- package/src/plugins/ui.ts +0 -57
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { generateText } from 'ai';
|
|
2
|
+
import { openai } from '@ai-sdk/openai';
|
|
3
|
+
import { anthropic } from '@ai-sdk/anthropic';
|
|
4
|
+
import { createDefaultContextEngine } from '../../harness/context.js';
|
|
5
|
+
import { saveConfig } from '../../app/config.js';
|
|
6
|
+
function resolveModel(modelString) {
|
|
7
|
+
const [provider, ...rest] = modelString.split('/');
|
|
8
|
+
const modelId = rest.join('/');
|
|
9
|
+
if (!modelId) {
|
|
10
|
+
throw new Error(`Invalid model string: "${modelString}". Expected "provider/model-id".`);
|
|
11
|
+
}
|
|
12
|
+
switch (provider) {
|
|
13
|
+
case 'openai':
|
|
14
|
+
return openai(modelId);
|
|
15
|
+
case 'anthropic':
|
|
16
|
+
return anthropic(modelId);
|
|
17
|
+
default:
|
|
18
|
+
throw new Error(`Unsupported AI provider: "${provider}"`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const asRecord = (value) => value && typeof value === 'object' && !Array.isArray(value)
|
|
22
|
+
? value
|
|
23
|
+
: {};
|
|
24
|
+
const readPersistedShortTermMessages = (state) => {
|
|
25
|
+
const source = state.threadDetails?.state ?? state.channelDetails?.state;
|
|
26
|
+
const record = asRecord(source);
|
|
27
|
+
const raw = record.shortTermMessages;
|
|
28
|
+
return Array.isArray(raw) ? raw : [];
|
|
29
|
+
};
|
|
30
|
+
const persistShortTermMessages = async (state, storage) => {
|
|
31
|
+
if (!storage)
|
|
32
|
+
return;
|
|
33
|
+
const shortTermMessages = state.shortTermMessages ?? [];
|
|
34
|
+
if (state.threadId) {
|
|
35
|
+
await storage.patchThreadState({
|
|
36
|
+
channelId: state.channelId,
|
|
37
|
+
threadId: state.threadId,
|
|
38
|
+
state: { shortTermMessages },
|
|
39
|
+
});
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
await storage.patchChannelState({
|
|
43
|
+
channelId: state.channelId,
|
|
44
|
+
state: { shortTermMessages },
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
async function buildSystemPrompt(state, system, context, storage, contextEngine) {
|
|
48
|
+
const sections = [];
|
|
49
|
+
if (system && typeof system === 'string')
|
|
50
|
+
sections.push(system);
|
|
51
|
+
if (system && typeof system === 'function' && context)
|
|
52
|
+
sections.push(await system(context));
|
|
53
|
+
if (contextEngine)
|
|
54
|
+
sections.push(await contextEngine.buildContext(state, storage));
|
|
55
|
+
return sections.join('\n\n');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Generic ai-sdk runtime plugin.
|
|
59
|
+
*
|
|
60
|
+
* Owns `agent:invoke`, runs the LLM, emits tool-call events, and stitches tool
|
|
61
|
+
* results back into the conversation. Tools are supplied externally by the
|
|
62
|
+
* loader (merged from every tool plugin attached to the same agent).
|
|
63
|
+
*/
|
|
64
|
+
export const aiSdkRuntime = (options) => (builder) => {
|
|
65
|
+
const { model: modelString = 'openai/gpt-4o-mini', system, storage, contextEngine = createDefaultContextEngine(), toolDefinitions = {}, } = options;
|
|
66
|
+
let currentModelString = modelString;
|
|
67
|
+
let model = resolveModel(currentModelString);
|
|
68
|
+
const ensureShortTermMessages = (state) => {
|
|
69
|
+
if (!state.shortTermMessages || state.shortTermMessages.length === 0) {
|
|
70
|
+
state.shortTermMessages = readPersistedShortTermMessages(state);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const mapToCoreMessages = (messages) => {
|
|
74
|
+
return messages.map((m) => {
|
|
75
|
+
if (m.role === 'assistant' && m.toolCalls) {
|
|
76
|
+
return {
|
|
77
|
+
role: 'assistant',
|
|
78
|
+
content: [
|
|
79
|
+
{ type: 'text', text: m.content || '' },
|
|
80
|
+
...m.toolCalls.map((tc) => ({
|
|
81
|
+
type: 'tool-call',
|
|
82
|
+
toolCallId: tc.id,
|
|
83
|
+
toolName: tc.function.name,
|
|
84
|
+
input: JSON.parse(tc.function.arguments),
|
|
85
|
+
})),
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (m.role === 'assistant') {
|
|
90
|
+
return { role: 'assistant', content: m.content || '' };
|
|
91
|
+
}
|
|
92
|
+
if (m.role === 'tool') {
|
|
93
|
+
return {
|
|
94
|
+
role: 'tool',
|
|
95
|
+
content: [
|
|
96
|
+
{
|
|
97
|
+
type: 'tool-result',
|
|
98
|
+
toolCallId: m.toolCallId,
|
|
99
|
+
toolName: m.toolName,
|
|
100
|
+
output: { type: 'text', value: JSON.stringify(m.content) },
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return m;
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
const runLLM = async function* (context, threadId) {
|
|
109
|
+
ensureShortTermMessages(context.state);
|
|
110
|
+
const systemPrompt = await buildSystemPrompt(context.state, system, context, storage, contextEngine);
|
|
111
|
+
const coreMessages = mapToCoreMessages(context.state.shortTermMessages || []);
|
|
112
|
+
try {
|
|
113
|
+
const result = await generateText({
|
|
114
|
+
model,
|
|
115
|
+
system: systemPrompt,
|
|
116
|
+
messages: coreMessages,
|
|
117
|
+
tools: toolDefinitions,
|
|
118
|
+
});
|
|
119
|
+
const toolCalls = result.toolCalls ?? [];
|
|
120
|
+
if (toolCalls.length > 0) {
|
|
121
|
+
context.state.shortTermMessages = [
|
|
122
|
+
...(context.state.shortTermMessages ?? []),
|
|
123
|
+
{
|
|
124
|
+
role: 'assistant',
|
|
125
|
+
content: result.text || '',
|
|
126
|
+
toolCalls: toolCalls.map((tc) => ({
|
|
127
|
+
id: tc.toolCallId,
|
|
128
|
+
type: 'function',
|
|
129
|
+
function: {
|
|
130
|
+
name: tc.toolName,
|
|
131
|
+
arguments: JSON.stringify(tc.input),
|
|
132
|
+
},
|
|
133
|
+
})),
|
|
134
|
+
},
|
|
135
|
+
];
|
|
136
|
+
await persistShortTermMessages(context.state, storage);
|
|
137
|
+
for (const toolCall of toolCalls) {
|
|
138
|
+
yield {
|
|
139
|
+
type: `action:${toolCall.toolName}`,
|
|
140
|
+
data: toolCall.input,
|
|
141
|
+
meta: {
|
|
142
|
+
toolCallId: toolCall.toolCallId,
|
|
143
|
+
agentId: context.state.agentId,
|
|
144
|
+
threadId,
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (result.text) {
|
|
150
|
+
if (toolCalls.length === 0) {
|
|
151
|
+
context.state.shortTermMessages = [
|
|
152
|
+
...(context.state.shortTermMessages ?? []),
|
|
153
|
+
{ role: 'assistant', content: result.text },
|
|
154
|
+
];
|
|
155
|
+
await persistShortTermMessages(context.state, storage);
|
|
156
|
+
}
|
|
157
|
+
yield {
|
|
158
|
+
type: 'agent:output',
|
|
159
|
+
data: { content: result.text },
|
|
160
|
+
meta: { agentId: context.state.agentId, threadId },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
166
|
+
const isApiKeyError = errorMessage.includes('API key') ||
|
|
167
|
+
errorMessage.includes('401') ||
|
|
168
|
+
errorMessage.includes('Unauthorized') ||
|
|
169
|
+
errorMessage.includes('authentication');
|
|
170
|
+
if (isApiKeyError) {
|
|
171
|
+
const [currentProvider, ...rest] = currentModelString.split('/');
|
|
172
|
+
const currentModelId = rest.join('/');
|
|
173
|
+
yield {
|
|
174
|
+
type: 'client:ui:widget',
|
|
175
|
+
data: {
|
|
176
|
+
kind: 'form',
|
|
177
|
+
widgetId: `api_key_request_${Date.now()}`,
|
|
178
|
+
title: `AI Provider API Key Required`,
|
|
179
|
+
description: `The AI provider returned an authentication error. Select your provider, model, and provide a valid API key to continue. The key never leaves your local runtime.`,
|
|
180
|
+
fields: [
|
|
181
|
+
{
|
|
182
|
+
id: 'provider',
|
|
183
|
+
label: 'Provider',
|
|
184
|
+
type: 'select',
|
|
185
|
+
required: true,
|
|
186
|
+
options: [
|
|
187
|
+
{ label: 'OpenAI', value: 'openai' },
|
|
188
|
+
{ label: 'Anthropic', value: 'anthropic' },
|
|
189
|
+
],
|
|
190
|
+
defaultValue: currentProvider === 'anthropic' ? 'anthropic' : 'openai',
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
id: 'model',
|
|
194
|
+
label: 'Model',
|
|
195
|
+
type: 'text',
|
|
196
|
+
description: 'Model name without the provider prefix (e.g. `gpt-4o-mini` or `claude-3-5-sonnet-20240620`).',
|
|
197
|
+
placeholder: 'gpt-4o-mini',
|
|
198
|
+
required: true,
|
|
199
|
+
defaultValue: currentModelId,
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: 'apiKey',
|
|
203
|
+
label: 'API Key',
|
|
204
|
+
type: 'text',
|
|
205
|
+
placeholder: `sk-...`,
|
|
206
|
+
required: true,
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
submitLabel: 'Save & Continue',
|
|
210
|
+
metadata: {
|
|
211
|
+
type: 'api_key_request',
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
meta: { agentId: context.state.agentId, threadId },
|
|
215
|
+
};
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
builder.on('agent:invoke', async function* (event, context) {
|
|
222
|
+
const routedTo = event.data?.agentId;
|
|
223
|
+
if (typeof routedTo === 'string' && routedTo && routedTo !== context.state.agentId) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const threadId = event.meta?.threadId || context.state.threadId;
|
|
227
|
+
ensureShortTermMessages(context.state);
|
|
228
|
+
context.state.shortTermMessages = [
|
|
229
|
+
...(context.state.shortTermMessages ?? []),
|
|
230
|
+
{
|
|
231
|
+
role: event.data?.role || 'user',
|
|
232
|
+
content: event?.data?.content || '',
|
|
233
|
+
},
|
|
234
|
+
];
|
|
235
|
+
await persistShortTermMessages(context.state, storage);
|
|
236
|
+
yield* runLLM(context, threadId);
|
|
237
|
+
});
|
|
238
|
+
builder.on('*', async function* (event, context) {
|
|
239
|
+
if (!event.type.endsWith(':result'))
|
|
240
|
+
return;
|
|
241
|
+
if (event.meta?.agentId !== context.state.agentId)
|
|
242
|
+
return;
|
|
243
|
+
const toolCallId = event.meta?.toolCallId;
|
|
244
|
+
if (!toolCallId)
|
|
245
|
+
return;
|
|
246
|
+
ensureShortTermMessages(context.state);
|
|
247
|
+
const toolName = event.type.replace(/^action:/, '').replace(/:result$/, '');
|
|
248
|
+
const resultData = event.data;
|
|
249
|
+
const content = typeof resultData === 'string' ? resultData : JSON.stringify(resultData);
|
|
250
|
+
context.state.shortTermMessages = [
|
|
251
|
+
...(context.state.shortTermMessages ?? []),
|
|
252
|
+
{ role: 'tool', content, toolCallId, toolName },
|
|
253
|
+
];
|
|
254
|
+
await persistShortTermMessages(context.state, storage);
|
|
255
|
+
const lastAssistant = [...(context.state.shortTermMessages ?? [])]
|
|
256
|
+
.reverse()
|
|
257
|
+
.find((m) => m.role === 'assistant' && Array.isArray(m.toolCalls) && m.toolCalls.length > 0);
|
|
258
|
+
if (lastAssistant && lastAssistant.toolCalls) {
|
|
259
|
+
const allFulfilled = lastAssistant.toolCalls.every((tc) => context.state.shortTermMessages?.some((m) => m.role === 'tool' && m.toolCallId === tc.id));
|
|
260
|
+
if (allFulfilled) {
|
|
261
|
+
if (toolName === 'handoff')
|
|
262
|
+
return;
|
|
263
|
+
const threadId = event.meta?.threadId || context.state.threadId;
|
|
264
|
+
yield* runLLM(context, threadId);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
builder.on('client:ui:widget:response', async function* (event, context) {
|
|
269
|
+
const { metadata, values } = event.data;
|
|
270
|
+
if (metadata?.type !== 'api_key_request')
|
|
271
|
+
return;
|
|
272
|
+
if (!values?.apiKey || !values?.provider || !values?.model)
|
|
273
|
+
return;
|
|
274
|
+
const provider = String(values.provider);
|
|
275
|
+
const modelId = String(values.model).trim();
|
|
276
|
+
const apiKey = String(values.apiKey);
|
|
277
|
+
if (provider !== 'openai' && provider !== 'anthropic') {
|
|
278
|
+
yield {
|
|
279
|
+
type: 'agent:output',
|
|
280
|
+
data: { content: `Unsupported provider: ${provider}` },
|
|
281
|
+
meta: { agentId: context.state.agentId },
|
|
282
|
+
};
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const envVar = provider === 'openai' ? 'OPENAI_API_KEY' : 'ANTHROPIC_API_KEY';
|
|
286
|
+
const newModelString = `${provider}/${modelId}`;
|
|
287
|
+
if (!storage)
|
|
288
|
+
return;
|
|
289
|
+
try {
|
|
290
|
+
await storage.createVariable({ key: envVar, value: apiKey, secret: true });
|
|
291
|
+
process.env[envVar] = apiKey;
|
|
292
|
+
currentModelString = newModelString;
|
|
293
|
+
model = resolveModel(currentModelString);
|
|
294
|
+
try {
|
|
295
|
+
saveConfig({ model: currentModelString });
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
// best-effort: config persistence failure shouldn't block the conversation
|
|
299
|
+
}
|
|
300
|
+
yield {
|
|
301
|
+
type: 'agent:output',
|
|
302
|
+
data: {
|
|
303
|
+
content: `Saved ${provider} API key and set model to \`${newModelString}\`.`,
|
|
304
|
+
},
|
|
305
|
+
meta: { agentId: context.state.agentId },
|
|
306
|
+
};
|
|
307
|
+
yield {
|
|
308
|
+
type: 'client:ui:widget',
|
|
309
|
+
data: {
|
|
310
|
+
widgetId: event.data.widgetId,
|
|
311
|
+
kind: 'message',
|
|
312
|
+
title: 'API Key Saved',
|
|
313
|
+
body: `Successfully saved ${provider} API key and selected model \`${newModelString}\`. You can now continue your conversation.`,
|
|
314
|
+
state: 'submitted',
|
|
315
|
+
actions: [{ id: 'ok', label: 'Got it', variant: 'primary' }],
|
|
316
|
+
},
|
|
317
|
+
meta: { agentId: context.state.agentId },
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
yield {
|
|
322
|
+
type: 'agent:output',
|
|
323
|
+
data: {
|
|
324
|
+
content: `Failed to save API key: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
325
|
+
},
|
|
326
|
+
meta: { agentId: context.state.agentId },
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
};
|