Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadFeedback.ts

75 lines2.3 KB

1import type { MessageId, 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 } : {};
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 }
46​
47export async function submitCodexFeedback<E>(input: {
48 readonly submission: CodexFeedbackSubmissionDetails;
49 readonly clearDraft: () => void;
50 readonly onUpdate: (submission: CodexFeedbackSubmission) => void;
51 readonly upload: () => Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>>;
52}): Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>> {
53 input.onUpdate({ ...input.submission, status: "uploading" });
54 input.clearDraft();
55​
56 const result = await input.upload();
57 if (result._tag === "Success") {
58 input.onUpdate({
59 ...input.submission,
60 status: "sent",
61 feedbackId: result.value.feedbackId,
62 });
63 } else if (isAtomCommandInterrupted(result)) {
64 input.onUpdate({ ...input.submission, status: "interrupted" });
65 } else {
66 const error = squashAtomCommandFailure(result);
67 input.onUpdate({
68 ...input.submission,
69 status: "failed",
70 errorMessage: error instanceof Error ? error.message : "An error occurred.",
71 });
72 }
73​
74 return result;