@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 237 lines 7.85 KB
Raw
1 import {
2 ASSISTANT_CITATION_MAX_TEXT_LENGTH,
3 EnvironmentId,
4 MessageId,
5 PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
6 ThreadId,
7 } from "@t3tools/contracts";
8 import {
9 expandAssistantCitationsForProvider,
10 serializeAssistantCitation,
11 } from "@t3tools/shared/assistantCitations";
12 import { describe, expect, it, vi } from "vite-plus/test";
13
14 import { isComposerSubmissionBlocked, submitComposerDraft } from "./composerSubmission";
15
16 const assistantCitation = {
17 version: 1 as const,
18 environmentId: EnvironmentId.make("source-environment"),
19 threadId: ThreadId.make("source-thread"),
20 messageId: MessageId.make("source-message"),
21 text: "Keep the parser shared.",
22 start: 0,
23 end: 23,
24 prefix: "",
25 suffix: "",
26 };
27
28 describe("submitComposerDraft", () => {
29 it("keeps an oversized draft editable and sends a corrected follow-up", () => {
30 let draft = "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS + 1);
31 let validationMessage: string | null = null;
32 const dispatchedDrafts: string[] = [];
33 const preventDefault = vi.fn();
34
35 const submit = () => {
36 const result = submitComposerDraft({
37 prompt: draft,
38 submissionTarget: "provider-turn",
39 event: { preventDefault },
40 onSend: () => {
41 dispatchedDrafts.push(draft);
42 },
43 });
44 validationMessage = result.validationMessage;
45 };
46
47 submit();
48
49 expect(dispatchedDrafts).toEqual([]);
50 expect(draft).toHaveLength(PROVIDER_SEND_TURN_MAX_INPUT_CHARS + 1);
51 expect(validationMessage).toBe(
52 "Prompt is 1 character over the 120,000-character limit. Shorten or split it before sending.",
53 );
54 expect(preventDefault).toHaveBeenCalledOnce();
55
56 draft = "Corrected prompt";
57 submit();
58
59 expect(dispatchedDrafts).toEqual(["Corrected prompt"]);
60 expect(validationMessage).toBeNull();
61 });
62
63 it("blocks when appended context pushes the provider input over the shared limit", () => {
64 const draft = "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
65 const onSend = vi.fn();
66
67 const result = submitComposerDraft({
68 prompt: draft,
69 providerInput: `${draft}\n\nTerminal context`,
70 submissionTarget: "provider-turn",
71 event: undefined,
72 onSend,
73 });
74
75 expect(result).toEqual({
76 validationMessage:
77 "Prompt is 18 characters over the 120,000-character limit. Shorten or split it before sending.",
78 didDispatch: false,
79 });
80 expect(draft).toHaveLength(PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
81 expect(onSend).not.toHaveBeenCalled();
82
83 const correctedResult = submitComposerDraft({
84 prompt: "Corrected prompt",
85 providerInput: "Corrected prompt\n\nShort terminal context",
86 submissionTarget: "provider-turn",
87 event: undefined,
88 onSend,
89 });
90
91 expect(correctedResult).toEqual({ validationMessage: null, didDispatch: true });
92 expect(onSend).toHaveBeenCalledOnce();
93 });
94
95 it("does not finish submission when the send boundary rejects composed provider input", () => {
96 const preventDefault = vi.fn();
97
98 const result = submitComposerDraft({
99 prompt: "Sendable raw draft",
100 submissionTarget: "provider-turn",
101 event: { preventDefault },
102 onSend: () => false,
103 });
104
105 expect(result).toEqual({ validationMessage: null, didDispatch: false });
106 expect(preventDefault).toHaveBeenCalledOnce();
107 });
108
109 it.each(["draft", "composed provider input"])(
110 "blocks a %s when citation expansion exceeds the shared limit",
111 (source) => {
112 const citation = serializeAssistantCitation(assistantCitation);
113 const followUp = `Explain ${citation}\n\nTerminal context`;
114 const providerInput = `${"x".repeat(
115 PROVIDER_SEND_TURN_MAX_INPUT_CHARS -
116 expandAssistantCitationsForProvider(followUp).length +
117 1,
118 )}${followUp}`;
119 const onSend = vi.fn();
120 const preventDefault = vi.fn();
121
122 expect(providerInput.length).toBeLessThan(PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
123 const result = submitComposerDraft({
124 prompt: source === "draft" ? providerInput : "Explain the quote",
125 ...(source === "composed provider input" ? { providerInput } : {}),
126 submissionTarget: "provider-turn",
127 event: { preventDefault },
128 onSend,
129 });
130
131 expect(result).toEqual({
132 validationMessage:
133 "Prompt is 1 character over the 120,000-character limit. Shorten or split it before sending.",
134 didDispatch: false,
135 });
136 expect(onSend).not.toHaveBeenCalled();
137 expect(preventDefault).toHaveBeenCalledOnce();
138 },
139 );
140
141 it("keeps the encoded-message limit when citation expansion is shorter", () => {
142 const citation = serializeAssistantCitation({
143 ...assistantCitation,
144 text: "é".repeat(ASSISTANT_CITATION_MAX_TEXT_LENGTH),
145 end: ASSISTANT_CITATION_MAX_TEXT_LENGTH,
146 });
147 const draft = `${"x".repeat(
148 PROVIDER_SEND_TURN_MAX_INPUT_CHARS - citation.length + 1,
149 )}${citation}`;
150 const onSend = vi.fn();
151
152 expect(expandAssistantCitationsForProvider(draft).length).toBeLessThan(
153 PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
154 );
155 const result = submitComposerDraft({
156 prompt: draft,
157 submissionTarget: "provider-turn",
158 event: undefined,
159 onSend,
160 });
161
162 expect(result).toEqual({
163 validationMessage:
164 "Prompt is 1 character over the 120,000-character limit. Shorten or split it before sending.",
165 didDispatch: false,
166 });
167 expect(onSend).not.toHaveBeenCalled();
168 });
169
170 it("sends canonical citations when expanded input fits exactly after trimming", () => {
171 const citation = serializeAssistantCitation(assistantCitation);
172 const draft = ` ${"x".repeat(
173 PROVIDER_SEND_TURN_MAX_INPUT_CHARS - expandAssistantCitationsForProvider(citation).length,
174 )}${citation} \n`;
175 const dispatchedDrafts: string[] = [];
176
177 const result = submitComposerDraft({
178 prompt: draft,
179 submissionTarget: "provider-turn",
180 event: undefined,
181 onSend: () => {
182 dispatchedDrafts.push(draft);
183 },
184 });
185
186 expect(result).toEqual({ validationMessage: null, didDispatch: true });
187 expect(dispatchedDrafts).toEqual([draft]);
188 expect(dispatchedDrafts[0]).toContain(citation);
189 expect(dispatchedDrafts[0]).not.toContain("<assistant_citations>");
190 });
191
192 it("does not apply citation-expanded prompt limits to pending user input answers", () => {
193 const citation = serializeAssistantCitation(assistantCitation);
194 const answer = `${"x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS - citation.length)}${citation}`;
195 const onSend = vi.fn();
196
197 expect(expandAssistantCitationsForProvider(answer).length).toBeGreaterThan(
198 PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
199 );
200 const result = submitComposerDraft({
201 prompt: answer,
202 submissionTarget: "pending-user-input",
203 event: undefined,
204 onSend,
205 });
206
207 expect(result).toEqual({ validationMessage: null, didDispatch: true });
208 expect(onSend).toHaveBeenCalledOnce();
209 });
210 });
211
212 describe("isComposerSubmissionBlocked", () => {
213 const sendBlocked = {
214 noProviderAvailable: true,
215 sendDisabledReason: "Messages loading",
216 attachmentBlockReason: null,
217 environmentUnavailable: false,
218 };
219
220 // regression: #9318
221 it("lets a pending answer through while provider-turn sends are blocked", () => {
222 expect(
223 isComposerSubmissionBlocked({ ...sendBlocked, submissionTarget: "pending-user-input" }),
224 ).toBe(false);
225 expect(isComposerSubmissionBlocked({ ...sendBlocked, submissionTarget: "provider-turn" })).toBe(
226 true,
227 );
228 });
229
230 it("still blocks a pending answer on its attachments or a lost connection", () => {
231 const answer = { ...sendBlocked, submissionTarget: "pending-user-input" as const };
232 expect(
233 isComposerSubmissionBlocked({ ...answer, attachmentBlockReason: "Attach the file again" }),
234 ).toBe(true);
235 expect(isComposerSubmissionBlocked({ ...answer, environmentUnavailable: true })).toBe(true);
236 });
237 });