| 1 | import type { AppBindings } from "./bridge"; |
| 2 | import type { StructuredInvocationSubmit } from "./invocationDisplay"; |
| 3 | import type { CollaborationMode, ToolApprovalMode } from "./types"; |
| 4 | import { submitAttachmentTurn } from "./attachmentSubmit"; |
| 5 | import { draftSubmit } from "./draftSubmit"; |
| 6 | import { isCompactSubmission } from "./sessionMaintenanceOperation"; |
| 7 | |
| 8 | export type ManagementReceipt = { operationId?: string; errorCode?: string }; |
| 9 | type SubmitOutcome = [0] | [1, string[]] | [2, ManagementReceipt] | [3, string]; |
| 10 | |
| 11 | export async function submitTurn( |
| 12 | app: AppBindings, |
| 13 | tabId: string, |
| 14 | submissionId: string, |
| 15 | display: string, |
| 16 | submit: string, |
| 17 | original: string, |
| 18 | structured?: StructuredInvocationSubmit, |
| 19 | initialGoal?: { goal: string; collaborationMode: CollaborationMode; toolApprovalMode: ToolApprovalMode }, |
| 20 | ): Promise<SubmitOutcome> { |
| 21 | let receipt: unknown; |
| 22 | if (structured?.modelApplicationChoice) { |
| 23 | if (!app.StartTurnWithModelApplication) throw new Error("Model application choice is unsupported by this host"); |
| 24 | const result=await app.StartTurnWithModelApplication(tabId,submissionId,{ |
| 25 | input:structured.input,display:structured.display,original, |
| 26 | goal:initialGoal?.goal,toolApprovalMode:initialGoal?.toolApprovalMode, |
| 27 | invocations:structured.invocations,attachments:structured.attachments ?? [], |
| 28 | },structured.modelApplicationChoice); |
| 29 | return initialGoal ? [1, []] : [3,result.turnId]; |
| 30 | } |
| 31 | // Management commands have no durable user row to edit. Preserve the actual |
| 32 | // instructions (including expanded paste blocks) through the typed admission. |
| 33 | if (isCompactSubmission(submit, structured, initialGoal)) receipt = typeof app.StartTurnForTab === "function" |
| 34 | ? await app.StartTurnForTab(tabId, submit, submissionId) |
| 35 | : await app.SubmitToTabWithID(tabId, submit, submissionId); |
| 36 | else if (structured?.attachments?.length) receipt = await submitAttachmentTurn(app, submissionId, structured, original, initialGoal); |
| 37 | else if (initialGoal) { |
| 38 | receipt = await app.SubmitInitialGoalToTabWithID( |
| 39 | tabId, |
| 40 | initialGoal.goal, |
| 41 | structured?.display.trim() || display, |
| 42 | structured?.input.trim() || submit, |
| 43 | structured?.invocations ?? [], |
| 44 | initialGoal.collaborationMode, |
| 45 | initialGoal.toolApprovalMode, |
| 46 | submissionId, |
| 47 | ); |
| 48 | } else if (structured) receipt = await app.SubmitInvocationsToTabWithID(tabId, structured.display.trim(), structured.input.trim(), structured.invocations, submissionId); |
| 49 | else if (original) receipt = await app.SubmitEditedDisplayToTabWithID(tabId, display, submit, original, submissionId); |
| 50 | else if (display !== submit) receipt = await app.SubmitDisplayToTabWithID(tabId, display, submit, submissionId); |
| 51 | else receipt = await draftSubmit(app, tabId, submit, submissionId); |
| 52 | if (initialGoal) return [1, Array.isArray(receipt) ? receipt : []]; |
| 53 | if (receipt && typeof receipt === "object" && "disposition" in receipt && receipt.disposition === "management_handled") return [2, { |
| 54 | operationId: "operationId" in receipt && typeof receipt.operationId === "string" ? receipt.operationId : undefined, |
| 55 | errorCode: "managementErrorCode" in receipt && typeof receipt.managementErrorCode === "string" ? receipt.managementErrorCode : undefined, |
| 56 | }]; |
| 57 | if (receipt && typeof receipt === "object" && "turnId" in receipt && typeof receipt.turnId === "string") return [3, receipt.turnId]; |
| 58 | return [0]; |
| 59 | } |
| 60 |