flavor-code 1.3.23 → 1.4.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/README.md +437 -434
  2. package/README.zh-CN.md +434 -431
  3. package/dist/{app-NTEUPJKJ.js → app-C5ZQUOKJ.js} +43 -155
  4. package/dist/astgraph/grammars.mjs +68 -68
  5. package/dist/astgraph/vendor/tree-sitter.js +3980 -3980
  6. package/dist/astgraph/vendor/zod/LICENSE +21 -21
  7. package/dist/astgraph/vendor/zod/index.js +4 -4
  8. package/dist/astgraph/vendor/zod/locales/index.js +1 -1
  9. package/dist/astgraph/vendor/zod/locales/package.json +7 -7
  10. package/dist/astgraph/vendor/zod/package.json +135 -135
  11. package/dist/astgraph/vendor/zod/v4/classic/checks.js +1 -1
  12. package/dist/astgraph/vendor/zod/v4/classic/coerce.js +17 -17
  13. package/dist/astgraph/vendor/zod/v4/classic/compat.js +31 -31
  14. package/dist/astgraph/vendor/zod/v4/classic/errors.js +48 -48
  15. package/dist/astgraph/vendor/zod/v4/classic/external.js +20 -20
  16. package/dist/astgraph/vendor/zod/v4/classic/from-json-schema.js +599 -599
  17. package/dist/astgraph/vendor/zod/v4/classic/index.js +4 -4
  18. package/dist/astgraph/vendor/zod/v4/classic/iso.js +30 -30
  19. package/dist/astgraph/vendor/zod/v4/classic/package.json +7 -7
  20. package/dist/astgraph/vendor/zod/v4/classic/parse.js +15 -15
  21. package/dist/astgraph/vendor/zod/v4/classic/schemas.js +1395 -1395
  22. package/dist/astgraph/vendor/zod/v4/core/api.js +1087 -1087
  23. package/dist/astgraph/vendor/zod/v4/core/checks.js +575 -575
  24. package/dist/astgraph/vendor/zod/v4/core/core.js +78 -78
  25. package/dist/astgraph/vendor/zod/v4/core/doc.js +35 -35
  26. package/dist/astgraph/vendor/zod/v4/core/errors.js +185 -185
  27. package/dist/astgraph/vendor/zod/v4/core/index.js +16 -16
  28. package/dist/astgraph/vendor/zod/v4/core/json-schema-generator.js +95 -95
  29. package/dist/astgraph/vendor/zod/v4/core/json-schema-processors.js +601 -601
  30. package/dist/astgraph/vendor/zod/v4/core/json-schema.js +1 -1
  31. package/dist/astgraph/vendor/zod/v4/core/package.json +7 -7
  32. package/dist/astgraph/vendor/zod/v4/core/parse.js +93 -93
  33. package/dist/astgraph/vendor/zod/v4/core/regexes.js +139 -139
  34. package/dist/astgraph/vendor/zod/v4/core/registries.js +51 -51
  35. package/dist/astgraph/vendor/zod/v4/core/schemas.js +2239 -2239
  36. package/dist/astgraph/vendor/zod/v4/core/standard-schema.js +1 -1
  37. package/dist/astgraph/vendor/zod/v4/core/to-json-schema.js +448 -448
  38. package/dist/astgraph/vendor/zod/v4/core/util.js +674 -674
  39. package/dist/astgraph/vendor/zod/v4/core/versions.js +5 -5
  40. package/dist/astgraph/vendor/zod/v4/index.js +3 -3
  41. package/dist/astgraph/vendor/zod/v4/locales/ar.js +106 -106
  42. package/dist/astgraph/vendor/zod/v4/locales/az.js +105 -105
  43. package/dist/astgraph/vendor/zod/v4/locales/be.js +156 -156
  44. package/dist/astgraph/vendor/zod/v4/locales/bg.js +120 -120
  45. package/dist/astgraph/vendor/zod/v4/locales/ca.js +107 -107
  46. package/dist/astgraph/vendor/zod/v4/locales/cs.js +111 -111
  47. package/dist/astgraph/vendor/zod/v4/locales/da.js +115 -115
  48. package/dist/astgraph/vendor/zod/v4/locales/de.js +108 -108
  49. package/dist/astgraph/vendor/zod/v4/locales/el.js +109 -109
  50. package/dist/astgraph/vendor/zod/v4/locales/en.js +113 -113
  51. package/dist/astgraph/vendor/zod/v4/locales/eo.js +109 -109
  52. package/dist/astgraph/vendor/zod/v4/locales/es.js +132 -132
  53. package/dist/astgraph/vendor/zod/v4/locales/fa.js +114 -114
  54. package/dist/astgraph/vendor/zod/v4/locales/fi.js +112 -112
  55. package/dist/astgraph/vendor/zod/v4/locales/fr-CA.js +107 -107
  56. package/dist/astgraph/vendor/zod/v4/locales/fr.js +125 -125
  57. package/dist/astgraph/vendor/zod/v4/locales/he.js +214 -214
  58. package/dist/astgraph/vendor/zod/v4/locales/hr.js +122 -122
  59. package/dist/astgraph/vendor/zod/v4/locales/hu.js +108 -108
  60. package/dist/astgraph/vendor/zod/v4/locales/hy.js +147 -147
  61. package/dist/astgraph/vendor/zod/v4/locales/id.js +106 -106
  62. package/dist/astgraph/vendor/zod/v4/locales/index.js +52 -52
  63. package/dist/astgraph/vendor/zod/v4/locales/is.js +109 -109
  64. package/dist/astgraph/vendor/zod/v4/locales/it.js +108 -108
  65. package/dist/astgraph/vendor/zod/v4/locales/ja.js +107 -107
  66. package/dist/astgraph/vendor/zod/v4/locales/ka.js +112 -112
  67. package/dist/astgraph/vendor/zod/v4/locales/kh.js +5 -5
  68. package/dist/astgraph/vendor/zod/v4/locales/km.js +110 -110
  69. package/dist/astgraph/vendor/zod/v4/locales/ko.js +111 -111
  70. package/dist/astgraph/vendor/zod/v4/locales/lt.js +203 -203
  71. package/dist/astgraph/vendor/zod/v4/locales/mk.js +109 -109
  72. package/dist/astgraph/vendor/zod/v4/locales/ms.js +107 -107
  73. package/dist/astgraph/vendor/zod/v4/locales/nl.js +110 -110
  74. package/dist/astgraph/vendor/zod/v4/locales/no.js +108 -108
  75. package/dist/astgraph/vendor/zod/v4/locales/ota.js +109 -109
  76. package/dist/astgraph/vendor/zod/v4/locales/package.json +7 -7
  77. package/dist/astgraph/vendor/zod/v4/locales/pl.js +109 -109
  78. package/dist/astgraph/vendor/zod/v4/locales/ps.js +114 -114
  79. package/dist/astgraph/vendor/zod/v4/locales/pt.js +108 -108
  80. package/dist/astgraph/vendor/zod/v4/locales/ro.js +119 -119
  81. package/dist/astgraph/vendor/zod/v4/locales/ru.js +156 -156
  82. package/dist/astgraph/vendor/zod/v4/locales/sl.js +109 -109
  83. package/dist/astgraph/vendor/zod/v4/locales/sv.js +110 -110
  84. package/dist/astgraph/vendor/zod/v4/locales/ta.js +110 -110
  85. package/dist/astgraph/vendor/zod/v4/locales/th.js +110 -110
  86. package/dist/astgraph/vendor/zod/v4/locales/tr.js +105 -105
  87. package/dist/astgraph/vendor/zod/v4/locales/ua.js +5 -5
  88. package/dist/astgraph/vendor/zod/v4/locales/uk.js +108 -108
  89. package/dist/astgraph/vendor/zod/v4/locales/ur.js +110 -110
  90. package/dist/astgraph/vendor/zod/v4/locales/uz.js +110 -110
  91. package/dist/astgraph/vendor/zod/v4/locales/vi.js +108 -108
  92. package/dist/astgraph/vendor/zod/v4/locales/yo.js +107 -107
  93. package/dist/astgraph/vendor/zod/v4/locales/zh-CN.js +109 -109
  94. package/dist/astgraph/vendor/zod/v4/locales/zh-TW.js +107 -107
  95. package/dist/astgraph/vendor/zod/v4/mini/checks.js +1 -1
  96. package/dist/astgraph/vendor/zod/v4/mini/coerce.js +22 -22
  97. package/dist/astgraph/vendor/zod/v4/mini/external.js +14 -14
  98. package/dist/astgraph/vendor/zod/v4/mini/index.js +3 -3
  99. package/dist/astgraph/vendor/zod/v4/mini/iso.js +34 -34
  100. package/dist/astgraph/vendor/zod/v4/mini/package.json +7 -7
  101. package/dist/astgraph/vendor/zod/v4/mini/parse.js +1 -1
  102. package/dist/astgraph/vendor/zod/v4/mini/schemas.js +961 -961
  103. package/dist/astgraph/vendor/zod/v4/package.json +7 -7
  104. package/dist/{chunk-7OICVL2M.js → chunk-2BF7CVWZ.js} +0 -42
  105. package/dist/{chunk-RQTYHUMK.js → chunk-3WCDZ6ZL.js} +15 -0
  106. package/dist/{chunk-Z62P4LL7.js → chunk-6MNOZ2TX.js} +8962 -8422
  107. package/dist/{chunk-VEUSROUQ.js → chunk-FOTMP4ZS.js} +1 -1
  108. package/dist/{chunk-SUSLIHXO.js → chunk-ODRWR4KK.js} +1 -1
  109. package/dist/{chunk-RPFWB45Y.js → chunk-URUVFECH.js} +1 -1
  110. package/dist/{claude-ink-6CEG2XZZ.js → claude-ink-LP32NSX5.js} +2 -3
  111. package/dist/cli-main.js +27 -11
  112. package/dist/config/load.d.ts +2 -0
  113. package/dist/desktop/main.js +1092 -541
  114. package/dist/desktop-renderer/assets/index-Bk6FYhyh.css +1 -0
  115. package/dist/desktop-renderer/assets/{index-D75ozXnZ.js → index-CqRIr-hz.js} +3 -3
  116. package/dist/desktop-renderer/assets/{interactive-terminal-CTFo4fK9.js → interactive-terminal-C4o7MNPO.js} +1 -1
  117. package/dist/desktop-renderer/index.html +2 -2
  118. package/dist/doctor.d.ts +38 -0
  119. package/dist/execution/docker.d.ts +5 -1
  120. package/dist/execution/types.d.ts +11 -0
  121. package/dist/jobs/registry.d.ts +1 -0
  122. package/dist/{load-4FWNXE6F.js → load-V2UIYYQI.js} +1 -1
  123. package/dist/sdk/index.js +4 -4
  124. package/dist/tools/shell.d.ts +17 -12
  125. package/dist/tools/types.d.ts +1 -0
  126. package/dist/ui/commands.d.ts +1 -1
  127. package/dist/ui/session.d.ts +1 -0
  128. package/dist/update/check.d.ts +20 -0
  129. package/dist/utils/semver.d.ts +3 -0
  130. package/dist/utils/spawn-executable.d.ts +2 -0
  131. package/package.json +1 -1
  132. package//346/212/200/346/234/257/346/226/271/346/241/210/346/212/245/345/221/212.md +5848 -5848
  133. package/dist/chunk-OTYFL4AG.js +0 -16
  134. package/dist/desktop-renderer/assets/index-ZZsUx916.css +0 -1
