@bastani/atomic-ai 0.9.90 → 0.10.0
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 +20 -0
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +12 -9
- 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 +6 -4
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/classifier-shared.d.ts.map +1 -1
- package/dist/api/classifier-shared.js +4 -3
- package/dist/api/classifier-shared.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +6 -3
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts +2 -1
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +3 -2
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +8 -4
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/llama-cpp-classify.d.ts.map +1 -1
- package/dist/api/llama-cpp-classify.js +6 -4
- package/dist/api/llama-cpp-classify.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +9 -16
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +34 -6
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-decisions.d.ts.map +1 -1
- package/dist/api/openai-decisions.js +2 -1
- package/dist/api/openai-decisions.js.map +1 -1
- package/dist/api/openai-images.d.ts.map +1 -1
- package/dist/api/openai-images.js +6 -4
- package/dist/api/openai-images.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +6 -4
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/openrouter-images.d.ts.map +1 -1
- package/dist/api/openrouter-images.js +6 -4
- package/dist/api/openrouter-images.js.map +1 -1
- package/dist/api/simple-options.d.ts.map +1 -1
- package/dist/api/simple-options.js +1 -0
- package/dist/api/simple-options.js.map +1 -1
- package/dist/api/system-one-shared.d.ts.map +1 -1
- package/dist/api/system-one-shared.js +2 -1
- package/dist/api/system-one-shared.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/providers/data/fireworks.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/github-copilot-stop-reason.d.ts +14 -0
- package/dist/providers/github-copilot-stop-reason.d.ts.map +1 -0
- package/dist/providers/github-copilot-stop-reason.js +27 -0
- package/dist/providers/github-copilot-stop-reason.js.map +1 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/provider-retry.d.ts +20 -1
- package/dist/utils/provider-retry.d.ts.map +1 -1
- package/dist/utils/provider-retry.js +46 -19
- package/dist/utils/provider-retry.js.map +1 -1
- package/dist/utils/retry-sleep.d.ts +10 -0
- package/dist/utils/retry-sleep.d.ts.map +1 -0
- package/dist/utils/retry-sleep.js +38 -0
- package/dist/utils/retry-sleep.js.map +1 -0
- package/dist/utils/retry.d.ts +6 -2
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +9 -1
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/sse.d.ts +11 -0
- package/dist/utils/sse.d.ts.map +1 -1
- package/dist/utils/sse.js +131 -0
- package/dist/utils/sse.js.map +1 -1
- 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 +3 -3
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { calculateCost, clampThinkingLevel } from "../models.js";
|
|
2
2
|
import { githubCopilotEmptyResponse, recordGitHubCopilotFailure } from "../providers/github-copilot-errors.js";
|
|
3
|
+
import { inferGitHubCopilotStopReason, recordGitHubCopilotInferredStopReason, } from "../providers/github-copilot-stop-reason.js";
|
|
3
4
|
import { GitHubCopilotStreamAttempt } from "../providers/github-copilot-stream-attempt.js";
|
|
4
5
|
import { appendProviderResponseFailureDiagnostic, formatProviderError, normalizeProviderError, } from "../utils/error-body.js";
|
|
5
6
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
@@ -8,10 +9,12 @@ import { headersToRecord } from "../utils/headers.js";
|
|
|
8
9
|
import { getIdentityHeaders } from "../utils/identity.js";
|
|
9
10
|
import { applyOpenRouterReportedCost, isOpenRouterModel, } from "../utils/openrouter-cost.js";
|
|
10
11
|
import { getAtomicEnvValue } from "../utils/provider-env.js";
|
|
11
|
-
import { retryProviderRequest } from "../utils/provider-retry.js";
|
|
12
|
-
import {
|
|
12
|
+
import { ProviderRetryTransport, retryProviderRequest } from "../utils/provider-retry.js";
|
|
13
|
+
import { RetryDeadlineError } from "../utils/retry-sleep.js";
|
|
14
|
+
import { admitOpenAIRequests, getFetchAdmission, wrapFetchPreservingAdmission } from "../utils/request-admission.js";
|
|
13
15
|
import { getProviderRetryAfterMs } from "../utils/retry-hints.js";
|
|
14
16
|
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
17
|
+
import { DoneEventWatch } from "../utils/sse.js";
|
|
15
18
|
import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadline.js";
|
|
16
19
|
import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
|
|
17
20
|
import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
|
|
@@ -212,13 +215,20 @@ export const stream = (model, context, options) => {
|
|
|
212
215
|
};
|
|
213
216
|
const streamDeadline = createStreamDeadline(options?.streamDeadlineMs, options?.signal);
|
|
214
217
|
const streamAttempt = new GitHubCopilotStreamAttempt();
|
|
218
|
+
// GitHub Copilot sometimes ends a response stream normally without a finish reason (seen with Gemini models);
|
|
219
|
+
// whether the stream delivered its final `[DONE]` tells such a response from a stream that was cut off.
|
|
220
|
+
const doneEvent = model.provider === "github-copilot" ? new DoneEventWatch() : undefined;
|
|
215
221
|
try {
|
|
216
222
|
const apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
217
223
|
const compat = getCompat(model);
|
|
218
224
|
const grammarToolInputProperties = createGrammarToolInputProperties(getDeclaredTools(normalizedContext.messages), compat.supportsOpenAIGrammarTools);
|
|
219
225
|
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
220
226
|
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
|
|
221
|
-
const
|
|
227
|
+
const retryTransport = ProviderRetryTransport.for(options);
|
|
228
|
+
const transportFetch = retryTransport?.fetch ?? options?.fetch;
|
|
229
|
+
const client = createClient(model, normalizedContext, apiKey, options?.headers, options?.identity, doneEvent
|
|
230
|
+
? wrapFetchPreservingAdmission(transportFetch ?? globalThis.fetch, (send) => doneEvent.watch(send))
|
|
231
|
+
: transportFetch, cacheSessionId, compat, options?.apiKey);
|
|
222
232
|
const thinkingOffFallback = options?.reasoningEffort
|
|
223
233
|
? undefined
|
|
224
234
|
: recordCompletionsThinkingOffFallback(model, compat, output);
|
|
@@ -240,7 +250,7 @@ export const stream = (model, context, options) => {
|
|
|
240
250
|
maxRetries: options?.maxRetries,
|
|
241
251
|
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
242
252
|
signal: streamDeadline.signal,
|
|
243
|
-
});
|
|
253
|
+
}, retryTransport);
|
|
244
254
|
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
245
255
|
stream.push({ type: "start", partial: output });
|
|
246
256
|
streamAttempt.respond(response.headers);
|
|
@@ -248,6 +258,8 @@ export const stream = (model, context, options) => {
|
|
|
248
258
|
let thinkingBlock = null;
|
|
249
259
|
let opaqueReasoning = null;
|
|
250
260
|
let hasFinishReason = false;
|
|
261
|
+
// Set when a finished tool call is not provably complete, which only a finish reason can then vouch for.
|
|
262
|
+
let incompleteToolCall = false;
|
|
251
263
|
const toolCallBlocksByIndex = new Map();
|
|
252
264
|
const toolCallBlocksById = new Map();
|
|
253
265
|
const toolCallStates = new Map();
|
|
@@ -284,6 +296,8 @@ export const stream = (model, context, options) => {
|
|
|
284
296
|
const input = toolCallStates.get(block).input;
|
|
285
297
|
toolCallStates.delete(block);
|
|
286
298
|
if (input.kind === "custom") {
|
|
299
|
+
// A custom tool's input has no end of its own on this API.
|
|
300
|
+
incompleteToolCall = true;
|
|
287
301
|
const delta = closeGrammarToolInputJson(input.buffer, input.property, undefined);
|
|
288
302
|
if (delta !== undefined) {
|
|
289
303
|
stream.push({
|
|
@@ -296,6 +310,10 @@ export const stream = (model, context, options) => {
|
|
|
296
310
|
}
|
|
297
311
|
else {
|
|
298
312
|
Object.assign(block, input.arguments.finish());
|
|
313
|
+
// Arguments cut off mid-object do not parse; a call with no arguments has `{}`.
|
|
314
|
+
if (!block.name || input.arguments.text.trim() === "" || block.argumentsError !== undefined) {
|
|
315
|
+
incompleteToolCall = true;
|
|
316
|
+
}
|
|
299
317
|
}
|
|
300
318
|
stream.push({
|
|
301
319
|
type: "toolcall_end",
|
|
@@ -530,18 +548,28 @@ export const stream = (model, context, options) => {
|
|
|
530
548
|
if (output.stopReason === "aborted") {
|
|
531
549
|
throw new Error("Request was aborted");
|
|
532
550
|
}
|
|
551
|
+
const inferredStopReason = doneEvent?.received && compat.supportsFinishReason && !hasFinishReason
|
|
552
|
+
? inferGitHubCopilotStopReason(output, !incompleteToolCall)
|
|
553
|
+
: undefined;
|
|
554
|
+
if (inferredStopReason) {
|
|
555
|
+
output.stopReason = inferredStopReason;
|
|
556
|
+
}
|
|
533
557
|
if (!hasFinishReason && !compat.supportsFinishReason) {
|
|
534
558
|
output.stopReason = output.content.some((block) => block.type === "toolCall") ? "toolUse" : "stop";
|
|
535
559
|
}
|
|
536
560
|
if (output.stopReason === "error") {
|
|
537
561
|
throw new Error(output.errorMessage || "Provider returned an error stop reason");
|
|
538
562
|
}
|
|
539
|
-
if ((compat.supportsFinishReason && !hasFinishReason
|
|
563
|
+
if ((compat.supportsFinishReason && !hasFinishReason && !inferredStopReason) ||
|
|
564
|
+
output.stopReason === "pending") {
|
|
540
565
|
throw new Error("Stream ended without finish_reason");
|
|
541
566
|
}
|
|
542
567
|
const emptyResponse = githubCopilotEmptyResponse(output);
|
|
543
568
|
if (emptyResponse)
|
|
544
569
|
throw emptyResponse;
|
|
570
|
+
if (inferredStopReason) {
|
|
571
|
+
recordGitHubCopilotInferredStopReason(output);
|
|
572
|
+
}
|
|
545
573
|
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
546
574
|
stream.end();
|
|
547
575
|
}
|
|
@@ -551,7 +579,7 @@ export const stream = (model, context, options) => {
|
|
|
551
579
|
applyStreamedReasoningDetails(block);
|
|
552
580
|
}
|
|
553
581
|
}
|
|
554
|
-
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
582
|
+
output.stopReason = options?.signal?.aborted || error instanceof RetryDeadlineError ? "aborted" : "error";
|
|
555
583
|
output.errorMessage = formatProviderError(normalizeProviderError(error));
|
|
556
584
|
appendProviderResponseFailureDiagnostic(output, error);
|
|
557
585
|
output.retryAfterMs = error instanceof Error ? getProviderRetryAfterMs(error) : undefined;
|