kimiflare 0.48.5 → 0.50.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 CHANGED
@@ -77,6 +77,107 @@ kimiflare -p "..." --dangerously-allow-all # auto-approve mutating tool
77
77
  kimiflare -p "..." --reasoning # include chain-of-thought in stderr
78
78
  ```
79
79
 
80
+ ### Headless SDK
81
+
82
+ Use KimiFlare programmatically from your own application — no TUI required.
83
+
84
+ ```ts
85
+ import { createAgentSession } from "kimiflare/sdk";
86
+
87
+ const { session } = await createAgentSession({
88
+ cwd: "/path/to/project",
89
+ config: {
90
+ accountId: process.env.CLOUDFLARE_ACCOUNT_ID,
91
+ apiToken: process.env.CLOUDFLARE_API_TOKEN,
92
+ model: "@cf/moonshotai/kimi-k2.6",
93
+ },
94
+ });
95
+
96
+ // Stream every event: text deltas, tool calls, tasks, usage
97
+ session.subscribe((event) => {
98
+ console.log(event.type, event);
99
+ });
100
+
101
+ // Send a prompt
102
+ await session.prompt("Refactor auth to JWT + Redis");
103
+
104
+ // Mid-flight correction while the agent is still running
105
+ await session.steer("Use Redis instead of in-memory store");
106
+
107
+ // After the turn finishes
108
+ await session.followUp("Also add unit tests");
109
+
110
+ // Clean up
111
+ session.dispose();
112
+ ```
113
+
114
+ **Key features:**
115
+ - `subscribe()` — receive typed events (`text_delta`, `tool_call`, `tool_result`, `task_update`, `usage`, `error`, `done`, etc.)
116
+ - `prompt()` / `steer()` / `followUp()` — full conversation lifecycle
117
+ - `pause()` / `resume()` — graceful preemption
118
+ - `getStatus()` / `getUsage()` — inspect session state
119
+ - Custom `permissionHandler` — decide programmatically whether to allow mutating tools
120
+ - Optional `memoryEnabled`, `lspEnabled`, `costAttribution` flags
121
+
122
+ #### SDK Authentication
123
+
124
+ The SDK needs a Cloudflare **Account ID** and **API Token** to call Workers AI directly. Credentials are resolved in this priority order:
125
+
126
+ 1. **Explicit `config` object** (recommended for apps)
127
+ 2. **Environment variables**: `CLOUDFLARE_ACCOUNT_ID` / `CF_ACCOUNT_ID`, `CLOUDFLARE_API_TOKEN` / `CF_API_TOKEN`
128
+ 3. **Config file**: `~/.config/kimiflare/config.json`
129
+
130
+ **For Electron / desktop apps**, we recommend storing credentials in the OS keychain (e.g. Electron `safeStorage` or `keytar`) and passing them explicitly:
131
+
132
+ ```ts
133
+ import { createAgentSession } from "kimiflare/sdk";
134
+
135
+ const accountId = await keytar.getPassword("kimiflare", "accountId");
136
+ const apiToken = await keytar.getPassword("kimiflare", "apiToken");
137
+
138
+ const { session } = await createAgentSession({
139
+ cwd: projectPath,
140
+ config: { accountId, apiToken },
141
+ });
142
+ ```
143
+
144
+ **For zero-credential onboarding**, use KimiFlare Cloud mode. The user authenticates via GitHub device flow and a Cloudflare Worker proxies AI requests. Your app never sees raw Cloudflare credentials — only a GitHub token and `remoteWorkerUrl`.
145
+
146
+ #### RPC mode (subprocess)
147
+
148
+ If you need process isolation or a non-Node consumer, run KimiFlare in JSONL-over-stdio RPC mode:
149
+
150
+ ```sh
151
+ node bin/kimiflare.mjs --mode rpc
152
+ ```
153
+
154
+ ```ts
155
+ import { spawn } from "node:child_process";
156
+
157
+ const proc = spawn("npx", ["kimiflare", "--mode", "rpc"], {
158
+ cwd: projectPath,
159
+ stdio: ["pipe", "pipe", "pipe"],
160
+ });
161
+
162
+ // Read events
163
+ proc.stdout.on("data", (chunk) => {
164
+ for (const line of chunk.toString().split("\n")) {
165
+ if (!line.trim()) continue;
166
+ const event = JSON.parse(line);
167
+ console.log(event.type, event);
168
+ }
169
+ });
170
+
171
+ // Send commands
172
+ proc.stdin.write(JSON.stringify({ type: "new_session" }) + "\n");
173
+ proc.stdin.write(JSON.stringify({ type: "prompt", message: "Hello" }) + "\n");
174
+
175
+ // Resolve a permission request
176
+ proc.stdin.write(
177
+ JSON.stringify({ type: "resolve_permission", requestId: "req_0", decision: "allow" }) + "\n"
178
+ );
179
+ ```
180
+
80
181
  ### Image understanding
81
182
 
82
183
  ```sh
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import { startRpcServer } from "../dist/sdk/index.js";
3
+ await startRpcServer();
@@ -0,0 +1,2 @@
1
+
2
+ export { }