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,314 @@
|
|
|
1
|
-
|
|
1
|
+
// @arch docs/architecture/cli/agent-adapters.md
|
|
2
|
+
// @test src/__tests__/cursor-adapter.test.ts
|
|
3
|
+
import { createInterface } from 'node:readline';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { AgentAdapter, registerAgent } from './adapter.js';
|
|
6
|
+
import { resolveBuiltinCommand, spawnResolvedCommand } from './command-spec.js';
|
|
7
|
+
import { buildAgentProcessEnv, buildManagedAgentProcessEnv } from '../agent-env.js';
|
|
8
|
+
import { buildInlineChannelPreamble } from './platform-instructions.js';
|
|
9
|
+
import { resolveManagedAgentAdapterEnforcement, } from '../security/managed-agent-policy.js';
|
|
10
|
+
import { registerManagedAgentRuntimeSecurity } from '../security/managed-agent-runtime.js';
|
|
11
|
+
import { compileCursorManagedPermissions, createCursorManagedConfigDirectory, CURSOR_MANAGED_RUNTIME_SECURITY, disposeCursorManagedConfigDirectory, } from './cursor-managed-permissions.js';
|
|
12
|
+
registerManagedAgentRuntimeSecurity('cursor', CURSOR_MANAGED_RUNTIME_SECURITY);
|
|
13
|
+
export class CursorAdapter extends AgentAdapter {
|
|
14
|
+
type = 'cursor';
|
|
15
|
+
process = null;
|
|
16
|
+
chatId = null;
|
|
17
|
+
workDir = null;
|
|
18
|
+
seq = 0;
|
|
19
|
+
runId = '';
|
|
20
|
+
emittedText = '';
|
|
21
|
+
externalChannel = null;
|
|
22
|
+
shennianSessionId = null;
|
|
23
|
+
extraEnv = {};
|
|
24
|
+
managedAgentPolicy = null;
|
|
25
|
+
managedConfigDir = null;
|
|
26
|
+
configure(options) {
|
|
27
|
+
this.shennianSessionId = options.sessionId ?? null;
|
|
28
|
+
this.externalChannel = options.externalChannel ?? null;
|
|
29
|
+
this.extraEnv = options.env ?? {};
|
|
30
|
+
if ('managedAgentPolicy' in options) {
|
|
31
|
+
this.managedAgentPolicy = options.managedAgentPolicy ?? null;
|
|
32
|
+
}
|
|
33
|
+
if (this.managedAgentPolicy)
|
|
34
|
+
this.workDir = this.managedAgentPolicy.adapterPolicy.workDir;
|
|
35
|
+
}
|
|
36
|
+
async start(_sessionId, workDir, agentSessionId) {
|
|
37
|
+
this.workDir = this.managedAgentPolicy?.adapterPolicy.workDir ?? workDir;
|
|
38
|
+
this.seq = 0;
|
|
39
|
+
if (agentSessionId)
|
|
40
|
+
this.chatId = agentSessionId;
|
|
41
|
+
}
|
|
42
|
+
async send(text, modelId, _reasoningEffort, _attachments, agentSystemPrompt, context) {
|
|
43
|
+
if (context?.source === 'room_activation' && !this.managedAgentPolicy) {
|
|
44
|
+
throw new Error('managed_agent_policy_missing');
|
|
45
|
+
}
|
|
46
|
+
await this.killProcess();
|
|
47
|
+
this.runId = randomUUID();
|
|
48
|
+
this.resetRunState();
|
|
49
|
+
const outgoing = `${buildInlineChannelPreamble(this.workDir ?? process.cwd(), this.externalChannel, this.shennianSessionId ?? undefined, agentSystemPrompt)}${text}`;
|
|
50
|
+
const args = ['-p', outgoing, '--output-format', 'stream-json', '--stream-partial-output'];
|
|
51
|
+
if (modelId) {
|
|
52
|
+
args.push('--model', modelId);
|
|
53
|
+
}
|
|
54
|
+
if (this.chatId) {
|
|
55
|
+
args.push('--continue');
|
|
56
|
+
}
|
|
57
|
+
await this.spawnAndParse(args);
|
|
58
|
+
}
|
|
59
|
+
async resume(agentSessionId) {
|
|
60
|
+
await this.killProcess();
|
|
61
|
+
this.chatId = agentSessionId;
|
|
62
|
+
this.runId = randomUUID();
|
|
63
|
+
this.resetRunState();
|
|
64
|
+
await this.spawnAndParse([
|
|
65
|
+
'--resume',
|
|
66
|
+
agentSessionId,
|
|
67
|
+
'--output-format',
|
|
68
|
+
'stream-json',
|
|
69
|
+
'--stream-partial-output',
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
async stop() {
|
|
73
|
+
await this.killProcess();
|
|
74
|
+
}
|
|
75
|
+
async spawnAndParse(args) {
|
|
76
|
+
const spec = resolveBuiltinCommand('cursor');
|
|
77
|
+
if (!spec) {
|
|
78
|
+
this.emit('error', new Error('Cursor Agent CLI not found. Expected "agent" or "cursor agent".'));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const managedLaunch = await this.prepareManagedLaunch();
|
|
82
|
+
let proc;
|
|
83
|
+
try {
|
|
84
|
+
proc = spawnResolvedCommand(spec, [...args, ...managedLaunch.args], {
|
|
85
|
+
cwd: this.workDir ?? undefined,
|
|
86
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
87
|
+
env: managedLaunch.env,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
await this.disposeManagedConfig(managedLaunch.configDir);
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
this.process = proc;
|
|
95
|
+
this.managedConfigDir = managedLaunch.configDir;
|
|
96
|
+
const rl = createInterface({ input: proc.stdout });
|
|
97
|
+
rl.on('line', (line) => {
|
|
98
|
+
if (!line.trim())
|
|
99
|
+
return;
|
|
100
|
+
try {
|
|
101
|
+
const obj = JSON.parse(line);
|
|
102
|
+
this.handleStreamEvent(obj);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// 非 JSON 行当作纯文本 delta
|
|
106
|
+
this.emitEvent({ state: 'delta', text: line });
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
let stderrBuf = '';
|
|
110
|
+
proc.stderr?.on('data', (chunk) => {
|
|
111
|
+
stderrBuf += chunk.toString();
|
|
112
|
+
});
|
|
113
|
+
proc.on('close', (code) => {
|
|
114
|
+
if (this.process !== proc)
|
|
115
|
+
return;
|
|
116
|
+
this.process = null;
|
|
117
|
+
void this.disposeManagedConfig(managedLaunch.configDir);
|
|
118
|
+
if (code !== 0 && code !== null) {
|
|
119
|
+
const msg = stderrBuf.trim() || `cursor agent exited with code ${code}`;
|
|
120
|
+
this.emitEvent({ state: 'error', message: msg });
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
proc.on('error', (err) => {
|
|
124
|
+
if (this.process !== proc)
|
|
125
|
+
return;
|
|
126
|
+
this.process = null;
|
|
127
|
+
void this.disposeManagedConfig(managedLaunch.configDir);
|
|
128
|
+
if (err.code === 'ENOENT') {
|
|
129
|
+
this.emit('error', new Error('Command "cursor" not found. Is Cursor CLI installed?'));
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
this.emit('error', err);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
async prepareManagedLaunch() {
|
|
137
|
+
const compiled = this.managedAgentPolicy;
|
|
138
|
+
if (!compiled) {
|
|
139
|
+
return {
|
|
140
|
+
args: ['--trust'],
|
|
141
|
+
env: buildAgentProcessEnv(this.extraEnv),
|
|
142
|
+
configDir: null,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const nativeConfig = compileCursorManagedPermissions(resolveManagedAgentAdapterEnforcement(compiled), compiled.adapterPolicy.workDir);
|
|
146
|
+
const configDir = await createCursorManagedConfigDirectory(nativeConfig);
|
|
147
|
+
const env = buildManagedAgentProcessEnv({
|
|
148
|
+
agentType: 'cursor',
|
|
149
|
+
providerEnv: this.extraEnv,
|
|
150
|
+
sessionEnv: this.extraEnv,
|
|
151
|
+
runtimeEnv: { NO_COLOR: '1' },
|
|
152
|
+
});
|
|
153
|
+
// CURSOR_CONFIG_DIR contains only Shennian-generated, non-secret policy.
|
|
154
|
+
// It is assigned after allowlist filtering so Cursor cannot fall back to
|
|
155
|
+
// the user's mutable global CLI configuration.
|
|
156
|
+
env.CURSOR_CONFIG_DIR = configDir;
|
|
157
|
+
return { args: nativeConfig.args, env, configDir };
|
|
158
|
+
}
|
|
159
|
+
async disposeManagedConfig(configDir) {
|
|
160
|
+
if (!configDir)
|
|
161
|
+
return;
|
|
162
|
+
if (this.managedConfigDir === configDir)
|
|
163
|
+
this.managedConfigDir = null;
|
|
164
|
+
await disposeCursorManagedConfigDirectory(configDir).catch(() => { });
|
|
165
|
+
}
|
|
166
|
+
handleStreamEvent(obj) {
|
|
167
|
+
if (obj.session_id && !this.chatId) {
|
|
168
|
+
this.chatId = obj.session_id;
|
|
169
|
+
}
|
|
170
|
+
if (obj.chat_id && !this.chatId) {
|
|
171
|
+
this.chatId = obj.chat_id;
|
|
172
|
+
}
|
|
173
|
+
const eventType = obj.type ?? obj.event;
|
|
174
|
+
switch (eventType) {
|
|
175
|
+
case 'system': {
|
|
176
|
+
// init event — session_id already captured above
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
case 'assistant': {
|
|
180
|
+
// Cursor's --stream-partial-output currently emits duplicate buffered
|
|
181
|
+
// flushes. Only timestamped assistant events without model_call_id are
|
|
182
|
+
// safe to treat as live deltas; use result.result as the terminal
|
|
183
|
+
// canonical text.
|
|
184
|
+
if (obj.message?.content) {
|
|
185
|
+
if (!this.isLiveAssistantDelta(obj))
|
|
186
|
+
break;
|
|
187
|
+
for (const block of obj.message.content) {
|
|
188
|
+
if (block.type === 'text' && block.text) {
|
|
189
|
+
this.emitTextDelta(block.text);
|
|
190
|
+
}
|
|
191
|
+
else if (block.type === 'thinking' && block.thinking) {
|
|
192
|
+
this.emitEvent({ state: 'delta', text: block.thinking, thinking: true });
|
|
193
|
+
}
|
|
194
|
+
else if (block.type === 'tool_use') {
|
|
195
|
+
this.emitEvent({ state: 'tool-call', name: block.name, args: block.input });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
// Legacy flat
|
|
201
|
+
if (obj.delta ?? obj.text) {
|
|
202
|
+
this.emitEvent({ state: 'delta', text: (obj.delta ?? obj.text) });
|
|
203
|
+
}
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
case 'user':
|
|
207
|
+
break; // echo of user message
|
|
208
|
+
case 'result': {
|
|
209
|
+
if (obj.subtype === 'success' || !obj.is_error) {
|
|
210
|
+
this.emitFinalResultDelta(obj.result);
|
|
211
|
+
const u = obj.usage;
|
|
212
|
+
this.emitEvent({
|
|
213
|
+
state: 'final',
|
|
214
|
+
agentSessionId: this.chatId ?? undefined,
|
|
215
|
+
usage: u
|
|
216
|
+
? {
|
|
217
|
+
inputTokens: u.inputTokens ?? u.input_tokens ?? 0,
|
|
218
|
+
outputTokens: u.outputTokens ?? u.output_tokens ?? 0,
|
|
219
|
+
}
|
|
220
|
+
: undefined,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
this.emitEvent({
|
|
225
|
+
state: 'error',
|
|
226
|
+
message: obj.result ?? obj.error ?? 'unknown error',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
case 'error': {
|
|
232
|
+
this.emitEvent({
|
|
233
|
+
state: 'error',
|
|
234
|
+
message: obj.error ?? 'unknown error',
|
|
235
|
+
});
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
// Legacy event types
|
|
239
|
+
case 'text':
|
|
240
|
+
case 'delta':
|
|
241
|
+
case 'content': {
|
|
242
|
+
const text = obj.delta ?? obj.text;
|
|
243
|
+
if (text)
|
|
244
|
+
this.emitEvent({ state: 'delta', text });
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case 'done': {
|
|
248
|
+
this.emitEvent({ state: 'final', agentSessionId: this.chatId ?? undefined });
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
default: {
|
|
252
|
+
if (obj.done) {
|
|
253
|
+
this.emitEvent({ state: 'final', agentSessionId: this.chatId ?? undefined });
|
|
254
|
+
}
|
|
255
|
+
else if (obj.delta ?? obj.text) {
|
|
256
|
+
this.emitEvent({ state: 'delta', text: (obj.delta ?? obj.text) });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
emitEvent(partial) {
|
|
262
|
+
const event = {
|
|
263
|
+
...partial,
|
|
264
|
+
runId: this.runId,
|
|
265
|
+
seq: this.seq++,
|
|
266
|
+
};
|
|
267
|
+
this.emit('agentEvent', event);
|
|
268
|
+
}
|
|
269
|
+
async killProcess() {
|
|
270
|
+
const proc = this.process;
|
|
271
|
+
if (!proc) {
|
|
272
|
+
await this.disposeManagedConfig(this.managedConfigDir);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
this.process = null;
|
|
276
|
+
proc.kill('SIGTERM');
|
|
277
|
+
await new Promise((resolve) => {
|
|
278
|
+
proc.on('close', resolve);
|
|
279
|
+
setTimeout(() => {
|
|
280
|
+
proc.kill('SIGKILL');
|
|
281
|
+
resolve();
|
|
282
|
+
}, 3000);
|
|
283
|
+
});
|
|
284
|
+
await this.disposeManagedConfig(this.managedConfigDir);
|
|
285
|
+
}
|
|
286
|
+
resetRunState() {
|
|
287
|
+
this.seq = 0;
|
|
288
|
+
this.emittedText = '';
|
|
289
|
+
}
|
|
290
|
+
isLiveAssistantDelta(obj) {
|
|
291
|
+
return obj.timestamp_ms != null && !obj.model_call_id;
|
|
292
|
+
}
|
|
293
|
+
emitTextDelta(text) {
|
|
294
|
+
if (!text)
|
|
295
|
+
return;
|
|
296
|
+
this.emittedText += text;
|
|
297
|
+
this.emitEvent({ state: 'delta', text });
|
|
298
|
+
}
|
|
299
|
+
emitFinalResultDelta(result) {
|
|
300
|
+
if (!result)
|
|
301
|
+
return;
|
|
302
|
+
if (!this.emittedText) {
|
|
303
|
+
this.emitTextDelta(result);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (!result.startsWith(this.emittedText))
|
|
307
|
+
return;
|
|
308
|
+
const tail = result.slice(this.emittedText.length);
|
|
309
|
+
if (tail) {
|
|
310
|
+
this.emitTextDelta(tail);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
registerAgent('cursor', () => new CursorAdapter());
|