import path from "node:path";
import { fileURLToPath } from "node:url";
const codeBaseSharedRoot = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(codeBaseSharedRoot, "../../../../..");
export function codeBaseToolSkillDocPath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "codeBase", storageGroup, toolId, `${toolId}.md`)
        .split(path.sep)
        .join("/");
}
export function codeBaseToolSourcePath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "agentCore", "agent_executionEngine", "basic_toolLayer", "baseTools", "codeBase", storageGroup, `${toolId}.ts`)
        .split(path.sep)
        .join("/");
}
export function codeBaseToolStoragePracticePath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "codeBase", storageGroup, toolId, "bestPractice.ts")
        .split(path.sep)
        .join("/");
}
export function jsonSchema(name, schema) {
    return { kind: "json-schema", name, schema };
}
export function createCodeBaseToolDefinition(options) {
    return {
        toolId: options.toolId,
        source: "builtin",
        family: "code",
        group: options.storageGroup,
        title: options.title,
        description: options.description,
        toolSkill: {
            docPath: codeBaseToolSkillDocPath(options.toolId, options.storageGroup),
            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: codeBaseToolSourcePath(options.toolId, options.storageGroup),
        metadata: {
            storagePracticePath: codeBaseToolStoragePracticePath(options.toolId, options.storageGroup),
            ...(options.metadata ?? {}),
        },
    };
}
export function buildCodeBasePracticeAuditMetadata(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 adaptCodeToolResultToInvokeResult(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 createCodeBaseCoreHandler(definition, invokeCore) {
    return {
        definition,
        async invoke(request) {
            return adaptCodeToolResultToInvokeResult(await invokeCore(request));
        },
    };
}
export function injectRuntimeInvocationMetadata(metadata, auditMetadata, request) {
    return {
        ...(auditMetadata ?? {}),
        runtimeId: request.runtimeId,
        sessionId: request.sessionId,
        toolCallId: request.toolCallId,
        ...(metadata ?? {}),
    };
}
