dirac-lang 0.1.2

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 (110) hide show
  1. package/.env.example +8 -0
  2. package/COMMUNITY.md +465 -0
  3. package/LIBRARIES.md +172 -0
  4. package/NAMESPACES.md +366 -0
  5. package/PROMOTION.md +257 -0
  6. package/QUICKSTART-LIBRARY.md +93 -0
  7. package/README.md +257 -0
  8. package/config.yml +6 -0
  9. package/config.yml.openai +4 -0
  10. package/dirac-http/examples/demo.di +9 -0
  11. package/dirac-http/lib/index.di +12 -0
  12. package/dist/chunk-NDIRTD3D.js +217 -0
  13. package/dist/chunk-S625X7ME.js +1071 -0
  14. package/dist/cli.d.ts +1 -0
  15. package/dist/cli.js +261 -0
  16. package/dist/index.d.ts +144 -0
  17. package/dist/index.js +22 -0
  18. package/dist/session-4QG7OERD.js +42 -0
  19. package/examples/add-demo.di +74 -0
  20. package/examples/add.bk +11 -0
  21. package/examples/advanced-math-demo.di +53 -0
  22. package/examples/calculator.di +32 -0
  23. package/examples/comprehensive.bk +29 -0
  24. package/examples/defvar-variable-demo.di +18 -0
  25. package/examples/direct-call.di +17 -0
  26. package/examples/disk-analysis.di +16 -0
  27. package/examples/executable-hello.di +7 -0
  28. package/examples/execute-demo.di +38 -0
  29. package/examples/file-manager.di +77 -0
  30. package/examples/file-stats.di +18 -0
  31. package/examples/hello.bk +1 -0
  32. package/examples/hello.di +5 -0
  33. package/examples/import-demo.di +31 -0
  34. package/examples/inline-test.bk +7 -0
  35. package/examples/lib/advanced-math.di +81 -0
  36. package/examples/lib/fileops.di +26 -0
  37. package/examples/lib/math.di +25 -0
  38. package/examples/lib/mongodb.di +96 -0
  39. package/examples/llm-agent.di +32 -0
  40. package/examples/llm-basic.di +12 -0
  41. package/examples/llm-command-no-exec.di +13 -0
  42. package/examples/llm-command.di +13 -0
  43. package/examples/llm-complex.di +141 -0
  44. package/examples/llm-recursive.di +31 -0
  45. package/examples/llm-reflection-test.di +19 -0
  46. package/examples/llm-subs.di +132 -0
  47. package/examples/llm-use-subs.di +6 -0
  48. package/examples/loop.di +12 -0
  49. package/examples/math-test.di +22 -0
  50. package/examples/mongodb-count-events.di +8 -0
  51. package/examples/mongodb-import-demo.di +25 -0
  52. package/examples/mongodb-simple-test.di +18 -0
  53. package/examples/nl-agent.di +47 -0
  54. package/examples/parameters-demo.di +68 -0
  55. package/examples/params-test.di +10 -0
  56. package/examples/recipe-chain.di +38 -0
  57. package/examples/recursive-llm.di +44 -0
  58. package/examples/sample-library/README.md +152 -0
  59. package/examples/sample-library/examples/demo.di +34 -0
  60. package/examples/sample-library/lib/index.di +65 -0
  61. package/examples/sample-library/package.json +31 -0
  62. package/examples/seamless.di +45 -0
  63. package/examples/shell-test.bk +10 -0
  64. package/examples/simple-import.di +13 -0
  65. package/examples/simple-recursive.di +26 -0
  66. package/examples/story-builder.di +45 -0
  67. package/examples/subroutine.di +23 -0
  68. package/examples/system-llm.di +21 -0
  69. package/examples/system-simple.di +3 -0
  70. package/examples/system-test.di +13 -0
  71. package/examples/task-assistant.di +27 -0
  72. package/examples/test-parameters.di +50 -0
  73. package/examples/two-styles.di +28 -0
  74. package/examples/var-debug.di +6 -0
  75. package/examples/var-inline.di +4 -0
  76. package/examples/var-test2.di +6 -0
  77. package/examples/variable-simple.di +16 -0
  78. package/examples/variable-test.di +22 -0
  79. package/filePath +1 -0
  80. package/greeting.txt +1 -0
  81. package/package.json +41 -0
  82. package/src/cli.ts +118 -0
  83. package/src/index.ts +33 -0
  84. package/src/llm/ollama.ts +58 -0
  85. package/src/runtime/braket-parser.ts +234 -0
  86. package/src/runtime/interpreter.ts +135 -0
  87. package/src/runtime/parser.ts +151 -0
  88. package/src/runtime/session.ts +228 -0
  89. package/src/tags/assign.ts +37 -0
  90. package/src/tags/call.ts +156 -0
  91. package/src/tags/defvar.ts +56 -0
  92. package/src/tags/eval.ts +68 -0
  93. package/src/tags/execute.ts +52 -0
  94. package/src/tags/expr.ts +128 -0
  95. package/src/tags/if.ts +58 -0
  96. package/src/tags/import.ts +66 -0
  97. package/src/tags/index.ts +37 -0
  98. package/src/tags/llm.ts +207 -0
  99. package/src/tags/loop.ts +43 -0
  100. package/src/tags/mongodb.ts +70 -0
  101. package/src/tags/output.ts +23 -0
  102. package/src/tags/parameters.ts +79 -0
  103. package/src/tags/require_module.ts +19 -0
  104. package/src/tags/subroutine.ts +52 -0
  105. package/src/tags/system.ts +70 -0
  106. package/src/tags/variable.ts +25 -0
  107. package/src/types/index.ts +101 -0
  108. package/src/utils/llm-adapter.ts +113 -0
  109. package/tools/create-library.sh +175 -0
  110. package/tsconfig.json +19 -0