@@ -1,13 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire } from "node:module"; const require = createRequire(import.meta.url);
3
3
  import {
4
- checkForUpdate,
5
4
  statusPresentation
6
- } from "./chunk-7OICVL2M.js";
5
+ } from "./chunk-2BF7CVWZ.js";
7
6
  import {
8
7
  COMMAND_DESCRIPTIONS,
9
8
  MVP_COMMANDS,
10
9
  charWidth,
10
+ checkForUpdate,
11
11
  createGlobTool,
12
12
  createProductionRuntime,
13
13
  createTranscriptState,
@@ -19,9 +19,9 @@ import {
19
19
  thinkingWindow,
20
20
  transcriptReducer,
21
21
  withTimeout
22
- } from "./chunk-Z62P4LL7.js";
22
+ } from "./chunk-6MNOZ2TX.js";
23
23
  import "./chunk-CIVPKHF4.js";
24
- import "./chunk-RPFWB45Y.js";
24
+ import "./chunk-URUVFECH.js";
25
25
  import {
26
26
  Box_default,
27
27
  ClockContext,
@@ -38,11 +38,10 @@ import {
38
38
  useStdout,
39
39
  use_app_default,
40
40
  use_input_default
41
- } from "./chunk-SUSLIHXO.js";
42
- import "./chunk-OTYFL4AG.js";
41
+ } from "./chunk-ODRWR4KK.js";
43
42
  import {
44
43
  execFileNoThrow
45
- } from "./chunk-RQTYHUMK.js";
44
+ } from "./chunk-3WCDZ6ZL.js";
46
45
  import "./chunk-S32JRDZK.js";
