|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | + |
| 3 | +const SYNTHETIC_PATTERNS = [ |
| 4 | + /^Tool \w+ returned an attachment:/, |
| 5 | + /^What did we do so far\?/, |
| 6 | + /^The following tool was executed by the user$/, |
| 7 | + /^Tool result:/i, |
| 8 | + /^Tool output:/i, |
| 9 | +] |
| 10 | + |
| 11 | +function isSynthetic(text: string): boolean { |
| 12 | + if (!text || typeof text !== "string") return false |
| 13 | + const trimmed = text.trim() |
| 14 | + return SYNTHETIC_PATTERNS.some((p) => p.test(trimmed)) |
| 15 | +} |
| 16 | + |
| 17 | +function hasSyntheticContent(content: unknown): boolean { |
| 18 | + if (typeof content === "string") return isSynthetic(content) |
| 19 | + if (!Array.isArray(content)) return false |
| 20 | + return content.some((part: any) => isSynthetic(part.text || part.content || "")) |
| 21 | +} |
| 22 | + |
| 23 | +function detectAgent(messages: any[]): boolean { |
| 24 | + if (!Array.isArray(messages) || messages.length === 0) return false |
| 25 | + const hasNonUser = messages.some((msg: any) => ["assistant", "tool"].includes(msg.role)) |
| 26 | + if (hasNonUser) return true |
| 27 | + const last = messages[messages.length - 1] |
| 28 | + if (last?.role === "user" && hasSyntheticContent(last.content)) return true |
| 29 | + return false |
| 30 | +} |
| 31 | + |
| 32 | +function getInitiator(body: any): "user" | "agent" { |
| 33 | + const messages = body?.messages || body?.input || [] |
| 34 | + return detectAgent(messages) ? "agent" : "user" |
| 35 | +} |
| 36 | + |
| 37 | +describe("plugin.copilot", () => { |
| 38 | + describe("isSynthetic", () => { |
| 39 | + test("detects tool attachment pattern", () => { |
| 40 | + expect(isSynthetic("Tool read_file returned an attachment:")).toBe(true) |
| 41 | + expect(isSynthetic("Tool bash returned an attachment:")).toBe(true) |
| 42 | + }) |
| 43 | + |
| 44 | + test("detects compaction pattern", () => { |
| 45 | + expect(isSynthetic("What did we do so far?")).toBe(true) |
| 46 | + expect(isSynthetic("What did we do so far? ")).toBe(true) |
| 47 | + }) |
| 48 | + |
| 49 | + test("detects subtask pattern", () => { |
| 50 | + expect(isSynthetic("The following tool was executed by the user")).toBe(true) |
| 51 | + }) |
| 52 | + |
| 53 | + test("ignores normal user messages", () => { |
| 54 | + expect(isSynthetic("Hello, can you help me?")).toBe(false) |
| 55 | + expect(isSynthetic("Read the file README.md")).toBe(false) |
| 56 | + expect(isSynthetic("What did we do yesterday?")).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + test("handles empty and invalid input", () => { |
| 60 | + expect(isSynthetic("")).toBe(false) |
| 61 | + expect(isSynthetic(null as any)).toBe(false) |
| 62 | + expect(isSynthetic(undefined as any)).toBe(false) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe("detectAgent", () => { |
| 67 | + test("first user message returns user", () => { |
| 68 | + expect(getInitiator({ messages: [{ role: "user", content: "Hello" }] })).toBe("user") |
| 69 | + }) |
| 70 | + |
| 71 | + test("empty messages returns user", () => { |
| 72 | + expect(getInitiator({ messages: [] })).toBe("user") |
| 73 | + expect(getInitiator({})).toBe("user") |
| 74 | + expect(getInitiator(null)).toBe("user") |
| 75 | + }) |
| 76 | + |
| 77 | + test("assistant message returns agent", () => { |
| 78 | + expect(getInitiator({ messages: [{ role: "user", content: "Hello" }, { role: "assistant", content: "Hi" }] })).toBe("agent") |
| 79 | + }) |
| 80 | + |
| 81 | + test("tool message returns agent", () => { |
| 82 | + expect(getInitiator({ messages: [{ role: "user", content: "Run test" }, { role: "tool", content: "Test passed" }] })).toBe("agent") |
| 83 | + }) |
| 84 | + |
| 85 | + test("synthetic tool attachment returns agent", () => { |
| 86 | + expect(getInitiator({ messages: [{ role: "user", content: "Tool read_file returned an attachment:" }] })).toBe("agent") |
| 87 | + }) |
| 88 | + |
| 89 | + test("synthetic compaction returns agent", () => { |
| 90 | + expect(getInitiator({ messages: [{ role: "user", content: "What did we do so far? " }] })).toBe("agent") |
| 91 | + }) |
| 92 | + |
| 93 | + test("synthetic with array content returns agent", () => { |
| 94 | + expect(getInitiator({ |
| 95 | + messages: [{ |
| 96 | + role: "user", |
| 97 | + content: [{ type: "text", text: "Tool bash returned an attachment:" }, { type: "file", url: "file://out.txt" }], |
| 98 | + }], |
| 99 | + })).toBe("agent") |
| 100 | + }) |
| 101 | + |
| 102 | + test("responses API format works", () => { |
| 103 | + expect(getInitiator({ input: [{ role: "user", content: "Hello" }] })).toBe("user") |
| 104 | + expect(getInitiator({ input: [{ role: "user", content: "Hello" }, { role: "assistant", content: "Hi" }] })).toBe("agent") |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe("regression: issues #8030 and #8067", () => { |
| 109 | + test("synthetic user message in conversation does not charge premium", () => { |
| 110 | + expect(getInitiator({ |
| 111 | + messages: [ |
| 112 | + { role: "user", content: "Read file.txt" }, |
| 113 | + { role: "assistant", content: "Reading..." }, |
| 114 | + { role: "user", content: "Tool read_file returned an attachment:" }, |
| 115 | + ], |
| 116 | + })).toBe("agent") |
| 117 | + }) |
| 118 | + |
| 119 | + test("multi-turn with real user follow-up still agent (assistant exists)", () => { |
| 120 | + expect(getInitiator({ |
| 121 | + messages: [ |
| 122 | + { role: "user", content: "Hello" }, |
| 123 | + { role: "assistant", content: "Hi" }, |
| 124 | + { role: "user", content: "Now do something else" }, |
| 125 | + ], |
| 126 | + })).toBe("agent") |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
0 commit comments