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