Skip to content

Commit c7c6a81

Browse files
authored
Merge pull request #11 from CopilotKit/feat/array-content-support
Add getTextContent for array-format message content
2 parents c53d59a + e52cbeb commit c7c6a81

5 files changed

Lines changed: 139 additions & 9 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@copilotkit/llmock",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Deterministic mock LLM server for testing (OpenAI, Anthropic, Gemini)",
55
"license": "MIT",
66
"packageManager": "pnpm@10.28.2",

‎src/__tests__/router.test.ts‎

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect } from "vitest";
2-
import { matchFixture, getLastMessageByRole } from "../router.js";
3-
import type { ChatCompletionRequest, ChatMessage, Fixture } from "../types.js";
2+
import { matchFixture, getLastMessageByRole, getTextContent } from "../router.js";
3+
import type { ChatCompletionRequest, ChatMessage, ContentPart, Fixture } from "../types.js";
44

55
// ---------------------------------------------------------------------------
66
// Helpers
@@ -54,6 +54,57 @@ describe("getLastMessageByRole", () => {
5454
});
5555
});
5656

57+
// ---------------------------------------------------------------------------
58+
// getTextContent
59+
// ---------------------------------------------------------------------------
60+
61+
describe("getTextContent", () => {
62+
it("returns the string as-is for string content", () => {
63+
expect(getTextContent("hello world")).toBe("hello world");
64+
});
65+
66+
it("returns null for null content", () => {
67+
expect(getTextContent(null)).toBeNull();
68+
});
69+
70+
it("extracts text from array-of-parts content", () => {
71+
const parts: ContentPart[] = [{ type: "text", text: "hello world" }];
72+
expect(getTextContent(parts)).toBe("hello world");
73+
});
74+
75+
it("concatenates multiple text parts", () => {
76+
const parts: ContentPart[] = [
77+
{ type: "text", text: "hello " },
78+
{ type: "text", text: "world" },
79+
];
80+
expect(getTextContent(parts)).toBe("hello world");
81+
});
82+
83+
it("ignores non-text parts in array content", () => {
84+
const parts: ContentPart[] = [
85+
{ type: "image_url", image_url: { url: "https://example.com/img.png" } },
86+
{ type: "text", text: "describe this" },
87+
];
88+
expect(getTextContent(parts)).toBe("describe this");
89+
});
90+
91+
it("returns null for array with no text parts", () => {
92+
const parts: ContentPart[] = [
93+
{ type: "image_url", image_url: { url: "https://example.com/img.png" } },
94+
];
95+
expect(getTextContent(parts)).toBeNull();
96+
});
97+
98+
it("returns null for empty array", () => {
99+
expect(getTextContent([])).toBeNull();
100+
});
101+
102+
it("returns null for array with only empty-string text parts", () => {
103+
const parts: ContentPart[] = [{ type: "text", text: "" }];
104+
expect(getTextContent(parts)).toBeNull();
105+
});
106+
});
107+
57108
// ---------------------------------------------------------------------------
58109
// matchFixture — empty / null cases
59110
// ---------------------------------------------------------------------------
@@ -106,6 +157,61 @@ describe("matchFixture — userMessage (string)", () => {
106157
});
107158
});
108159

