@haystackeditor/cli 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -12
- package/dist/assets/hooks/agent-context/detect.ts +136 -0
- package/dist/assets/hooks/agent-context/format.ts +99 -0
- package/dist/assets/hooks/agent-context/index.ts +39 -0
- package/dist/assets/hooks/agent-context/parsers/claude.ts +253 -0
- package/dist/assets/hooks/agent-context/parsers/gemini.ts +155 -0
- package/dist/assets/hooks/agent-context/parsers/opencode.ts +174 -0
- package/dist/assets/hooks/agent-context/tsconfig.json +13 -0
- package/dist/assets/hooks/agent-context/types.ts +58 -0
- package/dist/assets/hooks/llm-rules-template.md +56 -0
- package/dist/assets/hooks/package.json +11 -0
- package/dist/assets/hooks/scripts/commit-msg.sh +4 -0
- package/dist/assets/hooks/scripts/post-commit.sh +4 -0
- package/dist/assets/hooks/scripts/pre-commit.sh +92 -0
- package/dist/assets/hooks/scripts/pre-push.sh +25 -0
- package/dist/assets/hooks/scripts/prepare-commit-msg.sh +3 -0
- package/dist/assets/hooks/truncation-checker/ast-analyzer.ts +528 -0
- package/dist/assets/hooks/truncation-checker/index.ts +595 -0
- package/dist/assets/hooks/truncation-checker/tsconfig.json +13 -0
- package/dist/assets/skills/prepare-haystack.md +323 -0
- package/dist/assets/skills/secrets.md +164 -0
- package/dist/assets/skills/setup-external-sandbox.md +243 -0
- package/dist/assets/skills/setup-haystack.md +639 -0
- package/dist/assets/skills/submit.md +154 -0
- package/dist/assets/templates/CLAUDE.md.snippet +42 -0
- package/dist/assets/templates/haystack.yml +193 -0
- package/dist/commands/check-pending.d.ts +19 -0
- package/dist/commands/check-pending.js +217 -0
- package/dist/commands/config.d.ts +18 -12
- package/dist/commands/config.js +327 -52
- package/dist/commands/hooks.d.ts +17 -0
- package/dist/commands/hooks.js +269 -0
- package/dist/commands/install-session-hooks.d.ts +16 -0
- package/dist/commands/install-session-hooks.js +302 -0
- package/dist/commands/login.js +1 -1
- package/dist/commands/policy.d.ts +31 -0
- package/dist/commands/policy.js +365 -0
- package/dist/commands/skills.d.ts +8 -0
- package/dist/commands/skills.js +80 -0
- package/dist/commands/submit.d.ts +22 -0
- package/dist/commands/submit.js +428 -0
- package/dist/commands/triage.d.ts +16 -0
- package/dist/commands/triage.js +354 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +317 -2
- package/dist/tools/detect.d.ts +50 -0
- package/dist/tools/detect.js +853 -0
- package/dist/tools/fixtures.d.ts +38 -0
- package/dist/tools/fixtures.js +199 -0
- package/dist/tools/setup.d.ts +43 -0
- package/dist/tools/setup.js +597 -0
- package/dist/triage/prompts.d.ts +31 -0
- package/dist/triage/prompts.js +266 -0
- package/dist/triage/runner.d.ts +21 -0
- package/dist/triage/runner.js +325 -0
- package/dist/triage/traces.d.ts +20 -0
- package/dist/triage/traces.js +305 -0
- package/dist/triage/types.d.ts +47 -0
- package/dist/triage/types.js +7 -0
- package/dist/types.d.ts +1387 -191
- package/dist/types.js +254 -2
- package/dist/utils/analysis-api.d.ts +108 -0
- package/dist/utils/analysis-api.js +194 -0
- package/dist/utils/config.js +1 -1
- package/dist/utils/git.d.ts +80 -0
- package/dist/utils/git.js +302 -0
- package/dist/utils/github-api.d.ts +83 -0
- package/dist/utils/github-api.js +266 -0
- package/dist/utils/hooks.d.ts +26 -0
- package/dist/utils/hooks.js +226 -0
- package/dist/utils/pending-state.d.ts +38 -0
- package/dist/utils/pending-state.js +86 -0
- package/dist/utils/secrets.js +3 -3
- package/dist/utils/skill.d.ts +1 -1
- package/dist/utils/skill.js +658 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -4,19 +4,61 @@ Set up Haystack verification for your project. When PRs are opened, an AI agent
|
|
|
4
4
|
|
|
5
5
|
## Quick Start
|
|
6
6
|
|
|
7
|
+
### Fastest: AI-Assisted Setup
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx @haystackeditor/cli skills install
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This auto-detects your coding CLI (Claude Code, Codex, Cursor) and installs the Haystack MCP server.
|
|
14
|
+
|
|
15
|
+
Then invoke the setup in your coding CLI:
|
|
16
|
+
|
|
17
|
+
| CLI | How to invoke |
|
|
18
|
+
|-----|---------------|
|
|
19
|
+
| **Claude Code** | `/setup-haystack` |
|
|
20
|
+
| **Codex CLI** | `/setup-haystack` or ask "set up haystack verification" |
|
|
21
|
+
| **Cursor** | `/setup-haystack` (in Composer) |
|
|
22
|
+
|
|
23
|
+
The AI will analyze your codebase, create `.haystack.json`, and configure verification flows.
|
|
24
|
+
|
|
25
|
+
### Alternative: Direct CLI Setup
|
|
26
|
+
|
|
27
|
+
For quick setup without AI assistance:
|
|
28
|
+
|
|
7
29
|
```bash
|
|
8
30
|
npx @haystackeditor/cli init
|
|
9
31
|
```
|
|
10
32
|
|
|
11
|
-
This auto-detects your
|
|
12
|
-
- `.haystack.json` - Configuration for the verification agent
|
|
13
|
-
- `.agents/skills/haystack.md` - Skill file for AI agent discovery
|
|
33
|
+
This runs an interactive wizard that auto-detects your project and creates `.haystack.json`.
|
|
14
34
|
|
|
15
|
-
|
|
35
|
+
### Additional Skills
|
|
16
36
|
|
|
17
|
-
|
|
37
|
+
| Skill | Purpose |
|
|
38
|
+
|-------|---------|
|
|
39
|
+
| `/setup-haystack` | **Start here** - diagnoses project, creates config |
|
|
40
|
+
| `/prepare-haystack` | Add aria-labels and data-testid for browser automation |
|
|
41
|
+
| `/setup-haystack-secrets` | Configure API keys, LLM credentials, secrets |
|
|
18
42
|
|
|
19
|
-
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## CLI Commands
|
|
46
|
+
|
|
47
|
+
All commands can be run directly:
|
|
48
|
+
|
|
49
|
+
### `haystack skills`
|
|
50
|
+
|
|
51
|
+
Install AI skills into your coding CLI (auto-detects Claude Code, Codex, Cursor):
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npx @haystackeditor/cli skills install # Auto-detect CLI
|
|
55
|
+
npx @haystackeditor/cli skills install --cli codex # Install for Codex
|
|
56
|
+
npx @haystackeditor/cli skills install --cli cursor # Install for Cursor
|
|
57
|
+
npx @haystackeditor/cli skills install --cli manual # Show manual setup
|
|
58
|
+
npx @haystackeditor/cli skills list # List available skills
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### `haystack init`
|
|
20
62
|
|
|
21
63
|
```bash
|
|
22
64
|
npx @haystackeditor/cli init # Interactive wizard
|
|
@@ -73,6 +115,54 @@ npx @haystackeditor/cli secrets set API_KEY xxx --scope org --scope-id myorg
|
|
|
73
115
|
npx @haystackeditor/cli secrets set API_KEY xxx --scope repo --scope-id owner/repo
|
|
74
116
|
```
|
|
75
117
|
|
|
118
|
+
### `haystack submit`
|
|
119
|
+
|
|
120
|
+
Create a PR from current changes. Runs pre-PR triage (code review, rules validation, intent drift), pushes your branch, and opens the PR.
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
haystack submit # Triage → create PR → wait for analysis
|
|
124
|
+
haystack submit --title "Fix auth" # Custom PR title
|
|
125
|
+
haystack submit --draft # Create as draft PR
|
|
126
|
+
haystack submit --force # Skip triage checks
|
|
127
|
+
haystack submit --no-wait # Don't wait for analysis results
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**Review routing**: By default, PRs go to the auto-merge queue — if analysis passes, the PR is merged automatically. Use `--review` to route it for human review instead:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
haystack submit --review # Needs review (goes to assignment queue)
|
|
134
|
+
haystack submit --review octocat # Request review from a specific teammate
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
When `--review` is used without a username, the PR is labeled `haystack:needs-review` and appears in your team's assignment queue. When a username is provided, that person is also requested as a reviewer on GitHub.
|
|
138
|
+
|
|
139
|
+
### `haystack triage`
|
|
140
|
+
|
|
141
|
+
View Haystack analysis results for any PR. Shows the same data as the Haystack web feed: rating, verdict, structured findings with details, verified bugs, human review reasons, and agent fix prompts.
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
haystack triage 42 # Current repo, PR #42
|
|
145
|
+
haystack triage owner/repo#99 # Fully qualified
|
|
146
|
+
haystack triage https://github.com/o/r/pull/1 # From GitHub URL
|
|
147
|
+
haystack triage 42 --json # Machine-readable JSON output
|
|
148
|
+
haystack triage 42 --no-wait # Don't wait if analysis is pending
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The `--json` output includes `agentFixPrompt` fields — ready-to-paste instructions for coding agents to fix each finding.
|
|
152
|
+
|
|
153
|
+
### `haystack check-pending`
|
|
154
|
+
|
|
155
|
+
Check the status of a previously submitted PR's analysis (from the current repo):
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
haystack check-pending # Rich terminal output
|
|
159
|
+
haystack check-pending --hook # Minimal output (for session-start hooks)
|
|
160
|
+
haystack check-pending --json # Machine-readable JSON
|
|
161
|
+
haystack check-pending --clear # Clear pending state
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
76
166
|
## Configuration
|
|
77
167
|
|
|
78
168
|
The `init` command creates `.haystack.json`:
|
|
@@ -104,15 +194,18 @@ verification:
|
|
|
104
194
|
| Key user journeys | Flows describing what to verify |
|
|
105
195
|
| API calls needing auth | Fixtures to mock responses |
|
|
106
196
|
|
|
107
|
-
|
|
197
|
+
Use `/setup-haystack` in Claude Code for AI-assisted configuration of flows and fixtures.
|
|
198
|
+
|
|
199
|
+
---
|
|
108
200
|
|
|
109
201
|
## How It Works
|
|
110
202
|
|
|
111
|
-
1.
|
|
112
|
-
2.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
-
|
|
203
|
+
1. Run `npx @haystackeditor/cli init` or use `/setup-haystack` in Claude Code
|
|
204
|
+
2. Commit the generated `.haystack.json`
|
|
205
|
+
3. Install the [Haystack GitHub App](https://haystackeditor.com/github-app)
|
|
206
|
+
4. When PRs are opened, Haystack's AI agent:
|
|
207
|
+
- Spins up your app in a cloud sandbox
|
|
208
|
+
- Runs verification commands
|
|
116
209
|
- Captures screenshots and evidence
|
|
117
210
|
- Reports results on the PR
|
|
118
211
|
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import * as crypto from 'crypto';
|
|
5
|
+
import type { DetectionResult, AgentType } from './types.js';
|
|
6
|
+
|
|
7
|
+
const STALENESS_MINUTES = parseInt(
|
|
8
|
+
process.env.AGENT_CONTEXT_STALENESS_MINUTES ?? '5',
|
|
9
|
+
10,
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
export function sanitizePath(absPath: string): string {
|
|
13
|
+
return absPath.replace(/[^a-zA-Z0-9]/g, '-');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function sha256(input: string): string {
|
|
17
|
+
return crypto.createHash('sha256').update(input).digest('hex');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isRecent(filePath: string): boolean {
|
|
21
|
+
try {
|
|
22
|
+
const stat = fs.statSync(filePath);
|
|
23
|
+
const ageMs = Date.now() - stat.mtimeMs;
|
|
24
|
+
return ageMs < STALENESS_MINUTES * 60 * 1000;
|
|
25
|
+
} catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function findMostRecentFile(dir: string, pattern: RegExp): string | null {
|
|
31
|
+
if (!fs.existsSync(dir)) return null;
|
|
32
|
+
|
|
33
|
+
let entries: fs.Dirent[];
|
|
34
|
+
try {
|
|
35
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let bestPath: string | null = null;
|
|
41
|
+
let bestMtime = 0;
|
|
42
|
+
|
|
43
|
+
for (const entry of entries) {
|
|
44
|
+
if (!entry.isFile() || !pattern.test(entry.name)) continue;
|
|
45
|
+
const fullPath = path.join(dir, entry.name);
|
|
46
|
+
try {
|
|
47
|
+
const stat = fs.statSync(fullPath);
|
|
48
|
+
if (stat.mtimeMs > bestMtime) {
|
|
49
|
+
bestMtime = stat.mtimeMs;
|
|
50
|
+
bestPath = fullPath;
|
|
51
|
+
}
|
|
52
|
+
} catch {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return bestPath;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function extractSessionId(filePath: string, ext: string): string {
|
|
61
|
+
return path.basename(filePath, ext);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function detectClaude(repoPath: string): DetectionResult {
|
|
65
|
+
const overrideDir = process.env.ENTIRE_TEST_CLAUDE_PROJECT_DIR;
|
|
66
|
+
const sessionDir =
|
|
67
|
+
overrideDir ||
|
|
68
|
+
path.join(os.homedir(), '.claude', 'projects', sanitizePath(repoPath));
|
|
69
|
+
|
|
70
|
+
const sessionFile = findMostRecentFile(sessionDir, /\.jsonl$/);
|
|
71
|
+
if (!sessionFile || !isRecent(sessionFile)) {
|
|
72
|
+
return { detected: false };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
detected: true,
|
|
77
|
+
agent: 'claude-code',
|
|
78
|
+
sessionFilePath: sessionFile,
|
|
79
|
+
sessionId: extractSessionId(sessionFile, '.jsonl'),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function detectGemini(repoPath: string): DetectionResult {
|
|
84
|
+
const overrideDir = process.env.ENTIRE_TEST_GEMINI_PROJECT_DIR;
|
|
85
|
+
const projectHash = sha256(repoPath);
|
|
86
|
+
const sessionDir =
|
|
87
|
+
overrideDir ||
|
|
88
|
+
path.join(os.homedir(), '.gemini', 'tmp', projectHash, 'chats');
|
|
89
|
+
|
|
90
|
+
const sessionFile = findMostRecentFile(sessionDir, /^session-.*\.json$/);
|
|
91
|
+
if (!sessionFile || !isRecent(sessionFile)) {
|
|
92
|
+
return { detected: false };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
detected: true,
|
|
97
|
+
agent: 'gemini-cli',
|
|
98
|
+
sessionFilePath: sessionFile,
|
|
99
|
+
sessionId: extractSessionId(sessionFile, '.json'),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function detectOpenCode(repoPath: string): DetectionResult {
|
|
104
|
+
const overrideDir = process.env.ENTIRE_TEST_OPENCODE_PROJECT_DIR;
|
|
105
|
+
const sessionDir =
|
|
106
|
+
overrideDir ||
|
|
107
|
+
path.join(os.tmpdir(), 'entire-opencode', sanitizePath(repoPath));
|
|
108
|
+
|
|
109
|
+
const sessionFile = findMostRecentFile(sessionDir, /\.json$/);
|
|
110
|
+
if (!sessionFile || !isRecent(sessionFile)) {
|
|
111
|
+
return { detected: false };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
detected: true,
|
|
116
|
+
agent: 'opencode',
|
|
117
|
+
sessionFilePath: sessionFile,
|
|
118
|
+
sessionId: extractSessionId(sessionFile, '.json'),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const detectors: Array<(repoPath: string) => DetectionResult> = [
|
|
123
|
+
detectClaude,
|
|
124
|
+
detectGemini,
|
|
125
|
+
detectOpenCode,
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
export async function detectAgent(
|
|
129
|
+
repoPath: string,
|
|
130
|
+
): Promise<DetectionResult> {
|
|
131
|
+
for (const detect of detectors) {
|
|
132
|
+
const result = detect(repoPath);
|
|
133
|
+
if (result.detected) return result;
|
|
134
|
+
}
|
|
135
|
+
return { detected: false };
|
|
136
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { AgentContext, InteractionTurn } from './types.js';
|
|
2
|
+
|
|
3
|
+
const AGENT_DISPLAY_NAMES: Record<string, string> = {
|
|
4
|
+
'claude-code': 'Claude Code',
|
|
5
|
+
'gemini-cli': 'Gemini CLI',
|
|
6
|
+
opencode: 'OpenCode',
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// intentional: truncation for display formatting in hook output, not LLM context
|
|
10
|
+
function truncate(text: string, maxLen: number): string {
|
|
11
|
+
if (text.length <= maxLen) return text;
|
|
12
|
+
return text.slice(0, maxLen) + '...';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function formatNumber(n: number): string {
|
|
16
|
+
return n.toLocaleString('en-US');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function formatTurnSummary(turn: InteractionTurn): string {
|
|
20
|
+
const role = turn.role.toUpperCase();
|
|
21
|
+
const content = truncate(turn.content.replace(/\n/g, ' '), 200);
|
|
22
|
+
const toolNames = turn.toolUses.map((t) => t.toolName);
|
|
23
|
+
const toolSuffix = toolNames.length > 0 ? ` [tools: ${toolNames.join(', ')}]` : '';
|
|
24
|
+
return ` [${turn.turnIndex + 1}] ${role}: ${content}${toolSuffix}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function formatHuman(ctx: AgentContext): string {
|
|
28
|
+
const lines: string[] = [];
|
|
29
|
+
|
|
30
|
+
lines.push('========================================');
|
|
31
|
+
lines.push(' AGENT CONTEXT DETECTED');
|
|
32
|
+
lines.push('========================================');
|
|
33
|
+
lines.push(`Agent: ${AGENT_DISPLAY_NAMES[ctx.agent] || ctx.agent}`);
|
|
34
|
+
lines.push(`Session ID: ${ctx.sessionId}`);
|
|
35
|
+
|
|
36
|
+
if (ctx.metadata.model) {
|
|
37
|
+
lines.push(`Model: ${ctx.metadata.model}`);
|
|
38
|
+
}
|
|
39
|
+
if (ctx.metadata.version) {
|
|
40
|
+
lines.push(`Version: ${ctx.metadata.version}`);
|
|
41
|
+
}
|
|
42
|
+
if (ctx.metadata.gitBranch) {
|
|
43
|
+
lines.push(`Git Branch: ${ctx.metadata.gitBranch}`);
|
|
44
|
+
}
|
|
45
|
+
if (ctx.metadata.startedAt) {
|
|
46
|
+
lines.push(`Started At: ${ctx.metadata.startedAt}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
lines.push('');
|
|
50
|
+
lines.push('Task Prompt:');
|
|
51
|
+
lines.push(` ${truncate(ctx.taskPrompt.replace(/\n/g, ' '), 500)}`);
|
|
52
|
+
|
|
53
|
+
lines.push('');
|
|
54
|
+
lines.push(`Modified Files (${ctx.modifiedFiles.length}):`);
|
|
55
|
+
if (ctx.modifiedFiles.length === 0) {
|
|
56
|
+
lines.push(' (none)');
|
|
57
|
+
} else {
|
|
58
|
+
for (const f of ctx.modifiedFiles) {
|
|
59
|
+
// Find which tools modified this file
|
|
60
|
+
const tools = new Set<string>();
|
|
61
|
+
for (const turn of ctx.transcript) {
|
|
62
|
+
for (const mf of turn.modifiedFiles) {
|
|
63
|
+
if (mf.filePath === f) tools.add(mf.toolName);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
lines.push(` - ${f} (${Array.from(tools).join(', ')})`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
lines.push('');
|
|
71
|
+
lines.push('Token Usage:');
|
|
72
|
+
lines.push(
|
|
73
|
+
` Input: ${formatNumber(ctx.tokenUsage.inputTokens)} Output: ${formatNumber(ctx.tokenUsage.outputTokens)}`,
|
|
74
|
+
);
|
|
75
|
+
if (ctx.tokenUsage.cacheCreationTokens || ctx.tokenUsage.cacheReadTokens) {
|
|
76
|
+
lines.push(
|
|
77
|
+
` Cache Creation: ${formatNumber(ctx.tokenUsage.cacheCreationTokens || 0)} Cache Read: ${formatNumber(ctx.tokenUsage.cacheReadTokens || 0)}`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
lines.push('');
|
|
82
|
+
lines.push(`Transcript (${ctx.transcript.length} turns):`);
|
|
83
|
+
for (const turn of ctx.transcript) {
|
|
84
|
+
lines.push(formatTurnSummary(turn));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
lines.push('========================================');
|
|
88
|
+
return lines.join('\n');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function formatJson(ctx: AgentContext): string {
|
|
92
|
+
return JSON.stringify(ctx, null, 2);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function formatContext(ctx: AgentContext): string {
|
|
96
|
+
const mode = process.env.AGENT_CONTEXT_FORMAT || 'human';
|
|
97
|
+
if (mode === 'json') return formatJson(ctx);
|
|
98
|
+
return formatHuman(ctx);
|
|
99
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { detectAgent } from './detect.js';
|
|
2
|
+
import { ClaudeParser } from './parsers/claude.js';
|
|
3
|
+
import { GeminiParser } from './parsers/gemini.js';
|
|
4
|
+
import { OpenCodeParser } from './parsers/opencode.js';
|
|
5
|
+
import { formatContext } from './format.js';
|
|
6
|
+
import type { AgentParser } from './types.js';
|
|
7
|
+
|
|
8
|
+
const PARSERS: Record<string, AgentParser> = {
|
|
9
|
+
'claude-code': new ClaudeParser(),
|
|
10
|
+
'gemini-cli': new GeminiParser(),
|
|
11
|
+
opencode: new OpenCodeParser(),
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
const repoRoot = process.argv[2] || process.cwd();
|
|
16
|
+
|
|
17
|
+
const detection = await detectAgent(repoRoot);
|
|
18
|
+
|
|
19
|
+
if (!detection.detected || !detection.agent || !detection.sessionFilePath) {
|
|
20
|
+
if (process.env.AGENT_CONTEXT_VERBOSE) {
|
|
21
|
+
console.error('[agent-context] No AI agent session detected.');
|
|
22
|
+
}
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const parser = PARSERS[detection.agent];
|
|
27
|
+
if (!parser) {
|
|
28
|
+
console.error(`[agent-context] No parser for agent: ${detection.agent}`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const context = await parser.parse(detection.sessionFilePath, repoRoot);
|
|
33
|
+
const output = formatContext(context);
|
|
34
|
+
console.error(output);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
main().catch((err) => {
|
|
38
|
+
console.error(`[agent-context] Error: ${err.message}`);
|
|
39
|
+
});
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import type {
|
|
3
|
+
AgentContext,
|
|
4
|
+
AgentParser,
|
|
5
|
+
DetectionResult,
|
|
6
|
+
InteractionTurn,
|
|
7
|
+
ModifiedFile,
|
|
8
|
+
TokenUsage,
|
|
9
|
+
ToolUseInfo,
|
|
10
|
+
} from '../types.js';
|
|
11
|
+
import { detectAgent } from '../detect.js';
|
|
12
|
+
|
|
13
|
+
const FILE_MOD_TOOLS = new Set([
|
|
14
|
+
'Write',
|
|
15
|
+
'Edit',
|
|
16
|
+
'NotebookEdit',
|
|
17
|
+
'mcp__acp__Write',
|
|
18
|
+
'mcp__acp__Edit',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
interface ClaudeLine {
|
|
22
|
+
type?: string;
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
version?: string;
|
|
25
|
+
gitBranch?: string;
|
|
26
|
+
uuid?: string;
|
|
27
|
+
timestamp?: string;
|
|
28
|
+
message?: {
|
|
29
|
+
id?: string;
|
|
30
|
+
model?: string;
|
|
31
|
+
role?: string;
|
|
32
|
+
content?: unknown;
|
|
33
|
+
usage?: {
|
|
34
|
+
input_tokens?: number;
|
|
35
|
+
output_tokens?: number;
|
|
36
|
+
cache_creation_input_tokens?: number;
|
|
37
|
+
cache_read_input_tokens?: number;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ContentBlock {
|
|
43
|
+
type: string;
|
|
44
|
+
text?: string;
|
|
45
|
+
thinking?: string;
|
|
46
|
+
name?: string;
|
|
47
|
+
id?: string;
|
|
48
|
+
input?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function parseLines(raw: string): ClaudeLine[] {
|
|
52
|
+
const lines: ClaudeLine[] = [];
|
|
53
|
+
for (const line of raw.split('\n')) {
|
|
54
|
+
const trimmed = line.trim();
|
|
55
|
+
if (!trimmed) continue;
|
|
56
|
+
try {
|
|
57
|
+
lines.push(JSON.parse(trimmed));
|
|
58
|
+
} catch {
|
|
59
|
+
// Skip unparseable lines (e.g. partial write from concurrent agent)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return lines;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function extractTextContent(content: unknown): string {
|
|
66
|
+
if (typeof content === 'string') return content;
|
|
67
|
+
if (!Array.isArray(content)) return '';
|
|
68
|
+
|
|
69
|
+
const parts: string[] = [];
|
|
70
|
+
for (const block of content as ContentBlock[]) {
|
|
71
|
+
if (block.type === 'text' && block.text) {
|
|
72
|
+
parts.push(block.text);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return parts.join('\n');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function extractModifiedFiles(content: unknown): ModifiedFile[] {
|
|
79
|
+
if (!Array.isArray(content)) return [];
|
|
80
|
+
const files: ModifiedFile[] = [];
|
|
81
|
+
|
|
82
|
+
for (const block of content as ContentBlock[]) {
|
|
83
|
+
if (block.type !== 'tool_use' || !block.name) continue;
|
|
84
|
+
if (!FILE_MOD_TOOLS.has(block.name)) continue;
|
|
85
|
+
|
|
86
|
+
const filePath =
|
|
87
|
+
(block.input?.file_path as string) ||
|
|
88
|
+
(block.input?.path as string) ||
|
|
89
|
+
(block.input?.notebook_path as string);
|
|
90
|
+
if (filePath) {
|
|
91
|
+
files.push({
|
|
92
|
+
filePath,
|
|
93
|
+
toolName: block.name,
|
|
94
|
+
toolUseId: block.id,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return files;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function extractToolUses(content: unknown): ToolUseInfo[] {
|
|
102
|
+
if (!Array.isArray(content)) return [];
|
|
103
|
+
const tools: ToolUseInfo[] = [];
|
|
104
|
+
|
|
105
|
+
for (const block of content as ContentBlock[]) {
|
|
106
|
+
if (block.type !== 'tool_use' || !block.name) continue;
|
|
107
|
+
tools.push({
|
|
108
|
+
toolName: block.name,
|
|
109
|
+
toolUseId: block.id || '',
|
|
110
|
+
input: sanitizeInput(block.input || {}),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return tools;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// intentional: sanitize tool input for display in hook output, not LLM context
|
|
117
|
+
function sanitizeInput(input: Record<string, unknown>): Record<string, unknown> {
|
|
118
|
+
const sanitized: Record<string, unknown> = {};
|
|
119
|
+
for (const [key, value] of Object.entries(input)) {
|
|
120
|
+
if (typeof value === 'string' && value.length > 200) {
|
|
121
|
+
sanitized[key] = value.slice(0, 200) + '... (truncated)';
|
|
122
|
+
} else {
|
|
123
|
+
sanitized[key] = value;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return sanitized;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export class ClaudeParser implements AgentParser {
|
|
130
|
+
async detect(repoPath: string): Promise<DetectionResult> {
|
|
131
|
+
return detectAgent(repoPath);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async parse(
|
|
135
|
+
sessionFilePath: string,
|
|
136
|
+
_repoPath: string,
|
|
137
|
+
): Promise<AgentContext> {
|
|
138
|
+
const raw = fs.readFileSync(sessionFilePath, 'utf-8');
|
|
139
|
+
const allLines = parseLines(raw);
|
|
140
|
+
|
|
141
|
+
// Filter to user and assistant lines only
|
|
142
|
+
const userLines = allLines.filter((l) => l.type === 'user');
|
|
143
|
+
const assistantLines = allLines.filter((l) => l.type === 'assistant');
|
|
144
|
+
|
|
145
|
+
// Deduplicate assistant messages by message.id (streaming produces duplicates)
|
|
146
|
+
const dedupedAssistant = new Map<string, ClaudeLine>();
|
|
147
|
+
for (const line of assistantLines) {
|
|
148
|
+
const msgId = line.message?.id;
|
|
149
|
+
if (msgId) {
|
|
150
|
+
dedupedAssistant.set(msgId, line);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const uniqueAssistant = Array.from(dedupedAssistant.values());
|
|
154
|
+
|
|
155
|
+
// Build transcript turns
|
|
156
|
+
const transcript: InteractionTurn[] = [];
|
|
157
|
+
let turnIndex = 0;
|
|
158
|
+
|
|
159
|
+
// Interleave user and assistant messages by position
|
|
160
|
+
// User messages appear at even indices, assistant at odd
|
|
161
|
+
const maxTurns = Math.max(userLines.length, uniqueAssistant.length);
|
|
162
|
+
for (let i = 0; i < maxTurns; i++) {
|
|
163
|
+
if (i < userLines.length) {
|
|
164
|
+
const user = userLines[i];
|
|
165
|
+
transcript.push({
|
|
166
|
+
turnIndex: turnIndex++,
|
|
167
|
+
role: 'user',
|
|
168
|
+
content: extractTextContent(user.message?.content),
|
|
169
|
+
modifiedFiles: [],
|
|
170
|
+
toolUses: [],
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (i < uniqueAssistant.length) {
|
|
174
|
+
const asst = uniqueAssistant[i];
|
|
175
|
+
const content = asst.message?.content;
|
|
176
|
+
const usage = asst.message?.usage;
|
|
177
|
+
transcript.push({
|
|
178
|
+
turnIndex: turnIndex++,
|
|
179
|
+
role: 'assistant',
|
|
180
|
+
content: extractTextContent(content),
|
|
181
|
+
modifiedFiles: extractModifiedFiles(content),
|
|
182
|
+
tokenUsage: usage
|
|
183
|
+
? {
|
|
184
|
+
inputTokens: usage.input_tokens || 0,
|
|
185
|
+
outputTokens: usage.output_tokens || 0,
|
|
186
|
+
cacheCreationTokens: usage.cache_creation_input_tokens,
|
|
187
|
+
cacheReadTokens: usage.cache_read_input_tokens,
|
|
188
|
+
}
|
|
189
|
+
: undefined,
|
|
190
|
+
toolUses: extractToolUses(content),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Task prompt: first user message where content is a plain string
|
|
196
|
+
// (not a tool_result array) and not a system instruction injected by the host
|
|
197
|
+
let taskPrompt = '';
|
|
198
|
+
for (const user of userLines) {
|
|
199
|
+
const content = user.message?.content;
|
|
200
|
+
if (typeof content !== 'string') continue;
|
|
201
|
+
if (content.startsWith('<system_instruction>') || content.startsWith('<system-')) continue;
|
|
202
|
+
taskPrompt = content;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Aggregate token usage
|
|
207
|
+
const totalUsage: TokenUsage = {
|
|
208
|
+
inputTokens: 0,
|
|
209
|
+
outputTokens: 0,
|
|
210
|
+
cacheCreationTokens: 0,
|
|
211
|
+
cacheReadTokens: 0,
|
|
212
|
+
};
|
|
213
|
+
for (const asst of uniqueAssistant) {
|
|
214
|
+
const usage = asst.message?.usage;
|
|
215
|
+
if (usage) {
|
|
216
|
+
totalUsage.inputTokens += usage.input_tokens || 0;
|
|
217
|
+
totalUsage.outputTokens += usage.output_tokens || 0;
|
|
218
|
+
totalUsage.cacheCreationTokens! += usage.cache_creation_input_tokens || 0;
|
|
219
|
+
totalUsage.cacheReadTokens! += usage.cache_read_input_tokens || 0;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Collect all modified files (deduplicated)
|
|
224
|
+
const allModifiedFiles = new Set<string>();
|
|
225
|
+
for (const turn of transcript) {
|
|
226
|
+
for (const f of turn.modifiedFiles) {
|
|
227
|
+
allModifiedFiles.add(f.filePath);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Metadata from first lines
|
|
232
|
+
const firstUser = userLines[0];
|
|
233
|
+
const firstAssistant = uniqueAssistant[0];
|
|
234
|
+
const stat = fs.statSync(sessionFilePath);
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
agent: 'claude-code',
|
|
238
|
+
sessionId: firstUser?.sessionId || '',
|
|
239
|
+
sessionFilePath,
|
|
240
|
+
taskPrompt,
|
|
241
|
+
modifiedFiles: Array.from(allModifiedFiles),
|
|
242
|
+
tokenUsage: totalUsage,
|
|
243
|
+
transcript,
|
|
244
|
+
metadata: {
|
|
245
|
+
model: firstAssistant?.message?.model,
|
|
246
|
+
version: firstUser?.version,
|
|
247
|
+
gitBranch: firstUser?.gitBranch,
|
|
248
|
+
startedAt: firstUser?.timestamp,
|
|
249
|
+
sessionFileModifiedAt: stat.mtime.toISOString(),
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|