Skip to content

Commit 4795597

Browse files
Akshayclaude
authored andcommitted
refactor(answer_commit): if/else for answer_only coercion, add docstrings (AltimateAI#1700)
Addresses review: replace the nested ternary with if/else (same behaviour; existing answer_only tests cover every coerced value) and document normalizeAnswerArgs and decodeJsonShaped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WyL6JbJvKGuJD8gT1BWVx3
1 parent 2cde866 commit 4795597

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎packages/opencode/src/altimate/tools/answer-commit.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export type AnswerCommitInput = z.input<typeof parameters>
188188
// JSON and let the schema validate the result as usual; anything else is left for the normal error.
189189
const JSON_SHAPED_KEYS = ["fields", "asked", "rows", "unresolved", "readings"] as const
190190

191+
/** Parse a string that looks like a JSON array or object; return any other value, or invalid JSON, unchanged. */
191192
function decodeJsonShaped(value: unknown): unknown {
192193
if (typeof value !== "string" || !/^\s*[[{]/.test(value)) return value
193194
try {
@@ -199,6 +200,10 @@ function decodeJsonShaped(value: unknown): unknown {
199200
// altimate_change end
200201

201202
// altimate_change start — #1700: exported so raw-argument readers (MCP adapter/server) see the same decoded shape
203+
/**
204+
* Normalize raw answer_commit arguments before validation: decode JSON-string list/object inputs
205+
* and coerce string or numeric `answer_only` flags. Non-object input is returned unchanged.
206+
*/
202207
export function normalizeAnswerArgs(input: unknown): unknown {
203208
if (!input || typeof input !== "object" || Array.isArray(input)) return input
204209
const next: Record<string, unknown> = { ...input }
@@ -207,8 +212,11 @@ export function normalizeAnswerArgs(input: unknown): unknown {
207212
}
208213
if (!("answer_only" in next)) return next
209214
const value = typeof next.answer_only === "string" ? next.answer_only.toLowerCase() : next.answer_only
210-
next.answer_only =
211-
value === "true" || value === 1 ? true : value === "false" || value === 0 ? false : next.answer_only
215+
if (value === "true" || value === 1) {
216+
next.answer_only = true
217+
} else if (value === "false" || value === 0) {
218+
next.answer_only = false
219+
}
212220
return next
213221
}
214222

0 commit comments

Comments
 (0)