@jeffreycao/copilot-api 1.5.7 → 1.5.8
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/main.js +1 -1
- package/dist/{server-BowRWuxD.js → server-k1q2UIFm.js} +112 -116
- package/dist/server-k1q2UIFm.js.map +1 -0
- package/dist/{start-CnFPQFgt.js → start-YQSDnOkf.js} +2 -2
- package/dist/{start-CnFPQFgt.js.map → start-YQSDnOkf.js.map} +1 -1
- package/package.json +1 -1
- package/dist/server-BowRWuxD.js.map +0 -1
package/dist/main.js
CHANGED
|
@@ -23,7 +23,7 @@ if (typeof args["enterprise-url"] === "string") process.env.COPILOT_API_ENTERPRI
|
|
|
23
23
|
const { auth } = await import("./auth-k8txLjfN.js");
|
|
24
24
|
const { checkUsage } = await import("./check-usage-DKdBAt3u.js");
|
|
25
25
|
const { debug } = await import("./debug-DcC7ZPH0.js");
|
|
26
|
-
const { start } = await import("./start-
|
|
26
|
+
const { start } = await import("./start-YQSDnOkf.js");
|
|
27
27
|
const main = defineCommand({
|
|
28
28
|
meta: {
|
|
29
29
|
name: "copilot-api",
|
|
@@ -248,6 +248,117 @@ async function checkRateLimit(state$1) {
|
|
|
248
248
|
consola.info("Rate limit wait completed, proceeding with request");
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/services/copilot/create-chat-completions.ts
|
|
253
|
+
const createChatCompletions = async (payload, options) => {
|
|
254
|
+
if (!state.copilotToken) throw new Error("Copilot token not found");
|
|
255
|
+
const enableVision = payload.messages.some((x) => typeof x.content !== "string" && x.content?.some((x$1) => x$1.type === "image_url"));
|
|
256
|
+
let isAgentCall = false;
|
|
257
|
+
if (payload.messages.length > 0) {
|
|
258
|
+
const lastMessage = payload.messages.at(-1);
|
|
259
|
+
if (lastMessage) isAgentCall = ["assistant", "tool"].includes(lastMessage.role);
|
|
260
|
+
}
|
|
261
|
+
const headers = {
|
|
262
|
+
...copilotHeaders(state, options.requestId, enableVision),
|
|
263
|
+
"x-initiator": isAgentCall ? "agent" : "user"
|
|
264
|
+
};
|
|
265
|
+
prepareInteractionHeaders(options.sessionId, Boolean(options.subagentMarker), headers);
|
|
266
|
+
prepareForCompact(headers, options.isCompact);
|
|
267
|
+
const response = await fetch(`${copilotBaseUrl(state)}/chat/completions`, {
|
|
268
|
+
method: "POST",
|
|
269
|
+
headers,
|
|
270
|
+
body: JSON.stringify(payload)
|
|
271
|
+
});
|
|
272
|
+
if (!response.ok) {
|
|
273
|
+
consola.error("Failed to create chat completions", response);
|
|
274
|
+
throw new HTTPError("Failed to create chat completions", response);
|
|
275
|
+
}
|
|
276
|
+
if (payload.stream) return events(response);
|
|
277
|
+
return await response.json();
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/routes/chat-completions/handler.ts
|
|
282
|
+
const logger$6 = createHandlerLogger("chat-completions-handler");
|
|
283
|
+
async function handleCompletion$1(c) {
|
|
284
|
+
await checkRateLimit(state);
|
|
285
|
+
let payload = await c.req.json();
|
|
286
|
+
debugJsonTail(logger$6, "Request payload:", {
|
|
287
|
+
value: payload,
|
|
288
|
+
tailLength: 400
|
|
289
|
+
});
|
|
290
|
+
const selectedModel = state.models?.data.find((model) => model.id === payload.model);
|
|
291
|
+
if (selectedModel?.id === "gpt-5.4") return c.json({ error: {
|
|
292
|
+
message: "Please use `/v1/responses` or `/v1/messages` API",
|
|
293
|
+
type: "invalid_request_error"
|
|
294
|
+
} }, 400);
|
|
295
|
+
if (state.manualApprove) await awaitApproval();
|
|
296
|
+
if (isNullish(payload.max_tokens)) {
|
|
297
|
+
payload = {
|
|
298
|
+
...payload,
|
|
299
|
+
max_tokens: selectedModel?.capabilities.limits.max_output_tokens
|
|
300
|
+
};
|
|
301
|
+
debugJson(logger$6, "Set max_tokens to:", payload.max_tokens);
|
|
302
|
+
}
|
|
303
|
+
const requestId = generateRequestIdFromPayload(payload);
|
|
304
|
+
logger$6.debug("Generated request ID:", requestId);
|
|
305
|
+
const sessionId = getUUID(requestId);
|
|
306
|
+
logger$6.debug("Extracted session ID:", sessionId);
|
|
307
|
+
const response = await createChatCompletions(payload, {
|
|
308
|
+
requestId,
|
|
309
|
+
sessionId
|
|
310
|
+
});
|
|
311
|
+
if (isNonStreaming$1(response)) {
|
|
312
|
+
debugJson(logger$6, "Non-streaming response:", response);
|
|
313
|
+
return c.json(response);
|
|
314
|
+
}
|
|
315
|
+
logger$6.debug("Streaming response");
|
|
316
|
+
return streamSSE(c, async (stream) => {
|
|
317
|
+
for await (const chunk of response) {
|
|
318
|
+
debugJson(logger$6, "Streaming chunk:", chunk);
|
|
319
|
+
await stream.writeSSE(chunk);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
const isNonStreaming$1 = (response) => Object.hasOwn(response, "choices");
|
|
324
|
+
|
|
325
|
+
//#endregion
|
|
326
|
+
//#region src/routes/chat-completions/route.ts
|
|
327
|
+
const completionRoutes = new Hono();
|
|
328
|
+
completionRoutes.post("/", async (c) => {
|
|
329
|
+
try {
|
|
330
|
+
return await handleCompletion$1(c);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
return await forwardError(c, error);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/services/copilot/create-embeddings.ts
|
|
338
|
+
const createEmbeddings = async (payload) => {
|
|
339
|
+
if (!state.copilotToken) throw new Error("Copilot token not found");
|
|
340
|
+
const response = await fetch(`${copilotBaseUrl(state)}/embeddings`, {
|
|
341
|
+
method: "POST",
|
|
342
|
+
headers: copilotHeaders(state),
|
|
343
|
+
body: JSON.stringify(payload)
|
|
344
|
+
});
|
|
345
|
+
if (!response.ok) throw new HTTPError("Failed to create embeddings", response);
|
|
346
|
+
return await response.json();
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
//#endregion
|
|
350
|
+
//#region src/routes/embeddings/route.ts
|
|
351
|
+
const embeddingRoutes = new Hono();
|
|
352
|
+
embeddingRoutes.post("/", async (c) => {
|
|
353
|
+
try {
|
|
354
|
+
const paylod = await c.req.json();
|
|
355
|
+
const response = await createEmbeddings(paylod);
|
|
356
|
+
return c.json(response);
|
|
357
|
+
} catch (error) {
|
|
358
|
+
return await forwardError(c, error);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
|
|
251
362
|
//#endregion
|
|
252
363
|
//#region src/lib/tokenizer.ts
|
|
253
364
|
const ENCODING_MAP = {
|
|
@@ -464,121 +575,6 @@ const getTokenCount = async (payload, model) => {
|
|
|
464
575
|
};
|
|
465
576
|
};
|
|
466
577
|
|
|
467
|
-
//#endregion
|
|
468
|
-
//#region src/services/copilot/create-chat-completions.ts
|
|
469
|
-
const createChatCompletions = async (payload, options) => {
|
|
470
|
-
if (!state.copilotToken) throw new Error("Copilot token not found");
|
|
471
|
-
const enableVision = payload.messages.some((x) => typeof x.content !== "string" && x.content?.some((x$1) => x$1.type === "image_url"));
|
|
472
|
-
let isAgentCall = false;
|
|
473
|
-
if (payload.messages.length > 0) {
|
|
474
|
-
const lastMessage = payload.messages.at(-1);
|
|
475
|
-
if (lastMessage) isAgentCall = ["assistant", "tool"].includes(lastMessage.role);
|
|
476
|
-
}
|
|
477
|
-
const headers = {
|
|
478
|
-
...copilotHeaders(state, options.requestId, enableVision),
|
|
479
|
-
"x-initiator": isAgentCall ? "agent" : "user"
|
|
480
|
-
};
|
|
481
|
-
prepareInteractionHeaders(options.sessionId, Boolean(options.subagentMarker), headers);
|
|
482
|
-
prepareForCompact(headers, options.isCompact);
|
|
483
|
-
const response = await fetch(`${copilotBaseUrl(state)}/chat/completions`, {
|
|
484
|
-
method: "POST",
|
|
485
|
-
headers,
|
|
486
|
-
body: JSON.stringify(payload)
|
|
487
|
-
});
|
|
488
|
-
if (!response.ok) {
|
|
489
|
-
consola.error("Failed to create chat completions", response);
|
|
490
|
-
throw new HTTPError("Failed to create chat completions", response);
|
|
491
|
-
}
|
|
492
|
-
if (payload.stream) return events(response);
|
|
493
|
-
return await response.json();
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
//#endregion
|
|
497
|
-
//#region src/routes/chat-completions/handler.ts
|
|
498
|
-
const logger$6 = createHandlerLogger("chat-completions-handler");
|
|
499
|
-
async function handleCompletion$1(c) {
|
|
500
|
-
await checkRateLimit(state);
|
|
501
|
-
let payload = await c.req.json();
|
|
502
|
-
debugJsonTail(logger$6, "Request payload:", {
|
|
503
|
-
value: payload,
|
|
504
|
-
tailLength: 400
|
|
505
|
-
});
|
|
506
|
-
const selectedModel = state.models?.data.find((model) => model.id === payload.model);
|
|
507
|
-
try {
|
|
508
|
-
if (selectedModel) {
|
|
509
|
-
const tokenCount = await getTokenCount(payload, selectedModel);
|
|
510
|
-
logger$6.info("Current token count:", tokenCount);
|
|
511
|
-
} else logger$6.warn("No model selected, skipping token count calculation");
|
|
512
|
-
} catch (error) {
|
|
513
|
-
logger$6.warn("Failed to calculate token count:", error);
|
|
514
|
-
}
|
|
515
|
-
if (state.manualApprove) await awaitApproval();
|
|
516
|
-
if (isNullish(payload.max_tokens)) {
|
|
517
|
-
payload = {
|
|
518
|
-
...payload,
|
|
519
|
-
max_tokens: selectedModel?.capabilities.limits.max_output_tokens
|
|
520
|
-
};
|
|
521
|
-
debugJson(logger$6, "Set max_tokens to:", payload.max_tokens);
|
|
522
|
-
}
|
|
523
|
-
const requestId = generateRequestIdFromPayload(payload);
|
|
524
|
-
logger$6.debug("Generated request ID:", requestId);
|
|
525
|
-
const sessionId = getUUID(requestId);
|
|
526
|
-
logger$6.debug("Extracted session ID:", sessionId);
|
|
527
|
-
const response = await createChatCompletions(payload, {
|
|
528
|
-
requestId,
|
|
529
|
-
sessionId
|
|
530
|
-
});
|
|
531
|
-
if (isNonStreaming$1(response)) {
|
|
532
|
-
debugJson(logger$6, "Non-streaming response:", response);
|
|
533
|
-
return c.json(response);
|
|
534
|
-
}
|
|
535
|
-
logger$6.debug("Streaming response");
|
|
536
|
-
return streamSSE(c, async (stream) => {
|
|
537
|
-
for await (const chunk of response) {
|
|
538
|
-
debugJson(logger$6, "Streaming chunk:", chunk);
|
|
539
|
-
await stream.writeSSE(chunk);
|
|
540
|
-
}
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
const isNonStreaming$1 = (response) => Object.hasOwn(response, "choices");
|
|
544
|
-
|
|
545
|
-
//#endregion
|
|
546
|
-
//#region src/routes/chat-completions/route.ts
|
|
547
|
-
const completionRoutes = new Hono();
|
|
548
|
-
completionRoutes.post("/", async (c) => {
|
|
549
|
-
try {
|
|
550
|
-
return await handleCompletion$1(c);
|
|
551
|
-
} catch (error) {
|
|
552
|
-
return await forwardError(c, error);
|
|
553
|
-
}
|
|
554
|
-
});
|
|
555
|
-
|
|
556
|
-
//#endregion
|
|
557
|
-
//#region src/services/copilot/create-embeddings.ts
|
|
558
|
-
const createEmbeddings = async (payload) => {
|
|
559
|
-
if (!state.copilotToken) throw new Error("Copilot token not found");
|
|
560
|
-
const response = await fetch(`${copilotBaseUrl(state)}/embeddings`, {
|
|
561
|
-
method: "POST",
|
|
562
|
-
headers: copilotHeaders(state),
|
|
563
|
-
body: JSON.stringify(payload)
|
|
564
|
-
});
|
|
565
|
-
if (!response.ok) throw new HTTPError("Failed to create embeddings", response);
|
|
566
|
-
return await response.json();
|
|
567
|
-
};
|
|
568
|
-
|
|
569
|
-
//#endregion
|
|
570
|
-
//#region src/routes/embeddings/route.ts
|
|
571
|
-
const embeddingRoutes = new Hono();
|
|
572
|
-
embeddingRoutes.post("/", async (c) => {
|
|
573
|
-
try {
|
|
574
|
-
const paylod = await c.req.json();
|
|
575
|
-
const response = await createEmbeddings(paylod);
|
|
576
|
-
return c.json(response);
|
|
577
|
-
} catch (error) {
|
|
578
|
-
return await forwardError(c, error);
|
|
579
|
-
}
|
|
580
|
-
});
|
|
581
|
-
|
|
582
578
|
//#endregion
|
|
583
579
|
//#region src/lib/models.ts
|
|
584
580
|
const findEndpointModel = (sdkModelId) => {
|
|
@@ -3008,4 +3004,4 @@ server.route("/:provider/v1/models", providerModelRoutes);
|
|
|
3008
3004
|
|
|
3009
3005
|
//#endregion
|
|
3010
3006
|
export { server };
|
|
3011
|
-
//# sourceMappingURL=server-
|
|
3007
|
+
//# sourceMappingURL=server-k1q2UIFm.js.map
|