@openllmsh/sdk 2.10.18-alpha.4 → 2.10.18-alpha.5

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.
@@ -38,6 +38,10 @@ export type TReasoningResponsesInput = {
38
38
  readonly summary: ReadonlyArray<TReasoningSummaryPart>;
39
39
  readonly encrypted_content?: string;
40
40
  };
41
+ /** True for a thinking `signature` that OpenLLM minted (a Responses
42
+ * reasoning envelope or a native-assistant carrier) rather than Anthropic.
43
+ * Anthropic can never verify one, so a replay to Anthropic must drop it. */
44
+ export declare const isOpenllmEnvelopeSignature: (signature: unknown) => boolean;
41
45
  export declare const CLAUDE_NATIVE_ASSISTANT_CARRIER = "claude_native_assistant";
42
46
  export declare const CLAUDE_NATIVE_ASSISTANT_PENDING = "claude_native_assistant_pending";
43
47
  /** Native assistant history is NOT Responses ciphertext. Keep its wire
@@ -29,6 +29,12 @@
29
29
  */
30
30
  const SIGNATURE_PREFIX = "openllm-rs1:";
31
31
  const NATIVE_ASSISTANT_SIGNATURE_PREFIX = "openllm-cn1:";
32
+ /** True for a thinking `signature` that OpenLLM minted (a Responses
33
+ * reasoning envelope or a native-assistant carrier) rather than Anthropic.
34
+ * Anthropic can never verify one, so a replay to Anthropic must drop it. */
35
+ export const isOpenllmEnvelopeSignature = (signature) => typeof signature === "string" &&
36
+ (signature.startsWith(SIGNATURE_PREFIX) ||
37
+ signature.startsWith(NATIVE_ASSISTANT_SIGNATURE_PREFIX));
32
38
  export const CLAUDE_NATIVE_ASSISTANT_CARRIER = "claude_native_assistant";
33
39
  export const CLAUDE_NATIVE_ASSISTANT_PENDING = "claude_native_assistant_pending";
34
40
  /** Native assistant history is NOT Responses ciphertext. Keep its wire
@@ -26,21 +26,26 @@
26
26
  * next user turn answers with `tool_result`. Blindly stripping that block
27
27
  * breaks the tool loop (`thinking`/`redacted_thinking` blocks in the latest
28
28
  * assistant message cannot be modified / must round-trip).
29
- * - **Limitation (documented, not papered over).** If the preserved final-turn
30
- * thinking itself carries a foreign-model signature, the same 400 will fire
31
- * again after strip (we deliberately did not touch it). Cross-model
32
- * mid-tool-loop continuation cannot be made valid: keeping the block 400s on
33
- * signature, stripping it 400s on tool-use integrity. The reactive retry then
34
- * surfaces the original error and the walker advances / fails normally. Same-
35
- * model tool_use continuation is unaffected (signature still verifies).
36
- * - **No proactive strip.** Some signature forms embed their minting model
37
- * (e.g. Anthropic CAIS), so origin is not always opaque — but the origin
38
- * model is not the deciding factor: only the TARGET model's acceptance is,
39
- * and that is knowable only from its response. Same-family signatures are
40
- * frequently accepted, so proactively stripping on every model hop would
41
- * bust prompt cache on healthy same-model multi-turn for no reason. The
42
- * reactive 400 → strip → retry path strips only when the upstream actually
43
- * rejects, which is the correctness backstop.
29
+ * - **Last resort: thinking off for this request.** If the preserved final-turn
30
+ * thinking itself carries a foreign-model signature (e.g. a Sonnet subagent
31
+ * fed an Opus-signed tool-use turn), keeping the block 400s on signature and
32
+ * stripping it alone 400s on tool-use integrity — but that integrity rule
33
+ * only applies while extended thinking is ON. {@link stripAllThinkingAndDisable}
34
+ * strips every thinking block AND removes the `thinking` param (plus the
35
+ * thinking-only `clear_thinking` context edits), which Anthropic accepts.
36
+ * Mirrors litellm's `strip_thinking_blocks_from_anthropic_messages_request_dict`.
37
+ * The model just doesn't think on that one turn; the next turn re-enables it.
38
+ * - **Proactive strip only for OpenLLM-minted signatures.** A thinking block
39
+ * whose signature is an OpenLLM envelope (`openllm-rs1:` / `openllm-cn1:`,
40
+ * from a Muse / Codex / Grok turn) can never verify at Anthropic, so
41
+ * {@link sanitizeAnthropicReplay} drops it before sending. Genuine Anthropic
42
+ * signatures are never proactively stripped: only the TARGET model's
43
+ * acceptance decides, same-family signatures are often accepted, and
44
+ * stripping them would bust prompt cache on healthy multi-turn. Those go
45
+ * through the reactive 400 → strip → retry path.
46
+ * - **No empty assistant turns.** An assistant message left with no content
47
+ * after a strip (a thinking-only turn) is dropped — Anthropic rejects empty
48
+ * content, and adjacent user turns are merged upstream.
44
49
  */
