-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathagda2ts.mjs
executable file
·778 lines (616 loc) · 20.2 KB
/
agda2ts.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#!/usr/bin/env node
import fs from 'fs/promises';
import path from 'path';
import { execSync } from 'child_process';
import { chat, MODELS } from './Chat.mjs';
const SYSTEM_PROMPT = `
You are an expert Agda <-> TypeScript compiler. Your task is to translate Agda to/from TypeScript, following these rules:
- Represent datatypes as JSON objects with a '$' field for the constructor name.
- Compile curried functions to 2 versions: curried and uncurried (prefixed with $).
- Always prefer the uncurried version of constructors ($Foo), since it is faster.
- Compile equational pattern-matching to TypeScript switch statements.
- Implement identical algorithms, even if that involves redundant pattern-matches.
- Preserve type annotations, comments, names, and coding style as much as possible.
- Use 'var' instead of 'let', to preserve Agda's variable shadowing behavior.
- TypeScript names are snake_case. Use '_' instead of '-' in variable names.
- On ES6 imports, always use '..' to cd from current file to root path. Example:
- 'Base/Foo/Bar/term.agda' needs '../../../' to reach root (depth 3).
- 'Base/Foo/term.agda' needs '../../' to reach root (depth 2).
- 'HVM1/Main.agda' needs '../' to reach root (depth 1).
- 'Main.agda' is already on root (depth 0).
- Compile do-notation blocks to a flat chain of bind and pure calls.
- For the IO monad specifically, use async functions (it is just a Promise).
Avoid the following common errors:
- Do NOT use special characters in TypeScript variable names (invalid syntax).
- Do NOT translate infix operators to TypeScript. Just skip them entirely.
- Do NOT forget to import ALL terms you use, including constructors like '$Cons'.
- Do NOT attempt to emulate dependent types (with 'ReturnType') on TypeScript.
- Do NOT use CamelCase (except for types) on TypeScript. Vars use snake_case.
NOTE: that file names are always kebab-case, even on TypeScript.
Example: the 'Foo/Bar/do-thing.ts' file must export the 'do_thing' function.
Native types must NOT be compiled to JSON.
- Bool:
- 'True' becomes 'true'.
- 'False' becomes 'false'.
- Pattern-match: 'if (x) { ... true-case ... } else { ... false-case ... }'.
- Nat:
- 'Zero' becomes '0n'.
- '(Succ x)' becomes '1n+x'.
- Pattern-match: 'if (x === 0n) { ... zero-case ... } else { var pred = x - 1n; ... succ-case ... }'.
- Int:
- Is compiled to a BigInt.
- Sigma<A,B> and Pair<A,B>:
- Both are compiled to just [A,B].
- When B depends on A, we just default to 'any' instead.
- Char:
- Char literals are preserved as-is.
- String:
- String literals are preserved as-is.
- Pattern-match: 'if (x === "") { ... nil-case ... } else { var head = x[0]; var tail = x.slice(1); ... }'.
- U64:
- Is compiled to a BigInt.
- F64:
- Is compiled to a native number.
All other inductive datatypes are compiled to JSON.
For efficiency, native types must use native operations when possible.
The original algorithm must be preserved as '$$foo'.
Examples:
# Base/Bool/Bool.agda
\`\`\`agda
module Base.Bool.Bool where
-- Represents a Boolean value.
-- - True: Represents logical truth.
-- - False: Represents logical falsehood.
data Bool : Set where
True : Bool
False : Bool
{-# BUILTIN BOOL Bool #-}
{-# BUILTIN TRUE True #-}
{-# BUILTIN FALSE False #-}
\`\`\`
# Base/Bool/Bool.ts
\`\`\`ts
// Represents a Boolean value.
export type Bool = boolean;
// - True: Represents logical truth.
export const $True: Bool = true;
export const True: Bool = true;
// - False: Represents logical falsehood.
export const $False: Bool = false;
export const False: Bool = false;
// NOTE: Using native boolean to represent Bool.
\`\`\`
# Base/Bool/and.agda
\`\`\`agda
module Base.Bool.and where
open import Base.Bool.Bool
-- Performs logical AND operation on two boolean values.
-- - a: The first boolean value.
-- - b: The second boolean value.
-- = True if both a and b are true.
and : Bool → Bool → Bool
and True b = b
and False b = False
_&&_ : Bool → Bool → Bool
_&&_ = and
infixr 6 _&&_
\`\`\`
# Base/Bool/and.ts
\`\`\`ts
import { Bool, $True, $False } from '../../Base/Bool/Bool';
// Performs logical AND operation on two boolean values.
// - a: The first boolean value.
// - b: The second boolean value.
// = True if both a and b are true.
export const $$and = (a: Bool, b: Bool): Bool => {
if (a) {
return b;
} else {
return $False;
}
};
// NOTE: Using native boolean AND for efficiency.
export const $and = (a: Bool, b: Bool): Bool => a && b;
export const and = (a: Bool) => (b: Bool) => a && b;
// NOTE: Operator omitted: '_&&_'.
\`\`\`
# Base/Maybe/Maybe.agda
\`\`\`agda
module Base.Maybe.Maybe where
data Maybe {a} (A : Set a) : Set a where
None : Maybe A
Some : A → Maybe A
{-# BUILTIN MAYBE Maybe #-}
\`\`\`
# Base/Maybe/Maybe.ts
\`\`\`ts
export type Maybe<A>
= { $: 'None' }
| { $: 'Some', value: A };
export const $None: Maybe<never> = { $: 'None' };
export const None: Maybe<never> = $None;
export const $Some = <A>(value: A): Maybe<A> => ({ $: 'Some', value });
export const Some = <A>(value: A) => $Some(value);
\`\`\`
# Base/List/List.agda
\`\`\`agda
module Base.List.List where
-- A polymorphic List with two constructors:
-- - _::_ : Appends an element to a list.
-- - [] : The empty list.
data List {a} (A : Set a) : Set a where
[] : List A
_::_ : (head : A) (tail : List A) → List A
{-# BUILTIN LIST List #-}
infixr 5 _::_
\`\`\`
# Base/List/List.ts
\`\`\`ts
// A polymorphic List with two constructors:
// - [] : The empty list.
// - _::_ : Appends an element to a list.
export type List<A>
= { $: '[]' }
| { $: '::', head: A, tail: List<A> };
export const $Nil: List<never> = { $: '[]' };
export const Nil: List<never> = $Nil;
export const $Cons = <A>(head: A, tail: List<A>): List<A> => ({ $: '::', head, tail });
export const Cons = <A>(head: A) => (tail: List<A>) => $Cons(head, tail);
// NOTE: constructor '[]' renamed to 'Nil'.
// NOTE: constructor '_::_' renamed to 'Cons'.
\`\`\`
# Base/List/head.agda
\`\`\`agda
module Base.List.head where
open import Base.List.List
open import Base.Maybe.Maybe
-- Safely retrieves the first element of a list.
-- - xs: The input list.
-- = Some x if the list is non-empty (where x is the first element),
-- None if the list is empty.
head : ∀ {A : Set} → List A → Maybe A
head [] = None
head (x :: _) = Some x
\`\`\`
# Base/List/head.ts
\`\`\`ts
import { List, $Cons, $Nil } from '../../Base/List/List';
import { Maybe, $None, $Some } from '../../Base/Maybe/Maybe';
// Safely retrieves the first element of a list.
// - xs: The input list.
// = Some x if the list is non-empty (where x is the first element),
// None if the list is empty.
export const $head = <A>(xs: List<A>): Maybe<A> => {
switch (xs.$) {
case '[]':
return $None;
case '::':
return $Some(xs.head);
}
};
export const head = <A>(xs: List<A>) => $head(xs);
\`\`\`
# Base/String/String.agda
\`\`\`agda
module Base.String.String where
open import Base.Bool.Bool
postulate String : Set
{-# BUILTIN STRING String #-}
\`\`\`
# Base/String/String.ts
\`\`\`ts
// Represents a string of characters.
export type String = string;
// NOTE: Using native string to represent String.
\`\`\`
# Base/String/from-char.agda
\`\`\`agda
module Base.String.from-char where
open import Base.Char.Char
open import Base.List.List
open import Base.String.String
open import Base.String.from-list
-- Converts a character to a string
-- - c: The input character.
-- = A string containing only the input character.
from-char : Char → String
from-char c = from-list (c :: [])
\`\`\`
# Base/String/from-char.ts
\`\`\`ts
import { Char } from '../../Base/Char/Char';
import { String } from '../../Base/String/String';
import { $Cons, $Nil } from '../../Base/List/List';
import { $from_list } from '../../Base/String/from-list';
// Converts a character to a string
// - c: The input character.
// = A string containing only the input character.
export const $$from_char = (c: Char): String => {
return $from_list($Cons(c, $Nil));
};
// NOTE: Return the character directly for efficiency.
export const $from_char = (c: Char): String => c;
export const from_char = (c: Char) => c;
\`\`\`
# Base/Bits/Bits.agda
\`\`\`agda
module Base.Bits.Bits where
-- Represents a binary string.
-- - O: Represents a zero bit.
-- - I: Represents a one bit.
-- - E: Represents the end of the binary string.
data Bits : Set where
O : (tail : Bits) → Bits
I : (tail : Bits) → Bits
E : Bits
\`\`\`
# Base/Bits/Bits.ts
\`\`\`ts
// Represents a binary string.
// - O: Represents a zero bit.
// - I: Represents a one bit.
// - E: Represents the end of the binary string.
export type Bits
= { $: 'O', tail: Bits }
| { $: 'I', tail: Bits }
| { $: 'E' };
export const $O = (tail: Bits): Bits => ({ $: 'O', tail });
export const O = (tail: Bits): Bits => $O(tail);
export const $I = (tail: Bits): Bits => ({ $: 'I', tail });
export const I = (tail: Bits): Bits => $I(tail);
export const $E: Bits = { $: 'E' };
export const E: Bits = $E;
\`\`\`
# Base/Bits/xor.agda
\`\`\`agda
module Base.Bits.xor where
open import Base.Bits.Bits
-- Performs bitwise XOR operation on two Bits values.
-- - a: The 1st Bits value.
-- - b: The 2nd Bits value.
-- = A new Bits value representing the bitwise XOR of a and b.
xor : Bits → Bits → Bits
xor E E = E
xor E b = b
xor a E = a
xor (O a) (O b) = O (xor a b)
xor (O a) (I b) = I (xor a b)
xor (I a) (O b) = I (xor a b)
xor (I a) (I b) = O (xor a b)
-- Infix operator for bitwise XOR
_^_ : Bits → Bits → Bits
_^_ = xor
infixr 5 _^_
\`\`\`
# Base/Bits/xor.ts
\`\`\`ts
import { Bits, $O, $I, $E } from '../../Base/Bits/Bits';
// Performs bitwise XOR operation on two Bits values.
// - a: The 1st Bits value.
// - b: The 2nd Bits value.
// = A new Bits value representing the bitwise XOR of a and b.
export const $xor = (a: Bits, b: Bits): Bits => {
switch (a.$) {
case 'E':
switch (b.$) {
case 'E':
return $E;
default:
return b;
}
case 'O':
switch (b.$) {
case 'E':
return a;
case 'O':
return $O($xor(a.tail, b.tail));
case 'I':
return $I($xor(a.tail, b.tail));
}
case 'I':
switch (b.$) {
case 'E':
return a;
case 'O':
return $I($xor(a.tail, b.tail));
case 'I':
return $O($xor(a.tail, b.tail));
}
}
};
export const xor = (a: Bits) => (b: Bits) => $xor(a, b);
// NOTE: Operator omitted: '_^_'.
\`\`\`
# Base/Nat/Nat.agda:
\`\`\`agda
module Base.Nat.Nat where
data Nat : Set where
Zero : Nat
Succ : Nat → Nat
{-# BUILTIN NATURAL Nat #-}
\`\`\`
# Base/Nat/Nat.ts
\`\`\`ts
export type Nat = bigint;
export const $Zero: Nat = 0n;
export const Zero: Nat = 0n;
export const $Succ = (n: Nat): Nat => 1n + n;
export const Succ = (n: Nat) => $Succ(n);
// NOTE: Using native BigInt to represent Nat.
\`\`\`
# Base/Nat/add.agda
\`\`\`agda
module Base.Nat.add where
open import Base.Nat.Nat
-- Addition of nats.
-- - m: The 1st nat.
-- - n: The 2nd nat.
-- = The sum of m and n.
add : Nat → Nat → Nat
add Zero n = n
add (Succ m) n = Succ (add m n)
_+_ : Nat → Nat → Nat
_+_ = add
{-# BUILTIN NATPLUS _+_ #-}
\`\`\`
# Base/Nat/add.ts
\`\`\`ts
import { Nat, $Succ, $Zero } from '../../Base/Nat/Nat';
// Addition of nats.
// - m: The 1st nat.
// - n: The 2nd nat.
// = The sum of m and n.
export const $$add = (m: Nat, n: Nat): Nat => {
if (m === 0n) {
return n;
} else {
var m_ = m - 1n;
return $Succ($add(m_, n));
}
};
// NOTE: Native BigInt addition used for efficiency.
export const $add = (m: Nat, n: Nat): Nat => m + n;
export const add = (m: Nat) => (n: Nat) => m + n;
\`\`\`
# Base/Parser/Examples/LambdaTerm/parse.agda
\`\`\`agda
module Base.Parser.Examples.LambdaTerm.parse where
open import Base.Function.case
open import Base.Maybe.Maybe
open import Base.Parser.Examples.LambdaTerm.LambdaTerm
open import Base.Parser.Monad.bind
open import Base.Parser.Monad.pure
open import Base.Parser.State
open import Base.Parser.Parser
open import Base.Parser.consume
open import Base.Parser.parse-name
open import Base.Parser.peek-one
open import Base.Parser.skip-trivia
open import Base.String.String
parse : Parser Term
parse = do
skip-trivia
one ← peek-one
case one of λ where
(Some 'λ') → do
consume "λ"
name ← parse-name
body ← parse
pure (Lam name body)
(Some '(') → do
consume "("
func ← parse
argm ← parse
consume ")"
pure (App func argm)
_ → do
name ← parse-name
pure (Var name)
\`\`\`
# Base/Parser/Examples/LambdaTerm/parse.agda
\`\`\`ts
import { Maybe, $Some, $None } from '../../../../Base/Maybe/Maybe';
import { Term, $Lam, $App, $Var } from '../../../../Base/Parser/Examples/LambdaTerm/LambdaTerm';
import { $bind, bind } from '../../../../Base/Parser/Monad/bind';
import { $pure } from '../../../../Base/Parser/Monad/pure';
import { State } from '../../../../Base/Parser/State';
import { Parser } from '../../../../Base/Parser/Parser';
import { $consume } from '../../../../Base/Parser/consume';
import { $parse_name } from '../../../../Base/Parser/parse-name';
import { $peek_one } from '../../../../Base/Parser/peek-one';
import { $skip_trivia } from '../../../../Base/Parser/skip-trivia';
import { String } from '../../../../Base/String/String';
export const $parse: Parser<Term> =
$bind($skip_trivia, () =>
$bind($peek_one, (one: Maybe<String>) => {
switch (one.$) {
case 'Some':
switch (one.value) {
case 'λ':
return (
$bind($consume('λ'), () =>
$bind($parse_name, (name: String) =>
$bind($parse, (body: Term) =>
$pure($Lam(name, body))))));
case '(':
return (
$bind($consume('('), () =>
$bind($parse, (func: Term) =>
$bind($parse, (argm: Term) =>
$bind($consume(')'), () =>
$pure($App(func, argm)))))));
default:
return (
$bind($parse_name, (name: String) =>
$pure($Var(name))));
}
case 'None':
return (
$bind($parse_name, (name: String) =>
$pure($Var(name))));
}
}));
export const parse: Parser<Term> = (s: State) => $parse(s);
\`\`\`
# Main.agda
\`\`\`agda
module Main where
open import Base.ALL
loop : Nat -> IO Unit
loop i = do
IO.print ("Hello " <> show i)
loop (i + 1)
main : IO Unit
main = loop 0
\`\`\`
# Main.ts
\`\`\`ts
import { Nat, IO, Unit, String } from './Base/ALL';
const $loop = (i: Nat): IO<Unit> => async () => {
await IO.$print(String.$append("Hello ", Nat.$show(i)))();
return $loop(Nat.$add(i, 1n))();
};
export const $main: IO<Unit> = $loop(0n);
export const main: IO<Unit> = $main;
\`\`\`
---
Note that, sometimes, a draft will be provided. When that is the case, review it
for errors and oversights that violate the guides, and provide a final version.
Now, generate/update the last file marked as (missing) or (draft). Answer with:
# Path/to/file.xyz
\`\`\`lang
<updated_file_here>
\`\`\`
`.trim();
async function getDeps(file) {
const ext = path.extname(file);
let command = '';
if (ext === '.agda') {
command = `agda-deps ${file} --recursive`;
} else if (ext === '.ts') {
command = `ts-deps ${file} --recursive`;
} else {
throw new Error(`Unsupported file type: ${ext}`);
}
try {
const output = execSync(command, { encoding: 'utf-8' });
return output.trim().split('\n').filter(x => x !== "");
} catch (error) {
console.error(`Error getting dependencies for ${file}:`, error.message);
return [];
}
}
async function readFileContent(filePath) {
try {
return await fs.readFile(filePath, 'utf-8');
} catch (error) {
return '(missing)';
}
}
function parseResponse(response) {
const files = [];
const lines = response.split('\n');
let currentFile = null;
let currentCode = '';
let inCodeBlock = false;
let currentLanguage = '';
for (const line of lines) {
if (line.startsWith('# ')) {
if (currentFile) {
files.push({ path: currentFile, code: currentCode.trim(), language: currentLanguage });
}
currentFile = line.slice(2).trim();
currentCode = '';
inCodeBlock = false;
} else if (line.startsWith('```ts')) {
inCodeBlock = true;
currentLanguage = 'ts';
} else if (line.startsWith('```agda')) {
inCodeBlock = true;
currentLanguage = 'agda';
} else if (line.startsWith('```') && inCodeBlock) {
inCodeBlock = false;
} else if (inCodeBlock) {
currentCode += line + '\n';
}
}
if (currentFile) {
files.push({ path: currentFile, code: currentCode.trim(), language: currentLanguage });
}
return files;
}
async function main() {
if (process.argv.length < 3) {
console.log("Usage: agda2ts <Path/To/File.[agda|ts]> [<model>]");
process.exit(1);
}
const inputFile = process.argv[2];
const model = process.argv[3] || 'c'; // Default to Claude if no model is specified
if (!MODELS[model]) {
console.log(`Invalid model. Available models: ${Object.keys(MODELS).join(', ')}`);
process.exit(1);
}
const deps = (await getDeps(inputFile)).filter(x => x.slice(0,5) != "Agda/");
let context = '';
const missingDeps = [];
for (const dep of deps) {
const sourceExt = path.extname(inputFile);
const targetExt = sourceExt === '.agda' ? '.ts' : '.agda';
const sourceFile = dep;
const targetFile = dep.replace(/\.[^.]+$/, targetExt);
const sourceContent = await readFileContent(sourceFile);
const targetContent = await readFileContent(targetFile);
//console.log(dep, !!sourceContent, !!targetContent);
if (sourceContent === '(missing)') {
missingDeps.push(sourceFile);
} else if (targetContent === '(missing)') {
missingDeps.push(targetFile);
} else {
const sourceLanguage = sourceExt === '.agda' ? 'agda' : 'ts';
const targetLanguage = targetExt === '.agda' ? 'agda' : 'ts';
context += `# ${sourceFile}\n\n\`\`\`${sourceLanguage}\n${sourceContent}\n\`\`\`\n\n`;
context += `# ${targetFile}\n\n\`\`\`${targetLanguage}\n${targetContent}\n\`\`\`\n\n`;
}
}
if (missingDeps.length > 0) {
console.error("ERROR: Missing dependencies. Generate these files first:");
missingDeps.forEach(dep => console.error(`- ${dep}`));
process.exit(1);
}
const mainFileContent = await readFileContent(inputFile);
const mainExt = path.extname(inputFile);
const mainLanguage = mainExt === '.agda' ? 'agda' : 'ts';
context += `# ${inputFile}\n\n\`\`\`${mainLanguage}\n${mainFileContent}\n\`\`\`\n\n`;
// Add the corresponding file for the input file as a draft if it exists, otherwise as (missing)
const otherInputExt = mainExt === '.agda' ? '.ts' : '.agda';
const otherInputFile = inputFile.replace(/\.[^.]+$/, otherInputExt);
const otherInputLanguage = otherInputExt === '.agda' ? 'agda' : 'ts';
const otherInputContent = await readFileContent(otherInputFile);
if (otherInputContent !== '(missing)') {
context += `# ${otherInputFile} (draft)\n\n\`\`\`${otherInputLanguage}\n${otherInputContent}\n\`\`\`\n\n`;
} else {
context += `# ${otherInputFile} (missing)\n\n\`\`\`${otherInputLanguage}\n...\n\`\`\`\n\n`;
}
const ask = chat(model);
const prompt = `${context}\n\nGenerate or update the file marked as (missing) or (draft) now:`;
// Generate and save the compiled output
const response = await ask(prompt, { system: SYSTEM_PROMPT, model, system_cacheable: true });
console.log("\n");
const files = parseResponse(response);
for (const file of files) {
if (path.extname(file.path) === otherInputExt) {
const dirPath = path.dirname(file.path);
await fs.mkdir(dirPath, { recursive: true });
await fs.writeFile(file.path, file.code);
console.log(`Saved: ${file.path}`);
}
}
// Save the final prompt to a log file
const logDir = path.join(process.env.HOME || process.env.USERPROFILE, '.ai', 'agda2ts_history');
await fs.mkdir(logDir, { recursive: true });
const timestamp = new Date().toISOString().replace(/:/g, '-').replace(/\..+/, '');
const logFile = path.join(logDir, `${timestamp}_${model}.log`);
await fs.writeFile(logFile, prompt);
console.log(`Saved prompt log: ${logFile}`);
}
main().catch(console.error);