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/dist/e2e.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { decideTurn } from "./jev.js";
|
|
2
|
+
import { loadPolicy } from "./policy.js";
|
|
3
|
+
import { formatUsage, summarizeUsage, turnCost } from "./usage.js";
|
|
4
|
+
import { dirname } from "node:path";
|
|
5
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
6
|
+
export const PROGRAMMING_CASES = [
|
|
7
|
+
{
|
|
8
|
+
id: "ask-basic",
|
|
9
|
+
category: "Ask",
|
|
10
|
+
expectedLane: "flash",
|
|
11
|
+
prompt: "what does package.json do in a Node project?",
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
id: "repo-command",
|
|
15
|
+
category: "Ops",
|
|
16
|
+
expectedLane: "flash",
|
|
17
|
+
prompt: "how do I run the unit tests in this repo?",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: "small-rename",
|
|
21
|
+
category: "Localized edit",
|
|
22
|
+
expectedLane: "flash",
|
|
23
|
+
prompt: "rename getUser to fetchUser in src/api.ts",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: "small-ui",
|
|
27
|
+
category: "Localized edit",
|
|
28
|
+
expectedLane: "craft",
|
|
29
|
+
prompt: "add a loading spinner to the submit button in LoginForm.tsx",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "react-explain",
|
|
33
|
+
category: "Ask",
|
|
34
|
+
expectedLane: "flash",
|
|
35
|
+
prompt: "explain what useMemo is for in this React component",
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: "hook-extract",
|
|
39
|
+
category: "Implementation",
|
|
40
|
+
expectedLane: "craft",
|
|
41
|
+
prompt: "extract the fetch in page.tsx into a reusable hook",
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: "timezone-test",
|
|
45
|
+
category: "Debug",
|
|
46
|
+
expectedLane: "craft",
|
|
47
|
+
prompt: "the test formatDate.test.ts is failing on timezone, fix the assertion",
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: "typescript-error",
|
|
51
|
+
category: "Debug",
|
|
52
|
+
expectedLane: "craft",
|
|
53
|
+
prompt: "this TypeScript error: Type X is not assignable to type Y on the props",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: "suite-leak",
|
|
57
|
+
category: "Debug",
|
|
58
|
+
expectedLane: "forge",
|
|
59
|
+
prompt: "the auth test fails only when run with the full suite, find the leak",
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: "auth-redesign",
|
|
63
|
+
category: "Architecture",
|
|
64
|
+
expectedLane: "forge",
|
|
65
|
+
prompt: "redesign authentication across the Next.js app and the API gateway",
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: "websocket-race",
|
|
69
|
+
category: "Hard debug",
|
|
70
|
+
expectedLane: "forge",
|
|
71
|
+
prompt: "debug a race in websocket reconnect that duplicates subscriptions",
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: "prod-migration",
|
|
75
|
+
category: "High stakes",
|
|
76
|
+
expectedLane: "forge",
|
|
77
|
+
prompt: "ship a production database migration that changes user permissions and preserves existing sessions",
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
function percent(n) {
|
|
81
|
+
return `${n.toFixed(1)}%`;
|
|
82
|
+
}
|
|
83
|
+
function parseOutputFlag(argv, name) {
|
|
84
|
+
const prefix = `${name}=`;
|
|
85
|
+
const inline = argv.find((arg) => arg.startsWith(prefix));
|
|
86
|
+
if (inline)
|
|
87
|
+
return inline.slice(prefix.length);
|
|
88
|
+
const index = argv.indexOf(name);
|
|
89
|
+
if (index >= 0)
|
|
90
|
+
return argv[index + 1] || null;
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
function ensureParent(path) {
|
|
94
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
95
|
+
}
|
|
96
|
+
function rowFromDecision(policy, testCase, decision) {
|
|
97
|
+
const forge = turnCost(policy, policy.lanes.forge.model, policy.lanes.forge.effort);
|
|
98
|
+
const actual = turnCost(policy, decision.model, decision.effort);
|
|
99
|
+
return {
|
|
100
|
+
...testCase,
|
|
101
|
+
actualLane: decision.lane,
|
|
102
|
+
model: decision.model,
|
|
103
|
+
effort: decision.effort,
|
|
104
|
+
degraded: decision.degraded,
|
|
105
|
+
jevMs: decision.jev_ms,
|
|
106
|
+
promptChars: decision.prompt_chars,
|
|
107
|
+
expectedMatch: decision.lane === testCase.expectedLane,
|
|
108
|
+
baselineUsage: forge,
|
|
109
|
+
actualUsage: actual,
|
|
110
|
+
savedPct: forge > 0 ? ((forge - actual) / forge) * 100 : 0,
|
|
111
|
+
reason: decision.reason.join("; "),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function formatMarkdownReport(policy, rows, generatedAt = new Date()) {
|
|
115
|
+
const summary = summarizeUsage(policy, rows.map((row) => ({
|
|
116
|
+
lane: row.actualLane,
|
|
117
|
+
model: row.model,
|
|
118
|
+
effort: row.effort,
|
|
119
|
+
degraded: row.degraded,
|
|
120
|
+
})));
|
|
121
|
+
const matches = rows.filter((row) => row.expectedMatch).length;
|
|
122
|
+
const passRate = rows.length ? (matches / rows.length) * 100 : 0;
|
|
123
|
+
const liveRows = rows.filter((row) => !row.degraded);
|
|
124
|
+
const avgJevMs = liveRows.length ? liveRows.reduce((acc, row) => acc + row.jevMs, 0) / liveRows.length : 0;
|
|
125
|
+
const degradedNote = summary.degraded
|
|
126
|
+
? `\n\n> ${summary.degraded} degraded cases used the fallback lane and should not be used as savings proof.`
|
|
127
|
+
: "";
|
|
128
|
+
const table = rows
|
|
129
|
+
.map((row) => `| ${[
|
|
130
|
+
row.id,
|
|
131
|
+
row.category,
|
|
132
|
+
row.expectedLane,
|
|
133
|
+
row.actualLane,
|
|
134
|
+
row.model,
|
|
135
|
+
row.effort || "",
|
|
136
|
+
row.expectedMatch ? "yes" : "no",
|
|
137
|
+
row.degraded ? "yes" : "no",
|
|
138
|
+
row.jevMs,
|
|
139
|
+
row.actualUsage.toFixed(2),
|
|
140
|
+
percent(row.savedPct),
|
|
141
|
+
row.prompt.replace(/\|/g, "\\|"),
|
|
142
|
+
].join(" | ")} |`)
|
|
143
|
+
.join("\n");
|
|
144
|
+
return `# Codex Sift E2E Routing Report
|
|
145
|
+
|
|
146
|
+
Generated: ${generatedAt.toISOString()}
|
|
147
|
+
|
|
148
|
+
## Summary
|
|
149
|
+
|
|
150
|
+
- Cases: ${rows.length}
|
|
151
|
+
- Live Jev cases: ${summary.liveTurns}
|
|
152
|
+
- Degraded cases: ${summary.degraded}
|
|
153
|
+
- Policy expectation match: ${matches}/${rows.length} (${percent(passRate)})
|
|
154
|
+
- Average Jev latency: ${avgJevMs.toFixed(0)} ms
|
|
155
|
+
- Baseline: every live turn on ${summary.forgeModel}
|
|
156
|
+
- Usage: ${summary.alwaysForge.toFixed(2)} -> ${summary.actual.toFixed(2)}
|
|
157
|
+
- Estimated quota saved: ${percent(summary.savedPct)}
|
|
158
|
+
|
|
159
|
+
${degradedNote}
|
|
160
|
+
|
|
161
|
+
## Lane Mix
|
|
162
|
+
|
|
163
|
+
${Object.entries(summary.mix)
|
|
164
|
+
.sort((a, b) => b[1] - a[1])
|
|
165
|
+
.map(([lane, count]) => `- ${lane}: ${count}`)
|
|
166
|
+
.join("\n")}
|
|
167
|
+
|
|
168
|
+
## Case Results
|
|
169
|
+
|
|
170
|
+
| ID | Category | Policy expected | Actual | Model | Effort | Policy match | Degraded | Jev ms | Usage | Saved vs forge | Prompt |
|
|
171
|
+
| --- | --- | --- | --- | --- | --- | --- | --- | ---: | ---: | ---: | --- |
|
|
172
|
+
${table}
|
|
173
|
+
|
|
174
|
+
## Interpretation
|
|
175
|
+
|
|
176
|
+
Sift is compared against an always-forge baseline. This report measures routing and quota efficiency, not answer quality. The policy expectation column validates whether each representative prompt landed on the lane this repository expects. A case is counted as savings evidence only when Jev answered live; degraded fallback cases are listed for reliability tracking but excluded from the savings calculation.
|
|
177
|
+
`;
|
|
178
|
+
}
|
|
179
|
+
export function formatConsoleReport(policy, rows) {
|
|
180
|
+
const lines = ["E2E programming session (Jev only — Codex quota is not used)", ""];
|
|
181
|
+
for (const row of rows) {
|
|
182
|
+
const status = row.expectedMatch ? "match" : "check";
|
|
183
|
+
const tag = row.degraded ? " degraded" : "";
|
|
184
|
+
lines.push(`${row.actualLane.padEnd(6)} ${row.model.padEnd(14)} ${String(row.effort || "-").padEnd(7)} ${percent(row.savedPct).padEnd(10)} ${status.padEnd(5)} ${row.id}${tag}`);
|
|
185
|
+
}
|
|
186
|
+
lines.push("", formatUsage(summarizeUsage(policy, rows.map((row) => ({
|
|
187
|
+
lane: row.actualLane,
|
|
188
|
+
model: row.model,
|
|
189
|
+
effort: row.effort,
|
|
190
|
+
degraded: row.degraded,
|
|
191
|
+
})))));
|
|
192
|
+
return lines.join("\n");
|
|
193
|
+
}
|
|
194
|
+
export async function runE2E(argv = []) {
|
|
195
|
+
const policy = loadPolicy();
|
|
196
|
+
if (!process.env.TYPESAFE_API_KEY) {
|
|
197
|
+
process.stderr.write("codex-sift e2e needs TYPESAFE_API_KEY. Export it in this terminal and retry.\n");
|
|
198
|
+
return 1;
|
|
199
|
+
}
|
|
200
|
+
const rows = [];
|
|
201
|
+
for (const testCase of PROGRAMMING_CASES) {
|
|
202
|
+
const decision = await decideTurn(policy, testCase.prompt);
|
|
203
|
+
rows.push(rowFromDecision(policy, testCase, decision));
|
|
204
|
+
}
|
|
205
|
+
process.stdout.write(`${formatConsoleReport(policy, rows)}\n`);
|
|
206
|
+
const jsonOut = parseOutputFlag(argv, "--json-out");
|
|
207
|
+
if (jsonOut) {
|
|
208
|
+
ensureParent(jsonOut);
|
|
209
|
+
const summary = summarizeUsage(policy, rows.map((row) => ({
|
|
210
|
+
lane: row.actualLane,
|
|
211
|
+
model: row.model,
|
|
212
|
+
effort: row.effort,
|
|
213
|
+
degraded: row.degraded,
|
|
214
|
+
})));
|
|
215
|
+
writeFileSync(jsonOut, `${JSON.stringify({ generatedAt: new Date().toISOString(), summary, rows }, null, 2)}\n`);
|
|
216
|
+
process.stdout.write(`wrote ${jsonOut}\n`);
|
|
217
|
+
}
|
|
218
|
+
const markdownOut = parseOutputFlag(argv, "--out");
|
|
219
|
+
if (markdownOut) {
|
|
220
|
+
ensureParent(markdownOut);
|
|
221
|
+
writeFileSync(markdownOut, formatMarkdownReport(policy, rows));
|
|
222
|
+
process.stdout.write(`wrote ${markdownOut}\n`);
|
|
223
|
+
}
|
|
224
|
+
return rows.some((row) => row.degraded) ? 1 : 0;
|
|
225
|
+
}
|
package/dist/env.d.ts
ADDED
package/dist/env.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { siftHome } from "./policy.js";
|
|
4
|
+
export function envFilePath() {
|
|
5
|
+
return join(siftHome(), "env");
|
|
6
|
+
}
|
|
7
|
+
export function loadLocalEnv() {
|
|
8
|
+
const path = envFilePath();
|
|
9
|
+
if (!existsSync(path))
|
|
10
|
+
return;
|
|
11
|
+
const text = readFileSync(path, "utf8");
|
|
12
|
+
for (const raw of text.split("\n")) {
|
|
13
|
+
const line = raw.trim();
|
|
14
|
+
if (!line || line.startsWith("#"))
|
|
15
|
+
continue;
|
|
16
|
+
const eq = line.indexOf("=");
|
|
17
|
+
if (eq <= 0)
|
|
18
|
+
continue;
|
|
19
|
+
const key = line.slice(0, eq).trim();
|
|
20
|
+
let value = line.slice(eq + 1).trim();
|
|
21
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
22
|
+
value = value.slice(1, -1);
|
|
23
|
+
}
|
|
24
|
+
if (!process.env[key])
|
|
25
|
+
process.env[key] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function writeLocalEnvKey(apiKey) {
|
|
29
|
+
mkdirSync(siftHome(), { recursive: true });
|
|
30
|
+
const path = envFilePath();
|
|
31
|
+
const existing = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
32
|
+
const lines = existing
|
|
33
|
+
.split("\n")
|
|
34
|
+
.filter((line) => !line.startsWith("TYPESAFE_API_KEY="));
|
|
35
|
+
while (lines.length && lines[lines.length - 1] === "")
|
|
36
|
+
lines.pop();
|
|
37
|
+
lines.push(`TYPESAFE_API_KEY=${apiKey.trim()}`);
|
|
38
|
+
writeFileSync(path, `${lines.join("\n")}\n`, { mode: 0o600 });
|
|
39
|
+
chmodSync(path, 0o600);
|
|
40
|
+
process.env.TYPESAFE_API_KEY = apiKey.trim();
|
|
41
|
+
return path;
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runExplain(): number;
|
package/dist/explain.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { loadPolicy } from "./policy.js";
|
|
2
|
+
import { readLast } from "./log.js";
|
|
3
|
+
export function runExplain() {
|
|
4
|
+
const last = readLast();
|
|
5
|
+
if (!last) {
|
|
6
|
+
process.stdout.write("No Sift decision yet. Run `codex-sift` and send a prompt first.\n");
|
|
7
|
+
return 1;
|
|
8
|
+
}
|
|
9
|
+
const policy = loadPolicy();
|
|
10
|
+
const lines = [
|
|
11
|
+
`at: ${last.at}`,
|
|
12
|
+
`lane: ${last.lane} -> ${last.model}${last.effort ? ` (effort ${last.effort})` : ""}`,
|
|
13
|
+
`degraded: ${last.degraded ? "yes" : "no"}`,
|
|
14
|
+
`jev_ms: ${last.jev_ms}`,
|
|
15
|
+
`reason: ${last.reason.join("; ")}`,
|
|
16
|
+
`task_kind: ${last.signals.task_kind} (confidence ${last.signals.task_kind_confidence.toFixed(2)})`,
|
|
17
|
+
`difficulty: ${last.signals.difficulty.toFixed(2)} (confidence ${last.signals.difficulty_confidence.toFixed(2)})`,
|
|
18
|
+
`confidence used: ${last.signals.confidence.toFixed(2)}`,
|
|
19
|
+
`is_chitchat: ${last.signals.is_chitchat.toFixed(2)}`,
|
|
20
|
+
`needs_reasoning: ${last.signals.needs_reasoning.toFixed(2)}`,
|
|
21
|
+
`needs_planning: ${last.signals.needs_planning.toFixed(2)}`,
|
|
22
|
+
`high_stakes: ${last.signals.high_stakes.toFixed(2)}`,
|
|
23
|
+
`cross_cutting: ${last.signals.cross_cutting.toFixed(2)}`,
|
|
24
|
+
`prompt: ${last.prompt_preview}`,
|
|
25
|
+
`lanes: ${Object.entries(policy.lanes).map(([k, v]) => `${k}=${v.model}`).join(", ")}`,
|
|
26
|
+
];
|
|
27
|
+
process.stdout.write(`${lines.join("\n")}\n`);
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
package/dist/expr.d.ts
ADDED
package/dist/expr.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
const OPS = ["==", "!=", ">=", "<=", ">", "<"];
|
|
2
|
+
function tokenize(input) {
|
|
3
|
+
const tokens = [];
|
|
4
|
+
let i = 0;
|
|
5
|
+
const s = input.trim();
|
|
6
|
+
while (i < s.length) {
|
|
7
|
+
const c = s[i];
|
|
8
|
+
if (/\s/.test(c)) {
|
|
9
|
+
i++;
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
if (c === "(") {
|
|
13
|
+
tokens.push({ kind: "lparen" });
|
|
14
|
+
i++;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (c === ")") {
|
|
18
|
+
tokens.push({ kind: "rparen" });
|
|
19
|
+
i++;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (c === "[") {
|
|
23
|
+
tokens.push({ kind: "lbrack" });
|
|
24
|
+
i++;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (c === "]") {
|
|
28
|
+
tokens.push({ kind: "rbrack" });
|
|
29
|
+
i++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (c === ",") {
|
|
33
|
+
tokens.push({ kind: "comma" });
|
|
34
|
+
i++;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const two = s.slice(i, i + 2);
|
|
38
|
+
if (OPS.includes(two)) {
|
|
39
|
+
tokens.push({ kind: "op", value: two });
|
|
40
|
+
i += 2;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (OPS.includes(c)) {
|
|
44
|
+
tokens.push({ kind: "op", value: c });
|
|
45
|
+
i++;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (c === '"' || c === "'") {
|
|
49
|
+
const q = c;
|
|
50
|
+
i++;
|
|
51
|
+
let value = "";
|
|
52
|
+
while (i < s.length && s[i] !== q) {
|
|
53
|
+
value += s[i++];
|
|
54
|
+
}
|
|
55
|
+
i++;
|
|
56
|
+
tokens.push({ kind: "str", value });
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (/[0-9.]/.test(c)) {
|
|
60
|
+
const start = i;
|
|
61
|
+
while (i < s.length && /[0-9.]/.test(s[i]))
|
|
62
|
+
i++;
|
|
63
|
+
tokens.push({ kind: "num", value: Number(s.slice(start, i)) });
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (/[A-Za-z_]/.test(c)) {
|
|
67
|
+
const start = i;
|
|
68
|
+
while (i < s.length && /[A-Za-z0-9_]/.test(s[i]))
|
|
69
|
+
i++;
|
|
70
|
+
tokens.push({ kind: "id", value: s.slice(start, i) });
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
throw new Error(`Unexpected character '${c}' in expression: ${input}`);
|
|
74
|
+
}
|
|
75
|
+
tokens.push({ kind: "eof" });
|
|
76
|
+
return tokens;
|
|
77
|
+
}
|
|
78
|
+
class Parser {
|
|
79
|
+
tokens;
|
|
80
|
+
values;
|
|
81
|
+
constructor(tokens, values) {
|
|
82
|
+
this.tokens = tokens;
|
|
83
|
+
this.values = values;
|
|
84
|
+
}
|
|
85
|
+
i = 0;
|
|
86
|
+
peek() {
|
|
87
|
+
return this.tokens[this.i];
|
|
88
|
+
}
|
|
89
|
+
eat() {
|
|
90
|
+
return this.tokens[this.i++];
|
|
91
|
+
}
|
|
92
|
+
parse() {
|
|
93
|
+
const value = this.parseOr();
|
|
94
|
+
if (this.peek().kind !== "eof") {
|
|
95
|
+
throw new Error("Trailing tokens in expression");
|
|
96
|
+
}
|
|
97
|
+
return Boolean(value);
|
|
98
|
+
}
|
|
99
|
+
parseOr() {
|
|
100
|
+
let left = this.parseAnd();
|
|
101
|
+
while (this.peek().kind === "id" && this.peek().value === "or") {
|
|
102
|
+
this.eat();
|
|
103
|
+
const right = this.parseAnd();
|
|
104
|
+
left = left || right;
|
|
105
|
+
}
|
|
106
|
+
return left;
|
|
107
|
+
}
|
|
108
|
+
parseAnd() {
|
|
109
|
+
let left = this.parseCmp();
|
|
110
|
+
while (this.peek().kind === "id" && this.peek().value === "and") {
|
|
111
|
+
this.eat();
|
|
112
|
+
const right = this.parseCmp();
|
|
113
|
+
left = left && right;
|
|
114
|
+
}
|
|
115
|
+
return left;
|
|
116
|
+
}
|
|
117
|
+
parseCmp() {
|
|
118
|
+
if (this.peek().kind === "lparen") {
|
|
119
|
+
this.eat();
|
|
120
|
+
const inner = this.parseOr();
|
|
121
|
+
if (this.peek().kind !== "rparen")
|
|
122
|
+
throw new Error("Missing )");
|
|
123
|
+
this.eat();
|
|
124
|
+
return inner;
|
|
125
|
+
}
|
|
126
|
+
const first = this.eat();
|
|
127
|
+
if (first.kind === "id" && first.value === "true")
|
|
128
|
+
return true;
|
|
129
|
+
if (first.kind === "id" && first.value === "false")
|
|
130
|
+
return false;
|
|
131
|
+
if (first.kind !== "id")
|
|
132
|
+
throw new Error("Expected identifier");
|
|
133
|
+
const next = this.peek();
|
|
134
|
+
if (next.kind === "id" && next.value === "in") {
|
|
135
|
+
this.eat();
|
|
136
|
+
const list = this.parseList();
|
|
137
|
+
return list.includes(String(this.lookup(first.value)));
|
|
138
|
+
}
|
|
139
|
+
if (next.kind === "op") {
|
|
140
|
+
const op = this.eat();
|
|
141
|
+
const rightTok = this.eat();
|
|
142
|
+
const left = this.lookup(first.value);
|
|
143
|
+
const right = this.literal(rightTok);
|
|
144
|
+
return compare(left, op.value, right);
|
|
145
|
+
}
|
|
146
|
+
return Boolean(this.lookup(first.value));
|
|
147
|
+
}
|
|
148
|
+
parseList() {
|
|
149
|
+
if (this.peek().kind !== "lbrack")
|
|
150
|
+
throw new Error("Expected [list]");
|
|
151
|
+
this.eat();
|
|
152
|
+
const items = [];
|
|
153
|
+
while (this.peek().kind !== "rbrack" && this.peek().kind !== "eof") {
|
|
154
|
+
const tok = this.eat();
|
|
155
|
+
if (tok.kind === "id" || tok.kind === "str")
|
|
156
|
+
items.push(tok.value);
|
|
157
|
+
else if (tok.kind === "num")
|
|
158
|
+
items.push(String(tok.value));
|
|
159
|
+
if (this.peek().kind === "comma")
|
|
160
|
+
this.eat();
|
|
161
|
+
}
|
|
162
|
+
if (this.peek().kind !== "rbrack")
|
|
163
|
+
throw new Error("Missing ]");
|
|
164
|
+
this.eat();
|
|
165
|
+
return items;
|
|
166
|
+
}
|
|
167
|
+
lookup(name) {
|
|
168
|
+
if (!(name in this.values))
|
|
169
|
+
throw new Error(`Unknown signal '${name}'`);
|
|
170
|
+
return this.values[name];
|
|
171
|
+
}
|
|
172
|
+
literal(tok) {
|
|
173
|
+
if (tok.kind === "num")
|
|
174
|
+
return tok.value;
|
|
175
|
+
if (tok.kind === "str")
|
|
176
|
+
return tok.value;
|
|
177
|
+
if (tok.kind === "id") {
|
|
178
|
+
if (tok.value === "true")
|
|
179
|
+
return true;
|
|
180
|
+
if (tok.value === "false")
|
|
181
|
+
return false;
|
|
182
|
+
if (tok.value in this.values)
|
|
183
|
+
return this.values[tok.value];
|
|
184
|
+
return tok.value;
|
|
185
|
+
}
|
|
186
|
+
throw new Error("Expected literal");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function compare(left, op, right) {
|
|
190
|
+
if (op === "==")
|
|
191
|
+
return left === right;
|
|
192
|
+
if (op === "!=")
|
|
193
|
+
return left !== right;
|
|
194
|
+
const l = Number(left);
|
|
195
|
+
const r = Number(right);
|
|
196
|
+
if (op === ">")
|
|
197
|
+
return l > r;
|
|
198
|
+
if (op === ">=")
|
|
199
|
+
return l >= r;
|
|
200
|
+
if (op === "<")
|
|
201
|
+
return l < r;
|
|
202
|
+
if (op === "<=")
|
|
203
|
+
return l <= r;
|
|
204
|
+
throw new Error(`Unknown operator ${op}`);
|
|
205
|
+
}
|
|
206
|
+
export function evalWhen(expr, values) {
|
|
207
|
+
if (typeof expr === "boolean")
|
|
208
|
+
return expr;
|
|
209
|
+
return new Parser(tokenize(String(expr)), values).parse();
|
|
210
|
+
}
|
package/dist/jev.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Decision, Policy, Signals } from "./types.js";
|
|
2
|
+
export declare class JevUnavailable extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
type JevAnswer = {
|
|
6
|
+
type?: string;
|
|
7
|
+
choice?: string;
|
|
8
|
+
score?: number;
|
|
9
|
+
noul?: number;
|
|
10
|
+
confidence?: number;
|
|
11
|
+
};
|
|
12
|
+
type JevResponse = {
|
|
13
|
+
model?: string;
|
|
14
|
+
answers?: Record<string, JevAnswer>;
|
|
15
|
+
};
|
|
16
|
+
export declare function answersToSignals(answers: Record<string, JevAnswer>, hasImage: boolean): Signals;
|
|
17
|
+
export declare function callJev(state: unknown, policy: Policy): Promise<JevResponse>;
|
|
18
|
+
export declare function decideTurn(policy: Policy, prompt: string, extras?: {
|
|
19
|
+
hasImage?: boolean;
|
|
20
|
+
retry?: boolean;
|
|
21
|
+
previousLane?: string;
|
|
22
|
+
}): Promise<Decision>;
|
|
23
|
+
export {};
|