@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.
Files changed (73) hide show
  1. package/CHANGELOG.md +17 -2
  2. package/dist/api/anthropic-messages.d.ts.map +1 -1
  3. package/dist/api/anthropic-messages.js +92 -16
  4. package/dist/api/anthropic-messages.js.map +1 -1
  5. package/dist/api/azure-openai-responses.d.ts.map +1 -1
  6. package/dist/api/azure-openai-responses.js +3 -1
  7. package/dist/api/azure-openai-responses.js.map +1 -1
  8. package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
  9. package/dist/api/bedrock-converse-stream.js +60 -9
  10. package/dist/api/bedrock-converse-stream.js.map +1 -1
  11. package/dist/api/google-generative-ai.d.ts.map +1 -1
  12. package/dist/api/google-generative-ai.js +3 -1
  13. package/dist/api/google-generative-ai.js.map +1 -1
  14. package/dist/api/google-vertex.d.ts.map +1 -1
  15. package/dist/api/google-vertex.js +3 -1
  16. package/dist/api/google-vertex.js.map +1 -1
  17. package/dist/api/mistral-conversations.d.ts.map +1 -1
  18. package/dist/api/mistral-conversations.js +3 -1
  19. package/dist/api/mistral-conversations.js.map +1 -1
  20. package/dist/api/openai-codex-responses.d.ts.map +1 -1
  21. package/dist/api/openai-codex-responses.js +41 -8
  22. package/dist/api/openai-codex-responses.js.map +1 -1
  23. package/dist/api/openai-completions.d.ts.map +1 -1
  24. package/dist/api/openai-completions.js +78 -14
  25. package/dist/api/openai-completions.js.map +1 -1
  26. package/dist/api/openai-responses.d.ts.map +1 -1
  27. package/dist/api/openai-responses.js +3 -1
  28. package/dist/api/openai-responses.js.map +1 -1
  29. package/dist/api/pi-messages.d.ts.map +1 -1
  30. package/dist/api/pi-messages.js +3 -1
  31. package/dist/api/pi-messages.js.map +1 -1
  32. package/dist/api/simple-options.d.ts +12 -0
  33. package/dist/api/simple-options.d.ts.map +1 -1
  34. package/dist/api/simple-options.js +52 -3
  35. package/dist/api/simple-options.js.map +1 -1
  36. package/dist/api/strict-tool-limits.d.ts +15 -6
  37. package/dist/api/strict-tool-limits.d.ts.map +1 -1
  38. package/dist/api/strict-tool-limits.js +21 -7
  39. package/dist/api/strict-tool-limits.js.map +1 -1
  40. package/dist/index.d.ts +2 -1
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +2 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/providers/data/.manifest.json +1 -1
  45. package/dist/providers/data/openrouter.json +1 -1
  46. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  47. package/dist/types.d.ts +6 -0
  48. package/dist/types.d.ts.map +1 -1
  49. package/dist/types.js.map +1 -1
  50. package/dist/utils/estimate.d.ts +1 -0
  51. package/dist/utils/estimate.d.ts.map +1 -1
  52. package/dist/utils/estimate.js +1 -1
  53. package/dist/utils/estimate.js.map +1 -1
  54. package/dist/utils/provider-retry.d.ts.map +1 -1
  55. package/dist/utils/provider-retry.js +4 -0
  56. package/dist/utils/provider-retry.js.map +1 -1
  57. package/dist/utils/retry.d.ts +2 -1
  58. package/dist/utils/retry.d.ts.map +1 -1
  59. package/dist/utils/retry.js +6 -1
  60. package/dist/utils/retry.js.map +1 -1
  61. package/dist/utils/tls-certificate.d.ts +75 -0
  62. package/dist/utils/tls-certificate.d.ts.map +1 -0
  63. package/dist/utils/tls-certificate.js +343 -0
  64. package/dist/utils/tls-certificate.js.map +1 -0
  65. package/dist/utils/transcript.d.ts +8 -1
  66. package/dist/utils/transcript.d.ts.map +1 -1
  67. package/dist/utils/transcript.js +82 -7
  68. package/dist/utils/transcript.js.map +1 -1
  69. package/dist/utils/version.d.ts +1 -1
  70. package/dist/utils/version.d.ts.map +1 -1
  71. package/dist/utils/version.js +1 -1
  72. package/dist/utils/version.js.map +1 -1
  73. 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
- addCacheControlToLastConversationMessage(messages, cacheControl);
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 && checkpoint !== messages.at(-1))
989
- addCacheControlToMessage(checkpoint, cacheControl);
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);