Changesets · Branches · Tags · Compare
| Changeset | 1c83b0b1adf27e2a947795f59bd1e032ee06aea73a39126e0eba9059d89053ca |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-08-23 10:50 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-08-23 10:50 -07:00 |
| Parents | 15bc6182e084 |
3 files changed
modified extensions/cli/src/compaction.pruneLastMessage.test.ts +45 −45
| @@ -9,123 +9,123 @@ | ||
| 9 | 9 | expect(result).toEqual([]); |
| 10 | 10 | }); |
| 11 | 11 | |
| 12 | - it("should preserve history ending with assistant message", () => { | |
| 12 | + it("should remove single user message", () => { | |
| 13 | 13 | const history: ChatCompletionMessageParam[] = [ |
| 14 | - { role: "system", content: "System" }, | |
| 15 | 14 | { role: "user", content: "Hello" }, |
| 16 | - { role: "assistant", content: "Hi there" }, | |
| 17 | 15 | ]; |
| 18 | 16 | |
| 19 | 17 | const result = pruneLastMessage(history); |
| 20 | - expect(result).toEqual(history); | |
| 18 | + expect(result).toEqual([]); | |
| 21 | 19 | }); |
| 22 | 20 | |
| 23 | - it("should preserve history ending with tool message", () => { | |
| 21 | + it("should return empty array for single system message", () => { | |
| 24 | 22 | const history: ChatCompletionMessageParam[] = [ |
| 25 | 23 | { role: "system", content: "System" }, |
| 26 | - { role: "user", content: "Run command" }, | |
| 27 | - { role: "assistant", content: "Running...", tool_calls: [{ id: "1" } as any] }, | |
| 28 | - { role: "tool", content: "Command output", tool_call_id: "1" }, | |
| 29 | 24 | ]; |
| 30 | 25 | |
| 31 | 26 | const result = pruneLastMessage(history); |
| 32 | - expect(result).toEqual(history); | |
| 27 | + expect(result).toEqual([]); | |
| 33 | 28 | }); |
| 34 | 29 | |
| 35 | - it("should prune user messages at the end", () => { | |
| 30 | + it("should return empty array for single assistant message", () => { | |
| 36 | 31 | const history: ChatCompletionMessageParam[] = [ |
| 37 | - { role: "system", content: "System" }, | |
| 38 | - { role: "user", content: "Hello" }, | |
| 39 | - { role: "assistant", content: "Hi there" }, | |
| 40 | - { role: "user", content: "Another question" }, | |
| 32 | + { role: "assistant", content: "Hello" }, | |
| 41 | 33 | ]; |
| 42 | 34 | |
| 43 | 35 | const result = pruneLastMessage(history); |
| 44 | - expect(result).toEqual([ | |
| 45 | - { role: "system", content: "System" }, | |
| 46 | - { role: "user", content: "Hello" }, | |
| 47 | - { role: "assistant", content: "Hi there" }, | |
| 48 | - ]); | |
| 36 | + expect(result).toEqual([]); | |
| 37 | + }); | |
| 38 | + | |
| 39 | + it("should return empty array for single tool message", () => { | |
| 40 | + const history: ChatCompletionMessageParam[] = [ | |
| 41 | + { role: "tool", content: "Tool output", tool_call_id: "1" }, | |
| 42 | + ]; | |
| 43 | + | |
| 44 | + const result = pruneLastMessage(history); | |
| 45 | + expect(result).toEqual([]); | |
| 49 | 46 | }); |
| 50 | 47 | |
| 51 | - it("should prune multiple user messages at the end", () => { | |
| 48 | + it("should remove last message when second-to-last is user", () => { | |
| 52 | 49 | const history: ChatCompletionMessageParam[] = [ |
| 53 | 50 | { role: "system", content: "System" }, |
| 54 | 51 | { role: "user", content: "Hello" }, |
| 55 | 52 | { role: "assistant", content: "Hi there" }, |
| 56 | - { role: "user", content: "Question 1" }, | |
| 57 | - { role: "user", content: "Question 2" }, | |
| 58 | 53 | ]; |
| 59 | 54 | |
| 60 | 55 | const result = pruneLastMessage(history); |
| 61 | 56 | expect(result).toEqual([ |
| 62 | 57 | { role: "system", content: "System" }, |
| 63 | - { role: "user", content: "Hello" }, | |
| 64 | - { role: "assistant", content: "Hi there" }, | |
| 65 | 58 | ]); |
| 66 | 59 | }); |
| 67 | 60 | |
| 68 | - it("should handle tool call sequences correctly", () => { | |
| 61 | + it("should remove assistant+tool sequence when second-to-last is assistant with tool calls", () => { | |
| 69 | 62 | const history: ChatCompletionMessageParam[] = [ |
| 70 | 63 | { role: "system", content: "System" }, |
| 71 | - { role: "user", content: "Do something" }, | |
| 72 | - { role: "assistant", content: "I'll help", tool_calls: [{ id: "1" } as any] }, | |
| 73 | - { role: "tool", content: "Tool result", tool_call_id: "1" }, | |
| 74 | - { role: "user", content: "Follow up question" }, | |
| 64 | + { role: "user", content: "Run command" }, | |
| 65 | + { role: "assistant", content: "Running...", tool_calls: [{ id: "1" } as any] }, | |
| 66 | + { role: "tool", content: "Command output", tool_call_id: "1" }, | |
| 75 | 67 | ]; |
| 76 | 68 | |
| 77 | 69 | const result = pruneLastMessage(history); |
| 78 | 70 | expect(result).toEqual([ |
| 79 | 71 | { role: "system", content: "System" }, |
| 80 | - { role: "user", content: "Do something" }, | |
| 81 | - { role: "assistant", content: "I'll help", tool_calls: [{ id: "1" } as any] }, | |
| 82 | - { role: "tool", content: "Tool result", tool_call_id: "1" }, | |
| 72 | + { role: "user", content: "Run command" }, | |
| 83 | 73 | ]); |
| 84 | 74 | }); |
| 85 | 75 | |
| 86 | - it("should return only system message when no assistant/tool messages exist", () => { | |
| 76 | + it("should prune last message when second-to-last is not user or assistant with tool calls", () => { | |
| 87 | 77 | const history: ChatCompletionMessageParam[] = [ |
| 88 | 78 | { role: "system", content: "System" }, |
| 89 | - { role: "user", content: "Hello" }, | |
| 90 | - { role: "user", content: "Another message" }, | |
| 79 | + { role: "assistant", content: "Hi there" }, | |
| 80 | + { role: "user", content: "Another question" }, | |
| 91 | 81 | ]; |
| 92 | 82 | |
| 93 | 83 | const result = pruneLastMessage(history); |
| 94 | 84 | expect(result).toEqual([ |
| 95 | 85 | { role: "system", content: "System" }, |
| 86 | + { role: "assistant", content: "Hi there" }, | |
| 96 | 87 | ]); |
| 97 | 88 | }); |
| 98 | 89 | |
| 99 | - it("should return empty array when no system message and no assistant/tool messages", () => { | |
| 90 | + it("should handle multiple user messages by removing two at a time", () => { | |
| 100 | 91 | const history: ChatCompletionMessageParam[] = [ |
| 101 | - { role: "user", content: "Hello" }, | |
| 102 | - { role: "user", content: "Another message" }, | |
| 92 | + { role: "system", content: "System" }, | |
| 93 | + { role: "user", content: "Question 1" }, | |
| 94 | + { role: "user", content: "Question 2" }, | |
| 103 | 95 | ]; |
| 104 | 96 | |
| 105 | 97 | const result = pruneLastMessage(history); |
| 106 | - expect(result).toEqual([]); | |
| 98 | + expect(result).toEqual([ | |
| 99 | + { role: "system", content: "System" }, | |
| 100 | + ]); | |
| 107 | 101 | }); |
| 108 | 102 | |
| 109 | - it("should handle history with only system message", () => { | |
| 103 | + it("should handle tool call sequences by removing user after tool", () => { | |
| 110 | 104 | const history: ChatCompletionMessageParam[] = [ |
| 111 | 105 | { role: "system", content: "System" }, |
| 106 | + { role: "user", content: "Do something" }, | |
| 107 | + { role: "assistant", content: "I'll help", tool_calls: [{ id: "1" } as any] }, | |
| 108 | + { role: "tool", content: "Tool result", tool_call_id: "1" }, | |
| 109 | + { role: "user", content: "Follow up question" }, | |
| 112 | 110 | ]; |
| 113 | 111 | |
| 114 | 112 | const result = pruneLastMessage(history); |
| 115 | 113 | expect(result).toEqual([ |
| 116 | 114 | { role: "system", content: "System" }, |
| 115 | + { role: "user", content: "Do something" }, | |
| 116 | + { role: "assistant", content: "I'll help", tool_calls: [{ id: "1" } as any] }, | |
| 117 | + { role: "tool", content: "Tool result", tool_call_id: "1" }, | |
| 117 | 118 | ]); |
| 118 | 119 | }); |
| 119 | 120 | |
| 120 | - it("should handle history with only assistant message", () => { | |
| 121 | + it("should remove both user messages when consecutive", () => { | |
| 121 | 122 | const history: ChatCompletionMessageParam[] = [ |
| 122 | - { role: "assistant", content: "Hello" }, | |
| 123 | + { role: "user", content: "Hello" }, | |
| 124 | + { role: "user", content: "Another message" }, | |
| 123 | 125 | ]; |
| 124 | 126 | |
| 125 | 127 | const result = pruneLastMessage(history); |
| 126 | - expect(result).toEqual([ | |
| 127 | - { role: "assistant", content: "Hello" }, | |
| 128 | - ]); | |
| 128 | + expect(result).toEqual([]); | |
| 129 | 129 | }); |
| 130 | 130 | |
| 131 | 131 | it("should handle complex conversation flow", () => { |
modified extensions/cli/src/compaction.ts +5 −0
| @@ -169,6 +169,11 @@ | ||
| 169 | 169 | return chatHistory; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | + if (chatHistory.length === 1) { | |
| 173 | + // Only one message - always return empty array | |
| 174 | + return []; | |
| 175 | + } | |
| 176 | + | |
| 172 | 177 | const secondToLastIndex = chatHistory.length - 2; |
| 173 | 178 | const secondToLastMessage = chatHistory[secondToLastIndex]; |
| 174 | 179 |
modified extensions/cli/src/slashCommands.test.ts +1 −1
| @@ -108,7 +108,7 @@ | ||
| 108 | 108 | expect(result?.output).toContain("Authentication:"); |
| 109 | 109 | expect(result?.output).toContain("Authenticated via environment variable"); |
| 110 | 110 | expect(result?.output).toContain("Configuration:"); |
| 111 | - expect(result?.output).toContain("Using default configuration"); | |
| 111 | + expect(result?.output).toContain("Config not found"); | |
| 112 | 112 | expect(result?.exit).toBe(false); |
| 113 | 113 | }); |
| 114 | 114 |