Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/acp/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@

import { each, race, stream, until } from "effection";
import type { Channel, Operation } from "effection";
import type { AgentPromptCheckpoint, AgentPromptEvent, Session } from "@executablemd/agent/api";
import type {
AgentPromptCheckpoint,
AgentPromptEvent,
Session,
SessionConfiguration,
} from "@executablemd/agent/api";
import type { AcpRuntimeTurn, AcpRuntimeTurnResult } from "./acpx-runtime.ts";
import { checkpointFromResult } from "./checkpoint.ts";
import { XmdResponse, XMD_RESPONSE, xmdPromptEvent } from "./xmd-response.ts";
Expand All @@ -34,6 +39,7 @@ import type { TurnUsage } from "./usage.ts";
export interface TurnIdentity {
agent: string;
session: Session;
observedConfiguration?: SessionConfiguration | void;
}

/**
Expand Down Expand Up @@ -65,7 +71,14 @@ export function* consumeTurn(
closeFailed?: () => Operation<void>,
usage?: TurnUsage,
): Operation<void> {
yield* channel.send({ type: "started", agent: identity.agent, session: identity.session });
yield* channel.send({
type: "started",
agent: identity.agent,
session: identity.session,
...(identity.observedConfiguration === undefined
? {}
: { observedConfiguration: identity.observedConfiguration }),
});
let text = "";
let terminal: AgentPromptEvent;
let named: AgentPromptCheckpoint | undefined;
Expand Down
157 changes: 111 additions & 46 deletions packages/acp/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2887,7 +2887,20 @@ function* useAcpxProviderState(
return {
agent: agentName,
sessionKey,
readStatus: () => until(readStatus.call(runtime, { handle: entry.handle })),
verify: agentName === "claude" && configuration !== undefined,
readStatus: () =>
until(
readStatus.call(runtime, {
handle: entry.handle,
...(agentName === "claude" && configuration !== undefined
? {
verifyConfiguration: true,
...modelRequest(configuration),
...(configuration.effort === undefined ? {} : { inspectEffort: true }),
}
: {}),
}),
),
/**
* One write, and the Promise it started, owned by this operation.
*
Expand All @@ -2899,7 +2912,7 @@ function* useAcpxProviderState(
* yields, so restoration cannot begin until this exact write is done
* moving the conversation.
*/
write(optionId, value) {
write(optionId, value, restoreContextWindow) {
return scoped(function* (): Operation<void> {
if (writeOption === undefined) {
throw new AcpConfigurationError(
Expand All @@ -2925,6 +2938,7 @@ function* useAcpxProviderState(
handle: entry.handle,
key: optionId,
value,
...(restoreContextWindow === undefined ? {} : { restoreContextWindow }),
// The same attempt's requested model for ordinary and restoring
// writes alike: either may be the one that reconnects.
...modelRequest(configuration),
Expand Down Expand Up @@ -2990,10 +3004,16 @@ function* useAcpxProviderState(
configuration,
);
return {
...(configuration.model === undefined ? {} : { model: selectors.model?.set.selected ?? "" }),
...(configuration.model === undefined
? {}
: {
model: selectors.native?.matches
? configuration.model
: (selectors.model?.set.selected ?? ""),
}),
...(configuration.effort === undefined
? {}
: { effort: selectors.effort?.set.selected ?? "" }),
: { effort: selectors.native?.effort ?? selectors.effort?.set.selected ?? "" }),
};
}

Expand Down Expand Up @@ -3195,7 +3215,8 @@ function* useAcpxProviderState(
// under the wrong model is a turn that cannot be taken back, so the
// values are applied and verified first and the turn only follows a
// configuration this provider put into force.
if (desired !== undefined) {
const verifyAtDispatch = desired !== undefined && agentName === "claude";
if (desired !== undefined && !verifyAtDispatch) {
yield* configureSession(agentName, entry, desired);
}

Expand Down Expand Up @@ -3232,37 +3253,38 @@ function* useAcpxProviderState(
// The runtime that made this handle, never "the" runtime: a turn
// started through another partition would be a second child in a
// conversation the first already owns.
const turn = entry.runtime.runtime.startTurn({
handle: entry.handle,
text: content,
...(options?.images === undefined ? {} : { attachments: [...options.images] }),
mode: "prompt",
requestId,
timeoutMs,
...modelRequest(desired),
...(callbacks === undefined
? {}
: {
onElicitation: (request, context) =>
scope
.run(function* (): Operation<
Result<import("./acpx-runtime.ts").AcpElicitationResponse>
> {
try {
return Ok(yield* callbacks.elicitation(request, context.signal));
} catch (error) {
return Err(toError(error));
}
})
.then((result) => {
if (!result.ok) {
throw result.error;
}
return result.value;
}),
}),
});
activeTurns.add(turn);
const start = () =>
entry.runtime.runtime.startTurn({
handle: entry.handle,
text: content,
...(options?.images === undefined ? {} : { attachments: [...options.images] }),
mode: "prompt",
requestId,
timeoutMs,
...modelRequest(desired),
...(desired?.effort === undefined ? {} : { requestedEffort: desired.effort }),
...(callbacks === undefined
? {}
: {
onElicitation: (request, context) =>
scope
.run(function* (): Operation<
Result<import("./acpx-runtime.ts").AcpElicitationResponse>
> {
try {
return Ok(yield* callbacks.elicitation(request, context.signal));
} catch (error) {
return Err(toError(error));
}
})
.then((result) => {
if (!result.ok) {
throw result.error;
}
return result.value;
}),
}),
});
const usage: TurnUsage | undefined =
reportUsage === undefined || promptUsageHost === undefined
? undefined
Expand All @@ -3280,8 +3302,13 @@ function* useAcpxProviderState(
},
}),
});
const dispatched: { turn?: AcpRuntimeTurn } = {};
let completed = false;
yield* ensure(function* () {
const turn = dispatched.turn;
if (turn === undefined) {
return;
}
activeTurns.delete(turn);
if (!completed) {
try {
Expand All @@ -3292,9 +3319,7 @@ function* useAcpxProviderState(
}
});

// Registered after the turn exists and before its events are
// consumed, so a permission request arriving mid-turn reaches the
// turn it belongs to and nothing else.
// Register before dispatch so the first permission request reaches its turn.
const denials = { denied: false };
if (strict) {
const sessionKey = entry.session.sessionKey;
Expand All @@ -3306,18 +3331,58 @@ function* useAcpxProviderState(
});
}

let observedConfiguration: SessionConfiguration | void = undefined;
const awaitsMaterialization =
constructing &&
adapterFor(agentName)?.acceptance === "first-turn-acceptance" &&
prepared.kind === "details";
function startTracked(): AcpRuntimeTurn {
const turn = start();
dispatched.turn = turn;
activeTurns.add(turn);
return turn;
}
function* dispatch(): Operation<void> {
yield* scoped(function* () {
const turn = startTracked();
let accepted = false;
yield* ensure(function* () {
if (accepted) {
return;
}
yield* until(turn.cancel());
yield* until(turn.result);
completed = true;
});
try {
observedConfiguration = yield* until(turn.promptStarted);
} catch (error) {
throw sessionResumeFailure(error, entry.session.sessionKey);
}
accepted = true;
});
}
if (desired !== undefined && verifyAtDispatch) {
yield* applySessionConfiguration(
configurationTarget(agentName, entry, true, desired),
desired,
dispatch,
);
} else if (awaitsMaterialization) {
startTracked();
} else {
yield* dispatch();
}
const turn = dispatched.turn;
if (turn === undefined) {
throw new Error("The prompt dispatch did not produce a turn.");
}
if (
constructing &&
adapterFor(agentName)?.acceptance === "first-turn-acceptance" &&
prepared.kind === "details"
) {
yield* materialize(entry, prepared.details, turn, usage);
} else {
try {
yield* until(turn.promptStarted);
} catch (error) {
throw sessionResumeFailure(error, entry.session.sessionKey);
}
}

const channel = createChannel<AgentPromptEvent, string>();
Expand All @@ -3331,7 +3396,7 @@ function* useAcpxProviderState(
// under. A record is written from this, so it has to be what
// actually ran rather than what an element once held. A name
// resolves to the session it named, which carries no settings.
{ agent: agentName, session: ranIn ?? entry.session },
{ agent: agentName, session: ranIn ?? entry.session, observedConfiguration },
channel,
() => {
completed = true;
Expand Down
Loading
Loading