@gajae-code/ai 0.1.1
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/CHANGELOG.md +2644 -0
- package/README.md +1181 -0
- package/dist/types/api-registry.d.ts +30 -0
- package/dist/types/auth-broker/client.d.ts +66 -0
- package/dist/types/auth-broker/index.d.ts +5 -0
- package/dist/types/auth-broker/refresher.d.ts +25 -0
- package/dist/types/auth-broker/remote-store.d.ts +96 -0
- package/dist/types/auth-broker/server.d.ts +32 -0
- package/dist/types/auth-broker/types.d.ts +105 -0
- package/dist/types/auth-broker/wire-schemas.d.ts +412 -0
- package/dist/types/auth-gateway/http.d.ts +39 -0
- package/dist/types/auth-gateway/index.d.ts +3 -0
- package/dist/types/auth-gateway/server.d.ts +17 -0
- package/dist/types/auth-gateway/types.d.ts +115 -0
- package/dist/types/auth-storage.d.ts +641 -0
- package/dist/types/cli.d.ts +2 -0
- package/dist/types/index.d.ts +49 -0
- package/dist/types/model-cache.d.ts +17 -0
- package/dist/types/model-manager.d.ts +62 -0
- package/dist/types/model-thinking.d.ts +71 -0
- package/dist/types/models.d.ts +12 -0
- package/dist/types/provider-details.d.ts +24 -0
- package/dist/types/provider-models/bundled-references.d.ts +4 -0
- package/dist/types/provider-models/descriptors.d.ts +48 -0
- package/dist/types/provider-models/google.d.ts +20 -0
- package/dist/types/provider-models/index.d.ts +5 -0
- package/dist/types/provider-models/ollama.d.ts +7 -0
- package/dist/types/provider-models/openai-compat.d.ts +237 -0
- package/dist/types/provider-models/special.d.ts +16 -0
- package/dist/types/providers/amazon-bedrock.d.ts +36 -0
- package/dist/types/providers/anthropic-messages-server-schema.d.ts +450 -0
- package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
- package/dist/types/providers/anthropic.d.ts +188 -0
- package/dist/types/providers/aws-credentials.d.ts +43 -0
- package/dist/types/providers/aws-eventstream.d.ts +38 -0
- package/dist/types/providers/aws-sigv4.d.ts +55 -0
- package/dist/types/providers/azure-openai-responses.d.ts +15 -0
- package/dist/types/providers/cursor/gen/agent_pb.d.ts +13022 -0
- package/dist/types/providers/cursor.d.ts +42 -0
- package/dist/types/providers/error-message.d.ts +27 -0
- package/dist/types/providers/github-copilot-headers.d.ts +40 -0
- package/dist/types/providers/gitlab-duo.d.ts +27 -0
- package/dist/types/providers/google-auth.d.ts +24 -0
- package/dist/types/providers/google-gemini-cli.d.ts +72 -0
- package/dist/types/providers/google-gemini-headers.d.ts +18 -0
- package/dist/types/providers/google-shared.d.ts +163 -0
- package/dist/types/providers/google-types.d.ts +138 -0
- package/dist/types/providers/google-vertex.d.ts +7 -0
- package/dist/types/providers/google.d.ts +4 -0
- package/dist/types/providers/grammar.d.ts +1 -0
- package/dist/types/providers/kimi.d.ts +27 -0
- package/dist/types/providers/mock.d.ts +175 -0
- package/dist/types/providers/ollama.d.ts +6 -0
- package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
- package/dist/types/providers/openai-chat-server-schema.d.ts +814 -0
- package/dist/types/providers/openai-chat-server.d.ts +16 -0
- package/dist/types/providers/openai-codex/constants.d.ts +26 -0
- package/dist/types/providers/openai-codex/request-transformer.d.ts +49 -0
- package/dist/types/providers/openai-codex/response-handler.d.ts +17 -0
- package/dist/types/providers/openai-codex-responses.d.ts +67 -0
- package/dist/types/providers/openai-completions-compat.d.ts +25 -0
- package/dist/types/providers/openai-completions.d.ts +33 -0
- package/dist/types/providers/openai-responses-server-schema.d.ts +392 -0
- package/dist/types/providers/openai-responses-server.d.ts +17 -0
- package/dist/types/providers/openai-responses-shared.d.ts +89 -0
- package/dist/types/providers/openai-responses.d.ts +32 -0
- package/dist/types/providers/pi-native-client.d.ts +13 -0
- package/dist/types/providers/pi-native-server.d.ts +68 -0
- package/dist/types/providers/register-builtins.d.ts +31 -0
- package/dist/types/providers/synthetic.d.ts +26 -0
- package/dist/types/providers/transform-messages.d.ts +12 -0
- package/dist/types/providers/vision-guard.d.ts +8 -0
- package/dist/types/rate-limit-utils.d.ts +19 -0
- package/dist/types/stream.d.ts +24 -0
- package/dist/types/types.d.ts +746 -0
- package/dist/types/usage/claude.d.ts +3 -0
- package/dist/types/usage/gemini.d.ts +2 -0
- package/dist/types/usage/github-copilot.d.ts +7 -0
- package/dist/types/usage/google-antigravity.d.ts +2 -0
- package/dist/types/usage/kimi.d.ts +2 -0
- package/dist/types/usage/minimax-code.d.ts +2 -0
- package/dist/types/usage/openai-codex.d.ts +3 -0
- package/dist/types/usage/shared.d.ts +1 -0
- package/dist/types/usage/zai.d.ts +2 -0
- package/dist/types/usage.d.ts +258 -0
- package/dist/types/utils/abort.d.ts +19 -0
- package/dist/types/utils/anthropic-auth.d.ts +31 -0
- package/dist/types/utils/discovery/antigravity.d.ts +61 -0
- package/dist/types/utils/discovery/codex.d.ts +38 -0
- package/dist/types/utils/discovery/cursor.d.ts +23 -0
- package/dist/types/utils/discovery/gemini.d.ts +25 -0
- package/dist/types/utils/discovery/index.d.ts +4 -0
- package/dist/types/utils/discovery/openai-compatible.d.ts +72 -0
- package/dist/types/utils/event-stream.d.ts +28 -0
- package/dist/types/utils/fireworks-model-id.d.ts +10 -0
- package/dist/types/utils/foundry.d.ts +1 -0
- package/dist/types/utils/h2-fetch.d.ts +22 -0
- package/dist/types/utils/http-inspector.d.ts +31 -0
- package/dist/types/utils/idle-iterator.d.ts +67 -0
- package/dist/types/utils/json-parse.d.ts +10 -0
- package/dist/types/utils/oauth/alibaba-coding-plan.d.ts +18 -0
- package/dist/types/utils/oauth/anthropic.d.ts +22 -0
- package/dist/types/utils/oauth/api-key-login.d.ts +35 -0
- package/dist/types/utils/oauth/api-key-validation.d.ts +27 -0
- package/dist/types/utils/oauth/callback-server.d.ts +57 -0
- package/dist/types/utils/oauth/cerebras.d.ts +1 -0
- package/dist/types/utils/oauth/cloudflare-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/cursor.d.ts +15 -0
- package/dist/types/utils/oauth/deepseek.d.ts +10 -0
- package/dist/types/utils/oauth/firepass.d.ts +1 -0
- package/dist/types/utils/oauth/fireworks.d.ts +1 -0
- package/dist/types/utils/oauth/github-copilot.d.ts +38 -0
- package/dist/types/utils/oauth/gitlab-duo.d.ts +3 -0
- package/dist/types/utils/oauth/google-antigravity.d.ts +11 -0
- package/dist/types/utils/oauth/google-gemini-cli.d.ts +10 -0
- package/dist/types/utils/oauth/google-oauth-shared.d.ts +28 -0
- package/dist/types/utils/oauth/huggingface.d.ts +19 -0
- package/dist/types/utils/oauth/index.d.ts +38 -0
- package/dist/types/utils/oauth/kagi.d.ts +17 -0
- package/dist/types/utils/oauth/kilo.d.ts +5 -0
- package/dist/types/utils/oauth/kimi.d.ts +21 -0
- package/dist/types/utils/oauth/litellm.d.ts +18 -0
- package/dist/types/utils/oauth/lm-studio.d.ts +17 -0
- package/dist/types/utils/oauth/minimax-code.d.ts +28 -0
- package/dist/types/utils/oauth/moonshot.d.ts +1 -0
- package/dist/types/utils/oauth/nanogpt.d.ts +1 -0
- package/dist/types/utils/oauth/nvidia.d.ts +18 -0
- package/dist/types/utils/oauth/ollama-cloud.d.ts +2 -0
- package/dist/types/utils/oauth/ollama.d.ts +18 -0
- package/dist/types/utils/oauth/openai-codex.d.ts +21 -0
- package/dist/types/utils/oauth/opencode.d.ts +18 -0
- package/dist/types/utils/oauth/parallel.d.ts +17 -0
- package/dist/types/utils/oauth/perplexity.d.ts +9 -0
- package/dist/types/utils/oauth/pkce.d.ts +8 -0
- package/dist/types/utils/oauth/qianfan.d.ts +17 -0
- package/dist/types/utils/oauth/qwen-portal.d.ts +19 -0
- package/dist/types/utils/oauth/synthetic.d.ts +1 -0
- package/dist/types/utils/oauth/tavily.d.ts +17 -0
- package/dist/types/utils/oauth/together.d.ts +1 -0
- package/dist/types/utils/oauth/types.d.ts +44 -0
- package/dist/types/utils/oauth/venice.d.ts +18 -0
- package/dist/types/utils/oauth/vercel-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/vllm.d.ts +16 -0
- package/dist/types/utils/oauth/xiaomi.d.ts +19 -0
- package/dist/types/utils/oauth/zai.d.ts +18 -0
- package/dist/types/utils/oauth/zenmux.d.ts +1 -0
- package/dist/types/utils/overflow.d.ts +54 -0
- package/dist/types/utils/parse-bind.d.ts +23 -0
- package/dist/types/utils/provider-response.d.ts +3 -0
- package/dist/types/utils/retry-after.d.ts +3 -0
- package/dist/types/utils/retry.d.ts +26 -0
- package/dist/types/utils/schema/adapt.d.ts +24 -0
- package/dist/types/utils/schema/compatibility.d.ts +30 -0
- package/dist/types/utils/schema/dereference.d.ts +11 -0
- package/dist/types/utils/schema/draft.d.ts +10 -0
- package/dist/types/utils/schema/equality.d.ts +4 -0
- package/dist/types/utils/schema/fields.d.ts +49 -0
- package/dist/types/utils/schema/index.d.ts +13 -0
- package/dist/types/utils/schema/json-schema-validator.d.ts +12 -0
- package/dist/types/utils/schema/meta-validator.d.ts +2 -0
- package/dist/types/utils/schema/normalize.d.ts +93 -0
- package/dist/types/utils/schema/spill.d.ts +8 -0
- package/dist/types/utils/schema/stamps.d.ts +25 -0
- package/dist/types/utils/schema/types.d.ts +4 -0
- package/dist/types/utils/schema/wire.d.ts +54 -0
- package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
- package/dist/types/utils/sse-debug.d.ts +10 -0
- package/dist/types/utils/tool-call-healing.d.ts +71 -0
- package/dist/types/utils/tool-choice.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +17 -0
- package/dist/types/utils.d.ts +28 -0
- package/package.json +146 -0
- package/src/api-registry.ts +96 -0
- package/src/auth-broker/client.ts +358 -0
- package/src/auth-broker/index.ts +5 -0
- package/src/auth-broker/refresher.ts +127 -0
- package/src/auth-broker/remote-store.ts +623 -0
- package/src/auth-broker/server.ts +644 -0
- package/src/auth-broker/types.ts +127 -0
- package/src/auth-broker/wire-schemas.ts +200 -0
- package/src/auth-gateway/http.ts +194 -0
- package/src/auth-gateway/index.ts +3 -0
- package/src/auth-gateway/server.ts +717 -0
- package/src/auth-gateway/types.ts +134 -0
- package/src/auth-storage.ts +4104 -0
- package/src/cli.ts +262 -0
- package/src/index.ts +54 -0
- package/src/model-cache.ts +129 -0
- package/src/model-manager.ts +450 -0
- package/src/model-thinking.ts +691 -0
- package/src/models.json +73853 -0
- package/src/models.json.d.ts +9 -0
- package/src/models.ts +56 -0
- package/src/prompts/turn-aborted-guidance.md +4 -0
- package/src/provider-details.ts +90 -0
- package/src/provider-models/bundled-references.ts +38 -0
- package/src/provider-models/descriptors.ts +308 -0
- package/src/provider-models/google.ts +91 -0
- package/src/provider-models/index.ts +5 -0
- package/src/provider-models/ollama.ts +153 -0
- package/src/provider-models/openai-compat.ts +2275 -0
- package/src/provider-models/special.ts +67 -0
- package/src/providers/amazon-bedrock.ts +849 -0
- package/src/providers/anthropic-messages-server-schema.ts +229 -0
- package/src/providers/anthropic-messages-server.ts +677 -0
- package/src/providers/anthropic.ts +2696 -0
- package/src/providers/aws-credentials.ts +501 -0
- package/src/providers/aws-eventstream.ts +185 -0
- package/src/providers/aws-sigv4.ts +218 -0
- package/src/providers/azure-openai-responses.ts +337 -0
- package/src/providers/cursor/gen/agent_pb.ts +15274 -0
- package/src/providers/cursor/proto/agent.proto +3526 -0
- package/src/providers/cursor/proto/buf.gen.yaml +6 -0
- package/src/providers/cursor/proto/buf.yaml +17 -0
- package/src/providers/cursor.ts +2561 -0
- package/src/providers/error-message.ts +21 -0
- package/src/providers/github-copilot-headers.ts +140 -0
- package/src/providers/gitlab-duo.ts +372 -0
- package/src/providers/google-auth.ts +252 -0
- package/src/providers/google-gemini-cli.ts +795 -0
- package/src/providers/google-gemini-headers.ts +41 -0
- package/src/providers/google-shared.ts +902 -0
- package/src/providers/google-types.ts +167 -0
- package/src/providers/google-vertex.ts +88 -0
- package/src/providers/google.ts +41 -0
- package/src/providers/grammar.ts +70 -0
- package/src/providers/kimi.ts +52 -0
- package/src/providers/mock.ts +500 -0
- package/src/providers/ollama.ts +544 -0
- package/src/providers/openai-anthropic-shim.ts +138 -0
- package/src/providers/openai-chat-server-schema.ts +243 -0
- package/src/providers/openai-chat-server.ts +628 -0
- package/src/providers/openai-codex/constants.ts +43 -0
- package/src/providers/openai-codex/request-transformer.ts +161 -0
- package/src/providers/openai-codex/response-handler.ts +81 -0
- package/src/providers/openai-codex-responses.ts +2598 -0
- package/src/providers/openai-completions-compat.ts +279 -0
- package/src/providers/openai-completions.ts +1853 -0
- package/src/providers/openai-responses-server-schema.ts +290 -0
- package/src/providers/openai-responses-server.ts +1183 -0
- package/src/providers/openai-responses-shared.ts +800 -0
- package/src/providers/openai-responses.ts +621 -0
- package/src/providers/pi-native-client.ts +228 -0
- package/src/providers/pi-native-server.ts +210 -0
- package/src/providers/register-builtins.ts +412 -0
- package/src/providers/synthetic.ts +50 -0
- package/src/providers/transform-messages.ts +309 -0
- package/src/providers/vision-guard.ts +31 -0
- package/src/rate-limit-utils.ts +84 -0
- package/src/stream.ts +895 -0
- package/src/types.ts +884 -0
- package/src/usage/claude.ts +431 -0
- package/src/usage/gemini.ts +250 -0
- package/src/usage/github-copilot.ts +421 -0
- package/src/usage/google-antigravity.ts +201 -0
- package/src/usage/kimi.ts +271 -0
- package/src/usage/minimax-code.ts +31 -0
- package/src/usage/openai-codex.ts +503 -0
- package/src/usage/shared.ts +10 -0
- package/src/usage/zai.ts +247 -0
- package/src/usage.ts +183 -0
- package/src/utils/abort.ts +51 -0
- package/src/utils/anthropic-auth.ts +87 -0
- package/src/utils/discovery/antigravity.ts +261 -0
- package/src/utils/discovery/codex.ts +371 -0
- package/src/utils/discovery/cursor.ts +306 -0
- package/src/utils/discovery/gemini.ts +248 -0
- package/src/utils/discovery/index.ts +4 -0
- package/src/utils/discovery/openai-compatible.ts +224 -0
- package/src/utils/event-stream.ts +142 -0
- package/src/utils/fireworks-model-id.ts +30 -0
- package/src/utils/foundry.ts +8 -0
- package/src/utils/h2-fetch.ts +60 -0
- package/src/utils/http-inspector.ts +176 -0
- package/src/utils/idle-iterator.ts +250 -0
- package/src/utils/json-parse.ts +148 -0
- package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
- package/src/utils/oauth/anthropic.ts +200 -0
- package/src/utils/oauth/api-key-login.ts +87 -0
- package/src/utils/oauth/api-key-validation.ts +92 -0
- package/src/utils/oauth/callback-server.ts +276 -0
- package/src/utils/oauth/cerebras.ts +16 -0
- package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
- package/src/utils/oauth/cursor.ts +157 -0
- package/src/utils/oauth/deepseek.ts +53 -0
- package/src/utils/oauth/firepass.ts +24 -0
- package/src/utils/oauth/fireworks.ts +15 -0
- package/src/utils/oauth/github-copilot.ts +362 -0
- package/src/utils/oauth/gitlab-duo.ts +123 -0
- package/src/utils/oauth/google-antigravity.ts +200 -0
- package/src/utils/oauth/google-gemini-cli.ts +256 -0
- package/src/utils/oauth/google-oauth-shared.ts +110 -0
- package/src/utils/oauth/huggingface.ts +62 -0
- package/src/utils/oauth/index.ts +444 -0
- package/src/utils/oauth/kagi.ts +47 -0
- package/src/utils/oauth/kilo.ts +87 -0
- package/src/utils/oauth/kimi.ts +254 -0
- package/src/utils/oauth/litellm.ts +47 -0
- package/src/utils/oauth/lm-studio.ts +38 -0
- package/src/utils/oauth/minimax-code.ts +78 -0
- package/src/utils/oauth/moonshot.ts +16 -0
- package/src/utils/oauth/nanogpt.ts +15 -0
- package/src/utils/oauth/nvidia.ts +70 -0
- package/src/utils/oauth/oauth.html +199 -0
- package/src/utils/oauth/ollama-cloud.ts +28 -0
- package/src/utils/oauth/ollama.ts +47 -0
- package/src/utils/oauth/openai-codex.ts +299 -0
- package/src/utils/oauth/opencode.ts +49 -0
- package/src/utils/oauth/parallel.ts +46 -0
- package/src/utils/oauth/perplexity.ts +206 -0
- package/src/utils/oauth/pkce.ts +18 -0
- package/src/utils/oauth/qianfan.ts +58 -0
- package/src/utils/oauth/qwen-portal.ts +60 -0
- package/src/utils/oauth/synthetic.ts +16 -0
- package/src/utils/oauth/tavily.ts +46 -0
- package/src/utils/oauth/together.ts +16 -0
- package/src/utils/oauth/types.ts +94 -0
- package/src/utils/oauth/venice.ts +59 -0
- package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
- package/src/utils/oauth/vllm.ts +40 -0
- package/src/utils/oauth/xiaomi.ts +137 -0
- package/src/utils/oauth/zai.ts +60 -0
- package/src/utils/oauth/zenmux.ts +15 -0
- package/src/utils/overflow.ts +137 -0
- package/src/utils/parse-bind.ts +54 -0
- package/src/utils/provider-response.ts +30 -0
- package/src/utils/retry-after.ts +110 -0
- package/src/utils/retry.ts +54 -0
- package/src/utils/schema/CONSTRAINTS.md +164 -0
- package/src/utils/schema/adapt.ts +36 -0
- package/src/utils/schema/compatibility.ts +435 -0
- package/src/utils/schema/dereference.ts +98 -0
- package/src/utils/schema/draft.ts +341 -0
- package/src/utils/schema/equality.ts +97 -0
- package/src/utils/schema/fields.ts +190 -0
- package/src/utils/schema/index.ts +13 -0
- package/src/utils/schema/json-schema-validator.ts +577 -0
- package/src/utils/schema/meta-validator.ts +167 -0
- package/src/utils/schema/normalize.ts +1588 -0
- package/src/utils/schema/spill.ts +43 -0
- package/src/utils/schema/stamps.ts +97 -0
- package/src/utils/schema/types.ts +11 -0
- package/src/utils/schema/wire.ts +213 -0
- package/src/utils/schema/zod-decontaminate.ts +331 -0
- package/src/utils/sse-debug.ts +289 -0
- package/src/utils/tool-call-healing.ts +271 -0
- package/src/utils/tool-choice.ts +99 -0
- package/src/utils/validation.ts +1019 -0
- package/src/utils.ts +166 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Api, Model } from "../types";
|
|
2
|
+
|
|
3
|
+
export function createProviderErrorMessage(model: Model<Api>, err: unknown) {
|
|
4
|
+
return {
|
|
5
|
+
role: "assistant" as const,
|
|
6
|
+
content: [{ type: "text" as const, text: err instanceof Error ? err.message : String(err) }],
|
|
7
|
+
api: model.api,
|
|
8
|
+
provider: model.provider,
|
|
9
|
+
model: model.id,
|
|
10
|
+
usage: {
|
|
11
|
+
input: 0,
|
|
12
|
+
output: 0,
|
|
13
|
+
cacheRead: 0,
|
|
14
|
+
cacheWrite: 0,
|
|
15
|
+
totalTokens: 0,
|
|
16
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
17
|
+
},
|
|
18
|
+
stopReason: "error" as const,
|
|
19
|
+
timestamp: Date.now(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { Message } from "../types";
|
|
2
|
+
import { getGitHubCopilotBaseUrl, parseGitHubCopilotApiKey } from "../utils/oauth/github-copilot";
|
|
3
|
+
/**
|
|
4
|
+
* Infer whether the current request to Copilot is user-initiated or agent-initiated.
|
|
5
|
+
* Accepts `unknown[]` because providers may pass pre-converted message shapes.
|
|
6
|
+
*/
|
|
7
|
+
export type CopilotInitiator = "user" | "agent";
|
|
8
|
+
export type CopilotPremiumRequests = number;
|
|
9
|
+
export type CopilotDynamicHeaders = {
|
|
10
|
+
headers: Record<string, string>;
|
|
11
|
+
initiator: CopilotInitiator;
|
|
12
|
+
premiumRequests: CopilotPremiumRequests;
|
|
13
|
+
};
|
|
14
|
+
export function resolveGitHubCopilotBaseUrl(
|
|
15
|
+
baseUrl: string | undefined,
|
|
16
|
+
apiKey: string | undefined,
|
|
17
|
+
): string | undefined {
|
|
18
|
+
if (!apiKey) return baseUrl;
|
|
19
|
+
const { enterpriseUrl } = parseGitHubCopilotApiKey(apiKey);
|
|
20
|
+
if (!enterpriseUrl) return baseUrl;
|
|
21
|
+
if (baseUrl && !baseUrl.includes("githubcopilot.com")) return baseUrl;
|
|
22
|
+
return getGitHubCopilotBaseUrl(enterpriseUrl);
|
|
23
|
+
}
|
|
24
|
+
export function inferCopilotInitiator(messages: unknown[]): CopilotInitiator {
|
|
25
|
+
if (messages.length === 0) return "user";
|
|
26
|
+
|
|
27
|
+
const last = messages[messages.length - 1] as Record<string, unknown>;
|
|
28
|
+
const attribution = last.attribution;
|
|
29
|
+
if (typeof attribution === "string") {
|
|
30
|
+
const normalizedAttribution = attribution.trim().toLowerCase();
|
|
31
|
+
if (normalizedAttribution === "user" || normalizedAttribution === "agent") {
|
|
32
|
+
return normalizedAttribution;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const role = last.role as string | undefined;
|
|
37
|
+
if (!role) return "user";
|
|
38
|
+
|
|
39
|
+
if (role !== "user") return "agent";
|
|
40
|
+
|
|
41
|
+
// Check if last content block is a tool_result (Anthropic-converted shape)
|
|
42
|
+
const content = last.content;
|
|
43
|
+
if (Array.isArray(content) && content.length > 0) {
|
|
44
|
+
const lastBlock = content[content.length - 1] as Record<string, unknown>;
|
|
45
|
+
if (lastBlock.type === "tool_result") {
|
|
46
|
+
return "agent";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return "user";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Check whether any message in the conversation contains image content. */
|
|
54
|
+
export function hasCopilotVisionInput(messages: Message[]): boolean {
|
|
55
|
+
return messages.some(msg => {
|
|
56
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
57
|
+
return msg.content.some(c => c.type === "image");
|
|
58
|
+
}
|
|
59
|
+
if (msg.role === "toolResult" && Array.isArray(msg.content)) {
|
|
60
|
+
return msg.content.some(c => c.type === "image");
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Resolve an explicitly configured Copilot initiator header, if present.
|
|
68
|
+
* Handles case-insensitive X-Initiator keys and returns the last valid value.
|
|
69
|
+
*/
|
|
70
|
+
export function getCopilotInitiatorOverride(headers: Record<string, string> | undefined): CopilotInitiator | undefined {
|
|
71
|
+
if (!headers) return undefined;
|
|
72
|
+
|
|
73
|
+
let override: CopilotInitiator | undefined;
|
|
74
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
75
|
+
if (key.toLowerCase() !== "x-initiator") continue;
|
|
76
|
+
const normalized = value.trim().toLowerCase();
|
|
77
|
+
if (normalized === "user" || normalized === "agent") {
|
|
78
|
+
override = normalized;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return override;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type CopilotPlanTier = "free" | "paid";
|
|
86
|
+
|
|
87
|
+
function normalizeCopilotPlanTier(planTier: string | undefined): CopilotPlanTier {
|
|
88
|
+
if (planTier === "paid") return "paid";
|
|
89
|
+
return "free";
|
|
90
|
+
}
|
|
91
|
+
export function getCopilotPremiumMultiplier(premiumMultiplier: number | undefined, planTier?: string): number {
|
|
92
|
+
const normalizedMultiplier = premiumMultiplier ?? 1;
|
|
93
|
+
if (normalizeCopilotPlanTier(planTier) === "free" && normalizedMultiplier === 0) {
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
return normalizedMultiplier;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function getCopilotPremiumRequests(params: {
|
|
100
|
+
initiator: CopilotInitiator;
|
|
101
|
+
premiumMultiplier?: number;
|
|
102
|
+
planTier?: string;
|
|
103
|
+
}): CopilotPremiumRequests {
|
|
104
|
+
if (params.initiator === "agent") return 0;
|
|
105
|
+
return getCopilotPremiumMultiplier(params.premiumMultiplier, params.planTier);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Build dynamic Copilot headers that vary per-request.
|
|
110
|
+
* Static headers (User-Agent, Editor-Version, etc.) come from model.headers.
|
|
111
|
+
*/
|
|
112
|
+
export function buildCopilotDynamicHeaders(params: {
|
|
113
|
+
messages: unknown[];
|
|
114
|
+
hasImages: boolean;
|
|
115
|
+
premiumMultiplier?: number;
|
|
116
|
+
headers?: Record<string, string>;
|
|
117
|
+
initiatorOverride?: CopilotInitiator;
|
|
118
|
+
planTier?: string;
|
|
119
|
+
}): CopilotDynamicHeaders {
|
|
120
|
+
const initiator =
|
|
121
|
+
params.initiatorOverride ?? getCopilotInitiatorOverride(params.headers) ?? inferCopilotInitiator(params.messages);
|
|
122
|
+
const headers: Record<string, string> = {
|
|
123
|
+
"X-Initiator": initiator,
|
|
124
|
+
"Openai-Intent": "conversation-edits",
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
if (params.hasImages) {
|
|
128
|
+
headers["Copilot-Vision-Request"] = "true";
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
headers,
|
|
133
|
+
initiator,
|
|
134
|
+
premiumRequests: getCopilotPremiumRequests({
|
|
135
|
+
initiator,
|
|
136
|
+
premiumMultiplier: params.premiumMultiplier,
|
|
137
|
+
planTier: params.planTier,
|
|
138
|
+
}),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { ANTHROPIC_THINKING, mapAnthropicToolChoice } from "../stream";
|
|
2
|
+
import type { Api, Context, FetchImpl, Model, SimpleStreamOptions } from "../types";
|
|
3
|
+
import { AssistantMessageEventStream } from "../utils/event-stream";
|
|
4
|
+
import { createProviderErrorMessage } from "./error-message";
|
|
5
|
+
import type { OpenAICompletionsOptions } from "./openai-completions";
|
|
6
|
+
import type { OpenAIResponsesOptions } from "./openai-responses";
|
|
7
|
+
import { streamAnthropic, streamOpenAICompletions, streamOpenAIResponses } from "./register-builtins";
|
|
8
|
+
|
|
9
|
+
const GITLAB_COM_URL = "https://gitlab.com";
|
|
10
|
+
const AI_GATEWAY_URL = "https://cloud.gitlab.com";
|
|
11
|
+
const ANTHROPIC_PROXY_URL = `${AI_GATEWAY_URL}/ai/v1/proxy/anthropic/`;
|
|
12
|
+
const OPENAI_PROXY_URL = `${AI_GATEWAY_URL}/ai/v1/proxy/openai/v1`;
|
|
13
|
+
const DIRECT_ACCESS_TTL_MS = 25 * 60 * 1000;
|
|
14
|
+
|
|
15
|
+
type GitLabProvider = "anthropic" | "openai";
|
|
16
|
+
type GitLabOpenAIApiType = "chat" | "responses";
|
|
17
|
+
|
|
18
|
+
export type GitLabModelMapping = {
|
|
19
|
+
provider: GitLabProvider;
|
|
20
|
+
model: string;
|
|
21
|
+
openaiApiType?: GitLabOpenAIApiType;
|
|
22
|
+
name: string;
|
|
23
|
+
reasoning: boolean;
|
|
24
|
+
input: ("text" | "image")[];
|
|
25
|
+
cost: { input: number; output: number; cacheRead: number; cacheWrite: number };
|
|
26
|
+
contextWindow: number;
|
|
27
|
+
maxTokens: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const MODEL_MAPPINGS: Record<string, GitLabModelMapping> = {
|
|
31
|
+
"duo-chat-opus-4-6": {
|
|
32
|
+
provider: "anthropic",
|
|
33
|
+
model: "claude-opus-4-6",
|
|
34
|
+
name: "Duo Chat Opus 4.6",
|
|
35
|
+
reasoning: true,
|
|
36
|
+
input: ["text", "image"],
|
|
37
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
38
|
+
contextWindow: 200000,
|
|
39
|
+
maxTokens: 64000,
|
|
40
|
+
},
|
|
41
|
+
"duo-chat-sonnet-4-6": {
|
|
42
|
+
provider: "anthropic",
|
|
43
|
+
model: "claude-sonnet-4-6",
|
|
44
|
+
name: "Duo Chat Sonnet 4.6",
|
|
45
|
+
reasoning: true,
|
|
46
|
+
input: ["text", "image"],
|
|
47
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
48
|
+
contextWindow: 200000,
|
|
49
|
+
maxTokens: 64000,
|
|
50
|
+
},
|
|
51
|
+
"duo-chat-opus-4-5": {
|
|
52
|
+
provider: "anthropic",
|
|
53
|
+
model: "claude-opus-4-5-20251101",
|
|
54
|
+
name: "Duo Chat Opus 4.5",
|
|
55
|
+
reasoning: true,
|
|
56
|
+
input: ["text", "image"],
|
|
57
|
+
cost: { input: 15, output: 75, cacheRead: 1.5, cacheWrite: 18.75 },
|
|
58
|
+
contextWindow: 200000,
|
|
59
|
+
maxTokens: 64000,
|
|
60
|
+
},
|
|
61
|
+
"duo-chat-sonnet-4-5": {
|
|
62
|
+
provider: "anthropic",
|
|
63
|
+
model: "claude-sonnet-4-5-20250929",
|
|
64
|
+
name: "Duo Chat Sonnet 4.5",
|
|
65
|
+
reasoning: true,
|
|
66
|
+
input: ["text", "image"],
|
|
67
|
+
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
68
|
+
contextWindow: 200000,
|
|
69
|
+
maxTokens: 64000,
|
|
70
|
+
},
|
|
71
|
+
"duo-chat-haiku-4-5": {
|
|
72
|
+
provider: "anthropic",
|
|
73
|
+
model: "claude-haiku-4-5-20251001",
|
|
74
|
+
name: "Duo Chat Haiku 4.5",
|
|
75
|
+
reasoning: true,
|
|
76
|
+
input: ["text", "image"],
|
|
77
|
+
cost: { input: 1, output: 5, cacheRead: 0.1, cacheWrite: 1.25 },
|
|
78
|
+
contextWindow: 200000,
|
|
79
|
+
maxTokens: 64000,
|
|
80
|
+
},
|
|
81
|
+
"duo-chat-gpt-5-1": {
|
|
82
|
+
provider: "openai",
|
|
83
|
+
model: "gpt-5.1-2025-11-13",
|
|
84
|
+
openaiApiType: "chat",
|
|
85
|
+
name: "Duo Chat GPT-5.1",
|
|
86
|
+
reasoning: true,
|
|
87
|
+
input: ["text", "image"],
|
|
88
|
+
cost: { input: 2.5, output: 10, cacheRead: 0, cacheWrite: 0 },
|
|
89
|
+
contextWindow: 128000,
|
|
90
|
+
maxTokens: 16384,
|
|
91
|
+
},
|
|
92
|
+
"duo-chat-gpt-5-2": {
|
|
93
|
+
provider: "openai",
|
|
94
|
+
model: "gpt-5.2-2025-12-11",
|
|
95
|
+
openaiApiType: "chat",
|
|
96
|
+
name: "Duo Chat GPT-5.2",
|
|
97
|
+
reasoning: true,
|
|
98
|
+
input: ["text", "image"],
|
|
99
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
100
|
+
contextWindow: 128000,
|
|
101
|
+
maxTokens: 16384,
|
|
102
|
+
},
|
|
103
|
+
"duo-chat-gpt-5-mini": {
|
|
104
|
+
provider: "openai",
|
|
105
|
+
model: "gpt-5-mini-2025-08-07",
|
|
106
|
+
openaiApiType: "chat",
|
|
107
|
+
name: "Duo Chat GPT-5 Mini",
|
|
108
|
+
reasoning: true,
|
|
109
|
+
input: ["text", "image"],
|
|
110
|
+
cost: { input: 0.15, output: 0.6, cacheRead: 0, cacheWrite: 0 },
|
|
111
|
+
contextWindow: 128000,
|
|
112
|
+
maxTokens: 16384,
|
|
113
|
+
},
|
|
114
|
+
"duo-chat-gpt-5-codex": {
|
|
115
|
+
provider: "openai",
|
|
116
|
+
model: "gpt-5-codex",
|
|
117
|
+
openaiApiType: "responses",
|
|
118
|
+
name: "Duo Chat GPT-5 Codex",
|
|
119
|
+
reasoning: true,
|
|
120
|
+
input: ["text", "image"],
|
|
121
|
+
cost: { input: 2.5, output: 10, cacheRead: 0, cacheWrite: 0 },
|
|
122
|
+
contextWindow: 272000,
|
|
123
|
+
maxTokens: 128000,
|
|
124
|
+
},
|
|
125
|
+
"duo-chat-gpt-5-2-codex": {
|
|
126
|
+
provider: "openai",
|
|
127
|
+
model: "gpt-5.2-codex",
|
|
128
|
+
openaiApiType: "responses",
|
|
129
|
+
name: "Duo Chat GPT-5.2 Codex",
|
|
130
|
+
reasoning: true,
|
|
131
|
+
input: ["text", "image"],
|
|
132
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
133
|
+
contextWindow: 272000,
|
|
134
|
+
maxTokens: 128000,
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export function getModelMapping(modelId: string): GitLabModelMapping | undefined {
|
|
139
|
+
const direct = MODEL_MAPPINGS[modelId];
|
|
140
|
+
if (direct) return direct;
|
|
141
|
+
|
|
142
|
+
// Support canonical model IDs (e.g. "gpt-5-OpenAI code backend", "Anthropic model-sonnet-4-5-20250929")
|
|
143
|
+
// in addition to Duo aliases (e.g. "duo-chat-gpt-5-OpenAI code backend").
|
|
144
|
+
return Object.values(MODEL_MAPPINGS).find(mapping => mapping.model === modelId);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function getGitLabDuoModels(): Model<Api>[] {
|
|
148
|
+
return Object.entries(MODEL_MAPPINGS).map(([id, mapping]) => ({
|
|
149
|
+
id,
|
|
150
|
+
name: mapping.name,
|
|
151
|
+
api:
|
|
152
|
+
mapping.provider === "anthropic"
|
|
153
|
+
? "anthropic-messages"
|
|
154
|
+
: mapping.openaiApiType === "responses"
|
|
155
|
+
? "openai-responses"
|
|
156
|
+
: "openai-completions",
|
|
157
|
+
provider: "gitlab-duo",
|
|
158
|
+
baseUrl: mapping.provider === "anthropic" ? ANTHROPIC_PROXY_URL : OPENAI_PROXY_URL,
|
|
159
|
+
reasoning: mapping.reasoning,
|
|
160
|
+
input: [...mapping.input],
|
|
161
|
+
cost: { ...mapping.cost },
|
|
162
|
+
contextWindow: mapping.contextWindow,
|
|
163
|
+
maxTokens: mapping.maxTokens,
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
interface DirectAccessToken {
|
|
168
|
+
token: string;
|
|
169
|
+
headers: Record<string, string>;
|
|
170
|
+
expiresAt: number;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const directAccessCache = new Map<string, DirectAccessToken>();
|
|
174
|
+
|
|
175
|
+
async function getDirectAccessToken(
|
|
176
|
+
gitlabAccessToken: string,
|
|
177
|
+
fetchImpl: FetchImpl = fetch,
|
|
178
|
+
): Promise<DirectAccessToken> {
|
|
179
|
+
const cached = directAccessCache.get(gitlabAccessToken);
|
|
180
|
+
if (cached && cached.expiresAt > Date.now()) {
|
|
181
|
+
return cached;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const response = await fetchImpl(`${GITLAB_COM_URL}/api/v4/ai/third_party_agents/direct_access`, {
|
|
185
|
+
method: "POST",
|
|
186
|
+
headers: {
|
|
187
|
+
Authorization: `Bearer ${gitlabAccessToken}`,
|
|
188
|
+
"Content-Type": "application/json",
|
|
189
|
+
},
|
|
190
|
+
body: JSON.stringify({
|
|
191
|
+
feature_flags: { DuoAgentPlatformNext: true },
|
|
192
|
+
}),
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
if (!response.ok) {
|
|
196
|
+
const detail = await response.text();
|
|
197
|
+
if (response.status === 403) {
|
|
198
|
+
throw new Error(`GitLab Duo access denied. Ensure Duo is enabled for this account. ${detail}`);
|
|
199
|
+
}
|
|
200
|
+
throw new Error(`Failed to get GitLab Duo direct access token: ${response.status} ${detail}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const payload = (await response.json()) as { token?: string; headers?: Record<string, string> };
|
|
204
|
+
if (!payload.token || typeof payload.token !== "string") {
|
|
205
|
+
throw new Error("GitLab Duo direct access response missing token");
|
|
206
|
+
}
|
|
207
|
+
if (!payload.headers || typeof payload.headers !== "object") {
|
|
208
|
+
throw new Error("GitLab Duo direct access response missing headers");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const token: DirectAccessToken = {
|
|
212
|
+
token: payload.token,
|
|
213
|
+
headers: payload.headers,
|
|
214
|
+
expiresAt: Date.now() + DIRECT_ACCESS_TTL_MS,
|
|
215
|
+
};
|
|
216
|
+
directAccessCache.set(gitlabAccessToken, token);
|
|
217
|
+
return token;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function clearGitLabDuoDirectAccessCache(): void {
|
|
221
|
+
directAccessCache.clear();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function isGitLabDuoModel(model: Model<Api>): boolean {
|
|
225
|
+
return model.provider === "gitlab-duo";
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function streamGitLabDuo(
|
|
229
|
+
model: Model<Api>,
|
|
230
|
+
context: Context,
|
|
231
|
+
options?: SimpleStreamOptions,
|
|
232
|
+
): AssistantMessageEventStream {
|
|
233
|
+
const stream = new AssistantMessageEventStream();
|
|
234
|
+
|
|
235
|
+
(async () => {
|
|
236
|
+
try {
|
|
237
|
+
if (!options?.apiKey) {
|
|
238
|
+
throw new Error("Missing GitLab access token. Run /login gitlab-duo or set GITLAB_TOKEN.");
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const mapping = getModelMapping(model.id);
|
|
242
|
+
if (!mapping) {
|
|
243
|
+
throw new Error(`Unsupported GitLab Duo model: ${model.id}`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const directAccess = await getDirectAccessToken(options.apiKey, options.fetch);
|
|
247
|
+
const headers = {
|
|
248
|
+
...directAccess.headers,
|
|
249
|
+
...options.headers,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const reasoningEffort = options.reasoning;
|
|
253
|
+
|
|
254
|
+
const inner =
|
|
255
|
+
mapping.provider === "anthropic"
|
|
256
|
+
? streamAnthropic(
|
|
257
|
+
{
|
|
258
|
+
...model,
|
|
259
|
+
id: mapping.model,
|
|
260
|
+
api: "anthropic-messages",
|
|
261
|
+
baseUrl: ANTHROPIC_PROXY_URL,
|
|
262
|
+
} as Model<"anthropic-messages">,
|
|
263
|
+
context,
|
|
264
|
+
{
|
|
265
|
+
apiKey: directAccess.token,
|
|
266
|
+
isOAuth: true,
|
|
267
|
+
temperature: options.temperature,
|
|
268
|
+
topP: options.topP,
|
|
269
|
+
topK: options.topK,
|
|
270
|
+
minP: options.minP,
|
|
271
|
+
presencePenalty: options.presencePenalty,
|
|
272
|
+
repetitionPenalty: options.repetitionPenalty,
|
|
273
|
+
maxTokens: options.maxTokens ?? Math.min(model.maxTokens, 32000),
|
|
274
|
+
signal: options.signal,
|
|
275
|
+
cacheRetention: options.cacheRetention,
|
|
276
|
+
headers,
|
|
277
|
+
maxRetryDelayMs: options.maxRetryDelayMs,
|
|
278
|
+
metadata: options.metadata,
|
|
279
|
+
sessionId: options.sessionId,
|
|
280
|
+
providerSessionState: options.providerSessionState,
|
|
281
|
+
onPayload: options.onPayload,
|
|
282
|
+
onResponse: options.onResponse,
|
|
283
|
+
onSseEvent: options.onSseEvent,
|
|
284
|
+
fetch: options.fetch,
|
|
285
|
+
thinkingEnabled: Boolean(reasoningEffort) && model.reasoning,
|
|
286
|
+
thinkingBudgetTokens: reasoningEffort
|
|
287
|
+
? (options.thinkingBudgets?.[reasoningEffort] ?? ANTHROPIC_THINKING[reasoningEffort])
|
|
288
|
+
: undefined,
|
|
289
|
+
reasoning: reasoningEffort,
|
|
290
|
+
toolChoice: mapAnthropicToolChoice(options.toolChoice),
|
|
291
|
+
},
|
|
292
|
+
)
|
|
293
|
+
: mapping.openaiApiType === "responses"
|
|
294
|
+
? streamOpenAIResponses(
|
|
295
|
+
{
|
|
296
|
+
...model,
|
|
297
|
+
id: mapping.model,
|
|
298
|
+
api: "openai-responses",
|
|
299
|
+
baseUrl: OPENAI_PROXY_URL,
|
|
300
|
+
} as Model<"openai-responses">,
|
|
301
|
+
context,
|
|
302
|
+
{
|
|
303
|
+
apiKey: directAccess.token,
|
|
304
|
+
temperature: options.temperature,
|
|
305
|
+
topP: options.topP,
|
|
306
|
+
topK: options.topK,
|
|
307
|
+
minP: options.minP,
|
|
308
|
+
presencePenalty: options.presencePenalty,
|
|
309
|
+
repetitionPenalty: options.repetitionPenalty,
|
|
310
|
+
maxTokens: options.maxTokens ?? model.maxTokens,
|
|
311
|
+
signal: options.signal,
|
|
312
|
+
cacheRetention: options.cacheRetention,
|
|
313
|
+
headers,
|
|
314
|
+
maxRetryDelayMs: options.maxRetryDelayMs,
|
|
315
|
+
metadata: options.metadata,
|
|
316
|
+
sessionId: options.sessionId,
|
|
317
|
+
providerSessionState: options.providerSessionState,
|
|
318
|
+
onPayload: options.onPayload,
|
|
319
|
+
onResponse: options.onResponse,
|
|
320
|
+
onSseEvent: options.onSseEvent,
|
|
321
|
+
fetch: options.fetch,
|
|
322
|
+
reasoning: reasoningEffort,
|
|
323
|
+
toolChoice: options.toolChoice,
|
|
324
|
+
} satisfies OpenAIResponsesOptions,
|
|
325
|
+
)
|
|
326
|
+
: streamOpenAICompletions(
|
|
327
|
+
{
|
|
328
|
+
...model,
|
|
329
|
+
id: mapping.model,
|
|
330
|
+
api: "openai-completions",
|
|
331
|
+
baseUrl: OPENAI_PROXY_URL,
|
|
332
|
+
} as Model<"openai-completions">,
|
|
333
|
+
context,
|
|
334
|
+
{
|
|
335
|
+
apiKey: directAccess.token,
|
|
336
|
+
temperature: options.temperature,
|
|
337
|
+
topP: options.topP,
|
|
338
|
+
topK: options.topK,
|
|
339
|
+
minP: options.minP,
|
|
340
|
+
presencePenalty: options.presencePenalty,
|
|
341
|
+
repetitionPenalty: options.repetitionPenalty,
|
|
342
|
+
maxTokens: options.maxTokens ?? model.maxTokens,
|
|
343
|
+
signal: options.signal,
|
|
344
|
+
cacheRetention: options.cacheRetention,
|
|
345
|
+
headers,
|
|
346
|
+
maxRetryDelayMs: options.maxRetryDelayMs,
|
|
347
|
+
metadata: options.metadata,
|
|
348
|
+
sessionId: options.sessionId,
|
|
349
|
+
providerSessionState: options.providerSessionState,
|
|
350
|
+
onPayload: options.onPayload,
|
|
351
|
+
onResponse: options.onResponse,
|
|
352
|
+
onSseEvent: options.onSseEvent,
|
|
353
|
+
fetch: options.fetch,
|
|
354
|
+
reasoning: reasoningEffort,
|
|
355
|
+
toolChoice: options.toolChoice,
|
|
356
|
+
} satisfies OpenAICompletionsOptions,
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
for await (const event of inner) {
|
|
360
|
+
stream.push(event);
|
|
361
|
+
}
|
|
362
|
+
} catch (err) {
|
|
363
|
+
stream.push({
|
|
364
|
+
type: "error",
|
|
365
|
+
reason: "error",
|
|
366
|
+
error: createProviderErrorMessage(model, err),
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
})();
|
|
370
|
+
|
|
371
|
+
return stream;
|
|
372
|
+
}
|