@xcanwin/manyoyo 5.8.5 → 5.8.9
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 +1 -0
- package/bin/manyoyo.js +265 -174
- package/lib/global-config.js +1 -198
- package/lib/image-build.js +20 -4
- package/lib/init-config.js +22 -10
- package/lib/json5-text-edit.js +238 -0
- package/lib/plugin/playwright-bootstrap.js +116 -0
- package/lib/plugin/playwright-command-output.js +95 -0
- package/lib/plugin/playwright-container-runtime.js +94 -0
- package/lib/plugin/playwright-extension-manager.js +265 -0
- package/lib/plugin/playwright-extension-paths.js +98 -0
- package/lib/plugin/playwright-host-runtime.js +114 -0
- package/lib/plugin/playwright-scene-config.js +137 -0
- package/lib/plugin/playwright-scene-drivers.js +285 -0
- package/lib/plugin/playwright-scene-state.js +80 -0
- package/lib/plugin/playwright.js +169 -1049
- package/lib/runtime-normalizers.js +65 -0
- package/lib/runtime-resolver.js +195 -0
- package/lib/web/agent-command.js +153 -0
- package/lib/web/api-route-helpers.js +88 -0
- package/lib/web/container-exec.js +215 -0
- package/lib/web/http-handlers.js +163 -0
- package/lib/web/runtime-state.js +50 -0
- package/lib/web/server-context.js +71 -0
- package/lib/web/server-lifecycle.js +129 -0
- package/lib/web/server.js +293 -2496
- package/lib/web/session-api-routes.js +390 -0
- package/lib/web/structured-output.js +149 -0
- package/lib/web/structured-trace.js +603 -0
- package/lib/web/system-api-routes.js +114 -0
- package/lib/web/terminal-session.js +205 -0
- package/lib/web/upgrade-handler.js +94 -0
- package/package.json +1 -1
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function createSessionApiRoutes(deps) {
|
|
4
|
+
const {
|
|
5
|
+
req,
|
|
6
|
+
res,
|
|
7
|
+
ctx,
|
|
8
|
+
state,
|
|
9
|
+
WEB_DEFAULT_AGENT_ID,
|
|
10
|
+
withSessionRef,
|
|
11
|
+
withJsonBody,
|
|
12
|
+
withSessionJsonBody,
|
|
13
|
+
getRequiredBodyText,
|
|
14
|
+
prepareAgentRequest,
|
|
15
|
+
sendJson,
|
|
16
|
+
sendNdjson,
|
|
17
|
+
buildCreateRuntime,
|
|
18
|
+
ensureWebContainer,
|
|
19
|
+
setWebSessionAgentPromptCommand,
|
|
20
|
+
patchWebSessionHistory,
|
|
21
|
+
listWebManyoyoContainers,
|
|
22
|
+
listWebHistorySessionNames,
|
|
23
|
+
loadWebSessionHistory,
|
|
24
|
+
listWebAgentSessions,
|
|
25
|
+
buildSessionSummary,
|
|
26
|
+
createWebAgentSession,
|
|
27
|
+
saveWebSessionHistory,
|
|
28
|
+
buildWebSessionKey,
|
|
29
|
+
getWebAgentSession,
|
|
30
|
+
createEmptyWebAgentSession,
|
|
31
|
+
buildSessionDetail,
|
|
32
|
+
hasOwn,
|
|
33
|
+
setWebAgentSessionPromptCommand,
|
|
34
|
+
appendWebSessionMessage,
|
|
35
|
+
execCommandInWebContainer,
|
|
36
|
+
finalizeWebAgentExecution,
|
|
37
|
+
execAgentInWebContainerStream,
|
|
38
|
+
appendWebAgentTraceMessage,
|
|
39
|
+
stopWebAgentRun,
|
|
40
|
+
removeWebSessionHistory
|
|
41
|
+
} = deps;
|
|
42
|
+
|
|
43
|
+
return [
|
|
44
|
+
{
|
|
45
|
+
method: 'GET',
|
|
46
|
+
match: currentPath => currentPath === '/api/sessions' ? [] : null,
|
|
47
|
+
handler: async () => {
|
|
48
|
+
const containerMap = listWebManyoyoContainers(ctx);
|
|
49
|
+
const names = new Set([
|
|
50
|
+
...Object.keys(containerMap),
|
|
51
|
+
...listWebHistorySessionNames(state.webHistoryDir, ctx.isValidContainerName)
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
const sessions = Array.from(names)
|
|
55
|
+
.flatMap(name => {
|
|
56
|
+
const history = loadWebSessionHistory(state.webHistoryDir, name);
|
|
57
|
+
return listWebAgentSessions(history, { includeSyntheticDefault: true })
|
|
58
|
+
.map(agentSession => buildSessionSummary(ctx, state, containerMap, {
|
|
59
|
+
containerName: name,
|
|
60
|
+
agentId: agentSession.agentId
|
|
61
|
+
}))
|
|
62
|
+
.filter(Boolean);
|
|
63
|
+
})
|
|
64
|
+
.sort((a, b) => {
|
|
65
|
+
const timeA = a.updatedAt ? new Date(a.updatedAt).getTime() : 0;
|
|
66
|
+
const timeB = b.updatedAt ? new Date(b.updatedAt).getTime() : 0;
|
|
67
|
+
return timeB - timeA;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
sendJson(res, 200, { sessions });
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
method: 'POST',
|
|
75
|
+
match: currentPath => currentPath === '/api/sessions' ? [] : null,
|
|
76
|
+
handler: withJsonBody(async payload => {
|
|
77
|
+
let runtime = null;
|
|
78
|
+
try {
|
|
79
|
+
runtime = buildCreateRuntime(ctx, state, payload);
|
|
80
|
+
} catch (e) {
|
|
81
|
+
sendJson(res, 400, { error: e.message || '创建参数错误' });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
await ensureWebContainer(ctx, state, runtime);
|
|
86
|
+
setWebSessionAgentPromptCommand(state.webHistoryDir, runtime.containerName, runtime.agentPromptCommand);
|
|
87
|
+
patchWebSessionHistory(state.webHistoryDir, runtime.containerName, {
|
|
88
|
+
applied: runtime.applied
|
|
89
|
+
});
|
|
90
|
+
sendJson(res, 200, { name: runtime.containerName, applied: runtime.applied });
|
|
91
|
+
})
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
method: 'POST',
|
|
95
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/agents$/),
|
|
96
|
+
handler: withSessionRef(async sessionRef => {
|
|
97
|
+
const history = loadWebSessionHistory(state.webHistoryDir, sessionRef.containerName);
|
|
98
|
+
const agentSession = createWebAgentSession(history);
|
|
99
|
+
saveWebSessionHistory(state.webHistoryDir, sessionRef.containerName, history);
|
|
100
|
+
sendJson(res, 200, {
|
|
101
|
+
name: buildWebSessionKey(sessionRef.containerName, agentSession.agentId),
|
|
102
|
+
containerName: sessionRef.containerName,
|
|
103
|
+
agentId: agentSession.agentId,
|
|
104
|
+
agentName: agentSession.agentName
|
|
105
|
+
});
|
|
106
|
+
})
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
method: 'GET',
|
|
110
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/messages$/),
|
|
111
|
+
handler: withSessionRef(async sessionRef => {
|
|
112
|
+
const history = loadWebSessionHistory(state.webHistoryDir, sessionRef.containerName);
|
|
113
|
+
const agentSession = getWebAgentSession(history, sessionRef.agentId)
|
|
114
|
+
|| createEmptyWebAgentSession(sessionRef.agentId);
|
|
115
|
+
sendJson(res, 200, {
|
|
116
|
+
name: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId),
|
|
117
|
+
containerName: sessionRef.containerName,
|
|
118
|
+
agentId: sessionRef.agentId,
|
|
119
|
+
messages: agentSession.messages
|
|
120
|
+
});
|
|
121
|
+
})
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
method: 'GET',
|
|
125
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/detail$/),
|
|
126
|
+
handler: withSessionRef(async sessionRef => {
|
|
127
|
+
const containerMap = listWebManyoyoContainers(ctx);
|
|
128
|
+
const detail = buildSessionDetail(ctx, state, containerMap, sessionRef);
|
|
129
|
+
sendJson(res, 200, { name: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId), detail });
|
|
130
|
+
})
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
method: 'PUT',
|
|
134
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/agent-template$/),
|
|
135
|
+
handler: withSessionJsonBody(async (sessionRef, payload) => {
|
|
136
|
+
const normalizedPayload = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
|
|
137
|
+
const hasContainerTemplate = hasOwn(normalizedPayload, 'containerAgentPromptCommand');
|
|
138
|
+
const hasAgentOverride = hasOwn(normalizedPayload, 'agentPromptCommandOverride');
|
|
139
|
+
if (!hasContainerTemplate && !hasAgentOverride) {
|
|
140
|
+
sendJson(res, 400, { error: '至少提供一个模板字段' });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (hasAgentOverride && sessionRef.agentId === WEB_DEFAULT_AGENT_ID) {
|
|
144
|
+
sendJson(res, 400, { error: '默认 AGENT 不支持单独覆盖模板,请直接修改容器模板' });
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
if (hasContainerTemplate) {
|
|
150
|
+
setWebSessionAgentPromptCommand(
|
|
151
|
+
state.webHistoryDir,
|
|
152
|
+
sessionRef.containerName,
|
|
153
|
+
normalizedPayload.containerAgentPromptCommand
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
if (hasAgentOverride) {
|
|
157
|
+
setWebAgentSessionPromptCommand(
|
|
158
|
+
state.webHistoryDir,
|
|
159
|
+
sessionRef,
|
|
160
|
+
normalizedPayload.agentPromptCommandOverride
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
} catch (e) {
|
|
164
|
+
sendJson(res, 400, { error: e.message || '保存 Agent 模板失败' });
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const containerMap = listWebManyoyoContainers(ctx);
|
|
169
|
+
const detail = buildSessionDetail(ctx, state, containerMap, sessionRef);
|
|
170
|
+
sendJson(res, 200, {
|
|
171
|
+
saved: true,
|
|
172
|
+
name: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId),
|
|
173
|
+
detail
|
|
174
|
+
});
|
|
175
|
+
}, '请求参数错误')
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
method: 'POST',
|
|
179
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/run$/),
|
|
180
|
+
handler: withSessionJsonBody(async (sessionRef, payload) => {
|
|
181
|
+
const command = getRequiredBodyText(payload, 'command', 'command 不能为空');
|
|
182
|
+
if (!command) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
await ensureWebContainer(ctx, state, sessionRef.containerName, sessionRef);
|
|
187
|
+
appendWebSessionMessage(state.webHistoryDir, sessionRef, 'user', command);
|
|
188
|
+
const result = await execCommandInWebContainer(ctx, sessionRef.containerName, command);
|
|
189
|
+
appendWebSessionMessage(
|
|
190
|
+
state.webHistoryDir,
|
|
191
|
+
sessionRef,
|
|
192
|
+
'assistant',
|
|
193
|
+
result.output,
|
|
194
|
+
{ exitCode: result.exitCode }
|
|
195
|
+
);
|
|
196
|
+
sendJson(res, 200, { exitCode: result.exitCode, output: result.output });
|
|
197
|
+
})
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
method: 'POST',
|
|
201
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/agent$/),
|
|
202
|
+
handler: withSessionJsonBody(async (sessionRef, payload) => {
|
|
203
|
+
const prompt = getRequiredBodyText(payload, 'prompt', 'prompt 不能为空');
|
|
204
|
+
if (!prompt) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const prepared = await prepareAgentRequest(sessionRef, prompt);
|
|
209
|
+
if (!prepared) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const { agentSession, agentMeta, command, contextMode, resumeAttempted, resumeSucceeded, resumeError } = prepared;
|
|
214
|
+
appendWebSessionMessage(state.webHistoryDir, sessionRef, 'user', prompt, {
|
|
215
|
+
mode: 'agent',
|
|
216
|
+
contextMode
|
|
217
|
+
});
|
|
218
|
+
const result = await execCommandInWebContainer(ctx, sessionRef.containerName, command, {
|
|
219
|
+
agentProgram: agentMeta.agentProgram
|
|
220
|
+
});
|
|
221
|
+
finalizeWebAgentExecution(state, sessionRef, agentSession, agentMeta, {
|
|
222
|
+
contextMode,
|
|
223
|
+
resumeAttempted,
|
|
224
|
+
resumeSucceeded,
|
|
225
|
+
resumeError
|
|
226
|
+
}, result);
|
|
227
|
+
sendJson(res, 200, {
|
|
228
|
+
exitCode: result.exitCode,
|
|
229
|
+
output: result.output,
|
|
230
|
+
contextMode,
|
|
231
|
+
resumeAttempted,
|
|
232
|
+
resumeSucceeded,
|
|
233
|
+
interrupted: result.interrupted === true
|
|
234
|
+
});
|
|
235
|
+
})
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
method: 'POST',
|
|
239
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/agent\/stream$/),
|
|
240
|
+
handler: withSessionJsonBody(async (sessionRef, payload) => {
|
|
241
|
+
const prompt = getRequiredBodyText(payload, 'prompt', 'prompt 不能为空');
|
|
242
|
+
if (!prompt) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (state.agentRuns.has(sessionRef.containerName)) {
|
|
246
|
+
sendJson(res, 409, { error: '当前会话已有运行中的 agent 任务' });
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const prepared = await prepareAgentRequest(sessionRef, prompt);
|
|
251
|
+
if (!prepared) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const { agentSession, agentMeta, command, contextMode, resumeAttempted, resumeSucceeded, resumeError } = prepared;
|
|
256
|
+
const traceLines = ['[执行过程]'];
|
|
257
|
+
const traceEvents = [];
|
|
258
|
+
appendWebSessionMessage(state.webHistoryDir, sessionRef, 'user', prompt, {
|
|
259
|
+
mode: 'agent',
|
|
260
|
+
contextMode
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
res.writeHead(200, {
|
|
264
|
+
'Content-Type': 'application/x-ndjson; charset=utf-8',
|
|
265
|
+
'Cache-Control': 'no-store',
|
|
266
|
+
'X-Accel-Buffering': 'no'
|
|
267
|
+
});
|
|
268
|
+
sendNdjson(res, {
|
|
269
|
+
type: 'meta',
|
|
270
|
+
containerName: sessionRef.containerName,
|
|
271
|
+
sessionName: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId),
|
|
272
|
+
contextMode,
|
|
273
|
+
resumeAttempted,
|
|
274
|
+
resumeSucceeded,
|
|
275
|
+
agentProgram: agentMeta.agentProgram
|
|
276
|
+
});
|
|
277
|
+
if (contextMode) {
|
|
278
|
+
traceLines.push(`上下文模式: ${contextMode}`);
|
|
279
|
+
}
|
|
280
|
+
if (resumeAttempted) {
|
|
281
|
+
traceLines.push(resumeSucceeded ? '会话恢复成功' : '会话恢复失败,已回退到历史注入');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
try {
|
|
285
|
+
const result = await execAgentInWebContainerStream(ctx, state, sessionRef, command, {
|
|
286
|
+
agentProgram: agentMeta.agentProgram,
|
|
287
|
+
onEvent: event => {
|
|
288
|
+
if (event && event.type === 'trace' && event.text) {
|
|
289
|
+
traceLines.push(String(event.text));
|
|
290
|
+
if (event.traceEvent && typeof event.traceEvent === 'object') {
|
|
291
|
+
traceEvents.push(event.traceEvent);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
sendNdjson(res, event);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
traceLines.push(result.interrupted === true ? '[任务] 已停止' : '[任务] 已完成');
|
|
298
|
+
appendWebAgentTraceMessage(state.webHistoryDir, sessionRef, traceLines.join('\n'), {
|
|
299
|
+
traceEvents,
|
|
300
|
+
contextMode,
|
|
301
|
+
resumeAttempted,
|
|
302
|
+
resumeSucceeded,
|
|
303
|
+
interrupted: result.interrupted === true
|
|
304
|
+
});
|
|
305
|
+
finalizeWebAgentExecution(state, sessionRef, agentSession, agentMeta, {
|
|
306
|
+
contextMode,
|
|
307
|
+
resumeAttempted,
|
|
308
|
+
resumeSucceeded,
|
|
309
|
+
resumeError
|
|
310
|
+
}, result);
|
|
311
|
+
sendNdjson(res, {
|
|
312
|
+
type: 'result',
|
|
313
|
+
exitCode: result.exitCode,
|
|
314
|
+
output: result.output,
|
|
315
|
+
contextMode,
|
|
316
|
+
resumeAttempted,
|
|
317
|
+
resumeSucceeded,
|
|
318
|
+
interrupted: result.interrupted === true
|
|
319
|
+
});
|
|
320
|
+
} catch (e) {
|
|
321
|
+
traceLines.push(`[错误] ${e && e.message ? e.message : 'Agent 执行失败'}`);
|
|
322
|
+
appendWebAgentTraceMessage(state.webHistoryDir, sessionRef, traceLines.join('\n'), {
|
|
323
|
+
traceEvents,
|
|
324
|
+
contextMode,
|
|
325
|
+
resumeAttempted,
|
|
326
|
+
resumeSucceeded,
|
|
327
|
+
interrupted: true
|
|
328
|
+
});
|
|
329
|
+
sendNdjson(res, {
|
|
330
|
+
type: 'error',
|
|
331
|
+
error: e && e.message ? e.message : 'Agent 执行失败'
|
|
332
|
+
});
|
|
333
|
+
} finally {
|
|
334
|
+
res.end();
|
|
335
|
+
}
|
|
336
|
+
})
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
method: 'POST',
|
|
340
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/agent\/stop$/),
|
|
341
|
+
handler: withSessionRef(async sessionRef => {
|
|
342
|
+
const stopped = stopWebAgentRun(state, sessionRef.containerName);
|
|
343
|
+
if (!stopped) {
|
|
344
|
+
sendJson(res, 404, { error: '当前会话没有运行中的 agent 任务' });
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
sendJson(res, 200, { ok: true, stopping: true });
|
|
348
|
+
})
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
method: 'POST',
|
|
352
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/remove$/),
|
|
353
|
+
handler: withSessionRef(async sessionRef => {
|
|
354
|
+
if (ctx.containerExists(sessionRef.containerName)) {
|
|
355
|
+
ctx.removeContainer(sessionRef.containerName);
|
|
356
|
+
appendWebSessionMessage(state.webHistoryDir, sessionRef, 'system', `容器 ${sessionRef.containerName} 已删除。`);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
sendJson(res, 200, { removed: true, name: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId) });
|
|
360
|
+
})
|
|
361
|
+
},
|
|
362
|
+
{
|
|
363
|
+
method: 'POST',
|
|
364
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/remove-with-history$/),
|
|
365
|
+
handler: withSessionRef(async sessionRef => {
|
|
366
|
+
const history = loadWebSessionHistory(state.webHistoryDir, sessionRef.containerName);
|
|
367
|
+
if (history.agents && typeof history.agents === 'object') {
|
|
368
|
+
if (sessionRef.agentId === WEB_DEFAULT_AGENT_ID) {
|
|
369
|
+
delete history.agents[WEB_DEFAULT_AGENT_ID];
|
|
370
|
+
} else {
|
|
371
|
+
delete history.agents[sessionRef.agentId];
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (!Object.keys(history.agents || {}).length && !ctx.containerExists(sessionRef.containerName)) {
|
|
375
|
+
removeWebSessionHistory(state.webHistoryDir, sessionRef.containerName);
|
|
376
|
+
} else {
|
|
377
|
+
saveWebSessionHistory(state.webHistoryDir, sessionRef.containerName, history);
|
|
378
|
+
}
|
|
379
|
+
sendJson(res, 200, {
|
|
380
|
+
removedHistory: true,
|
|
381
|
+
name: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId)
|
|
382
|
+
});
|
|
383
|
+
})
|
|
384
|
+
}
|
|
385
|
+
];
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
module.exports = {
|
|
389
|
+
createSessionApiRoutes
|
|
390
|
+
};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function parseJsonObjectLine(line) {
|
|
4
|
+
const text = String(line || '').trim();
|
|
5
|
+
if (!text) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
const payload = JSON.parse(text);
|
|
10
|
+
return payload && typeof payload === 'object' ? payload : null;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function collectStructuredText(value) {
|
|
17
|
+
if (typeof value === 'string') {
|
|
18
|
+
return value.trim();
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(value)) {
|
|
21
|
+
return value.map(item => collectStructuredText(item)).filter(Boolean).join('\n').trim();
|
|
22
|
+
}
|
|
23
|
+
if (!value || typeof value !== 'object') {
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
if (typeof value.text === 'string' && value.text.trim()) {
|
|
27
|
+
return value.text.trim();
|
|
28
|
+
}
|
|
29
|
+
if (typeof value.content === 'string' && value.content.trim()) {
|
|
30
|
+
return value.content.trim();
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(value.content)) {
|
|
33
|
+
return value.content.map(item => collectStructuredText(item)).filter(Boolean).join('\n').trim();
|
|
34
|
+
}
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function createStructuredOutputHelpers(deps) {
|
|
39
|
+
const {
|
|
40
|
+
pickFirstString,
|
|
41
|
+
toPlainObject,
|
|
42
|
+
extractAgentMessageFromCodexJsonl
|
|
43
|
+
} = deps;
|
|
44
|
+
|
|
45
|
+
function extractClaudeAgentMessage(text) {
|
|
46
|
+
let lastMessage = '';
|
|
47
|
+
for (const rawLine of String(text || '').split('\n')) {
|
|
48
|
+
const payload = parseJsonObjectLine(rawLine);
|
|
49
|
+
if (!payload || payload.type !== 'assistant') {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const message = toPlainObject(payload.message);
|
|
53
|
+
const content = Array.isArray(message.content) ? message.content : [];
|
|
54
|
+
const nextMessage = content
|
|
55
|
+
.filter(item => item && typeof item === 'object' && item.type === 'text')
|
|
56
|
+
.map(item => collectStructuredText(item))
|
|
57
|
+
.filter(Boolean)
|
|
58
|
+
.join('\n')
|
|
59
|
+
.trim();
|
|
60
|
+
if (nextMessage) {
|
|
61
|
+
lastMessage = nextMessage;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return lastMessage.trim();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function extractGeminiAgentMessage(text) {
|
|
68
|
+
let lastMessage = '';
|
|
69
|
+
let deltaMessage = '';
|
|
70
|
+
for (const rawLine of String(text || '').split('\n')) {
|
|
71
|
+
const payload = parseJsonObjectLine(rawLine);
|
|
72
|
+
if (!payload || payload.type !== 'message' || payload.role !== 'assistant') {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const content = collectStructuredText(payload.content);
|
|
76
|
+
if (!content) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (payload.delta === true) {
|
|
80
|
+
deltaMessage += content;
|
|
81
|
+
lastMessage = deltaMessage.trim();
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
deltaMessage = '';
|
|
85
|
+
lastMessage = content;
|
|
86
|
+
}
|
|
87
|
+
return lastMessage.trim();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function extractOpenCodeAgentMessage(text) {
|
|
91
|
+
let lastMessage = '';
|
|
92
|
+
let deltaMessage = '';
|
|
93
|
+
for (const rawLine of String(text || '').split('\n')) {
|
|
94
|
+
const payload = parseJsonObjectLine(rawLine);
|
|
95
|
+
if (!payload) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const eventType = pickFirstString(payload.type);
|
|
99
|
+
const message = toPlainObject(payload.message);
|
|
100
|
+
const role = pickFirstString(payload.role, message.role);
|
|
101
|
+
if (eventType !== 'message' && eventType !== 'assistant' && eventType !== 'assistant_message' && eventType !== 'text') {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (role && role !== 'assistant') {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const content = collectStructuredText(message.content || payload.content || payload.text || payload);
|
|
108
|
+
if (!content) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (payload.delta === true) {
|
|
112
|
+
deltaMessage += content;
|
|
113
|
+
lastMessage = deltaMessage.trim();
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
deltaMessage = '';
|
|
117
|
+
lastMessage = content;
|
|
118
|
+
}
|
|
119
|
+
return lastMessage.trim();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function extractAgentMessageFromStructuredOutput(agentProgram, text) {
|
|
123
|
+
if (agentProgram === 'codex') {
|
|
124
|
+
return extractAgentMessageFromCodexJsonl(text);
|
|
125
|
+
}
|
|
126
|
+
if (agentProgram === 'claude') {
|
|
127
|
+
return extractClaudeAgentMessage(text);
|
|
128
|
+
}
|
|
129
|
+
if (agentProgram === 'gemini') {
|
|
130
|
+
return extractGeminiAgentMessage(text);
|
|
131
|
+
}
|
|
132
|
+
if (agentProgram === 'opencode') {
|
|
133
|
+
return extractOpenCodeAgentMessage(text);
|
|
134
|
+
}
|
|
135
|
+
return '';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
parseJsonObjectLine,
|
|
140
|
+
collectStructuredText,
|
|
141
|
+
extractAgentMessageFromStructuredOutput
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
module.exports = {
|
|
146
|
+
parseJsonObjectLine,
|
|
147
|
+
collectStructuredText,
|
|
148
|
+
createStructuredOutputHelpers
|
|
149
|
+
};
|