160+
describe("matchFixture — userMessage (array content)", () => {
161+
it("matches when user content is array-of-parts with matching text", () => {
162+
const fixture = makeFixture({ userMessage: "hello" });
163+
const req = makeReq({
164+
messages: [{ role: "user", content: [{ type: "text", text: "say hello world" }] }],
165+
});
166+
expect(matchFixture([fixture], req)).toBe(fixture);
167+
});
168+
169+
it("does not match when array-of-parts text does not include the string", () => {
170+
const fixture = makeFixture({ userMessage: "goodbye" });
171+
const req = makeReq({
172+
messages: [{ role: "user", content: [{ type: "text", text: "hello" }] }],
173+
});
174+
expect(matchFixture([fixture], req)).toBeNull();
175+
});
176+
177+
it("matches regexp against array-of-parts text", () => {
178+
const fixture = makeFixture({ userMessage: /^hello/i });
179+
const req = makeReq({
180+
messages: [{ role: "user", content: [{ type: "text", text: "Hello world" }] }],
181+
});
182+
expect(matchFixture([fixture], req)).toBe(fixture);
183+
});
184+
185+
it("concatenates multiple text parts for matching", () => {
186+
const fixture = makeFixture({ userMessage: "hello world" });
187+
const req = makeReq({
188+
messages: [
189+
{
190+
role: "user",
191+
content: [
192+
{ type: "text", text: "hello " },
193+
{ type: "text", text: "world" },
194+
],
195+
},
196+
],
197+
});
198+
expect(matchFixture([fixture], req)).toBe(fixture);
199+
});
200+
201+
it("skips array content with no text parts", () => {
202+
const fixture = makeFixture({ userMessage: "hello" });
203+
const req = makeReq({
204+
messages: [
205+
{
206+
role: "user",
207+
content: [{ type: "image_url", image_url: { url: "https://example.com" } }],
208+
},
209+
],
210+
});
211+
expect(matchFixture([fixture], req)).toBeNull();
212+
});
213+
});
214+
109215
describe("matchFixture — userMessage (RegExp)", () => {
110216
it("matches when the last user message satisfies the regexp", () => {
111217
const fixture = makeFixture({ userMessage: /^hello/i });

‎src/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { loadFixtureFile, loadFixturesFromDir } from "./fixture-loader.js";
1111
export { Journal } from "./journal.js";
1212

1313
// Router
14-
export { matchFixture } from "./router.js";
14+
export { matchFixture, getTextContent } from "./router.js";
1515

1616
// Provider handlers
1717
export { handleResponses } from "./responses.js";
@@ -35,6 +35,7 @@ export { writeSSEStream, writeErrorResponse } from "./sse-writer.js";
3535
export type {
3636
ChatMessage,
3737
ChatCompletionRequest,
38+
ContentPart,
3839
ToolDefinition,
3940
FixtureMatch,
4041
TextResponse,

‎src/router.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ChatCompletionRequest, ChatMessage, Fixture } from "./types.js";
1+
import type { ChatCompletionRequest, ChatMessage, ContentPart, Fixture } from "./types.js";
22

33
export function getLastMessageByRole(messages: ChatMessage[], role: string): ChatMessage | null {
44
for (let i = messages.length - 1; i >= 0; i--) {
@@ -7,6 +7,22 @@ export function getLastMessageByRole(messages: ChatMessage[], role: string): Cha
77
return null;
88
}
99

10+
/**
11+
* Extract the text content from a message's content field.
12+
* Handles both plain string content and array-of-parts content
13+
* (e.g. `[{type: "text", text: "..."}]` as sent by some SDKs).
14+
*/
15+
export function getTextContent(content: string | ContentPart[] | null): string | null {
16+
if (typeof content === "string") return content;
17+
if (Array.isArray(content)) {
18+
const texts = content
19+
.filter((p) => p.type === "text" && typeof p.text === "string" && p.text !== "")
20+
.map((p) => p.text as string);
21+
return texts.length > 0 ? texts.join("") : null;
22+
}
23+
return null;
24+
}
25+
1026
export function matchFixture(fixtures: Fixture[], req: ChatCompletionRequest): Fixture | null {
1127
for (const fixture of fixtures) {
1228
const { match } = fixture;
@@ -19,11 +35,12 @@ export function matchFixture(fixtures: Fixture[], req: ChatCompletionRequest): F
1935
// userMessage — match against the last user message content
2036
if (match.userMessage !== undefined) {
2137
const msg = getLastMessageByRole(req.messages, "user");
22-
if (!msg || typeof msg.content !== "string") continue;
38+
const text = msg ? getTextContent(msg.content) : null;
39+
if (!text) continue;
2340
if (typeof match.userMessage === "string") {
24-
if (!msg.content.includes(match.userMessage)) continue;
41+
if (!text.includes(match.userMessage)) continue;
2542
} else {
26-
if (!match.userMessage.test(msg.content)) continue;
43+
if (!match.userMessage.test(text)) continue;
2744
}
2845
}
2946

‎src/types.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// OpenAI Chat Completion request types (subset we care about)
22

3+
export interface ContentPart {
4+
type: string;
5+
text?: string;
6+
[key: string]: unknown;
7+
}
8+
39
export interface ChatMessage {
410
role: "system" | "user" | "assistant" | "tool";
5-
content: string | null;
11+
content: string | ContentPart[] | null;
612
name?: string;
713
tool_calls?: ToolCallMessage[];
814
tool_call_id?: string;

0 commit comments

Comments
 (0)