@anagnole/claude-cli 0.1.0 → 0.1.1
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 +151 -0
- package/package.json +8 -2
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# @anagnole/claude-cli
|
|
2
|
+
|
|
3
|
+
A shared library for spawning, parsing, and managing [Claude Code CLI](https://code.claude.com) sessions from TypeScript/Node.js. Exposes the full power of the CLI — MCP servers, permission modes, tool control, git worktrees, cost limits, and more.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @anagnole/claude-cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires the Claude CLI (`claude`) to be installed and available in your PATH.
|
|
12
|
+
|
|
13
|
+
## Basic usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { spawnClaude, NdjsonParser } from "@anagnole/claude-cli";
|
|
17
|
+
|
|
18
|
+
// Spawn a Claude CLI process
|
|
19
|
+
const child = spawnClaude({
|
|
20
|
+
prompt: "Explain this codebase",
|
|
21
|
+
model: "claude-sonnet-4-6",
|
|
22
|
+
streaming: true,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Parse streaming NDJSON output
|
|
26
|
+
const parser = new NdjsonParser();
|
|
27
|
+
child.stdout.on("data", (chunk) => {
|
|
28
|
+
for (const event of parser.feed(chunk.toString())) {
|
|
29
|
+
console.log(event);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Full CLI options
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { spawnClaude } from "@anagnole/claude-cli";
|
|
38
|
+
|
|
39
|
+
const child = spawnClaude({
|
|
40
|
+
prompt: "Fix the auth bug",
|
|
41
|
+
model: "claude-sonnet-4-6",
|
|
42
|
+
streaming: false,
|
|
43
|
+
|
|
44
|
+
// System prompt
|
|
45
|
+
systemPrompt: "You are a senior engineer.",
|
|
46
|
+
appendSystemPrompt: true,
|
|
47
|
+
|
|
48
|
+
// Session management
|
|
49
|
+
resumeSessionId: "uuid-from-previous-run",
|
|
50
|
+
// or: continueConversation: true,
|
|
51
|
+
// or: forkSession: true,
|
|
52
|
+
|
|
53
|
+
// Permissions & tools
|
|
54
|
+
permissionMode: "bypassPermissions",
|
|
55
|
+
allowedTools: ["Bash(git *)", "Read", "Edit"],
|
|
56
|
+
disallowedTools: ["Write"],
|
|
57
|
+
// cliTools: "" to disable all, "default" for all
|
|
58
|
+
|
|
59
|
+
// MCP servers
|
|
60
|
+
mcpConfig: "/path/to/mcp-config.json",
|
|
61
|
+
strictMcpConfig: true,
|
|
62
|
+
|
|
63
|
+
// Workspace
|
|
64
|
+
workingDirectory: "/path/to/project",
|
|
65
|
+
worktree: "feature-branch",
|
|
66
|
+
addDirs: ["../shared-lib"],
|
|
67
|
+
|
|
68
|
+
// Safety limits
|
|
69
|
+
maxTurns: 10,
|
|
70
|
+
maxBudgetUsd: 1.00,
|
|
71
|
+
|
|
72
|
+
// Other
|
|
73
|
+
effort: "high",
|
|
74
|
+
fallbackModel: "claude-haiku-4-5",
|
|
75
|
+
claudePath: "/custom/path/to/claude",
|
|
76
|
+
jsonSchema: { type: "object", properties: { answer: { type: "string" } } },
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Session management
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { SessionMap, type MessageParam } from "@anagnole/claude-cli";
|
|
84
|
+
|
|
85
|
+
const sessions = new SessionMap();
|
|
86
|
+
|
|
87
|
+
// Hash message history to find a resumable CLI session
|
|
88
|
+
const messages: MessageParam[] = [
|
|
89
|
+
{ role: "user", content: "Hello" },
|
|
90
|
+
{ role: "assistant", content: "Hi!" },
|
|
91
|
+
{ role: "user", content: "What did I say?" },
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
const hash = SessionMap.hashContext(messages);
|
|
95
|
+
const sessionId = sessions.lookup(hash, "claude-sonnet-4-6");
|
|
96
|
+
|
|
97
|
+
// After a response, store for future --resume
|
|
98
|
+
sessions.store(
|
|
99
|
+
[...messages, { role: "assistant", content: "You said Hello" }],
|
|
100
|
+
"cli-session-uuid",
|
|
101
|
+
"claude-sonnet-4-6",
|
|
102
|
+
);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Transform helpers
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import {
|
|
109
|
+
extractPrompt, // Get last user message as text
|
|
110
|
+
extractSystem, // Flatten system prompt blocks to string
|
|
111
|
+
warnUnsupported, // Log warnings for unsupported API params
|
|
112
|
+
buildResponse, // CLI JSON result → Anthropic API response shape
|
|
113
|
+
generateMsgId, // Generate msg_... IDs
|
|
114
|
+
createStreamState, // Initialize SSE streaming state
|
|
115
|
+
transformEvent, // CLI NDJSON event → Anthropic SSE strings
|
|
116
|
+
} from "@anagnole/claude-cli";
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Subpath imports
|
|
120
|
+
|
|
121
|
+
For lighter imports that avoid pulling in all modules:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
import { spawnClaude } from "@anagnole/claude-cli/cli";
|
|
125
|
+
import { NdjsonParser } from "@anagnole/claude-cli/parser";
|
|
126
|
+
import { SessionMap } from "@anagnole/claude-cli/session";
|
|
127
|
+
import type { MessagesRequest } from "@anagnole/claude-cli/types";
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## All exports
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
// CLI
|
|
134
|
+
spawnClaude, SpawnOptions, NdjsonParser
|
|
135
|
+
|
|
136
|
+
// Session
|
|
137
|
+
SessionMap
|
|
138
|
+
|
|
139
|
+
// Transform
|
|
140
|
+
extractPrompt, extractSystem, warnUnsupported
|
|
141
|
+
buildResponse, generateMsgId
|
|
142
|
+
createStreamState, transformEvent, StreamState
|
|
143
|
+
|
|
144
|
+
// Types
|
|
145
|
+
ContentBlock, SystemBlock, MessageParam,
|
|
146
|
+
MessagesRequest, MessagesResponse, Usage, ApiError, apiError
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Companion: API server
|
|
150
|
+
|
|
151
|
+
This package also comes with a companion HTTP server (`@anagnole/claude-api-server`) that wraps the core library as an Anthropic Messages API-compatible endpoint. See the [monorepo README](https://github.com/anagnole/claude-api) for details.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anagnole/claude-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Shared library for spawning, parsing, and managing Claude Code CLI sessions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -35,7 +35,13 @@
|
|
|
35
35
|
"build": "tsc",
|
|
36
36
|
"prepublishOnly": "tsc"
|
|
37
37
|
},
|
|
38
|
-
"keywords": [
|
|
38
|
+
"keywords": [
|
|
39
|
+
"claude",
|
|
40
|
+
"cli",
|
|
41
|
+
"anthropic",
|
|
42
|
+
"proxy",
|
|
43
|
+
"agent"
|
|
44
|
+
],
|
|
39
45
|
"license": "MIT",
|
|
40
46
|
"engines": {
|
|
41
47
|
"node": ">=18"
|