import { requestJSON } from './http-client.mjs';
try {
const model = process.env.API_MODEL;
if (!model?.trim()) throw new Error('Set model.');
const original = 'export function greet(name) {\n return `Hello, ${name}!`;\n}\n';
const result = await requestJSON('/v1/chat/completions', {
model,
messages: [
{ role: 'system', content: 'Return the complete revised source only, without Markdown fences or explanations.' },
{ role: 'user', content: `Replace Hello with Welcome in this source. Keep everything else unchanged.\n${original}` },
],
prediction: { type: 'content', content: original },
});
const choice = result.choices?.[0];
const message = choice?.message;
if (!Array.isArray(result.choices) || result.choices.length !== 1 || choice?.index !== 0
|| choice.finish_reason !== 'stop' || message?.role !== 'assistant'
|| message.refusal != null || message.tool_calls != null || message.function_call != null
|| typeof message.content !== 'string' || !message.content.trim()) throw new Error('Incomplete text.');
// Generated code is returned for review, never written over source files or executed.
console.log(JSON.stringify({ proposed_source: message.content }));
} catch {
console.error('Prediction example failed. Check native Chat support and completion status. No automatic retry.');
process.exitCode = 1;
}