@agnt-sdk/studio 0.0.1
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 +162 -0
- package/dist/AgntExecutor.d.ts +56 -0
- package/dist/AgntExecutor.d.ts.map +1 -0
- package/dist/AgntExecutor.js +117 -0
- package/dist/AgntExecutor.js.map +1 -0
- package/dist/BaseExecutor.d.ts +69 -0
- package/dist/BaseExecutor.d.ts.map +1 -0
- package/dist/BaseExecutor.js +528 -0
- package/dist/BaseExecutor.js.map +1 -0
- package/dist/ImageCache.d.ts +94 -0
- package/dist/ImageCache.d.ts.map +1 -0
- package/dist/ImageCache.js +232 -0
- package/dist/ImageCache.js.map +1 -0
- package/dist/cli/commands/init.d.ts +5 -0
- package/dist/cli/commands/init.d.ts.map +1 -0
- package/dist/cli/commands/init.js +47 -0
- package/dist/cli/commands/init.js.map +1 -0
- package/dist/cli/commands/pull.d.ts +10 -0
- package/dist/cli/commands/pull.d.ts.map +1 -0
- package/dist/cli/commands/pull.js +82 -0
- package/dist/cli/commands/pull.js.map +1 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +28 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/utils/api.d.ts +38 -0
- package/dist/cli/utils/api.d.ts.map +1 -0
- package/dist/cli/utils/api.js +44 -0
- package/dist/cli/utils/api.js.map +1 -0
- package/dist/cli/utils/config.d.ts +5 -0
- package/dist/cli/utils/config.d.ts.map +1 -0
- package/dist/cli/utils/config.js +5 -0
- package/dist/cli/utils/config.js.map +1 -0
- package/dist/conditions.d.ts +14 -0
- package/dist/conditions.d.ts.map +1 -0
- package/dist/conditions.js +59 -0
- package/dist/conditions.js.map +1 -0
- package/dist/executorFactory.d.ts +10 -0
- package/dist/executorFactory.d.ts.map +1 -0
- package/dist/executorFactory.js +45 -0
- package/dist/executorFactory.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/anthropic.d.ts +22 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +280 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/bedrock.d.ts +23 -0
- package/dist/providers/bedrock.d.ts.map +1 -0
- package/dist/providers/bedrock.js +281 -0
- package/dist/providers/bedrock.js.map +1 -0
- package/dist/providers/deepseek.d.ts +22 -0
- package/dist/providers/deepseek.d.ts.map +1 -0
- package/dist/providers/deepseek.js +193 -0
- package/dist/providers/deepseek.js.map +1 -0
- package/dist/providers/google.d.ts +22 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +357 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/openai.d.ts +22 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +194 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/systemTools.d.ts +6 -0
- package/dist/systemTools.d.ts.map +1 -0
- package/dist/systemTools.js +15 -0
- package/dist/systemTools.js.map +1 -0
- package/dist/tracing.d.ts +32 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +38 -0
- package/dist/tracing.js.map +1 -0
- package/dist/types.d.ts +280 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +76 -0
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BaseExecutor — V2 PromptManifest native executor
|
|
3
|
+
*
|
|
4
|
+
* Operates directly on the v2 manifest spec:
|
|
5
|
+
* - Renders files/blocks from spec.files[] (no pre-compiled system/user strings)
|
|
6
|
+
* - Evaluates conditions on files, blocks, tools, and models at runtime
|
|
7
|
+
* - Respects enableToolCalls: false (skips tool loop, returns raw LLM text)
|
|
8
|
+
* - Routes models via routingStrategy (fallback | random | conditional)
|
|
9
|
+
*/
|
|
10
|
+
import { evaluateCondition } from './conditions.js';
|
|
11
|
+
import { sendTrace } from './tracing.js';
|
|
12
|
+
import { SYSTEM_TOOL_NAMES } from './systemTools.js';
|
|
13
|
+
export default class BaseExecutor {
|
|
14
|
+
manifest;
|
|
15
|
+
variables;
|
|
16
|
+
toolRouter;
|
|
17
|
+
credentials;
|
|
18
|
+
log;
|
|
19
|
+
debug;
|
|
20
|
+
messages;
|
|
21
|
+
onToolCall;
|
|
22
|
+
cancelled;
|
|
23
|
+
toolErrorCount;
|
|
24
|
+
forceNextTool;
|
|
25
|
+
executorFactory;
|
|
26
|
+
instructions;
|
|
27
|
+
primaryModelConfig;
|
|
28
|
+
provider;
|
|
29
|
+
model;
|
|
30
|
+
allToolDefs;
|
|
31
|
+
tracing;
|
|
32
|
+
files;
|
|
33
|
+
maxMessages;
|
|
34
|
+
modelPricing;
|
|
35
|
+
initialToolChoice;
|
|
36
|
+
constructor({ manifest, variables = {}, toolRouter, credentials, messages = [], onToolCall, log = console.log, logLevel = 'info', tracing, files, maxMessages = 50, initialToolChoice, executorFactory }) {
|
|
37
|
+
this.manifest = manifest;
|
|
38
|
+
if (!this.manifest)
|
|
39
|
+
throw new Error('[BaseExecutor] manifest is required');
|
|
40
|
+
this.validateManifest();
|
|
41
|
+
this.variables = variables;
|
|
42
|
+
this.toolRouter = toolRouter || {};
|
|
43
|
+
this.credentials = credentials || {};
|
|
44
|
+
this.log = logLevel === 'silent' ? () => { } : log;
|
|
45
|
+
this.debug = logLevel === 'debug' ? log : () => { };
|
|
46
|
+
this.files = files;
|
|
47
|
+
this.maxMessages = maxMessages;
|
|
48
|
+
this.messages = messages;
|
|
49
|
+
this.onToolCall = onToolCall;
|
|
50
|
+
this.cancelled = false;
|
|
51
|
+
this.toolErrorCount = {};
|
|
52
|
+
this.forceNextTool = undefined;
|
|
53
|
+
this.tracing = tracing;
|
|
54
|
+
this.executorFactory = executorFactory;
|
|
55
|
+
// Select primary model from spec (respects routing strategy + conditions)
|
|
56
|
+
const primaryModel = this.selectPrimaryModel();
|
|
57
|
+
// Normalize: expose .name for provider adapter compatibility
|
|
58
|
+
this.primaryModelConfig = { ...primaryModel, name: primaryModel.model };
|
|
59
|
+
this.provider = primaryModel.provider;
|
|
60
|
+
this.model = primaryModel.model;
|
|
61
|
+
// Determine initialToolChoice
|
|
62
|
+
const enableToolCalls = this.manifest.spec.enableToolCalls !== false;
|
|
63
|
+
this.initialToolChoice = initialToolChoice ?? (enableToolCalls ? 'required' : 'none');
|
|
64
|
+
// Build active tool definitions (filtered by condition)
|
|
65
|
+
this.allToolDefs = this.buildToolDefinitions();
|
|
66
|
+
// Build system instructions from spec.files[section=system]
|
|
67
|
+
this.instructions = this.buildInstructions();
|
|
68
|
+
}
|
|
69
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
70
|
+
// Manifest validation
|
|
71
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
72
|
+
validateManifest() {
|
|
73
|
+
if (this.manifest.kind !== 'PromptManifest') {
|
|
74
|
+
throw new Error('[BaseExecutor] manifest.kind must be "PromptManifest"');
|
|
75
|
+
}
|
|
76
|
+
if (this.manifest.apiVersion !== 'v2') {
|
|
77
|
+
throw new Error('[BaseExecutor] manifest.apiVersion must be "v2"');
|
|
78
|
+
}
|
|
79
|
+
const spec = this.manifest.spec;
|
|
80
|
+
if (!spec)
|
|
81
|
+
throw new Error('[BaseExecutor] manifest.spec is required');
|
|
82
|
+
if (!spec.models || spec.models.length === 0) {
|
|
83
|
+
throw new Error('[BaseExecutor] manifest.spec.models is required and must not be empty');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
87
|
+
// Model selection
|
|
88
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
89
|
+
selectPrimaryModel() {
|
|
90
|
+
const models = this.manifest.spec.models;
|
|
91
|
+
const strategy = this.manifest.spec.routingStrategy ?? 'fallback';
|
|
92
|
+
if (strategy === 'random') {
|
|
93
|
+
const idx = Math.floor(Math.random() * models.length);
|
|
94
|
+
return models[idx];
|
|
95
|
+
}
|
|
96
|
+
if (strategy === 'conditional' || strategy === 'conditional_with_fallback') {
|
|
97
|
+
const passing = models.filter(m => evaluateCondition(m.condition, this.variables));
|
|
98
|
+
if (passing.length > 0)
|
|
99
|
+
return passing[0];
|
|
100
|
+
if (strategy === 'conditional_with_fallback') {
|
|
101
|
+
// Fall through to fallback logic below
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
throw new Error('[BaseExecutor] No model condition passed and strategy is "conditional"');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// fallback: pick lowest fallbackOrder, then array order
|
|
108
|
+
const sorted = [...models].sort((a, b) => (a.fallbackOrder ?? 0) - (b.fallbackOrder ?? 0));
|
|
109
|
+
return sorted[0];
|
|
110
|
+
}
|
|
111
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
112
|
+
// Tool definitions
|
|
113
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
114
|
+
buildToolDefinitions() {
|
|
115
|
+
if (this.manifest.spec.enableToolCalls === false)
|
|
116
|
+
return [];
|
|
117
|
+
const tools = this.manifest.spec.tools ?? [];
|
|
118
|
+
return tools
|
|
119
|
+
.filter(t => evaluateCondition(t.condition, this.variables))
|
|
120
|
+
.map(t => this.processToolDefinition({
|
|
121
|
+
type: 'function',
|
|
122
|
+
function: {
|
|
123
|
+
name: t.name,
|
|
124
|
+
description: t.description ?? '',
|
|
125
|
+
parameters: t.parameters ?? { type: 'object', properties: {} }
|
|
126
|
+
}
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Reorder tool schema properties so reasoning fields appear first.
|
|
131
|
+
* Ensures LLM generates reason before action when strict: true.
|
|
132
|
+
*/
|
|
133
|
+
processToolDefinition(tool) {
|
|
134
|
+
const processed = JSON.parse(JSON.stringify(tool));
|
|
135
|
+
if (processed.function?.parameters) {
|
|
136
|
+
processed.function.parameters = this.reorderReasoningFields(processed.function.parameters);
|
|
137
|
+
}
|
|
138
|
+
return processed;
|
|
139
|
+
}
|
|
140
|
+
reorderReasoningFields(schema) {
|
|
141
|
+
if (!schema?.properties)
|
|
142
|
+
return schema;
|
|
143
|
+
const processed = { ...schema };
|
|
144
|
+
const reasoningField = ['reason', 'reasoning'].find(f => f in schema.properties);
|
|
145
|
+
if (reasoningField) {
|
|
146
|
+
const { [reasoningField]: rf, ...rest } = processed.properties;
|
|
147
|
+
processed.properties = { [reasoningField]: rf, ...rest };
|
|
148
|
+
if (Array.isArray(processed.required) && processed.required.includes(reasoningField)) {
|
|
149
|
+
processed.required = [
|
|
150
|
+
reasoningField,
|
|
151
|
+
...processed.required.filter((f) => f !== reasoningField)
|
|
152
|
+
];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return processed;
|
|
156
|
+
}
|
|
157
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
158
|
+
// Block rendering
|
|
159
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
160
|
+
/**
|
|
161
|
+
* Render all files for a given section into a single string.
|
|
162
|
+
*/
|
|
163
|
+
renderSection(section) {
|
|
164
|
+
const files = (this.manifest.spec.files ?? [])
|
|
165
|
+
.filter(f => f.section === section)
|
|
166
|
+
.sort((a, b) => a.order - b.order);
|
|
167
|
+
const parts = [];
|
|
168
|
+
for (const file of files) {
|
|
169
|
+
if (!evaluateCondition(file.condition, this.variables))
|
|
170
|
+
continue;
|
|
171
|
+
const content = this.renderBlocks(file.blocks ?? []);
|
|
172
|
+
if (content)
|
|
173
|
+
parts.push(content);
|
|
174
|
+
}
|
|
175
|
+
return parts.join('\n\n');
|
|
176
|
+
}
|
|
177
|
+
renderBlocks(blocks) {
|
|
178
|
+
const sorted = [...blocks].sort((a, b) => a.order - b.order);
|
|
179
|
+
const parts = [];
|
|
180
|
+
for (const block of sorted) {
|
|
181
|
+
if (!evaluateCondition(block.condition, this.variables))
|
|
182
|
+
continue;
|
|
183
|
+
const rendered = this.renderBlock(block);
|
|
184
|
+
if (rendered)
|
|
185
|
+
parts.push(rendered);
|
|
186
|
+
}
|
|
187
|
+
return parts.join('\n\n');
|
|
188
|
+
}
|
|
189
|
+
renderBlock(block) {
|
|
190
|
+
switch (block.type) {
|
|
191
|
+
case 'text':
|
|
192
|
+
return this.populateTemplate(block.content ?? '', this.variables);
|
|
193
|
+
case 'heading': {
|
|
194
|
+
const level = Math.min(Math.max(block.headingLevel ?? 2, 1), 6);
|
|
195
|
+
return `${'#'.repeat(level)} ${block.content ?? ''}`;
|
|
196
|
+
}
|
|
197
|
+
case 'divider':
|
|
198
|
+
return '---';
|
|
199
|
+
case 'variable': {
|
|
200
|
+
const val = this.variables[block.variableKey ?? ''];
|
|
201
|
+
return val !== undefined && val !== null ? String(val) : '';
|
|
202
|
+
}
|
|
203
|
+
case 'component_ref': {
|
|
204
|
+
const comp = this.manifest.resolvedDependencies?.components?.find(c => c.name === block.componentName);
|
|
205
|
+
return comp?.content ? this.populateTemplate(comp.content, this.variables) : '';
|
|
206
|
+
}
|
|
207
|
+
case 'assistant_ref': {
|
|
208
|
+
const assistant = this.manifest.resolvedDependencies?.assistants?.find(a => a.name === block.assistantName);
|
|
209
|
+
if (!assistant)
|
|
210
|
+
return '';
|
|
211
|
+
if (assistant.blocks)
|
|
212
|
+
return this.renderBlocks(assistant.blocks);
|
|
213
|
+
return assistant.content ? this.populateTemplate(assistant.content, this.variables) : '';
|
|
214
|
+
}
|
|
215
|
+
case 'skill_ref': {
|
|
216
|
+
const skill = this.manifest.resolvedDependencies?.skills?.find(s => s.name === block.skillName);
|
|
217
|
+
if (!skill)
|
|
218
|
+
return '';
|
|
219
|
+
if (block.scenarioName) {
|
|
220
|
+
const scenario = skill.scenarios?.find(sc => sc.name === block.scenarioName);
|
|
221
|
+
return scenario?.content ? this.populateTemplate(scenario.content, this.variables) : '';
|
|
222
|
+
}
|
|
223
|
+
return skill.content ? this.populateTemplate(skill.content, this.variables) : '';
|
|
224
|
+
}
|
|
225
|
+
default:
|
|
226
|
+
return '';
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
buildInstructions() {
|
|
230
|
+
return this.renderSection('system');
|
|
231
|
+
}
|
|
232
|
+
populateTemplate(template, variables) {
|
|
233
|
+
if (!template)
|
|
234
|
+
return '';
|
|
235
|
+
return template.replace(/\{([^}]+)\}/g, (match, key) => {
|
|
236
|
+
if (key.startsWith('component.')) {
|
|
237
|
+
const compName = key.slice('component.'.length);
|
|
238
|
+
const comp = this.manifest.resolvedDependencies?.components?.find(c => c.name === compName);
|
|
239
|
+
return comp?.content ? this.populateTemplate(comp.content, variables) : match;
|
|
240
|
+
}
|
|
241
|
+
return variables[key] !== undefined ? String(variables[key]) : match;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
245
|
+
// Tool choice normalization
|
|
246
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
247
|
+
normalizeToolChoice(toolChoice) {
|
|
248
|
+
if (toolChoice === 'auto' || toolChoice === 'required' || toolChoice === 'none') {
|
|
249
|
+
return toolChoice;
|
|
250
|
+
}
|
|
251
|
+
return { type: 'function', function: { name: toolChoice } };
|
|
252
|
+
}
|
|
253
|
+
cancel() {
|
|
254
|
+
this.cancelled = true;
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
258
|
+
// Variable validation
|
|
259
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
260
|
+
validateVariables() {
|
|
261
|
+
const schema = this.manifest.spec.variables ?? [];
|
|
262
|
+
for (const varDef of schema) {
|
|
263
|
+
if (!varDef.required)
|
|
264
|
+
continue;
|
|
265
|
+
// Skip if the variable's own condition doesn't pass
|
|
266
|
+
if (!evaluateCondition(varDef.condition, this.variables))
|
|
267
|
+
continue;
|
|
268
|
+
if (this.variables[varDef.key] === undefined) {
|
|
269
|
+
throw new Error(`[BaseExecutor] Required variable missing: ${varDef.key}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
274
|
+
// Message building
|
|
275
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
276
|
+
buildInitialMessages() {
|
|
277
|
+
const messages = [];
|
|
278
|
+
messages.push({ role: 'system', content: this.instructions });
|
|
279
|
+
const userContent = this.renderSection('messages');
|
|
280
|
+
let finalUserContent;
|
|
281
|
+
if (this.files && this.files.length > 0) {
|
|
282
|
+
finalUserContent = [{ type: 'text', text: userContent }, ...this.files];
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
finalUserContent = userContent;
|
|
286
|
+
}
|
|
287
|
+
messages.push({ role: 'user', content: finalUserContent });
|
|
288
|
+
return messages;
|
|
289
|
+
}
|
|
290
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
291
|
+
// Pricing
|
|
292
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
293
|
+
calculateCost(inputTokens, outputTokens) {
|
|
294
|
+
if (this.modelPricing) {
|
|
295
|
+
return (inputTokens / 1_000_000) * this.modelPricing.inputTokensPer1M +
|
|
296
|
+
(outputTokens / 1_000_000) * this.modelPricing.outputTokensPer1M;
|
|
297
|
+
}
|
|
298
|
+
// Default: Claude Sonnet pricing
|
|
299
|
+
return (inputTokens / 1_000_000) * 3 + (outputTokens / 1_000_000) * 15;
|
|
300
|
+
}
|
|
301
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
302
|
+
// Main execution
|
|
303
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
304
|
+
async execute() {
|
|
305
|
+
try {
|
|
306
|
+
this.validateVariables();
|
|
307
|
+
if (this.messages.length === 0) {
|
|
308
|
+
this.messages = this.buildInitialMessages();
|
|
309
|
+
}
|
|
310
|
+
const enableToolCalls = this.manifest.spec.enableToolCalls !== false;
|
|
311
|
+
const toolChoice = this.normalizeToolChoice(this.initialToolChoice);
|
|
312
|
+
const turnStart = Date.now();
|
|
313
|
+
const result = await this.invoke(this.messages, {
|
|
314
|
+
tools: enableToolCalls ? this.allToolDefs : [],
|
|
315
|
+
tool_choice: enableToolCalls ? toolChoice : 'none'
|
|
316
|
+
});
|
|
317
|
+
const turnDuration = Date.now() - turnStart;
|
|
318
|
+
const usage = {
|
|
319
|
+
inputTokens: result.usage?.input_tokens || 0,
|
|
320
|
+
outputTokens: result.usage?.output_tokens || 0,
|
|
321
|
+
totalCostUSD: 0
|
|
322
|
+
};
|
|
323
|
+
usage.totalCostUSD = this.calculateCost(usage.inputTokens, usage.outputTokens);
|
|
324
|
+
this.messages.push(result.message);
|
|
325
|
+
await this.sendTurnTrace(result.usage || { input_tokens: 0, output_tokens: 0 }, turnDuration, usage.totalCostUSD);
|
|
326
|
+
// No tools → return message content directly
|
|
327
|
+
if (!enableToolCalls || this.allToolDefs.length === 0) {
|
|
328
|
+
const content = result.message.content;
|
|
329
|
+
return {
|
|
330
|
+
ok: !this.cancelled,
|
|
331
|
+
usage,
|
|
332
|
+
result: typeof content === 'string' ? content : content,
|
|
333
|
+
messages: this.messages
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
// Tool loop
|
|
337
|
+
let output;
|
|
338
|
+
const hasToolRouter = Object.keys(this.toolRouter).length > 0;
|
|
339
|
+
if (hasToolRouter) {
|
|
340
|
+
output = await this.runToolLoop(result.message, usage);
|
|
341
|
+
// Handle pause signal from runToolLoop
|
|
342
|
+
if (output && typeof output === 'object' && output.__paused) {
|
|
343
|
+
return {
|
|
344
|
+
ok: true,
|
|
345
|
+
paused: true,
|
|
346
|
+
pendingToolCall: output.pendingToolCall,
|
|
347
|
+
usage,
|
|
348
|
+
result: null,
|
|
349
|
+
messages: this.messages
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
else if (result.message.tool_calls && result.message.tool_calls.length > 0) {
|
|
354
|
+
output = result.message.tool_calls[0].args;
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
output = result.message.content;
|
|
358
|
+
}
|
|
359
|
+
return { ok: !this.cancelled, usage, result: output, messages: this.messages };
|
|
360
|
+
}
|
|
361
|
+
catch (error) {
|
|
362
|
+
return {
|
|
363
|
+
ok: false,
|
|
364
|
+
usage: { inputTokens: 0, outputTokens: 0, totalCostUSD: 0 },
|
|
365
|
+
result: null,
|
|
366
|
+
messages: this.messages,
|
|
367
|
+
error: error.message
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
372
|
+
// Tool loop
|
|
373
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
374
|
+
async runToolLoop(message, usage) {
|
|
375
|
+
const terminatingTools = ['finish_agent_run', 'output'];
|
|
376
|
+
while (this.hasToolCalls(message)) {
|
|
377
|
+
if (this.cancelled)
|
|
378
|
+
break;
|
|
379
|
+
if (this.messages.length >= this.maxMessages) {
|
|
380
|
+
throw new Error(`[BaseExecutor] Message stack exceeded ${this.maxMessages} messages. ` +
|
|
381
|
+
'Agent must call finish_agent_run to complete.');
|
|
382
|
+
}
|
|
383
|
+
// Tag each tool call with source before routing
|
|
384
|
+
const taggedToolCalls = message.tool_calls.map(tc => ({
|
|
385
|
+
...tc,
|
|
386
|
+
source: (SYSTEM_TOOL_NAMES.has(tc.name) ? 'system' : 'custom')
|
|
387
|
+
}));
|
|
388
|
+
// Check for custom tools not in the router — these trigger pause
|
|
389
|
+
if (this.onToolCall) {
|
|
390
|
+
for (const tc of taggedToolCalls) {
|
|
391
|
+
if (tc.source === 'custom' && !this.toolRouter[tc.name] && !terminatingTools.includes(tc.name)) {
|
|
392
|
+
this.log(`[BaseExecutor] Custom tool '${tc.name}' not in router — pausing execution`);
|
|
393
|
+
const cbResult = await this.onToolCall({ toolCall: tc, toolResponse: undefined });
|
|
394
|
+
if (cbResult?.pause) {
|
|
395
|
+
return { __paused: true, pendingToolCall: tc };
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const toolResults = await this.handleToolCalls(taggedToolCalls);
|
|
401
|
+
this.forceNextTool = toolResults.find(r => r.forceNextTool)?.forceNextTool;
|
|
402
|
+
for (const r of toolResults) {
|
|
403
|
+
this.messages.push({ role: 'tool', tool_call_id: r.tool_call_id, content: JSON.stringify(r.content) });
|
|
404
|
+
}
|
|
405
|
+
if (this.onToolCall) {
|
|
406
|
+
for (let i = 0; i < taggedToolCalls.length; i++) {
|
|
407
|
+
const tc = taggedToolCalls[i];
|
|
408
|
+
if (terminatingTools.includes(tc.name))
|
|
409
|
+
continue;
|
|
410
|
+
if (tc.source === 'custom' && !this.toolRouter[tc.name])
|
|
411
|
+
continue; // already handled above
|
|
412
|
+
const cbResult = await this.onToolCall({ toolCall: tc, toolResponse: toolResults[i]?.content });
|
|
413
|
+
if (cbResult?.abort) {
|
|
414
|
+
this.cancelled = true;
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (this.cancelled)
|
|
420
|
+
break;
|
|
421
|
+
const termCall = taggedToolCalls.find(c => terminatingTools.includes(c.name));
|
|
422
|
+
if (termCall) {
|
|
423
|
+
const termResult = toolResults.find(r => r.tool_call_id === termCall.id);
|
|
424
|
+
if (termResult && (termResult.content.completed === false || termResult.content.error)) {
|
|
425
|
+
this.log('[BaseExecutor] Terminating tool rejected, continuing');
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
return termCall.args;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
const toolChoiceStr = this.forceNextTool ?? 'required';
|
|
432
|
+
const toolChoice = this.normalizeToolChoice(toolChoiceStr);
|
|
433
|
+
const turnStart = Date.now();
|
|
434
|
+
const result = await this.invoke(this.messages, { tools: this.allToolDefs, tool_choice: toolChoice });
|
|
435
|
+
const turnDuration = Date.now() - turnStart;
|
|
436
|
+
usage.inputTokens += result.usage?.input_tokens || 0;
|
|
437
|
+
usage.outputTokens += result.usage?.output_tokens || 0;
|
|
438
|
+
usage.totalCostUSD = this.calculateCost(usage.inputTokens, usage.outputTokens);
|
|
439
|
+
message = result.message;
|
|
440
|
+
this.messages.push(message);
|
|
441
|
+
const turnCost = this.calculateCost(result.usage?.input_tokens || 0, result.usage?.output_tokens || 0);
|
|
442
|
+
await this.sendTurnTrace(result.usage || { input_tokens: 0, output_tokens: 0 }, turnDuration, turnCost);
|
|
443
|
+
}
|
|
444
|
+
if (this.cancelled)
|
|
445
|
+
return { ok: false, status: 'cancelled' };
|
|
446
|
+
throw new Error('[BaseExecutor] Agent ended without calling terminating tool');
|
|
447
|
+
}
|
|
448
|
+
async handleToolCalls(toolCalls) {
|
|
449
|
+
const results = [];
|
|
450
|
+
for (const tc of toolCalls) {
|
|
451
|
+
this.log(`[BaseExecutor] Executing tool: ${tc.name}`);
|
|
452
|
+
let toolResult;
|
|
453
|
+
if (tc.name === 'finish_agent_run') {
|
|
454
|
+
const handler = this.toolRouter[tc.name];
|
|
455
|
+
if (handler) {
|
|
456
|
+
try {
|
|
457
|
+
toolResult = await handler.execute(tc.args);
|
|
458
|
+
}
|
|
459
|
+
catch (err) {
|
|
460
|
+
toolResult = { completed: false, error: true, message: err.message };
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
toolResult = tc.args;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
const handler = this.toolRouter[tc.name];
|
|
469
|
+
if (!handler) {
|
|
470
|
+
toolResult = { completed: false, error: true, message: `Tool '${tc.name}' not found` };
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
try {
|
|
474
|
+
toolResult = await handler.execute(tc.args);
|
|
475
|
+
this.toolErrorCount[tc.name] = 0;
|
|
476
|
+
}
|
|
477
|
+
catch (err) {
|
|
478
|
+
this.toolErrorCount[tc.name] = (this.toolErrorCount[tc.name] || 0) + 1;
|
|
479
|
+
if (this.toolErrorCount[tc.name] >= 3)
|
|
480
|
+
throw err;
|
|
481
|
+
toolResult = { completed: false, error: true, message: 'Tool error. Please try a different approach.' };
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
results.push({ tool_call_id: tc.id, content: toolResult, forceNextTool: toolResult?.forceNextTool });
|
|
486
|
+
}
|
|
487
|
+
return results;
|
|
488
|
+
}
|
|
489
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
490
|
+
// Tracing
|
|
491
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
492
|
+
async sendTurnTrace(turnUsage, turnDuration, cost) {
|
|
493
|
+
if (!this.tracing)
|
|
494
|
+
return;
|
|
495
|
+
const lastAssistant = [...this.messages].reverse().find(m => m.role === 'assistant');
|
|
496
|
+
const lastIdx = lastAssistant ? this.messages.lastIndexOf(lastAssistant) : -1;
|
|
497
|
+
const messagesWithoutOutput = lastIdx >= 0
|
|
498
|
+
? [...this.messages.slice(0, lastIdx), ...this.messages.slice(lastIdx + 1)]
|
|
499
|
+
: this.messages;
|
|
500
|
+
await sendTrace(this.tracing, {
|
|
501
|
+
promptName: this.manifest.metadata.name,
|
|
502
|
+
manifest: this.manifest,
|
|
503
|
+
etag: this.manifest.metadata.etag || null,
|
|
504
|
+
variables: this.variables,
|
|
505
|
+
messages: messagesWithoutOutput.slice(2),
|
|
506
|
+
output: lastAssistant || null,
|
|
507
|
+
inputTokens: turnUsage.input_tokens,
|
|
508
|
+
outputTokens: turnUsage.output_tokens,
|
|
509
|
+
totalTokens: turnUsage.input_tokens + turnUsage.output_tokens,
|
|
510
|
+
cost,
|
|
511
|
+
duration: turnDuration,
|
|
512
|
+
model: { provider: this.provider, name: this.model, metadata: this.primaryModelConfig.metadata || {} },
|
|
513
|
+
status: 'completed',
|
|
514
|
+
metadata: { turnNumber: this.messages.filter(m => m.role === 'assistant').length },
|
|
515
|
+
tags: this.tracing.tags || []
|
|
516
|
+
}, this.log);
|
|
517
|
+
}
|
|
518
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
519
|
+
// Abstract — implemented by provider adapters
|
|
520
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
521
|
+
async invoke(_messages, _options) {
|
|
522
|
+
throw new Error('invoke() must be implemented by provider adapter');
|
|
523
|
+
}
|
|
524
|
+
hasToolCalls(_message) {
|
|
525
|
+
throw new Error('hasToolCalls() must be implemented by provider adapter');
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
//# sourceMappingURL=BaseExecutor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseExecutor.js","sourceRoot":"","sources":["../src/BaseExecutor.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAqBH,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,MAAM,CAAC,OAAO,OAAO,YAAY;IACrB,QAAQ,CAAmB;IAC3B,SAAS,CAAsB;IAC/B,UAAU,CAAa;IACvB,WAAW,CAAM;IACjB,GAAG,CAA4C;IAC/C,KAAK,CAA4C;IACjD,QAAQ,CAAY;IACpB,UAAU,CAAoB;IAC9B,SAAS,CAAU;IACnB,cAAc,CAAyB;IACvC,aAAa,CAAU;IACvB,eAAe,CAAgD;IAC/D,YAAY,CAAS;IACrB,kBAAkB,CAAmC;IACrD,QAAQ,CAAS;IACjB,KAAK,CAAS;IACd,WAAW,CAAmB;IAC9B,OAAO,CAAiB;IACxB,KAAK,CAAc;IACnB,WAAW,CAAS;IACpB,YAAY,CAAgB;IAC5B,iBAAiB,CAAwC;IAEnE,YAAY,EACV,QAAQ,EACR,SAAS,GAAG,EAAE,EACd,UAAU,EACV,WAAW,EACX,QAAQ,GAAG,EAAE,EACb,UAAU,EACV,GAAG,GAAG,OAAO,CAAC,G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B;QACxD,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;IACtE,CAAC;IAED,YAAY,CAAC,QAAiB;QAC5B,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;IAC5E,CAAC;CACF"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
interface ImageCacheConfig {
|
|
2
|
+
log?: (message: string, ...args: any[]) => void;
|
|
3
|
+
maxCacheSize?: number;
|
|
4
|
+
}
|
|
5
|
+
interface CacheStats {
|
|
6
|
+
hits: number;
|
|
7
|
+
misses: number;
|
|
8
|
+
stores: number;
|
|
9
|
+
evictions: number;
|
|
10
|
+
}
|
|
11
|
+
interface CacheStatsWithRates extends CacheStats {
|
|
12
|
+
hitRate: string;
|
|
13
|
+
cacheSize: number;
|
|
14
|
+
maxSize: number;
|
|
15
|
+
}
|
|
16
|
+
interface ProviderStats {
|
|
17
|
+
anthropic: number;
|
|
18
|
+
openai: number;
|
|
19
|
+
bedrock: number;
|
|
20
|
+
deepseek: number;
|
|
21
|
+
}
|
|
22
|
+
interface MemorySize {
|
|
23
|
+
bytes: number;
|
|
24
|
+
kb: string;
|
|
25
|
+
mb: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* ImageCache - Caches processed images per provider format
|
|
29
|
+
*
|
|
30
|
+
* Prevents reprocessing the same image when switching providers multiple times.
|
|
31
|
+
* Stores images in multiple formats (anthropic, openai, bedrock) keyed by source URL.
|
|
32
|
+
*
|
|
33
|
+
* Example:
|
|
34
|
+
* - Original: https://example.com/image.jpg
|
|
35
|
+
* - Cache stores:
|
|
36
|
+
* - anthropic: "data:image/jpeg;base64,..."
|
|
37
|
+
* - openai: "data:image/jpeg;base64,..." (or original URL)
|
|
38
|
+
* - bedrock: { type: "image", image: { format: "jpeg", source: { bytes: Uint8Array } } }
|
|
39
|
+
*
|
|
40
|
+
* Cache key: SHA256 hash of source URL for consistent lookup
|
|
41
|
+
*/
|
|
42
|
+
export default class ImageCache {
|
|
43
|
+
#private;
|
|
44
|
+
private log;
|
|
45
|
+
private maxCacheSize;
|
|
46
|
+
private cache;
|
|
47
|
+
private stats;
|
|
48
|
+
constructor({ log, maxCacheSize }?: ImageCacheConfig);
|
|
49
|
+
/**
|
|
50
|
+
* Get cached image for specific provider
|
|
51
|
+
* Returns null if not found
|
|
52
|
+
*/
|
|
53
|
+
get(imageSource: any, provider: string): any | null;
|
|
54
|
+
/**
|
|
55
|
+
* Store processed image for specific provider
|
|
56
|
+
*/
|
|
57
|
+
set(imageSource: any, provider: string, processedImage: any): void;
|
|
58
|
+
/**
|
|
59
|
+
* Check if image is cached for specific provider
|
|
60
|
+
*/
|
|
61
|
+
has(imageSource: any, provider: string): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Get all cached formats for an image
|
|
64
|
+
* Returns object with available providers
|
|
65
|
+
*/
|
|
66
|
+
getAll(imageSource: any): Record<string, any> | null;
|
|
67
|
+
/**
|
|
68
|
+
* Clear entire cache
|
|
69
|
+
*/
|
|
70
|
+
clear(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Get cache statistics
|
|
73
|
+
*/
|
|
74
|
+
getStats(): CacheStatsWithRates;
|
|
75
|
+
/**
|
|
76
|
+
* Reset statistics
|
|
77
|
+
*/
|
|
78
|
+
resetStats(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Get cache size in bytes (approximate)
|
|
81
|
+
* Useful for monitoring memory usage
|
|
82
|
+
*/
|
|
83
|
+
getMemorySize(): MemorySize;
|
|
84
|
+
/**
|
|
85
|
+
* Log cache status
|
|
86
|
+
*/
|
|
87
|
+
logStatus(): void;
|
|
88
|
+
/**
|
|
89
|
+
* Estimate provider-specific memory usage
|
|
90
|
+
*/
|
|
91
|
+
getProviderStats(): ProviderStats;
|
|
92
|
+
}
|
|
93
|
+
export {};
|
|
94
|
+
//# sourceMappingURL=ImageCache.d.ts.map
|