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