@causyne/agent 1.20.120 → 1.20.122
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 +3 -3
- package/package.json +1 -1
- package/src/acp/agentCatalog.js +81 -26
- package/src/acp/agentCatalog.ts +73 -29
- package/src/acp/client.js +210 -53
- package/src/acp/client.ts +202 -52
- package/src/acp/peerProfiles.js +55 -22
- package/src/acp/peerProfiles.ts +51 -22
- package/src/acp/protocol.js +37 -8
- package/src/acp/protocol.ts +30 -8
- package/src/acp/server.js +123 -25
- package/src/acp/server.ts +124 -24
- package/src/agent/errorClassifier.js +117 -0
- package/src/agent/errorClassifier.ts +165 -0
- package/src/agent/promptIntent.js +200 -0
- package/src/agent/promptIntent.ts +203 -0
- package/src/agent/toolArgValidator.js +97 -0
- package/src/agent/toolArgValidator.ts +112 -0
- package/src/agentLoop.js +33 -344
- package/src/agentLoop.ts +51 -352
- package/src/approvalGrants.js +19 -7
- package/src/approvalGrants.ts +20 -7
- package/src/approvalPolicy.js +48 -15
- package/src/approvalPolicy.ts +46 -15
- package/src/autoUpdate.js +116 -10
- package/src/autoUpdate.ts +149 -10
- package/src/browser/causyneBrowser.js +3 -2
- package/src/browser/causyneBrowser.ts +3 -2
- package/src/browser/searchProviders.js +1 -1
- package/src/browser/searchProviders.ts +1 -1
- package/src/cliMode.js +16 -2
- package/src/commands.js +10 -9
- package/src/config.js +288 -272
- package/src/config.ts +342 -0
- package/src/configVault.js +271 -0
- package/src/configVault.ts +273 -0
- package/src/continuum/compactor.js +2 -1
- package/src/continuum/compactor.ts +2 -1
- package/src/continuum/sanitizer.js +10 -3
- package/src/continuum/sanitizer.ts +9 -3
- package/src/continuum/ui/continuumPickerView.js +3 -2
- package/src/continuum/ui/continuumPickerView.ts +3 -2
- package/src/core/contextCompactor.js +70 -11
- package/src/core/contextCompactor.ts +83 -11
- package/src/cortex/vectorDb.js +26 -15
- package/src/cortex/vectorDb.ts +26 -16
- package/src/creative/liveBridge.js +7 -5
- package/src/creative/liveBridge.ts +7 -5
- package/src/creative/studioScripts.js +16 -5
- package/src/creative/studioScripts.ts +14 -5
- package/src/decisionPolicy.js +16 -4
- package/src/decisionPolicy.ts +14 -4
- package/src/documentEngine/antiSlopAuditor.js +10 -5
- package/src/documentEngine/antiSlopAuditor.ts +11 -5
- package/src/documentEngine/documentForge.js +47 -15
- package/src/documentEngine/documentForge.ts +43 -14
- package/src/dynamicCatalogue.js +42 -73
- package/src/dynamicCatalogue.ts +74 -60
- package/src/engines/design.js +2 -1
- package/src/engines/design.ts +2 -1
- package/src/engines/film.js +4 -4
- package/src/engines/film.ts +2 -2
- package/src/engines/light.js +2 -1
- package/src/engines/light.ts +2 -1
- package/src/engines/registry.js +1 -1
- package/src/engines/registry.ts +1 -1
- package/src/engines/reverseHarness.js +4 -3
- package/src/engines/reverseHarness.ts +4 -3
- package/src/entrypoint.js +77 -4
- package/src/headlessCli.js +305 -0
- package/src/headlessCli.ts +381 -0
- package/src/historyPruner.js +3 -2
- package/src/historyPruner.ts +3 -2
- package/src/hooks.js +3 -0
- package/src/hooks.ts +3 -0
- package/src/implementationPlan.js +21 -11
- package/src/implementationPlan.ts +21 -11
- package/src/infra/atomicWriter.js +4 -1
- package/src/infra/atomicWriter.ts +4 -1
- package/src/launchIntent.js +1 -1
- package/src/launchIntent.ts +1 -1
- package/src/mcp/mcpClient.js +38 -11
- package/src/mcp/mcpClient.ts +44 -11
- package/src/mcp/mcpRegistry.js +2 -5
- package/src/mcp/mcpRegistry.ts +2 -5
- package/src/mcp/mcpSecurity.js +189 -0
- package/src/mcp/mcpSecurity.ts +190 -0
- package/src/memory/scopedFacts.js +1 -1
- package/src/memory/scopedFacts.ts +1 -1
- package/src/memory/symbolFolder.js +1 -1
- package/src/memory/symbolFolder.ts +1 -1
- package/src/modelRouter.js +1 -1
- package/src/modelRouter.ts +1 -1
- package/src/observer/evidence.js +10 -4
- package/src/observer/evidence.ts +10 -4
- package/src/osKeychain.js +263 -0
- package/src/osKeychain.ts +287 -0
- package/src/perception.js +3 -2
- package/src/perception.ts +3 -2
- package/src/planCache.js +2 -2
- package/src/planCache.ts +2 -2
- package/src/pluginsFactory/pluginsFactory.js +19 -11
- package/src/pluginsFactory/pluginsFactory.ts +18 -11
- package/src/promptMentions.js +2 -2
- package/src/promptMentions.ts +2 -2
- package/src/providers/acp.js +1 -1
- package/src/providers/acp.ts +1 -1
- package/src/providers/anthropic.js +328 -385
- package/src/providers/anthropic.ts +386 -0
- package/src/providers/antigravity.js +10 -4
- package/src/providers/antigravity.ts +10 -4
- package/src/providers/base.js +221 -319
- package/src/providers/base.ts +340 -0
- package/src/providers/byokHttp.js +397 -0
- package/src/providers/byokHttp.ts +497 -0
- package/src/providers/byokStream.js +748 -0
- package/src/providers/byokStream.ts +863 -0
- package/src/providers/dashscope-byok.js +98 -81
- package/src/providers/dashscope-byok.ts +114 -0
- package/src/providers/deepseek.js +74 -64
- package/src/providers/deepseek.ts +99 -0
- package/src/providers/factory.js +35 -28
- package/src/providers/factory.ts +40 -0
- package/src/providers/gemini.js +229 -253
- package/src/providers/gemini.ts +261 -0
- package/src/providers/loopGuard.js +9 -5
- package/src/providers/loopGuard.ts +10 -5
- package/src/providers/moonshot.js +71 -64
- package/src/providers/moonshot.ts +93 -0
- package/src/providers/muse.js +82 -152
- package/src/providers/muse.ts +122 -0
- package/src/providers/ollama.js +116 -93
- package/src/providers/ollama.ts +135 -0
- package/src/providers/openai-compatible.js +114 -65
- package/src/providers/openai-compatible.ts +139 -0
- package/src/providers/openai.js +22 -15
- package/src/providers/openai.ts +30 -0
- package/src/providers/openrouter.js +194 -98
- package/src/providers/openrouter.ts +202 -97
- package/src/providers/pi.js +6 -0
- package/src/providers/pi.ts +6 -0
- package/src/providers/qwen-byok.js +97 -71
- package/src/providers/qwen-byok.ts +113 -0
- package/src/providers/toolContracts.js +40 -14
- package/src/providers/toolContracts.ts +35 -14
- package/src/providers/wireNormalizer.js +78 -11
- package/src/providers/wireNormalizer.ts +68 -12
- package/src/providers/xai.js +74 -64
- package/src/providers/xai.ts +97 -0
- package/src/reliability/assetEvidence.js +4 -2
- package/src/reliability/assetEvidence.ts +4 -2
- package/src/sandbox.js +486 -530
- package/src/sandbox.ts +552 -0
- package/src/sandboxPolicy.js +1287 -0
- package/src/sandboxPolicy.ts +1381 -0
- package/src/sessionJournal.js +55 -3
- package/src/sessionJournal.ts +58 -3
- package/src/skills/skillResourceSafety.js +19 -8
- package/src/skills/skillResourceSafety.ts +16 -7
- package/src/skillsManager.js +3 -3
- package/src/skillsManager.ts +3 -3
- package/src/smartToolsUse.js +28 -4
- package/src/smartToolsUse.ts +29 -7
- package/src/stallGuard.js +45 -22
- package/src/stallGuard.ts +46 -22
- package/src/subagentJournal.js +6 -4
- package/src/subagentJournal.ts +6 -4
- package/src/textDiff.js +6 -1
- package/src/textDiff.ts +6 -1
- package/src/timeMachine.js +7 -1
- package/src/timeMachine.ts +7 -1
- package/src/tokenTracker.js +43 -28
- package/src/tokenTracker.ts +44 -30
- package/src/toolPacks.js +3 -1
- package/src/toolPacks.ts +3 -1
- package/src/tools/archive.js +245 -35
- package/src/tools/archive.ts +244 -35
- package/src/tools/computerUse.js +7 -3
- package/src/tools/computerUse.ts +7 -3
- package/src/tools/creativeApps.js +13 -8
- package/src/tools/creativeApps.ts +22 -9
- package/src/tools/deferredMcpSearch.js +26 -14
- package/src/tools/deferredMcpSearch.ts +26 -14
- package/src/tools/desktopOpen.js +69 -10
- package/src/tools/desktopOpen.ts +70 -11
- package/src/tools/documentForge.js +167 -82
- package/src/tools/documentForge.ts +169 -84
- package/src/tools/fsEdit.js +101 -30
- package/src/tools/fsEdit.ts +105 -32
- package/src/tools/fsFind.js +8 -5
- package/src/tools/fsFind.ts +7 -4
- package/src/tools/fsRead.js +119 -13
- package/src/tools/fsRead.ts +124 -13
- package/src/tools/fsWrite.js +17 -5
- package/src/tools/fsWrite.ts +17 -7
- package/src/tools/generateMediaViaApi.js +82 -19
- package/src/tools/generateMediaViaApi.ts +80 -19
- package/src/tools/memoryRecall.js +62 -11
- package/src/tools/memoryRecall.ts +70 -13
- package/src/tools/memoryWrite.js +27 -14
- package/src/tools/memoryWrite.ts +30 -14
- package/src/tools/multiEdit.js +100 -30
- package/src/tools/multiEdit.ts +104 -32
- package/src/tools/nativeEngine.js +33 -18
- package/src/tools/nativeEngine.ts +31 -18
- package/src/tools/openSourceToolProvisioner.js +49 -14
- package/src/tools/openSourceToolProvisioner.ts +54 -15
- package/src/tools/osSandbox.js +513 -0
- package/src/tools/osSandbox.ts +588 -0
- package/src/tools/payloadSanitizer.js +40 -8
- package/src/tools/payloadSanitizer.ts +39 -8
- package/src/tools/peerAgent.js +96 -34
- package/src/tools/peerAgent.ts +114 -37
- package/src/tools/persistentShell.js +8 -3
- package/src/tools/pluginsFactory.js +38 -6
- package/src/tools/pluginsFactory.ts +42 -6
- package/src/tools/readNativeSkill.js +10 -8
- package/src/tools/readNativeSkill.ts +10 -8
- package/src/tools/requestDecision.js +10 -3
- package/src/tools/requestDecision.ts +10 -3
- package/src/tools/resolveAutomator.js +91 -17
- package/src/tools/resolveAutomator.ts +102 -17
- package/src/tools/reverseEngineering.js +168 -41
- package/src/tools/reverseEngineering.ts +161 -41
- package/src/tools/subprocessEnv.js +197 -0
- package/src/tools/subprocessEnv.ts +212 -0
- package/src/tools/sysRun.js +108 -35
- package/src/tools/sysRun.ts +119 -31
- package/src/tools/toolDispatch.js +24 -1
- package/src/tools/toolDispatch.ts +21 -1
- package/src/tools/toolOutputStore.js +62 -23
- package/src/tools/toolOutputStore.ts +56 -23
- package/src/tools/toolScanner.js +13 -10
- package/src/tools/toolScanner.ts +13 -10
- package/src/tools/toolSelfRepair.js +94 -28
- package/src/tools/toolSelfRepair.ts +96 -28
- package/src/tools/uiForge.js +71 -6
- package/src/tools/uiForge.ts +74 -6
- package/src/tools/viewImage.js +62 -17
- package/src/tools/viewImage.ts +57 -12
- package/src/tools/visionInspector.js +15 -3
- package/src/tools/visionInspector.ts +15 -3
- package/src/tools/walkerSafety.js +2 -0
- package/src/tools/walkerSafety.ts +1 -0
- package/src/tools/webFetch.js +105 -10
- package/src/tools/webFetch.ts +105 -11
- package/src/tui/banner.js +3 -2
- package/src/tui/banner.ts +3 -2
- package/src/tui/consoleGuard.js +12 -4
- package/src/tui/consoleGuard.ts +9 -4
- package/src/tui/messageInteraction.js +2 -3
- package/src/tui/messageInteraction.ts +2 -3
- package/src/tui/modelPicker.js +1 -0
- package/src/tui/modelPicker.ts +1 -0
- package/src/tui/overlay.js +3 -1
- package/src/tui/overlay.ts +3 -1
- package/src/tui/sessionPicker.js +3 -2
- package/src/tui/sessionPicker.ts +3 -2
- package/src/tui/sessionPickerView.js +7 -1
- package/src/tui/sessionPickerView.ts +6 -1
- package/src/tui/toolOutputNormalizer.js +2 -3
- package/src/tui/toolOutputNormalizer.ts +4 -3
- package/src/tui/viewport.js +4 -4
- package/src/tui/viewport.ts +5 -5
- package/src/tui/workspaceIdentity.js +2 -1
- package/src/tui/workspaceIdentity.ts +2 -1
- package/src/uncertainty.js +8 -3
- package/src/uncertainty.ts +8 -3
- package/src/utils/paths.js +8 -5
- package/src/utils/paths.ts +9 -7
- package/src/vfeTracker.js +9 -3
- package/src/vfeTracker.ts +9 -3
- package/src/config.d.ts +0 -2
- package/src/providers/base.d.ts +0 -94
- package/src/providers/factory.d.ts +0 -1
- package/src/sandbox.d.ts +0 -1
package/src/providers/base.js
CHANGED
|
@@ -1,342 +1,244 @@
|
|
|
1
1
|
import { canonicalModelId } from '../modelIds.js';
|
|
2
|
-
|
|
3
2
|
// Fields the agent loop attaches to messages for its own bookkeeping. They must
|
|
4
3
|
// never reach a provider API, which rejects unknown message properties.
|
|
5
4
|
const INTERNAL_MESSAGE_FIELDS = ['cacheAnchor', 'spillPath'];
|
|
6
|
-
|
|
7
|
-
// Container formats each vendor actually accepts for audio input. Kept here so
|
|
8
|
-
|
|
9
|
-
// instead of three drifting copies.
|
|
5
|
+
// Container formats each vendor accepts for audio input.
|
|
10
6
|
const AUDIO_MIME_BY_FORMAT = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
wav: 'audio/wav',
|
|
8
|
+
mp3: 'audio/mpeg',
|
|
9
|
+
m4a: 'audio/mp4',
|
|
10
|
+
aac: 'audio/aac',
|
|
11
|
+
ogg: 'audio/ogg',
|
|
12
|
+
flac: 'audio/flac',
|
|
13
|
+
webm: 'audio/webm'
|
|
18
14
|
};
|
|
19
|
-
|
|
20
15
|
/** Map a container format token to the MIME type a provider expects. */
|
|
21
16
|
export function mimeTypeForAudioFormat(format) {
|
|
22
|
-
|
|
17
|
+
const key = String(format || '').trim().toLowerCase();
|
|
18
|
+
return Object.hasOwn(AUDIO_MIME_BY_FORMAT, key) ? AUDIO_MIME_BY_FORMAT[key] : 'audio/wav';
|
|
23
19
|
}
|
|
24
|
-
|
|
25
20
|
/**
|
|
26
|
-
* Map a MIME type to the short format token OpenAI's input_audio
|
|
27
|
-
*
|
|
28
|
-
* OpenAI accepts only 'wav' and 'mp3'. Anything else is reported honestly as
|
|
29
|
-
* itself so the provider layer can reject it with a clear message rather than
|
|
30
|
-
* mislabelling an m4a as an mp3 and letting the vendor fail on decode — which
|
|
31
|
-
* is exactly what the previous hardcoded format:'mp3' did.
|
|
21
|
+
* Map a MIME type to the short format token OpenAI's input_audio expects.
|
|
32
22
|
*/
|
|
33
23
|
export function audioFormatForMimeType(mimeType) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
24
|
+
const mime = String(mimeType || '').toLowerCase().split(';')[0]?.trim() || '';
|
|
25
|
+
for (const [format, candidate] of Object.entries(AUDIO_MIME_BY_FORMAT)) {
|
|
26
|
+
if (candidate === mime)
|
|
27
|
+
return format;
|
|
28
|
+
}
|
|
29
|
+
if (mime === 'audio/mp3' || mime === 'audio/x-mpeg')
|
|
30
|
+
return 'mp3';
|
|
31
|
+
if (mime === 'audio/x-wav' || mime === 'audio/wave')
|
|
32
|
+
return 'wav';
|
|
33
|
+
if (mime === 'audio/x-m4a')
|
|
34
|
+
return 'm4a';
|
|
35
|
+
if (mime === 'audio/x-flac')
|
|
36
|
+
return 'flac';
|
|
37
|
+
if (mime === 'audio/x-aac')
|
|
38
|
+
return 'aac';
|
|
39
|
+
return 'wav';
|
|
42
40
|
}
|
|
43
|
-
|
|
44
41
|
/** True for an audio content block in either the OpenAI or the Gemini shape. */
|
|
45
42
|
export function isAudioBlock(block) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
if (!block || typeof block !== 'object')
|
|
44
|
+
return false;
|
|
45
|
+
if (block.type === 'input_audio')
|
|
46
|
+
return true;
|
|
47
|
+
return Boolean(block.inlineData && String(block.inlineData.mimeType || '').startsWith('audio/'));
|
|
49
48
|
}
|
|
50
|
-
|
|
51
49
|
export class LLMProvider {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
*/
|
|
135
|
-
async transcribeAudio() {
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
get canTranscribe() {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
get supportsAudio() {
|
|
145
|
-
if (this.audioOverride !== null) return this.audioOverride;
|
|
146
|
-
const model = (this.model || '').toLowerCase();
|
|
147
|
-
return this.audioModels.some(prefix => prefix === '*' || model.includes(prefix));
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Send a chat request to the LLM.
|
|
152
|
-
* @param {Array} messages - [{ role: 'system'|'user'|'assistant', content: '...' }]
|
|
153
|
-
* @param {Object} options - { temperature, maxTokens, jsonMode, tools }
|
|
154
|
-
* @returns {Promise<{ content: string, usage: { promptTokens: number, completionTokens: number } }>}
|
|
155
|
-
*/
|
|
156
|
-
async chat(messages, options = {}) {
|
|
157
|
-
throw new Error('chat() must be implemented by provider');
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Prepare a message list for this specific model.
|
|
162
|
-
*
|
|
163
|
-
* Providers used to hardcode "strip every image block" because their whole
|
|
164
|
-
* vendor was assumed blind. Now that vision is decided per model, that blanket
|
|
165
|
-
* strip would delete images from models that can actually read them — so the
|
|
166
|
-
* decision is made here, once, against supportsVision.
|
|
167
|
-
*/
|
|
168
|
-
prepareMessages(messages) {
|
|
169
|
-
const clean = this.stripInternalFields(messages);
|
|
170
|
-
|
|
171
|
-
// Vision and audio are decided separately. A model can read images and not
|
|
172
|
-
// hear audio (most of them), so the two strips cannot share one branch —
|
|
173
|
-
// an audio-blind vision model used to keep its input_audio block and 400.
|
|
174
|
-
const shaped = this.supportsVision
|
|
175
|
-
? clean.map(msg => this.normalizeImageBlocks(msg))
|
|
176
|
-
: clean.map(msg => this.flattenToTextOnly(msg));
|
|
177
|
-
|
|
178
|
-
return this.supportsAudio
|
|
179
|
-
? shaped.map(msg => this.normalizeAudioBlocks(msg))
|
|
180
|
-
: shaped.map(msg => this.stripAudioBlocks(msg));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Translate audio blocks into this provider's wire shape.
|
|
185
|
-
*
|
|
186
|
-
* The agent loop emits the OpenAI `input_audio` shape as the canonical form;
|
|
187
|
-
* Gemini needs `inlineData`. History replayed from an earlier session — or
|
|
188
|
-
* from before a provider switch — can carry either, so both are accepted.
|
|
189
|
-
*/
|
|
190
|
-
normalizeAudioBlocks(msg) {
|
|
191
|
-
if (!Array.isArray(msg.content)) return msg;
|
|
192
|
-
|
|
193
|
-
const content = msg.content.map(block => {
|
|
194
|
-
if (!block) return block;
|
|
195
|
-
|
|
196
|
-
if (this.audioPayloadFormat === 'gemini') {
|
|
197
|
-
if (block.type === 'input_audio') {
|
|
198
|
-
return {
|
|
199
|
-
inlineData: {
|
|
200
|
-
mimeType: mimeTypeForAudioFormat(block.input_audio?.format),
|
|
201
|
-
data: block.input_audio?.data
|
|
50
|
+
apiKey;
|
|
51
|
+
model;
|
|
52
|
+
baseUrl;
|
|
53
|
+
visionModels;
|
|
54
|
+
imagePayloadFormat;
|
|
55
|
+
visionOverride;
|
|
56
|
+
audioModels;
|
|
57
|
+
audioPayloadFormat;
|
|
58
|
+
maxAudioSeconds;
|
|
59
|
+
maxAudioBytes;
|
|
60
|
+
audioOverride;
|
|
61
|
+
preserveThinkingBlocks;
|
|
62
|
+
preserveReasoningContent;
|
|
63
|
+
constructor(config) {
|
|
64
|
+
const cfg = config && typeof config === 'object' ? config : {};
|
|
65
|
+
this.apiKey = cfg.apiKey;
|
|
66
|
+
this.model = String(canonicalModelId(cfg.model ?? '') || '');
|
|
67
|
+
this.baseUrl = cfg.baseUrl;
|
|
68
|
+
this.visionModels = [];
|
|
69
|
+
this.imagePayloadFormat = 'openai';
|
|
70
|
+
this.visionOverride = typeof cfg.supportsVision === 'boolean'
|
|
71
|
+
? cfg.supportsVision
|
|
72
|
+
: null;
|
|
73
|
+
this.audioModels = [];
|
|
74
|
+
this.audioPayloadFormat = 'openai';
|
|
75
|
+
this.maxAudioSeconds = null;
|
|
76
|
+
this.maxAudioBytes = null;
|
|
77
|
+
this.audioOverride = typeof cfg.supportsAudio === 'boolean'
|
|
78
|
+
? cfg.supportsAudio
|
|
79
|
+
: null;
|
|
80
|
+
this.preserveThinkingBlocks = false;
|
|
81
|
+
this.preserveReasoningContent = true;
|
|
82
|
+
}
|
|
83
|
+
get supportsVision() {
|
|
84
|
+
if (this.visionOverride !== null)
|
|
85
|
+
return this.visionOverride;
|
|
86
|
+
const model = (this.model || '').toLowerCase();
|
|
87
|
+
return this.visionModels.some(prefix => prefix === '*' || model.includes(prefix));
|
|
88
|
+
}
|
|
89
|
+
audioAlternative() {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
async transcribeAudio(_base64, _mimeType, _options) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
get canTranscribe() {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
get supportsAudio() {
|
|
99
|
+
if (this.audioOverride !== null)
|
|
100
|
+
return this.audioOverride;
|
|
101
|
+
const model = (this.model || '').toLowerCase();
|
|
102
|
+
return this.audioModels.some(prefix => prefix === '*' || model.includes(prefix));
|
|
103
|
+
}
|
|
104
|
+
async chat(_messages, _options = {}) {
|
|
105
|
+
throw new Error('chat() must be implemented by provider');
|
|
106
|
+
}
|
|
107
|
+
prepareMessages(messages) {
|
|
108
|
+
const clean = this.stripInternalFields(messages);
|
|
109
|
+
const shaped = this.supportsVision
|
|
110
|
+
? clean.map(msg => this.normalizeImageBlocks(msg))
|
|
111
|
+
: clean.map(msg => this.flattenToTextOnly(msg));
|
|
112
|
+
return this.supportsAudio
|
|
113
|
+
? shaped.map(msg => this.normalizeAudioBlocks(msg))
|
|
114
|
+
: shaped.map(msg => this.stripAudioBlocks(msg));
|
|
115
|
+
}
|
|
116
|
+
normalizeAudioBlocks(msg) {
|
|
117
|
+
if (!Array.isArray(msg.content))
|
|
118
|
+
return msg;
|
|
119
|
+
const content = msg.content.map((block) => {
|
|
120
|
+
if (!block || typeof block !== 'object')
|
|
121
|
+
return block;
|
|
122
|
+
if (this.audioPayloadFormat === 'gemini') {
|
|
123
|
+
if (block.type === 'input_audio') {
|
|
124
|
+
return {
|
|
125
|
+
inlineData: {
|
|
126
|
+
mimeType: mimeTypeForAudioFormat(block.input_audio?.format),
|
|
127
|
+
data: block.input_audio?.data
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return block;
|
|
202
132
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
* Translate image blocks into this provider's wire shape. The agent loop emits
|
|
248
|
-
* whichever shape imagePayloadFormat asks for, but history replayed from an
|
|
249
|
-
* earlier session — or from before a provider switch — can carry the other.
|
|
250
|
-
*/
|
|
251
|
-
normalizeImageBlocks(msg) {
|
|
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
|
-
}
|
|
133
|
+
if (block.inlineData && String(block.inlineData.mimeType || '').startsWith('audio/')) {
|
|
134
|
+
return {
|
|
135
|
+
type: 'input_audio',
|
|
136
|
+
input_audio: {
|
|
137
|
+
data: block.inlineData.data,
|
|
138
|
+
format: audioFormatForMimeType(block.inlineData.mimeType)
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return block;
|
|
143
|
+
});
|
|
144
|
+
return { ...msg, content };
|
|
145
|
+
}
|
|
146
|
+
stripAudioBlocks(msg) {
|
|
147
|
+
if (!Array.isArray(msg.content))
|
|
148
|
+
return msg;
|
|
149
|
+
if (!msg.content.some(isAudioBlock))
|
|
150
|
+
return msg;
|
|
151
|
+
const content = msg.content
|
|
152
|
+
.filter((block) => !isAudioBlock(block))
|
|
153
|
+
.concat([{
|
|
154
|
+
type: 'text',
|
|
155
|
+
text: '[Audio content removed — the selected model cannot process audio. Causyne transcribed the request on-device instead where possible.]'
|
|
156
|
+
}]);
|
|
157
|
+
return { ...msg, content };
|
|
158
|
+
}
|
|
159
|
+
normalizeImageBlocks(msg) {
|
|
160
|
+
if (!Array.isArray(msg.content))
|
|
161
|
+
return msg;
|
|
162
|
+
if (this.imagePayloadFormat === 'anthropic') {
|
|
163
|
+
const content = msg.content.map((block) => {
|
|
164
|
+
if (block && block.type === 'image_url' && typeof block.image_url?.url === 'string') {
|
|
165
|
+
const commaIdx = block.image_url.url.indexOf('base64,');
|
|
166
|
+
if (commaIdx !== -1 && block.image_url.url.startsWith('data:')) {
|
|
167
|
+
const rawMedia = (block.image_url.url.slice(5, commaIdx).split(';')[0] || 'image/png').trim().toLowerCase();
|
|
168
|
+
const mediaType = rawMedia === 'image/jpg' ? 'image/jpeg' : rawMedia;
|
|
169
|
+
const data = block.image_url.url.slice(commaIdx + 7);
|
|
170
|
+
if (data)
|
|
171
|
+
return { type: 'image', source: { type: 'base64', media_type: mediaType, data } };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return block;
|
|
175
|
+
});
|
|
176
|
+
return { ...msg, content };
|
|
262
177
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
178
|
+
if (this.imagePayloadFormat !== 'openai')
|
|
179
|
+
return msg;
|
|
180
|
+
const content = msg.content.map((block) => {
|
|
181
|
+
if (block && block.type === 'image' && block.source?.type === 'base64' && typeof block.source.data === 'string' && block.source.data) {
|
|
182
|
+
const mime = typeof block.source.media_type === 'string' && block.source.media_type ? block.source.media_type : 'image/png';
|
|
183
|
+
return {
|
|
184
|
+
type: 'image_url',
|
|
185
|
+
image_url: { url: `data:${mime};base64,${block.source.data}` }
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return block;
|
|
189
|
+
});
|
|
190
|
+
return { ...msg, content };
|
|
191
|
+
}
|
|
192
|
+
flattenToTextOnly(msg) {
|
|
193
|
+
if (!Array.isArray(msg.content))
|
|
194
|
+
return msg;
|
|
195
|
+
const audioBlocks = this.supportsAudio ? msg.content.filter(isAudioBlock) : [];
|
|
196
|
+
const textParts = msg.content
|
|
197
|
+
.filter((block) => block && typeof block === 'object' && block.type === 'text' && typeof block.text === 'string')
|
|
198
|
+
.map((block) => block.text);
|
|
199
|
+
const text = textParts.join('\n') ||
|
|
200
|
+
'[Image content removed — not supported by this model]';
|
|
201
|
+
if (audioBlocks.length === 0)
|
|
202
|
+
return { ...msg, content: text };
|
|
203
|
+
return { ...msg, content: [{ type: 'text', text }, ...audioBlocks] };
|
|
266
204
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
205
|
+
stripInternalFields(messages) {
|
|
206
|
+
if (!Array.isArray(messages))
|
|
207
|
+
return [];
|
|
208
|
+
const fieldsToStrip = [...INTERNAL_MESSAGE_FIELDS, 'thoughtSignature'];
|
|
209
|
+
if (!this.preserveThinkingBlocks)
|
|
210
|
+
fieldsToStrip.push('thinking_blocks');
|
|
211
|
+
if (!this.preserveReasoningContent)
|
|
212
|
+
fieldsToStrip.push('reasoning_content');
|
|
213
|
+
return messages.filter((msg) => msg && typeof msg === 'object').map(msg => {
|
|
214
|
+
let needsCopy = false;
|
|
215
|
+
for (const field of fieldsToStrip) {
|
|
216
|
+
if (field in msg) {
|
|
217
|
+
needsCopy = true;
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (!needsCopy)
|
|
222
|
+
return msg;
|
|
223
|
+
const clean = { ...msg };
|
|
224
|
+
for (const field of fieldsToStrip)
|
|
225
|
+
delete clean[field];
|
|
226
|
+
return clean;
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
normalizeUsage(rawUsage) {
|
|
230
|
+
const nonNeg = (v) => (typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : 0);
|
|
231
|
+
const promptTokens = nonNeg(rawUsage?.prompt_tokens);
|
|
232
|
+
const completionTokens = nonNeg(rawUsage?.completion_tokens);
|
|
233
|
+
const rawCached = rawUsage?.prompt_tokens_details?.cached_tokens ??
|
|
234
|
+
rawUsage?.prompt_cache_hit_tokens ??
|
|
235
|
+
rawUsage?.cache_read_input_tokens ??
|
|
236
|
+
0;
|
|
237
|
+
const cachedPromptTokens = Math.min(nonNeg(rawCached), promptTokens);
|
|
271
238
|
return {
|
|
272
|
-
|
|
273
|
-
|
|
239
|
+
promptTokens,
|
|
240
|
+
completionTokens,
|
|
241
|
+
cachedPromptTokens
|
|
274
242
|
};
|
|
275
|
-
|
|
276
|
-
return block;
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
return { ...msg, content };
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Collapse multimodal content to plain text for a model that cannot see.
|
|
284
|
-
*
|
|
285
|
-
* Audio blocks are deliberately preserved here rather than filtered out:
|
|
286
|
-
* this runs before the audio strip in prepareMessages, and a model that is
|
|
287
|
-
* blind is not necessarily deaf. Flattening to a bare string at this point
|
|
288
|
-
* would destroy an audio block that the audio branch was about to keep.
|
|
289
|
-
*/
|
|
290
|
-
flattenToTextOnly(msg) {
|
|
291
|
-
if (!Array.isArray(msg.content)) return msg;
|
|
292
|
-
|
|
293
|
-
const audioBlocks = this.supportsAudio ? msg.content.filter(isAudioBlock) : [];
|
|
294
|
-
|
|
295
|
-
const textParts = msg.content
|
|
296
|
-
.filter(block => block.type === 'text')
|
|
297
|
-
.map(block => block.text);
|
|
298
|
-
|
|
299
|
-
const text = textParts.join('\n') ||
|
|
300
|
-
'[Image content removed — not supported by this model]';
|
|
301
|
-
|
|
302
|
-
if (audioBlocks.length === 0) return { ...msg, content: text };
|
|
303
|
-
|
|
304
|
-
return { ...msg, content: [{ type: 'text', text }, ...audioBlocks] };
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/** Remove internal bookkeeping fields before sending messages upstream. */
|
|
308
|
-
stripInternalFields(messages) {
|
|
309
|
-
return messages.map(msg => {
|
|
310
|
-
let needsCopy = false;
|
|
311
|
-
for (const field of INTERNAL_MESSAGE_FIELDS) {
|
|
312
|
-
if (field in msg) { needsCopy = true; break; }
|
|
313
|
-
}
|
|
314
|
-
if (!needsCopy) return msg;
|
|
315
|
-
|
|
316
|
-
const clean = { ...msg };
|
|
317
|
-
for (const field of INTERNAL_MESSAGE_FIELDS) delete clean[field];
|
|
318
|
-
return clean;
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// Normalize usage across different provider response formats.
|
|
323
|
-
//
|
|
324
|
-
// Cached prompt tokens are surfaced separately because they are billed at a
|
|
325
|
-
// fraction of the base input rate: OpenAI-style APIs report them under
|
|
326
|
-
// prompt_tokens_details.cached_tokens, DeepSeek under prompt_cache_hit_tokens.
|
|
327
|
-
|
|
328
|
-
// disfavour once caching starts working.
|
|
329
|
-
normalizeUsage(rawUsage) {
|
|
330
|
-
const promptTokens = rawUsage?.prompt_tokens || 0;
|
|
331
|
-
const cachedPromptTokens =
|
|
332
|
-
rawUsage?.prompt_tokens_details?.cached_tokens ??
|
|
333
|
-
rawUsage?.prompt_cache_hit_tokens ??
|
|
334
|
-
0;
|
|
335
|
-
|
|
336
|
-
return {
|
|
337
|
-
promptTokens,
|
|
338
|
-
completionTokens: rawUsage?.completion_tokens || 0,
|
|
339
|
-
cachedPromptTokens: Math.min(cachedPromptTokens, promptTokens)
|
|
340
|
-
};
|
|
341
|
-
}
|
|
243
|
+
}
|
|
342
244
|
}
|