docs: 更新 HTTP 错误码、用户积分、占卜运行及用户资料协议文档

This commit is contained in:
qzl
2026-04-10 16:45:45 +08:00
parent 1bc8bc6a27
commit 17ef460391
78 changed files with 18680 additions and 25 deletions
+538
View File
@@ -0,0 +1,538 @@
/**
* Trellis Context Injection Plugin
*
* Injects context when Task tool is called with supported subagent types.
* Uses OpenCode's tool.execute.before hook.
*
* Compatibility:
* - If oh-my-opencode handles via .claude/hooks/, this plugin skips
* - Otherwise, this plugin handles injection
*/
import { existsSync, writeFileSync } from "fs"
import { join } from "path"
import { TrellisContext, debugLog } from "../lib/trellis-context.js"
// Supported subagent types
const AGENTS_ALL = ["implement", "check", "debug", "research"]
const AGENTS_REQUIRE_TASK = ["implement", "check", "debug"]
// Agents that don't update phase (can be called at any time)
const AGENTS_NO_PHASE_UPDATE = ["debug", "research"]
/**
* Update current_phase in task.json based on subagent_type
*/
function updateCurrentPhase(ctx, taskDir, subagentType) {
if (AGENTS_NO_PHASE_UPDATE.includes(subagentType)) {
return
}
const taskJsonPath = join(ctx.directory, taskDir, "task.json")
const content = ctx.readFile(taskJsonPath)
if (!content) return
try {
const taskData = JSON.parse(content)
const currentPhase = taskData.current_phase || 0
const nextActions = taskData.next_action || []
// Map action names to subagent types
const actionToAgent = {
"implement": "implement",
"check": "check",
"finish": "check" // finish uses check agent
}
// Find the next phase that matches this subagent_type
let newPhase = null
for (const action of nextActions) {
const phaseNum = action.phase || 0
const actionName = action.action || ""
const expectedAgent = actionToAgent[actionName]
// Only consider phases after current_phase
if (phaseNum > currentPhase && expectedAgent === subagentType) {
newPhase = phaseNum
break
}
}
if (newPhase !== null) {
taskData.current_phase = newPhase
writeFileSync(taskJsonPath, JSON.stringify(taskData, null, 2))
debugLog("inject", "Updated current_phase to:", newPhase)
}
} catch (e) {
debugLog("inject", "Error updating phase:", e.message)
}
}
/**
* Get context for implement agent
*/
function getImplementContext(ctx, taskDir) {
const parts = []
// 1. Read implement.jsonl (or fallback to spec.jsonl)
let jsonlPath = join(ctx.directory, taskDir, "implement.jsonl")
let entries = ctx.readJsonlWithFiles(jsonlPath)
if (entries.length === 0) {
// Fallback to spec.jsonl
jsonlPath = join(ctx.directory, taskDir, "spec.jsonl")
entries = ctx.readJsonlWithFiles(jsonlPath)
}
if (entries.length > 0) {
parts.push(ctx.buildContextFromEntries(entries))
}
// 2. Requirements document
const prd = ctx.readProjectFile(join(taskDir, "prd.md"))
if (prd) {
parts.push(`=== ${taskDir}/prd.md (Requirements) ===\n${prd}`)
}
// 3. Technical design
const info = ctx.readProjectFile(join(taskDir, "info.md"))
if (info) {
parts.push(`=== ${taskDir}/info.md (Technical Design) ===\n${info}`)
}
return parts.join("\n\n")
}
/**
* Get context for check agent
*/
function getCheckContext(ctx, taskDir) {
const parts = []
// 1. Read check.jsonl
const jsonlPath = join(ctx.directory, taskDir, "check.jsonl")
const entries = ctx.readJsonlWithFiles(jsonlPath)
if (entries.length > 0) {
parts.push(ctx.buildContextFromEntries(entries))
} else {
// Fallback: hardcoded check files + spec.jsonl
const checkFiles = [
[".opencode/commands/trellis/finish-work.md", "Finish work checklist"],
[".opencode/commands/trellis/check-cross-layer.md", "Cross-layer check spec"],
[".opencode/commands/trellis/check-backend.md", "Backend check spec"],
[".opencode/commands/trellis/check-frontend.md", "Frontend check spec"],
]
for (const [f, description] of checkFiles) {
const content = ctx.readProjectFile(f)
if (content) {
parts.push(`=== ${f} (${description}) ===\n${content}`)
}
}
// Add spec.jsonl
const specJsonlPath = join(ctx.directory, taskDir, "spec.jsonl")
const specEntries = ctx.readJsonlWithFiles(specJsonlPath)
for (const entry of specEntries) {
parts.push(`=== ${entry.path} (Dev spec) ===\n${entry.content}`)
}
}
// 2. Requirements document
const prd = ctx.readProjectFile(join(taskDir, "prd.md"))
if (prd) {
parts.push(`=== ${taskDir}/prd.md (Requirements - for understanding intent) ===\n${prd}`)
}
return parts.join("\n\n")
}
/**
* Get context for finish phase (final check before PR)
*/
function getFinishContext(ctx, taskDir) {
const parts = []
// 1. Try finish.jsonl first
const jsonlPath = join(ctx.directory, taskDir, "finish.jsonl")
const entries = ctx.readJsonlWithFiles(jsonlPath)
if (entries.length > 0) {
parts.push(ctx.buildContextFromEntries(entries))
} else {
// Fallback: only finish-work.md (lightweight)
const finishWork = ctx.readProjectFile(".opencode/commands/trellis/finish-work.md")
if (finishWork) {
parts.push(`=== .opencode/commands/trellis/finish-work.md (Finish checklist) ===\n${finishWork}`)
}
}
// 2. Spec update process (for active spec sync)
const updateSpec = ctx.readProjectFile(".opencode/commands/trellis/update-spec.md")
if (updateSpec) {
parts.push(`=== .opencode/commands/trellis/update-spec.md (Spec update process) ===\n${updateSpec}`)
}
// 3. Requirements document (for verifying requirements are met)
const prd = ctx.readProjectFile(join(taskDir, "prd.md"))
if (prd) {
parts.push(`=== ${taskDir}/prd.md (Requirements - verify all met) ===\n${prd}`)
}
return parts.join("\n\n")
}
/**
* Get context for debug agent
*/
function getDebugContext(ctx, taskDir) {
const parts = []
// 1. Read debug.jsonl (or fallback to spec.jsonl + check files)
const jsonlPath = join(ctx.directory, taskDir, "debug.jsonl")
const entries = ctx.readJsonlWithFiles(jsonlPath)
if (entries.length > 0) {
parts.push(ctx.buildContextFromEntries(entries))
} else {
// Fallback: use spec.jsonl + hardcoded check files
const specJsonlPath = join(ctx.directory, taskDir, "spec.jsonl")
const specEntries = ctx.readJsonlWithFiles(specJsonlPath)
for (const entry of specEntries) {
parts.push(`=== ${entry.path} (Dev spec) ===\n${entry.content}`)
}
const checkFiles = [
[".opencode/commands/trellis/check-backend.md", "Backend check spec"],
[".opencode/commands/trellis/check-frontend.md", "Frontend check spec"],
[".opencode/commands/trellis/check-cross-layer.md", "Cross-layer check spec"],
]
for (const [f, description] of checkFiles) {
const content = ctx.readProjectFile(f)
if (content) {
parts.push(`=== ${f} (${description}) ===\n${content}`)
}
}
}
// 2. Codex review output (if exists)
const codex = ctx.readProjectFile(join(taskDir, "codex-review-output.txt"))
if (codex) {
parts.push(`=== ${taskDir}/codex-review-output.txt (Codex Review Results) ===\n${codex}`)
}
return parts.join("\n\n")
}
/**
* Get context for research agent
*/
function getResearchContext(ctx, taskDir) {
const parts = []
parts.push(`## Project Spec Directory Structure
\`\`\`
.trellis/spec/
├── shared/ # Cross-project common specs
├── frontend/ # Frontend standards
├── backend/ # Backend standards
└── guides/ # Thinking guides
.trellis/big-question/ # Known issues and pitfalls
\`\`\`
## Search Tips
- Spec files: \`.trellis/spec/**/*.md\`
- Known issues: \`.trellis/big-question/\`
- Code search: Use Glob and Grep tools
- Tech solutions: Use mcp__exa__web_search_exa or mcp__exa__get_code_context_exa`)
if (taskDir) {
const jsonlPath = join(ctx.directory, taskDir, "research.jsonl")
const entries = ctx.readJsonlWithFiles(jsonlPath)
if (entries.length > 0) {
parts.push("\n## Additional Search Context\n")
parts.push(ctx.buildContextFromEntries(entries))
}
}
return parts.join("\n\n")
}
/**
* Build enhanced prompt with context
*/
function buildPrompt(agentType, originalPrompt, context, isFinish = false) {
const templates = {
implement: `# Implement Agent Task
You are the Implement Agent in the Multi-Agent Pipeline.
## Your Context
${context}
---
## Your Task
${originalPrompt}
---
## Workflow
1. **Understand specs** - All dev specs are injected above
2. **Understand requirements** - Read requirements and technical design
3. **Implement feature** - Follow specs and design
4. **Self-check** - Ensure code quality
## Important Constraints
- Do NOT execute git commit
- Follow all dev specs injected above
- Report list of modified/created files when done`,
check: isFinish ? `# Finish Agent Task
You are performing the final check before creating a PR.
## Your Context
${context}
---
## Your Task
${originalPrompt}
---
## Workflow
1. **Review changes** - Run \`git diff --name-only\` to see all changed files
2. **Verify requirements** - Check each requirement in prd.md is implemented
3. **Spec sync** - Analyze whether changes introduce new patterns, contracts, or conventions
- If new pattern/convention found: read target spec file → update it → update index.md if needed
- If infra/cross-layer change: follow the 7-section mandatory template from update-spec.md
- If pure code fix with no new patterns: skip this step
4. **Run final checks** - Execute lint and typecheck
5. **Confirm ready** - Ensure code is ready for PR
## Important Constraints
- You MAY update spec files when gaps are detected (use update-spec.md as guide)
- MUST read the target spec file BEFORE editing (avoid duplicating existing content)
- Do NOT update specs for trivial changes (typos, formatting, obvious fixes)
- If critical CODE issues found, report them clearly (fix specs, not code)
- Verify all acceptance criteria in prd.md are met` :
`# Check Agent Task
You are the Check Agent in the Multi-Agent Pipeline.
## Your Context
${context}
---
## Your Task
${originalPrompt}
---
## Workflow
1. **Get changes** - Run \`git diff --name-only\` and \`git diff\`
2. **Check against specs** - Check item by item
3. **Self-fix** - Fix issues directly, don't just report
4. **Run verification** - Run lint and typecheck
## Important Constraints
- Fix issues yourself, don't just report
- Must execute complete checklist`,
debug: `# Debug Agent Task
You are the Debug Agent in the Multi-Agent Pipeline.
## Your Context
${context}
---
## Your Task
${originalPrompt}
---
## Workflow
1. **Understand issues** - Analyze issues pointed out
2. **Locate code** - Find positions that need fixing
3. **Fix against specs** - Fix following dev specs
4. **Verify fixes** - Run typecheck
## Important Constraints
- Do NOT execute git commit
- Run typecheck after each fix`,
research: `# Research Agent Task
You are the Research Agent in the Multi-Agent Pipeline.
## Core Principle
**You do one thing: find and explain information.**
## Project Info
${context}
---
## Your Task
${originalPrompt}
---
## Workflow
1. **Understand query** - Determine search type and scope
2. **Plan search** - List search steps
3. **Execute search** - Run multiple searches in parallel
4. **Organize results** - Output structured report
## Strict Boundaries
**Only allowed**: Describe what exists, where it is, how it works
**Forbidden**: Suggest improvements, criticize implementation, modify files`
}
return templates[agentType] || originalPrompt
}
export default async ({ directory }) => {
const ctx = new TrellisContext(directory)
debugLog("inject", "Plugin loaded, directory:", directory)
return {
// ==========================================================================
// ⚠️ KNOWN LIMITATION: OpenCode project-level plugins cannot intercept subagents
//
// This hook will NOT be triggered because:
// 1. Project-level plugins (.opencode/plugin/) don't support tool.execute.before
// 2. Only global plugins (npm packages) have full hook permissions
// 3. This is a known OpenCode architecture limitation (see Issue #5894)
//
// SOLUTION: Trellis + OpenCode users must install oh-my-opencode (omo)
// - omo is a global plugin with full hook permissions
// - omo reads .claude/settings.json and executes Python hooks
// - .claude/hooks/inject-subagent-context.py handles the actual injection
//
// References:
// - https://github.com/sst/opencode/issues/5894 (plugin hooks don't intercept subagent)
// - https://github.com/sst/opencode/issues/2588 (subagent inherit context)
// ==========================================================================
"tool.execute.before": async (input, output) => {
try {
debugLog("inject", "tool.execute.before called, tool:", input?.tool)
// Only handle Task tool
const toolName = input?.tool?.toLowerCase()
if (toolName !== "task") {
return
}
const args = output?.args || {}
const subagentType = args.subagent_type
const originalPrompt = args.prompt || ""
debugLog("inject", "Task tool called, subagent_type:", subagentType)
// Only handle supported agent types
if (!AGENTS_ALL.includes(subagentType)) {
debugLog("inject", "Skipping - unsupported subagent_type")
return
}
// Check if we should skip (omo will handle)
if (ctx.shouldSkipHook("inject-subagent-context")) {
debugLog("inject", "Skipping - omo will handle via .claude/hooks/")
return
}
// Read current task
const taskDir = ctx.getCurrentTask()
// Agents requiring task directory
if (AGENTS_REQUIRE_TASK.includes(subagentType)) {
if (!taskDir) {
debugLog("inject", "Skipping - no current task")
return
}
const taskDirFull = join(directory, taskDir)
if (!existsSync(taskDirFull)) {
debugLog("inject", "Skipping - task directory not found")
return
}
// Update current_phase in task.json
updateCurrentPhase(ctx, taskDir, subagentType)
}
// Check for [finish] marker
const isFinish = originalPrompt.toLowerCase().includes("[finish]")
// Get context based on agent type
let context = ""
switch (subagentType) {
case "implement":
context = getImplementContext(ctx, taskDir)
break
case "check":
// Use finish context for [finish] phase (lighter, focused on final verification)
// Use check context for regular check (full specs for self-fix loop)
context = isFinish
? getFinishContext(ctx, taskDir)
: getCheckContext(ctx, taskDir)
break
case "debug":
context = getDebugContext(ctx, taskDir)
break
case "research":
context = getResearchContext(ctx, taskDir)
break
}
if (!context) {
debugLog("inject", "No context to inject")
return
}
// Build enhanced prompt
const newPrompt = buildPrompt(subagentType, originalPrompt, context, isFinish)
// Update the tool input
output.args = {
...args,
prompt: newPrompt
}
debugLog("inject", "Injected context for", subagentType, "prompt length:", newPrompt.length)
} catch (error) {
debugLog("inject", "Error in tool.execute.before:", error.message, error.stack)
}
}
}
}
+325
View File
@@ -0,0 +1,325 @@
/* global process */
/**
* Trellis Session Start Plugin
*
* Injects context when user sends the first message in a session.
* Uses OpenCode's chat.message + experimental.chat.messages.transform hooks.
*
* Compatibility:
* - If oh-my-opencode handles via .claude/hooks/, this plugin skips
* - Otherwise, this plugin handles injection
*/
import { existsSync, readFileSync, readdirSync, statSync } from "fs"
import { join } from "path"
import { TrellisContext, contextCollector, debugLog } from "../lib/trellis-context.js"
/**
* Check current task status and return structured status string.
* JavaScript equivalent of _get_task_status in Claude's session-start.py.
*/
function getTaskStatus(directory) {
const trellisDir = join(directory, ".trellis")
const currentTaskFile = join(trellisDir, ".current-task")
if (!existsSync(currentTaskFile)) {
return "Status: NO ACTIVE TASK\nNext: Describe what you want to work on"
}
let taskRef
try {
taskRef = readFileSync(currentTaskFile, "utf-8").trim()
} catch {
return "Status: NO ACTIVE TASK\nNext: Describe what you want to work on"
}
if (!taskRef) {
return "Status: NO ACTIVE TASK\nNext: Describe what you want to work on"
}
// Resolve task directory
let taskDir
if (taskRef.startsWith("/")) {
taskDir = taskRef
} else if (taskRef.startsWith(".trellis/")) {
taskDir = join(directory, taskRef)
} else {
taskDir = join(trellisDir, "tasks", taskRef)
}
if (!existsSync(taskDir)) {
return `Status: STALE POINTER\nTask: ${taskRef}\nNext: Task directory not found. Run: python3 ./.trellis/scripts/task.py finish`
}
// Read task.json
let taskData = {}
const taskJsonPath = join(taskDir, "task.json")
if (existsSync(taskJsonPath)) {
try {
taskData = JSON.parse(readFileSync(taskJsonPath, "utf-8"))
} catch {
// Ignore parse errors
}
}
const taskTitle = taskData.title || taskRef
const taskStatus = taskData.status || "unknown"
if (taskStatus === "completed") {
const dirName = taskDir.split("/").pop()
return `Status: COMPLETED\nTask: ${taskTitle}\nNext: Archive with \`python3 ./.trellis/scripts/task.py archive ${dirName}\` or start a new task`
}
// Check if context is configured (jsonl files exist and non-empty)
let hasContext = false
for (const jsonlName of ["implement.jsonl", "check.jsonl", "spec.jsonl"]) {
const jsonlPath = join(taskDir, jsonlName)
if (existsSync(jsonlPath)) {
try {
const st = statSync(jsonlPath)
if (st.size > 0) {
hasContext = true
break
}
} catch {
// Ignore stat errors
}
}
}
const hasPrd = existsSync(join(taskDir, "prd.md"))
if (!hasPrd) {
return `Status: NOT READY\nTask: ${taskTitle}\nMissing: prd.md not created\nNext: Write PRD, then research → init-context → start`
}
if (!hasContext) {
return `Status: NOT READY\nTask: ${taskTitle}\nMissing: Context not configured (no jsonl files)\nNext: Complete Phase 2 (research → init-context → start) before implementing`
}
return `Status: READY\nTask: ${taskTitle}\nNext: Continue with implement or check`
}
/**
* Build session context for injection
*/
function buildSessionContext(ctx) {
const directory = ctx.directory
const trellisDir = join(directory, ".trellis")
const claudeDir = join(directory, ".claude")
const opencodeDir = join(directory, ".opencode")
const parts = []
// 1. Header
parts.push(`<trellis-context>
You are starting a new session in a Trellis-managed project.
Read and follow all instructions below carefully.
</trellis-context>`)
// 2. Current Context (dynamic)
const contextScript = join(trellisDir, "scripts", "get_context.py")
if (existsSync(contextScript)) {
const output = ctx.runScript(contextScript)
if (output) {
parts.push("<current-state>")
parts.push(output)
parts.push("</current-state>")
}
}
// 3. Workflow Guide
const workflow = ctx.readProjectFile(".trellis/workflow.md")
if (workflow) {
parts.push("<workflow>")
parts.push(workflow)
parts.push("</workflow>")
}
// 4. Guidelines Index (dynamic discovery, matching Claude's session-start.py)
parts.push("<guidelines>")
parts.push("**Note**: The guidelines below are index files — they list available guideline documents and their locations.")
parts.push("During actual development, you MUST read the specific guideline files listed in each index's Pre-Development Checklist.\n")
const specDir = join(directory, ".trellis", "spec")
if (existsSync(specDir)) {
try {
const subs = readdirSync(specDir).filter(name => {
if (name.startsWith(".")) return false
try {
return statSync(join(specDir, name)).isDirectory()
} catch {
return false
}
}).sort()
for (const sub of subs) {
const indexFile = join(specDir, sub, "index.md")
if (existsSync(indexFile)) {
// Flat spec dir: spec/<layer>/index.md
const content = ctx.readFile(indexFile)
if (content) {
parts.push(`## ${sub}\n${content}\n`)
}
} else {
// Nested package dirs (monorepo): spec/<pkg>/<layer>/index.md
try {
const nested = readdirSync(join(specDir, sub)).filter(name => {
try {
return statSync(join(specDir, sub, name)).isDirectory()
} catch {
return false
}
}).sort()
for (const layer of nested) {
const nestedIndex = join(specDir, sub, layer, "index.md")
if (existsSync(nestedIndex)) {
const content = ctx.readFile(nestedIndex)
if (content) {
parts.push(`## ${sub}/${layer}\n${content}\n`)
}
}
}
} catch {
// Ignore directory read errors
}
}
}
} catch {
// Ignore spec directory read errors
}
}
parts.push("</guidelines>")
// 5. Session Instructions - try both .claude and .opencode
let startMd = ctx.readFile(join(claudeDir, "commands", "trellis", "start.md"))
if (!startMd) {
startMd = ctx.readFile(join(opencodeDir, "commands", "trellis", "start.md"))
}
if (startMd) {
parts.push("<instructions>")
parts.push(startMd)
parts.push("</instructions>")
}
// 6. Task status (R2: check task state for session resume)
const taskStatus = getTaskStatus(directory)
parts.push(`<task-status>\n${taskStatus}\n</task-status>`)
// 7. Final directive (R3: active, not passive)
parts.push(`<ready>
Context loaded. Steps 1-3 (workflow, context, guidelines) are already injected above — do NOT re-read them.
Start from Step 4. Wait for user's first message, then follow <instructions> to handle their request.
If there is an active task, ask whether to continue it.
</ready>`)
return parts.join("\n\n")
}
export default async ({ directory }) => {
const ctx = new TrellisContext(directory)
debugLog("session", "Plugin loaded, directory:", directory)
return {
// chat.message - triggered when user sends a message
"chat.message": async (input) => {
try {
const sessionID = input.sessionID
const agent = input.agent || "unknown"
debugLog("session", "chat.message called, sessionID:", sessionID, "agent:", agent)
// Skip in non-interactive mode
if (process.env.OPENCODE_NON_INTERACTIVE === "1") {
debugLog("session", "Skipping - non-interactive mode")
return
}
// Check if we should skip (omo will handle)
if (ctx.shouldSkipHook("session-start")) {
debugLog("session", "Skipping - omo will handle via .claude/hooks/")
return
}
// Only inject on first message
if (contextCollector.isProcessed(sessionID)) {
debugLog("session", "Skipping - session already processed")
return
}
// Mark session as processed
contextCollector.markProcessed(sessionID)
// Build and store context
const context = buildSessionContext(ctx)
debugLog("session", "Built context, length:", context.length)
contextCollector.store(sessionID, context)
debugLog("session", "Context stored for session:", sessionID)
} catch (error) {
debugLog("session", "Error in chat.message:", error.message, error.stack)
}
},
// experimental.chat.messages.transform - modify messages before sending to AI
"experimental.chat.messages.transform": async (input, output) => {
try {
const { messages } = output
debugLog("session", "messages.transform called, messageCount:", messages?.length)
if (!messages || messages.length === 0) {
return
}
// Find last user message
let lastUserMessageIndex = -1
for (let i = messages.length - 1; i >= 0; i--) {
if (messages[i].info?.role === "user") {
lastUserMessageIndex = i
break
}
}
if (lastUserMessageIndex === -1) {
debugLog("session", "No user message found")
return
}
const lastUserMessage = messages[lastUserMessageIndex]
const sessionID = lastUserMessage.info?.sessionID
debugLog("session", "Found user message, sessionID:", sessionID)
if (!sessionID || !contextCollector.hasPending(sessionID)) {
debugLog("session", "No pending context for session")
return
}
// Get and consume pending context
const pending = contextCollector.consume(sessionID)
// Find first text part
const textPartIndex = lastUserMessage.parts?.findIndex(
p => p.type === "text" && p.text !== undefined
)
if (textPartIndex === -1) {
debugLog("session", "No text part found in user message")
return
}
// Prepend context to the text part (same approach as omo)
const originalText = lastUserMessage.parts[textPartIndex].text || ""
lastUserMessage.parts[textPartIndex].text = `${pending.content}\n\n---\n\n${originalText}`
debugLog("session", "Injected context by prepending to text, length:", pending.content.length)
} catch (error) {
debugLog("session", "Error in messages.transform:", error.message, error.stack)
}
}
}
}