import path from "node:path";
import { fileURLToPath } from "node:url";
const omniSharedRoot = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(omniSharedRoot, "../../../../../");
export function jsonSchema(name, schema) {
    return { kind: "json-schema", name, schema };
}
export function omniToolSkillDocPath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "omniBase", storageGroup, toolId, `${toolId}.md`)
        .split(path.sep)
        .join("/");
}
export function omniToolSourcePath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "agentCore", "agent_executionEngine", "basic_toolLayer", "baseTools", "omniBase", storageGroup, `${toolId}.ts`)
        .split(path.sep)
        .join("/");
}
export function omniToolStoragePracticePath(toolId, storageGroup) {
    return path
        .join(repoRoot, "src", "storagePool", "baseToolStorage", "omniBase", storageGroup, toolId, "bestPractice.ts")
        .split(path.sep)
        .join("/");
}
export function createOmniBaseToolDefinition(options) {
    return {
        toolId: options.toolId,
        source: "builtin",
        family: "omni",
        group: options.storageGroup,
        title: options.title,
        description: options.description,
        toolSkill: {
            docPath: omniToolSkillDocPath(options.toolId, options.storageGroup),
            summary: options.summary,
            riskLevel: options.riskLevel ?? "risky",
        },
        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 ?? "risky",
        permissionHints: [...options.permissionHints],
        dependencies: options.dependencies,
        storagePolicy: options.storagePolicy ?? {
            storesMaterial: true,
            storesResult: true,
            storesAudit: true,
            reusable: false,
        },
        sourcePath: omniToolSourcePath(options.toolId, options.storageGroup),
        metadata: {
            storagePracticePath: omniToolStoragePracticePath(options.toolId, options.storageGroup),
            ...(options.metadata ?? {}),
        },
    };
}
export function orderOmniPractices(practices, preferredProvider) {
    if (preferredProvider === undefined)
        return practices;
    return [
        ...practices.filter((practice) => practice.providerName === preferredProvider),
        ...practices.filter((practice) => practice.providerName !== preferredProvider),
    ];
}
export function selectOmniProviderPractice(practices, dependencies, fallbackPractice) {
    const ordered = orderOmniPractices(practices, dependencies.preferredProvider);
    for (const practice of ordered) {
        const provider = practice.createProvider(dependencies);
        if (provider !== undefined) {
            return { providerName: practice.providerName, practice, provider };
        }
    }
    return {
        providerName: fallbackPractice.providerName,
        practice: fallbackPractice,
        provider: fallbackPractice.createProvider(dependencies),
    };
}
export function buildOmniPracticeAuditMetadata(selection) {
    return {
        selectedPractice: selection.providerName,
        selectedPracticeSource: selection.practice.source.label,
        selectedPracticeSourceKind: selection.practice.source.kind,
        selectedPracticeSourcePath: selection.practice.source.path,
        directCliSupport: selection.practice.directCliSupport,
        sideEffectPolicy: selection.practice.sideEffectPolicy,
        notes: selection.practice.notes,
    };
}
export function injectRuntimeInvocationMetadata(base, existing, request) {
    return {
        ...base,
        ...(existing ?? {}),
        ...(request.metadata ?? {}),
        runtimeId: request.runtimeId,
        sessionId: request.sessionId,
        toolCallId: request.toolCallId,
    };
}
export function adaptOmniCoreResult(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: result.audit === undefined ? undefined : { audit: result.audit },
    };
}
export function createOmniCoreHandler(definition, invokeCore) {
    return {
        definition,
        async invoke(request) {
            return adaptOmniCoreResult(await invokeCore(request));
        },
    };
}
