| 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 preserve history ending with assistant message", () => { |
| 13 | const history: ChatCompletionMessageParam[] = [ |
| 14 | { role: "system", content: "System" }, |
| 15 | { role: "user", content: "Hello" }, |
| 16 | { role: "assistant", content: "Hi there" }, |
| 17 | ]; |
| 18 | |
| 19 | const result = pruneLastMessage(history); |
| 20 | expect(result).toEqual(history); |
| 21 | }); |
| 22 | |
| 23 | it("should preserve history ending with tool message", () => { |
| 24 | const history: ChatCompletionMessageParam[] = [ |
| 25 | { 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 | ]; |
| 30 | |
| 31 | const result = pruneLastMessage(history); |
| 32 | expect(result).toEqual(history); |
| 33 | }); |
| 34 | |
| 35 | it("should prune user messages at the end", () => { |
| 36 | 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" }, |
| 41 | ]; |
| 42 | |
| 43 | 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 | ]); |
| 49 | }); |
| 50 | |
| 51 | it("should prune multiple user messages at the end", () => { |
| 52 | const history: ChatCompletionMessageParam[] = [ |
| 53 | { role: "system", content: "System" }, |
| 54 | { role: "user", content: "Hello" }, |
| 55 | { role: "assistant", content: "Hi there" }, |
| 56 | { role: "user", content: "Question 1" }, |
| 57 | { role: "user", content: "Question 2" }, |
| 58 | ]; |
| 59 | |
| 60 | const result = pruneLastMessage(history); |
| 61 | expect(result).toEqual([ |
| 62 | { role: "system", content: "System" }, |
| 63 | { role: "user", content: "Hello" }, |
| 64 | { role: "assistant", content: "Hi there" }, |
| 65 | ]); |
| 66 | }); |
| 67 | |
| 68 | it("should handle tool call sequences correctly", () => { |
| 69 | const history: ChatCompletionMessageParam[] = [ |
| 70 | { 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" }, |
| 75 | ]; |
| 76 | |
| 77 | const result = pruneLastMessage(history); |
| 78 | expect(result).toEqual([ |
| 79 | { 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" }, |
| 83 | ]); |
| 84 | }); |
| 85 | |
| 86 | it("should return only system message when no assistant/tool messages exist", () => { |
| 87 | const history: ChatCompletionMessageParam[] = [ |
| 88 | { role: "system", content: "System" }, |
| 89 | { role: "user", content: "Hello" }, |
| 90 | { role: "user", content: "Another message" }, |
| 91 | ]; |
| 92 | |
| 93 | const result = pruneLastMessage(history); |
| 94 | expect(result).toEqual([ |
| 95 | { role: "system", content: "System" }, |
| 96 | ]); |
| 97 | }); |
| 98 | |
| 99 | it("should return empty array when no system message and no assistant/tool messages", () => { |
| 100 | const history: ChatCompletionMessageParam[] = [ |
| 101 | { role: "user", content: "Hello" }, |
| 102 | { role: "user", content: "Another message" }, |
| 103 | ]; |
| 104 | |
| 105 | const result = pruneLastMessage(history); |
| 106 | expect(result).toEqual([]); |
| 107 | }); |
| 108 | |
| 109 | it("should handle history with only system message", () => { |
| 110 | const history: ChatCompletionMessageParam[] = [ |
| 111 | { role: "system", content: "System" }, |
| 112 | ]; |
| 113 | |
| 114 | const result = pruneLastMessage(history); |
| 115 | expect(result).toEqual([ |
| 116 | { role: "system", content: "System" }, |
| 117 | ]); |
| 118 | }); |
| 119 | |
| 120 | it("should handle history with only assistant message", () => { |
| 121 | const history: ChatCompletionMessageParam[] = [ |
| 122 | { role: "assistant", content: "Hello" }, |
| 123 | ]; |
| 124 | |
| 125 | const result = pruneLastMessage(history); |
| 126 | expect(result).toEqual([ |
| 127 | { role: "assistant", content: "Hello" }, |
| 128 | ]); |
| 129 | }); |
| 130 | |
| 131 | it("should handle complex conversation flow", () => { |
| 132 | const history: ChatCompletionMessageParam[] = [ |
| 133 | { role: "system", content: "System" }, |
| 134 | { role: "user", content: "Request 1" }, |
| 135 | { role: "assistant", content: "Response 1" }, |
| 136 | { role: "user", content: "Request 2" }, |
| 137 | { role: "assistant", content: "Using tool", tool_calls: [{ id: "1" } as any] }, |
| 138 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 139 | { role: "assistant", content: "Final response" }, |
| 140 | { role: "user", content: "Follow up 1" }, |
| 141 | { role: "user", content: "Follow up 2" }, |
| 142 | ]; |
| 143 | |
| 144 | const result = pruneLastMessage(history); |
| 145 | expect(result).toEqual([ |
| 146 | { role: "system", content: "System" }, |
| 147 | { role: "user", content: "Request 1" }, |
| 148 | { role: "assistant", content: "Response 1" }, |
| 149 | { role: "user", content: "Request 2" }, |
| 150 | { role: "assistant", content: "Using tool", tool_calls: [{ id: "1" } as any] }, |
| 151 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 152 | { role: "assistant", content: "Final response" }, |
| 153 | ]); |
| 154 | }); |
| 155 | |
| 156 | it("should handle history starting with non-system message", () => { |
| 157 | const history: ChatCompletionMessageParam[] = [ |
| 158 | { role: "user", content: "Hello" }, |
| 159 | { role: "assistant", content: "Hi" }, |
| 160 | { role: "user", content: "Follow up" }, |
| 161 | ]; |
| 162 | |
| 163 | const result = pruneLastMessage(history); |
| 164 | expect(result).toEqual([ |
| 165 | { role: "user", content: "Hello" }, |
| 166 | { role: "assistant", content: "Hi" }, |
| 167 | ]); |
| 168 | }); |
| 169 | }); |