dirac-lang 0.1.37 → 0.1.38

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.
@@ -9,6 +9,7 @@ import {
9
9
  cleanToBoundary,
10
10
  emit,
11
11
  flushCurrentException,
12
+ getAvailableSubroutines,
12
13
  getCurrentExceptions,
13
14
  getCurrentParameters,
14
15
  getParentSubroutine,
@@ -447,12 +448,12 @@ async function executeIf(session, element) {
447
448
  const condition = await evaluatePredicate(session, conditionElement);
448
449
  if (condition) {
449
450
  if (thenElement) {
450
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
451
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-FJ52HYZC.js");
451
452
  await integrateChildren2(session, thenElement);
452
453
  }
453
454
  } else {
454
455
  if (elseElement) {
455
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
456
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-FJ52HYZC.js");
456
457
  await integrateChildren2(session, elseElement);
457
458
  }
458
459
  }
@@ -465,7 +466,7 @@ async function evaluatePredicate(session, predicateElement) {
465
466
  return await evaluateCondition(session, predicateElement);
466
467
  }
467
468
  const outputLengthBefore = session.output.length;
468
- const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
469
+ const { integrate: integrate2 } = await import("./interpreter-FJ52HYZC.js");
469
470
  await integrate2(session, predicateElement);
470
471
  const newOutputChunks = session.output.slice(outputLengthBefore);
471
472
  const result = newOutputChunks.join("").trim();
@@ -488,11 +489,11 @@ async function evaluateCondition(session, condElement) {
488
489
  }
489
490
  const outputLengthBefore = session.output.length;
490
491
  const args = [];
491
- const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
492
+ const { integrate: integrate2 } = await import("./interpreter-FJ52HYZC.js");
492
493
  for (const child of condElement.children) {
493
494
  if (child.tag.toLowerCase() === "arg") {
494
495
  const argOutputStart = session.output.length;
495
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
496
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-FJ52HYZC.js");
496
497
  await integrateChildren2(session, child);
497
498
  const newChunks = session.output.slice(argOutputStart);
498
499
  const argValue = newChunks.join("");
@@ -602,8 +603,8 @@ async function executeLLM(session, element) {
602
603
  console.error("[LLM] Full prompt sent to LLM (noextra):\n" + prompt + "\n");
603
604
  }
604
605
  } else {
605
- const { getAvailableSubroutines } = await import("./session-UBATJEND.js");
606
- const subroutines = getAvailableSubroutines(session);
606
+ const { getAvailableSubroutines: getAvailableSubroutines2 } = await import("./session-UBATJEND.js");
607
+ const subroutines = getAvailableSubroutines2(session);
607
608
  if (session.debug) {
608
609
  console.error(
609
610
  "[LLM] Subroutines available at prompt composition:",
@@ -1284,8 +1285,8 @@ async function getBestTagMatch(candidate, allowed) {
1284
1285
  return { tag: allowed[bestIdx], score: bestScore };
1285
1286
  }
1286
1287
  async function executeTagCheck(session, element) {
1287
- const { getAvailableSubroutines } = await import("./session-UBATJEND.js");
1288
- const subroutines = getAvailableSubroutines(session);
1288
+ const { getAvailableSubroutines: getAvailableSubroutines2 } = await import("./session-UBATJEND.js");
1289
+ const subroutines = getAvailableSubroutines2(session);
1289
1290
  const allowed = new Set(subroutines.map((s) => s.name));
1290
1291
  console.error("[tag-check] Allowed subroutines:", Array.from(allowed));
1291
1292
  const autocorrect = element.attributes?.autocorrect === "true";
@@ -1377,7 +1378,7 @@ async function executeTagCheck(session, element) {
1377
1378
  const executeTag = correctedTag || tagName;
1378
1379
  console.error(`[tag-check] Executing <${executeTag}/> as all checks passed and execute=true.`);
1379
1380
  const elementToExecute = correctedTag ? { ...child, tag: correctedTag } : child;
1380
- const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
1381
+ const { integrate: integrate2 } = await import("./interpreter-FJ52HYZC.js");
1381
1382
  await integrate2(session, elementToExecute);
1382
1383
  }
1383
1384
  }
@@ -1386,7 +1387,7 @@ async function executeTagCheck(session, element) {
1386
1387
  // src/tags/throw.ts
1387
1388
  async function executeThrow(session, element) {
1388
1389
  const exceptionName = element.attributes?.name || "exception";
1389
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1390
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-FJ52HYZC.js");
1390
1391
  const exceptionDom = {
1391
1392
  tag: "exception-content",
1392
1393
  attributes: { name: exceptionName },
@@ -1399,7 +1400,7 @@ async function executeThrow(session, element) {
1399
1400
  // src/tags/try.ts
1400
1401
  async function executeTry(session, element) {
1401
1402
  setExceptionBoundary(session);
1402
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1403
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-FJ52HYZC.js");
1403
1404
  await integrateChildren2(session, element);
1404
1405
  unsetExceptionBoundary(session);
1405
1406
  }
@@ -1409,7 +1410,7 @@ async function executeCatch(session, element) {
1409
1410
  const exceptionName = element.attributes?.name || "exception";
1410
1411
  const caughtCount = lookupException(session, exceptionName);
1411
1412
  if (caughtCount > 0) {
1412
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1413
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-FJ52HYZC.js");
1413
1414
  await integrateChildren2(session, element);
1414
1415
  }
1415
1416
  flushCurrentException(session);
@@ -1418,7 +1419,7 @@ async function executeCatch(session, element) {
1418
1419
  // src/tags/exception.ts
1419
1420
  async function executeException(session, element) {
1420
1421
  const exceptions = getCurrentExceptions(session);
1421
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1422
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-FJ52HYZC.js");
1422
1423
  for (const exceptionDom of exceptions) {
1423
1424
  await integrateChildren2(session, exceptionDom);
1424
1425
  }
@@ -1538,6 +1539,80 @@ function escapeXml2(text) {
1538
1539
  return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
1539
1540
  }
1540
1541
 
1542
+ // src/tags/list-subroutines.ts
1543
+ async function executeListSubroutines(session, element) {
1544
+ const format = element.attributes.format || "text";
1545
+ const outputVar = element.attributes.output;
1546
+ const subroutines = getAvailableSubroutines(session);
1547
+ let output = "";
1548
+ switch (format) {
1549
+ case "braket":
1550
+ output = formatAsBraKet(subroutines);
1551
+ break;
1552
+ case "xml":
1553
+ output = formatAsXml(subroutines);
1554
+ break;
1555
+ case "json":
1556
+ output = JSON.stringify(subroutines, null, 2);
1557
+ break;
1558
+ case "text":
1559
+ default:
1560
+ output = formatAsText(subroutines);
1561
+ break;
1562
+ }
1563
+ if (outputVar) {
1564
+ setVariable(session, outputVar, output, false);
1565
+ } else {
1566
+ emit(session, output);
1567
+ }
1568
+ }
1569
+ function formatAsBraKet(subroutines) {
1570
+ const lines = ["Available subroutines:\n"];
1571
+ for (const sub of subroutines) {
1572
+ const params = sub.parameters?.map(
1573
+ (p) => `${p.name}=${p.type || "any"}`
1574
+ ).join(" ") || "";
1575
+ const braLine = params ? `<${sub.name} ${params}|` : `<${sub.name}|`;
1576
+ lines.push(braLine);
1577
+ if (sub.description) {
1578
+ lines.push(` ${sub.description}`);
1579
+ }
1580
+ lines.push("");
1581
+ }
1582
+ return lines.join("\n");
1583
+ }
1584
+ function formatAsXml(subroutines) {
1585
+ const lines = ["Available subroutines:\n"];
1586
+ for (const sub of subroutines) {
1587
+ const params = sub.parameters?.map(
1588
+ (p) => `param-${p.name}="${p.type || "any"}"`
1589
+ ).join(" ") || "";
1590
+ const xmlLine = params ? `<subroutine name="${sub.name}" ${params}/>` : `<subroutine name="${sub.name}"/>`;
1591
+ lines.push(xmlLine);
1592
+ if (sub.description) {
1593
+ lines.push(` <!-- ${sub.description} -->`);
1594
+ }
1595
+ lines.push("");
1596
+ }
1597
+ return lines.join("\n");
1598
+ }
1599
+ function formatAsText(subroutines) {
1600
+ const lines = ["Available subroutines:\n"];
1601
+ for (const sub of subroutines) {
1602
+ lines.push(`${sub.name}(`);
1603
+ if (sub.parameters && sub.parameters.length > 0) {
1604
+ for (const param of sub.parameters) {
1605
+ const required = param.required ? " [required]" : "";
1606
+ const desc = param.description ? ` - ${param.description}` : "";
1607
+ lines.push(` ${param.name}: ${param.type || "any"}${required}${desc}`);
1608
+ }
1609
+ }
1610
+ lines.push(`)${sub.description ? " - " + sub.description : ""}
1611
+ `);
1612
+ }
1613
+ return lines.join("\n");
1614
+ }
1615
+
1541
1616
  // src/tags/foreach.ts
1542
1617
  import { XMLParser } from "fast-xml-parser";
1543
1618
  async function executeForeach(session, element) {
@@ -1558,7 +1633,7 @@ async function executeForeach(session, element) {
1558
1633
  const parser2 = new DiracParser2();
1559
1634
  try {
1560
1635
  const fromElement = parser2.parse(fromAttr);
1561
- const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
1636
+ const { integrate: integrate2 } = await import("./interpreter-FJ52HYZC.js");
1562
1637
  await integrate2(session, fromElement);
1563
1638
  } catch (e) {
1564
1639
  session.output = savedOutput;
@@ -1902,6 +1977,9 @@ async function integrate(session, element) {
1902
1977
  case "available-subroutines":
1903
1978
  await executeAvailableSubroutines(session, element);
1904
1979
  break;
1980
+ case "list-subroutines":
1981
+ await executeListSubroutines(session, element);
1982
+ break;
1905
1983
  case "foreach":
1906
1984
  await executeForeach(session, element);
1907
1985
  break;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  integrate
3
- } from "./chunk-MVMLG434.js";
3
+ } from "./chunk-DGWZB2VC.js";
4
4
  import {
5
5
  DiracParser
6
6
  } from "./chunk-HRHAMPOB.js";
@@ -277,15 +277,37 @@ var BraKetParser = class {
277
277
  }
278
278
  return `"${value}"`;
279
279
  }
280
+ /**
281
+ * Escape XML special characters in text content
282
+ */
283
+ escapeXml(text) {
284
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
285
+ }
280
286
  /**
281
287
  * Convert inline kets within text content
282
288
  * Example: "Hello |variable name=x> world" → "Hello <variable name="x"/> world"
289
+ * Example: "if x < 10 and y > 5" → "if x &lt; 10 and y &gt; 5"
283
290
  */
284
291
  convertInlineKets(text) {
285
- return text.replace(/\|([a-zA-Z_][a-zA-Z0-9_-]*)\s*([^>]*?)>/g, (match, tag, attrs) => {
292
+ const parts = [];
293
+ let lastIndex = 0;
294
+ const ketRegex = /\|([a-zA-Z_][a-zA-Z0-9_-]*)\s*([^>]*?)>/g;
295
+ let match;
296
+ while ((match = ketRegex.exec(text)) !== null) {
297
+ if (match.index > lastIndex) {
298
+ const beforeText = text.substring(lastIndex, match.index);
299
+ parts.push(this.escapeXml(beforeText));
300
+ }
301
+ const [, tag, attrs] = match;
286
302
  const attrStr = attrs.trim() ? ` ${this.convertAttributes(attrs.trim())}` : "";
287
- return `<${tag}${attrStr}/>`;
288
- });
303
+ parts.push(`<${tag}${attrStr}/>`);
304
+ lastIndex = ketRegex.lastIndex;
305
+ }
306
+ if (lastIndex < text.length) {
307
+ const remainingText = text.substring(lastIndex);
308
+ parts.push(this.escapeXml(remainingText));
309
+ }
310
+ return parts.join("");
289
311
  }
290
312
  };
291
313
 
package/dist/cli.js CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  BraKetParser
4
- } from "./chunk-E66LTAOE.js";
4
+ } from "./chunk-UEFKQRYN.js";
5
5
  import {
6
6
  execute
7
- } from "./chunk-4J2UJXJZ.js";
8
- import "./chunk-MVMLG434.js";
7
+ } from "./chunk-MEATHM35.js";
8
+ import "./chunk-DGWZB2VC.js";
9
9
  import "./chunk-HRHAMPOB.js";
10
10
  import "./chunk-4QLTSCDG.js";
11
11
  import "./chunk-GLXVY235.js";
@@ -46,6 +46,7 @@ var package_default = {
46
46
  license: "MIT",
47
47
  dependencies: {
48
48
  "@anthropic-ai/sdk": "^0.30.1",
49
+ "dirac-stdlib": "^0.1.0",
49
50
  dotenv: "^17.2.3",
50
51
  "fast-xml-parser": "^4.3.5",
51
52
  "js-yaml": "^4.1.1",
@@ -95,7 +96,7 @@ async function main() {
95
96
  process.exit(0);
96
97
  }
97
98
  if (args[0] === "shell") {
98
- const { DiracShell } = await import("./shell-B5RBRWK2.js");
99
+ const { DiracShell } = await import("./shell-HKCK2KIM.js");
99
100
  const shellConfig = { debug: false };
100
101
  for (let i = 1; i < args.length; i++) {
101
102
  const arg = args[i];
@@ -108,7 +109,8 @@ async function main() {
108
109
  Object.assign(shellConfig, {
109
110
  llmProvider: configData.llmProvider,
110
111
  llmModel: configData.llmModel,
111
- customLLMUrl: configData.customLLMUrl
112
+ customLLMUrl: configData.customLLMUrl,
113
+ initScript: configData.initScript
112
114
  });
113
115
  }
114
116
  }
@@ -121,12 +123,13 @@ async function main() {
121
123
  shellConfig.llmProvider = shellConfig.llmProvider || configData.llmProvider;
122
124
  shellConfig.llmModel = shellConfig.llmModel || configData.llmModel;
123
125
  shellConfig.customLLMUrl = shellConfig.customLLMUrl || configData.customLLMUrl;
126
+ shellConfig.initScript = shellConfig.initScript || configData.initScript;
124
127
  } catch (err) {
125
128
  }
126
129
  }
127
130
  }
128
131
  const shell = new DiracShell(shellConfig);
129
- shell.start();
132
+ await shell.start();
130
133
  return;
131
134
  }
132
135
  if (args.length === 0) {
package/dist/index.d.ts CHANGED
@@ -95,6 +95,7 @@ interface DiracConfig {
95
95
  llmProvider?: string;
96
96
  llmModel?: string;
97
97
  customLLMUrl?: string;
98
+ initScript?: string;
98
99
  }
99
100
 
100
101
  /**
package/dist/index.js CHANGED
@@ -2,10 +2,10 @@ import {
2
2
  createLLMAdapter,
3
3
  execute,
4
4
  executeUserCommand
5
- } from "./chunk-4J2UJXJZ.js";
5
+ } from "./chunk-MEATHM35.js";
6
6
  import {
7
7
  integrate
8
- } from "./chunk-MVMLG434.js";
8
+ } from "./chunk-DGWZB2VC.js";
9
9
  import {
10
10
  DiracParser
11
11
  } from "./chunk-HRHAMPOB.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  integrate,
3
3
  integrateChildren
4
- } from "./chunk-MVMLG434.js";
4
+ } from "./chunk-DGWZB2VC.js";
5
5
  import "./chunk-HRHAMPOB.js";
6
6
  import "./chunk-4QLTSCDG.js";
7
7
  import "./chunk-GLXVY235.js";
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  BraKetParser
4
- } from "./chunk-E66LTAOE.js";
4
+ } from "./chunk-UEFKQRYN.js";
5
5
  import {
6
6
  integrate
7
- } from "./chunk-MVMLG434.js";
7
+ } from "./chunk-DGWZB2VC.js";
8
8
  import {
9
9
  DiracParser
10
10
  } from "./chunk-HRHAMPOB.js";
@@ -318,7 +318,7 @@ Examples:
318
318
  });
319
319
  });
320
320
  }
321
- start() {
321
+ async start() {
322
322
  console.log("Dirac Shell v0.1.0");
323
323
  console.log("Type :help for commands, :exit to quit\n");
324
324
  if (this.config.llmProvider) {
@@ -327,8 +327,31 @@ Examples:
327
327
  } else {
328
328
  console.log("Warning: No LLM provider configured. Set LLM_PROVIDER environment variable.\n");
329
329
  }
330
+ if (this.config.initScript) {
331
+ await this.runInitScript(this.config.initScript);
332
+ }
330
333
  this.rl.prompt();
331
334
  }
335
+ async runInitScript(scriptPath) {
336
+ try {
337
+ const resolvedPath = path.isAbsolute(scriptPath) ? scriptPath : path.join(process.cwd(), scriptPath);
338
+ if (!fs.existsSync(resolvedPath)) {
339
+ console.log(`Init script not found: ${scriptPath}
340
+ `);
341
+ return;
342
+ }
343
+ console.log(`Loading init script: ${scriptPath}`);
344
+ const scriptContent = fs.readFileSync(resolvedPath, "utf-8");
345
+ const xml = this.braketParser.parse(scriptContent);
346
+ const ast = this.xmlParser.parse(xml);
347
+ await integrate(this.session, ast);
348
+ console.log(`Init script loaded.
349
+ `);
350
+ } catch (err) {
351
+ console.error(`Error loading init script: ${err instanceof Error ? err.message : String(err)}
352
+ `);
353
+ }
354
+ }
332
355
  };
333
356
  async function main() {
334
357
  let config = {
@@ -342,7 +365,8 @@ async function main() {
342
365
  ...config,
343
366
  llmProvider: configData.llmProvider || process.env.LLM_PROVIDER,
344
367
  llmModel: configData.llmModel || process.env.LLM_MODEL,
345
- customLLMUrl: configData.customLLMUrl || process.env.CUSTOM_LLM_URL
368
+ customLLMUrl: configData.customLLMUrl || process.env.CUSTOM_LLM_URL,
369
+ initScript: configData.initScript
346
370
  };
347
371
  } catch (err) {
348
372
  console.error("Warning: Could not load config.yml");
@@ -353,7 +377,7 @@ async function main() {
353
377
  config.customLLMUrl = process.env.CUSTOM_LLM_URL;
354
378
  }
355
379
  const shell = new DiracShell(config);
356
- shell.start();
380
+ await shell.start();
357
381
  }
358
382
  if (import.meta.url === `file://${process.argv[1]}`) {
359
383
  main();
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  integrate
3
- } from "./chunk-MVMLG434.js";
3
+ } from "./chunk-DGWZB2VC.js";
4
4
  import {
5
5
  DiracParser
6
6
  } from "./chunk-HRHAMPOB.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dirac-lang",
3
- "version": "0.1.37",
3
+ "version": "0.1.38",
4
4
  "description": "LLM-Augmented Declarative Execution",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -30,6 +30,7 @@
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
32
  "@anthropic-ai/sdk": "^0.30.1",
33
+ "dirac-stdlib": "^0.1.0",
33
34
  "dotenv": "^17.2.3",
34
35
  "fast-xml-parser": "^4.3.5",
35
36
  "js-yaml": "^4.1.1",