dsh-ccgui 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +327 -2
- package/THIRD_PARTY_NOTICES.md +64 -0
- package/assets/logo.png +0 -0
- package/cordis.patch.yml +32 -0
- package/lib/abortable.d.ts +12 -0
- package/lib/abortable.js +27 -0
- package/lib/adapter.d.ts +39 -0
- package/lib/adapter.js +64 -0
- package/lib/async-queue.d.ts +26 -0
- package/lib/async-queue.js +55 -0
- package/lib/cli-session.d.ts +99 -0
- package/lib/cli-session.js +384 -0
- package/lib/cli-tool.d.ts +18 -0
- package/lib/cli-tool.js +101 -0
- package/lib/client.js +4039 -0
- package/lib/constants.d.ts +22 -0
- package/lib/constants.js +22 -0
- package/lib/git/git-read.d.ts +65 -0
- package/lib/git/git-read.js +139 -0
- package/lib/git/git-requests.d.ts +44 -0
- package/lib/git/git-requests.js +153 -0
- package/lib/git/git-routes.d.ts +38 -0
- package/lib/git/git-routes.js +78 -0
- package/lib/git/git-run.d.ts +58 -0
- package/lib/git/git-run.js +97 -0
- package/lib/git/git-status.d.ts +51 -0
- package/lib/git/git-status.js +122 -0
- package/lib/git/git-write.d.ts +75 -0
- package/lib/git/git-write.js +134 -0
- package/lib/index.d.ts +78 -0
- package/lib/index.js +229 -0
- package/lib/model-cache.d.ts +33 -0
- package/lib/model-cache.js +101 -0
- package/lib/model-catalog.d.ts +63 -0
- package/lib/model-catalog.js +107 -0
- package/lib/model-options.d.ts +26 -0
- package/lib/model-options.js +46 -0
- package/lib/model-probe.d.ts +16 -0
- package/lib/model-probe.js +91 -0
- package/lib/npx-cache.d.ts +39 -0
- package/lib/npx-cache.js +97 -0
- package/lib/permission.d.ts +17 -0
- package/lib/permission.js +34 -0
- package/lib/prompt-images.d.ts +44 -0
- package/lib/prompt-images.js +46 -0
- package/lib/request-input.d.ts +42 -0
- package/lib/request-input.js +113 -0
- package/lib/session/connect.d.ts +41 -0
- package/lib/session/connect.js +68 -0
- package/lib/session/drive-tool.d.ts +39 -0
- package/lib/session/drive-tool.js +37 -0
- package/lib/session/model-state.d.ts +40 -0
- package/lib/session/model-state.js +68 -0
- package/lib/session/outcomes.d.ts +31 -0
- package/lib/session/outcomes.js +67 -0
- package/lib/session/permission-queue.d.ts +24 -0
- package/lib/session/permission-queue.js +33 -0
- package/lib/session/read-step.d.ts +31 -0
- package/lib/session/read-step.js +53 -0
- package/lib/session/tool-mirror.d.ts +65 -0
- package/lib/session/tool-mirror.js +131 -0
- package/lib/session/turn-queue.d.ts +76 -0
- package/lib/session/turn-queue.js +102 -0
- package/lib/session/types.d.ts +58 -0
- package/lib/session/types.js +6 -0
- package/lib/session-registry.d.ts +124 -0
- package/lib/session-registry.js +189 -0
- package/lib/stdio-transport.d.ts +28 -0
- package/lib/stdio-transport.js +164 -0
- package/lib/stream-blocks.d.ts +52 -0
- package/lib/stream-blocks.js +95 -0
- package/lib/tool-record.d.ts +100 -0
- package/lib/tool-record.js +167 -0
- package/lib/transport.d.ts +35 -0
- package/lib/transport.js +5 -0
- package/package.json +136 -4
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI turns for one session: prompts waiting their turn, the turn in flight, and
|
|
3
|
+
* cancelling it. Only one turn runs at a time; steering sent mid-turn waits here and
|
|
4
|
+
* runs as the next turn.
|
|
5
|
+
* @module dsh-ccgui/session/turn-queue
|
|
6
|
+
*/
|
|
7
|
+
import type { ContentBlock as AcpContentBlock, StopReason } from '@agentclientprotocol/sdk';
|
|
8
|
+
import type { AcpTransport } from '../transport.ts';
|
|
9
|
+
/** How a CLI turn ended. */
|
|
10
|
+
export type TurnEnd = {
|
|
11
|
+
readonly type: 'turn-end';
|
|
12
|
+
readonly stopReason: StopReason;
|
|
13
|
+
} | {
|
|
14
|
+
readonly type: 'failure';
|
|
15
|
+
readonly error: Error;
|
|
16
|
+
};
|
|
17
|
+
/** User input waiting to be sent as a CLI turn. */
|
|
18
|
+
export interface QueuedPrompt {
|
|
19
|
+
readonly ids: readonly string[];
|
|
20
|
+
readonly blocks: readonly AcpContentBlock[];
|
|
21
|
+
}
|
|
22
|
+
/** Where a turn is sent. */
|
|
23
|
+
interface TurnTarget {
|
|
24
|
+
readonly transport: AcpTransport;
|
|
25
|
+
readonly acpSessionId: string;
|
|
26
|
+
}
|
|
27
|
+
/** What the queue needs from its session. */
|
|
28
|
+
export interface TurnHooks {
|
|
29
|
+
/** @returns where to send a turn, or undefined while the session cannot take one. */
|
|
30
|
+
readonly target: () => TurnTarget | undefined;
|
|
31
|
+
/** A prompt is going out; its messages now count as sent. */
|
|
32
|
+
readonly onSend: (prompt: QueuedPrompt) => void;
|
|
33
|
+
/** A turn ended. A cancelled turn is not reported to the reader, who already left it. */
|
|
34
|
+
readonly onEnd: (event: TurnEnd, wasCancelling: boolean) => void;
|
|
35
|
+
/** A cancelled CLI did not finish its turn within the grace period. */
|
|
36
|
+
readonly onCancelTimeout: () => void;
|
|
37
|
+
/** How long a cancelled CLI may take to finish its turn before it is abandoned. */
|
|
38
|
+
readonly cancelGraceMs: number;
|
|
39
|
+
}
|
|
40
|
+
/** The turn state machine of one CLI session. */
|
|
41
|
+
export declare class TurnQueue {
|
|
42
|
+
private readonly hooks;
|
|
43
|
+
private pending;
|
|
44
|
+
private running;
|
|
45
|
+
private stopping;
|
|
46
|
+
private ended;
|
|
47
|
+
/** Aborted when the CLI turn ends, which withdraws any question still on screen. */
|
|
48
|
+
private lifetime;
|
|
49
|
+
private cancelTimer;
|
|
50
|
+
constructor(hooks: TurnHooks);
|
|
51
|
+
/** Whether a CLI turn is in flight. */
|
|
52
|
+
get active(): boolean;
|
|
53
|
+
/** Whether the turn in flight has been asked to stop. */
|
|
54
|
+
get cancelling(): boolean;
|
|
55
|
+
/** Whether a turn is running or waiting. */
|
|
56
|
+
get busy(): boolean;
|
|
57
|
+
/** Ids of the user messages queued but not sent yet. */
|
|
58
|
+
get queuedIds(): readonly string[];
|
|
59
|
+
/** Aborted when the current turn ends. */
|
|
60
|
+
get signal(): AbortSignal;
|
|
61
|
+
/** @returns a promise that settles when the current turn ends. */
|
|
62
|
+
untilEnded(): Promise<void>;
|
|
63
|
+
enqueue(prompt: QueuedPrompt): void;
|
|
64
|
+
clearPending(): void;
|
|
65
|
+
/** Withdraw anything still asked within the current turn. */
|
|
66
|
+
abortLifetime(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Ask the CLI to stop the turn in flight.
|
|
69
|
+
* @param sendCancel - sends `session/cancel` to the CLI.
|
|
70
|
+
*/
|
|
71
|
+
beginCancel(sendCancel: () => void): void;
|
|
72
|
+
end(event: TurnEnd): void;
|
|
73
|
+
/** Send the next queued prompt if no turn is running and the session can take one. */
|
|
74
|
+
pump(): void;
|
|
75
|
+
}
|
|
76
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI turns for one session: prompts waiting their turn, the turn in flight, and
|
|
3
|
+
* cancelling it. Only one turn runs at a time; steering sent mid-turn waits here and
|
|
4
|
+
* runs as the next turn.
|
|
5
|
+
* @module dsh-ccgui/session/turn-queue
|
|
6
|
+
*/
|
|
7
|
+
/** The turn state machine of one CLI session. */
|
|
8
|
+
export class TurnQueue {
|
|
9
|
+
hooks;
|
|
10
|
+
pending = [];
|
|
11
|
+
running = false;
|
|
12
|
+
stopping = false;
|
|
13
|
+
ended = Promise.withResolvers();
|
|
14
|
+
/** Aborted when the CLI turn ends, which withdraws any question still on screen. */
|
|
15
|
+
lifetime = new AbortController();
|
|
16
|
+
cancelTimer;
|
|
17
|
+
constructor(hooks) {
|
|
18
|
+
this.hooks = hooks;
|
|
19
|
+
}
|
|
20
|
+
/** Whether a CLI turn is in flight. */
|
|
21
|
+
get active() {
|
|
22
|
+
return this.running;
|
|
23
|
+
}
|
|
24
|
+
/** Whether the turn in flight has been asked to stop. */
|
|
25
|
+
get cancelling() {
|
|
26
|
+
return this.stopping;
|
|
27
|
+
}
|
|
28
|
+
/** Whether a turn is running or waiting. */
|
|
29
|
+
get busy() {
|
|
30
|
+
return this.running || this.pending.length > 0;
|
|
31
|
+
}
|
|
32
|
+
/** Ids of the user messages queued but not sent yet. */
|
|
33
|
+
get queuedIds() {
|
|
34
|
+
return this.pending.flatMap((prompt) => prompt.ids);
|
|
35
|
+
}
|
|
36
|
+
/** Aborted when the current turn ends. */
|
|
37
|
+
get signal() {
|
|
38
|
+
return this.lifetime.signal;
|
|
39
|
+
}
|
|
40
|
+
/** @returns a promise that settles when the current turn ends. */
|
|
41
|
+
untilEnded() {
|
|
42
|
+
return this.ended.promise;
|
|
43
|
+
}
|
|
44
|
+
enqueue(prompt) {
|
|
45
|
+
this.pending = [...this.pending, prompt];
|
|
46
|
+
this.pump();
|
|
47
|
+
}
|
|
48
|
+
clearPending() {
|
|
49
|
+
this.pending = [];
|
|
50
|
+
}
|
|
51
|
+
/** Withdraw anything still asked within the current turn. */
|
|
52
|
+
abortLifetime() {
|
|
53
|
+
this.lifetime.abort();
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Ask the CLI to stop the turn in flight.
|
|
57
|
+
* @param sendCancel - sends `session/cancel` to the CLI.
|
|
58
|
+
*/
|
|
59
|
+
beginCancel(sendCancel) {
|
|
60
|
+
this.stopping = true;
|
|
61
|
+
sendCancel();
|
|
62
|
+
// A CLI that ignores session/cancel would block every later prompt and may keep
|
|
63
|
+
// changing files, so it is stopped outright once the grace period ends.
|
|
64
|
+
this.cancelTimer = setTimeout(() => {
|
|
65
|
+
this.hooks.onCancelTimeout();
|
|
66
|
+
}, this.hooks.cancelGraceMs);
|
|
67
|
+
this.cancelTimer.unref();
|
|
68
|
+
}
|
|
69
|
+
end(event) {
|
|
70
|
+
if (!this.running)
|
|
71
|
+
return;
|
|
72
|
+
this.running = false;
|
|
73
|
+
clearTimeout(this.cancelTimer);
|
|
74
|
+
const wasCancelling = this.stopping;
|
|
75
|
+
this.stopping = false;
|
|
76
|
+
this.lifetime.abort();
|
|
77
|
+
this.hooks.onEnd(event, wasCancelling);
|
|
78
|
+
this.ended.resolve();
|
|
79
|
+
this.pump();
|
|
80
|
+
}
|
|
81
|
+
/** Send the next queued prompt if no turn is running and the session can take one. */
|
|
82
|
+
pump() {
|
|
83
|
+
if (this.running)
|
|
84
|
+
return;
|
|
85
|
+
const target = this.hooks.target();
|
|
86
|
+
if (target === undefined)
|
|
87
|
+
return;
|
|
88
|
+
const [next, ...rest] = this.pending;
|
|
89
|
+
if (next === undefined)
|
|
90
|
+
return;
|
|
91
|
+
this.pending = rest;
|
|
92
|
+
this.hooks.onSend(next);
|
|
93
|
+
this.running = true;
|
|
94
|
+
this.ended = Promise.withResolvers();
|
|
95
|
+
this.lifetime = new AbortController();
|
|
96
|
+
target.transport.prompt(target.acpSessionId, next.blocks).then((stopReason) => {
|
|
97
|
+
this.end({ type: 'turn-end', stopReason });
|
|
98
|
+
}, (error) => {
|
|
99
|
+
this.end({ type: 'failure', error: error instanceof Error ? error : new Error(String(error)) });
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract of a CLI session: what it is built from, what one harness step asks of it,
|
|
3
|
+
* and the events its reader consumes.
|
|
4
|
+
* @module dsh-ccgui/session/types
|
|
5
|
+
*/
|
|
6
|
+
import type { RequestMessage } from '@deepseek-ai/dsh-llm';
|
|
7
|
+
import type { CliModelList } from '../model-options.ts';
|
|
8
|
+
import type { ReadImage } from '../prompt-images.ts';
|
|
9
|
+
import type { AcpTransport, TransportHandlers } from '../transport.ts';
|
|
10
|
+
import type { TurnEnd } from './turn-queue.ts';
|
|
11
|
+
/** Construction inputs for one CLI session. */
|
|
12
|
+
export interface CliSessionOptions {
|
|
13
|
+
readonly dshSessionId: string;
|
|
14
|
+
/** The provider route of this CLI, which tells its native sessions from another CLI's. */
|
|
15
|
+
readonly provider: string;
|
|
16
|
+
readonly cwd: string;
|
|
17
|
+
/** CLI display name used in user-facing messages. */
|
|
18
|
+
readonly label: string;
|
|
19
|
+
/** Opens the connection on first use. */
|
|
20
|
+
readonly openTransport: (handlers: TransportHandlers) => AcpTransport;
|
|
21
|
+
readonly mintCallId?: () => string;
|
|
22
|
+
/** How long a cancelled CLI may take to finish its turn before it is abandoned. */
|
|
23
|
+
readonly cancelGraceMs?: number;
|
|
24
|
+
/** How long the CLI may take to accept a session, which must allow for a cold `npx` download. */
|
|
25
|
+
readonly startTimeoutMs?: number;
|
|
26
|
+
/** How long a pending command's reports must stay quiet before it counts as started. */
|
|
27
|
+
readonly toolSettleMs?: number;
|
|
28
|
+
/** What this CLI reported about its models when asked ahead of any session. */
|
|
29
|
+
readonly knownModels?: () => CliModelList | undefined;
|
|
30
|
+
/** Reads pasted images from the harness; absent when it has no attachment service. */
|
|
31
|
+
readonly readImage?: ReadImage;
|
|
32
|
+
/** Receives failures that have no caller left to reject. */
|
|
33
|
+
readonly onError?: (error: unknown) => void;
|
|
34
|
+
/** User messages an earlier CLI process for this harness session received. */
|
|
35
|
+
readonly sentMessageIds?: ReadonlySet<string>;
|
|
36
|
+
/**
|
|
37
|
+
* Settles once any earlier process for this harness session has stopped, because
|
|
38
|
+
* two CLIs must not work on one native session and working directory at once.
|
|
39
|
+
*/
|
|
40
|
+
readonly after?: Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
/** One harness model request. */
|
|
43
|
+
export interface StreamRequest {
|
|
44
|
+
readonly messages: readonly RequestMessage[];
|
|
45
|
+
/** Ids of the user messages in the open harness turn; omitted, every message qualifies. */
|
|
46
|
+
readonly currentTurnMessageIds?: ReadonlySet<string>;
|
|
47
|
+
/** The model selected in the harness; omitted or `default` means the CLI's own. */
|
|
48
|
+
readonly model?: string;
|
|
49
|
+
readonly signal?: AbortSignal;
|
|
50
|
+
}
|
|
51
|
+
/** What the step reader consumes, in the order it happened. */
|
|
52
|
+
export type SessionEvent = {
|
|
53
|
+
readonly type: 'text' | 'reasoning';
|
|
54
|
+
readonly text: string;
|
|
55
|
+
} | {
|
|
56
|
+
readonly type: 'tool-ready';
|
|
57
|
+
readonly acpId: string;
|
|
58
|
+
} | TurnEnd;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps harness sessions to their live CLI sessions, and follows each session's log
|
|
3
|
+
* to know where its turns begin and end. It holds no framework state, so `index.ts`
|
|
4
|
+
* can stay plain wiring.
|
|
5
|
+
* @module dsh-ccgui/session-registry
|
|
6
|
+
*/
|
|
7
|
+
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
8
|
+
import type { ToolRunContext } from '@deepseek-ai/dsh-tools';
|
|
9
|
+
import type { ApprovalOutcome } from '@deepseek-ai/dsh-user-approval';
|
|
10
|
+
import type { CliSession, PermissionAsk } from './cli-session.ts';
|
|
11
|
+
import type { ToolOutcome } from './tool-record.ts';
|
|
12
|
+
/** One CLI that speaks ACP over stdio. */
|
|
13
|
+
export interface CliAgentConfig {
|
|
14
|
+
/** Names the provider route: `cli-<id>`. */
|
|
15
|
+
id: string;
|
|
16
|
+
/** Display name in menus and messages. */
|
|
17
|
+
name: string;
|
|
18
|
+
/** Executable to start. */
|
|
19
|
+
command: string;
|
|
20
|
+
/** Arguments that make it serve ACP on stdio. */
|
|
21
|
+
args: string[];
|
|
22
|
+
/** Added to the scrubbed environment; the place for this CLI's own credentials. */
|
|
23
|
+
env: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
/** What a new CLI session inherits from the one it replaces. */
|
|
26
|
+
export interface SessionStart {
|
|
27
|
+
readonly sentMessageIds: ReadonlySet<string>;
|
|
28
|
+
/** Settles once the previous process has stopped. */
|
|
29
|
+
readonly after: Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
/** The harness facilities the registry uses. */
|
|
32
|
+
export interface RegistryDeps {
|
|
33
|
+
getAgent(sessionId: string): Agent | undefined;
|
|
34
|
+
/** The preset this agent runs under, if any. */
|
|
35
|
+
composedPreset(agent: Agent): string | undefined;
|
|
36
|
+
ask(agent: Agent, request: PermissionAsk, signal: AbortSignal): Promise<ApprovalOutcome>;
|
|
37
|
+
warn(message: string): void;
|
|
38
|
+
/** Someone chose this CLI, so starting it is now acceptable. */
|
|
39
|
+
onCliSelected(provider: string): void;
|
|
40
|
+
createSession(config: CliAgentConfig, sessionId: string, cwd: string, start: SessionStart): CliSession;
|
|
41
|
+
}
|
|
42
|
+
/** The fields of a session log event the registry reads. */
|
|
43
|
+
export interface ObservedEvent {
|
|
44
|
+
readonly type: string;
|
|
45
|
+
readonly data?: unknown;
|
|
46
|
+
}
|
|
47
|
+
/** Owns every live CLI session in this process. */
|
|
48
|
+
export declare class CliSessionRegistry {
|
|
49
|
+
private readonly deps;
|
|
50
|
+
private readonly agentsByProvider;
|
|
51
|
+
private sessions;
|
|
52
|
+
private stopping;
|
|
53
|
+
private turnMessages;
|
|
54
|
+
constructor(agents: readonly CliAgentConfig[], deps: RegistryDeps);
|
|
55
|
+
/** Provider routes of all configured CLIs. */
|
|
56
|
+
get providers(): readonly string[];
|
|
57
|
+
/**
|
|
58
|
+
* Read a route's display name.
|
|
59
|
+
* @param provider - a provider route.
|
|
60
|
+
* @returns the CLI's name, or undefined for any other route.
|
|
61
|
+
*/
|
|
62
|
+
labelFor(provider: string): string | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Check whether a provider route is one of the configured CLIs.
|
|
65
|
+
* @param provider - a provider route.
|
|
66
|
+
* @returns whether it is.
|
|
67
|
+
*/
|
|
68
|
+
isCliRoute(provider: string): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Read the CLI behind a route.
|
|
71
|
+
* @param provider - a provider route.
|
|
72
|
+
* @returns its configuration, if it is a CLI.
|
|
73
|
+
*/
|
|
74
|
+
configFor(provider: string): CliAgentConfig | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Point an agent at the CLI its preset stands for. A preset whose id is a CLI route
|
|
77
|
+
* selects that CLI, unless the session already chose one of its models. A session that
|
|
78
|
+
* opens on a CLI counts as choosing it.
|
|
79
|
+
* @param agent - the agent being announced or switched.
|
|
80
|
+
*/
|
|
81
|
+
routeAgent(agent: Agent): void;
|
|
82
|
+
/**
|
|
83
|
+
* Follow one session log event. Tracking turns live is what keeps a prompt from
|
|
84
|
+
* being sent twice: a failed turn leaves its message at the end of the history, and
|
|
85
|
+
* only messages logged since the latest `turn/start` count as new. That holds across
|
|
86
|
+
* a closed session, a replaced process, and a restart, with nothing extra to persist.
|
|
87
|
+
* @param sessionId - the harness session.
|
|
88
|
+
* @param event - the logged event.
|
|
89
|
+
*/
|
|
90
|
+
observeEvent(sessionId: string, event: ObservedEvent): void;
|
|
91
|
+
/**
|
|
92
|
+
* Read which user messages belong to a session's open turn.
|
|
93
|
+
* @param sessionId - the harness session.
|
|
94
|
+
* @returns their ids; empty outside a turn.
|
|
95
|
+
*/
|
|
96
|
+
currentTurnMessageIds(sessionId: string): ReadonlySet<string>;
|
|
97
|
+
/**
|
|
98
|
+
* Find or create the CLI session for a harness session.
|
|
99
|
+
* @param provider - the requested CLI route.
|
|
100
|
+
* @param sessionId - the harness session.
|
|
101
|
+
* @returns a CLI session; a dead one is returned until its failure has been read.
|
|
102
|
+
*/
|
|
103
|
+
resolveSession(provider: string, sessionId: string): CliSession;
|
|
104
|
+
/**
|
|
105
|
+
* Wait for the CLI's result of one mirrored call.
|
|
106
|
+
* @param exec - the tool execution; its call id identifies the CLI call.
|
|
107
|
+
* @returns the finished call's outcome.
|
|
108
|
+
*/
|
|
109
|
+
waitForTool(exec: ToolRunContext): Promise<ToolOutcome>;
|
|
110
|
+
/**
|
|
111
|
+
* Forget a harness session and stop its CLI, if it has one.
|
|
112
|
+
* @param sessionId - the harness session.
|
|
113
|
+
*/
|
|
114
|
+
disposeSession(sessionId: string): void;
|
|
115
|
+
/**
|
|
116
|
+
* Stop every CLI.
|
|
117
|
+
* @param reason - why, shown to any turn this cuts off.
|
|
118
|
+
*/
|
|
119
|
+
disposeAll(reason?: string): void;
|
|
120
|
+
private noticeSelection;
|
|
121
|
+
private recordTurnMessage;
|
|
122
|
+
/** Stop the process only; the harness session and its open turn carry on. */
|
|
123
|
+
private stopSession;
|
|
124
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps harness sessions to their live CLI sessions, and follows each session's log
|
|
3
|
+
* to know where its turns begin and end. It holds no framework state, so `index.ts`
|
|
4
|
+
* can stay plain wiring.
|
|
5
|
+
* @module dsh-ccgui/session-registry
|
|
6
|
+
*/
|
|
7
|
+
import { LlmError } from '@deepseek-ai/dsh-llm';
|
|
8
|
+
import { CLI_TOOL_NAME, cliRoute, DEFAULT_MODEL_ID } from "./constants.js";
|
|
9
|
+
const NO_MESSAGES = new Set();
|
|
10
|
+
/** This preset's tool executor only accepts `run_code`, so it refuses every mirrored call. */
|
|
11
|
+
const PTC_PRESET = 'ptc';
|
|
12
|
+
function without(map, key) {
|
|
13
|
+
return new Map([...map].filter(([candidate]) => candidate !== key));
|
|
14
|
+
}
|
|
15
|
+
/** Owns every live CLI session in this process. */
|
|
16
|
+
export class CliSessionRegistry {
|
|
17
|
+
deps;
|
|
18
|
+
agentsByProvider;
|
|
19
|
+
sessions = new Map();
|
|
20
|
+
stopping = new Map();
|
|
21
|
+
turnMessages = new Map();
|
|
22
|
+
constructor(agents, deps) {
|
|
23
|
+
this.deps = deps;
|
|
24
|
+
this.agentsByProvider = new Map(agents.map((agent) => [cliRoute(agent.id), agent]));
|
|
25
|
+
}
|
|
26
|
+
/** Provider routes of all configured CLIs. */
|
|
27
|
+
get providers() {
|
|
28
|
+
return [...this.agentsByProvider.keys()];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Read a route's display name.
|
|
32
|
+
* @param provider - a provider route.
|
|
33
|
+
* @returns the CLI's name, or undefined for any other route.
|
|
34
|
+
*/
|
|
35
|
+
labelFor(provider) {
|
|
36
|
+
return this.agentsByProvider.get(provider)?.name;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Check whether a provider route is one of the configured CLIs.
|
|
40
|
+
* @param provider - a provider route.
|
|
41
|
+
* @returns whether it is.
|
|
42
|
+
*/
|
|
43
|
+
isCliRoute(provider) {
|
|
44
|
+
return this.agentsByProvider.has(provider);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Read the CLI behind a route.
|
|
48
|
+
* @param provider - a provider route.
|
|
49
|
+
* @returns its configuration, if it is a CLI.
|
|
50
|
+
*/
|
|
51
|
+
configFor(provider) {
|
|
52
|
+
return this.agentsByProvider.get(provider);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Point an agent at the CLI its preset stands for. A preset whose id is a CLI route
|
|
56
|
+
* selects that CLI, unless the session already chose one of its models. A session that
|
|
57
|
+
* opens on a CLI counts as choosing it.
|
|
58
|
+
* @param agent - the agent being announced or switched.
|
|
59
|
+
*/
|
|
60
|
+
routeAgent(agent) {
|
|
61
|
+
const preset = this.deps.composedPreset(agent);
|
|
62
|
+
if (preset !== undefined && this.agentsByProvider.has(preset) && agent.model.current()?.provider !== preset) {
|
|
63
|
+
agent.model.select({ provider: preset, model: DEFAULT_MODEL_ID });
|
|
64
|
+
}
|
|
65
|
+
this.noticeSelection(agent.model.current()?.provider);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Follow one session log event. Tracking turns live is what keeps a prompt from
|
|
69
|
+
* being sent twice: a failed turn leaves its message at the end of the history, and
|
|
70
|
+
* only messages logged since the latest `turn/start` count as new. That holds across
|
|
71
|
+
* a closed session, a replaced process, and a restart, with nothing extra to persist.
|
|
72
|
+
* @param sessionId - the harness session.
|
|
73
|
+
* @param event - the logged event.
|
|
74
|
+
*/
|
|
75
|
+
observeEvent(sessionId, event) {
|
|
76
|
+
switch (event.type) {
|
|
77
|
+
case 'turn/start':
|
|
78
|
+
this.turnMessages = new Map([...this.turnMessages, [sessionId, new Set()]]);
|
|
79
|
+
return;
|
|
80
|
+
case 'user/message':
|
|
81
|
+
this.recordTurnMessage(sessionId, event.data?.id);
|
|
82
|
+
return;
|
|
83
|
+
case 'turn/end':
|
|
84
|
+
this.turnMessages = without(this.turnMessages, sessionId);
|
|
85
|
+
this.sessions.get(sessionId)?.session.abandonTurn();
|
|
86
|
+
return;
|
|
87
|
+
case 'model/selection':
|
|
88
|
+
this.noticeSelection(event.data?.provider);
|
|
89
|
+
return;
|
|
90
|
+
default:
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Read which user messages belong to a session's open turn.
|
|
95
|
+
* @param sessionId - the harness session.
|
|
96
|
+
* @returns their ids; empty outside a turn.
|
|
97
|
+
*/
|
|
98
|
+
currentTurnMessageIds(sessionId) {
|
|
99
|
+
return this.turnMessages.get(sessionId) ?? NO_MESSAGES;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Find or create the CLI session for a harness session.
|
|
103
|
+
* @param provider - the requested CLI route.
|
|
104
|
+
* @param sessionId - the harness session.
|
|
105
|
+
* @returns a CLI session; a dead one is returned until its failure has been read.
|
|
106
|
+
*/
|
|
107
|
+
resolveSession(provider, sessionId) {
|
|
108
|
+
const config = this.agentsByProvider.get(provider);
|
|
109
|
+
if (config === undefined)
|
|
110
|
+
throw new LlmError(`未知的 CLI 路由:${provider}`, 'CLI_START_FAILED');
|
|
111
|
+
const agent = this.deps.getAgent(sessionId);
|
|
112
|
+
if (agent === undefined)
|
|
113
|
+
throw new LlmError(`找不到会话 ${sessionId}。`, 'CLI_START_FAILED');
|
|
114
|
+
if (this.deps.composedPreset(agent) === PTC_PRESET) {
|
|
115
|
+
throw new LlmError(`${config.name} 不能在 PTC 模式下运行,请切回标准模式。`, 'CLI_WRONG_PRESET');
|
|
116
|
+
}
|
|
117
|
+
const current = this.sessions.get(sessionId);
|
|
118
|
+
if (current?.provider === provider && !current.session.replaceable)
|
|
119
|
+
return current.session;
|
|
120
|
+
this.stopSession(sessionId);
|
|
121
|
+
const { cwd } = agent.session.header;
|
|
122
|
+
if (cwd === undefined)
|
|
123
|
+
throw new LlmError(`${config.name} 需要工作目录,但这个会话没有。`, 'CLI_START_FAILED');
|
|
124
|
+
const session = this.deps.createSession(config, sessionId, cwd, {
|
|
125
|
+
sentMessageIds: current?.session.sentMessageIds ?? NO_MESSAGES,
|
|
126
|
+
after: this.stopping.get(sessionId) ?? Promise.resolve(),
|
|
127
|
+
});
|
|
128
|
+
this.sessions = new Map([...this.sessions, [sessionId, { provider, session }]]);
|
|
129
|
+
return session;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Wait for the CLI's result of one mirrored call.
|
|
133
|
+
* @param exec - the tool execution; its call id identifies the CLI call.
|
|
134
|
+
* @returns the finished call's outcome.
|
|
135
|
+
*/
|
|
136
|
+
async waitForTool(exec) {
|
|
137
|
+
const { agent, callId, signal } = exec;
|
|
138
|
+
const session = agent === undefined ? undefined : this.sessions.get(agent.id)?.session;
|
|
139
|
+
if (agent === undefined || session === undefined) {
|
|
140
|
+
throw new Error(`${CLI_TOOL_NAME} 只记录 CLI 发出的调用,不能直接使用。`);
|
|
141
|
+
}
|
|
142
|
+
return session.waitForTool(callId, (request, askSignal) => this.deps.ask(agent, request, askSignal), signal);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Forget a harness session and stop its CLI, if it has one.
|
|
146
|
+
* @param sessionId - the harness session.
|
|
147
|
+
*/
|
|
148
|
+
disposeSession(sessionId) {
|
|
149
|
+
this.turnMessages = without(this.turnMessages, sessionId);
|
|
150
|
+
this.stopSession(sessionId);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Stop every CLI.
|
|
154
|
+
* @param reason - why, shown to any turn this cuts off.
|
|
155
|
+
*/
|
|
156
|
+
disposeAll(reason) {
|
|
157
|
+
for (const sessionId of this.sessions.keys()) {
|
|
158
|
+
this.turnMessages = without(this.turnMessages, sessionId);
|
|
159
|
+
this.stopSession(sessionId, reason);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
noticeSelection(provider) {
|
|
163
|
+
if (typeof provider === 'string' && this.agentsByProvider.has(provider))
|
|
164
|
+
this.deps.onCliSelected(provider);
|
|
165
|
+
}
|
|
166
|
+
recordTurnMessage(sessionId, id) {
|
|
167
|
+
const messages = this.turnMessages.get(sessionId);
|
|
168
|
+
if (messages === undefined || typeof id !== 'string')
|
|
169
|
+
return;
|
|
170
|
+
this.turnMessages = new Map([...this.turnMessages, [sessionId, new Set([...messages, id])]]);
|
|
171
|
+
}
|
|
172
|
+
/** Stop the process only; the harness session and its open turn carry on. */
|
|
173
|
+
stopSession(sessionId, reason) {
|
|
174
|
+
const live = this.sessions.get(sessionId);
|
|
175
|
+
if (live === undefined)
|
|
176
|
+
return;
|
|
177
|
+
this.sessions = without(this.sessions, sessionId);
|
|
178
|
+
const stoppedSelf = live.session.dispose(false, reason).catch((error) => {
|
|
179
|
+
this.deps.warn(`ccgui: failed to stop ${live.provider}: ${String(error)}`);
|
|
180
|
+
});
|
|
181
|
+
// Chained so that quick replacements still wait for the oldest process, not
|
|
182
|
+
// only the latest one, which may never have started.
|
|
183
|
+
const stopped = Promise.all([this.stopping.get(sessionId), stoppedSelf]).then(() => {
|
|
184
|
+
if (this.stopping.get(sessionId) === stopped)
|
|
185
|
+
this.stopping = without(this.stopping, sessionId);
|
|
186
|
+
});
|
|
187
|
+
this.stopping = new Map([...this.stopping, [sessionId, stopped]]);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP over a child process's stdio, using the harness subprocess seam so the CLI
|
|
3
|
+
* gets the shared credential scrub and managed teardown. Process teardown and SDK
|
|
4
|
+
* wiring are adapted from `@deepseek-ai/dsh-subagent-acp`; see THIRD_PARTY_NOTICES.md.
|
|
5
|
+
* @module dsh-ccgui/stdio-transport
|
|
6
|
+
*/
|
|
7
|
+
import type { SubprocessHandle, SubprocessSpawnSpec } from '@deepseek-ai/dsh-subprocess';
|
|
8
|
+
import type { AcpTransport, TransportHandlers } from './transport.ts';
|
|
9
|
+
/** How to start one CLI. */
|
|
10
|
+
export interface StdioTransportSpec {
|
|
11
|
+
readonly argv: readonly string[];
|
|
12
|
+
readonly cwd: string;
|
|
13
|
+
/** Added to the seam's scrubbed environment; the place for a CLI's own credentials. */
|
|
14
|
+
readonly env: Readonly<Record<string, string>>;
|
|
15
|
+
/** Signal-escalation grace in milliseconds. */
|
|
16
|
+
readonly graceMs: number;
|
|
17
|
+
/** Time after stdin EOF for the CLI to flush and exit by itself, in milliseconds. */
|
|
18
|
+
readonly eofGraceMs: number;
|
|
19
|
+
readonly spawn: (spec: SubprocessSpawnSpec) => SubprocessHandle;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Start a CLI and connect to it. The returned operations reject once the process
|
|
23
|
+
* is gone, because a request the CLI will never answer would otherwise hang.
|
|
24
|
+
* @param spec - the command and its process settings.
|
|
25
|
+
* @param handlers - callbacks from the CLI.
|
|
26
|
+
* @returns the connected transport.
|
|
27
|
+
*/
|
|
28
|
+
export declare function openStdioTransport(spec: StdioTransportSpec, handlers: TransportHandlers): AcpTransport;
|