feat: add verified desktop ChatGPT subscription connection
This commit is contained in:
@@ -2,6 +2,7 @@ import type { Agent, AgentEvent, AgentMessage } from "@earendil-works/pi-agent-c
|
||||
import type { SteerMessage } from "../../core/contracts.ts";
|
||||
import { noteSteerVisible } from "../jobs.ts";
|
||||
import { ToolCallIdUniquifier } from "./call-ids.ts";
|
||||
import type { ChatGPTRequestCycle } from "../../agent/chatgpt-request-cycle.ts";
|
||||
|
||||
/**
|
||||
* THE NATIVE WIRE (Wave D, owner-approved 2026-09-26): one agent turn, streamed
|
||||
@@ -71,6 +72,7 @@ export type TurnWireEvent =
|
||||
| { type: "error"; errorText: string };
|
||||
|
||||
export interface PiTurnSpec {
|
||||
requestCycle?: ChatGPTRequestCycle;
|
||||
/** The per-turn agent (stateless engine — built from the record). */
|
||||
agent: Agent;
|
||||
/** The handler's error-masking policy for LOOP-level failures (the
|
||||
@@ -117,7 +119,7 @@ export interface PiTurnHandle {
|
||||
}
|
||||
|
||||
export function runPiTurn(spec: PiTurnSpec): PiTurnHandle {
|
||||
const { agent, onError, onFinish, usedToolCallIds, updateFlushMs = 100 } = spec;
|
||||
const { agent, onError, onFinish, usedToolCallIds, updateFlushMs = 100, requestCycle } = spec;
|
||||
const uniquifier = usedToolCallIds ? new ToolCallIdUniquifier(usedToolCallIds) : undefined;
|
||||
|
||||
let settled = false;
|
||||
@@ -248,11 +250,24 @@ export function runPiTurn(spec: PiTurnSpec): PiTurnHandle {
|
||||
settleWith();
|
||||
}
|
||||
};
|
||||
agent.subscribe(handle);
|
||||
requestCycle?.bindRelease(handle);
|
||||
agent.subscribe((event) => {
|
||||
if (!requestCycle?.hold(event)) handle(event);
|
||||
});
|
||||
|
||||
void (async () => {
|
||||
try {
|
||||
await agent.continue();
|
||||
do {
|
||||
await agent.continue();
|
||||
if (!requestCycle) break;
|
||||
const denied = requestCycle.deniedBeforeDispatch;
|
||||
const outcome = requestCycle.finishNativeCycle();
|
||||
if (denied) agent.state.messages = outcome.messages;
|
||||
if (outcome.action !== "resumeForeground") {
|
||||
handle({ type: "agent_end", messages: [...agent.state.messages] });
|
||||
break;
|
||||
}
|
||||
} while (!sawAbort);
|
||||
} catch (err) {
|
||||
// A loop-level failure (not a provider error — those ride the
|
||||
// stream as stopReason "error"): surface, close, persist. If the
|
||||
@@ -318,6 +333,7 @@ export function runPiTurn(spec: PiTurnSpec): PiTurnHandle {
|
||||
abort() {
|
||||
if (settled) return;
|
||||
sawAbort = true;
|
||||
requestCycle?.abort();
|
||||
agent.abort();
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user