@xcanwin/manyoyo 5.8.9 → 5.8.10

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.
@@ -1,71 +0,0 @@
1
- 'use strict';
2
-
3
- const os = require('os');
4
- const path = require('path');
5
-
6
- function createWebServerContextHelpers(options = {}) {
7
- const createInitialWebRuntimeState = options.createInitialWebRuntimeState || (base => base);
8
- const getDefaultWebConfigPath = options.getDefaultWebConfigPath || (() => '');
9
-
10
- return {
11
- createWebServerContext(rawOptions = {}) {
12
- const fallbackLogger = {
13
- info: () => {},
14
- warn: () => {},
15
- error: () => {}
16
- };
17
- const ctx = {
18
- serverHost: rawOptions.serverHost || '127.0.0.1',
19
- serverPort: rawOptions.serverPort,
20
- authUser: rawOptions.authUser,
21
- authPass: rawOptions.authPass,
22
- authPassAuto: rawOptions.authPassAuto,
23
- dockerCmd: rawOptions.dockerCmd,
24
- hostPath: rawOptions.hostPath,
25
- containerPath: rawOptions.containerPath,
26
- imageName: rawOptions.imageName,
27
- imageVersion: rawOptions.imageVersion,
28
- execCommandPrefix: rawOptions.execCommandPrefix,
29
- execCommand: rawOptions.execCommand,
30
- execCommandSuffix: rawOptions.execCommandSuffix,
31
- contModeArgs: rawOptions.contModeArgs,
32
- containerExtraArgs: rawOptions.containerExtraArgs,
33
- containerEnvs: rawOptions.containerEnvs,
34
- containerVolumes: rawOptions.containerVolumes,
35
- containerPorts: rawOptions.containerPorts,
36
- validateHostPath: rawOptions.validateHostPath,
37
- formatDate: rawOptions.formatDate,
38
- isValidContainerName: rawOptions.isValidContainerName,
39
- containerExists: rawOptions.containerExists,
40
- getContainerStatus: rawOptions.getContainerStatus,
41
- waitForContainerReady: rawOptions.waitForContainerReady,
42
- dockerExecArgs: rawOptions.dockerExecArgs,
43
- showImagePullHint: rawOptions.showImagePullHint,
44
- removeContainer: rawOptions.removeContainer,
45
- logger: rawOptions.logger && typeof rawOptions.logger.info === 'function' ? rawOptions.logger : fallbackLogger,
46
- colors: rawOptions.colors || {
47
- GREEN: '',
48
- CYAN: '',
49
- YELLOW: '',
50
- NC: ''
51
- }
52
- };
53
-
54
- if (!ctx.authUser || !ctx.authPass) {
55
- throw new Error('Web 认证配置缺失,请设置 serve -U / serve -P');
56
- }
57
-
58
- return ctx;
59
- },
60
- createWebServerState(rawOptions = {}) {
61
- return createInitialWebRuntimeState({
62
- webHistoryDir: rawOptions.webHistoryDir || path.join(os.homedir(), '.manyoyo', 'web-history'),
63
- webConfigPath: rawOptions.webConfigPath || getDefaultWebConfigPath()
64
- });
65
- }
66
- };
67
- }
68
-
69
- module.exports = {
70
- createWebServerContextHelpers
71
- };
@@ -1,129 +0,0 @@
1
- 'use strict';
2
-
3
- function createWebServerLifecycleHelpers(options = {}) {
4
- const http = options.http;
5
- const WebSocket = options.WebSocket;
6
- const formatUrlHost = options.formatUrlHost || (host => host);
7
- const normalizeTerminalSize = options.normalizeTerminalSize || ((cols, rows) => ({ cols, rows }));
8
- const bindTerminalWebSocket = options.bindTerminalWebSocket || (() => {});
9
- const handleWebUpgradeRequest = options.handleWebUpgradeRequest || (() => {});
10
- const sendJson = options.sendJson || (() => {});
11
- const sendHtml = options.sendHtml || (() => {});
12
- const cleanupWebRuntimeState = options.cleanupWebRuntimeState || (() => {});
13
-
14
- return {
15
- createWsServer(ctx, state) {
16
- const wsServer = new WebSocket.Server({
17
- noServer: true,
18
- maxPayload: 1024 * 1024
19
- });
20
- wsServer.on('error', err => {
21
- ctx.logger.error('ws server error', err);
22
- });
23
-
24
- wsServer.on('connection', (ws, req, meta = {}) => {
25
- const containerName = meta.containerName;
26
- if (!containerName || !ctx.isValidContainerName(containerName)) {
27
- ws.close();
28
- return;
29
- }
30
- const { cols, rows } = normalizeTerminalSize(meta.cols, meta.rows);
31
- bindTerminalWebSocket(ctx, state, ws, containerName, cols, rows);
32
- });
33
-
34
- return wsServer;
35
- },
36
- createHttpServer(ctx, state, wsServer, handleWebHttpRequest) {
37
- const server = http.createServer(async (req, res) => {
38
- try {
39
- const fallbackHost = `${formatUrlHost(ctx.serverHost)}:${ctx.serverPort}`;
40
- const url = new URL(req.url, `http://${req.headers.host || fallbackHost}`);
41
- const pathname = url.pathname;
42
- await handleWebHttpRequest(req, res, pathname, ctx, state);
43
- } catch (error) {
44
- ctx.logger.error('http request error', {
45
- method: req && req.method ? req.method : '',
46
- url: req && req.url ? req.url : '',
47
- message: error && error.message ? error.message : 'Server Error'
48
- });
49
- if ((req.url || '').startsWith('/api/')) {
50
- sendJson(res, 500, { error: error.message || 'Server Error' });
51
- } else {
52
- sendHtml(res, 500, '<h1>500 Server Error</h1>');
53
- }
54
- }
55
- });
56
- server.on('error', err => {
57
- ctx.logger.error('http server error', err);
58
- });
59
- server.on('close', () => {
60
- ctx.logger.warn('http server closed');
61
- });
62
- server.on('upgrade', (req, socket, head) => {
63
- handleWebUpgradeRequest(req, socket, head, wsServer, ctx, state, server.__manyoyoListenPort || ctx.serverPort);
64
- });
65
- return server;
66
- },
67
- async listenWebServer(server, ctx) {
68
- let listenPort = ctx.serverPort;
69
- await new Promise((resolve, reject) => {
70
- server.once('error', err => {
71
- ctx.logger.error('http server listen failed', err);
72
- reject(err);
73
- });
74
- server.listen(ctx.serverPort, ctx.serverHost, () => {
75
- const address = server.address();
76
- if (address && typeof address === 'object' && typeof address.port === 'number') {
77
- listenPort = address.port;
78
- }
79
- server.__manyoyoListenPort = listenPort;
80
- const { GREEN, CYAN, YELLOW, NC } = ctx.colors;
81
- const listenHost = formatUrlHost(ctx.serverHost);
82
- console.log(`${GREEN}✅ MANYOYO Web 服务已启动: http://${listenHost}:${listenPort}${NC}`);
83
- console.log(`${CYAN}提示: 左侧是 manyoyo 容器会话列表,中间是活动/终端/配置/检查工作台,右侧显示当前会话上下文。${NC}`);
84
- if (ctx.serverHost === '0.0.0.0') {
85
- console.log(`${CYAN}提示: 当前监听全部网卡,请用本机局域网 IP 访问。${NC}`);
86
- }
87
- console.log(`${CYAN}🔐 登录用户名: ${YELLOW}${ctx.authUser}${NC}`);
88
- if (ctx.authPassAuto) {
89
- console.log(`${CYAN}🔐 登录密码(本次随机): ${YELLOW}${ctx.authPass}${NC}`);
90
- } else {
91
- console.log(`${CYAN}🔐 登录密码: 使用你配置的 serve -P / serverPass / MANYOYO_SERVER_PASS${NC}`);
92
- }
93
- ctx.logger.info('web server started', {
94
- host: ctx.serverHost,
95
- port: listenPort,
96
- authUser: ctx.authUser,
97
- authPassAuto: Boolean(ctx.authPassAuto)
98
- });
99
- resolve();
100
- });
101
- });
102
- return listenPort;
103
- },
104
- closeWebServer(server, wsServer, ctx, state) {
105
- return new Promise(resolve => {
106
- ctx.logger.info('web server closing');
107
- cleanupWebRuntimeState(state);
108
-
109
- const closeHttp = () => {
110
- if (!server.listening) {
111
- resolve();
112
- return;
113
- }
114
- server.close(() => resolve());
115
- };
116
-
117
- try {
118
- wsServer.close(() => closeHttp());
119
- } catch {
120
- closeHttp();
121
- }
122
- });
123
- }
124
- };
125
- }
126
-
127
- module.exports = {
128
- createWebServerLifecycleHelpers
129
- };
@@ -1,390 +0,0 @@
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
- };