@cortexkit/pi-anthropic-auth 2.15.0 → 2.15.2

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 (2) hide show
  1. package/dist/convert.js +34 -45
  2. package/package.json +2 -2
package/dist/convert.js CHANGED
@@ -135,8 +135,16 @@ function convertTextAndImages(content) {
135
135
  }
136
136
  return blocks;
137
137
  }
138
- function convertMessages(messages, targetModelId, visibleReplies) {
138
+ function convertMessages(messages, targetModelId, visibleReplies, droppedAssistants) {
139
139
  const result = [];
140
+ // Effort history counts source assistant turns, including ones with nothing
141
+ // safe to replay. Keep their boundaries until effort markers are placed;
142
+ // buildAnthropicRequest removes these placeholders before any wire output.
143
+ const appendDroppedAssistant = () => {
144
+ const placeholder = { role: 'assistant', content: [] };
145
+ droppedAssistants.add(placeholder);
146
+ result.push(placeholder);
147
+ };
140
148
  for (let index = 0; index < messages.length; index++) {
141
149
  const message = messages[index];
142
150
  if (!message)
@@ -173,6 +181,7 @@ function convertMessages(messages, targetModelId, visibleReplies) {
173
181
  // a matching tool_result message. Interrupted Pi runs can leave an
174
182
  // aborted assistant message with a partial toolCall and no result; drop
175
183
  // that incomplete assistant turn from the replayed history.
184
+ appendDroppedAssistant();
176
185
  continue;
177
186
  }
178
187
  const blocks = [];
@@ -191,17 +200,10 @@ function convertMessages(messages, targetModelId, visibleReplies) {
191
200
  ANTHROPIC_REPLAY_APIS.has(message.api) &&
192
201
  message.model === targetModelId &&
193
202
  !isOpenAIReasoningSignature(signature);
194
- // Match Pi's built-in origin check: only replay signatures from the
195
- // same Anthropic API/model. Preserve visible foreign reasoning as
196
- // ordinary text, but drop opaque redacted/OpenAI encrypted state.
197
- if (hasSignature && !signatureBelongsToTarget) {
198
- if (!thinking.redacted &&
199
- !isOpenAIReasoningSignature(signature) &&
200
- thinking.thinking.trim()) {
201
- blocks.push({ type: 'text', text: sanitize(thinking.thinking) });
202
- }
203
+ // Replay reasoning only with a signature from the same Anthropic
204
+ // API/model. Anthropic refuses reasoning replayed as ordinary text.
205
+ if (hasSignature && !signatureBelongsToTarget)
203
206
  continue;
204
- }
205
207
  if (thinking.redacted) {
206
208
  if (hasSignature) {
207
209
  blocks.push({
@@ -211,35 +213,17 @@ function convertMessages(messages, targetModelId, visibleReplies) {
211
213
  }
212
214
  continue;
213
215
  }
214
- if (!hasSignature) {
215
- // Anthropic rejects unsigned thinking blocks. Preserve readable
216
- // output from interrupted streams as ordinary assistant text.
217
- if (thinking.thinking.trim()) {
218
- blocks.push({
219
- type: 'text',
220
- text: sanitize(thinking.thinking),
221
- });
222
- }
216
+ if (!hasSignature)
223
217
  continue;
224
- }
225
- if (hasLoneSurrogate(thinking.thinking)) {
226
- // Signature authenticates the thinking bytes exactly. Sanitizing the
227
- // text would invalidate it, so downgrade malformed signed thinking
228
- // to plain text and omit the signature instead.
229
- if (thinking.thinking.trim()) {
230
- blocks.push({
231
- type: 'text',
232
- text: sanitize(thinking.thinking),
233
- });
234
- }
235
- }
236
- else {
237
- blocks.push({
238
- type: 'thinking',
239
- thinking: thinking.thinking,
240
- signature,
241
- });
242
- }
218
+ // A signature authenticates the exact bytes. Sanitizing malformed
219
+ // thinking would invalidate it, and sending lone surrogates is invalid.
220
+ if (hasLoneSurrogate(thinking.thinking))
221
+ continue;
222
+ blocks.push({
223
+ type: 'thinking',
224
+ thinking: thinking.thinking,
225
+ signature,
226
+ });
243
227
  }
244
228
  else if (block.type === 'toolCall') {
245
229
  blocks.push({
@@ -253,12 +237,13 @@ function convertMessages(messages, targetModelId, visibleReplies) {
253
237
  if (blocks.length) {
254
238
  const converted = { role: 'assistant', content: blocks };
255
239
  result.push(converted);
256
- // Converted thinking can become text for replay, but it was never a
257
- // visible reply. Count only actual text blocks, using the exact outgoing
258
- // strings collected above, so the note decision for this message stays
259
- // the same on every later request.
240
+ // Only actual text is a visible reply. Use the exact outgoing strings so
241
+ // later requests make the same decision about adding a turn-end note.
260
242
  visibleReplies.set(converted, visibleText);
261
243
  }
244
+ else {
245
+ appendDroppedAssistant();
246
+ }
262
247
  continue;
263
248
  }
264
249
  if (message.role === 'toolResult') {
@@ -561,10 +546,13 @@ export async function buildAnthropicRequest(modelId, context, options, cache, fa
561
546
  // the Anthropic messages again after conversion removes empty user messages.
562
547
  assertNoMeaningfulTrailingAssistant(context.messages);
563
548
  const visibleReplies = new WeakMap();
564
- const messages = convertMessages(context.messages, modelId, visibleReplies);
549
+ const droppedAssistants = new WeakSet();
550
+ const messages = convertMessages(context.messages, modelId, visibleReplies, droppedAssistants);
565
551
  stripEmptyTrailingAssistantMessages(messages);
566
552
  const baseLastMessage = forkBase
567
- ? messages[convertMessages(forkBase.messages, modelId, new WeakMap()).length - 1]
553
+ ? messages
554
+ .slice(0, convertMessages(forkBase.messages, modelId, new WeakMap(), new WeakSet()).length)
555
+ .findLast((message) => !droppedAssistants.has(message))
568
556
  : undefined;
569
557
  // The head's final user block was an array so it could carry its cache
570
558
  // marker. Keep that block shape when it is no longer the fork's newest user.
@@ -690,6 +678,7 @@ export async function buildAnthropicRequest(modelId, context, options, cache, fa
690
678
  }
691
679
  }
692
680
  const forkEffortApplied = applyPiForkEffort(body, controls.effortTransitions ?? [], controls.forkEffort);
681
+ body.messages = body.messages.filter((message) => !droppedAssistants.has(message));
693
682
  if (identity) {
694
683
  applyTurnEndNotes(body, {
695
684
  tools: controls.turnEndNote?.tools,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-anthropic-auth",
3
- "version": "2.15.0",
3
+ "version": "2.15.2",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,7 +36,7 @@
36
36
  "prepublishOnly": "bun run build"
37
37
  },
38
38
  "dependencies": {
39
- "@cortexkit/anthropic-auth-core": "2.15.0"
39
+ "@cortexkit/anthropic-auth-core": "2.15.2"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@earendil-works/pi-ai": ">=0.86.1 <2",