@pantheon.ai/mcp 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1072 -0
- package/package.json +11 -2
- package/.idea/compiler.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/pantheon-mcp.iml +0 -8
- package/.idea/vcs.xml +0 -6
- package/packages/sdk/package.json +0 -16
- package/packages/sdk/src/api/auth.ts +0 -22
- package/packages/sdk/src/api/connectors.ts +0 -27
- package/packages/sdk/src/api/environments.ts +0 -53
- package/packages/sdk/src/api/projects.ts +0 -143
- package/packages/sdk/src/api/stream-proxy.ts +0 -17
- package/packages/sdk/src/index.ts +0 -16
- package/packages/sdk/src/lib/api-executor.ts +0 -169
- package/packages/sdk/src/lib/api.ts +0 -265
- package/packages/sdk/src/lib/stream-parser.ts +0 -30
- package/packages/sdk/src/lib/validator.ts +0 -19
- package/packages/sdk/src/lib/vercel-ai-hack.ts +0 -133
- package/packages/sdk/src/lib/zod.ts +0 -32
- package/packages/sdk/src/schemas/ai.ts +0 -473
- package/packages/sdk/src/schemas/branch.ts +0 -99
- package/packages/sdk/src/schemas/common.ts +0 -19
- package/packages/sdk/src/schemas/environments.ts +0 -19
- package/packages/sdk/src/schemas/exploration.ts +0 -54
- package/packages/sdk/src/schemas/fs.ts +0 -38
- package/packages/sdk/src/schemas/project.ts +0 -208
- package/packages/sdk/tsconfig.json +0 -17
- package/src/index.ts +0 -34
- package/src/mcp/mcp.ts +0 -172
- package/src/stdio.ts +0 -11
- package/src/streamableHttp.ts +0 -60
- package/tsconfig.json +0 -17
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1072 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import { getErrorMessage } from "@ai-sdk/provider";
|
|
4
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
5
|
+
import { parseTemplate } from "url-template";
|
|
6
|
+
import z$1, { z } from "zod";
|
|
7
|
+
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
|
|
8
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
9
|
+
|
|
10
|
+
//#region packages/sdk/src/lib/stream-parser.ts
|
|
11
|
+
var StreamResponseParser = class {
|
|
12
|
+
options;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
async handleResponse(response, context) {
|
|
17
|
+
this.options.validateResponse?.(response);
|
|
18
|
+
if (!response.body) throw new Error("Response body is missing");
|
|
19
|
+
return this.options.pipe(response.body, Object.freeze({
|
|
20
|
+
...context,
|
|
21
|
+
response
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region packages/sdk/src/lib/validator.ts
|
|
28
|
+
const VALIDATOR_SYMBOL = Symbol("validator");
|
|
29
|
+
function isValidator(object) {
|
|
30
|
+
return typeof object === "object" && object !== null && object[VALIDATOR_SYMBOL] === true && typeof object.parse === "function";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region packages/sdk/src/lib/api.ts
|
|
35
|
+
function typeOf() {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
function createUrlTemplate(method) {
|
|
39
|
+
return function(arr, ...params) {
|
|
40
|
+
let url = arr[0];
|
|
41
|
+
let expandedPaths = false;
|
|
42
|
+
for (let i = 1; i < arr.length; i++) if (params[i - 1] === "path*") {
|
|
43
|
+
url += "<path*>";
|
|
44
|
+
expandedPaths = true;
|
|
45
|
+
} else url += "{" + params[i - 1].replace(/\?$/, "") + "}" + arr[i];
|
|
46
|
+
const [pathPart, searchPart] = url.split("?");
|
|
47
|
+
const pathPartTemplate = expandedPaths ? { expand: (context) => {
|
|
48
|
+
return parseTemplate(pathPart.replace(/<path\*>/, String(context["path*"]).replace(/^\//, ""))).expand(context);
|
|
49
|
+
} } : parseTemplate(pathPart);
|
|
50
|
+
const searchParamsBuilders = [];
|
|
51
|
+
if (searchPart) searchPart.split("&").forEach((entry) => {
|
|
52
|
+
const [key, value = ""] = entry.split("=").map(decodeURIComponent);
|
|
53
|
+
if (/^\{[^}]+}$/.test(value)) {
|
|
54
|
+
const templateValue = value.slice(1, -1);
|
|
55
|
+
searchParamsBuilders.push((usp, context) => {
|
|
56
|
+
if (templateValue in context) {
|
|
57
|
+
const value = context[templateValue];
|
|
58
|
+
if (value === void 0) return;
|
|
59
|
+
if (Array.isArray(value)) value.forEach((v) => usp.append(key, String(v)));
|
|
60
|
+
else usp.append(key, String(value));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
} else if (value !== "") {
|
|
64
|
+
const template = parseTemplate(value);
|
|
65
|
+
searchParamsBuilders.push((usp, context) => {
|
|
66
|
+
usp.append(key, template.expand(context));
|
|
67
|
+
});
|
|
68
|
+
} else searchParamsBuilders.push((usp) => {
|
|
69
|
+
usp.append(key, "");
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
method,
|
|
74
|
+
signature: `${method} ${url}`,
|
|
75
|
+
templateUrl: url,
|
|
76
|
+
template: { expand: (context) => {
|
|
77
|
+
const pathPart = pathPartTemplate.expand(context);
|
|
78
|
+
const usp = new URLSearchParams();
|
|
79
|
+
searchParamsBuilders.forEach((template) => {
|
|
80
|
+
template(usp, context);
|
|
81
|
+
});
|
|
82
|
+
if (Array.from(usp).length > 0) return pathPart + "?" + usp.toString();
|
|
83
|
+
else return pathPart;
|
|
84
|
+
} },
|
|
85
|
+
__params__: typeOf()
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const get = createUrlTemplate("get");
|
|
90
|
+
const post = createUrlTemplate("post");
|
|
91
|
+
const put = createUrlTemplate("put");
|
|
92
|
+
const del = createUrlTemplate("delete");
|
|
93
|
+
const patch = createUrlTemplate("patch");
|
|
94
|
+
function defineApi(urlTemplate, _type, responseSchema) {
|
|
95
|
+
const method = urlTemplate.method;
|
|
96
|
+
return {
|
|
97
|
+
method: urlTemplate.method,
|
|
98
|
+
signature: urlTemplate.signature,
|
|
99
|
+
url: ((params) => urlTemplate.template.expand(params ?? {})),
|
|
100
|
+
requestInit: (body) => {
|
|
101
|
+
if (method === "get" || method === "delete") return { method };
|
|
102
|
+
if (body instanceof ReadableStream) return {
|
|
103
|
+
method,
|
|
104
|
+
body,
|
|
105
|
+
duplex: "half"
|
|
106
|
+
};
|
|
107
|
+
else if (body instanceof FormData) return {
|
|
108
|
+
method,
|
|
109
|
+
body
|
|
110
|
+
};
|
|
111
|
+
else return {
|
|
112
|
+
method,
|
|
113
|
+
body: JSON.stringify(body),
|
|
114
|
+
headers: { "Content-Type": "application/json" }
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
handleResponse: async (response, context) => {
|
|
118
|
+
if (responseSchema instanceof StreamResponseParser) return responseSchema.handleResponse(response, context);
|
|
119
|
+
else if (responseSchema === "raw") return response;
|
|
120
|
+
else if (isValidator(responseSchema)) return responseSchema.parse(response);
|
|
121
|
+
else return responseSchema.parse(await response.json());
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region packages/sdk/src/lib/zod.ts
|
|
128
|
+
var ZodStream = class extends TransformStream {
|
|
129
|
+
constructor({ schema, onParseFailed }) {
|
|
130
|
+
super({ async transform(chunk, controller) {
|
|
131
|
+
const result = await schema.safeParseAsync(chunk);
|
|
132
|
+
if (result.success) controller.enqueue(result.data);
|
|
133
|
+
else if (onParseFailed) onParseFailed(result.error, chunk, controller);
|
|
134
|
+
else controller.error(result.error);
|
|
135
|
+
} });
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const zodJsonDate = z.string();
|
|
139
|
+
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region packages/sdk/src/schemas/branch.ts
|
|
142
|
+
const branchManifestArtifactSchema = z.object({
|
|
143
|
+
type: z.string(),
|
|
144
|
+
path: z.string(),
|
|
145
|
+
description: z.string()
|
|
146
|
+
});
|
|
147
|
+
const branchManifestSchema = z.object({
|
|
148
|
+
summary: z.string().optional(),
|
|
149
|
+
export: z.object({
|
|
150
|
+
include_patterns: z.string().array(),
|
|
151
|
+
exclude_patterns: z.string().array()
|
|
152
|
+
}).optional(),
|
|
153
|
+
artifacts: branchManifestArtifactSchema.array().optional()
|
|
154
|
+
});
|
|
155
|
+
const snapSchema = z.object({
|
|
156
|
+
snap_id: z.string(),
|
|
157
|
+
snap_status: z.string(),
|
|
158
|
+
snap_type: z.string(),
|
|
159
|
+
started_at: zodJsonDate,
|
|
160
|
+
finished_at: zodJsonDate,
|
|
161
|
+
exit_code: z.number().nullable(),
|
|
162
|
+
error: z.string().nullable(),
|
|
163
|
+
step_index: z.number(),
|
|
164
|
+
event_stream_id: z.string().nullable(),
|
|
165
|
+
tool_use_id: z.string().nullable(),
|
|
166
|
+
manifest_status: z.string().nullable(),
|
|
167
|
+
manifest_task_id: z.string().nullable(),
|
|
168
|
+
manifest_snap_id: z.string().nullable()
|
|
169
|
+
});
|
|
170
|
+
const snapDetailsSchema = snapSchema.extend({
|
|
171
|
+
output: z.string().nullable(),
|
|
172
|
+
command: z.string().array().nullable().optional()
|
|
173
|
+
});
|
|
174
|
+
const branchStatusSchema = z.enum([
|
|
175
|
+
"created",
|
|
176
|
+
"pending",
|
|
177
|
+
"running",
|
|
178
|
+
"ready_for_manifest",
|
|
179
|
+
"manifesting",
|
|
180
|
+
"succeed",
|
|
181
|
+
"failed"
|
|
182
|
+
]);
|
|
183
|
+
const branchSchema = z.object({
|
|
184
|
+
id: z.string(),
|
|
185
|
+
name: z.string(),
|
|
186
|
+
display_name: z.string(),
|
|
187
|
+
status: branchStatusSchema,
|
|
188
|
+
status_text: z.string().nullable(),
|
|
189
|
+
level: z.number(),
|
|
190
|
+
project_id: z.string(),
|
|
191
|
+
parent_id: z.string().nullable(),
|
|
192
|
+
created_at: zodJsonDate,
|
|
193
|
+
updated_at: zodJsonDate,
|
|
194
|
+
latest_snap_id: z.string().nullable(),
|
|
195
|
+
latest_snap: snapSchema.nullable(),
|
|
196
|
+
manifest: branchManifestSchema.nullable()
|
|
197
|
+
});
|
|
198
|
+
const branchSnapsSchema = z.object({
|
|
199
|
+
branch_id: z.string(),
|
|
200
|
+
branch_name: z.string(),
|
|
201
|
+
branch_status: branchStatusSchema,
|
|
202
|
+
branch_status_text: z.string().nullable(),
|
|
203
|
+
steps: z.array(snapDetailsSchema),
|
|
204
|
+
steps_total: z.number()
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region packages/sdk/src/schemas/common.ts
|
|
209
|
+
function paged(schema) {
|
|
210
|
+
return z.object({
|
|
211
|
+
items: z.array(schema),
|
|
212
|
+
total: z.number(),
|
|
213
|
+
page: z.number(),
|
|
214
|
+
size: z.number(),
|
|
215
|
+
pages: z.number()
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region packages/sdk/src/schemas/exploration.ts
|
|
221
|
+
const explorationSchema = z.object({
|
|
222
|
+
id: z.string(),
|
|
223
|
+
project_id: z.string(),
|
|
224
|
+
parent_branch_id: z.string(),
|
|
225
|
+
tdd_baseline_branch_id: z.string().nullable(),
|
|
226
|
+
background_task_id: z.string().nullable(),
|
|
227
|
+
num_branches: z.number().int(),
|
|
228
|
+
shared_prompt_sequence: z.string().array(),
|
|
229
|
+
status_text: z.string(),
|
|
230
|
+
call_agent: z.string().nullable(),
|
|
231
|
+
started_at: zodJsonDate.nullable(),
|
|
232
|
+
ended_at: zodJsonDate.nullable(),
|
|
233
|
+
created_at: zodJsonDate,
|
|
234
|
+
updated_at: zodJsonDate,
|
|
235
|
+
agent: z.string(),
|
|
236
|
+
status: z.string()
|
|
237
|
+
});
|
|
238
|
+
const explorationResultSchema = z.object({
|
|
239
|
+
id: z.string(),
|
|
240
|
+
project_id: z.string(),
|
|
241
|
+
parent_branch_id: z.string(),
|
|
242
|
+
baseline_branch_id: z.string().nullable(),
|
|
243
|
+
background_task_id: z.string().nullable(),
|
|
244
|
+
status: z.string(),
|
|
245
|
+
status_text: z.string(),
|
|
246
|
+
started_at: zodJsonDate,
|
|
247
|
+
ended_at: zodJsonDate.nullable(),
|
|
248
|
+
branches: z.object({
|
|
249
|
+
branch_id: z.string(),
|
|
250
|
+
branch_name: z.string(),
|
|
251
|
+
branch_display_name: z.string(),
|
|
252
|
+
latest_snap_id: z.string(),
|
|
253
|
+
execution_results: z.object({
|
|
254
|
+
execution_id: z.string(),
|
|
255
|
+
status: z.string(),
|
|
256
|
+
status_text: z.string(),
|
|
257
|
+
branch_id: z.string(),
|
|
258
|
+
snap_id: z.string(),
|
|
259
|
+
background_task_id: z.string().nullable(),
|
|
260
|
+
started_at: zodJsonDate,
|
|
261
|
+
last_polled_at: zodJsonDate,
|
|
262
|
+
ended_at: zodJsonDate.nullable(),
|
|
263
|
+
exit_code: z.number().nullable()
|
|
264
|
+
}).array()
|
|
265
|
+
}).array()
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
//#region packages/sdk/src/schemas/fs.ts
|
|
270
|
+
const fsEntrySchema = z.object({
|
|
271
|
+
name: z.string(),
|
|
272
|
+
is_dir: z.boolean(),
|
|
273
|
+
size: z.number(),
|
|
274
|
+
mode: z.string(),
|
|
275
|
+
mod_time: zodJsonDate
|
|
276
|
+
});
|
|
277
|
+
const FSResponseValidator = Object.freeze({
|
|
278
|
+
[VALIDATOR_SYMBOL]: true,
|
|
279
|
+
__phantom__: typeOf(),
|
|
280
|
+
async parse(response) {
|
|
281
|
+
if (response.headers.get("Content-Type") === "application/json+directory") {
|
|
282
|
+
const data = await response.json();
|
|
283
|
+
return fsEntrySchema.array().parse(data);
|
|
284
|
+
} else {
|
|
285
|
+
const blob = await response.blob();
|
|
286
|
+
return new File([blob], response.url.split("/").pop(), { type: response.headers.get("content-type") ?? "application/octet-stream" });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region packages/sdk/src/schemas/ai.ts
|
|
293
|
+
const providerMetadataSchema = z$1.record(z$1.string(), z$1.record(z$1.string(), z$1.any()));
|
|
294
|
+
const messageMetadataSchema = z$1.object().loose();
|
|
295
|
+
const toolsShapeMap = { fake: {
|
|
296
|
+
input: z$1.object({ fakeIn: z$1.string() }).loose(),
|
|
297
|
+
output: z$1.object({ fakeOut: z$1.string() }).loose()
|
|
298
|
+
} };
|
|
299
|
+
const dataTypes = {
|
|
300
|
+
delta: z$1.object().loose(),
|
|
301
|
+
"message-metadata": z$1.object().loose(),
|
|
302
|
+
"ui-collapsed": z$1.object({
|
|
303
|
+
toolParts: z$1.number(),
|
|
304
|
+
reasoningParts: z$1.number(),
|
|
305
|
+
collapsed: z$1.boolean(),
|
|
306
|
+
parts: z$1.object().loose().array()
|
|
307
|
+
}),
|
|
308
|
+
"ui-running-tools": z$1.object({
|
|
309
|
+
toolParts: z$1.object().loose().array(),
|
|
310
|
+
textParts: z$1.object().loose().array(),
|
|
311
|
+
reasoningParts: z$1.object().loose().array()
|
|
312
|
+
})
|
|
313
|
+
};
|
|
314
|
+
const chunksShapeMap = {
|
|
315
|
+
"text-start": {
|
|
316
|
+
id: z$1.string(),
|
|
317
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
318
|
+
},
|
|
319
|
+
"text-delta": {
|
|
320
|
+
delta: z$1.string(),
|
|
321
|
+
id: z$1.string(),
|
|
322
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
323
|
+
},
|
|
324
|
+
"text-end": {
|
|
325
|
+
id: z$1.string(),
|
|
326
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
327
|
+
},
|
|
328
|
+
"reasoning-start": {
|
|
329
|
+
id: z$1.string(),
|
|
330
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
331
|
+
},
|
|
332
|
+
"reasoning-delta": {
|
|
333
|
+
delta: z$1.string(),
|
|
334
|
+
id: z$1.string(),
|
|
335
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
336
|
+
},
|
|
337
|
+
"reasoning-end": {
|
|
338
|
+
id: z$1.string(),
|
|
339
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
340
|
+
},
|
|
341
|
+
error: { errorText: z$1.string() },
|
|
342
|
+
"tool-input-available": {
|
|
343
|
+
toolCallId: z$1.string(),
|
|
344
|
+
toolName: z$1.string(),
|
|
345
|
+
input: z$1.unknown(),
|
|
346
|
+
providerExecuted: z$1.boolean().optional(),
|
|
347
|
+
providerMetadata: providerMetadataSchema.optional(),
|
|
348
|
+
dynamic: z$1.boolean().optional()
|
|
349
|
+
},
|
|
350
|
+
"tool-input-error": {
|
|
351
|
+
toolCallId: z$1.string(),
|
|
352
|
+
toolName: z$1.string(),
|
|
353
|
+
input: z$1.unknown(),
|
|
354
|
+
providerExecuted: z$1.boolean().optional(),
|
|
355
|
+
providerMetadata: providerMetadataSchema.optional(),
|
|
356
|
+
dynamic: z$1.boolean().optional(),
|
|
357
|
+
errorText: z$1.string()
|
|
358
|
+
},
|
|
359
|
+
"tool-output-available": {
|
|
360
|
+
toolCallId: z$1.string(),
|
|
361
|
+
output: z$1.unknown(),
|
|
362
|
+
providerExecuted: z$1.boolean().optional(),
|
|
363
|
+
dynamic: z$1.boolean().optional(),
|
|
364
|
+
preliminary: z$1.boolean().optional()
|
|
365
|
+
},
|
|
366
|
+
"tool-output-error": {
|
|
367
|
+
toolCallId: z$1.string(),
|
|
368
|
+
providerExecuted: z$1.boolean().optional(),
|
|
369
|
+
dynamic: z$1.boolean().optional(),
|
|
370
|
+
errorText: z$1.string()
|
|
371
|
+
},
|
|
372
|
+
"tool-input-start": {
|
|
373
|
+
toolCallId: z$1.string(),
|
|
374
|
+
toolName: z$1.string(),
|
|
375
|
+
providerExecuted: z$1.boolean().optional(),
|
|
376
|
+
dynamic: z$1.boolean().optional()
|
|
377
|
+
},
|
|
378
|
+
"tool-input-delta": {
|
|
379
|
+
toolCallId: z$1.string(),
|
|
380
|
+
inputTextDelta: z$1.string()
|
|
381
|
+
},
|
|
382
|
+
"source-url": {
|
|
383
|
+
sourceId: z$1.string(),
|
|
384
|
+
url: z$1.string(),
|
|
385
|
+
title: z$1.string().optional(),
|
|
386
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
387
|
+
},
|
|
388
|
+
"source-document": {
|
|
389
|
+
sourceId: z$1.string(),
|
|
390
|
+
mediaType: z$1.string(),
|
|
391
|
+
title: z$1.string(),
|
|
392
|
+
filename: z$1.string().optional(),
|
|
393
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
394
|
+
},
|
|
395
|
+
file: {
|
|
396
|
+
url: z$1.string(),
|
|
397
|
+
mediaType: z$1.string()
|
|
398
|
+
},
|
|
399
|
+
"start-step": {},
|
|
400
|
+
"finish-step": {},
|
|
401
|
+
start: {
|
|
402
|
+
messageId: z$1.string().optional(),
|
|
403
|
+
messageMetadata: messageMetadataSchema.optional()
|
|
404
|
+
},
|
|
405
|
+
finish: { messageMetadata: messageMetadataSchema.optional() },
|
|
406
|
+
abort: {},
|
|
407
|
+
"message-metadata": { messageMetadata: messageMetadataSchema }
|
|
408
|
+
};
|
|
409
|
+
const unknownDataChunkSchema = z$1.object({
|
|
410
|
+
type: z$1.string().regex(/^data-/),
|
|
411
|
+
id: z$1.string().optional(),
|
|
412
|
+
data: z$1.unknown(),
|
|
413
|
+
transient: z$1.boolean().optional()
|
|
414
|
+
});
|
|
415
|
+
const dataChunkSchema = Object.entries(dataTypes).map(([name, schema]) => z$1.object({
|
|
416
|
+
type: z$1.literal(`data-${name}`),
|
|
417
|
+
id: z$1.string().optional(),
|
|
418
|
+
data: schema,
|
|
419
|
+
transient: z$1.boolean().optional()
|
|
420
|
+
}));
|
|
421
|
+
const chunkSchema = Object.entries(chunksShapeMap).map(([type, schema]) => z$1.object({
|
|
422
|
+
type: z$1.literal(type),
|
|
423
|
+
...schema
|
|
424
|
+
}));
|
|
425
|
+
const uiMessageChunkSchema = z$1.discriminatedUnion("type", [...chunkSchema, ...dataChunkSchema]);
|
|
426
|
+
const partsShapeMap = {
|
|
427
|
+
text: {
|
|
428
|
+
text: z$1.string(),
|
|
429
|
+
state: z$1.enum(["streaming", "done"]).optional(),
|
|
430
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
431
|
+
},
|
|
432
|
+
reasoning: {
|
|
433
|
+
text: z$1.string(),
|
|
434
|
+
state: z$1.enum(["streaming", "done"]).optional(),
|
|
435
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
436
|
+
},
|
|
437
|
+
"source-url": {
|
|
438
|
+
sourceId: z$1.string(),
|
|
439
|
+
url: z$1.string(),
|
|
440
|
+
title: z$1.string().optional(),
|
|
441
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
442
|
+
},
|
|
443
|
+
"source-document": {
|
|
444
|
+
sourceId: z$1.string(),
|
|
445
|
+
mediaType: z$1.string(),
|
|
446
|
+
title: z$1.string(),
|
|
447
|
+
filename: z$1.string().optional(),
|
|
448
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
449
|
+
},
|
|
450
|
+
file: {
|
|
451
|
+
url: z$1.string(),
|
|
452
|
+
mediaType: z$1.string(),
|
|
453
|
+
filename: z$1.string().optional(),
|
|
454
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
455
|
+
},
|
|
456
|
+
"step-start": {}
|
|
457
|
+
};
|
|
458
|
+
const dynamicToolSchema = z$1.discriminatedUnion("state", [
|
|
459
|
+
z$1.object({
|
|
460
|
+
type: z$1.literal("dynamic-tool"),
|
|
461
|
+
toolName: z$1.string(),
|
|
462
|
+
toolCallId: z$1.string(),
|
|
463
|
+
state: z$1.literal("input-streaming"),
|
|
464
|
+
input: z$1.unknown().optional(),
|
|
465
|
+
output: z$1.never().optional(),
|
|
466
|
+
errorText: z$1.never().optional()
|
|
467
|
+
}),
|
|
468
|
+
z$1.object({
|
|
469
|
+
type: z$1.literal("dynamic-tool"),
|
|
470
|
+
toolName: z$1.string(),
|
|
471
|
+
toolCallId: z$1.string(),
|
|
472
|
+
state: z$1.literal("input-available"),
|
|
473
|
+
input: z$1.unknown(),
|
|
474
|
+
output: z$1.never().optional(),
|
|
475
|
+
errorText: z$1.never().optional(),
|
|
476
|
+
callProviderMetadata: providerMetadataSchema.optional()
|
|
477
|
+
}),
|
|
478
|
+
z$1.object({
|
|
479
|
+
type: z$1.literal("dynamic-tool"),
|
|
480
|
+
toolName: z$1.string(),
|
|
481
|
+
toolCallId: z$1.string(),
|
|
482
|
+
state: z$1.literal("output-available"),
|
|
483
|
+
input: z$1.unknown(),
|
|
484
|
+
output: z$1.unknown(),
|
|
485
|
+
errorText: z$1.never().optional(),
|
|
486
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
487
|
+
preliminary: z$1.boolean().optional()
|
|
488
|
+
}),
|
|
489
|
+
z$1.object({
|
|
490
|
+
type: z$1.literal("dynamic-tool"),
|
|
491
|
+
toolName: z$1.string(),
|
|
492
|
+
toolCallId: z$1.string(),
|
|
493
|
+
state: z$1.literal("output-error"),
|
|
494
|
+
input: z$1.unknown(),
|
|
495
|
+
output: z$1.never().optional(),
|
|
496
|
+
errorText: z$1.string(),
|
|
497
|
+
callProviderMetadata: providerMetadataSchema.optional()
|
|
498
|
+
})
|
|
499
|
+
]);
|
|
500
|
+
const dataPartSchema = Object.entries(dataTypes).map(([name, schema]) => z$1.object({
|
|
501
|
+
type: z$1.literal(`data-${name}`),
|
|
502
|
+
id: z$1.string().optional(),
|
|
503
|
+
data: schema
|
|
504
|
+
}));
|
|
505
|
+
const notDefinedDataType = z$1.string().regex(/data-/).refine((type) => !Object.keys(dataTypes).includes(type.slice(5)));
|
|
506
|
+
const unknownDataPartSchema = z$1.object({
|
|
507
|
+
type: notDefinedDataType,
|
|
508
|
+
id: z$1.string().optional(),
|
|
509
|
+
data: z$1.unknown()
|
|
510
|
+
});
|
|
511
|
+
const notDefinedToolType = z$1.string().regex(/tool-/).refine((type) => !Object.keys(toolsShapeMap).includes(type.slice(5)));
|
|
512
|
+
const unknownToolPartSchema = z$1.discriminatedUnion("state", [
|
|
513
|
+
z$1.object({
|
|
514
|
+
type: notDefinedToolType,
|
|
515
|
+
toolCallId: z$1.string(),
|
|
516
|
+
state: z$1.literal("input-streaming"),
|
|
517
|
+
input: z$1.unknown().optional(),
|
|
518
|
+
output: z$1.never().optional(),
|
|
519
|
+
providerExecuted: z$1.boolean().optional(),
|
|
520
|
+
errorText: z$1.never().optional()
|
|
521
|
+
}),
|
|
522
|
+
z$1.object({
|
|
523
|
+
type: notDefinedToolType,
|
|
524
|
+
toolCallId: z$1.string(),
|
|
525
|
+
state: z$1.literal("input-available"),
|
|
526
|
+
input: z$1.unknown(),
|
|
527
|
+
output: z$1.never().optional(),
|
|
528
|
+
providerExecuted: z$1.boolean().optional(),
|
|
529
|
+
errorText: z$1.never().optional(),
|
|
530
|
+
callProviderMetadata: providerMetadataSchema.optional()
|
|
531
|
+
}),
|
|
532
|
+
z$1.object({
|
|
533
|
+
type: notDefinedToolType,
|
|
534
|
+
toolCallId: z$1.string(),
|
|
535
|
+
state: z$1.literal("output-available"),
|
|
536
|
+
input: z$1.unknown(),
|
|
537
|
+
output: z$1.unknown(),
|
|
538
|
+
errorText: z$1.never().optional(),
|
|
539
|
+
providerExecuted: z$1.boolean().optional(),
|
|
540
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
541
|
+
preliminary: z$1.boolean().optional()
|
|
542
|
+
}),
|
|
543
|
+
z$1.object({
|
|
544
|
+
type: notDefinedToolType,
|
|
545
|
+
toolCallId: z$1.string(),
|
|
546
|
+
state: z$1.literal("output-error"),
|
|
547
|
+
input: z$1.unknown().optional(),
|
|
548
|
+
rawInput: z$1.unknown().optional(),
|
|
549
|
+
output: z$1.never().optional(),
|
|
550
|
+
errorText: z$1.string(),
|
|
551
|
+
providerExecuted: z$1.boolean().optional(),
|
|
552
|
+
callProviderMetadata: providerMetadataSchema.optional()
|
|
553
|
+
})
|
|
554
|
+
]);
|
|
555
|
+
const partSchema = Object.entries(partsShapeMap).map(([type, schema]) => z$1.object({
|
|
556
|
+
type: z$1.literal(type),
|
|
557
|
+
...schema
|
|
558
|
+
}));
|
|
559
|
+
const toolsPartSchema = Object.entries(toolsShapeMap).map(([type, schema]) => z$1.discriminatedUnion("state", [
|
|
560
|
+
z$1.object({
|
|
561
|
+
type: z$1.literal(`tool-${type}`),
|
|
562
|
+
toolCallId: z$1.string(),
|
|
563
|
+
state: z$1.literal("input-streaming"),
|
|
564
|
+
input: schema.input.partial().optional(),
|
|
565
|
+
output: z$1.never().optional(),
|
|
566
|
+
providerExecuted: z$1.boolean().optional(),
|
|
567
|
+
errorText: z$1.never().optional()
|
|
568
|
+
}),
|
|
569
|
+
z$1.object({
|
|
570
|
+
type: z$1.literal(`tool-${type}`),
|
|
571
|
+
toolCallId: z$1.string(),
|
|
572
|
+
state: z$1.literal("input-available"),
|
|
573
|
+
input: schema.input,
|
|
574
|
+
output: z$1.never().optional(),
|
|
575
|
+
providerExecuted: z$1.boolean().optional(),
|
|
576
|
+
errorText: z$1.never().optional(),
|
|
577
|
+
callProviderMetadata: providerMetadataSchema.optional()
|
|
578
|
+
}),
|
|
579
|
+
z$1.object({
|
|
580
|
+
type: z$1.literal(`tool-${type}`),
|
|
581
|
+
toolCallId: z$1.string(),
|
|
582
|
+
state: z$1.literal("output-available"),
|
|
583
|
+
input: schema.input,
|
|
584
|
+
output: schema.output,
|
|
585
|
+
errorText: z$1.never().optional(),
|
|
586
|
+
providerExecuted: z$1.boolean().optional(),
|
|
587
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
588
|
+
preliminary: z$1.boolean().optional()
|
|
589
|
+
}),
|
|
590
|
+
z$1.object({
|
|
591
|
+
type: z$1.literal(`tool-${type}`),
|
|
592
|
+
toolCallId: z$1.string(),
|
|
593
|
+
state: z$1.literal("output-error"),
|
|
594
|
+
input: schema.input.optional(),
|
|
595
|
+
rawInput: z$1.unknown().optional(),
|
|
596
|
+
output: z$1.never().optional(),
|
|
597
|
+
errorText: z$1.string(),
|
|
598
|
+
providerExecuted: z$1.boolean().optional(),
|
|
599
|
+
callProviderMetadata: providerMetadataSchema.optional()
|
|
600
|
+
})
|
|
601
|
+
]));
|
|
602
|
+
const uiMessagePartSchema = z$1.discriminatedUnion("type", [
|
|
603
|
+
...partSchema,
|
|
604
|
+
...toolsPartSchema,
|
|
605
|
+
...dataPartSchema,
|
|
606
|
+
dynamicToolSchema
|
|
607
|
+
]);
|
|
608
|
+
|
|
609
|
+
//#endregion
|
|
610
|
+
//#region packages/sdk/src/schemas/project.ts
|
|
611
|
+
const projectSchema = z.object({
|
|
612
|
+
id: z.string(),
|
|
613
|
+
name: z.string(),
|
|
614
|
+
display_name: z.string(),
|
|
615
|
+
category: z.string(),
|
|
616
|
+
description: z.string(),
|
|
617
|
+
user_id: z.string(),
|
|
618
|
+
root_branch_id: z.string().nullable(),
|
|
619
|
+
ongoing_stream_id: z.string().nullable(),
|
|
620
|
+
status: z.enum([
|
|
621
|
+
"CREATED",
|
|
622
|
+
"PROVISIONING",
|
|
623
|
+
"READY",
|
|
624
|
+
"RUNNING",
|
|
625
|
+
"PAUSED",
|
|
626
|
+
"FAILED",
|
|
627
|
+
"ARCHIVED"
|
|
628
|
+
]).nullable().optional(),
|
|
629
|
+
error_message: z.string().nullable().optional(),
|
|
630
|
+
created_at: zodJsonDate,
|
|
631
|
+
updated_at: zodJsonDate
|
|
632
|
+
});
|
|
633
|
+
const projectMessageSchema = z.object({
|
|
634
|
+
id: z.string(),
|
|
635
|
+
role: z.enum([
|
|
636
|
+
"system",
|
|
637
|
+
"user",
|
|
638
|
+
"assistant"
|
|
639
|
+
]),
|
|
640
|
+
metadata: messageMetadataSchema.nullable().optional(),
|
|
641
|
+
parts: z.array(z.object().loose()),
|
|
642
|
+
ordinal: z.number(),
|
|
643
|
+
project_id: z.string(),
|
|
644
|
+
source_request_id: z.string().nullable().optional(),
|
|
645
|
+
event_stream_id: z.string().nullable(),
|
|
646
|
+
status: z.string().nullable(),
|
|
647
|
+
is_finished: z.boolean(),
|
|
648
|
+
created_at: zodJsonDate,
|
|
649
|
+
updated_at: zodJsonDate
|
|
650
|
+
});
|
|
651
|
+
const projectMessageStreamParser = new StreamResponseParser({ pipe: (input) => {
|
|
652
|
+
return input.pipeThrough(new TextDecoderStream()).pipeThrough(new BufferedLinesStream("\n\n")).pipeThrough(new SSEDataDecoderStream()).pipeThrough(new ZodStream({ schema: z.object().loose() }));
|
|
653
|
+
} });
|
|
654
|
+
const projectRawMessageStreamParser = new StreamResponseParser({ pipe: (input) => {
|
|
655
|
+
return input.pipeThrough(new TextDecoderStream()).pipeThrough(new BufferedLinesStream("\n\n")).pipeThrough(new SSEDataDecoderStream()).pipeThrough(new ZodStream({ schema: z.object().loose() }));
|
|
656
|
+
} });
|
|
657
|
+
const projectQueuedRequestSchema = z.object({
|
|
658
|
+
id: z.string(),
|
|
659
|
+
type: z.string(),
|
|
660
|
+
payload: z.unknown(),
|
|
661
|
+
requested_at: zodJsonDate,
|
|
662
|
+
summary: z.string()
|
|
663
|
+
});
|
|
664
|
+
const projectBackgroundTaskSchema = z.object({
|
|
665
|
+
task_id: z.string(),
|
|
666
|
+
name: z.string().nullable(),
|
|
667
|
+
action_type: z.enum(["execution", "exploration"]),
|
|
668
|
+
action_id: z.string(),
|
|
669
|
+
status: z.enum([
|
|
670
|
+
"SUCCESS",
|
|
671
|
+
"FAILURE",
|
|
672
|
+
"PENDING",
|
|
673
|
+
"STARTED"
|
|
674
|
+
]).nullable(),
|
|
675
|
+
result: z.object().loose().nullable(),
|
|
676
|
+
retries: z.number().nullable(),
|
|
677
|
+
started_at: zodJsonDate.nullable(),
|
|
678
|
+
last_polled_at: zodJsonDate.nullable(),
|
|
679
|
+
ended_at: zodJsonDate.nullable()
|
|
680
|
+
});
|
|
681
|
+
var BufferedLinesStream = class extends TransformStream {
|
|
682
|
+
constructor(sep = "\n") {
|
|
683
|
+
let buffer = "";
|
|
684
|
+
super({
|
|
685
|
+
transform(chunk, controller) {
|
|
686
|
+
chunk = buffer += chunk;
|
|
687
|
+
while (true) {
|
|
688
|
+
const index = chunk.indexOf(sep);
|
|
689
|
+
if (index === -1) {
|
|
690
|
+
buffer = chunk;
|
|
691
|
+
break;
|
|
692
|
+
}
|
|
693
|
+
const line = chunk.slice(0, index).trim();
|
|
694
|
+
if (line) controller.enqueue(line);
|
|
695
|
+
chunk = chunk.slice(index + sep.length);
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
flush() {
|
|
699
|
+
if (buffer) console.warn("buffered lines stream has unprocessed lines:", buffer);
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
var SSEDataDecoderStream = class extends TransformStream {
|
|
705
|
+
constructor() {
|
|
706
|
+
let handle = void 0;
|
|
707
|
+
super({ transform: (chunk, controller) => {
|
|
708
|
+
clearTimeout(handle);
|
|
709
|
+
handle = setTimeout(() => {
|
|
710
|
+
controller.error(/* @__PURE__ */ new Error("Read timeout, please retry."));
|
|
711
|
+
}, 15e3);
|
|
712
|
+
if (/^data:/.test(chunk)) {
|
|
713
|
+
const part = chunk.replace(/^data: /, "").trim();
|
|
714
|
+
if (part === "[DONE]") {} else try {
|
|
715
|
+
controller.enqueue(JSON.parse(part));
|
|
716
|
+
} catch (e) {
|
|
717
|
+
console.error("invalid sse chunk", chunk, e);
|
|
718
|
+
controller.error(e);
|
|
719
|
+
}
|
|
720
|
+
} else console.debug("ignore sse comment", chunk);
|
|
721
|
+
} });
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
//#endregion
|
|
726
|
+
//#region packages/sdk/src/api/projects.ts
|
|
727
|
+
const listProjects = defineApi(get`/api/v1/projects?page=${"page"}&size=${"size"}&user_id=${"user_id?"}&category=${"category?"}`, null, paged(projectSchema));
|
|
728
|
+
const getProject = defineApi(get`/api/v1/projects/${"projectId"}`, null, projectSchema);
|
|
729
|
+
const createProject = defineApi(post`/api/v1/projects`, typeOf(), projectSchema);
|
|
730
|
+
const listProjectMessages = defineApi(get`/api/v1/projects/${"projectId"}/messages`, null, z.object({
|
|
731
|
+
messages: projectMessageSchema.array(),
|
|
732
|
+
ongoing_stream_id: z.string().nullable()
|
|
733
|
+
}));
|
|
734
|
+
const deleteProjectMessage = defineApi(del`/api/v1/projects/${"projectId"}/messages/${"messageId"}`, null, "raw");
|
|
735
|
+
const postProjectMessage = defineApi(post`/api/v1/projects/${"projectId"}`, typeOf(), z.object({
|
|
736
|
+
messages: projectMessageSchema.array(),
|
|
737
|
+
ongoing_stream_id: z.string().nullable()
|
|
738
|
+
}));
|
|
739
|
+
const continueProject = defineApi(post`/api/v1/projects/${"projectId"}/continue`, typeOf(), "raw");
|
|
740
|
+
const stopProject = defineApi(post`/api/v1/projects/${"projectId"}/stop`, typeOf(), "raw");
|
|
741
|
+
const getProjectBranch = defineApi(get`/api/v1/projects/${"projectId"}/branches/${"branchId"}`, null, branchSchema);
|
|
742
|
+
const getProjectBranchOutput = defineApi(get`/api/v1/projects/${"projectId"}/branches/${"branchId"}/output?full_output=${"fullOutput?"}`, null, z.string());
|
|
743
|
+
const listProjectBranches = defineApi(get`/api/v1/projects/${"projectId"}/branches?page=${"page"}&size=${"size"}`, null, paged(branchSchema));
|
|
744
|
+
const listProjectBranchSnaps = defineApi(get`/api/v1/projects/${"projectId"}/branches/${"branchId"}/snaps`, null, branchSnapsSchema);
|
|
745
|
+
const listProjectBackgroundTasks = defineApi(get`/api/v1/projects/${"projectId"}/background_tasks?statuses=${"statuses?"}`, null, z.array(projectBackgroundTaskSchema));
|
|
746
|
+
const createProjectExploration = defineApi(post`/api/v1/projects/${"projectId"}/explorations`, typeOf(), explorationSchema);
|
|
747
|
+
const listProjectExplorations = defineApi(get`/api/v1/projects/${"projectId"}/explorations`, null, explorationSchema.array());
|
|
748
|
+
const getProjectExplorationResult = defineApi(get`/api/v1/projects/${"projectId"}/explorations/${"explorationId"}/result`, null, explorationResultSchema);
|
|
749
|
+
const getProjectBranchFs = defineApi(get`/api/v1/projects/${"projectId"}/branches/${"branchId"}/fs/${"path*"}`, null, FSResponseValidator);
|
|
750
|
+
const getProjectBranchFsPreview = defineApi(get`/api/v1/projects/${"projectId"}/branches/${"branchId"}/preview/${"path*"}`, null, FSResponseValidator);
|
|
751
|
+
|
|
752
|
+
//#endregion
|
|
753
|
+
//#region packages/sdk/src/api/connectors.ts
|
|
754
|
+
const getGithubUser = defineApi(get`/api/v1/connectors/github/user`, null, z.object({ github_username: z.string() }).nullable());
|
|
755
|
+
const listGithubRepositories = defineApi(get`/api/v1/user/github/repositories?page=${"page"}&size=${"size"}`, null, paged(z.object({
|
|
756
|
+
repo_id: z.number(),
|
|
757
|
+
repo_name: z.string(),
|
|
758
|
+
public: z.boolean(),
|
|
759
|
+
installation_id: z.number()
|
|
760
|
+
})));
|
|
761
|
+
|
|
762
|
+
//#endregion
|
|
763
|
+
//#region packages/sdk/src/api/stream-proxy.ts
|
|
764
|
+
const readMessageStream = defineApi(get`/ai_stream_proxy/v2/streams/${"streamId"}/stream?format=vercel-ai-ui-message-stream-v1&skip=${"skip?"}`, null, projectMessageStreamParser);
|
|
765
|
+
const readRawMessageStream = defineApi(get`/ai_stream_proxy/v2/streams/${"streamId"}/stream?format=opaque-stream-json`, null, projectRawMessageStreamParser);
|
|
766
|
+
|
|
767
|
+
//#endregion
|
|
768
|
+
//#region packages/sdk/src/api/auth.ts
|
|
769
|
+
const login = defineApi(post`/api/v1/auth/login`, typeOf(), "raw");
|
|
770
|
+
const logout = defineApi(post`/api/v1/auth/logout`, null, "raw");
|
|
771
|
+
const getMe = defineApi(get`/api/v1/users/me`, null, z.object({
|
|
772
|
+
id: z.string(),
|
|
773
|
+
email: z.string(),
|
|
774
|
+
is_active: z.boolean(),
|
|
775
|
+
is_superuser: z.boolean(),
|
|
776
|
+
is_verified: z.boolean()
|
|
777
|
+
}));
|
|
778
|
+
|
|
779
|
+
//#endregion
|
|
780
|
+
//#region packages/sdk/src/lib/api-executor.ts
|
|
781
|
+
var ApiError = class extends Error {
|
|
782
|
+
response;
|
|
783
|
+
context;
|
|
784
|
+
responseBody;
|
|
785
|
+
constructor(response, context, responseBody) {
|
|
786
|
+
super(`${context.request.method} ${context.url}: ${response.status} ${response.statusText} ${getServerErrorMessage(responseBody)}`);
|
|
787
|
+
this.response = response;
|
|
788
|
+
this.context = context;
|
|
789
|
+
this.responseBody = responseBody;
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
function createApiExecutor({ baseUrl, headers, validateResponse }) {
|
|
793
|
+
const errorCallbacks = [];
|
|
794
|
+
function onError(callback) {
|
|
795
|
+
errorCallbacks.push(callback);
|
|
796
|
+
return () => {
|
|
797
|
+
const index = errorCallbacks.indexOf(callback);
|
|
798
|
+
if (index !== -1) errorCallbacks.splice(index, 1);
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
async function execute(api, params, requestBody, requestInit) {
|
|
802
|
+
const url = baseUrl ? new URL(api.url(params), baseUrl).toString() : api.url(params);
|
|
803
|
+
const { headers: initHeaders, ...init } = api.requestInit(requestBody);
|
|
804
|
+
const executorHeaders = new Headers(headers);
|
|
805
|
+
if (initHeaders) new Headers(initHeaders).forEach((value, key) => {
|
|
806
|
+
executorHeaders.set(key, value);
|
|
807
|
+
});
|
|
808
|
+
const request = new Request(url, {
|
|
809
|
+
...requestInit,
|
|
810
|
+
...init,
|
|
811
|
+
headers: executorHeaders,
|
|
812
|
+
credentials: "include"
|
|
813
|
+
});
|
|
814
|
+
const context = Object.freeze({
|
|
815
|
+
api,
|
|
816
|
+
params,
|
|
817
|
+
requestBody,
|
|
818
|
+
url,
|
|
819
|
+
request
|
|
820
|
+
});
|
|
821
|
+
try {
|
|
822
|
+
const response = await fetch(request);
|
|
823
|
+
await validateResponse(response, context);
|
|
824
|
+
return await api.handleResponse(response, context);
|
|
825
|
+
} catch (error) {
|
|
826
|
+
for (const callback of errorCallbacks) callback(error, context);
|
|
827
|
+
return Promise.reject(error);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
return Object.freeze({
|
|
831
|
+
execute,
|
|
832
|
+
onError
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
async function validateResponse(response, context) {
|
|
836
|
+
if (!response.ok) throw new ApiError(response, context, await response.clone().json().catch(() => response.clone().text()).catch(() => void 0));
|
|
837
|
+
}
|
|
838
|
+
function getServerErrorMessage(error) {
|
|
839
|
+
if (error == null) return "No error message.";
|
|
840
|
+
if (typeof error === "string") return error;
|
|
841
|
+
if (typeof error === "object") {
|
|
842
|
+
for (const key of [
|
|
843
|
+
"message",
|
|
844
|
+
"error",
|
|
845
|
+
"errMsg",
|
|
846
|
+
"errorMsg",
|
|
847
|
+
"detail",
|
|
848
|
+
"details"
|
|
849
|
+
]) if (key in error) return String(error[key]);
|
|
850
|
+
return JSON.stringify(error, void 0, 2);
|
|
851
|
+
}
|
|
852
|
+
return String(error);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
//#endregion
|
|
856
|
+
//#region package.json
|
|
857
|
+
var version = "0.0.3";
|
|
858
|
+
|
|
859
|
+
//#endregion
|
|
860
|
+
//#region src/mcp/mcp.ts
|
|
861
|
+
async function mcpServer({ authorizationHeader }) {
|
|
862
|
+
const executor = createApiExecutor({
|
|
863
|
+
baseUrl: "https://pantheon-ai.tidb.ai/",
|
|
864
|
+
headers: authorizationHeader ? { "Authorization": authorizationHeader } : void 0,
|
|
865
|
+
validateResponse
|
|
866
|
+
});
|
|
867
|
+
try {
|
|
868
|
+
await executor.execute(getMe, null, null);
|
|
869
|
+
} catch {
|
|
870
|
+
throw 401;
|
|
871
|
+
}
|
|
872
|
+
const mcpServer = new McpServer({
|
|
873
|
+
name: "Pantheon MCP",
|
|
874
|
+
version,
|
|
875
|
+
websiteUrl: "https://pantheon-ai.tidb.ai/"
|
|
876
|
+
});
|
|
877
|
+
function registerApiTool(name, api, { paramsSchema, bodySchema, ...options }) {
|
|
878
|
+
mcpServer.registerTool(name, {
|
|
879
|
+
...options,
|
|
880
|
+
inputSchema: z.object({
|
|
881
|
+
params: paramsSchema,
|
|
882
|
+
body: bodySchema
|
|
883
|
+
})
|
|
884
|
+
}, async ({ params, body }) => {
|
|
885
|
+
try {
|
|
886
|
+
const result = await executor.execute(api, params, body);
|
|
887
|
+
return {
|
|
888
|
+
content: [{
|
|
889
|
+
type: "text",
|
|
890
|
+
text: JSON.stringify(result)
|
|
891
|
+
}],
|
|
892
|
+
structuredContent: result
|
|
893
|
+
};
|
|
894
|
+
} catch (e) {
|
|
895
|
+
return { content: [{
|
|
896
|
+
type: "text",
|
|
897
|
+
text: getErrorMessage(e)
|
|
898
|
+
}] };
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
registerApiTool("listProjects", listProjects, {
|
|
903
|
+
title: "List pantheon projects",
|
|
904
|
+
paramsSchema: pageParams,
|
|
905
|
+
bodySchema: z.null(),
|
|
906
|
+
outputSchema: paged(projectSchema)
|
|
907
|
+
});
|
|
908
|
+
registerApiTool("createProject", createProject, {
|
|
909
|
+
title: "Create pantheon project",
|
|
910
|
+
paramsSchema: z.null(),
|
|
911
|
+
bodySchema: z.object({
|
|
912
|
+
first_prompt: z.string(),
|
|
913
|
+
project_type: z.enum(["general", "github"]),
|
|
914
|
+
github_repo_id: z.number().optional(),
|
|
915
|
+
github_installation_id: z.number().optional(),
|
|
916
|
+
env_id: z.string().optional()
|
|
917
|
+
}),
|
|
918
|
+
outputSchema: projectSchema
|
|
919
|
+
});
|
|
920
|
+
registerApiTool("listProjectBranches", listProjectBranches, {
|
|
921
|
+
title: "List project branches",
|
|
922
|
+
paramsSchema: pageParams.extend({ projectId: z.string() }),
|
|
923
|
+
bodySchema: z.null(),
|
|
924
|
+
outputSchema: paged(branchSchema)
|
|
925
|
+
});
|
|
926
|
+
registerApiTool("getProject", getProject, {
|
|
927
|
+
title: "Get pantheon project",
|
|
928
|
+
paramsSchema: z.object({ projectId: z.string() }),
|
|
929
|
+
bodySchema: z.null(),
|
|
930
|
+
outputSchema: projectSchema
|
|
931
|
+
});
|
|
932
|
+
registerApiTool("getProjectBranch", getProjectBranch, {
|
|
933
|
+
title: "Get pantheon project branch info",
|
|
934
|
+
paramsSchema: z.object({
|
|
935
|
+
projectId: z.string(),
|
|
936
|
+
branchId: z.string()
|
|
937
|
+
}),
|
|
938
|
+
bodySchema: z.null(),
|
|
939
|
+
outputSchema: branchSchema
|
|
940
|
+
});
|
|
941
|
+
registerApiTool("getProjectBranchOutput", getProjectBranchOutput, {
|
|
942
|
+
title: "Get pantheon project branch output",
|
|
943
|
+
paramsSchema: z.object({
|
|
944
|
+
projectId: z.string(),
|
|
945
|
+
branchId: z.string(),
|
|
946
|
+
fullOutput: z.boolean().optional().default(false)
|
|
947
|
+
}),
|
|
948
|
+
bodySchema: z.null(),
|
|
949
|
+
outputSchema: z.string()
|
|
950
|
+
});
|
|
951
|
+
registerApiTool("createProjectExploration", createProjectExploration, {
|
|
952
|
+
title: "Create pantheon project exploration",
|
|
953
|
+
paramsSchema: z.object({ projectId: z.string() }),
|
|
954
|
+
bodySchema: z.object({
|
|
955
|
+
parent_branch_id: z.string(),
|
|
956
|
+
shared_prompt_sequence: z.string().array().min(1).max(4),
|
|
957
|
+
num_branches: z.number().int().min(1).max(50).optional().default(3),
|
|
958
|
+
agent: z.string().optional()
|
|
959
|
+
}),
|
|
960
|
+
outputSchema: explorationSchema
|
|
961
|
+
});
|
|
962
|
+
registerApiTool("listProjectExplorations", listProjectExplorations, {
|
|
963
|
+
title: "List project explorations",
|
|
964
|
+
paramsSchema: z.object({ projectId: z.string() }),
|
|
965
|
+
bodySchema: z.null(),
|
|
966
|
+
outputSchema: explorationSchema.array()
|
|
967
|
+
});
|
|
968
|
+
registerApiTool("getProjectExplorationResult", getProjectExplorationResult, {
|
|
969
|
+
title: "Get project exploration result",
|
|
970
|
+
paramsSchema: z.object({
|
|
971
|
+
projectId: z.string(),
|
|
972
|
+
explorationId: z.string()
|
|
973
|
+
}),
|
|
974
|
+
bodySchema: z.null(),
|
|
975
|
+
outputSchema: explorationResultSchema
|
|
976
|
+
});
|
|
977
|
+
return mcpServer;
|
|
978
|
+
}
|
|
979
|
+
const pageParams = z.object({
|
|
980
|
+
page: z.number().int().min(1),
|
|
981
|
+
size: z.number().int().min(1).max(100)
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
//#endregion
|
|
985
|
+
//#region src/stdio.ts
|
|
986
|
+
async function startStdio({ apiKey }) {
|
|
987
|
+
const stdio = new StdioServerTransport();
|
|
988
|
+
await (await mcpServer({ authorizationHeader: `Bearer ${apiKey}` })).connect(stdio);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
//#endregion
|
|
992
|
+
//#region src/streamableHttp.ts
|
|
993
|
+
const port = process.env.PORT ? parseInt(process.env.PORT) : 9986;
|
|
994
|
+
async function startStreamableHttpServer({ apiKey }) {
|
|
995
|
+
const app = createMcpExpressApp();
|
|
996
|
+
app.all("/mcp", async (req, res) => {
|
|
997
|
+
const authorizationHeader = req.get("authorization") ?? (apiKey ? `Bearer ${apiKey}` : void 0);
|
|
998
|
+
let server;
|
|
999
|
+
try {
|
|
1000
|
+
server = await mcpServer({ authorizationHeader });
|
|
1001
|
+
} catch (e) {
|
|
1002
|
+
if (e === 401) {
|
|
1003
|
+
res.status(400).send("401 Need Login");
|
|
1004
|
+
return;
|
|
1005
|
+
} else {
|
|
1006
|
+
console.error("Error creating MCP server:", e);
|
|
1007
|
+
res.status(500).send("Internal server error");
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
|
|
1012
|
+
try {
|
|
1013
|
+
await server.connect(transport);
|
|
1014
|
+
await transport.handleRequest(req, res, req.body);
|
|
1015
|
+
res.on("close", () => {
|
|
1016
|
+
transport.close();
|
|
1017
|
+
server.close();
|
|
1018
|
+
});
|
|
1019
|
+
} catch (error) {
|
|
1020
|
+
console.error("Error handling MCP request:", error);
|
|
1021
|
+
if (!res.headersSent) res.status(500).json({
|
|
1022
|
+
jsonrpc: "2.0",
|
|
1023
|
+
error: {
|
|
1024
|
+
code: -32603,
|
|
1025
|
+
message: "Internal server error"
|
|
1026
|
+
},
|
|
1027
|
+
id: null
|
|
1028
|
+
});
|
|
1029
|
+
transport.close().catch(() => {});
|
|
1030
|
+
server.close().catch(() => {});
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
app.listen(port);
|
|
1034
|
+
console.log("Listening on port 3001");
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
//#endregion
|
|
1038
|
+
//#region src/index.ts
|
|
1039
|
+
const args = process.argv.slice(2);
|
|
1040
|
+
function printHelp(message) {
|
|
1041
|
+
console.error(`${message || "Usage"}
|
|
1042
|
+
npx @pantheon.ai/mcp stdio <Api-Key>
|
|
1043
|
+
PORT=10086 npx @pantheon.ai/mcp http [Optional Api-Key]
|
|
1044
|
+
`);
|
|
1045
|
+
}
|
|
1046
|
+
if (args.includes("--help") || args.includes("-h") || !args.length) {
|
|
1047
|
+
printHelp();
|
|
1048
|
+
process.exit(0);
|
|
1049
|
+
}
|
|
1050
|
+
if (args.includes("--version")) {
|
|
1051
|
+
console.error(version);
|
|
1052
|
+
process.exit(0);
|
|
1053
|
+
}
|
|
1054
|
+
switch (args[0]) {
|
|
1055
|
+
case "stdio":
|
|
1056
|
+
if (!args[1]) {
|
|
1057
|
+
printHelp("Api-Key is required for stdio mode");
|
|
1058
|
+
process.exit(1);
|
|
1059
|
+
}
|
|
1060
|
+
await startStdio({ apiKey: args[1] });
|
|
1061
|
+
break;
|
|
1062
|
+
case "http":
|
|
1063
|
+
if (!args[1]) console.error("API Key not provided, client connections must provide authentication header.");
|
|
1064
|
+
await startStreamableHttpServer({ apiKey: args[1] });
|
|
1065
|
+
break;
|
|
1066
|
+
default:
|
|
1067
|
+
printHelp();
|
|
1068
|
+
process.exit(1);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
//#endregion
|
|
1072
|
+
export { };
|