ai-sdk-provider-codex-cli 0.1.0-ai-sdk-v4

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,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ben Vargas
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.
22
+
package/README.md ADDED
@@ -0,0 +1,210 @@
1
+ # AI SDK Provider for Codex CLI
2
+
3
+ [![npm version](https://img.shields.io/npm/v/ai-sdk-provider-codex-cli.svg)](https://www.npmjs.com/package/ai-sdk-provider-codex-cli)
4
+ [![npm downloads](https://img.shields.io/npm/dm/ai-sdk-provider-codex-cli.svg)](https://www.npmjs.com/package/ai-sdk-provider-codex-cli)
5
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6
+ ![Node >= 18](https://img.shields.io/badge/node-%3E%3D18-43853d?logo=node.js&logoColor=white)
7
+ ![AI SDK v4](https://img.shields.io/badge/AI%20SDK-v4-000?logo=vercel&logoColor=white)
8
+ ![Modules: ESM + CJS](https://img.shields.io/badge/modules-ESM%20%2B%20CJS-3178c6)
9
+ ![TypeScript](https://img.shields.io/badge/TypeScript-blue)
10
+ [![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/ben-vargas/ai-sdk-provider-codex-cli/issues)
11
+ [![Latest Release](https://img.shields.io/github/v/release/ben-vargas/ai-sdk-provider-codex-cli?display_name=tag)](https://github.com/ben-vargas/ai-sdk-provider-codex-cli/releases/latest)
12
+
13
+ A community provider for Vercel AI SDK v4 that uses OpenAI’s Codex CLI (non‑interactive `codex exec`) to talk to GPT‑5 class models with your ChatGPT Plus/Pro subscription. The provider spawns the Codex CLI process, parses its JSONL output, and adapts it to the AI SDK LanguageModelV1 interface.
14
+
15
+ Note: For AI SDK v5 support, see the `main` branch and install the `latest` tag from npm. This branch targets AI SDK v4 and is published under the `ai-sdk-v4` dist-tag.
16
+
17
+ - Works with `generateText`, `streamText`, and `generateObject` (JSON schemas via prompt engineering)
18
+ - Uses ChatGPT OAuth from `codex login` (tokens in `~/.codex/auth.json`) or `OPENAI_API_KEY`
19
+ - Node-only (spawns a local process); supports CI and local dev
20
+
21
+ ## Version Compatibility
22
+
23
+ - Provider (v5 line, main branch): published as `latest`, compatible with AI SDK v5.
24
+ - Provider (v4 line, this branch): published as `ai-sdk-v4`, compatible with AI SDK v4.
25
+
26
+ | Provider Version | AI SDK Version | NPM Tag | Status | Branch |
27
+ |------------------|----------------|-------------|--------------|------------|
28
+ | 0.1.x | v5 | `latest` | Active | `main` |
29
+ | 0.1.0-ai-sdk-v4 | v4 | `ai-sdk-v4` | Maintenance | `ai-sdk-v4`|
30
+
31
+ ### Installing the Right Version
32
+
33
+ - AI SDK v5 (recommended): `npm i ai-sdk-provider-codex-cli ai` (or `@latest`)
34
+ - AI SDK v4 (this branch): `npm i ai-sdk-provider-codex-cli@ai-sdk-v4 ai@^4.3.16`
35
+
36
+ ## AI SDK v4 vs v5
37
+
38
+ - For AI SDK v4 (this branch):
39
+ - Install: `npm i ai-sdk-provider-codex-cli@ai-sdk-v4 ai@^4.3.16`
40
+ - Usage (non-streaming):
41
+
42
+ ```ts
43
+ import { generateText } from 'ai';
44
+ import { codexCli } from 'ai-sdk-provider-codex-cli';
45
+
46
+ const { text } = await generateText({
47
+ model: codexCli('gpt-5', { allowNpx: true, skipGitRepoCheck: true }),
48
+ prompt: 'Hello from v4',
49
+ });
50
+ ```
51
+
52
+ - Usage (streaming):
53
+
54
+ ```ts
55
+ import { streamText } from 'ai';
56
+ import { codexCli } from 'ai-sdk-provider-codex-cli';
57
+
58
+ const { textStream } = await streamText({
59
+ model: codexCli('gpt-5', { allowNpx: true, skipGitRepoCheck: true }),
60
+ prompt: 'Stream a short reply',
61
+ });
62
+ for await (const chunk of textStream) process.stdout.write(chunk);
63
+ ```
64
+
65
+ - For AI SDK v5 (main branch):
66
+ - Install: `npm i ai-sdk-provider-codex-cli ai` (or `@latest`)
67
+ - Usage (streaming):
68
+
69
+ ```ts
70
+ import { streamText } from 'ai';
71
+ import { codexCli } from 'ai-sdk-provider-codex-cli';
72
+
73
+ const result = streamText({
74
+ model: codexCli('gpt-5'),
75
+ prompt: 'Hello from v5',
76
+ });
77
+ const text = await result.text;
78
+ ```
79
+
80
+ - See the `main` branch README for full v5 docs and examples.
81
+
82
+ ## Installation
83
+
84
+ 1. Install and authenticate Codex CLI
85
+
86
+ ```bash
87
+ npm i -g @openai/codex
88
+ codex login # or set OPENAI_API_KEY
89
+ ```
90
+
91
+ 2. Install provider and AI SDK
92
+
93
+ ```bash
94
+ # AI SDK v4 users (this branch)
95
+ npm i ai@^4.3.16 ai-sdk-provider-codex-cli@ai-sdk-v4
96
+
97
+ # AI SDK v5 users (use main branch/latest tag)
98
+ # npm i ai ai-sdk-provider-codex-cli
99
+ ```
100
+
101
+ ## Quick Start
102
+
103
+ Text generation
104
+
105
+ ```js
106
+ import { generateText } from 'ai';
107
+ import { codexCli } from 'ai-sdk-provider-codex-cli';
108
+
109
+ const model = codexCli('gpt-5', {
110
+ allowNpx: true,
111
+ skipGitRepoCheck: true,
112
+ approvalMode: 'on-failure',
113
+ sandboxMode: 'workspace-write',
114
+ });
115
+
116
+ const { text } = await generateText({
117
+ model,
118
+ prompt: 'Reply with a single word: hello.',
119
+ });
120
+ console.log(text);
121
+ ```
122
+
123
+ Streaming
124
+
125
+ ```js
126
+ import { streamText } from 'ai';
127
+ import { codexCli } from 'ai-sdk-provider-codex-cli';
128
+
129
+ const { textStream } = await streamText({
130
+ model: codexCli('gpt-5', { allowNpx: true, skipGitRepoCheck: true }),
131
+ prompt: 'Write two short lines of encouragement.',
132
+ });
133
+ for await (const chunk of textStream) process.stdout.write(chunk);
134
+ ```
135
+
136
+ Object generation (Zod)
137
+
138
+ ```js
139
+ import { generateObject } from 'ai';
140
+ import { z } from 'zod';
141
+ import { codexCli } from 'ai-sdk-provider-codex-cli';
142
+
143
+ const schema = z.object({ name: z.string(), age: z.number().int() });
144
+ const { object } = await generateObject({
145
+ model: codexCli('gpt-5', { allowNpx: true, skipGitRepoCheck: true }),
146
+ schema,
147
+ prompt: 'Generate a small user profile.',
148
+ });
149
+ console.log(object);
150
+ ```
151
+
152
+ ## Features
153
+
154
+ - AI SDK v5 compatible (LanguageModelV2)
155
+ - Streaming and non‑streaming
156
+ - JSON object generation with Zod schemas (prompt‑engineered)
157
+ - Safe defaults for non‑interactive automation (`on-failure`, `workspace-write`, `--skip-git-repo-check`)
158
+ - Fallback to `npx @openai/codex` when not on PATH (`allowNpx`)
159
+
160
+ ### Streaming behavior
161
+
162
+ When using `codex exec --json`, the Codex CLI intentionally suppresses token/assistant deltas in its JSON event stream. Instead, it writes the final assistant message to the file you pass via `--output-last-message` and then signals completion. This provider:
163
+
164
+ - emits `response-metadata` early (as soon as the session is configured), and
165
+ - returns the final text as a single `text-delta` right before `finish`.
166
+
167
+ This is expected behavior for JSON mode in Codex exec, so streaming typically “feels” like a final chunk rather than a gradual trickle.
168
+
169
+ ## Documentation
170
+
171
+ - Getting started, configuration, and troubleshooting live in `docs/` (v4 docs coming soon in this branch).
172
+ - See [examples/](examples/) for runnable scripts covering core usage, streaming, permissions/sandboxing, and object generation.
173
+
174
+ ## Authentication
175
+
176
+ - Preferred: ChatGPT OAuth via `codex login` (stores tokens at `~/.codex/auth.json`)
177
+ - Alternative: export `OPENAI_API_KEY` in the provider’s `env` settings (forwarded to the spawned process)
178
+
179
+ ## Configuration (high level)
180
+
181
+ - `allowNpx`: If true, falls back to `npx -y @openai/codex` when Codex is not on PATH
182
+ - `cwd`: Working directory for Codex
183
+ - Autonomy/sandbox:
184
+ - `fullAuto` (equivalent to `--full-auto`)
185
+ - `dangerouslyBypassApprovalsAndSandbox` (bypass approvals and sandbox; dangerous)
186
+ - Otherwise the provider writes `-c approval_policy=...` and `-c sandbox_mode=...` for you; defaults to `on-failure` and `workspace-write`
187
+ - `skipGitRepoCheck`: enable by default for CI/non‑repo contexts
188
+ - `color`: `always` | `never` | `auto`
189
+ - `outputLastMessageFile`: by default the provider sets a temp path and reads it to capture final text reliably
190
+
191
+ See [docs/ai-sdk-v5/configuration.md](docs/ai-sdk-v5/configuration.md) for the full list and examples.
192
+
193
+ ## Zod Compatibility
194
+
195
+ - Peer supports `zod@^3`
196
+ - Validation logic normalizes zod v3/v4 error shapes
197
+
198
+ ## Limitations
199
+
200
+ - Node ≥ 18, local process only (no Edge)
201
+ - Codex JSON mode (`codex exec --json`) suppresses mid‑response deltas; streaming typically yields a final chunk. The CLI writes the final assistant text via `--output-last-message`, which this provider reads and emits at the end.
202
+ - Some AI SDK parameters are unsupported by Codex CLI (e.g., temperature/topP/penalties); the provider surfaces warnings and ignores them
203
+
204
+ ## Disclaimer
205
+
206
+ This is a community provider and not an official OpenAI or Vercel product. You are responsible for complying with all applicable terms and ensuring safe usage.
207
+
208
+ ## License
209
+
210
+ MIT