typenative 0.5.0 → 0.6.0

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.
Files changed (144) hide show
  1. package/.github/workflows/ci.yml +2 -2
  2. package/.github/workflows/npm-publish-github-packages.yml +2 -2
  3. package/CHANGELOG.md +271 -1
  4. package/CONTRIBUTION.md +2 -1
  5. package/README.md +15 -7
  6. package/bin/cli.js +1 -0
  7. package/bin/index.js +62 -3
  8. package/bin/main.js +1 -0
  9. package/bin/node/core.js +41 -0
  10. package/bin/node/events.js +15 -10
  11. package/bin/node/process.js +13 -0
  12. package/bin/node/punycode.js +2 -2
  13. package/bin/node/timers.js +4 -2
  14. package/bin/node/url.js +1 -1
  15. package/bin/parse-node.js +2 -2
  16. package/bin/regex_vm.js +2057 -0
  17. package/bin/transpiler.js +17225 -4830
  18. package/bin/typecheck-node.js +2 -1
  19. package/bin/version.js +1 -1
  20. package/conformance.md +57 -15
  21. package/examples/README.md +46 -23
  22. package/examples/angular-templates/README.md +139 -0
  23. package/examples/angular-templates/bench.mjs +173 -0
  24. package/examples/angular-templates/dom-type-globals.d.ts +21 -0
  25. package/examples/angular-templates/dom-type-stubs.ts +21 -0
  26. package/examples/angular-templates/main.ts +135 -0
  27. package/examples/angular-templates/parity.mjs +157 -0
  28. package/examples/angular-templates/run-node.mjs +69 -0
  29. package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
  30. package/examples/desktop-notes/README.md +11 -13
  31. package/examples/dupe-finder/README.md +7 -5
  32. package/examples/fibonacci/README.md +7 -7
  33. package/examples/hello-world/README.md +6 -6
  34. package/examples/json-server/README.md +19 -18
  35. package/examples/mandelbrot-png/README.md +11 -13
  36. package/examples/marked-cli/README.md +38 -37
  37. package/examples/openai-chat/README.md +148 -0
  38. package/examples/openai-chat/bench.mjs +175 -0
  39. package/examples/openai-chat/main.ts +488 -0
  40. package/examples/react-compiler/README.md +170 -0
  41. package/examples/react-compiler/bench.mjs +181 -0
  42. package/examples/react-compiler/main.ts +158 -0
  43. package/examples/react-compiler/parity.mjs +362 -0
  44. package/examples/react-compiler/run-node.mjs +88 -0
  45. package/examples/react-compiler/vendor/babel/LICENSE +22 -0
  46. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
  47. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
  48. package/examples/react-compiler/vendor/charcodes/package.json +25 -0
  49. package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
  50. package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
  51. package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
  52. package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
  53. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
  54. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
  55. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
  56. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
  57. package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
  58. package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
  59. package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
  60. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
  61. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
  62. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
  63. package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
  64. package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
  65. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
  66. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
  67. package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
  68. package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
  69. package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
  70. package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
  71. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
  72. package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
  73. package/examples/react-compiler/vendor/zod/LICENSE +21 -0
  74. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
  75. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
  76. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
  77. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
  78. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
  79. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
  80. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
  81. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
  82. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
  83. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
  84. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
  85. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
  86. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
  87. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
  88. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
  89. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
  90. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
  91. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
  92. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
  93. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
  94. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
  95. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
  96. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
  97. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
  98. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
  99. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
  100. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
  101. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
  102. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
  103. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
  104. package/examples/react-compiler/vendor.mjs +688 -0
  105. package/examples/todo-cli/README.md +9 -9
  106. package/examples/yaml-cli/README.md +38 -28
  107. package/package.json +8 -5
  108. package/performance/README.md +195 -0
  109. package/performance/any-dynamic.ts +21 -0
  110. package/performance/any-record-view.ts +20 -0
  111. package/performance/arr-hof.ts +21 -0
  112. package/performance/arr-push-index.ts +15 -0
  113. package/performance/arr-sort.ts +21 -0
  114. package/performance/async-await.ts +24 -0
  115. package/performance/class-dispatch.ts +52 -0
  116. package/performance/events-emit.ts +27 -0
  117. package/performance/exc-throw-rare.ts +25 -0
  118. package/performance/exc-try-catch.ts +26 -0
  119. package/performance/fn-closures.ts +29 -0
  120. package/performance/fn-recursion-tree.ts +31 -0
  121. package/performance/gen-for-of.ts +46 -0
  122. package/performance/json-roundtrip.ts +27 -0
  123. package/performance/map-delete.ts +24 -0
  124. package/performance/map-string-keys.ts +22 -0
  125. package/performance/num-arith-loop.ts +14 -0
  126. package/performance/num-bitwise.ts +21 -0
  127. package/performance/num-mod-int.ts +16 -0
  128. package/performance/num-to-string.ts +16 -0
  129. package/performance/obj-alloc.ts +32 -0
  130. package/performance/regex-exec.ts +29 -0
  131. package/performance/regex-site.ts +28 -0
  132. package/performance/set-ops.ts +20 -0
  133. package/performance/str-char-scan.ts +35 -0
  134. package/performance/str-concat-plus.ts +18 -0
  135. package/performance/str-concat.ts +24 -0
  136. package/performance/str-short-scan.ts +26 -0
  137. package/performance/str-split-join.ts +23 -0
  138. package/performance/str-template.ts +16 -0
  139. package/performance/union-switch.ts +57 -0
  140. package/tsparser/check.go +2 -1
  141. package/tsparser/main.go +1 -0
  142. package/types/typenative-npm.d.ts +3 -2
  143. package/types/typenative.d.ts +42 -12
  144. package/ROUND2-HANDOFF.md +0 -200