47
46
 
48
47
  // src/ui/app.tsx
@@ -1523,160 +1522,45 @@ function isBridgeMethod(value, method) {
1523
1522
  && typeof value[method] === "function";
1524
1523
  }
1525
1524
 
1526
- function safeDataForType(pasteboard, type) {
1527
- try {
1528
- return isBridgeMethod(pasteboard, "dataForType")
1529
- ? pasteboard.dataForType(type)
1530
- : null;
1531
- } catch (error) {
1532
- return null;
1533
- }
1534
- }
1535
-
1536
- function pngDataFromTiff(tiff) {
1537
- if (!isBridgeMethod(tiff, "base64EncodedStringWithOptions")) {
1538
- return null;
1539
- }
1540
- try {
1541
- const representation = $.NSBitmapImageRep.imageRepWithData(tiff);
1542
- if (!isBridgeMethod(representation, "representationUsingTypeProperties")) {
1543
- return null;
1544
- }
1545
- const converted = representation.representationUsingTypeProperties(
1546
- $.NSBitmapImageFileTypePNG,
1547
- $({})
1548
- );
1549
- return isBridgeMethod(converted, "base64EncodedStringWithOptions")
1550
- ? converted
1551
- : null;
1552
- } catch (error) {
1553
- return null;
1554
- }
1555
- }
1556
-
1557
- function base64FromData(data) {
1558
- if (!isBridgeMethod(data, "base64EncodedStringWithOptions")) {
1559
- return null;
1560
- }
1561
- try {
1562
- const encoded = data.base64EncodedStringWithOptions(0);
1563
- if (!encoded) {
1564
- return null;
1565
- }
1566
- const unwrapped = ObjC.unwrap(encoded);
1567
- return typeof unwrapped === "string" && unwrapped.length > 0
1568
- ? unwrapped
1569
- : null;
1570
- } catch (error) {
1571
- return null;
1572
- }
1573
- }
1574
-
1575
- function firstReadableObject(pasteboard, objectClass) {
1576
- try {
1577
- if (!isBridgeMethod(pasteboard, "readObjectsForClassesOptions")) {
1578
- return null;
1579
- }
1580
- const classes = $.NSArray.arrayWithObject(objectClass);
1581
- const objects = pasteboard.readObjectsForClassesOptions(classes, $({}));
1582
- return objects === null || objects === undefined ? null : objects.firstObject;
1583
- } catch (error) {
1584
- return null;
1585
- }
1586
- }
1587
-
1588
- function imageFromWholePasteboard(pasteboard) {
1589
- try {
1590
- const image = $.NSImage.alloc.initWithPasteboard(pasteboard);
1591
- return image === null || image === undefined ? null : image;
1592
- } catch (error) {
1593
- return null;
1594
- }
1595
- }
1596
-
1597
- function imageFromFileUrl(pasteboard) {
1598
- try {
1599
- const url = firstReadableObject(pasteboard, $.NSURL);
1600
- if (url === null || url === undefined || !url.isFileURL) {
1601
- return null;
1602
- }
1603
- const image = $.NSImage.alloc.initWithContentsOfURL(url);
1604
- return image === null || image === undefined ? null : image;
1605
- } catch (error) {
1606
- return null;
1607
- }
1608
- }
1609
-
1610
- function base64FromImage(image) {
1611
- try {
1612
- return image === null || image === undefined
1613
- ? null
1614
- : base64FromData(pngDataFromTiff(image.TIFFRepresentation));
1615
- } catch (error) {
1616
- return null;
1617
- }
1618
- }
1619
-
1620
- function pasteboardTypeNames(pasteboard) {
1621
- try {
1622
- const types = pasteboard.types;
1623
- if (!isBridgeMethod(types, "objectAtIndex")) {
1624
- return [];
1625
- }
1626
- const count = Number(types.count);
1627
- const names = [];
1628
- for (let index = 0; index < count; index += 1) {
1629
- const name = ObjC.unwrap(types.objectAtIndex(index));
1630
- if (typeof name === "string") {
1631
- names.push(name);
1525
+ try {
1526
+ const pasteboard = $.NSPasteboard.generalPasteboard;
1527
+ let data = null;
1528
+ if (isBridgeMethod(pasteboard, "dataForType")) {
1529
+ const png = pasteboard.dataForType($.NSPasteboardTypePNG);
1530
+ if (isBridgeMethod(png, "base64EncodedStringWithOptions")) {
1531
+ data = png;
1532
+ } else {
1533
+ const tiff = pasteboard.dataForType($.NSPasteboardTypeTIFF);
1534
+ // NSData.length is exposed by JXA as a property value, not a
1535
+ // callable bridge method. Probe a real NSData method so a valid
1536
+ // TIFF-only pasteboard item is not mistaken for ObjC nil.
1537
+ if (isBridgeMethod(tiff, "base64EncodedStringWithOptions")) {
1538
+ const representation = $.NSBitmapImageRep.imageRepWithData(tiff);
1539
+ if (isBridgeMethod(representation, "representationUsingTypeProperties")) {
1540
+ const converted = representation.representationUsingTypeProperties(
1541
+ $.NSBitmapImageFileTypePNG,
1542
+ $.NSDictionary.dictionary
1543
+ );
1544
+ if (isBridgeMethod(converted, "base64EncodedStringWithOptions")) {
1545
+ data = converted;
1546
+ }
1547
+ }
1632
1548
  }
1633
1549
  }
1634
- return names;
1635
- } catch (error) {
1636
- return [];
1637
1550
  }
1638
- }
1639
-
1640
- const pasteboard = $.NSPasteboard.generalPasteboard;
1641
- let encoded = base64FromData(safeDataForType(pasteboard, $("public.png")));
1642
-
1643
- if (!encoded) {
1644
- encoded = base64FromData(
1645
- pngDataFromTiff(safeDataForType(pasteboard, $("public.tiff")))
1646
- );
1647
- }
1648
-
1649
- // Ask AppKit for objects conforming to NSPasteboardReading. This covers image
1650
- // formats and lazy providers that do not expose direct PNG/TIFF NSData.
1651
- if (!encoded) {
1652
- encoded = base64FromImage(firstReadableObject(pasteboard, $.NSImage));
1653
- }
1654
-
1655
- if (!encoded) {
1656
- encoded = base64FromImage(imageFromWholePasteboard(pasteboard));
1657
- }
1658
-
1659
- // Finder copies files as file URLs instead of image bytes. Load the first
1660
- // local file through NSImage, which also rejects non-image files.
1661
- if (!encoded) {
1662
- encoded = base64FromImage(imageFromFileUrl(pasteboard));
1663
- }
1664
-
1665
- if (!encoded) {
1666
- const types = pasteboardTypeNames(pasteboard);
1667
- const advertisedImage = types.some(function (type) {
1668
- return /image|png|tiff|jpeg|jpg|heic|gif|webp|bmp|pdf|file-url|filenames/i.test(type);
1669
- });
1670
- if (advertisedImage) {
1671
- throw new Error(
1672
- "Clipboard advertises image or file data but AppKit could not decode it. Types: "
1673
- + types.join(", ")
1674
- );
1551
+ if (!data) {
1552
+ $.exit(3);
1675
1553
  }
1554
+ const encoded = data.base64EncodedStringWithOptions(0);
1555
+ if (!encoded) {
1556
+ $.exit(3);
1557
+ }
1558
+ const output = encoded.stringByAppendingString("\n")
1559
+ .dataUsingEncoding($.NSUTF8StringEncoding);
1560
+ $.NSFileHandle.fileHandleWithStandardOutput.writeData(output);
1561
+ } catch (error) {
1676
1562
  $.exit(3);
1677
1563
  }
1678
-
1679
- console.log(encoded);
1680
1564
  `.trim();
1681
1565
  function shouldReadClipboardImage(input, key, isPasted) {
1682
1566
  return (key.ctrl || key.meta === true) && input.toLowerCase() === "v" || isPasted && input.length === 0;
@@ -3252,6 +3136,10 @@ function CommandPresentationView({
3252
3136
  ] }),
3253
3137
  stdout.total === 0 ? null : /* @__PURE__ */ jsx5(CommandOutputSection, { label: "OUTPUT", value: stdout, tone, error: false }),
3254
3138
  stderr.total === 0 ? null : /* @__PURE__ */ jsx5(CommandOutputSection, { label: "ERROR", value: stderr, tone, error: true }),
3139
+ presentation.diagnostic === void 0 ? null : /* @__PURE__ */ jsxs4(Box_default, { children: [
3140
+ /* @__PURE__ */ jsx5(Text, { color: tone, children: "\u2502 " }),
3141
+ /* @__PURE__ */ jsx5(Text, { color: tone, children: presentation.diagnostic })
3142
+ ] }),
3255
3143
  /* @__PURE__ */ jsxs4(Box_default, { children: [
3256
3144
  /* @__PURE__ */ jsx5(Text, { color: tone, children: "\u2514\u2500 " }),
3257
3145
  /* @__PURE__ */ jsx5(Text, { dimColor: true, children: commandFooter(presentation, state) })
@@ -1,68 +1,68 @@
1
- // astgraph grammar loading — web-tree-sitter WASM runtime, vendored under ./vendor.
2
- // Resolves the vendor directory relative to this module so it works both from the
3
- // source tree (src/init/astgraph) and the copied plugin root (.flavor/plugins/astgraph).
4
-
5
- import { dirname, join } from "node:path";
6
- import { fileURLToPath } from "node:url";
7
-
8
- import { Language, Parser } from "./vendor/tree-sitter.js";
9
-
10
- const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
11
- export const VENDOR_DIR = join(MODULE_DIR, "vendor");
12
-
13
- const GRAMMAR_FILES = {
14
- typescript: "tree-sitter-typescript.wasm",
15
- tsx: "tree-sitter-tsx.wasm",
16
- javascript: "tree-sitter-javascript.wasm",
17
- jsx: "tree-sitter-javascript.wasm",
18
- };
19
-
20
- export const SUPPORTED_LANGUAGES = Object.keys(GRAMMAR_FILES);
21
-
22
- export function languageForFile(path) {
23
- const lower = path.toLowerCase();
24
- if (lower.endsWith(".tsx")) return "tsx";
25
- if (lower.endsWith(".ts") || lower.endsWith(".mts") || lower.endsWith(".cts")) return "typescript";
26
- if (lower.endsWith(".jsx")) return "jsx";
27
- if (lower.endsWith(".js") || lower.endsWith(".mjs") || lower.endsWith(".cjs")) return "javascript";
28
- return undefined;
29
- }
30
-
31
- let initialized = false;
32
- let initPromise;
33
- const languageCache = new Map();
34
- const parserCache = new Map();
35
-
36
- /** Initialize the WASM runtime once. Safe to call repeatedly. */
37
- export async function initGrammars() {
38
- if (initialized) return;
39
- initPromise ??= Parser.init({ locateFile: (name) => join(VENDOR_DIR, name) }).then(() => {
40
- initialized = true;
41
- });
42
- await initPromise;
43
- }
44
-
45
- /** Load (and cache) a grammar for one of the supported languages. */
46
- export async function loadLanguage(language) {
47
- const file = GRAMMAR_FILES[language];
48
- if (file === undefined) throw new Error(`astgraph: unsupported language "${language}"`);
49
- await initGrammars();
50
- let languageInstance = languageCache.get(language);
51
- if (languageInstance === undefined) {
52
- languageInstance = await Language.load(join(VENDOR_DIR, file));
53
- languageCache.set(language, languageInstance);
54
- }
55
- return languageInstance;
56
- }
57
-
58
- /** Parse source text and return the syntax tree. */
59
- export async function parseSource(source, language) {
60
- const languageInstance = await loadLanguage(language);
61
- let parser = parserCache.get(language);
62
- if (parser === undefined) {
63
- parser = new Parser();
64
- parser.setLanguage(languageInstance);
65
- parserCache.set(language, parser);
66
- }
67
- return parser.parse(source);
68
- }
1
+ // astgraph grammar loading — web-tree-sitter WASM runtime, vendored under ./vendor.
2
+ // Resolves the vendor directory relative to this module so it works both from the
3
+ // source tree (src/init/astgraph) and the copied plugin root (.flavor/plugins/astgraph).
4
+
5
+ import { dirname, join } from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+
8
+ import { Language, Parser } from "./vendor/tree-sitter.js";
9
+
10
+ const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
11
+ export const VENDOR_DIR = join(MODULE_DIR, "vendor");
12
+
13
+ const GRAMMAR_FILES = {
14
+ typescript: "tree-sitter-typescript.wasm",
15
+ tsx: "tree-sitter-tsx.wasm",
16
+ javascript: "tree-sitter-javascript.wasm",
17
+ jsx: "tree-sitter-javascript.wasm",
18
+ };
19
+
20
+ export const SUPPORTED_LANGUAGES = Object.keys(GRAMMAR_FILES);
21
+
22
+ export function languageForFile(path) {
23
+ const lower = path.toLowerCase();
24
+ if (lower.endsWith(".tsx")) return "tsx";
25
+ if (lower.endsWith(".ts") || lower.endsWith(".mts") || lower.endsWith(".cts")) return "typescript";
26
+ if (lower.endsWith(".jsx")) return "jsx";
27
+ if (lower.endsWith(".js") || lower.endsWith(".mjs") || lower.endsWith(".cjs")) return "javascript";
28
+ return undefined;
29
+ }
30
+
31
+ let initialized = false;
32
+ let initPromise;
33
+ const languageCache = new Map();
34
+ const parserCache = new Map();
35
+
36
+ /** Initialize the WASM runtime once. Safe to call repeatedly. */
37
+ export async function initGrammars() {
38
+ if (initialized) return;
39
+ initPromise ??= Parser.init({ locateFile: (name) => join(VENDOR_DIR, name) }).then(() => {
40
+ initialized = true;
41
+ });
42
+ await initPromise;
43
+ }
44
+
45
+ /** Load (and cache) a grammar for one of the supported languages. */
46
+ export async function loadLanguage(language) {
47
+ const file = GRAMMAR_FILES[language];
48
+ if (file === undefined) throw new Error(`astgraph: unsupported language "${language}"`);
49
+ await initGrammars();
50
+ let languageInstance = languageCache.get(language);
51
+ if (languageInstance === undefined) {
52
+ languageInstance = await Language.load(join(VENDOR_DIR, file));
53
+ languageCache.set(language, languageInstance);
54
+ }
55
+ return languageInstance;
56
+ }
57
+
58
+ /** Parse source text and return the syntax tree. */
59
+ export async function parseSource(source, language) {
60
+ const languageInstance = await loadLanguage(language);
61
+ let parser = parserCache.get(language);
62
+ if (parser === undefined) {
63
+ parser = new Parser();
64
+ parser.setLanguage(languageInstance);
65
+ parserCache.set(language, parser);
66
+ }
67
+ return parser.parse(source);
68
+ }