|
7 | 7 | * parent tool.boundary. Leftover steers at idle, idle-with-fleet, or |
8 | 8 | * post-interrupt share the send path (sendQueue, inFlight, token refresh). |
9 | 9 | */ |
10 | | -import { AgentClosedError } from "@intx/agent"; |
11 | 10 | import type { PendingImageAttachment } from "./image-attachments.js"; |
| 11 | +import type { AgentDeliveryResult } from "./deliver-agent-message.js"; |
12 | 12 | import type { ProductHostDeliver } from "./product-host.js"; |
13 | 13 | import { ASK_DIRECTOR_WAKE_PREFIX } from "../subagent/fleet-report.js"; |
14 | 14 | import { MAILBOX_MAIL_WAKE_PREFIX } from "../subagent/mailbox-mail-drive.js"; |
15 | 15 |
|
16 | | -export type AgentDeliveryNotDeliveredReason = |
17 | | - | "agent-closed" |
18 | | - | "session-unavailable" |
19 | | - | "superseded" |
20 | | - | "preparation-failed"; |
21 | | - |
22 | | -export type AgentDeliveryResult = |
23 | | - | { readonly status: "accepted" } |
24 | | - | { |
25 | | - readonly status: "not-delivered"; |
26 | | - readonly reason: AgentDeliveryNotDeliveredReason; |
27 | | - readonly detail: string; |
28 | | - } |
29 | | - | { |
30 | | - readonly status: "uncertain"; |
31 | | - readonly detail: string; |
32 | | - }; |
33 | | - |
34 | | -export interface DeliverAgentMessageDeps { |
35 | | - getFatalBuildError: () => Error | null; |
36 | | - deliverToLiveAgent: () => void; |
37 | | -} |
38 | | - |
39 | | -/** |
40 | | - * Guards a queued/steer deliver against a mid-rebuild or closed agent. The |
41 | | - * shell paints the delivered row and pops the queue item before this runs, so |
42 | | - * the caller must settle ownership from the structured result. |
43 | | - */ |
44 | | -export async function deliverAgentMessage( |
45 | | - deps: DeliverAgentMessageDeps, |
46 | | -): Promise<AgentDeliveryResult> { |
47 | | - const fatal = deps.getFatalBuildError(); |
48 | | - if (fatal !== null) { |
49 | | - return { |
50 | | - status: "not-delivered", |
51 | | - reason: "session-unavailable", |
52 | | - detail: fatal.message, |
53 | | - }; |
54 | | - } |
55 | | - try { |
56 | | - deps.deliverToLiveAgent(); |
57 | | - return { status: "accepted" }; |
58 | | - } catch (err) { |
59 | | - if (err instanceof AgentClosedError) { |
60 | | - return { |
61 | | - status: "not-delivered", |
62 | | - reason: "agent-closed", |
63 | | - detail: err.message, |
64 | | - }; |
65 | | - } |
66 | | - return { |
67 | | - status: "uncertain", |
68 | | - detail: err instanceof Error ? err.message : String(err), |
69 | | - }; |
70 | | - } |
71 | | -} |
72 | | - |
73 | | -/** Operator-facing copy for a settled delivery that did not accept. */ |
74 | | -export function deliveryResultNotice( |
75 | | - result: Exclude<AgentDeliveryResult, { status: "accepted" }>, |
76 | | - disposition: "restored" | "deferred" | "none" = "none", |
77 | | -): string { |
78 | | - if (result.status === "uncertain") { |
79 | | - const base = `Delivery failed: ${result.detail}. Delivery status is uncertain; review the transcript before sending again.`; |
80 | | - return appendDisposition(base, disposition); |
81 | | - } |
82 | | - if (result.reason === "agent-closed") { |
83 | | - if (disposition === "restored") { |
84 | | - return "Message not delivered because the agent closed. It is back in the prompt; press Enter to send it."; |
85 | | - } |
86 | | - if (disposition === "deferred") { |
87 | | - return "Message not delivered because the agent closed. Your current draft is unchanged; the message will return to the prompt after you send it."; |
88 | | - } |
89 | | - return "Message not delivered because the agent closed."; |
90 | | - } |
91 | | - const base = `Message not delivered: ${result.detail}`; |
92 | | - return appendDisposition(base, disposition); |
93 | | -} |
94 | | - |
95 | | -function appendDisposition( |
96 | | - base: string, |
97 | | - disposition: "restored" | "deferred" | "none", |
98 | | -): string { |
99 | | - if (disposition === "restored") { |
100 | | - return `${base} It is back in the prompt; press Enter to send it.`; |
101 | | - } |
102 | | - if (disposition === "deferred") { |
103 | | - return `${base} Your current draft is unchanged; the message will return to the prompt after you send it.`; |
104 | | - } |
105 | | - return base; |
106 | | -} |
107 | | - |
108 | 16 | export type DeliverySettle = (result: AgentDeliveryResult) => void; |
109 | 17 | type MaybeAsyncDeliveryResult = |
110 | 18 | | Promise<AgentDeliveryResult> |
|
0 commit comments