import path from "node:path";
import { fileURLToPath } from "node:url";
const skillBaseSharedRoot = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(skillBaseSharedRoot, "../../../../..");
export function skillBaseToolSkillDocPath(toolId) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "skillBase", toolId, `${toolId}.md`)
        .split(path.sep)
        .join("/");
}
export function skillBaseToolSourcePath(toolId) {
    return path
        .join(repoRoot, "src", "agentCore", "agent_executionEngine", "basic_toolLayer", "baseTools", "skillBase", `${toolId}.ts`)
        .split(path.sep)
        .join("/");
}
export function skillBaseToolStoragePracticePath(toolId) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "skillBase", toolId, "bestPractice.ts")
        .split(path.sep)
        .join("/");
}
export function jsonSchema(name, schema) {
    return { kind: "json-schema", name, schema };
}
export function createSkillBaseToolDefinition(options) {
    return {
        toolId: options.toolId,
        source: "builtin",
        family: "skill",
        group: "(flat)",
        title: options.title,
        description: options.description,
        toolSkill: {
            docPath: skillBaseToolSkillDocPath(options.toolId),
            summary: options.summary,
            riskLevel: options.riskLevel ?? "normal",
        },
        inputSchema: options.inputSchema ?? jsonSchema(`${options.toolId}.input`, { type: "object", additionalProperties: true }),
        outputSchema: options.outputSchema ?? jsonSchema(`${options.toolId}.output`, { type: "object", additionalProperties: true }),
        riskLevel: options.riskLevel ?? "normal",
        permissionHints: [...options.permissionHints],
        dependencies: options.dependencies,
        storagePolicy: options.storagePolicy ?? {
            storesMaterial: true,
            storesResult: true,
            storesAudit: true,
            reusable: true,
        },
        sourcePath: skillBaseToolSourcePath(options.toolId),
        metadata: {
            storagePracticePath: skillBaseToolStoragePracticePath(options.toolId),
            ...(options.metadata ?? {}),
        },
    };
}
export function buildSkillBasePracticeAuditMetadata(selection) {
    return {
        selectedPractice: selection.providerName,
        selectedPracticeSource: selection.sourceLabel,
        selectedPracticeSourceKind: selection.sourceKind,
        selectedPracticeSourcePath: selection.sourcePath,
        directCliSupport: selection.directCliSupport,
        sideEffectPolicy: selection.sideEffectPolicy,
        notes: selection.notes ?? [],
    };
}
export function adaptSkillToolResultToInvokeResult(result) {
    if (!result.ok) {
        return {
            ok: false,
            toolId: result.toolId,
            error: {
                code: result.error.code,
                message: result.error.message,
                publicSafe: true,
            },
            events: result.events,
        };
    }
    return {
        ok: true,
        toolId: result.toolId,
        output: result.output,
        events: result.events,
        metadata: {
            audit: result.audit,
        },
    };
}
export function createSkillBaseCoreHandler(definition, invokeCore) {
    return {
        definition,
        async invoke(request) {
            return adaptSkillToolResultToInvokeResult(await invokeCore(request));
        },
    };
}
export function injectRuntimeInvocationMetadata(metadata, auditMetadata, request) {
    return {
        ...(auditMetadata ?? {}),
        runtimeId: request.runtimeId,
        sessionId: request.sessionId,
        toolCallId: request.toolCallId,
        ...(metadata ?? {}),
    };
}
