Engineering workflow commands for Claude Code

Engineering workflow commands for Claude Code.
<!-- SKILLS_TABLE_START -->
| Command | Description | When to use |
|---|---|---|
/cc-cmds:autopilot | 목표 하나를 받아 이 세션이 라우터가 되어 스킬 호출을 스스로 정하며 완주시키는 파이프라인의 킥오프와 아침 보고 | 사용자가 설계 문서·레포·PR·브랜치, 또는 아직 산출물이 없는 목표를 던져 두고 설계·감사·구현·리뷰·머지·적용까지 알아서 이어지게 하고 싶을 때 — 진행은 이 터미널로 중계되고 중요한 결정만 물어 온다. 또는 그렇게 돌린 런의 아침 보고를 받을 때 |
/cc-cmds:autopilot-router-shift | 게이트가 띄운 헤드리스 라우터 샤드가 받는 라우팅 루프 — 스냅숏을 읽어 한 행위를 정하고 게이트에 넘기며, 상한·승인·종단·중단·대기에서 인수인계 행을 남기고 끝난다 | 이 커맨드는 사람이 치는 것이 아니다 — 라우터 샤드가 받는 스킬이다. 리드 세션이 gate.sh act --kind router-shift 로 교대를 시작할 때 그 샤드가 이 문서를 프롬프트로 받는다 |
/cc-cmds:dashboard | 이 호스트의 autopilot 런들을 한 화면에 보여주는 로컬 대시보드 서버를 띄우고 링크를 알려준다 | 사용자가 런 진행 대시보드를 다시 열거나, 서버가 꺼졌다는 안내(「서버가 꺼져 있습니다 — /cc-cmds:dashboard」)를 보고 다시 올리려 할 때 |
/cc-cmds:design | 에이전트 팀을 활용한 기능 설계 토론 진행 | 사용자가 새 기능 설계/아키텍처 결정/다관점 검토가 필요한 설계 논의를 요청할 때 |
/cc-cmds:design-analyze | 에이전트 팀을 활용한 제3자 설계 문서 다관점 분석 (읽기 전용) | 타인이 작성한 설계/리팩토링 문서를 원본 수정 없이 다관점으로 분석하고 분석 산출물(보고서/주석본/피드백)을 생성하고자 할 때 |
/cc-cmds:design-audit | 동결된 설계 문서를 독립 리더 팬아웃으로 1회 감사하고 정합 조정 1회 후 정지 (반복 루프 없음) | 설계 문서 작성이 끝나 더 이상 수정하지 않을 시점에, 문서를 동결한 뒤 레포 실측 기반 독립 감사로 잔여 결함을 드러내고 이름 붙은 하류 소유자에게 인계하고자 할 때 (design 종단 이후 · implement 직전) |
/cc-cmds:design-audit-unattended | 동결된 설계 문서를 독립 리더 팬아웃으로 1회 감사하고 정합 조정 1회 후 정지 (무인 — 사람 확인 없이 park) | 자율 파이프라인 드라이버가 감사 스테이지를 헤드리스로 디스패치할 때. 사람이 직접 부르는 경우에는 /cc-cmds:design-audit를 쓸 것 |
/cc-cmds:design-base | 여러 티켓으로 나눌 큰 작업의 베이스 설계 — 티켓 간 계약과 병렬화 최대의 티켓 분할을 설계하고, 감사 뒤 트래커 발행까지 | 요청이 각자 독자적 설계 판단이 필요한 조각들로 나뉘는 큰 작업이라, 단일 설계 문서 하나로 구현 슬라이스를 나누기보다 먼저 조각 간 계약과 티켓 분할을 정하고 조각마다 따로 설계하려 할 때 |
/cc-cmds:design-base-unattended | 베이스 설계의 무인 팔 — 좌석·드라이버가 파견하면 베이스 문서의 토론·저장(드라이버면 동결까지)을, --split 이면 감사 뒤 분할 점검과 트래커 발행을 돈다 (질문은 park·정지 기록으로) | 사람이 직접 부르지 않는다. design-base 리드 좌석이 Step 2 승인 뒤 claude -p 로 파견하거나(다리), autopilot 이 베이스 런의 설계 단계와 분할 단계로 파견한다(스테이지) |
/cc-cmds:design-discuss-unattended | 설계 세션의 무인 팔 — 좌석이 파견하면 Step 3 토론과 Step 4 종합·저장을 도는 다리, autopilot 드라이버가 파견하면 Step 7U 동결까지 도는 스테이지 (질문은 park·정지 기록으로) | 사람이 직접 부르지 않는다. design 리드 좌석이 Step 2 승인 뒤 claude -p 로 파견하거나(다리), autopilot 드라이버가 design_required 인 런의 첫 스테이지로 파견한다(스테이지) |
/cc-cmds:design-lite | 2인 팀을 활용한 경량 설계 토론 | 깊은 다관점 분석보다 빠른 방향 설정이 우선될 때 (sonnet 단독 합성으로 미묘한 invariant 누락 가능) |
/cc-cmds:design-reconverge | 반증된 검증 항목이나 설계 결함 발견 하나에 스코프된 재수렴 — 설계를 고치고 두 값 판정 후 정지 (무인) | 자율 파이프라인 드라이버가 사다리 R2(재설계) 레인에 진입할 때, 라우터 교대가 구현 단계의 잔여 항목 반증(implement-unattended Step 1.5d 또는 Step 3 구현 중 반증 중단)을 재수렴으로 파견할 때, 사람이 감사 종합 질문의 후보 요구를 채택해 라우터 교대가 재수렴으로 넘길 때, 또는 구현 단계의 구속 티어 이탈 중단(implement-unattended CFI-U3)에 사람이 재수렴 을 골라(또는 매니페스트의 구속-이탈 자동 채택 행으로) 라우터 교대가 넘길 때, 또는 확인 리뷰 상한의 P0 잔존으로 끝난 런의 후속 런이 첫 단계에서 재수렴을 골랐을 때, 또는 무인 설계 사다리 정지에 사람이 골격 재검토 를 골라 라우터 교대가 잔여 동결 재검토 범위로 넘길 때(사람 답이 재부착이 아니라 재수렴으로 가는 예외), 또는 무인 감사가 상한 안의 회차에서 골격급 결함을 내고 게이트가 그 감사 키에 자동 재수렴을 열어 라우터 교대나 구동기가 감사 골격급 범위로 넘길 때. 사람이 참여하는 재설계는 /cc-cmds:design으로 처음부터 다시 수렴할 것 |
/cc-cmds:design-solo-unattended | 리드 단독 설계의 무인 팔 — autopilot 이 solo 티어 런과 team-4 단일 설계의 solo-audit 경로 런의 설계 단계로 파견하면 리드 혼자 요약을 쓰고 동결한다 (팀 없음, 질문은 정지 기록으로) | 사람이 직접 부르지 않는다. autopilot 드라이버·게이트가 선택 함수가 이 스킬을 고른 런 — solo 티어이거나 team-4 이고 design_path 가 solo-audit 이며 design_required 인 런 — 의 설계 스테이지로 파견한다 |
/cc-cmds:design-upgrade | 팀 구성 강화 분석 (모델·역할 축) | 직전 /design 팀 구성 제안에서 opus 승격이 유의미한 역할이 있는지, 또는 누락 도메인을 메울 신규 역할·과부하 역할 분할이 필요한지 second-opinion으로 검토할 때 |
/cc-cmds:implement | 설계 문서 기반 구현 | 사용자가 작성된 설계 문서를 바탕으로 단계적 계획을 세우고 실제 구현을 수행하기를 원할 때 |
/cc-cmds:implement-unattended | 설계 문서 기반 구현 (무인 — 사람 확인 없이 오케스트레이터가 라우팅) | 자율 파이프라인 드라이버가 세그먼트 구현 스테이지를 헤드리스로 디스패치할 때. 사람이 직접 부르는 경우에는 /cc-cmds:implement를 쓸 것 |
/cc-cmds:review | 에이전트 팀을 활용한 다관점 코드 리뷰 | 사용자가 PR/로컬 diff/파일 경로에 대한 다관점 코드 리뷰(보안/성능/품질 등)를 요청할 때 |
/cc-cmds:review-lite | 2인 팀을 활용한 경량 코드 리뷰 | 빠른 코드 리뷰가 목적이고 다관점 심층 분석이 불필요할 때 (큰 PR coverage gap, 미묘한 race condition·authn bypass 검출률 약화 가능) |
/cc-cmds:review-unattended | 에이전트 팀을 활용한 다관점 코드 리뷰 (무인 — 사람 확인 없이 리포트까지 완주) | 자율 파이프라인 드라이버가 리뷰 스테이지를 헤드리스로 디스패치할 때. 사람이 직접 부르는 경우에는 /cc-cmds:review를 쓸 것 |
/cc-cmds:review-upgrade | 리뷰어 구성 강화 분석 (모델·역할 축) | 직전 /review Step 3 리뷰어 구성 제안에서 opus 승격이 유의미한 역할이 있는지, 누락된 리뷰 관점을 메울 신규 리뷰어 추가가 필요한지, 또는 과부하 리뷰어 분할이 필요한지 second-opinion으로 검토할 때 |
/cc-cmds:visual | 설계 문서의 화면을 로컬에서 생성·검토하고, Claude Design 에 발행·동기하며, 구현 승인을 사이드카에 쓴다 (대화형, 고정 계정 좌석) | 무인 런이 검토 통과 로 남긴 화면을 아침에 발행·캔버스 편집·동기한 뒤 구현을 승인할 때, 또는 사이드카 없는 설계 문서의 화면을 대화형으로 처음 만들 때. 무인 파이프라인 스테이지는 생성에 /cc-cmds:visual-unattended, 계획에 S2P 단계가 있는 런의 발행·승인에 /cc-cmds:visual-publish-unattended 를 쓴다 |
/cc-cmds:visual-publish-unattended | 런 안에서 검토 통과 화면을 고정 계정으로 Claude Design 에 발행하고, 사람의 시각 판정 답에 따라 구현 승인·다시 생성·금지 후보 반영을 한다 (무인 — 사람 판단은 중단 기록으로) | 자율 파이프라인이 계획에 동결된 S2P 단계를 헤드리스로 파견할 때. 사람이 직접 발행할 때는 /cc-cmds:visual 을 쓸 것 |
/cc-cmds:visual-unattended | 동결된 설계 문서의 화면을 로컬에서 생성하고 세 회 안의 검토로 검토 통과 까지 올린다 (무인 — 사람 확인 없이 정지 기록) | 자율 파이프라인 드라이버나 라우터가 시각 단계를 헤드리스로 파견할 때. 사람이 직접 부르는 경우에는 /cc-cmds:visual 을 쓸 것 |
<!-- SKILLS_TABLE_END -->
/cc-cmds:design, /cc-cmds:design-audit 등 에이전트 팀 기반 커맨드는 Claude Code 2.1.178 이상이 필요합니다. 팀원은 nameless background task(Agent 도구)로 구동되므로 별도 환경변수 설정은 필요 없습니다(이전에 안내하던 CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS는 더 이상 요구되지 않습니다).
1단계 — 설치 (필수 선결 조건):
brew install terminal-notifier
brew install jq # PreToolUse hook 의존성
2단계 — 자연어 요청:
사용 예시 (대화 중 이렇게 말하면 됩니다):
| 이렇게 말하세요 | 동작 |
|---|---|
"npm run build 끝나면 알림 줘" | 단발 — 완료 시 1회 |
"배포 완료되면 알려줘" | 단발 — 완료 시 1회 |
"PR 리뷰 시작할 때랑 끝날 때 알림 줘" | 단발 (2회) — 각 시점 발송 |
"lint 끝날 때, 빌드 끝날 때, 배포 완료 시 알림 줘" | 단발 (3회) — 각 시점 발송 |
"작업이 70% 정도 끝나면 알림 줘" | 단발 — 모델 추정 시점 1회 (근사값) |
"매 단계마다 알림 줘" | 반복 — 매 turn 발송; "알림 취소"로 중단 |
"알림 취소" | 반복·단발 모두 취소 |
※ 진행률·중간 지점 표현 주의 — "70% 정도 끝나면", "중간쯤 되면"처럼 백분율이나 중간 지점을 지정하면, 모델이 작업을 진행하며 스스로 추정한 시점에 알림을 1회 보냅니다. 진행률 측정기나 타이머가 따로 동작하는 것이 아니므로 실제 발송 시점은 모델의 주관적 판단에 따른 근사값입니다.
3단계 — 최초 macOS 권한 승인 (1단계 완료 후): 첫 알림 시 macOS 권한 다이얼로그가 표시됩니다. 미리 트리거하려면 "알림 테스트 한 번 해줘"로 발화하여 테스트 알림을 받고 허용을 클릭하세요. (Claude Code의 Bash 권한 다이얼로그는 플러그인의 PreToolUse hook이 자동 승인하므로 표시되지 않습니다 — macOS 알림 권한 다이얼로그만 1회 응답하면 됩니다.) 다이얼로그를 놓쳤다면 시스템 설정 → 알림 → terminal-notifier에서 수동 활성화. 권한 거부 후 복구는 셸에서 terminal-notifier -message 'cc-cmds permission test' -title '[cc-cmds] test' -group cc-cmds-active-notify -execute ':' 직접 실행으로 재트리거 (스킬 bypass와 동일 형식이라 banner 외관이 일치).
배너 클릭 — active-notify 배너, autopilot 런 배너, 일반 세션 훅 배너를 클릭하면 그 배너를 띄운 세션의 iTerm2 창·탭으로 이동하고 tmux window·pane까지 선택합니다. iTerm2 tmux 통합(tmux -CC)에서도, iTerm2 탭 안의 일반 tmux에서도 동작합니다. iTerm2가 아닌 터미널, tmux 밖의 세션, 이미 닫힌 pane에서는 클릭해도 아무 일도 일어나지 않습니다. 처음 클릭할 때 macOS가 iTerm2 제어(자동화)를 허용할지 한 번 묻고, 거부하면 클릭은 아무 일도 하지 않습니다. 이 선택은 시스템 설정 → 개인 정보 보호 및 보안 → 자동화에서 되돌릴 수 있습니다.
terminal-notifier가 없거나 macOS가 아니면 알림은 오류 없이 비활성화됩니다.
/cc-cmds:visual 은 설계 문서의 화면을 로컬에서 만들고 검토한 뒤, Claude Design 에 발행하고 캔버스 편집을 되당겨 구현을 승인하는 대화형 스킬입니다. 무인 런에서는 /cc-cmds:visual-unattended 가 화면을 검토 통과 까지만 올리고, 발행·동기·구현 승인은 아침에 사람 좌석에서 /cc-cmds:visual 로 합니다.
~/.config/cc-cmds/claude-design 의 account = <주소> 와 claude auth status 의 계정이 같을 때만 진행합니다. 이 파일은 사람이 직접 쓰며, 없거나 다르면 그 국면에서 멈춥니다.~/.config/cc-cmds/visual-bans 로 심어지고, 검토자가 찾은 새 패턴은 사람이 하나씩 승인한 뒤에만 더해집니다. 레포 디자인 시스템의 금지 해제 줄로 항목별로 끌 수 있습니다.# 1. 마켓플레이스 등록
/plugin marketplace add Nharu/cc-cmds
# 2. 플러그인 설치
/plugin install cc-cmds@cc-cmds
/cc-cmds:design <task>
/cc-cmds:design-lite <task>
/cc-cmds:design-audit <design-doc-path> [<note>] [--base]
/cc-cmds:design-upgrade
/cc-cmds:implement <design-doc-path>
/cc-cmds:review [<target>] [<directive>]
/cc-cmds:review-lite [<target>]
각 커맨드의 옵션·입력 형태 세부는 아래 Options 섹션 참조.
대화형 세션에서는 상태 표시줄이 고른 autopilot 런의 상태를 autopilot 패널로 보여 주며(살아 있는 런마다 한 번 저절로 열림), /autopilot-status 로 그 패널을 열고 닫는다. 패널은 전체 화면 세션에서 110칸 이상이라 도킹될 때만 보이고, 그 밖의 화면에서는 명령이 안내 한 줄만 띄운다 — 동작 규약과 전체 화면을 켜는 법은 plugins/cc-cmds/hooks/README.md 참조.
/plugin update cc-cmds
/plugin uninstall cc-cmds
<!-- SKILLS_OPTIONS_START -->
Usage: /cc-cmds:autopilot [의도 또는 대상] [--report]
| Option | Default | Summary |
|---|---|---|
<의도 또는 대상> | (optional) | 이 런이 무엇에 관한 것인지 — 설계 문서 경로(.md), 레포 슬러그, PR·브랜치 참조, 또는 아직 산출물이 없는 자유 텍스트 의도. 앵커 종류는 1막의 진입 판정이 정한다. 같은 세션이 연 런이 멈춘 뒤 비우거나 다시·이어서·재개 같은 재개 낱말, 또는 그 런의 의도 문면 그대로 부르면 그 런의 답을 이어받아 새 런을 바로 시작한다(재킥오프). 끝나지 않은 채 멈춘 런은 다시·재킥오프 같은 재킥 낱말로 부를 때만 이어받아 닫고, 빈 인자·재개 낱말·의도 문면으로는 이어받지 않고 그 런의 재개를 안내한다. |
--report | off (킥오프 모드 — 1막 인터뷰 후 드라이버 기동) | 아침 보고 모드. 그 런의 매니페스트·원장·보고서, 설계 문서, 그리고 매니페스트나 run 행이 없을 때 킥오프 흔적을 읽어 한국어로 렌더링만 하고, 새 런을 시작하지 않는다. |
Parsing (
<의도 또는 대상>):$ARGUMENTS전체를 의도로 읽는다..md토큰이 있으면 문서 앵커 후보로 우선 해석하되, 최종 앵커 종류는 진입 판정과 사용자 확인이 정한다. 재킥오프인지는 1막 맨 앞의 판별이 닫힌 낱말 목록으로 정하고, 그 밖의 인자는 새 런이다.
Usage: (사람이 치는 커맨드가 아니다 — gate.sh act --kind router-shift 가 claude -p 로 넘긴다)
autopilot 의 Act 2b 를 대신 도는 헤드리스 좌석이다. 사람에게 묻는 자리도, 배너를 띄우는 자리도, 진행 채널을 여는 자리도 아니다 — 그 셋은 전부 리드에 남는다. 이 샤드가 하는 것은 스냅숏을 읽고 한 행위를 정해 게이트에 넘기는 것뿐이며, 끝날 때 후임이 읽을 인수인계 행 하나를 남긴다.
Usage: /cc-cmds:dashboard [<run-id>]
| Option | Default | Summary |
|---|---|---|
<run-id> | (optional) | 링크가 바로 가리킬 런 id. 없으면 전체 런 목록 링크를 낸다. |
Parsing (
<run-id>): 인자 전체를 런 id 하나로 쓴다. 비어 있으면 목록 링크다.
Usage: /cc-cmds:design <task>
| Option | Default | Summary |
|---|---|---|
<task> | (required) | 설계 토론을 진행할 작업 주제 (자유형 한국어/영문 텍스트). |
Usage: /cc-cmds:design-analyze <design-doc-path> [--no-codebase] [--report-only]
| Option | Default | Summary |
|---|---|---|
<design-doc-path> | (required) | 분석 대상 제3자 설계 문서 경로 (.md). 원본은 절대 수정하지 않음. |
--no-codebase | off (코드베이스 grounding 활성) | 코드베이스 교차검증 비활성화 — 문서 자체만으로 분석 (doc-only 모드). |
--report-only | off (산출물 대화형 선택) | Step 7 산출물 선택 대화만 건너뛰고 보고서만 생성. Step 6 워크스루(발견별 검토)는 그대로 유지 — 완전 비대화 아님(산출물 범위 한정 플래그). |
Usage: /cc-cmds:design-audit <design-doc-path> [<note>] [--base]
| Option | Default | Summary |
|---|---|---|
<design-doc-path> | (required) | 감사 대상 설계 문서 경로 (.md). 첫 리더 spawn 직전의 sha256으로 동결되며, 감사가 끝날 때까지 어떤 바이트도 수정되지 않는다. |
<note> | (optional) | 문서 경로 뒤 자유 텍스트. 전 리더에게 축어로 동일하게 주입되는 초점 메모 (리더별로 다르게 주면 보강 통계가 무의미해지므로 금지). |
--base | off | base 설계 문서 모드 — 기존 내용의 정합·완결만 감사하고 신규 구현 세부 제안을 금지한다. FE 파이프라인이 확장한 base 문서의 호출 형태. |
Usage: /cc-cmds:design-audit-unattended <design-doc-path> [<note>] [--base]
| Option | Default | Summary |
|---|---|---|
<design-doc-path> | (required) | 감사 대상 설계 문서 경로 (.md). 드라이버가 메인 워크트리 절대 경로로 넘긴다. 첫 리더 spawn 직전의 sha256으로 동결된다. |
<note> | (optional) | 문서 경로 뒤 자유 텍스트. 전 리더에게 축어로 동일하게 주입되는 초점 메모. |
--base | off | base 설계 문서 모드 — 기존 내용의 정합·완결만 감사하고 신규 구현 세부 제안을 금지한다. |
Usage: /cc-cmds:design-base <task> | --split <문서>
| Option | Default | Summary |
|---|---|---|
<task> | (required) | 베이스 설계를 진행할 작업 주제 (자유형 한국어/영문 텍스트). --split 모드에서는 쓰지 않는다. |
--split <문서> | off | 분할 모드 — 동결·감사를 마친 베이스 설계 문서를 다시 점검하고, 고른 트래커에 베이스 티켓과 각 티켓을 발행하거나 문서만으로 분할을 기록한다. |
Parsing (
<task>):$ARGUMENTS에--split토큰이 없으면 전체가 작업 주제다.
Parsing (
--split <문서>):--split다음의 첫.md토큰이 베이스 문서 경로다. 있으면 설계 흐름(Step 1–7)은 돌지 않는다.
Usage: /cc-cmds:design-base-unattended <brief-or-doc-path> [<task-sentence>] | --split <문서>
| Option | Default | Summary |
|---|---|---|
<brief-or-doc-path> | (required) | 좌석 파견: docs/design-brief/{slug}.md — design-base 좌석이 쓴 인터뷰 브리프. 드라이버 파견: 베이스 설계 문서 경로(메인 워크트리 절대 경로 — 아직 없을 수 있다). |
<task-sentence> | (optional) | 드라이버 파견에서만 — 매니페스트 ## 의도 의 첫 줄. |
--split <문서> | off | 분할 단계 — 감사를 마친 베이스 문서를 다시 점검하고 매니페스트 베이스 발행 행대로 트래커에 발행하거나 문서만으로 기록한다. 드라이버 파견에서만 쓴다. |
_Parsing (
<brief-or-doc-path>):$ARGUMENTS의 첫.md토큰. 어느 파견인지는CC_PIPELINE_RUN_ID의 유무로 가른다._
Parsing (
<task-sentence>): 첫.md토큰 이후의 모든 내용. 좌석 파견과--split에서는 무시한다.
Parsing (
--split <문서>):--split다음의 첫.md토큰이 베이스 문서 경로(메인 워크트리 절대 경로)다.
Usage: /cc-cmds:design-discuss-unattended <brief-or-doc-path> [<task-sentence>]
| Option | Default | Summary |
|---|---|---|
<brief-or-doc-path> | (required) | 좌석 파견: docs/design-brief/{slug}.md — 좌석이 쓴 인터뷰 브리프. 드라이버 파견: 설계 문서 경로(메인 워크트리 절대 경로, 드라이버가 준다 — 아직 없을 수 있다). |
<task-sentence> | (optional) | 드라이버 파견에서만 — 매니페스트 ## 의도 의 첫 줄. 인터뷰 브리프가 없을 때 과제 문면이 되는 한 문장. |
_Parsing (
<brief-or-doc-path>):$ARGUMENTS의 첫.md토큰. 어느 파견인지는 이 인자가 아니라CC_PIPELINE_RUN_ID의 유무로 가른다._
Parsing (
<task-sentence>): 첫.md토큰 이후의 모든 내용. 좌석 파견에서는 무시한다.
Usage: /cc-cmds:design-lite <task>
| Option | Default | Summary |
|---|---|---|
<task> | (required) | 설계 토론을 진행할 작업 주제 (자유형 한국어/영문 텍스트). |
Usage: /cc-cmds:design-reconverge <design-doc-path> <scope>
| Option | Default | Summary |
|---|---|---|
<design-doc-path> | (required) | 재수렴 대상 설계 문서 경로 (.md). 드라이버와 라우터 교대가 메인 워크트리 절대 경로로 넘긴다. |
<scope> | (required) | 재수렴 스코프. R<n> 형태의 잔여 검증 항목 식별자, (정규화 파일 경로, 카테고리 태그) 형태의 문제 동일성, 감사 종합 요구 <중단 기록 절대 경로> 형태의 채택된 감사 종합 요구, 구속 티어 이탈 <중단 기록 절대 경로> 형태의 사람이 재수렴을 고른 구속 티어 이탈, 설계 수준 잔여 <리뷰 리포트 절대 경로> 형태의 확인 리뷰가 설계 수준으로 분류한 잔여 발견, 상한 P0 잔여 <리뷰 리포트 절대 경로> 형태의 앞 런이 확인 리뷰 상한에서 남긴 P0 발견, 또는 잔여 동결 재검토 <중단 기록 절대 경로> 형태의 사람이 골격 재검토 를 고른 사다리 정지, 또는 경로 없는 문구 감사 골격급 형태의 무인 감사가 상한 안의 회차에서 낸 골격급 결함(보고서는 설계 문서에서 유도한다). |
Parsing (
<design-doc-path>):$ARGUMENTS의 첫.md토큰을 경로로 해석.
Parsing (
<scope>): 첫.md토큰 이후의 모든 내용. 비어 있으면 중단 기록을 남기고 정지 — 스코프 없는 재설계는 이 스킬이 하는 일이 아니다.
Usage: /cc-cmds:design-solo-unattended <doc-path> [<task-sentence>]
| Option | Default | Summary |
|---|---|---|
<doc-path> | (required) | 설계 문서 경로(메인 워크트리 절대 경로, 드라이버가 준다 — 아직 없을 수 있다). |
<task-sentence> | (optional) | 매니페스트 ## 의도 의 첫 줄. 인터뷰 기록이 없을 때 과제 문면이 되는 한 문장. |
_Parsing (
<doc-path>):$ARGUMENTS의 첫.md토큰. 드라이버 파견 전용이라CC_PIPELINE_RUN_ID가 비어 있으면 아무것도 쓰지 않고 끝난다._
Parsing (
<task-sentence>): 첫.md토큰 이후의 모든 내용.
Usage: /cc-cmds:design-upgrade
이 커맨드는 별도 인자를 받지 않으며, 직전 /design 팀 구성 제안이 현재 대화 컨텍스트에 있어야 동작한다. 모델 승격과 역할 추가·분할은 강화가 유의미할 때만 제안하며, 그 외에는 유지 사유를 제시한다. 독립 실행 시 결과가 불정확할 수 있다.
Usage: /cc-cmds:implement <design-doc-path> [scope-directive]
| Option | Default | Summary |
|---|---|---|
<design-doc-path> | (required) | 구현 대상 설계 문서 경로 (.md). |
[scope-directive] | (optional) | 구현 범위를 좁히는 자유형 자연어 지시문 (예: "Phase 2", "PR #0"). |
Parsing (
<design-doc-path>):$ARGUMENTS의 첫.md토큰을 경로로 해석. 이후 토큰은 scope directive로 전달.
Parsing (
[scope-directive]): 첫.md토큰 이후의 모든 내용. 단일 바깥쪽 쌍따옴표로 감싸져 있으면 그 쌍만 제거하고 안쪽 따옴표·구두점은 보존.
Usage: /cc-cmds:implement-unattended <design-doc-path> [scope-directive]
| Option | Default | Summary |
|---|---|---|
<design-doc-path> | (required) | 구현 대상 설계 문서 경로 (.md). 드라이버가 메인 워크트리 절대 경로로 넘긴다. |
[scope-directive] | (optional) | 구현 범위를 좁히는 자유형 자연어 지시문. 드라이버가 세그먼트 범위를, 라우터의 수정 재파견이 리뷰 리포트의 수정 대상을 이 자리에 싣는다. |
Parsing (
<design-doc-path>):$ARGUMENTS의 첫.md토큰을 경로로 해석. 이후 토큰은 scope directive로 전달.
Parsing (
[scope-directive]): 첫.md토큰 이후의 모든 내용. 단일 바깥쪽 쌍따옴표로 감싸져 있으면 그 쌍만 제거하고 안쪽 따옴표·구두점은 보존.
Usage: /cc-cmds:review [<target>] [--base-sha <sha>] [--declared-files <csv>] [<directive>]
| Option | Default | Summary |
|---|---|---|
<target> | (optional) | 리뷰 대상. 입력 형태에 따라 PR/브랜치/파일 모드로 자동 분기. |
<directive> | (optional) | 리뷰 관점 지시문. <target> 뒤에 자연어로 부가 (예: "보안 중심으로"). |
--base-sha <sha> | off (gh pr view … baseRefName 또는 기본 브랜치에서 base 를 스스로 유도) | diff 의 base 를 호출자가 지정. 이미 base 를 아는 호출자(파이프라인 드라이버 등)가 리뷰의 재유도를 없애기 위해 넘긴다. 넘겨받은 값은 신뢰하지 않고 git merge-base --is-ancestor 로 검증하며, 검증에 실패하면 기존 유도로 폴백하고 그 사실을 리포트 개요에 남긴다. |
--declared-files <csv> | off (변경 파일 집합을 diff 에서만 유도) | 이 변경이 건드리기로 선언된 파일 집합(쉼표 구분). diff 는 무엇이 바뀌었는지만 말하고 무엇이 바뀌기로 되어 있었는지는 말하지 않으므로, 선언 밖 파일이 리뷰 범위 안에 있을 때 그것을 지목할 수 있게 한다. |
<target> 입력 형태별 처리:
https://github.com/owner/repo/pull/42 → PR 번호 추출 후 gh pr view로 메타데이터 수집42 → 숫자만일 때 PR 번호로 해석feat/auth-flow → 하이픈·영문 포함 시 브랜치로 해석, gh pr list --head로 연관 PR 조회src/auth/ → 파일 리뷰 모드; gh 명령 사용 안 함PR #42 보안 중심으로 → 타겟 추출 후 지시문을 팀 구성·컨텍스트 패키지·보고서에 전파. 지시문은 깊이/커버리지에만 영향; severity는 기술 기준으로 독립 평가.Parsing (
<target>): 숫자만 포함된 토큰(42)은 PR 번호, 하이픈·영문 포함 토큰(42-fix-bug)은 브랜치로 해석. 순수 숫자 + 브랜치 동시 존재 시 PR 번호 우선. 어느 형태에도 해당되지 않으면AskUserQuestion으로 명확화.
Parsing (
<directive>): 지시문은 severity 기준을 변경하지 않음 — 리뷰 팀 구성과 컨텍스트 가중치에만 영향. 인식된 플래그와 그 값을 뺀 나머지가 지시문이며, 인식되지 않는--토큰은 지시문으로 흡수하지 않고 경고 후 폐기한다.
Parsing (
--base-sha <sha>):--base-sha다음 토큰을 값으로 취한다. 값이 없으면 플래그를 무시하고 기존 유도를 쓴다. 이 토큰과 값은<directive>추출 전에 인자열에서 제거된다.
Parsing (
--declared-files <csv>):--declared-files다음 토큰을 값으로 취한다. 쉼표·공백을 포함할 수 있으므로 인용 부호로 감싸 넘긴다. 이 토큰과 값은<directive>추출 전에 인자열에서 제거된다.
Usage: /cc-cmds:review-lite [<target>] [--base-sha <sha>] [--declared-files <csv>]
| Option | Default | Summary |
|---|---|---|
<target> | (optional) | 리뷰 대상 (PR 번호/URL, 브랜치, 파일/디렉토리, 또는 생략 시 현재 브랜치 자동 감지). PR 크기 무관 — 큰 PR 은 report 의 리뷰 범위 섹션에 미커버 영역 명시. |
--base-sha <sha> | off (gh pr view … baseRefName 또는 기본 브랜치에서 base 를 스스로 유도) | diff 의 base 를 호출자가 지정. 넘겨받은 값은 git merge-base --is-ancestor 로 검증하며, 실패하면 기존 유도로 폴백하고 그 사실을 리포트 개요에 남긴다. lite 에서도 동일하다. |
--declared-files <csv> | off (변경 파일 집합을 diff 에서만 유도) | 이 변경이 건드리기로 선언된 파일 집합(쉼표 구분). diff 는 무엇이 바뀌었는지만 말하므로, 선언 밖 파일을 지목할 수 있게 한다. |
Parsing (
--base-sha <sha>):--base-sha다음 토큰을 값으로 취한다. 값이 없으면 플래그를 무시하고 기존 유도를 쓴다.
Parsing (
--declared-files <csv>):--declared-files다음 토큰을 값으로 취한다. 쉼표·공백을 포함할 수 있으므로 인용 부호로 감싸 넘긴다.
Usage: /cc-cmds:review-unattended <target> [--report-path <abs-path>] [--base-sha <sha>] [--declared-files <csv>] [--seat-commits <list>] [--slice <id>] [--basis-cycle <n>] [--basis-review-head <sha>] [--basis-report-path <abs-path>] [--resolve-basis-cycle <n>] [--resolve-basis-head <sha>] [--resolve-base <sha>] [--recover --scratch-dir <abs-path>] [--lead-fix <abs-path>] [--recheck <abs-path>] [<directive>]
| Option | Default | Summary |
|---|---|---|
<target> | (required) | 리뷰 대상. 드라이버가 방금 만든 PR 번호나 브랜치를 넘긴다. |
<directive> | (optional) | 리뷰 관점 지시문. severity 기준은 바꾸지 않고 팀 구성과 컨텍스트 가중치에만 영향. |
--report-path <abs-path> | off (리포트를 cwd 상대 docs/reviews/{slug}.md에 기록) | 뒤에 오는 메인 워크트리 절대 경로에 리포트를 기록한다. 세그먼트 워크트리에서 실행될 때 리포트가 그 트리에 떨어져 철거와 함께 파괴되는 것을 막는 유일한 수단. |
--base-sha <sha> | off (gh pr view … baseRefName 또는 기본 브랜치에서 base 를 스스로 유도) | diff 의 base 를 드라이버가 지정. 드라이버는 세그먼트가 갈라져 나온 base 를 이미 알고 있으므로, 이 값이 있으면 리뷰가 그것을 다시 유도하지 않는다. 넘겨받은 값은 신뢰하지 않고 git merge-base --is-ancestor 로 검증하며, 실패하면 기존 유도로 폴백하고 그 사실을 리포트 개요에 남긴다. |
--declared-files <csv> | off (변경 파일 집합을 diff 에서만 유도) | 이 세그먼트가 건드리기로 선언된 파일 집합(쉼표 구분). diff 는 무엇이 바뀌었는지만 말하고 무엇이 바뀌기로 되어 있었는지는 말하지 않으므로, 선언 밖 파일이 리뷰 범위 안에 있을 때 그것을 지목할 수 있게 한다. |
--seat-commits <list> | off (이 세그먼트에 좌석이 적용한 강제 표면 편집이 없다) | 사람 승인 뒤 게이트가 이 세그먼트 브랜치에 만든 좌석 적용 커밋 목록(<sha> (<경로>, …); …). 그 커밋이 바꾼 파일은 선언 집합 안이므로 리뷰 범위이며, 리뷰는 그 변경을 다른 커밋과 똑같이 읽고 개요에 커밋과 경로를 한 줄로 남긴다. |
--slice <id> | off (슬라이스 범위를 적용하지 않고 모든 발견을 센다 — 개요에 적용 안 됨 — 인자 없음) | 이 세그먼트가 구현한 설계 슬라이스 id. 지시문의 설계는 <doc> 이 가리키는 설계 문서의 ## 구현 슬라이싱 에서 같은 id 의 블록을 이 슬라이스의 범위로 읽고, 다른 선언 슬라이스에 배정된 P1·P2·P3 발견을 ## 다른 슬라이스 몫 (집계 제외) 절로 옮겨 집계에서 뺀다. 문서·절·블록이 없으면 모두 센다. |
--basis-cycle <n> | off (delta mode is not attempted; review runs full) | The 사이클 number of this segment's most recent FULL review cycle. Required together with --basis-review-head and --basis-report-path to attempt delta mode — all three or none. Any one missing or malformed drops the whole attempt to a full review, never a halt. |
--basis-review-head <sha> | off (delta mode is not attempted; review runs full) | The 리뷰 HEAD of the cycle named by --basis-cycle. Verified with git merge-base --is-ancestor against the target head named explicitly, never the caller's ambient HEAD. On failure, or when the three-flag set is incomplete or malformed, the arm falls back to a full review and records why in the report overview. |
--basis-report-path <abs-path> | off (delta mode is not attempted; review runs full) | Main-worktree absolute path to the --basis-cycle report — the source of the prior findings this cycle re-adjudicates. Read-only; never written by this arm. Must be absolute or it is treated as malformed. |
--resolve-basis-cycle <n> | off (resolution mode is not attempted; review runs full) | The 사이클 number of this segment's last cycle that is not a resolution, whose 리뷰 HEAD a base resolution merged onto the base. Required together with --resolve-basis-head and --resolve-base to attempt resolution mode — all three or none. Any one missing or malformed drops the whole attempt to a full review, never a halt. |
--resolve-basis-head <sha> | off (resolution mode is not attempted; review runs full) | B — the 리뷰 HEAD of the cycle named by --resolve-basis-cycle. Must be an ancestor of the target head named explicitly. |
--resolve-base <sha> | off (resolution mode is not attempted; review runs full) | T — the base the resolution merged, as the gate's 베이스 판정 row named it. The review reads only what <target head> ^B ^T adds, with --remerge-diff. |
| --recover | off (팀을 띄워 S
hooks/autopilot-status.tsx 351 lines1// 런 상태 패널 mod. 대화형 세션에서만 깨어나, 상태 표시줄이 고른 런 하나의 상태를
2// orchestrator/run-pane.sh 에서 받아 패널에 그린다. 문구·순서·색조·부류·주기와 줄 상한은
3// 모두 그 헬퍼가 정하고, 이 모듈은 언제 헬퍼를 부르고 언제 패널을 여닫는지만 정한다.
4// 이 모듈이 플러그인의 유일한 `modules` 진입이며, 질문지 서브 mod 의 등록도 넘겨준다.
5import { atom, read, update } from 'claude-code'
6import type { Hook, Register } from 'claude-code'
7
8import type { PaneControl, PaneLine, PaneLines, PanePart } from '../types'
9import { register as registerQuestionForm } from './question-form/index'
10
11type Dollar = Parameters<Hook<'session.start'>>[0]
12
13const PANE = 'autopilot-status'
14const TITLE = 'autopilot'
15const TICK_MS = 10_000
16const IDLE_MS = 60_000
17const HELPER_TIMEOUT_MS = 8000
18// 도크가 이 폭부터 놓이므로, 그보다 좁으면 명령이 열지 않고 안내만 띄운다.
19const DOCK_MIN_COLUMNS = 110
20// 도크에 요청하는 본문 폭. 헬퍼의 --cols 기본값과 같다.
21const DOCK_COLUMNS = 44
22// 시계가 이 시간 넘게 틱을 내지 않았으면 끝난 것으로 보고 다시 건다.
23const STALL_MS = 3 * TICK_MS
24const NOT_DOCKED = 'autopilot 패널은 전체 화면(110칸 이상)에서만 보입니다'
25// 런 id 기록이 세션 내내 쌓이지 않게 끝에서부터 이만큼만 남긴다.
26const KEEP_IDS = 100
27// 헬퍼가 내는 묶음의 닫힌 집합. 여기 없는 묶음의 줄은 버린다.
28const BUNDLES = new Set([
29 'none',
30 'title',
31 'session-title',
32 'head',
33 'head-detail',
34 'gap',
35 'seg-heading',
36 'seg',
37 'seg-detail',
38 'seg-folded',
39 'gate-none',
40 'approval',
41 'block-heading',
42 'block-reason',
43 'cone-unresolved',
44 'orphan',
45 'event-heading',
46 'event',
47])
48
49const snapshot = atom({ plugin: 'cc-cmds', key: 'paneLines' } as const, {
50 lines: [],
51 warning: null,
52} as PaneLines)
53
54const control = atom({ plugin: 'cc-cmds', key: 'paneControl' } as const, {
55 autoOpenedFor: [],
56 dismissedFor: [],
57 lastRunAt: null,
58 lastMtime: null,
59 lastSid: null,
60 rid: null,
61 indexPath: null,
62 refreshMs: IDLE_MS,
63} as PaneControl)
64
65type Head = { kind: string; rid: string | null; indexPath: string | null; refreshMs: number }
66
67// 조각 하나의 색조 `<normal|dim|ok|warn|error|accent>[.b]` 를 색조와 굵기로 나눈다.
68const part = (tone: string, text: string): PanePart =>
69 tone.endsWith('.b') ? { tone: tone.slice(0, -2), bold: true, text } : { tone, bold: false, text }
70
71// 헬퍼 출력 규약 cc-pane 2: 머리 행 `cc-pane<TAB>2<TAB>부류<TAB>rid<TAB>목록 경로<TAB>refresh_ms`,
72// 이어서 `묶음<TAB><cut|wrap><TAB>색조<TAB>글[<TAB>색조<TAB>글]…` 본문. 머리 행이 규약과
73// 다르면 그 까닭을, 맞으면 머리와 본문을 돌려준다. 스키마는 정확히 2 만 받는다.
74const parse = (stdout: string): { head: Head; lines: PaneLine[] } | string => {
75 const rows = stdout.split('\n').filter(row => row !== '')
76 const cells = rows.length > 0 ? rows[0].split('\t') : []
77 if (cells.length !== 6 || cells[0] !== 'cc-pane') return '출력 형식이 맞지 않음'
78 if (cells[1] !== '2') return `헬퍼 규약 cc-pane ${cells[1]} 은 읽지 않음`
79 const refreshMs = Number(cells[5])
80 const head: Head = {
81 kind: cells[2],
82 rid: cells[3] === '-' || cells[3] === '' ? null : cells[3],
83 indexPath: cells[4].startsWith('/') ? cells[4] : null,
84 refreshMs: Number.isFinite(refreshMs) && refreshMs > 0 ? refreshMs : IDLE_MS,
85 }
86 const lines: PaneLine[] = []
87 for (const row of rows.slice(1)) {
88 const [bundle, mode, ...rest] = row.split('\t')
89 if (!BUNDLES.has(bundle)) continue
90 const parts: PanePart[] = []
91 for (let i = 0; i < rest.length; i += 2) parts.push(part(rest[i], rest[i + 1] ?? ''))
92 lines.push({ bundle, wrap: mode === 'wrap', parts })
93 }
94
95 return { head, lines }
96}
97
98const remember = (ids: readonly string[], id: string): string[] =>
99 ids.includes(id) ? [...ids] : [...ids, id].slice(-KEEP_IDS)
100
101// ui.close 뒤의 닫힘 기록. 사람이 닫은 것만 현재 rid 로 기록한다. 명령의 닫기(plugin)는
102// 명령 처리기가 직접 기록하고, 적재 해제(unload)는 기록하지 않는다.
103export const noteClose = (c: PaneControl, origin: string): PaneControl =>
104 origin === 'person' && c.rid !== null ? { ...c, dismissedFor: remember(c.dismissedFor, c.rid) } : c
105
106// 색조를 테마 키나 Text 속성으로 바꾼다. 모르는 색조는 normal 처럼 속성 없이 그린다.
107const toneProps = (tone: string, bold: boolean) => {
108 const weight = bold ? ({ bold: true } as const) : {}
109 switch (tone) {
110 case 'ok':
111 return { color: 'success', ...weight } as const
112 case 'warn':
113 return { color: 'warning', ...weight } as const
114 case 'error':
115 return { color: 'error', ...weight } as const
116 case 'accent':
117 return { color: 'suggestion', ...weight } as const
118 case 'dim':
119 return { dimColor: true, ...weight } as const
120 default:
121 return weight
122 }
123}
124
125// 아래는 모두 모듈 변수다. 모듈이 다시 적재되면 초기값으로 돌아가고, 이전 시계도 함께
126// 사라진다. 그래서 재적재 뒤에는 첫 띠 렌더 전까지 저절로 열지 않고, 첫 도크 렌더
127// 전까지 헬퍼 기본 폭을 쓴다.
128// 헬퍼는 한 번에 하나만 돈다.
129let isRunning = false
130let timer: { cancel: () => void } | undefined
131// session.start 가 대화형·비파이프라인 가드를 통과해 시계를 걸었는지. 이 세션에서만
132// 시계를 다시 건다.
133let isArmed = false
134// 마지막 틱이 시작한 시각. 시계를 건 시각이 초기값이다.
135let lastTickAt = 0
136// 직전 틱이 본 패널의 놓임.
137let wasPlaced = false
138// 띠 렌더가 알려 준 전체 화면 여부. 알기 전에는 undefined.
139let layout: boolean | undefined
140// 마지막 도크 렌더의 본문 폭과 행 수. 도크로 그려지기 전에는 undefined.
141let dock: { columns: number; rows: number } | undefined
142
143const isCount = (n: unknown): n is number => typeof n === 'number' && Number.isInteger(n) && n > 0
144
145async function statMtime($: Dollar, path: string | null): Promise<number | null> {
146 if (path === null || !path.startsWith('/')) return null
147 try {
148 return (await $.fs.stat(path)).mtimeMs
149 } catch {
150 // 목록이 아직 없거나 지워졌다(ENOENT). 「없음」으로 본다.
151 return null
152 }
153}
154
155// 헬퍼를 한 번 돌리고 결과를 상태에 반영한다. 실패하면 마지막 좋은 줄을 두고 경고 줄만
156// 더하며, 자동으로 열지 않는다. tickPath 는 tickMtime 을 잰 목록 경로이고, startedAt 은
157// 헬퍼를 부르기 전에 읽은 시각이다. 헬퍼가 걸린 시간을 다음 기한에 더하지 않도록
158// lastRunAt 에는 이 값을 적는다.
159async function runHelper($: Dollar, sid: string, tickMtime: number | null, tickPath: string | null, startedAt: number) {
160 if (isRunning) return
161 isRunning = true
162 try {
163 let parsed: ReturnType<typeof parse> = '실행하지 못함'
164 let failure: string | null = null
165 const size = dock === undefined ? [] : ['--cols', String(dock.columns), '--rows', String(dock.rows)]
166 try {
167 const ran = await $.process.run(['bash', `${$.plugin.root}/orchestrator/run-pane.sh`, sid, ...size], {
168 timeoutMs: HELPER_TIMEOUT_MS,
169 })
170 if (ran.exitCode !== 0) failure = `종료 코드 ${ran.exitCode}`
171 else parsed = parse(ran.stdout)
172 } catch {
173 failure = '실행하지 못함'
174 }
175
176 if (failure !== null || typeof parsed === 'string') {
177 const why = failure ?? parsed
178 await update($, snapshot, s => ({ ...s, warning: `⚠ 런 상태를 읽지 못했다 (${why})` }))
179 await update($, control, c => ({ ...c, lastRunAt: startedAt, lastSid: sid, lastMtime: tickMtime }))
180
181 return
182 }
183
184 const { head, lines } = parsed
185 const mtime = head.indexPath === tickPath ? tickMtime : await statMtime($, head.indexPath)
186 await update($, snapshot, () => ({ lines, warning: null }))
187 const before = await update($, control, c => ({
188 ...c,
189 lastRunAt: startedAt,
190 lastSid: sid,
191 lastMtime: mtime,
192 rid: head.rid,
193 indexPath: head.indexPath,
194 refreshMs: head.refreshMs,
195 }))
196
197 // 자동 열기: 전체 화면인 줄 안 세션에서, 살아 있는 런마다 한 번, 사람이 닫은 적 없는
198 // 런만, 패널이 이미 열려 있지 않을 때만. 놓였는지가 아니라 열려 있는지로 본다 —
199 // 좁은 터미널에서 기다리는 패널도 열려 있는 것이다.
200 const rid = head.rid
201 if (layout !== true || head.kind !== 'live' || rid === null) return
202 if (before.autoOpenedFor.includes(rid) || before.dismissedFor.includes(rid)) return
203 const panes = await $.ui.panes()
204 if (panes.some(pane => pane.id === PANE)) return
205 await update($, control, c => ({ ...c, autoOpenedFor: remember(c.autoOpenedFor, rid) }))
206 await $.ui.open({ id: PANE, title: TITLE, columns: DOCK_COLUMNS })
207 } finally {
208 isRunning = false
209 }
210}
211
212// 매 틱은 프로세스 없이 세션 id·패널·목록 mtime 만 보고, 조건이 맞을 때만 헬퍼를 부른다.
213// 기한에는 반 틱의 여유를 둔다. 틱마다 시계를 읽기까지의 지연이 엇갈려도 놓인 live
214// 패널이 한 틱을 건너뛰지 않게 하기 위해서다.
215async function tick($: Dollar) {
216 const now = await $.clock.now()
217 lastTickAt = now
218 if (isRunning) return
219 const sid = await $.session.id()
220 const panes = await $.ui.panes()
221 const isPlaced = panes.find(one => one.id === PANE)?.isPlaced === true
222 // 놓임을 알리는 사건이 없으므로, 거짓에서 참으로 바뀐 것을 본 틱에서 곧바로 돌린다.
223 const becamePlaced = isPlaced && !wasPlaced
224 wasPlaced = isPlaced
225 const c = await read($, control)
226 const mtime = await statMtime($, c.indexPath)
227 const period = isPlaced ? c.refreshMs : IDLE_MS
228 const isDue =
229 c.lastRunAt === null ||
230 sid !== c.lastSid ||
231 mtime !== c.lastMtime ||
232 becamePlaced ||
233 now - c.lastRunAt >= period - TICK_MS / 2
234 if (isDue) await runHelper($, sid, mtime, c.indexPath, now)
235}
236
237// 시계 콜백은 tick 을 기다리지 않고 곧바로 돌아온다. 그래야 주기가 고정된 채로 남는다.
238const arm = ($: Dollar) => {
239 timer?.cancel()
240 timer = $.clock.every(TICK_MS, () => {
241 tick($).catch(() => undefined)
242 })
243}
244
245// 시계는 거부된 주기에서 끝날 수 있다. 시계를 건 세션에서 틱이 오래 없었으면 다시 건다.
246async function rearmIfStalled($: Dollar) {
247 if (!isArmed) return
248 const now = await $.clock.now()
249 if (now - lastTickAt <= STALL_MS) return
250 lastTickAt = now
251 arm($)
252}
253
254export const register: Register = (on, options) => {
255 on('session.start', async ($, e, next) => {
256 if (!e.isInteractive) return next(e)
257 const segment = await $.env.get('CC_PIPELINE_SEGMENT')
258 const runId = await $.env.get('CC_PIPELINE_RUN_ID')
259 const stageId = await $.env.get('CC_PIPELINE_STAGE_ID')
260 const shiftId = await $.env.get('CC_PIPELINE_SHIFT_ID')
261 const reentry = await $.env.get('CC_PIPELINE_REENTRY')
262 if (segment || runId || stageId || shiftId || reentry) return next(e)
263
264 await $.command.register({
265 name: 'autopilot-status',
266 description: '런 상태 패널을 열거나 닫는다',
267 immediate: true,
268 })
269 lastTickAt = await $.clock.now()
270 isArmed = true
271 arm($)
272
273 return next(e)
274 })
275
276 // 명령은 「놓여 있음」으로 토글한다. 사람이 보는 것은 놓인 패널뿐이기 때문이다. 놓이지
277 // 않은 패널을 도킹할 수 없는 화면이면 열지 않고 안내 한 줄만 띄운다.
278 on('command.run', { command: 'autopilot-status' }, async ($, e) => {
279 layout = e.presentation.isFullscreen
280 await rearmIfStalled($)
281 const pane = (await $.ui.panes()).find(one => one.id === PANE)
282 if (pane?.isPlaced) {
283 // 이 닫기는 출처가 plugin 으로 오므로 ui.close 훅이 기록하지 않는다. 여기서 기록한다.
284 const { rid } = await read($, control)
285 if (rid !== null) await update($, control, c => ({ ...c, dismissedFor: remember(c.dismissedFor, rid) }))
286 await $.ui.close({ id: PANE })
287
288 return {}
289 }
290 if (!e.presentation.isFullscreen || e.presentation.columns < DOCK_MIN_COLUMNS) {
291 $.ui.toast(`${NOT_DOCKED} — plugins/cc-cmds/hooks/README.md`)
292
293 return {}
294 }
295 await $.ui.open({ id: PANE, title: TITLE, columns: DOCK_COLUMNS })
296 const sid = await $.session.id()
297 const c = await read($, control)
298 const mtime = await statMtime($, c.indexPath)
299 await runHelper($, sid, mtime, c.indexPath, await $.clock.now())
300
301 return {}
302 })
303
304 // 사람이 닫은 런으로는 다시 저절로 열지 않는다. 기록이 실패해도 닫기는 막지 않는다.
305 on('ui.close', async ($, e, next) => {
306 await rearmIfStalled($)
307 if (e.id === PANE && e.origin.kind === 'person') await update($, control, c => noteClose(c, 'person'))
308
309 return next(e)
310 }).catch(($, e, next) => next(e))
311
312 // 띠는 그리지 않고 지나가며, 이 세션이 전체 화면인지만 적는다. 자동 열기는 이것을 본다.
313 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
314 layout = e.viewport?.isFullscreen
315
316 return next(e)
317 })
318
319 on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
320 const { Box, Text } = $.ui.resolve(e)
321 if (e.props.placement !== 'dock') return <Text dimColor wrap="truncate-end">{NOT_DOCKED}</Text>
322 // 도크의 폭과 행 수는 다음 헬퍼 실행이 줄 상한과 감기 추정에 쓴다.
323 const columns = e.props.bodyColumns
324 const rows = e.props.scroll.bodyRows
325 if (isCount(columns) && isCount(rows)) dock = { columns, rows }
326 const { lines, warning } = await read($, snapshot)
327
328 // 한 줄은 바깥 Text 하나이고 조각은 그 안의 Text 다. 그래야 조각마다 색이 남은 채
329 // 줄 전체가 한 번 감기거나 한 번 잘린다. 빈 줄은 높이를 잃지 않게 공백 하나로 그린다.
330 return (
331 <Box flexDirection="column">
332 {lines.length === 0 && warning === null && <Text dimColor>런 상태를 읽는 중</Text>}
333 {lines.map(line => (
334 <Text wrap={line.wrap ? 'wrap' : 'truncate-end'}>
335 {line.parts.every(one => one.text === '')
336 ? ' '
337 : line.parts.map(one => <Text {...toneProps(one.tone, one.bold)}>{one.text}</Text>)}
338 </Text>
339 ))}
340 {warning !== null && (
341 <Text color="warning" wrap="truncate-end">
342 {warning}
343 </Text>
344 )}
345 </Box>
346 )
347 })
348
349 registerQuestionForm(on, options)
350}
351hooks/question-form/index.tsx 501 lines1// 질문지 서브 mod. 모델이 mcp__cc-cmds__question_form 으로 여러 질문을 한 장의 패널로
2// 묻고, 사람이 제출하면 답 묶음을 플러그인 출처의 새 프롬프트로 보내고 패널을 닫는다.
3// 훅 안에서 사람을 기다리지 않는다 — 도구는 열기만 하고 턴을 끝내게 하며, 답은 누름
4// 처리기가 낸다. `$` 는 import 너머로 넘어가지 않으므로 `$` 를 쓰는 함수는 모두 이 파일에 둔다.
5import { atom, read, update } from 'claude-code'
6import type { Hook, Register } from 'claude-code'
7
8import { anyMarked } from '../pipeline-marks'
9import { bannerCall } from './banner'
10import { bundleText, counts, mimicsHeader, mintFormId } from './bundle'
11import type { FormStatus } from './bundle'
12import { SUBMIT_KEY, arrowTarget, landingKey, layoutRows, openEditorKey, paneSize, revealKeys } from './layout'
13import { drawForm } from './render'
14import type { FormHandlers } from './render'
15import {
16 INPUT_SCHEMA,
17 NO_FORM_TOAST,
18 PANE_ID,
19 STAMP,
20 TOOL_DESCRIPTION,
21 TOOL_NAME,
22 busyResult,
23 contextLine,
24 formTitle,
25 openResult,
26 sentToast,
27 statusLine,
28 unavailableResult,
29} from './spec'
30import type { FormInput } from './spec'
31import {
32 advance,
33 cancel,
34 carryDrafts,
35 choose,
36 closeEditor,
37 commitText,
38 decideCall,
39 expired,
40 isOpen,
41 jump,
42 openEditor,
43 openRecord,
44 personClose,
45 placed,
46 reopen,
47 restorable,
48 retreat,
49 submit,
50 typeText,
51} from './transitions'
52import type { FormEditor, FormRecord, Move } from './transitions'
53import { normalizeForm, validateForm } from './validate'
54
55type Dollar = Parameters<Hook<'session.start'>>[0]
56
57const recordAtom = atom({ plugin: 'cc-cmds', key: 'questionForm.record' } as const, null as FormRecord | null)
58const lastOriginAtom = atom({ plugin: 'cc-cmds', key: 'questionForm.lastPersonOrigin' } as const, null as string | null)
59const focusedAtom = atom({ plugin: 'cc-cmds', key: 'questionForm.focusedElement' } as const, null as string | null)
60
61const STORE_PREFIX = 'questionForm.open.'
62
63// 마지막으로 그린 패널 본문의 칸 수. 크기를 잴 때 줄이 감기는 폭으로 쓴다. 그리는 중에는
64// 상태를 쓸 수 없으므로 모듈 변수에 둔다 — 틀려도 크기 요청이 조금 어긋날 뿐이다.
65let lastBodyColumns = 80
66
67// 머리줄을 흉내 낸 프롬프트 가운데 이 플러그인의 제출이 아닌 것에 도장을 단다.
68const needsStamp = (e: { text: string; origin: { kind: string; name?: unknown } }) =>
69 mimicsHeader(e.text) && !(e.origin.kind === 'plugin' && e.origin.name === 'cc-cmds')
70
71// 도구 호출이 상태를 쓰기 전의 기록. .catch 가 이 호출이 만든 것을 되돌릴 때 읽는다.
72const before = new Map<string, FormRecord | null>()
73
74// 표지 다섯을 리터럴로 읽는다. 셸 쪽 라우터 술어(넷)의 엄격한 상위 집합이다.
75async function pipelineMarked($: Dollar): Promise<boolean> {
76 return anyMarked([
77 await $.env.get('CC_PIPELINE_SEGMENT'),
78 await $.env.get('CC_PIPELINE_STAGE_ID'),
79 await $.env.get('CC_PIPELINE_SHIFT_ID'),
80 await $.env.get('CC_PIPELINE_RUN_ID'),
81 await $.env.get('CC_PIPELINE_REENTRY'),
82 ])
83}
84
85// 프로세스를 넘는 보관: 열린 기록만 세션 id 아래에 두고, 아니면 지운다. 키는 기록을 연
86// 세션의 것이고, 기록이 없으면 sessionId 를, 그것도 없으면 지금 세션을 쓴다. 보관 실패는
87// 질문지 자체를 막지 않는다.
88async function mirror($: Dollar, rec: FormRecord | null, sessionId?: string) {
89 try {
90 const key = STORE_PREFIX + (rec?.sessionId ?? sessionId ?? (await $.session.id()))
91 if (isOpen(rec ?? undefined)) await $.store.set(key, rec)
92 else await $.store.delete(key)
93 } catch {
94 // 보관은 덤이다.
95 }
96}
97
98async function writeRecord($: Dollar, rec: FormRecord | null) {
99 await update($, recordAtom, () => rec)
100 await mirror($, rec)
101}
102
103// 열린 기록이 바뀌는 자리에서 포커스 값을 그 기록의 착지 key 로 맞추고, 열린 기록이 없으면
104// 비운다. 앞 질문지의 key 가 남아 ↑/↓ 가 엉뚱한 자리부터 세지 않게 한다.
105async function landFocus($: Dollar, rec: FormRecord | null) {
106 try {
107 await update($, focusedAtom, () => (rec && isOpen(rec) ? landingKey(rec) : null))
108 } catch {
109 // 미리보기와 ↑/↓ 의 출발점만 어긋난다.
110 }
111}
112
113// 포커스가 key 로 옮겨진 뒤: key 를 적고, 그 key 로 다시 그린 줄 모형에서 그 항목을
114// 보이게 한다. 적은 뒤에 부르는 것은 미리보기 줄이 생기거나 사라진 트리에서 찾게 하려는 것이다.
115async function follow($: Dollar, key: string) {
116 await update($, focusedAtom, () => key)
117 const rec = await read($, recordAtom)
118 if (!rec || !isOpen(rec)) return
119 for (const target of revealKeys(layoutRows(rec, key), key)) {
120 try {
121 await $.ui.scroll({ to: { key: target }, in: PANE_ID, block: 'nearest' })
122 } catch {
123 // 보이게 하기는 덤이다.
124 }
125 }
126}
127
128// 사람이 포커스를 열린 입력칸이 아닌 곳으로 옮겼으면 그 칸을 닫는다. 친 글은 이미 답에
129// 적혀 있으므로, 칸이 내려간 자리에서 위 줄이 전체 글을 한 번만 보인다(엔진은 포커스 없는
130// Input 을 한 줄로 잘라 그려 같은 글이 두 번 보였다). 다시 열면 그 글이 씨앗이다.
131// 직전 포커스 값과 견주지 않는다 — 그 값이 실제 고리와 어긋나도 칸은 닫혀야 한다.
132async function leaveEditor($: Dollar, element: string | undefined) {
133 const rec = await read($, recordAtom)
134 const open = rec && isOpen(rec) ? openEditorKey(rec) : undefined
135 if (open === undefined || open === element) return
136 await change($, r => ({ record: openEditorKey(r) === open ? closeEditor(r) : r }))
137}
138
139// 패널을 열 때 함께 주는 제목과 크기. 크기는 지금 보이는 줄에서 잰다.
140type Frame = { title: string; rows: number; columns: number }
141
142function frameOf(rec: FormRecord): Frame {
143 const { answered, total } = counts(rec.form, rec.drafts)
144 return { title: formTitle(rec.form.title, answered, total), ...paneSize(rec, lastBodyColumns) }
145}
146
147const sameFrame = (a: Frame, b: Frame) => a.title === b.title && a.rows === b.rows && a.columns === b.columns
148
149const openPane = ($: Dollar, rec: FormRecord, focus?: true) =>
150 $.ui.open({ id: PANE_ID, ...frameOf(rec), ...(focus ? { focus } : {}) })
151
152const statusOf = (rec: FormRecord) => {
153 const { answered, total } = counts(rec.form, rec.drafts)
154 return statusLine(answered, total)
155}
156
157// 새로 연 질문지의 배너. 실패는 삼킨다 — 배너가 실패해도 질문지는 열린다.
158async function banner($: Dollar, form: FormInput) {
159 try {
160 const call = bannerCall($.plugin.root, await $.session.id(), form)
161 await $.process.run(call.argv, { stdin: call.stdin, env: call.env, timeoutMs: call.timeoutMs })
162 } catch {
163 // 배너 없이 연다.
164 }
165}
166
167// 다시 불러온 뒤나 프로세스를 넘어 되살린 질문지를 요청 없이 다시 연다. 배치 문턱에
168// 못 미치면 그려지지 않으므로 상태 줄을 띄운다.
169async function reshow($: Dollar, rec: FormRecord) {
170 const opened = await openPane($, rec)
171 const next = placed(rec, opened.isPlaced)
172 await writeRecord($, next)
173 await landFocus($, next)
174 await $.ui.status(next.hidden ? statusOf(next) : undefined)
175}
176
177// 이 세션이 연 기록만 되살린다. 다른 세션의 기록은 상태에서 내리고, 그 보관 키는 그
178// 세션으로 돌아올 때를 위해 남긴다. 7일이 지난 보관은 이 세션의 것이어도 지운다.
179async function restore($: Dollar) {
180 const sid = await $.session.id()
181 const now = await $.clock.now()
182 const held = restorable((await read($, recordAtom)) ?? undefined, sid)
183 if (!held) {
184 await update($, recordAtom, r => (isOpen(r ?? undefined) ? null : r))
185 await landFocus($, null)
186 }
187 let show = held
188 for (const key of await $.store.keys()) {
189 if (!key.startsWith(STORE_PREFIX)) continue
190 const stored = (await $.store.get(key)) as FormRecord | undefined
191 if (key === STORE_PREFIX + sid) {
192 if (held) continue
193 show = restorable(stored, sid, now)
194 if (!show) await $.store.delete(key)
195 } else if (!stored || expired(stored, now)) {
196 await $.store.delete(key)
197 }
198 }
199 if (show) await reshow($, show)
200}
201
202// 프로세스 안 /resume 으로 앞 대화에 돌아오면 session.start 가 오지 않아 restore 가 돌지
203// 않는다. 그래서 사람이 그 대화에서 프롬프트나 /question-form 을 칠 때, 메모리에 기록이
204// 없으면 지금 세션의 보관을 되살린다. 7일이 지난 보관은 되살리지 않고 지운다.
205async function rejoin($: Dollar) {
206 if ((await read($, recordAtom)) !== null) return
207 const sid = await $.session.id()
208 const key = STORE_PREFIX + sid
209 const stored = (await $.store.get(key)) as FormRecord | undefined
210 if (!stored) return
211 const alive = restorable(stored, sid, await $.clock.now())
212 if (alive) await reshow($, alive)
213 else await $.store.delete(key)
214}
215
216// 묶음을 내는 공통 경로: CAS 로 기록을 소비한 쪽만 제출한다. 묶음을 낸 자리에서 패널을
217// 닫고 기록을 버린다 — 답은 묶음이 들고 가고, 다음 질문지는 새로 열린다.
218async function finish($: Dollar, status: FormStatus) {
219 let taken: FormRecord | undefined
220 await update($, recordAtom, r => {
221 taken = status === '제출' ? submit(r ?? undefined) : cancel(r ?? undefined)
222 return taken ? null : r
223 })
224 if (!taken) return
225 const done = taken
226 await landFocus($, null)
227 await mirror($, null, done.sessionId)
228 await $.prompt.submit({ text: bundleText(done.id, status, done.form, done.drafts) })
229 await $.ui.status(undefined)
230 await $.ui.close({ id: PANE_ID })
231 if (status === '제출') {
232 const { answered, total } = counts(done.form, done.drafts)
233 await $.ui.toast(sentToast(answered, total))
234 }
235}
236
237// 열린 기록을 순수 전이로 바꾼다. 제목이나 바라는 크기가 바뀌었으면 패널을 다시 열고,
238// 전이가 포커스 자리를 정했으면 그리로 옮긴다(옮기지 못해도 답은 그대로다). 옮겼으면 그
239// 자리를 적고 보이게 하는 일도 여기서 한다 — 자기 $.ui.focus 가 자기 ui.focus 훅을
240// 지나는지에 기대지 않는다.
241async function change($: Dollar, fn: (rec: FormRecord) => { record: FormRecord; focus?: string }) {
242 let after: { record: FormRecord; focus?: string } | undefined
243 let was: Frame | undefined
244 await update($, recordAtom, r => {
245 if (!isOpen(r ?? undefined)) return r
246 const rec = r as FormRecord
247 was = frameOf(rec)
248 after = fn(rec)
249 return after.record
250 })
251 if (!after || !was) return
252 await mirror($, after.record)
253 if (!sameFrame(frameOf(after.record), was)) await openPane($, after.record)
254 if (after.focus) {
255 try {
256 const r = await $.ui.focus({ requestId: PANE_ID, key: after.focus })
257 if (!r.deny) await follow($, after.focus)
258 } catch {
259 // 포커스는 덤이다.
260 }
261 }
262}
263
264// 전이가 말한 이동을 포커스 자리로: 다음 질문의 첫 조작부, 질문이 끝나면 [제출].
265const focusOf = (rec: FormRecord, move: Move) => (move === 'next' ? landingKey(rec) : move === 'end' ? SUBMIT_KEY : undefined)
266
267function handlers($: Dollar): FormHandlers {
268 const run = (fn: (rec: FormRecord) => { record: FormRecord; focus?: string }) => void change($, fn).catch(() => undefined)
269 const moved = (r: { record: FormRecord; move: Move }) => ({ record: r.record, focus: focusOf(r.record, r.move) })
270 const step = (go: (rec: FormRecord) => FormRecord | undefined) => (rec: FormRecord) => {
271 const next = go(rec)
272 return next ? { record: next, focus: landingKey(next) } : { record: rec, focus: SUBMIT_KEY }
273 }
274 return {
275 choose: (qid, label) => run(rec => moved(choose(rec, qid, label))),
276 jump: qid => run(rec => {
277 const next = jump(rec, qid)
278 return { record: next, focus: landingKey(next) }
279 }),
280 openEditor: (qid: string, field: FormEditor['field']) => run(rec => {
281 const next = openEditor(rec, qid, field)
282 return { record: next, focus: landingKey(next) }
283 }),
284 typeText: (qid, field, value) => run(rec => ({ record: typeText(rec, qid, field, value) })),
285 commitText: (qid, field, value) => run(rec => moved(commitText(rec, qid, field, value))),
286 next: () => run(step(advance)),
287 prev: () => run(rec => {
288 const back = retreat(rec)
289 return back ? { record: back, focus: landingKey(back) } : { record: rec }
290 }),
291 submit: () => void finish($, '제출').catch(() => undefined),
292 cancel: () => void finish($, '취소').catch(() => undefined),
293 }
294}
295
296export const register: Register = on => {
297 // 대화형 세션에서만, 파이프라인 표지가 없을 때만 도구와 명령을 등록한다.
298 on('session.start', { isInteractive: true }, async ($, e, next) => {
299 // 등록이나 되살리기가 실패해도 세션과 같은 플러그인의 다른 시작 훅은 그대로 간다.
300 // 도구가 등록되지 않으면 스킬은 ToolSearch 결과로 그것을 알고 AskUserQuestion 으로 묻는다.
301 try {
302 if (!(await pipelineMarked($))) {
303 await $.tool.register({ name: TOOL_NAME, description: TOOL_DESCRIPTION, inputSchema: INPUT_SCHEMA })
304 await $.command.register({ name: 'question-form', description: '열린 질문지 패널을 다시 연다', immediate: true })
305 await restore($)
306 }
307 } catch {
308 // 위 주석과 같다.
309 }
310
311 return next(e)
312 })
313
314 // 매처는 validate 가 소스에서 읽으므로 상수가 아니라 리터럴로 적는다.
315 on('tool.call', { tool: 'mcp__cc-cmds__question_form' }, async ($, e) => {
316 if (e.agentId) return { deny: unavailableResult('subagent') }
317 if (await pipelineMarked($)) return { deny: unavailableResult('pipeline') }
318 if (!(await $.session.surfaces()).includes('terminal')) return { deny: unavailableResult('surface') }
319 if ((await read($, lastOriginAtom)) === 'bridge') return { deny: unavailableResult('remote') }
320
321 const args = e as unknown as Record<string, unknown>
322 const input = { title: args.title, intro: args.intro, replaces: args.replaces, questions: args.questions }
323 const current = (await read($, recordAtom)) ?? undefined
324 const invalid = validateForm(input, isOpen(current) ? current.id : undefined)
325 if (invalid) return { deny: invalid }
326 const decision = decideCall(current, input.replaces as string | undefined)
327 if (decision.kind === 'busy') return { deny: busyResult(decision.id) }
328
329 const form = normalizeForm(input as FormInput)
330 const id = mintFormId()
331 const drafts = decision.kind === 'replace' && current ? carryDrafts(current.form, current.drafts, form) : {}
332 let rec = openRecord({ id, toolUseId: e.tool_use_id, form, drafts, sessionId: await $.session.id(), now: await $.clock.now() })
333 before.set(e.tool_use_id, current ?? null)
334 await writeRecord($, rec)
335 await landFocus($, rec)
336 const opened = await openPane($, rec, true)
337 if (!opened.isPlaced) {
338 rec = placed(rec, false)
339 await writeRecord($, rec)
340 }
341 await $.ui.status(rec.hidden ? statusOf(rec) : undefined)
342 before.delete(e.tool_use_id)
343 if (decision.kind === 'new') await banner($, form)
344
345 return { result: openResult(id, form.questions.length, opened.isPlaced) }
346 }).catch(async ($, e) => {
347 // 이 호출이 만든 기록과 패널을 지우고 거절한다.
348 if (before.has(e.tool_use_id)) {
349 const prev = before.get(e.tool_use_id) ?? null
350 before.delete(e.tool_use_id)
351 try {
352 await writeRecord($, prev)
353 await landFocus($, prev)
354 if (!prev) await $.ui.close({ id: PANE_ID })
355 await $.ui.status(undefined)
356 } catch {
357 // 되돌리기도 실패하면 거절만 한다.
358 }
359 }
360
361 return { deny: unavailableResult('error') }
362 })
363
364 // 사람의 프롬프트: 머리줄을 흉내 낸 사본에 도장, 출처 기록, 돌아온 대화의 보관
365 // 되살리기, 열린 질문지가 있으면 맥락 줄. 이 mod 자신의 제출은 이 훅을 지나지 않는다.
366 on('prompt.submit', async ($, e, next) => {
367 const context = [...(e.context ?? [])]
368 const origin = e.origin
369 if (needsStamp(e)) context.push(STAMP)
370 if (origin.kind === 'composer' || origin.kind === 'bridge') {
371 await update($, lastOriginAtom, () => origin.kind)
372 try {
373 await rejoin($)
374 } catch {
375 // 되살리지 못해도 맥락 줄과 출처 기록은 그대로 간다.
376 }
377 const rec = await read($, recordAtom)
378 if (isOpen(rec ?? undefined)) context.push(contextLine((rec as FormRecord).id))
379 }
380
381 return next(context.length === (e.context ?? []).length ? e : { ...e, context })
382 }).catch(($, e, next) => {
383 // 출처 기록이나 맥락 줄이 실패해도 도장은 빠지지 않는다.
384 if (!needsStamp(e)) return next(e)
385
386 return next({ ...e, context: [...(e.context ?? []), STAMP] })
387 })
388
389 // 사람의 닫기 표시만 다룬다: 숨기고 상태 줄을 띄운다. 이 mod 자신의 닫기는 지나지 않는다.
390 on('ui.close', { id: 'cc-cmds-question-form' }, async ($, e, next) => {
391 if (e.origin.kind === 'person') {
392 const { record, showStatus } = personClose((await read($, recordAtom)) ?? undefined)
393 await writeRecord($, record ?? null)
394 if (showStatus && record) await $.ui.status(statusOf(record))
395 }
396
397 return next(e)
398 }).catch(($, e, next) => next(e))
399
400 on('ui.render', { component: 'Pane', requestId: 'cc-cmds-question-form' }, async ($, e) => {
401 const el = $.ui.resolve(e)
402 lastBodyColumns = e.props.bodyColumns
403 const rec = await read($, recordAtom)
404 const focused = await read($, focusedAtom)
405 if (rec && isOpen(rec)) {
406 return drawForm(el, layoutRows(rec, focused), rec.form.questions.map(q => q.id), handlers($))
407 }
408 const { Text } = el
409
410 return <Text dimColor>{NO_FORM_TOAST}</Text>
411 })
412
413 // 포커스 이동은 막지 않는다. 옮겨진 뒤에만 포커스를 받은 요소를 적고 보이게 한다.
414 // 맡는 것은 사람·엔진이 일으킨 이동(Tab, 클릭, autoFocus 착지)이며 출처를 가리지 않는다.
415 // 요소가 없으면(엔진 자신의 정지점) 비우기만 한다. 사람이 옮긴 것이면 열린 입력칸을 떠났는지 본다.
416 on('ui.focus', { component: 'Pane', requestId: 'cc-cmds-question-form' }, async ($, e, next) => {
417 const r = await next(e)
418 if (!r.deny) {
419 try {
420 if (e.origin?.kind === 'person') await leaveEditor($, e.element)
421 if (e.element) await follow($, e.element)
422 else await update($, focusedAtom, () => null)
423 } catch {
424 // 미리보기가 늦거나 입력칸이 열린 채 남을 뿐 답은 그대로다.
425 }
426 }
427
428 return r
429 }).catch(($, e, next) => next(e))
430
431 // 긴 질문지에서 엔진은 ↑/↓ 를 한 줄 스크롤로 보낸다. 사람의 키보드 줄 이동이면 창을
432 // 움직이지 않고 포커스를 다음·이전 정지점으로 옮긴다. 무엇을 가로챌지와 어디로 옮길지는
433 // arrowTarget 이 정하고, 이 훅은 그 결과를 옮기기만 한다 — 시험 키트는 이 mod 자신의
434 // $.ui.focus 를 돌리지 못하므로 판단은 훅 밖에서 시험한다.
435 // 이 훅의 프레임 아래에서 일어난 스크롤(보이게 하기)에는 처리기가 대신 답하므로, 처리기는
436 // 넘기기만 한다 — {} 를 돌려주면 안쪽의 보이게 하기가 막힌다.
437 on('ui.scroll', { component: 'Pane', requestId: 'cc-cmds-question-form' }, async ($, e, next) => {
438 const rec = await read($, recordAtom)
439 if (!rec || !isOpen(rec)) return next(e)
440 const focused = await read($, focusedAtom)
441 const target = arrowTarget(layoutRows(rec, focused), focused, e)
442 if (target === null) return next(e)
443 try {
444 const r = await $.ui.focus({ requestId: PANE_ID, key: target })
445 if (r.deny) return next(e)
446 } catch {
447 return next(e)
448 }
449 try {
450 await leaveEditor($, target)
451 await follow($, target)
452 } catch {
453 // 포커스는 이미 옮겨졌다.
454 }
455
456 return {}
457 }).catch(($, e, next) => next(e))
458
459 // 사람이 친 명령이므로 폭과 무관하게 패널이 놓인다.
460 on('command.run', { command: 'question-form' }, async $ => {
461 try {
462 await rejoin($)
463 } catch {
464 // 되살리지 못하면 메모리의 기록만 본다.
465 }
466 const rec = reopen((await read($, recordAtom)) ?? undefined)
467 if (!rec) {
468 await $.ui.toast(NO_FORM_TOAST)
469
470 return {}
471 }
472 await writeRecord($, rec)
473 await landFocus($, rec)
474 await openPane($, rec, true)
475 await $.ui.status(undefined)
476
477 return {}
478 })
479
480 // 프로세스가 다른 세션으로 이어지는 두 끝(/clear, 프로세스 안 /resume)에서는 끝나는
481 // 대화의 질문지를 다음 대화에 남기지 않는다. 그 대화를 버리는 /clear 는 보관도 지우고,
482 // /resume 은 보관을 남긴다. 같은 프로세스에서 그 대화로 돌아오면 첫 프롬프트나
483 // /question-form 에서 rejoin 이, 새 프로세스로 다시 열면 session.start 의 restore 가
484 // 되살린다.
485 on('session.end', async ($, e, next) => {
486 if (e.reason === 'clear' || e.reason === 'resume') {
487 await update($, recordAtom, () => null)
488 await landFocus($, null)
489 try {
490 if (e.reason === 'clear') await $.store.delete(STORE_PREFIX + e.sessionId)
491 await $.ui.close({ id: PANE_ID })
492 } catch {
493 // 보관은 덤이다.
494 }
495 await $.ui.status(undefined)
496 }
497
498 return next(e)
499 })
500}
501hooks/pipeline-marks.ts 8 lines1// 파이프라인 표지 판정. `$` 를 받지 않는 순수 함수만 둔다 — 표지 변수는
2// 부르는 쪽이 같은 파일에서 리터럴 이름으로 읽어 값만 넘긴다.
3
4// 값이 참인 표지가 하나라도 있으면 참. 정의돼 있어도 빈 문자열이면 표지가 아니다.
5export function anyMarked(values: readonly (string | undefined | null)[]): boolean {
6 return values.some(v => typeof v === 'string' && v !== '')
7}
8hooks/question-form/banner.ts 24 lines1// 새로 연 질문지의 배너: 셸 훅 session-ask-notify.sh 를 AskUserQuestion 과
2// 같은 PreToolUse stdin 모양으로 부르기 위한 인자를 만든다. `$` 를 쓰지 않으며
3// 실제 실행은 index.tsx 가 한다. agent_id 는 넣지 않는다(주 세션의 질문지다).
4
5import { TOOL_FULL_NAME } from './spec'
6import type { FormInput } from './spec'
7
8export const BANNER_TIMEOUT_MS = 5000
9
10export function bannerCall(root: string, sessionId: string, form: FormInput) {
11 const stdin = JSON.stringify({
12 hook_event_name: 'PreToolUse',
13 session_id: sessionId,
14 tool_name: TOOL_FULL_NAME,
15 tool_input: { questions: form.questions.map(q => ({ header: q.header, question: q.question })) },
16 })
17 return {
18 argv: ['/bin/bash', `${root}/hooks/session-ask-notify.sh`],
19 stdin,
20 env: { CLAUDE_PLUGIN_ROOT: root },
21 timeoutMs: BANNER_TIMEOUT_MS,
22 }
23}
24hooks/question-form/bundle.ts 139 lines1// 답 묶음: 머리줄, 머리줄 정규식, `cc-form-answers/1` JSON, 문답 기록의
2// `### 답 n` 본문과 같은 `answer` 문면. `$` 를 쓰지 않는다.
3
4import { NOT_APPLICABLE, UNANSWERED } from './spec'
5import type { Draft, Drafts, FormInput, FormOption, FormQuestion } from '../../types'
6
7export type { Draft, Drafts }
8
9export type FormStatus = '제출' | '취소'
10
11export const EMPTY_DRAFT: Draft = { selected: [], other: '', note: '' }
12
13export const HEADER_RE = /^\[cc-cmds 질문지 답\] form=(f-[0-9a-f]{8}) status=(제출|취소) /
14
15const HEADER_MARK = '[cc-cmds 질문지 답]'
16
17// 도장을 달 문면인가: 머리줄 표지가 어디에든 있으면 흉내다. 앞에 붙은 줄바꿈·공백이나
18// 분해형(NFD) 한글로 머리줄 정규식을 비껴가도 도장이 빠지지 않게 정규화한 뒤 찾는다.
19export const mimicsHeader = (text: string): boolean => text.normalize('NFC').includes(HEADER_MARK)
20
21export function mintFormId(random: () => number = Math.random): string {
22 let hex = ''
23 for (let i = 0; i < 8; i++) hex += Math.floor(random() * 16).toString(16)
24 return `f-${hex}`
25}
26
27// 입력칸 값은 한 줄 글이다. 엔진은 제어 문자를 품은 Button 라벨·text 자식을 그리지 않고
28// 패널 전체를 자기 대체 화면으로 바꾸므로, 값을 받는 자리와 그리는 자리(Button 라벨·답
29// 요약), 보내는 자리(답 묶음)에서 걸러 낸다. 줄바꿈·탭은 공백 하나로 바꾸고 나머지 제어
30// 문자(C0·DEL·C1)는 뺀다.
31export const inputValue = (v: string): string =>
32 v.replace(/\r\n|[\r\n\t]/g, ' ').replace(/[\u0000-\u001f\u007f-\u009f]/g, '')
33
34export const draftOf = (drafts: Drafts, id: string): Draft => drafts[id] ?? EMPTY_DRAFT
35
36// 고치기 전에 보관된 초안에는 제어 문자가 남아 있을 수 있으므로 거른 글로 판정한다.
37export function isAnswered(q: FormQuestion, d: Draft): boolean {
38 const other = inputValue(d.other).trim()
39 if (q.kind === 'text') return other !== ''
40 return d.selected.length > 0 || other !== ''
41}
42
43// `when` 이 가리키는 앞 질문이 보이고 그 질문에서 라벨 하나라도 골랐을 때만 보인다.
44export function isVisible(form: FormInput, drafts: Drafts, q: FormQuestion): boolean {
45 if (!q.when) return true
46 const when = q.when
47 const target = form.questions.find(x => x.id === when.id)
48 if (!target || !isVisible(form, drafts, target)) return false
49 return draftOf(drafts, target.id).selected.some(s => when.selected.includes(s))
50}
51
52export function counts(form: FormInput, drafts: Drafts): { answered: number; total: number } {
53 const visible = form.questions.filter(q => isVisible(form, drafts, q))
54 return { answered: visible.filter(q => isAnswered(q, draftOf(drafts, q.id))).length, total: visible.length }
55}
56
57// 문답 기록 `### 답 n` 본문. 라벨은 받은 그대로 쓰고 추천 접미를 붙이지 않는다 — 추천은
58// 묶음의 `recommended` 가 따로 싣는다. single 은 고른 라벨,
59// multi 는 한 줄에 하나, 선택지 대신 쓴 자유 입력은 축자, multi 라벨 옆의
60// 자유 입력은 마지막 줄 `자유 입력:`, 메모는 맨 끝 `메모:`, 답이 없으면 `미답`.
61export function answerBody(q: FormQuestion, d: Draft): string {
62 const lines: string[] = []
63 const other = d.other.trim() === '' ? '' : d.other
64 if (!isAnswered(q, d)) {
65 lines.push(UNANSWERED)
66 } else if (q.kind === 'text') {
67 lines.push(other)
68 } else if (q.kind === 'single') {
69 lines.push(d.selected.length > 0 ? d.selected[0] : other)
70 } else {
71 for (const s of d.selected) lines.push(s)
72 if (other !== '') lines.push(d.selected.length > 0 ? `자유 입력: ${other}` : other)
73 }
74 if (d.note.trim() !== '') lines.push(`메모: ${d.note}`)
75 return lines.join('\n')
76}
77
78export type BundleAnswer = {
79 id: string
80 header: string
81 group?: string
82 question: string
83 kind: FormQuestion['kind']
84 options: string[]
85 recommended?: { label: string; by: NonNullable<FormOption['recommended']> }[]
86 state: '답' | '미답' | '해당 없음'
87 selected: string[]
88 other: string
89 note: string
90 answer: string
91}
92
93export type BundleJson = { schema: 'cc-form-answers/1'; form: string; status: FormStatus; answers: BundleAnswer[] }
94
95export function bundleJson(formId: string, status: FormStatus, form: FormInput, drafts: Drafts): BundleJson {
96 const answers = form.questions.map((q): BundleAnswer => {
97 const raw = draftOf(drafts, q.id)
98 const d: Draft = { ...raw, other: inputValue(raw.other), note: inputValue(raw.note) }
99 const options = (q.options ?? []).map(o => o.label)
100 const recommended = (q.options ?? []).flatMap(o => (o.recommended ? [{ label: o.label, by: o.recommended }] : []))
101 const base = {
102 id: q.id,
103 header: q.header,
104 ...(q.group ? { group: q.group } : {}),
105 question: q.question,
106 kind: q.kind,
107 options,
108 ...(recommended.length > 0 ? { recommended } : {}),
109 }
110 if (!isVisible(form, drafts, q)) {
111 return { ...base, state: NOT_APPLICABLE, selected: [], other: '', note: '', answer: '' }
112 }
113 return {
114 ...base,
115 state: isAnswered(q, d) ? '답' : UNANSWERED,
116 selected: [...d.selected],
117 other: d.other,
118 note: d.note,
119 answer: answerBody(q, d),
120 }
121 })
122 return { schema: 'cc-form-answers/1', form: formId, status, answers }
123}
124
125export function headerLine(formId: string, status: FormStatus, answered: number, total: number): string {
126 return `[cc-cmds 질문지 답] form=${formId} status=${status} 답=${answered}/${total}`
127}
128
129export function bundleText(formId: string, status: FormStatus, form: FormInput, drafts: Drafts): string {
130 const { answered, total } = counts(form, drafts)
131 const json = JSON.stringify(bundleJson(formId, status, form, drafts))
132 return `${headerLine(formId, status, answered, total)}\n\`\`\`json\n${json}\n\`\`\``
133}
134
135export function parseHeader(text: string): { form: string; status: FormStatus } | undefined {
136 const m = HEADER_RE.exec(text)
137 return m ? { form: m[1], status: m[2] as FormStatus } : undefined
138}
139hooks/question-form/layout.ts 424 lines1// 질문지 패널의 줄 모형. 기록과 포커스에서 그릴 줄의 목록을 만들고(layoutRows), 같은
2// 줄로 패널이 바랄 크기를 잰다(paneSize). render.tsx 는 줄을 요소로 옮길 뿐이다.
3// `$` 를 쓰지 않는다.
4
5import { counts, draftOf, inputValue, isAnswered, isVisible } from './bundle'
6import {
7 ANSWER_LABEL,
8 CANCEL_LABEL,
9 EDITOR_SUBMIT_LABEL,
10 HELP_LINE,
11 NEXT_LABEL,
12 NOTE_ADD_LABEL,
13 NOTE_LABEL,
14 NOTE_PLACEHOLDER,
15 OTHER_LABEL,
16 OTHER_PLACEHOLDER,
17 PREV_LABEL,
18 SUBMIT_LABEL,
19 SUMMARY_MAX,
20 TEXT_PLACEHOLDER,
21 TO_SUBMIT_LABEL,
22 UNANSWERED,
23 counterLine,
24} from './spec'
25import type { FormOption, FormQuestion } from './spec'
26import { questionOf } from './transitions'
27import type { FormEditor, FormRecord } from './transitions'
28import type { Draft } from './bundle'
29
30// 선택지·입력칸·버튼의 key. 미리보기는 포커스 값의 q<i>-o<j> 꼴에서 선택지를 찾는다.
31export const foldedKey = (qi: number) => `q${qi}`
32export const optionKey = (qi: number, oi: number) => `q${qi}-o${oi}`
33export const otherKey = (qi: number) => `q${qi}-other`
34export const noteKey = (qi: number) => `q${qi}-note`
35export const answerKey = (qi: number) => `q${qi}-answer`
36// 입력칸의 key 는 세대마다 다르다. 표면은 Enter 뒤 그 key 의 글을 비우고, 그려진 value 는
37// 앞 그림과 값이 다를 때만 다시 적용하므로, 같은 key 로 다시 열면 빈 칸이 보인다.
38export const editorKey = (qi: number, field: FormEditor['field'], gen: number) => `q${qi}-${field}-input-${gen}`
39export const PREV_KEY = 'prev'
40export const NEXT_KEY = 'next'
41export const SUBMIT_KEY = 'submit'
42export const CANCEL_KEY = 'cancel'
43
44export type Row =
45 | { kind: 'blank' }
46 | { kind: 'title'; text: string; counter: string }
47 | { kind: 'intro'; text: string }
48 | { kind: 'group'; text: string }
49 | { kind: 'folded'; key: string; n: number; header: string; summary: string; answered: boolean }
50 | { kind: 'current'; n: number; header: string; question: string }
51 | { kind: 'detail'; text: string }
52 | { kind: 'option'; key: string; hotkey?: string; glyph: string; label: string; recommended?: string; description: string; autoFocus: boolean }
53 | { kind: 'preview'; text: string }
54 | { kind: 'other'; key: string; hotkey: string; glyph: string; text: string; description: string }
55 | { kind: 'answer'; key: string; text: string; autoFocus: boolean }
56 | { kind: 'editor'; key: string; qid: string; field: FormEditor['field']; value: string; text: string; placeholder: string; autoFocus: boolean }
57 | { kind: 'note'; key: string; hotkey: string; text: string }
58 | { kind: 'nav'; prev: boolean; next: 'question' | 'submit' | 'none' }
59 | { kind: 'actions'; submit: string; cancel: string }
60 | { kind: 'help'; text: string }
61
62const glyphOf = (q: FormQuestion, on: boolean) => (q.kind === 'multi' ? (on ? '■' : '□') : on ? '●' : '○')
63
64const cut = (s: string, max: number) => {
65 const cps = [...s.replace(/\s+/g, ' ').trim()]
66 return cps.length > max ? `${cps.slice(0, max - 1).join('')}…` : cps.join('')
67}
68
69// 선택지 설명 줄의 들여쓰기: 단축키·표지(`1: ● `) 너비만큼 라벨 밑으로 맞춘다.
70export const descriptionIndent = (hotkey?: string) => (hotkey ? 5 : 2)
71
72// 빈 줄은 둘 겹치지 않고, 맨 앞에도 두지 않는다.
73function pushBlank(rows: Row[]) {
74 const last = rows.at(-1)
75 if (last && last.kind !== 'blank') rows.push({ kind: 'blank' })
76}
77
78// 접힌 질문 줄의 답 요약 한 줄. 라벨은 추천 접미 없이 보이고, 메모가 있으면 끝에 표시한다.
79export function answerSummary(q: FormQuestion, d: Draft): string {
80 const other = inputValue(d.other).trim()
81 const parts: string[] = []
82 if (!isAnswered(q, d)) parts.push(UNANSWERED)
83 else if (q.kind === 'text') parts.push(other)
84 else {
85 for (const s of d.selected) parts.push(s)
86 if (other !== '') parts.push(`${OTHER_LABEL}: ${other}`)
87 }
88 const line = cut(parts.join(' · '), SUMMARY_MAX)
89 return inputValue(d.note).trim() === '' ? line : `${line} · +${NOTE_LABEL}`
90}
91
92// 포커스가 이 질문의 선택지 버튼에 있으면 그 선택지의 미리보기, 아니면 undefined.
93export function previewFor(qi: number, q: FormQuestion, focused: string | null): string | undefined {
94 const m = focused === null ? null : /^q(\d+)-o(\d+)$/.exec(focused)
95 if (!m || Number(m[1]) !== qi) return undefined
96 return (q.options ?? [])[Number(m[2]) - 1]?.preview
97}
98
99// 열린 입력칸의 key. 열린 칸이 없으면 undefined.
100export function openEditorKey(rec: FormRecord): string | undefined {
101 const ed = rec.editor
102 const qi = ed ? rec.form.questions.findIndex(q => q.id === ed.id) + 1 : 0
103 return ed && qi > 0 ? editorKey(qi, ed.field, ed.gen) : undefined
104}
105
106// 커서 질문이 열릴 때 포커스가 설 곳: 열린 입력칸, text 의 답 줄, 아니면 첫 선택지.
107export function landingKey(rec: FormRecord): string {
108 const qi = rec.form.questions.findIndex(q => q.id === rec.cursor) + 1
109 const q = questionOf(rec, rec.cursor)
110 if (!q || qi < 1) return SUBMIT_KEY
111 if (rec.editor && rec.editor.id === rec.cursor) return editorKey(qi, rec.editor.field, rec.editor.gen)
112 if (q.kind === 'text') return answerKey(qi)
113 return (q.options ?? []).length > 0 ? optionKey(qi, 1) : otherKey(qi)
114}
115
116// 편집기 줄의 들여쓰기와, 같은 Input 줄 안에 그려지는 「 ⏎ 확정」 몫. 엔진은 들여쓰기와
117// 상관없이 본문 폭 − 확정 몫 − 1 칸에서 글을 감고, 상자가 그보다 좁으면 감긴 첫 줄이
118// 들어가지 않아 글 전체를 한 줄로 잘라 그린다. 상자 폭(본문 폭 − 들여쓰기 − 확정 몫)이
119// 감는 폭과 같아지는 최대 들여쓰기가 1칸이다.
120export const EDITOR_INDENT = 1
121export const EDITOR_SUBMIT_WIDTH = displayWidth(` ⏎ ${EDITOR_SUBMIT_LABEL}`)
122
123// 엔진이 입력칸의 글을 감는 대로 나눈 줄들. 칸 폭은 최소 12칸이고 감는 폭은 그보다 1칸
124// 좁다. 공백 단위로 앞에서부터 채우고, 감는 폭보다 긴 한 단어는 글자 단위로 끊는다.
125// 높이를 바라는 데만 쓴다 — 글과 커서를 놓는 것은 엔진이다.
126export const editorWrapWidth = (bodyColumns: number) => Math.max(11, bodyColumns - EDITOR_SUBMIT_WIDTH - 1)
127
128export function wrapEditorText(text: string, bodyColumns: number): string[] {
129 if (text === '') return []
130 const width = editorWrapWidth(bodyColumns)
131 const lines: string[] = []
132 let line: string | null = null
133 for (const word of text.split(' ')) {
134 if (line !== null && displayWidth(`${line} ${word}`) <= width) {
135 line = `${line} ${word}`
136 continue
137 }
138 if (line !== null) lines.push(line)
139 line = ''
140 for (const ch of word) {
141 if (line !== '' && displayWidth(line + ch) > width) {
142 lines.push(line)
143 line = ''
144 }
145 line += ch
146 }
147 }
148 if (line !== null) lines.push(line)
149 return lines
150}
151
152// 편집기 줄. 앞말 라벨은 두지 않는다 — 바로 위 줄(기타·메모 줄이나 펼친 질문의 머리말)이
153// 이미 그 칸이 무엇인지 말한다. value 는 입력칸에 넘기는 씨앗이고, text 는 높이를 셀 지금 글이다.
154function editorRows(rec: FormRecord, qi: number, q: FormQuestion, field: FormEditor['field'], autoFocus: boolean): Row[] {
155 const ed = rec.editor
156 if (!ed || ed.id !== q.id || ed.field !== field) return []
157 const placeholder = field === 'note' ? NOTE_PLACEHOLDER : q.kind === 'text' ? q.placeholder ?? TEXT_PLACEHOLDER : OTHER_PLACEHOLDER
158 const d = draftOf(rec.drafts, q.id)
159 const text = inputValue(field === 'note' ? d.note : d.other)
160 return [{ kind: 'editor', key: editorKey(qi, field, ed.gen), qid: q.id, field, value: inputValue(ed.seed), text, placeholder, autoFocus }]
161}
162
163// 이 질문의 그 편집기가 열려 있는가. 그동안 위 줄은 라벨만 보인다 — 사람이 포커스를 입력칸
164// 밖으로 옮기면 칸이 닫히므로, 열려 있는 동안이 곧 그 칸에서 치는 동안이다.
165function editing(rec: FormRecord, q: FormQuestion, field: FormEditor['field']): boolean {
166 const ed = rec.editor
167 return !!ed && ed.id === q.id && ed.field === field
168}
169
170function currentRows(rec: FormRecord, qi: number, q: FormQuestion, focused: string | null): Row[] {
171 const d = draftOf(rec.drafts, q.id)
172 const landing = landingKey(rec)
173 const rows: Row[] = [{ kind: 'current', n: qi, header: q.header, question: q.question }]
174 if (q.detail) rows.push({ kind: 'detail', text: q.detail })
175 pushBlank(rows)
176 if (q.kind === 'text') {
177 const editor = editorRows(rec, qi, q, 'other', landing === editorKey(qi, 'other', rec.editor?.gen ?? 0))
178 const answer = inputValue(d.other)
179 if (editor.length > 0) rows.push(...editor)
180 else rows.push({ kind: 'answer', key: answerKey(qi), text: answer.trim() === '' ? UNANSWERED : answer, autoFocus: landing === answerKey(qi) })
181 } else {
182 ;(q.options ?? []).forEach((o: FormOption, index) => {
183 const key = optionKey(qi, index + 1)
184 rows.push({
185 kind: 'option',
186 key,
187 ...(index < 9 ? { hotkey: String(index + 1) } : {}),
188 glyph: glyphOf(q, d.selected.includes(o.label)),
189 label: o.label,
190 ...(o.recommended ? { recommended: o.recommended } : {}),
191 description: o.description,
192 autoFocus: landing === key,
193 })
194 })
195 const preview = previewFor(qi, q, focused)
196 if (preview !== undefined) rows.push({ kind: 'preview', text: preview })
197 if (q.allowOther !== false) {
198 const other = inputValue(d.other).trim()
199 const labelOnly = editing(rec, q, 'other')
200 rows.push({
201 kind: 'other',
202 key: otherKey(qi),
203 hotkey: '0',
204 glyph: glyphOf(q, other !== ''),
205 text: other === '' || labelOnly ? OTHER_LABEL : `${OTHER_LABEL}: ${other}`,
206 description: other === '' && !labelOnly ? OTHER_PLACEHOLDER : '',
207 })
208 rows.push(...editorRows(rec, qi, q, 'other', landing === editorKey(qi, 'other', rec.editor?.gen ?? 0)))
209 }
210 }
211 if (q.allowNote !== false) {
212 const note = inputValue(d.note).trim()
213 const text = editing(rec, q, 'note') ? NOTE_LABEL : note === '' ? NOTE_ADD_LABEL : `${NOTE_LABEL}: ${note}`
214 rows.push({ kind: 'note', key: noteKey(qi), hotkey: 'm', text })
215 rows.push(...editorRows(rec, qi, q, 'note', landing === editorKey(qi, 'note', rec.editor?.gen ?? 0)))
216 }
217 const ids = rec.form.questions.filter(x => isVisible(rec.form, rec.drafts, x)).map(x => x.id)
218 const at = ids.indexOf(q.id)
219 pushBlank(rows)
220 rows.push({ kind: 'nav', prev: at > 0, next: at < ids.length - 1 ? 'question' : 'submit' })
221 return rows
222}
223
224// focused 는 포커스 값(요소의 key)이다. 선택지 미리보기를 정한다.
225export function layoutRows(rec: FormRecord, focused: string | null): Row[] {
226 const { form, drafts } = rec
227 const { answered, total } = counts(form, drafts)
228 const rows: Row[] = [{ kind: 'title', text: form.title, counter: counterLine(answered, total) }]
229 if (form.intro) rows.push({ kind: 'intro', text: form.intro })
230 pushBlank(rows)
231 let group: string | undefined
232 form.questions.forEach((q, index) => {
233 if (!isVisible(form, drafts, q)) return
234 const qi = index + 1
235 if (q.group && q.group !== group) {
236 pushBlank(rows)
237 rows.push({ kind: 'group', text: q.group })
238 }
239 group = q.group
240 // 펼친 질문은 위아래 빈 줄로 접힌 줄들과 떼어 놓는다.
241 if (q.id === rec.cursor) {
242 if (rows.at(-1)?.kind !== 'group') pushBlank(rows)
243 rows.push(...currentRows(rec, qi, q, focused))
244 pushBlank(rows)
245 return
246 }
247 const d = draftOf(drafts, q.id)
248 rows.push({ kind: 'folded', key: foldedKey(qi), n: qi, header: q.header, summary: answerSummary(q, d), answered: isAnswered(q, d) })
249 })
250 pushBlank(rows)
251 rows.push({ kind: 'actions', submit: SUBMIT_LABEL, cancel: CANCEL_LABEL })
252 rows.push({ kind: 'help', text: HELP_LINE })
253 return rows
254}
255
256// 한 줄이 그리는 Button·Input 의 key. render.tsx 의 drawForm 이 다는 key 와 같아야 한다.
257function stopsOf(row: Row): string[] {
258 switch (row.kind) {
259 case 'folded':
260 case 'option':
261 case 'other':
262 case 'answer':
263 case 'editor':
264 case 'note':
265 return [row.key]
266 case 'nav':
267 return [...(row.prev ? [PREV_KEY] : []), ...(row.next !== 'none' ? [NEXT_KEY] : [])]
268 case 'actions':
269 return [SUBMIT_KEY, CANCEL_KEY]
270 default:
271 return []
272 }
273}
274
275// ↑/↓ 가 거치는 정지점: 패널에 그려지는 Button·Input 의 key 를 화면 순서로. Tab 과 같다.
276export const focusStops = (rows: Row[]): string[] => rows.flatMap(stopsOf)
277
278// 보이게 하기의 대상 상자 key. 그리기와 보이게 하기가 같은 함수를 쓴다.
279export const blockKey = (key: string) => `${key}-block`
280export const headKey = (n: number) => `q${n}-head`
281
282// 펼친 질문의 첫 정지점과 그 질문 번호. 펼친 질문이 없으면 undefined.
283export function firstStopOfCurrent(rows: Row[]): { key: string; n: number } | undefined {
284 const at = rows.findIndex(r => r.kind === 'current')
285 const head = rows[at]
286 if (!head || head.kind !== 'current') return undefined
287 const key = focusStops(rows.slice(at + 1))[0]
288 return key === undefined ? undefined : { key, n: head.n }
289}
290
291// key 로 포커스가 옮겨 갔을 때 차례대로 보이게 할 대상. 선택지는 설명까지 담은 바깥 상자를,
292// 펼친 질문의 첫 정지점이면 머리말을 먼저, 항목을 나중에 — 창이 둘을 함께 담지 못하면
293// 나중에 부른 항목이 이긴다.
294export function revealKeys(rows: Row[], key: string): string[] {
295 const row = rows.find(r => 'key' in r && r.key === key)
296 const target = row?.kind === 'option' ? blockKey(key) : key
297 const first = firstStopOfCurrent(rows)
298 return first?.key === key ? [headKey(first.n), target] : [target]
299}
300
301// 지금 포커스에서 by 칸 옮긴 정지점. 양 끝에서 멈추고 돌지 않는다. 포커스가 목록에 없으면
302// 아래로는 첫 정지점, 위로는 마지막 정지점이다. stops 는 비어 있지 않아야 한다.
303export function stepStop(stops: string[], focused: string | null, by: number): string {
304 const at = focused === null ? -1 : stops.indexOf(focused)
305 if (at < 0) return (by > 0 ? stops[0] : stops[stops.length - 1]) as string
306 return stops[Math.min(stops.length - 1, Math.max(0, at + by))] as string
307}
308
309// ui.scroll 입력 가운데 가로채기 판단에 쓰는 필드.
310export type ArrowInput = { by: number; bodyRows: number; origin?: { kind: string }; pointer?: unknown }
311
312// ↑/↓ 가로채기가 포커스를 옮길 정지점. null 이면 엔진의 스크롤로 넘긴다. 사람의 키보드 한
313// 줄 이동만 가로챈다 — 휠에는 pointer 가 붙고, 페이지 키는 bodyRows, Home/End 는
314// contentRows 만큼이다. 이미 끝에 있어 옮길 곳이 지금 자리와 같으면 넘겨, 포커스는 멈춘
315// 채 남은 줄을 볼 수 있게 한다.
316export function arrowTarget(rows: Row[], focused: string | null, input: ArrowInput): string | null {
317 if (input.origin?.kind !== 'person' || input.pointer !== undefined) return null
318 const step = Math.abs(input.by)
319 if (!(step > 0 && step < input.bodyRows)) return null
320 const stops = focusStops(rows)
321 if (stops.length === 0) return null
322 const target = stepStop(stops, focused, input.by)
323 return target === focused ? null : target
324}
325
326// 감긴 편집기 글의 마지막 줄에 커서와 조합 글자 몫 2칸이 남지 않으면 엔진은 한 줄을 더 쓴다.
327function editorText(row: Extract<Row, { kind: 'editor' }>, bodyColumns: number): string {
328 const indent = ' '.repeat(EDITOR_INDENT)
329 const lines = wrapEditorText(row.text, bodyColumns)
330 if (lines.length === 0) return `${indent}${row.placeholder} ⏎ ${EDITOR_SUBMIT_LABEL}`
331 if (displayWidth(lines.at(-1)!) + 2 > editorWrapWidth(bodyColumns)) lines.push('')
332 return lines.map((line, i) => `${indent}${line}${i === 0 ? ` ⏎ ${EDITOR_SUBMIT_LABEL}` : ''}`).join('\n')
333}
334
335// 한 줄이 보이는 대로의 글. 크기를 재는 데만 쓴다. bodyColumns 는 편집기 글을 감을 본문
336// 폭이고, 없으면 80 으로 어림한다.
337export function rowText(row: Row, bodyColumns = 80): string {
338 switch (row.kind) {
339 case 'blank':
340 return ''
341 case 'title':
342 return `${row.text} ${row.counter}`
343 case 'folded':
344 return ` ${row.n} ${row.header} ${row.summary}`
345 case 'current':
346 return `▸ ${row.n} ${row.header} ${row.question}`
347 case 'detail':
348 case 'intro':
349 return ` ${row.text}`
350 case 'option':
351 return (
352 ` ${row.hotkey ? `${row.hotkey}: ` : ''}${row.glyph} ${row.label}${row.recommended ? ` (${row.recommended})` : ''}` +
353 (row.description ? `\n${' '.repeat(2 + descriptionIndent(row.hotkey))}${row.description}` : '')
354 )
355 case 'preview':
356 return ` │ ${row.text}`
357 case 'other':
358 return ` ${row.hotkey}: ${row.glyph} ${row.text} ${row.description}`
359 case 'answer':
360 return ` ${ANSWER_LABEL}: ${row.text}`
361 case 'editor':
362 return editorText(row, bodyColumns)
363 case 'note':
364 return ` ${row.hotkey}: ${row.text}`
365 case 'nav':
366 return ` ${row.prev ? `p: ${PREV_LABEL} ` : ''}${row.next === 'question' ? `n: ${NEXT_LABEL}` : row.next === 'submit' ? `n: ${TO_SUBMIT_LABEL}` : ''}`
367 case 'actions':
368 return `[ ${row.submit} ] [ ${row.cancel} ]`
369 case 'group':
370 case 'help':
371 return row.text
372 }
373}
374
375// 터미널 칸 수: 한글·한중일 글자와 이모지는 두 칸, 나머지는 한 칸으로 어림한다.
376export function displayWidth(s: string): number {
377 let w = 0
378 for (const ch of s) {
379 const cp = ch.codePointAt(0) ?? 0
380 if (cp < 0x20 || (cp >= 0x300 && cp <= 0x36f)) continue
381 const wide =
382 (cp >= 0x1100 && cp <= 0x115f) ||
383 (cp >= 0x2e80 && cp <= 0xa4cf) ||
384 (cp >= 0xac00 && cp <= 0xd7a3) ||
385 (cp >= 0xf900 && cp <= 0xfaff) ||
386 (cp >= 0xfe30 && cp <= 0xfe4f) ||
387 (cp >= 0xff00 && cp <= 0xff60) ||
388 (cp >= 0xffe0 && cp <= 0xffe6) ||
389 (cp >= 0x1f300 && cp <= 0x1faff) ||
390 (cp >= 0x20000 && cp <= 0x3fffd)
391 w += wide ? 2 : 1
392 }
393 return w
394}
395
396export const ROWS_MIN = 6
397export const ROWS_MAX = 40
398export const COLUMNS_MIN = 60
399export const COLUMNS_MAX = 100
400
401// 감겨도 되는 산문 줄. 가로 크기를 잴 때 빼고, 세로를 잴 때는 감긴 줄 수로 센다.
402const PROSE: Row['kind'][] = ['intro', 'detail', 'preview', 'help']
403
404// 패널이 바랄 크기. 세로는 지금 보이는 줄이 bodyColumns 에서 감기는 줄 수에, 커서 질문의
405// 가장 긴 미리보기 자리를 더한 것(포커스가 옮겨 미리보기가 나타나도 크기가 흔들리지
406// 않게). 가로는 산문이 아닌 가장 긴 줄에 맞추되 60~100칸 안이다. 둘 다 요청일 뿐 결정은
407// 엔진이 한다. 포커스가 있는 입력칸은 엔진이 감긴 대로 여러 줄로 그리므로 편집기 줄도
408// 감긴 줄 수로 센다. 포커스 없이 줄을 만들어 위 줄의 전체 글까지 함께 세므로, 입력칸에
409// 포커스가 있어 위 줄이 라벨만인 동안에는 남는 쪽이다.
410export function paneSize(rec: FormRecord, bodyColumns: number): { rows: number; columns: number } {
411 const rows = layoutRows(rec, null)
412 const widest = rows
413 .filter(r => !PROSE.includes(r.kind))
414 .reduce((w, r) => Math.max(w, ...rowText(r, bodyColumns).split('\n').map(displayWidth)), 0)
415 const columns = Math.min(COLUMNS_MAX, Math.max(COLUMNS_MIN, widest + 4))
416 const width = Math.max(20, bodyColumns)
417 const lines = (s: string) => s.split('\n').reduce((n, line) => n + Math.max(1, Math.ceil(displayWidth(line) / width)), 0)
418 let height = rows.reduce((n, r) => n + lines(rowText(r, bodyColumns)), 0)
419 const q = questionOf(rec, rec.cursor)
420 const previews = (q?.options ?? []).map(o => (o.preview === undefined ? 0 : lines(o.preview)))
421 height += Math.max(0, ...previews)
422 return { rows: Math.min(ROWS_MAX, Math.max(ROWS_MIN, height + 1)), columns }
423}
424hooks/question-form/render.tsx 166 lines1// 질문지 패널의 그리기. `$` 를 받지 않는다 — 요소 표와 줄 모형, 처리기 함수를 인자로
2// 받는 순수 함수다. 처리기 안의 `$` 호출은 index.tsx 가 정의한다.
3
4import { EDITOR_SUBMIT_LABEL, NEXT_LABEL, PREV_LABEL, TO_SUBMIT_LABEL, UNANSWERED } from './spec'
5import { CANCEL_KEY, EDITOR_INDENT, NEXT_KEY, PREV_KEY, SUBMIT_KEY, blockKey, descriptionIndent, headKey } from './layout'
6import type { Row } from './layout'
7import type { FormEditor } from './transitions'
8
9// $.ui.resolve(e) 가 돌려주는 요소 표 가운데 이 패널이 쓰는 것.
10export type FormElements = { Box: any; Text: any; Button: any; Input: any }
11
12export type FormHandlers = {
13 choose: (qid: string, label: string) => void
14 jump: (qid: string) => void
15 openEditor: (qid: string, field: FormEditor['field']) => void
16 typeText: (qid: string, field: FormEditor['field'], value: string) => void
17 commitText: (qid: string, field: FormEditor['field'], value: string) => void
18 next: () => void
19 prev: () => void
20 submit: () => void
21 cancel: () => void
22}
23
24// 포커스를 받은 버튼은 엔진이 반전해 그리므로, 버튼에는 고르는 대상(번호·표지·라벨)만
25// 담고 설명·추천·답 요약은 버튼 밖에 흐리게 둔다. 강조색은 펼친 질문의 머리말에만 쓴다.
26
27// 접힌 질문 줄과 선택지 줄은 key 로 질문 번호를 되찾는다.
28const qidOfKey = (key: string) => Number(/^q(\d+)/.exec(key)?.[1] ?? 0)
29
30export function drawForm(el: FormElements, rows: Row[], ids: string[], on: FormHandlers) {
31 const { Box, Text, Button, Input } = el
32 const qid = (key: string) => ids[qidOfKey(key) - 1] ?? ''
33 const auto = (on: boolean) => (on ? { autoFocus: true as const } : {})
34 const drawn = rows.map(row => {
35 switch (row.kind) {
36 case 'blank':
37 return <Text> </Text>
38 case 'title':
39 return (
40 <Text>
41 <Text bold>{row.text}</Text>
42 <Text dimColor>{` ${row.counter}`}</Text>
43 </Text>
44 )
45 case 'intro':
46 case 'detail':
47 return (
48 <Box paddingLeft={2}>
49 <Text dimColor>{row.text}</Text>
50 </Box>
51 )
52 case 'group':
53 return <Text bold>{row.text}</Text>
54 case 'folded':
55 return (
56 <Box paddingLeft={2} flexDirection="row" gap={2}>
57 <Button key={row.key} plain onPress={() => on.jump(qid(row.key))}>
58 {`${row.n} ${row.header}`}
59 </Button>
60 {row.answered ? <Text dimColor>{row.summary}</Text> : <Text color="warning">{row.summary}</Text>}
61 </Box>
62 )
63 case 'current':
64 // 펼친 질문의 첫 정지점으로 갈 때 이 줄도 함께 보이게 하려고 key 를 단 상자로 감싼다.
65 return (
66 <Box key={headKey(row.n)}>
67 <Text>
68 <Text bold color="suggestion">{`▸ ${row.n} ${row.header}`}</Text>
69 <Text bold>{` ${row.question}`}</Text>
70 </Text>
71 </Box>
72 )
73 case 'option':
74 // 바깥 상자의 key 는 버튼과 그 아래 설명을 함께 보이게 하는 대상이다.
75 return (
76 <Box key={blockKey(row.key)} paddingLeft={2} flexDirection="column">
77 <Box flexDirection="row" gap={2}>
78 <Button key={row.key} plain {...(row.hotkey ? { hotkey: row.hotkey } : {})} {...auto(row.autoFocus)} onPress={() => on.choose(qid(row.key), row.label)}>
79 {`${row.glyph} ${row.label}`}
80 </Button>
81 {row.recommended ? <Text dimColor>{`(${row.recommended})`}</Text> : ''}
82 </Box>
83 {row.description ? (
84 <Box paddingLeft={descriptionIndent(row.hotkey)}>
85 <Text dimColor>{row.description}</Text>
86 </Box>
87 ) : (
88 ''
89 )}
90 </Box>
91 )
92 case 'preview':
93 return (
94 <Box paddingLeft={4} flexDirection="column">
95 {row.text.split('\n').map(line => (
96 <Text>
97 <Text dimColor>{'│ '}</Text>
98 {line}
99 </Text>
100 ))}
101 </Box>
102 )
103 case 'other':
104 return (
105 <Box paddingLeft={2} flexDirection="row" gap={2}>
106 <Button key={row.key} plain hotkey={row.hotkey} onPress={() => on.openEditor(qid(row.key), 'other')}>
107 {`${row.glyph} ${row.text}`}
108 </Button>
109 {row.description ? <Text dimColor>{row.description}</Text> : ''}
110 </Box>
111 )
112 case 'answer':
113 return (
114 <Box paddingLeft={2}>
115 <Button key={row.key} plain {...auto(row.autoFocus)} onPress={() => on.openEditor(qid(row.key), 'other')}>
116 {row.text === UNANSWERED ? <Text dimColor>{row.text}</Text> : row.text}
117 </Button>
118 </Box>
119 )
120 case 'editor':
121 return (
122 <Box paddingLeft={EDITOR_INDENT}>
123 <Input
124 key={row.key}
125 {...auto(row.autoFocus)}
126 placeholder={row.placeholder}
127 value={row.value}
128 submitLabel={EDITOR_SUBMIT_LABEL}
129 onInput={(v: string) => on.typeText(row.qid, row.field, v)}
130 onSubmit={(v: string) => on.commitText(row.qid, row.field, v)}
131 />
132 </Box>
133 )
134 case 'note':
135 return (
136 <Box paddingLeft={2}>
137 <Button key={row.key} plain hotkey={row.hotkey} dimColor onPress={() => on.openEditor(qid(row.key), 'note')}>
138 {row.text}
139 </Button>
140 </Box>
141 )
142 case 'nav':
143 return (
144 <Box paddingLeft={2} flexDirection="row" gap={2}>
145 {row.prev ? <Button key={PREV_KEY} plain hotkey="p" dimColor label={PREV_LABEL} onPress={() => on.prev()} /> : ''}
146 {row.next !== 'none' ? (
147 <Button key={NEXT_KEY} plain hotkey="n" dimColor label={row.next === 'submit' ? TO_SUBMIT_LABEL : NEXT_LABEL} onPress={() => on.next()} />
148 ) : (
149 ''
150 )}
151 </Box>
152 )
153 case 'actions':
154 return (
155 <Box flexDirection="row" gap={1}>
156 <Button key={SUBMIT_KEY} variant="primary" label={row.submit} onPress={() => on.submit()} />
157 <Button key={CANCEL_KEY} label={row.cancel} onPress={() => on.cancel()} />
158 </Box>
159 )
160 case 'help':
161 return <Text dimColor>{row.text}</Text>
162 }
163 })
164 return <Box flexDirection="column">{drawn}</Box>
165}
166hooks/question-form/spec.ts 145 lines1// 질문지 도구의 계약: 입력 모양, 예약 라벨, 상한, 그리고 모델과 사람에게
2// 보이는 문면 상수. `$` 를 쓰지 않는다.
3
4import type { FormInput, FormOption, FormQuestion } from '../../types'
5
6// 입력 모양은 $.state 계약 파일이 갖는다. 기록이 입력을 그대로 담기 때문이다.
7export type { FormInput, FormOption, FormQuestion }
8
9export const TOOL_NAME = 'question_form'
10export const TOOL_FULL_NAME = 'mcp__cc-cmds__question_form'
11export const PANE_ID = 'cc-cmds-question-form'
12export const COMMAND_NAME = 'question-form'
13export const TOKEN_PREFIX = 'QUESTION_FORM_'
14
15export const TITLE_MAX = 40
16export const HEADER_MAX = 12
17export const ID_RE = /^[a-z0-9][a-z0-9_-]{0,31}$/
18export const TOTAL_MAX = 90000
19export const FORM_ID_RE = /^f-[0-9a-f]{8}$/
20
21// 문답 기록의 첫 답 줄을 라벨과 대조하는 읽기가 모호해지지 않게 막는 라벨.
22export const RESERVED_LABELS = ['미답', '해당 없음']
23export const RESERVED_PREFIXES = ['메모:', '자유 입력:']
24// 「기타」는 allowOther 가 그리므로 손으로 만든 같은 계열 라벨을 받지 않는다.
25export const HANDMADE_OTHER_LABELS = ['기타', '직접 입력', '직접 지정', 'Other']
26export const RECOMMEND_ARROW = ' ← '
27
28// 제어 문자 거절 사유. 한 줄로 그려지는 칸은 줄바꿈·탭까지 모든 제어 문자를, 여러 줄로
29// 감기는 칸은 엔진이 받는 줄바꿈·탭·CR 말고 나머지를 거절한다.
30export const LINE_CONTROL_REASON = '제어 문자를 넣지 않습니다(줄바꿈·탭 포함). 한 줄로 그려지는 칸입니다'
31export const PROSE_CONTROL_REASON = '줄바꿈·탭 말고는 제어 문자를 넣지 않습니다'
32const LINE_RULE = '제어 문자를 넣지 않는다(줄바꿈·탭 포함)'
33const PROSE_RULE = '줄바꿈·탭 말고는 제어 문자를 넣지 않는다'
34
35export const UNAVAILABLE_REASONS = ['subagent', 'pipeline', 'surface', 'remote', 'error'] as const
36export type UnavailableReason = (typeof UNAVAILABLE_REASONS)[number]
37
38export const TOOL_DESCRIPTION = [
39 '사람에게 여러 질문을 한 장의 질문지 패널로 묻는다. 지금 물을 수 있는 질문이 둘 이상이거나, 한 질문이 선택지 다섯 개 이상·메모·자유 입력만의 답·복수 선택의 미리보기를 필요로 할 때 쓴다.',
40 `결과의 첫 토큰을 읽는다(<tool_use_error> 감싸개를 벗기고 처음 나오는 ${TOKEN_PREFIX}[A-Z]+).`,
41 `${TOKEN_PREFIX}OPEN 이면 이 턴을 곧바로 끝낸다. 답은 사람이 제출하면 새 턴에 [cc-cmds 질문지 답] 으로 시작하는 메시지로 온다.`,
42 `${TOKEN_PREFIX}INVALID 이면 입력을 고쳐 다시 부르고, ${TOKEN_PREFIX}BUSY 이면 replaces 로 갈아 끼우거나 답을 기다리고, ${TOKEN_PREFIX}UNAVAILABLE 이면 AskUserQuestion 으로 묻는다.`,
43].join(' ')
44
45const OPTION_SCHEMA = {
46 type: 'object',
47 properties: {
48 label: { type: 'string', description: `선택지 라벨. 「 ← 」를 넣지 않는다. 추천은 recommended 로만 표시한다. ${LINE_RULE}` },
49 description: { type: 'string', description: `선택지 설명. ${PROSE_RULE}` },
50 preview: { type: 'string', description: `포커스가 이 선택지에 있을 때 질문 아래에 보일 미리보기. ${PROSE_RULE}` },
51 recommended: { type: 'string', enum: ['추천', '에이전트 추천'], description: 'single 질문에는 하나까지' },
52 },
53 required: ['label', 'description'],
54 additionalProperties: false,
55} as const
56
57export const INPUT_SCHEMA = {
58 type: 'object',
59 properties: {
60 title: { type: 'string', description: `패널 제목. ${TITLE_MAX}자 이하. ${LINE_RULE}` },
61 intro: { type: 'string', description: `첫 질문 위 안내 문단. ${PROSE_RULE}` },
62 replaces: { type: 'string', description: '지금 열린 질문지 id. 그 질문지를 이 질문지로 갈아 끼운다' },
63 questions: {
64 type: 'array',
65 minItems: 1,
66 items: {
67 type: 'object',
68 properties: {
69 id: { type: 'string', description: '^[a-z0-9][a-z0-9_-]{0,31}$, 질문지 안에서 유일' },
70 header: { type: 'string', description: `짧은 머리말. NFC 기준 ${HEADER_MAX} 코드포인트 이하. ${LINE_RULE}` },
71 group: { type: 'string', description: `같은 값끼리 묶어 보인다. ${LINE_RULE}` },
72 question: { type: 'string', description: `질문 문장. ${PROSE_RULE}` },
73 detail: { type: 'string', description: `질문 아래 설명. ${PROSE_RULE}` },
74 kind: { type: 'string', enum: ['single', 'multi', 'text'] },
75 options: {
76 type: 'array',
77 items: OPTION_SCHEMA,
78 description: 'single·multi 는 2개 이상, text 는 두지 않는다. 「기타」·「직접 입력」 같은 라벨은 만들지 않는다(allowOther 가 그린다)',
79 },
80 allowOther: { type: 'boolean', description: '기본 true. 「기타」 자유 입력을 보인다' },
81 allowNote: { type: 'boolean', description: '기본 true. 「메모 (선택)」 입력칸을 보인다' },
82 placeholder: { type: 'string', description: `text 입력칸 안내. ${LINE_RULE}` },
83 when: {
84 type: 'object',
85 properties: {
86 id: { type: 'string', description: '앞쪽 single·multi 질문의 id' },
87 selected: { type: 'array', items: { type: 'string' }, description: '그 질문의 라벨 가운데 하나라도 고르면 이 질문을 보인다' },
88 },
89 required: ['id', 'selected'],
90 additionalProperties: false,
91 },
92 },
93 required: ['id', 'header', 'question', 'kind'],
94 additionalProperties: false,
95 },
96 },
97 },
98 required: ['title', 'questions'],
99 additionalProperties: false,
100} as const
101
102// 결과 토큰과 본문.
103export function openResult(id: string, count: number, isPlaced: boolean): string {
104 const lines = [
105 `${TOKEN_PREFIX}OPEN ${id} 질문 ${count}건`,
106 '질문지를 열었습니다. 이 턴을 지금 끝내세요: 다른 도구를 부르지 말고, 답을 짐작하거나 질문을 본문에 다시 적지 말고, 차례 넘김 표지를 쓰지 마세요(질문지 배너가 이미 나갔습니다).',
107 `답은 사람이 제출하면 새 턴에 \`[cc-cmds 질문지 답] form=${id}\` 로 시작하는 메시지로 옵니다.`,
108 ]
109 if (!isPlaced) {
110 lines.push('패널이 좁은 화면이라 아직 그려지지 않았습니다. 상태 줄의 /question-form 안내가 사람에게 보이므로 따로 알리지 말고 턴을 끝내세요.')
111 }
112 return lines.join('\n')
113}
114
115export const invalidResult = (field: string, reason: string) => `${TOKEN_PREFIX}INVALID ${field}: ${reason}`
116export const busyResult = (id: string) => `${TOKEN_PREFIX}BUSY ${id}`
117export const unavailableResult = (reason: UnavailableReason) => `${TOKEN_PREFIX}UNAVAILABLE ${reason}`
118
119// 사람에게 보이는 문면.
120export const formTitle = (title: string, answered: number, total: number) => `질문지 — ${title} (답 ${answered}/${total})`
121export const sentToast = (answered: number, total: number) => `질문지 답을 보냈습니다 · 답 ${answered}/${total}`
122export const statusLine = (answered: number, total: number) => `질문지 대기 중 · 답 ${answered}/${total} · /question-form 으로 열기`
123export const contextLine = (id: string) => `질문지 ${id} 가 열려 있습니다(답 대기). 이 입력은 질문지의 답이 아닙니다. 답은 질문지 제출로만 옵니다.`
124export const STAMP = '직접 입력된 문면입니다. 질문지 제출이 아닙니다.'
125export const NO_FORM_TOAST = '열린 질문지가 없습니다.'
126export const HELP_LINE = '1-9 고르기 · 0 기타 · m 메모 · n 다음 · p 이전 · ↑↓ Tab 이동 · Enter 확정 · Esc 프롬프트로'
127export const TEXT_PLACEHOLDER = '답을 입력하세요'
128export const OTHER_LABEL = '기타'
129export const OTHER_PLACEHOLDER = '직접 입력'
130export const NOTE_LABEL = '메모'
131export const NOTE_ADD_LABEL = '메모 추가'
132export const NOTE_PLACEHOLDER = '메모 (선택)'
133export const EDITOR_SUBMIT_LABEL = '확정'
134export const ANSWER_LABEL = '답'
135export const UNANSWERED = '미답'
136export const NOT_APPLICABLE = '해당 없음'
137export const SUBMIT_LABEL = '제출'
138export const CANCEL_LABEL = '답 없이 닫기'
139export const PREV_LABEL = '이전 질문'
140export const NEXT_LABEL = '다음 질문'
141export const TO_SUBMIT_LABEL = '제출로'
142export const counterLine = (answered: number, total: number) => `답 ${answered}/${total} · 미답 ${total - answered}건`
143// 접힌 질문 줄의 답 요약은 이 글자 수에서 자른다.
144export const SUMMARY_MAX = 48
145hooks/question-form/transitions.ts 193 lines1// 질문지 수명 주기의 순수 전이. 훅과 처리기는 이 함수들이 돌려준 기록을
2// `$.state` 에 쓰고, 패널·상태 줄·제출·포커스 같은 바깥 일은 스스로 한다.
3// 시험 키트는 사람의 닫기를 일으킬 수 없으므로 전이는 여기서 따로 시험한다.
4
5import { draftOf, inputValue, isVisible } from './bundle'
6import { holdsRefusedControl } from './validate'
7import type { Draft, Drafts, FormEditor, FormInput, FormPhase, FormRecord } from '../../types'
8
9export type { FormEditor, FormPhase, FormRecord }
10
11export const STORE_TTL_MS = 7 * 24 * 60 * 60 * 1000
12
13export const isOpen = (r: FormRecord | undefined): r is FormRecord => r !== undefined && r.phase === 'open'
14
15// 도구 호출이 무엇이 되는가: 열린 질문지가 있으면 replaces 일 때만 갈아 끼우고
16// 아니면 BUSY. 아무것도 없으면 새로 연다(배너는 새로 연 경우만).
17export type CallDecision = { kind: 'busy'; id: string } | { kind: 'replace' } | { kind: 'new' }
18
19export function decideCall(current: FormRecord | undefined, replaces: string | undefined): CallDecision {
20 if (isOpen(current)) return replaces === current.id ? { kind: 'replace' } : { kind: 'busy', id: current.id }
21 return { kind: 'new' }
22}
23
24// 갈아 끼울 때 id·종류·라벨 집합이 같은 질문만 쓰던 답을 이어 받는다.
25export function carryDrafts(prev: FormInput, prevDrafts: Drafts, next: FormInput): Drafts {
26 const out: Drafts = {}
27 for (const q of next.questions) {
28 const old = prev.questions.find(x => x.id === q.id)
29 const d = prevDrafts[q.id]
30 if (!old || !d || old.kind !== q.kind) continue
31 const a = (old.options ?? []).map(o => o.label).sort()
32 const b = (q.options ?? []).map(o => o.label).sort()
33 if (a.length === b.length && a.every((l, i) => l === b[i])) out[q.id] = d
34 }
35 return out
36}
37
38export function openRecord(args: {
39 id: string
40 toolUseId: string
41 form: FormInput
42 drafts: Drafts
43 sessionId: string
44 now: number
45}): FormRecord {
46 const { now, ...rest } = args
47 const rec: FormRecord = { ...rest, phase: 'open', hidden: false, cursor: '', editor: null, editorGen: 0, savedAt: now }
48 return land(settleCursor(rec))
49}
50
51// 열기 결과 반영: 배치되지 않았으면 숨김(상태 줄).
52export const placed = (r: FormRecord, isPlaced: boolean): FormRecord => ({ ...r, hidden: !isPlaced })
53
54// 보이는 질문의 id 를 차례로.
55export const visibleIds = (r: FormRecord): string[] =>
56 r.form.questions.filter(q => isVisible(r.form, r.drafts, q)).map(q => q.id)
57
58export const questionOf = (r: FormRecord, id: string) => r.form.questions.find(q => q.id === id)
59
60// 커서가 보이지 않는 질문을 가리키면(when 의 앞 답이 바뀌었거나 보관에서 되살렸거나)
61// 가장 가까운 앞쪽의 보이는 질문으로, 그것도 없으면 첫 질문으로 옮기고 입력칸을 닫는다.
62export function settleCursor(r: FormRecord): FormRecord {
63 const ids = visibleIds(r)
64 if (ids.includes(r.cursor)) return r
65 const order = r.form.questions.map(q => q.id)
66 const at = order.indexOf(r.cursor)
67 const before = at < 0 ? [] : ids.filter(id => order.indexOf(id) < at)
68 return { ...r, cursor: before[before.length - 1] ?? ids[0] ?? r.cursor, editor: null }
69}
70
71// 커서가 답 없는 text 질문에 놓이면 그 답 칸을 연다. 답이 있으면 답 줄을 보인다.
72export function land(r: FormRecord): FormRecord {
73 const q = questionOf(r, r.cursor)
74 if (!q || q.kind !== 'text') return r
75 if (r.editor && r.editor.id === r.cursor) return r
76 return inputValue(draftOf(r.drafts, r.cursor).other).trim() === '' ? openEditor(r, r.cursor, 'other') : r
77}
78
79// 커서 이동. 보이지 않는 질문으로는 가지 않는다.
80export function jump(r: FormRecord, id: string): FormRecord {
81 if (!visibleIds(r).includes(id) || id === r.cursor) return r
82 return land({ ...r, cursor: id, editor: null })
83}
84
85// 다음·이전 보이는 질문. 끝이면 undefined.
86export function advance(r: FormRecord): FormRecord | undefined {
87 const ids = visibleIds(r)
88 const next = ids[ids.indexOf(r.cursor) + 1]
89 return next === undefined ? undefined : jump(r, next)
90}
91
92export function retreat(r: FormRecord): FormRecord | undefined {
93 const ids = visibleIds(r)
94 const at = ids.indexOf(r.cursor)
95 const prev = at > 0 ? ids[at - 1] : undefined
96 return prev === undefined ? undefined : jump(r, prev)
97}
98
99// 입력칸 열기. 세대가 하나 오르고 그때의 답이 씨앗이 된다 — 입력칸의 key 는 세대마다
100// 다르고 그려지는 value 는 씨앗으로 고정이라, 표면이 Enter 뒤에 비운 글이 다시
101// 열린 칸에 묻어 오지 않고, 치는 동안의 다시 그리기가 치던 글을 되돌리지 않는다.
102// 세대는 기록 안에서 되돌지 않는다: 표면은 한 번 쓴 key 의 빈 글을 패널이 사는 동안 기억한다.
103export function openEditor(r: FormRecord, id: string, field: FormEditor['field']): FormRecord {
104 const gen = r.editorGen + 1
105 const d = draftOf(r.drafts, id)
106 return { ...r, cursor: id, editor: { id, field, gen, seed: field === 'note' ? d.note : d.other }, editorGen: gen }
107}
108
109export const closeEditor = (r: FormRecord): FormRecord => (r.editor ? { ...r, editor: null } : r)
110
111function withDraft(r: FormRecord, id: string, patch: Partial<Draft>): FormRecord {
112 return { ...r, drafts: { ...r.drafts, [id]: { ...draftOf(r.drafts, id), ...patch } } }
113}
114
115// 전이 뒤 포커스가 갈 곳: 그대로, 다음 질문의 첫 조작부, 질문이 끝나 [제출].
116export type Move = 'stay' | 'next' | 'end'
117
118function forward(r: FormRecord): { record: FormRecord; move: Move } {
119 const next = advance(r)
120 return next ? { record: next, move: 'next' } : { record: r, move: 'end' }
121}
122
123// 선택지 누름. single 은 고르면 기타를 비우고 다음 질문으로, 다시 누르면 풀린다.
124// multi 는 켜고 끈다. 그 질문의 기타 입력칸이 열려 있었으면 닫는다.
125export function choose(r: FormRecord, id: string, label: string): { record: FormRecord; move: Move } {
126 const q = questionOf(r, id)
127 const d = draftOf(r.drafts, id)
128 if (!q || q.kind === 'text') return { record: r, move: 'stay' }
129 const base = r.editor?.id === id && r.editor.field === 'other' ? closeEditor(r) : r
130 if (q.kind === 'multi') {
131 const selected = d.selected.includes(label) ? d.selected.filter(s => s !== label) : [...d.selected, label]
132 return { record: settleCursor(withDraft(base, id, { selected })), move: 'stay' }
133 }
134 if (d.selected.includes(label)) return { record: settleCursor(withDraft(base, id, { selected: [] })), move: 'stay' }
135 return forward(settleCursor(withDraft(base, id, { selected: [label], other: '' })))
136}
137
138// 입력칸의 글이 바뀔 때마다: 답을 바로 적는다(확정 없이 나가도 답으로 남는다).
139// single 의 기타에 글이 있으면 고른 선택지는 풀린다. 초안에는 거른 글만 적는다.
140export function typeText(r: FormRecord, id: string, field: FormEditor['field'], value: string): FormRecord {
141 value = inputValue(value)
142 if (field === 'note') return withDraft(r, id, { note: value })
143 const q = questionOf(r, id)
144 const patch: Partial<Draft> = q?.kind === 'single' && value.trim() !== '' ? { other: value, selected: [] } : { other: value }
145 return settleCursor(withDraft(r, id, patch))
146}
147
148// Enter 로 확정: 답을 적고 입력칸을 닫는다. single 의 기타와 text 의 답은 글이 있으면
149// 다음 질문으로 간다. 메모와 multi 의 기타는 그 자리에 남는다.
150export function commitText(r: FormRecord, id: string, field: FormEditor['field'], value: string): { record: FormRecord; move: Move } {
151 const typed = typeText(r, id, field, value)
152 const record = typed.editor?.id === id && typed.editor.field === field ? closeEditor(typed) : typed
153 const q = questionOf(record, id)
154 const moves = field === 'other' && inputValue(value).trim() !== '' && (q?.kind === 'single' || q?.kind === 'text')
155 return moves ? forward(record) : { record, move: 'stay' }
156}
157
158// [제출]: CAS open→submitted. 이미 소비된 기록이면 undefined(두 번째 누름 무시).
159export function submit(r: FormRecord | undefined): FormRecord | undefined {
160 if (!isOpen(r)) return undefined
161 return { ...r, phase: 'submitted', hidden: false }
162}
163
164// [답 없이 닫기]: CAS open→cancelled.
165export function cancel(r: FormRecord | undefined): FormRecord | undefined {
166 if (!isOpen(r)) return undefined
167 return { ...r, phase: 'cancelled' }
168}
169
170// 사람의 닫기 표시: 열린 질문지는 숨길 뿐 기록과 BUSY 를 유지한다.
171export function personClose(r: FormRecord | undefined): { record: FormRecord | undefined; showStatus: boolean } {
172 if (isOpen(r)) return { record: { ...r, hidden: true }, showStatus: true }
173 return { record: undefined, showStatus: false }
174}
175
176// /question-form: 열린 질문지를 다시 보인다. 없으면 undefined(알림).
177export function reopen(r: FormRecord | undefined): FormRecord | undefined {
178 return isOpen(r) ? { ...r, hidden: false } : undefined
179}
180
181export const expired = (stored: FormRecord, now: number) => now - stored.savedAt > STORE_TTL_MS
182
183// 보관에서 되살릴지: 같은 세션의 열린 기록만 되살린다. now 를 주면 7일이 지난 기록은
184// 같은 세션의 것이어도 되살리지 않는다. 앞 판이 보관한 기록에는 커서와 입력칸이
185// 없으므로 첫 질문에서 시작하게 채운다. 모델 입력에 엔진이 거부하는 제어 문자가 든
186// 기록(검사가 생기기 전에 열린 것)은 그려도 패널이 깨지므로 되살리지 않는다.
187export function restorable(stored: FormRecord | undefined, sessionId: string, now?: number): FormRecord | undefined {
188 if (!isOpen(stored) || stored.sessionId !== sessionId) return undefined
189 if (now !== undefined && expired(stored, now)) return undefined
190 if (holdsRefusedControl(stored.form)) return undefined
191 return land(settleCursor({ ...stored, cursor: stored.cursor ?? '', editor: stored.editor ?? null, editorGen: stored.editorGen ?? 0 }))
192}
193hooks/question-form/validate.ts 159 lines1// 질문지 입력 전체 검증. 엔진은 입력 스키마를 강제하지 않으므로 이 파일이
2// 유일한 검사다. 첫 위반 하나를 `QUESTION_FORM_INVALID <칸>: <사유>` 로 돌려준다.
3
4import {
5 HANDMADE_OTHER_LABELS,
6 HEADER_MAX,
7 ID_RE,
8 LINE_CONTROL_REASON,
9 PROSE_CONTROL_REASON,
10 RECOMMEND_ARROW,
11 RESERVED_LABELS,
12 RESERVED_PREFIXES,
13 TITLE_MAX,
14 TOTAL_MAX,
15 invalidResult,
16} from './spec'
17import type { FormInput } from './spec'
18
19const isObj = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v)
20const isStr = (v: unknown): v is string => typeof v === 'string'
21const codePoints = (s: string) => [...s].length
22
23// 입력 안 모든 문자열의 글자 수 합.
24export function totalChars(v: unknown): number {
25 if (isStr(v)) return codePoints(v)
26 if (Array.isArray(v)) return v.reduce((n: number, x) => n + totalChars(x), 0)
27 if (isObj(v)) return Object.values(v).reduce((n: number, x) => n + totalChars(x), 0)
28 return 0
29}
30
31// 한 줄로 그려지는 칸(제목·머리말·묶음·안내글·라벨)은 C0·DEL·C1 전부를, 여러 줄로
32// 감기는 칸(안내 문단·질문 문장·설명·선택지 설명·미리보기)은 엔진이 받는 줄바꿈·탭·CR 을
33// 뺀 나머지를 금한다. 뒤쪽 집합이 곧 엔진이 패널 전체를 거부하는 문자다.
34const LINE_CONTROL_RE = /[\u0000-\u001f\u007f-\u009f]/
35const PROSE_CONTROL_RE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/
36
37// 엔진이 거부하는 문자를 품은 칸이 하나라도 있는가. 보관에서 되살릴 기록은 이 검사가
38// 생기기 전에 열렸을 수 있으므로 되살리기 전에 다시 건다. 한 줄 칸의 줄바꿈·탭은 엔진이
39// 그리므로 여기서는 걸지 않는다.
40export function holdsRefusedControl(form: FormInput): boolean {
41 const refused = (s: string | undefined) => s !== undefined && PROSE_CONTROL_RE.test(s)
42 if (refused(form.title) || refused(form.intro)) return true
43 return form.questions.some(
44 q =>
45 refused(q.header) ||
46 refused(q.question) ||
47 refused(q.group) ||
48 refused(q.detail) ||
49 refused(q.placeholder) ||
50 (q.options ?? []).some(o => refused(o.label) || refused(o.description) || refused(o.preview)),
51 )
52}
53
54function labelProblem(label: string): string | undefined {
55 if (label.includes(RECOMMEND_ARROW.trim())) return '라벨에 「←」를 넣지 않습니다. 추천은 recommended 로 표시합니다'
56 if (HANDMADE_OTHER_LABELS.includes(label.trim())) return `「${label.trim()}」 라벨은 만들지 않습니다. allowOther 가 기타 입력을 그립니다`
57 if (RESERVED_LABELS.includes(label.trim())) return `「${label.trim()}」는 예약 라벨입니다`
58 for (const p of RESERVED_PREFIXES) if (label.trim().startsWith(p)) return `「${p}」로 시작하는 라벨은 예약돼 있습니다`
59 return undefined
60}
61
62// openId: 지금 열린(제출·취소되지 않은) 질문지 id. 없으면 undefined.
63export function validateForm(input: unknown, openId: string | undefined): string | undefined {
64 const bad = invalidResult
65 if (!isObj(input)) return bad('input', '객체가 아닙니다')
66 if (!isStr(input.title) || input.title.trim() === '') return bad('title', '필수입니다')
67 if (codePoints(input.title) > TITLE_MAX) return bad('title', `${TITLE_MAX}자를 넘습니다`)
68 if (LINE_CONTROL_RE.test(input.title)) return bad('title', LINE_CONTROL_REASON)
69 if (input.intro !== undefined && !isStr(input.intro)) return bad('intro', '문자열이 아닙니다')
70 if (input.intro !== undefined && PROSE_CONTROL_RE.test(input.intro)) return bad('intro', PROSE_CONTROL_REASON)
71 if (input.replaces !== undefined) {
72 if (!isStr(input.replaces)) return bad('replaces', '문자열이 아닙니다')
73 if (input.replaces !== openId) return bad('replaces', openId ? `열린 질문지 id 는 ${openId} 입니다` : '열린 질문지가 없습니다')
74 }
75 if (!Array.isArray(input.questions) || input.questions.length === 0) return bad('questions', '1개 이상이어야 합니다')
76
77 const seen = new Map<string, { index: number; kind: string; labels: string[] }>()
78 for (let i = 0; i < input.questions.length; i++) {
79 const q = input.questions[i]
80 const at = `questions[${i}]`
81 if (!isObj(q)) return bad(at, '객체가 아닙니다')
82 if (!isStr(q.id) || !ID_RE.test(q.id)) return bad(`${at}.id`, '^[a-z0-9][a-z0-9_-]{0,31}$ 에 맞지 않습니다')
83 if (seen.has(q.id)) return bad(`${at}.id`, `「${q.id}」가 겹칩니다`)
84 if (!isStr(q.header) || q.header.trim() === '') return bad(`${at}.header`, '필수입니다')
85 if (codePoints(q.header.normalize('NFC')) > HEADER_MAX) return bad(`${at}.header`, `NFC 기준 ${HEADER_MAX} 코드포인트를 넘습니다`)
86 if (LINE_CONTROL_RE.test(q.header)) return bad(`${at}.header`, LINE_CONTROL_REASON)
87 if (!isStr(q.question) || q.question.trim() === '') return bad(`${at}.question`, '필수입니다')
88 if (PROSE_CONTROL_RE.test(q.question)) return bad(`${at}.question`, PROSE_CONTROL_REASON)
89 for (const k of ['group', 'detail', 'placeholder'] as const) {
90 const v = q[k]
91 if (v === undefined) continue
92 if (!isStr(v)) return bad(`${at}.${k}`, '문자열이 아닙니다')
93 const prose = k === 'detail'
94 if ((prose ? PROSE_CONTROL_RE : LINE_CONTROL_RE).test(v)) return bad(`${at}.${k}`, prose ? PROSE_CONTROL_REASON : LINE_CONTROL_REASON)
95 }
96 for (const k of ['allowOther', 'allowNote'] as const) {
97 if (q[k] !== undefined && typeof q[k] !== 'boolean') return bad(`${at}.${k}`, '참·거짓 값이 아닙니다')
98 }
99 if (q.kind !== 'single' && q.kind !== 'multi' && q.kind !== 'text') return bad(`${at}.kind`, 'single · multi · text 가운데 하나여야 합니다')
100
101 const labels: string[] = []
102 if (q.kind === 'text') {
103 if (q.options !== undefined && (!Array.isArray(q.options) || q.options.length > 0)) return bad(`${at}.options`, 'text 질문에는 선택지를 두지 않습니다')
104 } else {
105 if (!Array.isArray(q.options) || q.options.length < 2) return bad(`${at}.options`, `${q.kind} 질문은 선택지가 2개 이상이어야 합니다`)
106 let recommended = 0
107 for (let j = 0; j < q.options.length; j++) {
108 const o = q.options[j]
109 const oat = `${at}.options[${j}]`
110 if (!isObj(o)) return bad(oat, '객체가 아닙니다')
111 if (!isStr(o.label) || o.label.trim() === '') return bad(`${oat}.label`, '필수입니다')
112 if (LINE_CONTROL_RE.test(o.label)) return bad(`${oat}.label`, LINE_CONTROL_REASON)
113 if (!isStr(o.description)) return bad(`${oat}.description`, '필수입니다')
114 if (PROSE_CONTROL_RE.test(o.description)) return bad(`${oat}.description`, PROSE_CONTROL_REASON)
115 if (o.preview !== undefined && !isStr(o.preview)) return bad(`${oat}.preview`, '문자열이 아닙니다')
116 if (isStr(o.preview) && PROSE_CONTROL_RE.test(o.preview)) return bad(`${oat}.preview`, PROSE_CONTROL_REASON)
117 const problem = labelProblem(o.label)
118 if (problem) return bad(`${oat}.label`, problem)
119 if (labels.includes(o.label)) return bad(`${oat}.label`, `「${o.label}」가 겹칩니다`)
120 labels.push(o.label)
121 if (o.recommended !== undefined) {
122 if (o.recommended !== '추천' && o.recommended !== '에이전트 추천') return bad(`${oat}.recommended`, '「추천」이나 「에이전트 추천」이어야 합니다')
123 recommended++
124 }
125 }
126 if (q.kind === 'single' && recommended > 1) return bad(`${at}.options`, 'single 질문에 추천이 둘 이상입니다')
127 }
128
129 if (q.when !== undefined) {
130 const w = q.when
131 if (!isObj(w) || !isStr(w.id) || !Array.isArray(w.selected) || w.selected.length === 0 || !w.selected.every(isStr)) {
132 return bad(`${at}.when`, '{id, selected[]} 모양이어야 합니다')
133 }
134 const target = seen.get(w.id)
135 if (!target) return bad(`${at}.when`, `「${w.id}」는 앞쪽 질문이 아닙니다`)
136 if (target.kind === 'text') return bad(`${at}.when`, `「${w.id}」는 single·multi 질문이 아닙니다`)
137 const missing = (w.selected as string[]).find(s => !target.labels.includes(s))
138 if (missing !== undefined) return bad(`${at}.when`, `「${w.id}」에 「${missing}」 라벨이 없습니다`)
139 }
140 seen.set(q.id, { index: i, kind: q.kind, labels })
141 }
142
143 if (totalChars(input) > TOTAL_MAX) return bad('input', `글자 총량이 ${TOTAL_MAX}자를 넘습니다`)
144 return undefined
145}
146
147// 검증을 통과한 입력에 기본값을 채운다.
148export function normalizeForm(input: FormInput): FormInput {
149 return {
150 ...input,
151 questions: input.questions.map(q => ({
152 ...q,
153 options: q.kind === 'text' ? [] : q.options ?? [],
154 allowOther: q.allowOther ?? true,
155 allowNote: q.allowNote ?? true,
156 })),
157 }
158}
159types/index.d.ts 115 lines1// 런 상태 패널 mod(hooks/autopilot-status.tsx)와 질문지 서브 mod(hooks/question-form/)가
2// $.state 에 두는 값의 계약.
3
4/** 한 줄 안의 색조 조각. 색조는 헬퍼가 정하고 mod 는 그리기만 한다. */
5export type PanePart = { tone: string; bold: boolean; text: string }
6
7/**
8 * 헬퍼 본문 한 줄: 묶음 id, 감을지(`wrap`) 끝을 자를지(`cut`), 그리고 조각들.
9 * 줄 하나는 한 번 감기거나 한 번 잘린다.
10 */
11export type PaneLine = { bundle: string; wrap: boolean; parts: PanePart[] }
12
13/** 패널이 그리는 것: 마지막으로 성공한 헬퍼 실행의 본문과, 그 뒤 실패를 알리는 경고 줄. */
14export type PaneLines = { lines: PaneLine[]; warning: string | null }
15
16/** 자동 열기·닫힘 기록과 갱신 주기 판단에 쓰는 값. 모듈이 다시 적재되어도 남아야 한다. */
17export type PaneControl = {
18 /** 묻지 않고 패널을 연 런 id. 런마다 한 번만 연다. */
19 autoOpenedFor: string[]
20 /** 사람이 패널을 닫은 런 id. 이 런으로는 다시 저절로 열지 않는다. */
21 dismissedFor: string[]
22 /** 마지막 헬퍼 실행을 시작하기 전에 읽은 시각(ms). 실행한 적 없으면 null. */
23 lastRunAt: number | null
24 /** 마지막으로 본 세션 목록 mtime. 목록이 없으면 null. */
25 lastMtime: number | null
26 /** 마지막 헬퍼 실행에 넘긴 세션 id. */
27 lastSid: string | null
28 /** 마지막 좋은 머리 행의 런 id. 런이 없으면 null. */
29 rid: string | null
30 /** 마지막 좋은 머리 행의 세션 목록 절대 경로. 없으면 null. */
31 indexPath: string | null
32 /** 마지막 좋은 머리 행의 갱신 주기(ms). */
33 refreshMs: number
34}
35
36/** 질문지 선택지. 추천 표시는 라벨이 아니라 recommended 로만 한다. */
37export type FormOption = {
38 label: string
39 description: string
40 preview?: string
41 recommended?: '추천' | '에이전트 추천'
42}
43
44/** 질문지 질문 하나. when 은 앞쪽 single·multi 질문의 라벨에 건다. */
45export type FormQuestion = {
46 id: string
47 header: string
48 group?: string
49 question: string
50 detail?: string
51 kind: 'single' | 'multi' | 'text'
52 options?: FormOption[]
53 allowOther?: boolean
54 allowNote?: boolean
55 placeholder?: string
56 when?: { id: string; selected: string[] }
57}
58
59/** 질문지 도구의 입력. */
60export type FormInput = {
61 title: string
62 intro?: string
63 replaces?: string
64 questions: FormQuestion[]
65}
66
67/** 쓰던 답. selected 는 라벨 원문(추천 접미 없음), other 는 기타 입력이나 text 질문의 답. */
68export type Draft = { selected: string[]; other: string; note: string }
69export type Drafts = Record<string, Draft>
70
71export type FormPhase = 'open' | 'submitted' | 'cancelled'
72
73/**
74 * 열려 있는 입력칸 하나: 어느 질문의 어느 칸인지, 열 때마다 하나씩 오르는 세대(입력칸 key 에
75 * 들어간다), 연 순간의 글(그려지는 value 로 고정된다).
76 */
77export type FormEditor = { id: string; field: 'other' | 'note'; gen: number; seed: string }
78
79/** 질문지 기록 하나. 제출·취소된 기록은 묶음을 낸 자리에서 버려지므로 열린 것만 남는다. */
80export type FormRecord = {
81 id: string
82 toolUseId: string
83 form: FormInput
84 drafts: Drafts
85 phase: FormPhase
86 /** 사람이 닫기 표시를 눌렀거나 열 때 배치되지 않아 패널 대신 상태 줄을 보이는 중. */
87 hidden: boolean
88 /** 펼쳐 보이는 질문의 id. 나머지 질문은 한 줄로 접힌다. */
89 cursor: string
90 /** 열려 있는 「기타」·「메모」 입력칸. 없으면 null. */
91 editor: FormEditor | null
92 /** 이 기록에서 입력칸을 연 횟수. 입력칸 key 의 세대이며, 칸을 닫아도 되돌지 않는다. */
93 editorGen: number
94 sessionId: string
95 savedAt: number
96}
97
98/** 질문지가 읽는 사람 프롬프트의 출처 낱말(composer·bridge 등). 모르면 null. */
99export type PromptOriginKind = string | null
100
101declare module 'claude-code' {
102 interface PluginState {
103 'cc-cmds': {
104 paneLines: PaneLines
105 paneControl: PaneControl
106 /** 지금 열린 질문지 기록. 없으면 null. */
107 'questionForm.record': FormRecord | null
108 /** 마지막 사람 프롬프트의 출처. bridge 이면 질문지를 쓸 수 없다. */
109 'questionForm.lastPersonOrigin': PromptOriginKind
110 /** 질문지 패널에서 포커스를 받은 요소의 key. 엔진 자신의 정지점이면 null. */
111 'questionForm.focusedElement': string | null
112 }
113 }
114}
115