// tools/contribute-context.ts — request_context and contribute_context tools (SPEC-036)
// request_context: generates a prompt asking the user for reference documentation.
// contribute_context: persists structured knowledge extracted by the agent.
import { knowledgeStore } from '../storage/index.js';
import { detectMissingAreas, buildRequestPrompt } from '../engine/context-merger.js';
import { compactJson } from './output-formatter.js';
// ---------------------------------------------------------------------------
// request_context
// ---------------------------------------------------------------------------
/**
 * Generate a prompt that the agent presents to the user, asking for
 * reference documentation (README, PRD, architecture diagrams, etc.).
 * Sets knowledge.contextRequested = true to prevent repeated prompts.
 */
export async function handleRequestContext(args) {
    const { projectId, force = false } = args;
    try {
        const knowledge = await knowledgeStore.getKnowledge(projectId);
        if (!knowledge) {
            return {
                content: [
                    {
                        type: 'text',
                        text: `Project "${projectId}" not found. Run init_project first.`,
                    },
                ],
                isError: true,
            };
        }
        // Guard: skip if already requested (unless force = true)
        const alreadyRequested = knowledge.contextRequested === true;
        if (alreadyRequested && !force) {
            const result = {
                prompt: '',
                missingAreas: [],
                alreadyRequested: true,
            };
            return { content: [{ type: 'text', text: compactJson(result) }] };
        }
        const missingAreas = detectMissingAreas(knowledge);
        // No missing areas — nothing to ask
        if (missingAreas.length === 0) {
            const result = {
                prompt: 'Project knowledge is complete. No additional documentation needed.',
                missingAreas: [],
                alreadyRequested: false,
            };
            return { content: [{ type: 'text', text: compactJson(result) }] };
        }
        const prompt = buildRequestPrompt(missingAreas, knowledge);
        // Persist the contextRequested flag
        await knowledgeStore.saveKnowledge(projectId, {
            ...knowledge,
            contextRequested: true,
        });
        const result = { prompt, missingAreas, alreadyRequested: false };
        return { content: [{ type: 'text', text: compactJson(result) }] };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `request_context error: ${message}` }],
            isError: true,
        };
    }
}
// ---------------------------------------------------------------------------
// contribute_context
// ---------------------------------------------------------------------------
/**
 * Persist structured knowledge that the agent extracted from user documents.
 * Merges intelligently with existing auto-detected knowledge and reports conflicts.
 */
export async function handleContributeContext(args) {
    const { projectId, context, source } = args;
    // Validate additionalContext length
    if (context.additionalContext && context.additionalContext.length > 10_000) {
        return {
            content: [
                {
                    type: 'text',
                    text: 'additionalContext exceeds maximum length of 10 000 characters. ' +
                        'Please trim or split into multiple contributions.',
                },
            ],
            isError: true,
        };
    }
    try {
        const result = await knowledgeStore.mergeContributedContext(projectId, context, source);
        return { content: [{ type: 'text', text: compactJson(result) }] };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `contribute_context error: ${message}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=contribute-context.js.map