import path from "node:path";
import { fileURLToPath } from "node:url";
const gitBaseSharedRoot = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(gitBaseSharedRoot, "../../../../..");
export function gitBaseToolSkillDocPath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "gitBase", storageGroup, toolId, `${toolId}.md`)
        .split(path.sep)
        .join("/");
}
export function gitBaseToolSourcePath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "agentCore", "agent_executionEngine", "basic_toolLayer", "baseTools", "gitBase", storageGroup, `${toolId}.ts`)
        .split(path.sep)
        .join("/");
}
export function gitBaseToolStoragePracticePath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "gitBase", storageGroup, toolId, "bestPractice.ts")
        .split(path.sep)
        .join("/");
}
export function jsonSchema(name, schema) {
    return { kind: "json-schema", name, schema };
}
export function createGitBaseToolDefinition(options) {
    return {
        toolId: options.toolId,
        source: "builtin",
        family: "git",
        group: options.storageGroup,
        title: options.title,
        description: options.description,
        toolSkill: {
            docPath: gitBaseToolSkillDocPath(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: gitBaseToolSourcePath(options.toolId, options.storageGroup),
        metadata: {
            storagePracticePath: gitBaseToolStoragePracticePath(options.toolId, options.storageGroup),
            ...(options.metadata ?? {}),
        },
    };
}
export function buildGitBasePracticeAuditMetadata(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 adaptGitToolResultToInvokeResult(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 createGitBaseCoreHandler(definition, invokeCore) {
    return {
        definition,
        async invoke(request) {
            return adaptGitToolResultToInvokeResult(await invokeCore(request));
        },
    };
}
export function injectRuntimeInvocationMetadata(metadata, auditMetadata, request) {
    return {
        ...(auditMetadata ?? {}),
        runtimeId: request.runtimeId,
        sessionId: request.sessionId,
        toolCallId: request.toolCallId,
        ...(metadata ?? {}),
    };
}
