| 1 | import { ChatCompletionMessageParam } from "openai/resources.mjs"; |
| 2 | import { describe, it, expect } from "vitest"; |
| 3 | |
| 4 | import { pruneLastMessage } from "./compaction.js"; |
| 5 | |
| 6 | describe("pruneLastMessage", () => { |
| 7 | it("should return empty array for empty input", () => { |
| 8 | const result = pruneLastMessage([]); |
| 9 | expect(result).toEqual([]); |
| 10 | }); |
| 11 | |
| 12 | it("should remove single user message", () => { |
| 13 | const history: ChatCompletionMessageParam[] = [ |
| 14 | { role: "user", content: "Hello" }, |
| 15 | ]; |
| 16 | |
| 17 | const result = pruneLastMessage(history); |
| 18 | expect(result).toEqual([]); |
| 19 | }); |
| 20 | |
| 21 | it("should return empty array for single system message", () => { |
| 22 | const history: ChatCompletionMessageParam[] = [ |
| 23 | { role: "system", content: "System" }, |
| 24 | ]; |
| 25 | |
| 26 | const result = pruneLastMessage(history); |
| 27 | expect(result).toEqual([]); |
| 28 | }); |
| 29 | |
| 30 | it("should return empty array for single assistant message", () => { |
| 31 | const history: ChatCompletionMessageParam[] = [ |
| 32 | { role: "assistant", content: "Hello" }, |
| 33 | ]; |
| 34 | |
| 35 | const result = pruneLastMessage(history); |
| 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([]); |
| 46 | }); |
| 47 | |
| 48 | it("should remove last message when second-to-last is user", () => { |
| 49 | const history: ChatCompletionMessageParam[] = [ |
| 50 | { role: "system", content: "System" }, |
| 51 | { role: "user", content: "Hello" }, |
| 52 | { role: "assistant", content: "Hi there" }, |
| 53 | ]; |
| 54 | |
| 55 | const result = pruneLastMessage(history); |
| 56 | expect(result).toEqual([{ role: "system", content: "System" }]); |
| 57 | }); |
| 58 | |
| 59 | it("should remove assistant+tool sequence when second-to-last is assistant with tool calls", () => { |
| 60 | const history: ChatCompletionMessageParam[] = [ |
| 61 | { role: "system", content: "System" }, |
| 62 | { role: "user", content: "Run command" }, |
| 63 | { |
| 64 | role: "assistant", |
| 65 | content: "Running...", |
| 66 | tool_calls: [{ id: "1" } as any], |
| 67 | }, |
| 68 | { role: "tool", content: "Command output", tool_call_id: "1" }, |
| 69 | ]; |
| 70 | |
| 71 | const result = pruneLastMessage(history); |
| 72 | expect(result).toEqual([ |
| 73 | { role: "system", content: "System" }, |
| 74 | { role: "user", content: "Run command" }, |
| 75 | ]); |
| 76 | }); |
| 77 | |
| 78 | it("should prune last message when second-to-last is not user or assistant with tool calls", () => { |
| 79 | const history: ChatCompletionMessageParam[] = [ |
| 80 | { role: "system", content: "System" }, |
| 81 | { role: "assistant", content: "Hi there" }, |
| 82 | { role: "user", content: "Another question" }, |
| 83 | ]; |
| 84 | |
| 85 | const result = pruneLastMessage(history); |
| 86 | expect(result).toEqual([ |
| 87 | { role: "system", content: "System" }, |
| 88 | { role: "assistant", content: "Hi there" }, |
| 89 | ]); |
| 90 | }); |
| 91 | |
| 92 | it("should handle multiple user messages by removing two at a time", () => { |
| 93 | const history: ChatCompletionMessageParam[] = [ |
| 94 | { role: "system", content: "System" }, |
| 95 | { role: "user", content: "Question 1" }, |
| 96 | { role: "user", content: "Question 2" }, |
| 97 | ]; |
| 98 | |
| 99 | const result = pruneLastMessage(history); |
| 100 | expect(result).toEqual([{ role: "system", content: "System" }]); |
| 101 | }); |
| 102 | |
| 103 | it("should handle tool call sequences by removing user after tool", () => { |
| 104 | const history: ChatCompletionMessageParam[] = [ |
| 105 | { role: "system", content: "System" }, |
| 106 | { role: "user", content: "Do something" }, |
| 107 | { |
| 108 | role: "assistant", |
| 109 | content: "I'll help", |
| 110 | tool_calls: [{ id: "1" } as any], |
| 111 | }, |
| 112 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 113 | { role: "user", content: "Follow up question" }, |
| 114 | ]; |
| 115 | |
| 116 | const result = pruneLastMessage(history); |
| 117 | expect(result).toEqual([ |
| 118 | { role: "system", content: "System" }, |
| 119 | { role: "user", content: "Do something" }, |
| 120 | { |
| 121 | role: "assistant", |
| 122 | content: "I'll help", |
| 123 | tool_calls: [{ id: "1" } as any], |
| 124 | }, |
| 125 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 126 | ]); |
| 127 | }); |
| 128 | |
| 129 | it("should remove both user messages when consecutive", () => { |
| 130 | const history: ChatCompletionMessageParam[] = [ |
| 131 | { role: "user", content: "Hello" }, |
| 132 | { role: "user", content: "Another message" }, |
| 133 | ]; |
| 134 | |
| 135 | const result = pruneLastMessage(history); |
| 136 | expect(result).toEqual([]); |
| 137 | }); |
| 138 | |
| 139 | it("should handle complex conversation flow", () => { |
| 140 | const history: ChatCompletionMessageParam[] = [ |
| 141 | { role: "system", content: "System" }, |
| 142 | { role: "user", content: "Request 1" }, |
| 143 | { role: "assistant", content: "Response 1" }, |
| 144 | { role: "user", content: "Request 2" }, |
| 145 | { |
| 146 | role: "assistant", |
| 147 | content: "Using tool", |
| 148 | tool_calls: [{ id: "1" } as any], |
| 149 | }, |
| 150 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 151 | { role: "assistant", content: "Final response" }, |
| 152 | { role: "user", content: "Follow up 1" }, |
| 153 | { role: "user", content: "Follow up 2" }, |
| 154 | ]; |
| 155 | |
| 156 | const result = pruneLastMessage(history); |
| 157 | expect(result).toEqual([ |
| 158 | { role: "system", content: "System" }, |
| 159 | { role: "user", content: "Request 1" }, |
| 160 | { role: "assistant", content: "Response 1" }, |
| 161 | { role: "user", content: "Request 2" }, |
| 162 | { |
| 163 | role: "assistant", |
| 164 | content: "Using tool", |
| 165 | tool_calls: [{ id: "1" } as any], |
| 166 | }, |
| 167 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 168 | { role: "assistant", content: "Final response" }, |
| 169 | ]); |
| 170 | }); |
| 171 | |
| 172 | it("should handle history starting with non-system message", () => { |
| 173 | const history: ChatCompletionMessageParam[] = [ |
| 174 | { role: "user", content: "Hello" }, |
| 175 | { role: "assistant", content: "Hi" }, |
| 176 | { role: "user", content: "Follow up" }, |
| 177 | ]; |
| 178 | |
| 179 | const result = pruneLastMessage(history); |
| 180 | expect(result).toEqual([ |
| 181 | { role: "user", content: "Hello" }, |
| 182 | { role: "assistant", content: "Hi" }, |
| 183 | ]); |
| 184 | }); |
| 185 | }); |