typenative 0.5.0 → 0.6.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/.github/workflows/ci.yml +2 -2
- package/.github/workflows/npm-publish-github-packages.yml +2 -2
- package/CHANGELOG.md +339 -0
- package/CONTRIBUTION.md +2 -1
- package/README.md +16 -7
- package/bin/cli.js +1 -0
- package/bin/index.js +62 -3
- package/bin/main.js +1 -0
- package/bin/node/core.js +46 -0
- package/bin/node/events.js +154 -15
- package/bin/node/http.js +1265 -127
- package/bin/node/process.js +13 -0
- package/bin/node/punycode.js +2 -2
- package/bin/node/stream.js +56 -7
- package/bin/node/stream_web.js +2 -2
- package/bin/node/timers.js +4 -2
- package/bin/node/url.js +28 -3
- package/bin/parse-node.js +2 -2
- package/bin/regex_vm.js +2105 -0
- package/bin/transpiler.js +17970 -4419
- package/bin/typecheck-node.js +2 -1
- package/bin/version.js +1 -1
- package/conformance.md +57 -15
- package/examples/README.md +50 -28
- package/examples/angular-templates/README.md +141 -0
- package/examples/angular-templates/bench.mjs +173 -0
- package/examples/angular-templates/dom-type-globals.d.ts +21 -0
- package/examples/angular-templates/dom-type-stubs.ts +21 -0
- package/examples/angular-templates/main.ts +135 -0
- package/examples/angular-templates/parity.mjs +157 -0
- package/examples/angular-templates/run-node.mjs +69 -0
- package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
- package/examples/desktop-notes/README.md +11 -13
- package/examples/dupe-finder/README.md +7 -5
- package/examples/fibonacci/README.md +7 -7
- package/examples/hello-world/README.md +6 -6
- package/examples/json-server/README.md +22 -18
- package/examples/mandelbrot-png/README.md +11 -13
- package/examples/marked-cli/README.md +60 -44
- package/examples/marked-cli/main.ts +122 -4
- package/examples/openai-chat/README.md +148 -0
- package/examples/openai-chat/bench.mjs +175 -0
- package/examples/openai-chat/main.ts +488 -0
- package/examples/react-compiler/README.md +185 -0
- package/examples/react-compiler/bench.mjs +181 -0
- package/examples/react-compiler/main.ts +324 -0
- package/examples/react-compiler/parity.mjs +362 -0
- package/examples/react-compiler/run-node.mjs +90 -0
- package/examples/react-compiler/vendor/babel/LICENSE +22 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
- package/examples/react-compiler/vendor/charcodes/package.json +25 -0
- package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
- package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
- package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
- package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
- package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
- package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
- package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
- package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
- package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
- package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
- package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
- package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
- package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
- package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
- package/examples/react-compiler/vendor/zod/LICENSE +21 -0
- package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
- package/examples/react-compiler/vendor.mjs +688 -0
- package/examples/todo-cli/README.md +9 -9
- package/examples/yaml-cli/README.md +50 -34
- package/examples/yaml-cli/main.ts +236 -14
- package/package.json +8 -5
- package/performance/README.md +196 -0
- package/performance/any-dynamic.ts +21 -0
- package/performance/any-record-view.ts +20 -0
- package/performance/arr-hof.ts +21 -0
- package/performance/arr-push-index.ts +15 -0
- package/performance/arr-sort.ts +21 -0
- package/performance/async-await.ts +24 -0
- package/performance/class-dispatch.ts +52 -0
- package/performance/events-emit.ts +27 -0
- package/performance/exc-throw-rare.ts +25 -0
- package/performance/exc-try-catch.ts +26 -0
- package/performance/fn-closures.ts +29 -0
- package/performance/fn-recursion-tree.ts +31 -0
- package/performance/gen-for-of.ts +46 -0
- package/performance/json-roundtrip.ts +27 -0
- package/performance/map-delete.ts +24 -0
- package/performance/map-string-keys.ts +22 -0
- package/performance/num-arith-loop.ts +14 -0
- package/performance/num-bitwise.ts +21 -0
- package/performance/num-mod-int.ts +16 -0
- package/performance/num-to-string.ts +16 -0
- package/performance/obj-alloc.ts +32 -0
- package/performance/regex-exec.ts +29 -0
- package/performance/regex-site.ts +28 -0
- package/performance/set-ops.ts +20 -0
- package/performance/str-char-scan.ts +35 -0
- package/performance/str-concat-plus.ts +18 -0
- package/performance/str-concat.ts +24 -0
- package/performance/str-short-scan.ts +26 -0
- package/performance/str-split-join.ts +23 -0
- package/performance/str-template.ts +16 -0
- package/performance/union-switch.ts +57 -0
- package/tsparser/check.go +2 -1
- package/tsparser/main.go +1 -0
- package/types/typenative-npm.d.ts +3 -2
- package/types/typenative.d.ts +42 -12
- package/ROUND2-HANDOFF.md +0 -200
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// One-shot latency benchmark: Node.js vs TypeNative for the chat client.
|
|
2
|
+
//
|
|
3
|
+
// A chat request spends most of its time waiting on the model, so this bench
|
|
4
|
+
// measures what a client actually adds: process startup, opening the HTTPS
|
|
5
|
+
// connection, and parsing the SSE stream as it arrives. A local mock server
|
|
6
|
+
// (plain node:http) stands in for the API: it answers POST
|
|
7
|
+
// /v1/chat/completions with a deterministic event stream of ~45 chunks sent
|
|
8
|
+
// with pauses between them, exactly the shape OpenRouter and OpenAI return.
|
|
9
|
+
//
|
|
10
|
+
// First both runtimes answer the same question once and their stdout is
|
|
11
|
+
// compared byte for byte (parity). Then N full process runs (spawn to exit)
|
|
12
|
+
// of the one-shot client are timed against the mock.
|
|
13
|
+
//
|
|
14
|
+
// Usage: node bench.mjs [runs=30]
|
|
15
|
+
|
|
16
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
17
|
+
import http from 'node:http';
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { fileURLToPath } from 'node:url';
|
|
21
|
+
|
|
22
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
const repoRoot = path.resolve(here, '..', '..');
|
|
24
|
+
const cli = path.join(repoRoot, 'bin', 'cli.js');
|
|
25
|
+
const runs = Number(process.argv[2] ?? '30') || 30;
|
|
26
|
+
const exeName = process.platform === 'win32' ? 'openai-chat.exe' : 'openai-chat';
|
|
27
|
+
const exePath = path.join(here, 'dist', exeName);
|
|
28
|
+
|
|
29
|
+
// The deterministic answer the mock streams, one chunk per word
|
|
30
|
+
const words = [
|
|
31
|
+
'Server-sent', 'events', 'are', 'just', 'a', 'long', 'HTTP', 'response:', 'the', 'server',
|
|
32
|
+
'keeps', 'the', 'connection', 'open', 'and', 'writes', '"data:"', 'lines', 'separated', 'by',
|
|
33
|
+
'blank', 'lines.', 'A', 'chat', 'client', 'reads', 'each', 'event', 'as', 'it', 'arrives,',
|
|
34
|
+
'pulls', 'the', 'text', 'delta', 'out', 'of', 'its', 'JSON,', 'and', 'prints', 'it', 'immediately.',
|
|
35
|
+
];
|
|
36
|
+
const expectedAnswer = words.join(' ');
|
|
37
|
+
const expectedUsage = { prompt_tokens: 11, completion_tokens: words.length, total_tokens: 11 + words.length };
|
|
38
|
+
|
|
39
|
+
const chunkJson = (delta, extra = {}) =>
|
|
40
|
+
JSON.stringify({
|
|
41
|
+
id: 'chatcmpl-bench',
|
|
42
|
+
object: 'chat.completion.chunk',
|
|
43
|
+
created: 1700000000,
|
|
44
|
+
model: 'bench-model',
|
|
45
|
+
choices: [{ index: 0, delta, finish_reason: null }],
|
|
46
|
+
...extra,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function startMockServer() {
|
|
50
|
+
const server = http.createServer((req, res) => {
|
|
51
|
+
if (req.method !== 'POST' || !req.url.endsWith('/chat/completions')) {
|
|
52
|
+
res.writeHead(404, { 'content-type': 'application/json' });
|
|
53
|
+
res.end(JSON.stringify({ error: { message: 'not found' } }));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (req.headers.authorization !== 'Bearer bench-key') {
|
|
57
|
+
res.writeHead(401, { 'content-type': 'application/json' });
|
|
58
|
+
res.end(JSON.stringify({ error: { message: 'Invalid API key' } }));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
req.resume();
|
|
62
|
+
res.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
63
|
+
// The keep-alive comment real providers send while the model warms up
|
|
64
|
+
res.write(': OPENROUTER PROCESSING\n\n');
|
|
65
|
+
const events = [
|
|
66
|
+
`data: ${chunkJson({ role: 'assistant' })}\n\n`,
|
|
67
|
+
...words.map((word, i) => `data: ${chunkJson({ content: word + (i < words.length - 1 ? ' ' : '') })}\n\n`),
|
|
68
|
+
`data: ${chunkJson({}, { choices: [{ index: 0, delta: {}, finish_reason: 'stop' }], usage: expectedUsage })}\n\n`,
|
|
69
|
+
'data: [DONE]\n\n',
|
|
70
|
+
];
|
|
71
|
+
let i = 0;
|
|
72
|
+
const tick = () => {
|
|
73
|
+
if (i < events.length) {
|
|
74
|
+
res.write(events[i++]);
|
|
75
|
+
setTimeout(tick, 4);
|
|
76
|
+
} else {
|
|
77
|
+
res.end();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
tick();
|
|
81
|
+
});
|
|
82
|
+
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(server)));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function stats(samples) {
|
|
86
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
87
|
+
const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
|
|
88
|
+
return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function fmt(ms) {
|
|
92
|
+
return `${ms.toFixed(2)} ms`.padStart(11);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Async spawn on purpose: the mock API lives in this process's event loop,
|
|
96
|
+
// and spawnSync would block the loop so the server could never answer.
|
|
97
|
+
function oneShot(command, args) {
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
const child = spawn(command, args, {
|
|
100
|
+
cwd: here,
|
|
101
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
102
|
+
env: { ...process.env, OPENROUTER_API_KEY: '', OPENAI_API_KEY: '' },
|
|
103
|
+
});
|
|
104
|
+
let stdout = '';
|
|
105
|
+
let stderr = '';
|
|
106
|
+
child.stdout.on('data', (chunk) => {
|
|
107
|
+
stdout += chunk;
|
|
108
|
+
});
|
|
109
|
+
child.stderr.on('data', (chunk) => {
|
|
110
|
+
stderr += chunk;
|
|
111
|
+
});
|
|
112
|
+
child.on('exit', (code) => resolve({ status: code, stdout, stderr }));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 1. Compile the client to a native binary.
|
|
117
|
+
fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
|
|
118
|
+
console.log('Compiling main.ts to a native binary with TypeNative...');
|
|
119
|
+
const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
|
|
120
|
+
cwd: here,
|
|
121
|
+
stdio: 'inherit',
|
|
122
|
+
});
|
|
123
|
+
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
124
|
+
|
|
125
|
+
// 2. Start the mock API and check both runtimes produce identical output.
|
|
126
|
+
const server = await startMockServer();
|
|
127
|
+
const port = server.address().port;
|
|
128
|
+
const mockBase = `http://127.0.0.1:${port}/v1`;
|
|
129
|
+
const question = 'What are server-sent events?';
|
|
130
|
+
const args = ['--api-key', 'bench-key', '--base-url', mockBase, question];
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
console.log('\nChecking parity (same question, byte-for-byte stdout)...');
|
|
134
|
+
const nodeRun = await oneShot(process.execPath, ['main.ts', ...args]);
|
|
135
|
+
const nativeRun = await oneShot(exePath, args);
|
|
136
|
+
for (const [name, run] of [['Node.js', nodeRun], ['TypeNative', nativeRun]]) {
|
|
137
|
+
if (run.status !== 0) {
|
|
138
|
+
console.error(`${name} run failed (exit ${run.status}):\n${run.stderr}`);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
}
|
|
141
|
+
if (run.stdout !== `${expectedAnswer}\n`) {
|
|
142
|
+
console.error(`${name} stdout does not match the expected answer:\n"${run.stdout}"`);
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
console.log('parity: Node.js and TypeNative stdout identical, matches the streamed answer.');
|
|
147
|
+
|
|
148
|
+
// 3. Time full one-shot runs (spawn -> exit) under both runtimes.
|
|
149
|
+
console.log(`\nTiming ${runs} full one-shot runs of each runtime...\n`);
|
|
150
|
+
const timeRuns = async (command, argv) => {
|
|
151
|
+
const samples = [];
|
|
152
|
+
for (let i = 0; i < runs + 2; i++) {
|
|
153
|
+
const t0 = performance.now();
|
|
154
|
+
const result = await oneShot(command, argv);
|
|
155
|
+
const elapsed = performance.now() - t0;
|
|
156
|
+
if (result.status !== 0) {
|
|
157
|
+
console.error(`run failed: ${command} ${argv.join(' ')}\n${result.stderr}`);
|
|
158
|
+
process.exit(1);
|
|
159
|
+
}
|
|
160
|
+
if (i >= 2) samples.push(elapsed); // first two are warmup
|
|
161
|
+
}
|
|
162
|
+
return stats(samples);
|
|
163
|
+
};
|
|
164
|
+
const nodeStats = await timeRuns(process.execPath, ['main.ts', ...args]);
|
|
165
|
+
const nativeStats = await timeRuns(exePath, args);
|
|
166
|
+
|
|
167
|
+
console.log(`\`openai-chat "<question>"\` against a local SSE server, ${runs} runs (full process: spawn -> exit):\n`);
|
|
168
|
+
console.log('| Runtime | min | median | mean | max |');
|
|
169
|
+
console.log('|------------|-------------|-------------|-------------|-------------|');
|
|
170
|
+
console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |`);
|
|
171
|
+
console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |`);
|
|
172
|
+
console.log(`\nTypeNative is ${(nodeStats.median / nativeStats.median).toFixed(1)}x faster per answer (median).\n`);
|
|
173
|
+
} finally {
|
|
174
|
+
server.close();
|
|
175
|
+
}
|
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
// A chat client for any OpenAI-compatible API: answers stream into the
|
|
2
|
+
// terminal token by token while the model is still writing. Works with the
|
|
3
|
+
// free OpenRouter API out of the box, and with OpenAI or any other
|
|
4
|
+
// /v1/chat/completions server via --base-url.
|
|
5
|
+
//
|
|
6
|
+
// openai-chat interactive multi-turn REPL
|
|
7
|
+
// openai-chat "explain SSE in one line" one question, streamed answer
|
|
8
|
+
// openai-chat --model google/gemma-4-31b-it:free
|
|
9
|
+
// openai-chat --base-url https://api.openai.com/v1 --model gpt-4o-mini
|
|
10
|
+
//
|
|
11
|
+
// The API key comes from OPENROUTER_API_KEY or OPENAI_API_KEY (free keys at
|
|
12
|
+
// openrouter.ai/keys), or pass --api-key. In the REPL: /exit quits, /clear
|
|
13
|
+
// resets the conversation, /system <text> sets the system prompt, and Ctrl+C
|
|
14
|
+
// aborts the answer in flight without killing the session.
|
|
15
|
+
//
|
|
16
|
+
// The interesting part is the stream: fetch() POSTs with stream: true and the
|
|
17
|
+
// response body — a server-sent-event stream — is parsed line by line as
|
|
18
|
+
// chunks arrive, printing each content delta the moment the server sends it.
|
|
19
|
+
import { createInterface } from 'node:readline/promises';
|
|
20
|
+
|
|
21
|
+
interface ChatMessage {
|
|
22
|
+
role: string;
|
|
23
|
+
content: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Delta {
|
|
27
|
+
role?: string;
|
|
28
|
+
content?: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface StreamChoice {
|
|
32
|
+
delta?: Delta;
|
|
33
|
+
finish_reason?: string | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface TokenUsage {
|
|
37
|
+
prompt_tokens?: number;
|
|
38
|
+
completion_tokens?: number;
|
|
39
|
+
total_tokens?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface StreamChunk {
|
|
43
|
+
choices?: StreamChoice[];
|
|
44
|
+
usage?: TokenUsage | null;
|
|
45
|
+
error?: { message?: string } | string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface Config {
|
|
49
|
+
apiKey: string;
|
|
50
|
+
baseUrl: string;
|
|
51
|
+
model: string;
|
|
52
|
+
system: string;
|
|
53
|
+
maxTokens: number | null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const OPENROUTER_BASE = 'https://openrouter.ai/api/v1';
|
|
57
|
+
const OPENAI_BASE = 'https://api.openai.com/v1';
|
|
58
|
+
const OPENROUTER_MODEL = 'google/gemma-4-31b-it:free';
|
|
59
|
+
const OPENAI_MODEL = 'gpt-4o-mini';
|
|
60
|
+
const REQUEST_TIMEOUT_MS = 120000;
|
|
61
|
+
|
|
62
|
+
// Set while a response is streaming, so Ctrl+C aborts the request instead of
|
|
63
|
+
// killing the process; interrupted is read by whoever awaited the stream.
|
|
64
|
+
let activeRequest: AbortController | null = null;
|
|
65
|
+
let interrupted = false;
|
|
66
|
+
|
|
67
|
+
process.on('SIGINT', () => {
|
|
68
|
+
if (activeRequest !== null) {
|
|
69
|
+
interrupted = true;
|
|
70
|
+
activeRequest.abort();
|
|
71
|
+
} else {
|
|
72
|
+
process.exit(130);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
function usage(): void {
|
|
77
|
+
console.log(`usage: openai-chat [options] [question...]
|
|
78
|
+
|
|
79
|
+
Asks an OpenAI-compatible chat API and streams the answer. With a question
|
|
80
|
+
the client runs once and exits; without one it starts an interactive
|
|
81
|
+
multi-turn REPL (/exit quits, /clear resets, /system <text> sets the system
|
|
82
|
+
prompt).
|
|
83
|
+
|
|
84
|
+
Options:
|
|
85
|
+
--model <id> model id, provider default when omitted
|
|
86
|
+
--base-url <url> API root, default https://openrouter.ai/api/v1
|
|
87
|
+
(or https://api.openai.com/v1 for OpenAI keys)
|
|
88
|
+
--api-key <key> API key; otherwise OPENROUTER_API_KEY or
|
|
89
|
+
OPENAI_API_KEY from the environment
|
|
90
|
+
--system <prompt> system prompt for the conversation
|
|
91
|
+
--max-tokens <n> cap on generated tokens
|
|
92
|
+
-h, --help show this help`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface ParsedArgs {
|
|
96
|
+
help: boolean;
|
|
97
|
+
apiKey: string;
|
|
98
|
+
baseUrl: string;
|
|
99
|
+
model: string;
|
|
100
|
+
system: string;
|
|
101
|
+
maxTokens: number | null;
|
|
102
|
+
positional: string[];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function parseArgs(argv: string[]): ParsedArgs | null {
|
|
106
|
+
const parsed: ParsedArgs = {
|
|
107
|
+
help: false,
|
|
108
|
+
apiKey: '',
|
|
109
|
+
baseUrl: '',
|
|
110
|
+
model: '',
|
|
111
|
+
system: '',
|
|
112
|
+
maxTokens: null,
|
|
113
|
+
positional: [],
|
|
114
|
+
};
|
|
115
|
+
const valueFlags = ['--api-key', '--base-url', '--model', '--system', '--max-tokens'];
|
|
116
|
+
let i = 0;
|
|
117
|
+
while (i < argv.length) {
|
|
118
|
+
const arg = argv[i];
|
|
119
|
+
if (!arg.startsWith('--')) {
|
|
120
|
+
parsed.positional.push(arg);
|
|
121
|
+
i += 1;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
let name = arg;
|
|
125
|
+
let value: string | undefined;
|
|
126
|
+
const eq = arg.indexOf('=');
|
|
127
|
+
if (eq >= 0) {
|
|
128
|
+
name = arg.slice(0, eq);
|
|
129
|
+
value = arg.slice(eq + 1);
|
|
130
|
+
}
|
|
131
|
+
if (name === '--help' || name === '-h') {
|
|
132
|
+
parsed.help = true;
|
|
133
|
+
i += 1;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (valueFlags.indexOf(name) < 0) {
|
|
137
|
+
console.error(`openai-chat: unknown option ${arg}`);
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
if (value === undefined) {
|
|
141
|
+
value = argv[i + 1];
|
|
142
|
+
i += 2;
|
|
143
|
+
} else {
|
|
144
|
+
i += 1;
|
|
145
|
+
}
|
|
146
|
+
if (value === undefined) {
|
|
147
|
+
console.error(`openai-chat: ${name} needs a value`);
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
if (name === '--api-key') parsed.apiKey = value;
|
|
151
|
+
else if (name === '--base-url') parsed.baseUrl = value;
|
|
152
|
+
else if (name === '--model') parsed.model = value;
|
|
153
|
+
else if (name === '--system') parsed.system = value;
|
|
154
|
+
else if (name === '--max-tokens') {
|
|
155
|
+
const n = Number(value);
|
|
156
|
+
if (Number.isNaN(n) || n <= 0 || Math.floor(n) !== n) {
|
|
157
|
+
console.error(`openai-chat: --max-tokens wants a positive whole number, got ${value}`);
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
parsed.maxTokens = n;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return parsed;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function resolveConfig(parsed: ParsedArgs): Config | null {
|
|
167
|
+
let apiKey = parsed.apiKey;
|
|
168
|
+
let baseUrl = parsed.baseUrl;
|
|
169
|
+
let model = parsed.model;
|
|
170
|
+
if (apiKey === '' && process.env.OPENROUTER_API_KEY) {
|
|
171
|
+
apiKey = process.env.OPENROUTER_API_KEY;
|
|
172
|
+
}
|
|
173
|
+
if (apiKey === '' && process.env.OPENAI_API_KEY) {
|
|
174
|
+
apiKey = process.env.OPENAI_API_KEY;
|
|
175
|
+
}
|
|
176
|
+
if (apiKey === '') {
|
|
177
|
+
console.error(`openai-chat: no API key found.
|
|
178
|
+
Get a free one at https://openrouter.ai/keys and set it with
|
|
179
|
+
|
|
180
|
+
export OPENROUTER_API_KEY=... (sh)
|
|
181
|
+
set OPENROUTER_API_KEY=... (cmd)
|
|
182
|
+
$env:OPENROUTER_API_KEY = "..." (PowerShell)
|
|
183
|
+
|
|
184
|
+
or pass --api-key. OPENAI_API_KEY works too, as does --base-url for any
|
|
185
|
+
other OpenAI-compatible server.`);
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
// An explicit --base-url decides the provider; otherwise an OpenRouter key
|
|
189
|
+
// (from env or flag) talks to OpenRouter, anything else to OpenAI.
|
|
190
|
+
if (baseUrl === '') {
|
|
191
|
+
baseUrl = baseUrlFor(parsed);
|
|
192
|
+
}
|
|
193
|
+
if (model === '') {
|
|
194
|
+
model = baseUrl.indexOf('openrouter.ai') >= 0 ? OPENROUTER_MODEL : OPENAI_MODEL;
|
|
195
|
+
}
|
|
196
|
+
if (baseUrl.endsWith('/')) baseUrl = baseUrl.slice(0, -1);
|
|
197
|
+
return { apiKey, baseUrl, model, system: parsed.system, maxTokens: parsed.maxTokens };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function baseUrlFor(parsed: ParsedArgs): string {
|
|
201
|
+
const envBase = process.env.OPENAI_BASE_URL;
|
|
202
|
+
if (envBase) return envBase;
|
|
203
|
+
if (parsed.apiKey !== '') return OPENROUTER_BASE;
|
|
204
|
+
return process.env.OPENROUTER_API_KEY ? OPENROUTER_BASE : OPENAI_BASE;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// POSTs the conversation with stream: true and feeds every content delta to
|
|
208
|
+
// onDelta as it arrives. Resolves with the usage block from the final chunk
|
|
209
|
+
// (providers that honor stream_options.include_usage), or null.
|
|
210
|
+
async function streamChat(cfg: Config, messages: ChatMessage[], onDelta: (text: string) => void): Promise<TokenUsage | null> {
|
|
211
|
+
const controller = new AbortController();
|
|
212
|
+
activeRequest = controller;
|
|
213
|
+
const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
|
|
214
|
+
try {
|
|
215
|
+
const headers: Record<string, string> = {
|
|
216
|
+
'Authorization': `Bearer ${cfg.apiKey}`,
|
|
217
|
+
'Content-Type': 'application/json',
|
|
218
|
+
};
|
|
219
|
+
if (cfg.baseUrl.indexOf('openrouter.ai') >= 0) {
|
|
220
|
+
headers['X-Title'] = 'TypeNative openai-chat example';
|
|
221
|
+
}
|
|
222
|
+
const body: Record<string, unknown> = {
|
|
223
|
+
model: cfg.model,
|
|
224
|
+
messages,
|
|
225
|
+
stream: true,
|
|
226
|
+
stream_options: { include_usage: true },
|
|
227
|
+
};
|
|
228
|
+
if (cfg.maxTokens !== null) body.max_tokens = cfg.maxTokens;
|
|
229
|
+
const response = await fetch(`${cfg.baseUrl}/chat/completions`, {
|
|
230
|
+
method: 'POST',
|
|
231
|
+
headers,
|
|
232
|
+
body: JSON.stringify(body),
|
|
233
|
+
signal: controller.signal,
|
|
234
|
+
});
|
|
235
|
+
if (!response.ok) {
|
|
236
|
+
const text = await response.text();
|
|
237
|
+
throw new Error(describeHttpError(response.status, text));
|
|
238
|
+
}
|
|
239
|
+
if (response.body === null) {
|
|
240
|
+
throw new Error('the server returned a response with no body');
|
|
241
|
+
}
|
|
242
|
+
return await readEventStream(response.body, onDelta);
|
|
243
|
+
} finally {
|
|
244
|
+
activeRequest = null;
|
|
245
|
+
clearTimeout(timer);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function describeHttpError(status: number, body: string): string {
|
|
250
|
+
let detail = body.trim();
|
|
251
|
+
try {
|
|
252
|
+
const parsed = JSON.parse(body) as StreamChunk;
|
|
253
|
+
if (typeof parsed.error === 'string') detail = parsed.error;
|
|
254
|
+
else if (parsed.error && typeof parsed.error.message === 'string') detail = parsed.error.message;
|
|
255
|
+
} catch {
|
|
256
|
+
// not JSON — show the raw body
|
|
257
|
+
}
|
|
258
|
+
if (detail === '') return `HTTP ${status}`;
|
|
259
|
+
return `HTTP ${status}: ${detail}`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function readEventStream(body: ReadableStream<Uint8Array>, onDelta: (text: string) => void): Promise<TokenUsage | null> {
|
|
263
|
+
const reader = body.getReader();
|
|
264
|
+
const decoder = new TextDecoder();
|
|
265
|
+
let buffer = '';
|
|
266
|
+
let dataLines: string[] = [];
|
|
267
|
+
let finished = false;
|
|
268
|
+
let usage: TokenUsage | null = null;
|
|
269
|
+
|
|
270
|
+
// One SSE event is one or more data: lines ended by a blank line; [DONE]
|
|
271
|
+
// marks the end of the stream. Comments (": OPENROUTER PROCESSING"
|
|
272
|
+
// keep-alives) and event/id/retry fields are ignored; a frame that fails
|
|
273
|
+
// to parse is skipped rather than fatal.
|
|
274
|
+
const dispatch = (): void => {
|
|
275
|
+
if (dataLines.length === 0) return;
|
|
276
|
+
const data = dataLines.join('\n');
|
|
277
|
+
dataLines = [];
|
|
278
|
+
if (data === '[DONE]') {
|
|
279
|
+
finished = true;
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
let chunk: StreamChunk | null = null;
|
|
283
|
+
try {
|
|
284
|
+
chunk = JSON.parse(data) as StreamChunk;
|
|
285
|
+
} catch {
|
|
286
|
+
chunk = null; // malformed frame: skip it
|
|
287
|
+
}
|
|
288
|
+
if (chunk !== null) {
|
|
289
|
+
if (chunk.usage) usage = chunk.usage;
|
|
290
|
+
const content = chunk.choices?.[0]?.delta?.content;
|
|
291
|
+
if (typeof content === 'string' && content.length > 0) onDelta(content);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
while (!finished) {
|
|
296
|
+
const { value, done } = await reader.read();
|
|
297
|
+
if (done) break;
|
|
298
|
+
buffer += decoder.decode(value, { stream: true });
|
|
299
|
+
let newlineAt = buffer.indexOf('\n');
|
|
300
|
+
while (newlineAt >= 0 && !finished) {
|
|
301
|
+
const line = buffer.slice(0, newlineAt).replace(/\r$/, '');
|
|
302
|
+
buffer = buffer.slice(newlineAt + 1);
|
|
303
|
+
if (line.startsWith(':')) {
|
|
304
|
+
// SSE comment
|
|
305
|
+
} else if (line === '') {
|
|
306
|
+
dispatch();
|
|
307
|
+
} else if (line.startsWith('data:')) {
|
|
308
|
+
let payload = line.slice(5);
|
|
309
|
+
if (payload.startsWith(' ')) payload = payload.slice(1);
|
|
310
|
+
dataLines.push(payload);
|
|
311
|
+
}
|
|
312
|
+
newlineAt = buffer.indexOf('\n');
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (finished) {
|
|
316
|
+
reader.cancel();
|
|
317
|
+
}
|
|
318
|
+
buffer += decoder.decode();
|
|
319
|
+
if (buffer !== '') {
|
|
320
|
+
// Trailing bytes without a final blank line still form an event.
|
|
321
|
+
const tail = buffer.replace(/\r$/, '');
|
|
322
|
+
if (tail.startsWith('data:')) dataLines.push(tail.slice(5).replace(/^ /, ''));
|
|
323
|
+
dispatch();
|
|
324
|
+
}
|
|
325
|
+
return usage;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function messageOf(err: unknown): string {
|
|
329
|
+
if (err instanceof Error) return err.message;
|
|
330
|
+
return String(err);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function friendlyError(err: unknown, cfg: Config): string {
|
|
334
|
+
const message = messageOf(err);
|
|
335
|
+
if (message.indexOf('fetch failed') >= 0) {
|
|
336
|
+
return `could not reach ${cfg.baseUrl} — check the URL and your connection (${message})`;
|
|
337
|
+
}
|
|
338
|
+
if (message.indexOf('abort') >= 0 || message.indexOf('timeout') >= 0) {
|
|
339
|
+
return `the request timed out after ${Math.round(REQUEST_TIMEOUT_MS / 1000)}s (${message})`;
|
|
340
|
+
}
|
|
341
|
+
return message;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function statsLine(usage: TokenUsage | null, elapsedMs: number): string {
|
|
345
|
+
const seconds = (elapsedMs / 1000).toFixed(1);
|
|
346
|
+
if (usage && typeof usage.total_tokens === 'number') {
|
|
347
|
+
const parts: string[] = [];
|
|
348
|
+
if (typeof usage.prompt_tokens === 'number') parts.push(`${usage.prompt_tokens} prompt`);
|
|
349
|
+
if (typeof usage.completion_tokens === 'number') parts.push(`${usage.completion_tokens} completion`);
|
|
350
|
+
if (parts.length > 0) return `[${parts.join(' + ')} tokens · ${seconds}s]`;
|
|
351
|
+
return `[${usage.total_tokens} tokens · ${seconds}s]`;
|
|
352
|
+
}
|
|
353
|
+
return `[${seconds}s]`;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// One question in, streamed answer out. Returns the process exit code.
|
|
357
|
+
// Note: no return inside the catch — generated Go falls through to the code
|
|
358
|
+
// after the try/catch when a catch returns (see this example's README).
|
|
359
|
+
async function oneShot(cfg: Config, question: string): Promise<number> {
|
|
360
|
+
const messages: ChatMessage[] = [];
|
|
361
|
+
if (cfg.system !== '') messages.push({ role: 'system', content: cfg.system });
|
|
362
|
+
messages.push({ role: 'user', content: question });
|
|
363
|
+
const started = Date.now();
|
|
364
|
+
let usage: TokenUsage | null = null;
|
|
365
|
+
let code = 0;
|
|
366
|
+
let failure = '';
|
|
367
|
+
try {
|
|
368
|
+
usage = await streamChat(cfg, messages, (text) => process.stdout.write(text));
|
|
369
|
+
} catch (err) {
|
|
370
|
+
if (interrupted) {
|
|
371
|
+
code = 130;
|
|
372
|
+
} else {
|
|
373
|
+
code = 1;
|
|
374
|
+
failure = friendlyError(err, cfg);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
process.stdout.write('\n');
|
|
378
|
+
if (code === 1) console.error(`openai-chat: ${failure}`);
|
|
379
|
+
if (code === 0) console.error(statsLine(usage, Date.now() - started));
|
|
380
|
+
return code;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async function repl(cfg: Config): Promise<void> {
|
|
384
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
385
|
+
rl.setPrompt('you> ');
|
|
386
|
+
console.log(`openai-chat — ${cfg.model} on ${cfg.baseUrl}`);
|
|
387
|
+
console.log('Multi-turn: each message sees the whole conversation. /exit quits, /clear');
|
|
388
|
+
console.log('resets, /system <text> sets the system prompt. Ctrl+C aborts an answer.');
|
|
389
|
+
const history: ChatMessage[] = [];
|
|
390
|
+
if (cfg.system !== '') history.push({ role: 'system', content: cfg.system });
|
|
391
|
+
// Iterating the interface (rather than question()) queues lines that are
|
|
392
|
+
// typed while an answer is still streaming, so nothing is lost.
|
|
393
|
+
rl.prompt();
|
|
394
|
+
for await (const line of rl) {
|
|
395
|
+
const input = line.trim();
|
|
396
|
+
if (input === '') {
|
|
397
|
+
showPrompt(rl);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (input === '/exit' || input === '/quit') break;
|
|
401
|
+
if (input === '/clear') {
|
|
402
|
+
history.splice(0, history.length);
|
|
403
|
+
if (cfg.system !== '') history.push({ role: 'system', content: cfg.system });
|
|
404
|
+
console.log('conversation cleared.');
|
|
405
|
+
showPrompt(rl);
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (input === '/system' || input.startsWith('/system ')) {
|
|
409
|
+
const text = input.slice(7).trim();
|
|
410
|
+
if (text === '') {
|
|
411
|
+
console.error('usage: /system <prompt text>');
|
|
412
|
+
showPrompt(rl);
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
const existing = history.findIndex((m) => m.role === 'system');
|
|
416
|
+
if (existing >= 0) history[existing] = { role: 'system', content: text };
|
|
417
|
+
else history.unshift({ role: 'system', content: text });
|
|
418
|
+
console.log('system prompt set.');
|
|
419
|
+
showPrompt(rl);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
history.push({ role: 'user', content: input });
|
|
423
|
+
process.stdout.write('assistant> ');
|
|
424
|
+
const started = Date.now();
|
|
425
|
+
let answer = '';
|
|
426
|
+
let usage: TokenUsage | null = null;
|
|
427
|
+
let failed = false;
|
|
428
|
+
try {
|
|
429
|
+
usage = await streamChat(cfg, history, (text) => {
|
|
430
|
+
answer += text;
|
|
431
|
+
process.stdout.write(text);
|
|
432
|
+
});
|
|
433
|
+
} catch (err) {
|
|
434
|
+
failed = true;
|
|
435
|
+
if (interrupted) {
|
|
436
|
+
interrupted = false;
|
|
437
|
+
process.stdout.write('\n');
|
|
438
|
+
if (answer !== '') history.push({ role: 'assistant', content: answer });
|
|
439
|
+
} else {
|
|
440
|
+
history.pop(); // drop the unanswered turn so the retry starts clean
|
|
441
|
+
console.error(`\nerror: ${friendlyError(err, cfg)}`);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (failed) {
|
|
445
|
+
showPrompt(rl);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
process.stdout.write('\n');
|
|
449
|
+
console.error(statsLine(usage, Date.now() - started));
|
|
450
|
+
if (answer !== '') history.push({ role: 'assistant', content: answer });
|
|
451
|
+
showPrompt(rl);
|
|
452
|
+
}
|
|
453
|
+
rl.close();
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// The interface can close (EOF) while an answer is still streaming; Node
|
|
457
|
+
// throws ERR_USE_AFTER_CLOSE from prompt() then, and a missing prompt after
|
|
458
|
+
// that is fine because no more input will arrive.
|
|
459
|
+
function showPrompt(rl: ReturnType<typeof createInterface>): void {
|
|
460
|
+
try {
|
|
461
|
+
rl.prompt();
|
|
462
|
+
} catch {
|
|
463
|
+
// interface closed
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
async function main(): Promise<number> {
|
|
468
|
+
const parsed = parseArgs(process.argv.slice(2));
|
|
469
|
+
if (parsed === null) {
|
|
470
|
+
usage();
|
|
471
|
+
return 1;
|
|
472
|
+
}
|
|
473
|
+
if (parsed.help) {
|
|
474
|
+
usage();
|
|
475
|
+
return 0;
|
|
476
|
+
}
|
|
477
|
+
const cfg = resolveConfig(parsed);
|
|
478
|
+
if (cfg === null) return 1;
|
|
479
|
+
if (parsed.positional.length > 0) {
|
|
480
|
+
return oneShot(cfg, parsed.positional.join(' '));
|
|
481
|
+
}
|
|
482
|
+
await repl(cfg);
|
|
483
|
+
return 0;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
main().then((code: number) => {
|
|
487
|
+
if (code !== 0) process.exit(code);
|
|
488
|
+
});
|