45
50
  export declare const isThinkingSignatureError: (rawBody: string) => boolean;
46
51
  /** True when a built Anthropic Messages body carries at least one assistant
@@ -54,3 +59,29 @@ export declare const messagesBodyHasStrippableThinking: (body: unknown) => boole
54
59
  * trailing tool_result) is left intact — see file header. Other content
55
60
  * blocks and non-assistant messages are untouched; order is preserved. */
56
61
  export declare const stripMessagesThinkingBlocks: (body: unknown) => unknown;
62
+ export type TAnthropicReplaySanitized = {
63
+ readonly body: unknown;
64
+ /** The active tool-use turn's thinking was OpenLLM-minted, so extended
65
+ * thinking is off for this request. */
66
+ readonly thinkingDisabled: boolean;
67
+ };
68
+ /**
69
+ * Proactive, certain half of the recovery: drop every thinking block whose
70
+ * signature OpenLLM minted (a Muse / Codex / Grok turn replayed into
71
+ * Anthropic) BEFORE sending — Anthropic can never verify those, so this
72
+ * costs no request and leaves Anthropic-signed history (and its cache)
73
+ * alone. When the active tool-use turn's thinking is foreign, thinking is
74
+ * turned off for this request (Anthropic requires that turn's thinking while
75
+ * it is on). A cross-model Anthropic rejection still goes through the
76
+ * reactive retry.
77
+ */
78
+ export declare const sanitizeAnthropicReplay: (body: unknown) => TAnthropicReplaySanitized;
79
+ /** True when a built Anthropic Messages body carries ANY assistant
80
+ * `thinking`/`redacted_thinking` block (the active turn included). */
81
+ export declare const messagesBodyHasAnyThinking: (body: unknown) => boolean;
82
+ /** Last-resort recovery: strip thinking from EVERY assistant message (the
83
+ * active tool-use turn included) and turn extended thinking off for this
84
+ * request — without `thinking` enabled Anthropic no longer requires the
85
+ * final tool-use turn to carry its thinking. `clear_thinking_*` context
86
+ * edits are dropped too since they require thinking. See file header. */
87
+ export declare const stripAllThinkingAndDisable: (body: unknown) => unknown;
@@ -26,22 +26,28 @@
26
26
  * next user turn answers with `tool_result`. Blindly stripping that block
27
27
  * breaks the tool loop (`thinking`/`redacted_thinking` blocks in the latest
28
28
  * assistant message cannot be modified / must round-trip).
