@causyne/agent 1.20.117 → 1.20.120
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/package.json +7 -2
- package/src/agentLoop.js +29 -12
- package/src/agentLoop.ts +29 -16
- package/src/browser/causyneBrowser.js +231 -136
- package/src/browser/causyneBrowser.ts +229 -140
- package/src/browser/searchProviders.js +41 -30
- package/src/browser/searchProviders.ts +24 -14
- package/src/browser/workspacePathSafety.js +50 -0
- package/src/browser/workspacePathSafety.ts +50 -0
- package/src/browser/workspacePreview.js +27 -4
- package/src/browser/workspacePreview.ts +28 -4
- package/src/cliMode.js +20 -4
- package/src/commands.js +12 -14
- package/src/creative/liveBridge.js +34 -1
- package/src/creative/liveBridge.ts +28 -1
- package/src/observer/evidence.js +23 -0
- package/src/observer/evidence.ts +21 -0
- package/src/plugins/pluginManager.js +74 -0
- package/src/providers/anthropic.js +33 -17
- package/src/providers/base.js +14 -0
- package/src/providers/dashscope-byok.js +13 -7
- package/src/providers/deepseek.js +13 -18
- package/src/providers/gemini.js +33 -25
- package/src/providers/moonshot.js +13 -12
- package/src/providers/ollama.js +21 -21
- package/src/providers/openai-compatible.js +12 -19
- package/src/providers/openrouter.js +20 -90
- package/src/providers/openrouter.ts +31 -94
- package/src/providers/qwen-byok.js +13 -7
- package/src/providers/toolContracts.js +9 -3
- package/src/providers/toolContracts.ts +9 -3
- package/src/providers/wireNormalizer.js +238 -0
- package/src/providers/wireNormalizer.ts +279 -0
- package/src/providers/xai.js +13 -12
- package/src/reporting.js +12 -4
- package/src/skillsManager.js +3 -2
- package/src/skillsManager.ts +3 -2
- package/src/tools/browserAutomator.js +13 -1
- package/src/tools/browserAutomator.ts +13 -0
- package/src/tools/creativeApps.js +2 -1
- package/src/tools/creativeApps.ts +2 -1
- package/src/tools/gameAssetFetcher.js +130 -26
- package/src/tools/gameAssetHarvester.js +80 -58
- package/src/tools/nativeEngine.js +1 -1
- package/src/tools/nativeEngine.ts +1 -1
- package/src/tools/pluginManager.js +17 -2
- package/src/tools/registry.js +13 -0
- package/src/tools/toolScanner.js +596 -140
- package/src/tools/toolScanner.ts +665 -147
- package/src/tools/toolSelfRepair.js +243 -114
- package/src/tools/toolSelfRepair.ts +262 -111
- package/src/tools/webFetch.js +161 -24
- package/src/tools/webFetch.ts +157 -22
- package/src/tools/webSearch.js +33 -4
- package/src/tools/webSearch.ts +27 -4
- package/src/tui/composerControls.js +16 -3
- package/src/tui/composerControls.ts +14 -3
- package/src/tui/prompt.js +122 -16
- package/src/tui/sessionPicker.js +132 -35
- package/src/tui/sessionPicker.ts +153 -32
- package/src/tui/sessionPickerView.js +277 -34
- package/src/tui/sessionPickerView.ts +328 -34
- package/src/tui/statusLine.js +3 -0
- package/src/tui/statusLine.ts +5 -0
- package/src/tui/usageCard.js +119 -47
- package/src/tui/usageCard.ts +133 -49
- package/src/tui/workspaceIdentity.js +133 -0
- package/src/tui/workspaceIdentity.ts +146 -0
- package/src/tui/workspaceSwitch.js +2 -0
- package/src/tui/workspaceSwitch.ts +1 -0
- package/src/verification/boundedExplorer.js +119 -0
- package/src/verification/cliProbe.js +116 -0
- package/src/verification/featureMap.js +78 -0
- package/src/verification/interactivePty.js +188 -0
- package/src/verification/propertyProbe.js +121 -0
- package/src/verification/ptyProbe.js +125 -0
- package/src/verification/skillCatalog.d.ts +1 -0
- package/src/verification/skillCatalog.js +60 -0
- package/src/verification/statechart.js +143 -0
- package/src/verification/uiRuntime.js +57 -0
- package/src/verificationPlan.js +1 -1
- package/src/verificationPlan.ts +1 -1
package/src/observer/evidence.ts
CHANGED
|
@@ -178,6 +178,27 @@ export function isRenderedNativeBrowserAction(tool: string): boolean {
|
|
|
178
178
|
return ['browser_act', 'preview', 'snapshot'].includes(tool.toLowerCase());
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
+
/** Only inspected, successful DOM output counts as browser verification. */
|
|
182
|
+
export function inspectBrowserToolEvidence(result: unknown): { verified: boolean; runtimeErrors: string[] } {
|
|
183
|
+
if (!result || typeof result !== 'object') return { verified: false, runtimeErrors: [] };
|
|
184
|
+
const data = result as { success?: boolean; data?: unknown };
|
|
185
|
+
if (data.success !== true || !Array.isArray(data.data)) return { verified: false, runtimeErrors: [] };
|
|
186
|
+
const runtimeErrors: string[] = [];
|
|
187
|
+
let verified = false;
|
|
188
|
+
for (const item of data.data) {
|
|
189
|
+
if (!item || typeof item !== 'object') continue;
|
|
190
|
+
const action = item as { action?: string; output?: string; data?: { runtimeErrors?: unknown } };
|
|
191
|
+
if (Array.isArray(action.data?.runtimeErrors)) {
|
|
192
|
+
runtimeErrors.push(...action.data.runtimeErrors.filter((error): error is string => typeof error === 'string'));
|
|
193
|
+
}
|
|
194
|
+
if (['snapshot', 'scrape'].includes(action.action ?? '')
|
|
195
|
+
&& typeof action.output === 'string' && action.output.trim()) {
|
|
196
|
+
verified = true;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return { verified: verified && runtimeErrors.length === 0, runtimeErrors };
|
|
200
|
+
}
|
|
201
|
+
|
|
181
202
|
export function isVerificationCommand(command: string): boolean {
|
|
182
203
|
return VERIFICATION_COMMAND.test(command);
|
|
183
204
|
}
|
|
@@ -304,6 +304,80 @@ export function createPlugin({ name, description, toolName, toolDescription, par
|
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
306
|
|
|
307
|
+
|
|
308
|
+
export function createSkillPlugin({
|
|
309
|
+
name, description, skillId, skillName, skillDescription, skillContent, licenseText
|
|
310
|
+
} = {}) {
|
|
311
|
+
if (!NAME.test(name || '')) return { ok: false, error: 'Invalid plugin name' };
|
|
312
|
+
if (!NAME.test(skillId || '')) return { ok: false, error: 'Invalid skill id' };
|
|
313
|
+
if (typeof description !== 'string' || !description.trim() || description.length > 1000) {
|
|
314
|
+
return { ok: false, error: 'Plugin description must be 1..1000 characters' };
|
|
315
|
+
}
|
|
316
|
+
if (typeof skillName !== 'string' || !skillName.trim() || skillName.length > 120) {
|
|
317
|
+
return { ok: false, error: 'Skill name must be 1..120 characters' };
|
|
318
|
+
}
|
|
319
|
+
if (typeof skillDescription !== 'string' || !skillDescription.trim()
|
|
320
|
+
|| skillDescription.length > 400) {
|
|
321
|
+
return { ok: false, error: 'Skill description must be 1..400 characters' };
|
|
322
|
+
}
|
|
323
|
+
if (typeof skillContent !== 'string' || !skillContent.trim()
|
|
324
|
+
|| Buffer.byteLength(skillContent, 'utf8') > 32 * 1024) {
|
|
325
|
+
return { ok: false, error: 'Skill content must be nonempty and at most 32 KB' };
|
|
326
|
+
}
|
|
327
|
+
if (typeof licenseText !== 'string' || licenseText.length > 32 * 1024) {
|
|
328
|
+
return { ok: false, error: 'Full third-party license text is required' };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const isMit = /^MIT License\s*$/mi.test(licenseText)
|
|
332
|
+
&& /Permission is hereby granted, free of charge/i.test(licenseText)
|
|
333
|
+
&& /above copyright notice/i.test(licenseText);
|
|
334
|
+
const isApache = /Apache License\s*Version 2\.0/i.test(licenseText)
|
|
335
|
+
&& /TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION/i.test(licenseText);
|
|
336
|
+
if (!isMit && !isApache) {
|
|
337
|
+
return { ok: false, error: 'Only reviewed MIT or Apache-2.0 license text is accepted' };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const manifest = {
|
|
341
|
+
schemaVersion: 1, name, version: '0.1.0', description: description.trim(),
|
|
342
|
+
capabilities: [], tools: [],
|
|
343
|
+
skills: [{
|
|
344
|
+
id: skillId, name: skillName.trim(),
|
|
345
|
+
description: skillDescription.trim(), file: 'SKILL.md'
|
|
346
|
+
}]
|
|
347
|
+
};
|
|
348
|
+
ensureRoot();
|
|
349
|
+
const directory = path.join(PLUGIN_ROOT, name);
|
|
350
|
+
if (fs.existsSync(directory)) return { ok: false, error: 'Plugin already exists' };
|
|
351
|
+
let created = false;
|
|
352
|
+
try {
|
|
353
|
+
fs.mkdirSync(directory, { mode: 0o700 });
|
|
354
|
+
created = true;
|
|
355
|
+
fs.writeFileSync(path.join(directory, 'SKILL.md'), skillContent, {
|
|
356
|
+
mode: 0o600, flag: 'wx'
|
|
357
|
+
});
|
|
358
|
+
fs.writeFileSync(path.join(directory, 'LICENSE'), licenseText, {
|
|
359
|
+
mode: 0o600, flag: 'wx'
|
|
360
|
+
});
|
|
361
|
+
fs.writeFileSync(path.join(directory, 'plugin.json'),
|
|
362
|
+
JSON.stringify(manifest, null, 2) + '\n', {
|
|
363
|
+
mode: 0o600, flag: 'wx'
|
|
364
|
+
});
|
|
365
|
+
const reviewed = readPlugin(directory);
|
|
366
|
+
return {
|
|
367
|
+
ok: true, name, enabled: false, tools: [],
|
|
368
|
+
skills: reviewed.skills.map(entry => entry.domain + '/' + entry.id),
|
|
369
|
+
license: isMit ? 'MIT' : 'Apache-2.0',
|
|
370
|
+
digest: reviewed.digest, path: directory,
|
|
371
|
+
message: 'Skill packaged disabled. Review its instructions and license before explicit enablement.'
|
|
372
|
+
};
|
|
373
|
+
} catch (error) {
|
|
374
|
+
if (created) {
|
|
375
|
+
try { fs.rmSync(directory, { recursive: true, force: true }); } catch {}
|
|
376
|
+
}
|
|
377
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
307
381
|
function enabledTools() {
|
|
308
382
|
const state = readState();
|
|
309
383
|
const tools = [];
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { canonicalModelId } from '../modelIds.js';
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
3
|
import { LLMProvider } from './base.js';
|
|
4
|
-
import { scanAndAdaptToolsForProvider } from '../tools/toolScanner.js';
|
|
4
|
+
import { scanAndAdaptToolsForProvider, normalizeToolName } from '../tools/toolScanner.js';
|
|
5
|
+
import {
|
|
6
|
+
prepareMessagesForAnthropic,
|
|
7
|
+
safeParseJsonObject,
|
|
8
|
+
sanitizeProviderToolId,
|
|
9
|
+
serializeAndDecodeArguments
|
|
10
|
+
} from './wireNormalizer.js';
|
|
5
11
|
|
|
6
12
|
// Models that support server-side tool search (defer_loading + tool_reference
|
|
7
13
|
// expansion). On these, Causyne hands Anthropic the complete 69-tool catalogue
|
|
@@ -203,6 +209,7 @@ export class AnthropicProvider extends LLMProvider {
|
|
|
203
209
|
}
|
|
204
210
|
|
|
205
211
|
async chat(messages, options = {}) {
|
|
212
|
+
messages = prepareMessagesForAnthropic(this, messages);
|
|
206
213
|
// Anthropic separates the system prompt from the message list.
|
|
207
214
|
const systemBlocks = [];
|
|
208
215
|
const anthropicMessages = [];
|
|
@@ -227,11 +234,11 @@ export class AnthropicProvider extends LLMProvider {
|
|
|
227
234
|
const isMidConversation = anthropicMessages.length > 0;
|
|
228
235
|
const lastEmitted = anthropicMessages[anthropicMessages.length - 1];
|
|
229
236
|
if (isMidConversation && allowInlineSystem && lastEmitted?.role === 'user') {
|
|
230
|
-
anthropicMessages.push({ role: 'system', content: String(msg.content) });
|
|
237
|
+
anthropicMessages.push({ role: 'system', content: String(msg.content ?? '') });
|
|
231
238
|
continue;
|
|
232
239
|
}
|
|
233
240
|
|
|
234
|
-
const block = { type: 'text', text: String(msg.content) };
|
|
241
|
+
const block = { type: 'text', text: String(msg.content ?? '') };
|
|
235
242
|
// Stable system blocks are cache anchors; everything volatile is ordered
|
|
236
243
|
// after them by the agent loop, which is what makes the prefix cacheable.
|
|
237
244
|
// Capped so the reserved tool breakpoint always fits within Anthropic's 4.
|
|
@@ -244,8 +251,8 @@ export class AnthropicProvider extends LLMProvider {
|
|
|
244
251
|
// Translate OpenAI tool response → Anthropic tool_result block
|
|
245
252
|
const toolResultBlock = {
|
|
246
253
|
type: 'tool_result',
|
|
247
|
-
tool_use_id: msg.tool_call_id,
|
|
248
|
-
content: msg.content
|
|
254
|
+
tool_use_id: sanitizeProviderToolId(msg.tool_call_id, 'toolu', 0),
|
|
255
|
+
content: msg.content ?? ''
|
|
249
256
|
};
|
|
250
257
|
// Anthropic requires consecutive tool_results to be grouped into a single 'user' message
|
|
251
258
|
const lastMsg = anthropicMessages[anthropicMessages.length - 1];
|
|
@@ -267,20 +274,21 @@ export class AnthropicProvider extends LLMProvider {
|
|
|
267
274
|
if (msg.content) {
|
|
268
275
|
contentBlocks.push({ type: 'text', text: msg.content });
|
|
269
276
|
}
|
|
270
|
-
for (
|
|
277
|
+
for (let idx = 0; idx < msg.tool_calls.length; idx++) {
|
|
278
|
+
const tc = msg.tool_calls[idx];
|
|
271
279
|
contentBlocks.push({
|
|
272
280
|
type: 'tool_use',
|
|
273
|
-
id: tc.id,
|
|
274
|
-
name: tc.function
|
|
275
|
-
input:
|
|
281
|
+
id: sanitizeProviderToolId(tc.id, 'toolu', idx),
|
|
282
|
+
name: normalizeToolName(tc.function?.name || ''),
|
|
283
|
+
input: safeParseJsonObject(tc.function?.arguments)
|
|
276
284
|
});
|
|
277
285
|
}
|
|
278
286
|
anthropicMessages.push({ role: 'assistant', content: contentBlocks });
|
|
279
287
|
} else {
|
|
280
288
|
// These are the loop's own bookkeeping. The API rejects unknown message
|
|
281
289
|
// properties, so every one of them has to be stripped here.
|
|
282
|
-
const { cacheAnchor, thinking_blocks, reasoning_content, ...clean } = msg;
|
|
283
|
-
anthropicMessages.push(clean);
|
|
290
|
+
const { cacheAnchor, thinking_blocks, reasoning_content, spillPath, ...clean } = msg;
|
|
291
|
+
anthropicMessages.push({ ...clean, content: clean.content ?? '' });
|
|
284
292
|
}
|
|
285
293
|
}
|
|
286
294
|
|
|
@@ -297,12 +305,18 @@ export class AnthropicProvider extends LLMProvider {
|
|
|
297
305
|
}
|
|
298
306
|
|
|
299
307
|
const thinking = this.buildThinking();
|
|
300
|
-
if (thinking)
|
|
308
|
+
if (thinking) {
|
|
309
|
+
if (thinking.type === 'enabled' && typeof thinking.budget_tokens === 'number' && payload.max_tokens > 1024) {
|
|
310
|
+
thinking.budget_tokens = Math.max(1024, Math.min(thinking.budget_tokens, payload.max_tokens - 1));
|
|
311
|
+
}
|
|
312
|
+
payload.thinking = thinking;
|
|
313
|
+
}
|
|
301
314
|
|
|
302
315
|
const effort = this.resolveEffort();
|
|
303
316
|
// effort lives inside output_config, not at the top level.
|
|
304
317
|
if (effort) payload.output_config = { ...(payload.output_config || {}), effort };
|
|
305
318
|
|
|
319
|
+
let freeFormPaths = new Map();
|
|
306
320
|
if (options.tools && options.tools.length > 0) {
|
|
307
321
|
const useToolSearch = this.supportsToolSearch();
|
|
308
322
|
const coreToolCount = Number.isInteger(options.coreToolCount) ? options.coreToolCount : options.tools.length;
|
|
@@ -312,6 +326,7 @@ export class AnthropicProvider extends LLMProvider {
|
|
|
312
326
|
coreToolCount
|
|
313
327
|
});
|
|
314
328
|
const converted = scanReport.adaptedTools;
|
|
329
|
+
freeFormPaths = scanReport.freeFormPaths;
|
|
315
330
|
|
|
316
331
|
if (useToolSearch) {
|
|
317
332
|
payload.tools = [
|
|
@@ -358,21 +373,22 @@ export class AnthropicProvider extends LLMProvider {
|
|
|
358
373
|
const thinkingBlocks = [];
|
|
359
374
|
let reasoningContent = '';
|
|
360
375
|
|
|
361
|
-
for (const block of data.content) {
|
|
376
|
+
for (const block of (Array.isArray(data.content) ? data.content : [])) {
|
|
362
377
|
if (block.type === 'thinking' || block.type === 'redacted_thinking') {
|
|
363
378
|
// Kept verbatim for the echo above. The readable summary, when the user
|
|
364
379
|
// asked for one, is surfaced separately.
|
|
365
380
|
thinkingBlocks.push(block);
|
|
366
381
|
if (typeof block.thinking === 'string') reasoningContent += block.thinking;
|
|
367
382
|
} else if (block.type === 'text') {
|
|
368
|
-
content += block.text;
|
|
383
|
+
content += block.text || '';
|
|
369
384
|
} else if (block.type === 'tool_use') {
|
|
385
|
+
const callName = normalizeToolName(block.name || '');
|
|
370
386
|
toolCalls.push({
|
|
371
|
-
id: block.id,
|
|
387
|
+
id: sanitizeProviderToolId(block.id, 'toolu', toolCalls.length),
|
|
372
388
|
type: 'function',
|
|
373
389
|
function: {
|
|
374
|
-
name:
|
|
375
|
-
arguments:
|
|
390
|
+
name: callName,
|
|
391
|
+
arguments: serializeAndDecodeArguments(block.input || {}, callName, freeFormPaths)
|
|
376
392
|
}
|
|
377
393
|
});
|
|
378
394
|
}
|
package/src/providers/base.js
CHANGED
|
@@ -250,6 +250,20 @@ export class LLMProvider {
|
|
|
250
250
|
*/
|
|
251
251
|
normalizeImageBlocks(msg) {
|
|
252
252
|
if (!Array.isArray(msg.content)) return msg;
|
|
253
|
+
if (this.imagePayloadFormat === 'anthropic') {
|
|
254
|
+
const content = msg.content.map(block => {
|
|
255
|
+
if (block && block.type === 'image_url' && typeof block.image_url?.url === 'string') {
|
|
256
|
+
const commaIdx = block.image_url.url.indexOf('base64,');
|
|
257
|
+
if (commaIdx !== -1 && block.image_url.url.startsWith('data:')) {
|
|
258
|
+
const mediaType = block.image_url.url.slice(5, commaIdx).split(';')[0] || 'image/png';
|
|
259
|
+
const data = block.image_url.url.slice(commaIdx + 7);
|
|
260
|
+
return { type: 'image', source: { type: 'base64', media_type: mediaType, data } };
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return block;
|
|
264
|
+
});
|
|
265
|
+
return { ...msg, content };
|
|
266
|
+
}
|
|
253
267
|
if (this.imagePayloadFormat !== 'openai') return msg;
|
|
254
268
|
|
|
255
269
|
const content = msg.content.map(block => {
|
|
@@ -2,6 +2,7 @@ import { canonicalModelId } from '../modelIds.js';
|
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
3
|
import { LLMProvider } from './base.js';
|
|
4
4
|
import { scanAndAdaptToolsForProvider } from '../tools/toolScanner.js';
|
|
5
|
+
import { normalizeOpenAIChatChoiceResponse } from './wireNormalizer.js';
|
|
5
6
|
|
|
6
7
|
// Direct DashScope API connection for BYOK (unlike dashscope.js which is the proxy for Free Demo)
|
|
7
8
|
export class DashscopeByokProvider extends LLMProvider {
|
|
@@ -44,10 +45,12 @@ export class DashscopeByokProvider extends LLMProvider {
|
|
|
44
45
|
payload.response_format = { type: 'json_object' };
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
|
|
48
|
+
let freeFormPaths = new Map();
|
|
49
|
+
if (options.tools && options.tools.length > 0) {
|
|
48
50
|
const scanReport = scanAndAdaptToolsForProvider(options.tools, 'dashscope');
|
|
49
51
|
payload.tools = scanReport.adaptedTools;
|
|
50
52
|
payload.tool_choice = "auto";
|
|
53
|
+
freeFormPaths = scanReport.freeFormPaths;
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
const response = await fetch(this.baseUrl, {
|
|
@@ -73,13 +76,16 @@ export class DashscopeByokProvider extends LLMProvider {
|
|
|
73
76
|
}
|
|
74
77
|
|
|
75
78
|
const data = await response.json();
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
const normalized = normalizeOpenAIChatChoiceResponse(data, {
|
|
80
|
+
prefix: 'call_dashscope',
|
|
81
|
+
freeFormPaths
|
|
82
|
+
});
|
|
83
|
+
|
|
79
84
|
return {
|
|
80
|
-
content: content
|
|
81
|
-
toolCalls:
|
|
82
|
-
|
|
85
|
+
content: normalized.content,
|
|
86
|
+
toolCalls: normalized.toolCalls,
|
|
87
|
+
reasoningContent: normalized.reasoningContent,
|
|
88
|
+
usage: this.normalizeUsage(normalized.rawUsage)
|
|
83
89
|
};
|
|
84
90
|
}
|
|
85
91
|
}
|
|
@@ -2,6 +2,7 @@ import { canonicalModelId } from '../modelIds.js';
|
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
3
|
import { LLMProvider } from './base.js';
|
|
4
4
|
import { scanAndAdaptToolsForProvider } from '../tools/toolScanner.js';
|
|
5
|
+
import { normalizeOpenAIChatChoiceResponse } from './wireNormalizer.js';
|
|
5
6
|
|
|
6
7
|
export class DeepSeekProvider extends LLMProvider {
|
|
7
8
|
constructor(config) {
|
|
@@ -38,10 +39,12 @@ export class DeepSeekProvider extends LLMProvider {
|
|
|
38
39
|
throw new Error("API key is required.");
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
let freeFormPaths = new Map();
|
|
43
|
+
if (options.tools && options.tools.length > 0) {
|
|
42
44
|
const scanReport = scanAndAdaptToolsForProvider(options.tools, 'deepseek');
|
|
43
45
|
payload.tools = scanReport.adaptedTools;
|
|
44
46
|
payload.tool_choice = "auto";
|
|
47
|
+
freeFormPaths = scanReport.freeFormPaths;
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
const response = await fetch(targetUrl, {
|
|
@@ -56,24 +59,16 @@ export class DeepSeekProvider extends LLMProvider {
|
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
const data = await response.json();
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// Proxy Format
|
|
65
|
-
content = data.reply || '';
|
|
66
|
-
toolCalls = data.tool_calls || [];
|
|
67
|
-
} else if (data.choices && data.choices[0] && data.choices[0].message) {
|
|
68
|
-
// Direct API Format
|
|
69
|
-
content = data.choices[0].message.content || '';
|
|
70
|
-
toolCalls = data.choices[0].message.tool_calls || [];
|
|
71
|
-
}
|
|
72
|
-
|
|
62
|
+
const normalized = normalizeOpenAIChatChoiceResponse(data, {
|
|
63
|
+
prefix: 'call_deepseek',
|
|
64
|
+
freeFormPaths
|
|
65
|
+
});
|
|
66
|
+
|
|
73
67
|
return {
|
|
74
|
-
content: content,
|
|
75
|
-
toolCalls:
|
|
76
|
-
|
|
68
|
+
content: normalized.content,
|
|
69
|
+
toolCalls: normalized.toolCalls,
|
|
70
|
+
reasoningContent: normalized.reasoningContent,
|
|
71
|
+
usage: this.normalizeUsage(normalized.rawUsage)
|
|
77
72
|
};
|
|
78
73
|
}
|
|
79
74
|
}
|
package/src/providers/gemini.js
CHANGED
|
@@ -5,8 +5,14 @@ import {
|
|
|
5
5
|
scanAndAdaptToolsForProvider,
|
|
6
6
|
decodeFreeFormArguments,
|
|
7
7
|
adaptSchemaForGemini,
|
|
8
|
-
isEmptyObjectSchema
|
|
8
|
+
isEmptyObjectSchema,
|
|
9
|
+
normalizeToolName
|
|
9
10
|
} from '../tools/toolScanner.js';
|
|
11
|
+
import {
|
|
12
|
+
safeParseJsonObject,
|
|
13
|
+
sanitizeProviderToolId,
|
|
14
|
+
serializeAndDecodeArguments
|
|
15
|
+
} from './wireNormalizer.js';
|
|
10
16
|
|
|
11
17
|
export { adaptSchemaForGemini, isEmptyObjectSchema };
|
|
12
18
|
|
|
@@ -134,25 +140,29 @@ export class GeminiProvider extends LLMProvider {
|
|
|
134
140
|
for (const tc of msg.tool_calls) {
|
|
135
141
|
parts.push({
|
|
136
142
|
functionCall: {
|
|
137
|
-
name: tc.function
|
|
138
|
-
args:
|
|
139
|
-
? JSON.parse(tc.function.arguments || '{}')
|
|
140
|
-
: (tc.function.arguments || {})
|
|
143
|
+
name: normalizeToolName(tc.function?.name || ''),
|
|
144
|
+
args: safeParseJsonObject(tc.function?.arguments)
|
|
141
145
|
}
|
|
142
146
|
});
|
|
143
147
|
}
|
|
144
148
|
contents.push({ role, parts });
|
|
145
149
|
} else if (msg.role === 'tool') {
|
|
146
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
150
|
+
// Batch consecutive tool responses into a single 'user' message for parallel calls.
|
|
151
|
+
const responsePart = {
|
|
152
|
+
functionResponse: {
|
|
153
|
+
name: normalizeToolName(msg.name || ''),
|
|
154
|
+
response: { result: msg.content ?? '' }
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const lastEntry = contents[contents.length - 1];
|
|
158
|
+
if (lastEntry && lastEntry.role === 'user' && Array.isArray(lastEntry.parts) && lastEntry.parts[0]?.functionResponse) {
|
|
159
|
+
lastEntry.parts.push(responsePart);
|
|
160
|
+
} else {
|
|
161
|
+
contents.push({
|
|
162
|
+
role: 'user',
|
|
163
|
+
parts: [responsePart]
|
|
164
|
+
});
|
|
165
|
+
}
|
|
156
166
|
} else {
|
|
157
167
|
if (Array.isArray(msg.content)) {
|
|
158
168
|
const mappedParts = msg.content.map(part => {
|
|
@@ -185,7 +195,7 @@ export class GeminiProvider extends LLMProvider {
|
|
|
185
195
|
});
|
|
186
196
|
contents.push({ role, parts: mappedParts });
|
|
187
197
|
} else {
|
|
188
|
-
contents.push({ role, parts: [{ text: msg.content }] });
|
|
198
|
+
contents.push({ role, parts: [{ text: msg.content ?? '' }] });
|
|
189
199
|
}
|
|
190
200
|
}
|
|
191
201
|
}
|
|
@@ -232,22 +242,20 @@ export class GeminiProvider extends LLMProvider {
|
|
|
232
242
|
|
|
233
243
|
let content = '';
|
|
234
244
|
let toolCalls = [];
|
|
245
|
+
const candidateParts = Array.isArray(firstCandidate.content?.parts) ? firstCandidate.content.parts : [];
|
|
235
246
|
|
|
236
|
-
for (const part of
|
|
247
|
+
for (const part of candidateParts) {
|
|
237
248
|
if (part.text) {
|
|
238
249
|
content += part.text;
|
|
239
250
|
} else if (part.functionCall) {
|
|
240
|
-
const
|
|
241
|
-
const
|
|
242
|
-
part.functionCall.args || {},
|
|
243
|
-
paths
|
|
244
|
-
);
|
|
251
|
+
const callName = normalizeToolName(part.functionCall.name || '');
|
|
252
|
+
const serializedArgs = serializeAndDecodeArguments(part.functionCall.args || {}, callName, freeFormPaths);
|
|
245
253
|
toolCalls.push({
|
|
246
|
-
id:
|
|
254
|
+
id: sanitizeProviderToolId(part.functionCall.id, 'call_gemini', toolCalls.length),
|
|
247
255
|
type: 'function',
|
|
248
256
|
function: {
|
|
249
|
-
name:
|
|
250
|
-
arguments:
|
|
257
|
+
name: callName,
|
|
258
|
+
arguments: serializedArgs
|
|
251
259
|
}
|
|
252
260
|
});
|
|
253
261
|
}
|
|
@@ -2,6 +2,7 @@ import { canonicalModelId } from '../modelIds.js';
|
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
3
|
import { LLMProvider } from './base.js';
|
|
4
4
|
import { scanAndAdaptToolsForProvider } from '../tools/toolScanner.js';
|
|
5
|
+
import { normalizeOpenAIChatChoiceResponse } from './wireNormalizer.js';
|
|
5
6
|
|
|
6
7
|
export class MoonshotProvider extends LLMProvider {
|
|
7
8
|
constructor(config) {
|
|
@@ -37,10 +38,12 @@ export class MoonshotProvider extends LLMProvider {
|
|
|
37
38
|
throw new Error(`Moonshot AI (Kimi) API requires a valid API key (BYOK mode). Please set it in your configuration.`);
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
let freeFormPaths = new Map();
|
|
42
|
+
if (options.tools && options.tools.length > 0) {
|
|
41
43
|
const scanReport = scanAndAdaptToolsForProvider(options.tools, 'moonshot');
|
|
42
44
|
payload.tools = scanReport.adaptedTools;
|
|
43
45
|
payload.tool_choice = "auto";
|
|
46
|
+
freeFormPaths = scanReport.freeFormPaths;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
const response = await fetch(this.baseUrl, {
|
|
@@ -55,18 +58,16 @@ export class MoonshotProvider extends LLMProvider {
|
|
|
55
58
|
}
|
|
56
59
|
|
|
57
60
|
const data = await response.json();
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
toolCalls = data.choices[0].message.tool_calls || [];
|
|
64
|
-
}
|
|
65
|
-
|
|
61
|
+
const normalized = normalizeOpenAIChatChoiceResponse(data, {
|
|
62
|
+
prefix: 'call_moonshot',
|
|
63
|
+
freeFormPaths
|
|
64
|
+
});
|
|
65
|
+
|
|
66
66
|
return {
|
|
67
|
-
content: content,
|
|
68
|
-
toolCalls:
|
|
69
|
-
|
|
67
|
+
content: normalized.content,
|
|
68
|
+
toolCalls: normalized.toolCalls,
|
|
69
|
+
reasoningContent: normalized.reasoningContent,
|
|
70
|
+
usage: this.normalizeUsage(normalized.rawUsage)
|
|
70
71
|
};
|
|
71
72
|
}
|
|
72
73
|
}
|
package/src/providers/ollama.js
CHANGED
|
@@ -2,6 +2,12 @@ import { canonicalModelId } from '../modelIds.js';
|
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
3
|
import { LLMProvider } from './base.js';
|
|
4
4
|
import { scanAndAdaptToolsForProvider } from '../tools/toolScanner.js';
|
|
5
|
+
import {
|
|
6
|
+
normalizeMessagesForOllama,
|
|
7
|
+
normalizeOpenAIToolCalls,
|
|
8
|
+
extractTextToolCalls
|
|
9
|
+
} from './wireNormalizer.js';
|
|
10
|
+
import { stripToolCallMarkup } from '../implementationPlan.js';
|
|
5
11
|
|
|
6
12
|
export class OllamaProvider extends LLMProvider {
|
|
7
13
|
constructor(config) {
|
|
@@ -42,7 +48,7 @@ export class OllamaProvider extends LLMProvider {
|
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
async chat(messages, options = {}) {
|
|
45
|
-
const sanitizedMessages = this.prepareMessages(messages);
|
|
51
|
+
const sanitizedMessages = normalizeMessagesForOllama(this.prepareMessages(messages));
|
|
46
52
|
|
|
47
53
|
const payload = {
|
|
48
54
|
model: this.model,
|
|
@@ -53,9 +59,11 @@ export class OllamaProvider extends LLMProvider {
|
|
|
53
59
|
}
|
|
54
60
|
};
|
|
55
61
|
|
|
56
|
-
|
|
62
|
+
let freeFormPaths = new Map();
|
|
63
|
+
if (options.tools && options.tools.length > 0) {
|
|
57
64
|
const scanReport = scanAndAdaptToolsForProvider(options.tools, 'ollama');
|
|
58
65
|
payload.tools = scanReport.adaptedTools;
|
|
66
|
+
freeFormPaths = scanReport.freeFormPaths;
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
const response = await fetch(this.baseUrl, {
|
|
@@ -71,28 +79,20 @@ export class OllamaProvider extends LLMProvider {
|
|
|
71
79
|
|
|
72
80
|
const data = await response.json();
|
|
73
81
|
|
|
74
|
-
// Ollama returns tool calls with no id
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
type: 'function',
|
|
84
|
-
function: {
|
|
85
|
-
name: call.function?.name,
|
|
86
|
-
// Ollama hands back a parsed object where the OpenAI shape carries a
|
|
87
|
-
// JSON string. Normalise so the whole pipeline sees one contract.
|
|
88
|
-
arguments: typeof call.function?.arguments === 'string'
|
|
89
|
-
? call.function.arguments
|
|
90
|
-
: JSON.stringify(call.function?.arguments || {})
|
|
82
|
+
// Ollama returns tool calls with no id and object arguments instead of JSON strings.
|
|
83
|
+
// Normalize through wireNormalizer so sanitizeSequences and toolSelfRepair see one contract.
|
|
84
|
+
let content = typeof data.message?.content === 'string' ? data.message.content : '';
|
|
85
|
+
let toolCalls = normalizeOpenAIToolCalls(data.message?.tool_calls, freeFormPaths, 'call_ollama');
|
|
86
|
+
if (toolCalls.length === 0 && content) {
|
|
87
|
+
const extracted = extractTextToolCalls(content, 'call_ollama_text', freeFormPaths);
|
|
88
|
+
if (extracted.length > 0) {
|
|
89
|
+
toolCalls = extracted;
|
|
90
|
+
content = stripToolCallMarkup(content).trim();
|
|
91
91
|
}
|
|
92
|
-
}
|
|
92
|
+
}
|
|
93
93
|
|
|
94
94
|
return {
|
|
95
|
-
content
|
|
95
|
+
content,
|
|
96
96
|
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
97
97
|
usage: {
|
|
98
98
|
promptTokens: data.prompt_eval_count || 0,
|
|
@@ -2,6 +2,7 @@ import { canonicalModelId } from '../modelIds.js';
|
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
3
|
import { LLMProvider } from './base.js';
|
|
4
4
|
import { scanAndAdaptToolsForProvider } from '../tools/toolScanner.js';
|
|
5
|
+
import { normalizeOpenAIChatChoiceResponse } from './wireNormalizer.js';
|
|
5
6
|
|
|
6
7
|
export class OpenAICompatibleProvider extends LLMProvider {
|
|
7
8
|
constructor(config) {
|
|
@@ -37,10 +38,12 @@ export class OpenAICompatibleProvider extends LLMProvider {
|
|
|
37
38
|
payload.response_format = { type: 'json_object' };
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
let freeFormPaths = new Map();
|
|
42
|
+
if (options.tools && options.tools.length > 0) {
|
|
41
43
|
const scanReport = scanAndAdaptToolsForProvider(options.tools, 'openai');
|
|
42
44
|
payload.tools = scanReport.adaptedTools;
|
|
43
45
|
payload.tool_choice = "auto";
|
|
46
|
+
freeFormPaths = scanReport.freeFormPaths;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
const response = await fetch(this.baseUrl, {
|
|
@@ -58,26 +61,16 @@ export class OpenAICompatibleProvider extends LLMProvider {
|
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
const data = await response.json();
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (data.choices && data.choices[0] && data.choices[0].message) {
|
|
67
|
-
const message = data.choices[0].message;
|
|
68
|
-
content = message.content || '';
|
|
69
|
-
toolCalls = message.tool_calls || [];
|
|
70
|
-
// DeepSeek and several compatible endpoints return the chain of thought
|
|
71
|
-
// in a sibling field. It was being discarded, so the reasoning the user
|
|
72
|
-
// paid for never reached them.
|
|
73
|
-
reasoningContent = message.reasoning_content || message.reasoning || '';
|
|
74
|
-
}
|
|
64
|
+
const normalized = normalizeOpenAIChatChoiceResponse(data, {
|
|
65
|
+
prefix: 'call_oai',
|
|
66
|
+
freeFormPaths
|
|
67
|
+
});
|
|
75
68
|
|
|
76
69
|
return {
|
|
77
|
-
content: content,
|
|
78
|
-
toolCalls:
|
|
79
|
-
reasoningContent: reasoningContent
|
|
80
|
-
usage: this.normalizeUsage(
|
|
70
|
+
content: normalized.content,
|
|
71
|
+
toolCalls: normalized.toolCalls,
|
|
72
|
+
reasoningContent: normalized.reasoningContent,
|
|
73
|
+
usage: this.normalizeUsage(normalized.rawUsage)
|
|
81
74
|
};
|
|
82
75
|
}
|
|
83
76
|
}
|