Feat Kiro OAuth, Fix Codex
This commit is contained in:
185
open-sse/translator/response/kiro-to-openai.js
Normal file
185
open-sse/translator/response/kiro-to-openai.js
Normal file
@@ -0,0 +1,185 @@
|
||||
/**
|
||||
* Kiro to OpenAI Response Translator
|
||||
* Converts Kiro/AWS CodeWhisperer streaming events to OpenAI SSE format
|
||||
*/
|
||||
import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
|
||||
/**
|
||||
* Parse Kiro SSE event and convert to OpenAI format
|
||||
* Kiro events: assistantResponseEvent, codeEvent, supplementaryWebLinksEvent, etc.
|
||||
*/
|
||||
function convertKiroToOpenAI(chunk, state) {
|
||||
|
||||
if (!chunk) return null;
|
||||
|
||||
// If chunk is already in OpenAI format (from executor transform), return as-is
|
||||
if (chunk.object === "chat.completion.chunk" && chunk.choices) {
|
||||
return chunk;
|
||||
}
|
||||
|
||||
|
||||
console.log("chunk", chunk);
|
||||
|
||||
// Handle string chunk (raw SSE data)
|
||||
let data = chunk;
|
||||
if (typeof chunk === "string") {
|
||||
// Parse SSE format: event:xxx\ndata:xxx
|
||||
const lines = chunk.split("\n");
|
||||
let eventType = "";
|
||||
let eventData = "";
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("event:")) {
|
||||
eventType = line.slice(6).trim();
|
||||
} else if (line.startsWith(":event-type:")) {
|
||||
eventType = line.slice(12).trim();
|
||||
} else if (line.startsWith("data:")) {
|
||||
eventData = line.slice(5).trim();
|
||||
} else if (line.startsWith(":content-type:")) {
|
||||
// Skip content-type header
|
||||
} else if (line.trim() && !line.startsWith(":")) {
|
||||
// Raw JSON data
|
||||
eventData = line.trim();
|
||||
}
|
||||
}
|
||||
|
||||
if (!eventData) return null;
|
||||
|
||||
try {
|
||||
data = JSON.parse(eventData);
|
||||
data._eventType = eventType;
|
||||
} catch {
|
||||
// Not JSON, might be raw text
|
||||
data = { text: eventData, _eventType: eventType };
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize state if needed
|
||||
if (!state.responseId) {
|
||||
state.responseId = `chatcmpl-${Date.now()}`;
|
||||
state.created = Math.floor(Date.now() / 1000);
|
||||
state.chunkIndex = 0;
|
||||
}
|
||||
|
||||
const eventType = data._eventType || data.event || "";
|
||||
|
||||
// Handle different Kiro event types
|
||||
if (eventType === "assistantResponseEvent" || data.assistantResponseEvent) {
|
||||
const content = data.assistantResponseEvent?.content || data.content || "";
|
||||
if (!content) return null;
|
||||
|
||||
const openaiChunk = {
|
||||
id: state.responseId,
|
||||
object: "chat.completion.chunk",
|
||||
created: state.created,
|
||||
model: state.model || "kiro",
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: {
|
||||
...(state.chunkIndex === 0 ? { role: "assistant" } : {}),
|
||||
content: content
|
||||
},
|
||||
finish_reason: null
|
||||
}]
|
||||
};
|
||||
|
||||
state.chunkIndex++;
|
||||
return openaiChunk;
|
||||
}
|
||||
|
||||
// Handle reasoning/thinking events
|
||||
if (eventType === "reasoningContentEvent" || data.reasoningContentEvent) {
|
||||
const content = data.reasoningContentEvent?.content || data.content || "";
|
||||
if (!content) return null;
|
||||
|
||||
// Convert to thinking block format (Claude-style)
|
||||
const openaiChunk = {
|
||||
id: state.responseId,
|
||||
object: "chat.completion.chunk",
|
||||
created: state.created,
|
||||
model: state.model || "kiro",
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: {
|
||||
...(state.chunkIndex === 0 ? { role: "assistant" } : {}),
|
||||
content: `<thinking>${content}</thinking>`
|
||||
},
|
||||
finish_reason: null
|
||||
}]
|
||||
};
|
||||
|
||||
state.chunkIndex++;
|
||||
return openaiChunk;
|
||||
}
|
||||
|
||||
// Handle tool use events
|
||||
if (eventType === "toolUseEvent" || data.toolUseEvent) {
|
||||
const toolUse = data.toolUseEvent || data;
|
||||
const toolCallId = toolUse.toolUseId || `call_${Date.now()}`;
|
||||
const toolName = toolUse.name || "";
|
||||
const toolInput = toolUse.input || {};
|
||||
|
||||
const openaiChunk = {
|
||||
id: state.responseId,
|
||||
object: "chat.completion.chunk",
|
||||
created: state.created,
|
||||
model: state.model || "kiro",
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: {
|
||||
...(state.chunkIndex === 0 ? { role: "assistant" } : {}),
|
||||
tool_calls: [{
|
||||
index: 0,
|
||||
id: toolCallId,
|
||||
type: "function",
|
||||
function: {
|
||||
name: toolName,
|
||||
arguments: JSON.stringify(toolInput)
|
||||
}
|
||||
}]
|
||||
},
|
||||
finish_reason: null
|
||||
}]
|
||||
};
|
||||
|
||||
state.chunkIndex++;
|
||||
return openaiChunk;
|
||||
}
|
||||
|
||||
// Handle completion/done events
|
||||
if (eventType === "messageStopEvent" || eventType === "done" || data.messageStopEvent) {
|
||||
const openaiChunk = {
|
||||
id: state.responseId,
|
||||
object: "chat.completion.chunk",
|
||||
created: state.created,
|
||||
model: state.model || "kiro",
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: {},
|
||||
finish_reason: "stop"
|
||||
}]
|
||||
};
|
||||
|
||||
return openaiChunk;
|
||||
}
|
||||
|
||||
// Handle usage events
|
||||
if (eventType === "usageEvent" || data.usageEvent) {
|
||||
const usage = data.usageEvent || data;
|
||||
state.usage = {
|
||||
prompt_tokens: usage.inputTokens || 0,
|
||||
completion_tokens: usage.outputTokens || 0,
|
||||
total_tokens: (usage.inputTokens || 0) + (usage.outputTokens || 0)
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
// Unknown event type - skip
|
||||
return null;
|
||||
}
|
||||
|
||||
// Register translator
|
||||
register(FORMATS.KIRO, FORMATS.OPENAI, null, convertKiroToOpenAI);
|
||||
|
||||
export { convertKiroToOpenAI };
|
||||
205
open-sse/translator/response/openai-to-claude.new.js
Normal file
205
open-sse/translator/response/openai-to-claude.new.js
Normal file
@@ -0,0 +1,205 @@
|
||||
import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
|
||||
// Prefix for Claude OAuth tool names (must match request translator)
|
||||
const CLAUDE_OAUTH_TOOL_PREFIX = "proxy_";
|
||||
|
||||
// Helper: stop thinking block if started
|
||||
function stopThinkingBlock(state, results) {
|
||||
if (!state.thinkingBlockStarted) return;
|
||||
results.push({
|
||||
type: "content_block_stop",
|
||||
index: state.thinkingBlockIndex
|
||||
});
|
||||
state.thinkingBlockStarted = false;
|
||||
}
|
||||
|
||||
// Helper: stop text block if started
|
||||
function stopTextBlock(state, results) {
|
||||
if (!state.textBlockStarted || state.textBlockClosed) return;
|
||||
state.textBlockClosed = true;
|
||||
results.push({
|
||||
type: "content_block_stop",
|
||||
index: state.textBlockIndex
|
||||
});
|
||||
state.textBlockStarted = false;
|
||||
}
|
||||
|
||||
// Convert OpenAI stream chunk to Claude format
|
||||
function openaiToClaudeResponse(chunk, state) {
|
||||
if (!chunk || !chunk.choices?.[0]) return null;
|
||||
|
||||
const results = [];
|
||||
const choice = chunk.choices[0];
|
||||
const delta = choice.delta;
|
||||
|
||||
// First chunk - ALWAYS send message_start first
|
||||
if (!state.messageStartSent) {
|
||||
state.messageStartSent = true;
|
||||
state.messageId = chunk.id?.replace("chatcmpl-", "") || `msg_${Date.now()}`;
|
||||
if (!state.messageId || state.messageId === "chat" || state.messageId.length < 8) {
|
||||
state.messageId = chunk.extend_fields?.requestId ||
|
||||
chunk.extend_fields?.traceId ||
|
||||
`msg_${Date.now()}`;
|
||||
}
|
||||
state.model = chunk.model || "unknown";
|
||||
state.nextBlockIndex = 0;
|
||||
results.push({
|
||||
type: "message_start",
|
||||
message: {
|
||||
id: state.messageId,
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
model: state.model,
|
||||
content: [],
|
||||
stop_reason: null,
|
||||
stop_sequence: null,
|
||||
usage: { input_tokens: 0, output_tokens: 0 }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Handle reasoning_content (thinking) - GLM, DeepSeek, etc.
|
||||
const reasoningContent = delta?.reasoning_content || delta?.reasoning;
|
||||
if (reasoningContent) {
|
||||
stopTextBlock(state, results);
|
||||
|
||||
if (!state.thinkingBlockStarted) {
|
||||
state.thinkingBlockIndex = state.nextBlockIndex++;
|
||||
state.thinkingBlockStarted = true;
|
||||
results.push({
|
||||
type: "content_block_start",
|
||||
index: state.thinkingBlockIndex,
|
||||
content_block: { type: "thinking", thinking: "" }
|
||||
});
|
||||
}
|
||||
|
||||
results.push({
|
||||
type: "content_block_delta",
|
||||
index: state.thinkingBlockIndex,
|
||||
delta: { type: "thinking_delta", thinking: reasoningContent }
|
||||
});
|
||||
}
|
||||
|
||||
// Handle regular content
|
||||
if (delta?.content) {
|
||||
stopThinkingBlock(state, results);
|
||||
|
||||
if (!state.textBlockStarted) {
|
||||
state.textBlockIndex = state.nextBlockIndex++;
|
||||
state.textBlockStarted = true;
|
||||
state.textBlockClosed = false;
|
||||
results.push({
|
||||
type: "content_block_start",
|
||||
index: state.textBlockIndex,
|
||||
content_block: { type: "text", text: "" }
|
||||
});
|
||||
}
|
||||
|
||||
results.push({
|
||||
type: "content_block_delta",
|
||||
index: state.textBlockIndex,
|
||||
delta: { type: "text_delta", text: delta.content }
|
||||
});
|
||||
}
|
||||
|
||||
// Tool calls - accumulate arguments instead of emitting immediately
|
||||
if (delta?.tool_calls) {
|
||||
for (const tc of delta.tool_calls) {
|
||||
const idx = tc.index ?? 0;
|
||||
|
||||
if (tc.id) {
|
||||
stopThinkingBlock(state, results);
|
||||
stopTextBlock(state, results);
|
||||
|
||||
const toolBlockIndex = state.nextBlockIndex++;
|
||||
|
||||
// Strip prefix from tool name for response
|
||||
let toolName = tc.function?.name || "";
|
||||
if (toolName.startsWith(CLAUDE_OAUTH_TOOL_PREFIX)) {
|
||||
toolName = toolName.slice(CLAUDE_OAUTH_TOOL_PREFIX.length);
|
||||
}
|
||||
|
||||
// Initialize accumulator for this tool
|
||||
state.toolCalls.set(idx, {
|
||||
id: tc.id,
|
||||
name: toolName,
|
||||
blockIndex: toolBlockIndex,
|
||||
arguments: "", // Accumulate arguments here
|
||||
startEmitted: false // Track if content_block_start sent
|
||||
});
|
||||
}
|
||||
|
||||
// Accumulate arguments instead of emitting immediately
|
||||
if (tc.function?.arguments) {
|
||||
const toolInfo = state.toolCalls.get(idx);
|
||||
if (toolInfo) {
|
||||
toolInfo.arguments += tc.function.arguments;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Finish - emit all accumulated tools in correct order
|
||||
if (choice.finish_reason) {
|
||||
stopThinkingBlock(state, results);
|
||||
stopTextBlock(state, results);
|
||||
|
||||
// STEP 1: Emit all content_block_start for tools (like CLIProxyAPIPlus)
|
||||
const sortedTools = Array.from(state.toolCalls.entries()).sort((a, b) => a[0] - b[0]);
|
||||
for (const [, toolInfo] of sortedTools) {
|
||||
if (!toolInfo.startEmitted) {
|
||||
results.push({
|
||||
type: "content_block_start",
|
||||
index: toolInfo.blockIndex,
|
||||
content_block: {
|
||||
type: "tool_use",
|
||||
id: toolInfo.id,
|
||||
name: toolInfo.name,
|
||||
input: {}
|
||||
}
|
||||
});
|
||||
toolInfo.startEmitted = true;
|
||||
}
|
||||
}
|
||||
|
||||
// STEP 2: Emit input_json_delta + content_block_stop for each tool
|
||||
for (const [, toolInfo] of sortedTools) {
|
||||
if (toolInfo.arguments) {
|
||||
results.push({
|
||||
type: "content_block_delta",
|
||||
index: toolInfo.blockIndex,
|
||||
delta: { type: "input_json_delta", partial_json: toolInfo.arguments }
|
||||
});
|
||||
}
|
||||
|
||||
results.push({
|
||||
type: "content_block_stop",
|
||||
index: toolInfo.blockIndex
|
||||
});
|
||||
}
|
||||
|
||||
results.push({
|
||||
type: "message_delta",
|
||||
delta: { stop_reason: convertFinishReason(choice.finish_reason) },
|
||||
usage: { output_tokens: 0 }
|
||||
});
|
||||
results.push({ type: "message_stop" });
|
||||
}
|
||||
|
||||
return results.length > 0 ? results : null;
|
||||
}
|
||||
|
||||
// Convert OpenAI finish_reason to Claude stop_reason
|
||||
function convertFinishReason(reason) {
|
||||
switch (reason) {
|
||||
case "stop": return "end_turn";
|
||||
case "length": return "max_tokens";
|
||||
case "tool_calls": return "tool_use";
|
||||
default: return "end_turn";
|
||||
}
|
||||
}
|
||||
|
||||
// Register
|
||||
register(FORMATS.OPENAI, FORMATS.CLAUDE, null, openaiToClaudeResponse);
|
||||
|
||||
Reference in New Issue
Block a user