import { anthropicSkillIteratePractice } from "./anthropic.js";
import { deepmindSkillIteratePractice } from "./deepmind.js";
import { openaiSkillIteratePractice } from "./openai.js";
import { buildSkillBasePracticeAuditMetadata, createSkillBaseCoreHandler, createSkillBaseToolDefinition, injectRuntimeInvocationMetadata, jsonSchema } from "../_shared/baseToolAdapter.js";
import { executeSkillIterate as executeSkillIterateCore } from "./core.js";
import { skillIterateDependencyDeclarations } from "./dependencies.js";
export { planSkillIteration, skillIterateDescriptor, executeSkillIterate as executeSkillIterateCore } from "./core.js";
const practices = [anthropicSkillIteratePractice, openaiSkillIteratePractice, deepmindSkillIteratePractice];
function ordered(preferredProvider) {
    return preferredProvider === undefined ? practices : [...practices.filter((practice) => practice.providerName === preferredProvider), ...practices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectSkillIteratePractice(dependencies = {}) {
    for (const practice of ordered(dependencies.preferredProvider)) {
        const provider = practice.createProvider({ executor: dependencies.executor, provider: dependencies.provider });
        if (provider !== undefined)
            return { providerName: practice.providerName, practice, provider };
    }
    return { providerName: "praxis-native", practice: { providerName: "praxis-native", source: { kind: "praxis-native", label: "Praxis dry-run fallback", path: undefined }, directCliSupport: false, sideEffectPolicy: "runtime-governed", notes: ["No runtime filesystem reader/writer is available; dry-run remains available."], createProvider: () => undefined }, provider: undefined };
}
export async function executeSkillIterate(request = {}) {
    const selection = selectSkillIteratePractice(request);
    return executeSkillIterateCore({ ...request, provider: selection.provider, context: { ...request.context, auditMetadata: { ...(request.context?.auditMetadata ?? {}), ...buildSkillBasePracticeAuditMetadata({ providerName: selection.providerName, sourceLabel: selection.practice.source.label, sourceKind: selection.practice.source.kind, sourcePath: selection.practice.source.path, directCliSupport: selection.practice.directCliSupport, sideEffectPolicy: selection.practice.sideEffectPolicy, notes: selection.practice.notes }) } } });
}
export const skillIterateBaseToolDefinition = createSkillBaseToolDefinition({ toolId: "skill.iterate", title: "Skill Iterate", description: "Iterate an existing skill package through governed runtime filesystem support.", summary: "Use skill.iterate to modify SKILL.md and skill support files with a patch-style plan.", riskLevel: "risky", permissionHints: ["skill:write", "filesystem:write"], dependencies: skillIterateDependencyDeclarations, inputSchema: jsonSchema("skill.iterate.input", { type: "object", additionalProperties: true }), outputSchema: jsonSchema("skill.iterate.output", { type: "object", additionalProperties: true }) });
export const skillIterateHandler = createSkillBaseCoreHandler(skillIterateBaseToolDefinition, async (request) => {
    const selection = selectSkillIteratePractice({ ...request.input, executor: request.executor });
    return executeSkillIterateCore({ ...request.input, provider: selection.provider, context: { ...request.input.context, runtimeId: request.input.context?.runtimeId ?? request.runtimeId, sessionId: request.input.context?.sessionId ?? request.sessionId, invocationId: request.input.context?.invocationId ?? request.toolCallId, auditMetadata: injectRuntimeInvocationMetadata(buildSkillBasePracticeAuditMetadata({ providerName: selection.providerName, sourceLabel: selection.practice.source.label, sourceKind: selection.practice.source.kind, sourcePath: selection.practice.source.path, directCliSupport: selection.practice.directCliSupport, sideEffectPolicy: selection.practice.sideEffectPolicy, notes: selection.practice.notes }), request.input.context?.auditMetadata, request) } });
});
