@bastani/pi-ai 0.9.63 → 0.9.64-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 CHANGED
@@ -4,6 +4,26 @@ This package is a Bastani fork of `@earendil-works/pi-ai`. Upstream history at t
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.9.64] - 2026-11-02
8
+
9
+ ### Added
10
+
11
+ - `api/anthropic-assistant-content` exports `anthropicAssistantReplayContent(message, model)`, which returns the content blocks an Anthropic Messages request for `model` carries for one transcript turn, or none when the request omits the turn, using the same transformation and conversion as the request. Callers that repair provider payloads can match transcript turns to payload messages without copying the converter's rules ([#4185](https://github.com/bastani-inc/atomic/issues/4185))
12
+
13
+ ### Fixed
14
+
15
+ - The Anthropic Messages converter sends signed `thinking` text back exactly as Anthropic returned it instead of stripping unpaired surrogates from it, which changed the latest assistant message and failed the request with `thinking or redacted_thinking blocks in the latest assistant message cannot be modified` ([#4185](https://github.com/bastani-inc/atomic/issues/4185))
16
+
17
+ ## [0.9.64-alpha.1] - 2026-11-02
18
+
19
+ ### Added
20
+
21
+ - `api/anthropic-assistant-content` exports `anthropicAssistantReplayContent(message, model)`, which returns the content blocks an Anthropic Messages request for `model` carries for one transcript turn, or none when the request omits the turn, using the same transformation and conversion as the request. Callers that repair provider payloads can match transcript turns to payload messages without copying the converter's rules ([#4185](https://github.com/bastani-inc/atomic/issues/4185))
22
+
23
+ ### Fixed
24
+
25
+ - The Anthropic Messages converter sends signed `thinking` text back exactly as Anthropic returned it instead of stripping unpaired surrogates from it, which changed the latest assistant message and failed the request with `thinking or redacted_thinking blocks in the latest assistant message cannot be modified` ([#4185](https://github.com/bastani-inc/atomic/issues/4185))
26
+
7
27
  ## [0.9.59] - 2026-11-01
8
28
 
9
29
  ### Fixed
@@ -0,0 +1,24 @@
1
+ import type { BetaContentBlockParam as ContentBlockParam } from "@anthropic-ai/sdk/resources/beta/messages/messages.js";
2
+ import type { Api, AssistantMessage, Model } from "../types.ts";
3
+ export interface AnthropicAssistantContentOptions {
4
+ /** Send unsigned thinking as `thinking` with an empty signature, as some compatible providers expect. */
5
+ allowEmptySignature: boolean;
6
+ /** The tool name sent on the wire, for example the Claude Code name under OAuth. */
7
+ toolName?: (name: string) => string;
8
+ }
9
+ /**
10
+ * Convert an assistant turn already prepared by `transformMessages` into the content blocks an
11
+ * Anthropic Messages request carries for it. An empty result means the request omits the turn.
12
+ */
13
+ export declare function convertAnthropicAssistantContent(message: AssistantMessage, options: AnthropicAssistantContentOptions): ContentBlockParam[];
14
+ /**
15
+ * The content blocks that an Anthropic Messages request for `model` carries for one transcript
16
+ * turn, produced by the same `transformMessages` pass and block conversion as the request itself.
17
+ *
18
+ * Empty when the request omits the turn: an errored or aborted turn, or one with nothing left to
19
+ * send, such as a turn that held only another model's reasoning. A turn that keeps a server-side
20
+ * `fallback` marker always has content, because the marker is replayed even when every other block
21
+ * is dropped. Tool call IDs and names are the transcript's, not their normalized or OAuth wire forms.
22
+ */
23
+ export declare function anthropicAssistantReplayContent(message: AssistantMessage, model: Model<Api>): ContentBlockParam[];
24
+ //# sourceMappingURL=anthropic-assistant-content.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic-assistant-content.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-assistant-content.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,IAAI,iBAAiB,EAAE,MAAM,uDAAuD,CAAC;AACxH,OAAO,KAAK,EAA2B,GAAG,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAIzF,MAAM,WAAW,gCAAgC;IAChD,yGAAyG;IACzG,mBAAmB,EAAE,OAAO,CAAC;IAC7B,oFAAoF;IACpF,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CACpC;AAED;;;GAGG;AACH,wBAAgB,gCAAgC,CAC/C,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,gCAAgC,GACvC,iBAAiB,EAAE,CAyErB;AAED;;;;;;;;GAQG;AACH,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,iBAAiB,EAAE,CAQjH"}
@@ -0,0 +1,99 @@
1
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
2
+ import { transformMessages } from "./transform-messages.js";
3
+ /**
4
+ * Convert an assistant turn already prepared by `transformMessages` into the content blocks an
5
+ * Anthropic Messages request carries for it. An empty result means the request omits the turn.
6
+ */
7
+ export function convertAnthropicAssistantContent(message, options) {
8
+ const blocks = [];
9
+ for (const block of message.content) {
10
+ if (block.type === "text") {
11
+ if (block.text.trim().length === 0)
12
+ continue;
13
+ blocks.push({
14
+ type: "text",
15
+ text: sanitizeSurrogates(block.text),
16
+ });
17
+ }
18
+ else if (block.type === "thinking") {
19
+ // Redacted thinking: pass the opaque payload back as redacted_thinking
20
+ if (block.redacted) {
21
+ blocks.push({
22
+ type: "redacted_thinking",
23
+ data: block.thinkingSignature,
24
+ });
25
+ continue;
26
+ }
27
+ const thinkingSignature = block.thinkingSignature;
28
+ const hasThinkingSignature = !!thinkingSignature && thinkingSignature.trim().length > 0;
29
+ if (block.thinking.trim().length === 0 && !hasThinkingSignature)
30
+ continue;
31
+ // If thinking signature is missing/empty (e.g., from aborted stream),
32
+ // convert to plain text for Anthropic. Some compatible providers emit
33
+ // and accept empty signatures, so let marked models preserve the block.
34
+ if (!hasThinkingSignature) {
35
+ blocks.push(options.allowEmptySignature
36
+ ? {
37
+ type: "thinking",
38
+ thinking: sanitizeSurrogates(block.thinking),
39
+ signature: "",
40
+ }
41
+ : {
42
+ type: "text",
43
+ text: sanitizeSurrogates(block.thinking),
44
+ });
45
+ }
46
+ else {
47
+ // The signature covers these exact bytes, and Anthropic rejects a latest assistant
48
+ // message whose signed thinking differs from what it returned, so no sanitizing here.
49
+ blocks.push({
50
+ type: "thinking",
51
+ thinking: block.thinking,
52
+ signature: thinkingSignature,
53
+ });
54
+ }
55
+ }
56
+ else if (block.type === "toolCall") {
57
+ blocks.push({
58
+ type: "tool_use",
59
+ id: block.id,
60
+ name: options.toolName ? options.toolName(block.name) : block.name,
61
+ input: block.arguments ?? {},
62
+ });
63
+ }
64
+ else if (block.type === "fallback") {
65
+ // The server-side fallback boundary must go back on the wire at the position it
66
+ // arrived: "Keep it exactly where it appeared. The API uses its position to
67
+ // validate the thinking blocks around it, so a request that echoes thinking
68
+ // blocks from both sides of the boundary is rejected if the block is omitted or
69
+ // moved." `transformMessages` drops the pre-boundary thinking; without this
70
+ // branch the marker would be dropped too, which is the failure that rule names.
71
+ // https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback
72
+ //
73
+ // The SDK types lag this block, matching the cast on the stream side.
74
+ blocks.push({
75
+ type: "fallback",
76
+ from: { model: block.fromModel },
77
+ to: { model: block.toModel },
78
+ });
79
+ }
80
+ }
81
+ return blocks;
82
+ }
83
+ /**
84
+ * The content blocks that an Anthropic Messages request for `model` carries for one transcript
85
+ * turn, produced by the same `transformMessages` pass and block conversion as the request itself.
86
+ *
87
+ * Empty when the request omits the turn: an errored or aborted turn, or one with nothing left to
88
+ * send, such as a turn that held only another model's reasoning. A turn that keeps a server-side
89
+ * `fallback` marker always has content, because the marker is replayed even when every other block
90
+ * is dropped. Tool call IDs and names are the transcript's, not their normalized or OAuth wire forms.
91
+ */
92
+ export function anthropicAssistantReplayContent(message, model) {
93
+ const transformed = transformMessages([message], model).find((candidate) => candidate.role === "assistant");
94
+ if (!transformed)
95
+ return [];
96
+ const compat = model.api === "anthropic-messages" ? model.compat : undefined;
97
+ return convertAnthropicAssistantContent(transformed, { allowEmptySignature: compat?.allowEmptySignature === true });
98
+ }
99
+ //# sourceMappingURL=anthropic-assistant-content.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic-assistant-content.js","sourceRoot":"","sources":["../../src/api/anthropic-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ACpC,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,+BAA+B,CAAC,OAAyB,EAAE,KAAiB;IAC3F,MAAM,WAAW,GAAG,iBAAiB,CAAC,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,CAAC,IAAI,CAC3D,CAAC,SAAS,EAAiC,EAAE,CAAC,SAAS,CAAC,IAAI,KAAK,WAAW,CAC5E,CAAC;IACF,IAAI,CAAC,WAAW;QAAE,OAAO,EAAE,CAAC;IAC5B,MAAM,MAAM,GACX,KAAK,CAAC,GAAG,KAAK,oBAAoB,CAAC,CAAC,CAAE,KAAK,CAAC,MAA8C,CAAC,CAAC,CAAC,SAAS,CAAC;IACxG,OAAO,gCAAgC,CAAC,WAAW,EAAE,EAAE,mBAAmB,EAAE,MAAM,EAAE,mBAAmB,KAAK,IAAI,EAAE,CAAC,CAAC;AACrH,CAAC","sourcesContent":["import type { BetaContentBlockParam as ContentBlockParam } from \"@anthropic-ai/sdk/resources/beta/messages/messages.js\";\nimport type { AnthropicMessagesCompat, Api, AssistantMessage, Model } from \"../types.ts\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.ts\";\nimport { transformMessages } from \"./transform-messages.ts\";\n\nexport interface AnthropicAssistantContentOptions {\n\t/** Send unsigned thinking as `thinking` with an empty signature, as some compatible providers expect. */\n\tallowEmptySignature: boolean;\n\t/** The tool name sent on the wire, for example the Claude Code name under OAuth. */\n\ttoolName?: (name: string) => string;\n}\n\n/**\n * Convert an assistant turn already prepared by `transformMessages` into the content blocks an\n * Anthropic Messages request carries for it. An empty result means the request omits the turn.\n */\nexport function convertAnthropicAssistantContent(\n\tmessage: AssistantMessage,\n\toptions: AnthropicAssistantContentOptions,\n): ContentBlockParam[] {\n\tconst blocks: ContentBlockParam[] = [];\n\n\tfor (const block of message.content) {\n\t\tif (block.type === \"text\") {\n\t\t\tif (block.text.trim().length === 0) continue;\n\t\t\tblocks.push({\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: sanitizeSurrogates(block.text),\n\t\t\t});\n\t\t} else if (block.type === \"thinking\") {\n\t\t\t// Redacted thinking: pass the opaque payload back as redacted_thinking\n\t\t\tif (block.redacted) {\n\t\t\t\tblocks.push({\n\t\t\t\t\ttype: \"redacted_thinking\",\n\t\t\t\t\tdata: block.thinkingSignature!,\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst thinkingSignature = block.thinkingSignature;\n\t\t\tconst hasThinkingSignature = !!thinkingSignature && thinkingSignature.trim().length > 0;\n\t\t\tif (block.thinking.trim().length === 0 && !hasThinkingSignature) continue;\n\t\t\t// If thinking signature is missing/empty (e.g., from aborted stream),\n\t\t\t// convert to plain text for Anthropic. Some compatible providers emit\n\t\t\t// and accept empty signatures, so let marked models preserve the block.\n\t\t\tif (!hasThinkingSignature) {\n\t\t\t\tblocks.push(\n\t\t\t\t\toptions.allowEmptySignature\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\t\t\tthinking: sanitizeSurrogates(block.thinking),\n\t\t\t\t\t\t\t\tsignature: \"\",\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: sanitizeSurrogates(block.thinking),\n\t\t\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// The signature covers these exact bytes, and Anthropic rejects a latest assistant\n\t\t\t\t// message whose signed thinking differs from what it returned, so no sanitizing here.\n\t\t\t\tblocks.push({\n\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\tthinking: block.thinking,\n\t\t\t\t\tsignature: thinkingSignature,\n\t\t\t\t});\n\t\t\t}\n\t\t} else if (block.type === \"toolCall\") {\n\t\t\tblocks.push({\n\t\t\t\ttype: \"tool_use\",\n\t\t\t\tid: block.id,\n\t\t\t\tname: options.toolName ? options.toolName(block.name) : block.name,\n\t\t\t\tinput: block.arguments ?? {},\n\t\t\t});\n\t\t} else if (block.type === \"fallback\") {\n\t\t\t// The server-side fallback boundary must go back on the wire at the position it\n\t\t\t// arrived: \"Keep it exactly where it appeared. The API uses its position to\n\t\t\t// validate the thinking blocks around it, so a request that echoes thinking\n\t\t\t// blocks from both sides of the boundary is rejected if the block is omitted or\n\t\t\t// moved.\" `transformMessages` drops the pre-boundary thinking; without this\n\t\t\t// branch the marker would be dropped too, which is the failure that rule names.\n\t\t\t// https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback\n\t\t\t//\n\t\t\t// The SDK types lag this block, matching the cast on the stream side.\n\t\t\tblocks.push({\n\t\t\t\ttype: \"fallback\",\n\t\t\t\tfrom: { model: block.fromModel },\n\t\t\t\tto: { model: block.toModel },\n\t\t\t} as unknown as ContentBlockParam);\n\t\t}\n\t}\n\n\treturn blocks;\n}\n\n/**\n * The content blocks that an Anthropic Messages request for `model` carries for one transcript\n * turn, produced by the same `transformMessages` pass and block conversion as the request itself.\n *\n * Empty when the request omits the turn: an errored or aborted turn, or one with nothing left to\n * send, such as a turn that held only another model's reasoning. A turn that keeps a server-side\n * `fallback` marker always has content, because the marker is replayed even when every other block\n * is dropped. Tool call IDs and names are the transcript's, not their normalized or OAuth wire forms.\n */\nexport function anthropicAssistantReplayContent(message: AssistantMessage, model: Model<Api>): ContentBlockParam[] {\n\tconst transformed = transformMessages([message], model).find(\n\t\t(candidate): candidate is AssistantMessage => candidate.role === \"assistant\",\n\t);\n\tif (!transformed) return [];\n\tconst compat =\n\t\tmodel.api === \"anthropic-messages\" ? (model.compat as AnthropicMessagesCompat | undefined) : undefined;\n\treturn convertAnthropicAssistantContent(transformed, { allowEmptySignature: compat?.allowEmptySignature === true });\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,SAAkD,MAAM,mBAAmB,C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AE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAqPhE,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACtD;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB;;;;;;;;;;;OAWG;IACH,eAAe,CAAC,EAAE,wBAAwB,CAAC;IAC3C;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACtE;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB;AAsTD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CA6YzE,CAAC;AA4BF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAgDlF,CAAC"}
@@ -18,6 +18,7 @@ import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadli
18
18
  import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
19
19
  import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
20
20
  import { getCurrentTools, getInitialSystemMessage, resolveTranscript, } from "../utils/transcript.js";
21
+ import { convertAnthropicAssistantContent } from "./anthropic-assistant-content.js";
21
22
  import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, isStrictToolLimitErrorMessage, prefersJsonSchemaStrictSampling, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
22
23
  import { buildCopilotDynamicHeaders, hasCopilotVisionInput, preserveCopilotIntegrationHeader, } from "./github-copilot-headers.js";
23
24
  import { adjustMaxTokensForThinking, buildBaseOptions, clampMaxTokensToContext } from "./simple-options.js";
@@ -1433,77 +1434,10 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1433
1434
  }
1434
1435
  }
1435
1436
  else if (msg.role === "assistant") {
1436
- const blocks = [];
1437
- for (const block of msg.content) {
1438
- if (block.type === "text") {
1439
- if (block.text.trim().length === 0)
1440
- continue;
1441
- blocks.push({
1442
- type: "text",
1443
- text: sanitizeSurrogates(block.text),
1444
- });
1445
- }
1446
- else if (block.type === "thinking") {
1447
- // Redacted thinking: pass the opaque payload back as redacted_thinking
1448
- if (block.redacted) {
1449
- blocks.push({
1450
- type: "redacted_thinking",
1451
- data: block.thinkingSignature,
1452
- });
1453
- continue;
1454
- }
1455
- const thinkingSignature = block.thinkingSignature;
1456
- const hasThinkingSignature = !!thinkingSignature && thinkingSignature.trim().length > 0;
1457
- if (block.thinking.trim().length === 0 && !hasThinkingSignature)
1458
- continue;
1459
- // If thinking signature is missing/empty (e.g., from aborted stream),
1460
- // convert to plain text for Anthropic. Some compatible providers emit
1461
- // and accept empty signatures, so let marked models preserve the block.
1462
- if (!hasThinkingSignature) {
1463
- blocks.push(allowEmptySignature
1464
- ? {
1465
- type: "thinking",
1466
- thinking: sanitizeSurrogates(block.thinking),
1467
- signature: "",
1468
- }
1469
- : {
1470
- type: "text",
1471
- text: sanitizeSurrogates(block.thinking),
1472
- });
1473
- }
1474
- else {
1475
- blocks.push({
1476
- type: "thinking",
1477
- thinking: sanitizeSurrogates(block.thinking),
1478
- signature: thinkingSignature,
1479
- });
1480
- }
1481
- }
1482
- else if (block.type === "toolCall") {
1483
- blocks.push({
1484
- type: "tool_use",
1485
- id: block.id,
1486
- name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
1487
- input: block.arguments ?? {},
1488
- });
1489
- }
1490
- else if (block.type === "fallback") {
1491
- // The server-side fallback boundary must go back on the wire at the position it
1492
- // arrived: "Keep it exactly where it appeared. The API uses its position to
1493
- // validate the thinking blocks around it, so a request that echoes thinking
1494
- // blocks from both sides of the boundary is rejected if the block is omitted or
1495
- // moved." `transformMessages` drops the pre-boundary thinking; without this
1496
- // branch the marker would be dropped too, which is the failure that rule names.
1497
- // https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback
1498
- //
1499
- // The SDK types lag this block, matching the cast on the stream side.
1500
- blocks.push({
1501
- type: "fallback",
1502
- from: { model: block.fromModel },
1503
- to: { model: block.toModel },
1504
- });
1505
- }
1506
- }
1437
+ const blocks = convertAnthropicAssistantContent(msg, {
1438
+ allowEmptySignature,
1439
+ toolName: isOAuthToken ? toClaudeCodeName : undefined,
1440
+ });
1507
1441
  if (blocks.length === 0)
1508
1442
  continue;
1509
1443
  flushPendingSystemMessages();