pi-anthropic-oauth 0.2.0 → 0.2.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/package.json +1 -1
- package/src/convert.ts +28 -0
- package/src/stream.ts +22 -1
package/package.json
CHANGED
package/src/convert.ts
CHANGED
|
@@ -11,8 +11,10 @@ type ToolResultContentBlock =
|
|
|
11
11
|
};
|
|
12
12
|
};
|
|
13
13
|
import type {
|
|
14
|
+
Api,
|
|
14
15
|
ImageContent,
|
|
15
16
|
Message,
|
|
17
|
+
Model,
|
|
16
18
|
TextContent,
|
|
17
19
|
ThinkingContent,
|
|
18
20
|
Tool,
|
|
@@ -52,6 +54,7 @@ export function fromClaudeCodeToolName(name: string, tools?: Tool[]): string {
|
|
|
52
54
|
export function convertPiMessagesToAnthropic(
|
|
53
55
|
messages: Message[],
|
|
54
56
|
isOAuth: boolean,
|
|
57
|
+
model: Pick<Model<Api>, "provider" | "api" | "id">,
|
|
55
58
|
): MessageParam[] {
|
|
56
59
|
const params: MessageParam[] = [];
|
|
57
60
|
const toolIdMap = new Map<string, string>();
|
|
@@ -131,9 +134,34 @@ export function convertPiMessagesToAnthropic(
|
|
|
131
134
|
|
|
132
135
|
const blocks: ContentBlockParam[] = [];
|
|
133
136
|
const emittedToolUseIds: string[] = [];
|
|
137
|
+
const isSameModel =
|
|
138
|
+
message.provider === model.provider &&
|
|
139
|
+
message.api === model.api &&
|
|
140
|
+
message.model === model.id;
|
|
134
141
|
for (const block of message.content) {
|
|
135
142
|
if (block.type === "text" && block.text.trim()) {
|
|
136
143
|
blocks.push({ type: "text", text: sanitizeSurrogates(block.text) });
|
|
144
|
+
} else if (block.type === "thinking") {
|
|
145
|
+
const thinkingSignature = block.thinkingSignature?.trim();
|
|
146
|
+
if (block.redacted) {
|
|
147
|
+
if (isSameModel && thinkingSignature) {
|
|
148
|
+
blocks.push({
|
|
149
|
+
type: "redacted_thinking",
|
|
150
|
+
data: block.thinkingSignature!,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
} else if (isSameModel && thinkingSignature) {
|
|
154
|
+
blocks.push({
|
|
155
|
+
type: "thinking",
|
|
156
|
+
thinking: sanitizeSurrogates(block.thinking),
|
|
157
|
+
signature: block.thinkingSignature!,
|
|
158
|
+
});
|
|
159
|
+
} else if (block.thinking.trim()) {
|
|
160
|
+
blocks.push({
|
|
161
|
+
type: "text",
|
|
162
|
+
text: sanitizeSurrogates(block.thinking),
|
|
163
|
+
});
|
|
164
|
+
}
|
|
137
165
|
} else if (block.type === "toolCall") {
|
|
138
166
|
const anthropicId = getAnthropicToolId(block.id);
|
|
139
167
|
blocks.push({
|
package/src/stream.ts
CHANGED
|
@@ -144,7 +144,7 @@ export function streamAnthropicOAuth(
|
|
|
144
144
|
|
|
145
145
|
const params: MessageCreateParamsStreaming = {
|
|
146
146
|
model: model.id,
|
|
147
|
-
messages: convertPiMessagesToAnthropic(context.messages, isOAuth),
|
|
147
|
+
messages: convertPiMessagesToAnthropic(context.messages, isOAuth, model),
|
|
148
148
|
max_tokens: maxTokens,
|
|
149
149
|
stream: true,
|
|
150
150
|
};
|
|
@@ -248,6 +248,19 @@ export function streamAnthropicOAuth(
|
|
|
248
248
|
contentIndex: output.content.length - 1,
|
|
249
249
|
partial: output,
|
|
250
250
|
});
|
|
251
|
+
} else if (event.content_block.type === "redacted_thinking") {
|
|
252
|
+
output.content.push({
|
|
253
|
+
type: "thinking",
|
|
254
|
+
thinking: "[Reasoning redacted]",
|
|
255
|
+
thinkingSignature: event.content_block.data,
|
|
256
|
+
redacted: true,
|
|
257
|
+
index: event.index,
|
|
258
|
+
} as IndexedBlock);
|
|
259
|
+
stream.push({
|
|
260
|
+
type: "thinking_start",
|
|
261
|
+
contentIndex: output.content.length - 1,
|
|
262
|
+
partial: output,
|
|
263
|
+
});
|
|
251
264
|
} else if (event.content_block.type === "tool_use") {
|
|
252
265
|
output.content.push({
|
|
253
266
|
type: "toolCall",
|
|
@@ -378,6 +391,14 @@ export function streamAnthropicOAuth(
|
|
|
378
391
|
output.usage.cacheWrite =
|
|
379
392
|
(event.usage as { cache_creation_input_tokens?: number })
|
|
380
393
|
.cache_creation_input_tokens || 0;
|
|
394
|
+
const thinkingTokens = (
|
|
395
|
+
event.usage as {
|
|
396
|
+
output_tokens_details?: { thinking_tokens?: number };
|
|
397
|
+
}
|
|
398
|
+
).output_tokens_details?.thinking_tokens;
|
|
399
|
+
if (thinkingTokens != null) {
|
|
400
|
+
output.usage.reasoning = thinkingTokens;
|
|
401
|
+
}
|
|
381
402
|
output.usage.totalTokens =
|
|
382
403
|
output.usage.input +
|
|
383
404
|
output.usage.output +
|