feat(#60): runEditTurn streams progress + accepts AbortSignal (INV-43/44/47)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+50
-11
@@ -9,6 +9,9 @@
|
||||
* never bundled (esbuild keeps it external).
|
||||
*/
|
||||
|
||||
import type { TurnProgressSnapshot } from "./turnProgress";
|
||||
import { createTurnProgressState, reduceTurnProgress } from "./turnProgress";
|
||||
|
||||
export interface EditTurnResult {
|
||||
replacement: string;
|
||||
model: string;
|
||||
@@ -16,6 +19,19 @@ export interface EditTurnResult {
|
||||
sessionId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for runEditTurn. Both new fields are purely additive observability /
|
||||
* control over the existing turn (INV-44): `onProgress` streams reduced progress
|
||||
* snapshots out; `signal` cancels the turn in. Neither touches the result path,
|
||||
* and neither pulls `vscode` into this module (INV-43 — `AbortSignal` is a web
|
||||
* standard, the progress snapshot is a domain type).
|
||||
*/
|
||||
export interface RunEditTurnOptions {
|
||||
modelId?: string;
|
||||
onProgress?: (snapshot: TurnProgressSnapshot) => void;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
const SYSTEM_PROMPT = [
|
||||
"You are a precise text editor embedded in VS Code.",
|
||||
"You will be given a piece of text and an instruction.",
|
||||
@@ -40,7 +56,7 @@ export function extractReplacement(outputText: string, selectedText: string): st
|
||||
export async function runEditTurn(
|
||||
instruction: string,
|
||||
selectedText: string,
|
||||
opts?: { modelId?: string },
|
||||
opts?: RunEditTurnOptions,
|
||||
): Promise<EditTurnResult> {
|
||||
const sdk = await import("@cline/sdk");
|
||||
const modelId = opts?.modelId ?? "sonnet";
|
||||
@@ -49,16 +65,39 @@ export async function runEditTurn(
|
||||
modelId,
|
||||
systemPrompt: SYSTEM_PROMPT,
|
||||
});
|
||||
const result = await agent.run(
|
||||
`<instruction>\n${instruction}\n</instruction>\n<text>\n${selectedText}\n</text>`,
|
||||
);
|
||||
// The SDK's AgentRunResult.status union is "completed" | "aborted" | "failed"
|
||||
// (@cline/shared agent.d.ts) — "completed" is the success status.
|
||||
if (result.status !== "completed") {
|
||||
throw new Error(
|
||||
`claude-code turn ${result.status}: ${result.error?.message ?? "unknown error"} ` +
|
||||
"(is Claude Code installed and signed in?)",
|
||||
|
||||
// Stream reduced progress snapshots out (INV-44 additive) and wire cancellation
|
||||
// in via the AbortSignal (INV-47). agent.subscribe returns its unsubscribe fn.
|
||||
let state = createTurnProgressState();
|
||||
const unsubscribe = opts?.onProgress
|
||||
? agent.subscribe((event) => {
|
||||
const next = reduceTurnProgress(state, event);
|
||||
state = next.state;
|
||||
if (next.snapshot) opts.onProgress!(next.snapshot);
|
||||
})
|
||||
: undefined;
|
||||
const onAbort = () => agent.abort();
|
||||
opts?.signal?.addEventListener("abort", onAbort);
|
||||
// Handle a signal that was already aborted before the turn started (the
|
||||
// "abort" event won't re-fire for an already-aborted signal).
|
||||
if (opts?.signal?.aborted) onAbort();
|
||||
|
||||
try {
|
||||
const result = await agent.run(
|
||||
`<instruction>\n${instruction}\n</instruction>\n<text>\n${selectedText}\n</text>`,
|
||||
);
|
||||
// The SDK's AgentRunResult.status union is "completed" | "aborted" | "failed"
|
||||
// (@cline/shared agent.d.ts) — "completed" is the success status. An aborted
|
||||
// turn (user cancel) falls into this throw; the call site reflects "cancelled".
|
||||
if (result.status !== "completed") {
|
||||
throw new Error(
|
||||
`claude-code turn ${result.status}: ${result.error?.message ?? "unknown error"} ` +
|
||||
"(is Claude Code installed and signed in?)",
|
||||
);
|
||||
}
|
||||
return { replacement: extractReplacement(result.outputText, selectedText), model: modelId, sessionId: result.runId };
|
||||
} finally {
|
||||
unsubscribe?.();
|
||||
opts?.signal?.removeEventListener("abort", onAbort);
|
||||
}
|
||||
return { replacement: extractReplacement(result.outputText, selectedText), model: modelId, sessionId: result.runId };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user