codex-sift 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +252 -2
- package/dist/catalog.d.ts +19 -0
- package/dist/catalog.js +68 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +108 -0
- package/dist/demo.d.ts +2 -0
- package/dist/demo.js +33 -0
- package/dist/doctor.d.ts +1 -0
- package/dist/doctor.js +49 -0
- package/dist/e2e.d.ts +24 -0
- package/dist/e2e.js +225 -0
- package/dist/env.d.ts +3 -0
- package/dist/env.js +42 -0
- package/dist/explain.d.ts +1 -0
- package/dist/explain.js +29 -0
- package/dist/expr.d.ts +2 -0
- package/dist/expr.js +210 -0
- package/dist/jev.d.ts +23 -0
- package/dist/jev.js +156 -0
- package/dist/launch.d.ts +4 -0
- package/dist/launch.js +155 -0
- package/dist/log.d.ts +10 -0
- package/dist/log.js +33 -0
- package/dist/menu.d.ts +15 -0
- package/dist/menu.js +42 -0
- package/dist/policy.d.ts +20 -0
- package/dist/policy.js +179 -0
- package/dist/proxy.d.ts +7 -0
- package/dist/proxy.js +132 -0
- package/dist/redact.d.ts +1 -0
- package/dist/redact.js +21 -0
- package/dist/report.d.ts +1 -0
- package/dist/report.js +38 -0
- package/dist/setup.d.ts +10 -0
- package/dist/setup.js +125 -0
- package/dist/turns.d.ts +15 -0
- package/dist/turns.js +137 -0
- package/dist/types.d.ts +56 -0
- package/dist/types.js +2 -0
- package/dist/upstream.d.ts +16 -0
- package/dist/upstream.js +120 -0
- package/dist/usage.d.ts +21 -0
- package/dist/usage.js +62 -0
- package/package.json +59 -4
- package/policy.yaml +76 -0
- package/scripts/statusline.js +13 -0
- package/skills/sift-explain/SKILL.md +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 codex-sift contributors
|
|
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
CHANGED
|
@@ -1,3 +1,253 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Codex Sift
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Free, open-source model routing inside Codex CLI.**
|
|
4
|
+
|
|
5
|
+
Save your strongest model for the turns that need it.
|
|
6
|
+
|
|
7
|
+
[Install](#install) · [Demo](#routing-demo) · [Results](#how-much-usage-can-codex-sift-save) · [Commands](#commands) · [FAQ](https://codex-sift.vercel.app/#faq) · [Star history](#star-history)
|
|
8
|
+
|
|
9
|
+
[](https://github.com/romanmeclazcke/codex-sift/actions/workflows/test.yml)
|
|
10
|
+
[](LICENSE)
|
|
11
|
+
[](https://www.npmjs.com/package/codex-sift)
|
|
12
|
+
[](https://github.com/romanmeclazcke/codex-sift/stargazers)
|
|
13
|
+
|
|
14
|
+
Sift is a model router for the [OpenAI Codex CLI](https://developers.openai.com/codex). It keeps the native Codex experience, but routes each user turn to the cheapest model lane that should be able to handle it.
|
|
15
|
+
|
|
16
|
+
## How much usage can Codex Sift save?
|
|
17
|
+
|
|
18
|
+
**Codex Sift used 48.8% less estimated Codex quota** in a November 26, 2026 routing test on 12 representative requests, compared with sending every request to the `forge` lane (`gpt-5.6-sol`, high reasoning effort). The routing policy estimates quota from model and reasoning-effort weights. The estimate is specific to this workload and policy. [Full routing report and per-request results](https://github.com/romanmeclazcke/codex-sift/blob/main/reports/e2e-routing.md)
|
|
19
|
+
|
|
20
|
+

|
|
21
|
+
|
|
22
|
+
In a separate live Codex CLI test on three short coding questions, the models selected by Sift used **8.7% fewer input tokens** than always using `forge` (54,458 vs. 59,654). Both strategies answered **3/3** correctly. Sift took 17.7 seconds in total and the fixed model took 16.0 seconds. Each strategy ran once, and the input counts include cached tokens. [Benchmark methodology and comparison](https://codex-sift.vercel.app/benchmark) · [Raw CLI measurements](https://github.com/romanmeclazcke/codex-sift/blob/main/reports/model-comparison.json)
|
|
23
|
+
|
|
24
|
+
To reproduce the routing test with your TypeSafe API key, run `npm ci` and `npm run e2e:report` from the repository.
|
|
25
|
+
|
|
26
|
+
The problem is simple: coding sessions mix tiny asks, everyday implementation, and genuinely hard work. Running every turn on the strongest model is easy, but it spends premium quota on prompts like “explain this file”, “run the tests”, or “rename this helper”. Sift adds a small decision layer before Codex spends the turn.
|
|
27
|
+
|
|
28
|
+
On every **user turn**, Sift asks [TypeSafe Jev](https://docs.typesafe.ai) how hard the task is. Your YAML policy then picks a lane — **flash**, **craft**, or **forge** — and Codex runs against that model. You still talk to the native TUI.
|
|
29
|
+
|
|
30
|
+
Jev does not write code. It returns typed signals in about 300 ms. Sift’s YAML policy turns those signals into a model id.
|
|
31
|
+
|
|
32
|
+
This is a Codex-only router. It is not an MCP server, and it does not patch `~/.codex/config.toml`.
|
|
33
|
+
|
|
34
|
+
To get started, run `npx codex-sift setup` with Node.js 20+, the Codex CLI, and a TypeSafe API key. Then run `npx codex-sift` to launch Codex with routing enabled.
|
|
35
|
+
|
|
36
|
+
[Reduce Codex CLI token usage](https://codex-sift.vercel.app/guides/en) · [Cómo reducir el consumo de Codex CLI](https://codex-sift.vercel.app/guides/es) · [Routing benchmark](https://codex-sift.vercel.app/benchmark) · [Manual model switching vs Codex Sift](https://codex-sift.vercel.app/compare)
|
|
37
|
+
|
|
38
|
+
## Why this shape
|
|
39
|
+
|
|
40
|
+
Codex has no hook that can change the model *before* a turn is billed. The supported interception point is a custom `model_provider`. Sift starts a loopback Responses proxy, launches Codex with process-local `-c` overrides, and forwards your existing ChatGPT or API-key login upstream.
|
|
41
|
+
|
|
42
|
+
```text
|
|
43
|
+
you → codex-sift → Codex TUI
|
|
44
|
+
│
|
|
45
|
+
POST /responses model=sift
|
|
46
|
+
│
|
|
47
|
+
Sift proxy (127.0.0.1)
|
|
48
|
+
│ Jev + policy.yaml
|
|
49
|
+
└─► chatgpt.com/backend-api/codex or api.openai.com
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
If you pin another model in `/model`, routing pauses. Pick **Sift** again to resume.
|
|
53
|
+
|
|
54
|
+
## Routing demo
|
|
55
|
+
|
|
56
|
+

|
|
57
|
+
|
|
58
|
+
| Example | Lane | Why |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| Explain package.json | `flash` | Short lookup |
|
|
61
|
+
| Add a loading state to the form | `craft` | Bounded implementation |
|
|
62
|
+
| Plan a production migration | `forge` | High-stakes change |
|
|
63
|
+
|
|
64
|
+
[Try the interactive demo](https://codex-sift.vercel.app/)
|
|
65
|
+
|
|
66
|
+
## Install
|
|
67
|
+
|
|
68
|
+
Global (recommended):
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
npm install -g codex-sift
|
|
72
|
+
codex-sift setup
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Or without a global install:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npx codex-sift setup
|
|
79
|
+
npx codex-sift
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
From this repo (development):
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
cd codex-sift
|
|
86
|
+
npm install
|
|
87
|
+
npm run build
|
|
88
|
+
npm link
|
|
89
|
+
codex-sift setup
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`setup` lists your Codex models as `1, 2, 3`. You pick one for **flash**, **craft**, and **forge**. It can also save `TYPESAFE_API_KEY` to `~/.codex-sift/env` (mode 600) so you do not have to export it every session.
|
|
93
|
+
|
|
94
|
+
## Onboarding
|
|
95
|
+
|
|
96
|
+
The onboarding flow has three steps.
|
|
97
|
+
|
|
98
|
+
First, choose the concrete Codex model behind each lane:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
codex-sift setup
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The default mental model is:
|
|
105
|
+
|
|
106
|
+
| Lane | Use it for |
|
|
107
|
+
|---|---|
|
|
108
|
+
| `flash` | Questions, small ops, tiny edits |
|
|
109
|
+
| `craft` | Normal implementation and bounded debugging |
|
|
110
|
+
| `forge` | Hard diagnosis, architecture, auth, migrations, production-risk work |
|
|
111
|
+
|
|
112
|
+
Second, sanity-check your environment:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
codex-sift doctor
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
This confirms Codex is available, your auth mode is detected, and the TypeSafe key is present for live Jev routing.
|
|
119
|
+
|
|
120
|
+
Third, start Codex through Sift from any project:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
cd /path/to/your-project
|
|
124
|
+
codex-sift
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`codex-sift` takes the same arguments as `codex`. Sessions, tools, sandbox, `/resume`, and permissions stay native.
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
codex-sift exec "explain src/cli.ts"
|
|
131
|
+
codex-sift resume --last
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
After any routed turn, inspect the latest decision:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
codex-sift explain
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Or, from inside Codex, use `$sift-explain`.
|
|
141
|
+
|
|
142
|
+
## Commands
|
|
143
|
+
|
|
144
|
+
| Command | Purpose |
|
|
145
|
+
|---|---|
|
|
146
|
+
| `codex-sift setup` | Onboarding: pick lane models by number |
|
|
147
|
+
| `codex-sift models` | Print the numbered Codex catalog |
|
|
148
|
+
| `codex-sift` | Interactive Codex with routing |
|
|
149
|
+
| `codex-sift route "…"` | Decide a lane; do not start Codex |
|
|
150
|
+
| `codex-sift explain` | Print the last decision |
|
|
151
|
+
| `codex-sift report` | Quota saved vs always-forge (`--live`, `--since=today`, `--json`) |
|
|
152
|
+
| `codex-sift demo` | Classify a sample coding day with Jev; no Codex usage |
|
|
153
|
+
| `codex-sift doctor` | Codex binary, auth, Jev key |
|
|
154
|
+
| `codex-sift init` | Copy `policy.yaml` to `~/.codex-sift/` |
|
|
155
|
+
| `--sift-off` | Bypass the proxy for one invocation |
|
|
156
|
+
|
|
157
|
+
Inside Codex, `$sift-explain` (skill installed on launch) reads `~/.codex-sift/last.json`.
|
|
158
|
+
|
|
159
|
+
## Show the savings
|
|
160
|
+
|
|
161
|
+
Codex with a ChatGPT subscription bills **quota**, not API invoices. Sift’s claim is: the same coding day uses fewer forge-tier turns.
|
|
162
|
+
|
|
163
|
+
1. Work normally through `codex-sift` for a session.
|
|
164
|
+
2. In another terminal:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
codex-sift report --live --since=today
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
That prints lane mix and “usage vs always-Sol”. Degraded turns (Jev down) are excluded so the number is not inflated.
|
|
171
|
+
|
|
172
|
+
To show the idea without burning Codex quota, Jev-classify a canned day:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
codex-sift demo
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
For a publishable E2E report with per-case tables:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
npm run e2e:report
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The E2E battery calls Jev for representative coding-agent prompts, compares each route against an always-forge baseline, and writes both human-readable Markdown and machine-readable JSON. It measures routing and quota efficiency; it does not claim response quality unless you manually run the same prompts through Codex and score the answers.
|
|
185
|
+
|
|
186
|
+
Weights live in `policy.yaml` as `usage_weight` × `effort_weight`. Tune them against `/usage` after a week of real sessions.
|
|
187
|
+
|
|
188
|
+
## Lanes
|
|
189
|
+
|
|
190
|
+
Edit the lanes in `~/.codex-sift/policy.yaml`, or run `codex-sift setup` again and type `1`, `2`, `3`. First matching rule wins.
|
|
191
|
+
|
|
192
|
+
| Lane | Default model | Use |
|
|
193
|
+
|---|---|---|
|
|
194
|
+
| `flash` | `gpt-5.6-luna` | Lookups, ops, tiny edits |
|
|
195
|
+
| `craft` | `gpt-5.6-terra` | Everyday implementation |
|
|
196
|
+
| `forge` | `gpt-5.6-sol` | Hard, high-stakes, or uncertain work |
|
|
197
|
+
|
|
198
|
+
Jev is asked atomic questions (`task_kind`, `difficulty`, `needs_reasoning`, `needs_planning`, `high_stakes`, `cross_cutting`). The policy never asks Jev “which model?”.
|
|
199
|
+
|
|
200
|
+
If Jev times out or `TYPESAFE_API_KEY` is missing, Sift fail-opens to `on_jev_down` (default `craft`) and marks the decision `degraded`.
|
|
201
|
+
|
|
202
|
+
## What is not routed
|
|
203
|
+
|
|
204
|
+
- Tool-loop continuations of the same user turn (the chosen model stays pinned)
|
|
205
|
+
- `codex-auto-review` and compact/review prompts
|
|
206
|
+
- Codex housekeeping that is not a `/responses` call
|
|
207
|
+
- Invocations with `-m` / `--model` pointing at a real model
|
|
208
|
+
|
|
209
|
+
## Auth
|
|
210
|
+
|
|
211
|
+
Sift does not read your tokens to call OpenAI. Codex sends `Authorization` and, on ChatGPT login, `chatgpt-account-id`. The proxy forwards those headers:
|
|
212
|
+
|
|
213
|
+
- ChatGPT / OAuth → `https://chatgpt.com/backend-api/codex/responses`
|
|
214
|
+
- API key → `https://api.openai.com/v1/responses`
|
|
215
|
+
|
|
216
|
+
A ChatGPT token is not valid on the platform API. Do not point this proxy at `api.openai.com` while logged in with ChatGPT.
|
|
217
|
+
|
|
218
|
+
## Status line (optional)
|
|
219
|
+
|
|
220
|
+
```toml
|
|
221
|
+
# ~/.codex/config.toml
|
|
222
|
+
statusLine = "node /absolute/path/to/codex-sift/scripts/statusline.js"
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Configuration
|
|
226
|
+
|
|
227
|
+
| Variable | Meaning |
|
|
228
|
+
|---|---|
|
|
229
|
+
| `TYPESAFE_API_KEY` | Jev credentials |
|
|
230
|
+
| `TYPESAFE_BASE_URL` | Override, default `https://api.typesafe.ai` |
|
|
231
|
+
| `SIFT_POLICY` | Extra YAML overlay |
|
|
232
|
+
| `SIFT_HOME` | Default `~/.codex-sift` |
|
|
233
|
+
| `SIFT_DISABLED=1` | Disable routing |
|
|
234
|
+
| `CODEX_BIN` | Codex executable if it is not on `PATH` |
|
|
235
|
+
| `SIFT_CHATGPT_ORIGIN` / `SIFT_OPENAI_ORIGIN` | Test upstreams |
|
|
236
|
+
|
|
237
|
+
Pin `jev_model` in YAML (`jev-1.13.0`). Do not ship thresholds against `jev-latest`.
|
|
238
|
+
|
|
239
|
+
## Development
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
npm test
|
|
243
|
+
npm run build
|
|
244
|
+
npx tsx src/cli.ts route "rename the unused helper"
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT
|
|
250
|
+
|
|
251
|
+
## Star history
|
|
252
|
+
|
|
253
|
+
[](https://www.star-history.com/#romanmeclazcke/codex-sift&Date)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Policy } from "./types.js";
|
|
2
|
+
type ModelInfo = Record<string, unknown> & {
|
|
3
|
+
slug?: string;
|
|
4
|
+
display_name?: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
visibility?: string;
|
|
7
|
+
priority?: number;
|
|
8
|
+
};
|
|
9
|
+
type Catalog = {
|
|
10
|
+
models: ModelInfo[];
|
|
11
|
+
[k: string]: unknown;
|
|
12
|
+
};
|
|
13
|
+
export declare function injectVirtualModel(catalog: Catalog, policy: Policy): Catalog;
|
|
14
|
+
export declare function injectIntoUnknown(payload: unknown, policy: Policy): unknown;
|
|
15
|
+
export declare function loadBundledCatalog(codexBin: string): Catalog;
|
|
16
|
+
export declare function cachedCatalogPath(codexVersion: string): string;
|
|
17
|
+
export declare function ensureInjectedCatalog(codexBin: string, policy: Policy): string;
|
|
18
|
+
export declare function readCatalog(path: string): Catalog;
|
|
19
|
+
export {};
|
package/dist/catalog.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { siftHome } from "./policy.js";
|
|
5
|
+
function parseJsonBlob(text) {
|
|
6
|
+
const start = text.indexOf("{");
|
|
7
|
+
if (start < 0)
|
|
8
|
+
throw new Error("No JSON object in catalog output");
|
|
9
|
+
return JSON.parse(text.slice(start));
|
|
10
|
+
}
|
|
11
|
+
export function injectVirtualModel(catalog, policy) {
|
|
12
|
+
const models = Array.isArray(catalog.models) ? catalog.models.map((m) => ({ ...m })) : [];
|
|
13
|
+
const slug = policy.virtual_model.slug;
|
|
14
|
+
const existing = models.findIndex((m) => m.slug === slug);
|
|
15
|
+
const template = models.find((m) => m.slug === "gpt-5.6-terra") ||
|
|
16
|
+
models.find((m) => m.visibility === "list") ||
|
|
17
|
+
models[0];
|
|
18
|
+
if (!template)
|
|
19
|
+
return { ...catalog, models };
|
|
20
|
+
const entry = {
|
|
21
|
+
...template,
|
|
22
|
+
slug,
|
|
23
|
+
display_name: policy.virtual_model.display_name,
|
|
24
|
+
description: policy.virtual_model.description,
|
|
25
|
+
visibility: "list",
|
|
26
|
+
supported_in_api: true,
|
|
27
|
+
priority: 0,
|
|
28
|
+
};
|
|
29
|
+
if (existing >= 0)
|
|
30
|
+
models[existing] = entry;
|
|
31
|
+
else
|
|
32
|
+
models.unshift(entry);
|
|
33
|
+
return { ...catalog, models };
|
|
34
|
+
}
|
|
35
|
+
export function injectIntoUnknown(payload, policy) {
|
|
36
|
+
if (!payload || typeof payload !== "object")
|
|
37
|
+
return payload;
|
|
38
|
+
const rec = payload;
|
|
39
|
+
if (Array.isArray(rec.models))
|
|
40
|
+
return injectVirtualModel(rec, policy);
|
|
41
|
+
return payload;
|
|
42
|
+
}
|
|
43
|
+
export function loadBundledCatalog(codexBin) {
|
|
44
|
+
const result = spawnSync(codexBin, ["debug", "models", "--bundled"], {
|
|
45
|
+
encoding: "utf8",
|
|
46
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
47
|
+
});
|
|
48
|
+
if (result.status !== 0) {
|
|
49
|
+
throw new Error(result.stderr || "codex debug models --bundled failed");
|
|
50
|
+
}
|
|
51
|
+
return parseJsonBlob(result.stdout || "");
|
|
52
|
+
}
|
|
53
|
+
export function cachedCatalogPath(codexVersion) {
|
|
54
|
+
return join(siftHome(), `catalog-${codexVersion.replace(/[^\w.-]+/g, "_")}.json`);
|
|
55
|
+
}
|
|
56
|
+
export function ensureInjectedCatalog(codexBin, policy) {
|
|
57
|
+
mkdirSync(siftHome(), { recursive: true });
|
|
58
|
+
const version = spawnSync(codexBin, ["--version"], { encoding: "utf8" }).stdout.trim() || "unknown";
|
|
59
|
+
const path = cachedCatalogPath(version);
|
|
60
|
+
if (existsSync(path))
|
|
61
|
+
return path;
|
|
62
|
+
const injected = injectVirtualModel(loadBundledCatalog(codexBin), policy);
|
|
63
|
+
writeFileSync(path, JSON.stringify(injected));
|
|
64
|
+
return path;
|
|
65
|
+
}
|
|
66
|
+
export function readCatalog(path) {
|
|
67
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
68
|
+
}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { copyFileSync, mkdirSync, existsSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { runDemo } from "./demo.js";
|
|
6
|
+
import { runDoctor } from "./doctor.js";
|
|
7
|
+
import { loadLocalEnv } from "./env.js";
|
|
8
|
+
import { runE2E } from "./e2e.js";
|
|
9
|
+
import { runExplain } from "./explain.js";
|
|
10
|
+
import { decideTurn } from "./jev.js";
|
|
11
|
+
import { launchCodex } from "./launch.js";
|
|
12
|
+
import { writeDecision } from "./log.js";
|
|
13
|
+
import { bundledPolicyPath, loadPolicy, siftHome } from "./policy.js";
|
|
14
|
+
import { runReport } from "./report.js";
|
|
15
|
+
import { runModels, runSetup } from "./setup.js";
|
|
16
|
+
const HELP = `codex-sift — route each Codex turn to the cheapest model that can handle it
|
|
17
|
+
|
|
18
|
+
Usage:
|
|
19
|
+
codex-sift [codex args...] Launch Codex with Sift routing
|
|
20
|
+
codex-sift setup Pick flash/craft/forge models by number
|
|
21
|
+
codex-sift models List Codex models as 1, 2, 3
|
|
22
|
+
codex-sift route "prompt" Decide a lane without calling Codex
|
|
23
|
+
codex-sift explain Show the last routing decision
|
|
24
|
+
codex-sift report Lane mix and estimated Codex quota saved
|
|
25
|
+
codex-sift demo Classify a sample coding day (Jev only)
|
|
26
|
+
codex-sift e2e Programming-prompt battery vs always-forge
|
|
27
|
+
codex-sift doctor Check Codex, auth, and Jev
|
|
28
|
+
codex-sift init Copy bundled policy.yaml to ~/.codex-sift/
|
|
29
|
+
|
|
30
|
+
Environment:
|
|
31
|
+
TYPESAFE_API_KEY Required for live Jev judgments
|
|
32
|
+
SIFT_POLICY Extra policy YAML path
|
|
33
|
+
SIFT_HOME Default ~/.codex-sift
|
|
34
|
+
SIFT_DISABLED=1 Bypass routing
|
|
35
|
+
CODEX_BIN Codex executable
|
|
36
|
+
|
|
37
|
+
Flags:
|
|
38
|
+
--sift-off Forward this invocation to Codex unchanged
|
|
39
|
+
--sift-lane=forge Skip Jev and pin a lane for this invocation
|
|
40
|
+
`;
|
|
41
|
+
async function main(argv) {
|
|
42
|
+
loadLocalEnv();
|
|
43
|
+
const [cmd, ...rest] = argv;
|
|
44
|
+
if (cmd === "-h" || cmd === "--help" || cmd === "help") {
|
|
45
|
+
process.stdout.write(HELP);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
if (!cmd)
|
|
49
|
+
return launchCodex([]);
|
|
50
|
+
if (cmd === "-v" || cmd === "--version" || cmd === "version") {
|
|
51
|
+
const { version } = createRequire(import.meta.url)("../package.json");
|
|
52
|
+
process.stdout.write(`codex-sift ${version}\n`);
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
if (cmd === "doctor")
|
|
56
|
+
return runDoctor();
|
|
57
|
+
if (cmd === "explain")
|
|
58
|
+
return runExplain();
|
|
59
|
+
if (cmd === "report")
|
|
60
|
+
return runReport(rest);
|
|
61
|
+
if (cmd === "demo")
|
|
62
|
+
return runDemo();
|
|
63
|
+
if (cmd === "e2e")
|
|
64
|
+
return runE2E(rest);
|
|
65
|
+
if (cmd === "init")
|
|
66
|
+
return runInit();
|
|
67
|
+
if (cmd === "setup")
|
|
68
|
+
return runSetup(rest);
|
|
69
|
+
if (cmd === "models")
|
|
70
|
+
return runModels();
|
|
71
|
+
if (cmd === "route")
|
|
72
|
+
return runRoute(rest);
|
|
73
|
+
return launchCodex(argv);
|
|
74
|
+
}
|
|
75
|
+
async function runRoute(args) {
|
|
76
|
+
const jsonMode = args.includes("--json");
|
|
77
|
+
const prompt = args.filter((a) => a !== "--json").join(" ").trim();
|
|
78
|
+
if (!prompt) {
|
|
79
|
+
process.stderr.write("usage: codex-sift route [--json] <prompt>\n");
|
|
80
|
+
return 1;
|
|
81
|
+
}
|
|
82
|
+
const policy = loadPolicy();
|
|
83
|
+
const decision = await decideTurn(policy, prompt);
|
|
84
|
+
writeDecision(decision, prompt);
|
|
85
|
+
if (jsonMode) {
|
|
86
|
+
process.stdout.write(`${JSON.stringify(decision, null, 2)}\n`);
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
process.stdout.write(`${decision.lane} -> ${decision.model}${decision.effort ? ` (${decision.effort})` : ""}${decision.degraded ? " [degraded]" : ""}\n${decision.reason.join("; ")}\n`);
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
function runInit() {
|
|
93
|
+
mkdirSync(siftHome(), { recursive: true });
|
|
94
|
+
const dest = join(siftHome(), "policy.yaml");
|
|
95
|
+
if (existsSync(dest)) {
|
|
96
|
+
process.stdout.write(`already exists: ${dest}\n`);
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
copyFileSync(bundledPolicyPath(), dest);
|
|
100
|
+
process.stdout.write(`wrote ${dest}\n`);
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
main(process.argv.slice(2))
|
|
104
|
+
.then((code) => process.exit(code))
|
|
105
|
+
.catch((err) => {
|
|
106
|
+
process.stderr.write(`${err instanceof Error ? err.stack || err.message : err}\n`);
|
|
107
|
+
process.exit(1);
|
|
108
|
+
});
|
package/dist/demo.d.ts
ADDED
package/dist/demo.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { decideTurn } from "./jev.js";
|
|
2
|
+
import { loadPolicy } from "./policy.js";
|
|
3
|
+
import { formatUsage, summarizeUsage } from "./usage.js";
|
|
4
|
+
export const DEMO_PROMPTS = [
|
|
5
|
+
"hola",
|
|
6
|
+
"what does this repository do?",
|
|
7
|
+
"rename the unused helper in utils.ts",
|
|
8
|
+
"git status and commit the typo fix",
|
|
9
|
+
"explain the function that builds the nav",
|
|
10
|
+
"the test in auth.test.ts is failing, find why",
|
|
11
|
+
"add a loading spinner to the submit button",
|
|
12
|
+
"thanks",
|
|
13
|
+
"redesign auth across billing and the API gateway",
|
|
14
|
+
"stop the double fetch on the home page",
|
|
15
|
+
];
|
|
16
|
+
export async function runDemo() {
|
|
17
|
+
const policy = loadPolicy();
|
|
18
|
+
if (!process.env.TYPESAFE_API_KEY) {
|
|
19
|
+
process.stderr.write("codex-sift demo needs TYPESAFE_API_KEY so Jev can classify the sample day.\n");
|
|
20
|
+
return 1;
|
|
21
|
+
}
|
|
22
|
+
process.stdout.write("Sample coding day (Jev only — Codex is not called)\n\n");
|
|
23
|
+
const rows = [];
|
|
24
|
+
for (const prompt of DEMO_PROMPTS) {
|
|
25
|
+
const decision = await decideTurn(policy, prompt);
|
|
26
|
+
rows.push(decision);
|
|
27
|
+
const tag = decision.degraded ? " degraded" : "";
|
|
28
|
+
process.stdout.write(`${decision.lane.padEnd(6)} ${decision.model.padEnd(14)} ${String(decision.effort || "").padEnd(7)} ${prompt}${tag}\n`);
|
|
29
|
+
}
|
|
30
|
+
process.stdout.write(`\n${formatUsage(summarizeUsage(policy, rows))}\n`);
|
|
31
|
+
process.stdout.write("That is the pitch: same work, less forge quota, because cheap turns never touch Sol.\n");
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
package/dist/doctor.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runDoctor(): Promise<number>;
|
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { loadLocalEnv } from "./env.js";
|
|
4
|
+
import { resolveCodexBin } from "./launch.js";
|
|
5
|
+
import { loadPolicy, siftHome } from "./policy.js";
|
|
6
|
+
function maskExists(path) {
|
|
7
|
+
return existsSync(path);
|
|
8
|
+
}
|
|
9
|
+
function authMode() {
|
|
10
|
+
const authPath = join(process.env.HOME || ".", ".codex", "auth.json");
|
|
11
|
+
if (!existsSync(authPath))
|
|
12
|
+
return "missing";
|
|
13
|
+
try {
|
|
14
|
+
const raw = readFileSync(authPath, "utf8");
|
|
15
|
+
if (/chatgpt|tokens/i.test(raw) && !/"openai_api_key"\s*:\s*"[^"]+"/.test(raw))
|
|
16
|
+
return "chatgpt";
|
|
17
|
+
if (/openai_api_key/.test(raw))
|
|
18
|
+
return "api-key";
|
|
19
|
+
return "present";
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return "unreadable";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export async function runDoctor() {
|
|
26
|
+
loadLocalEnv();
|
|
27
|
+
const bin = resolveCodexBin();
|
|
28
|
+
const policy = loadPolicy();
|
|
29
|
+
const userPolicy = join(siftHome(), "policy.yaml");
|
|
30
|
+
const lines = [
|
|
31
|
+
`codex binary: ${bin}`,
|
|
32
|
+
`sift home: ${siftHome()}`,
|
|
33
|
+
`policy lanes: ${Object.keys(policy.lanes).join(", ")}`,
|
|
34
|
+
`flash: ${policy.lanes.flash?.model}`,
|
|
35
|
+
`craft: ${policy.lanes.craft?.model}`,
|
|
36
|
+
`forge: ${policy.lanes.forge?.model}`,
|
|
37
|
+
`virtual model: ${policy.virtual_model.slug}`,
|
|
38
|
+
`TYPESAFE_API_KEY: ${process.env.TYPESAFE_API_KEY ? "set" : "MISSING"}`,
|
|
39
|
+
`codex auth: ${authMode()}`,
|
|
40
|
+
`codex config: ${maskExists(join(process.env.HOME || ".", ".codex", "config.toml")) ? "found" : "missing"}`,
|
|
41
|
+
`user policy: ${existsSync(userPolicy) ? userPolicy : "bundled defaults (run codex-sift setup)"}`,
|
|
42
|
+
];
|
|
43
|
+
process.stdout.write(`${lines.join("\n")}\n`);
|
|
44
|
+
if (!process.env.TYPESAFE_API_KEY) {
|
|
45
|
+
process.stderr.write("Set TYPESAFE_API_KEY or run `codex-sift setup` to save it.\n");
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
package/dist/e2e.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Policy } from "./types.js";
|
|
2
|
+
export type E2ECase = {
|
|
3
|
+
id: string;
|
|
4
|
+
category: string;
|
|
5
|
+
expectedLane: string;
|
|
6
|
+
prompt: string;
|
|
7
|
+
};
|
|
8
|
+
export type E2ERow = E2ECase & {
|
|
9
|
+
actualLane: string;
|
|
10
|
+
model: string;
|
|
11
|
+
effort?: string;
|
|
12
|
+
degraded: boolean;
|
|
13
|
+
jevMs: number;
|
|
14
|
+
promptChars: number;
|
|
15
|
+
expectedMatch: boolean;
|
|
16
|
+
baselineUsage: number;
|
|
17
|
+
actualUsage: number;
|
|
18
|
+
savedPct: number;
|
|
19
|
+
reason: string;
|
|
20
|
+
};
|
|
21
|
+
export declare const PROGRAMMING_CASES: E2ECase[];
|
|
22
|
+
export declare function formatMarkdownReport(policy: Policy, rows: E2ERow[], generatedAt?: Date): string;
|
|
23
|
+
export declare function formatConsoleReport(policy: Policy, rows: E2ERow[]): string;
|
|
24
|
+
export declare function runE2E(argv?: string[]): Promise<number>;
|