forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter-tanstack-input.test.ts
More file actions
211 lines (175 loc) · 6.64 KB
/
Copy pathconverter-tanstack-input.test.ts
File metadata and controls
211 lines (175 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { describe, it, expect } from "vitest";
import { convertInputToTanStackAI } from "../converters/tanstack";
import { createDefaultInput } from "./agent-test-helpers";
describe("convertInputToTanStackAI", () => {
// -------------------------------------------------------------------------
// Message filtering
// -------------------------------------------------------------------------
describe("message filtering", () => {
it("filters out system messages from the messages array", () => {
const input = createDefaultInput({
messages: [
{ role: "system", content: "You are helpful" },
{ role: "user", content: "Hello" },
],
});
const { messages } = convertInputToTanStackAI(input);
expect(messages).toHaveLength(1);
expect(messages[0].role).toBe("user");
expect(messages[0].content).toBe("Hello");
});
it("filters out developer messages from the messages array", () => {
const input = createDefaultInput({
messages: [
{ role: "developer", content: "Internal instruction" },
{ role: "user", content: "Hi" },
],
});
const { messages } = convertInputToTanStackAI(input);
expect(messages).toHaveLength(1);
expect(messages[0].role).toBe("user");
});
it("extracts system and developer messages into systemPrompts", () => {
const input = createDefaultInput({
messages: [
{ role: "system", content: "System prompt" },
{ role: "developer", content: "Dev instruction" },
{ role: "user", content: "Hello" },
],
});
const { systemPrompts } = convertInputToTanStackAI(input);
expect(systemPrompts).toContain("System prompt");
expect(systemPrompts).toContain("Dev instruction");
});
it("preserves user and assistant messages in order", () => {
const input = createDefaultInput({
messages: [
{ role: "user", content: "Question 1" },
{ role: "assistant", content: "Answer 1" },
{ role: "user", content: "Question 2" },
],
});
const { messages } = convertInputToTanStackAI(input);
expect(messages).toHaveLength(3);
expect(messages[0]).toMatchObject({
role: "user",
content: "Question 1",
});
expect(messages[1]).toMatchObject({
role: "assistant",
content: "Answer 1",
});
expect(messages[2]).toMatchObject({
role: "user",
content: "Question 2",
});
});
});
// -------------------------------------------------------------------------
// Tool call mapping
// -------------------------------------------------------------------------
describe("tool call mapping", () => {
it("maps assistant message toolCalls to TanStack format", () => {
const input = createDefaultInput({
messages: [
{
role: "assistant",
content: null,
toolCalls: [
{
id: "tc-1",
type: "function",
function: { name: "getWeather", arguments: '{"city":"NYC"}' },
},
],
},
],
});
const { messages } = convertInputToTanStackAI(input);
expect(messages).toHaveLength(1);
expect(messages[0].toolCalls).toHaveLength(1);
expect(messages[0].toolCalls![0]).toEqual({
id: "tc-1",
type: "function",
function: { name: "getWeather", arguments: '{"city":"NYC"}' },
});
});
it("maps tool messages with toolCallId", () => {
const input = createDefaultInput({
messages: [
{
role: "tool",
content: '{"temp": 72}',
toolCallId: "tc-1",
},
],
});
const { messages } = convertInputToTanStackAI(input);
expect(messages).toHaveLength(1);
expect(messages[0].role).toBe("tool");
expect(messages[0].toolCallId).toBe("tc-1");
});
});
// -------------------------------------------------------------------------
// Context injection
// -------------------------------------------------------------------------
describe("context injection", () => {
it("appends context entries to systemPrompts", () => {
const input = createDefaultInput({
context: [
{ description: "User preferences", value: "Dark mode enabled" },
{ description: "Current page", value: "/dashboard" },
],
});
const { systemPrompts } = convertInputToTanStackAI(input);
expect(systemPrompts).toContain("User preferences:\nDark mode enabled");
expect(systemPrompts).toContain("Current page:\n/dashboard");
});
it("does not add context when context array is empty", () => {
const input = createDefaultInput({ context: [] });
const { systemPrompts } = convertInputToTanStackAI(input);
expect(systemPrompts).toHaveLength(0);
});
});
// -------------------------------------------------------------------------
// State serialization
// -------------------------------------------------------------------------
describe("state serialization", () => {
it("serializes non-empty state into systemPrompts", () => {
const input = createDefaultInput({
state: { count: 42, items: ["a", "b"] },
});
const { systemPrompts } = convertInputToTanStackAI(input);
const statePrompt = systemPrompts.find((p) =>
p.startsWith("Application State:"),
);
expect(statePrompt).toBeDefined();
expect(statePrompt).toContain('"count": 42');
});
it("does not add state when state is empty object", () => {
const input = createDefaultInput({ state: {} });
const { systemPrompts } = convertInputToTanStackAI(input);
expect(
systemPrompts.find((p) => p.startsWith("Application State:")),
).toBeUndefined();
});
it("does not add state when state is null", () => {
const input = createDefaultInput({ state: null });
const { systemPrompts } = convertInputToTanStackAI(input);
expect(
systemPrompts.find((p) => p.startsWith("Application State:")),
).toBeUndefined();
});
});
// -------------------------------------------------------------------------
// Empty input
// -------------------------------------------------------------------------
describe("empty input", () => {
it("returns empty messages and systemPrompts for default input", () => {
const input = createDefaultInput();
const { messages, systemPrompts } = convertInputToTanStackAI(input);
expect(messages).toHaveLength(0);
expect(systemPrompts).toHaveLength(0);
});
});
});