@xcanwin/manyoyo 5.7.14 → 5.8.0

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/lib/web/server.js CHANGED
@@ -137,6 +137,7 @@ function createEmptyWebAgentSession(agentId, agentName) {
137
137
  return {
138
138
  agentId,
139
139
  agentName: normalizeWebAgentName(agentId, agentName),
140
+ agentPromptCommand: '',
140
141
  updatedAt: null,
141
142
  messages: [],
142
143
  lastResumeAt: null,
@@ -150,6 +151,9 @@ function normalizeWebAgentSessionRecord(agentId, rawAgent) {
150
151
  return {
151
152
  agentId,
152
153
  agentName: normalizeWebAgentName(agentId, source.agentName),
154
+ agentPromptCommand: typeof source.agentPromptCommand === 'string'
155
+ ? normalizeAgentPromptCommandTemplate(source.agentPromptCommand, `agents.${agentId}.agentPromptCommand`)
156
+ : '',
153
157
  updatedAt: typeof source.updatedAt === 'string' ? source.updatedAt : null,
154
158
  messages: Array.isArray(source.messages) ? source.messages : [],
155
159
  lastResumeAt: typeof source.lastResumeAt === 'string' ? source.lastResumeAt : null,
@@ -253,7 +257,7 @@ function saveWebSessionHistory(webHistoryDir, containerName, history) {
253
257
  ensureWebHistoryDir(webHistoryDir);
254
258
  const filePath = getWebHistoryFile(webHistoryDir, containerName);
255
259
  const normalized = normalizeWebHistoryRecord(containerName, history);
256
- const runtimeMeta = getAgentRuntimeMeta(normalized);
260
+ const runtimeMeta = getAgentRuntimeMeta(normalized.agentPromptCommand || '');
257
261
  const defaultAgent = getWebAgentSession(normalized, WEB_DEFAULT_AGENT_ID) || createEmptyWebAgentSession(WEB_DEFAULT_AGENT_ID);
258
262
  const legacyCompatible = {
259
263
  ...normalized,
@@ -383,6 +387,81 @@ function setWebSessionAgentPromptCommand(webHistoryDir, containerName, agentProm
383
387
  saveWebSessionHistory(webHistoryDir, containerName, history);
384
388
  }
385
389
 
390
+ function setWebAgentSessionPromptCommand(webHistoryDir, sessionRefOrContainerName, agentPromptCommand) {
391
+ const sessionRef = typeof sessionRefOrContainerName === 'string'
392
+ ? { containerName: sessionRefOrContainerName, agentId: WEB_DEFAULT_AGENT_ID }
393
+ : sessionRefOrContainerName;
394
+ if (sessionRef.agentId === WEB_DEFAULT_AGENT_ID) {
395
+ throw new Error('默认 AGENT 请直接修改容器级 agentPromptCommand');
396
+ }
397
+ const history = loadWebSessionHistory(webHistoryDir, sessionRef.containerName);
398
+ const agentSession = getWebAgentSession(history, sessionRef.agentId, { create: true });
399
+ agentSession.agentPromptCommand = normalizeAgentPromptCommandTemplate(
400
+ agentPromptCommand,
401
+ `agents.${sessionRef.agentId}.agentPromptCommand`
402
+ );
403
+ saveWebSessionHistory(webHistoryDir, sessionRef.containerName, history);
404
+ }
405
+
406
+ function deriveAgentPromptCommandFromDefaultCommand(defaultCommand) {
407
+ const normalizedCommand = String(defaultCommand || '').trim();
408
+ if (!normalizedCommand) {
409
+ return '';
410
+ }
411
+ try {
412
+ return normalizeAgentPromptCommandTemplate(
413
+ resolveAgentPromptCommandTemplate(normalizedCommand),
414
+ 'agentPromptCommand'
415
+ );
416
+ } catch (e) {
417
+ return '';
418
+ }
419
+ }
420
+
421
+ function resolveEffectiveSessionAgentPromptCommand(history, defaultCommand) {
422
+ return resolveEffectiveAgentPromptCommandForSession(history, WEB_DEFAULT_AGENT_ID, defaultCommand);
423
+ }
424
+
425
+ function resolveEffectiveAgentPromptCommandForSession(history, agentId, defaultCommand) {
426
+ const sessionHistory = history && typeof history === 'object' ? history : {};
427
+ const requestedAgentId = String(agentId || WEB_DEFAULT_AGENT_ID).trim() || WEB_DEFAULT_AGENT_ID;
428
+ const agentSession = getWebAgentSession(sessionHistory, requestedAgentId);
429
+ const agentTemplate = agentSession && typeof agentSession.agentPromptCommand === 'string'
430
+ ? normalizeAgentPromptCommandTemplate(agentSession.agentPromptCommand, `agents.${requestedAgentId}.agentPromptCommand`)
431
+ : '';
432
+ if (isAgentPromptCommandEnabled(agentTemplate)) {
433
+ return agentTemplate;
434
+ }
435
+ const historyTemplate = typeof sessionHistory.agentPromptCommand === 'string'
436
+ ? normalizeAgentPromptCommandTemplate(sessionHistory.agentPromptCommand, 'agentPromptCommand')
437
+ : '';
438
+ if (isAgentPromptCommandEnabled(historyTemplate)) {
439
+ return historyTemplate;
440
+ }
441
+ return deriveAgentPromptCommandFromDefaultCommand(defaultCommand);
442
+ }
443
+
444
+ function getEffectiveAgentPromptCommandSource(history, agentId, defaultCommand) {
445
+ const sessionHistory = history && typeof history === 'object' ? history : {};
446
+ const requestedAgentId = String(agentId || WEB_DEFAULT_AGENT_ID).trim() || WEB_DEFAULT_AGENT_ID;
447
+ const agentSession = getWebAgentSession(sessionHistory, requestedAgentId);
448
+ const agentTemplate = agentSession && typeof agentSession.agentPromptCommand === 'string'
449
+ ? normalizeAgentPromptCommandTemplate(agentSession.agentPromptCommand, `agents.${requestedAgentId}.agentPromptCommand`)
450
+ : '';
451
+ if (isAgentPromptCommandEnabled(agentTemplate)) {
452
+ return 'agent';
453
+ }
454
+ const historyTemplate = typeof sessionHistory.agentPromptCommand === 'string'
455
+ ? normalizeAgentPromptCommandTemplate(sessionHistory.agentPromptCommand, 'agentPromptCommand')
456
+ : '';
457
+ if (isAgentPromptCommandEnabled(historyTemplate)) {
458
+ return 'container';
459
+ }
460
+ return isAgentPromptCommandEnabled(deriveAgentPromptCommandFromDefaultCommand(defaultCommand))
461
+ ? 'inferred'
462
+ : 'none';
463
+ }
464
+
386
465
  function patchWebSessionHistory(webHistoryDir, containerName, patch) {
387
466
  const history = loadWebSessionHistory(webHistoryDir, containerName);
388
467
  if (!patch || typeof patch !== 'object') {
@@ -694,10 +773,9 @@ function extractAgentMessageFromStructuredOutput(agentProgram, text) {
694
773
  return '';
695
774
  }
696
775
 
697
- function getAgentRuntimeMeta(history) {
698
- const sessionHistory = history && typeof history === 'object' ? history : {};
699
- const template = normalizeAgentPromptCommandTemplate(sessionHistory.agentPromptCommand, 'agentPromptCommand');
700
- const agentProgram = resolveAgentProgram(template);
776
+ function getAgentRuntimeMeta(template) {
777
+ const normalizedTemplate = normalizeAgentPromptCommandTemplate(template, 'agentPromptCommand');
778
+ const agentProgram = resolveAgentProgram(normalizedTemplate);
701
779
  const resumeCommand = buildAgentResumeCommand(agentProgram);
702
780
  return {
703
781
  agentProgram: agentProgram || '',
@@ -1362,17 +1440,34 @@ function prepareCodexTraceEvent(payload) {
1362
1440
  async function prepareWebAgentExecution(ctx, state, sessionRef, prompt) {
1363
1441
  const history = loadWebSessionHistory(state.webHistoryDir, sessionRef.containerName);
1364
1442
  const agentSession = getWebAgentSession(history, sessionRef.agentId, { create: true });
1365
- const normalizedTemplate = normalizeAgentPromptCommandTemplate(history.agentPromptCommand, 'agentPromptCommand');
1366
- if (normalizedTemplate !== history.agentPromptCommand) {
1367
- history.agentPromptCommand = normalizedTemplate;
1443
+ const containerMap = listWebManyoyoContainers(ctx);
1444
+ const containerInfo = containerMap[sessionRef.containerName] || {};
1445
+ const normalizedContainerTemplate = normalizeAgentPromptCommandTemplate(history.agentPromptCommand, 'agentPromptCommand');
1446
+ if (normalizedContainerTemplate !== history.agentPromptCommand) {
1447
+ history.agentPromptCommand = normalizedContainerTemplate;
1368
1448
  saveWebSessionHistory(state.webHistoryDir, sessionRef.containerName, history);
1369
1449
  }
1370
- if (!isAgentPromptCommandEnabled(history.agentPromptCommand)) {
1450
+ if (agentSession && typeof agentSession.agentPromptCommand === 'string') {
1451
+ const normalizedAgentTemplate = normalizeAgentPromptCommandTemplate(
1452
+ agentSession.agentPromptCommand,
1453
+ `agents.${sessionRef.agentId}.agentPromptCommand`
1454
+ );
1455
+ if (normalizedAgentTemplate !== agentSession.agentPromptCommand) {
1456
+ agentSession.agentPromptCommand = normalizedAgentTemplate;
1457
+ saveWebSessionHistory(state.webHistoryDir, sessionRef.containerName, history);
1458
+ }
1459
+ }
1460
+ const effectiveTemplate = resolveEffectiveAgentPromptCommandForSession(
1461
+ history,
1462
+ sessionRef.agentId,
1463
+ containerInfo.defaultCommand
1464
+ );
1465
+ if (!isAgentPromptCommandEnabled(effectiveTemplate)) {
1371
1466
  throw new Error('当前会话未配置 agentPromptCommand');
1372
1467
  }
1373
1468
 
1374
1469
  await ensureWebContainer(ctx, state, sessionRef.containerName, sessionRef);
1375
- const agentMeta = getAgentRuntimeMeta(history);
1470
+ const agentMeta = getAgentRuntimeMeta(effectiveTemplate);
1376
1471
  const hasPriorConversation = hasAgentConversationHistory(agentSession);
1377
1472
  let resumeAttempted = false;
1378
1473
  let resumeSucceeded = false;
@@ -1391,7 +1486,7 @@ async function prepareWebAgentExecution(ctx, state, sessionRef, prompt) {
1391
1486
  const effectivePrompt = resumeSucceeded
1392
1487
  ? prompt
1393
1488
  : buildAgentPromptWithHistory(agentSession, prompt);
1394
- const command = buildWebAgentExecCommand(history.agentPromptCommand, effectivePrompt, agentMeta.agentProgram);
1489
+ const command = buildWebAgentExecCommand(effectiveTemplate, effectivePrompt, agentMeta.agentProgram);
1395
1490
  const contextMode = resumeSucceeded ? 'resume' : (hasPriorConversation ? 'history-injected' : 'first-turn');
1396
1491
 
1397
1492
  return {
@@ -2204,11 +2299,23 @@ function listWebManyoyoContainers(ctx) {
2204
2299
  if (!imageName.includes('manyoyo') && !name.startsWith('manyoyo-') && !name.startsWith('my-')) {
2205
2300
  return;
2206
2301
  }
2302
+ let defaultCommand = '';
2303
+ try {
2304
+ defaultCommand = String(
2305
+ ctx.dockerExecArgs(
2306
+ ['inspect', '-f', '{{index .Config.Labels "manyoyo.default_cmd"}}', name],
2307
+ { ignoreError: true }
2308
+ ) || ''
2309
+ ).trim();
2310
+ } catch (e) {
2311
+ defaultCommand = '';
2312
+ }
2207
2313
  map[name] = {
2208
2314
  name,
2209
2315
  status: status || 'unknown',
2210
2316
  image: imageName,
2211
- createdAt: estimateStartTimeFromStatus(status)
2317
+ createdAt: estimateStartTimeFromStatus(status),
2318
+ defaultCommand
2212
2319
  };
2213
2320
  });
2214
2321
 
@@ -2588,7 +2695,6 @@ function buildSessionSummary(ctx, state, containerMap, sessionRef) {
2588
2695
  const containerName = sessionRef && sessionRef.containerName ? sessionRef.containerName : '';
2589
2696
  const agentId = sessionRef && sessionRef.agentId ? sessionRef.agentId : WEB_DEFAULT_AGENT_ID;
2590
2697
  const history = loadWebSessionHistory(state.webHistoryDir, containerName);
2591
- const agentMeta = getAgentRuntimeMeta(history);
2592
2698
  const agentSession = getWebAgentSession(history, agentId)
2593
2699
  || (agentId === WEB_DEFAULT_AGENT_ID ? createEmptyWebAgentSession(WEB_DEFAULT_AGENT_ID) : null);
2594
2700
  if (!agentSession) {
@@ -2596,10 +2702,15 @@ function buildSessionSummary(ctx, state, containerMap, sessionRef) {
2596
2702
  }
2597
2703
  const latestMessage = agentSession.messages.length ? agentSession.messages[agentSession.messages.length - 1] : null;
2598
2704
  const containerInfo = containerMap[containerName] || {};
2705
+ const effectiveAgentPromptCommand = resolveEffectiveAgentPromptCommandForSession(history, agentId, containerInfo.defaultCommand);
2706
+ const agentMeta = getAgentRuntimeMeta(effectiveAgentPromptCommand);
2707
+ const effectiveAgentProgram = agentMeta.agentProgram || resolveAgentProgram(effectiveAgentPromptCommand);
2708
+ const effectiveResumeSupported = agentMeta.resumeSupported || Boolean(buildAgentResumeCommand(effectiveAgentProgram));
2599
2709
  const applied = history.applied && typeof history.applied === 'object' && !Array.isArray(history.applied)
2600
2710
  ? history.applied
2601
2711
  : buildSessionFallbackApplied(ctx, state, containerName, history, {
2602
- status: containerInfo.status || 'history'
2712
+ status: containerInfo.status || 'history',
2713
+ defaultCommand: containerInfo.defaultCommand || ''
2603
2714
  });
2604
2715
  const updatedAt = agentSession.updatedAt || history.updatedAt || (latestMessage && latestMessage.timestamp) || containerInfo.createdAt || null;
2605
2716
  return {
@@ -2611,9 +2722,9 @@ function buildSessionSummary(ctx, state, containerMap, sessionRef) {
2611
2722
  image: containerInfo.image || '',
2612
2723
  updatedAt,
2613
2724
  messageCount: agentSession.messages.length,
2614
- agentEnabled: isAgentPromptCommandEnabled(history.agentPromptCommand),
2615
- agentProgram: agentMeta.agentProgram,
2616
- resumeSupported: agentMeta.resumeSupported,
2725
+ agentEnabled: isAgentPromptCommandEnabled(effectiveAgentPromptCommand),
2726
+ agentProgram: effectiveAgentProgram || '',
2727
+ resumeSupported: effectiveResumeSupported,
2617
2728
  hostPath: applied.hostPath || '',
2618
2729
  containerPath: applied.containerPath || ''
2619
2730
  };
@@ -2622,14 +2733,18 @@ function buildSessionSummary(ctx, state, containerMap, sessionRef) {
2622
2733
  function buildSessionFallbackApplied(ctx, state, name, history, summary) {
2623
2734
  const snapshot = readWebConfigSnapshot(state.webConfigPath);
2624
2735
  const defaults = buildConfigDefaults(ctx, snapshot.parseError ? {} : snapshot.parsed);
2625
- const effectiveAgentPromptCommand = history.agentPromptCommand || defaults.agentPromptCommand || '';
2626
- const effectiveAgentProgram = resolveAgentProgram(effectiveAgentPromptCommand) || '';
2627
- const effectiveResumeSupported = Boolean(buildAgentResumeCommand(effectiveAgentProgram));
2628
- const defaultCommand = buildDefaultCommand(
2736
+ const configuredDefaultCommand = buildDefaultCommand(
2629
2737
  defaults.shellPrefix,
2630
2738
  defaults.shell,
2631
2739
  defaults.shellSuffix
2632
2740
  ) || buildStaticContainerRuntime(ctx, name).defaultCommand;
2741
+ const defaultCommand = pickFirstString(
2742
+ summary && summary.defaultCommand,
2743
+ configuredDefaultCommand
2744
+ );
2745
+ const effectiveAgentPromptCommand = resolveEffectiveSessionAgentPromptCommand(history, defaultCommand);
2746
+ const effectiveAgentProgram = resolveAgentProgram(effectiveAgentPromptCommand) || '';
2747
+ const effectiveResumeSupported = Boolean(buildAgentResumeCommand(effectiveAgentProgram));
2633
2748
 
2634
2749
  return {
2635
2750
  containerName: name,
@@ -2656,7 +2771,12 @@ function buildSessionFallbackApplied(ctx, state, name, history, summary) {
2656
2771
  function buildSessionDetail(ctx, state, containerMap, name) {
2657
2772
  const sessionRef = typeof name === 'string' ? parseWebSessionKey(name) : name;
2658
2773
  const history = loadWebSessionHistory(state.webHistoryDir, sessionRef.containerName);
2659
- const normalizedTemplate = normalizeAgentPromptCommandTemplate(history.agentPromptCommand, 'agentPromptCommand');
2774
+ const containerInfo = containerMap[sessionRef.containerName] || {};
2775
+ const normalizedTemplate = resolveEffectiveAgentPromptCommandForSession(
2776
+ history,
2777
+ sessionRef.agentId,
2778
+ containerInfo.defaultCommand
2779
+ );
2660
2780
  const summary = buildSessionSummary(ctx, state, containerMap, sessionRef);
2661
2781
  const agentSession = getWebAgentSession(history, sessionRef.agentId)
2662
2782
  || (sessionRef.agentId === WEB_DEFAULT_AGENT_ID ? createEmptyWebAgentSession(WEB_DEFAULT_AGENT_ID) : null);
@@ -2677,6 +2797,14 @@ function buildSessionDetail(ctx, state, containerMap, name) {
2677
2797
  latestRole: latestMessage && latestMessage.role ? String(latestMessage.role) : '',
2678
2798
  latestTimestamp: latestMessage && latestMessage.timestamp ? latestMessage.timestamp : summary.updatedAt,
2679
2799
  agentPromptCommand: normalizedTemplate || '',
2800
+ containerAgentPromptCommand: typeof history.agentPromptCommand === 'string'
2801
+ ? normalizeAgentPromptCommandTemplate(history.agentPromptCommand, 'agentPromptCommand')
2802
+ : '',
2803
+ agentPromptCommandOverride: agentSession && typeof agentSession.agentPromptCommand === 'string'
2804
+ ? normalizeAgentPromptCommandTemplate(agentSession.agentPromptCommand, `agents.${sessionRef.agentId}.agentPromptCommand`)
2805
+ : '',
2806
+ inferredAgentPromptCommand: deriveAgentPromptCommandFromDefaultCommand(containerInfo.defaultCommand),
2807
+ agentPromptSource: getEffectiveAgentPromptCommandSource(history, sessionRef.agentId, containerInfo.defaultCommand),
2680
2808
  agentProgram: summary.agentProgram || '',
2681
2809
  resumeSupported: summary.resumeSupported === true,
2682
2810
  lastResumeAt: agentSession.lastResumeAt || null,
@@ -3216,6 +3344,63 @@ async function handleWebApi(req, res, pathname, ctx, state) {
3216
3344
  sendJson(res, 200, { name: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId), detail });
3217
3345
  }
3218
3346
  },
3347
+ {
3348
+ method: 'PUT',
3349
+ match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/agent-template$/),
3350
+ handler: async match => {
3351
+ const sessionRef = getValidSessionRef(ctx, res, match[1]);
3352
+ if (!sessionRef) {
3353
+ return;
3354
+ }
3355
+
3356
+ let payload = null;
3357
+ try {
3358
+ payload = await readJsonBody(req);
3359
+ } catch (e) {
3360
+ sendJson(res, 400, { error: e.message || '请求参数错误' });
3361
+ return;
3362
+ }
3363
+ const normalizedPayload = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
3364
+ const hasContainerTemplate = hasOwn(normalizedPayload, 'containerAgentPromptCommand');
3365
+ const hasAgentOverride = hasOwn(normalizedPayload, 'agentPromptCommandOverride');
3366
+ if (!hasContainerTemplate && !hasAgentOverride) {
3367
+ sendJson(res, 400, { error: '至少提供一个模板字段' });
3368
+ return;
3369
+ }
3370
+ if (hasAgentOverride && sessionRef.agentId === WEB_DEFAULT_AGENT_ID) {
3371
+ sendJson(res, 400, { error: '默认 AGENT 不支持单独覆盖模板,请直接修改容器模板' });
3372
+ return;
3373
+ }
3374
+
3375
+ try {
3376
+ if (hasContainerTemplate) {
3377
+ setWebSessionAgentPromptCommand(
3378
+ state.webHistoryDir,
3379
+ sessionRef.containerName,
3380
+ normalizedPayload.containerAgentPromptCommand
3381
+ );
3382
+ }
3383
+ if (hasAgentOverride) {
3384
+ setWebAgentSessionPromptCommand(
3385
+ state.webHistoryDir,
3386
+ sessionRef,
3387
+ normalizedPayload.agentPromptCommandOverride
3388
+ );
3389
+ }
3390
+ } catch (e) {
3391
+ sendJson(res, 400, { error: e.message || '保存 Agent 模板失败' });
3392
+ return;
3393
+ }
3394
+
3395
+ const containerMap = listWebManyoyoContainers(ctx);
3396
+ const detail = buildSessionDetail(ctx, state, containerMap, sessionRef);
3397
+ sendJson(res, 200, {
3398
+ saved: true,
3399
+ name: buildWebSessionKey(sessionRef.containerName, sessionRef.agentId),
3400
+ detail
3401
+ });
3402
+ }
3403
+ },
3219
3404
  {
3220
3405
  method: 'POST',
3221
3406
  match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/run$/),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xcanwin/manyoyo",
3
- "version": "5.7.14",
3
+ "version": "5.8.0",
4
4
  "imageVersion": "1.9.0-common",
5
5
  "playwrightCliVersion": "0.1.1",
6
6
  "description": "AI Agent CLI Security Sandbox for Docker and Podman",