@bastani/atomic-ai 0.10.2 → 0.10.3-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 +17 -2
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +92 -16
- 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 +3 -1
- 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 +60 -9
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +3 -1
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +3 -1
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/api/mistral-conversations.js +3 -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 +41 -8
- 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 +78 -14
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +3 -1
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/pi-messages.d.ts.map +1 -1
- package/dist/api/pi-messages.js +3 -1
- package/dist/api/pi-messages.js.map +1 -1
- package/dist/api/simple-options.d.ts +12 -0
- package/dist/api/simple-options.d.ts.map +1 -1
- package/dist/api/simple-options.js +52 -3
- package/dist/api/simple-options.js.map +1 -1
- package/dist/api/strict-tool-limits.d.ts +15 -6
- package/dist/api/strict-tool-limits.d.ts.map +1 -1
- package/dist/api/strict-tool-limits.js +21 -7
- package/dist/api/strict-tool-limits.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/estimate.d.ts +1 -0
- package/dist/utils/estimate.d.ts.map +1 -1
- package/dist/utils/estimate.js +1 -1
- package/dist/utils/estimate.js.map +1 -1
- package/dist/utils/provider-retry.d.ts.map +1 -1
- package/dist/utils/provider-retry.js +4 -0
- package/dist/utils/provider-retry.js.map +1 -1
- package/dist/utils/retry.d.ts +2 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +6 -1
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/tls-certificate.d.ts +75 -0
- package/dist/utils/tls-certificate.d.ts.map +1 -0
- package/dist/utils/tls-certificate.js +343 -0
- package/dist/utils/tls-certificate.js.map +1 -0
- package/dist/utils/transcript.d.ts +8 -1
- package/dist/utils/transcript.d.ts.map +1 -1
- package/dist/utils/transcript.js +82 -7
- package/dist/utils/transcript.js.map +1 -1
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.d.ts.map +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +3 -3
|
@@ -3,6 +3,7 @@ import { githubCopilotEmptyResponse, recordGitHubCopilotFailure } from "../provi
|
|
|
3
3
|
import { inferGitHubCopilotStopReason, recordGitHubCopilotInferredStopReason, } from "../providers/github-copilot-stop-reason.js";
|
|
4
4
|
import { GitHubCopilotStreamAttempt } from "../providers/github-copilot-stream-attempt.js";
|
|
5
5
|
import { appendProviderResponseFailureDiagnostic, formatProviderError, normalizeProviderError, } from "../utils/error-body.js";
|
|
6
|
+
import { recordTlsCertificateRefusal, transportFetch } from "../utils/tls-certificate.js";
|
|
6
7
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
7
8
|
import { shortHash } from "../utils/hash.js";
|
|
8
9
|
import { headersToRecord } from "../utils/headers.js";
|
|
@@ -583,6 +584,7 @@ export const stream = (model, context, options) => {
|
|
|
583
584
|
output.stopReason = options?.signal?.aborted || error instanceof RetryDeadlineError ? "aborted" : "error";
|
|
584
585
|
output.errorMessage = formatProviderError(normalizeProviderError(error));
|
|
585
586
|
appendProviderResponseFailureDiagnostic(output, error);
|
|
587
|
+
recordTlsCertificateRefusal(output, error);
|
|
586
588
|
output.retryAfterMs = error instanceof Error ? getProviderRetryAfterMs(error) : undefined;
|
|
587
589
|
// Some providers via OpenRouter give additional information in this field.
|
|
588
590
|
// normalizeProviderError already stringifies the parsed body (error.error)
|
|
@@ -660,7 +662,7 @@ function createClient(model, context, apiKey, optionsHeaders, identity, fetch, s
|
|
|
660
662
|
apiKey,
|
|
661
663
|
baseURL: model.baseUrl,
|
|
662
664
|
dangerouslyAllowBrowser: true,
|
|
663
|
-
fetch: getFetchAdmission(fetch)?.fetch ?? fetch,
|
|
665
|
+
fetch: transportFetch(getFetchAdmission(fetch)?.fetch ?? fetch),
|
|
664
666
|
// Affinity defaults yield to explicit model and request headers, including null suppression.
|
|
665
667
|
defaultHeaders: { ...sessionAffinityHeaders, ...headers },
|
|
666
668
|
}), fetch);
|
|
@@ -982,11 +984,74 @@ function getCompatCacheControl(compat, cacheRetention) {
|
|
|
982
984
|
function applyAnthropicCacheControl(messages, tools, cacheControl) {
|
|
983
985
|
addCacheControlToSystemPrompt(messages, cacheControl);
|
|
984
986
|
addCacheControlToLastTool(tools, cacheControl);
|
|
985
|
-
|
|
987
|
+
// Where the request the last reply answered put its newest breakpoint, by the same rule.
|
|
988
|
+
const replyAt = replyPositions.get(messages);
|
|
989
|
+
const previous = replyAt === undefined ? undefined : lastCacheablePart(messages, replyAt);
|
|
990
|
+
const newest = lastCacheablePart(messages, messages.length);
|
|
991
|
+
if (newest)
|
|
992
|
+
addCacheControlToMessage(messages[newest.index], cacheControl);
|
|
986
993
|
// The fourth and last breakpoint Claude allows.
|
|
987
994
|
const checkpoint = cacheCheckpoints.get(messages);
|
|
988
|
-
if (checkpoint
|
|
989
|
-
|
|
995
|
+
if (checkpoint) {
|
|
996
|
+
if (checkpoint !== messages.at(-1))
|
|
997
|
+
addCacheControlToMessage(checkpoint, cacheControl);
|
|
998
|
+
}
|
|
999
|
+
else if (previous && newest && positionsBetween(messages, previous, newest) >= CACHE_LOOKBACK_POSITIONS) {
|
|
1000
|
+
// One turn added so many blocks that the newest breakpoint no longer reaches back to the previous request's.
|
|
1001
|
+
addCacheControlToMessage(messages[previous.index], cacheControl);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Claude finds an earlier request's cached prefix from a breakpoint only within this many positions, the breakpoint
|
|
1006
|
+
* included. Every content part, tool call and reasoning field counts as one position here, which is never fewer than
|
|
1007
|
+
* Claude counts for the blocks they become.
|
|
1008
|
+
*/
|
|
1009
|
+
const CACHE_LOOKBACK_POSITIONS = 20;
|
|
1010
|
+
/**
|
|
1011
|
+
* The last user, assistant or tool message before index `end` that can take a breakpoint, as the newest breakpoint is
|
|
1012
|
+
* placed: on its last text, or on the whole of a non-empty string.
|
|
1013
|
+
*/
|
|
1014
|
+
function lastCacheablePart(messages, end) {
|
|
1015
|
+
for (let index = end - 1; index >= 0; index--) {
|
|
1016
|
+
const message = messages[index];
|
|
1017
|
+
if (message.role !== "user" && message.role !== "assistant" && message.role !== "tool")
|
|
1018
|
+
continue;
|
|
1019
|
+
const content = message.content;
|
|
1020
|
+
const part = typeof content === "string"
|
|
1021
|
+
? content.length > 0
|
|
1022
|
+
? 0
|
|
1023
|
+
: -1
|
|
1024
|
+
: Array.isArray(content)
|
|
1025
|
+
? content.findLastIndex((item) => item?.type === "text")
|
|
1026
|
+
: -1;
|
|
1027
|
+
if (part === -1)
|
|
1028
|
+
continue;
|
|
1029
|
+
// An assistant message's reasoning goes before its text.
|
|
1030
|
+
return { index, offset: part + (message.role === "assistant" && hasReasoning(message) ? 1 : 0) };
|
|
1031
|
+
}
|
|
1032
|
+
return undefined;
|
|
1033
|
+
}
|
|
1034
|
+
/** Positions after the part `from` marks through the part `to` marks, counted until they reach the lookback. */
|
|
1035
|
+
function positionsBetween(messages, from, to) {
|
|
1036
|
+
if (from.index === to.index)
|
|
1037
|
+
return to.offset - from.offset;
|
|
1038
|
+
let positions = messagePositions(messages[from.index]) - from.offset;
|
|
1039
|
+
for (let index = from.index + 1; index < to.index && positions < CACHE_LOOKBACK_POSITIONS; index++) {
|
|
1040
|
+
positions += messagePositions(messages[index]);
|
|
1041
|
+
}
|
|
1042
|
+
return positions + to.offset;
|
|
1043
|
+
}
|
|
1044
|
+
function messagePositions(message) {
|
|
1045
|
+
const content = message.content;
|
|
1046
|
+
const parts = typeof content === "string" ? 1 : Array.isArray(content) ? content.length : 0;
|
|
1047
|
+
if (message.role !== "assistant")
|
|
1048
|
+
return Math.max(1, parts);
|
|
1049
|
+
return Math.max(1, parts + (message.tool_calls?.length ?? 0) + (hasReasoning(message) ? 1 : 0));
|
|
1050
|
+
}
|
|
1051
|
+
function hasReasoning(message) {
|
|
1052
|
+
return (message.reasoning_details !== undefined ||
|
|
1053
|
+
message.reasoning_opaque !== undefined ||
|
|
1054
|
+
OPENAI_COMPLETIONS_REASONING_FIELDS.some((field) => message[field] !== undefined));
|
|
990
1055
|
}
|
|
991
1056
|
function addCacheControlToSystemPrompt(messages, cacheControl) {
|
|
992
1057
|
for (const message of messages) {
|
|
@@ -996,16 +1061,6 @@ function addCacheControlToSystemPrompt(messages, cacheControl) {
|
|
|
996
1061
|
}
|
|
997
1062
|
}
|
|
998
1063
|
}
|
|
999
|
-
function addCacheControlToLastConversationMessage(messages, cacheControl) {
|
|
1000
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1001
|
-
const message = messages[i];
|
|
1002
|
-
if (message.role === "user" || message.role === "assistant" || message.role === "tool") {
|
|
1003
|
-
if (addCacheControlToMessage(message, cacheControl)) {
|
|
1004
|
-
return;
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
1064
|
function addCacheControlToLastTool(tools, cacheControl) {
|
|
1010
1065
|
if (!tools || tools.length === 0) {
|
|
1011
1066
|
return;
|
|
@@ -1087,6 +1142,7 @@ export function convertMessages(model, context, compat, options) {
|
|
|
1087
1142
|
let systemIndex = 0;
|
|
1088
1143
|
// The input sent up to a message with a `cacheBreakpoint` hint ends with the last user or tool message so far.
|
|
1089
1144
|
let checkpoint;
|
|
1145
|
+
let replyAt;
|
|
1090
1146
|
const markCheckpoint = () => {
|
|
1091
1147
|
const last = params.at(-1);
|
|
1092
1148
|
if (last?.role === "user" || last?.role === "tool")
|
|
@@ -1265,6 +1321,7 @@ export function convertMessages(model, context, compat, options) {
|
|
|
1265
1321
|
if (!hasContent && !assistantMsg.tool_calls) {
|
|
1266
1322
|
continue;
|
|
1267
1323
|
}
|
|
1324
|
+
replyAt = params.length;
|
|
1268
1325
|
params.push(assistantMsg);
|
|
1269
1326
|
}
|
|
1270
1327
|
else if (msg.role === "toolResult") {
|
|
@@ -1335,10 +1392,17 @@ export function convertMessages(model, context, compat, options) {
|
|
|
1335
1392
|
}
|
|
1336
1393
|
if (checkpoint)
|
|
1337
1394
|
cacheCheckpoints.set(params, checkpoint);
|
|
1395
|
+
if (replyAt !== undefined)
|
|
1396
|
+
replyPositions.set(params, replyAt);
|
|
1338
1397
|
return params;
|
|
1339
1398
|
}
|
|
1340
1399
|
/** The message a `cacheBreakpoint` hint asked a breakpoint for, by the message list `convertMessages` returned. */
|
|
1341
1400
|
const cacheCheckpoints = new WeakMap();
|
|
1401
|
+
/**
|
|
1402
|
+
* The index of the last reply in the message list `convertMessages` returned: the request it answered sent the
|
|
1403
|
+
* messages before it.
|
|
1404
|
+
*/
|
|
1405
|
+
const replyPositions = new WeakMap();
|
|
1342
1406
|
function convertTools(tools, compat) {
|
|
1343
1407
|
return tools.map((tool) => {
|
|
1344
1408
|
const grammar = resolveGrammarConstrainedSampling(tool, compat.supportsOpenAIGrammarTools);
|