The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { thrownErrorMessage } from "../errors/translatable.ts"; |
| 2 | import { MessageId, type ProviderUploadFeedbackResult } from "@t3tools/contracts"; |
| 3 | |
| 4 | import { |
| 5 | isAtomCommandInterrupted, |
| 6 | squashAtomCommandFailure, |
| 7 | type AtomCommandResult, |
| 8 | } from "./runtime.ts"; |
| 9 | import { mFallback, msg } from "gt-i18n"; |
| 10 | import type { MFunctionType } from "gt-i18n/types"; |
| 11 | |
| 12 | type CodexFeedbackSubmissionDetails = { |
| 13 | readonly id: MessageId; |
| 14 | readonly command: string; |
| 15 | readonly createdAt: string; |
| 16 | }; |
| 17 | |
| 18 | export 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 | |
| 25 | export 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 } : {}; |
| 32 | } |
| 33 | |
| 34 | export function codexFeedbackNotice( |
| 35 | submission: CodexFeedbackSubmission, |
| 36 | resolve: MFunctionType = mFallback, |
| 37 | ) { |
| 38 | switch (submission.status) { |
| 39 | case "interrupted": |
| 40 | return null; |
| 41 | case "uploading": |
| 42 | return { title: resolve(msg("Sending feedback to OpenAI...")), description: undefined }; |
| 43 | case "sent": |
| 44 | return { |
| 45 | title: resolve(msg("Feedback sent to OpenAI")), |
| 46 | description: resolve(msg("Thread ID: {feedbackId}", { feedbackId: submission.feedbackId })), |
| 47 | }; |
| 48 | case "failed": |
| 49 | return { |
| 50 | title: resolve(msg("Could not send feedback to OpenAI")), |
| 51 | description: submission.errorMessage, |
| 52 | }; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | export 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 | |
| 66 | export function codexFeedbackMessage( |
| 67 | submission: CodexFeedbackSubmission, |
| 68 | role: "user" | "assistant" = "user", |
| 69 | resolve: MFunctionType = mFallback, |
| 70 | ): LocalChatMessage { |
| 71 | const text = |
| 72 | role === "user" |
| 73 | ? submission.command |
| 74 | : submission.status === "sent" |
| 75 | ? resolve( |
| 76 | msg("Feedback sent to OpenAI.\n\nThread ID: `{feedbackId}`", { |
| 77 | feedbackId: submission.feedbackId, |
| 78 | }), |
| 79 | ) |
| 80 | : submission.status === "failed" |
| 81 | ? resolve( |
| 82 | msg("Could not send feedback to OpenAI.\n\n{errorMessage}", { |
| 83 | errorMessage: submission.errorMessage, |
| 84 | }), |
| 85 | ) |
| 86 | : resolve(msg("Sending feedback to OpenAI...")); |
| 87 | |
| 88 | return { |
| 89 | id: role === "user" ? submission.id : MessageId.make(`${submission.id}:feedback`), |
| 90 | role, |
| 91 | text, |
| 92 | turnId: null, |
| 93 | streaming: false, |
| 94 | createdAt: submission.createdAt, |
| 95 | updatedAt: submission.createdAt, |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | export async function submitCodexFeedback<E>( |
| 100 | input: { |
| 101 | readonly submission: CodexFeedbackSubmissionDetails; |
| 102 | readonly clearDraft: () => void; |
| 103 | readonly onUpdate: (submission: CodexFeedbackSubmission) => void; |
| 104 | readonly upload: () => Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>>; |
| 105 | }, |
| 106 | resolve: MFunctionType = mFallback, |
| 107 | ): Promise<AtomCommandResult<ProviderUploadFeedbackResult, E>> { |
| 108 | input.onUpdate({ ...input.submission, status: "uploading" }); |
| 109 | input.clearDraft(); |
| 110 | |
| 111 | const result = await input.upload(); |
| 112 | if (result._tag === "Success") { |
| 113 | input.onUpdate({ |
| 114 | ...input.submission, |
| 115 | status: "sent", |
| 116 | feedbackId: result.value.feedbackId, |
| 117 | }); |
| 118 | } else if (isAtomCommandInterrupted(result)) { |
| 119 | input.onUpdate({ ...input.submission, status: "interrupted" }); |
| 120 | } else { |
| 121 | const error = squashAtomCommandFailure(result); |
| 122 | input.onUpdate({ |
| 123 | ...input.submission, |
| 124 | status: "failed", |
| 125 | errorMessage: |
| 126 | error instanceof Error |
| 127 | ? (thrownErrorMessage(error, resolve) ?? error.message) |
| 128 | : resolve(msg("An error occurred.")), |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | return result; |
| 133 | } |