29
- * - **Limitation (documented, not papered over).** If the preserved final-turn
30
- * thinking itself carries a foreign-model signature, the same 400 will fire
31
- * again after strip (we deliberately did not touch it). Cross-model
32
- * mid-tool-loop continuation cannot be made valid: keeping the block 400s on
33
- * signature, stripping it 400s on tool-use integrity. The reactive retry then
34
- * surfaces the original error and the walker advances / fails normally. Same-
35
- * model tool_use continuation is unaffected (signature still verifies).
36
- * - **No proactive strip.** Some signature forms embed their minting model
37
- * (e.g. Anthropic CAIS), so origin is not always opaque — but the origin
38
- * model is not the deciding factor: only the TARGET model's acceptance is,
39
- * and that is knowable only from its response. Same-family signatures are
40
- * frequently accepted, so proactively stripping on every model hop would
41
- * bust prompt cache on healthy same-model multi-turn for no reason. The
42
- * reactive 400 → strip → retry path strips only when the upstream actually
43
- * rejects, which is the correctness backstop.
29
+ * - **Last resort: thinking off for this request.** If the preserved final-turn
30
+ * thinking itself carries a foreign-model signature (e.g. a Sonnet subagent
31
+ * fed an Opus-signed tool-use turn), keeping the block 400s on signature and
32
+ * stripping it alone 400s on tool-use integrity — but that integrity rule
33
+ * only applies while extended thinking is ON. {@link stripAllThinkingAndDisable}
34
+ * strips every thinking block AND removes the `thinking` param (plus the
35
+ * thinking-only `clear_thinking` context edits), which Anthropic accepts.
36
+ * Mirrors litellm's `strip_thinking_blocks_from_anthropic_messages_request_dict`.
37
+ * The model just doesn't think on that one turn; the next turn re-enables it.
38
+ * - **Proactive strip only for OpenLLM-minted signatures.** A thinking block
39
+ * whose signature is an OpenLLM envelope (`openllm-rs1:` / `openllm-cn1:`,
40
+ * from a Muse / Codex / Grok turn) can never verify at Anthropic, so
41
+ * {@link sanitizeAnthropicReplay} drops it before sending. Genuine Anthropic
42
+ * signatures are never proactively stripped: only the TARGET model's
43
+ * acceptance decides, same-family signatures are often accepted, and
44
+ * stripping them would bust prompt cache on healthy multi-turn. Those go
45
+ * through the reactive 400 → strip → retry path.
46
+ * - **No empty assistant turns.** An assistant message left with no content
47
+ * after a strip (a thinking-only turn) is dropped — Anthropic rejects empty
48
+ * content, and adjacent user turns are merged upstream.
44
49
  */
50
+ import { isOpenllmEnvelopeSignature } from "../adapters/messages/reasoning-signature.js";
45
51
  /** Anthropic 400 body for an undecryptable / foreign-model thinking signature.
46
52
  * Matches the documented phrasing and tolerates the `messages.N.content.M: `
47
53
  * prefix Anthropic attaches; also accepts a bare `Invalid \`signature\` in
@@ -115,35 +121,98 @@ export const messagesBodyHasStrippableThinking = (body) => {
115
121
  return false;
116
122
  return messages.some((msg, i) => isAssistantWithThinking(msg) && !isActiveToolUseContinuation(messages, i));
117
123
  };
124
+ /** Remove the blocks `drop` selects from assistant messages, and drop any
125
+ * assistant message the removal leaves empty. `drop` sees the message's
126
+ * ORIGINAL index (before any message is removed). */
127
+ const stripAssistantBlocks = (messages, drop) => {
128
+ const out = [];
129
+ messages.forEach((msg, i) => {
130
+ const content = messageContent(msg);
131
+ if (msg?.role !== "assistant" ||
132
+ content === null ||
133
+ !content.some((block) => drop(block, i))) {
134
+ out.push(msg);
135
+ return;
136
+ }
137
+ const kept = content.filter((block) => !drop(block, i));
138
+ if (kept.length > 0) {
139
+ out.push({ ...msg, content: kept });
140
+ }
141
+ });
142
+ return out;
143
+ };
144
+ const withMessages = (body, map) => {
145
+ if (body === null || typeof body !== "object")
146
+ return body;
147
+ const messages = body.messages;
148
+ if (!Array.isArray(messages))
149
+ return body;
150
+ return { ...body, messages: map(messages) };
151
+ };
118
152
  /** Return a copy of a built Anthropic Messages body with `thinking` and
119
153
  * `redacted_thinking` blocks removed from every HISTORICAL assistant message.
120
154
  * The active tool-use continuation assistant turn (tool_use answered by a
121
155
  * trailing tool_result) is left intact — see file header. Other content
122
156
  * blocks and non-assistant messages are untouched; order is preserved. */
