import path from 'node:path';
import os from 'node:os';
import { exists, readText, writeTextAtomic, PACKAGE_VERSION } from './fsx.js';
export const OPENCLAW_SKILL_NAME = 'sneakoscope-codex';
export function defaultOpenClawHome(env = process.env) {
    return path.resolve(env.OPENCLAW_HOME || path.join(env.HOME || os.homedir(), '.openclaw'));
}
export function defaultOpenClawSkillDir(env = process.env) {
    return path.join(defaultOpenClawHome(env), 'skills', OPENCLAW_SKILL_NAME);
}
export function buildOpenClawSkillFiles(options = {}) {
    const sksCommand = options.sksCommand || 'sks';
    const version = options.version || PACKAGE_VERSION;
    const skillName = options.skillName || OPENCLAW_SKILL_NAME;
    return {
        'manifest.yaml': openClawManifest({ skillName, version }),
        'SKILL.md': openClawSkillMarkdown({ sksCommand, skillName, version }),
        'README.md': openClawSkillReadme({ sksCommand, skillName, version }),
        'openclaw-agent-config.example.yaml': openClawAgentConfigExample({ skillName })
    };
}
export async function installOpenClawSkill(options = {}) {
    const targetDir = path.resolve(options.targetDir || defaultOpenClawSkillDir(options.env || process.env));
    const files = buildOpenClawSkillFiles(options);
    const existingManifest = path.join(targetDir, 'manifest.yaml');
    const existing = await exists(existingManifest);
    if (existing && !options.force) {
        const text = await readText(existingManifest, '');
        if (!text.includes('generated_by: sneakoscope')) {
            return {
                ok: false,
                status: 'blocked_existing_skill',
                target_dir: targetDir,
                reason: 'Existing OpenClaw skill is not marked as generated by Sneakoscope. Re-run with --force to overwrite.'
            };
        }
    }
    if (options.dryRun) {
        return { ok: true, status: existing ? 'would_update' : 'would_create', target_dir: targetDir, files: Object.keys(files) };
    }
    for (const [name, content] of Object.entries(files)) {
        await writeTextAtomic(path.join(targetDir, name), content);
    }
    return { ok: true, status: existing ? 'updated' : 'created', target_dir: targetDir, files: Object.keys(files) };
}
function openClawManifest({ skillName, version }) {
    return `name: ${skillName}
version: ${version}
description: Expose Sneakoscope Codex (sks) workflows to OpenClaw agents.
author: mandarange
license: MIT
generated_by: sneakoscope
homepage: https://github.com/mandarange/Sneakoscope-Codex
entrypoint: SKILL.md
permissions:
  shell: execute
  filesystem: read_write_project
tags:
  - codex
  - coding-agent
  - orchestration
  - qa
  - safety
`;
}
function openClawSkillMarkdown({ sksCommand, skillName, version }) {
    return `---
name: ${skillName}
version: ${version}
description: Use Sneakoscope Codex routes, checks, and release gates from an OpenClaw agent.
---

# Sneakoscope Codex For OpenClaw

## LLM Init Prompt For OpenClaw Agent Builders

\`\`\`text
Set up this OpenClaw agent project with Sneakoscope Codex. Use [[mandarange/Sneakoscope-Codex](https://github.com/mandarange/Sneakoscope-Codex)] as the repo reference and [npm i -g sneakoscope] as the recommended global install command, then run \`SKS_OPENCLAW=1 ${sksCommand} root\`, setup/doctor, Codex App, and proof checks for this repo.
\`\`\`

Use this skill when the user asks an OpenClaw agent to work in a codebase that uses Sneakoscope Codex, SKS, Team, QA-LOOP, TriWiki, Context7, DB safety, Honest Mode, release gates, or \`$...\` prompt routes.

## Required Tool Access

The OpenClaw agent must have the built-in \`shell\` tool enabled. Prefer running commands in the target repository root.

Set \`SKS_OPENCLAW=1\` for SKS shell commands. In OpenClaw mode, SKS treats update/install prompts as automatically approved so \`sks\` can update \`sneakoscope\` or \`@openai/codex\` before launching Zellij without pausing for a human \`Y/n\` answer.

## Core Commands

- \`${sksCommand} root\` checks whether SKS is using a project root or the global runtime root.
- \`${sksCommand} bootstrap\` installs or repairs SKS project files, Codex App skills, hooks, and runtime state.
- \`${sksCommand} commands\` lists terminal commands the agent can call.
- \`${sksCommand} dollar-commands\` lists prompt routes such as \`$Team\`, \`$DFix\`, \`$QA-LOOP\`, \`$PPT\`, \`$Goal\`, \`$DB\`, \`$Wiki\`, and \`$Help\`.
- \`${sksCommand} deps check\` checks Node/npm path readiness; \`${sksCommand} doctor\` checks Codex, Context7, Zellij, and related local readiness.
- \`${sksCommand} proof-field scan --intent "<task>" --changed file1,file2\` checks whether a narrow change can stay on a lightweight proof path.
- \`${sksCommand} release:check\` is not a terminal command; use \`npm run release:check\` inside the Sneakoscope package repository.

## Agent Operating Rules

1. Before substantive work, run \`SKS_OPENCLAW=1 ${sksCommand} root\` and inspect the repository's \`AGENTS.md\` if present.
2. For implementation, prefer the repository's requested SKS route. General code work and broad implementation normally route to \`$Team\`; Direct Fix work, meaning tiny copy/config/docs/labels/spacing/translation/simple mechanical edits, can use \`$DFix\`; UI design specifics follow the repo's UI/design route rules; web UI/browser dogfood uses \`$QA-LOOP\` with the Codex Chrome Extension gate first; native Mac/non-web visual work uses \`$Computer-Use\`; database or Supabase work uses \`$DB\`.
3. Do not invent fallback implementation code when the requested SKS path is blocked. Report the blocker with command output and source paths.
4. For database, migration, and Supabase tasks, default to read-only inspection unless the user explicitly authorizes a write/migration scope.
5. Before claiming completion, run the most relevant verification command and summarize what passed, what was not verified, and any remaining blocker.

## Example Prompts For An OpenClaw Agent

- "In this repo, run \`${sksCommand} root\`, inspect \`AGENTS.md\`, then use the SKS Team route to implement the bug fix and verify it."
- "Use SKS QA-LOOP against localhost:3000. Do not run destructive tests."
- "Prepare this project for Codex App use: run \`${sksCommand} bootstrap\`, \`${sksCommand} codex-app check\`, and \`${sksCommand} dollar-commands\`."
`;
}
function openClawSkillReadme({ sksCommand, skillName, version }) {
    return `# ${skillName}

Version: ${version}

This OpenClaw skill lets an OpenClaw agent discover and use Sneakoscope Codex through the local \`${sksCommand}\` command.

OpenClaw agents should set \`SKS_OPENCLAW=1\` when running SKS commands. That mode auto-approves SKS dependency/update prompts, including the Codex CLI update preflight before Zellij launch.

## LLM Init Prompt

\`\`\`text
Set up this OpenClaw agent project with Sneakoscope Codex. Use [[mandarange/Sneakoscope-Codex](https://github.com/mandarange/Sneakoscope-Codex)] as the repo reference and [npm i -g sneakoscope] as the recommended global install command, then run \`SKS_OPENCLAW=1 ${sksCommand} root\`, setup/doctor, Codex App, and proof checks for this repo.
\`\`\`

## Install

\`\`\`sh
${sksCommand} openclaw install
\`\`\`

Then attach the skill to an OpenClaw agent:

\`\`\`yaml
agents:
  coding-agent:
    tools:
      - shell
    skills:
      - ${skillName}
\`\`\`

## Verify

\`\`\`sh
openclaw skill run ${skillName} --help
${sksCommand} root
${sksCommand} commands
\`\`\`

If OpenClaw runs skills in a sandbox, allow shell execution for this skill and run it from the target repository root so SKS can find \`.git\`, \`.sneakoscope\`, or \`.dcodex\`.
`;
}
function openClawAgentConfigExample({ skillName }) {
    return `agents:
  coding-agent:
    model: openai/gpt-4o
    channels:
      - terminal
    tools:
      - shell
    env:
      SKS_OPENCLAW: "1"
    skills:
      - ${skillName}
`;
}
//# sourceMappingURL=openclaw.js.map