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

composerPromptHistory.test.ts

237 lines9.3 KB

1import { EnvironmentId, ScheduledTaskId, ThreadId } from "@t3tools/contracts";
2import {
3 formatComposerContextReference,
4 threadContextRecord,
5} from "@t3tools/shared/composerContextReferences";
6import { describe, expect, it } from "vite-plus/test";
7​
8import {
9 buildComposerPromptHistoryEntries,
10 recallableComposerPrompt,
11 stepComposerPromptHistory,
12 type ComposerPromptHistoryPosition,
13} from "./composerPromptHistory";
14import { collectComposerPromptInlineTokens } from "../../composer-editor-mentions";
15import { environmentContextRecord } from "../../lib/composerContextRecords";
16​
17const entries = buildComposerPromptHistoryEntries([
18 { id: "m1", role: "user", text: "first" },
19 { id: "a1", role: "assistant", text: "reply" },
20 { id: "m2", role: "user", text: "second" },
21 { id: "m3", role: "user", text: "third" },
22]);
23​
24function backward(position: ComposerPromptHistoryPosition | null, currentPrompt: string) {
25 return stepComposerPromptHistory({ direction: "backward", entries, position, currentPrompt });
27​
28function forward(position: ComposerPromptHistoryPosition | null, currentPrompt: string) {
29 return stepComposerPromptHistory({ direction: "forward", entries, position, currentPrompt });
31​
32describe("recallableComposerPrompt", () => {
33 it.each([
34 "Render <preview_annotation>",
35 "Render\n<preview_annotation>\nhere",
36 "Render &lt;preview_annotation&gt;",
37 ])("strips an annotation whose comment contains a literal opening tag: %s", (comment) => {
38 expect(
39 recallableComposerPrompt(
40 `Prompt\n<preview_annotation>\nComment: ${comment}\n</preview_annotation>`,
41 ),
42 ).toBe("Prompt");
43 });
44​
45 it("preserves a malformed annotation containing a nested opening tag", () => {
46 const text =
47 "Prompt\n<preview_annotation>\nouter literal\n<preview_annotation>\ninner\n</preview_annotation>";
48 expect(recallableComposerPrompt(text)).toBe(text);
49 });
50 it("does not strip a terminal label embedded in ordinary prose", () => {
51 expect(
52 recallableComposerPrompt(
53 "email@build:7\n<terminal_context>\n- Build line 7:\n output\n</terminal_context>",
54 ),
55 ).toBe("email@build:7");
56 });
57 it("strips legacy send-time context blocks and the ultrathink prefix", () => {
58 const sent =
59 "Ultrathink:\nInvestigate this\n\n<terminal_context>\n- Terminal 1 lines 12-13:\n 12 | git status\n 13 | On branch main\n</terminal_context>\n\n<element_context>\n- <button>:\n url: https://example.com\n</element_context>";
60 expect(recallableComposerPrompt(sent)).toBe("Investigate this");
61 });
62​
63 it.each([
64 ["Look at @terminal-1:4 please", "Look at please"],
65 [
66 "@terminal-1:4 typed twice: @terminal-1:4\n indented code",
67 "typed twice: @terminal-1:4\n indented code",
68 ],
69 [
70 "see @terminal-1:40 and @terminal-1:4-12 then @terminal-1:4",
71 "see @terminal-1:40 and @terminal-1:4-12 then",
72 ],
73 ])("removes only the matching legacy terminal label from %s", (typed, expected) => {
74 const sent =
75 typed + "\n\n<terminal_context>\n- Terminal 1 line 4:\n 4 | ls\n</terminal_context>";
76 expect(recallableComposerPrompt(sent)).toBe(expected);
77 });
78​
79 it("strips only review comments appended at the end", () => {
80 const block = '<review_comment filePath="src/app.ts">Keep this configurable.</review_comment>';
81 expect(recallableComposerPrompt("Please update this.\n\n" + block)).toBe("Please update this.");
82 const midPrompt = "Before\n\n" + block + "\n\nAfter";
83 expect(recallableComposerPrompt(midPrompt)).toBe(midPrompt);
84 expect(recallableComposerPrompt(midPrompt + "\n\n" + block)).toBe(midPrompt);
85 });
86​
87 it("removes canonical references without restoring dangling chips", () => {
88 const sent =
89 "Look at [Terminal](t3-context://v1/terminal/term-1) please\n indented code\n![image](t3-context://v1/image/img-1)";
90 expect(recallableComposerPrompt(sent)).toBe("Look at please\n indented code");
91 expect(recallableComposerPrompt("![image](t3-context://v1/image/img-1)")).toBe("");
92 });
93});
94​
95describe("buildComposerPromptHistoryEntries", () => {
96 it("collapses consecutive duplicates onto the newest message id", () => {
97 const collapsed = buildComposerPromptHistoryEntries([
98 { id: "m1", role: "user", text: "same" },
99 { id: "m2", role: "user", text: "same" },
100 { id: "m3", role: "user", text: "other" },
101 { id: "m4", role: "user", text: "same" },
102 ]);
103 expect(collapsed.map(({ id, prompt }) => ({ id, prompt }))).toEqual([
104 { id: "m2", prompt: "same" },
105 { id: "m3", prompt: "other" },
106 { id: "m4", prompt: "same" },
107 ]);
108 });
109​
110 it("keeps thread and machine mentions with the records the draft needs to resolve them", () => {
111 const environmentId = EnvironmentId.make("env-1");
112 const thread = threadContextRecord(
113 { environmentId, threadId: ThreadId.make("thread-1") },
114 "Correct invoice charge categories",
115 );
116 const unmentioned = threadContextRecord(
117 { environmentId, threadId: ThreadId.make("thread-2") },
118 "Removed before send",
119 );
120 const machine = environmentContextRecord(environmentId, "Build box");
121 const threadChip = formatComposerContextReference(thread);
122 const machineChip = formatComposerContextReference(machine);
123 const [entry] = buildComposerPromptHistoryEntries([
124 {
125 id: "m1",
126 role: "user",
127 text: `Agent in ${threadChip} finished on ${machineChip}. See [Gone](t3-context://v1/thread/gone) ![shot](t3-context://v1/image/img-1)`,
128 context: { version: 1, records: [thread, unmentioned, machine] },
129 },
130 ]);
131 expect(entry).toEqual({
132 id: "m1",
133 prompt: `Agent in ${threadChip} finished on ${machineChip}. See`,
134 mentions: { threadContexts: [thread], environmentContexts: [machine] },
135 });
136 const recalled = stepComposerPromptHistory({
137 direction: "backward",
138 entries: [entry!],
139 position: null,
140 currentPrompt: "",
141 });
142 expect(recalled?.mentions).toEqual({
143 threadContexts: [thread],
144 environmentContexts: [machine],
145 });
146 });
147​
148 it("gives a recalled message ending in a file chip the space that keeps it a chip", () => {
149 const [entry] = buildComposerPromptHistoryEntries([
150 { id: "m1", role: "user", text: "Check @README.md" },
151 ]);
152 expect(entry?.prompt).toBe("Check @README.md ");
153 expect(collectComposerPromptInlineTokens(entry!.prompt).map((token) => token.source)).toEqual([
154 "@README.md",
155 ]);
156 });
157​
158 it("skips messages sent by automations, agents, and T3 Code", () => {
159 const typed = buildComposerPromptHistoryEntries([
160 { id: "m1", role: "user", text: "typed" },
161 { id: "m2", role: "user", text: "scheduled", scheduledTaskId: ScheduledTaskId.make("task") },
162 {
163 id: "scheduled-task-message:task:1:scheduled",
164 role: "user",
165 text: "[Triggered by schedule task: Daily]\n\nlegacy scheduled",
166 },
167 { id: "m3", role: "user", text: "from agent", createdBy: "agent", creationSource: "mcp" },
168 { id: "m4", role: "user", text: "from server", createdBy: "agent", creationSource: "server" },
169 ]);
170 expect(typed.map((entry) => entry.id)).toEqual(["m1"]);
171 });
172});
173​
174describe("stepComposerPromptHistory", () => {
175 it("walks back from the newest entry", () => {
176 const first = backward(null, "");
177 expect(first).toMatchObject({
178 position: { entryId: "m3", recalled: "third" },
179 prompt: "third",
180 });
181 expect(backward(first!.position, "third")?.prompt).toBe("second");
182 });
183​
184 it("walks forward and empties the composer past the newest entry", () => {
185 const newer = forward({ entryId: "m2", recalled: "second" }, "second");
186 expect(newer?.prompt).toBe("third");
187 expect(forward(newer!.position, "third")).toEqual({
188 position: null,
189 prompt: "",
190 mentions: { threadContexts: [], environmentContexts: [] },
191 });
192 });
193​
194 it("treats an edited recall as a fresh draft", () => {
195 const position: ComposerPromptHistoryPosition = { entryId: "m3", recalled: "third" };
196 expect(backward(position, "third edited")).toBeNull();
197 expect(forward(position, "third edited")).toBeNull();
198 expect(backward(position, "")?.position).toEqual({ entryId: "m3", recalled: "third" });
199 });
200​
201 it("follows the entry by id when the list changes under it", () => {
202 const grown = buildComposerPromptHistoryEntries([
203 { id: "m0", role: "user", text: "zeroth" },
204 { id: "m1", role: "user", text: "A" },
205 { id: "m2", role: "user", text: "B" },
206 { id: "m3", role: "user", text: "A" },
207 ]);
208 const older = stepComposerPromptHistory({
209 direction: "backward",
210 entries: grown,
211 position: { entryId: "m1", recalled: "A" },
212 currentPrompt: "A",
213 });
214 expect(older?.prompt).toBe("zeroth");
215 const missing = stepComposerPromptHistory({
216 direction: "forward",
217 entries: grown,
218 position: { entryId: "gone", recalled: "not sent" },
219 currentPrompt: "not sent",
220 });
221 expect(missing).toBeNull();
222 });
223​
224 it("falls back to matching text when a duplicate collapse retires the id", () => {
225 const collapsed = buildComposerPromptHistoryEntries([
226 { id: "m1", role: "user", text: "first" },
227 { id: "m3", role: "user", text: "A" },
228 ]);
229 const step = stepComposerPromptHistory({
230 direction: "backward",
231 entries: collapsed,
232 position: { entryId: "m2", recalled: "A" },
233 currentPrompt: "A",
234 });
235 expect(step?.prompt).toBe("first");
236 });
237});