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 codexFeedbackMessage(
36 submission: CodexFeedbackSubmission,
37 role: "user" | "assistant" = "user",
38): OrchestrationMessage {
39 const text =
40 role === "user"
41 ? submission.command
42 : submission.status === "sent"
43 ? `Feedback sent to OpenAI.\n\nThread ID: \`${submission.feedbackId}\``
44 : submission.status === "failed"
45 ? `Could not send feedback to OpenAI.\n\n${submission.errorMessage}`
46 : "Sending feedback to OpenAI...";
47
48 return {
49 id: role === "user" ? submission.id : MessageId.make(`${submission.id}:feedback`),
50 role,
51 text,
52 turnId: null,
53 streaming: false,
54 createdAt: submission.createdAt,
55 updatedAt: submission.createdAt,
56 };
57}
58
59export async function submitCodexFeedback<E>(input: {
60 readonly submission: CodexFeedbackSubmissionDetails;
61 readonly clearDraft: () => void;
62 readonly onUpdate: (submission: CodexFeedbackSubmission) => void;
63 readonly upload: () => Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>>;
64}): Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>> {
65 input.onUpdate({ ...input.submission, status: "uploading" });
66 input.clearDraft();
67
68 const result = await input.upload();
69 if (result._tag === "Success") {
70 input.onUpdate({
71 ...input.submission,
72 status: "sent",
73 feedbackId: result.value.feedbackId,
74 });
75 } else if (isAtomCommandInterrupted(result)) {
76 input.onUpdate({ ...input.submission, status: "interrupted" });
77 } else {
78 const error = squashAtomCommandFailure(result);
79 input.onUpdate({
80 ...input.submission,
81 status: "failed",
82 errorMessage: error instanceof Error ? error.message : "An error occurred.",
83 });
84 }
85
86 return result;
87}