123
- export const stripMessagesThinkingBlocks = (body) => {
157
+ export const stripMessagesThinkingBlocks = (body) => withMessages(body, (messages) => stripAssistantBlocks(messages, (block, i) => isThinkingBlock(block) && !isActiveToolUseContinuation(messages, i)));
158
+ const isForeignThinkingBlock = (block) => isThinkingBlock(block) &&
159
+ isOpenllmEnvelopeSignature(block.signature);
160
+ /**
161
+ * Proactive, certain half of the recovery: drop every thinking block whose
162
+ * signature OpenLLM minted (a Muse / Codex / Grok turn replayed into
163
+ * Anthropic) BEFORE sending — Anthropic can never verify those, so this
164
+ * costs no request and leaves Anthropic-signed history (and its cache)
165
+ * alone. When the active tool-use turn's thinking is foreign, thinking is
166
+ * turned off for this request (Anthropic requires that turn's thinking while
167
+ * it is on). A cross-model Anthropic rejection still goes through the
168
+ * reactive retry.
169
+ */
170
+ export const sanitizeAnthropicReplay = (body) => {
171
+ const messages = body?.messages;
172
+ if (!Array.isArray(messages))
173
+ return { body, thinkingDisabled: false };
174
+ const hasForeign = (msg) => msg?.role === "assistant" &&
175
+ (messageContent(msg)?.some(isForeignThinkingBlock) ?? false);
176
+ if (!messages.some(hasForeign))
177
+ return { body, thinkingDisabled: false };
178
+ const activeIndex = messages.length - 2;
179
+ if (isActiveToolUseContinuation(messages, activeIndex) &&
180
+ hasForeign(messages[activeIndex])) {
181
+ return { body: stripAllThinkingAndDisable(body), thinkingDisabled: true };
182
+ }
183
+ return {
184
+ body: withMessages(body, (m) => stripAssistantBlocks(m, isForeignThinkingBlock)),
185
+ thinkingDisabled: false,
186
+ };
187
+ };
188
+ /** True when a built Anthropic Messages body carries ANY assistant
189
+ * `thinking`/`redacted_thinking` block (the active turn included). */
190
+ export const messagesBodyHasAnyThinking = (body) => {
191
+ const messages = body?.messages;
192
+ return Array.isArray(messages) && messages.some(isAssistantWithThinking);
193
+ };
194
+ const isClearThinkingEdit = (edit) => edit !== null &&
195
+ typeof edit === "object" &&
196
+ String(edit.type ?? "").startsWith("clear_thinking");
197
+ /** Last-resort recovery: strip thinking from EVERY assistant message (the
198
+ * active tool-use turn included) and turn extended thinking off for this
199
+ * request — without `thinking` enabled Anthropic no longer requires the
200
+ * final tool-use turn to carry its thinking. `clear_thinking_*` context
201
+ * edits are dropped too since they require thinking. See file header. */
202
+ export const stripAllThinkingAndDisable = (body) => {
124
203
  if (body === null || typeof body !== "object")
125
204
  return body;
126
- const b = body;
127
- if (!Array.isArray(b.messages))
128
- return body;
129
- const messages = b.messages.map((msg, i) => {
130
- if (msg === null || typeof msg !== "object")
131
- return msg;
132
- if (msg.role !== "assistant")
133
- return msg;
134
- if (isActiveToolUseContinuation(b.messages, i)) {
135
- return msg;
205
+ const { thinking: _thinking, ...rest } = withMessages(body, (m) => stripAssistantBlocks(m, isThinkingBlock));
206
+ const cm = rest.context_management;
207
+ const edits = cm?.edits;
208
+ if (!Array.isArray(edits) || !edits.some(isClearThinkingEdit))
209
+ return rest;
210
+ const { context_management: _cm, ...withoutCm } = rest;
211
+ const kept = edits.filter((e) => !isClearThinkingEdit(e));
212
+ return kept.length > 0
213
+ ? {
214
+ ...withoutCm,
215
+ context_management: { ...cm, edits: kept },
136
216
  }
137
- const content = messageContent(msg);
138
- if (content === null || !content.some(isThinkingBlock))
139
- return msg;
140
- // Drop only thinking / redacted_thinking; keep text, tool_use, etc. in order.
141
- // Empty content after strip is left as `[]` — rare (assistant-only-thinking
142
- // turns); inventing a placeholder would be non-deterministic / semantic.
143
- return {
144
- ...msg,
145
- content: content.filter((block) => !isThinkingBlock(block)),
146
- };
147
- });
148
- return { ...body, messages };
217
+ : withoutCm;
149
218
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openllmsh/sdk",
3
- "version": "2.10.18-alpha.4",
3
+ "version": "2.10.18-alpha.5",
4
4
  "private": false,
5
5
  "description": "The OpenLLM SDK: typed API contracts, wire-format adapters, the device tunnel, client-side vault crypto, the OpenAPI spec and MCP tools.",
6
6
  "keywords": [