@@ -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
+ });
@@ -0,0 +1,170 @@
1
+ # React Compiler
2
+
3
+ The React Compiler, natively. [vendor/react-compiler/](vendor/react-compiler/)
4
+ holds `babel-plugin-react-compiler`'s TypeScript source (the compiler's
5
+ `packages/babel-plugin-react-compiler/src`, MIT licensed), and
6
+ [vendor/babel/](vendor/babel/) the Babel packages it runs on (parser, types,
7
+ traverse, generator, template, code-frame and helpers), with zod 4 and its
8
+ other npm dependencies. [main.ts](main.ts) parses a component or hook with the
9
+ Babel parser, runs the compiler's full pipeline (HIR building, dozens of
10
+ transform passes, code generation) on it, and prints the auto-memoized output
11
+ the Babel plugin would emit, plus one `// CompileSuccess` or `// CompileError`
12
+ line per function from the compiler's own logger.
13
+
14
+ This is the largest and deepest real codebase TypeNative compiles: about 470
15
+ vendored files whose graph is full of import cycles (the React Compiler's
16
+ entrypoint barrels re-export each other's modules), built on Babel's
17
+ `NodePath`/`Scope` machinery (mixin-built classes with prototype methods
18
+ assigned at module init through computed keys, function values carrying
19
+ expando properties, `$constructor`-style factory functions that read `this`
20
+ under `new`), zod schemas validated at startup, discriminated unions narrowed
21
+ across dozens of passes, generators, and `WeakMap`s.
22
+
23
+ ## Usage
24
+
25
+ ```sh
26
+ react-compile # compile the built-in demo component
27
+ react-compile app.jsx # compile a component file
28
+ ```
29
+
30
+ The first line of the input can carry the compiler's test pragmas, as in its
31
+ own fixtures: `// @compilationMode:"infer" @enableJsxOutlining`. A file the
32
+ compiler rejects prints `// compile error: …` and exits with code 1.
33
+
34
+ ## Run it
35
+
36
+ With Node.js (the vendored sources use enums, so types are compiled first —
37
+ `run-node.mjs` does that once and caches the result):
38
+
39
+ ```sh
40
+ node run-node.mjs app.jsx
41
+ ```
42
+
43
+ With TypeNative:
44
+
45
+ ```sh
46
+ npx typenative --source main.ts --output dist/react-compile
47
+ ./dist/react-compile app.jsx
48
+ ```
49
+
50
+ From a checkout of this repository:
51
+
52
+ ```sh
53
+ node bin/cli.js --source examples/react-compiler/main.ts --output dist/react-compile
54
+ ```
55
+
56
+ The vendored sources are checked upstream by their own toolchains, not tsc
57
+ (Babel does not type-check its sources with tsc), so the example's
58
+ [tsconfig.json](tsconfig.json) sets `noCheck`: `main.ts` itself is still
59
+ checked, with the vendored tree's imports resolved.
60
+
61
+ ## Status: what works natively
62
+
63
+ Compiling `main.ts` links ~470 vendored modules and builds a native binary.
64
+ The transpiler work this needed is in TypeNative itself (see the CHANGELOG):
65
+ module initialization now follows the import graph's evaluation order with
66
+ JS's cycle semantics; `new F()` on a constructor-function value gets JS `new`
67
+ semantics; dynamic `fn.bind()`, `Object.assign` onto function values,
68
+ computed `C.prototype[k] = fn` assignments, and dynamic array-method calls
69
+ that share their name with a class method all behave as in JS; and an empty
70
+ object literal is no longer a zero-size Go value (every `{}' was one shared
71
+ object).
72
+
73
+ At runtime the binary initializes every module the way Node evaluates them —
74
+ zod's whole schema graph constructs and `parseEnvironmentConfig` fills in
75
+ every default — and the compiler's whole pipeline runs as it does under
76
+ Node: the Babel parser parses the program (JSX and TypeScript included),
77
+ traverse dispatches visitors, Scope collects bindings, the lowering builds
78
+ the HIR, every inference, validation and memoization pass runs, and the
79
+ Babel generator prints the result. Every parity fixture's output — the
80
+ memoized code, the `// CompileSuccess` counts, the compile errors with their
81
+ code frames — is byte-identical to Node's.
82
+
83
+ Most of what this took was JS object and array identity, which Go's value
84
+ semantics break unless the transpiler keeps it: an object converted to
85
+ another struct type stays one object (union members are views of the same
86
+ memory; a converted object is the same converted object every time); arrays
87
+ stay shared references through any-typed fields, getters, dynamic calls and
88
+ `splice`; a callee's changes to a converted argument (an array, a Map or Set,
89
+ an object's fields) are written back to the caller's; and `slice` copies.
90
+ The rest were JS semantics at the edges: getters in object literals stay
91
+ live, `Object.prototype` methods read as values, default parameters apply to
92
+ an explicit `undefined`, `null` and `undefined` stay apart where code tells
93
+ them apart, Map misses read as `undefined`, and keys written as `undefined`
94
+ are own keys.
95
+
96
+ ## Parity with Node.js
97
+
98
+ [node parity.mjs](parity.mjs) compiles `main.ts` to a native binary and runs
99
+ the compiler over each fixture (components and hooks through
100
+ `useState`/`useMemo`/`useRef`/`useEffect` and custom hooks, the compiler's test
101
+ pragmas, `"use no memo"`, and the error paths) with both runtimes.
102
+ stdout, stderr and the exit code must match byte for byte — including the memo
103
+ slot and block counts in the `// CompileSuccess` lines. Failures are written
104
+ to `.cache/failures/`. All 15 fixtures match:
105
+
106
+ ```sh
107
+ node parity.mjs
108
+ ```
109
+
110
+ ## Benchmark
111
+
112
+ Reproduce with `node bench.mjs [runs=30] [sizeRuns=5] [kilobytes=8,16,32,64]`.
113
+ The script compiles `main.ts`, then times **complete process runs** (spawn to
114
+ exit) of `react-compile` under both runtimes, alternating Node and native runs,
115
+ and stops if the two outputs ever differ:
116
+
117
+ - **(a) per-call latency:** the built-in demo (a `TodoList` component and a
118
+ `useTotal` hook), the size of a file a build compiles once per process.
119
+ - **(b) scaling:** generated component files from 8 to 64 KB (screens of
120
+ components with state, derived lists, handlers and conditionals, from a
121
+ fixed-seed generator, so every machine gets the same input).
122
+
123
+ **Environment:** Windows 11 Pro, AMD Ryzen 5 7600 (6 cores, 12 threads),
124
+ Node v26.13.0, Go 1.27.4, TypeNative 0.5.0 (unreleased changes), default
125
+ (non-`--release`) build. The binary is 26.6 MB.
126
+
127
+ (a) `react-compile` (built-in demo), 30 runs each:
128
+
129
+ | Runtime | min | median | mean | max |
130
+ | ---------- | --------- | --------- | --------- | --------- |
131
+ | Node.js | 356.79 ms | 362.79 ms | 363.00 ms | 368.99 ms |
132
+ | TypeNative | 131.46 ms | 133.23 ms | 134.95 ms | 175.96 ms |
133
+
134
+ (b) `react-compile generated-screen.jsx`, 5 runs per size (medians):
135
+
136
+ | Source | Node.js | TypeNative | TypeNative vs Node |
137
+ | ----------------- | --------- | ----------- | ------------------ |
138
+ | 9 KB, 238 lines | 464.77 ms | 567.89 ms | 1.2x slower |
139
+ | 17 KB, 460 lines | 527.17 ms | 1,039.11 ms | 2.0x slower |
140
+ | 32 KB, 892 lines | 634.59 ms | 1,964.48 ms | 3.1x slower |
141
+ | 64 KB, 1774 lines | 860.94 ms | 3,911.76 ms | 4.5x slower |
142
+
143
+ **Per call, TypeNative is 2.7x faster** (133 ms vs 363 ms, median). Node
144
+ spends most of a short run loading and type-stripping the ~470 vendored
145
+ modules; the native binary starts with them compiled in.
146
+
147
+ **On larger inputs TypeNative is slower, and the gap grows with size:** Node
148
+ compiles each further KB in about 7 ms once V8 has warmed up, the native
149
+ build in about 60 ms. The compiler's passes run through TypeNative's dynamic
150
+ paths (any-typed property reads, prototype-method dispatch, reflection-based
151
+ struct conversions) on every node of every pass, where V8's inline caches
152
+ make the same operations nearly free.
153
+
154
+ The native build was about 100 ms per KB before a profiling pass (6.6 s at
155
+ 64 KB, now 3.9 s), which made those paths cheaper without changing what they
156
+ do:
157
+
158
+ - `s += part` on a string field (Babel's output buffer appends thousands of
159
+ times between flushes) appends in place instead of copying the whole
160
+ string each time, which made printing quadratic in the output size; it was
161
+ more than half of all allocation.
162
+ - Accessor, view-copy and added-property registries are skipped for object
163
+ types that never had an entry, so ordinary property reads stay on the
164
+ reflection-free fast path.
165
+ - Prototype methods (Babel's `NodePath` methods) are called directly, and the
166
+ common callback shapes are called and adapted without `reflect`.
167
+
168
+ What remains is spread thinly over the dynamic paths. The largest single
169
+ item is generator functions, which run as goroutines with a thread wakeup
170
+ per step (about a tenth of the run).