newmark-agent 0.5.4 → 0.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/conversation-utility-host.bundle.cjs +230 -66
- package/dist/core/agent.d.ts +1 -1
- package/dist/core/agent.js +55 -25
- package/dist/core/agentKernelRunner.js +55 -9
- package/dist/core/autoRouter.d.ts +1 -0
- package/dist/core/autoRouter.js +9 -0
- package/dist/core/conversationKernel.d.ts +12 -0
- package/dist/core/conversationKernel.js +32 -1
- package/dist/core/electronUtilityRuntimePool.js +7 -2
- package/dist/core/emptyResponseRetry.d.ts +16 -0
- package/dist/core/emptyResponseRetry.js +25 -0
- package/dist/core/terminalOutputBuffer.d.ts +24 -0
- package/dist/core/terminalOutputBuffer.js +92 -0
- package/dist/core/types.d.ts +21 -0
- package/dist/core/wslAgentRuntimePool.js +6 -2
- package/dist/llm/provider.d.ts +4 -4
- package/dist/llm/provider.js +55 -21
- package/dist/main.js +46 -3
- package/dist/preload.js +1 -0
- package/dist/providers/chat-completions.adapter.js +17 -4
- package/dist/providers/provider-events.d.ts +13 -0
- package/dist/providers/provider-events.js +55 -4
- package/dist/providers/responses.adapter.js +23 -9
- package/dist/server.js +21 -0
- package/dist/ui/index.html +2415 -216
- package/dist/ui/lucide-sprite.svg +4 -0
- package/dist/wsl-agent-host.bundle.cjs +230 -66
- package/package.json +10 -3
package/dist/llm/provider.js
CHANGED
|
@@ -43,9 +43,6 @@ const child_process_1 = require("child_process");
|
|
|
43
43
|
const agentKernelDiagnostics_1 = require("../core/agentKernelDiagnostics");
|
|
44
44
|
const chat_messages_1 = require("../providers/chat-messages");
|
|
45
45
|
const providers_1 = require("../providers");
|
|
46
|
-
// Keep provider requests below the release-harness/user-visible command
|
|
47
|
-
// deadline. A provider that does not answer must produce one bounded error;
|
|
48
|
-
// it must not restart the same request through every Windows transport.
|
|
49
46
|
// Provider responses are intentionally unbounded. User cancellation, transport
|
|
50
47
|
// errors, and tool-specific limits remain the only automatic stop conditions.
|
|
51
48
|
const DEFAULT_PROVIDER_REQUEST_TIMEOUT_MS = 0;
|
|
@@ -939,7 +936,7 @@ class LLMProvider {
|
|
|
939
936
|
* `provider_adapters_v2` context flag. Request serialization and SSE
|
|
940
937
|
* normalization are delegated to the shared provider adapters while the
|
|
941
938
|
* transport orchestration (loopback node-http, fetch -> node-http fallback,
|
|
942
|
-
*
|
|
939
|
+
* cancellation-only streaming and the 4xx Chat -> Responses downgrade) stay here.
|
|
943
940
|
* The emitted request body and StreamToken stream are byte-equivalent to
|
|
944
941
|
* the legacy inlined path.
|
|
945
942
|
*/
|
|
@@ -1082,9 +1079,9 @@ class LLMProvider {
|
|
|
1082
1079
|
}
|
|
1083
1080
|
/**
|
|
1084
1081
|
* Loopback-aware transport injected into adapter `execute`. Streaming
|
|
1085
|
-
* requests retain the fetch-to-node fallback for transport failures
|
|
1086
|
-
*
|
|
1087
|
-
*
|
|
1082
|
+
* requests retain the fetch-to-node fallback for transport failures. They
|
|
1083
|
+
* have no response deadline; only caller cancellation or a concrete
|
|
1084
|
+
* transport/provider failure may end the request.
|
|
1088
1085
|
*/
|
|
1089
1086
|
buildProviderAdapterTransport() {
|
|
1090
1087
|
return async (request, signal) => {
|
|
@@ -1096,7 +1093,9 @@ class LLMProvider {
|
|
|
1096
1093
|
forwardAbort();
|
|
1097
1094
|
else
|
|
1098
1095
|
signal?.addEventListener('abort', forwardAbort, { once: true });
|
|
1099
|
-
|
|
1096
|
+
// Streaming provider responses are intentionally unbounded. Only
|
|
1097
|
+
// caller cancellation or an explicit provider failure may end them.
|
|
1098
|
+
const effectiveTimeout = 0;
|
|
1100
1099
|
const timer = effectiveTimeout > 0
|
|
1101
1100
|
? setTimeout(() => abort.abort(providerTimeoutError(effectiveTimeout)), effectiveTimeout)
|
|
1102
1101
|
: undefined;
|
|
@@ -1247,7 +1246,9 @@ class LLMProvider {
|
|
|
1247
1246
|
forwardAbort();
|
|
1248
1247
|
else
|
|
1249
1248
|
signal?.addEventListener('abort', forwardAbort, { once: true });
|
|
1250
|
-
|
|
1249
|
+
// Streaming provider responses are intentionally unbounded. Do not turn
|
|
1250
|
+
// silence into an empty-response failure.
|
|
1251
|
+
const effectiveTimeout = 0;
|
|
1251
1252
|
const timeout = effectiveTimeout > 0
|
|
1252
1253
|
? setTimeout(() => abort.abort(providerTimeoutError(effectiveTimeout)), effectiveTimeout)
|
|
1253
1254
|
: undefined;
|
|
@@ -1285,10 +1286,14 @@ class LLMProvider {
|
|
|
1285
1286
|
}
|
|
1286
1287
|
const decoder = new TextDecoder();
|
|
1287
1288
|
let buffer = '';
|
|
1288
|
-
|
|
1289
|
+
const toolCalls = new Map();
|
|
1290
|
+
const toolCallOrder = [];
|
|
1291
|
+
let syntheticToolIndex = 0;
|
|
1292
|
+
let lastToolIndex = 0;
|
|
1289
1293
|
let currentReasoningContent = '';
|
|
1290
1294
|
let contentPolicyBlocked = false;
|
|
1291
1295
|
let emittedContent = false;
|
|
1296
|
+
let explicitCompletion = false;
|
|
1292
1297
|
const streamSignal = signal || new AbortController().signal;
|
|
1293
1298
|
while (true) {
|
|
1294
1299
|
const { done, value } = await (0, providers_1.readProviderStreamChunk)(reader, streamSignal);
|
|
@@ -1302,8 +1307,10 @@ class LLMProvider {
|
|
|
1302
1307
|
if (!trimmed.startsWith('data: '))
|
|
1303
1308
|
continue;
|
|
1304
1309
|
const data = trimmed.slice(6);
|
|
1305
|
-
if (data === '[DONE]')
|
|
1310
|
+
if (data === '[DONE]') {
|
|
1311
|
+
explicitCompletion = true;
|
|
1306
1312
|
continue;
|
|
1313
|
+
}
|
|
1307
1314
|
try {
|
|
1308
1315
|
const json = JSON.parse(data);
|
|
1309
1316
|
if (json.usage)
|
|
@@ -1324,27 +1331,54 @@ class LLMProvider {
|
|
|
1324
1331
|
}
|
|
1325
1332
|
if (delta.tool_calls) {
|
|
1326
1333
|
for (const tc of delta.tool_calls) {
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1334
|
+
const rawIndex = Number(tc.index);
|
|
1335
|
+
const index = Number.isInteger(rawIndex) && rawIndex >= 0
|
|
1336
|
+
? rawIndex
|
|
1337
|
+
: (tc.id ? syntheticToolIndex++ : lastToolIndex);
|
|
1338
|
+
lastToolIndex = index;
|
|
1339
|
+
let call = toolCalls.get(index);
|
|
1340
|
+
if (!call && (tc.id || tc.function?.name)) {
|
|
1341
|
+
call = { id: tc.id || '', name: tc.function?.name || '', argumentParts: [] };
|
|
1342
|
+
toolCalls.set(index, call);
|
|
1343
|
+
toolCallOrder.push(index);
|
|
1335
1344
|
}
|
|
1345
|
+
if (!call)
|
|
1346
|
+
continue;
|
|
1347
|
+
if (tc.id && !call.id)
|
|
1348
|
+
call.id = tc.id;
|
|
1349
|
+
if (tc.function?.name && !call.name)
|
|
1350
|
+
call.name = tc.function.name;
|
|
1351
|
+
if (tc.function?.arguments)
|
|
1352
|
+
call.argumentParts.push(tc.function.arguments);
|
|
1336
1353
|
}
|
|
1337
1354
|
}
|
|
1338
1355
|
}
|
|
1339
1356
|
catch { /* skip malformed JSON */ }
|
|
1340
1357
|
}
|
|
1341
1358
|
}
|
|
1342
|
-
if (
|
|
1343
|
-
|
|
1359
|
+
if (toolCallOrder.length) {
|
|
1360
|
+
for (const index of toolCallOrder) {
|
|
1361
|
+
const call = toolCalls.get(index);
|
|
1362
|
+
if (!call)
|
|
1363
|
+
continue;
|
|
1364
|
+
yield {
|
|
1365
|
+
type: 'tool_call',
|
|
1366
|
+
text: '',
|
|
1367
|
+
toolCall: {
|
|
1368
|
+
id: call.id,
|
|
1369
|
+
name: call.name,
|
|
1370
|
+
arguments: (0, providers_1.assembleCompatibleToolArguments)(call.argumentParts),
|
|
1371
|
+
},
|
|
1372
|
+
reasoningContent: currentReasoningContent || undefined,
|
|
1373
|
+
};
|
|
1374
|
+
}
|
|
1344
1375
|
}
|
|
1345
1376
|
else if (!emittedContent && contentPolicyBlocked) {
|
|
1346
1377
|
yield { type: 'text', text: '[Error] Content policy refusal (content_filter).' };
|
|
1347
1378
|
}
|
|
1379
|
+
else if (!explicitCompletion && !emittedContent && !currentReasoningContent) {
|
|
1380
|
+
yield { type: 'text', text: '[LLM Error] GitHub Models stream ended before an explicit completion.' };
|
|
1381
|
+
}
|
|
1348
1382
|
}
|
|
1349
1383
|
finally {
|
|
1350
1384
|
reader?.releaseLock();
|
package/dist/main.js
CHANGED
|
@@ -77,6 +77,7 @@ const compat_1 = require("./core/compat");
|
|
|
77
77
|
const dshCompatibility_1 = require("./core/dshCompatibility");
|
|
78
78
|
const mcpManager_1 = require("./core/mcpManager");
|
|
79
79
|
const workEventCoalescer_1 = require("./core/workEventCoalescer");
|
|
80
|
+
const terminalOutputBuffer_1 = require("./core/terminalOutputBuffer");
|
|
80
81
|
const cli_help_1 = require("./cli-help");
|
|
81
82
|
const APP_NAME = 'Newmark Agent';
|
|
82
83
|
const APP_ID = 'ai.newmark.agent';
|
|
@@ -3020,6 +3021,10 @@ else {
|
|
|
3020
3021
|
activePromptLeases.set(promptLeaseKey, (activePromptLeases.get(promptLeaseKey) || 0) + 1);
|
|
3021
3022
|
activePromptWorkspaces.set(promptLeaseWorkspaceKey, (activePromptWorkspaces.get(promptLeaseWorkspaceKey) || 0) + 1);
|
|
3022
3023
|
const targetConversation = target.conversationId;
|
|
3024
|
+
// 发送命令时锁定输入框选择的模型:接受命令后的整个运行过程都以该
|
|
3025
|
+
// 模型为准(唯一例外是显式的不可用回退,且回退会以结构化事件同步
|
|
3026
|
+
// 到前端输入框下方的选择区,而不是隐藏的参数回退)。
|
|
3027
|
+
const requestedModel = agent.model;
|
|
3023
3028
|
const options = {
|
|
3024
3029
|
mode: agent.mode,
|
|
3025
3030
|
model: agent.ensureUsableModelSelection(),
|
|
@@ -3027,6 +3032,21 @@ else {
|
|
|
3027
3032
|
inputMode: agent.inputMode,
|
|
3028
3033
|
engine: agent.engine,
|
|
3029
3034
|
};
|
|
3035
|
+
if (options.model && requestedModel && options.model !== requestedModel) {
|
|
3036
|
+
const usableConfig = agent.activeModelConfig();
|
|
3037
|
+
broadcastAgentWorkEvent({
|
|
3038
|
+
id: `model-fallback-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
|
3039
|
+
conversationId: target.conversationId,
|
|
3040
|
+
type: 'status',
|
|
3041
|
+
content: `[Model fallback] ${requestedModel} unavailable; switched to ${options.model}.`,
|
|
3042
|
+
mode: agent.modeName(),
|
|
3043
|
+
model: options.model,
|
|
3044
|
+
timestamp: new Date().toISOString(),
|
|
3045
|
+
workspaceId: target.workspaceId,
|
|
3046
|
+
workspaceKey: target.workspaceKey,
|
|
3047
|
+
fallback: { from: requestedModel, to: options.model, providerId: usableConfig?.provider_id || agent.activeDeployment()?.providerId },
|
|
3048
|
+
});
|
|
3049
|
+
}
|
|
3030
3050
|
const queueMode = agent.inputMode === 'guide' ? 'steer' : 'followUp';
|
|
3031
3051
|
let result;
|
|
3032
3052
|
if (wslBackendEnabled()) {
|
|
@@ -4432,10 +4452,16 @@ else {
|
|
|
4432
4452
|
}
|
|
4433
4453
|
});
|
|
4434
4454
|
const ptySessions = new Map();
|
|
4435
|
-
const
|
|
4436
|
-
session
|
|
4455
|
+
const terminalOutput = new terminalOutputBuffer_1.TerminalOutputBuffer((sessionId, text) => {
|
|
4456
|
+
const session = ptySessions.get(sessionId);
|
|
4457
|
+
if (session)
|
|
4458
|
+
session.buffer = terminalOutput.history(sessionId);
|
|
4437
4459
|
if (mainWindow && !mainWindow.isDestroyed())
|
|
4438
4460
|
mainWindow.webContents.send('pty:data', sessionId, text);
|
|
4461
|
+
});
|
|
4462
|
+
const sendTerminalData = (sessionId, session, text) => {
|
|
4463
|
+
terminalOutput.push(sessionId, text);
|
|
4464
|
+
session.buffer = terminalOutput.history(sessionId);
|
|
4439
4465
|
};
|
|
4440
4466
|
electron_1.ipcMain.handle('pty:spawn', async (_event, shellId) => {
|
|
4441
4467
|
const sessionId = (0, crypto_1.randomUUID)().slice(0, 8);
|
|
@@ -4457,7 +4483,9 @@ else {
|
|
|
4457
4483
|
ptySessions.set(sessionId, session);
|
|
4458
4484
|
proc.onData(text => sendTerminalData(sessionId, session, text));
|
|
4459
4485
|
proc.onExit(event => {
|
|
4486
|
+
terminalOutput.flush(sessionId);
|
|
4460
4487
|
ptySessions.delete(sessionId);
|
|
4488
|
+
terminalOutput.close(sessionId);
|
|
4461
4489
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
|
4462
4490
|
mainWindow.webContents.send('pty:exit', sessionId, event.exitCode);
|
|
4463
4491
|
}
|
|
@@ -4515,7 +4543,7 @@ else {
|
|
|
4515
4543
|
const session = ptySessions.get(sessionId);
|
|
4516
4544
|
if (!session)
|
|
4517
4545
|
return { buffer: '' };
|
|
4518
|
-
return { buffer: session.buffer };
|
|
4546
|
+
return { buffer: terminalOutput.history(sessionId) || session.buffer };
|
|
4519
4547
|
});
|
|
4520
4548
|
const terminalOwnerFor = (conversationId, actorId) => (0, terminalTakeover_1.normalizeTerminalTakeoverOwner)({
|
|
4521
4549
|
backend: wslBackendEnabled() ? 'wsl' : (process.platform === 'win32' ? 'windows' : process.platform),
|
|
@@ -5206,6 +5234,21 @@ else {
|
|
|
5206
5234
|
// make an ordinary minimize click disappear from the taskbar.
|
|
5207
5235
|
win?.minimize();
|
|
5208
5236
|
});
|
|
5237
|
+
electron_1.ipcMain.handle('glass:captureBackdrop', async (event, requestedSize) => {
|
|
5238
|
+
const image = await event.sender.capturePage();
|
|
5239
|
+
const sourceSize = image.getSize();
|
|
5240
|
+
const width = Math.max(1, Math.min(sourceSize.width, Math.round(Number(requestedSize?.width) || sourceSize.width)));
|
|
5241
|
+
const height = Math.max(1, Math.min(sourceSize.height, Math.round(Number(requestedSize?.height) || sourceSize.height)));
|
|
5242
|
+
const resized = sourceSize.width === width && sourceSize.height === height
|
|
5243
|
+
? image
|
|
5244
|
+
: image.resize({ width, height, quality: 'good' });
|
|
5245
|
+
return {
|
|
5246
|
+
bytes: resized.toJPEG(82),
|
|
5247
|
+
mimeType: 'image/jpeg',
|
|
5248
|
+
width,
|
|
5249
|
+
height,
|
|
5250
|
+
};
|
|
5251
|
+
});
|
|
5209
5252
|
electron_1.ipcMain.handle('app:maximize', () => {
|
|
5210
5253
|
const win = electron_1.BrowserWindow.getFocusedWindow() || mainWindow;
|
|
5211
5254
|
if (win?.isMaximized())
|
package/dist/preload.js
CHANGED
|
@@ -6,6 +6,7 @@ contextBridge.exposeInMainWorld('api', {
|
|
|
6
6
|
startupWaitForBackend: () => ipcRenderer.invoke('startup:waitForBackend'),
|
|
7
7
|
startupUiReady: (payload) => ipcRenderer.invoke('startup:uiReady', payload),
|
|
8
8
|
startupUiFailed: (payload) => ipcRenderer.invoke('startup:uiFailed', payload),
|
|
9
|
+
captureLiquidBackdrop: (size) => ipcRenderer.invoke('glass:captureBackdrop', size),
|
|
9
10
|
onStartupStatus: (callback) => {
|
|
10
11
|
ipcRenderer.on('startup:status', (_event, payload) => callback(payload));
|
|
11
12
|
},
|
|
@@ -105,6 +105,8 @@ class ChatCompletionsAdapter {
|
|
|
105
105
|
let contentPolicyBlocked = false;
|
|
106
106
|
let emittedContent = false;
|
|
107
107
|
let emittedTool = false;
|
|
108
|
+
let emittedReasoning = false;
|
|
109
|
+
let explicitCompletion = false;
|
|
108
110
|
try {
|
|
109
111
|
while (true) {
|
|
110
112
|
const { done, value } = await (0, provider_events_1.readProviderStreamChunk)(reader, signal);
|
|
@@ -118,8 +120,10 @@ class ChatCompletionsAdapter {
|
|
|
118
120
|
if (!trimmed.startsWith('data: '))
|
|
119
121
|
continue;
|
|
120
122
|
const data = trimmed.slice(6);
|
|
121
|
-
if (data === '[DONE]')
|
|
123
|
+
if (data === '[DONE]') {
|
|
124
|
+
explicitCompletion = true;
|
|
122
125
|
continue;
|
|
126
|
+
}
|
|
123
127
|
let json;
|
|
124
128
|
try {
|
|
125
129
|
json = JSON.parse(data);
|
|
@@ -135,13 +139,18 @@ class ChatCompletionsAdapter {
|
|
|
135
139
|
if ((0, provider_events_1.isContentPolicyBlocked)(json))
|
|
136
140
|
contentPolicyBlocked = true;
|
|
137
141
|
const choices = Array.isArray(json.choices) ? json.choices : [];
|
|
138
|
-
const
|
|
142
|
+
const choice = choices[0];
|
|
143
|
+
if (choice?.finish_reason !== undefined && choice.finish_reason !== null)
|
|
144
|
+
explicitCompletion = true;
|
|
145
|
+
const delta = choice?.delta;
|
|
139
146
|
if (!delta)
|
|
140
147
|
continue;
|
|
141
148
|
if (delta.reasoning_content) {
|
|
142
149
|
const reasoning = this.extractText(delta.reasoning_content);
|
|
143
|
-
if (reasoning)
|
|
150
|
+
if (reasoning) {
|
|
151
|
+
emittedReasoning = true;
|
|
144
152
|
yield { type: 'reasoning.summary.delta', delta: reasoning };
|
|
153
|
+
}
|
|
145
154
|
}
|
|
146
155
|
const textDelta = this.extractText(delta.content);
|
|
147
156
|
if (textDelta) {
|
|
@@ -190,7 +199,7 @@ class ChatCompletionsAdapter {
|
|
|
190
199
|
type: 'tool_call.completed',
|
|
191
200
|
id: currentToolCall.id,
|
|
192
201
|
name: currentToolCall.name,
|
|
193
|
-
arguments: currentToolCall.argumentParts
|
|
202
|
+
arguments: (0, provider_events_1.assembleCompatibleToolArguments)(currentToolCall.argumentParts),
|
|
194
203
|
};
|
|
195
204
|
}
|
|
196
205
|
}
|
|
@@ -198,6 +207,10 @@ class ChatCompletionsAdapter {
|
|
|
198
207
|
yield { type: 'response.failed', error: '[Error] Content policy refusal (content_filter).' };
|
|
199
208
|
return;
|
|
200
209
|
}
|
|
210
|
+
if (!explicitCompletion && !emittedContent && !emittedTool && !emittedReasoning) {
|
|
211
|
+
yield { type: 'response.failed', error: '[LLM Error] Chat stream ended before an explicit completion.' };
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
201
214
|
yield { type: 'response.completed' };
|
|
202
215
|
}
|
|
203
216
|
finally {
|
|
@@ -17,12 +17,25 @@ export declare function providerStreamTimeoutError(timeoutMs: number): Error;
|
|
|
17
17
|
* Read one SSE chunk with both user cancellation and an inactivity deadline.
|
|
18
18
|
* Cancelling the reader is important: rejecting the race alone leaves the
|
|
19
19
|
* provider socket alive and lets later requests accumulate behind it.
|
|
20
|
+
*
|
|
21
|
+
* timeoutMs defaults to 0 (no stream idle deadline), matching the request-
|
|
22
|
+
* level DEFAULT_PROVIDER_REQUEST_TIMEOUT_MS = 0 and the Android client's
|
|
23
|
+
* readTimeout(0) / SSE_IDLE_TIMEOUT_MS = 0L. A caller that still wants an
|
|
24
|
+
* inactivity cap passes an explicit positive value (the recovery verify
|
|
25
|
+
* passes 50ms to prove reader cancellation).
|
|
20
26
|
*/
|
|
21
27
|
export declare function readProviderStreamChunk(reader: ReadableStreamDefaultReader<Uint8Array>, signal: AbortSignal, timeoutMs?: number): Promise<ReadableStreamReadResult<Uint8Array>>;
|
|
22
28
|
export declare function parseProviderSse(raw: string): Array<{
|
|
23
29
|
event?: string;
|
|
24
30
|
data: string;
|
|
25
31
|
}>;
|
|
32
|
+
/**
|
|
33
|
+
* Compatible gateways may stream function arguments as JSON deltas or repeat
|
|
34
|
+
* a cumulative snapshot on every SSE frame. Keep the normal incremental form
|
|
35
|
+
* when it parses, then fall back to snapshot folding. Returning malformed
|
|
36
|
+
* concatenated snapshots would erase the model's correction at tool parsing.
|
|
37
|
+
*/
|
|
38
|
+
export declare function assembleCompatibleToolArguments(parts: string[]): string;
|
|
26
39
|
/**
|
|
27
40
|
* Detect content-policy refusals across the provider failure shapes.
|
|
28
41
|
* Mirrors `LLMProvider.contentPolicyBlocked` semantics exactly so the
|
|
@@ -5,6 +5,7 @@ exports.providerAbortError = providerAbortError;
|
|
|
5
5
|
exports.providerStreamTimeoutError = providerStreamTimeoutError;
|
|
6
6
|
exports.readProviderStreamChunk = readProviderStreamChunk;
|
|
7
7
|
exports.parseProviderSse = parseProviderSse;
|
|
8
|
+
exports.assembleCompatibleToolArguments = assembleCompatibleToolArguments;
|
|
8
9
|
exports.isContentPolicyBlocked = isContentPolicyBlocked;
|
|
9
10
|
exports.normalizeProviderUsage = normalizeProviderUsage;
|
|
10
11
|
exports.estimateRequestTokens = estimateRequestTokens;
|
|
@@ -46,8 +47,14 @@ function providerStreamTimeoutError(timeoutMs) {
|
|
|
46
47
|
* Read one SSE chunk with both user cancellation and an inactivity deadline.
|
|
47
48
|
* Cancelling the reader is important: rejecting the race alone leaves the
|
|
48
49
|
* provider socket alive and lets later requests accumulate behind it.
|
|
50
|
+
*
|
|
51
|
+
* timeoutMs defaults to 0 (no stream idle deadline), matching the request-
|
|
52
|
+
* level DEFAULT_PROVIDER_REQUEST_TIMEOUT_MS = 0 and the Android client's
|
|
53
|
+
* readTimeout(0) / SSE_IDLE_TIMEOUT_MS = 0L. A caller that still wants an
|
|
54
|
+
* inactivity cap passes an explicit positive value (the recovery verify
|
|
55
|
+
* passes 50ms to prove reader cancellation).
|
|
49
56
|
*/
|
|
50
|
-
async function readProviderStreamChunk(reader, signal, timeoutMs =
|
|
57
|
+
async function readProviderStreamChunk(reader, signal, timeoutMs = 0) {
|
|
51
58
|
if (signal.aborted)
|
|
52
59
|
throw providerAbortError(signal);
|
|
53
60
|
let timer;
|
|
@@ -56,9 +63,14 @@ async function readProviderStreamChunk(reader, signal, timeoutMs = 30_000) {
|
|
|
56
63
|
onAbort = () => reject(providerAbortError(signal));
|
|
57
64
|
signal.addEventListener('abort', onAbort, { once: true });
|
|
58
65
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
66
|
+
// timeoutMs <= 0 means no inactivity deadline (unlimited). setTimeout with
|
|
67
|
+
// 0 would fire on the next tick, so we only arm the timer for positive
|
|
68
|
+
// values and race a never-settling promise otherwise.
|
|
69
|
+
const timeoutPromise = timeoutMs > 0
|
|
70
|
+
? new Promise((_, reject) => {
|
|
71
|
+
timer = setTimeout(() => reject(providerStreamTimeoutError(timeoutMs)), timeoutMs);
|
|
72
|
+
})
|
|
73
|
+
: new Promise(() => undefined);
|
|
62
74
|
try {
|
|
63
75
|
return await Promise.race([reader.read(), abortPromise, timeoutPromise]);
|
|
64
76
|
}
|
|
@@ -94,6 +106,45 @@ function parseProviderSse(raw) {
|
|
|
94
106
|
}
|
|
95
107
|
return events;
|
|
96
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Compatible gateways may stream function arguments as JSON deltas or repeat
|
|
111
|
+
* a cumulative snapshot on every SSE frame. Keep the normal incremental form
|
|
112
|
+
* when it parses, then fall back to snapshot folding. Returning malformed
|
|
113
|
+
* concatenated snapshots would erase the model's correction at tool parsing.
|
|
114
|
+
*/
|
|
115
|
+
function assembleCompatibleToolArguments(parts) {
|
|
116
|
+
const nonEmpty = (parts || []).map(String).filter(part => part && part !== 'null');
|
|
117
|
+
if (!nonEmpty.length)
|
|
118
|
+
return '{}';
|
|
119
|
+
const isJsonObject = (value) => {
|
|
120
|
+
try {
|
|
121
|
+
const parsed = JSON.parse(value);
|
|
122
|
+
return !!parsed && typeof parsed === 'object' && !Array.isArray(parsed);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const incremental = nonEmpty.join('');
|
|
129
|
+
if (isJsonObject(incremental))
|
|
130
|
+
return incremental;
|
|
131
|
+
let compatible = '';
|
|
132
|
+
for (const incoming of nonEmpty) {
|
|
133
|
+
if (!compatible)
|
|
134
|
+
compatible = incoming;
|
|
135
|
+
else if (incoming === compatible)
|
|
136
|
+
continue;
|
|
137
|
+
else if (incoming.startsWith(compatible))
|
|
138
|
+
compatible = incoming;
|
|
139
|
+
else if (compatible.startsWith(incoming))
|
|
140
|
+
continue;
|
|
141
|
+
else
|
|
142
|
+
compatible += incoming;
|
|
143
|
+
}
|
|
144
|
+
if (isJsonObject(compatible))
|
|
145
|
+
return compatible;
|
|
146
|
+
return [...nonEmpty].reverse().find(isJsonObject) || compatible;
|
|
147
|
+
}
|
|
97
148
|
/**
|
|
98
149
|
* Detect content-policy refusals across the provider failure shapes.
|
|
99
150
|
* Mirrors `LLMProvider.contentPolicyBlocked` semantics exactly so the
|
|
@@ -155,6 +155,7 @@ class ResponsesAdapter {
|
|
|
155
155
|
const key = `${String(payload.item_id || '')}:${String(payload.summary_index || 0)}`;
|
|
156
156
|
const delta = this.extractText(payload.delta);
|
|
157
157
|
if (delta) {
|
|
158
|
+
emittedContent = true;
|
|
158
159
|
reasoningSummaries.set(key, (reasoningSummaries.get(key) || '') + delta);
|
|
159
160
|
yield { type: 'reasoning.summary.delta', delta };
|
|
160
161
|
}
|
|
@@ -185,7 +186,7 @@ class ResponsesAdapter {
|
|
|
185
186
|
calls.set(key, {
|
|
186
187
|
id: String(item.call_id || item.id || key),
|
|
187
188
|
name: String(item.name || ''),
|
|
188
|
-
|
|
189
|
+
argumentParts: item.arguments ? [String(item.arguments)] : [],
|
|
189
190
|
emitted: false,
|
|
190
191
|
});
|
|
191
192
|
}
|
|
@@ -193,9 +194,10 @@ class ResponsesAdapter {
|
|
|
193
194
|
}
|
|
194
195
|
if (eventType === 'response.function_call_arguments.delta') {
|
|
195
196
|
const key = String(payload.item_id || payload.call_id || payload.output_index || '');
|
|
196
|
-
const call = calls.get(key) || { id: String(payload.call_id || key), name: String(payload.name || ''),
|
|
197
|
+
const call = calls.get(key) || { id: String(payload.call_id || key), name: String(payload.name || ''), argumentParts: [], emitted: false };
|
|
197
198
|
const delta = String(payload.delta || '');
|
|
198
|
-
|
|
199
|
+
if (delta)
|
|
200
|
+
call.argumentParts.push(delta);
|
|
199
201
|
calls.set(key, call);
|
|
200
202
|
yield { type: 'tool_call.arguments.delta', id: call.id, delta };
|
|
201
203
|
continue;
|
|
@@ -207,19 +209,21 @@ class ResponsesAdapter {
|
|
|
207
209
|
const call = calls.get(key) || {
|
|
208
210
|
id: String(item.call_id || item.id || key),
|
|
209
211
|
name: String(item.name || ''),
|
|
210
|
-
|
|
212
|
+
argumentParts: item.arguments ? [String(item.arguments)] : [],
|
|
211
213
|
emitted: false,
|
|
212
214
|
};
|
|
213
215
|
call.id = String(item.call_id || call.id);
|
|
214
216
|
call.name = String(item.name || call.name);
|
|
215
|
-
|
|
217
|
+
if (typeof item.arguments === 'string' && item.arguments)
|
|
218
|
+
call.argumentParts.push(item.arguments);
|
|
216
219
|
if (!call.emitted) {
|
|
217
220
|
call.emitted = true;
|
|
221
|
+
const argumentsJson = (0, provider_events_1.assembleCompatibleToolArguments)(call.argumentParts);
|
|
218
222
|
yield { type: 'tool_call.started', id: call.id, name: call.name };
|
|
219
|
-
if (
|
|
220
|
-
yield { type: 'tool_call.arguments.delta', id: call.id, delta:
|
|
223
|
+
if (argumentsJson !== '{}') {
|
|
224
|
+
yield { type: 'tool_call.arguments.delta', id: call.id, delta: argumentsJson };
|
|
221
225
|
}
|
|
222
|
-
yield { type: 'tool_call.completed', id: call.id, name: call.name, arguments:
|
|
226
|
+
yield { type: 'tool_call.completed', id: call.id, name: call.name, arguments: argumentsJson };
|
|
223
227
|
}
|
|
224
228
|
calls.set(key, call);
|
|
225
229
|
}
|
|
@@ -252,9 +256,19 @@ class ResponsesAdapter {
|
|
|
252
256
|
yield { type: 'response.failed', error: '[LLM Error] Responses stream ended before response.completed.' };
|
|
253
257
|
}
|
|
254
258
|
else if (!emittedContent && calls.size === 0) {
|
|
255
|
-
yield { type: 'response.failed', error: '[Error]
|
|
259
|
+
yield { type: 'response.failed', error: '[Error] Provider returned an empty response.' };
|
|
256
260
|
}
|
|
257
261
|
else {
|
|
262
|
+
// Some compatible Responses providers omit output_item.done but still
|
|
263
|
+
// complete the response. Preserve that valid tool activity and fold
|
|
264
|
+
// cumulative argument snapshots before handing it to the kernel.
|
|
265
|
+
for (const call of calls.values()) {
|
|
266
|
+
if (call.emitted)
|
|
267
|
+
continue;
|
|
268
|
+
const argumentsJson = (0, provider_events_1.assembleCompatibleToolArguments)(call.argumentParts);
|
|
269
|
+
yield { type: 'tool_call.started', id: call.id, name: call.name };
|
|
270
|
+
yield { type: 'tool_call.completed', id: call.id, name: call.name, arguments: argumentsJson };
|
|
271
|
+
}
|
|
258
272
|
yield { type: 'response.completed' };
|
|
259
273
|
}
|
|
260
274
|
}
|
package/dist/server.js
CHANGED
|
@@ -1298,6 +1298,27 @@ async function handleApi(req, res, body) {
|
|
|
1298
1298
|
mobileJson(res, await hostedConversationUiAction({ workspaceId, conversationId }, action, String(params.value || ''), params));
|
|
1299
1299
|
return;
|
|
1300
1300
|
}
|
|
1301
|
+
case '/api/queue-action': {
|
|
1302
|
+
// Desktop-renderer queue mutations. The mobile endpoint requires a
|
|
1303
|
+
// pairing token; the renderer shares the same process and is trusted,
|
|
1304
|
+
// so it gets a dedicated unauthenticated route that reuses the same
|
|
1305
|
+
// GUI runtime-pool queueAction path (update/delete/reorder/guide).
|
|
1306
|
+
const params = JSON.parse(body || '{}');
|
|
1307
|
+
const workspaceId = String(params.workspaceId || '');
|
|
1308
|
+
const conversationId = String(params.conversationId || '');
|
|
1309
|
+
const action = String(params.action || '');
|
|
1310
|
+
const allowed = new Set(['queue_enqueue', 'queue_update', 'queue_delete', 'queue_reorder', 'queue_toggle_pause', 'queue_guide']);
|
|
1311
|
+
if (!workspaceId || !conversationId || !allowed.has(action)) {
|
|
1312
|
+
jsonResponse(res, { error: 'workspaceId, conversationId, and a valid queue action are required' }, 400);
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
if (!hostedConversationUiAction) {
|
|
1316
|
+
jsonResponse(res, { error: 'This action requires the GUI-hosted runtime pool' }, 409);
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
jsonResponse(res, await hostedConversationUiAction({ workspaceId, conversationId }, action, String(params.value || ''), params));
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1301
1322
|
case '/api/mobile/conversation-rename': {
|
|
1302
1323
|
const params = JSON.parse(body || '{}');
|
|
1303
1324
|
const workspaceId = String(params.workspaceId || '');
|