@@ -0,0 +1,65 @@
1
+ <dirac>
2
+
3
+ <subroutine name="STR_UPPERCASE">
4
+ <eval>
5
+ const caller = getParams();
6
+ const text = caller.attributes.text || '';
7
+ console.log(text.toUpperCase());
8
+ </eval>
9
+ </subroutine>
10
+
11
+ <subroutine name="STR_LOWERCASE">
12
+ <eval>
13
+ const caller = getParams();
14
+ const text = caller.attributes.text || '';
15
+ console.log(text.toLowerCase());
16
+ </eval>
17
+ </subroutine>
18
+
19
+ <subroutine name="STR_TRIM">
20
+ <eval>
21
+ const caller = getParams();
22
+ const text = caller.attributes.text || '';
23
+ console.log(text.trim());
24
+ </eval>
25
+ </subroutine>
26
+
27
+ <subroutine name="STR_SUBSTRING">
28
+ <eval>
29
+ const caller = getParams();
30
+ const text = caller.attributes.text || '';
31
+ const start = parseInt(caller.attributes.start || 0);
32
+ const end = caller.attributes.end ? parseInt(caller.attributes.end) : undefined;
33
+ console.log(text.substring(start, end));
34
+ </eval>
35
+ </subroutine>
36
+
37
+ <subroutine name="STR_REPLACE">
38
+ <eval>
39
+ const caller = getParams();
40
+ const text = caller.attributes.text || '';
41
+ const find = caller.attributes.find || '';
42
+ const replace = caller.attributes.replace || '';
43
+ console.log(text.replace(new RegExp(find, 'g'), replace));
44
+ </eval>
45
+ </subroutine>
46
+
47
+ <subroutine name="STR_SPLIT">
48
+ <eval>
49
+ const caller = getParams();
50
+ const text = caller.attributes.text || '';
51
+ const delimiter = caller.attributes.delimiter || ',';
52
+ const parts = text.split(delimiter);
53
+ console.log(JSON.stringify(parts));
54
+ </eval>
55
+ </subroutine>
56
+
57
+ <subroutine name="STR_LENGTH">
58
+ <eval>
59
+ const caller = getParams();
60
+ const text = caller.attributes.text || '';
61
+ console.log(text.length);
62
+ </eval>
63
+ </subroutine>
64
+
65
+ </dirac>
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "dirac-string",
3
+ "version": "1.0.0",
4
+ "description": "String manipulation library for Dirac",
5
+ "main": "lib/index.di",
6
+ "scripts": {
7
+ "test": "dirac tests/test.di"
8
+ },
9
+ "keywords": [
10
+ "dirac",
11
+ "string",
12
+ "text",
13
+ "manipulation"
14
+ ],
15
+ "author": "Dirac Community",
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/dirac-lang/dirac-string"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/dirac-lang/dirac-string/issues"
23
+ },
24
+ "homepage": "https://github.com/dirac-lang/dirac-string#readme",
25
+ "dirac": {
26
+ "version": ">=0.1.0",
27
+ "exports": {
28
+ "main": "lib/index.di"
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,45 @@
1
+ <!-- Seamless LLM-Dirac Interleaving - THE PHILOSOPHICAL DEMO -->
2
+ <dirac>
3
+ <output>Seamless Execution Demo&#10;&#10;</output>
4
+
5
+ <!-- Define a subroutine that lists files -->
6
+ <subroutine name="LIST_FILES">
7
+ <eval name="files">
8
+ return fs.readdirSync('examples')
9
+ .filter(f => f.endsWith('.di'))
10
+ .slice(0, 5)
11
+ .join(', ');
12
+ </eval>
13
+ <output>Files found: ${files}&#10;</output>
14
+ </subroutine>
15
+
16
+ <!-- Define a subroutine that counts something -->
17
+ <subroutine name="COUNT_FILES">
18
+ <eval name="count">
19
+ return fs.readdirSync('examples').filter(f => f.endsWith('.di')).length;
20
+ </eval>
21
+ <output>Total count: ${count}&#10;</output>
22
+ </subroutine>
23
+
24
+ <!-- Now the magic: LLM returns Dirac tags that get executed immediately -->
25
+ <output>Asking LLM to analyze and respond with executable tags...&#10;&#10;</output>
26
+
27
+ <LLM execute="true" maxTokens="200">
28
+ You are analyzing a project directory.
29
+
30
+ Respond by calling ONLY these Dirac subroutines (XML tags):
31
+ - &lt;LIST_FILES /&gt; - lists example files
32
+ - &lt;COUNT_FILES /&gt; - counts total files
33
+
34
+ Your response should be pure XML like:
35
+ &lt;dirac&gt;
36
+ &lt;output&gt;Analysis:&#10;&lt;/output&gt;
37
+ &lt;LIST_FILES /&gt;
38
+ &lt;COUNT_FILES /&gt;
39
+ &lt;/dirac&gt;
40
+
41
+ Return ONLY the XML, no explanation.
42
+ </LLM>
43
+
44
+ <output>&#10;Done!&#10;</output>
45
+ </dirac>
@@ -0,0 +1,10 @@
1
+ <listfiles|
2
+ |parameters select=@dir>
3
+ |system>
4
+ ls -la |variable name=dir>
5
+ echo "Files in |variable name=dir>:"
6
+ find |variable name=dir> -name "*.bk"
7
+
8
+ |output>Listing files in directory:
9
+ |output>
10
+ |listfiles dir=examples>
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env dirac
2
+ <!-- Simple import test -->
3
+ <dirac>
4
+ <import src="./lib/math.di" />
5
+
6
+ <defvar name="x" value="7" />
7
+ <output>Testing SQUARE with x=${x}&#10;</output>
8
+
9
+ <eval name="result">return 7 * 7;</eval>
10
+ <output>Direct calc: ${result}&#10;</output>
11
+
12
+ <call name="SQUARE" />
13
+ </dirac>
@@ -0,0 +1,26 @@
1
+ <!-- Simple Recursive Demo - LISP Style -->
2
+ <dirac>
3
+ <output>Simple Recursion Demo&#10;&#10;</output>
4
+
5
+ <!-- Subroutine that calculates something -->
6
+ <subroutine name="CALC">
7
+ <eval name="result">return 5 * 5;</eval>
8
+ <output>Calculated: ${result}&#10;</output>
9
+ </subroutine>
10
+
11
+ <!-- Subroutine with nested LLM call -->
12
+ <subroutine name="NESTED">
13
+ <output>Entering nested call...&#10;</output>
14
+ <LLM execute="true" maxTokens="50">
15
+ &lt;dirac&gt;&lt;output&gt;From nested LLM!&#10;&lt;/output&gt;&lt;/dirac&gt;
16
+ </LLM>
17
+ </subroutine>
18
+
19
+ <!-- Start with direct LLM execution -->
20
+ <output>Starting...&#10;</output>
21
+ <LLM execute="true" maxTokens="50">
22
+ Copy this EXACTLY: &lt;dirac&gt;&lt;CALC /&gt;&lt;NESTED /&gt;&lt;/dirac&gt;
23
+ </LLM>
24
+
25
+ <output>Done!&#10;</output>
26
+ </dirac>
@@ -0,0 +1,45 @@
1
+ <!-- Story Builder - Loop with LLM -->
2
+ <dirac>
3
+ <defvar name="character" value="a curious robot" />
4
+ <defvar name="setting" value="abandoned space station" />
5
+
6
+ <output>Creating a story about ${character} in ${setting}...&#10;&#10;</output>
7
+
8
+ <!-- Generate the opening -->
9
+ <LLM output="opening" maxTokens="150">
10
+ Write the opening paragraph of a story about ${character} in ${setting}.
11
+ End with a cliffhanger.
12
+ </LLM>
13
+ <output>${opening}&#10;&#10;</output>
14
+
15
+ <!-- Generate 3 story beats -->
16
+ <defvar name="context" value="${opening}" />
17
+
18
+ <loop var="i" from="1" to="3">
19
+ <output>--- Part ${i} ---&#10;</output>
20
+
21
+ <LLM output="nextPart" maxTokens="150">
22
+ Continue this story with the next paragraph:
23
+
24
+ ${context}
25
+
26
+ Make it exciting and end with a cliffhanger.
27
+ </LLM>
28
+
29
+ <output>${nextPart}&#10;&#10;</output>
30
+
31
+ <!-- Update context for next iteration -->
32
+ <assign name="context" value="${context} ${nextPart}" />
33
+ </loop>
34
+
35
+ <!-- Generate conclusion -->
36
+ <output>--- Conclusion ---&#10;</output>
37
+ <LLM output="ending" maxTokens="150">
38
+ Write a satisfying conclusion to this story:
39
+
40
+ ${context}
41
+
42
+ Wrap up the plot in 2-3 sentences.
43
+ </LLM>
44
+ <output>${ending}&#10;</output>
45
+ </dirac>
@@ -0,0 +1,23 @@
1
+ <!-- Subroutine and parameters test -->
2
+ <dirac>
3
+ <subroutine name="greet">
4
+ <parameters>
5
+ <variable name="name" passby="value" />
6
+ </parameters>
7
+ <output>Hello, ${name}!</output>
8
+ </subroutine>
9
+
10
+ <call name="greet">
11
+ <parameters>
12
+ <variable value="Alice" />
13
+ </parameters>
14
+ </call>
15
+
16
+ <output> </output>
17
+
18
+ <call name="greet">
19
+ <parameters>
20
+ <variable value="Bob" />
21
+ </parameters>
22
+ </call>
23
+ </dirac>
@@ -0,0 +1,21 @@
1
+ <dirac>
2
+ <!-- Get file listing -->
3
+ <defvar name="dir">examples</defvar>
4
+ <output>Getting file listing from ${dir}...</output>
5
+ <output>&#10;</output>
6
+
7
+ <llm noextra="true" output="fileList">
8
+ <system>ls -lh ${dir} | head -10</system>
9
+
10
+ Above is a directory listing. Please analyze it and tell me:
11
+ 1. How many files are shown?
12
+ 2. What is the total size of the largest file?
13
+ 3. What file types do you see (by extension)?
14
+
15
+ Please be concise.
16
+ </llm>
17
+
18
+ <output>&#10;&#10;LLM Analysis:</output>
19
+ <output>&#10;</output>
20
+ <output><variable name="fileList"/></output>
21
+ </dirac>
@@ -0,0 +1,3 @@
1
+ <dirac>
2
+ <system>echo "Hello from system"</system>
3
+ </dirac>
@@ -0,0 +1,13 @@
1
+ <dirac>
2
+ <!-- Test 1: Simple command -->
3
+ <output>Current directory:</output>
4
+ <output>&#10;</output>
5
+ <system>pwd</system>
6
+ <output>&#10;</output>
7
+
8
+ <!-- Test 2: Command with variable substitution -->
9
+ <defvar name="dir">examples</defvar>
10
+ <output>Files in ${dir}:</output>
11
+ <output>&#10;</output>
12
+ <system>ls -la ${dir} | head -5</system>
13
+ </dirac>
@@ -0,0 +1,27 @@
1
+ <!-- Task Assistant - LLM breaks down and executes tasks -->
2
+ <dirac>
3
+ <defvar name="task" value="organize a birthday party" />
4
+
5
+ <output>Task: ${task}&#10;&#10;</output>
6
+
7
+ <!-- Ask LLM to break down the task -->
8
+ <output>Breaking down the task...&#10;</output>
9
+ <LLM output="steps" maxTokens="300">
10
+ Break down "${task}" into 3-5 concrete action steps.
11
+ Return ONLY a numbered list, one step per line.
12
+ Keep each step brief and actionable.
13
+ </LLM>
14
+
15
+ <output>&#10;Steps to complete:&#10;${steps}&#10;&#10;</output>
16
+
17
+ <!-- Now get LLM to provide tips for the first step -->
18
+ <output>Getting tips for step 1...&#10;</output>
19
+ <LLM output="tips" maxTokens="200">
20
+ For the first step in this list:
21
+ ${steps}
22
+
23
+ Provide 2-3 quick practical tips. Be concise.
24
+ </LLM>
25
+
26
+ <output>&#10;Tips:&#10;${tips}&#10;</output>
27
+ </dirac>
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env dirac
2
+ <dirac>
3
+ <!-- Test 1: parameters select="*" - access child elements -->
4
+ <subroutine name="GREET">
5
+ <output>Hello, </output>
6
+ <parameters select="*"/>
7
+ <output>!</output>
8
+ </subroutine>
9
+
10
+ <output>Test 1 - Child elements:</output>
11
+ <GREET>
12
+ <output>Alice</output>
13
+ </GREET>
14
+ <output>&#10;</output>
15
+
16
+ <!-- Test 2: parameters select="@attr" - access specific attribute -->
17
+ <subroutine name="SQUARE">
18
+ <output>x=</output>
19
+ <parameters select="@x"/>
20
+ <output> squared = ?</output>
21
+ </subroutine>
22
+
23
+ <output>Test 2 - Attribute access: </output>
24
+ <SQUARE x="5"/>
25
+ <output>&#10;</output>
26
+
27
+ <!-- Test 3: parameters select="@*" - access all attributes -->
28
+ <subroutine name="SHOW_ATTRS">
29
+ <output>Attributes: </output>
30
+ <parameters select="@*"/>
31
+ <output>&#10;</output>
32
+ </subroutine>
33
+
34
+ <output>Test 3 - All attributes:</output>
35
+ <SHOW_ATTRS name="test" value="123" color="red"/>
36
+
37
+ <!-- Test 4: Multiple child elements -->
38
+ <subroutine name="LIST">
39
+ <output>Items: </output>
40
+ <parameters select="*"/>
41
+ <output>&#10;</output>
42
+ </subroutine>
43
+
44
+ <output>Test 4 - Multiple children: </output>
45
+ <LIST>
46
+ <output>Apple, </output>
47
+ <output>Banana, </output>
48
+ <output>Orange</output>
49
+ </LIST>
50
+ </dirac>
@@ -0,0 +1,28 @@
1
+ <!-- Comparing Two Styles of LLM-Code Execution -->
2
+ <dirac>
3
+ <output>=== Style Comparison Demo ===&#10;&#10;</output>
4
+
5
+ <!-- STYLE 1: EXPLICIT (Clear, Debuggable) -->
6
+ <output>--- Explicit Style (Clarity) ---&#10;</output>
7
+ <LLM output="generatedCode" maxTokens="100">
8
+ Return ONLY this XML, nothing else:
9
+ &lt;dirac&gt;&lt;eval name="result"&gt;return 10 + 20;&lt;/eval&gt;&lt;output&gt;Sum: ${result}&lt;/output&gt;&lt;/dirac&gt;
10
+ </LLM>
11
+
12
+ <output>Generated code:&#10;${generatedCode}&#10;&#10;</output>
13
+ <output>Executing...&#10;</output>
14
+ <execute source="generatedCode" />
15
+ <output>&#10;&#10;</output>
16
+
17
+ <!-- STYLE 2: LISP (Seamless, Direct) -->
18
+ <output>--- LISP Style (Seamless) ---&#10;</output>
19
+ <LLM execute="true" maxTokens="100">
20
+ Return ONLY this XML, nothing else:
21
+ &lt;dirac&gt;&lt;eval name="result"&gt;return 30 + 40;&lt;/eval&gt;&lt;output&gt;Sum: ${result}&lt;/output&gt;&lt;/dirac&gt;
22
+ </LLM>
23
+ <output>&#10;</output>
24
+
25
+ <output>--- When to Use Each ---&#10;</output>
26
+ <output>Explicit: Debugging, inspection, conditional execution&#10;</output>
27
+ <output>LISP: Real-time interaction, streaming, recursive calls&#10;</output>
28
+ </dirac>
@@ -0,0 +1,6 @@
1
+ <dirac>
2
+ <defvar name="test">Hello World</defvar>
3
+ <output>Start: </output>
4
+ <variable name="test"/>
5
+ <output> :End</output>
6
+ </dirac>
@@ -0,0 +1,4 @@
1
+ <dirac>
2
+ <defvar name="user">World</defvar>
3
+ <output>Hello, <variable name="user"/>!</output>
4
+ </dirac>
@@ -0,0 +1,6 @@
1
+ <dirac>
2
+ <defvar name="name">Alice</defvar>
3
+ <output>Before</output>
4
+ <variable name="name"/>
5
+ <output>After</output>
6
+ </dirac>
@@ -0,0 +1,16 @@
1
+ <dirac>
2
+ <!-- Test 1: Simple text variable -->
3
+ <defvar name="name">Alice</defvar>
4
+ <output>Hello, <variable name="name"/>!</output>
5
+ <output>&#10;</output>
6
+
7
+ <!-- Test 2: Using ${} substitution -->
8
+ <output>Using substitution: Hello, ${name}!</output>
9
+ <output>&#10;</output>
10
+
11
+ <!-- Test 3: Number variable -->
12
+ <defvar name="x">5</defvar>
13
+ <defvar name="y">3</defvar>
14
+ <output>x = <variable name="x"/>, y = <variable name="y"/></output>
15
+ <output>&#10;</output>
16
+ </dirac>
@@ -0,0 +1,22 @@
1
+ <dirac>
2
+ <!-- Test 1: Simple text variable -->
3
+ <defvar name="name">Alice</defvar>
4
+ <output>Hello, <variable name="name"/>!</output>
5
+ <output>&#10;</output>
6
+
7
+ <!-- Test 2: Using ${} substitution -->
8
+ <output>Using substitution: Hello, ${name}!</output>
9
+ <output>&#10;</output>
10
+
11
+ <!-- Test 3: Variable with XML code as string -->
12
+ <defvar name="template">&lt;output&gt;Template says: Hello, &lt;variable name="name"/&gt;!&lt;/output&gt;</defvar>
13
+
14
+ <output>Raw template: </output>
15
+ <variable name="template"/>
16
+ <output>&#10;</output>
17
+
18
+ <!-- Test 4: Execute the template -->
19
+ <output>Executed: </output>
20
+ <execute source="template"/>
21
+ <output>&#10;</output>
22
+ </dirac>
package/filePath ADDED
@@ -0,0 +1 @@
1
+ fileContent
package/greeting.txt ADDED
@@ -0,0 +1 @@
1
+ hello world
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "dirac-lang",
3
+ "version": "0.1.2",
4
+ "description": "LLM-Augmented Declarative Execution",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "dirac": "dist/cli.js"
10
+ },
11
+ "scripts": {
12
+ "dev": "tsx src/cli.ts",
13
+ "build": "tsup src/index.ts src/cli.ts --format esm --dts --clean",
14
+ "test": "vitest",
15
+ "typecheck": "tsc --noEmit"
16
+ },
17
+ "keywords": [
18
+ "llm",
19
+ "declarative",
20
+ "dirac",
21
+ "bra-ket"
22
+ ],
23
+ "author": "Zhi Wang",
24
+ "license": "MIT",
25
+ "dependencies": {
26
+ "@anthropic-ai/sdk": "^0.30.1",
27
+ "dotenv": "^17.2.3",
28
+ "fast-xml-parser": "^4.3.5",
29
+ "js-yaml": "^4.1.1",
30
+ "mongodb": "^7.0.0",
31
+ "openai": "^6.16.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/js-yaml": "^4.0.9",
35
+ "@types/node": "^20.11.0",
36
+ "tsup": "^8.0.1",
37
+ "tsx": "^4.7.0",
38
+ "typescript": "^5.3.3",
39
+ "vitest": "^1.2.0"
40
+ }
41
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Dirac CLI
5
+ */
6
+
7
+ import 'dotenv/config';
8
+ import fs from 'fs';
9
+ import yaml from 'js-yaml';
10
+ import { resolve, extname } from 'path';
11
+ import { execute } from './index.js';
12
+ import { BraKetParser } from './runtime/braket-parser.js';
13
+
14
+ async function main() {
15
+ const args = process.argv.slice(2);
16
+
17
+ if (args.length === 0) {
18
+ console.error('Usage: dirac <file.di|file.bk>');
19
+ console.error('');
20
+ console.error('File formats:');
21
+ console.error(' .di XML notation (verbose)');
22
+ console.error(' .bk Bra-ket notation (compact)');
23
+ console.error('');
24
+ console.error('Options:');
25
+ console.error(' --debug Enable debug output');
26
+ console.error(' --emit-xml Output intermediate XML (for .bk files)');
27
+ console.error(' --model <name> Set default LLM model');
28
+ console.error(' --max-llm <n> Maximum LLM calls (default: 100)');
29
+ console.error(' --max-depth <n> Maximum recursion depth (default: 50)');
30
+ process.exit(1);
31
+ }
32
+
33
+ // Parse options
34
+ const config: any = {};
35
+ let filePath: string | undefined;
36
+ let emitXml = false;
37
+ let configFile: string | undefined;
38
+
39
+ for (let i = 0; i < args.length; i++) {
40
+ const arg = args[i];
41
+
42
+ if (arg === '--debug') {
43
+ config.debug = true;
44
+ } else if (arg === '--emit-xml') {
45
+ emitXml = true;
46
+ } else if (arg === '--model' && i + 1 < args.length) {
47
+ config.model = args[++i];
48
+ } else if (arg === '--provider' && i + 1 < args.length) {
49
+ config.llmProvider = args[++i];
50
+ } else if ((arg === '-f' || arg === '--config') && i + 1 < args.length) {
51
+ configFile = args[++i];
52
+ } else if (arg === '--max-llm' && i + 1 < args.length) {
53
+ config.maxLLMCalls = parseInt(args[++i], 10);
54
+ } else if (arg === '--max-depth' && i + 1 < args.length) {
55
+ config.maxDepth = parseInt(args[++i], 10);
56
+ } else if (!arg.startsWith('--')) {
57
+ filePath = arg;
58
+ }
59
+ }
60
+
61
+ // Load config from YAML file if specified
62
+ if (configFile) {
63
+ try {
64
+ const loadedConfig = yaml.load(fs.readFileSync(resolve(configFile), 'utf8')) || {};
65
+ Object.assign(config, loadedConfig);
66
+ } catch (err) {
67
+ console.error('Failed to load config file:', err);
68
+ process.exit(1);
69
+ }
70
+ }
71
+
72
+ if (!filePath) {
73
+ console.error('Error: No input file specified');
74
+ process.exit(1);
75
+ }
76
+
77
+ try {
78
+ const fullPath = resolve(process.cwd(), filePath);
79
+ let source = fs.readFileSync(fullPath, 'utf-8');
80
+ const ext = extname(fullPath);
81
+
82
+ // Convert bra-ket notation to XML if needed
83
+ if (ext === '.bk') {
84
+ if (config.debug) {
85
+ console.error(`[Dirac] Compiling bra-ket notation to XML`);
86
+ }
87
+
88
+ const braketParser = new BraKetParser();
89
+ const xml = braketParser.parse(source);
90
+
91
+ if (emitXml) {
92
+ console.log(xml);
93
+ return;
94
+ }
95
+
96
+ source = xml;
97
+ }
98
+
99
+ if (config.debug) {
100
+ console.error(`[Dirac] Executing ${fullPath}`);
101
+ }
102
+
103
+ // Pass file path to config for import resolution
104
+ config.filePath = fullPath;
105
+
106
+ const result = await execute(source, config);
107
+ process.stdout.write(result);
108
+
109
+ } catch (error) {
110
+ console.error('Error:', error instanceof Error ? error.message : String(error));
111
+ if (config.debug && error instanceof Error && error.stack) {
112
+ console.error(error.stack);
113
+ }
114
+ process.exit(1);
115
+ }
116
+ }
117
+
118
+ main();