shuiyuan1223

coding-standards

0
0
# Install this skill:
npx skills add shuiyuan1223/skillfix_xuetang --skill "coding-standards"

Install specific skill from multi-skill repository

# Description

Use when writing or reviewing code for the PHA project. Covers coding conventions, architecture rules, and common patterns.

# SKILL.md


name: coding-standards
description: Use when writing or reviewing code for the PHA project. Covers coding conventions, architecture rules, and common patterns.


PHA Coding Standards

Iron Rules (MUST follow)

AgentOS ๅ“ฒๅญฆ

  1. Frontend = Pure Renderer โ€” ui/src/ only renders A2UI JSON. No business logic, no API calls, no data processing.
  2. UI = Agent Output โ€” All pages generated in src/gateway/pages.ts via A2UIGenerator, not frontend.
  3. MCP is the only tool API โ€” Tools exposed via MCP protocol in src/tools/. Agent never calls internal functions directly.
  4. Tools = MCP, Knowledge = Skills โ€” Data operations โ†’ MCP Tool; Expert knowledge โ†’ Skill (src/skills/*/SKILL.md). Never use bare JSON config files as Skills.

็”Ÿๆˆๅผ UI ็บฆๆŸ

  1. A2UI is the only UI protocol โ€” 4 Surfaces: main, sidebar, modal, toast. Each is a component tree { components[], root_id }.
  2. Use A2UIGenerator โ€” const ui = new A2UIGenerator("main") โ†’ compose โ†’ ui.build(rootId). Never hand-write JSON component trees.
  3. No Emoji Icons โ€” Use icon names ("heart", "brain") in icon properties, never emoji ("โค๏ธ").
  4. i18n Sync โ€” Every user-facing string uses t(). Changes require updating types.ts + zh-CN.ts + en.ts simultaneously.
  5. Dual Frontend Sync โ€” Web (React A2UIRenderer.tsx) and TUI (tui-renderer.ts) consume same A2UI data. New component types need renderers in both.
  6. Tool Display Names โ€” New MCP tools must be added to TOOL_DISPLAY_NAMES in ui/src/components/a2ui/A2UIRenderer.tsx for Chinese display names.

้€šไฟกๅ่ฎฎ็บฆๆŸ

  1. Dual Channel โ€” Chat messages via SSE (POST /api/ag-ui), everything else via WebSocket (/ws). SSE sends AG-UI events (RunStarted/TextMessageContent/ToolCallStart etc.), WebSocket sends A2UI page updates.
  2. State Ownership โ€” Server (GatewaySession) is the single source of truth for chat history. Frontend maintains local chat state for SSE real-time rendering, but rebuilds from server A2UI on page refresh. Non-chat UI state is fully server-driven.
  3. Action Pattern โ€” User interactions: { type: "action", action: "handler_name", payload: {...} }. Action strings must match handleAction() in server.ts exactly.
  4. Navigate Pattern โ€” Sidebar clicks: { type: "navigate", view: "view_id" }. View IDs must match handleNavigate() cases exactly.

ๅทฅ็จ‹็บชๅพ‹

  1. Keep it Simple โ€” No premature abstractions. No over-engineering. Three similar lines > one premature abstraction.

TypeScript Conventions

  • Runtime: Bun
  • Build: tsc --outDir dist (backend) + vite build (frontend)
  • Imports: Use .js extension for local imports (ESM)
  • Type imports: Use import type where possible
  • No any: Except in AgentTool<any>[] array (variance workaround)
  • Formatting: Prettier (auto via lint-staged)
  • Linting: ESLint (auto via lint-staged)

File Organization

What Where
CLI commands src/commands/xxx.ts
MCP tool definitions src/tools/xxx-tools.ts
Git MCP tools src/tools/git-tools.ts
AgentTool adapters src/agent/tools.ts, src/agent/git-agent-tools.ts
A2UI page generators src/gateway/pages.ts, src/gateway/evolution-lab.ts
Route/action handlers src/gateway/server.ts
A2UI component types src/gateway/a2ui.ts
Evolution system src/evolution/*.ts
Agent Skills src/skills/*/SKILL.md
Memory system src/memory/*.ts
Translations src/locales/{types,zh-CN,en}.ts
Frontend renderer ui/src/components/a2ui/A2UIRenderer.tsx
Frontend icons ui/src/lib/icons.tsx
Tests tests/unit/, tests/integration/
Test fixtures tests/fixtures/

Common Patterns

Singleton Services

import { getMemoryManager } from "../memory/index.js";
import { getDataSource } from "../tools/health-data.js";
import { getUserUuid } from "../utils/config.js";

Git Operations

  • All git operations should go through src/evolution/version-manager.ts or src/tools/git-tools.ts
  • Use gitCommitFiles(files, message) for commits, not raw execSync("git add && git commit")
  • Import from "../evolution/version-manager.js" for gitCommitFiles, getProjectRoot, etc.

Error Handling

  • Tools: Return { success: false, error: message }, never throw
  • Server: try/catch with console.error, send error to client
  • Profile extraction: Best-effort, wrap in try/catch and ignore failures

Session State in GatewaySession

  • Add private fields to GatewaySession class for page-specific state
  • Reset state when navigating away
  • Pattern: this.xxxField for state, read in handleNavigate, write in handleAction
  • SSE mode flags: _sseMode (chat active via SSE), _chatLock (prevents concurrent chat requests)

SSE Chat Flow

Frontend POST /api/ag-ui โ†’ GatewaySession.handleChatSSE()
  โ†’ Agent processes message
  โ†’ SSE events stream back: RunStarted โ†’ TextMessageContent* โ†’ ToolCallStart/End โ†’ RunFinished
  โ†’ Frontend updates local chat state in real-time

Commit Convention

feat: ๆ–ฐๅŠŸ่ƒฝ
fix: ไฟฎๅค
refactor: ้‡ๆž„
docs: ๆ–‡ๆกฃ
chore: ๆ‚้กน

Pre-commit Checklist

bun run check        # TypeScript types
bun run lint         # ESLint
bun run format:check # Prettier
bun test             # Tests
bun run build        # Full build

# Supported AI Coding Agents

This skill is compatible with the SKILL.md standard and works with all major AI coding agents:

Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.