@codex-native/sdk 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 N-API for Rust
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,379 @@
1
+ # Codex Native SDK
2
+
3
+ Embed the Codex agent in your Node.js workflows and apps with native performance.
4
+
5
+ The Native SDK provides Rust-powered bindings via [napi-rs](https://napi.rs/), giving you direct access to Codex functionality without spawning child processes. This enables custom tool registration, agent orchestration, and native performance optimizations.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @codex-native/sdk
11
+ ```
12
+
13
+ Requires Node.js 18+.
14
+
15
+ ## Quickstart
16
+
17
+ ```typescript
18
+ import { Codex } from "@codex-native/sdk";
19
+
20
+ const codex = new Codex();
21
+ const thread = codex.startThread();
22
+ const turn = await thread.run("Diagnose the test failure and propose a fix");
23
+
24
+ console.log(turn.finalResponse);
25
+ console.log(turn.items);
26
+ ```
27
+
28
+ Call `run()` repeatedly on the same `Thread` instance to continue that conversation.
29
+
30
+ ```typescript
31
+ const nextTurn = await thread.run("Implement the fix");
32
+ ```
33
+
34
+ ### Streaming responses
35
+
36
+ `run()` buffers events until the turn finishes. To react to intermediate progress—tool calls, streaming responses, and file diffs—use `runStreamed()` instead, which returns an async generator of structured events.
37
+
38
+ ```typescript
39
+ const { events } = await thread.runStreamed("Diagnose the test failure and propose a fix");
40
+
41
+ for await (const event of events) {
42
+ switch (event.type) {
43
+ case "item.completed":
44
+ console.log("item", event.item);
45
+ break;
46
+ case "turn.completed":
47
+ console.log("usage", event.usage);
48
+ break;
49
+ }
50
+ }
51
+ ```
52
+
53
+ ### Structured output
54
+
55
+ The Codex agent can produce a JSON response that conforms to a specified schema. The schema can be provided for each turn as a plain JSON object.
56
+
57
+ ```typescript
58
+ const schema = {
59
+ type: "object",
60
+ properties: {
61
+ summary: { type: "string" },
62
+ status: { type: "string", enum: ["ok", "action_required"] },
63
+ },
64
+ required: ["summary", "status"],
65
+ additionalProperties: false,
66
+ } as const;
67
+
68
+ const turn = await thread.run("Summarize repository status", { outputSchema: schema });
69
+ console.log(turn.finalResponse);
70
+ ```
71
+
72
+ You can also create a JSON schema from a [Zod schema](https://github.com/colinhacks/zod) using the [`zod-to-json-schema`](https://www.npmjs.com/package/zod-to-json-schema) package and setting the `target` to `"openAi"`.
73
+
74
+ ```typescript
75
+ const schema = z.object({
76
+ summary: z.string(),
77
+ status: z.enum(["ok", "action_required"]),
78
+ });
79
+
80
+ const turn = await thread.run("Summarize repository status", {
81
+ outputSchema: zodToJsonSchema(schema, { target: "openAi" }),
82
+ });
83
+ console.log(turn.finalResponse);
84
+ ```
85
+
86
+ ### Attaching images
87
+
88
+ Provide structured input entries when you need to include images alongside text. Text entries are concatenated into the final prompt while image entries are passed to Codex via the native bridge.
89
+
90
+ ```typescript
91
+ const turn = await thread.run([
92
+ { type: "text", text: "Describe these screenshots" },
93
+ { type: "local_image", path: "./ui.png" },
94
+ { type: "local_image", path: "./diagram.jpg" },
95
+ ]);
96
+ ```
97
+
98
+ ### Resuming an existing thread
99
+
100
+ Threads are persisted in `~/.codex/sessions`. If you lose the in-memory `Thread` object, reconstruct it with `resumeThread()` and keep going.
101
+
102
+ ```typescript
103
+ const savedThreadId = process.env.CODEX_THREAD_ID!;
104
+ const thread = codex.resumeThread(savedThreadId);
105
+ await thread.run("Implement the fix");
106
+ ```
107
+
108
+ ### Working directory controls
109
+
110
+ Codex runs in the current working directory by default. To avoid unrecoverable errors, Codex requires the working directory to be a Git repository. You can skip the Git repository check by passing the `skipGitRepoCheck` option when creating a thread.
111
+
112
+ ```typescript
113
+ const thread = codex.startThread({
114
+ workingDirectory: "/path/to/project",
115
+ skipGitRepoCheck: true,
116
+ });
117
+ ```
118
+
119
+ ## Native-Specific Features
120
+
121
+ The Native SDK provides additional capabilities beyond the TypeScript SDK:
122
+
123
+ ### Custom Tool Registration
124
+
125
+ Register JavaScript functions as tools that Codex can discover and invoke during execution. Tools are registered globally on the `Codex` instance and become available to all threads and agents.
126
+
127
+ ```typescript
128
+ const codex = new Codex();
129
+
130
+ codex.registerTool({
131
+ name: "calculator",
132
+ description: "Performs arithmetic operations",
133
+ parameters: {
134
+ type: "object",
135
+ properties: {
136
+ operation: { type: "string", enum: ["add", "subtract", "multiply", "divide"] },
137
+ a: { type: "number" },
138
+ b: { type: "number" },
139
+ },
140
+ required: ["operation", "a", "b"],
141
+ },
142
+ handler: (err, invocation) => {
143
+ if (err) {
144
+ return { error: err.message };
145
+ }
146
+
147
+ const { operation, a, b } = JSON.parse(invocation.arguments);
148
+ let result: number;
149
+
150
+ switch (operation) {
151
+ case "add": result = a + b; break;
152
+ case "subtract": result = a - b; break;
153
+ case "multiply": result = a * b; break;
154
+ case "divide": result = a / b; break;
155
+ }
156
+
157
+ return {
158
+ output: `Result: ${result}`,
159
+ success: true
160
+ };
161
+ },
162
+ });
163
+
164
+ const thread = codex.startThread();
165
+ await thread.run("Calculate 42 times 17");
166
+ ```
167
+
168
+ **Tool Handler Signature:**
169
+
170
+ Handlers use Node.js error-first callback convention:
171
+ - `err`: Error object if invocation failed, `null` otherwise
172
+ - `invocation`: Object containing:
173
+ - `callId`: Unique identifier for this tool call
174
+ - `toolName`: Name of the tool being invoked
175
+ - `arguments`: JSON string of the tool arguments (if `parameters` provided)
176
+ - `input`: JSON string of the tool input (for custom tools)
177
+
178
+ **Return Value:**
179
+
180
+ Return an object with:
181
+ - `output`: String output to send back to the model
182
+ - `success` (optional): Boolean indicating success/failure
183
+ - `error` (optional): Error message if the tool execution failed
184
+
185
+ ### Agent Orchestration
186
+
187
+ Create specialized agents with custom system prompts and tools for multi-agent workflows.
188
+
189
+ ```typescript
190
+ const codex = new Codex();
191
+
192
+ // Register tools available to agents
193
+ codex.registerTool({
194
+ name: "search_codebase",
195
+ description: "Search the codebase for specific patterns",
196
+ parameters: { /* ... */ },
197
+ handler: (err, inv) => {
198
+ // Search implementation
199
+ return { output: "Found 5 matches", success: true };
200
+ },
201
+ });
202
+
203
+ // Create a specialized agent
204
+ const reviewAgent = codex.createAgent({
205
+ name: "code_reviewer",
206
+ instructions: "You are a senior code reviewer. Focus on security, performance, and maintainability.",
207
+ });
208
+
209
+ const result = await reviewAgent.run("Review the authentication module");
210
+ console.log(result.finalResponse);
211
+ ```
212
+
213
+ ### Agent Handoffs
214
+
215
+ Agents can hand off tasks to other agents for specialized processing.
216
+
217
+ ```typescript
218
+ const codex = new Codex();
219
+
220
+ const testAgent = codex.createAgent({
221
+ name: "test_writer",
222
+ instructions: "You write comprehensive unit tests with high coverage.",
223
+ });
224
+
225
+ const securityAgent = codex.createAgent({
226
+ name: "security_auditor",
227
+ instructions: "You perform security audits and identify vulnerabilities.",
228
+ });
229
+
230
+ // Start with test agent
231
+ const testResult = await testAgent.run("Write tests for the auth module");
232
+
233
+ // Hand off to security agent
234
+ const securityResult = await securityAgent.run(
235
+ "Review the tests from the previous agent and add security-focused test cases"
236
+ );
237
+ ```
238
+
239
+ Agents automatically have access to the conversation history, enabling seamless handoffs between specialized agents.
240
+
241
+ ## API Options
242
+
243
+ ### Codex Constructor Options
244
+
245
+ ```typescript
246
+ interface CodexOptions {
247
+ apiKey?: string; // Responses API key (defaults to ANTHROPIC_API_KEY env var)
248
+ baseUrl?: string; // API base URL override
249
+ skipGitRepoCheck?: boolean; // Skip Git repository validation
250
+ }
251
+ ```
252
+
253
+ ### Thread Options
254
+
255
+ ```typescript
256
+ interface ThreadOptions {
257
+ model?: string; // Model to use (e.g., "claude-sonnet-4")
258
+ sandboxMode?: "read-only" | "workspace-write" | "danger-full-access";
259
+ workingDirectory?: string; // Directory to run Codex in
260
+ skipGitRepoCheck?: boolean; // Skip Git repository validation
261
+ }
262
+ ```
263
+
264
+ ### Turn Options
265
+
266
+ ```typescript
267
+ interface TurnOptions {
268
+ outputSchema?: JsonValue; // JSON schema for structured output
269
+ }
270
+ ```
271
+
272
+ ### Tool Registration Options
273
+
274
+ ```typescript
275
+ interface ToolRegistration {
276
+ name: string; // Tool name (used by model to invoke)
277
+ description?: string; // Description of what the tool does
278
+ parameters?: JsonValue; // JSON Schema for tool parameters
279
+ strict?: boolean; // Enable strict schema validation
280
+ supportsParallel?: boolean; // Whether tool supports parallel execution
281
+ handler: (err: Error | null, invocation: ToolInvocation) => ToolResponse;
282
+ }
283
+ ```
284
+
285
+ ## Architecture
286
+
287
+ The Native SDK uses [napi-rs](https://napi.rs/) to bridge JavaScript and Rust:
288
+
289
+ - **Native Bindings**: Direct Rust FFI via NAPI for zero-copy performance
290
+ - **Tool Registration**: JavaScript functions are stored as ThreadsafeFunction callbacks
291
+ - **Event Streaming**: Async generators powered by Tokio runtime
292
+ - **Session Management**: Native threads and agents run in the Rust codex-core
293
+
294
+ Platform-specific binaries are automatically selected at runtime:
295
+ - macOS: `codex_native.darwin-{arm64,x64}.node`
296
+ - Windows: `codex_native.win32-{x64,arm64,ia32}-msvc.node`
297
+ - Linux: `codex_native.linux-{x64,arm64}-{gnu,musl}.node`
298
+
299
+ ## Development
300
+
301
+ Build from source:
302
+
303
+ ```bash
304
+ pnpm install
305
+ pnpm --filter @codex-native/sdk run build
306
+ ```
307
+
308
+ Run tests:
309
+
310
+ ```bash
311
+ pnpm --filter @codex-native/sdk test
312
+ ```
313
+
314
+ The build emits:
315
+ - Platform-specific `.node` binaries (native addons)
316
+ - TypeScript declarations in `index.d.ts`
317
+ - ESM wrapper in `dist/index.mjs`
318
+
319
+ ## Publishing
320
+
321
+ The Native SDK uses napi-rs's multi-platform publishing strategy with automated release scripts.
322
+
323
+ ### Release Scripts
324
+
325
+ ```bash
326
+ # Patch release (0.0.x)
327
+ pnpm run release:patch
328
+
329
+ # Minor release (0.x.0)
330
+ pnpm run release:minor
331
+
332
+ # Major release (x.0.0)
333
+ pnpm run release:major
334
+
335
+ # Dry run (test without publishing)
336
+ pnpm run release:dry
337
+ ```
338
+
339
+ ### What Happens During Release
340
+
341
+ 1. **Version bump**: Updates version in package.json and all platform packages
342
+ 2. **Build**: Compiles native binary + TypeScript wrapper
343
+ 3. **Test**: Runs full test suite (34 tests)
344
+ 4. **Prepublish**: Copies binaries to platform packages via `prepublishOnly` hook
345
+ 5. **Publish**: Publishes 9 packages to npm:
346
+ - Main: `@codex-native/sdk`
347
+ - Platforms: `@codex-native/{darwin-arm64,darwin-x64,linux-x64-gnu,linux-arm64-gnu,linux-x64-musl,linux-arm64-musl,win32-x64-msvc,win32-arm64-msvc}`
348
+
349
+ ### Multi-Platform CI/CD
350
+
351
+ For full cross-platform support, build on CI for all 8 targets:
352
+
353
+ ```yaml
354
+ # .github/workflows/release.yml
355
+ strategy:
356
+ matrix:
357
+ settings:
358
+ - host: macos-latest
359
+ target: aarch64-apple-darwin
360
+ - host: macos-latest
361
+ target: x86_64-apple-darwin
362
+ - host: ubuntu-latest
363
+ target: x86_64-unknown-linux-gnu
364
+ - host: ubuntu-latest
365
+ target: aarch64-unknown-linux-gnu
366
+ # ... other platforms
367
+ ```
368
+
369
+ After building all platforms:
370
+ ```bash
371
+ pnpm run artifacts # Download and organize binaries
372
+ pnpm run release # Publish all packages
373
+ ```
374
+
375
+ npm automatically installs the correct platform package as an optional dependency.
376
+
377
+ ## License
378
+
379
+ See [LICENSE](../../LICENSE)
Binary file