@causyne/agent 1.11.0 → 1.12.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/docs/THIRD_PARTY_NOTICES.md +12 -0
- package/package.json +1 -1
- package/src/acp/protocol.js +1 -0
- package/src/acp/protocol.ts +22 -0
- package/src/acp/server.js +158 -21
- package/src/acp/server.ts +167 -21
- package/src/agentLoop.d.ts +22 -1
- package/src/agentLoop.js +15 -5
- package/src/approvalGrants.js +33 -0
- package/src/approvalGrants.ts +50 -0
- package/src/decisionPolicy.js +2 -0
- package/src/decisionPolicy.ts +1 -0
- package/src/engines/cad.js +113 -0
- package/src/engines/cad.ts +128 -0
- package/src/engines/film.js +108 -0
- package/src/engines/film.ts +104 -0
- package/src/engines/installer.js +226 -0
- package/src/engines/installer.ts +248 -0
- package/src/engines/photo.js +126 -0
- package/src/engines/photo.ts +137 -0
- package/src/engines/registry.js +186 -0
- package/src/engines/registry.ts +185 -0
- package/src/engines/sound.js +117 -0
- package/src/engines/sound.ts +130 -0
- package/src/engines/types.js +1 -0
- package/src/engines/types.ts +73 -0
- package/src/memoryManager.js +9 -3
- package/src/promptMentions.js +1 -1
- package/src/promptMentions.ts +1 -1
- package/src/sandbox.js +8 -0
- package/src/sessionJournal.js +12 -2
- package/src/sessionJournal.ts +12 -2
- package/src/toolPacks.js +15 -7
- package/src/tools/generateMediaViaApi.js +1 -1
- package/src/tools/nativeEngine.js +464 -0
- package/src/tools/nativeEngine.ts +433 -0
- package/src/tools/openSourceToolProvisioner.js +510 -0
- package/src/tools/openSourceToolProvisioner.ts +590 -0
- package/src/tools/registry.js +4 -1
- package/src/tui/completion.js +2 -2
|
@@ -104,6 +104,18 @@ contain adaptations from upstream open-source work:
|
|
|
104
104
|
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
105
105
|
> SOFTWARE.
|
|
106
106
|
|
|
107
|
+
The native creative engines operated via `src/tools/nativeEngine.ts` and `src/engines/`
|
|
108
|
+
power **Causyne Native Engines** (including **Causyne Film Engine**, **Causyne Photo Engine**, **Causyne Sound Engine**, and **Causyne CAD Engine**).
|
|
109
|
+
They execute as external headless engine binaries installed into the user's sovereign
|
|
110
|
+
`~/.causyne/engines/` directory with explicit user consent and are not bundled into
|
|
111
|
+
Causyne Agent's npm package.
|
|
112
|
+
|
|
113
|
+
Upstream open-source engine components are dual-licensed under MIT and Apache-2.0. Upstream
|
|
114
|
+
names are used solely for required legal copyright attribution and not as product trademarks:
|
|
115
|
+
|
|
116
|
+
> Copyright (c) 2026 The open-source engine contributors.
|
|
117
|
+
> Licensed under the Apache License, Version 2.0 or the MIT License.
|
|
118
|
+
|
|
107
119
|
|
|
108
120
|
Repository scanning found no third-party copyright header or license marker in
|
|
109
121
|
the project-authored source and skill files. That negative scan alone cannot
|
package/package.json
CHANGED
package/src/acp/protocol.js
CHANGED
package/src/acp/protocol.ts
CHANGED
|
@@ -15,6 +15,7 @@ export const ACP_METHODS = {
|
|
|
15
15
|
CANCEL: 'session/cancel',
|
|
16
16
|
UPDATE: 'session/update',
|
|
17
17
|
LOAD: 'session/load',
|
|
18
|
+
FORK: 'session/fork',
|
|
18
19
|
LIST: 'session/list',
|
|
19
20
|
CLOSE: 'session/close',
|
|
20
21
|
SET_MODE: 'session/set_mode',
|
|
@@ -150,6 +151,7 @@ export interface SessionCapabilities {
|
|
|
150
151
|
delete?: Record<string, never>;
|
|
151
152
|
additionalDirectories?: Record<string, never>;
|
|
152
153
|
close?: Record<string, never>;
|
|
154
|
+
fork?: Record<string, never>;
|
|
153
155
|
}
|
|
154
156
|
|
|
155
157
|
export interface AgentCapabilities {
|
|
@@ -249,6 +251,26 @@ export interface CloseSessionRequest {
|
|
|
249
251
|
|
|
250
252
|
export type CloseSessionResponse = Record<string, never>;
|
|
251
253
|
|
|
254
|
+
export interface LoadSessionRequest {
|
|
255
|
+
sessionId: string;
|
|
256
|
+
cwd?: string;
|
|
257
|
+
mcpServers?: McpServer[];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export interface LoadSessionResponse {
|
|
261
|
+
modes?: SessionModeState;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface ForkSessionRequest {
|
|
265
|
+
sessionId: string;
|
|
266
|
+
cwd?: string;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface ForkSessionResponse {
|
|
270
|
+
sessionId: string;
|
|
271
|
+
modes?: SessionModeState;
|
|
272
|
+
}
|
|
273
|
+
|
|
252
274
|
export type ToolCallStatus = 'pending' | 'in_progress' | 'completed' | 'failed';
|
|
253
275
|
|
|
254
276
|
export type ToolKind =
|
package/src/acp/server.js
CHANGED
|
@@ -2,6 +2,8 @@ import crypto from 'node:crypto';
|
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { AgentLoop } from '../agentLoop.js';
|
|
5
|
+
import { createApprovalScope } from '../approvalGrants.js';
|
|
6
|
+
import { createSession, getSession, saveSessionMessages } from '../sessionJournal.js';
|
|
5
7
|
import { ACP_ERROR_CODES, ACP_METHODS, ACP_PROTOCOL_VERSION, isJsonRpcNotification, isJsonRpcRequest, isJsonRpcResponse } from './protocol.js';
|
|
6
8
|
const MODE_TO_AUTONOMY = {
|
|
7
9
|
autonomous: 'A',
|
|
@@ -27,6 +29,21 @@ function textFromPrompt(prompt) {
|
|
|
27
29
|
}
|
|
28
30
|
return parts.join('\n').slice(0, MAX_PROMPT_CHARS);
|
|
29
31
|
}
|
|
32
|
+
function textFromContent(content) {
|
|
33
|
+
if (typeof content === 'string')
|
|
34
|
+
return content;
|
|
35
|
+
if (!Array.isArray(content))
|
|
36
|
+
return '';
|
|
37
|
+
const parts = [];
|
|
38
|
+
for (const block of content) {
|
|
39
|
+
if (block !== null && typeof block === 'object' && !Array.isArray(block)) {
|
|
40
|
+
const item = block;
|
|
41
|
+
if (item.type === 'text' && typeof item.text === 'string')
|
|
42
|
+
parts.push(item.text);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return parts.join('\n');
|
|
46
|
+
}
|
|
30
47
|
function toolKind(name) {
|
|
31
48
|
if (/^(fs_read|read_file|read|fs_list|list_dir|view_file)$/.test(name))
|
|
32
49
|
return 'read';
|
|
@@ -193,7 +210,10 @@ export class AcpServer {
|
|
|
193
210
|
this.writeResult(request.id, this.closeSession(request.params));
|
|
194
211
|
return;
|
|
195
212
|
case ACP_METHODS.LOAD:
|
|
196
|
-
this.
|
|
213
|
+
this.writeResult(request.id, this.loadSession(request.params));
|
|
214
|
+
return;
|
|
215
|
+
case ACP_METHODS.FORK:
|
|
216
|
+
this.writeResult(request.id, this.forkSession(request.params));
|
|
197
217
|
return;
|
|
198
218
|
default:
|
|
199
219
|
this.writeError(request.id, ACP_ERROR_CODES.METHOD_NOT_FOUND, `Unknown method: ${request.method}`);
|
|
@@ -209,10 +229,10 @@ export class AcpServer {
|
|
|
209
229
|
throw this.paramsError('initialize requires protocolVersion');
|
|
210
230
|
}
|
|
211
231
|
const capabilities = {
|
|
212
|
-
loadSession:
|
|
232
|
+
loadSession: true,
|
|
213
233
|
promptCapabilities: { image: false, audio: false, embeddedContext: false },
|
|
214
234
|
mcpCapabilities: { http: false, sse: false },
|
|
215
|
-
sessionCapabilities: { list: {}, close: {} }
|
|
235
|
+
sessionCapabilities: { list: {}, close: {}, fork: {} }
|
|
216
236
|
};
|
|
217
237
|
return {
|
|
218
238
|
protocolVersion: ACP_PROTOCOL_VERSION,
|
|
@@ -235,15 +255,26 @@ export class AcpServer {
|
|
|
235
255
|
if (Array.isArray(params.additionalDirectories) && params.additionalDirectories.length > 0) {
|
|
236
256
|
throw this.paramsError('additionalDirectories is not supported by this agent');
|
|
237
257
|
}
|
|
238
|
-
|
|
258
|
+
const cwd = this.resolveWorkspace(params.cwd);
|
|
259
|
+
this.bindWorkspace(cwd);
|
|
260
|
+
const record = createSession(cwd, 'Untitled', false);
|
|
261
|
+
const session = this.addSession(record.id, cwd, record.title, MODES.currentModeId);
|
|
262
|
+
return this.sessionResponse(session);
|
|
263
|
+
}
|
|
264
|
+
resolveWorkspace(cwd) {
|
|
265
|
+
if (typeof cwd !== 'string' || !cwd.trim())
|
|
266
|
+
throw this.paramsError('cwd is required');
|
|
239
267
|
try {
|
|
240
|
-
|
|
241
|
-
if (!fs.statSync(
|
|
268
|
+
const resolved = fs.realpathSync(path.resolve(cwd));
|
|
269
|
+
if (!fs.statSync(resolved).isDirectory())
|
|
242
270
|
throw new Error('not a directory');
|
|
271
|
+
return resolved;
|
|
243
272
|
}
|
|
244
273
|
catch {
|
|
245
|
-
throw this.paramsError(`cwd is not an accessible directory: ${
|
|
274
|
+
throw this.paramsError(`cwd is not an accessible directory: ${cwd}`);
|
|
246
275
|
}
|
|
276
|
+
}
|
|
277
|
+
bindWorkspace(cwd) {
|
|
247
278
|
if (this.workspaceRoot === null) {
|
|
248
279
|
this.workspaceRoot = cwd;
|
|
249
280
|
process.chdir(cwd);
|
|
@@ -251,23 +282,34 @@ export class AcpServer {
|
|
|
251
282
|
else if (this.workspaceRoot !== cwd) {
|
|
252
283
|
throw this.paramsError(`server is bound to ${this.workspaceRoot}; one workspace per server`);
|
|
253
284
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
285
|
+
}
|
|
286
|
+
addSession(id, cwd, title, modeId) {
|
|
287
|
+
const session = {
|
|
288
|
+
id,
|
|
289
|
+
memorySessionId: id,
|
|
290
|
+
title,
|
|
257
291
|
cwd,
|
|
258
|
-
modeId
|
|
292
|
+
modeId,
|
|
259
293
|
loop: null,
|
|
260
294
|
busy: false,
|
|
261
295
|
cancelled: false,
|
|
262
296
|
updatedAt: Date.now(),
|
|
263
|
-
queue: Promise.resolve()
|
|
264
|
-
|
|
297
|
+
queue: Promise.resolve(),
|
|
298
|
+
approvalScope: createApprovalScope()
|
|
299
|
+
};
|
|
300
|
+
this.sessions.set(id, session);
|
|
301
|
+
return session;
|
|
302
|
+
}
|
|
303
|
+
modesFor(session) {
|
|
265
304
|
return {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
305
|
+
currentModeId: session.modeId,
|
|
306
|
+
availableModes: MODES.availableModes.map((mode) => ({ ...mode }))
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
sessionResponse(session) {
|
|
310
|
+
return {
|
|
311
|
+
sessionId: session.id,
|
|
312
|
+
modes: this.modesFor(session)
|
|
271
313
|
};
|
|
272
314
|
}
|
|
273
315
|
requireSession(sessionId) {
|
|
@@ -303,9 +345,22 @@ export class AcpServer {
|
|
|
303
345
|
readyState: 1,
|
|
304
346
|
send: (payload) => this.onLoopPayload(session, payload)
|
|
305
347
|
};
|
|
348
|
+
const loopOptions = {
|
|
349
|
+
workspaceRoot: session.cwd,
|
|
350
|
+
memorySessionId: session.memorySessionId,
|
|
351
|
+
approvalScope: session.approvalScope
|
|
352
|
+
};
|
|
306
353
|
const loop = this.loopFactory
|
|
307
|
-
? this.loopFactory({
|
|
308
|
-
|
|
354
|
+
? this.loopFactory({
|
|
355
|
+
sessionId: session.id,
|
|
356
|
+
memorySessionId: session.memorySessionId,
|
|
357
|
+
taskId,
|
|
358
|
+
userPrompt: text,
|
|
359
|
+
cwd: session.cwd,
|
|
360
|
+
sink,
|
|
361
|
+
approvalScope: session.approvalScope
|
|
362
|
+
})
|
|
363
|
+
: new AgentLoop(sink, taskId, text, loopOptions);
|
|
309
364
|
const autonomy = MODE_TO_AUTONOMY[session.modeId];
|
|
310
365
|
if (autonomy) {
|
|
311
366
|
try {
|
|
@@ -327,6 +382,9 @@ export class AcpServer {
|
|
|
327
382
|
finally {
|
|
328
383
|
session.loop = null;
|
|
329
384
|
session.busy = false;
|
|
385
|
+
const record = getSession(session.memorySessionId);
|
|
386
|
+
if (record)
|
|
387
|
+
session.title = record.title;
|
|
330
388
|
if (session.cancelled)
|
|
331
389
|
stopReason = 'cancelled';
|
|
332
390
|
}
|
|
@@ -485,6 +543,7 @@ export class AcpServer {
|
|
|
485
543
|
};
|
|
486
544
|
const options = [
|
|
487
545
|
{ optionId: 'allow_once', name: 'Allow this action', kind: 'allow_once' },
|
|
546
|
+
{ optionId: 'allow_always', name: 'Always allow this tool in this conversation', kind: 'allow_always' },
|
|
488
547
|
{ optionId: 'reject_once', name: 'Reject this action', kind: 'reject_once' }
|
|
489
548
|
];
|
|
490
549
|
void this.sendClientRequest(session, ACP_METHODS.REQUEST_PERMISSION, {
|
|
@@ -492,7 +551,10 @@ export class AcpServer {
|
|
|
492
551
|
toolCall,
|
|
493
552
|
options
|
|
494
553
|
}).then((response) => {
|
|
495
|
-
const
|
|
554
|
+
const selected = response?.outcome.outcome === 'selected' ? response.outcome.optionId : '';
|
|
555
|
+
if (selected === 'allow_always')
|
|
556
|
+
session.approvalScope.grantAlways(tool);
|
|
557
|
+
const approved = selected === 'allow_once' || selected === 'allow_always';
|
|
496
558
|
session.loop?.handleApprovalResponse(toolCallId, approved);
|
|
497
559
|
}).catch(() => {
|
|
498
560
|
session.loop?.handleApprovalResponse(toolCallId, false);
|
|
@@ -541,6 +603,7 @@ export class AcpServer {
|
|
|
541
603
|
sessions.push({
|
|
542
604
|
sessionId: session.id,
|
|
543
605
|
cwd: session.cwd,
|
|
606
|
+
title: session.title,
|
|
544
607
|
updatedAt: new Date(session.updatedAt).toISOString()
|
|
545
608
|
});
|
|
546
609
|
}
|
|
@@ -559,6 +622,80 @@ export class AcpServer {
|
|
|
559
622
|
this.sessions.delete(session.id);
|
|
560
623
|
return {};
|
|
561
624
|
}
|
|
625
|
+
/** Resume a persisted conversation, replaying its transcript to the client. */
|
|
626
|
+
loadSession(params) {
|
|
627
|
+
if (!params || typeof params.sessionId !== 'string' || !params.sessionId) {
|
|
628
|
+
throw this.paramsError('session/load requires sessionId');
|
|
629
|
+
}
|
|
630
|
+
const live = this.sessions.get(params.sessionId);
|
|
631
|
+
if (live) {
|
|
632
|
+
this.bindWorkspace(live.cwd);
|
|
633
|
+
this.replayTranscript(live, getSession(live.memorySessionId)?.messages ?? []);
|
|
634
|
+
return { modes: this.modesFor(live) };
|
|
635
|
+
}
|
|
636
|
+
const record = getSession(params.sessionId);
|
|
637
|
+
if (!record)
|
|
638
|
+
throw this.notFound(`Unknown session: ${params.sessionId}`);
|
|
639
|
+
const requested = params.cwd ?? record.workspace;
|
|
640
|
+
const cwd = this.resolveWorkspace(requested);
|
|
641
|
+
if (record.workspace && path.resolve(record.workspace) !== cwd) {
|
|
642
|
+
throw this.paramsError('session workspace does not match cwd');
|
|
643
|
+
}
|
|
644
|
+
this.bindWorkspace(cwd);
|
|
645
|
+
const session = this.addSession(record.id, cwd, record.title, MODES.currentModeId);
|
|
646
|
+
this.replayTranscript(session, record.messages);
|
|
647
|
+
return { modes: this.modesFor(session) };
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Copy the conversation into a new session and deliberately start with a
|
|
651
|
+
* fresh approval scope. Permissions are granted to a conversation, never to
|
|
652
|
+
* the transcript, so a fork must ask again for anything dangerous.
|
|
653
|
+
*/
|
|
654
|
+
forkSession(params) {
|
|
655
|
+
if (!params || typeof params.sessionId !== 'string' || !params.sessionId) {
|
|
656
|
+
throw this.paramsError('session/fork requires sessionId');
|
|
657
|
+
}
|
|
658
|
+
const source = this.sessions.get(params.sessionId);
|
|
659
|
+
const sourceRecord = getSession(params.sessionId);
|
|
660
|
+
if (!source && !sourceRecord)
|
|
661
|
+
throw this.notFound(`Unknown session: ${params.sessionId}`);
|
|
662
|
+
if (source?.busy)
|
|
663
|
+
throw this.paramsError('session is busy; cancel it before forking');
|
|
664
|
+
const requested = params.cwd ?? source?.cwd ?? sourceRecord?.workspace;
|
|
665
|
+
const cwd = this.resolveWorkspace(requested);
|
|
666
|
+
if (source && source.cwd !== cwd)
|
|
667
|
+
throw this.paramsError('fork must stay in the source workspace');
|
|
668
|
+
if (sourceRecord?.workspace && path.resolve(sourceRecord.workspace) !== cwd) {
|
|
669
|
+
throw this.paramsError('fork must stay in the source workspace');
|
|
670
|
+
}
|
|
671
|
+
this.bindWorkspace(cwd);
|
|
672
|
+
const title = `Fork of ${source?.title || sourceRecord?.title || 'Untitled'}`;
|
|
673
|
+
const created = createSession(cwd, title, false);
|
|
674
|
+
if (sourceRecord && sourceRecord.messages.length > 0) {
|
|
675
|
+
saveSessionMessages(created.id, sourceRecord.messages);
|
|
676
|
+
}
|
|
677
|
+
const session = this.addSession(created.id, cwd, title, source?.modeId ?? MODES.currentModeId);
|
|
678
|
+
return this.sessionResponse(session);
|
|
679
|
+
}
|
|
680
|
+
replayTranscript(session, messages) {
|
|
681
|
+
for (const message of messages) {
|
|
682
|
+
const role = typeof message.role === 'string' ? message.role : '';
|
|
683
|
+
const text = textFromContent(message.content);
|
|
684
|
+
if (!text)
|
|
685
|
+
continue;
|
|
686
|
+
if (role === 'user') {
|
|
687
|
+
this.notify(session.id, { sessionUpdate: 'user_message_chunk', content: { type: 'text', text } });
|
|
688
|
+
}
|
|
689
|
+
else if (role === 'assistant') {
|
|
690
|
+
this.notify(session.id, { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text } });
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
notFound(message) {
|
|
695
|
+
const error = new Error(message);
|
|
696
|
+
error.acpCode = ACP_ERROR_CODES.RESOURCE_NOT_FOUND;
|
|
697
|
+
return error;
|
|
698
|
+
}
|
|
562
699
|
sendClientRequest(session, method, params) {
|
|
563
700
|
const id = `server-${this.nextClientRequestId++}`;
|
|
564
701
|
return new Promise((resolve, reject) => {
|
package/src/acp/server.ts
CHANGED
|
@@ -3,6 +3,9 @@ import fs from 'node:fs';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { AgentLoop } from '../agentLoop.js';
|
|
5
5
|
import type { AgentLoopEvent } from '../agentLoop.js';
|
|
6
|
+
import { createApprovalScope } from '../approvalGrants.js';
|
|
7
|
+
import type { ApprovalScope } from '../approvalGrants.js';
|
|
8
|
+
import { createSession, getSession, saveSessionMessages } from '../sessionJournal.js';
|
|
6
9
|
import {
|
|
7
10
|
ACP_ERROR_CODES,
|
|
8
11
|
ACP_METHODS,
|
|
@@ -16,6 +19,8 @@ import type {
|
|
|
16
19
|
CancelNotification,
|
|
17
20
|
CloseSessionRequest,
|
|
18
21
|
ContentBlock,
|
|
22
|
+
ForkSessionRequest,
|
|
23
|
+
ForkSessionResponse,
|
|
19
24
|
InitializeRequest,
|
|
20
25
|
InitializeResult,
|
|
21
26
|
JsonRpcError,
|
|
@@ -24,6 +29,8 @@ import type {
|
|
|
24
29
|
JsonRpcResponse,
|
|
25
30
|
ListSessionsRequest,
|
|
26
31
|
ListSessionsResponse,
|
|
32
|
+
LoadSessionRequest,
|
|
33
|
+
LoadSessionResponse,
|
|
27
34
|
NewSessionRequest,
|
|
28
35
|
NewSessionResponse,
|
|
29
36
|
PermissionOption,
|
|
@@ -48,10 +55,12 @@ export interface AcpEventSink {
|
|
|
48
55
|
|
|
49
56
|
export interface AcpLoopFactoryParams {
|
|
50
57
|
sessionId: string;
|
|
58
|
+
memorySessionId: string;
|
|
51
59
|
taskId: string;
|
|
52
60
|
userPrompt: string;
|
|
53
61
|
cwd: string;
|
|
54
62
|
sink: AcpEventSink;
|
|
63
|
+
approvalScope: ApprovalScope;
|
|
55
64
|
}
|
|
56
65
|
|
|
57
66
|
export type AcpLoopFactory = (params: AcpLoopFactoryParams) => AgentLoop;
|
|
@@ -73,6 +82,8 @@ interface PendingClientRequest {
|
|
|
73
82
|
|
|
74
83
|
interface AcpSession {
|
|
75
84
|
id: string;
|
|
85
|
+
memorySessionId: string;
|
|
86
|
+
title: string;
|
|
76
87
|
cwd: string;
|
|
77
88
|
modeId: string;
|
|
78
89
|
loop: AgentLoop | null;
|
|
@@ -80,6 +91,8 @@ interface AcpSession {
|
|
|
80
91
|
cancelled: boolean;
|
|
81
92
|
updatedAt: number;
|
|
82
93
|
queue: Promise<unknown>;
|
|
94
|
+
/** Session-local standing approvals. Forked/resumed sessions never inherit. */
|
|
95
|
+
approvalScope: ApprovalScope;
|
|
83
96
|
}
|
|
84
97
|
|
|
85
98
|
const MODE_TO_AUTONOMY: Record<string, 'A' | 'B' | 'C'> = {
|
|
@@ -109,6 +122,19 @@ function textFromPrompt(prompt: ContentBlock[]): string | null {
|
|
|
109
122
|
return parts.join('\n').slice(0, MAX_PROMPT_CHARS);
|
|
110
123
|
}
|
|
111
124
|
|
|
125
|
+
function textFromContent(content: unknown): string {
|
|
126
|
+
if (typeof content === 'string') return content;
|
|
127
|
+
if (!Array.isArray(content)) return '';
|
|
128
|
+
const parts: string[] = [];
|
|
129
|
+
for (const block of content) {
|
|
130
|
+
if (block !== null && typeof block === 'object' && !Array.isArray(block)) {
|
|
131
|
+
const item = block as Record<string, unknown>;
|
|
132
|
+
if (item.type === 'text' && typeof item.text === 'string') parts.push(item.text);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return parts.join('\n');
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
function toolKind(name: string): ToolKind {
|
|
113
139
|
if (/^(fs_read|read_file|read|fs_list|list_dir|view_file)$/.test(name)) return 'read';
|
|
114
140
|
if (/^(fs_write|fs_edit|MultiEdit|write_file|edit_file)$/.test(name)) return 'edit';
|
|
@@ -267,7 +293,10 @@ export class AcpServer {
|
|
|
267
293
|
this.writeResult(request.id, this.closeSession(request.params as CloseSessionRequest | undefined));
|
|
268
294
|
return;
|
|
269
295
|
case ACP_METHODS.LOAD:
|
|
270
|
-
this.
|
|
296
|
+
this.writeResult(request.id, this.loadSession(request.params as LoadSessionRequest | undefined));
|
|
297
|
+
return;
|
|
298
|
+
case ACP_METHODS.FORK:
|
|
299
|
+
this.writeResult(request.id, this.forkSession(request.params as ForkSessionRequest | undefined));
|
|
271
300
|
return;
|
|
272
301
|
default:
|
|
273
302
|
this.writeError(request.id, ACP_ERROR_CODES.METHOD_NOT_FOUND, `Unknown method: ${request.method}`);
|
|
@@ -283,10 +312,10 @@ export class AcpServer {
|
|
|
283
312
|
throw this.paramsError('initialize requires protocolVersion');
|
|
284
313
|
}
|
|
285
314
|
const capabilities: AgentCapabilities = {
|
|
286
|
-
loadSession:
|
|
315
|
+
loadSession: true,
|
|
287
316
|
promptCapabilities: { image: false, audio: false, embeddedContext: false },
|
|
288
317
|
mcpCapabilities: { http: false, sse: false },
|
|
289
|
-
sessionCapabilities: { list: {}, close: {} }
|
|
318
|
+
sessionCapabilities: { list: {}, close: {}, fork: {} }
|
|
290
319
|
};
|
|
291
320
|
return {
|
|
292
321
|
protocolVersion: ACP_PROTOCOL_VERSION,
|
|
@@ -311,36 +340,62 @@ export class AcpServer {
|
|
|
311
340
|
if (Array.isArray(params.additionalDirectories) && params.additionalDirectories.length > 0) {
|
|
312
341
|
throw this.paramsError('additionalDirectories is not supported by this agent');
|
|
313
342
|
}
|
|
314
|
-
|
|
343
|
+
const cwd = this.resolveWorkspace(params.cwd);
|
|
344
|
+
this.bindWorkspace(cwd);
|
|
345
|
+
const record = createSession(cwd, 'Untitled', false);
|
|
346
|
+
const session = this.addSession(record.id, cwd, record.title, MODES.currentModeId);
|
|
347
|
+
return this.sessionResponse(session);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private resolveWorkspace(cwd: unknown): string {
|
|
351
|
+
if (typeof cwd !== 'string' || !cwd.trim()) throw this.paramsError('cwd is required');
|
|
315
352
|
try {
|
|
316
|
-
|
|
317
|
-
if (!fs.statSync(
|
|
353
|
+
const resolved = fs.realpathSync(path.resolve(cwd));
|
|
354
|
+
if (!fs.statSync(resolved).isDirectory()) throw new Error('not a directory');
|
|
355
|
+
return resolved;
|
|
318
356
|
} catch {
|
|
319
|
-
throw this.paramsError(`cwd is not an accessible directory: ${
|
|
357
|
+
throw this.paramsError(`cwd is not an accessible directory: ${cwd}`);
|
|
320
358
|
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private bindWorkspace(cwd: string): void {
|
|
321
362
|
if (this.workspaceRoot === null) {
|
|
322
363
|
this.workspaceRoot = cwd;
|
|
323
364
|
process.chdir(cwd);
|
|
324
365
|
} else if (this.workspaceRoot !== cwd) {
|
|
325
366
|
throw this.paramsError(`server is bound to ${this.workspaceRoot}; one workspace per server`);
|
|
326
367
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private addSession(id: string, cwd: string, title: string, modeId: string): AcpSession {
|
|
371
|
+
const session: AcpSession = {
|
|
372
|
+
id,
|
|
373
|
+
memorySessionId: id,
|
|
374
|
+
title,
|
|
330
375
|
cwd,
|
|
331
|
-
modeId
|
|
376
|
+
modeId,
|
|
332
377
|
loop: null,
|
|
333
378
|
busy: false,
|
|
334
379
|
cancelled: false,
|
|
335
380
|
updatedAt: Date.now(),
|
|
336
|
-
queue: Promise.resolve()
|
|
337
|
-
|
|
381
|
+
queue: Promise.resolve(),
|
|
382
|
+
approvalScope: createApprovalScope()
|
|
383
|
+
};
|
|
384
|
+
this.sessions.set(id, session);
|
|
385
|
+
return session;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private modesFor(session: AcpSession): SessionModeState {
|
|
338
389
|
return {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
390
|
+
currentModeId: session.modeId,
|
|
391
|
+
availableModes: MODES.availableModes.map((mode) => ({ ...mode }))
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private sessionResponse(session: AcpSession): NewSessionResponse {
|
|
396
|
+
return {
|
|
397
|
+
sessionId: session.id,
|
|
398
|
+
modes: this.modesFor(session)
|
|
344
399
|
};
|
|
345
400
|
}
|
|
346
401
|
|
|
@@ -381,9 +436,22 @@ export class AcpServer {
|
|
|
381
436
|
readyState: 1,
|
|
382
437
|
send: (payload: string) => this.onLoopPayload(session, payload)
|
|
383
438
|
};
|
|
439
|
+
const loopOptions = {
|
|
440
|
+
workspaceRoot: session.cwd,
|
|
441
|
+
memorySessionId: session.memorySessionId,
|
|
442
|
+
approvalScope: session.approvalScope
|
|
443
|
+
};
|
|
384
444
|
const loop = this.loopFactory
|
|
385
|
-
? this.loopFactory({
|
|
386
|
-
|
|
445
|
+
? this.loopFactory({
|
|
446
|
+
sessionId: session.id,
|
|
447
|
+
memorySessionId: session.memorySessionId,
|
|
448
|
+
taskId,
|
|
449
|
+
userPrompt: text,
|
|
450
|
+
cwd: session.cwd,
|
|
451
|
+
sink,
|
|
452
|
+
approvalScope: session.approvalScope
|
|
453
|
+
})
|
|
454
|
+
: new AgentLoop(sink, taskId, text, loopOptions);
|
|
387
455
|
const autonomy = MODE_TO_AUTONOMY[session.modeId];
|
|
388
456
|
if (autonomy) {
|
|
389
457
|
try {
|
|
@@ -402,6 +470,8 @@ export class AcpServer {
|
|
|
402
470
|
} finally {
|
|
403
471
|
session.loop = null;
|
|
404
472
|
session.busy = false;
|
|
473
|
+
const record = getSession(session.memorySessionId);
|
|
474
|
+
if (record) session.title = record.title;
|
|
405
475
|
if (session.cancelled) stopReason = 'cancelled';
|
|
406
476
|
}
|
|
407
477
|
return stopReason;
|
|
@@ -550,6 +620,7 @@ export class AcpServer {
|
|
|
550
620
|
};
|
|
551
621
|
const options: PermissionOption[] = [
|
|
552
622
|
{ optionId: 'allow_once', name: 'Allow this action', kind: 'allow_once' },
|
|
623
|
+
{ optionId: 'allow_always', name: 'Always allow this tool in this conversation', kind: 'allow_always' },
|
|
553
624
|
{ optionId: 'reject_once', name: 'Reject this action', kind: 'reject_once' }
|
|
554
625
|
];
|
|
555
626
|
void this.sendClientRequest<RequestPermissionResponse>(session, ACP_METHODS.REQUEST_PERMISSION, {
|
|
@@ -557,7 +628,9 @@ export class AcpServer {
|
|
|
557
628
|
toolCall,
|
|
558
629
|
options
|
|
559
630
|
}).then((response) => {
|
|
560
|
-
const
|
|
631
|
+
const selected = response?.outcome.outcome === 'selected' ? response.outcome.optionId : '';
|
|
632
|
+
if (selected === 'allow_always') session.approvalScope.grantAlways(tool);
|
|
633
|
+
const approved = selected === 'allow_once' || selected === 'allow_always';
|
|
561
634
|
session.loop?.handleApprovalResponse(toolCallId, approved);
|
|
562
635
|
}).catch(() => {
|
|
563
636
|
session.loop?.handleApprovalResponse(toolCallId, false);
|
|
@@ -606,6 +679,7 @@ export class AcpServer {
|
|
|
606
679
|
sessions.push({
|
|
607
680
|
sessionId: session.id,
|
|
608
681
|
cwd: session.cwd,
|
|
682
|
+
title: session.title,
|
|
609
683
|
updatedAt: new Date(session.updatedAt).toISOString()
|
|
610
684
|
});
|
|
611
685
|
}
|
|
@@ -625,6 +699,78 @@ export class AcpServer {
|
|
|
625
699
|
return {};
|
|
626
700
|
}
|
|
627
701
|
|
|
702
|
+
/** Resume a persisted conversation, replaying its transcript to the client. */
|
|
703
|
+
private loadSession(params: LoadSessionRequest | undefined): LoadSessionResponse {
|
|
704
|
+
if (!params || typeof params.sessionId !== 'string' || !params.sessionId) {
|
|
705
|
+
throw this.paramsError('session/load requires sessionId');
|
|
706
|
+
}
|
|
707
|
+
const live = this.sessions.get(params.sessionId);
|
|
708
|
+
if (live) {
|
|
709
|
+
this.bindWorkspace(live.cwd);
|
|
710
|
+
this.replayTranscript(live, getSession(live.memorySessionId)?.messages ?? []);
|
|
711
|
+
return { modes: this.modesFor(live) };
|
|
712
|
+
}
|
|
713
|
+
const record = getSession(params.sessionId);
|
|
714
|
+
if (!record) throw this.notFound(`Unknown session: ${params.sessionId}`);
|
|
715
|
+
const requested = params.cwd ?? record.workspace;
|
|
716
|
+
const cwd = this.resolveWorkspace(requested);
|
|
717
|
+
if (record.workspace && path.resolve(record.workspace) !== cwd) {
|
|
718
|
+
throw this.paramsError('session workspace does not match cwd');
|
|
719
|
+
}
|
|
720
|
+
this.bindWorkspace(cwd);
|
|
721
|
+
const session = this.addSession(record.id, cwd, record.title, MODES.currentModeId);
|
|
722
|
+
this.replayTranscript(session, record.messages);
|
|
723
|
+
return { modes: this.modesFor(session) };
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Copy the conversation into a new session and deliberately start with a
|
|
728
|
+
* fresh approval scope. Permissions are granted to a conversation, never to
|
|
729
|
+
* the transcript, so a fork must ask again for anything dangerous.
|
|
730
|
+
*/
|
|
731
|
+
private forkSession(params: ForkSessionRequest | undefined): ForkSessionResponse {
|
|
732
|
+
if (!params || typeof params.sessionId !== 'string' || !params.sessionId) {
|
|
733
|
+
throw this.paramsError('session/fork requires sessionId');
|
|
734
|
+
}
|
|
735
|
+
const source = this.sessions.get(params.sessionId);
|
|
736
|
+
const sourceRecord = getSession(params.sessionId);
|
|
737
|
+
if (!source && !sourceRecord) throw this.notFound(`Unknown session: ${params.sessionId}`);
|
|
738
|
+
if (source?.busy) throw this.paramsError('session is busy; cancel it before forking');
|
|
739
|
+
const requested = params.cwd ?? source?.cwd ?? sourceRecord?.workspace;
|
|
740
|
+
const cwd = this.resolveWorkspace(requested);
|
|
741
|
+
if (source && source.cwd !== cwd) throw this.paramsError('fork must stay in the source workspace');
|
|
742
|
+
if (sourceRecord?.workspace && path.resolve(sourceRecord.workspace) !== cwd) {
|
|
743
|
+
throw this.paramsError('fork must stay in the source workspace');
|
|
744
|
+
}
|
|
745
|
+
this.bindWorkspace(cwd);
|
|
746
|
+
const title = `Fork of ${source?.title || sourceRecord?.title || 'Untitled'}`;
|
|
747
|
+
const created = createSession(cwd, title, false);
|
|
748
|
+
if (sourceRecord && sourceRecord.messages.length > 0) {
|
|
749
|
+
saveSessionMessages(created.id, sourceRecord.messages);
|
|
750
|
+
}
|
|
751
|
+
const session = this.addSession(created.id, cwd, title, source?.modeId ?? MODES.currentModeId);
|
|
752
|
+
return this.sessionResponse(session) as ForkSessionResponse;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
private replayTranscript(session: AcpSession, messages: Array<{ role?: unknown; content?: unknown }>): void {
|
|
756
|
+
for (const message of messages) {
|
|
757
|
+
const role = typeof message.role === 'string' ? message.role : '';
|
|
758
|
+
const text = textFromContent(message.content);
|
|
759
|
+
if (!text) continue;
|
|
760
|
+
if (role === 'user') {
|
|
761
|
+
this.notify(session.id, { sessionUpdate: 'user_message_chunk', content: { type: 'text', text } });
|
|
762
|
+
} else if (role === 'assistant') {
|
|
763
|
+
this.notify(session.id, { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text } });
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
private notFound(message: string): Error & { acpCode: number } {
|
|
769
|
+
const error = new Error(message) as Error & { acpCode: number };
|
|
770
|
+
error.acpCode = ACP_ERROR_CODES.RESOURCE_NOT_FOUND;
|
|
771
|
+
return error;
|
|
772
|
+
}
|
|
773
|
+
|
|
628
774
|
private sendClientRequest<T>(session: AcpSession, method: string, params: Record<string, unknown>): Promise<T> {
|
|
629
775
|
const id = `server-${this.nextClientRequestId++}`;
|
|
630
776
|
return new Promise<T>((resolve, reject) => {
|