import { completeSimple } from "@threadwell/ai";
import { parseThreadSuiteSynthesisJson } from "./synthesis-schema.js";
export class ThreadSuiteAiSynthesisClient {
    model;
    options;
    constructor(model, options) {
        this.model = model;
        this.options = options;
    }
    async complete(prompt) {
        const message = await completeSimple(this.model, { messages: [{ role: "user", content: [{ type: "text", text: prompt }], timestamp: Date.now() }] }, this.options);
        return message.content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n");
    }
}
export async function synthesizeThreadSuiteProject(evidencePack, client) {
    const raw = await client.complete(buildThreadSuiteSynthesisPrompt(evidencePack));
    try {
        return parseThreadSuiteSynthesisJson(raw, evidencePack.items.map((item) => item.path));
    }
    catch (error) {
        const repaired = await client.complete(buildThreadSuiteRepairPrompt(raw, error));
        return parseThreadSuiteSynthesisJson(repaired, evidencePack.items.map((item) => item.path));
    }
}
export function buildThreadSuiteRepairPrompt(raw, error) {
    return [
        "Repair this ThreadSuite synthesis into strict JSON matching the requested schema.",
        "Return only JSON. Do not add markdown fences.",
        `Validation error: ${error instanceof Error ? error.message : String(error)}`,
        "Raw output:",
        raw,
    ].join("\n\n");
}
export function buildThreadSuiteSynthesisPrompt(evidencePack) {
    return [
        "You are ThreadSuite, a project intelligence bootstrapper for a coding agent.",
        "Analyze the evidence pack and return only strict JSON. Do not use markdown fences.",
        "Every major claim must cite evidence paths from the evidence pack. Use unknowns when evidence is missing.",
        "For complete ThreadSuite, produce audit-grade navigation artifacts: projectMap, retrievalRoutes, riskRegister, and agentPolicy.",
        "Schema:",
        JSON.stringify({
            projectPurpose: "string",
            productSummary: "string",
            architecture: {
                style: "string",
                components: [{ name: "string", responsibility: "string", evidence: ["path"] }],
                dataFlow: ["string"],
                controlFlow: ["string"],
            },
            commands: [{ command: "string", purpose: "string", confidence: "low|medium|high", evidence: ["path"] }],
            entrypoints: [{ path: "string", role: "string", evidence: ["path"] }],
            importantFiles: [{ path: "string", why: "string" }],
            domainConcepts: ["string"],
            risks: [{ risk: "string", reason: "string", evidence: ["path"] }],
            unknowns: [{ question: "string", missingEvidence: "string" }],
            recommendations: ["string"],
            adrCandidates: [
                { title: "string", context: "string", decision: "string", consequences: "string", evidence: ["path"] },
            ],
            memoryCapsules: [{ category: "string", summary: "string", content: "string", evidence: ["path"] }],
            projectMap: {
                components: [
                    {
                        name: "string",
                        responsibility: "string",
                        dependsOn: ["component name"],
                        files: ["path"],
                        evidence: ["path"],
                        confidence: "low|medium|high",
                    },
                ],
                entrypoints: [{ path: "string", role: "string", evidence: ["path"], confidence: "low|medium|high" }],
                coreFiles: [{ path: "string", reason: "string", evidence: ["path"], confidence: "low|medium|high" }],
            },
            retrievalRoutes: [
                {
                    intent: "architecture|bug_triage|feature_work|tests|config_setup|commands|docs|release",
                    firstFiles: ["path"],
                    queryHints: ["string"],
                    rationale: "string",
                    evidence: ["path"],
                    confidence: "low|medium|high",
                },
            ],
            riskRegister: {
                items: [
                    {
                        title: "string",
                        severity: "low|medium|high",
                        reason: "string",
                        evidence: ["path"],
                        confidence: "low|medium|high",
                    },
                ],
                openQuestions: [{ question: "string", whyItMatters: "string", evidence: ["path"] }],
            },
            agentPolicy: {
                safeCommands: ["string"],
                expensiveCommands: ["string"],
                validationExpectations: ["string"],
                conventions: ["string"],
            },
        }, null, 2),
        "Evidence pack:",
        JSON.stringify({
            cwd: evidencePack.cwd,
            items: evidencePack.items.map((item) => ({
                path: item.path,
                kind: item.kind,
                content: item.content,
                truncated: item.truncated,
            })),
            skipped: evidencePack.skipped,
        }, null, 2),
    ].join("\n\n");
}
//# sourceMappingURL=synthesis-service.js.map