-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetPrompt.test.ts
More file actions
251 lines (207 loc) · 6.92 KB
/
getPrompt.test.ts
File metadata and controls
251 lines (207 loc) · 6.92 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { test, expect, describe } from "bun:test"
import { getPrompt, buildSystemPrompt } from "utils/getPrompt"
import type { AgentChatConfig } from "store"
describe("getPrompt", () => {
test("should return a lazy function", () => {
const lazyPrompt = getPrompt("system.md")
expect(typeof lazyPrompt).toBe("function")
})
test("should load system prompt from file when invoked", async () => {
const lazyPrompt = getPrompt("system.md")
const prompt = await lazyPrompt()
expect(typeof prompt).toBe("string")
expect(prompt.length).toBeGreaterThan(0)
})
test("should trim whitespace from prompt", async () => {
const lazyPrompt = getPrompt("system.md")
const prompt = await lazyPrompt()
expect(prompt).toBe(prompt.trim())
})
})
describe("buildSystemPrompt", () => {
test("should include current date", async () => {
const config: AgentChatConfig = {
mcpServers: {},
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("Current date:")
})
test("should use default prompt when systemPrompt is not provided", async () => {
const config: AgentChatConfig = {
mcpServers: {},
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("You are a helpful agent.")
})
test("should use custom systemPrompt when provided", async () => {
const config: AgentChatConfig = {
mcpServers: {},
systemPrompt: async () => "You are a custom agent.",
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("You are a custom agent.")
expect(prompt).not.toContain("You are a helpful agent.")
})
test("should include MCP server prompts", async () => {
const config: AgentChatConfig = {
mcpServers: {
github: {
command: "node",
args: [],
description: "GitHub server",
prompt: async () => "GitHub server instructions",
},
},
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("# github MCP Server")
expect(prompt).toContain("GitHub server instructions")
})
test("should combine multiple MCP server prompts", async () => {
const config: AgentChatConfig = {
mcpServers: {
github: {
command: "node",
args: [],
description: "GitHub server",
prompt: async () => "GitHub instructions",
},
gitlab: {
command: "node",
args: [],
description: "GitLab server",
prompt: async () => "GitLab instructions",
},
},
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("# github MCP Server")
expect(prompt).toContain("GitHub instructions")
expect(prompt).toContain("# gitlab MCP Server")
expect(prompt).toContain("GitLab instructions")
})
test("should skip MCP servers without prompts", async () => {
const config: AgentChatConfig = {
mcpServers: {
github: {
command: "node",
args: [],
description: "GitHub server",
prompt: async () => "GitHub instructions",
},
gitlab: {
command: "node",
args: [],
description: "GitLab server",
},
},
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("# github MCP Server")
expect(prompt).not.toContain("# gitlab MCP Server")
})
test("should build prompt with custom system prompt and MCP prompts", async () => {
const config: AgentChatConfig = {
mcpServers: {
github: {
command: "node",
args: [],
description: "GitHub server",
prompt: async () => "GitHub instructions",
},
},
systemPrompt: async () => "Custom base prompt",
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("Current date:")
expect(prompt).toContain("Custom base prompt")
expect(prompt).toContain("# github MCP Server")
expect(prompt).toContain("GitHub instructions")
})
test("should include inferred MCP servers in system prompt", async () => {
const config: AgentChatConfig = {
mcpServers: {},
}
const inferredServers = new Set(["github", "gitlab"])
const prompt = await buildSystemPrompt({
config,
inferredServers,
})
expect(prompt).toContain("Server Selection Context")
expect(prompt).toContain("github, gitlab")
})
test("should include connected and failed MCP servers sections", async () => {
const config: AgentChatConfig = {
mcpServers: {},
}
const mcpServers = [
{ name: "github", status: "connected" },
{ name: "gitlab", status: "failed" },
]
const prompt = await buildSystemPrompt({
config,
mcpServers,
})
expect(prompt).toContain("Available MCP Servers")
expect(prompt).toContain("- github")
expect(prompt).toContain("ONLY servers with available tools")
expect(prompt).toContain("Unavailable MCP Servers")
expect(prompt).toContain("- gitlab")
expect(prompt).toContain("These servers have NO tools available")
})
test("should not include connection status sections when no mcpServers provided", async () => {
const config: AgentChatConfig = {
mcpServers: {},
}
const prompt = await buildSystemPrompt({
config,
})
expect(prompt).not.toContain("Available MCP Servers")
expect(prompt).not.toContain("Unavailable MCP Servers")
})
test("should handle empty inferred servers set", async () => {
const config: AgentChatConfig = {
mcpServers: {},
}
const inferredServers = new Set<string>()
const prompt = await buildSystemPrompt({
config,
inferredServers,
})
expect(prompt).toContain("Current date:")
expect(prompt).not.toContain("Server Selection Context")
})
test("should work without inferredServers parameter", async () => {
const config: AgentChatConfig = {
mcpServers: {},
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("Current date:")
expect(prompt).not.toContain("Server Selection Context")
})
test("should skip disabled MCP servers", async () => {
const config: AgentChatConfig = {
mcpServers: {
github: {
command: "node",
args: [],
description: "GitHub server",
prompt: async () => "GitHub instructions",
enabled: true,
},
gitlab: {
command: "node",
args: [],
description: "GitLab server",
prompt: async () => "GitLab instructions",
enabled: false,
},
},
}
const prompt = await buildSystemPrompt({ config })
expect(prompt).toContain("# github MCP Server")
expect(prompt).toContain("GitHub instructions")
expect(prompt).not.toContain("# gitlab MCP Server")
expect(prompt).not.toContain("GitLab instructions")
})
})