shennian 0.3.85 → 0.3.86
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/bin/shennian.js +1 -1
- package/dist/publish-build-manifest.json +28 -28
- package/dist/src/agents/adapter.js +19 -1
- package/dist/src/agents/claude-managed-permissions.js +248 -1
- package/dist/src/agents/claude.js +393 -8
- package/dist/src/agents/codex-managed-permissions.js +294 -1
- package/dist/src/agents/codex-managed-room-tools.js +249 -1
- package/dist/src/agents/codex-utils.js +200 -7
- package/dist/src/agents/codex.js +1352 -17
- package/dist/src/agents/config-status.js +226 -1
- package/dist/src/agents/cursor-managed-permissions.js +299 -2
- package/dist/src/agents/cursor.js +314 -1
- package/dist/src/agents/custom.js +418 -4
- package/dist/src/agents/external-channel-instructions.js +94 -11
- package/dist/src/agents/gemini.js +180 -1
- package/dist/src/agents/manager.d.ts +2 -0
- package/dist/src/agents/manager.js +250 -18
- package/dist/src/agents/model-registry/cache.js +37 -1
- package/dist/src/agents/model-registry/discovery.js +188 -2
- package/dist/src/agents/model-registry/parsers.js +521 -4
- package/dist/src/agents/model-registry/runner.js +30 -1
- package/dist/src/agents/model-registry/service.js +115 -1
- package/dist/src/agents/model-registry/types.js +8 -1
- package/dist/src/agents/model-registry.js +19 -1
- package/dist/src/agents/native-capabilities.js +237 -1
- package/dist/src/agents/openclaw.js +293 -2
- package/dist/src/agents/opencode-managed-permissions.js +187 -2
- package/dist/src/agents/opencode.js +298 -1
- package/dist/src/agents/platform-instructions.js +46 -17
- package/dist/src/agents/runtime-compatibility.js +44 -1
- package/dist/src/agents/workbuddy-managed-permissions.js +329 -2
- package/dist/src/agents/workbuddy.js +444 -3
- package/dist/src/commands/room.js +2 -2
- package/dist/src/fs/boundary.js +185 -1
- package/dist/src/fs/handler.js +130 -1
- package/dist/src/fs/security.js +32 -1
- package/dist/src/fs/text-decoder.js +110 -1
- package/dist/src/index.js +587 -3
- package/dist/src/manager/prompt.js +34 -29
- package/dist/src/manager/registry.js +317 -1
- package/dist/src/manager/retired-external-channel-compat.js +40 -1
- package/dist/src/manager/runtime.d.ts +4 -0
- package/dist/src/manager/runtime.js +1514 -23
- package/dist/src/native-fusion/codex-parser.js +660 -4
- package/dist/src/native-fusion/codex-user-input.js +102 -5
- package/dist/src/native-fusion/config.js +5 -1
- package/dist/src/native-fusion/file-watcher.js +191 -1
- package/dist/src/native-fusion/local-repository.js +569 -2
- package/dist/src/native-fusion/opencode-parser.js +130 -3
- package/dist/src/native-fusion/parsers.js +140 -3
- package/dist/src/native-fusion/service.js +739 -2
- package/dist/src/native-fusion/session-header.js +101 -2
- package/dist/src/native-fusion/state.js +104 -2
- package/dist/src/native-fusion/types.js +1 -1
- package/dist/src/personal-sync/machine-authority.js +34 -1
- package/dist/src/room/activation-audit.js +45 -2
- package/dist/src/room/activation-policy.js +30 -1
- package/dist/src/room/activation-service.d.ts +1 -0
- package/dist/src/room/activation-service.js +408 -8
- package/dist/src/room/invite-bootstrap.js +156 -1
- package/dist/src/room/managed-context.d.ts +1 -1
- package/dist/src/room/managed-context.js +1 -1
- package/dist/src/room/managed-prompt-composer.d.ts +1 -1
- package/dist/src/room/managed-prompt-composer.js +6 -5
- package/dist/src/room/managed-room-binding.js +167 -1
- package/dist/src/room/managed-room-ipc.d.ts +2 -2
- package/dist/src/room/managed-room-ipc.js +2 -2
- package/dist/src/room/mirror-sync-service.js +183 -1
- package/dist/src/room/room-action-client.d.ts +11 -0
- package/dist/src/room/room-action-client.js +1 -1
- package/dist/src/room/room-binding-client.d.ts +1 -0
- package/dist/src/room/room-binding-client.js +151 -1
- package/dist/src/security/managed-agent-policy.d.ts +3 -2
- package/dist/src/security/managed-agent-policy.js +1 -1
- package/dist/src/session/archive-zip.js +220 -1
- package/dist/src/session/deletion-sync-state.js +136 -2
- package/dist/src/session/handlers/agent-capabilities.js +14 -1
- package/dist/src/session/handlers/agent-config.js +150 -1
- package/dist/src/session/handlers/agents.js +55 -1
- package/dist/src/session/handlers/chat.d.ts +1 -1
- package/dist/src/session/handlers/chat.js +1030 -2
- package/dist/src/session/handlers/control.js +61 -1
- package/dist/src/session/handlers/fs.js +594 -1
- package/dist/src/session/handlers/message-status.js +15 -1
- package/dist/src/session/handlers/remote-access.js +43 -1
- package/dist/src/session/handlers/room-managed.js +101 -1
- package/dist/src/session/handlers/session-refresh.js +35 -1
- package/dist/src/session/handlers/title.js +79 -1
- package/dist/src/session/handlers/tool-detail.js +149 -1
- package/dist/src/session/handlers/voice-window.js +62 -1
- package/dist/src/session/history-subscriptions.js +470 -1
- package/dist/src/session/manager.js +658 -1
- package/dist/src/session/native-cleanup-outbox.js +287 -2
- package/dist/src/session/native-session-lifecycle.js +327 -6
- package/dist/src/session/queue.d.ts +5 -0
- package/dist/src/session/queue.js +8 -8
- package/dist/src/upgrade/scheduler.js +126 -1
- package/node_modules/@shennian/wire/dist/agent-workspace.js +1 -1
- package/package.json +1 -1
|
@@ -1 +1,180 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { AgentAdapter, registerAgent } from './adapter.js';
|
|
4
|
+
import { resolveBuiltinCommand, spawnResolvedCommand } from './command-spec.js';
|
|
5
|
+
import { buildAgentProcessEnv } from '../agent-env.js';
|
|
6
|
+
import { buildInlineChannelPreamble } from './platform-instructions.js';
|
|
7
|
+
function num(v) {
|
|
8
|
+
return typeof v === 'number' && Number.isFinite(v) ? v : undefined;
|
|
9
|
+
}
|
|
10
|
+
function usageFromStats(stats) {
|
|
11
|
+
if (!stats)
|
|
12
|
+
return undefined;
|
|
13
|
+
const input = num(stats.input_tokens) ??
|
|
14
|
+
num(stats.prompt_tokens) ??
|
|
15
|
+
num(stats.inputTokens) ??
|
|
16
|
+
num(stats.promptTokenCount);
|
|
17
|
+
const output = num(stats.output_tokens) ??
|
|
18
|
+
num(stats.completion_tokens) ??
|
|
19
|
+
num(stats.outputTokens) ??
|
|
20
|
+
num(stats.candidatesTokenCount) ??
|
|
21
|
+
num(stats.responseTokenCount);
|
|
22
|
+
if (input === undefined && output === undefined)
|
|
23
|
+
return undefined;
|
|
24
|
+
return {
|
|
25
|
+
inputTokens: input ?? 0,
|
|
26
|
+
outputTokens: output ?? 0,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export class GeminiAdapter extends AgentAdapter {
|
|
30
|
+
type = 'gemini';
|
|
31
|
+
process = null;
|
|
32
|
+
agentSessionId = null;
|
|
33
|
+
workDir = null;
|
|
34
|
+
seq = 0;
|
|
35
|
+
runId = '';
|
|
36
|
+
externalChannel = null;
|
|
37
|
+
shennianSessionId = null;
|
|
38
|
+
extraEnv = {};
|
|
39
|
+
configure(options) {
|
|
40
|
+
this.shennianSessionId = options.sessionId ?? null;
|
|
41
|
+
this.externalChannel = options.externalChannel ?? null;
|
|
42
|
+
this.extraEnv = options.env ?? {};
|
|
43
|
+
}
|
|
44
|
+
async start(_sessionId, workDir, agentSessionId) {
|
|
45
|
+
this.workDir = workDir;
|
|
46
|
+
this.seq = 0;
|
|
47
|
+
if (agentSessionId)
|
|
48
|
+
this.agentSessionId = agentSessionId;
|
|
49
|
+
}
|
|
50
|
+
async send(text, modelId, _reasoningEffort, _attachments, agentSystemPrompt) {
|
|
51
|
+
await this.killProcess();
|
|
52
|
+
this.runId = randomUUID();
|
|
53
|
+
this.seq = 0;
|
|
54
|
+
const outgoing = `${buildInlineChannelPreamble(this.workDir ?? process.cwd(), this.externalChannel, this.shennianSessionId ?? undefined, agentSystemPrompt)}${text}`;
|
|
55
|
+
const args = ['-p', outgoing, '--output-format', 'stream-json'];
|
|
56
|
+
if (modelId) {
|
|
57
|
+
args.push('-m', modelId);
|
|
58
|
+
}
|
|
59
|
+
if (this.agentSessionId) {
|
|
60
|
+
args.push('--resume', this.agentSessionId);
|
|
61
|
+
}
|
|
62
|
+
this.spawnAndParse(args);
|
|
63
|
+
}
|
|
64
|
+
async resume(agentSessionId) {
|
|
65
|
+
await this.killProcess();
|
|
66
|
+
this.agentSessionId = agentSessionId;
|
|
67
|
+
this.runId = randomUUID();
|
|
68
|
+
this.seq = 0;
|
|
69
|
+
this.spawnAndParse(['--resume', agentSessionId, '--output-format', 'stream-json']);
|
|
70
|
+
}
|
|
71
|
+
async stop() {
|
|
72
|
+
await this.killProcess();
|
|
73
|
+
}
|
|
74
|
+
spawnAndParse(args) {
|
|
75
|
+
const spec = resolveBuiltinCommand('gemini');
|
|
76
|
+
if (!spec) {
|
|
77
|
+
this.emit('error', new Error('Command "gemini" not found. Is Gemini CLI installed?'));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const proc = spawnResolvedCommand(spec, args, {
|
|
81
|
+
cwd: this.workDir ?? undefined,
|
|
82
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
83
|
+
env: buildAgentProcessEnv(this.extraEnv),
|
|
84
|
+
});
|
|
85
|
+
this.process = proc;
|
|
86
|
+
const rl = createInterface({ input: proc.stdout });
|
|
87
|
+
rl.on('line', (line) => {
|
|
88
|
+
if (!line.trim())
|
|
89
|
+
return;
|
|
90
|
+
try {
|
|
91
|
+
const obj = JSON.parse(line);
|
|
92
|
+
this.handleStreamEvent(obj);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
this.emitEvent({ state: 'delta', text: line });
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
let stderrBuf = '';
|
|
99
|
+
proc.stderr?.on('data', (chunk) => {
|
|
100
|
+
stderrBuf += chunk.toString();
|
|
101
|
+
});
|
|
102
|
+
proc.on('close', (code) => {
|
|
103
|
+
if (this.process !== proc)
|
|
104
|
+
return;
|
|
105
|
+
this.process = null;
|
|
106
|
+
if (code !== 0 && code !== null) {
|
|
107
|
+
const msg = stderrBuf.trim() || `gemini exited with code ${code}`;
|
|
108
|
+
this.emitEvent({ state: 'error', message: msg });
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
proc.on('error', (err) => {
|
|
112
|
+
if (this.process !== proc)
|
|
113
|
+
return;
|
|
114
|
+
this.process = null;
|
|
115
|
+
if (err.code === 'ENOENT') {
|
|
116
|
+
this.emit('error', new Error('Command "gemini" not found. Is Gemini CLI installed?'));
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
this.emit('error', err);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
handleStreamEvent(obj) {
|
|
124
|
+
switch (obj.type) {
|
|
125
|
+
case 'init': {
|
|
126
|
+
if (obj.session_id) {
|
|
127
|
+
this.agentSessionId = obj.session_id;
|
|
128
|
+
}
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case 'message': {
|
|
132
|
+
if (obj.role === 'assistant' && obj.content) {
|
|
133
|
+
this.emitEvent({ state: 'delta', text: obj.content });
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case 'result': {
|
|
138
|
+
if (obj.status === 'success') {
|
|
139
|
+
this.emitEvent({
|
|
140
|
+
state: 'final',
|
|
141
|
+
agentSessionId: this.agentSessionId ?? undefined,
|
|
142
|
+
usage: usageFromStats(obj.stats),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else if (obj.status === 'error') {
|
|
146
|
+
const msg = (typeof obj.error === 'string' && obj.error) ||
|
|
147
|
+
(typeof obj.message === 'string' && obj.message) ||
|
|
148
|
+
'gemini result error';
|
|
149
|
+
this.emitEvent({ state: 'error', message: msg });
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
default:
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
emitEvent(partial) {
|
|
158
|
+
const event = {
|
|
159
|
+
...partial,
|
|
160
|
+
runId: this.runId,
|
|
161
|
+
seq: this.seq++,
|
|
162
|
+
};
|
|
163
|
+
this.emit('agentEvent', event);
|
|
164
|
+
}
|
|
165
|
+
async killProcess() {
|
|
166
|
+
const proc = this.process;
|
|
167
|
+
if (!proc)
|
|
168
|
+
return;
|
|
169
|
+
this.process = null;
|
|
170
|
+
proc.kill('SIGTERM');
|
|
171
|
+
await new Promise((resolve) => {
|
|
172
|
+
proc.on('close', resolve);
|
|
173
|
+
setTimeout(() => {
|
|
174
|
+
proc.kill('SIGKILL');
|
|
175
|
+
resolve();
|
|
176
|
+
}, 3000);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
registerAgent('gemini', () => new GeminiAdapter());
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AgentSendContext, AgentSendResult } from './adapter.js';
|
|
2
2
|
import { AgentAdapter } from './adapter.js';
|
|
3
3
|
import type { ChatAttachmentMeta, ExternalChannelSessionStatus } from '@shennian/wire';
|
|
4
|
+
import type { CompiledManagedAgentPolicy } from '../security/managed-agent-policy.js';
|
|
4
5
|
type ManagerModelId = 'codex' | 'claude';
|
|
5
6
|
/** Decorates a real provider Adapter with Manager session capabilities. New
|
|
6
7
|
* sessions use this class and keep their real agentType/modelId. */
|
|
@@ -17,6 +18,7 @@ export declare class ManagerModeAdapter extends AgentAdapter {
|
|
|
17
18
|
sessionId?: string;
|
|
18
19
|
externalChannel?: ExternalChannelSessionStatus | null;
|
|
19
20
|
env?: NodeJS.ProcessEnv;
|
|
21
|
+
managedAgentPolicy?: CompiledManagedAgentPolicy | null;
|
|
20
22
|
}): void;
|
|
21
23
|
start(sessionId: string, workDir: string, agentSessionId?: string | null): Promise<void>;
|
|
22
24
|
send(text: string, modelId?: string, reasoningEffort?: string, attachments?: ChatAttachmentMeta[], agentSystemPrompt?: string | null, context?: AgentSendContext): Promise<void | AgentSendResult>;
|
|
@@ -1,18 +1,250 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
// @arch docs/features/manager-agent.md
|
|
2
|
+
// @test src/__tests__/manager-adapter.test.ts
|
|
3
|
+
import { AgentAdapter, registerAgent } from './adapter.js';
|
|
4
|
+
import { CodexAdapter } from './codex.js';
|
|
5
|
+
import { ClaudeAdapter } from './claude.js';
|
|
6
|
+
import { MANAGER_SYSTEM_PROMPT, buildManagerPrompt } from '../manager/prompt.js';
|
|
7
|
+
import { getManagerRuntimeService } from '../manager/runtime.js';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { PLATFORM_OUTPUT_INSTRUCTIONS } from './platform-instructions.js';
|
|
11
|
+
function normalizeManagerModel(modelId) {
|
|
12
|
+
return modelId === 'claude' ? 'claude' : 'codex';
|
|
13
|
+
}
|
|
14
|
+
function safeSessionDirName(sessionId) {
|
|
15
|
+
return sessionId.replace(/[^a-zA-Z0-9_.-]/g, '_');
|
|
16
|
+
}
|
|
17
|
+
function readProjectAgentsMd(workDir) {
|
|
18
|
+
const agentsPath = path.join(workDir, 'AGENTS.md');
|
|
19
|
+
try {
|
|
20
|
+
return fs.readFileSync(agentsPath, 'utf8').trim();
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return '';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function managerInstructionsPath(workDir, sessionId) {
|
|
27
|
+
return path.join(workDir, '.shennian', 'runtime', 'manager', safeSessionDirName(sessionId), 'instructions.md');
|
|
28
|
+
}
|
|
29
|
+
function buildStableManagerInstructions(workDir, managerSessionId) {
|
|
30
|
+
const projectInstructions = readProjectAgentsMd(workDir);
|
|
31
|
+
const managerRuntime = getManagerRuntimeService();
|
|
32
|
+
const channelInstructions = managerRuntime?.getManagerExternalChannelSystemPrompt(managerSessionId) ?? '';
|
|
33
|
+
const workerDefaults = typeof managerRuntime?.getManagerWorkerDefaults === 'function'
|
|
34
|
+
? managerRuntime.getManagerWorkerDefaults(managerSessionId)
|
|
35
|
+
: undefined;
|
|
36
|
+
const workerDefaultInstructions = workerDefaults?.agentType
|
|
37
|
+
? `默认 worker Agent:${workerDefaults.agentType}${workerDefaults.modelId ? `\n默认 worker 模型:${workerDefaults.modelId}` : ''}。除非用户明确要求或任务明显需要其他 Agent/模型,创建 worker 时优先使用这个默认组合。`
|
|
38
|
+
: '';
|
|
39
|
+
const sections = [
|
|
40
|
+
'# Shennian Manager Instructions',
|
|
41
|
+
'This file is generated by Shennian for a Manager Agent substrate session. Do not edit it by hand.',
|
|
42
|
+
projectInstructions
|
|
43
|
+
? `## Project Instructions\n\n${projectInstructions}`
|
|
44
|
+
: '',
|
|
45
|
+
`## Manager Instructions\n\n${MANAGER_SYSTEM_PROMPT}`,
|
|
46
|
+
workerDefaultInstructions
|
|
47
|
+
? `## Worker Defaults\n\n${workerDefaultInstructions}`
|
|
48
|
+
: '',
|
|
49
|
+
channelInstructions
|
|
50
|
+
? `## External Message Channel Instructions\n\n${channelInstructions}`
|
|
51
|
+
: '',
|
|
52
|
+
PLATFORM_OUTPUT_INSTRUCTIONS,
|
|
53
|
+
].filter(Boolean);
|
|
54
|
+
return `${sections.join('\n\n')}\n`;
|
|
55
|
+
}
|
|
56
|
+
function ensureManagerInstructionsFile(workDir, sessionId) {
|
|
57
|
+
const filePath = managerInstructionsPath(workDir, sessionId);
|
|
58
|
+
const content = buildStableManagerInstructions(workDir, sessionId);
|
|
59
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
60
|
+
try {
|
|
61
|
+
if (fs.readFileSync(filePath, 'utf8') === content)
|
|
62
|
+
return filePath;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// Write the generated instruction file below.
|
|
66
|
+
}
|
|
67
|
+
fs.writeFileSync(filePath, content);
|
|
68
|
+
return filePath;
|
|
69
|
+
}
|
|
70
|
+
/** Decorates a real provider Adapter with Manager session capabilities. New
|
|
71
|
+
* sessions use this class and keep their real agentType/modelId. */
|
|
72
|
+
export class ManagerModeAdapter extends AgentAdapter {
|
|
73
|
+
inner;
|
|
74
|
+
substrate;
|
|
75
|
+
type;
|
|
76
|
+
sessionId = '';
|
|
77
|
+
workDir = '';
|
|
78
|
+
agentSessionId = null;
|
|
79
|
+
configured = {};
|
|
80
|
+
constructor(inner, substrate) {
|
|
81
|
+
super();
|
|
82
|
+
this.inner = inner;
|
|
83
|
+
this.substrate = substrate;
|
|
84
|
+
this.type = inner.type;
|
|
85
|
+
inner.on('agentEvent', (event) => {
|
|
86
|
+
if (event.agentSessionId)
|
|
87
|
+
this.agentSessionId = event.agentSessionId;
|
|
88
|
+
this.emit('agentEvent', { ...event, source: event.source ?? substrate });
|
|
89
|
+
});
|
|
90
|
+
inner.on('error', (error) => this.emit('error', error));
|
|
91
|
+
}
|
|
92
|
+
configure(options) {
|
|
93
|
+
this.configured = options;
|
|
94
|
+
if (options.sessionId) {
|
|
95
|
+
getManagerRuntimeService()?.setManagedAgentPolicy(options.sessionId, options.managedAgentPolicy ?? null);
|
|
96
|
+
}
|
|
97
|
+
this.applyEnvironment();
|
|
98
|
+
}
|
|
99
|
+
async start(sessionId, workDir, agentSessionId) {
|
|
100
|
+
this.sessionId = sessionId;
|
|
101
|
+
this.workDir = workDir;
|
|
102
|
+
this.agentSessionId = agentSessionId ?? null;
|
|
103
|
+
this.applyEnvironment();
|
|
104
|
+
await this.inner.start(sessionId, workDir, agentSessionId);
|
|
105
|
+
getManagerRuntimeService()?.registerManager({
|
|
106
|
+
sessionId, agentSessionId: this.agentSessionId, workDir, modelId: this.substrate,
|
|
107
|
+
agentType: this.type === 'claude' ? 'claude' : 'codex', status: 'idle',
|
|
108
|
+
});
|
|
109
|
+
getManagerRuntimeService()?.bindManagerAdapterEvents(sessionId, this.inner);
|
|
110
|
+
}
|
|
111
|
+
async send(text, modelId, reasoningEffort, attachments, agentSystemPrompt, context) {
|
|
112
|
+
getManagerRuntimeService()?.registerManager({
|
|
113
|
+
sessionId: this.sessionId, agentSessionId: this.agentSessionId, workDir: this.workDir,
|
|
114
|
+
modelId: this.substrate, agentType: this.type === 'claude' ? 'claude' : 'codex',
|
|
115
|
+
providerModelId: modelId ?? null, status: 'running',
|
|
116
|
+
});
|
|
117
|
+
await getManagerRuntimeService()?.ready();
|
|
118
|
+
this.applyEnvironment();
|
|
119
|
+
const managerInstructions = buildStableManagerInstructions(this.workDir, this.sessionId);
|
|
120
|
+
const combined = combineManagerInstructions(managerInstructions, agentSystemPrompt);
|
|
121
|
+
return this.inner.send(buildManagerPrompt(text), modelId, reasoningEffort, attachments, combined, context);
|
|
122
|
+
}
|
|
123
|
+
async resume(agentSessionId) {
|
|
124
|
+
this.agentSessionId = agentSessionId;
|
|
125
|
+
await this.inner.resume(agentSessionId);
|
|
126
|
+
}
|
|
127
|
+
async stop() {
|
|
128
|
+
getManagerRuntimeService()?.setManagedAgentPolicy(this.sessionId, null);
|
|
129
|
+
await this.inner.stop();
|
|
130
|
+
}
|
|
131
|
+
getStatus() { return this.inner.getStatus?.() ?? Promise.resolve({ active: false }); }
|
|
132
|
+
setTitle(agentSessionId, title, workDir) {
|
|
133
|
+
return this.inner.setTitle?.(agentSessionId, title, workDir) ?? Promise.resolve();
|
|
134
|
+
}
|
|
135
|
+
applyEnvironment() {
|
|
136
|
+
const managerEnv = this.sessionId
|
|
137
|
+
? getManagerRuntimeService()?.getInjectedEnv(this.sessionId, this.agentSessionId, this.workDir, this.substrate) ?? {}
|
|
138
|
+
: {};
|
|
139
|
+
this.inner.configure?.({
|
|
140
|
+
...this.configured,
|
|
141
|
+
sessionId: this.configured.sessionId ?? this.sessionId,
|
|
142
|
+
env: { ...(this.configured.env ?? {}), ...managerEnv },
|
|
143
|
+
managedAgentPolicy: this.configured.managedAgentPolicy ?? null,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export function combineManagerInstructions(managerInstructions, systemPrompt) {
|
|
148
|
+
const prompt = systemPrompt?.trim() ?? '';
|
|
149
|
+
if (!prompt)
|
|
150
|
+
return managerInstructions;
|
|
151
|
+
const roomMarkers = ['[智能体私有系统提示词]', '[智能体身份]'];
|
|
152
|
+
const index = roomMarkers
|
|
153
|
+
.map((marker) => prompt.indexOf(marker))
|
|
154
|
+
.filter((position) => position >= 0)
|
|
155
|
+
.sort((a, b) => a - b)[0] ?? -1;
|
|
156
|
+
if (prompt.startsWith('[神念受管会话规则]') && index >= 0) {
|
|
157
|
+
return `${prompt.slice(0, index)}[会话模式]\n${managerInstructions.trim()}\n\n${prompt.slice(index)}`;
|
|
158
|
+
}
|
|
159
|
+
return `${managerInstructions.trim()}\n\n${prompt}`;
|
|
160
|
+
}
|
|
161
|
+
export class ManagerAdapter extends AgentAdapter {
|
|
162
|
+
type = 'manager';
|
|
163
|
+
inner = null;
|
|
164
|
+
sessionId = '';
|
|
165
|
+
workDir = '';
|
|
166
|
+
agentSessionId = null;
|
|
167
|
+
modelId = 'codex';
|
|
168
|
+
async start(sessionId, workDir, agentSessionId) {
|
|
169
|
+
this.sessionId = sessionId;
|
|
170
|
+
this.workDir = workDir;
|
|
171
|
+
this.agentSessionId = agentSessionId ?? null;
|
|
172
|
+
getManagerRuntimeService()?.registerManager({
|
|
173
|
+
sessionId,
|
|
174
|
+
agentSessionId: this.agentSessionId,
|
|
175
|
+
workDir,
|
|
176
|
+
modelId: this.modelId,
|
|
177
|
+
agentType: 'manager',
|
|
178
|
+
status: 'idle',
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
async send(text, modelId) {
|
|
182
|
+
const nextModel = normalizeManagerModel(modelId);
|
|
183
|
+
if (!this.inner || nextModel !== this.modelId) {
|
|
184
|
+
await this.inner?.stop().catch(() => { });
|
|
185
|
+
this.inner?.removeAllListeners();
|
|
186
|
+
this.modelId = nextModel;
|
|
187
|
+
const instructionsFile = ensureManagerInstructionsFile(this.workDir, this.sessionId);
|
|
188
|
+
const systemPrompt = buildStableManagerInstructions(this.workDir, this.sessionId);
|
|
189
|
+
this.inner = nextModel === 'claude'
|
|
190
|
+
? new ClaudeAdapter({ systemPrompt, hidden: true })
|
|
191
|
+
: new CodexAdapter({ modelInstructionsFile: instructionsFile, hidden: true });
|
|
192
|
+
this.bindInner(this.inner);
|
|
193
|
+
await this.inner.start(this.sessionId, this.workDir, this.agentSessionId);
|
|
194
|
+
getManagerRuntimeService()?.bindManagerAdapterEvents(this.sessionId, this.inner);
|
|
195
|
+
}
|
|
196
|
+
getManagerRuntimeService()?.registerManager({
|
|
197
|
+
sessionId: this.sessionId,
|
|
198
|
+
agentSessionId: this.agentSessionId,
|
|
199
|
+
workDir: this.workDir,
|
|
200
|
+
modelId: this.modelId,
|
|
201
|
+
agentType: 'manager',
|
|
202
|
+
status: 'running',
|
|
203
|
+
});
|
|
204
|
+
await getManagerRuntimeService()?.ready();
|
|
205
|
+
await this.withManagerEnv(() => this.inner.send(buildManagerPrompt(text)));
|
|
206
|
+
}
|
|
207
|
+
async resume(agentSessionId) {
|
|
208
|
+
this.agentSessionId = agentSessionId;
|
|
209
|
+
if (this.inner)
|
|
210
|
+
await this.inner.resume(agentSessionId);
|
|
211
|
+
}
|
|
212
|
+
async stop() {
|
|
213
|
+
await this.inner?.stop();
|
|
214
|
+
}
|
|
215
|
+
bindInner(inner) {
|
|
216
|
+
inner.on('agentEvent', (event) => {
|
|
217
|
+
if (event.agentSessionId)
|
|
218
|
+
this.agentSessionId = event.agentSessionId;
|
|
219
|
+
this.emit('agentEvent', this.wrapEvent(event));
|
|
220
|
+
});
|
|
221
|
+
inner.on('error', (error) => this.emit('error', error));
|
|
222
|
+
}
|
|
223
|
+
wrapEvent(event) {
|
|
224
|
+
return {
|
|
225
|
+
...event,
|
|
226
|
+
source: event.source ?? this.modelId,
|
|
227
|
+
agentSessionId: event.agentSessionId ?? this.agentSessionId ?? undefined,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
async withManagerEnv(work) {
|
|
231
|
+
const env = getManagerRuntimeService()?.getInjectedEnv(this.sessionId, this.agentSessionId, this.workDir, this.modelId) ?? {};
|
|
232
|
+
const previous = new Map();
|
|
233
|
+
for (const [key, value] of Object.entries(env)) {
|
|
234
|
+
previous.set(key, process.env[key]);
|
|
235
|
+
process.env[key] = value;
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
return await work();
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
for (const [key, value] of previous) {
|
|
242
|
+
if (value === undefined)
|
|
243
|
+
delete process.env[key];
|
|
244
|
+
else
|
|
245
|
+
process.env[key] = value;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
registerAgent('manager', () => new ManagerAdapter());
|
|
@@ -1 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
// @arch docs/architecture/cli/model-discovery.md
|
|
2
|
+
// @test src/__tests__/model-switching.test.ts
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { CACHE_TTL_MS, CACHE_VERSION, MODEL_CACHE_PATH } from './types.js';
|
|
6
|
+
function ensureDirExists() {
|
|
7
|
+
fs.mkdirSync(path.dirname(MODEL_CACHE_PATH), { recursive: true });
|
|
8
|
+
}
|
|
9
|
+
export function readCache() {
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(fs.readFileSync(MODEL_CACHE_PATH, 'utf-8'));
|
|
12
|
+
if (parsed.version === CACHE_VERSION && parsed.agents && typeof parsed.agents === 'object') {
|
|
13
|
+
return parsed;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
// fall through
|
|
18
|
+
}
|
|
19
|
+
return { version: CACHE_VERSION, agents: {} };
|
|
20
|
+
}
|
|
21
|
+
export function writeCache(cache) {
|
|
22
|
+
try {
|
|
23
|
+
ensureDirExists();
|
|
24
|
+
fs.writeFileSync(MODEL_CACHE_PATH, JSON.stringify(cache, null, 2));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// Cache is best-effort. Discovery should still succeed when local
|
|
28
|
+
// filesystem writes are unavailable (e.g. restricted sandboxes).
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function isCacheFresh(entry, version) {
|
|
32
|
+
if (!entry)
|
|
33
|
+
return false;
|
|
34
|
+
if (entry.version !== version)
|
|
35
|
+
return false;
|
|
36
|
+
return Date.now() - entry.refreshedAt < CACHE_TTL_MS;
|
|
37
|
+
}
|