Skip to content

Commit d5a4fde

Browse files
committed
⬆️ update nanollm
1 parent b990f4d commit d5a4fde

11 files changed

Lines changed: 10157 additions & 113 deletions

File tree

26.03/nanollm/.test-dist/src/converters/requests.js

Lines changed: 99 additions & 48 deletions
Large diffs are not rendered by default.

26.03/nanollm/.test-dist/src/converters/responses.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ export function normalizeOpenAIResponsesResponse(response) {
4141
}
4242
if (item.type === "custom_tool_call") {
4343
toolCalls.push({ kind: "custom", id: item.call_id, name: item.name, payload: item.input });
44+
continue;
45+
}
46+
if (item.type === "reasoning") {
47+
for (const part of item.content ?? []) {
48+
parts.push({ type: "thinking", thinking: part.text });
49+
}
50+
for (const part of item.summary ?? []) {
51+
parts.push({ type: "thinking", thinking: part.text });
52+
}
53+
if (item.encrypted_content) {
54+
parts.push({ type: "redacted_thinking", data: item.encrypted_content });
55+
}
4456
}
4557
}
4658
return {
@@ -64,6 +76,14 @@ export function normalizeAnthropicResponse(response) {
6476
parts.push(text(block.text));
6577
continue;
6678
}
79+
if (block.type === "thinking") {
80+
parts.push({ type: "thinking", thinking: block.thinking, signature: block.signature });
81+
continue;
82+
}
83+
if (block.type === "redacted_thinking") {
84+
parts.push({ type: "redacted_thinking", data: block.data });
85+
continue;
86+
}
6787
if (block.type === "tool_use" || block.type === "server_tool_use") {
6888
toolCalls.push({
6989
kind: "function",
@@ -116,20 +136,38 @@ export function denormalizeToOpenAIResponsesResponse(response) {
116136
object: "response",
117137
created_at: response.createdAt,
118138
model: response.model,
119-
output_text: collapseText(response.message.parts),
139+
output_text: collapseText(response.message.parts.filter((part) => part.type === "text" || part.type === "refusal")),
120140
error: null,
121141
incomplete_details: null,
122142
instructions: null,
123143
metadata: null,
124144
output: [
145+
...response.message.parts
146+
.filter((part) => part.type === "thinking" || part.type === "redacted_thinking")
147+
.map((part, index) => part.type === "thinking"
148+
? {
149+
id: `reasoning_${index}`,
150+
type: "reasoning",
151+
summary: [{ type: "summary_text", text: part.thinking }],
152+
content: [{ type: "reasoning_text", text: part.thinking }],
153+
encrypted_content: part.signature ?? null,
154+
status: "completed",
155+
}
156+
: {
157+
id: `reasoning_${index}`,
158+
type: "reasoning",
159+
summary: [],
160+
encrypted_content: part.data,
161+
status: "completed",
162+
}),
125163
...(response.message.parts.length > 0
126164
? [
127165
{
128166
id: "msg_1",
129167
type: "message",
130168
role: "assistant",
131169
status: "completed",
132-
content: response.message.parts.map((part) => part.type === "text"
170+
content: response.message.parts.filter((part) => part.type !== "thinking" && part.type !== "redacted_thinking").map((part) => part.type === "text"
133171
? { type: "output_text", text: part.text, annotations: [] }
134172
: { type: "refusal", refusal: part.text }),
135173
},

26.03/nanollm/.test-dist/src/converters/test.js

Lines changed: 3637 additions & 0 deletions
Large diffs are not rendered by default.

26.03/nanollm/.test-dist/tests/run.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "node:assert/strict";
2-
import { anthropicMessageRequestToChatParams, anthropicMessageRequestToResponsesRequest, anthropicMessageToChatCompletion, chatCompletionToAnthropicMessage, chatParamsToAnthropicMessageRequest, chatParamsToResponsesRequest, responsesRequestToAnthropicMessageRequest, responsesRequestToChatParams, responsesResponseToChatCompletion, } from "../src/converters/index.js";
2+
import { anthropicMessageRequestToChatParams, anthropicMessageRequestToResponsesRequest, anthropicMessageToChatCompletion, anthropicMessageToResponsesResponse, chatCompletionToAnthropicMessage, chatParamsToAnthropicMessageRequest, chatParamsToResponsesRequest, responsesRequestToAnthropicMessageRequest, responsesRequestToChatParams, responsesResponseToChatCompletion, } from "../src/converters/index.js";
33
function run(name, fn) {
44
try {
55
fn();
@@ -241,3 +241,64 @@ run("chat response round-trip through anthropic preserves tool call name", () =>
241241
const chat = anthropicMessageToChatCompletion(anthropic);
242242
assert.equal((chat.choices[0].message.tool_calls?.[0]).function.name, "lookup");
243243
});
244+
run("anthropic request thinking block is preserved in responses request", () => {
245+
const responses = anthropicMessageRequestToResponsesRequest({
246+
model: "claude-sonnet-4-5",
247+
max_tokens: 1024,
248+
messages: [
249+
{
250+
role: "assistant",
251+
content: [
252+
{
253+
type: "thinking",
254+
thinking: "I should call the weather tool.",
255+
signature: "sig_1",
256+
},
257+
{
258+
type: "text",
259+
text: "Let me check.",
260+
},
261+
],
262+
},
263+
],
264+
});
265+
const input = responses.input;
266+
assert.equal(input[0].type, "reasoning");
267+
assert.equal(input[0].content[0].text, "I should call the weather tool.");
268+
assert.equal(input[1].type, "message");
269+
});
270+
run("anthropic response thinking block is preserved in responses response", () => {
271+
const responses = anthropicMessageToResponsesResponse({
272+
id: "msg_thinking",
273+
type: "message",
274+
role: "assistant",
275+
model: "claude-sonnet-4-5",
276+
container: null,
277+
stop_reason: "end_turn",
278+
stop_sequence: null,
279+
content: [
280+
{
281+
type: "thinking",
282+
thinking: "Need to reason first.",
283+
signature: "sig_resp",
284+
},
285+
{
286+
type: "text",
287+
text: "final answer",
288+
citations: null,
289+
},
290+
],
291+
usage: {
292+
input_tokens: 1,
293+
output_tokens: 1,
294+
cache_creation_input_tokens: null,
295+
cache_read_input_tokens: null,
296+
cache_creation: null,
297+
inference_geo: null,
298+
service_tier: null,
299+
server_tool_use: null,
300+
},
301+
});
302+
assert.equal(responses.output[0].type, "reasoning");
303+
assert.equal(responses.output[1].type, "message");
304+
});

26.03/nanollm/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"start": "node --experimental-strip-types server.ts",
88
"dev": "node --watch --experimental-strip-types server.ts",
99
"typecheck": "tsc --noEmit",
10-
"test": "tsc -p tsconfig.build.json && node .test-dist/tests/run.js"
10+
"test": "tsc -p tsconfig.build.json && node .test-dist/tests/run.js",
11+
"converter:test": "tsc -p tsconfig.build.json && node .test-dist/src/converters/test.js"
1112
},
1213
"keywords": [],
1314
"author": "",

0 commit comments

Comments
 (0)