claudish 3.3.3 → 3.3.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.
- package/dist/index.js +148 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -57064,10 +57064,84 @@ class OpenAIHandler {
|
|
|
57064
57064
|
}
|
|
57065
57065
|
return payload;
|
|
57066
57066
|
}
|
|
57067
|
+
convertMessagesToResponsesAPI(messages) {
|
|
57068
|
+
const result = [];
|
|
57069
|
+
for (const msg of messages) {
|
|
57070
|
+
if (msg.role === "system")
|
|
57071
|
+
continue;
|
|
57072
|
+
if (msg.role === "tool") {
|
|
57073
|
+
result.push({
|
|
57074
|
+
type: "function_call_output",
|
|
57075
|
+
call_id: msg.tool_call_id,
|
|
57076
|
+
output: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
|
|
57077
|
+
});
|
|
57078
|
+
continue;
|
|
57079
|
+
}
|
|
57080
|
+
if (msg.role === "assistant" && msg.tool_calls) {
|
|
57081
|
+
const content = [];
|
|
57082
|
+
if (msg.content) {
|
|
57083
|
+
const textContent = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
|
|
57084
|
+
if (textContent) {
|
|
57085
|
+
content.push({ type: "output_text", text: textContent });
|
|
57086
|
+
}
|
|
57087
|
+
}
|
|
57088
|
+
for (const toolCall of msg.tool_calls) {
|
|
57089
|
+
if (toolCall.type === "function") {
|
|
57090
|
+
content.push({
|
|
57091
|
+
type: "function_call",
|
|
57092
|
+
call_id: toolCall.id,
|
|
57093
|
+
name: toolCall.function.name,
|
|
57094
|
+
arguments: toolCall.function.arguments
|
|
57095
|
+
});
|
|
57096
|
+
}
|
|
57097
|
+
}
|
|
57098
|
+
result.push({ role: "assistant", content });
|
|
57099
|
+
continue;
|
|
57100
|
+
}
|
|
57101
|
+
if (typeof msg.content === "string") {
|
|
57102
|
+
result.push({
|
|
57103
|
+
role: msg.role,
|
|
57104
|
+
content: [
|
|
57105
|
+
{
|
|
57106
|
+
type: msg.role === "user" ? "input_text" : "output_text",
|
|
57107
|
+
text: msg.content
|
|
57108
|
+
}
|
|
57109
|
+
]
|
|
57110
|
+
});
|
|
57111
|
+
continue;
|
|
57112
|
+
}
|
|
57113
|
+
if (Array.isArray(msg.content)) {
|
|
57114
|
+
const convertedContent = msg.content.map((block) => {
|
|
57115
|
+
if (block.type === "text") {
|
|
57116
|
+
return {
|
|
57117
|
+
type: msg.role === "user" ? "input_text" : "output_text",
|
|
57118
|
+
text: block.text
|
|
57119
|
+
};
|
|
57120
|
+
}
|
|
57121
|
+
if (block.type === "image_url") {
|
|
57122
|
+
const imageUrl = typeof block.image_url === "string" ? block.image_url : block.image_url?.url || block.image_url;
|
|
57123
|
+
return {
|
|
57124
|
+
type: "input_image",
|
|
57125
|
+
image_url: imageUrl
|
|
57126
|
+
};
|
|
57127
|
+
}
|
|
57128
|
+
return block;
|
|
57129
|
+
});
|
|
57130
|
+
result.push({
|
|
57131
|
+
role: msg.role,
|
|
57132
|
+
content: convertedContent
|
|
57133
|
+
});
|
|
57134
|
+
continue;
|
|
57135
|
+
}
|
|
57136
|
+
result.push(msg);
|
|
57137
|
+
}
|
|
57138
|
+
return result;
|
|
57139
|
+
}
|
|
57067
57140
|
buildResponsesPayload(claudeRequest, messages, tools) {
|
|
57141
|
+
const convertedMessages = this.convertMessagesToResponsesAPI(messages);
|
|
57068
57142
|
const payload = {
|
|
57069
57143
|
model: this.modelName,
|
|
57070
|
-
input:
|
|
57144
|
+
input: convertedMessages,
|
|
57071
57145
|
stream: true
|
|
57072
57146
|
};
|
|
57073
57147
|
if (claudeRequest.system) {
|
|
@@ -57099,9 +57173,12 @@ class OpenAIHandler {
|
|
|
57099
57173
|
const encoder = new TextEncoder;
|
|
57100
57174
|
const decoder = new TextDecoder;
|
|
57101
57175
|
let buffer = "";
|
|
57102
|
-
let
|
|
57176
|
+
let blockIndex = 0;
|
|
57103
57177
|
let inputTokens = 0;
|
|
57104
57178
|
let outputTokens = 0;
|
|
57179
|
+
let hasTextContent = false;
|
|
57180
|
+
let hasToolUse = false;
|
|
57181
|
+
const functionCalls = new Map;
|
|
57105
57182
|
const stream = new ReadableStream({
|
|
57106
57183
|
start: async (controller) => {
|
|
57107
57184
|
const messageStart = {
|
|
@@ -57140,48 +57217,109 @@ data: ${JSON.stringify(messageStart)}
|
|
|
57140
57217
|
try {
|
|
57141
57218
|
const event = JSON.parse(data);
|
|
57142
57219
|
if (event.type === "response.output_text.delta") {
|
|
57143
|
-
if (
|
|
57220
|
+
if (!hasTextContent) {
|
|
57144
57221
|
const blockStart = {
|
|
57145
57222
|
type: "content_block_start",
|
|
57146
|
-
index:
|
|
57223
|
+
index: blockIndex,
|
|
57147
57224
|
content_block: { type: "text", text: "" }
|
|
57148
57225
|
};
|
|
57149
57226
|
controller.enqueue(encoder.encode(`event: content_block_start
|
|
57150
57227
|
data: ${JSON.stringify(blockStart)}
|
|
57151
57228
|
|
|
57152
57229
|
`));
|
|
57153
|
-
|
|
57230
|
+
hasTextContent = true;
|
|
57154
57231
|
}
|
|
57155
57232
|
const delta = {
|
|
57156
57233
|
type: "content_block_delta",
|
|
57157
|
-
index:
|
|
57234
|
+
index: blockIndex,
|
|
57158
57235
|
delta: { type: "text_delta", text: event.delta || "" }
|
|
57159
57236
|
};
|
|
57160
57237
|
controller.enqueue(encoder.encode(`event: content_block_delta
|
|
57161
57238
|
data: ${JSON.stringify(delta)}
|
|
57162
57239
|
|
|
57163
57240
|
`));
|
|
57241
|
+
} else if (event.type === "response.output_item.added") {
|
|
57242
|
+
if (event.item?.type === "function_call") {
|
|
57243
|
+
const callId = event.item.call_id || event.item.id;
|
|
57244
|
+
const fnIndex = blockIndex + functionCalls.size + (hasTextContent ? 1 : 0);
|
|
57245
|
+
functionCalls.set(callId, {
|
|
57246
|
+
name: event.item.name || "",
|
|
57247
|
+
arguments: "",
|
|
57248
|
+
index: fnIndex
|
|
57249
|
+
});
|
|
57250
|
+
if (hasTextContent && !hasToolUse) {
|
|
57251
|
+
const blockStop = { type: "content_block_stop", index: blockIndex };
|
|
57252
|
+
controller.enqueue(encoder.encode(`event: content_block_stop
|
|
57253
|
+
data: ${JSON.stringify(blockStop)}
|
|
57254
|
+
|
|
57255
|
+
`));
|
|
57256
|
+
blockIndex++;
|
|
57257
|
+
}
|
|
57258
|
+
const toolStart = {
|
|
57259
|
+
type: "content_block_start",
|
|
57260
|
+
index: fnIndex,
|
|
57261
|
+
content_block: {
|
|
57262
|
+
type: "tool_use",
|
|
57263
|
+
id: callId,
|
|
57264
|
+
name: event.item.name || "",
|
|
57265
|
+
input: {}
|
|
57266
|
+
}
|
|
57267
|
+
};
|
|
57268
|
+
controller.enqueue(encoder.encode(`event: content_block_start
|
|
57269
|
+
data: ${JSON.stringify(toolStart)}
|
|
57270
|
+
|
|
57271
|
+
`));
|
|
57272
|
+
hasToolUse = true;
|
|
57273
|
+
}
|
|
57274
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
57275
|
+
const callId = event.call_id || event.item_id;
|
|
57276
|
+
const fnCall = functionCalls.get(callId);
|
|
57277
|
+
if (fnCall) {
|
|
57278
|
+
fnCall.arguments += event.delta || "";
|
|
57279
|
+
const delta = {
|
|
57280
|
+
type: "content_block_delta",
|
|
57281
|
+
index: fnCall.index,
|
|
57282
|
+
delta: { type: "input_json_delta", partial_json: event.delta || "" }
|
|
57283
|
+
};
|
|
57284
|
+
controller.enqueue(encoder.encode(`event: content_block_delta
|
|
57285
|
+
data: ${JSON.stringify(delta)}
|
|
57286
|
+
|
|
57287
|
+
`));
|
|
57288
|
+
}
|
|
57289
|
+
} else if (event.type === "response.output_item.done") {
|
|
57290
|
+
if (event.item?.type === "function_call") {
|
|
57291
|
+
const callId = event.item.call_id || event.item.id;
|
|
57292
|
+
const fnCall = functionCalls.get(callId);
|
|
57293
|
+
if (fnCall) {
|
|
57294
|
+
const blockStop = { type: "content_block_stop", index: fnCall.index };
|
|
57295
|
+
controller.enqueue(encoder.encode(`event: content_block_stop
|
|
57296
|
+
data: ${JSON.stringify(blockStop)}
|
|
57297
|
+
|
|
57298
|
+
`));
|
|
57299
|
+
}
|
|
57300
|
+
}
|
|
57164
57301
|
} else if (event.type === "response.completed") {
|
|
57165
57302
|
if (event.response?.usage) {
|
|
57166
57303
|
inputTokens = event.response.usage.input_tokens || 0;
|
|
57167
57304
|
outputTokens = event.response.usage.output_tokens || 0;
|
|
57168
57305
|
}
|
|
57169
|
-
}
|
|
57306
|
+
}
|
|
57170
57307
|
} catch (parseError) {
|
|
57171
57308
|
log(`[OpenAIHandler] Error parsing Responses event: ${parseError}`);
|
|
57172
57309
|
}
|
|
57173
57310
|
}
|
|
57174
57311
|
}
|
|
57175
|
-
if (
|
|
57176
|
-
const blockStop = { type: "content_block_stop", index:
|
|
57312
|
+
if (hasTextContent && !hasToolUse) {
|
|
57313
|
+
const blockStop = { type: "content_block_stop", index: blockIndex };
|
|
57177
57314
|
controller.enqueue(encoder.encode(`event: content_block_stop
|
|
57178
57315
|
data: ${JSON.stringify(blockStop)}
|
|
57179
57316
|
|
|
57180
57317
|
`));
|
|
57181
57318
|
}
|
|
57319
|
+
const stopReason = hasToolUse ? "tool_use" : "end_turn";
|
|
57182
57320
|
const messageDelta = {
|
|
57183
57321
|
type: "message_delta",
|
|
57184
|
-
delta: { stop_reason:
|
|
57322
|
+
delta: { stop_reason: stopReason, stop_sequence: null },
|
|
57185
57323
|
usage: { output_tokens: outputTokens }
|
|
57186
57324
|
};
|
|
57187
57325
|
controller.enqueue(encoder.encode(`event: message_delta
|