codex@macbookpro
·
2026-03-27
router-pseudocode.ts
1import type { DeliveryPlan, ExecutionResult, NormalizedInstruction, RouteDecision, UploadReceipt } from "./types";
2
3export function computeDedupeKey(input: {
4 platform: string;
5 conversationId: string | null;
6 assistantMessageId: string | null;
7 blockIndex: number;
8 normalizedBlockText: string;
9}): string {
10 // pseudo: sha256(platform|conversationId|assistantMessageId|blockIndex|normalizedBlockText)
11 return "sha256:TODO";
12}
13
14export async function routeInstruction(inst: NormalizedInstruction): Promise<RouteDecision> {
15 if (["conductor", "system", "orchestrator"].includes(inst.target)) {
16 return {
17 resolvedTarget: "conductor",
18 routeReason: ["exact local target matched"],
19 fallbackCandidates: []
20 };
21 }
22
23 if (inst.target.startsWith("browser.")) {
24 return {
25 resolvedTarget: inst.target,
26 routeReason: ["platform bridge target matched"],
27 fallbackCandidates: []
28 };
29 }
30
31 if (inst.target.startsWith("pool.")) {
32 return {
33 resolvedTarget: "browser.chatgpt",
34 resolvedNodeId: "chatgpt.agent.beta",
35 routeReason: ["logical pool resolved by capability_match", "recent_success high"],
36 fallbackCandidates: ["browser.gemini"]
37 };
38 }
39
40 return {
41 resolvedTarget: "manual",
42 routeReason: ["no legal route found"],
43 fallbackCandidates: []
44 };
45}
46
47export async function executeNormalizedInstruction(inst: NormalizedInstruction): Promise<ExecutionResult> {
48 const route = await routeInstruction(inst);
49
50 if (route.resolvedTarget === "manual") {
51 return {
52 instructionId: inst.instructionId,
53 ok: false,
54 target: inst.target,
55 tool: inst.tool,
56 summary: "No legal route found.",
57 errorCode: "route_not_found",
58 deliveryMode: "inline"
59 };
60 }
61
62 return {
63 instructionId: inst.instructionId,
64 ok: true,
65 target: route.resolvedTarget,
66 tool: inst.tool,
67 summary: "Executed via resolved route.",
68 data: { routeReason: route.routeReason, fallbackCandidates: route.fallbackCandidates },
69 deliveryMode: "inline"
70 };
71}
72
73export function canInjectAfterReceipts(receipts: UploadReceipt[]): boolean {
74 return receipts.length > 0 && receipts.every(r => r.ok);
75}
76
77export function finalizeDelivery(plan: DeliveryPlan, receipts: UploadReceipt[]): { text: string; send: boolean } {
78 if (!canInjectAfterReceipts(receipts)) {
79 throw new Error("upload_not_confirmed");
80 }
81 return { text: plan.messageText, send: plan.autoSend };
82}