neoagent 3.2.1-beta.2 → 3.2.1-beta.4
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/extensions/chrome-browser/background.mjs +318 -88
- package/extensions/chrome-browser/http.mjs +136 -0
- package/extensions/chrome-browser/protocol.mjs +511 -89
- package/flutter_app/lib/main_chat.dart +118 -739
- package/flutter_app/lib/main_controller.dart +29 -20
- package/flutter_app/lib/main_models.dart +3 -0
- package/flutter_app/lib/main_operations.dart +334 -321
- package/flutter_app/lib/main_settings.dart +4 -3
- package/flutter_app/lib/main_shared.dart +14 -11
- package/flutter_app/lib/src/desktop_companion_actions.dart +185 -31
- package/flutter_app/lib/src/desktop_companion_io.dart +319 -86
- package/flutter_app/windows/runner/flutter_window.cpp +143 -32
- package/lib/manager.js +106 -89
- package/lib/schema_migrations.js +67 -13
- package/package.json +20 -13
- package/runtime/paths.js +49 -5
- package/server/guest_agent.js +52 -30
- package/server/http/middleware.js +24 -0
- package/server/http/routes.js +4 -6
- package/server/public/.last_build_id +1 -1
- package/server/public/assets/fonts/MaterialIcons-Regular.otf +0 -0
- package/server/public/flutter_bootstrap.js +1 -1
- package/server/public/main.dart.js +30803 -30805
- package/server/routes/admin.js +1 -1
- package/server/routes/android.js +30 -34
- package/server/routes/browser.js +23 -15
- package/server/routes/desktop.js +18 -1
- package/server/routes/integrations.js +5 -1
- package/server/routes/memory.js +1 -0
- package/server/routes/settings.js +16 -5
- package/server/routes/social_reach.js +12 -3
- package/server/routes/social_video.js +4 -0
- package/server/services/ai/compaction.js +7 -2
- package/server/services/ai/history.js +44 -5
- package/server/services/ai/integrated_tools/http_request.js +8 -0
- package/server/services/ai/loop/agent_engine_core.js +347 -162
- package/server/services/ai/loop/callbacks.js +1 -0
- package/server/services/ai/loop/completion_judge.js +12 -0
- package/server/services/ai/loop/conversation_loop.js +348 -242
- package/server/services/ai/loop/messaging_delivery.js +129 -57
- package/server/services/ai/loop/model_call_guard.js +91 -0
- package/server/services/ai/loop/model_io.js +8 -41
- package/server/services/ai/loop/tool_dispatch.js +19 -8
- package/server/services/ai/loopPolicy.js +24 -19
- package/server/services/ai/model_discovery.js +227 -0
- package/server/services/ai/model_identity.js +71 -0
- package/server/services/ai/models.js +67 -162
- package/server/services/ai/providerRetry.js +17 -59
- package/server/services/ai/provider_selector.js +111 -0
- package/server/services/ai/providers/anthropic.js +2 -2
- package/server/services/ai/providers/claudeCode.js +15 -28
- package/server/services/ai/providers/githubCopilot.js +36 -16
- package/server/services/ai/providers/google.js +23 -5
- package/server/services/ai/providers/grok.js +4 -3
- package/server/services/ai/providers/grokOauth.js +13 -22
- package/server/services/ai/providers/nvidia.js +10 -5
- package/server/services/ai/providers/ollama.js +102 -82
- package/server/services/ai/providers/ollama_stream.js +142 -0
- package/server/services/ai/providers/openai.js +39 -5
- package/server/services/ai/providers/openaiCodex.js +11 -4
- package/server/services/ai/providers/openrouter.js +29 -7
- package/server/services/ai/providers/provider_error.js +36 -0
- package/server/services/ai/settings.js +26 -2
- package/server/services/ai/taskAnalysis.js +5 -1
- package/server/services/ai/terminal_reply.js +45 -0
- package/server/services/ai/toolEvidence.js +58 -29
- package/server/services/ai/tools.js +124 -111
- package/server/services/android/controller.js +770 -237
- package/server/services/android/process.js +140 -0
- package/server/services/android/sdk_download.js +143 -0
- package/server/services/android/uia.js +6 -5
- package/server/services/artifacts/store.js +24 -0
- package/server/services/browser/controller.js +736 -385
- package/server/services/browser/extension/gateway.js +40 -16
- package/server/services/browser/extension/protocol.js +12 -1
- package/server/services/browser/extension/provider.js +59 -47
- package/server/services/browser/extension/registry.js +155 -34
- package/server/services/cli/executor.js +62 -9
- package/server/services/desktop/gateway.js +41 -4
- package/server/services/desktop/protocol.js +3 -0
- package/server/services/desktop/provider.js +39 -42
- package/server/services/desktop/registry.js +137 -52
- package/server/services/integrations/figma/provider.js +78 -12
- package/server/services/integrations/github/common.js +11 -6
- package/server/services/integrations/github/provider.js +52 -53
- package/server/services/integrations/google/provider.js +55 -19
- package/server/services/integrations/home_assistant/network.js +17 -20
- package/server/services/integrations/home_assistant/provider.js +7 -5
- package/server/services/integrations/home_assistant/tools.js +17 -5
- package/server/services/integrations/http.js +51 -0
- package/server/services/integrations/manager.js +158 -53
- package/server/services/integrations/microsoft/provider.js +80 -13
- package/server/services/integrations/neoarchive/provider.js +55 -29
- package/server/services/integrations/neorecall/client.js +17 -10
- package/server/services/integrations/neorecall/provider.js +20 -11
- package/server/services/integrations/notion/provider.js +16 -13
- package/server/services/integrations/oauth_provider.js +115 -51
- package/server/services/integrations/slack/provider.js +98 -9
- package/server/services/integrations/spotify/provider.js +67 -71
- package/server/services/integrations/trello/provider.js +21 -7
- package/server/services/integrations/weather/provider.js +18 -12
- package/server/services/integrations/whatsapp/provider.js +76 -16
- package/server/services/manager.js +87 -1
- package/server/services/memory/embedding_index.js +20 -8
- package/server/services/memory/embeddings.js +151 -90
- package/server/services/memory/ingestion.js +50 -9
- package/server/services/memory/ingestion_documents.js +13 -3
- package/server/services/memory/manager.js +52 -19
- package/server/services/messaging/automation.js +84 -9
- package/server/services/messaging/http_platforms.js +33 -13
- package/server/services/messaging/inbound_queue.js +78 -24
- package/server/services/messaging/inbound_store.js +224 -0
- package/server/services/messaging/manager.js +326 -51
- package/server/services/messaging/typing_keepalive.js +5 -2
- package/server/services/messaging/whatsapp.js +22 -14
- package/server/services/network/http.js +210 -0
- package/server/services/network/safe_request.js +307 -0
- package/server/services/runtime/backends/local-vm.js +214 -66
- package/server/services/runtime/manager.js +17 -12
- package/server/services/social_reach/channels/github.js +10 -4
- package/server/services/social_reach/channels/reddit.js +4 -4
- package/server/services/social_reach/channels/rss.js +2 -2
- package/server/services/social_reach/channels/social_video.js +12 -7
- package/server/services/social_reach/channels/v2ex.js +21 -8
- package/server/services/social_reach/channels/x.js +2 -2
- package/server/services/social_reach/channels/xueqiu.js +5 -5
- package/server/services/social_reach/service.js +9 -6
- package/server/services/social_reach/utils.js +65 -14
- package/server/services/social_video/service.js +160 -50
- package/server/services/tasks/integration_runtime.js +18 -8
- package/server/services/tasks/runtime.js +39 -4
- package/server/services/voice/agentBridge.js +17 -4
- package/server/services/voice/bufferedLiveRelayAdapter.js +5 -0
- package/server/services/voice/liveSession.js +31 -0
- package/server/services/voice/openaiSpeech.js +33 -8
- package/server/services/voice/providers.js +233 -151
- package/server/services/voice/runtimeManager.js +118 -20
- package/server/services/voice/turnRunner.js +6 -0
- package/server/services/wearable/firmware_manifest.js +51 -13
- package/server/services/wearable/service.js +1 -0
- package/server/utils/abort.js +96 -0
- package/server/utils/cloud-security.js +110 -3
- package/server/utils/files.js +31 -0
- package/server/utils/image_payload.js +95 -0
- package/server/utils/retry.js +107 -0
|
@@ -1,4 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
1
3
|
const { BaseProvider } = require('./base');
|
|
4
|
+
const { fetchResponseText, readResponseText } = require('../../network/http');
|
|
5
|
+
const { createAbortError, isAbortError, throwIfAborted } = require('../../../utils/abort');
|
|
6
|
+
const { readOllamaStream } = require('./ollama_stream');
|
|
7
|
+
|
|
8
|
+
const MAX_CHAT_RESPONSE_BYTES = 16 * 1024 * 1024;
|
|
9
|
+
|
|
10
|
+
function ollamaError(message, status = null) {
|
|
11
|
+
const error = new Error(
|
|
12
|
+
status == null
|
|
13
|
+
? `Ollama request failed: ${message}`
|
|
14
|
+
: `Ollama request failed (HTTP ${status}): ${message}`,
|
|
15
|
+
);
|
|
16
|
+
if (status != null) error.status = status;
|
|
17
|
+
if (/does not support tools|tools.*not supported/i.test(String(message || ''))) {
|
|
18
|
+
error.code = 'OLLAMA_TOOLS_UNSUPPORTED';
|
|
19
|
+
}
|
|
20
|
+
return error;
|
|
21
|
+
}
|
|
2
22
|
|
|
3
23
|
class OllamaProvider extends BaseProvider {
|
|
4
24
|
constructor(config = {}) {
|
|
@@ -9,22 +29,20 @@ class OllamaProvider extends BaseProvider {
|
|
|
9
29
|
}
|
|
10
30
|
|
|
11
31
|
async listModels(signal = null) {
|
|
12
|
-
const controller = new AbortController();
|
|
13
|
-
const abortFromParent = () => controller.abort(signal?.reason);
|
|
14
|
-
if (signal?.aborted) abortFromParent();
|
|
15
|
-
else signal?.addEventListener('abort', abortFromParent, { once: true });
|
|
16
|
-
const timer = setTimeout(() => controller.abort(), 5000);
|
|
17
32
|
try {
|
|
18
|
-
const
|
|
19
|
-
|
|
33
|
+
const { response, text } = await fetchResponseText(`${this.baseUrl}/api/tags`, {
|
|
34
|
+
maxResponseBytes: 2 * 1024 * 1024,
|
|
35
|
+
serviceName: 'Ollama model catalog',
|
|
36
|
+
signal,
|
|
37
|
+
timeoutMs: 5000,
|
|
38
|
+
});
|
|
39
|
+
if (!response.ok) throw new Error(`Ollama /api/tags returned HTTP ${response.status}`);
|
|
40
|
+
const data = JSON.parse(text || '{}');
|
|
20
41
|
this.models = (data.models || []).map(m => m.name);
|
|
21
42
|
return this.models;
|
|
22
43
|
} catch (err) {
|
|
23
|
-
if (signal
|
|
44
|
+
if (isAbortError(err, signal)) throw createAbortError(signal);
|
|
24
45
|
return [];
|
|
25
|
-
} finally {
|
|
26
|
-
clearTimeout(timer);
|
|
27
|
-
signal?.removeEventListener('abort', abortFromParent);
|
|
28
46
|
}
|
|
29
47
|
}
|
|
30
48
|
|
|
@@ -45,13 +63,22 @@ class OllamaProvider extends BaseProvider {
|
|
|
45
63
|
message: `Downloading local Ollama model '${model}'. First-time pulls can take a while.`
|
|
46
64
|
});
|
|
47
65
|
try {
|
|
48
|
-
const
|
|
66
|
+
const { response, text } = await fetchResponseText(`${this.baseUrl}/api/pull`, {
|
|
49
67
|
method: 'POST',
|
|
50
68
|
headers: { 'Content-Type': 'application/json' },
|
|
51
69
|
body: JSON.stringify({ name: model, stream: false }),
|
|
70
|
+
maxResponseBytes: 2 * 1024 * 1024,
|
|
71
|
+
serviceName: 'Ollama model pull',
|
|
52
72
|
signal,
|
|
73
|
+
timeoutMs: 5 * 60 * 1000,
|
|
53
74
|
});
|
|
54
|
-
if (!
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
let detail = text;
|
|
77
|
+
try { detail = JSON.parse(text || '{}').error || text; } catch {}
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Pull failed with HTTP ${response.status}: ${String(detail || response.statusText).slice(0, 500)}`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
55
82
|
console.log(`[Ollama] Model '${model}' pulled successfully.`);
|
|
56
83
|
this.onStatus?.({
|
|
57
84
|
kind: 'model_download',
|
|
@@ -64,6 +91,7 @@ class OllamaProvider extends BaseProvider {
|
|
|
64
91
|
await this.listModels(signal);
|
|
65
92
|
return true;
|
|
66
93
|
} catch (e) {
|
|
94
|
+
if (isAbortError(e, signal)) throw createAbortError(signal);
|
|
67
95
|
this.onStatus?.({
|
|
68
96
|
kind: 'model_download',
|
|
69
97
|
status: 'failed',
|
|
@@ -137,35 +165,55 @@ class OllamaProvider extends BaseProvider {
|
|
|
137
165
|
signal,
|
|
138
166
|
});
|
|
139
167
|
if (!res.ok) {
|
|
140
|
-
const detail = await res
|
|
168
|
+
const detail = await readResponseText(res, {
|
|
169
|
+
maxResponseBytes: 64 * 1024,
|
|
170
|
+
serviceName: 'Ollama chat error',
|
|
171
|
+
}).catch(() => '');
|
|
141
172
|
let message = detail;
|
|
142
173
|
try { message = JSON.parse(detail)?.error || detail; } catch {}
|
|
143
|
-
|
|
144
|
-
if (/does not support tools|tools.*not supported/i.test(message)) {
|
|
145
|
-
err.code = 'OLLAMA_TOOLS_UNSUPPORTED';
|
|
146
|
-
}
|
|
147
|
-
throw err;
|
|
174
|
+
throw ollamaError(message || res.statusText, res.status);
|
|
148
175
|
}
|
|
149
176
|
return res;
|
|
150
177
|
}
|
|
151
178
|
|
|
179
|
+
async readChatResponse(body, signal = null) {
|
|
180
|
+
const res = await this.postChat(body, signal);
|
|
181
|
+
const text = await readResponseText(res, {
|
|
182
|
+
maxResponseBytes: MAX_CHAT_RESPONSE_BYTES,
|
|
183
|
+
serviceName: 'Ollama chat',
|
|
184
|
+
});
|
|
185
|
+
throwIfAborted(signal, 'Ollama chat aborted.');
|
|
186
|
+
let data;
|
|
187
|
+
try {
|
|
188
|
+
data = JSON.parse(text);
|
|
189
|
+
} catch (cause) {
|
|
190
|
+
throw new Error('Ollama /api/chat returned malformed JSON.', { cause });
|
|
191
|
+
}
|
|
192
|
+
if (data.error) throw ollamaError(data.error);
|
|
193
|
+
return data;
|
|
194
|
+
}
|
|
195
|
+
|
|
152
196
|
async chat(messages, tools = [], options = {}) {
|
|
153
197
|
const model = options.model || this.config.model || 'llama3.1';
|
|
154
198
|
await this.ensureModel(model, options.signal);
|
|
155
199
|
|
|
156
|
-
let
|
|
200
|
+
let data;
|
|
157
201
|
try {
|
|
158
|
-
|
|
202
|
+
data = await this.readChatResponse(
|
|
203
|
+
this.buildChatBody(messages, tools, { ...options, model }, false),
|
|
204
|
+
options.signal,
|
|
205
|
+
);
|
|
159
206
|
} catch (err) {
|
|
160
207
|
if (err.code === 'OLLAMA_TOOLS_UNSUPPORTED' && tools.length > 0) {
|
|
161
208
|
console.warn(`[Ollama] Model '${model}' does not support tools; retrying without them.`);
|
|
162
|
-
|
|
209
|
+
data = await this.readChatResponse(
|
|
210
|
+
this.buildChatBody(messages, [], { ...options, model }, false),
|
|
211
|
+
options.signal,
|
|
212
|
+
);
|
|
163
213
|
} else {
|
|
164
214
|
throw err;
|
|
165
215
|
}
|
|
166
216
|
}
|
|
167
|
-
|
|
168
|
-
const data = await res.json();
|
|
169
217
|
const msg = data.message || {};
|
|
170
218
|
|
|
171
219
|
return {
|
|
@@ -192,65 +240,37 @@ class OllamaProvider extends BaseProvider {
|
|
|
192
240
|
const model = options.model || this.config.model || 'llama3.1';
|
|
193
241
|
await this.ensureModel(model, options.signal);
|
|
194
242
|
|
|
195
|
-
let
|
|
196
|
-
|
|
197
|
-
res = await this.postChat(this.buildChatBody(messages, tools, { ...options, model }, true), options.signal);
|
|
198
|
-
} catch (err) {
|
|
199
|
-
if (err.code === 'OLLAMA_TOOLS_UNSUPPORTED' && tools.length > 0) {
|
|
200
|
-
console.warn(`[Ollama] Model '${model}' does not support tools; retrying stream without them.`);
|
|
201
|
-
res = await this.postChat(this.buildChatBody(messages, [], { ...options, model }, true), options.signal);
|
|
202
|
-
} else {
|
|
203
|
-
throw err;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const reader = res.body.getReader();
|
|
208
|
-
const decoder = new TextDecoder();
|
|
209
|
-
let content = '';
|
|
210
|
-
let buffer = '';
|
|
211
|
-
let accumulatedToolCalls = [];
|
|
212
|
-
|
|
243
|
+
let requestTools = tools;
|
|
244
|
+
let retriedWithoutTools = false;
|
|
213
245
|
while (true) {
|
|
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
|
-
type: 'done',
|
|
243
|
-
content,
|
|
244
|
-
toolCalls: accumulatedToolCalls,
|
|
245
|
-
finishReason: accumulatedToolCalls.length > 0 ? 'tool_calls' : 'stop',
|
|
246
|
-
usage: data.prompt_eval_count ? {
|
|
247
|
-
promptTokens: data.prompt_eval_count || 0,
|
|
248
|
-
completionTokens: data.eval_count || 0,
|
|
249
|
-
totalTokens: (data.prompt_eval_count || 0) + (data.eval_count || 0)
|
|
250
|
-
} : null
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
} catch {}
|
|
246
|
+
let emittedChunk = false;
|
|
247
|
+
try {
|
|
248
|
+
const res = await this.postChat(
|
|
249
|
+
this.buildChatBody(messages, requestTools, { ...options, model }, true),
|
|
250
|
+
options.signal,
|
|
251
|
+
);
|
|
252
|
+
for await (const chunk of readOllamaStream(res, {
|
|
253
|
+
errorFactory: ollamaError,
|
|
254
|
+
maxResponseBytes: MAX_CHAT_RESPONSE_BYTES,
|
|
255
|
+
signal: options.signal,
|
|
256
|
+
})) {
|
|
257
|
+
emittedChunk = true;
|
|
258
|
+
yield chunk;
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
} catch (err) {
|
|
262
|
+
if (
|
|
263
|
+
err.code === 'OLLAMA_TOOLS_UNSUPPORTED'
|
|
264
|
+
&& tools.length > 0
|
|
265
|
+
&& !retriedWithoutTools
|
|
266
|
+
&& !emittedChunk
|
|
267
|
+
) {
|
|
268
|
+
console.warn(`[Ollama] Model '${model}' does not support tools; retrying stream without them.`);
|
|
269
|
+
retriedWithoutTools = true;
|
|
270
|
+
requestTools = [];
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
throw err;
|
|
254
274
|
}
|
|
255
275
|
}
|
|
256
276
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { waitForAbortableResult } = require('../../network/http');
|
|
4
|
+
const { throwIfAborted } = require('../../../utils/abort');
|
|
5
|
+
|
|
6
|
+
function malformedStreamError(cause = null) {
|
|
7
|
+
const error = new Error('Ollama /api/chat returned malformed streaming JSON.', cause ? { cause } : undefined);
|
|
8
|
+
error.code = 'OLLAMA_STREAM_MALFORMED';
|
|
9
|
+
return error;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function streamEndedEarlyError() {
|
|
13
|
+
const error = new Error('Ollama stream ended before sending a completion marker.');
|
|
14
|
+
error.code = 'OLLAMA_STREAM_INCOMPLETE';
|
|
15
|
+
return error;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parseLine(line) {
|
|
19
|
+
if (!line.trim()) return null;
|
|
20
|
+
try {
|
|
21
|
+
const value = JSON.parse(line);
|
|
22
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
23
|
+
throw malformedStreamError();
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (error?.code === 'OLLAMA_STREAM_MALFORMED') throw error;
|
|
28
|
+
throw malformedStreamError(error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function usageFrom(data) {
|
|
33
|
+
if (data.prompt_eval_count == null && data.eval_count == null) return null;
|
|
34
|
+
const promptTokens = Number(data.prompt_eval_count) || 0;
|
|
35
|
+
const completionTokens = Number(data.eval_count) || 0;
|
|
36
|
+
return {
|
|
37
|
+
promptTokens,
|
|
38
|
+
completionTokens,
|
|
39
|
+
totalTokens: promptTokens + completionTokens,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function consumeData(data, state, errorFactory) {
|
|
44
|
+
if (data.error) throw errorFactory(data.error);
|
|
45
|
+
const chunks = [];
|
|
46
|
+
if (data.message?.content) {
|
|
47
|
+
const content = String(data.message.content);
|
|
48
|
+
state.content += content;
|
|
49
|
+
chunks.push({ type: 'content', content });
|
|
50
|
+
}
|
|
51
|
+
if (Array.isArray(data.message?.tool_calls)) {
|
|
52
|
+
for (const toolCall of data.message.tool_calls) {
|
|
53
|
+
const name = String(toolCall?.function?.name || '').trim();
|
|
54
|
+
if (!name) throw malformedStreamError();
|
|
55
|
+
state.toolCalls.push({
|
|
56
|
+
id: `call_ollama_${state.callSeed}_${state.nextCallIndex}`,
|
|
57
|
+
type: 'function',
|
|
58
|
+
function: {
|
|
59
|
+
name,
|
|
60
|
+
arguments: typeof toolCall.function.arguments === 'string'
|
|
61
|
+
? toolCall.function.arguments
|
|
62
|
+
: JSON.stringify(toolCall.function.arguments || {}),
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
state.nextCallIndex += 1;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (data.done === true) {
|
|
69
|
+
state.done = true;
|
|
70
|
+
chunks.push({
|
|
71
|
+
type: 'done',
|
|
72
|
+
content: state.content,
|
|
73
|
+
toolCalls: state.toolCalls,
|
|
74
|
+
finishReason: state.toolCalls.length > 0 ? 'tool_calls' : 'stop',
|
|
75
|
+
usage: usageFrom(data),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return chunks;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function* readOllamaStream(response, options = {}) {
|
|
82
|
+
const reader = response?.body?.getReader?.();
|
|
83
|
+
if (!reader) throw new Error('Ollama /api/chat returned no streaming response body.');
|
|
84
|
+
const decoder = new TextDecoder();
|
|
85
|
+
const maxResponseBytes = Number(options.maxResponseBytes) > 0
|
|
86
|
+
? Number(options.maxResponseBytes)
|
|
87
|
+
: 16 * 1024 * 1024;
|
|
88
|
+
const errorFactory = options.errorFactory || ((message) => new Error(String(message || 'Ollama error')));
|
|
89
|
+
const state = {
|
|
90
|
+
callSeed: Date.now(),
|
|
91
|
+
content: '',
|
|
92
|
+
done: false,
|
|
93
|
+
nextCallIndex: 0,
|
|
94
|
+
toolCalls: [],
|
|
95
|
+
};
|
|
96
|
+
let buffer = '';
|
|
97
|
+
let responseBytes = 0;
|
|
98
|
+
|
|
99
|
+
const emitLine = (line) => {
|
|
100
|
+
const data = parseLine(line);
|
|
101
|
+
return data ? consumeData(data, state, errorFactory) : [];
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
while (!state.done) {
|
|
106
|
+
throwIfAborted(options.signal, 'Ollama stream aborted.');
|
|
107
|
+
const { done, value } = await waitForAbortableResult(
|
|
108
|
+
reader.read(),
|
|
109
|
+
options.signal,
|
|
110
|
+
'Ollama stream aborted.',
|
|
111
|
+
);
|
|
112
|
+
if (done) break;
|
|
113
|
+
responseBytes += value?.byteLength || 0;
|
|
114
|
+
if (responseBytes > maxResponseBytes) {
|
|
115
|
+
const error = new Error('Ollama stream exceeded its response safety limit.');
|
|
116
|
+
error.code = 'HTTP_RESPONSE_TOO_LARGE';
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
buffer += decoder.decode(value, { stream: true });
|
|
121
|
+
const lines = buffer.split('\n');
|
|
122
|
+
buffer = lines.pop() || '';
|
|
123
|
+
for (const line of lines) {
|
|
124
|
+
for (const chunk of emitLine(line)) yield chunk;
|
|
125
|
+
if (state.done) return;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
buffer += decoder.decode();
|
|
130
|
+
if (buffer.trim()) {
|
|
131
|
+
for (const chunk of emitLine(buffer)) yield chunk;
|
|
132
|
+
}
|
|
133
|
+
if (!state.done) throw streamEndedEarlyError();
|
|
134
|
+
} finally {
|
|
135
|
+
await reader.cancel().catch(() => {});
|
|
136
|
+
reader.releaseLock?.();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = {
|
|
141
|
+
readOllamaStream,
|
|
142
|
+
};
|
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
const OpenAI = require('openai');
|
|
2
2
|
const { OpenAICompatibleProvider } = require('./openaiCompatible');
|
|
3
|
+
const { wrapProviderError } = require('./provider_error');
|
|
3
4
|
|
|
4
5
|
class OpenAIProvider extends OpenAICompatibleProvider {
|
|
5
6
|
constructor(config = {}) {
|
|
6
7
|
super(config);
|
|
7
8
|
this.name = 'openai';
|
|
8
9
|
this.models = [
|
|
10
|
+
'gpt-5.6',
|
|
11
|
+
'gpt-5.6-sol',
|
|
12
|
+
'gpt-5.6-terra',
|
|
13
|
+
'gpt-5.6-luna',
|
|
9
14
|
'gpt-5.5',
|
|
15
|
+
'gpt-5.4',
|
|
16
|
+
'gpt-5.4-mini',
|
|
17
|
+
'gpt-5.4-nano',
|
|
10
18
|
'gpt-5',
|
|
11
19
|
'gpt-5-mini',
|
|
12
20
|
'gpt-5-nano',
|
|
@@ -16,9 +24,35 @@ class OpenAIProvider extends OpenAICompatibleProvider {
|
|
|
16
24
|
'o4-mini'
|
|
17
25
|
];
|
|
18
26
|
// Reasoning models: no temperature, use max_completion_tokens, support reasoning_effort
|
|
19
|
-
this.reasoningModels = new Set([
|
|
27
|
+
this.reasoningModels = new Set([
|
|
28
|
+
'gpt-5.6',
|
|
29
|
+
'gpt-5.6-sol',
|
|
30
|
+
'gpt-5.6-terra',
|
|
31
|
+
'gpt-5.6-luna',
|
|
32
|
+
'gpt-5.5',
|
|
33
|
+
'gpt-5.4',
|
|
34
|
+
'gpt-5.4-mini',
|
|
35
|
+
'gpt-5.4-nano',
|
|
36
|
+
'gpt-5',
|
|
37
|
+
'gpt-5-mini',
|
|
38
|
+
'gpt-5-nano',
|
|
39
|
+
'gpt-5.2',
|
|
40
|
+
'gpt-5.1',
|
|
41
|
+
'o1',
|
|
42
|
+
'o3',
|
|
43
|
+
'o3-pro',
|
|
44
|
+
'o4-mini',
|
|
45
|
+
'o3-mini',
|
|
46
|
+
]);
|
|
20
47
|
this.contextWindows = {
|
|
21
|
-
'gpt-5.
|
|
48
|
+
'gpt-5.6': 1050000,
|
|
49
|
+
'gpt-5.6-sol': 1050000,
|
|
50
|
+
'gpt-5.6-terra': 1050000,
|
|
51
|
+
'gpt-5.6-luna': 1050000,
|
|
52
|
+
'gpt-5.5': 1050000,
|
|
53
|
+
'gpt-5.4': 1050000,
|
|
54
|
+
'gpt-5.4-mini': 400000,
|
|
55
|
+
'gpt-5.4-nano': 400000,
|
|
22
56
|
'gpt-5': 400000,
|
|
23
57
|
'gpt-5-mini': 400000,
|
|
24
58
|
'gpt-5-nano': 128000,
|
|
@@ -36,15 +70,15 @@ class OpenAIProvider extends OpenAICompatibleProvider {
|
|
|
36
70
|
});
|
|
37
71
|
}
|
|
38
72
|
|
|
39
|
-
async listModels() {
|
|
73
|
+
async listModels(signal = null) {
|
|
40
74
|
try {
|
|
41
|
-
const res = await this.client.models.list();
|
|
75
|
+
const res = await this.client.models.list({ signal });
|
|
42
76
|
const DROP = /dall-e|whisper|tts|embed|moderat|realtime|audio|transcribe|search-api|-image-|babbage|davinci-002|^sora|-instruct/i;
|
|
43
77
|
return res.data
|
|
44
78
|
.filter((m) => !DROP.test(m.id))
|
|
45
79
|
.map((m) => ({ id: m.id, name: m.id }));
|
|
46
80
|
} catch (err) {
|
|
47
|
-
throw
|
|
81
|
+
throw wrapProviderError(err, 'Failed to list OpenAI models', { signal });
|
|
48
82
|
}
|
|
49
83
|
}
|
|
50
84
|
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
1
3
|
const crypto = require('crypto');
|
|
2
4
|
const OpenAI = require('openai');
|
|
3
5
|
const { BaseProvider } = require('./base');
|
|
6
|
+
const { wrapProviderError } = require('./provider_error');
|
|
4
7
|
|
|
5
8
|
const DEFAULT_BASE_URL = 'https://chatgpt.com/backend-api/codex';
|
|
6
9
|
const OPENAI_CODEX_EMPTY_INPUT_TEXT = ' ';
|
|
@@ -439,8 +442,10 @@ class OpenAICodexProvider extends BaseProvider {
|
|
|
439
442
|
{ headers: this._requestHeaders(), signal: options.signal },
|
|
440
443
|
);
|
|
441
444
|
} catch (err) {
|
|
442
|
-
|
|
443
|
-
|
|
445
|
+
throw wrapProviderError(err, 'OpenAI Codex request failed', {
|
|
446
|
+
detail: formatOpenAIError(err),
|
|
447
|
+
signal: options.signal,
|
|
448
|
+
});
|
|
444
449
|
}
|
|
445
450
|
|
|
446
451
|
const toolCalls = extractToolCalls(response);
|
|
@@ -468,8 +473,10 @@ class OpenAICodexProvider extends BaseProvider {
|
|
|
468
473
|
{ headers: this._requestHeaders(), signal: options.signal },
|
|
469
474
|
);
|
|
470
475
|
} catch (err) {
|
|
471
|
-
|
|
472
|
-
|
|
476
|
+
throw wrapProviderError(err, 'OpenAI Codex request failed', {
|
|
477
|
+
detail: formatOpenAIError(err),
|
|
478
|
+
signal: options.signal,
|
|
479
|
+
});
|
|
473
480
|
}
|
|
474
481
|
|
|
475
482
|
let content = '';
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const OpenAI = require('openai');
|
|
2
2
|
const { OpenAICompatibleProvider } = require('./openaiCompatible');
|
|
3
|
+
const { fetchResponseText } = require('../../network/http');
|
|
4
|
+
const { wrapProviderError } = require('./provider_error');
|
|
3
5
|
|
|
4
6
|
const OPENROUTER_BASE_URL = 'https://openrouter.ai/api/v1';
|
|
5
7
|
|
|
@@ -25,12 +27,26 @@ class OpenRouterProvider extends OpenAICompatibleProvider {
|
|
|
25
27
|
});
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
async listModels() {
|
|
29
|
-
const
|
|
30
|
+
async listModels(signal = null) {
|
|
31
|
+
const { response, text } = await fetchResponseText(`${this.baseURL}/models`, {
|
|
30
32
|
headers: { 'Authorization': `Bearer ${this.client.apiKey}` },
|
|
33
|
+
maxResponseBytes: 5 * 1024 * 1024,
|
|
34
|
+
serviceName: 'OpenRouter model catalog',
|
|
35
|
+
signal,
|
|
31
36
|
});
|
|
32
|
-
if (!
|
|
33
|
-
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
const error = new Error(`OpenRouter /models returned HTTP ${response.status}`);
|
|
39
|
+
error.status = response.status;
|
|
40
|
+
error.headers = response.headers;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
let payload;
|
|
44
|
+
try {
|
|
45
|
+
payload = JSON.parse(text || '{}');
|
|
46
|
+
} catch {
|
|
47
|
+
throw new Error('OpenRouter /models returned invalid JSON.');
|
|
48
|
+
}
|
|
49
|
+
const { data } = payload;
|
|
34
50
|
const models = data || [];
|
|
35
51
|
for (const m of models) {
|
|
36
52
|
if (m.context_length) contextWindowCache.set(m.id, m.context_length);
|
|
@@ -74,7 +90,9 @@ class OpenRouterProvider extends OpenAICompatibleProvider {
|
|
|
74
90
|
try {
|
|
75
91
|
response = await this.client.chat.completions.create(params, { signal: options.signal });
|
|
76
92
|
} catch (err) {
|
|
77
|
-
throw
|
|
93
|
+
throw wrapProviderError(err, 'OpenRouter request failed', {
|
|
94
|
+
signal: options.signal,
|
|
95
|
+
});
|
|
78
96
|
}
|
|
79
97
|
// OpenRouter returns HTTP 200 even for errors (rate limits, model unavailable, etc.)
|
|
80
98
|
const orErr = this._extractOpenRouterError(response);
|
|
@@ -97,7 +115,9 @@ class OpenRouterProvider extends OpenAICompatibleProvider {
|
|
|
97
115
|
try {
|
|
98
116
|
stream = await this.client.chat.completions.create(params, { signal: options.signal });
|
|
99
117
|
} catch (err) {
|
|
100
|
-
throw
|
|
118
|
+
throw wrapProviderError(err, 'OpenRouter request failed', {
|
|
119
|
+
signal: options.signal,
|
|
120
|
+
});
|
|
101
121
|
}
|
|
102
122
|
|
|
103
123
|
let toolCalls = [];
|
|
@@ -148,7 +168,9 @@ class OpenRouterProvider extends OpenAICompatibleProvider {
|
|
|
148
168
|
}
|
|
149
169
|
} catch (err) {
|
|
150
170
|
// Re-throw SDK APIErrors (from OpenRouter SSE error events) with OpenRouter context.
|
|
151
|
-
throw
|
|
171
|
+
throw wrapProviderError(err, 'OpenRouter stream failed', {
|
|
172
|
+
signal: options.signal,
|
|
173
|
+
});
|
|
152
174
|
}
|
|
153
175
|
|
|
154
176
|
if (toolCalls.length > 0) {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { createAbortError, isAbortError } = require('../../../utils/abort');
|
|
4
|
+
|
|
5
|
+
function sanitizeProviderErrorDetail(value) {
|
|
6
|
+
return String(value || 'Unknown provider error')
|
|
7
|
+
.slice(0, 2000)
|
|
8
|
+
.replace(/\b(Bearer|Basic|token)\s+[A-Za-z0-9._~+/=-]+/gi, '$1 [redacted]')
|
|
9
|
+
.replace(/([?&](?:key|api_key|access_token)=)[^&\s]+/gi, '$1[redacted]')
|
|
10
|
+
.replace(
|
|
11
|
+
/\b(api[_-]?key|access_token|refresh_token|authorization)\b\s*[:=]\s*["']?[^\s,"'}\]]+/gi,
|
|
12
|
+
'$1=[redacted]',
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function wrapProviderError(error, prefix, options = {}) {
|
|
17
|
+
if (options.signal?.aborted) return createAbortError(options.signal);
|
|
18
|
+
if (isAbortError(error)) return error;
|
|
19
|
+
|
|
20
|
+
const detail = typeof options.detail === 'string'
|
|
21
|
+
? options.detail
|
|
22
|
+
: error?.message || String(error);
|
|
23
|
+
const wrapped = new Error(
|
|
24
|
+
`${prefix}: ${sanitizeProviderErrorDetail(detail)}`,
|
|
25
|
+
{ cause: error },
|
|
26
|
+
);
|
|
27
|
+
for (const property of ['status', 'statusCode', 'code', 'headers', 'response', 'type']) {
|
|
28
|
+
if (error?.[property] !== undefined) wrapped[property] = error[property];
|
|
29
|
+
}
|
|
30
|
+
return wrapped;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
sanitizeProviderErrorDetail,
|
|
35
|
+
wrapProviderError,
|
|
36
|
+
};
|
|
@@ -12,7 +12,7 @@ const AI_PROVIDER_DEFINITIONS = Object.freeze({
|
|
|
12
12
|
openai: {
|
|
13
13
|
id: 'openai',
|
|
14
14
|
label: 'OpenAI',
|
|
15
|
-
description: '
|
|
15
|
+
description: 'Current GPT models for general work, coding, and reasoning.',
|
|
16
16
|
envKey: 'OPENAI_API_KEY',
|
|
17
17
|
supportsApiKey: true,
|
|
18
18
|
supportsBaseUrl: true,
|
|
@@ -148,11 +148,19 @@ function createDefaultAiSettings() {
|
|
|
148
148
|
cost_mode: 'balanced_auto',
|
|
149
149
|
chat_history_window: 20,
|
|
150
150
|
tool_replay_budget_chars: 6000,
|
|
151
|
+
tool_replay_budget_file_chars: null,
|
|
152
|
+
tool_replay_budget_browser_chars: null,
|
|
153
|
+
tool_replay_budget_command_chars: null,
|
|
154
|
+
max_iterations: null,
|
|
155
|
+
max_consecutive_read_only_iterations: null,
|
|
156
|
+
max_consecutive_tool_failures: null,
|
|
157
|
+
max_model_failure_recoveries: null,
|
|
158
|
+
compaction_threshold: null,
|
|
151
159
|
subagent_max_iterations: 6,
|
|
152
160
|
subagent_max_children_per_run: 10,
|
|
153
161
|
assistant_behavior_notes: '',
|
|
154
162
|
auto_skill_learning: false,
|
|
155
|
-
fallback_model_id: 'gpt-5-nano',
|
|
163
|
+
fallback_model_id: 'openai::gpt-5-nano',
|
|
156
164
|
smarter_model_selector: true,
|
|
157
165
|
enabled_models: [],
|
|
158
166
|
default_chat_model: 'auto',
|
|
@@ -179,6 +187,14 @@ function parseSettingValue(value) {
|
|
|
179
187
|
}
|
|
180
188
|
}
|
|
181
189
|
|
|
190
|
+
function normalizeOptionalNumber(value, min, max, { integer = false } = {}) {
|
|
191
|
+
if (value == null || value === '') return null;
|
|
192
|
+
const parsed = Number(value);
|
|
193
|
+
if (!Number.isFinite(parsed)) return null;
|
|
194
|
+
const normalized = integer ? Math.floor(parsed) : parsed;
|
|
195
|
+
return Math.min(Math.max(normalized, min), max);
|
|
196
|
+
}
|
|
197
|
+
|
|
182
198
|
function normalizeProviderConfigs(rawConfigs) {
|
|
183
199
|
const defaults = createDefaultProviderConfigs();
|
|
184
200
|
const parsed = rawConfigs && typeof rawConfigs === 'object' && !Array.isArray(rawConfigs)
|
|
@@ -325,6 +341,14 @@ function getAiSettings(userId, agentId = null) {
|
|
|
325
341
|
|
|
326
342
|
settings.chat_history_window = Math.max(6, Math.min(Number(settings.chat_history_window) || DEFAULT_AI_SETTINGS.chat_history_window, 40));
|
|
327
343
|
settings.tool_replay_budget_chars = Math.max(1200, Math.min(Number(settings.tool_replay_budget_chars) || DEFAULT_AI_SETTINGS.tool_replay_budget_chars, 12000));
|
|
344
|
+
settings.tool_replay_budget_file_chars = normalizeOptionalNumber(settings.tool_replay_budget_file_chars, 500, 500_000, { integer: true });
|
|
345
|
+
settings.tool_replay_budget_browser_chars = normalizeOptionalNumber(settings.tool_replay_budget_browser_chars, 500, 500_000, { integer: true });
|
|
346
|
+
settings.tool_replay_budget_command_chars = normalizeOptionalNumber(settings.tool_replay_budget_command_chars, 500, 500_000, { integer: true });
|
|
347
|
+
settings.max_iterations = normalizeOptionalNumber(settings.max_iterations, 1, 400, { integer: true });
|
|
348
|
+
settings.max_consecutive_read_only_iterations = normalizeOptionalNumber(settings.max_consecutive_read_only_iterations, 3, 25, { integer: true });
|
|
349
|
+
settings.max_consecutive_tool_failures = normalizeOptionalNumber(settings.max_consecutive_tool_failures, 1, 50, { integer: true });
|
|
350
|
+
settings.max_model_failure_recoveries = normalizeOptionalNumber(settings.max_model_failure_recoveries, 0, 10, { integer: true });
|
|
351
|
+
settings.compaction_threshold = normalizeOptionalNumber(settings.compaction_threshold, 0.1, 1);
|
|
328
352
|
settings.subagent_max_iterations = Math.max(2, Math.min(Number(settings.subagent_max_iterations) || DEFAULT_AI_SETTINGS.subagent_max_iterations, 12));
|
|
329
353
|
settings.subagent_max_children_per_run = Math.max(
|
|
330
354
|
1,
|