import Ajv from "ajv";
//#region src/agent/workflow/structured-output-tool.ts
/**
* `structured_output` — terminating tool used to capture a subagent's final result.
*
* Injected into a subagent's toolset only when the workflow script asked for
* structured output via `agent(prompt, { schema })`. The subagent is instructed
* to invoke this tool exactly once with its final result; the tool sets
* `terminate: true` so the agent loop stops without paying for an extra
* assistant turn.
*
* Validation is done with ajv (already a xopc dep). pi-agent-core also validates
* arguments against `parameters` before calling `execute`, but we re-check here
* to (1) surface a clear error message back to the model when the schema is
* non-trivial, and (2) avoid trusting the upstream layer's validator strictness.
*
* Note: the file is named `structured-output-tool.ts` to avoid clashing with the
* existing `src/agent/tools/structured-output.ts` (an XML Element builder — a
* completely unrelated utility).
*/
const STRUCTURED_OUTPUT_TOOL_NAME = "structured_output";
const ajv = new Ajv({
	allErrors: true,
	strict: false
});
function createStructuredOutputTool(options) {
	const name = options.name ?? "structured_output";
	const validator = ajv.compile(options.schema);
	return {
		name,
		label: "Structured Output",
		description: "Return the final machine-readable result for this subagent task. Call this tool exactly once when finished. Do not emit a prose final answer afterwards.",
		parameters: options.schema,
		async execute(_toolCallId, params) {
			if (!validator(params)) return {
				content: [{
					type: "text",
					text: `structured_output: invalid arguments — ${ajv.errorsText(validator.errors, { separator: "; " })}. Adjust and call structured_output again.`
				}],
				details: params
			};
			options.capture.called = true;
			options.capture.value = params;
			return {
				content: [{
					type: "text",
					text: "Structured output received."
				}],
				details: params,
				terminate: true
			};
		}
	};
}
//#endregion
export { STRUCTURED_OUTPUT_TOOL_NAME, createStructuredOutputTool };

//# sourceMappingURL=structured-output-tool.js.map