mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-02 21:32:10 +03:00
refactor(open-sse): phase 6 — reduce @ts-ignore from 231 to 186
- Remove 209 standalone @ts-ignore annotations - Fix 11 root-cause object literals with Record<string, any> typing - Properly type message objects in responseTranslator, kiro executor - Re-insert 164 targeted @ts-ignore for remaining complex patterns - Net reduction: 231 → 186 @ts-ignore (-20%) - Zero TypeScript errors maintained
This commit is contained in:
@@ -466,13 +466,11 @@ export class CursorExecutor extends BaseExecutor {
|
||||
|
||||
console.log(`[CURSOR BUFFER] Final toolCalls count: ${toolCalls.length}`);
|
||||
|
||||
const message = {
|
||||
role: "assistant",
|
||||
const message: Record<string, any> = { role: "assistant",
|
||||
content: totalContent || null,
|
||||
};
|
||||
|
||||
if (toolCalls.length > 0) {
|
||||
// @ts-ignore
|
||||
message.tool_calls = toolCalls;
|
||||
}
|
||||
|
||||
|
||||
@@ -83,8 +83,7 @@ export class KiroExecutor extends BaseExecutor {
|
||||
let chunkIndex = 0;
|
||||
const responseId = `chatcmpl-${Date.now()}`;
|
||||
const created = Math.floor(Date.now() / 1000);
|
||||
const state = {
|
||||
endDetected: false,
|
||||
const state: Record<string, any> = { endDetected: false,
|
||||
finishEmitted: false,
|
||||
hasToolCalls: false,
|
||||
toolCallIndex: 0,
|
||||
@@ -118,15 +117,12 @@ export class KiroExecutor extends BaseExecutor {
|
||||
const eventType = event.headers[":event-type"] || "";
|
||||
|
||||
// Track total content length for token estimation
|
||||
// @ts-ignore
|
||||
if (!state.totalContentLength) state.totalContentLength = 0;
|
||||
// @ts-ignore
|
||||
if (!state.contextUsagePercentage) state.contextUsagePercentage = 0;
|
||||
|
||||
// Handle assistantResponseEvent
|
||||
if (eventType === "assistantResponseEvent" && event.payload?.content) {
|
||||
const content = event.payload.content;
|
||||
// @ts-ignore
|
||||
state.totalContentLength += content.length;
|
||||
|
||||
const chunk = {
|
||||
@@ -279,16 +275,13 @@ export class KiroExecutor extends BaseExecutor {
|
||||
|
||||
// Handle contextUsageEvent to extract contextUsagePercentage
|
||||
if (eventType === "contextUsageEvent" && event.payload?.contextUsagePercentage) {
|
||||
// @ts-ignore
|
||||
state.contextUsagePercentage = event.payload.contextUsagePercentage;
|
||||
// Mark that we received context usage event
|
||||
// @ts-ignore
|
||||
state.hasContextUsage = true;
|
||||
}
|
||||
|
||||
// Handle meteringEvent - mark that we received it
|
||||
if (eventType === "meteringEvent") {
|
||||
// @ts-ignore
|
||||
state.hasMeteringEvent = true;
|
||||
}
|
||||
|
||||
@@ -301,7 +294,6 @@ export class KiroExecutor extends BaseExecutor {
|
||||
const outputTokens = metrics.outputTokens || 0;
|
||||
|
||||
if (inputTokens > 0 || outputTokens > 0) {
|
||||
// @ts-ignore
|
||||
state.usage = {
|
||||
prompt_tokens: inputTokens,
|
||||
completion_tokens: outputTokens,
|
||||
@@ -312,31 +304,24 @@ export class KiroExecutor extends BaseExecutor {
|
||||
}
|
||||
|
||||
// Emit final chunk only after receiving BOTH meteringEvent AND contextUsageEvent
|
||||
// @ts-ignore
|
||||
if (state.hasMeteringEvent && state.hasContextUsage && !state.finishEmitted) {
|
||||
state.finishEmitted = true;
|
||||
|
||||
// Estimate tokens if not available from events
|
||||
// @ts-ignore
|
||||
if (!state.usage) {
|
||||
// Estimate output tokens from content length
|
||||
const estimatedOutputTokens =
|
||||
// @ts-ignore
|
||||
state.totalContentLength > 0
|
||||
// @ts-ignore
|
||||
? Math.max(1, Math.floor(state.totalContentLength / 4))
|
||||
: 0;
|
||||
|
||||
// Estimate input tokens from contextUsagePercentage
|
||||
// Kiro models typically have 200k context window
|
||||
const estimatedInputTokens =
|
||||
// @ts-ignore
|
||||
state.contextUsagePercentage > 0
|
||||
// @ts-ignore
|
||||
? Math.floor((state.contextUsagePercentage * 200000) / 100)
|
||||
: 0;
|
||||
|
||||
// @ts-ignore
|
||||
state.usage = {
|
||||
prompt_tokens: estimatedInputTokens,
|
||||
completion_tokens: estimatedOutputTokens,
|
||||
@@ -359,7 +344,6 @@ export class KiroExecutor extends BaseExecutor {
|
||||
};
|
||||
|
||||
// Include usage in final chunk if available
|
||||
// @ts-ignore
|
||||
if (state.usage) {
|
||||
// @ts-ignore
|
||||
finishChunk.usage = state.usage;
|
||||
|
||||
@@ -56,22 +56,17 @@ export function translateNonStreamingResponse(responseBody, targetFormat, source
|
||||
}
|
||||
}
|
||||
|
||||
const message = { role: "assistant" };
|
||||
const message: Record<string, any> = { role: "assistant" };
|
||||
if (textContent) {
|
||||
// @ts-ignore
|
||||
message.content = textContent;
|
||||
}
|
||||
if (reasoningContent) {
|
||||
// @ts-ignore
|
||||
message.reasoning_content = reasoningContent;
|
||||
}
|
||||
if (toolCalls.length > 0) {
|
||||
// @ts-ignore
|
||||
message.tool_calls = toolCalls;
|
||||
}
|
||||
// @ts-ignore
|
||||
if (!message.content && !message.tool_calls) {
|
||||
// @ts-ignore
|
||||
message.content = "";
|
||||
}
|
||||
|
||||
@@ -172,23 +167,18 @@ export function translateNonStreamingResponse(responseBody, targetFormat, source
|
||||
}
|
||||
|
||||
// Build OpenAI format message
|
||||
const message = { role: "assistant" };
|
||||
const message: Record<string, any> = { role: "assistant" };
|
||||
if (textContent) {
|
||||
// @ts-ignore
|
||||
message.content = textContent;
|
||||
}
|
||||
if (reasoningContent) {
|
||||
// @ts-ignore
|
||||
message.reasoning_content = reasoningContent;
|
||||
}
|
||||
if (toolCalls.length > 0) {
|
||||
// @ts-ignore
|
||||
message.tool_calls = toolCalls;
|
||||
}
|
||||
// If no content at all, set content to empty string
|
||||
// @ts-ignore
|
||||
if (!message.content && !message.tool_calls) {
|
||||
// @ts-ignore
|
||||
message.content = "";
|
||||
}
|
||||
|
||||
@@ -258,22 +248,17 @@ export function translateNonStreamingResponse(responseBody, targetFormat, source
|
||||
}
|
||||
}
|
||||
|
||||
const message = { role: "assistant" };
|
||||
const message: Record<string, any> = { role: "assistant" };
|
||||
if (textContent) {
|
||||
// @ts-ignore
|
||||
message.content = textContent;
|
||||
}
|
||||
if (thinkingContent) {
|
||||
// @ts-ignore
|
||||
message.reasoning_content = thinkingContent;
|
||||
}
|
||||
if (toolCalls.length > 0) {
|
||||
// @ts-ignore
|
||||
message.tool_calls = toolCalls;
|
||||
}
|
||||
// @ts-ignore
|
||||
if (!message.content && !message.tool_calls) {
|
||||
// @ts-ignore
|
||||
message.content = "";
|
||||
}
|
||||
|
||||
|
||||
@@ -44,12 +44,10 @@ export function parseSSEToOpenAIResponse(rawSSE, fallbackModel) {
|
||||
}
|
||||
}
|
||||
|
||||
const message = {
|
||||
role: "assistant",
|
||||
const message: Record<string, any> = { role: "assistant",
|
||||
content: contentParts.join(""),
|
||||
};
|
||||
if (reasoningParts.length > 0) {
|
||||
// @ts-ignore
|
||||
message.reasoning_content = reasoningParts.join("");
|
||||
}
|
||||
|
||||
|
||||
@@ -100,7 +100,6 @@ export function resolveNestedComboModels(combo, allCombos, visited = new Set(),
|
||||
if (visited.has(combo.name)) return []; // cycle safety
|
||||
visited.add(combo.name);
|
||||
|
||||
// @ts-ignore
|
||||
const combos = Array.isArray(allCombos) ? allCombos : allCombos?.combos || [];
|
||||
const resolved = [];
|
||||
|
||||
|
||||
@@ -199,17 +199,14 @@ function convertContent(content) {
|
||||
|
||||
// Assistant with tool calls
|
||||
if (toolCalls.length > 0) {
|
||||
const msg = { role: "assistant" };
|
||||
const msg: Record<string, any> = { role: "assistant" };
|
||||
if (textParts.length > 0) {
|
||||
// @ts-ignore
|
||||
msg.content =
|
||||
textParts.length === 1 && textParts[0].type === "text" ? textParts[0].text : textParts;
|
||||
}
|
||||
if (reasoningContent) {
|
||||
// @ts-ignore
|
||||
msg.reasoning_content = reasoningContent;
|
||||
}
|
||||
// @ts-ignore
|
||||
msg.tool_calls = toolCalls;
|
||||
return msg;
|
||||
}
|
||||
|
||||
@@ -190,12 +190,10 @@ function convertClaudeMessage(msg) {
|
||||
|
||||
// If has tool calls, return assistant message with tool_calls
|
||||
if (toolCalls.length > 0) {
|
||||
const result = { role: "assistant" };
|
||||
const result: Record<string, any> = { role: "assistant" };
|
||||
if (parts.length > 0) {
|
||||
// @ts-ignore
|
||||
result.content = parts.length === 1 && parts[0].type === "text" ? parts[0].text : parts;
|
||||
}
|
||||
// @ts-ignore
|
||||
result.tool_calls = toolCalls;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -121,12 +121,10 @@ function convertGeminiContent(content) {
|
||||
}
|
||||
|
||||
if (toolCalls.length > 0) {
|
||||
const result = { role: "assistant" };
|
||||
const result: Record<string, any> = { role: "assistant" };
|
||||
if (parts.length > 0) {
|
||||
// @ts-ignore
|
||||
result.content = parts.length === 1 ? parts[0].text : parts;
|
||||
}
|
||||
// @ts-ignore
|
||||
result.tool_calls = toolCalls;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -66,31 +66,26 @@ function convertMessages(messages) {
|
||||
|
||||
// Keep tool_calls structure for assistant messages
|
||||
if (msg.role === "assistant" && msg.tool_calls && msg.tool_calls.length > 0) {
|
||||
const assistantMsg = { role: "assistant" };
|
||||
const assistantMsg: Record<string, any> = { role: "assistant" };
|
||||
if (content) {
|
||||
// @ts-ignore
|
||||
assistantMsg.content = content;
|
||||
}
|
||||
// @ts-ignore
|
||||
assistantMsg.tool_calls = msg.tool_calls;
|
||||
|
||||
// Attach pending tool results to assistant message with tool_calls
|
||||
if (pendingToolResults.length > 0) {
|
||||
// @ts-ignore
|
||||
assistantMsg.tool_results = pendingToolResults;
|
||||
pendingToolResults = [];
|
||||
}
|
||||
|
||||
result.push(assistantMsg);
|
||||
} else if (content || pendingToolResults.length > 0) {
|
||||
const msgObj = {
|
||||
role: msg.role,
|
||||
const msgObj: Record<string, any> = { role: msg.role,
|
||||
content: content || "",
|
||||
};
|
||||
|
||||
// Attach pending tool results to this message
|
||||
if (pendingToolResults.length > 0) {
|
||||
// @ts-ignore
|
||||
msgObj.tool_results = pendingToolResults;
|
||||
pendingToolResults = [];
|
||||
}
|
||||
|
||||
@@ -442,7 +442,6 @@ export function createSSETransformStreamWithLogger(
|
||||
sourceFormat,
|
||||
provider,
|
||||
reqLogger,
|
||||
// @ts-ignore
|
||||
toolNameMap,
|
||||
model,
|
||||
connectionId,
|
||||
|
||||
Reference in New Issue
Block a user