@bastani/pi-ai 0.9.71-alpha.1 → 0.9.72-alpha.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 +64 -1
- package/README.md +78 -8
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +50 -10
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/azure-openai-responses.d.ts.map +1 -1
- package/dist/api/azure-openai-responses.js +7 -2
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +28 -1
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/bedrock-converse-stream.lazy.d.ts +7 -0
- package/dist/api/bedrock-converse-stream.lazy.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.lazy.js +18 -5
- package/dist/api/bedrock-converse-stream.lazy.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts +3 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +4 -2
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts +10 -2
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +31 -9
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.d.ts +3 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +4 -2
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/lazy.d.ts +2 -2
- package/dist/api/lazy.d.ts.map +1 -1
- package/dist/api/lazy.js +10 -9
- package/dist/api/lazy.js.map +1 -1
- package/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/api/mistral-conversations.js +12 -1
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +6 -1
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts +2 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +112 -15
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-images-options.d.ts +107 -0
- package/dist/api/openai-images-options.d.ts.map +1 -0
- package/dist/api/openai-images-options.js +106 -0
- package/dist/api/openai-images-options.js.map +1 -0
- package/dist/api/openai-images.d.ts +3 -11
- package/dist/api/openai-images.d.ts.map +1 -1
- package/dist/api/openai-images.js +117 -16
- package/dist/api/openai-images.js.map +1 -1
- package/dist/api/openai-responses-shared.js +6 -2
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +29 -7
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/auth/oauth/load.d.ts.map +1 -1
- package/dist/auth/oauth/load.js +5 -2
- package/dist/auth/oauth/load.js.map +1 -1
- package/dist/env-api-keys.d.ts.map +1 -1
- package/dist/env-api-keys.js +1 -0
- package/dist/env-api-keys.js.map +1 -1
- package/dist/image-generation-error.d.ts +203 -0
- package/dist/image-generation-error.d.ts.map +1 -0
- package/dist/image-generation-error.js +441 -0
- package/dist/image-generation-error.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/lazy-bedrock-provider.d.ts +10 -0
- package/dist/lazy-bedrock-provider.d.ts.map +1 -0
- package/dist/lazy-bedrock-provider.js +13 -0
- package/dist/lazy-bedrock-provider.js.map +1 -0
- package/dist/lazy-oauth.d.ts +10 -0
- package/dist/lazy-oauth.d.ts.map +1 -0
- package/dist/lazy-oauth.js +23 -0
- package/dist/lazy-oauth.js.map +1 -0
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +4 -0
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +4 -0
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +8 -8
- package/dist/models.js.map +1 -1
- package/dist/providers/all.d.ts.map +1 -1
- package/dist/providers/all.js +2 -0
- package/dist/providers/all.js.map +1 -1
- package/dist/providers/azure.d.ts.map +1 -1
- package/dist/providers/azure.js +2 -2
- package/dist/providers/azure.js.map +1 -1
- package/dist/providers/compat-schema.d.ts +15 -0
- package/dist/providers/compat-schema.d.ts.map +1 -1
- package/dist/providers/compat-schema.js +19 -1
- package/dist/providers/compat-schema.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/anthropic.json +1 -1
- package/dist/providers/data/azure.json +1 -1
- package/dist/providers/data/baseten.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/dist/providers/data/fireworks.json +1 -1
- package/dist/providers/data/github-copilot.json +1 -1
- package/dist/providers/data/google-vertex.json +1 -1
- package/dist/providers/data/google.json +1 -1
- package/dist/providers/data/nvidia.json +1 -1
- package/dist/providers/data/openai-images.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/qwen-token-plan-individual-cn.json +1 -0
- package/dist/providers/data/qwen-token-plan-individual.json +1 -1
- package/dist/providers/data/together.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/github-copilot-capabilities.d.ts +67 -0
- package/dist/providers/github-copilot-capabilities.d.ts.map +1 -0
- package/dist/providers/github-copilot-capabilities.js +81 -0
- package/dist/providers/github-copilot-capabilities.js.map +1 -0
- package/dist/providers/github-copilot-errors.d.ts +243 -0
- package/dist/providers/github-copilot-errors.d.ts.map +1 -0
- package/dist/providers/github-copilot-errors.js +720 -0
- package/dist/providers/github-copilot-errors.js.map +1 -0
- package/dist/providers/github-copilot.d.ts +1 -2
- package/dist/providers/github-copilot.d.ts.map +1 -1
- package/dist/providers/github-copilot.js +29 -4
- package/dist/providers/github-copilot.js.map +1 -1
- package/dist/providers/openai-codex.js +1 -1
- package/dist/providers/openai-codex.js.map +1 -1
- package/dist/providers/qwen-token-plan-individual-cn.d.ts +3 -0
- package/dist/providers/qwen-token-plan-individual-cn.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.js +15 -0
- package/dist/providers/qwen-token-plan-individual-cn.js.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts +5 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js +8 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js.map +1 -0
- package/dist/types.d.ts +29 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/error-body.d.ts +8 -0
- package/dist/utils/error-body.d.ts.map +1 -1
- package/dist/utils/error-body.js +28 -1
- package/dist/utils/error-body.js.map +1 -1
- package/dist/utils/model-operations.d.ts.map +1 -1
- package/dist/utils/model-operations.js +6 -0
- package/dist/utils/model-operations.js.map +1 -1
- package/dist/utils/overflow.d.ts +4 -0
- package/dist/utils/overflow.d.ts.map +1 -1
- package/dist/utils/overflow.js +16 -3
- package/dist/utils/overflow.js.map +1 -1
- package/dist/utils/provider-retry.d.ts +2 -0
- package/dist/utils/provider-retry.d.ts.map +1 -1
- package/dist/utils/provider-retry.js +2 -0
- package/dist/utils/provider-retry.js.map +1 -1
- package/dist/utils/retry.d.ts +3 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +9 -0
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/thinking-off.d.ts +23 -0
- package/dist/utils/thinking-off.d.ts.map +1 -0
- package/dist/utils/thinking-off.js +51 -0
- package/dist/utils/thinking-off.js.map +1 -0
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +9 -1
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
|
|
2
|
+
import { getProviderRetryAfterMs } from "../utils/retry-hints.js";
|
|
3
|
+
const MODEL_UNAVAILABLE_REASONS = ["integration", "not-supported", "wrong-api", "no-endpoint", "not-found"];
|
|
4
|
+
const INVALID_REQUEST_REASONS = ["reasoning-effort", "integration-id", "rejected"];
|
|
5
|
+
const UNRECOGNIZED_REASONS = ["unknown-status", "unknown-error-event", "malformed-body"];
|
|
6
|
+
/** `AssistantMessageDiagnostic.type` of the diagnostic that carries a serialized GitHub Copilot error. */
|
|
7
|
+
export const GITHUB_COPILOT_ERROR_DIAGNOSTIC = "github_copilot_error";
|
|
8
|
+
const MAX_CAUSE_DEPTH = 8;
|
|
9
|
+
const MAX_DETAIL_CHARS = 300;
|
|
10
|
+
/** Every code, as a set that relayed values of any type can be checked against. */
|
|
11
|
+
const GITHUB_COPILOT_ERROR_CODES = new Set([
|
|
12
|
+
"model-unavailable",
|
|
13
|
+
"rate-limited",
|
|
14
|
+
"quota-exhausted",
|
|
15
|
+
"content-filtered",
|
|
16
|
+
"prompt-too-large",
|
|
17
|
+
"invalid-request",
|
|
18
|
+
"authentication-failed",
|
|
19
|
+
"service-unavailable",
|
|
20
|
+
"stream-ended-early",
|
|
21
|
+
"empty-response",
|
|
22
|
+
"unrecognized-response",
|
|
23
|
+
]);
|
|
24
|
+
const RETRYABLE_CODES = new Set([
|
|
25
|
+
"rate-limited",
|
|
26
|
+
"service-unavailable",
|
|
27
|
+
"stream-ended-early",
|
|
28
|
+
"empty-response",
|
|
29
|
+
"unrecognized-response",
|
|
30
|
+
]);
|
|
31
|
+
const WHAT_FAILED = {
|
|
32
|
+
"model-unavailable": "GitHub Copilot model unavailable",
|
|
33
|
+
"rate-limited": "GitHub Copilot rate limit reached",
|
|
34
|
+
"quota-exhausted": "GitHub Copilot quota exhausted",
|
|
35
|
+
"content-filtered": "GitHub Copilot content filter blocked the request",
|
|
36
|
+
"prompt-too-large": "GitHub Copilot prompt too large",
|
|
37
|
+
"invalid-request": "GitHub Copilot rejected the request",
|
|
38
|
+
"authentication-failed": "GitHub Copilot authentication failed",
|
|
39
|
+
"service-unavailable": "GitHub Copilot service unavailable",
|
|
40
|
+
"stream-ended-early": "GitHub Copilot response stream ended early",
|
|
41
|
+
"empty-response": "GitHub Copilot returned an empty response",
|
|
42
|
+
"unrecognized-response": "GitHub Copilot returned an unrecognized response",
|
|
43
|
+
};
|
|
44
|
+
const HINTS = {
|
|
45
|
+
"model-unavailable": "Pick another model, or run /login to refresh the models this account can use.",
|
|
46
|
+
"rate-limited": "Wait for the rate limit to reset; Atomic retries the request when retries are enabled.",
|
|
47
|
+
"quota-exhausted": "Pick another model, or wait until the Copilot plan's quota resets.",
|
|
48
|
+
"content-filtered": "Rephrase the request or pick another model; retry.fallbackOnContentFilter decides whether a fallback model is tried.",
|
|
49
|
+
"prompt-too-large": "Run /compact or start a new session to shrink the conversation.",
|
|
50
|
+
"invalid-request": "Check the model's thinking level and any models.json overrides for this model.",
|
|
51
|
+
"authentication-failed": "Run /login to sign in to GitHub Copilot again, or check COPILOT_GITHUB_TOKEN.",
|
|
52
|
+
"service-unavailable": "Retry later; Atomic retries the request when retries are enabled.",
|
|
53
|
+
"stream-ended-early": "Retry the request; Atomic retries it when retries are enabled.",
|
|
54
|
+
"empty-response": "Retry the request; Atomic retries it when retries are enabled.",
|
|
55
|
+
"unrecognized-response": "Retry the request, and report it if it keeps happening.",
|
|
56
|
+
};
|
|
57
|
+
/** Hints for the reasons that need more than their code's hint. */
|
|
58
|
+
const MODEL_UNAVAILABLE_HINTS = {
|
|
59
|
+
integration: "Copilot does not offer this model to Atomic's integration. Pick another model, or run /login to refresh the model list.",
|
|
60
|
+
"wrong-api": "Run /login to refresh the model list so Atomic calls the API Copilot serves this model on.",
|
|
61
|
+
"no-endpoint": "Copilot has no endpoint for this model under this account's settings. Pick another model.",
|
|
62
|
+
};
|
|
63
|
+
const INVALID_REQUEST_HINTS = {
|
|
64
|
+
"reasoning-effort": "Pick a thinking level this model lists.",
|
|
65
|
+
"integration-id": "Remove the Copilot-Integration-Id header override from models.json.",
|
|
66
|
+
};
|
|
67
|
+
function failureMessage(code, modelId, hint) {
|
|
68
|
+
return `${WHAT_FAILED[code]}: ${modelId} (${code}). ${hint}`;
|
|
69
|
+
}
|
|
70
|
+
/** Base of every GitHub Copilot request failure. Catch this to handle the whole family. */
|
|
71
|
+
export class GitHubCopilotError extends Error {
|
|
72
|
+
name = "GitHubCopilotError";
|
|
73
|
+
code;
|
|
74
|
+
retryable;
|
|
75
|
+
hint;
|
|
76
|
+
modelId;
|
|
77
|
+
api;
|
|
78
|
+
status;
|
|
79
|
+
providerCode;
|
|
80
|
+
requestId;
|
|
81
|
+
detail;
|
|
82
|
+
constructor(code, context, options) {
|
|
83
|
+
const hint = options?.hint ?? HINTS[code];
|
|
84
|
+
super(failureMessage(code, context.modelId, hint), options?.cause === undefined ? undefined : { cause: options.cause });
|
|
85
|
+
this.code = code;
|
|
86
|
+
this.retryable = RETRYABLE_CODES.has(code);
|
|
87
|
+
this.hint = hint;
|
|
88
|
+
this.modelId = context.modelId;
|
|
89
|
+
this.api = context.api;
|
|
90
|
+
this.status = context.status;
|
|
91
|
+
this.providerCode = context.providerCode;
|
|
92
|
+
this.requestId = context.requestId;
|
|
93
|
+
this.detail = context.detail;
|
|
94
|
+
}
|
|
95
|
+
/** JSON-safe context fields; subclasses add their own. */
|
|
96
|
+
fields() {
|
|
97
|
+
return {
|
|
98
|
+
modelId: this.modelId,
|
|
99
|
+
api: this.api,
|
|
100
|
+
status: this.status ?? null,
|
|
101
|
+
providerCode: this.providerCode ?? null,
|
|
102
|
+
requestId: this.requestId ?? null,
|
|
103
|
+
detail: this.detail ?? null,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Copilot will not serve this model to this account, integration or API. Not retryable; a fallback model may work. */
|
|
108
|
+
export class GitHubCopilotModelUnavailableError extends GitHubCopilotError {
|
|
109
|
+
name = "GitHubCopilotModelUnavailableError";
|
|
110
|
+
reason;
|
|
111
|
+
constructor(reason, context, options) {
|
|
112
|
+
super("model-unavailable", context, { hint: MODEL_UNAVAILABLE_HINTS[reason], cause: options?.cause });
|
|
113
|
+
this.reason = reason;
|
|
114
|
+
}
|
|
115
|
+
fields() {
|
|
116
|
+
return { ...super.fields(), reason: this.reason };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** Copilot throttled the request. Retryable after `retryAfterMs` when the server sent a delay. */
|
|
120
|
+
export class GitHubCopilotRateLimitError extends GitHubCopilotError {
|
|
121
|
+
name = "GitHubCopilotRateLimitError";
|
|
122
|
+
retryAfterMs;
|
|
123
|
+
constructor(context, retryAfterMs, options) {
|
|
124
|
+
super("rate-limited", context, options);
|
|
125
|
+
this.retryAfterMs = retryAfterMs;
|
|
126
|
+
}
|
|
127
|
+
fields() {
|
|
128
|
+
return { ...super.fields(), retryAfterMs: this.retryAfterMs ?? null };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** The plan's quota or billing allowance for the model is used up. Not retryable until it resets. */
|
|
132
|
+
export class GitHubCopilotQuotaExhaustedError extends GitHubCopilotError {
|
|
133
|
+
name = "GitHubCopilotQuotaExhaustedError";
|
|
134
|
+
constructor(context, options) {
|
|
135
|
+
super("quota-exhausted", context, options);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** A content or safety filter blocked the request or the response. The same request is refused again. */
|
|
139
|
+
export class GitHubCopilotContentFilterError extends GitHubCopilotError {
|
|
140
|
+
name = "GitHubCopilotContentFilterError";
|
|
141
|
+
constructor(context, options) {
|
|
142
|
+
super("content-filtered", context, options);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/** The prompt exceeds the model's prompt-token limit. Compact the conversation instead of retrying. */
|
|
146
|
+
export class GitHubCopilotPromptTooLargeError extends GitHubCopilotError {
|
|
147
|
+
name = "GitHubCopilotPromptTooLargeError";
|
|
148
|
+
promptTokens;
|
|
149
|
+
limitTokens;
|
|
150
|
+
constructor(context, tokens, options) {
|
|
151
|
+
super("prompt-too-large", context, options);
|
|
152
|
+
this.promptTokens = tokens?.promptTokens;
|
|
153
|
+
this.limitTokens = tokens?.limitTokens;
|
|
154
|
+
}
|
|
155
|
+
fields() {
|
|
156
|
+
return { ...super.fields(), promptTokens: this.promptTokens ?? null, limitTokens: this.limitTokens ?? null };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Copilot rejected the request's shape or settings. Sending it again fails the same way. */
|
|
160
|
+
export class GitHubCopilotInvalidRequestError extends GitHubCopilotError {
|
|
161
|
+
name = "GitHubCopilotInvalidRequestError";
|
|
162
|
+
reason;
|
|
163
|
+
constructor(reason, context, options) {
|
|
164
|
+
super("invalid-request", context, { hint: INVALID_REQUEST_HINTS[reason], cause: options?.cause });
|
|
165
|
+
this.reason = reason;
|
|
166
|
+
}
|
|
167
|
+
fields() {
|
|
168
|
+
return { ...super.fields(), reason: this.reason };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/** Copilot refused the credential. Sign in again; another provider's model may still work. */
|
|
172
|
+
export class GitHubCopilotAuthenticationError extends GitHubCopilotError {
|
|
173
|
+
name = "GitHubCopilotAuthenticationError";
|
|
174
|
+
constructor(context, options) {
|
|
175
|
+
super("authentication-failed", context, options);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Copilot or the model behind it is down, overloaded or timed out. Retryable. */
|
|
179
|
+
export class GitHubCopilotServiceUnavailableError extends GitHubCopilotError {
|
|
180
|
+
name = "GitHubCopilotServiceUnavailableError";
|
|
181
|
+
constructor(context, options) {
|
|
182
|
+
super("service-unavailable", context, options);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/** The response stream stopped before its finish reason or terminal event. Retryable. */
|
|
186
|
+
export class GitHubCopilotStreamEndedEarlyError extends GitHubCopilotError {
|
|
187
|
+
name = "GitHubCopilotStreamEndedEarlyError";
|
|
188
|
+
constructor(context, options) {
|
|
189
|
+
super("stream-ended-early", context, options);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** The model finished with no text and no tool call, as Copilot's Gemini models sometimes do. Retryable. */
|
|
193
|
+
export class GitHubCopilotEmptyResponseError extends GitHubCopilotError {
|
|
194
|
+
name = "GitHubCopilotEmptyResponseError";
|
|
195
|
+
constructor(context, options) {
|
|
196
|
+
super("empty-response", context, options);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/** Copilot answered with a status, error event or body Atomic does not recognize. Retryable a bounded number of times. */
|
|
200
|
+
export class GitHubCopilotUnrecognizedResponseError extends GitHubCopilotError {
|
|
201
|
+
name = "GitHubCopilotUnrecognizedResponseError";
|
|
202
|
+
reason;
|
|
203
|
+
constructor(reason, context, options) {
|
|
204
|
+
super("unrecognized-response", context, options);
|
|
205
|
+
this.reason = reason;
|
|
206
|
+
}
|
|
207
|
+
fields() {
|
|
208
|
+
return { ...super.fields(), reason: this.reason };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const CODE_BY_NAME = {
|
|
212
|
+
GitHubCopilotModelUnavailableError: "model-unavailable",
|
|
213
|
+
GitHubCopilotRateLimitError: "rate-limited",
|
|
214
|
+
GitHubCopilotQuotaExhaustedError: "quota-exhausted",
|
|
215
|
+
GitHubCopilotContentFilterError: "content-filtered",
|
|
216
|
+
GitHubCopilotPromptTooLargeError: "prompt-too-large",
|
|
217
|
+
GitHubCopilotInvalidRequestError: "invalid-request",
|
|
218
|
+
GitHubCopilotAuthenticationError: "authentication-failed",
|
|
219
|
+
GitHubCopilotServiceUnavailableError: "service-unavailable",
|
|
220
|
+
GitHubCopilotStreamEndedEarlyError: "stream-ended-early",
|
|
221
|
+
GitHubCopilotEmptyResponseError: "empty-response",
|
|
222
|
+
GitHubCopilotUnrecognizedResponseError: "unrecognized-response",
|
|
223
|
+
};
|
|
224
|
+
function structural(value) {
|
|
225
|
+
return typeof value === "object" && value !== null ? value : undefined;
|
|
226
|
+
}
|
|
227
|
+
/** A family member relayed from another process or realm: an object with a family name and that name's code. */
|
|
228
|
+
function isRelayedFamilyMember(value) {
|
|
229
|
+
const candidate = structural(value);
|
|
230
|
+
if (typeof candidate?.name !== "string")
|
|
231
|
+
return false;
|
|
232
|
+
if (candidate.name === "GitHubCopilotError")
|
|
233
|
+
return GITHUB_COPILOT_ERROR_CODES.has(candidate.code);
|
|
234
|
+
return CODE_BY_NAME[candidate.name] === candidate.code;
|
|
235
|
+
}
|
|
236
|
+
/** Rebuild a relayed family member as its class; one that carries only its name and code gets that code's hint. */
|
|
237
|
+
function rebuildRelayedFamilyMember(found) {
|
|
238
|
+
const deserialized = deserializeGitHubCopilotError(found);
|
|
239
|
+
if (deserialized)
|
|
240
|
+
return deserialized;
|
|
241
|
+
if ("fields" in found || "hint" in found || "retryable" in found || "cause" in found)
|
|
242
|
+
return undefined;
|
|
243
|
+
if (Reflect.ownKeys(found).some((key) => key !== "name" && key !== "code" && key !== "message"))
|
|
244
|
+
return undefined;
|
|
245
|
+
if ("message" in found && typeof found.message !== "string")
|
|
246
|
+
return undefined;
|
|
247
|
+
return deserializeGitHubCopilotError({
|
|
248
|
+
name: found.name,
|
|
249
|
+
code: found.code,
|
|
250
|
+
message: found.message ?? "",
|
|
251
|
+
hint: HINTS[found.code],
|
|
252
|
+
retryable: RETRYABLE_CODES.has(found.code),
|
|
253
|
+
fields: {},
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* The typed error at one link of a cause chain: the link itself, rebuilt when it was relayed from another process, or
|
|
258
|
+
* the error its `diagnostics` carry when the link is a failed assistant message, as a workflow failure's cause is.
|
|
259
|
+
*/
|
|
260
|
+
function typedErrorAt(link) {
|
|
261
|
+
if (link instanceof GitHubCopilotError)
|
|
262
|
+
return link;
|
|
263
|
+
if (isRelayedFamilyMember(link))
|
|
264
|
+
return rebuildRelayedFamilyMember(link);
|
|
265
|
+
return getGitHubCopilotError(structural(link));
|
|
266
|
+
}
|
|
267
|
+
/** The first typed error `matches` accepts, walking at most MAX_CAUSE_DEPTH links of the cause chain. */
|
|
268
|
+
function findInCauseChain(error, matches) {
|
|
269
|
+
let current = error;
|
|
270
|
+
for (let depth = 0; depth < MAX_CAUSE_DEPTH; depth++) {
|
|
271
|
+
const typed = typedErrorAt(current);
|
|
272
|
+
if (typed !== undefined && matches(typed))
|
|
273
|
+
return typed;
|
|
274
|
+
current = structural(current)?.cause;
|
|
275
|
+
}
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* A guard that answers whether the error, anything in its cause chain, or a failed assistant message in that chain holds
|
|
280
|
+
* an error of this class. It returns a plain boolean, not a type predicate: the value passed in is usually the wrapper
|
|
281
|
+
* or the message, not the error. Use `findGitHubCopilotError()` for the instance.
|
|
282
|
+
*/
|
|
283
|
+
function guard(ctor) {
|
|
284
|
+
return (error) => findInCauseChain(error, (typed) => typed instanceof ctor) !== undefined;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* True when the error, anything in its cause chain, or a failed assistant message in that chain holds a GitHub Copilot
|
|
288
|
+
* error. A plain boolean, not a type predicate: use `findGitHubCopilotError()` for the instance.
|
|
289
|
+
*/
|
|
290
|
+
export function isGitHubCopilotError(error) {
|
|
291
|
+
return findGitHubCopilotError(error) !== undefined;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* First GitHub Copilot error in the cause chain, as an instance of its class: a link that is one, rebuilt when it was
|
|
295
|
+
* relayed from another process, or the error a failed assistant message's diagnostics carry. The chain is walked to a
|
|
296
|
+
* bounded depth, through JSON round-tripped links too.
|
|
297
|
+
*/
|
|
298
|
+
export function findGitHubCopilotError(error) {
|
|
299
|
+
return findInCauseChain(error, () => true);
|
|
300
|
+
}
|
|
301
|
+
/** True when the first GitHub Copilot error in the cause chain says the same request may be sent again. */
|
|
302
|
+
export function isRetryableGitHubCopilotError(error) {
|
|
303
|
+
return findGitHubCopilotError(error)?.retryable === true;
|
|
304
|
+
}
|
|
305
|
+
export const isGitHubCopilotModelUnavailableError = guard(GitHubCopilotModelUnavailableError);
|
|
306
|
+
export const isGitHubCopilotRateLimitError = guard(GitHubCopilotRateLimitError);
|
|
307
|
+
export const isGitHubCopilotQuotaExhaustedError = guard(GitHubCopilotQuotaExhaustedError);
|
|
308
|
+
export const isGitHubCopilotContentFilterError = guard(GitHubCopilotContentFilterError);
|
|
309
|
+
export const isGitHubCopilotPromptTooLargeError = guard(GitHubCopilotPromptTooLargeError);
|
|
310
|
+
export const isGitHubCopilotInvalidRequestError = guard(GitHubCopilotInvalidRequestError);
|
|
311
|
+
export const isGitHubCopilotAuthenticationError = guard(GitHubCopilotAuthenticationError);
|
|
312
|
+
export const isGitHubCopilotServiceUnavailableError = guard(GitHubCopilotServiceUnavailableError);
|
|
313
|
+
export const isGitHubCopilotStreamEndedEarlyError = guard(GitHubCopilotStreamEndedEarlyError);
|
|
314
|
+
export const isGitHubCopilotEmptyResponseError = guard(GitHubCopilotEmptyResponseError);
|
|
315
|
+
export const isGitHubCopilotUnrecognizedResponseError = guard(GitHubCopilotUnrecognizedResponseError);
|
|
316
|
+
function serializeCause(cause, depth) {
|
|
317
|
+
if (depth >= MAX_CAUSE_DEPTH || cause === undefined || cause === null)
|
|
318
|
+
return undefined;
|
|
319
|
+
const candidate = structural(cause);
|
|
320
|
+
const nested = serializeCause(candidate?.cause, depth + 1);
|
|
321
|
+
return {
|
|
322
|
+
name: typeof candidate?.name === "string" ? candidate.name : typeof cause,
|
|
323
|
+
message: truncate(cause instanceof Error ? cause.message : String(cause)),
|
|
324
|
+
...(typeof candidate?.code === "string" ? { code: candidate.code } : {}),
|
|
325
|
+
...(nested === undefined ? {} : { cause: nested }),
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
/** Wire form for crossing a process boundary; the cause chain is kept to a bounded depth. */
|
|
329
|
+
export function serializeGitHubCopilotError(error) {
|
|
330
|
+
const cause = serializeCause(error.cause, 0);
|
|
331
|
+
return {
|
|
332
|
+
name: error.name,
|
|
333
|
+
code: error.code,
|
|
334
|
+
message: error.message,
|
|
335
|
+
hint: error.hint,
|
|
336
|
+
retryable: error.retryable,
|
|
337
|
+
fields: error.fields(),
|
|
338
|
+
...(cause === undefined ? {} : { cause }),
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
function rehydrateCause(cause) {
|
|
342
|
+
if (cause === undefined)
|
|
343
|
+
return undefined;
|
|
344
|
+
const family = isRelayedFamilyMember(cause)
|
|
345
|
+
? rebuildRelayedFamilyMember({ name: cause.name, code: cause.code, message: cause.message })
|
|
346
|
+
: undefined;
|
|
347
|
+
const error = family ?? new Error(cause.message);
|
|
348
|
+
Object.defineProperties(error, {
|
|
349
|
+
message: { value: cause.message },
|
|
350
|
+
cause: { value: rehydrateCause(cause.cause), writable: true, configurable: true },
|
|
351
|
+
});
|
|
352
|
+
Object.defineProperty(error, "name", { value: cause.name });
|
|
353
|
+
if (cause.code !== undefined)
|
|
354
|
+
Object.defineProperty(error, "code", { value: cause.code, enumerable: true });
|
|
355
|
+
return error;
|
|
356
|
+
}
|
|
357
|
+
function stringField(fields, key) {
|
|
358
|
+
const value = fields[key];
|
|
359
|
+
return typeof value === "string" ? value : undefined;
|
|
360
|
+
}
|
|
361
|
+
function numberField(fields, key) {
|
|
362
|
+
const value = fields[key];
|
|
363
|
+
return typeof value === "number" ? value : undefined;
|
|
364
|
+
}
|
|
365
|
+
function contextFromFields(fields) {
|
|
366
|
+
return {
|
|
367
|
+
modelId: stringField(fields, "modelId") ?? "",
|
|
368
|
+
api: stringField(fields, "api") ?? "",
|
|
369
|
+
status: numberField(fields, "status"),
|
|
370
|
+
providerCode: stringField(fields, "providerCode"),
|
|
371
|
+
requestId: stringField(fields, "requestId"),
|
|
372
|
+
detail: stringField(fields, "detail"),
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function isJsonRecord(value) {
|
|
376
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
377
|
+
}
|
|
378
|
+
function isSerializedCause(value, depth = 0) {
|
|
379
|
+
if (value === undefined)
|
|
380
|
+
return true;
|
|
381
|
+
if (depth >= MAX_CAUSE_DEPTH || !isJsonRecord(value))
|
|
382
|
+
return false;
|
|
383
|
+
return (typeof value.name === "string" &&
|
|
384
|
+
typeof value.message === "string" &&
|
|
385
|
+
(value.code === undefined || typeof value.code === "string") &&
|
|
386
|
+
isSerializedCause(value.cause, depth + 1));
|
|
387
|
+
}
|
|
388
|
+
function isSerializedError(value) {
|
|
389
|
+
if (!isJsonRecord(value) || !isJsonRecord(value.fields))
|
|
390
|
+
return false;
|
|
391
|
+
return (typeof value.name === "string" &&
|
|
392
|
+
GITHUB_COPILOT_ERROR_CODES.has(value.code) &&
|
|
393
|
+
typeof value.message === "string" &&
|
|
394
|
+
typeof value.hint === "string" &&
|
|
395
|
+
typeof value.retryable === "boolean" &&
|
|
396
|
+
Object.values(value.fields).every((field) => field === null || typeof field === "string" || typeof field === "number") &&
|
|
397
|
+
isSerializedCause(value.cause));
|
|
398
|
+
}
|
|
399
|
+
/** A serialized reason: one of `allowed`, or `fallback` when the field is absent. Undefined when it is anything else. */
|
|
400
|
+
function reasonField(fields, allowed, fallback) {
|
|
401
|
+
const reason = fields.reason;
|
|
402
|
+
if (reason === undefined || reason === null)
|
|
403
|
+
return fallback;
|
|
404
|
+
return allowed.find((candidate) => candidate === reason);
|
|
405
|
+
}
|
|
406
|
+
/** Whether a serialized error's name, if it is a known class, goes with its code. */
|
|
407
|
+
function agreesWithClass(serialized) {
|
|
408
|
+
return (!Object.hasOwn(CODE_BY_NAME, serialized.name) ||
|
|
409
|
+
CODE_BY_NAME[serialized.name] === serialized.code);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Rebuild a serialized GitHub Copilot error as its own class. An unknown name becomes the family base with the
|
|
413
|
+
* original name, code, message, hint and retryability kept. Returns undefined for anything that is not a serialized
|
|
414
|
+
* GitHub Copilot error: a code this version does not know, a class name that does not go with its code, or a reason
|
|
415
|
+
* that class does not have.
|
|
416
|
+
*/
|
|
417
|
+
export function deserializeGitHubCopilotError(value) {
|
|
418
|
+
if (!isSerializedError(value) || !agreesWithClass(value))
|
|
419
|
+
return undefined;
|
|
420
|
+
const cause = rehydrateCause(value.cause);
|
|
421
|
+
const fields = value.fields;
|
|
422
|
+
const context = contextFromFields(fields);
|
|
423
|
+
switch (value.name) {
|
|
424
|
+
case "GitHubCopilotModelUnavailableError": {
|
|
425
|
+
const reason = reasonField(fields, MODEL_UNAVAILABLE_REASONS, "not-supported");
|
|
426
|
+
return reason === undefined ? undefined : new GitHubCopilotModelUnavailableError(reason, context, { cause });
|
|
427
|
+
}
|
|
428
|
+
case "GitHubCopilotRateLimitError":
|
|
429
|
+
return new GitHubCopilotRateLimitError(context, numberField(fields, "retryAfterMs"), { cause });
|
|
430
|
+
case "GitHubCopilotQuotaExhaustedError":
|
|
431
|
+
return new GitHubCopilotQuotaExhaustedError(context, { cause });
|
|
432
|
+
case "GitHubCopilotContentFilterError":
|
|
433
|
+
return new GitHubCopilotContentFilterError(context, { cause });
|
|
434
|
+
case "GitHubCopilotPromptTooLargeError":
|
|
435
|
+
return new GitHubCopilotPromptTooLargeError(context, { promptTokens: numberField(fields, "promptTokens"), limitTokens: numberField(fields, "limitTokens") }, { cause });
|
|
436
|
+
case "GitHubCopilotInvalidRequestError": {
|
|
437
|
+
const reason = reasonField(fields, INVALID_REQUEST_REASONS, "rejected");
|
|
438
|
+
return reason === undefined ? undefined : new GitHubCopilotInvalidRequestError(reason, context, { cause });
|
|
439
|
+
}
|
|
440
|
+
case "GitHubCopilotAuthenticationError":
|
|
441
|
+
return new GitHubCopilotAuthenticationError(context, { cause });
|
|
442
|
+
case "GitHubCopilotServiceUnavailableError":
|
|
443
|
+
return new GitHubCopilotServiceUnavailableError(context, { cause });
|
|
444
|
+
case "GitHubCopilotStreamEndedEarlyError":
|
|
445
|
+
return new GitHubCopilotStreamEndedEarlyError(context, { cause });
|
|
446
|
+
case "GitHubCopilotEmptyResponseError":
|
|
447
|
+
return new GitHubCopilotEmptyResponseError(context, { cause });
|
|
448
|
+
case "GitHubCopilotUnrecognizedResponseError": {
|
|
449
|
+
const reason = reasonField(fields, UNRECOGNIZED_REASONS, "malformed-body");
|
|
450
|
+
return reason === undefined
|
|
451
|
+
? undefined
|
|
452
|
+
: new GitHubCopilotUnrecognizedResponseError(reason, context, { cause });
|
|
453
|
+
}
|
|
454
|
+
default: {
|
|
455
|
+
const base = new GitHubCopilotError(value.code, context, { hint: value.hint, cause });
|
|
456
|
+
Object.defineProperties(base, {
|
|
457
|
+
name: { value: value.name },
|
|
458
|
+
message: { value: value.message },
|
|
459
|
+
retryable: { value: value.retryable },
|
|
460
|
+
});
|
|
461
|
+
return base;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/** Record `error` on a failed assistant message: its diagnostics carry the typed error across processes. */
|
|
466
|
+
export function recordGitHubCopilotError(output, error, providerText) {
|
|
467
|
+
output.errorMessage =
|
|
468
|
+
providerText === undefined || providerText === error.message
|
|
469
|
+
? error.message
|
|
470
|
+
: `${error.message}\n\n${providerText}`;
|
|
471
|
+
if (error instanceof GitHubCopilotRateLimitError && error.retryAfterMs !== undefined) {
|
|
472
|
+
output.retryAfterMs = error.retryAfterMs;
|
|
473
|
+
}
|
|
474
|
+
appendAssistantMessageDiagnostic(output, {
|
|
475
|
+
type: GITHUB_COPILOT_ERROR_DIAGNOSTIC,
|
|
476
|
+
timestamp: Date.now(),
|
|
477
|
+
error: { name: error.name, message: error.message, code: error.code },
|
|
478
|
+
details: JSON.parse(JSON.stringify(serializeGitHubCopilotError(error))),
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* The GitHub Copilot error an assistant message's diagnostics carry, rebuilt as its class: the latest valid one.
|
|
483
|
+
* Accepts a message read back from JSON, whose diagnostics are validated here.
|
|
484
|
+
*/
|
|
485
|
+
export function getGitHubCopilotError(message) {
|
|
486
|
+
const diagnostics = message?.diagnostics;
|
|
487
|
+
if (!Array.isArray(diagnostics))
|
|
488
|
+
return undefined;
|
|
489
|
+
for (const entry of [...diagnostics].reverse()) {
|
|
490
|
+
const diagnostic = structural(entry);
|
|
491
|
+
if (diagnostic?.type !== GITHUB_COPILOT_ERROR_DIAGNOSTIC)
|
|
492
|
+
continue;
|
|
493
|
+
const error = deserializeGitHubCopilotError(diagnostic.details);
|
|
494
|
+
if (error !== undefined)
|
|
495
|
+
return error;
|
|
496
|
+
}
|
|
497
|
+
return undefined;
|
|
498
|
+
}
|
|
499
|
+
function truncate(text) {
|
|
500
|
+
return text.length <= MAX_DETAIL_CHARS ? text : `${text.slice(0, MAX_DETAIL_CHARS)}…`;
|
|
501
|
+
}
|
|
502
|
+
function nonEmptyString(value) {
|
|
503
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* The error that holds the provider's response. When the retry helper gave up on a server-requested delay above its
|
|
507
|
+
* cap, it threw a wrapper that keeps only `status` and `retryAfterMs` and holds the original response error as its
|
|
508
|
+
* `cause`: no code, body or headers of its own. The response is then that cause. Any other error is its own response.
|
|
509
|
+
*/
|
|
510
|
+
function responseOf(error) {
|
|
511
|
+
const origin = error.cause;
|
|
512
|
+
const wrapsResponse = error.code === undefined &&
|
|
513
|
+
error.error === undefined &&
|
|
514
|
+
error.headers === undefined &&
|
|
515
|
+
origin instanceof Error &&
|
|
516
|
+
typeof origin.status === "number";
|
|
517
|
+
return wrapsResponse ? origin : error;
|
|
518
|
+
}
|
|
519
|
+
/** The SDK's request ID, else Copilot's `x-request-id` header, which the Anthropic SDK does not read. */
|
|
520
|
+
function requestIdOf(error) {
|
|
521
|
+
const headers = error.headers;
|
|
522
|
+
const header = headers instanceof Headers ? headers.get("x-request-id") : undefined;
|
|
523
|
+
return nonEmptyString(error.requestID) ?? nonEmptyString(error.request_id) ?? nonEmptyString(header);
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* The provider's code: the SDK's `code`, then the parsed body's `error.code` or `code`. A Messages `error` event
|
|
527
|
+
* arrives with no HTTP status and names its failure only by `type`, so that type is its code.
|
|
528
|
+
*/
|
|
529
|
+
function providerCodeOf(error) {
|
|
530
|
+
const body = structural(error.error);
|
|
531
|
+
return (nonEmptyString(error.code) ??
|
|
532
|
+
nonEmptyString(structural(body?.error)?.code) ??
|
|
533
|
+
nonEmptyString(body?.code) ??
|
|
534
|
+
(error.status === undefined && error.error !== undefined ? nonEmptyString(error.type) : undefined));
|
|
535
|
+
}
|
|
536
|
+
/** The provider's error text: the parsed body's message, else the SDK message without its status prefix. */
|
|
537
|
+
function providerDetailOf(error, status) {
|
|
538
|
+
const body = structural(error.error);
|
|
539
|
+
const bodyMessage = nonEmptyString(structural(body?.error)?.message) ??
|
|
540
|
+
nonEmptyString(body?.message);
|
|
541
|
+
if (bodyMessage !== undefined)
|
|
542
|
+
return truncate(bodyMessage);
|
|
543
|
+
if (typeof error.error === "string") {
|
|
544
|
+
const text = nonEmptyString(error.error);
|
|
545
|
+
return text === undefined ? undefined : truncate(text);
|
|
546
|
+
}
|
|
547
|
+
const message = status !== undefined && error.message.startsWith(`${status} `)
|
|
548
|
+
? error.message.slice(`${status} `.length)
|
|
549
|
+
: error.message;
|
|
550
|
+
const text = nonEmptyString(message);
|
|
551
|
+
return text === undefined ? undefined : truncate(text);
|
|
552
|
+
}
|
|
553
|
+
/** Provider codes Copilot sends, observed live or documented publicly, and the failure each one means. */
|
|
554
|
+
const PROVIDER_CODE_FAILURES = {
|
|
555
|
+
model_not_available_for_integrator: (context, cause) => new GitHubCopilotModelUnavailableError("integration", context, { cause }),
|
|
556
|
+
model_not_supported: (context, cause) => new GitHubCopilotModelUnavailableError("not-supported", context, { cause }),
|
|
557
|
+
unsupported_api_for_model: (context, cause) => new GitHubCopilotModelUnavailableError("wrong-api", context, { cause }),
|
|
558
|
+
model_max_prompt_tokens_exceeded: (context, cause) => promptTooLarge(context, cause),
|
|
559
|
+
context_length_exceeded: (context, cause) => promptTooLarge(context, cause),
|
|
560
|
+
invalid_reasoning_effort: (context, cause) => new GitHubCopilotInvalidRequestError("reasoning-effort", context, { cause }),
|
|
561
|
+
content_filter: (context, cause) => new GitHubCopilotContentFilterError(context, { cause }),
|
|
562
|
+
cyber_policy: (context, cause) => new GitHubCopilotContentFilterError(context, { cause }),
|
|
563
|
+
rate_limit_exceeded: (context, cause) => new GitHubCopilotRateLimitError(context, getProviderRetryAfterMs(cause), { cause }),
|
|
564
|
+
insufficient_quota: (context, cause) => new GitHubCopilotQuotaExhaustedError(context, { cause }),
|
|
565
|
+
quota_exceeded: (context, cause) => new GitHubCopilotQuotaExhaustedError(context, { cause }),
|
|
566
|
+
server_error: (context, cause) => new GitHubCopilotServiceUnavailableError(context, { cause }),
|
|
567
|
+
invalid_request_error: (context, cause) => badRequestTextFailure(undefined, context, cause) ??
|
|
568
|
+
new GitHubCopilotInvalidRequestError("rejected", context, { cause }),
|
|
569
|
+
authentication_error: (context, cause) => new GitHubCopilotAuthenticationError(context, { cause }),
|
|
570
|
+
permission_error: (context, cause) => new GitHubCopilotAuthenticationError(context, { cause }),
|
|
571
|
+
billing_error: (context, cause) => new GitHubCopilotQuotaExhaustedError(context, { cause }),
|
|
572
|
+
not_found_error: (context, cause) => new GitHubCopilotModelUnavailableError("not-found", context, { cause }),
|
|
573
|
+
request_too_large: (context, cause) => promptTooLarge(context, cause),
|
|
574
|
+
rate_limit_error: (context, cause) => new GitHubCopilotRateLimitError(context, getProviderRetryAfterMs(cause), { cause }),
|
|
575
|
+
overloaded_error: (context, cause) => new GitHubCopilotServiceUnavailableError(context, { cause }),
|
|
576
|
+
api_error: (context, cause) => new GitHubCopilotServiceUnavailableError(context, { cause }),
|
|
577
|
+
timeout_error: (context, cause) => new GitHubCopilotServiceUnavailableError(context, { cause }),
|
|
578
|
+
};
|
|
579
|
+
const PROMPT_TOKENS_PATTERN = /prompt token count of (\d+) exceeds the limit of (\d+)/i;
|
|
580
|
+
function promptTooLarge(context, cause) {
|
|
581
|
+
const match = context.detail?.match(PROMPT_TOKENS_PATTERN);
|
|
582
|
+
return new GitHubCopilotPromptTooLargeError(context, match ? { promptTokens: Number(match[1]), limitTokens: Number(match[2]) } : undefined, { cause });
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Plain-text 400 bodies Copilot sends without a JSON code, observed live. Each is matched in full at the boundary so
|
|
586
|
+
* the rest of Atomic branches on the typed code instead of on this text.
|
|
587
|
+
*/
|
|
588
|
+
function plainTextFailure(detail, context, cause) {
|
|
589
|
+
const text = detail?.toLowerCase();
|
|
590
|
+
if (text === "no model endpoints available given user constraints") {
|
|
591
|
+
return new GitHubCopilotModelUnavailableError("no-endpoint", context, { cause });
|
|
592
|
+
}
|
|
593
|
+
if (text === "bad request: unknown copilot-integration-id") {
|
|
594
|
+
return new GitHubCopilotInvalidRequestError("integration-id", context, { cause });
|
|
595
|
+
}
|
|
596
|
+
return undefined;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Copilot sends a used-up monthly quota as HTTP 429 with `x-ratelimit-exceeded: quota_exceeded` and the plain body
|
|
600
|
+
* `quota exceeded` (seen in public reports), unlike a short rate limit.
|
|
601
|
+
*/
|
|
602
|
+
function reportsExhaustedQuota(response) {
|
|
603
|
+
const exceeded = response.headers instanceof Headers ? response.headers.get("x-ratelimit-exceeded") : null;
|
|
604
|
+
return (exceeded?.trim().toLowerCase() === "quota_exceeded" ||
|
|
605
|
+
providerDetailOf(response, 429)?.toLowerCase() === "quota exceeded");
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* A 400 whose code Atomic does not recognize: Copilot's own prompt-token overflow text means the prompt is too large,
|
|
609
|
+
* whatever the code; without a code, the exact plain-text bodies Copilot sends are matched.
|
|
610
|
+
*/
|
|
611
|
+
function badRequestTextFailure(providerCode, context, cause) {
|
|
612
|
+
if (context.detail?.match(PROMPT_TOKENS_PATTERN))
|
|
613
|
+
return promptTooLarge(context, cause);
|
|
614
|
+
return providerCode === undefined ? plainTextFailure(context.detail, context, cause) : undefined;
|
|
615
|
+
}
|
|
616
|
+
function statusFailure(status, context, cause, reportsQuota) {
|
|
617
|
+
switch (status) {
|
|
618
|
+
case 401:
|
|
619
|
+
case 403:
|
|
620
|
+
return new GitHubCopilotAuthenticationError(context, { cause });
|
|
621
|
+
case 402:
|
|
622
|
+
return new GitHubCopilotQuotaExhaustedError(context, { cause });
|
|
623
|
+
case 404:
|
|
624
|
+
return new GitHubCopilotModelUnavailableError("not-found", context, { cause });
|
|
625
|
+
case 408:
|
|
626
|
+
return new GitHubCopilotServiceUnavailableError(context, { cause });
|
|
627
|
+
case 413:
|
|
628
|
+
return promptTooLarge(context, cause);
|
|
629
|
+
case 429:
|
|
630
|
+
return reportsQuota
|
|
631
|
+
? new GitHubCopilotQuotaExhaustedError(context, { cause })
|
|
632
|
+
: new GitHubCopilotRateLimitError(context, getProviderRetryAfterMs(cause), { cause });
|
|
633
|
+
}
|
|
634
|
+
if (status >= 500 && status <= 599)
|
|
635
|
+
return new GitHubCopilotServiceUnavailableError(context, { cause });
|
|
636
|
+
if (status >= 400 && status <= 499)
|
|
637
|
+
return new GitHubCopilotInvalidRequestError("rejected", context, { cause });
|
|
638
|
+
return new GitHubCopilotUnrecognizedResponseError("unknown-status", context, { cause });
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* The stop reasons each API reports when a content or safety filter ended the response. Responses also records a
|
|
642
|
+
* plain `refusal` for a refusal delta that does not end the turn, so each API has its own set.
|
|
643
|
+
*/
|
|
644
|
+
const CONTENT_FILTER_STOPS = {
|
|
645
|
+
"openai-completions": new Set(["content_filter"]),
|
|
646
|
+
"openai-responses": new Set(["incomplete.content_filter"]),
|
|
647
|
+
"anthropic-messages": new Set(["refusal", "sensitive"]),
|
|
648
|
+
};
|
|
649
|
+
/**
|
|
650
|
+
* Translate a failed Copilot request into its typed error, once, at the provider boundary.
|
|
651
|
+
*
|
|
652
|
+
* HTTP failures are classified by the provider's code first, then on a 400 by Copilot's prompt-token overflow text
|
|
653
|
+
* and the exact plain-text bodies it sends without a code, then by status (a 429 that reports a used-up quota is
|
|
654
|
+
* `quota-exhausted`). A failure after the stream started is a content-filter stop for its API, an in-stream error
|
|
655
|
+
* event, a malformed body, or a stream that ended early. Returns undefined for a failure that never reached Copilot (a
|
|
656
|
+
* connection error before any response), which keeps Atomic's generic network handling. Never call it for a
|
|
657
|
+
* cancelled request: cancellation is not a failure.
|
|
658
|
+
*/
|
|
659
|
+
export function classifyGitHubCopilotFailure(error, failure) {
|
|
660
|
+
const typed = findGitHubCopilotError(error);
|
|
661
|
+
if (typed !== undefined)
|
|
662
|
+
return typed;
|
|
663
|
+
const providerError = error instanceof Error ? error : new Error(String(error), { cause: error });
|
|
664
|
+
const response = responseOf(providerError);
|
|
665
|
+
const status = typeof providerError.status === "number" ? providerError.status : undefined;
|
|
666
|
+
const providerCode = providerCodeOf(response);
|
|
667
|
+
const context = {
|
|
668
|
+
modelId: failure.modelId,
|
|
669
|
+
api: failure.api,
|
|
670
|
+
status,
|
|
671
|
+
providerCode,
|
|
672
|
+
requestId: requestIdOf(response),
|
|
673
|
+
detail: providerDetailOf(response, typeof response.status === "number" ? response.status : undefined),
|
|
674
|
+
};
|
|
675
|
+
const codeFailure = providerCode === undefined ? undefined : PROVIDER_CODE_FAILURES[providerCode.toLowerCase()];
|
|
676
|
+
if (codeFailure !== undefined)
|
|
677
|
+
return codeFailure(context, providerError);
|
|
678
|
+
if (status !== undefined) {
|
|
679
|
+
return ((status === 400 ? badRequestTextFailure(providerCode, context, providerError) : undefined) ??
|
|
680
|
+
statusFailure(status, context, providerError, reportsExhaustedQuota(response)));
|
|
681
|
+
}
|
|
682
|
+
if (CONTENT_FILTER_STOPS[failure.api]?.has(failure.finishReason)) {
|
|
683
|
+
return new GitHubCopilotContentFilterError(context, { cause: providerError });
|
|
684
|
+
}
|
|
685
|
+
if (providerCode !== undefined) {
|
|
686
|
+
return new GitHubCopilotUnrecognizedResponseError("unknown-error-event", context, { cause: providerError });
|
|
687
|
+
}
|
|
688
|
+
if (providerError instanceof SyntaxError || providerError.error !== undefined) {
|
|
689
|
+
return new GitHubCopilotUnrecognizedResponseError("malformed-body", context, { cause: providerError });
|
|
690
|
+
}
|
|
691
|
+
return failure.streamStarted ? new GitHubCopilotStreamEndedEarlyError(context, { cause: providerError }) : undefined;
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Type a failed Copilot assistant message at the provider boundary: classify `error` and record it on `output`,
|
|
695
|
+
* keeping the provider's text after the typed message. A cancelled message is left alone.
|
|
696
|
+
*/
|
|
697
|
+
export function recordGitHubCopilotFailure(output, error, streamStarted) {
|
|
698
|
+
if (output.stopReason !== "error")
|
|
699
|
+
return;
|
|
700
|
+
const typed = classifyGitHubCopilotFailure(error, {
|
|
701
|
+
modelId: output.model,
|
|
702
|
+
api: output.api,
|
|
703
|
+
streamStarted,
|
|
704
|
+
finishReason: output.rawStopReason,
|
|
705
|
+
});
|
|
706
|
+
if (typed !== undefined)
|
|
707
|
+
recordGitHubCopilotError(output, typed, output.errorMessage);
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* The typed failure for a completed Copilot turn that has no text and no tool call, as Copilot's Gemini models
|
|
711
|
+
* sometimes return; undefined for any other turn. A length stop is left to the caller's length handling.
|
|
712
|
+
*/
|
|
713
|
+
export function githubCopilotEmptyResponse(output) {
|
|
714
|
+
if (output.provider !== "github-copilot" || (output.stopReason !== "stop" && output.stopReason !== "toolUse")) {
|
|
715
|
+
return undefined;
|
|
716
|
+
}
|
|
717
|
+
const answered = output.content.some((block) => block.type === "toolCall" || (block.type === "text" && block.text.trim().length > 0));
|
|
718
|
+
return answered ? undefined : new GitHubCopilotEmptyResponseError({ modelId: output.model, api: output.api });
|
|
719
|
+
}
|
|
720
|
+
//# sourceMappingURL=github-copilot-errors.js.map
|