Open History to see the last commit
1import {
2 MessageId,
3 type OrchestrationMessage,
4 type ProviderUploadFeedbackResult,
5} from "@t3tools/contracts";
6
7import {
8 isAtomCommandInterrupted,
9 squashAtomCommandFailure,
10 type AtomCommandResult,
11} from "./runtime.ts";
12
13type CodexFeedbackSubmissionDetails = {
14 readonly id: MessageId;
15 readonly command: string;
16 readonly createdAt: string;
17};
18
19export type CodexFeedbackSubmission = CodexFeedbackSubmissionDetails &
20 (
21 | { readonly status: "uploading" | "interrupted" }
22 | { readonly status: "sent"; readonly feedbackId: string }
23 | { readonly status: "failed"; readonly errorMessage: string }
24 );
25
26export function parseCodexFeedbackCommand(text: string): { readonly reason?: string } | null {
27 const match = /^\/feedback(?:\s+([\s\S]*))?$/iu.exec(text.trim());
28 if (!match) {
29 return null;
30 }
31 const reason = match[1]?.trim();
32 return reason ? { reason } : {};
33}
34
35export function codexFeedbackNotice(submission: CodexFeedbackSubmission) {
36 switch (submission.status) {
37 case "interrupted":
38 return null;
39 case "uploading":
40 return { title: "Sending feedback to OpenAI...", description: undefined };
41 case "sent":
42 return {
43 title: "Feedback sent to OpenAI",
44 description: `Thread ID: ${submission.feedbackId}`,
45 };
46 case "failed":
47 return { title: "Could not send feedback to OpenAI", description: submission.errorMessage };
48 }
49}
50
51export function beginCodexFeedbackSubmission(
52 submissionsInFlight: Set<string>,
53 threadKey: string,
54): (() => void) | null {
55 if (submissionsInFlight.has(threadKey)) return null;
56 submissionsInFlight.add(threadKey);
57 return () => submissionsInFlight.delete(threadKey);
58}
59
60export function codexFeedbackMessage(
61 submission: CodexFeedbackSubmission,
62 role: "user" | "assistant" = "user",
63): OrchestrationMessage & { readonly role: "user" | "assistant" } {
64 const text =
65 role === "user"
66 ? submission.command
67 : submission.status === "sent"
68 ? `Feedback sent to OpenAI.\n\nThread ID: \`${submission.feedbackId}\``
69 : submission.status === "failed"
70 ? `Could not send feedback to OpenAI.\n\n${submission.errorMessage}`
71 : "Sending feedback to OpenAI...";
72
73 return {
74 id: role === "user" ? submission.id : MessageId.make(`${submission.id}:feedback`),
75 role,
76 text,
77 turnId: null,
78 streaming: false,
79 createdAt: submission.createdAt,
80 updatedAt: submission.createdAt,
81 };
82}
83
84export async function submitCodexFeedback<E>(input: {
85 readonly submission: CodexFeedbackSubmissionDetails;
86 readonly clearDraft: () => void;
87 readonly onUpdate: (submission: CodexFeedbackSubmission) => void;
88 readonly upload: () => Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>>;
89}): Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>> {
90 input.onUpdate({ ...input.submission, status: "uploading" });
91 input.clearDraft();
92
93 const result = await input.upload();
94 if (result._tag === "Success") {
95 input.onUpdate({
96 ...input.submission,
97 status: "sent",
98 feedbackId: result.value.feedbackId,
99 });
100 } else if (isAtomCommandInterrupted(result)) {
101 input.onUpdate({ ...input.submission, status: "interrupted" });
102 } else {
103 const error = squashAtomCommandFailure(result);
104 input.onUpdate({
105 ...input.submission,
106 status: "failed",
107 errorMessage: error instanceof Error ? error.message : "An error occurred.",
108 });
109 }
110
111 return result;
112}