zenstory-ai/oh-story-claudecode / skills/story-setup
story-setup
Flagged网文写作工具集基础设施部署与检查。为 Claude Code / OpenCode / Codex / Google Antigravity / ZCode / OpenClaw / Reasonix 提供内置适配;Web AI / 通用 Agent 可走 skills + AGENTS.md 文件模式。触发方式:/story-setup、$story-setup、「准备写书」「帮我搭一下环境」「配置写作项目」「检查写作环境」。
references/zcode/hooks/story_zcode_hook.js
View on GitHub#!/usr/bin/env node
"use strict"
// ZCode hook adapter for oh-story writing projects. It has no third-party
// dependencies and emits only fields accepted by ZCode 3.3.4's strict hook
// output schema. Diagnostics go to stderr; a healthy no-op keeps stdout empty.
const fs = require("node:fs")
const path = require("node:path")
const { spawnSync } = require("node:child_process")
const core = require("./story_hook_core.js")
const {
existingDir,
safeRelative,
resolveTarget,
firstLine,
findFirst,
discoverActiveBook,
discoverAllBooks,
continuityFindings,
extractProseTargets,
extractPatchTargets,
proseBlockReason,
isProsePath,
duplicateTitleFindings,
proseAfterWrite,
shellWords,
isGitCommitCommand,
stagedMarkdownWarnings,
skippableLine,
proseNetFindings,
} = core
let hookInput = {}
try {
const raw = fs.readFileSync(0, "utf8")
if (raw.trim()) {
const parsed = JSON.parse(raw)
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) hookInput = parsed
}
} catch {
hookInput = {}
}
function emit(value) {
if (value && typeof value === "object") process.stdout.write(JSON.stringify(value))
}
function hookContext(event, text) {
return {
hookSpecificOutput: {
hookEventName: event,
additionalContext: text,
},
}
}
function deployedWorkspaceRoot() {
try {
const hooksDir = __dirname
if (path.basename(hooksDir) === "hooks" && path.basename(path.dirname(hooksDir)) === ".zcode") {
return path.dirname(path.dirname(hooksDir))
}
} catch {}
return null
}
function projectRoot() {
for (const name of ["ZCODE_PROJECT_DIR", "CLAUDE_PROJECT_DIR"]) {
const candidate = existingDir(process.env[name])
if (candidate) return candidate
}
const deployed = deployedWorkspaceRoot()
if (deployed) return deployed
const inputCwd = existingDir(hookInput.cwd)
const cwd = inputCwd || process.cwd()
try {
const result = spawnSync("git", ["rev-parse", "--show-toplevel"], {
cwd,
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
})
if (result.status === 0 && result.stdout.trim()) return path.resolve(result.stdout.trim())
} catch {}
return path.resolve(cwd)
}
function sessionStart() {
const root = projectRoot()
const messages = []
const sentinel = path.join(root, ".story-deployed")
if (fs.existsSync(sentinel)) {
let text = ""
try { text = fs.readFileSync(sentinel, "utf8") } catch {}
const match = text.match(/^target_cli:\s*(.+)$/m)
if (!match) {
messages.push("[story-setup] .story-deployed 缺少 target_cli;建议重新运行 $story-setup。")
} else if (!match[1].split(",").map((item) => item.trim()).includes("zcode")) {
messages.push("[story-setup] 当前部署标记未包含 zcode;如需 ZCode 项目适配,请重新运行 $story-setup 并选择 ZCode。")
}
}
const book = discoverActiveBook(root)
if (book) {
const context = path.join(book, "追踪", "上下文.md")
if (fs.existsSync(context)) {
messages.push(`[story context] 当前书目:${safeRelative(root, book)}。继续长篇写作前先读取 ${safeRelative(root, context)}。`)
} else {
messages.push(`[story context] 检测到写作项目:${safeRelative(root, book)}。`)
}
}
messages.push(...continuityFindings(root))
if (messages.length) emit(hookContext("SessionStart", messages.join("\n")))
}
function toolName(input) {
return String(input.tool_name || input.toolName || input.tool || input.name || "")
}
function toolPayload(input) {
for (const key of ["tool_input", "toolInput", "input", "parameters", "args"]) {
const value = input[key]
if (value && typeof value === "object" && !Array.isArray(value)) return value
}
return {}
}
function isPathInside(root, candidate, pathApi = path) {
const relation = pathApi.relative(root, candidate)
return relation === "" || (
!pathApi.isAbsolute(relation)
&& relation !== ".."
&& !relation.startsWith(`..${pathApi.sep}`)
)
}
function targetPaths(input) {
const root = projectRoot()
const inputCwd = existingDir(input.cwd)
const base = inputCwd && isPathInside(root, inputCwd) ? inputCwd : root
const name = toolName(input)
const payload = toolPayload(input)
const rawTargets = []
for (const key of ["file_path", "filePath", "path", "target", "filename"]) {
if (typeof payload[key] === "string") rawTargets.push(payload[key])
}
const command = typeof payload.command === "string" ? payload.command : ""
if (command) {
if (/bash/i.test(name)) rawTargets.push(...extractProseTargets(command))
else rawTargets.push(...extractPatchTargets(command), ...extractProseTargets(command))
}
for (const key of ["patch", "content", "text"]) {
if (typeof payload[key] === "string" && /applypatch|patch/i.test(name)) rawTargets.push(...extractPatchTargets(payload[key]))
}
return [...new Set(rawTargets.filter(Boolean).map((value) => resolveTarget(root, value, base)))]
}
function preToolProseGuard() {
const root = projectRoot()
for (const target of targetPaths(hookInput)) {
const reason = proseBlockReason(root, target)
if (reason) {
emit({
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: reason,
},
})
return
}
}
}
function preToolCommitAdvisory() {
const payload = toolPayload(hookInput)
const command = typeof payload.command === "string" ? payload.command : ""
if (!command || !isGitCommitCommand(command)) return
const warnings = stagedMarkdownWarnings(projectRoot())
if (warnings) emit(hookContext("PreToolUse", warnings))
}
function postToolProseCheck() {
const root = projectRoot()
const notes = targetPaths(hookInput).map((target) => proseAfterWrite(root, target)).filter(Boolean)
if (notes.length) emit(hookContext("PostToolUse", notes.join("\n\n")))
}
function main() {
const event = process.argv[2] || ""
try {
if (event === "session-start") sessionStart()
else if (event === "pre-tool-prose-guard") preToolProseGuard()
else if (event === "pre-tool-commit-advisory") preToolCommitAdvisory()
else if (event === "post-tool-prose-check") postToolProseCheck()
else {
process.stderr.write(`unknown oh-story ZCode hook event: ${event}\n`)
process.exitCode = 2
}
} catch (error) {
// Hook checks are defensive guardrails. Unexpected parse/filesystem failures
// fail open and are diagnosable without corrupting strict stdout JSON.
process.stderr.write(`[oh-story zcode hook] ${error instanceof Error ? error.message : String(error)}\n`)
}
}
if (require.main === module) main()
module.exports = {
continuityFindings,
proseNetFindings,
extractProseTargets,
extractPatchTargets,
isGitCommitCommand,
isPathInside,
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Unverified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |