/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { readFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
const MAX_CONTEXT_BYTES = 32 * 1024;
const TOOLING_CONTRACT = `
## Tooling Contract (MANDATORY)

You are operating inside the setup-agents web console. All workspace operations MUST use the \`sf setup-agents\` CLI.

### Required Commands
- \`sf setup-agents setup\` — initialize or reconfigure a workspace
- \`sf setup-agents dashboard\` — generate insights dashboard
- \`sf setup-agents serve\` — start the bridge server
- \`sf setup-agents insights\` — run analysis passes

### Rules
- ALWAYS use \`sf setup-agents <command>\` for any workspace operation.
- NEVER substitute with raw file writes, manual config edits, or other CLIs for tasks that sf setup-agents handles.
- If you need to run a setup-agents command, use the full \`sf setup-agents\` prefix.
- You may use other tools (git, npm, sf) for tasks outside the scope of setup-agents.
`;
export function buildContext(workspace) {
    const parts = [];
    parts.push(TOOLING_CONTRACT);
    appendConfigSection(workspace, parts);
    appendDocSection(workspace, parts);
    const projectName = resolveProjectName(workspace);
    const header = `Project: ${projectName}\nWorkspace: ${workspace}`;
    const full = [header, ...parts].join('\n');
    if (Buffer.byteLength(full, 'utf8') > MAX_CONTEXT_BYTES) {
        return full.slice(0, MAX_CONTEXT_BYTES);
    }
    return full;
}
function appendConfigSection(workspace, parts) {
    const configPath = join(workspace, '.setup-agents', 'config.json');
    if (!existsSync(configPath))
        return;
    try {
        const raw = readFileSync(configPath, 'utf8');
        const config = JSON.parse(raw);
        const profileNames = config.profiles?.map((p) => p.name).join(', ') ?? 'none';
        const tools = config.tools?.join(', ') ?? 'none';
        parts.push(`Profiles: ${profileNames}`);
        parts.push(`Tools: ${tools}`);
    }
    catch {
        // config unreadable, skip
    }
}
function appendDocSection(workspace, parts) {
    const docCandidates = ['CLAUDE.md', 'AGENTS.md'];
    for (const filename of docCandidates) {
        const docPath = join(workspace, filename);
        if (!existsSync(docPath))
            continue;
        try {
            const content = readFileSync(docPath, 'utf8');
            parts.push(`\n--- ${filename} ---\n${content}`);
        }
        catch {
            // unreadable, skip
        }
        break; // only include the first found
    }
}
function resolveProjectName(workspace) {
    const pkgPath = join(workspace, 'package.json');
    if (!existsSync(pkgPath))
        return workspace.split('/').pop() ?? 'project';
    try {
        const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
        return pkg.name ?? workspace.split('/').pop() ?? 'project';
    }
    catch {
        return workspace.split('/').pop() ?? 'project';
    }
}
//# sourceMappingURL=context-builder.js.map