Repository navigation
refactor(translator): extract pure message helpers from openai-to-kiro (852→751) #5947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 2 commits into
release/v3.8.44
from
refactor/split-openai-to-kiro-helpers
Jul 2, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
open-sse/translator/request/openai-to-kiro/messageHelpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Pure message/tool helpers for the OpenAI -> Kiro request translator. | ||
| // Extracted verbatim from openai-to-kiro.ts (no host imports). | ||
|
|
||
| export function parseToolInput(value: unknown) { | ||
| if (value && typeof value === "object" && !Array.isArray(value)) { | ||
| return value; | ||
| } | ||
| if (typeof value !== "string") { | ||
| return {}; | ||
| } | ||
|
|
||
| const trimmed = value.trim(); | ||
| if (!trimmed) { | ||
| return {}; | ||
| } | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(trimmed); | ||
| return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {}; | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Recursively sanitize JSON Schema for Kiro API. | ||
| * Kiro returns 400 "Improperly formed request" if: | ||
| * - `required` is an empty array [] | ||
| * - `additionalProperties` is present anywhere | ||
| */ | ||
| export function normalizeKiroToolSchema(schema: unknown): Record<string, unknown> { | ||
| if (!schema || typeof schema !== "object" || Array.isArray(schema)) { | ||
| return { type: "object", properties: {} }; | ||
| } | ||
|
|
||
| const result: Record<string, unknown> = {}; | ||
| const src = schema as Record<string, unknown>; | ||
|
|
||
| for (const [key, value] of Object.entries(src)) { | ||
| // Skip empty required arrays — Kiro rejects them | ||
| if (key === "required" && Array.isArray(value) && value.length === 0) { | ||
| continue; | ||
| } | ||
| // Skip additionalProperties — Kiro doesn't support it | ||
| if (key === "additionalProperties") { | ||
| continue; | ||
| } | ||
| // Recursively process nested objects | ||
| if ( | ||
| key === "properties" && | ||
| typeof value === "object" && | ||
| value !== null && | ||
| !Array.isArray(value) | ||
| ) { | ||
| const sanitizedProps: Record<string, unknown> = {}; | ||
| for (const [propName, propValue] of Object.entries(value as Record<string, unknown>)) { | ||
| sanitizedProps[propName] = normalizeKiroToolSchema(propValue); | ||
| } | ||
| result[key] = sanitizedProps; | ||
| } else if (typeof value === "object" && value !== null && !Array.isArray(value)) { | ||
| result[key] = normalizeKiroToolSchema(value); | ||
| } else if (Array.isArray(value)) { | ||
| result[key] = value.map((item) => | ||
| typeof item === "object" && item !== null && !Array.isArray(item) | ||
| ? normalizeKiroToolSchema(item) | ||
| : item | ||
| ); | ||
| } else { | ||
| result[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| export function serializeToolResultContent(content: unknown): string { | ||
| if (typeof content === "string") { | ||
| return content || "(no output)"; | ||
| } | ||
| if (!Array.isArray(content)) { | ||
| if (content !== null && content !== undefined) { | ||
| try { | ||
| return JSON.stringify(content); | ||
| } catch { | ||
| return "(no output)"; | ||
| } | ||
| } | ||
| return "(no output)"; | ||
| } | ||
| const parts: string[] = []; | ||
| for (const block of content as Array<Record<string, unknown>>) { | ||
| if (!block || typeof block !== "object") continue; | ||
| if (block.type === "text" && typeof block.text === "string") { | ||
| if (block.text) parts.push(block.text); | ||
| } else if (block.type === "image" || block.type === "image_url") { | ||
| const src = block.source as Record<string, unknown> | undefined; | ||
| const mediaType = src?.media_type ?? block.media_type ?? "image"; | ||
| parts.push(`[image: ${mediaType}]`); | ||
| } else { | ||
| try { | ||
| const str = JSON.stringify(block); | ||
| if (str && str !== "{}") parts.push(str); | ||
| } catch { | ||
| // skip unserializable block | ||
| } | ||
| } | ||
| } | ||
| return parts.join("\n") || "(no output)"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { dirname, join } from "node:path"; | ||
|
|
||
| // Split-guard for the openai-to-kiro message-helper extraction. | ||
| // The pure tool/message helpers (parseToolInput / normalizeKiroToolSchema / | ||
| // serializeToolResultContent) live in the leaf `openai-to-kiro/messageHelpers.ts`; | ||
| // the host imports them back for convertMessages. They were module-private, so the | ||
| // public export set is unchanged (no re-export needed). | ||
| const HERE = dirname(fileURLToPath(import.meta.url)); | ||
| const REQ = join(HERE, "../../open-sse/translator/request"); | ||
| const HOST = join(REQ, "openai-to-kiro.ts"); | ||
| const LEAF = join(REQ, "openai-to-kiro/messageHelpers.ts"); | ||
|
|
||
| test("leaf hosts the pure helpers and does not import the host", () => { | ||
| const src = readFileSync(LEAF, "utf8"); | ||
| for (const sym of ["parseToolInput", "normalizeKiroToolSchema", "serializeToolResultContent"]) { | ||
| assert.match(src, new RegExp(`export function ${sym}\\b`)); | ||
| } | ||
| assert.doesNotMatch(src, /from "\.\.\/openai-to-kiro\.ts"/); | ||
| }); | ||
|
|
||
| test("host imports the helpers back from the leaf", () => { | ||
| const src = readFileSync(HOST, "utf8"); | ||
| assert.match(src, /from "\.\/openai-to-kiro\/messageHelpers\.ts"/); | ||
| // buildKiroPayload stays exported on the host module. | ||
| assert.match(src, /export function buildKiroPayload\(/); | ||
| }); | ||
|
|
||
| test("normalizeKiroToolSchema strips empty required arrays and additionalProperties", async () => { | ||
| const { normalizeKiroToolSchema } = | ||
| await import("../../open-sse/translator/request/openai-to-kiro/messageHelpers.ts"); | ||
| const out = normalizeKiroToolSchema({ | ||
| type: "object", | ||
| required: [], | ||
| additionalProperties: false, | ||
| properties: { a: { type: "string" } }, | ||
| }); | ||
| assert.equal("required" in out, false); | ||
| assert.equal("additionalProperties" in out, false); | ||
| assert.deepEqual(out.properties, { a: { type: "string" } }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quando
contenté um array, qualquer elemento primitivo (como strings, números ou booleanos) é atualmente ignorado devido à verificação estritatypeof block !== "object". Isso resulta em uma serialização vazia ("(no output)") para arrays de primitivos. Tratar elementos primitivos diretamente, convertendo-os para string, melhora a robustez da função.