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

threadFeedback.ts

130 lines3.8 KB

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