Open History to see the last commit
1import { PROVIDER_DISPLAY_NAMES, type ProviderDriverKind } from "@t3tools/contracts";
2import * as Predicate from "effect/Predicate";
3
4const GENERIC_ERROR_PREFIXES = [
5 "Failed to dispatch orchestration V2 command",
6 "Failed to dispatch orchestration command ",
7 "Provider adapter failed while dispatching orchestration command ",
8];
9
10const CAPABILITY_REJECTION_MESSAGES: Record<string, (provider: string) => string> = {
11 queued_messages: (p) => `${p} cannot queue messages behind an active run.`,
12 active_steering: (p) =>
13 `${p} cannot redirect an active run. Stop it first, then send the message.`,
14 interrupt_restart_steering: (p) =>
15 `${p} cannot redirect an active run. Stop it first, then send the message.`,
16 interrupt: (p) => `${p} cannot stop a run once it has started.`,
17 native_fork: (p) => `${p} cannot fork this thread natively.`,
18 fork_from_turn: (p) => `${p} cannot fork from an earlier point in this thread.`,
19 rollback: (p) =>
20 `${p} cannot rewind its conversation, so this checkpoint cannot be restored on this thread.`,
21 rollback_snapshot: (p) =>
22 `${p} did not report its rewound conversation state, so the checkpoint was not restored.`,
23 context_handoff: (p) => `${p} cannot receive the context handoff needed for this switch.`,
24 strong_terminal_status: (p) =>
25 `${p} cannot confirm when its runs finish reliably enough for this.`,
26};
27
28function providerDisplayName(instanceId: string): string {
29 const known = PROVIDER_DISPLAY_NAMES[instanceId as ProviderDriverKind];
30 if (known !== undefined) return known;
31 const trimmed = instanceId.replace(/Agent$/i, "").trim();
32 if (trimmed.length === 0) return instanceId;
33 return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
34}
35
36function policyRejectionMessage(value: unknown): string | undefined {
37 if (!Predicate.isObject(value) || typeof value.providerInstanceId !== "string") return undefined;
38 const provider = providerDisplayName(value.providerInstanceId);
39 if (value._tag === "CommandPolicyCapabilityUnsupportedError") {
40 const capability = typeof value.capability === "string" ? value.capability : "";
41 return CAPABILITY_REJECTION_MESSAGES[capability]?.(provider);
42 }
43 if (value._tag === "CommandPolicyUnsupportedError") {
44 return `${provider} cannot deliver a message that way right now.`;
45 }
46 return undefined;
47}
48
49function textValue(value: unknown): string | undefined {
50 return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
51}
52
53function messageFrom(value: unknown): string | undefined {
54 if (typeof value === "string") {
55 return textValue(value);
56 }
57 const friendly = policyRejectionMessage(value);
58 if (friendly !== undefined) {
59 return friendly;
60 }
61 if (value instanceof Error) {
62 return textValue(value.message);
63 }
64 if (!Predicate.isObject(value)) {
65 return undefined;
66 }
67 return textValue(value.detail) ?? textValue(value.message);
68}
69
70function isGenericErrorMessage(message: string): boolean {
71 return GENERIC_ERROR_PREFIXES.some((prefix) => message.startsWith(prefix));
72}
73
74function collectErrorMessages(value: unknown, seen: Set<unknown>): ReadonlyArray<string> {
75 if (value === undefined || value === null || seen.has(value)) {
76 return [];
77 }
78 seen.add(value);
79
80 if (Array.isArray(value)) {
81 return value.flatMap((item) => collectErrorMessages(item, seen));
82 }
83
84 const message = messageFrom(value);
85 if (!Predicate.isObject(value)) {
86 return message === undefined ? [] : [message];
87 }
88
89 const nested = [
90 ...collectErrorMessages(value.cause, seen),
91 ...collectErrorMessages(value.error, seen),
92 ...collectErrorMessages(value.errors, seen),
93 ];
94
95 return message === undefined ? nested : [message, ...nested];
96}
97
98export function userFacingDispatchErrorMessage(cause: unknown): string | undefined {
99 const messages = collectErrorMessages(cause, new Set()).filter(
100 (message, index, allMessages) =>
101 !isGenericErrorMessage(message) && allMessages.indexOf(message) === index,
102 );
103 return messages.at(-1);
104}