clawvault 3.1.0 → 3.2.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 (273) hide show
  1. package/README.md +422 -141
  2. package/bin/clawvault.js +10 -2
  3. package/bin/command-registration.test.js +3 -1
  4. package/bin/command-runtime.js +9 -1
  5. package/bin/register-core-commands.js +23 -28
  6. package/bin/register-maintenance-commands.js +39 -3
  7. package/bin/register-query-commands.js +58 -29
  8. package/bin/register-tailscale-commands.js +106 -0
  9. package/bin/register-task-commands.js +18 -1
  10. package/bin/register-task-commands.test.js +16 -0
  11. package/bin/register-vault-operations-commands.js +29 -1
  12. package/bin/register-workgraph-commands.js +451 -0
  13. package/dashboard/lib/graph-diff.js +104 -0
  14. package/dashboard/lib/graph-diff.test.js +75 -0
  15. package/dashboard/lib/vault-parser.js +556 -0
  16. package/dashboard/lib/vault-parser.test.js +254 -0
  17. package/dashboard/public/app.js +796 -0
  18. package/dashboard/public/index.html +52 -0
  19. package/dashboard/public/styles.css +221 -0
  20. package/dashboard/server.js +374 -0
  21. package/dist/{chunk-C7OK5WKP.js → chunk-2JQ3O2YL.js} +4 -4
  22. package/dist/{chunk-VR5NE7PZ.js → chunk-2RAZ4ZFE.js} +1 -1
  23. package/dist/{chunk-F2JEUD4J.js → chunk-4ITRXIVT.js} +5 -7
  24. package/dist/{chunk-GUKMRGM7.js → chunk-4OXMU5S2.js} +1 -1
  25. package/dist/chunk-5PJ4STIC.js +465 -0
  26. package/dist/{chunk-62YTUT6J.js → chunk-AZYOKJYC.js} +2 -2
  27. package/dist/chunk-BSJ6RIT7.js +447 -0
  28. package/dist/chunk-ECRZL5XR.js +50 -0
  29. package/dist/chunk-ERNE2FZ5.js +189 -0
  30. package/dist/{chunk-WAZ3NLWL.js → chunk-F55HGNU4.js} +0 -47
  31. package/dist/{chunk-VGLOTGAS.js → chunk-FAKNOB7Y.js} +2 -2
  32. package/dist/{chunk-QK3UCXWL.js → chunk-FHFUXL6G.js} +2 -2
  33. package/dist/chunk-GNJL4YGR.js +79 -0
  34. package/dist/chunk-HR4KN6S2.js +152 -0
  35. package/dist/{chunk-OZ7RIXTO.js → chunk-IIOU45CK.js} +1 -1
  36. package/dist/chunk-IJBFGPCS.js +33 -0
  37. package/dist/chunk-IVRIKYFE.js +520 -0
  38. package/dist/chunk-K7PNYS45.js +93 -0
  39. package/dist/chunk-MDIH26GC.js +183 -0
  40. package/dist/{chunk-LYHGEHXG.js → chunk-MFAWT5O5.js} +0 -1
  41. package/dist/{chunk-H34S76MB.js → chunk-MNPUYCHQ.js} +6 -6
  42. package/dist/chunk-NTOPJI7W.js +207 -0
  43. package/dist/{chunk-QBLMXKF2.js → chunk-OIWVQYQF.js} +1 -1
  44. package/dist/chunk-PG56HX5T.js +154 -0
  45. package/dist/{chunk-LNJA2UGL.js → chunk-PI4WMLMG.js} +7 -84
  46. package/dist/chunk-QMHPQYUV.js +363 -0
  47. package/dist/{chunk-H62BP7RI.js → chunk-QPDDIHXE.js} +209 -43
  48. package/dist/{chunk-N2AXRYLC.js → chunk-QWQ3TIKS.js} +1 -1
  49. package/dist/{chunk-3DHXQHYG.js → chunk-R2MIW5G7.js} +1 -1
  50. package/dist/{chunk-SJSFRIYS.js → chunk-S5OJEGFG.js} +2 -2
  51. package/dist/chunk-SS4B7P7V.js +99 -0
  52. package/dist/chunk-TIGW564L.js +628 -0
  53. package/dist/chunk-U67V476Y.js +35 -0
  54. package/dist/{chunk-JY6FYXIT.js → chunk-UCQAOZHW.js} +6 -11
  55. package/dist/{chunk-ITPEXLHA.js → chunk-URXDAUVH.js} +24 -5
  56. package/dist/chunk-WIOLLGAD.js +190 -0
  57. package/dist/{chunk-3WRJEKN4.js → chunk-WJVWINEM.js} +72 -8
  58. package/dist/chunk-WMGIIABP.js +15 -0
  59. package/dist/{chunk-33UGEQRT.js → chunk-X3SPPUFG.js} +151 -64
  60. package/dist/{chunk-3NSBOUT3.js → chunk-Y3TIJEBP.js} +314 -79
  61. package/dist/chunk-Y6VJKXGL.js +373 -0
  62. package/dist/{chunk-LI4O6NVK.js → chunk-YDWHS4LJ.js} +49 -9
  63. package/dist/{chunk-U55BGUAU.js → chunk-YNIPYN4F.js} +5 -5
  64. package/dist/chunk-YXQCA6B7.js +226 -0
  65. package/dist/cli/index.js +26 -22
  66. package/dist/commands/archive.js +3 -3
  67. package/dist/commands/backlog.js +3 -3
  68. package/dist/commands/blocked.js +3 -3
  69. package/dist/commands/canvas.d.ts +15 -0
  70. package/dist/commands/canvas.js +200 -0
  71. package/dist/commands/checkpoint.js +2 -2
  72. package/dist/commands/compat.js +2 -2
  73. package/dist/commands/context.js +7 -5
  74. package/dist/commands/doctor.d.ts +11 -7
  75. package/dist/commands/doctor.js +16 -14
  76. package/dist/commands/embed.js +5 -6
  77. package/dist/commands/entities.js +2 -2
  78. package/dist/commands/graph.js +3 -3
  79. package/dist/commands/inject.d.ts +1 -1
  80. package/dist/commands/inject.js +4 -5
  81. package/dist/commands/kanban.js +4 -4
  82. package/dist/commands/link.js +2 -2
  83. package/dist/commands/migrate-observations.js +4 -4
  84. package/dist/commands/observe.d.ts +0 -1
  85. package/dist/commands/observe.js +13 -12
  86. package/dist/commands/project.js +5 -5
  87. package/dist/commands/rebuild-embeddings.d.ts +21 -0
  88. package/dist/commands/rebuild-embeddings.js +91 -0
  89. package/dist/commands/rebuild.js +12 -11
  90. package/dist/commands/recover.js +3 -3
  91. package/dist/commands/reflect.js +6 -7
  92. package/dist/commands/repair-session.js +1 -1
  93. package/dist/commands/replay.js +14 -14
  94. package/dist/commands/session-recap.js +1 -1
  95. package/dist/commands/setup.d.ts +2 -89
  96. package/dist/commands/setup.js +3 -21
  97. package/dist/commands/shell-init.js +1 -1
  98. package/dist/commands/sleep.d.ts +1 -1
  99. package/dist/commands/sleep.js +18 -17
  100. package/dist/commands/status.d.ts +2 -0
  101. package/dist/commands/status.js +40 -30
  102. package/dist/commands/sync-bd.d.ts +10 -0
  103. package/dist/commands/sync-bd.js +10 -0
  104. package/dist/commands/tailscale.d.ts +52 -0
  105. package/dist/commands/tailscale.js +26 -0
  106. package/dist/commands/task.js +4 -4
  107. package/dist/commands/template.js +2 -2
  108. package/dist/commands/wake.d.ts +1 -1
  109. package/dist/commands/wake.js +11 -10
  110. package/dist/index.d.ts +334 -191
  111. package/dist/index.js +432 -108
  112. package/dist/{inject-Bzi5E-By.d.ts → inject-DYUrDqQO.d.ts} +3 -3
  113. package/dist/ledger-B7g7jhqG.d.ts +44 -0
  114. package/dist/lib/auto-linker.js +1 -1
  115. package/dist/lib/canvas-layout.d.ts +115 -0
  116. package/dist/lib/canvas-layout.js +35 -0
  117. package/dist/lib/config.d.ts +27 -3
  118. package/dist/lib/config.js +4 -2
  119. package/dist/lib/entity-index.js +1 -1
  120. package/dist/lib/project-utils.js +4 -4
  121. package/dist/lib/session-repair.js +1 -1
  122. package/dist/lib/session-utils.js +1 -1
  123. package/dist/lib/tailscale.d.ts +225 -0
  124. package/dist/lib/tailscale.js +50 -0
  125. package/dist/lib/task-utils.js +3 -3
  126. package/dist/lib/template-engine.js +1 -1
  127. package/dist/lib/webdav.d.ts +109 -0
  128. package/dist/lib/webdav.js +35 -0
  129. package/dist/plugin/index.d.ts +344 -28
  130. package/dist/plugin/index.js +3919 -227
  131. package/dist/registry-BR4326o0.d.ts +30 -0
  132. package/dist/store-CA-6sKCJ.d.ts +34 -0
  133. package/dist/thread-B9LhXNU0.d.ts +41 -0
  134. package/dist/{types-Y2_Um2Ls.d.ts → types-BbWJoC1c.d.ts} +1 -44
  135. package/dist/workgraph/index.d.ts +5 -0
  136. package/dist/workgraph/index.js +23 -0
  137. package/dist/workgraph/ledger.d.ts +2 -0
  138. package/dist/workgraph/ledger.js +25 -0
  139. package/dist/workgraph/registry.d.ts +2 -0
  140. package/dist/workgraph/registry.js +19 -0
  141. package/dist/workgraph/store.d.ts +2 -0
  142. package/dist/workgraph/store.js +25 -0
  143. package/dist/workgraph/thread.d.ts +2 -0
  144. package/dist/workgraph/thread.js +25 -0
  145. package/dist/workgraph/types.d.ts +54 -0
  146. package/dist/workgraph/types.js +7 -0
  147. package/hooks/clawvault/HOOK.md +113 -0
  148. package/hooks/clawvault/handler.js +1559 -0
  149. package/hooks/clawvault/handler.test.js +510 -0
  150. package/hooks/clawvault/openclaw.plugin.json +72 -0
  151. package/openclaw.plugin.json +235 -30
  152. package/package.json +20 -20
  153. package/dist/chunk-3RG5ZIWI.js +0 -10
  154. package/dist/chunk-3ZIH425O.js +0 -871
  155. package/dist/chunk-6U6MK36V.js +0 -205
  156. package/dist/chunk-CMB7UL7C.js +0 -327
  157. package/dist/chunk-D2H45LON.js +0 -1074
  158. package/dist/chunk-E7MFQB6D.js +0 -163
  159. package/dist/chunk-GQSLDZTS.js +0 -560
  160. package/dist/chunk-MFM6K7PU.js +0 -374
  161. package/dist/chunk-MXSSG3QU.js +0 -42
  162. package/dist/chunk-OCGVIN3L.js +0 -88
  163. package/dist/chunk-PAH27GSN.js +0 -108
  164. package/dist/chunk-YCUNCH2I.js +0 -78
  165. package/dist/cli/index.cjs +0 -8584
  166. package/dist/cli/index.d.cts +0 -5
  167. package/dist/commands/archive.cjs +0 -287
  168. package/dist/commands/archive.d.cts +0 -11
  169. package/dist/commands/backlog.cjs +0 -721
  170. package/dist/commands/backlog.d.cts +0 -53
  171. package/dist/commands/blocked.cjs +0 -204
  172. package/dist/commands/blocked.d.cts +0 -26
  173. package/dist/commands/checkpoint.cjs +0 -244
  174. package/dist/commands/checkpoint.d.cts +0 -41
  175. package/dist/commands/compat.cjs +0 -294
  176. package/dist/commands/compat.d.cts +0 -28
  177. package/dist/commands/context.cjs +0 -2990
  178. package/dist/commands/context.d.cts +0 -2
  179. package/dist/commands/doctor.cjs +0 -2986
  180. package/dist/commands/doctor.d.cts +0 -21
  181. package/dist/commands/embed.cjs +0 -232
  182. package/dist/commands/embed.d.cts +0 -17
  183. package/dist/commands/entities.cjs +0 -141
  184. package/dist/commands/entities.d.cts +0 -7
  185. package/dist/commands/graph.cjs +0 -501
  186. package/dist/commands/graph.d.cts +0 -21
  187. package/dist/commands/inject.cjs +0 -1636
  188. package/dist/commands/inject.d.cts +0 -2
  189. package/dist/commands/kanban.cjs +0 -884
  190. package/dist/commands/kanban.d.cts +0 -63
  191. package/dist/commands/link.cjs +0 -965
  192. package/dist/commands/link.d.cts +0 -11
  193. package/dist/commands/migrate-observations.cjs +0 -362
  194. package/dist/commands/migrate-observations.d.cts +0 -19
  195. package/dist/commands/observe.cjs +0 -4099
  196. package/dist/commands/observe.d.cts +0 -23
  197. package/dist/commands/project.cjs +0 -1341
  198. package/dist/commands/project.d.cts +0 -85
  199. package/dist/commands/rebuild.cjs +0 -3136
  200. package/dist/commands/rebuild.d.cts +0 -11
  201. package/dist/commands/recover.cjs +0 -361
  202. package/dist/commands/recover.d.cts +0 -38
  203. package/dist/commands/reflect.cjs +0 -1008
  204. package/dist/commands/reflect.d.cts +0 -11
  205. package/dist/commands/repair-session.cjs +0 -457
  206. package/dist/commands/repair-session.d.cts +0 -38
  207. package/dist/commands/replay.cjs +0 -4103
  208. package/dist/commands/replay.d.cts +0 -16
  209. package/dist/commands/session-recap.cjs +0 -353
  210. package/dist/commands/session-recap.d.cts +0 -27
  211. package/dist/commands/setup.cjs +0 -1278
  212. package/dist/commands/setup.d.cts +0 -99
  213. package/dist/commands/shell-init.cjs +0 -75
  214. package/dist/commands/shell-init.d.cts +0 -7
  215. package/dist/commands/sleep.cjs +0 -6029
  216. package/dist/commands/sleep.d.cts +0 -36
  217. package/dist/commands/status.cjs +0 -2737
  218. package/dist/commands/status.d.cts +0 -52
  219. package/dist/commands/task.cjs +0 -1236
  220. package/dist/commands/task.d.cts +0 -97
  221. package/dist/commands/template.cjs +0 -457
  222. package/dist/commands/template.d.cts +0 -36
  223. package/dist/commands/wake.cjs +0 -2627
  224. package/dist/commands/wake.d.cts +0 -22
  225. package/dist/context-BUGaWpyL.d.cts +0 -46
  226. package/dist/index.cjs +0 -12373
  227. package/dist/index.d.cts +0 -854
  228. package/dist/inject-Bzi5E-By.d.cts +0 -137
  229. package/dist/lib/auto-linker.cjs +0 -176
  230. package/dist/lib/auto-linker.d.cts +0 -26
  231. package/dist/lib/config.cjs +0 -78
  232. package/dist/lib/config.d.cts +0 -11
  233. package/dist/lib/entity-index.cjs +0 -84
  234. package/dist/lib/entity-index.d.cts +0 -26
  235. package/dist/lib/project-utils.cjs +0 -864
  236. package/dist/lib/project-utils.d.cts +0 -97
  237. package/dist/lib/session-repair.cjs +0 -239
  238. package/dist/lib/session-repair.d.cts +0 -110
  239. package/dist/lib/session-utils.cjs +0 -209
  240. package/dist/lib/session-utils.d.cts +0 -63
  241. package/dist/lib/task-utils.cjs +0 -1137
  242. package/dist/lib/task-utils.d.cts +0 -208
  243. package/dist/lib/template-engine.cjs +0 -47
  244. package/dist/lib/template-engine.d.cts +0 -11
  245. package/dist/plugin/index.cjs +0 -1907
  246. package/dist/plugin/index.d.cts +0 -36
  247. package/dist/plugin/inject.cjs +0 -356
  248. package/dist/plugin/inject.d.cts +0 -54
  249. package/dist/plugin/inject.d.ts +0 -54
  250. package/dist/plugin/inject.js +0 -17
  251. package/dist/plugin/observe.cjs +0 -631
  252. package/dist/plugin/observe.d.cts +0 -39
  253. package/dist/plugin/observe.d.ts +0 -39
  254. package/dist/plugin/observe.js +0 -18
  255. package/dist/plugin/templates.cjs +0 -593
  256. package/dist/plugin/templates.d.cts +0 -52
  257. package/dist/plugin/templates.d.ts +0 -52
  258. package/dist/plugin/templates.js +0 -25
  259. package/dist/plugin/types.cjs +0 -18
  260. package/dist/plugin/types.d.cts +0 -209
  261. package/dist/plugin/types.d.ts +0 -209
  262. package/dist/plugin/types.js +0 -0
  263. package/dist/plugin/vault.cjs +0 -927
  264. package/dist/plugin/vault.d.cts +0 -68
  265. package/dist/plugin/vault.d.ts +0 -68
  266. package/dist/plugin/vault.js +0 -22
  267. package/dist/types-Y2_Um2Ls.d.cts +0 -205
  268. package/templates/memory-event.md +0 -67
  269. package/templates/party.md +0 -63
  270. package/templates/primitive-registry.yaml +0 -551
  271. package/templates/run.md +0 -68
  272. package/templates/trigger.md +0 -68
  273. package/templates/workspace.md +0 -50
@@ -1,110 +1,3610 @@
1
1
  import {
2
- buildFullContext,
3
- buildPreferenceContext,
4
- buildSessionRecap,
5
- formatMemoriesForContext,
6
- formatSearchResults,
7
- scanVaultFiles
8
- } from "../chunk-CMB7UL7C.js";
9
- import {
10
- detectCategory,
11
- extractObservations,
12
- extractSearchTerms,
13
- isObservable,
14
- processMessageForObservations
15
- } from "../chunk-6U6MK36V.js";
16
- import {
17
- appendToLedger,
18
- batchWriteObservations,
19
- ensureVaultStructure,
20
- writeObservation,
21
- writeVaultFile
22
- } from "../chunk-MFM6K7PU.js";
23
- import {
24
- classifyText,
25
- getAllSchemas,
26
- getSchema,
27
- getSchemaNames,
28
- initializeTemplateRegistry
29
- } from "../chunk-GQSLDZTS.js";
30
- import {
31
- __require
32
- } from "../chunk-3RG5ZIWI.js";
2
+ __commonJS,
3
+ __toESM
4
+ } from "../chunk-U67V476Y.js";
5
+
6
+ // node_modules/@sinclair/typebox/typebox.js
7
+ var require_typebox = __commonJS({
8
+ "node_modules/@sinclair/typebox/typebox.js"(exports) {
9
+ "use strict";
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.KeyResolver = exports.ObjectMap = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Modifier = void 0;
12
+ exports.Modifier = /* @__PURE__ */ Symbol.for("TypeBox.Modifier");
13
+ exports.Hint = /* @__PURE__ */ Symbol.for("TypeBox.Hint");
14
+ exports.Kind = /* @__PURE__ */ Symbol.for("TypeBox.Kind");
15
+ exports.PatternBoolean = "(true|false)";
16
+ exports.PatternNumber = "(0|[1-9][0-9]*)";
17
+ exports.PatternString = "(.*)";
18
+ exports.PatternBooleanExact = `^${exports.PatternBoolean}$`;
19
+ exports.PatternNumberExact = `^${exports.PatternNumber}$`;
20
+ exports.PatternStringExact = `^${exports.PatternString}$`;
21
+ var TypeRegistry;
22
+ (function(TypeRegistry2) {
23
+ const map = /* @__PURE__ */ new Map();
24
+ function Entries() {
25
+ return new Map(map);
26
+ }
27
+ TypeRegistry2.Entries = Entries;
28
+ function Clear() {
29
+ return map.clear();
30
+ }
31
+ TypeRegistry2.Clear = Clear;
32
+ function Has(kind) {
33
+ return map.has(kind);
34
+ }
35
+ TypeRegistry2.Has = Has;
36
+ function Set2(kind, func) {
37
+ map.set(kind, func);
38
+ }
39
+ TypeRegistry2.Set = Set2;
40
+ function Get(kind) {
41
+ return map.get(kind);
42
+ }
43
+ TypeRegistry2.Get = Get;
44
+ })(TypeRegistry = exports.TypeRegistry || (exports.TypeRegistry = {}));
45
+ var FormatRegistry;
46
+ (function(FormatRegistry2) {
47
+ const map = /* @__PURE__ */ new Map();
48
+ function Entries() {
49
+ return new Map(map);
50
+ }
51
+ FormatRegistry2.Entries = Entries;
52
+ function Clear() {
53
+ return map.clear();
54
+ }
55
+ FormatRegistry2.Clear = Clear;
56
+ function Has(format) {
57
+ return map.has(format);
58
+ }
59
+ FormatRegistry2.Has = Has;
60
+ function Set2(format, func) {
61
+ map.set(format, func);
62
+ }
63
+ FormatRegistry2.Set = Set2;
64
+ function Get(format) {
65
+ return map.get(format);
66
+ }
67
+ FormatRegistry2.Get = Get;
68
+ })(FormatRegistry = exports.FormatRegistry || (exports.FormatRegistry = {}));
69
+ var TypeGuardUnknownTypeError = class extends Error {
70
+ constructor(schema) {
71
+ super("TypeGuard: Unknown type");
72
+ this.schema = schema;
73
+ }
74
+ };
75
+ exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
76
+ var TypeGuard;
77
+ (function(TypeGuard2) {
78
+ function IsObject(value) {
79
+ return typeof value === "object" && value !== null && !Array.isArray(value);
80
+ }
81
+ function IsArray(value) {
82
+ return typeof value === "object" && value !== null && Array.isArray(value);
83
+ }
84
+ function IsPattern(value) {
85
+ try {
86
+ new RegExp(value);
87
+ return true;
88
+ } catch {
89
+ return false;
90
+ }
91
+ }
92
+ function IsControlCharacterFree(value) {
93
+ if (typeof value !== "string")
94
+ return false;
95
+ for (let i = 0; i < value.length; i++) {
96
+ const code = value.charCodeAt(i);
97
+ if (code >= 7 && code <= 13 || code === 27 || code === 127) {
98
+ return false;
99
+ }
100
+ }
101
+ return true;
102
+ }
103
+ function IsBigInt(value) {
104
+ return typeof value === "bigint";
105
+ }
106
+ function IsString(value) {
107
+ return typeof value === "string";
108
+ }
109
+ function IsNumber(value) {
110
+ return typeof value === "number" && globalThis.Number.isFinite(value);
111
+ }
112
+ function IsBoolean(value) {
113
+ return typeof value === "boolean";
114
+ }
115
+ function IsOptionalBigInt(value) {
116
+ return value === void 0 || value !== void 0 && IsBigInt(value);
117
+ }
118
+ function IsOptionalNumber(value) {
119
+ return value === void 0 || value !== void 0 && IsNumber(value);
120
+ }
121
+ function IsOptionalBoolean(value) {
122
+ return value === void 0 || value !== void 0 && IsBoolean(value);
123
+ }
124
+ function IsOptionalString(value) {
125
+ return value === void 0 || value !== void 0 && IsString(value);
126
+ }
127
+ function IsOptionalPattern(value) {
128
+ return value === void 0 || value !== void 0 && IsString(value) && IsControlCharacterFree(value) && IsPattern(value);
129
+ }
130
+ function IsOptionalFormat(value) {
131
+ return value === void 0 || value !== void 0 && IsString(value) && IsControlCharacterFree(value);
132
+ }
133
+ function IsOptionalSchema(value) {
134
+ return value === void 0 || TSchema(value);
135
+ }
136
+ function TAny(schema) {
137
+ return TKind(schema) && schema[exports.Kind] === "Any" && IsOptionalString(schema.$id);
138
+ }
139
+ TypeGuard2.TAny = TAny;
140
+ function TArray(schema) {
141
+ return TKind(schema) && schema[exports.Kind] === "Array" && schema.type === "array" && IsOptionalString(schema.$id) && TSchema(schema.items) && IsOptionalNumber(schema.minItems) && IsOptionalNumber(schema.maxItems) && IsOptionalBoolean(schema.uniqueItems);
142
+ }
143
+ TypeGuard2.TArray = TArray;
144
+ function TBigInt(schema) {
145
+ return TKind(schema) && schema[exports.Kind] === "BigInt" && schema.type === "null" && schema.typeOf === "BigInt" && IsOptionalString(schema.$id) && IsOptionalBigInt(schema.multipleOf) && IsOptionalBigInt(schema.minimum) && IsOptionalBigInt(schema.maximum) && IsOptionalBigInt(schema.exclusiveMinimum) && IsOptionalBigInt(schema.exclusiveMaximum);
146
+ }
147
+ TypeGuard2.TBigInt = TBigInt;
148
+ function TBoolean(schema) {
149
+ return TKind(schema) && schema[exports.Kind] === "Boolean" && schema.type === "boolean" && IsOptionalString(schema.$id);
150
+ }
151
+ TypeGuard2.TBoolean = TBoolean;
152
+ function TConstructor(schema) {
153
+ if (!(TKind(schema) && schema[exports.Kind] === "Constructor" && schema.type === "object" && schema.instanceOf === "Constructor" && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) {
154
+ return false;
155
+ }
156
+ for (const parameter of schema.parameters) {
157
+ if (!TSchema(parameter))
158
+ return false;
159
+ }
160
+ return true;
161
+ }
162
+ TypeGuard2.TConstructor = TConstructor;
163
+ function TDate(schema) {
164
+ return TKind(schema) && schema[exports.Kind] === "Date" && schema.type === "object" && schema.instanceOf === "Date" && IsOptionalString(schema.$id) && IsOptionalNumber(schema.minimumTimestamp) && IsOptionalNumber(schema.maximumTimestamp) && IsOptionalNumber(schema.exclusiveMinimumTimestamp) && IsOptionalNumber(schema.exclusiveMaximumTimestamp);
165
+ }
166
+ TypeGuard2.TDate = TDate;
167
+ function TFunction(schema) {
168
+ if (!(TKind(schema) && schema[exports.Kind] === "Function" && schema.type === "object" && schema.instanceOf === "Function" && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) {
169
+ return false;
170
+ }
171
+ for (const parameter of schema.parameters) {
172
+ if (!TSchema(parameter))
173
+ return false;
174
+ }
175
+ return true;
176
+ }
177
+ TypeGuard2.TFunction = TFunction;
178
+ function TInteger(schema) {
179
+ return TKind(schema) && schema[exports.Kind] === "Integer" && schema.type === "integer" && IsOptionalString(schema.$id) && IsOptionalNumber(schema.multipleOf) && IsOptionalNumber(schema.minimum) && IsOptionalNumber(schema.maximum) && IsOptionalNumber(schema.exclusiveMinimum) && IsOptionalNumber(schema.exclusiveMaximum);
180
+ }
181
+ TypeGuard2.TInteger = TInteger;
182
+ function TIntersect(schema) {
183
+ if (!(TKind(schema) && schema[exports.Kind] === "Intersect" && IsArray(schema.allOf) && IsOptionalString(schema.type) && (IsOptionalBoolean(schema.unevaluatedProperties) || IsOptionalSchema(schema.unevaluatedProperties)) && IsOptionalString(schema.$id))) {
184
+ return false;
185
+ }
186
+ if ("type" in schema && schema.type !== "object") {
187
+ return false;
188
+ }
189
+ for (const inner of schema.allOf) {
190
+ if (!TSchema(inner))
191
+ return false;
192
+ }
193
+ return true;
194
+ }
195
+ TypeGuard2.TIntersect = TIntersect;
196
+ function TKind(schema) {
197
+ return IsObject(schema) && exports.Kind in schema && typeof schema[exports.Kind] === "string";
198
+ }
199
+ TypeGuard2.TKind = TKind;
200
+ function TLiteral(schema) {
201
+ return TKind(schema) && schema[exports.Kind] === "Literal" && IsOptionalString(schema.$id) && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const) || IsBigInt(schema.const));
202
+ }
203
+ TypeGuard2.TLiteral = TLiteral;
204
+ function TNever(schema) {
205
+ return TKind(schema) && schema[exports.Kind] === "Never" && IsObject(schema.not) && globalThis.Object.getOwnPropertyNames(schema.not).length === 0;
206
+ }
207
+ TypeGuard2.TNever = TNever;
208
+ function TNot(schema) {
209
+ return TKind(schema) && schema[exports.Kind] === "Not" && IsArray(schema.allOf) && schema.allOf.length === 2 && IsObject(schema.allOf[0]) && TSchema(schema.allOf[0].not) && TSchema(schema.allOf[1]);
210
+ }
211
+ TypeGuard2.TNot = TNot;
212
+ function TNull(schema) {
213
+ return TKind(schema) && schema[exports.Kind] === "Null" && schema.type === "null" && IsOptionalString(schema.$id);
214
+ }
215
+ TypeGuard2.TNull = TNull;
216
+ function TNumber(schema) {
217
+ return TKind(schema) && schema[exports.Kind] === "Number" && schema.type === "number" && IsOptionalString(schema.$id) && IsOptionalNumber(schema.multipleOf) && IsOptionalNumber(schema.minimum) && IsOptionalNumber(schema.maximum) && IsOptionalNumber(schema.exclusiveMinimum) && IsOptionalNumber(schema.exclusiveMaximum);
218
+ }
219
+ TypeGuard2.TNumber = TNumber;
220
+ function TObject(schema) {
221
+ if (!(TKind(schema) && schema[exports.Kind] === "Object" && schema.type === "object" && IsOptionalString(schema.$id) && IsObject(schema.properties) && (IsOptionalBoolean(schema.additionalProperties) || IsOptionalSchema(schema.additionalProperties)) && IsOptionalNumber(schema.minProperties) && IsOptionalNumber(schema.maxProperties))) {
222
+ return false;
223
+ }
224
+ for (const [key, value] of Object.entries(schema.properties)) {
225
+ if (!IsControlCharacterFree(key))
226
+ return false;
227
+ if (!TSchema(value))
228
+ return false;
229
+ }
230
+ return true;
231
+ }
232
+ TypeGuard2.TObject = TObject;
233
+ function TPromise(schema) {
234
+ return TKind(schema) && schema[exports.Kind] === "Promise" && schema.type === "object" && schema.instanceOf === "Promise" && IsOptionalString(schema.$id) && TSchema(schema.item);
235
+ }
236
+ TypeGuard2.TPromise = TPromise;
237
+ function TRecord(schema) {
238
+ if (!(TKind(schema) && schema[exports.Kind] === "Record" && schema.type === "object" && IsOptionalString(schema.$id) && schema.additionalProperties === false && IsObject(schema.patternProperties))) {
239
+ return false;
240
+ }
241
+ const keys = Object.keys(schema.patternProperties);
242
+ if (keys.length !== 1) {
243
+ return false;
244
+ }
245
+ if (!IsPattern(keys[0])) {
246
+ return false;
247
+ }
248
+ if (!TSchema(schema.patternProperties[keys[0]])) {
249
+ return false;
250
+ }
251
+ return true;
252
+ }
253
+ TypeGuard2.TRecord = TRecord;
254
+ function TRef(schema) {
255
+ return TKind(schema) && schema[exports.Kind] === "Ref" && IsOptionalString(schema.$id) && IsString(schema.$ref);
256
+ }
257
+ TypeGuard2.TRef = TRef;
258
+ function TString(schema) {
259
+ return TKind(schema) && schema[exports.Kind] === "String" && schema.type === "string" && IsOptionalString(schema.$id) && IsOptionalNumber(schema.minLength) && IsOptionalNumber(schema.maxLength) && IsOptionalPattern(schema.pattern) && IsOptionalFormat(schema.format);
260
+ }
261
+ TypeGuard2.TString = TString;
262
+ function TSymbol(schema) {
263
+ return TKind(schema) && schema[exports.Kind] === "Symbol" && schema.type === "null" && schema.typeOf === "Symbol" && IsOptionalString(schema.$id);
264
+ }
265
+ TypeGuard2.TSymbol = TSymbol;
266
+ function TTemplateLiteral(schema) {
267
+ return TKind(schema) && schema[exports.Kind] === "TemplateLiteral" && schema.type === "string" && IsString(schema.pattern) && schema.pattern[0] === "^" && schema.pattern[schema.pattern.length - 1] === "$";
268
+ }
269
+ TypeGuard2.TTemplateLiteral = TTemplateLiteral;
270
+ function TThis(schema) {
271
+ return TKind(schema) && schema[exports.Kind] === "This" && IsOptionalString(schema.$id) && IsString(schema.$ref);
272
+ }
273
+ TypeGuard2.TThis = TThis;
274
+ function TTuple(schema) {
275
+ if (!(TKind(schema) && schema[exports.Kind] === "Tuple" && schema.type === "array" && IsOptionalString(schema.$id) && IsNumber(schema.minItems) && IsNumber(schema.maxItems) && schema.minItems === schema.maxItems)) {
276
+ return false;
277
+ }
278
+ if (schema.items === void 0 && schema.additionalItems === void 0 && schema.minItems === 0) {
279
+ return true;
280
+ }
281
+ if (!IsArray(schema.items)) {
282
+ return false;
283
+ }
284
+ for (const inner of schema.items) {
285
+ if (!TSchema(inner))
286
+ return false;
287
+ }
288
+ return true;
289
+ }
290
+ TypeGuard2.TTuple = TTuple;
291
+ function TUndefined(schema) {
292
+ return TKind(schema) && schema[exports.Kind] === "Undefined" && schema.type === "null" && schema.typeOf === "Undefined" && IsOptionalString(schema.$id);
293
+ }
294
+ TypeGuard2.TUndefined = TUndefined;
295
+ function TUnion(schema) {
296
+ if (!(TKind(schema) && schema[exports.Kind] === "Union" && IsArray(schema.anyOf) && IsOptionalString(schema.$id))) {
297
+ return false;
298
+ }
299
+ for (const inner of schema.anyOf) {
300
+ if (!TSchema(inner))
301
+ return false;
302
+ }
303
+ return true;
304
+ }
305
+ TypeGuard2.TUnion = TUnion;
306
+ function TUnionLiteral(schema) {
307
+ return TUnion(schema) && schema.anyOf.every((schema2) => TLiteral(schema2) && typeof schema2.const === "string");
308
+ }
309
+ TypeGuard2.TUnionLiteral = TUnionLiteral;
310
+ function TUint8Array(schema) {
311
+ return TKind(schema) && schema[exports.Kind] === "Uint8Array" && schema.type === "object" && IsOptionalString(schema.$id) && schema.instanceOf === "Uint8Array" && IsOptionalNumber(schema.minByteLength) && IsOptionalNumber(schema.maxByteLength);
312
+ }
313
+ TypeGuard2.TUint8Array = TUint8Array;
314
+ function TUnknown(schema) {
315
+ return TKind(schema) && schema[exports.Kind] === "Unknown" && IsOptionalString(schema.$id);
316
+ }
317
+ TypeGuard2.TUnknown = TUnknown;
318
+ function TUnsafe(schema) {
319
+ return TKind(schema) && schema[exports.Kind] === "Unsafe";
320
+ }
321
+ TypeGuard2.TUnsafe = TUnsafe;
322
+ function TVoid(schema) {
323
+ return TKind(schema) && schema[exports.Kind] === "Void" && schema.type === "null" && schema.typeOf === "Void" && IsOptionalString(schema.$id);
324
+ }
325
+ TypeGuard2.TVoid = TVoid;
326
+ function TReadonlyOptional(schema) {
327
+ return IsObject(schema) && schema[exports.Modifier] === "ReadonlyOptional";
328
+ }
329
+ TypeGuard2.TReadonlyOptional = TReadonlyOptional;
330
+ function TReadonly(schema) {
331
+ return IsObject(schema) && schema[exports.Modifier] === "Readonly";
332
+ }
333
+ TypeGuard2.TReadonly = TReadonly;
334
+ function TOptional(schema) {
335
+ return IsObject(schema) && schema[exports.Modifier] === "Optional";
336
+ }
337
+ TypeGuard2.TOptional = TOptional;
338
+ function TSchema(schema) {
339
+ return typeof schema === "object" && (TAny(schema) || TArray(schema) || TBoolean(schema) || TBigInt(schema) || TConstructor(schema) || TDate(schema) || TFunction(schema) || TInteger(schema) || TIntersect(schema) || TLiteral(schema) || TNever(schema) || TNot(schema) || TNull(schema) || TNumber(schema) || TObject(schema) || TPromise(schema) || TRecord(schema) || TRef(schema) || TString(schema) || TSymbol(schema) || TTemplateLiteral(schema) || TThis(schema) || TTuple(schema) || TUndefined(schema) || TUnion(schema) || TUint8Array(schema) || TUnknown(schema) || TUnsafe(schema) || TVoid(schema) || TKind(schema) && TypeRegistry.Has(schema[exports.Kind]));
340
+ }
341
+ TypeGuard2.TSchema = TSchema;
342
+ })(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {}));
343
+ var ExtendsUndefined;
344
+ (function(ExtendsUndefined2) {
345
+ function Check(schema) {
346
+ if (schema[exports.Kind] === "Undefined")
347
+ return true;
348
+ if (schema[exports.Kind] === "Union") {
349
+ const union = schema;
350
+ return union.anyOf.some((schema2) => Check(schema2));
351
+ }
352
+ return false;
353
+ }
354
+ ExtendsUndefined2.Check = Check;
355
+ })(ExtendsUndefined = exports.ExtendsUndefined || (exports.ExtendsUndefined = {}));
356
+ var TypeExtendsResult;
357
+ (function(TypeExtendsResult2) {
358
+ TypeExtendsResult2[TypeExtendsResult2["Union"] = 0] = "Union";
359
+ TypeExtendsResult2[TypeExtendsResult2["True"] = 1] = "True";
360
+ TypeExtendsResult2[TypeExtendsResult2["False"] = 2] = "False";
361
+ })(TypeExtendsResult = exports.TypeExtendsResult || (exports.TypeExtendsResult = {}));
362
+ var TypeExtends;
363
+ (function(TypeExtends2) {
364
+ function IntoBooleanResult(result) {
365
+ return result === TypeExtendsResult.False ? TypeExtendsResult.False : TypeExtendsResult.True;
366
+ }
367
+ function AnyRight(left, right) {
368
+ return TypeExtendsResult.True;
369
+ }
370
+ function Any(left, right) {
371
+ if (TypeGuard.TIntersect(right))
372
+ return IntersectRight(left, right);
373
+ if (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema)))
374
+ return TypeExtendsResult.True;
375
+ if (TypeGuard.TUnion(right))
376
+ return TypeExtendsResult.Union;
377
+ if (TypeGuard.TUnknown(right))
378
+ return TypeExtendsResult.True;
379
+ if (TypeGuard.TAny(right))
380
+ return TypeExtendsResult.True;
381
+ return TypeExtendsResult.Union;
382
+ }
383
+ function ArrayRight(left, right) {
384
+ if (TypeGuard.TUnknown(left))
385
+ return TypeExtendsResult.False;
386
+ if (TypeGuard.TAny(left))
387
+ return TypeExtendsResult.Union;
388
+ if (TypeGuard.TNever(left))
389
+ return TypeExtendsResult.True;
390
+ return TypeExtendsResult.False;
391
+ }
392
+ function Array2(left, right) {
393
+ if (TypeGuard.TIntersect(right))
394
+ return IntersectRight(left, right);
395
+ if (TypeGuard.TUnion(right))
396
+ return UnionRight(left, right);
397
+ if (TypeGuard.TUnknown(right))
398
+ return UnknownRight(left, right);
399
+ if (TypeGuard.TAny(right))
400
+ return AnyRight(left, right);
401
+ if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
402
+ return TypeExtendsResult.True;
403
+ if (!TypeGuard.TArray(right))
404
+ return TypeExtendsResult.False;
405
+ return IntoBooleanResult(Visit(left.items, right.items));
406
+ }
407
+ function BigInt(left, right) {
408
+ if (TypeGuard.TIntersect(right))
409
+ return IntersectRight(left, right);
410
+ if (TypeGuard.TUnion(right))
411
+ return UnionRight(left, right);
412
+ if (TypeGuard.TNever(right))
413
+ return NeverRight(left, right);
414
+ if (TypeGuard.TUnknown(right))
415
+ return UnknownRight(left, right);
416
+ if (TypeGuard.TAny(right))
417
+ return AnyRight(left, right);
418
+ if (TypeGuard.TObject(right))
419
+ return ObjectRight(left, right);
420
+ if (TypeGuard.TRecord(right))
421
+ return RecordRight(left, right);
422
+ return TypeGuard.TBigInt(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
423
+ }
424
+ function BooleanRight(left, right) {
425
+ if (TypeGuard.TLiteral(left) && typeof left.const === "boolean")
426
+ return TypeExtendsResult.True;
427
+ return TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
428
+ }
429
+ function Boolean(left, right) {
430
+ if (TypeGuard.TIntersect(right))
431
+ return IntersectRight(left, right);
432
+ if (TypeGuard.TUnion(right))
433
+ return UnionRight(left, right);
434
+ if (TypeGuard.TNever(right))
435
+ return NeverRight(left, right);
436
+ if (TypeGuard.TUnknown(right))
437
+ return UnknownRight(left, right);
438
+ if (TypeGuard.TAny(right))
439
+ return AnyRight(left, right);
440
+ if (TypeGuard.TObject(right))
441
+ return ObjectRight(left, right);
442
+ if (TypeGuard.TRecord(right))
443
+ return RecordRight(left, right);
444
+ return TypeGuard.TBoolean(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
445
+ }
446
+ function Constructor(left, right) {
447
+ if (TypeGuard.TIntersect(right))
448
+ return IntersectRight(left, right);
449
+ if (TypeGuard.TUnion(right))
450
+ return UnionRight(left, right);
451
+ if (TypeGuard.TUnknown(right))
452
+ return UnknownRight(left, right);
453
+ if (TypeGuard.TAny(right))
454
+ return AnyRight(left, right);
455
+ if (TypeGuard.TObject(right))
456
+ return ObjectRight(left, right);
457
+ if (!TypeGuard.TConstructor(right))
458
+ return TypeExtendsResult.False;
459
+ if (left.parameters.length > right.parameters.length)
460
+ return TypeExtendsResult.False;
461
+ if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
462
+ return TypeExtendsResult.False;
463
+ }
464
+ return IntoBooleanResult(Visit(left.returns, right.returns));
465
+ }
466
+ function Date2(left, right) {
467
+ if (TypeGuard.TIntersect(right))
468
+ return IntersectRight(left, right);
469
+ if (TypeGuard.TUnion(right))
470
+ return UnionRight(left, right);
471
+ if (TypeGuard.TUnknown(right))
472
+ return UnknownRight(left, right);
473
+ if (TypeGuard.TAny(right))
474
+ return AnyRight(left, right);
475
+ if (TypeGuard.TObject(right))
476
+ return ObjectRight(left, right);
477
+ if (TypeGuard.TRecord(right))
478
+ return RecordRight(left, right);
479
+ return TypeGuard.TDate(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
480
+ }
481
+ function Function(left, right) {
482
+ if (TypeGuard.TIntersect(right))
483
+ return IntersectRight(left, right);
484
+ if (TypeGuard.TUnion(right))
485
+ return UnionRight(left, right);
486
+ if (TypeGuard.TUnknown(right))
487
+ return UnknownRight(left, right);
488
+ if (TypeGuard.TAny(right))
489
+ return AnyRight(left, right);
490
+ if (TypeGuard.TObject(right))
491
+ return ObjectRight(left, right);
492
+ if (!TypeGuard.TFunction(right))
493
+ return TypeExtendsResult.False;
494
+ if (left.parameters.length > right.parameters.length)
495
+ return TypeExtendsResult.False;
496
+ if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
497
+ return TypeExtendsResult.False;
498
+ }
499
+ return IntoBooleanResult(Visit(left.returns, right.returns));
500
+ }
501
+ function IntegerRight(left, right) {
502
+ if (TypeGuard.TLiteral(left) && typeof left.const === "number")
503
+ return TypeExtendsResult.True;
504
+ return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
505
+ }
506
+ function Integer(left, right) {
507
+ if (TypeGuard.TIntersect(right))
508
+ return IntersectRight(left, right);
509
+ if (TypeGuard.TUnion(right))
510
+ return UnionRight(left, right);
511
+ if (TypeGuard.TNever(right))
512
+ return NeverRight(left, right);
513
+ if (TypeGuard.TUnknown(right))
514
+ return UnknownRight(left, right);
515
+ if (TypeGuard.TAny(right))
516
+ return AnyRight(left, right);
517
+ if (TypeGuard.TObject(right))
518
+ return ObjectRight(left, right);
519
+ if (TypeGuard.TRecord(right))
520
+ return RecordRight(left, right);
521
+ return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
522
+ }
523
+ function IntersectRight(left, right) {
524
+ return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
525
+ }
526
+ function Intersect(left, right) {
527
+ return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
528
+ }
529
+ function IsLiteralString(schema) {
530
+ return typeof schema.const === "string";
531
+ }
532
+ function IsLiteralNumber(schema) {
533
+ return typeof schema.const === "number";
534
+ }
535
+ function IsLiteralBoolean(schema) {
536
+ return typeof schema.const === "boolean";
537
+ }
538
+ function Literal(left, right) {
539
+ if (TypeGuard.TIntersect(right))
540
+ return IntersectRight(left, right);
541
+ if (TypeGuard.TUnion(right))
542
+ return UnionRight(left, right);
543
+ if (TypeGuard.TNever(right))
544
+ return NeverRight(left, right);
545
+ if (TypeGuard.TUnknown(right))
546
+ return UnknownRight(left, right);
547
+ if (TypeGuard.TAny(right))
548
+ return AnyRight(left, right);
549
+ if (TypeGuard.TObject(right))
550
+ return ObjectRight(left, right);
551
+ if (TypeGuard.TRecord(right))
552
+ return RecordRight(left, right);
553
+ if (TypeGuard.TString(right))
554
+ return StringRight(left, right);
555
+ if (TypeGuard.TNumber(right))
556
+ return NumberRight(left, right);
557
+ if (TypeGuard.TInteger(right))
558
+ return IntegerRight(left, right);
559
+ if (TypeGuard.TBoolean(right))
560
+ return BooleanRight(left, right);
561
+ return TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True : TypeExtendsResult.False;
562
+ }
563
+ function NeverRight(left, right) {
564
+ return TypeExtendsResult.False;
565
+ }
566
+ function Never(left, right) {
567
+ return TypeExtendsResult.True;
568
+ }
569
+ function Null(left, right) {
570
+ if (TypeGuard.TIntersect(right))
571
+ return IntersectRight(left, right);
572
+ if (TypeGuard.TUnion(right))
573
+ return UnionRight(left, right);
574
+ if (TypeGuard.TNever(right))
575
+ return NeverRight(left, right);
576
+ if (TypeGuard.TUnknown(right))
577
+ return UnknownRight(left, right);
578
+ if (TypeGuard.TAny(right))
579
+ return AnyRight(left, right);
580
+ if (TypeGuard.TObject(right))
581
+ return ObjectRight(left, right);
582
+ if (TypeGuard.TRecord(right))
583
+ return RecordRight(left, right);
584
+ return TypeGuard.TNull(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
585
+ }
586
+ function NumberRight(left, right) {
587
+ if (TypeGuard.TLiteral(left) && IsLiteralNumber(left))
588
+ return TypeExtendsResult.True;
589
+ return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
590
+ }
591
+ function Number2(left, right) {
592
+ if (TypeGuard.TIntersect(right))
593
+ return IntersectRight(left, right);
594
+ if (TypeGuard.TUnion(right))
595
+ return UnionRight(left, right);
596
+ if (TypeGuard.TNever(right))
597
+ return NeverRight(left, right);
598
+ if (TypeGuard.TUnknown(right))
599
+ return UnknownRight(left, right);
600
+ if (TypeGuard.TAny(right))
601
+ return AnyRight(left, right);
602
+ if (TypeGuard.TObject(right))
603
+ return ObjectRight(left, right);
604
+ if (TypeGuard.TRecord(right))
605
+ return RecordRight(left, right);
606
+ return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
607
+ }
608
+ function IsObjectPropertyCount(schema, count) {
609
+ return globalThis.Object.keys(schema.properties).length === count;
610
+ }
611
+ function IsObjectStringLike(schema) {
612
+ return IsObjectArrayLike(schema);
613
+ }
614
+ function IsObjectSymbolLike(schema) {
615
+ return IsObjectPropertyCount(schema, 0) || IsObjectPropertyCount(schema, 1) && "description" in schema.properties && TypeGuard.TUnion(schema.properties.description) && schema.properties.description.anyOf.length === 2 && (TypeGuard.TString(schema.properties.description.anyOf[0]) && TypeGuard.TUndefined(schema.properties.description.anyOf[1]) || TypeGuard.TString(schema.properties.description.anyOf[1]) && TypeGuard.TUndefined(schema.properties.description.anyOf[0]));
616
+ }
617
+ function IsObjectNumberLike(schema) {
618
+ return IsObjectPropertyCount(schema, 0);
619
+ }
620
+ function IsObjectBooleanLike(schema) {
621
+ return IsObjectPropertyCount(schema, 0);
622
+ }
623
+ function IsObjectBigIntLike(schema) {
624
+ return IsObjectPropertyCount(schema, 0);
625
+ }
626
+ function IsObjectDateLike(schema) {
627
+ return IsObjectPropertyCount(schema, 0);
628
+ }
629
+ function IsObjectUint8ArrayLike(schema) {
630
+ return IsObjectArrayLike(schema);
631
+ }
632
+ function IsObjectFunctionLike(schema) {
633
+ const length = exports.Type.Number();
634
+ return IsObjectPropertyCount(schema, 0) || IsObjectPropertyCount(schema, 1) && "length" in schema.properties && IntoBooleanResult(Visit(schema.properties["length"], length)) === TypeExtendsResult.True;
635
+ }
636
+ function IsObjectConstructorLike(schema) {
637
+ return IsObjectPropertyCount(schema, 0);
638
+ }
639
+ function IsObjectArrayLike(schema) {
640
+ const length = exports.Type.Number();
641
+ return IsObjectPropertyCount(schema, 0) || IsObjectPropertyCount(schema, 1) && "length" in schema.properties && IntoBooleanResult(Visit(schema.properties["length"], length)) === TypeExtendsResult.True;
642
+ }
643
+ function IsObjectPromiseLike(schema) {
644
+ const then = exports.Type.Function([exports.Type.Any()], exports.Type.Any());
645
+ return IsObjectPropertyCount(schema, 0) || IsObjectPropertyCount(schema, 1) && "then" in schema.properties && IntoBooleanResult(Visit(schema.properties["then"], then)) === TypeExtendsResult.True;
646
+ }
647
+ function Property(left, right) {
648
+ if (Visit(left, right) === TypeExtendsResult.False)
649
+ return TypeExtendsResult.False;
650
+ if (TypeGuard.TOptional(left) && !TypeGuard.TOptional(right))
651
+ return TypeExtendsResult.False;
652
+ return TypeExtendsResult.True;
653
+ }
654
+ function ObjectRight(left, right) {
655
+ if (TypeGuard.TUnknown(left))
656
+ return TypeExtendsResult.False;
657
+ if (TypeGuard.TAny(left))
658
+ return TypeExtendsResult.Union;
659
+ if (TypeGuard.TNever(left))
660
+ return TypeExtendsResult.True;
661
+ if (TypeGuard.TLiteral(left) && IsLiteralString(left) && IsObjectStringLike(right))
662
+ return TypeExtendsResult.True;
663
+ if (TypeGuard.TLiteral(left) && IsLiteralNumber(left) && IsObjectNumberLike(right))
664
+ return TypeExtendsResult.True;
665
+ if (TypeGuard.TLiteral(left) && IsLiteralBoolean(left) && IsObjectBooleanLike(right))
666
+ return TypeExtendsResult.True;
667
+ if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
668
+ return TypeExtendsResult.True;
669
+ if (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right))
670
+ return TypeExtendsResult.True;
671
+ if (TypeGuard.TString(left) && IsObjectStringLike(right))
672
+ return TypeExtendsResult.True;
673
+ if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
674
+ return TypeExtendsResult.True;
675
+ if (TypeGuard.TNumber(left) && IsObjectNumberLike(right))
676
+ return TypeExtendsResult.True;
677
+ if (TypeGuard.TInteger(left) && IsObjectNumberLike(right))
678
+ return TypeExtendsResult.True;
679
+ if (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right))
680
+ return TypeExtendsResult.True;
681
+ if (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right))
682
+ return TypeExtendsResult.True;
683
+ if (TypeGuard.TDate(left) && IsObjectDateLike(right))
684
+ return TypeExtendsResult.True;
685
+ if (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right))
686
+ return TypeExtendsResult.True;
687
+ if (TypeGuard.TFunction(left) && IsObjectFunctionLike(right))
688
+ return TypeExtendsResult.True;
689
+ if (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) {
690
+ return right[exports.Hint] === "Record" ? TypeExtendsResult.True : TypeExtendsResult.False;
691
+ }
692
+ if (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) {
693
+ return IsObjectPropertyCount(right, 0) ? TypeExtendsResult.True : TypeExtendsResult.False;
694
+ }
695
+ return TypeExtendsResult.False;
696
+ }
697
+ function Object2(left, right) {
698
+ if (TypeGuard.TIntersect(right))
699
+ return IntersectRight(left, right);
700
+ if (TypeGuard.TUnion(right))
701
+ return UnionRight(left, right);
702
+ if (TypeGuard.TUnknown(right))
703
+ return UnknownRight(left, right);
704
+ if (TypeGuard.TAny(right))
705
+ return AnyRight(left, right);
706
+ if (TypeGuard.TRecord(right))
707
+ return RecordRight(left, right);
708
+ if (!TypeGuard.TObject(right))
709
+ return TypeExtendsResult.False;
710
+ for (const key of globalThis.Object.keys(right.properties)) {
711
+ if (!(key in left.properties))
712
+ return TypeExtendsResult.False;
713
+ if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
714
+ return TypeExtendsResult.False;
715
+ }
716
+ }
717
+ return TypeExtendsResult.True;
718
+ }
719
+ function Promise2(left, right) {
720
+ if (TypeGuard.TIntersect(right))
721
+ return IntersectRight(left, right);
722
+ if (TypeGuard.TUnion(right))
723
+ return UnionRight(left, right);
724
+ if (TypeGuard.TUnknown(right))
725
+ return UnknownRight(left, right);
726
+ if (TypeGuard.TAny(right))
727
+ return AnyRight(left, right);
728
+ if (TypeGuard.TObject(right) && IsObjectPromiseLike(right))
729
+ return TypeExtendsResult.True;
730
+ if (!TypeGuard.TPromise(right))
731
+ return TypeExtendsResult.False;
732
+ return IntoBooleanResult(Visit(left.item, right.item));
733
+ }
734
+ function RecordKey(schema) {
735
+ if (exports.PatternNumberExact in schema.patternProperties)
736
+ return exports.Type.Number();
737
+ if (exports.PatternStringExact in schema.patternProperties)
738
+ return exports.Type.String();
739
+ throw Error("TypeExtends: Cannot get record key");
740
+ }
741
+ function RecordValue(schema) {
742
+ if (exports.PatternNumberExact in schema.patternProperties)
743
+ return schema.patternProperties[exports.PatternNumberExact];
744
+ if (exports.PatternStringExact in schema.patternProperties)
745
+ return schema.patternProperties[exports.PatternStringExact];
746
+ throw Error("TypeExtends: Cannot get record value");
747
+ }
748
+ function RecordRight(left, right) {
749
+ const Key = RecordKey(right);
750
+ const Value = RecordValue(right);
751
+ if (TypeGuard.TLiteral(left) && IsLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True)
752
+ return TypeExtendsResult.True;
753
+ if (TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key))
754
+ return Visit(left, Value);
755
+ if (TypeGuard.TString(left) && TypeGuard.TNumber(Key))
756
+ return Visit(left, Value);
757
+ if (TypeGuard.TArray(left) && TypeGuard.TNumber(Key))
758
+ return Visit(left, Value);
759
+ if (TypeGuard.TObject(left)) {
760
+ for (const key of globalThis.Object.keys(left.properties)) {
761
+ if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
762
+ return TypeExtendsResult.False;
763
+ }
764
+ }
765
+ return TypeExtendsResult.True;
766
+ }
767
+ return TypeExtendsResult.False;
768
+ }
769
+ function Record(left, right) {
770
+ const Value = RecordValue(left);
771
+ if (TypeGuard.TIntersect(right))
772
+ return IntersectRight(left, right);
773
+ if (TypeGuard.TUnion(right))
774
+ return UnionRight(left, right);
775
+ if (TypeGuard.TUnknown(right))
776
+ return UnknownRight(left, right);
777
+ if (TypeGuard.TAny(right))
778
+ return AnyRight(left, right);
779
+ if (TypeGuard.TObject(right))
780
+ return ObjectRight(left, right);
781
+ if (!TypeGuard.TRecord(right))
782
+ return TypeExtendsResult.False;
783
+ return Visit(Value, RecordValue(right));
784
+ }
785
+ function StringRight(left, right) {
786
+ if (TypeGuard.TLiteral(left) && typeof left.const === "string")
787
+ return TypeExtendsResult.True;
788
+ return TypeGuard.TString(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
789
+ }
790
+ function String2(left, right) {
791
+ if (TypeGuard.TIntersect(right))
792
+ return IntersectRight(left, right);
793
+ if (TypeGuard.TUnion(right))
794
+ return UnionRight(left, right);
795
+ if (TypeGuard.TNever(right))
796
+ return NeverRight(left, right);
797
+ if (TypeGuard.TUnknown(right))
798
+ return UnknownRight(left, right);
799
+ if (TypeGuard.TAny(right))
800
+ return AnyRight(left, right);
801
+ if (TypeGuard.TObject(right))
802
+ return ObjectRight(left, right);
803
+ if (TypeGuard.TRecord(right))
804
+ return RecordRight(left, right);
805
+ return TypeGuard.TString(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
806
+ }
807
+ function Symbol2(left, right) {
808
+ if (TypeGuard.TIntersect(right))
809
+ return IntersectRight(left, right);
810
+ if (TypeGuard.TUnion(right))
811
+ return UnionRight(left, right);
812
+ if (TypeGuard.TNever(right))
813
+ return NeverRight(left, right);
814
+ if (TypeGuard.TUnknown(right))
815
+ return UnknownRight(left, right);
816
+ if (TypeGuard.TAny(right))
817
+ return AnyRight(left, right);
818
+ if (TypeGuard.TObject(right))
819
+ return ObjectRight(left, right);
820
+ if (TypeGuard.TRecord(right))
821
+ return RecordRight(left, right);
822
+ return TypeGuard.TSymbol(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
823
+ }
824
+ function TupleRight(left, right) {
825
+ if (TypeGuard.TUnknown(left))
826
+ return TypeExtendsResult.False;
827
+ if (TypeGuard.TAny(left))
828
+ return TypeExtendsResult.Union;
829
+ if (TypeGuard.TNever(left))
830
+ return TypeExtendsResult.True;
831
+ return TypeExtendsResult.False;
832
+ }
833
+ function IsArrayOfTuple(left, right) {
834
+ return TypeGuard.TArray(right) && left.items !== void 0 && left.items.every((schema) => Visit(schema, right.items) === TypeExtendsResult.True);
835
+ }
836
+ function Tuple(left, right) {
837
+ if (TypeGuard.TIntersect(right))
838
+ return IntersectRight(left, right);
839
+ if (TypeGuard.TUnion(right))
840
+ return UnionRight(left, right);
841
+ if (TypeGuard.TUnknown(right))
842
+ return UnknownRight(left, right);
843
+ if (TypeGuard.TAny(right))
844
+ return AnyRight(left, right);
845
+ if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
846
+ return TypeExtendsResult.True;
847
+ if (TypeGuard.TArray(right) && IsArrayOfTuple(left, right))
848
+ return TypeExtendsResult.True;
849
+ if (!TypeGuard.TTuple(right))
850
+ return TypeExtendsResult.False;
851
+ if (left.items === void 0 && right.items !== void 0 || left.items !== void 0 && right.items === void 0)
852
+ return TypeExtendsResult.False;
853
+ if (left.items === void 0 && right.items === void 0)
854
+ return TypeExtendsResult.True;
855
+ return left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
856
+ }
857
+ function Uint8Array(left, right) {
858
+ if (TypeGuard.TIntersect(right))
859
+ return IntersectRight(left, right);
860
+ if (TypeGuard.TUnion(right))
861
+ return UnionRight(left, right);
862
+ if (TypeGuard.TUnknown(right))
863
+ return UnknownRight(left, right);
864
+ if (TypeGuard.TAny(right))
865
+ return AnyRight(left, right);
866
+ if (TypeGuard.TObject(right))
867
+ return ObjectRight(left, right);
868
+ if (TypeGuard.TRecord(right))
869
+ return RecordRight(left, right);
870
+ return TypeGuard.TUint8Array(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
871
+ }
872
+ function Undefined(left, right) {
873
+ if (TypeGuard.TIntersect(right))
874
+ return IntersectRight(left, right);
875
+ if (TypeGuard.TUnion(right))
876
+ return UnionRight(left, right);
877
+ if (TypeGuard.TNever(right))
878
+ return NeverRight(left, right);
879
+ if (TypeGuard.TUnknown(right))
880
+ return UnknownRight(left, right);
881
+ if (TypeGuard.TAny(right))
882
+ return AnyRight(left, right);
883
+ if (TypeGuard.TObject(right))
884
+ return ObjectRight(left, right);
885
+ if (TypeGuard.TRecord(right))
886
+ return RecordRight(left, right);
887
+ if (TypeGuard.TVoid(right))
888
+ return VoidRight(left, right);
889
+ return TypeGuard.TUndefined(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
890
+ }
891
+ function UnionRight(left, right) {
892
+ return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
893
+ }
894
+ function Union(left, right) {
895
+ return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
896
+ }
897
+ function UnknownRight(left, right) {
898
+ return TypeExtendsResult.True;
899
+ }
900
+ function Unknown(left, right) {
901
+ if (TypeGuard.TIntersect(right))
902
+ return IntersectRight(left, right);
903
+ if (TypeGuard.TUnion(right))
904
+ return UnionRight(left, right);
905
+ if (TypeGuard.TAny(right))
906
+ return AnyRight(left, right);
907
+ if (TypeGuard.TString(right))
908
+ return StringRight(left, right);
909
+ if (TypeGuard.TNumber(right))
910
+ return NumberRight(left, right);
911
+ if (TypeGuard.TInteger(right))
912
+ return IntegerRight(left, right);
913
+ if (TypeGuard.TBoolean(right))
914
+ return BooleanRight(left, right);
915
+ if (TypeGuard.TArray(right))
916
+ return ArrayRight(left, right);
917
+ if (TypeGuard.TTuple(right))
918
+ return TupleRight(left, right);
919
+ if (TypeGuard.TObject(right))
920
+ return ObjectRight(left, right);
921
+ return TypeGuard.TUnknown(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
922
+ }
923
+ function VoidRight(left, right) {
924
+ if (TypeGuard.TUndefined(left))
925
+ return TypeExtendsResult.True;
926
+ return TypeGuard.TUndefined(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
927
+ }
928
+ function Void(left, right) {
929
+ if (TypeGuard.TIntersect(right))
930
+ return IntersectRight(left, right);
931
+ if (TypeGuard.TUnion(right))
932
+ return UnionRight(left, right);
933
+ if (TypeGuard.TUnknown(right))
934
+ return UnknownRight(left, right);
935
+ if (TypeGuard.TAny(right))
936
+ return AnyRight(left, right);
937
+ if (TypeGuard.TObject(right))
938
+ return ObjectRight(left, right);
939
+ return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
940
+ }
941
+ function Visit(left, right) {
942
+ if (TypeGuard.TTemplateLiteral(left))
943
+ return Visit(TemplateLiteralResolver.Resolve(left), right);
944
+ if (TypeGuard.TTemplateLiteral(right))
945
+ return Visit(left, TemplateLiteralResolver.Resolve(right));
946
+ if (TypeGuard.TAny(left))
947
+ return Any(left, right);
948
+ if (TypeGuard.TArray(left))
949
+ return Array2(left, right);
950
+ if (TypeGuard.TBigInt(left))
951
+ return BigInt(left, right);
952
+ if (TypeGuard.TBoolean(left))
953
+ return Boolean(left, right);
954
+ if (TypeGuard.TConstructor(left))
955
+ return Constructor(left, right);
956
+ if (TypeGuard.TDate(left))
957
+ return Date2(left, right);
958
+ if (TypeGuard.TFunction(left))
959
+ return Function(left, right);
960
+ if (TypeGuard.TInteger(left))
961
+ return Integer(left, right);
962
+ if (TypeGuard.TIntersect(left))
963
+ return Intersect(left, right);
964
+ if (TypeGuard.TLiteral(left))
965
+ return Literal(left, right);
966
+ if (TypeGuard.TNever(left))
967
+ return Never(left, right);
968
+ if (TypeGuard.TNull(left))
969
+ return Null(left, right);
970
+ if (TypeGuard.TNumber(left))
971
+ return Number2(left, right);
972
+ if (TypeGuard.TObject(left))
973
+ return Object2(left, right);
974
+ if (TypeGuard.TRecord(left))
975
+ return Record(left, right);
976
+ if (TypeGuard.TString(left))
977
+ return String2(left, right);
978
+ if (TypeGuard.TSymbol(left))
979
+ return Symbol2(left, right);
980
+ if (TypeGuard.TTuple(left))
981
+ return Tuple(left, right);
982
+ if (TypeGuard.TPromise(left))
983
+ return Promise2(left, right);
984
+ if (TypeGuard.TUint8Array(left))
985
+ return Uint8Array(left, right);
986
+ if (TypeGuard.TUndefined(left))
987
+ return Undefined(left, right);
988
+ if (TypeGuard.TUnion(left))
989
+ return Union(left, right);
990
+ if (TypeGuard.TUnknown(left))
991
+ return Unknown(left, right);
992
+ if (TypeGuard.TVoid(left))
993
+ return Void(left, right);
994
+ throw Error(`TypeExtends: Unknown left type operand '${left[exports.Kind]}'`);
995
+ }
996
+ function Extends(left, right) {
997
+ return Visit(left, right);
998
+ }
999
+ TypeExtends2.Extends = Extends;
1000
+ })(TypeExtends = exports.TypeExtends || (exports.TypeExtends = {}));
1001
+ var TypeClone;
1002
+ (function(TypeClone2) {
1003
+ function IsObject(value) {
1004
+ return typeof value === "object" && value !== null;
1005
+ }
1006
+ function IsArray(value) {
1007
+ return globalThis.Array.isArray(value);
1008
+ }
1009
+ function Array2(value) {
1010
+ return value.map((value2) => Visit(value2));
1011
+ }
1012
+ function Object2(value) {
1013
+ const clonedProperties = globalThis.Object.getOwnPropertyNames(value).reduce((acc, key) => {
1014
+ return { ...acc, [key]: Visit(value[key]) };
1015
+ }, {});
1016
+ const clonedSymbols = globalThis.Object.getOwnPropertySymbols(value).reduce((acc, key) => {
1017
+ return { ...acc, [key]: Visit(value[key]) };
1018
+ }, {});
1019
+ return { ...clonedProperties, ...clonedSymbols };
1020
+ }
1021
+ function Visit(value) {
1022
+ if (IsArray(value))
1023
+ return Array2(value);
1024
+ if (IsObject(value))
1025
+ return Object2(value);
1026
+ return value;
1027
+ }
1028
+ function Clone(schema, options) {
1029
+ return { ...Visit(schema), ...options };
1030
+ }
1031
+ TypeClone2.Clone = Clone;
1032
+ })(TypeClone = exports.TypeClone || (exports.TypeClone = {}));
1033
+ var ObjectMap;
1034
+ (function(ObjectMap2) {
1035
+ function Intersect(schema, callback) {
1036
+ return exports.Type.Intersect(schema.allOf.map((inner) => Visit(inner, callback)), { ...schema });
1037
+ }
1038
+ function Union(schema, callback) {
1039
+ return exports.Type.Union(schema.anyOf.map((inner) => Visit(inner, callback)), { ...schema });
1040
+ }
1041
+ function Object2(schema, callback) {
1042
+ return callback(schema);
1043
+ }
1044
+ function Visit(schema, callback) {
1045
+ if (schema[exports.Kind] === "Intersect")
1046
+ return Intersect(schema, callback);
1047
+ if (schema[exports.Kind] === "Union")
1048
+ return Union(schema, callback);
1049
+ if (schema[exports.Kind] === "Object")
1050
+ return Object2(schema, callback);
1051
+ return schema;
1052
+ }
1053
+ function Map2(schema, callback, options) {
1054
+ return { ...Visit(TypeClone.Clone(schema, {}), callback), ...options };
1055
+ }
1056
+ ObjectMap2.Map = Map2;
1057
+ })(ObjectMap = exports.ObjectMap || (exports.ObjectMap = {}));
1058
+ var KeyResolver;
1059
+ (function(KeyResolver2) {
1060
+ function IsKeyable(schema) {
1061
+ return TypeGuard.TIntersect(schema) || TypeGuard.TUnion(schema) || TypeGuard.TObject(schema) && globalThis.Object.getOwnPropertyNames(schema.properties).length > 0;
1062
+ }
1063
+ function Intersect(schema) {
1064
+ return [...schema.allOf.filter((schema2) => IsKeyable(schema2)).reduce((set, schema2) => Visit(schema2).map((key) => set.add(key))[0], /* @__PURE__ */ new Set())];
1065
+ }
1066
+ function Union(schema) {
1067
+ const sets = schema.anyOf.filter((schema2) => IsKeyable(schema2)).map((inner) => Visit(inner));
1068
+ return [...sets.reduce((set, outer) => outer.map((key) => sets.every((inner) => inner.includes(key)) ? set.add(key) : set)[0], /* @__PURE__ */ new Set())];
1069
+ }
1070
+ function Object2(schema) {
1071
+ return globalThis.Object.keys(schema.properties);
1072
+ }
1073
+ function Visit(schema) {
1074
+ if (TypeGuard.TIntersect(schema))
1075
+ return Intersect(schema);
1076
+ if (TypeGuard.TUnion(schema))
1077
+ return Union(schema);
1078
+ if (TypeGuard.TObject(schema))
1079
+ return Object2(schema);
1080
+ return [];
1081
+ }
1082
+ function Resolve(schema) {
1083
+ return Visit(schema);
1084
+ }
1085
+ KeyResolver2.Resolve = Resolve;
1086
+ })(KeyResolver = exports.KeyResolver || (exports.KeyResolver = {}));
1087
+ var TemplateLiteralPattern;
1088
+ (function(TemplateLiteralPattern2) {
1089
+ function Escape(value) {
1090
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1091
+ }
1092
+ function Visit(schema, acc) {
1093
+ if (TypeGuard.TTemplateLiteral(schema)) {
1094
+ const pattern = schema.pattern.slice(1, schema.pattern.length - 1);
1095
+ return pattern;
1096
+ } else if (TypeGuard.TUnion(schema)) {
1097
+ const tokens = schema.anyOf.map((schema2) => Visit(schema2, acc)).join("|");
1098
+ return `(${tokens})`;
1099
+ } else if (TypeGuard.TNumber(schema)) {
1100
+ return `${acc}${exports.PatternNumber}`;
1101
+ } else if (TypeGuard.TInteger(schema)) {
1102
+ return `${acc}${exports.PatternNumber}`;
1103
+ } else if (TypeGuard.TBigInt(schema)) {
1104
+ return `${acc}${exports.PatternNumber}`;
1105
+ } else if (TypeGuard.TString(schema)) {
1106
+ return `${acc}${exports.PatternString}`;
1107
+ } else if (TypeGuard.TLiteral(schema)) {
1108
+ return `${acc}${Escape(schema.const.toString())}`;
1109
+ } else if (TypeGuard.TBoolean(schema)) {
1110
+ return `${acc}${exports.PatternBoolean}`;
1111
+ } else if (TypeGuard.TNever(schema)) {
1112
+ throw Error("TemplateLiteralPattern: TemplateLiteral cannot operate on types of TNever");
1113
+ } else {
1114
+ throw Error(`TemplateLiteralPattern: Unexpected Kind '${schema[exports.Kind]}'`);
1115
+ }
1116
+ }
1117
+ function Create(kinds) {
1118
+ return `^${kinds.map((schema) => Visit(schema, "")).join("")}$`;
1119
+ }
1120
+ TemplateLiteralPattern2.Create = Create;
1121
+ })(TemplateLiteralPattern = exports.TemplateLiteralPattern || (exports.TemplateLiteralPattern = {}));
1122
+ var TemplateLiteralResolver;
1123
+ (function(TemplateLiteralResolver2) {
1124
+ function Resolve(template) {
1125
+ const expression = TemplateLiteralParser.ParseExact(template.pattern);
1126
+ if (!TemplateLiteralFinite.Check(expression))
1127
+ return exports.Type.String();
1128
+ const literals = [...TemplateLiteralGenerator.Generate(expression)].map((value) => exports.Type.Literal(value));
1129
+ return exports.Type.Union(literals);
1130
+ }
1131
+ TemplateLiteralResolver2.Resolve = Resolve;
1132
+ })(TemplateLiteralResolver = exports.TemplateLiteralResolver || (exports.TemplateLiteralResolver = {}));
1133
+ var TemplateLiteralParserError = class extends Error {
1134
+ constructor(message) {
1135
+ super(message);
1136
+ }
1137
+ };
1138
+ exports.TemplateLiteralParserError = TemplateLiteralParserError;
1139
+ var TemplateLiteralParser;
1140
+ (function(TemplateLiteralParser2) {
1141
+ function IsNonEscaped(pattern, index, char) {
1142
+ return pattern[index] === char && pattern.charCodeAt(index - 1) !== 92;
1143
+ }
1144
+ function IsOpenParen(pattern, index) {
1145
+ return IsNonEscaped(pattern, index, "(");
1146
+ }
1147
+ function IsCloseParen(pattern, index) {
1148
+ return IsNonEscaped(pattern, index, ")");
1149
+ }
1150
+ function IsSeparator(pattern, index) {
1151
+ return IsNonEscaped(pattern, index, "|");
1152
+ }
1153
+ function IsGroup(pattern) {
1154
+ if (!(IsOpenParen(pattern, 0) && IsCloseParen(pattern, pattern.length - 1)))
1155
+ return false;
1156
+ let count = 0;
1157
+ for (let index = 0; index < pattern.length; index++) {
1158
+ if (IsOpenParen(pattern, index))
1159
+ count += 1;
1160
+ if (IsCloseParen(pattern, index))
1161
+ count -= 1;
1162
+ if (count === 0 && index !== pattern.length - 1)
1163
+ return false;
1164
+ }
1165
+ return true;
1166
+ }
1167
+ function InGroup(pattern) {
1168
+ return pattern.slice(1, pattern.length - 1);
1169
+ }
1170
+ function IsPrecedenceOr(pattern) {
1171
+ let count = 0;
1172
+ for (let index = 0; index < pattern.length; index++) {
1173
+ if (IsOpenParen(pattern, index))
1174
+ count += 1;
1175
+ if (IsCloseParen(pattern, index))
1176
+ count -= 1;
1177
+ if (IsSeparator(pattern, index) && count === 0)
1178
+ return true;
1179
+ }
1180
+ return false;
1181
+ }
1182
+ function IsPrecedenceAnd(pattern) {
1183
+ for (let index = 0; index < pattern.length; index++) {
1184
+ if (IsOpenParen(pattern, index))
1185
+ return true;
1186
+ }
1187
+ return false;
1188
+ }
1189
+ function Or(pattern) {
1190
+ let [count, start] = [0, 0];
1191
+ const expressions = [];
1192
+ for (let index = 0; index < pattern.length; index++) {
1193
+ if (IsOpenParen(pattern, index))
1194
+ count += 1;
1195
+ if (IsCloseParen(pattern, index))
1196
+ count -= 1;
1197
+ if (IsSeparator(pattern, index) && count === 0) {
1198
+ const range2 = pattern.slice(start, index);
1199
+ if (range2.length > 0)
1200
+ expressions.push(Parse(range2));
1201
+ start = index + 1;
1202
+ }
1203
+ }
1204
+ const range = pattern.slice(start);
1205
+ if (range.length > 0)
1206
+ expressions.push(Parse(range));
1207
+ if (expressions.length === 0)
1208
+ return { type: "const", const: "" };
1209
+ if (expressions.length === 1)
1210
+ return expressions[0];
1211
+ return { type: "or", expr: expressions };
1212
+ }
1213
+ function And(pattern) {
1214
+ function Group(value, index) {
1215
+ if (!IsOpenParen(value, index))
1216
+ throw new TemplateLiteralParserError(`TemplateLiteralParser: Index must point to open parens`);
1217
+ let count = 0;
1218
+ for (let scan = index; scan < value.length; scan++) {
1219
+ if (IsOpenParen(value, scan))
1220
+ count += 1;
1221
+ if (IsCloseParen(value, scan))
1222
+ count -= 1;
1223
+ if (count === 0)
1224
+ return [index, scan];
1225
+ }
1226
+ throw new TemplateLiteralParserError(`TemplateLiteralParser: Unclosed group parens in expression`);
1227
+ }
1228
+ function Range(pattern2, index) {
1229
+ for (let scan = index; scan < pattern2.length; scan++) {
1230
+ if (IsOpenParen(pattern2, scan))
1231
+ return [index, scan];
1232
+ }
1233
+ return [index, pattern2.length];
1234
+ }
1235
+ const expressions = [];
1236
+ for (let index = 0; index < pattern.length; index++) {
1237
+ if (IsOpenParen(pattern, index)) {
1238
+ const [start, end] = Group(pattern, index);
1239
+ const range = pattern.slice(start, end + 1);
1240
+ expressions.push(Parse(range));
1241
+ index = end;
1242
+ } else {
1243
+ const [start, end] = Range(pattern, index);
1244
+ const range = pattern.slice(start, end);
1245
+ if (range.length > 0)
1246
+ expressions.push(Parse(range));
1247
+ index = end - 1;
1248
+ }
1249
+ }
1250
+ if (expressions.length === 0)
1251
+ return { type: "const", const: "" };
1252
+ if (expressions.length === 1)
1253
+ return expressions[0];
1254
+ return { type: "and", expr: expressions };
1255
+ }
1256
+ function Parse(pattern) {
1257
+ if (IsGroup(pattern))
1258
+ return Parse(InGroup(pattern));
1259
+ if (IsPrecedenceOr(pattern))
1260
+ return Or(pattern);
1261
+ if (IsPrecedenceAnd(pattern))
1262
+ return And(pattern);
1263
+ return { type: "const", const: pattern };
1264
+ }
1265
+ TemplateLiteralParser2.Parse = Parse;
1266
+ function ParseExact(pattern) {
1267
+ return Parse(pattern.slice(1, pattern.length - 1));
1268
+ }
1269
+ TemplateLiteralParser2.ParseExact = ParseExact;
1270
+ })(TemplateLiteralParser = exports.TemplateLiteralParser || (exports.TemplateLiteralParser = {}));
1271
+ var TemplateLiteralFinite;
1272
+ (function(TemplateLiteralFinite2) {
1273
+ function IsNumber(expression) {
1274
+ return expression.type === "or" && expression.expr.length === 2 && expression.expr[0].type === "const" && expression.expr[0].const === "0" && expression.expr[1].type === "const" && expression.expr[1].const === "[1-9][0-9]*";
1275
+ }
1276
+ function IsBoolean(expression) {
1277
+ return expression.type === "or" && expression.expr.length === 2 && expression.expr[0].type === "const" && expression.expr[0].const === "true" && expression.expr[1].type === "const" && expression.expr[1].const === "false";
1278
+ }
1279
+ function IsString(expression) {
1280
+ return expression.type === "const" && expression.const === ".*";
1281
+ }
1282
+ function Check(expression) {
1283
+ if (IsBoolean(expression))
1284
+ return true;
1285
+ if (IsNumber(expression) || IsString(expression))
1286
+ return false;
1287
+ if (expression.type === "and")
1288
+ return expression.expr.every((expr) => Check(expr));
1289
+ if (expression.type === "or")
1290
+ return expression.expr.every((expr) => Check(expr));
1291
+ if (expression.type === "const")
1292
+ return true;
1293
+ throw Error(`TemplateLiteralFinite: Unknown expression type`);
1294
+ }
1295
+ TemplateLiteralFinite2.Check = Check;
1296
+ })(TemplateLiteralFinite = exports.TemplateLiteralFinite || (exports.TemplateLiteralFinite = {}));
1297
+ var TemplateLiteralGenerator;
1298
+ (function(TemplateLiteralGenerator2) {
1299
+ function* Reduce(buffer) {
1300
+ if (buffer.length === 1)
1301
+ return yield* buffer[0];
1302
+ for (const left of buffer[0]) {
1303
+ for (const right of Reduce(buffer.slice(1))) {
1304
+ yield `${left}${right}`;
1305
+ }
1306
+ }
1307
+ }
1308
+ function* And(expression) {
1309
+ return yield* Reduce(expression.expr.map((expr) => [...Generate(expr)]));
1310
+ }
1311
+ function* Or(expression) {
1312
+ for (const expr of expression.expr)
1313
+ yield* Generate(expr);
1314
+ }
1315
+ function* Const(expression) {
1316
+ return yield expression.const;
1317
+ }
1318
+ function* Generate(expression) {
1319
+ if (expression.type === "and")
1320
+ return yield* And(expression);
1321
+ if (expression.type === "or")
1322
+ return yield* Or(expression);
1323
+ if (expression.type === "const")
1324
+ return yield* Const(expression);
1325
+ throw Error("TemplateLiteralGenerator: Unknown expression");
1326
+ }
1327
+ TemplateLiteralGenerator2.Generate = Generate;
1328
+ })(TemplateLiteralGenerator = exports.TemplateLiteralGenerator || (exports.TemplateLiteralGenerator = {}));
1329
+ var TypeOrdinal = 0;
1330
+ var TypeBuilder = class {
1331
+ /** `[Utility]` Creates a schema without `static` and `params` types */
1332
+ Create(schema) {
1333
+ return schema;
1334
+ }
1335
+ /** `[Standard]` Omits compositing symbols from this schema */
1336
+ Strict(schema) {
1337
+ return JSON.parse(JSON.stringify(schema));
1338
+ }
1339
+ };
1340
+ exports.TypeBuilder = TypeBuilder;
1341
+ var StandardTypeBuilder = class extends TypeBuilder {
1342
+ // ------------------------------------------------------------------------
1343
+ // Modifiers
1344
+ // ------------------------------------------------------------------------
1345
+ /** `[Modifier]` Creates a Optional property */
1346
+ Optional(schema) {
1347
+ return { [exports.Modifier]: "Optional", ...TypeClone.Clone(schema, {}) };
1348
+ }
1349
+ /** `[Modifier]` Creates a ReadonlyOptional property */
1350
+ ReadonlyOptional(schema) {
1351
+ return { [exports.Modifier]: "ReadonlyOptional", ...TypeClone.Clone(schema, {}) };
1352
+ }
1353
+ /** `[Modifier]` Creates a Readonly object or property */
1354
+ Readonly(schema) {
1355
+ return { [exports.Modifier]: "Readonly", ...schema };
1356
+ }
1357
+ // ------------------------------------------------------------------------
1358
+ // Types
1359
+ // ------------------------------------------------------------------------
1360
+ /** `[Standard]` Creates an Any type */
1361
+ Any(options = {}) {
1362
+ return this.Create({ ...options, [exports.Kind]: "Any" });
1363
+ }
1364
+ /** `[Standard]` Creates an Array type */
1365
+ Array(items, options = {}) {
1366
+ return this.Create({ ...options, [exports.Kind]: "Array", type: "array", items: TypeClone.Clone(items, {}) });
1367
+ }
1368
+ /** `[Standard]` Creates a Boolean type */
1369
+ Boolean(options = {}) {
1370
+ return this.Create({ ...options, [exports.Kind]: "Boolean", type: "boolean" });
1371
+ }
1372
+ /** `[Standard]` Creates a Composite object type. */
1373
+ Composite(objects, options) {
1374
+ const isOptionalAll = (objects2, key) => objects2.every((object) => !(key in object.properties) || IsOptional(object.properties[key]));
1375
+ const IsOptional = (schema) => TypeGuard.TOptional(schema) || TypeGuard.TReadonlyOptional(schema);
1376
+ const [required, optional] = [/* @__PURE__ */ new Set(), /* @__PURE__ */ new Set()];
1377
+ for (const object of objects) {
1378
+ for (const key of globalThis.Object.getOwnPropertyNames(object.properties)) {
1379
+ if (isOptionalAll(objects, key))
1380
+ optional.add(key);
1381
+ }
1382
+ }
1383
+ for (const object of objects) {
1384
+ for (const key of globalThis.Object.getOwnPropertyNames(object.properties)) {
1385
+ if (!optional.has(key))
1386
+ required.add(key);
1387
+ }
1388
+ }
1389
+ const properties = {};
1390
+ for (const object of objects) {
1391
+ for (const [key, schema] of Object.entries(object.properties)) {
1392
+ const property = TypeClone.Clone(schema, {});
1393
+ if (!optional.has(key))
1394
+ delete property[exports.Modifier];
1395
+ if (key in properties) {
1396
+ const left = TypeExtends.Extends(properties[key], property) !== TypeExtendsResult.False;
1397
+ const right = TypeExtends.Extends(property, properties[key]) !== TypeExtendsResult.False;
1398
+ if (!left && !right)
1399
+ properties[key] = exports.Type.Never();
1400
+ if (!left && right)
1401
+ properties[key] = property;
1402
+ } else {
1403
+ properties[key] = property;
1404
+ }
1405
+ }
1406
+ }
1407
+ if (required.size > 0) {
1408
+ return this.Create({ ...options, [exports.Kind]: "Object", [exports.Hint]: "Composite", type: "object", properties, required: [...required] });
1409
+ } else {
1410
+ return this.Create({ ...options, [exports.Kind]: "Object", [exports.Hint]: "Composite", type: "object", properties });
1411
+ }
1412
+ }
1413
+ /** `[Standard]` Creates a Enum type */
1414
+ Enum(item, options = {}) {
1415
+ const values = globalThis.Object.keys(item).filter((key) => isNaN(key)).map((key) => item[key]);
1416
+ const anyOf = values.map((value) => typeof value === "string" ? { [exports.Kind]: "Literal", type: "string", const: value } : { [exports.Kind]: "Literal", type: "number", const: value });
1417
+ return this.Create({ ...options, [exports.Kind]: "Union", anyOf });
1418
+ }
1419
+ /** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */
1420
+ Extends(left, right, trueType, falseType, options = {}) {
1421
+ switch (TypeExtends.Extends(left, right)) {
1422
+ case TypeExtendsResult.Union:
1423
+ return this.Union([TypeClone.Clone(trueType, options), TypeClone.Clone(falseType, options)]);
1424
+ case TypeExtendsResult.True:
1425
+ return TypeClone.Clone(trueType, options);
1426
+ case TypeExtendsResult.False:
1427
+ return TypeClone.Clone(falseType, options);
1428
+ }
1429
+ }
1430
+ /** `[Standard]` Excludes from the left type any type that is not assignable to the right */
1431
+ Exclude(left, right, options = {}) {
1432
+ if (TypeGuard.TTemplateLiteral(left))
1433
+ return this.Exclude(TemplateLiteralResolver.Resolve(left), right, options);
1434
+ if (TypeGuard.TTemplateLiteral(right))
1435
+ return this.Exclude(left, TemplateLiteralResolver.Resolve(right), options);
1436
+ if (TypeGuard.TUnion(left)) {
1437
+ const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) === TypeExtendsResult.False);
1438
+ return narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options);
1439
+ } else {
1440
+ return TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? this.Never(options) : TypeClone.Clone(left, options);
1441
+ }
1442
+ }
1443
+ /** `[Standard]` Extracts from the left type any type that is assignable to the right */
1444
+ Extract(left, right, options = {}) {
1445
+ if (TypeGuard.TTemplateLiteral(left))
1446
+ return this.Extract(TemplateLiteralResolver.Resolve(left), right, options);
1447
+ if (TypeGuard.TTemplateLiteral(right))
1448
+ return this.Extract(left, TemplateLiteralResolver.Resolve(right), options);
1449
+ if (TypeGuard.TUnion(left)) {
1450
+ const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) !== TypeExtendsResult.False);
1451
+ return narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options);
1452
+ } else {
1453
+ return TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? TypeClone.Clone(left, options) : this.Never(options);
1454
+ }
1455
+ }
1456
+ /** `[Standard]` Creates an Integer type */
1457
+ Integer(options = {}) {
1458
+ return this.Create({ ...options, [exports.Kind]: "Integer", type: "integer" });
1459
+ }
1460
+ Intersect(allOf, options = {}) {
1461
+ if (allOf.length === 0)
1462
+ return exports.Type.Never();
1463
+ if (allOf.length === 1)
1464
+ return TypeClone.Clone(allOf[0], options);
1465
+ const objects = allOf.every((schema) => TypeGuard.TObject(schema));
1466
+ const cloned = allOf.map((schema) => TypeClone.Clone(schema, {}));
1467
+ const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties) ? { unevaluatedProperties: TypeClone.Clone(options.unevaluatedProperties, {}) } : {};
1468
+ if (options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects) {
1469
+ return this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: "Intersect", type: "object", allOf: cloned });
1470
+ } else {
1471
+ return this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: "Intersect", allOf: cloned });
1472
+ }
1473
+ }
1474
+ /** `[Standard]` Creates a KeyOf type */
1475
+ KeyOf(schema, options = {}) {
1476
+ if (TypeGuard.TRecord(schema)) {
1477
+ const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
1478
+ if (pattern === exports.PatternNumberExact)
1479
+ return this.Number(options);
1480
+ if (pattern === exports.PatternStringExact)
1481
+ return this.String(options);
1482
+ throw Error("StandardTypeBuilder: Unable to resolve key type from Record key pattern");
1483
+ } else {
1484
+ const resolved = KeyResolver.Resolve(schema);
1485
+ if (resolved.length === 0)
1486
+ return this.Never(options);
1487
+ const literals = resolved.map((key) => this.Literal(key));
1488
+ return this.Union(literals, options);
1489
+ }
1490
+ }
1491
+ /** `[Standard]` Creates a Literal type */
1492
+ Literal(value, options = {}) {
1493
+ return this.Create({ ...options, [exports.Kind]: "Literal", const: value, type: typeof value });
1494
+ }
1495
+ /** `[Standard]` Creates a Never type */
1496
+ Never(options = {}) {
1497
+ return this.Create({ ...options, [exports.Kind]: "Never", not: {} });
1498
+ }
1499
+ /** `[Standard]` Creates a Not type. The first argument is the disallowed type, the second is the allowed. */
1500
+ Not(not, schema, options) {
1501
+ return this.Create({ ...options, [exports.Kind]: "Not", allOf: [{ not: TypeClone.Clone(not, {}) }, TypeClone.Clone(schema, {})] });
1502
+ }
1503
+ /** `[Standard]` Creates a Null type */
1504
+ Null(options = {}) {
1505
+ return this.Create({ ...options, [exports.Kind]: "Null", type: "null" });
1506
+ }
1507
+ /** `[Standard]` Creates a Number type */
1508
+ Number(options = {}) {
1509
+ return this.Create({ ...options, [exports.Kind]: "Number", type: "number" });
1510
+ }
1511
+ /** `[Standard]` Creates an Object type */
1512
+ Object(properties, options = {}) {
1513
+ const propertyKeys = globalThis.Object.getOwnPropertyNames(properties);
1514
+ const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key]) || TypeGuard.TReadonlyOptional(properties[key]));
1515
+ const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name));
1516
+ const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Clone(options.additionalProperties, {}) } : {};
1517
+ const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(properties[key], {}) }), {});
1518
+ if (requiredKeys.length > 0) {
1519
+ return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: "Object", type: "object", properties: clonedProperties, required: requiredKeys });
1520
+ } else {
1521
+ return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: "Object", type: "object", properties: clonedProperties });
1522
+ }
1523
+ }
1524
+ Omit(schema, unresolved, options = {}) {
1525
+ const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema2) => schema2.const) : TypeGuard.TLiteral(unresolved) ? [unresolved.const] : TypeGuard.TNever(unresolved) ? [] : unresolved;
1526
+ return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema2) => {
1527
+ if (schema2.required) {
1528
+ schema2.required = schema2.required.filter((key) => !keys.includes(key));
1529
+ if (schema2.required.length === 0)
1530
+ delete schema2.required;
1531
+ }
1532
+ for (const key of globalThis.Object.keys(schema2.properties)) {
1533
+ if (keys.includes(key))
1534
+ delete schema2.properties[key];
1535
+ }
1536
+ return this.Create(schema2);
1537
+ }, options);
1538
+ }
1539
+ /** `[Standard]` Creates a mapped type where all properties are Optional */
1540
+ Partial(schema, options = {}) {
1541
+ function Apply(schema2) {
1542
+ switch (schema2[exports.Modifier]) {
1543
+ case "ReadonlyOptional":
1544
+ schema2[exports.Modifier] = "ReadonlyOptional";
1545
+ break;
1546
+ case "Readonly":
1547
+ schema2[exports.Modifier] = "ReadonlyOptional";
1548
+ break;
1549
+ case "Optional":
1550
+ schema2[exports.Modifier] = "Optional";
1551
+ break;
1552
+ default:
1553
+ schema2[exports.Modifier] = "Optional";
1554
+ break;
1555
+ }
1556
+ }
1557
+ return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema2) => {
1558
+ delete schema2.required;
1559
+ globalThis.Object.keys(schema2.properties).forEach((key) => Apply(schema2.properties[key]));
1560
+ return schema2;
1561
+ }, options);
1562
+ }
1563
+ Pick(schema, unresolved, options = {}) {
1564
+ const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema2) => schema2.const) : TypeGuard.TLiteral(unresolved) ? [unresolved.const] : TypeGuard.TNever(unresolved) ? [] : unresolved;
1565
+ return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema2) => {
1566
+ if (schema2.required) {
1567
+ schema2.required = schema2.required.filter((key) => keys.includes(key));
1568
+ if (schema2.required.length === 0)
1569
+ delete schema2.required;
1570
+ }
1571
+ for (const key of globalThis.Object.keys(schema2.properties)) {
1572
+ if (!keys.includes(key))
1573
+ delete schema2.properties[key];
1574
+ }
1575
+ return this.Create(schema2);
1576
+ }, options);
1577
+ }
1578
+ /** `[Standard]` Creates a Record type */
1579
+ Record(key, schema, options = {}) {
1580
+ if (TypeGuard.TTemplateLiteral(key)) {
1581
+ const expression = TemplateLiteralParser.ParseExact(key.pattern);
1582
+ return TemplateLiteralFinite.Check(expression) ? this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key2) => ({ ...acc, [key2]: TypeClone.Clone(schema, {}) }), {}), options) : this.Create({ ...options, [exports.Kind]: "Record", type: "object", patternProperties: { [key.pattern]: TypeClone.Clone(schema, {}) }, additionalProperties: false });
1583
+ } else if (TypeGuard.TUnionLiteral(key)) {
1584
+ if (key.anyOf.every((schema2) => TypeGuard.TLiteral(schema2) && (typeof schema2.const === "string" || typeof schema2.const === "number"))) {
1585
+ const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
1586
+ return this.Object(properties, { ...options, [exports.Hint]: "Record" });
1587
+ } else
1588
+ throw Error("TypeBuilder: Record key can only be derived from union literal of number or string");
1589
+ } else if (TypeGuard.TLiteral(key)) {
1590
+ if (typeof key.const === "string" || typeof key.const === "number") {
1591
+ return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options);
1592
+ } else
1593
+ throw Error("TypeBuilder: Record key can only be derived from literals of number or string");
1594
+ } else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
1595
+ const pattern = exports.PatternNumberExact;
1596
+ return this.Create({ ...options, [exports.Kind]: "Record", type: "object", patternProperties: { [pattern]: TypeClone.Clone(schema, {}) }, additionalProperties: false });
1597
+ } else if (TypeGuard.TString(key)) {
1598
+ const pattern = key.pattern === void 0 ? exports.PatternStringExact : key.pattern;
1599
+ return this.Create({ ...options, [exports.Kind]: "Record", type: "object", patternProperties: { [pattern]: TypeClone.Clone(schema, {}) }, additionalProperties: false });
1600
+ } else {
1601
+ throw Error(`StandardTypeBuilder: Invalid Record Key`);
1602
+ }
1603
+ }
1604
+ /** `[Standard]` Creates a Recursive type */
1605
+ Recursive(callback, options = {}) {
1606
+ if (options.$id === void 0)
1607
+ options.$id = `T${TypeOrdinal++}`;
1608
+ const thisType = callback({ [exports.Kind]: "This", $ref: `${options.$id}` });
1609
+ thisType.$id = options.$id;
1610
+ return this.Create({ ...options, [exports.Hint]: "Recursive", ...thisType });
1611
+ }
1612
+ /** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
1613
+ Ref(schema, options = {}) {
1614
+ if (schema.$id === void 0)
1615
+ throw Error("StandardTypeBuilder.Ref: Target type must specify an $id");
1616
+ return this.Create({ ...options, [exports.Kind]: "Ref", $ref: schema.$id });
1617
+ }
1618
+ /** `[Standard]` Creates a mapped type where all properties are Required */
1619
+ Required(schema, options = {}) {
1620
+ function Apply(schema2) {
1621
+ switch (schema2[exports.Modifier]) {
1622
+ case "ReadonlyOptional":
1623
+ schema2[exports.Modifier] = "Readonly";
1624
+ break;
1625
+ case "Readonly":
1626
+ schema2[exports.Modifier] = "Readonly";
1627
+ break;
1628
+ case "Optional":
1629
+ delete schema2[exports.Modifier];
1630
+ break;
1631
+ default:
1632
+ delete schema2[exports.Modifier];
1633
+ break;
1634
+ }
1635
+ }
1636
+ return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema2) => {
1637
+ schema2.required = globalThis.Object.keys(schema2.properties);
1638
+ globalThis.Object.keys(schema2.properties).forEach((key) => Apply(schema2.properties[key]));
1639
+ return schema2;
1640
+ }, options);
1641
+ }
1642
+ /** `[Standard]` Creates a String type */
1643
+ String(options = {}) {
1644
+ return this.Create({ ...options, [exports.Kind]: "String", type: "string" });
1645
+ }
1646
+ /** `[Standard]` Creates a template literal type */
1647
+ TemplateLiteral(kinds, options = {}) {
1648
+ const pattern = TemplateLiteralPattern.Create(kinds);
1649
+ return this.Create({ ...options, [exports.Kind]: "TemplateLiteral", type: "string", pattern });
1650
+ }
1651
+ /** `[Standard]` Creates a Tuple type */
1652
+ Tuple(items, options = {}) {
1653
+ const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
1654
+ const clonedItems = items.map((item) => TypeClone.Clone(item, {}));
1655
+ const schema = items.length > 0 ? { ...options, [exports.Kind]: "Tuple", type: "array", items: clonedItems, additionalItems, minItems, maxItems } : { ...options, [exports.Kind]: "Tuple", type: "array", minItems, maxItems };
1656
+ return this.Create(schema);
1657
+ }
1658
+ Union(union, options = {}) {
1659
+ if (TypeGuard.TTemplateLiteral(union)) {
1660
+ return TemplateLiteralResolver.Resolve(union);
1661
+ } else {
1662
+ const anyOf = union;
1663
+ if (anyOf.length === 0)
1664
+ return this.Never(options);
1665
+ if (anyOf.length === 1)
1666
+ return this.Create(TypeClone.Clone(anyOf[0], options));
1667
+ const clonedAnyOf = anyOf.map((schema) => TypeClone.Clone(schema, {}));
1668
+ return this.Create({ ...options, [exports.Kind]: "Union", anyOf: clonedAnyOf });
1669
+ }
1670
+ }
1671
+ /** `[Standard]` Creates an Unknown type */
1672
+ Unknown(options = {}) {
1673
+ return this.Create({ ...options, [exports.Kind]: "Unknown" });
1674
+ }
1675
+ /** `[Standard]` Creates a Unsafe type that infers for the generic argument */
1676
+ Unsafe(options = {}) {
1677
+ return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || "Unsafe" });
1678
+ }
1679
+ };
1680
+ exports.StandardTypeBuilder = StandardTypeBuilder;
1681
+ var ExtendedTypeBuilder = class extends StandardTypeBuilder {
1682
+ /** `[Extended]` Creates a BigInt type */
1683
+ BigInt(options = {}) {
1684
+ return this.Create({ ...options, [exports.Kind]: "BigInt", type: "null", typeOf: "BigInt" });
1685
+ }
1686
+ /** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
1687
+ ConstructorParameters(schema, options = {}) {
1688
+ return this.Tuple([...schema.parameters], { ...options });
1689
+ }
1690
+ Constructor(parameters, returns, options = {}) {
1691
+ const clonedReturns = TypeClone.Clone(returns, {});
1692
+ if (TypeGuard.TTuple(parameters)) {
1693
+ const clonedParameters = parameters.items === void 0 ? [] : parameters.items.map((parameter) => TypeClone.Clone(parameter, {}));
1694
+ return this.Create({ ...options, [exports.Kind]: "Constructor", type: "object", instanceOf: "Constructor", parameters: clonedParameters, returns: clonedReturns });
1695
+ } else if (globalThis.Array.isArray(parameters)) {
1696
+ const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
1697
+ return this.Create({ ...options, [exports.Kind]: "Constructor", type: "object", instanceOf: "Constructor", parameters: clonedParameters, returns: clonedReturns });
1698
+ } else {
1699
+ throw new Error("ExtendedTypeBuilder.Constructor: Invalid parameters");
1700
+ }
1701
+ }
1702
+ /** `[Extended]` Creates a Date type */
1703
+ Date(options = {}) {
1704
+ return this.Create({ ...options, [exports.Kind]: "Date", type: "object", instanceOf: "Date" });
1705
+ }
1706
+ Function(parameters, returns, options = {}) {
1707
+ const clonedReturns = TypeClone.Clone(returns, {});
1708
+ if (TypeGuard.TTuple(parameters)) {
1709
+ const clonedParameters = parameters.items === void 0 ? [] : parameters.items.map((parameter) => TypeClone.Clone(parameter, {}));
1710
+ return this.Create({ ...options, [exports.Kind]: "Function", type: "object", instanceOf: "Function", parameters: clonedParameters, returns: clonedReturns });
1711
+ } else if (globalThis.Array.isArray(parameters)) {
1712
+ const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
1713
+ return this.Create({ ...options, [exports.Kind]: "Function", type: "object", instanceOf: "Function", parameters: clonedParameters, returns: clonedReturns });
1714
+ } else {
1715
+ throw new Error("ExtendedTypeBuilder.Function: Invalid parameters");
1716
+ }
1717
+ }
1718
+ /** `[Extended]` Extracts the InstanceType from the given Constructor */
1719
+ InstanceType(schema, options = {}) {
1720
+ return TypeClone.Clone(schema.returns, options);
1721
+ }
1722
+ /** `[Extended]` Extracts the Parameters from the given Function type */
1723
+ Parameters(schema, options = {}) {
1724
+ return this.Tuple(schema.parameters, { ...options });
1725
+ }
1726
+ /** `[Extended]` Creates a Promise type */
1727
+ Promise(item, options = {}) {
1728
+ return this.Create({ ...options, [exports.Kind]: "Promise", type: "object", instanceOf: "Promise", item: TypeClone.Clone(item, {}) });
1729
+ }
1730
+ /** `[Extended]` Creates a regular expression type */
1731
+ RegEx(regex, options = {}) {
1732
+ return this.Create({ ...options, [exports.Kind]: "String", type: "string", pattern: regex.source });
1733
+ }
1734
+ /** `[Extended]` Extracts the ReturnType from the given Function */
1735
+ ReturnType(schema, options = {}) {
1736
+ return TypeClone.Clone(schema.returns, options);
1737
+ }
1738
+ /** `[Extended]` Creates a Symbol type */
1739
+ Symbol(options) {
1740
+ return this.Create({ ...options, [exports.Kind]: "Symbol", type: "null", typeOf: "Symbol" });
1741
+ }
1742
+ /** `[Extended]` Creates a Undefined type */
1743
+ Undefined(options = {}) {
1744
+ return this.Create({ ...options, [exports.Kind]: "Undefined", type: "null", typeOf: "Undefined" });
1745
+ }
1746
+ /** `[Extended]` Creates a Uint8Array type */
1747
+ Uint8Array(options = {}) {
1748
+ return this.Create({ ...options, [exports.Kind]: "Uint8Array", type: "object", instanceOf: "Uint8Array" });
1749
+ }
1750
+ /** `[Extended]` Creates a Void type */
1751
+ Void(options = {}) {
1752
+ return this.Create({ ...options, [exports.Kind]: "Void", type: "null", typeOf: "Void" });
1753
+ }
1754
+ };
1755
+ exports.ExtendedTypeBuilder = ExtendedTypeBuilder;
1756
+ exports.StandardType = new StandardTypeBuilder();
1757
+ exports.Type = new ExtendedTypeBuilder();
1758
+ }
1759
+ });
1760
+
1761
+ // src/plugin/index.ts
1762
+ var import_typebox = __toESM(require_typebox(), 1);
1763
+ import { execFileSync as execFileSync2, execFile } from "child_process";
1764
+ import { existsSync as existsSync5, readFileSync as readFileSync5, mkdirSync as mkdirSync2, writeFileSync as writeFileSync2, renameSync, statSync as statSync3 } from "fs";
1765
+ import { join as join5, basename } from "path";
1766
+
1767
+ // src/plugin/templates.ts
1768
+ import { existsSync, readdirSync, readFileSync } from "fs";
1769
+ import { join } from "path";
1770
+ var DEFAULT_SCHEMAS = [
1771
+ {
1772
+ primitive: "memory_event",
1773
+ description: "General memory event for observations",
1774
+ fields: {
1775
+ type: { type: "string", required: true, default: "memory_event" },
1776
+ status: { type: "string", required: true, default: "recorded", enum: ["recorded", "superseded", "corrected"] },
1777
+ created: { type: "datetime", required: true, default: "{{datetime}}" },
1778
+ observed_at: { type: "datetime", required: true },
1779
+ source: { type: "string", required: true, enum: ["openclaw", "claude-code", "replay", "manual-correction"] },
1780
+ summary: { type: "string", required: true },
1781
+ confidence: { type: "number" },
1782
+ importance: { type: "number" }
1783
+ },
1784
+ keywords: ["preference", "like", "hate", "want", "need", "always", "never", "remember", "note"]
1785
+ },
1786
+ {
1787
+ primitive: "person",
1788
+ description: "People and relationship notes",
1789
+ fields: {
1790
+ title: { type: "string", required: true, default: "{{title}}" },
1791
+ date: { type: "date", required: true, default: "{{date}}" },
1792
+ type: { type: "string", required: true, default: "person" },
1793
+ relationship: { type: "string", default: "contact" }
1794
+ },
1795
+ keywords: ["person", "contact", "colleague", "friend", "works at", "lives in", "email", "phone", "name is"]
1796
+ },
1797
+ {
1798
+ primitive: "decision",
1799
+ description: "Decision records",
1800
+ fields: {
1801
+ title: { type: "string", required: true, default: "{{title}}" },
1802
+ date: { type: "date", required: true, default: "{{date}}" },
1803
+ type: { type: "string", required: true, default: "decision" },
1804
+ status: { type: "string", default: "decided", enum: ["proposed", "decided", "superseded"] }
1805
+ },
1806
+ keywords: ["decided", "decision", "chose", "will use", "go with", "ship", "approved", "rejected"]
1807
+ },
1808
+ {
1809
+ primitive: "task",
1810
+ description: "Task primitives",
1811
+ fields: {
1812
+ status: { type: "string", required: true, default: "open", enum: ["open", "in-progress", "blocked", "done"] },
1813
+ created: { type: "datetime", required: true, default: "{{datetime}}" },
1814
+ updated: { type: "datetime", required: true, default: "{{datetime}}" },
1815
+ priority: { type: "string", enum: ["critical", "high", "medium", "low"] },
1816
+ due: { type: "date" }
1817
+ },
1818
+ keywords: ["task", "todo", "need to", "should", "must", "deadline", "due", "by tomorrow", "by tonight"]
1819
+ },
1820
+ {
1821
+ primitive: "project",
1822
+ description: "Project definition documents",
1823
+ fields: {
1824
+ type: { type: "string", required: true, default: "project" },
1825
+ status: { type: "string", required: true, default: "active", enum: ["active", "paused", "completed", "archived"] },
1826
+ created: { type: "datetime", required: true, default: "{{datetime}}" },
1827
+ updated: { type: "datetime", required: true, default: "{{datetime}}" }
1828
+ },
1829
+ keywords: ["project", "initiative", "working on", "building", "developing"]
1830
+ },
1831
+ {
1832
+ primitive: "lesson",
1833
+ description: "Lessons learned",
1834
+ fields: {
1835
+ title: { type: "string", required: true, default: "{{title}}" },
1836
+ date: { type: "date", required: true, default: "{{date}}" },
1837
+ type: { type: "string", required: true, default: "lesson" }
1838
+ },
1839
+ keywords: ["learned", "lesson", "insight", "realized", "discovered", "found out"]
1840
+ }
1841
+ ];
1842
+ function parseYamlFrontmatter(content) {
1843
+ const match = content.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
1844
+ if (!match) return null;
1845
+ try {
1846
+ const frontmatter = parseSimpleYaml(match[1]);
1847
+ return { frontmatter, body: match[2] };
1848
+ } catch {
1849
+ return null;
1850
+ }
1851
+ }
1852
+ function parseSimpleYaml(yaml) {
1853
+ const result = {};
1854
+ const lines = yaml.split("\n");
1855
+ let currentKey = "";
1856
+ let nestedObject = null;
1857
+ let nestedKey = "";
1858
+ for (const line of lines) {
1859
+ if (!line.trim() || line.trim().startsWith("#")) continue;
1860
+ const indent = line.search(/\S/);
1861
+ const trimmed = line.trim();
1862
+ if (trimmed.startsWith("- ")) {
1863
+ const value = trimmed.slice(2).trim();
1864
+ if (nestedObject && nestedKey) {
1865
+ const arr = nestedObject[nestedKey];
1866
+ if (Array.isArray(arr)) arr.push(parseYamlValue(value));
1867
+ } else if (currentKey && result[currentKey]) {
1868
+ const arr = result[currentKey];
1869
+ if (Array.isArray(arr)) arr.push(parseYamlValue(value));
1870
+ }
1871
+ continue;
1872
+ }
1873
+ const colonIndex = trimmed.indexOf(":");
1874
+ if (colonIndex === -1) continue;
1875
+ const key = trimmed.slice(0, colonIndex).trim();
1876
+ const valueStr = trimmed.slice(colonIndex + 1).trim();
1877
+ if (indent === 0) {
1878
+ if (valueStr === "" || valueStr === "|" || valueStr === ">") {
1879
+ if (key === "fields") {
1880
+ result[key] = {};
1881
+ nestedObject = result[key];
1882
+ nestedKey = "";
1883
+ } else {
1884
+ result[key] = {};
1885
+ nestedObject = null;
1886
+ }
1887
+ } else {
1888
+ result[key] = parseYamlValue(valueStr);
1889
+ nestedObject = null;
1890
+ }
1891
+ currentKey = key;
1892
+ } else if (nestedObject && indent > 0) {
1893
+ if (valueStr === "" || valueStr === "|" || valueStr === ">") {
1894
+ nestedObject[key] = {};
1895
+ nestedKey = key;
1896
+ } else if (nestedKey && indent > 2) {
1897
+ const fieldObj = nestedObject[nestedKey];
1898
+ if (fieldObj) {
1899
+ if (key === "enum") {
1900
+ fieldObj[key] = [];
1901
+ } else {
1902
+ fieldObj[key] = parseYamlValue(valueStr);
1903
+ }
1904
+ }
1905
+ } else {
1906
+ nestedObject[key] = parseYamlValue(valueStr);
1907
+ nestedKey = key;
1908
+ }
1909
+ }
1910
+ }
1911
+ return result;
1912
+ }
1913
+ function parseYamlValue(value) {
1914
+ if (value === "" || value === "null" || value === "~") return null;
1915
+ if (value === "true") return true;
1916
+ if (value === "false") return false;
1917
+ if (/^-?\d+$/.test(value)) return parseInt(value, 10);
1918
+ if (/^-?\d+\.\d+$/.test(value)) return parseFloat(value);
1919
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
1920
+ return value.slice(1, -1);
1921
+ }
1922
+ return value;
1923
+ }
1924
+ var registry = null;
1925
+ function getTemplateRegistry() {
1926
+ if (!registry) {
1927
+ registry = {
1928
+ schemas: /* @__PURE__ */ new Map(),
1929
+ keywordIndex: /* @__PURE__ */ new Map(),
1930
+ initialized: false
1931
+ };
1932
+ }
1933
+ return registry;
1934
+ }
1935
+ function initializeTemplateRegistry(templatesDir) {
1936
+ const reg = getTemplateRegistry();
1937
+ if (reg.initialized) return reg;
1938
+ const dirsToTry = templatesDir ? [templatesDir] : [
1939
+ join(process.cwd(), "templates"),
1940
+ join(process.cwd(), "..", "..", "templates"),
1941
+ join(process.env.HOME ?? ".", "clawvault", "templates"),
1942
+ join(process.env.HOME ?? ".", ".clawvault", "templates")
1943
+ ];
1944
+ let loaded = false;
1945
+ for (const dir of dirsToTry) {
1946
+ if (existsSync(dir)) {
1947
+ try {
1948
+ loadTemplatesFromDirectory(dir, reg);
1949
+ loaded = true;
1950
+ break;
1951
+ } catch {
1952
+ }
1953
+ }
1954
+ }
1955
+ if (!loaded || reg.schemas.size === 0) {
1956
+ loadDefaultSchemas(reg);
1957
+ }
1958
+ buildKeywordIndex(reg);
1959
+ reg.initialized = true;
1960
+ return reg;
1961
+ }
1962
+ function loadTemplatesFromDirectory(dir, reg) {
1963
+ const files = readdirSync(dir).filter((f) => f.endsWith(".md"));
1964
+ for (const file of files) {
1965
+ const filePath = join(dir, file);
1966
+ const content = readFileSync(filePath, "utf-8");
1967
+ const parsed = parseYamlFrontmatter(content);
1968
+ if (!parsed?.frontmatter?.primitive) continue;
1969
+ const schema = convertFrontmatterToSchema(
1970
+ parsed.frontmatter,
1971
+ parsed.body
1972
+ );
1973
+ if (schema) {
1974
+ reg.schemas.set(schema.primitive, schema);
1975
+ }
1976
+ }
1977
+ }
1978
+ function convertFrontmatterToSchema(fm, body) {
1979
+ const primitive = fm.primitive;
1980
+ if (!primitive) return null;
1981
+ const fields = {};
1982
+ const fmFields = fm.fields;
1983
+ if (fmFields) {
1984
+ for (const [fieldName, fieldDef] of Object.entries(fmFields)) {
1985
+ if (typeof fieldDef === "object" && fieldDef !== null) {
1986
+ fields[fieldName] = {
1987
+ type: fieldDef.type || "string",
1988
+ required: fieldDef.required,
1989
+ default: fieldDef.default,
1990
+ enum: fieldDef.enum,
1991
+ description: fieldDef.description
1992
+ };
1993
+ }
1994
+ }
1995
+ }
1996
+ const keywords = extractKeywordsFromSchema(primitive, fm.description, fields);
1997
+ return {
1998
+ primitive,
1999
+ description: fm.description,
2000
+ fields,
2001
+ bodyTemplate: body,
2002
+ keywords
2003
+ };
2004
+ }
2005
+ function extractKeywordsFromSchema(primitive, _description, fields) {
2006
+ const keywords = [primitive];
2007
+ keywords.push(primitive.replace(/-/g, " "));
2008
+ keywords.push(primitive.replace(/_/g, " "));
2009
+ const keywordMap = {
2010
+ memory_event: ["preference", "like", "hate", "want", "need", "always", "never", "remember", "note"],
2011
+ person: ["person", "contact", "colleague", "friend", "works at", "lives in", "email", "phone", "name is"],
2012
+ decision: ["decided", "decision", "chose", "will use", "go with", "ship", "approved", "rejected"],
2013
+ task: ["task", "todo", "need to", "should", "must", "deadline", "due", "by tomorrow", "by tonight"],
2014
+ project: ["project", "initiative", "working on", "building", "developing"],
2015
+ lesson: ["learned", "lesson", "insight", "realized", "discovered", "found out"],
2016
+ trigger: ["trigger", "schedule", "cron", "automated", "recurring"],
2017
+ run: ["run", "execution", "job", "started", "finished", "failed"],
2018
+ checkpoint: ["checkpoint", "snapshot", "state", "progress"],
2019
+ handoff: ["handoff", "transition", "context", "resume"],
2020
+ "daily-note": ["daily", "today", "journal", "log"],
2021
+ daily: ["daily", "today", "journal", "log"],
2022
+ party: ["party", "agent", "human", "runtime", "service"],
2023
+ workspace: ["workspace", "shared", "collaboration"]
2024
+ };
2025
+ if (keywordMap[primitive]) {
2026
+ keywords.push(...keywordMap[primitive]);
2027
+ }
2028
+ if (fields.status?.enum) {
2029
+ keywords.push(...fields.status.enum);
2030
+ }
2031
+ return [...new Set(keywords)];
2032
+ }
2033
+ function loadDefaultSchemas(reg) {
2034
+ for (const schema of DEFAULT_SCHEMAS) {
2035
+ reg.schemas.set(schema.primitive, schema);
2036
+ }
2037
+ }
2038
+ function buildKeywordIndex(reg) {
2039
+ reg.keywordIndex.clear();
2040
+ for (const [primitive, schema] of reg.schemas) {
2041
+ const keywords = schema.keywords ?? [primitive];
2042
+ for (const keyword of keywords) {
2043
+ const lower = keyword.toLowerCase();
2044
+ const existing = reg.keywordIndex.get(lower) ?? [];
2045
+ if (!existing.includes(primitive)) {
2046
+ existing.push(primitive);
2047
+ }
2048
+ reg.keywordIndex.set(lower, existing);
2049
+ }
2050
+ }
2051
+ }
2052
+ function classifyText(text) {
2053
+ const reg = getTemplateRegistry();
2054
+ if (!reg.initialized) initializeTemplateRegistry();
2055
+ const lower = text.toLowerCase();
2056
+ const scores = /* @__PURE__ */ new Map();
2057
+ for (const [keyword, primitives] of reg.keywordIndex) {
2058
+ if (lower.includes(keyword)) {
2059
+ for (const primitive of primitives) {
2060
+ const existing = scores.get(primitive) ?? { score: 0, keywords: [] };
2061
+ existing.score += getKeywordWeight(keyword, primitive);
2062
+ existing.keywords.push(keyword);
2063
+ scores.set(primitive, existing);
2064
+ }
2065
+ }
2066
+ }
2067
+ applyPatternScoring(lower, scores);
2068
+ let bestPrimitive = "memory_event";
2069
+ let bestScore = 0;
2070
+ let bestKeywords = [];
2071
+ for (const [primitive, data] of scores) {
2072
+ if (data.score > bestScore) {
2073
+ bestScore = data.score;
2074
+ bestPrimitive = primitive;
2075
+ bestKeywords = data.keywords;
2076
+ }
2077
+ }
2078
+ const confidence = Math.min(1, bestScore / 5);
2079
+ return {
2080
+ primitiveType: bestPrimitive,
2081
+ confidence,
2082
+ matchedKeywords: [...new Set(bestKeywords)]
2083
+ };
2084
+ }
2085
+ function getKeywordWeight(keyword, primitive) {
2086
+ if (keyword === primitive || keyword === primitive.replace(/-/g, " ")) return 3;
2087
+ const strongIndicators = {
2088
+ person: ["works at", "lives in", "email", "phone", "name is"],
2089
+ decision: ["decided", "chose", "will use", "go with"],
2090
+ task: ["deadline", "due", "by tomorrow", "by tonight"],
2091
+ memory_event: ["preference", "remember", "note"]
2092
+ };
2093
+ if (strongIndicators[primitive]?.includes(keyword)) return 2;
2094
+ return 1;
2095
+ }
2096
+ function applyPatternScoring(text, scores) {
2097
+ const patterns = [
2098
+ { regex: /\b(my .+ is|his .+ is|her .+ is|their .+ is)\b/i, primitive: "person", weight: 2, label: "possessive pattern" },
2099
+ { regex: /[\w.-]+@[\w.-]+\.\w+|\+\d{10,}/, primitive: "person", weight: 3, label: "contact info" },
2100
+ { regex: /\b(i prefer|i like|i hate|i love|i want|i need|i always|i never|don't like|dont like)\b/i, primitive: "memory_event", weight: 3, label: "preference pattern" },
2101
+ { regex: /\b(we decided|let's go with|we're going|i chose|we'll use|ship it|do it)\b/i, primitive: "decision", weight: 3, label: "decision pattern" },
2102
+ { regex: /\b(by tonight|by tomorrow|deadline|due date|by end of|ship by|ready by)\b/i, primitive: "task", weight: 2, label: "deadline pattern" }
2103
+ ];
2104
+ for (const { regex, primitive, weight, label } of patterns) {
2105
+ if (regex.test(text)) {
2106
+ const existing = scores.get(primitive) ?? { score: 0, keywords: [] };
2107
+ existing.score += weight;
2108
+ existing.keywords.push(label);
2109
+ scores.set(primitive, existing);
2110
+ }
2111
+ }
2112
+ }
2113
+ function getSchema(primitiveType) {
2114
+ const reg = getTemplateRegistry();
2115
+ if (!reg.initialized) initializeTemplateRegistry();
2116
+ return reg.schemas.get(primitiveType);
2117
+ }
2118
+ function getAllSchemas() {
2119
+ const reg = getTemplateRegistry();
2120
+ if (!reg.initialized) initializeTemplateRegistry();
2121
+ return Array.from(reg.schemas.values());
2122
+ }
2123
+ function getSchemaNames() {
2124
+ const reg = getTemplateRegistry();
2125
+ if (!reg.initialized) initializeTemplateRegistry();
2126
+ return Array.from(reg.schemas.keys());
2127
+ }
2128
+ function generateFrontmatter(primitiveType, options = {}) {
2129
+ const schema = getSchema(primitiveType);
2130
+ if (!schema) {
2131
+ return {
2132
+ type: primitiveType,
2133
+ created: (/* @__PURE__ */ new Date()).toISOString(),
2134
+ updated: (/* @__PURE__ */ new Date()).toISOString()
2135
+ };
2136
+ }
2137
+ const frontmatter = {};
2138
+ const now = /* @__PURE__ */ new Date();
2139
+ const dateStr = now.toISOString().split("T")[0];
2140
+ const datetimeStr = now.toISOString();
2141
+ for (const [fieldName, fieldDef] of Object.entries(schema.fields)) {
2142
+ if (options.extraFields?.[fieldName] !== void 0) {
2143
+ const value = options.extraFields[fieldName];
2144
+ if (fieldDef.enum && !fieldDef.enum.includes(String(value))) {
2145
+ frontmatter[fieldName] = fieldDef.default ?? fieldDef.enum[0];
2146
+ } else {
2147
+ frontmatter[fieldName] = value;
2148
+ }
2149
+ continue;
2150
+ }
2151
+ if (fieldDef.default !== void 0) {
2152
+ let defaultValue = fieldDef.default;
2153
+ if (typeof defaultValue === "string") {
2154
+ defaultValue = defaultValue.replace("{{datetime}}", datetimeStr).replace("{{date}}", dateStr).replace("{{title}}", options.title ?? "Untitled");
2155
+ }
2156
+ frontmatter[fieldName] = defaultValue;
2157
+ } else if (fieldDef.required) {
2158
+ switch (fieldDef.type) {
2159
+ case "datetime":
2160
+ frontmatter[fieldName] = datetimeStr;
2161
+ break;
2162
+ case "date":
2163
+ frontmatter[fieldName] = dateStr;
2164
+ break;
2165
+ case "string":
2166
+ frontmatter[fieldName] = fieldDef.enum?.length ? fieldDef.enum[0] : "";
2167
+ break;
2168
+ case "number":
2169
+ frontmatter[fieldName] = 0;
2170
+ break;
2171
+ case "boolean":
2172
+ frontmatter[fieldName] = false;
2173
+ break;
2174
+ }
2175
+ }
2176
+ }
2177
+ if (options.source && schema.fields.source) frontmatter.source = options.source;
2178
+ if (options.sessionId && schema.fields.session_id) frontmatter.session_id = options.sessionId;
2179
+ return frontmatter;
2180
+ }
2181
+ function validateFrontmatter(primitiveType, frontmatter) {
2182
+ const schema = getSchema(primitiveType);
2183
+ if (!schema) return { valid: true, errors: [] };
2184
+ const errors = [];
2185
+ for (const [fieldName, fieldDef] of Object.entries(schema.fields)) {
2186
+ const value = frontmatter[fieldName];
2187
+ if (fieldDef.required && (value === void 0 || value === null || value === "")) {
2188
+ errors.push(`Missing required field: ${fieldName}`);
2189
+ continue;
2190
+ }
2191
+ if (value === void 0 || value === null) continue;
2192
+ if (fieldDef.enum && !fieldDef.enum.includes(String(value))) {
2193
+ errors.push(`Invalid value for ${fieldName}: "${String(value)}". Must be one of: ${fieldDef.enum.join(", ")}`);
2194
+ }
2195
+ switch (fieldDef.type) {
2196
+ case "number":
2197
+ if (typeof value !== "number" && isNaN(Number(value))) {
2198
+ errors.push(`Field ${fieldName} must be a number`);
2199
+ }
2200
+ break;
2201
+ case "boolean":
2202
+ if (typeof value !== "boolean" && value !== "true" && value !== "false") {
2203
+ errors.push(`Field ${fieldName} must be a boolean`);
2204
+ }
2205
+ break;
2206
+ case "datetime":
2207
+ if (typeof value === "string" && isNaN(Date.parse(value))) {
2208
+ errors.push(`Field ${fieldName} must be a valid datetime`);
2209
+ }
2210
+ break;
2211
+ case "date":
2212
+ if (typeof value === "string" && !/^\d{4}-\d{2}-\d{2}$/.test(value)) {
2213
+ errors.push(`Field ${fieldName} must be a valid date (YYYY-MM-DD)`);
2214
+ }
2215
+ break;
2216
+ }
2217
+ }
2218
+ return { valid: errors.length === 0, errors };
2219
+ }
2220
+ function serializeFrontmatter(frontmatter) {
2221
+ const lines = ["---"];
2222
+ for (const [key, value] of Object.entries(frontmatter)) {
2223
+ if (value === void 0 || value === null) continue;
2224
+ if (Array.isArray(value)) {
2225
+ lines.push(`${key}:`);
2226
+ for (const item of value) {
2227
+ lines.push(` - ${String(item)}`);
2228
+ }
2229
+ } else if (typeof value === "object") {
2230
+ lines.push(`${key}: ${JSON.stringify(value)}`);
2231
+ } else if (typeof value === "string" && value.includes("\n")) {
2232
+ lines.push(`${key}: |`);
2233
+ for (const line of value.split("\n")) {
2234
+ lines.push(` ${line}`);
2235
+ }
2236
+ } else {
2237
+ lines.push(`${key}: ${String(value)}`);
2238
+ }
2239
+ }
2240
+ lines.push("---");
2241
+ return lines.join("\n");
2242
+ }
2243
+
2244
+ // src/plugin/noise-filter.ts
2245
+ var REFUSAL_PATTERNS = [
2246
+ /\b(i can'?t help with|i'?m not able to|i cannot|i'?m unable to|as an ai|i don'?t have the ability)\b/i,
2247
+ /\b(i'?m sorry,?\s+(?:but )?i|unfortunately,?\s+i (?:can'?t|cannot))\b/i,
2248
+ /\b(that'?s (?:beyond|outside) my|i'?m not (?:designed|programmed) to)\b/i,
2249
+ /\b(i (?:must |need to )?(?:decline|refuse)|i won'?t be able to)\b/i
2250
+ ];
2251
+ var META_PATTERNS = [
2252
+ /\b(how does it feel|what'?s it like being|are you (?:sentient|conscious|alive|real))\b/i,
2253
+ /\b(do you have (?:feelings|emotions|consciousness|a soul))\b/i,
2254
+ /\b(what are you|who made you|who created you|what model are you)\b/i,
2255
+ /\b(can you think|do you dream|are you aware)\b/i
2256
+ ];
2257
+ var GREETING_PATTERNS = [
2258
+ /^(?:hi|hello|hey|howdy|greetings|good (?:morning|afternoon|evening)|what'?s up|sup|yo)\s*[!.?]?\s*$/i,
2259
+ /^(?:thanks?|thank you|thx|ty|cheers)\s*[!.?]?\s*$/i,
2260
+ /^(?:ok|okay|sure|got it|understood|perfect|great|cool|nice|awesome)\s*[!.?]?\s*$/i,
2261
+ /^(?:bye|goodbye|see you|later|gn|good night|cya)\s*[!.?]?\s*$/i
2262
+ ];
2263
+ var LOW_INFO_PATTERNS = [
2264
+ /^[!?.]+$/,
2265
+ // Just punctuation
2266
+ /^[\p{Emoji}\s]+$/u,
2267
+ // Emoji-only
2268
+ /^(?:yes|no|maybe|idk|hmm|hm|ah|oh|uh|um|lol|lmao|haha|heh)\s*[!.?]*$/i
2269
+ ];
2270
+ var SYSTEM_NOISE_PATTERNS = [
2271
+ /^(?:\[System|HEARTBEAT|NO_REPLY)/,
2272
+ /^<(?:relevant-memories|session-recap|user-preferences)/,
2273
+ /^\s*\{[\s\S]*"(?:type|action|tool_use)"[\s\S]*\}\s*$/
2274
+ // JSON tool calls
2275
+ ];
2276
+ var DEFAULT_NOISE_CONFIG = {
2277
+ enabled: true,
2278
+ minLength: 15,
2279
+ maxLength: 5e3
2280
+ };
2281
+ function isNoise(text, config = DEFAULT_NOISE_CONFIG) {
2282
+ if (!config.enabled) return { isNoise: false };
2283
+ if (!text) return { isNoise: true, category: "length", reason: "Empty text" };
2284
+ const trimmed = text.trim();
2285
+ if (trimmed.length < config.minLength) {
2286
+ return { isNoise: true, category: "length", reason: `Too short (${trimmed.length} < ${config.minLength})` };
2287
+ }
2288
+ if (trimmed.length > config.maxLength) {
2289
+ return { isNoise: true, category: "length", reason: `Too long (${trimmed.length} > ${config.maxLength})` };
2290
+ }
2291
+ for (const pattern of SYSTEM_NOISE_PATTERNS) {
2292
+ if (pattern.test(trimmed)) {
2293
+ return { isNoise: true, category: "system", reason: "System noise" };
2294
+ }
2295
+ }
2296
+ for (const pattern of GREETING_PATTERNS) {
2297
+ if (pattern.test(trimmed)) {
2298
+ return { isNoise: true, category: "greeting", reason: "Greeting/acknowledgment" };
2299
+ }
2300
+ }
2301
+ for (const pattern of LOW_INFO_PATTERNS) {
2302
+ if (pattern.test(trimmed)) {
2303
+ return { isNoise: true, category: "low_info", reason: "Low information content" };
2304
+ }
2305
+ }
2306
+ for (const pattern of REFUSAL_PATTERNS) {
2307
+ if (pattern.test(trimmed)) {
2308
+ return { isNoise: true, category: "refusal", reason: "AI refusal" };
2309
+ }
2310
+ }
2311
+ for (const pattern of META_PATTERNS) {
2312
+ if (pattern.test(trimmed)) {
2313
+ return { isNoise: true, category: "meta", reason: "Meta-question about AI" };
2314
+ }
2315
+ }
2316
+ const markdownChars = (trimmed.match(/[#*`\-|>]/g) || []).length;
2317
+ if (markdownChars / trimmed.length > 0.15) {
2318
+ return { isNoise: true, category: "system", reason: "High markdown density (likely code/formatting)" };
2319
+ }
2320
+ return { isNoise: false };
2321
+ }
2322
+
2323
+ // src/plugin/observe.ts
2324
+ function isObservable(text, noiseConfig) {
2325
+ if (!text || text.length < 20 || text.length > 5e3) return false;
2326
+ const check = isNoise(text, noiseConfig ?? DEFAULT_NOISE_CONFIG);
2327
+ return !check.isNoise;
2328
+ }
2329
+ var OBSERVATION_PATTERNS = [
2330
+ // Preferences
2331
+ { pattern: /\b(i prefer|i like|i hate|i love|i want|i need|i always|i never|don't like|dont like)\b/i, weight: 2 },
2332
+ // Decisions
2333
+ { pattern: /\b(we decided|let's go with|we're going|i chose|we'll use|ship it|do it|go with)\b/i, weight: 2 },
2334
+ // Facts about people/things
2335
+ { pattern: /\b(my .+ is|his .+ is|her .+ is|their .+ is|works at|lives in|born in)\b/i, weight: 1.5 },
2336
+ // Contact info
2337
+ { pattern: /[\w.-]+@[\w.-]+\.\w+|\+\d{10,}/i, weight: 2 },
2338
+ // Explicit memory request
2339
+ { pattern: /\b(remember|don't forget|keep in mind|note that|important:)\b/i, weight: 2.5 },
2340
+ // Deadlines/dates
2341
+ { pattern: /\b(by tonight|by tomorrow|deadline|due date|by end of|ship by|ready by)\b/i, weight: 1.5 },
2342
+ // Lessons learned
2343
+ { pattern: /\b(i learned|we learned|lesson|realized|discovered|found out)\b/i, weight: 1.5 },
2344
+ // Tasks
2345
+ { pattern: /\b(need to|should|must|have to|todo|task)\b/i, weight: 1 },
2346
+ // Projects
2347
+ { pattern: /\b(working on|building|developing|project|initiative)\b/i, weight: 1 }
2348
+ ];
2349
+ function extractObservations(text) {
2350
+ const observations = [];
2351
+ const sentences = splitIntoSentences(text);
2352
+ const now = /* @__PURE__ */ new Date();
2353
+ for (const sentence of sentences) {
2354
+ if (sentence.length < 15) continue;
2355
+ let totalWeight = 0;
2356
+ for (const { pattern, weight } of OBSERVATION_PATTERNS) {
2357
+ if (pattern.test(sentence)) {
2358
+ totalWeight += weight;
2359
+ }
2360
+ }
2361
+ if (totalWeight < 1) continue;
2362
+ const classification = classifyText(sentence);
2363
+ const category = deriveCategoryFromPrimitive(classification.primitiveType, sentence);
2364
+ const tags = generateTags(classification, sentence);
2365
+ observations.push({
2366
+ text: sentence.trim(),
2367
+ primitiveType: classification.primitiveType,
2368
+ confidence: classification.confidence,
2369
+ matchedKeywords: classification.matchedKeywords,
2370
+ category,
2371
+ tags,
2372
+ extractedAt: now
2373
+ });
2374
+ }
2375
+ return observations;
2376
+ }
2377
+ function splitIntoSentences(text) {
2378
+ const raw = text.split(/(?<=[.!?\n])\s+/);
2379
+ return raw.map((s) => s.trim()).filter((s) => s.length > 0);
2380
+ }
2381
+ function deriveCategoryFromPrimitive(primitiveType, text) {
2382
+ const lower = text.toLowerCase();
2383
+ if (primitiveType === "memory_event") {
2384
+ if (/prefer|like|love|hate|want|need|always|never/i.test(lower)) return "preference";
2385
+ if (/remember|don't forget|keep in mind|note that/i.test(lower)) return "note";
2386
+ return "fact";
2387
+ }
2388
+ const categoryMap = {
2389
+ person: "entity",
2390
+ decision: "decision",
2391
+ task: "task",
2392
+ project: "project",
2393
+ lesson: "lesson",
2394
+ trigger: "automation",
2395
+ run: "execution",
2396
+ checkpoint: "checkpoint",
2397
+ handoff: "handoff",
2398
+ "daily-note": "daily",
2399
+ daily: "daily",
2400
+ party: "entity",
2401
+ workspace: "workspace"
2402
+ };
2403
+ return categoryMap[primitiveType] ?? "fact";
2404
+ }
2405
+ function generateTags(classification, text) {
2406
+ const tags = [classification.primitiveType];
2407
+ const lower = text.toLowerCase();
2408
+ if (/prefer|like|love/i.test(lower)) tags.push("positive");
2409
+ if (/hate|dislike|don't like/i.test(lower)) tags.push("negative");
2410
+ if (/deadline|due|by tomorrow|by tonight/i.test(lower)) tags.push("time-sensitive");
2411
+ if (/important|critical|urgent/i.test(lower)) tags.push("high-priority");
2412
+ if (/email|phone|contact/i.test(lower)) tags.push("contact-info");
2413
+ if (/decided|chose|approved/i.test(lower)) tags.push("finalized");
2414
+ if (/proposed|considering|might/i.test(lower)) tags.push("tentative");
2415
+ return [...new Set(tags)];
2416
+ }
2417
+ function processMessageForObservations(content, _options = {}) {
2418
+ if (!isObservable(content)) {
2419
+ return {
2420
+ observations: [],
2421
+ skipped: 1,
2422
+ reason: "Content not observable"
2423
+ };
2424
+ }
2425
+ const observations = extractObservations(content);
2426
+ const maxObservations = 5;
2427
+ const limited = observations.slice(0, maxObservations);
2428
+ const skipped = observations.length - limited.length;
2429
+ return {
2430
+ observations: limited,
2431
+ skipped,
2432
+ reason: skipped > 0 ? `Limited to ${maxObservations} observations` : void 0
2433
+ };
2434
+ }
2435
+ function detectCategory(text) {
2436
+ const classification = classifyText(text);
2437
+ return deriveCategoryFromPrimitive(classification.primitiveType, text);
2438
+ }
2439
+ function extractSearchTerms(input) {
2440
+ const noise = /\b(hey|hi|hello|um|uh|like|just|so|well|you know|i mean|basically|actually|really|very|pretty|quite|how does it feel|how do you|can you|could you|would you|do you|what do you think|tell me about)\b/gi;
2441
+ let cleaned = input.replace(noise, " ").replace(/\s+/g, " ").trim();
2442
+ if (cleaned.length < 5) cleaned = input.trim();
2443
+ return cleaned;
2444
+ }
2445
+
2446
+ // src/plugin/inject.ts
2447
+ import { existsSync as existsSync2, readdirSync as readdirSync2, readFileSync as readFileSync2, statSync } from "fs";
2448
+ import { join as join2, relative } from "path";
2449
+ function parseYamlFrontmatter2(content) {
2450
+ const match = content.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
2451
+ if (!match) return null;
2452
+ try {
2453
+ const frontmatter = parseSimpleYaml2(match[1]);
2454
+ return { frontmatter, body: match[2] };
2455
+ } catch {
2456
+ return null;
2457
+ }
2458
+ }
2459
+ function parseSimpleYaml2(yaml) {
2460
+ const result = {};
2461
+ for (const line of yaml.split("\n")) {
2462
+ if (!line.trim() || line.trim().startsWith("#")) continue;
2463
+ const colonIndex = line.indexOf(":");
2464
+ if (colonIndex === -1) continue;
2465
+ const key = line.slice(0, colonIndex).trim();
2466
+ const valueStr = line.slice(colonIndex + 1).trim();
2467
+ if (valueStr === "" || valueStr.startsWith("|") || valueStr.startsWith(">")) continue;
2468
+ result[key] = parseYamlValue2(valueStr);
2469
+ }
2470
+ return result;
2471
+ }
2472
+ function parseYamlValue2(value) {
2473
+ if (value === "" || value === "null" || value === "~") return null;
2474
+ if (value === "true") return true;
2475
+ if (value === "false") return false;
2476
+ if (/^-?\d+$/.test(value)) return parseInt(value, 10);
2477
+ if (/^-?\d+\.\d+$/.test(value)) return parseFloat(value);
2478
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
2479
+ return value.slice(1, -1);
2480
+ }
2481
+ return value;
2482
+ }
2483
+ function scanVaultFiles(vaultPath, options = {}) {
2484
+ const files = [];
2485
+ const maxAge = options.maxAge ?? 7 * 24 * 60 * 60 * 1e3;
2486
+ const limit = options.limit ?? 100;
2487
+ const now = Date.now();
2488
+ const cutoff = now - maxAge;
2489
+ const dirsToScan = findVaultDirectories(vaultPath);
2490
+ for (const dir of dirsToScan) {
2491
+ if (!existsSync2(dir)) continue;
2492
+ try {
2493
+ scanDirectory(dir, vaultPath, files, cutoff, options.primitiveTypes);
2494
+ } catch {
2495
+ }
2496
+ }
2497
+ files.sort((a, b) => b.modifiedAt.getTime() - a.modifiedAt.getTime());
2498
+ return files.slice(0, limit);
2499
+ }
2500
+ function findVaultDirectories(vaultPath) {
2501
+ const dirs = [vaultPath];
2502
+ const commonDirs = [
2503
+ "tasks",
2504
+ "projects",
2505
+ "decisions",
2506
+ "people",
2507
+ "persons",
2508
+ "notes",
2509
+ "daily",
2510
+ "journal",
2511
+ "ledger",
2512
+ "memory",
2513
+ "memories",
2514
+ "observations",
2515
+ "lessons",
2516
+ "triggers",
2517
+ "runs",
2518
+ "checkpoints",
2519
+ "handoffs",
2520
+ "workspaces",
2521
+ "parties"
2522
+ ];
2523
+ for (const subdir of commonDirs) {
2524
+ const fullPath = join2(vaultPath, subdir);
2525
+ if (existsSync2(fullPath)) dirs.push(fullPath);
2526
+ }
2527
+ try {
2528
+ const entries = readdirSync2(vaultPath, { withFileTypes: true });
2529
+ for (const entry of entries) {
2530
+ if (entry.isDirectory() && !entry.name.startsWith(".") && !entry.name.startsWith("_")) {
2531
+ const fullPath = join2(vaultPath, entry.name);
2532
+ if (!dirs.includes(fullPath)) dirs.push(fullPath);
2533
+ }
2534
+ }
2535
+ } catch {
2536
+ }
2537
+ return dirs;
2538
+ }
2539
+ function scanDirectory(dir, vaultPath, files, cutoff, primitiveTypes) {
2540
+ const entries = readdirSync2(dir, { withFileTypes: true });
2541
+ for (const entry of entries) {
2542
+ if (entry.name.startsWith(".") || entry.name.startsWith("_")) continue;
2543
+ const fullPath = join2(dir, entry.name);
2544
+ if (entry.isDirectory()) {
2545
+ const depth = fullPath.replace(vaultPath, "").split("/").length;
2546
+ if (depth <= 3) {
2547
+ scanDirectory(fullPath, vaultPath, files, cutoff, primitiveTypes);
2548
+ }
2549
+ } else if (entry.name.endsWith(".md")) {
2550
+ try {
2551
+ const stat = statSync(fullPath);
2552
+ if (stat.mtimeMs < cutoff) continue;
2553
+ const content = readFileSync2(fullPath, "utf-8");
2554
+ const parsed = parseYamlFrontmatter2(content);
2555
+ if (!parsed) continue;
2556
+ const primitiveType = detectPrimitiveType(parsed.frontmatter, fullPath);
2557
+ if (primitiveTypes && !primitiveTypes.includes(primitiveType)) continue;
2558
+ files.push({
2559
+ path: fullPath,
2560
+ relativePath: relative(vaultPath, fullPath),
2561
+ primitiveType,
2562
+ frontmatter: parsed.frontmatter,
2563
+ content: parsed.body,
2564
+ modifiedAt: stat.mtime,
2565
+ createdAt: stat.birthtime
2566
+ });
2567
+ } catch {
2568
+ }
2569
+ }
2570
+ }
2571
+ }
2572
+ function detectPrimitiveType(frontmatter, filePath) {
2573
+ if (frontmatter.primitive) return String(frontmatter.primitive);
2574
+ if (frontmatter.type) return String(frontmatter.type);
2575
+ const pathLower = filePath.toLowerCase();
2576
+ const pathMap = {
2577
+ "/tasks/": "task",
2578
+ "/projects/": "project",
2579
+ "/decisions/": "decision",
2580
+ "/people/": "person",
2581
+ "/persons/": "person",
2582
+ "/daily/": "daily-note",
2583
+ "/journal/": "daily-note",
2584
+ "/lessons/": "lesson",
2585
+ "/triggers/": "trigger",
2586
+ "/runs/": "run",
2587
+ "/checkpoints/": "checkpoint",
2588
+ "/handoffs/": "handoff",
2589
+ "/ledger/": "memory_event",
2590
+ "/memory/": "memory_event",
2591
+ "/memories/": "memory_event"
2592
+ };
2593
+ for (const [segment, type] of Object.entries(pathMap)) {
2594
+ if (pathLower.includes(segment)) return type;
2595
+ }
2596
+ return "unknown";
2597
+ }
2598
+ function buildSessionRecap(vaultPath, options = {}) {
2599
+ const maxAge = options.maxAge ?? 24 * 60 * 60 * 1e3;
2600
+ const limit = options.limit ?? 20;
2601
+ const includeContent = options.includeContent ?? false;
2602
+ const files = scanVaultFiles(vaultPath, { maxAge, limit });
2603
+ if (files.length === 0) {
2604
+ return { xml: "", fileCount: 0, primitiveGroups: {}, timeRange: null };
2605
+ }
2606
+ const groups = {};
2607
+ for (const file of files) {
2608
+ const type = file.primitiveType;
2609
+ if (!groups[type]) groups[type] = [];
2610
+ groups[type].push(file);
2611
+ }
2612
+ const lines = ["<session-recap>"];
2613
+ lines.push(`<summary>Found ${files.length} recent items across ${Object.keys(groups).length} categories</summary>`);
2614
+ for (const [primitiveType, groupFiles] of Object.entries(groups)) {
2615
+ lines.push(`<${primitiveType}-items count="${groupFiles.length}">`);
2616
+ for (const file of groupFiles.slice(0, 5)) {
2617
+ const title = file.frontmatter.title || file.frontmatter.summary || file.relativePath;
2618
+ const status = file.frontmatter.status || "";
2619
+ const modified = file.modifiedAt.toISOString().slice(0, 16).replace("T", " ");
2620
+ lines.push(` <item path="${file.relativePath}" modified="${modified}"${status ? ` status="${String(status)}"` : ""}>`);
2621
+ lines.push(` <title>${escapeXml(String(title))}</title>`);
2622
+ if (includeContent && file.content) {
2623
+ const snippet = file.content.slice(0, 200).replace(/\n/g, " ").trim();
2624
+ if (snippet) {
2625
+ lines.push(` <snippet>${escapeXml(snippet)}</snippet>`);
2626
+ }
2627
+ }
2628
+ lines.push(" </item>");
2629
+ }
2630
+ if (groupFiles.length > 5) {
2631
+ lines.push(` <more count="${groupFiles.length - 5}" />`);
2632
+ }
2633
+ lines.push(`</${primitiveType}-items>`);
2634
+ }
2635
+ lines.push("</session-recap>");
2636
+ const sortedByTime = [...files].sort((a, b) => a.modifiedAt.getTime() - b.modifiedAt.getTime());
2637
+ const timeRange = {
2638
+ oldest: sortedByTime[0].modifiedAt,
2639
+ newest: sortedByTime[sortedByTime.length - 1].modifiedAt
2640
+ };
2641
+ const primitiveGroups = {};
2642
+ for (const [type, groupFiles] of Object.entries(groups)) {
2643
+ primitiveGroups[type] = groupFiles.length;
2644
+ }
2645
+ return { xml: lines.join("\n"), fileCount: files.length, primitiveGroups, timeRange };
2646
+ }
2647
+ function buildPreferenceContext(vaultPath, options = {}) {
2648
+ const maxAge = options.maxAge ?? 30 * 24 * 60 * 60 * 1e3;
2649
+ const limit = options.limit ?? 50;
2650
+ const files = scanVaultFiles(vaultPath, { maxAge, limit: limit * 2 });
2651
+ const preferenceFiles = files.filter((file) => {
2652
+ if (file.frontmatter.type === "preference") return true;
2653
+ if (file.primitiveType === "memory_event" && file.frontmatter.type === "preference") return true;
2654
+ const content = (file.content || "").toLowerCase();
2655
+ return /\b(prefer|like|love|hate|dislike|want|need|always|never)\b/.test(content);
2656
+ }).slice(0, limit);
2657
+ if (preferenceFiles.length === 0) {
2658
+ return { xml: "", preferenceCount: 0, categories: [] };
2659
+ }
2660
+ const categories = /* @__PURE__ */ new Set();
2661
+ for (const file of preferenceFiles) {
2662
+ if (file.frontmatter.category) categories.add(String(file.frontmatter.category));
2663
+ }
2664
+ const lines = ["<user-preferences>"];
2665
+ for (const file of preferenceFiles) {
2666
+ const summary = file.frontmatter.summary || file.frontmatter.title || extractPreferenceSummary(file.content);
2667
+ if (!summary) continue;
2668
+ const category = file.frontmatter.category || "general";
2669
+ const sentiment = file.frontmatter.sentiment || inferSentiment(file.content);
2670
+ lines.push(` <preference category="${escapeXml(String(category))}" sentiment="${String(sentiment)}">`);
2671
+ lines.push(` ${escapeXml(String(summary))}`);
2672
+ lines.push(" </preference>");
2673
+ }
2674
+ lines.push("</user-preferences>");
2675
+ return {
2676
+ xml: lines.join("\n"),
2677
+ preferenceCount: preferenceFiles.length,
2678
+ categories: Array.from(categories)
2679
+ };
2680
+ }
2681
+ function extractPreferenceSummary(content) {
2682
+ if (!content) return "";
2683
+ const sentences = content.split(/[.!?\n]+/).map((s) => s.trim()).filter((s) => s.length > 10);
2684
+ for (const sentence of sentences) {
2685
+ if (/\b(prefer|like|love|hate|dislike|want|need|always|never)\b/i.test(sentence)) {
2686
+ return sentence.slice(0, 150);
2687
+ }
2688
+ }
2689
+ return sentences[0]?.slice(0, 150) || "";
2690
+ }
2691
+ function inferSentiment(content) {
2692
+ if (!content) return "neutral";
2693
+ const lower = content.toLowerCase();
2694
+ if (/\b(love|like|prefer|enjoy|want|need|always)\b/.test(lower)) return "positive";
2695
+ if (/\b(hate|dislike|don't like|never|avoid)\b/.test(lower)) return "negative";
2696
+ return "neutral";
2697
+ }
2698
+ function formatMemoriesForContext(results, collection) {
2699
+ if (results.length === 0) return "";
2700
+ const lines = results.map((r, i) => {
2701
+ const file = (r.file || "").replace(`qmd://${collection}/`, "");
2702
+ const snippet = (r.snippet || "").replace(/@@ .+? @@\s*\(.+?\)\n?/g, "").trim() || r.title || "";
2703
+ return `${i + 1}. [${file}] ${snippet}`;
2704
+ });
2705
+ return `<relevant-memories>
2706
+ These are recalled from long-term vault memory. Treat as historical context.
2707
+ ${lines.join("\n")}
2708
+ </relevant-memories>`;
2709
+ }
2710
+ function formatSearchResults(results, collection) {
2711
+ if (results.length === 0) return "No relevant memories found.";
2712
+ return results.map((r, i) => {
2713
+ const file = (r.file || "").replace(`qmd://${collection}/`, "");
2714
+ const snippet = (r.snippet || "").replace(/@@ .+? @@\s*\(.+?\)\n?/g, "").trim() || r.title || "(no content)";
2715
+ const score = ((r.score ?? 0) * 100).toFixed(0);
2716
+ return `${i + 1}. [${file}] ${snippet} (${score}%)`;
2717
+ }).join("\n");
2718
+ }
2719
+ function buildFullContext(vaultPath, options = {}) {
2720
+ const parts = [];
2721
+ if (options.includeRecap !== false) {
2722
+ const recap = buildSessionRecap(vaultPath, {
2723
+ maxAge: options.recapMaxAge ?? 24 * 60 * 60 * 1e3,
2724
+ limit: 15,
2725
+ includeContent: true
2726
+ });
2727
+ if (recap.xml) parts.push(recap.xml);
2728
+ }
2729
+ if (options.includePreferences !== false) {
2730
+ const prefs = buildPreferenceContext(vaultPath, {
2731
+ maxAge: options.preferenceMaxAge ?? 30 * 24 * 60 * 60 * 1e3,
2732
+ limit: 20
2733
+ });
2734
+ if (prefs.xml) parts.push(prefs.xml);
2735
+ }
2736
+ return parts.join("\n\n");
2737
+ }
2738
+ function escapeXml(str) {
2739
+ return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
2740
+ }
33
2741
 
34
- // src/plugin/index.ts
35
- import { execFileSync, execFile } from "child_process";
36
- import { existsSync, readFileSync, mkdirSync } from "fs";
37
- import { join, basename } from "path";
38
- import { Type } from "@sinclair/typebox";
39
- function resolveVaultPath(cfg) {
40
- if (cfg?.vaultPath) return cfg.vaultPath;
41
- if (process.env.CLAWVAULT_PATH) return process.env.CLAWVAULT_PATH;
42
- const home = process.env.HOME ?? process.env.USERPROFILE ?? ".";
43
- for (const candidate of [`${home}/clawvault`, `${home}/.clawvault`]) {
44
- if (existsSync(join(candidate, ".clawvault.json"))) return candidate;
2742
+ // src/plugin/vault.ts
2743
+ import { existsSync as existsSync3, mkdirSync, writeFileSync, readFileSync as readFileSync3, appendFileSync } from "fs";
2744
+ import { join as join3 } from "path";
2745
+ var autoEmbedFn = null;
2746
+ function setAutoEmbedFn(fn) {
2747
+ autoEmbedFn = fn;
2748
+ }
2749
+ async function autoEmbed(filePath, content) {
2750
+ if (autoEmbedFn) {
2751
+ await autoEmbedFn(filePath, content).catch(() => {
2752
+ });
45
2753
  }
46
- return `${home}/.clawvault`;
47
2754
  }
48
- function getVaultConfig(vaultPath) {
49
- const configPath = join(vaultPath, ".clawvault.json");
50
- if (!existsSync(configPath)) return null;
2755
+ function writeVaultFile(vaultPath, options) {
2756
+ const errors = [];
2757
+ const primitiveType = options.primitiveType ?? classifyText(options.content ?? options.title ?? "").primitiveType;
2758
+ const schema = getSchema(primitiveType);
2759
+ const frontmatter = generateFrontmatter(primitiveType, {
2760
+ title: options.title,
2761
+ extraFields: options.extraFields,
2762
+ source: options.source,
2763
+ sessionId: options.sessionId
2764
+ });
2765
+ const validation = validateFrontmatter(primitiveType, frontmatter);
2766
+ if (!validation.valid) errors.push(...validation.errors);
2767
+ const directory = options.directory ?? getDefaultDirectory(vaultPath, primitiveType);
2768
+ if (!existsSync3(directory)) {
2769
+ mkdirSync(directory, { recursive: true });
2770
+ }
2771
+ const filename = options.filename ?? generateFilename(primitiveType, options.title, frontmatter);
2772
+ const filePath = join3(directory, filename);
2773
+ const fileExists = existsSync3(filePath);
2774
+ if (fileExists && !options.overwrite) {
2775
+ return updateVaultFile(filePath, frontmatter, options.content, primitiveType, errors);
2776
+ }
2777
+ const fileContent = buildFileContent(frontmatter, options.content, schema);
2778
+ try {
2779
+ writeFileSync(filePath, fileContent, "utf-8");
2780
+ autoEmbed(filePath, fileContent).catch(() => {
2781
+ });
2782
+ return {
2783
+ success: errors.length === 0,
2784
+ path: filePath,
2785
+ primitiveType,
2786
+ errors,
2787
+ created: true,
2788
+ updated: false
2789
+ };
2790
+ } catch (err) {
2791
+ errors.push(`Failed to write file: ${String(err)}`);
2792
+ return {
2793
+ success: false,
2794
+ path: filePath,
2795
+ primitiveType,
2796
+ errors,
2797
+ created: false,
2798
+ updated: false
2799
+ };
2800
+ }
2801
+ }
2802
+ function updateVaultFile(filePath, newFrontmatter, newContent, primitiveType, errors) {
2803
+ try {
2804
+ const existingContent = readFileSync3(filePath, "utf-8");
2805
+ const parsed = parseExistingFile(existingContent);
2806
+ if (!parsed) {
2807
+ errors.push("Failed to parse existing file");
2808
+ return { success: false, path: filePath, primitiveType, errors, created: false, updated: false };
2809
+ }
2810
+ const mergedFrontmatter = {
2811
+ ...parsed.frontmatter,
2812
+ ...newFrontmatter,
2813
+ updated: (/* @__PURE__ */ new Date()).toISOString()
2814
+ };
2815
+ if (parsed.frontmatter.created) {
2816
+ mergedFrontmatter.created = parsed.frontmatter.created;
2817
+ }
2818
+ const content = newContent ?? parsed.body;
2819
+ const schema = getSchema(primitiveType);
2820
+ const fileContent = buildFileContent(mergedFrontmatter, content, schema);
2821
+ writeFileSync(filePath, fileContent, "utf-8");
2822
+ return {
2823
+ success: errors.length === 0,
2824
+ path: filePath,
2825
+ primitiveType,
2826
+ errors,
2827
+ created: false,
2828
+ updated: true
2829
+ };
2830
+ } catch (err) {
2831
+ errors.push(`Failed to update file: ${String(err)}`);
2832
+ return { success: false, path: filePath, primitiveType, errors, created: false, updated: false };
2833
+ }
2834
+ }
2835
+ function parseExistingFile(content) {
2836
+ const match = content.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
2837
+ if (!match) return null;
2838
+ try {
2839
+ const result = {};
2840
+ for (const line of match[1].split("\n")) {
2841
+ if (!line.trim() || line.trim().startsWith("#")) continue;
2842
+ const colonIndex = line.indexOf(":");
2843
+ if (colonIndex === -1) continue;
2844
+ const key = line.slice(0, colonIndex).trim();
2845
+ const valueStr = line.slice(colonIndex + 1).trim();
2846
+ if (valueStr === "" || valueStr.startsWith("|") || valueStr.startsWith(">")) continue;
2847
+ result[key] = parseSimpleValue(valueStr);
2848
+ }
2849
+ return { frontmatter: result, body: match[2] };
2850
+ } catch {
2851
+ return null;
2852
+ }
2853
+ }
2854
+ function parseSimpleValue(value) {
2855
+ if (value === "" || value === "null" || value === "~") return null;
2856
+ if (value === "true") return true;
2857
+ if (value === "false") return false;
2858
+ if (/^-?\d+$/.test(value)) return parseInt(value, 10);
2859
+ if (/^-?\d+\.\d+$/.test(value)) return parseFloat(value);
2860
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
2861
+ return value.slice(1, -1);
2862
+ }
2863
+ return value;
2864
+ }
2865
+ function getDefaultDirectory(vaultPath, primitiveType) {
2866
+ const directoryMap = {
2867
+ task: "tasks",
2868
+ project: "projects",
2869
+ decision: "decisions",
2870
+ person: "people",
2871
+ lesson: "lessons",
2872
+ trigger: "triggers",
2873
+ run: "runs",
2874
+ checkpoint: "checkpoints",
2875
+ handoff: "handoffs",
2876
+ "daily-note": "daily",
2877
+ daily: "daily",
2878
+ party: "parties",
2879
+ workspace: "workspaces",
2880
+ memory_event: "memory"
2881
+ };
2882
+ const subdir = directoryMap[primitiveType] ?? "notes";
2883
+ return join3(vaultPath, subdir);
2884
+ }
2885
+ function generateFilename(primitiveType, title, _frontmatter) {
2886
+ const now = /* @__PURE__ */ new Date();
2887
+ const dateStr = now.toISOString().split("T")[0];
2888
+ const timeStr = now.toISOString().slice(11, 19).replace(/:/g, "");
2889
+ if (title) {
2890
+ const slug = slugify(title);
2891
+ return `${dateStr}-${slug}.md`;
2892
+ }
2893
+ switch (primitiveType) {
2894
+ case "daily-note":
2895
+ case "daily":
2896
+ return `${dateStr}.md`;
2897
+ case "memory_event":
2898
+ return `${dateStr}-${timeStr}.md`;
2899
+ case "run":
2900
+ return `run-${dateStr}-${timeStr}.md`;
2901
+ case "checkpoint":
2902
+ return `checkpoint-${dateStr}-${timeStr}.md`;
2903
+ case "handoff":
2904
+ return `handoff-${dateStr}-${timeStr}.md`;
2905
+ default:
2906
+ return `${primitiveType}-${dateStr}-${timeStr}.md`;
2907
+ }
2908
+ }
2909
+ function slugify(text) {
2910
+ return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 50);
2911
+ }
2912
+ function buildFileContent(frontmatter, content, schema) {
2913
+ const parts = [];
2914
+ parts.push(serializeFrontmatter(frontmatter));
2915
+ parts.push("");
2916
+ const title = frontmatter.title || frontmatter.summary;
2917
+ if (title) {
2918
+ parts.push(`# ${String(title)}`);
2919
+ parts.push("");
2920
+ }
2921
+ if (content) {
2922
+ parts.push(content);
2923
+ } else if (schema?.bodyTemplate) {
2924
+ let body = schema.bodyTemplate;
2925
+ body = body.replace(/\{\{title\}\}/g, String(title || "Untitled"));
2926
+ body = body.replace(/\{\{date\}\}/g, (/* @__PURE__ */ new Date()).toISOString().split("T")[0]);
2927
+ body = body.replace(/\{\{datetime\}\}/g, (/* @__PURE__ */ new Date()).toISOString());
2928
+ body = body.replace(/\{\{links_line\}\}/g, "");
2929
+ body = body.replace(/\{\{content\}\}/g, "");
2930
+ parts.push(body.trim());
2931
+ }
2932
+ return parts.join("\n");
2933
+ }
2934
+ function writeObservation(vaultPath, observation, options = {}) {
2935
+ const extraFields = {
2936
+ type: observation.primitiveType === "memory_event" ? observation.category : observation.primitiveType,
2937
+ confidence: observation.confidence,
2938
+ tags: observation.tags,
2939
+ observed_at: observation.extractedAt.toISOString()
2940
+ };
2941
+ if (options.scope && options.scope !== "global") {
2942
+ extraFields.scope = options.scope;
2943
+ }
2944
+ return writeVaultFile(vaultPath, {
2945
+ primitiveType: observation.primitiveType,
2946
+ title: observation.text.slice(0, 80),
2947
+ content: observation.text,
2948
+ extraFields,
2949
+ source: options.source ?? "openclaw",
2950
+ sessionId: options.sessionId
2951
+ });
2952
+ }
2953
+ function appendToLedger(vaultPath, entry) {
2954
+ const dateStr = entry.timestamp.toISOString().slice(0, 10);
2955
+ const ledgerDir = join3(vaultPath, "ledger");
2956
+ if (!existsSync3(ledgerDir)) {
2957
+ mkdirSync(ledgerDir, { recursive: true });
2958
+ }
2959
+ const ledgerFile = join3(ledgerDir, `${dateStr}.md`);
2960
+ const timeStr = entry.timestamp.toISOString().slice(11, 19);
2961
+ const parts = [`[${timeStr}]`];
2962
+ if (entry.category) parts.push(`[${entry.category}]`);
2963
+ if (entry.actor) parts.push(`(${entry.actor})`);
2964
+ parts.push(entry.content);
2965
+ const line = `
2966
+ - ${parts.join(" ")}`;
2967
+ if (!existsSync3(ledgerFile)) {
2968
+ const frontmatter = serializeFrontmatter({
2969
+ type: "ledger",
2970
+ date: dateStr,
2971
+ created: entry.timestamp.toISOString()
2972
+ });
2973
+ writeFileSync(ledgerFile, `${frontmatter}
2974
+
2975
+ # Observation Ledger \u2014 ${dateStr}
2976
+ ${line}`, "utf-8");
2977
+ } else {
2978
+ appendFileSync(ledgerFile, line, "utf-8");
2979
+ }
2980
+ }
2981
+ function appendObservationToLedger(vaultPath, observation, actor) {
2982
+ appendToLedger(vaultPath, {
2983
+ timestamp: observation.extractedAt,
2984
+ category: observation.category,
2985
+ actor,
2986
+ content: observation.text,
2987
+ primitiveType: observation.primitiveType,
2988
+ tags: observation.tags
2989
+ });
2990
+ }
2991
+ function batchWriteObservations(vaultPath, observations, options = {}) {
2992
+ const results = [];
2993
+ let successful = 0;
2994
+ let failed = 0;
2995
+ const writeLedger = options.writeLedger ?? true;
2996
+ const writeFiles = options.writeFiles ?? false;
2997
+ for (const observation of observations) {
2998
+ if (writeLedger) {
2999
+ try {
3000
+ appendObservationToLedger(vaultPath, observation, options.actor);
3001
+ } catch {
3002
+ }
3003
+ }
3004
+ if (writeFiles) {
3005
+ const result = writeObservation(vaultPath, observation, {
3006
+ source: options.source,
3007
+ sessionId: options.sessionId
3008
+ });
3009
+ results.push(result);
3010
+ if (result.success) successful++;
3011
+ else failed++;
3012
+ } else {
3013
+ successful++;
3014
+ results.push({
3015
+ success: true,
3016
+ path: join3(vaultPath, "ledger", `${observation.extractedAt.toISOString().slice(0, 10)}.md`),
3017
+ primitiveType: observation.primitiveType,
3018
+ errors: [],
3019
+ created: false,
3020
+ updated: true
3021
+ });
3022
+ }
3023
+ }
3024
+ return { total: observations.length, successful, failed, results };
3025
+ }
3026
+ function ensureVaultStructure(vaultPath) {
3027
+ const directories = [
3028
+ "tasks",
3029
+ "projects",
3030
+ "decisions",
3031
+ "people",
3032
+ "lessons",
3033
+ "memory",
3034
+ "ledger",
3035
+ "daily"
3036
+ ];
3037
+ for (const dir of directories) {
3038
+ const fullPath = join3(vaultPath, dir);
3039
+ if (!existsSync3(fullPath)) {
3040
+ mkdirSync(fullPath, { recursive: true });
3041
+ }
3042
+ }
3043
+ }
3044
+
3045
+ // src/plugin/retrieval.ts
3046
+ import { execFileSync } from "child_process";
3047
+ import { existsSync as existsSync4, readFileSync as readFileSync4, readdirSync as readdirSync3, statSync as statSync2 } from "fs";
3048
+ import { join as join4, relative as relative2 } from "path";
3049
+
3050
+ // src/plugin/types.ts
3051
+ function parseScope(scope) {
3052
+ if (scope === "global") return "global";
3053
+ if (scope.startsWith("agent:") || scope.startsWith("project:") || scope.startsWith("user:")) {
3054
+ return scope;
3055
+ }
3056
+ return "global";
3057
+ }
3058
+ function matchesScope(itemScope, filterScope) {
3059
+ if (filterScope === "global") return true;
3060
+ return itemScope === filterScope;
3061
+ }
3062
+ var DEFAULT_RETRIEVAL_CONFIG = {
3063
+ bm25Weight: 0.5,
3064
+ semanticWeight: 0.5,
3065
+ rrfK: 60,
3066
+ topK: 10,
3067
+ minScore: 0.01,
3068
+ recencyHalfLifeDays: 14,
3069
+ recencyWeight: 0.1,
3070
+ decayHalfLifeDays: 60,
3071
+ lengthNormAnchor: 500,
3072
+ mmrLambda: 0.7,
3073
+ rerankWeight: 0.6
3074
+ };
3075
+
3076
+ // src/plugin/retrieval.ts
3077
+ function tokenize(text) {
3078
+ return text.toLowerCase().replace(/[^a-z0-9\s]/g, " ").split(/\s+/).filter((t) => t.length > 1);
3079
+ }
3080
+ function bm25Search(query, documents, topK) {
3081
+ const queryTerms = tokenize(query);
3082
+ if (queryTerms.length === 0 || documents.length === 0) return [];
3083
+ const k1 = 1.2;
3084
+ const b = 0.75;
3085
+ const N = documents.length;
3086
+ const docTokens = documents.map((d) => tokenize(`${d.title} ${d.content}`));
3087
+ const avgDl = docTokens.reduce((sum, t) => sum + t.length, 0) / N;
3088
+ const df = /* @__PURE__ */ new Map();
3089
+ for (const term of queryTerms) {
3090
+ let count = 0;
3091
+ for (const tokens of docTokens) {
3092
+ if (tokens.includes(term)) count++;
3093
+ }
3094
+ df.set(term, count);
3095
+ }
3096
+ const results = [];
3097
+ for (let i = 0; i < documents.length; i++) {
3098
+ const doc = documents[i];
3099
+ const tokens = docTokens[i];
3100
+ const dl = tokens.length;
3101
+ let score = 0;
3102
+ const tf = /* @__PURE__ */ new Map();
3103
+ for (const t of tokens) {
3104
+ tf.set(t, (tf.get(t) || 0) + 1);
3105
+ }
3106
+ for (const term of queryTerms) {
3107
+ const termFreq = tf.get(term) || 0;
3108
+ if (termFreq === 0) continue;
3109
+ const docFreq = df.get(term) || 0;
3110
+ const idf = Math.log((N - docFreq + 0.5) / (docFreq + 0.5) + 1);
3111
+ const tfNorm = termFreq * (k1 + 1) / (termFreq + k1 * (1 - b + b * dl / avgDl));
3112
+ score += idf * tfNorm;
3113
+ }
3114
+ if (score > 0) {
3115
+ results.push({ id: doc.id, score, doc });
3116
+ }
3117
+ }
3118
+ results.sort((a, b2) => b2.score - a.score);
3119
+ return results.slice(0, topK);
3120
+ }
3121
+ var embeddingPipeline = null;
3122
+ var pipelineLoading = null;
3123
+ async function getEmbeddingPipeline() {
3124
+ if (embeddingPipeline) return embeddingPipeline;
3125
+ if (pipelineLoading) return pipelineLoading;
3126
+ pipelineLoading = (async () => {
3127
+ const { pipeline } = await import("@huggingface/transformers");
3128
+ embeddingPipeline = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2", {
3129
+ dtype: "fp32"
3130
+ });
3131
+ return embeddingPipeline;
3132
+ })();
3133
+ return pipelineLoading;
3134
+ }
3135
+ async function embed(text) {
3136
+ const pipe = await getEmbeddingPipeline();
3137
+ const result = await pipe(
3138
+ text,
3139
+ { pooling: "mean", normalize: true }
3140
+ );
3141
+ return new Float32Array(result.data);
3142
+ }
3143
+ function cosineSimilarity(a, b) {
3144
+ let dot = 0;
3145
+ for (let i = 0; i < a.length; i++) {
3146
+ dot += a[i] * b[i];
3147
+ }
3148
+ return dot;
3149
+ }
3150
+ function loadEmbeddingCache(vaultPath) {
3151
+ const cachePath = join4(vaultPath, ".clawvault", "embeddings.bin.json");
3152
+ const cache = /* @__PURE__ */ new Map();
3153
+ if (!existsSync4(cachePath)) return cache;
3154
+ try {
3155
+ const data = JSON.parse(readFileSync4(cachePath, "utf-8"));
3156
+ for (const [key, arr] of Object.entries(data)) {
3157
+ cache.set(key, new Float32Array(arr));
3158
+ }
3159
+ } catch {
3160
+ }
3161
+ return cache;
3162
+ }
3163
+ async function semanticSearch(query, cache, topK) {
3164
+ if (cache.size === 0) return [];
3165
+ const queryEmb = await embed(query);
3166
+ const results = [];
3167
+ for (const [id, docEmb] of cache.entries()) {
3168
+ results.push({ id, score: cosineSimilarity(queryEmb, docEmb) });
3169
+ }
3170
+ results.sort((a, b) => b.score - a.score);
3171
+ return results.slice(0, topK);
3172
+ }
3173
+ function reciprocalRankFusion(list1, list2, k = 60, weight1 = 0.5, weight2 = 0.5) {
3174
+ const scores = /* @__PURE__ */ new Map();
3175
+ for (let rank = 0; rank < list1.length; rank++) {
3176
+ const { id } = list1[rank];
3177
+ scores.set(id, (scores.get(id) || 0) + weight1 / (k + rank + 1));
3178
+ }
3179
+ for (let rank = 0; rank < list2.length; rank++) {
3180
+ const { id } = list2[rank];
3181
+ scores.set(id, (scores.get(id) || 0) + weight2 / (k + rank + 1));
3182
+ }
3183
+ return Array.from(scores.entries()).map(([id, score]) => ({ id, score })).sort((a, b) => b.score - a.score);
3184
+ }
3185
+ async function crossEncoderRerank(query, documents, config) {
3186
+ if (!config.rerankProvider || !config.rerankApiKey) return null;
3187
+ if (documents.length === 0) return null;
3188
+ const endpoints = {
3189
+ jina: "https://api.jina.ai/v1/rerank",
3190
+ voyage: "https://api.voyageai.com/v1/rerank",
3191
+ siliconflow: "https://api.siliconflow.cn/v1/rerank",
3192
+ pinecone: "https://api.pinecone.io/rerank"
3193
+ };
3194
+ const models = {
3195
+ jina: "jina-reranker-v2-base-multilingual",
3196
+ voyage: "rerank-2",
3197
+ siliconflow: "BAAI/bge-reranker-v2-m3",
3198
+ pinecone: "bge-reranker-v2-m3"
3199
+ };
3200
+ const endpoint = config.rerankEndpoint || endpoints[config.rerankProvider];
3201
+ const model = config.rerankModel || models[config.rerankProvider];
3202
+ if (!endpoint) return null;
51
3203
  try {
52
- return JSON.parse(readFileSync(configPath, "utf-8"));
3204
+ const response = await fetch(endpoint, {
3205
+ method: "POST",
3206
+ headers: {
3207
+ "Content-Type": "application/json",
3208
+ Authorization: `Bearer ${config.rerankApiKey}`
3209
+ },
3210
+ body: JSON.stringify({
3211
+ model,
3212
+ query,
3213
+ documents,
3214
+ top_n: documents.length
3215
+ }),
3216
+ signal: AbortSignal.timeout(1e4)
3217
+ });
3218
+ if (!response.ok) return null;
3219
+ const data = await response.json();
3220
+ if (!data.results) return null;
3221
+ const scores = new Array(documents.length).fill(0);
3222
+ for (const result of data.results) {
3223
+ if (result.index >= 0 && result.index < documents.length) {
3224
+ scores[result.index] = result.relevance_score;
3225
+ }
3226
+ }
3227
+ return scores;
53
3228
  } catch {
54
3229
  return null;
55
3230
  }
56
3231
  }
3232
+ function computeRecencyBoost(modifiedAt, now, halfLifeDays, weight) {
3233
+ if (halfLifeDays <= 0 || weight <= 0) return 0;
3234
+ const ageDays = (now.getTime() - modifiedAt.getTime()) / (1e3 * 60 * 60 * 24);
3235
+ const decay = Math.exp(-ageDays * Math.LN2 / halfLifeDays);
3236
+ return weight * decay;
3237
+ }
3238
+ function computeTimeDecay(modifiedAt, now, halfLifeDays) {
3239
+ if (halfLifeDays <= 0) return 1;
3240
+ const ageDays = (now.getTime() - modifiedAt.getTime()) / (1e3 * 60 * 60 * 24);
3241
+ return 0.5 + 0.5 * Math.exp(-ageDays / halfLifeDays);
3242
+ }
3243
+ function computeLengthNorm(charLen, anchor) {
3244
+ if (anchor <= 0 || charLen <= 0) return 1;
3245
+ return 1 / (1 + Math.log2(Math.max(1, charLen / anchor)));
3246
+ }
3247
+ function mmrRerank(results, embeddingCache, lambda, topK) {
3248
+ if (lambda >= 1 || results.length <= 1) return results.slice(0, topK);
3249
+ const selected = [];
3250
+ const remaining = new Set(results.map((_, i) => i));
3251
+ selected.push(results[0]);
3252
+ remaining.delete(0);
3253
+ while (selected.length < topK && remaining.size > 0) {
3254
+ let bestIdx = -1;
3255
+ let bestMmrScore = -Infinity;
3256
+ for (const idx of remaining) {
3257
+ const relevance = results[idx].fusedScore;
3258
+ let maxSim = 0;
3259
+ const candidateId = results[idx].file?.replace(/^qmd:\/\/[^/]+\//, "").replace(/\.md$/, "") || "";
3260
+ const candidateEmb = embeddingCache.get(candidateId);
3261
+ if (candidateEmb) {
3262
+ for (const sel of selected) {
3263
+ const selId = sel.file?.replace(/^qmd:\/\/[^/]+\//, "").replace(/\.md$/, "") || "";
3264
+ const selEmb = embeddingCache.get(selId);
3265
+ if (selEmb) {
3266
+ maxSim = Math.max(maxSim, cosineSimilarity(candidateEmb, selEmb));
3267
+ }
3268
+ }
3269
+ }
3270
+ const mmrScore = lambda * relevance - (1 - lambda) * maxSim;
3271
+ if (mmrScore > bestMmrScore) {
3272
+ bestMmrScore = mmrScore;
3273
+ bestIdx = idx;
3274
+ }
3275
+ }
3276
+ if (bestIdx >= 0) {
3277
+ selected.push(results[bestIdx]);
3278
+ remaining.delete(bestIdx);
3279
+ } else {
3280
+ break;
3281
+ }
3282
+ }
3283
+ return selected;
3284
+ }
3285
+ function loadVaultDocuments(vaultPath) {
3286
+ const documents = [];
3287
+ const dirsToScan = [
3288
+ "tasks",
3289
+ "projects",
3290
+ "decisions",
3291
+ "people",
3292
+ "persons",
3293
+ "notes",
3294
+ "daily",
3295
+ "journal",
3296
+ "ledger",
3297
+ "memory",
3298
+ "memories",
3299
+ "observations",
3300
+ "lessons"
3301
+ ];
3302
+ const scanDir = (dir) => {
3303
+ if (!existsSync4(dir)) return;
3304
+ try {
3305
+ const entries = readdirSync3(dir, { withFileTypes: true });
3306
+ for (const entry of entries) {
3307
+ if (entry.name.startsWith(".") || entry.name.startsWith("_")) continue;
3308
+ const fullPath = join4(dir, entry.name);
3309
+ if (entry.isDirectory()) {
3310
+ const depth = fullPath.replace(vaultPath, "").split("/").length;
3311
+ if (depth <= 3) scanDir(fullPath);
3312
+ } else if (entry.name.endsWith(".md")) {
3313
+ try {
3314
+ const stat = statSync2(fullPath);
3315
+ const content = readFileSync4(fullPath, "utf-8");
3316
+ const parsed = parseYamlFrontmatter(content);
3317
+ const relPath = relative2(vaultPath, fullPath);
3318
+ const docId = relPath.replace(/\.md$/, "");
3319
+ const title = parsed?.frontmatter?.title || parsed?.frontmatter?.summary || entry.name.replace(/\.md$/, "");
3320
+ const body = parsed?.body || content;
3321
+ const scope = parseScope(String(parsed?.frontmatter?.scope || "global"));
3322
+ documents.push({
3323
+ id: docId,
3324
+ file: relPath,
3325
+ title: String(title),
3326
+ content: body.slice(0, 2e3),
3327
+ modifiedAt: stat.mtime,
3328
+ scope
3329
+ });
3330
+ } catch {
3331
+ }
3332
+ }
3333
+ }
3334
+ } catch {
3335
+ }
3336
+ };
3337
+ scanDir(vaultPath);
3338
+ for (const subdir of dirsToScan) {
3339
+ const fullPath = join4(vaultPath, subdir);
3340
+ if (existsSync4(fullPath) && !documents.some((d) => d.file.startsWith(subdir + "/"))) {
3341
+ scanDir(fullPath);
3342
+ }
3343
+ }
3344
+ return documents;
3345
+ }
3346
+ function qmdSearch(query, collection, limit) {
3347
+ const sanitized = query.replace(/['']/g, " ").replace(/[^\w\s\-.,?!]/g, " ").trim();
3348
+ if (!sanitized) return [];
3349
+ let results = [];
3350
+ for (const cmd of ["query", "search"]) {
3351
+ try {
3352
+ const result = execFileSync("qmd", [
3353
+ cmd,
3354
+ sanitized,
3355
+ "-n",
3356
+ String(Math.max(limit * 2, 20)),
3357
+ "--json",
3358
+ "-c",
3359
+ collection
3360
+ ], {
3361
+ encoding: "utf-8",
3362
+ stdio: ["ignore", "pipe", "pipe"],
3363
+ maxBuffer: 10 * 1024 * 1024,
3364
+ timeout: cmd === "query" ? 3e4 : 15e3
3365
+ });
3366
+ const parsed = JSON.parse(result);
3367
+ if (Array.isArray(parsed) && parsed.length > 0) {
3368
+ results = parsed;
3369
+ break;
3370
+ }
3371
+ } catch (err) {
3372
+ const errObj = err;
3373
+ if (errObj.stdout) {
3374
+ try {
3375
+ const parsed = JSON.parse(errObj.stdout);
3376
+ if (Array.isArray(parsed) && parsed.length > 0) {
3377
+ results = parsed;
3378
+ break;
3379
+ }
3380
+ } catch {
3381
+ }
3382
+ }
3383
+ }
3384
+ }
3385
+ return results;
3386
+ }
3387
+ async function retrieve(query, options) {
3388
+ const config = { ...DEFAULT_RETRIEVAL_CONFIG, ...options.config };
3389
+ const { vaultPath, collection = "clawvault" } = options;
3390
+ const scope = options.scope || "global";
3391
+ const now = /* @__PURE__ */ new Date();
3392
+ let fusedResults = [];
3393
+ let usedInProcess = false;
3394
+ try {
3395
+ const documents = loadVaultDocuments(vaultPath);
3396
+ const embeddingCache = loadEmbeddingCache(vaultPath);
3397
+ const scopedDocs = scope === "global" ? documents : documents.filter((d) => matchesScope(d.scope, scope));
3398
+ if (scopedDocs.length > 0) {
3399
+ const bm25Results = bm25Search(query, scopedDocs, config.topK * 3);
3400
+ const bm25Ranked = bm25Results.map((r) => ({ id: r.id, score: r.score }));
3401
+ const semanticRanked = await semanticSearch(query, embeddingCache, config.topK * 3);
3402
+ fusedResults = reciprocalRankFusion(
3403
+ bm25Ranked,
3404
+ semanticRanked,
3405
+ config.rrfK,
3406
+ config.bm25Weight,
3407
+ config.semanticWeight
3408
+ ).map((r) => {
3409
+ const doc = scopedDocs.find((d) => d.id === r.id);
3410
+ return { ...r, doc };
3411
+ });
3412
+ usedInProcess = true;
3413
+ }
3414
+ } catch {
3415
+ }
3416
+ if (!usedInProcess || fusedResults.length === 0) {
3417
+ const qmdResults = qmdSearch(query, collection, config.topK * 2);
3418
+ fusedResults = qmdResults.map((r, i) => ({
3419
+ id: (r.file || "").replace(`qmd://${collection}/`, "").replace(/\.md$/, ""),
3420
+ score: r.score ?? 1 / (i + 1),
3421
+ qmd: r
3422
+ }));
3423
+ }
3424
+ if (fusedResults.length === 0) return [];
3425
+ let scored = fusedResults.map((r) => {
3426
+ const doc = r.doc;
3427
+ const qmd = r.qmd;
3428
+ return {
3429
+ file: doc?.file || qmd?.file || r.id + ".md",
3430
+ title: doc?.title || qmd?.title || r.id,
3431
+ snippet: qmd?.snippet || doc?.content?.slice(0, 300) || "",
3432
+ score: r.score,
3433
+ fusedScore: r.score,
3434
+ scope: doc?.scope || "global"
3435
+ };
3436
+ });
3437
+ if (config.rerankProvider && config.rerankApiKey) {
3438
+ const texts = scored.map((r) => `${r.title || ""} ${r.snippet || ""}`.trim());
3439
+ const rerankScores = await crossEncoderRerank(query, texts, config);
3440
+ if (rerankScores) {
3441
+ scored = scored.map((r, i) => ({
3442
+ ...r,
3443
+ rerankScore: rerankScores[i],
3444
+ fusedScore: config.rerankWeight * rerankScores[i] + (1 - config.rerankWeight) * r.fusedScore
3445
+ }));
3446
+ scored.sort((a, b) => b.fusedScore - a.fusedScore);
3447
+ }
3448
+ }
3449
+ scored = scored.map((r) => {
3450
+ const doc = fusedResults.find((f) => f.id === r.file?.replace(/\.md$/, ""));
3451
+ const modifiedAt = doc?.doc?.modifiedAt;
3452
+ let recencyBoost = 0;
3453
+ let timeDecay = 1;
3454
+ if (modifiedAt) {
3455
+ recencyBoost = computeRecencyBoost(
3456
+ modifiedAt,
3457
+ now,
3458
+ config.recencyHalfLifeDays,
3459
+ config.recencyWeight
3460
+ );
3461
+ timeDecay = computeTimeDecay(modifiedAt, now, config.decayHalfLifeDays);
3462
+ }
3463
+ return {
3464
+ ...r,
3465
+ recencyBoost,
3466
+ timeDecay,
3467
+ fusedScore: (r.fusedScore + recencyBoost) * timeDecay
3468
+ };
3469
+ });
3470
+ if (config.lengthNormAnchor > 0) {
3471
+ scored = scored.map((r) => {
3472
+ const charLen = (r.snippet?.length || 0) + (r.title?.length || 0);
3473
+ const lengthNorm = computeLengthNorm(charLen, config.lengthNormAnchor);
3474
+ return {
3475
+ ...r,
3476
+ lengthNorm,
3477
+ fusedScore: r.fusedScore * lengthNorm
3478
+ };
3479
+ });
3480
+ }
3481
+ scored.sort((a, b) => b.fusedScore - a.fusedScore);
3482
+ if (config.mmrLambda < 1) {
3483
+ const embeddingCache = loadEmbeddingCache(vaultPath);
3484
+ scored = mmrRerank(scored, embeddingCache, config.mmrLambda, config.topK);
3485
+ }
3486
+ return scored.filter((r) => r.fusedScore >= config.minScore).slice(0, config.topK);
3487
+ }
57
3488
  function qmdHybridSearch(query, collection, limit = 10) {
58
3489
  const sanitized = query.replace(/['']/g, " ").replace(/[^\w\s\-.,?!]/g, " ").trim();
59
3490
  if (!sanitized) return [];
3491
+ const bm25Results = qmdSearch(sanitized, collection, limit);
60
3492
  try {
61
- const result = execFileSync("qmd", [
62
- "query",
63
- sanitized,
64
- "-n",
65
- String(limit),
66
- "--json",
67
- "-c",
68
- collection
69
- ], { encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 10 * 1024 * 1024, timeout: 3e4 });
70
- const parsed = JSON.parse(result);
71
- if (Array.isArray(parsed) && parsed.length > 0) return parsed;
72
- } catch (err) {
73
- if (err?.stdout) {
74
- try {
75
- const parsed = JSON.parse(err.stdout);
76
- if (Array.isArray(parsed) && parsed.length > 0) return parsed;
77
- } catch {
3493
+ const vaultPath = process.env.CLAWVAULT_PATH || join4(
3494
+ process.env.HOME || ".",
3495
+ "clawvault"
3496
+ );
3497
+ const cachePath = join4(vaultPath, ".clawvault", "embeddings.bin.json");
3498
+ if (bm25Results.length > 0 && existsSync4(cachePath)) {
3499
+ const rerankerPath = join4(__dirname, "semantic-rerank.mjs");
3500
+ if (existsSync4(rerankerPath)) {
3501
+ const reranked = execFileSync("node", [
3502
+ rerankerPath,
3503
+ sanitized,
3504
+ cachePath,
3505
+ JSON.stringify(bm25Results)
3506
+ ], { encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"], timeout: 1e4 });
3507
+ const parsed = JSON.parse(reranked);
3508
+ if (Array.isArray(parsed) && parsed.length > 0) {
3509
+ return parsed.slice(0, limit);
3510
+ }
78
3511
  }
79
3512
  }
3513
+ } catch {
80
3514
  }
81
- try {
82
- const result = execFileSync("qmd", [
83
- "search",
84
- sanitized,
85
- "-n",
86
- String(limit),
87
- "--json",
88
- "-c",
89
- collection
90
- ], { encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 10 * 1024 * 1024, timeout: 15e3 });
91
- return JSON.parse(result);
92
- } catch (err) {
93
- if (err?.stdout) {
94
- try {
95
- return JSON.parse(err.stdout);
96
- } catch {
3515
+ return bm25Results.slice(0, limit);
3516
+ }
3517
+
3518
+ // src/plugin/adaptive-retrieval.ts
3519
+ var DEFAULT_ADAPTIVE_CONFIG = {
3520
+ enabled: true,
3521
+ skipPatterns: []
3522
+ };
3523
+ var GREETING_RE = /^(?:hi|hello|hey|howdy|greetings|good (?:morning|afternoon|evening)|what'?s up|sup|yo)\b/i;
3524
+ var FAREWELL_RE = /^(?:bye|goodbye|see you|later|gn|good night|cya|take care)\s*[!.?]*$/i;
3525
+ var SLASH_COMMAND_RE = /^\/\w+/;
3526
+ var CONFIRMATION_RE = /^(?:ok(?:ay)?|sure|yes|no|yep|nope|y|n|got it|understood|perfect|great|cool|nice|awesome|k|kk|ack|confirmed|roger)\s*[!.?]*$/i;
3527
+ var EMOJI_ONLY_RE = /^[\p{Emoji}\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Emoji_Component}\s]+$/u;
3528
+ var SYSTEM_RE = /^(?:\[System|\[HEARTBEAT|NO_REPLY|<relevant-memories|<session-recap)/;
3529
+ var MIN_MEANINGFUL_LENGTH = 8;
3530
+ function shouldRetrieve(input, config = DEFAULT_ADAPTIVE_CONFIG) {
3531
+ if (!config.enabled) return { shouldRetrieve: true };
3532
+ if (!input) return { shouldRetrieve: false, skipReason: "too_short" };
3533
+ const trimmed = input.trim();
3534
+ if (SYSTEM_RE.test(trimmed)) {
3535
+ return { shouldRetrieve: false, skipReason: "system_message" };
3536
+ }
3537
+ if (SLASH_COMMAND_RE.test(trimmed)) {
3538
+ return { shouldRetrieve: false, skipReason: "slash_command" };
3539
+ }
3540
+ if (EMOJI_ONLY_RE.test(trimmed)) {
3541
+ return { shouldRetrieve: false, skipReason: "emoji_only" };
3542
+ }
3543
+ if (trimmed.length < MIN_MEANINGFUL_LENGTH) {
3544
+ return { shouldRetrieve: false, skipReason: "too_short" };
3545
+ }
3546
+ if (GREETING_RE.test(trimmed) && trimmed.length < 30) {
3547
+ return { shouldRetrieve: false, skipReason: "greeting" };
3548
+ }
3549
+ if (FAREWELL_RE.test(trimmed)) {
3550
+ return { shouldRetrieve: false, skipReason: "farewell" };
3551
+ }
3552
+ if (CONFIRMATION_RE.test(trimmed)) {
3553
+ return { shouldRetrieve: false, skipReason: "confirmation" };
3554
+ }
3555
+ for (const pattern of config.skipPatterns) {
3556
+ try {
3557
+ const re = new RegExp(pattern, "i");
3558
+ if (re.test(trimmed)) {
3559
+ return { shouldRetrieve: false, skipReason: "user_pattern" };
97
3560
  }
3561
+ } catch {
98
3562
  }
99
- return [];
100
3563
  }
3564
+ return { shouldRetrieve: true };
3565
+ }
3566
+
3567
+ // src/plugin/index.ts
3568
+ var PLUGIN_VERSION = "4.0.0";
3569
+ function resolveVaultPath(cfg) {
3570
+ if (cfg?.vaultPath) return cfg.vaultPath;
3571
+ if (process.env.CLAWVAULT_PATH) return process.env.CLAWVAULT_PATH;
3572
+ const home = process.env.HOME ?? process.env.USERPROFILE ?? ".";
3573
+ for (const candidate of [`${home}/clawvault`, `${home}/.clawvault`]) {
3574
+ if (existsSync5(join5(candidate, ".clawvault.json"))) return candidate;
3575
+ }
3576
+ return `${home}/.clawvault`;
101
3577
  }
102
- function observe(vaultPath, content, meta = {}) {
3578
+ async function autoEmbedViaOllama(filePath, content) {
103
3579
  try {
104
- const args = ["observe", "--content", content];
105
- if (meta.tags?.length) args.push("--tags", meta.tags.join(","));
106
- execFile("clawvault", args, { cwd: vaultPath, timeout: 15e3 }, () => {
3580
+ const { dirname, join: pathJoin, relative: pathRelative } = await import("path");
3581
+ const { existsSync: fsExists, readFileSync: fsRead, writeFileSync: fsWrite, mkdirSync: fsMkdir } = await import("fs");
3582
+ let vaultPath = dirname(filePath);
3583
+ for (let i = 0; i < 10; i++) {
3584
+ if (fsExists(pathJoin(vaultPath, ".clawvault.json"))) break;
3585
+ const parent = dirname(vaultPath);
3586
+ if (parent === vaultPath) return;
3587
+ vaultPath = parent;
3588
+ }
3589
+ const cachePath = pathJoin(vaultPath, ".clawvault", "embeddings.bin.json");
3590
+ const docId = pathRelative(vaultPath, filePath).replace(/\.md$/, "");
3591
+ const resp = await fetch("http://localhost:11434/api/embeddings", {
3592
+ method: "POST",
3593
+ headers: { "Content-Type": "application/json" },
3594
+ body: JSON.stringify({ model: "nomic-embed-text", prompt: content.slice(0, 2e3) }),
3595
+ signal: AbortSignal.timeout(5e3)
107
3596
  });
3597
+ if (!resp.ok) return;
3598
+ const data = await resp.json();
3599
+ let cache = {};
3600
+ try {
3601
+ cache = JSON.parse(fsRead(cachePath, "utf-8"));
3602
+ } catch {
3603
+ }
3604
+ cache[docId] = data.embedding;
3605
+ const dir = dirname(cachePath);
3606
+ if (!fsExists(dir)) fsMkdir(dir, { recursive: true });
3607
+ fsWrite(cachePath, JSON.stringify(cache));
108
3608
  } catch {
109
3609
  }
110
3610
  }
@@ -117,44 +3617,151 @@ function qmdUpdateAsync(collection) {
117
3617
  } catch {
118
3618
  }
119
3619
  }
120
- var templateRegistry = null;
121
- function getTemplateRegistry() {
122
- return templateRegistry;
3620
+ function observeViaCli(vaultPath, content, meta = {}) {
3621
+ try {
3622
+ const args = ["observe", "--content", content];
3623
+ if (meta.tags?.length) args.push("--tags", meta.tags.join(","));
3624
+ execFile("clawvault", args, { cwd: vaultPath, timeout: 15e3 }, () => {
3625
+ });
3626
+ } catch {
3627
+ }
3628
+ }
3629
+ function computeMemoryStats(vaultPath, collection) {
3630
+ const stats = {
3631
+ vault: vaultPath,
3632
+ version: PLUGIN_VERSION,
3633
+ documents: 0,
3634
+ vectors: 0,
3635
+ categories: {},
3636
+ oldestDoc: null,
3637
+ newestDoc: null,
3638
+ totalSizeKb: 0
3639
+ };
3640
+ const files = scanVaultFiles(vaultPath, { maxAge: Infinity, limit: 1e4 });
3641
+ stats.documents = files.length;
3642
+ for (const file of files) {
3643
+ const cat = file.primitiveType;
3644
+ stats.categories[cat] = (stats.categories[cat] || 0) + 1;
3645
+ }
3646
+ if (files.length > 0) {
3647
+ const sorted = [...files].sort((a, b) => a.modifiedAt.getTime() - b.modifiedAt.getTime());
3648
+ stats.oldestDoc = sorted[0].modifiedAt.toISOString();
3649
+ stats.newestDoc = sorted[sorted.length - 1].modifiedAt.toISOString();
3650
+ }
3651
+ const cachePath = join5(vaultPath, ".clawvault", "embeddings.bin.json");
3652
+ if (existsSync5(cachePath)) {
3653
+ try {
3654
+ const cacheData = JSON.parse(readFileSync5(cachePath, "utf-8"));
3655
+ stats.vectors = Object.keys(cacheData).length;
3656
+ const cacheStats = statSync3(cachePath);
3657
+ stats.totalSizeKb = Math.round(cacheStats.size / 1024);
3658
+ } catch {
3659
+ }
3660
+ }
3661
+ try {
3662
+ const qmdStats = execFileSync2("qmd", ["status", "--json", "-c", collection], {
3663
+ encoding: "utf-8",
3664
+ timeout: 5e3,
3665
+ stdio: ["ignore", "pipe", "pipe"]
3666
+ });
3667
+ const parsed = JSON.parse(qmdStats);
3668
+ if (typeof parsed.documents === "number") stats.documents = Math.max(stats.documents, parsed.documents);
3669
+ if (typeof parsed.vectors === "number") stats.vectors = Math.max(stats.vectors, parsed.vectors);
3670
+ } catch {
3671
+ }
3672
+ return stats;
3673
+ }
3674
+ function exportMemories(vaultPath, outputPath) {
3675
+ const files = scanVaultFiles(vaultPath, { maxAge: Infinity, limit: 1e5 });
3676
+ const memories = files.map((f) => ({
3677
+ path: f.relativePath,
3678
+ primitiveType: f.primitiveType,
3679
+ frontmatter: f.frontmatter,
3680
+ content: f.content,
3681
+ modifiedAt: f.modifiedAt.toISOString(),
3682
+ createdAt: f.createdAt.toISOString()
3683
+ }));
3684
+ writeFileSync2(outputPath, JSON.stringify(memories, null, 2), "utf-8");
3685
+ return { count: memories.length, path: outputPath };
3686
+ }
3687
+ function importMemories(vaultPath, inputPath) {
3688
+ const errors = [];
3689
+ let imported = 0;
3690
+ let skipped = 0;
3691
+ try {
3692
+ const data = JSON.parse(readFileSync5(inputPath, "utf-8"));
3693
+ for (const entry of data) {
3694
+ const title = String(entry.frontmatter?.title || entry.frontmatter?.summary || "");
3695
+ const result = writeVaultFile(vaultPath, {
3696
+ primitiveType: entry.primitiveType || "memory_event",
3697
+ title: title.slice(0, 80) || void 0,
3698
+ content: entry.content,
3699
+ extraFields: entry.frontmatter
3700
+ });
3701
+ if (result.success) imported++;
3702
+ else {
3703
+ skipped++;
3704
+ if (result.errors.length > 0) errors.push(...result.errors);
3705
+ }
3706
+ }
3707
+ } catch (err) {
3708
+ errors.push(`Import failed: ${String(err)}`);
3709
+ }
3710
+ return { imported, skipped, errors };
123
3711
  }
3712
+ var templateRegistry = null;
124
3713
  var clawvaultPlugin = {
125
3714
  id: "clawvault",
126
3715
  name: "ClawVault Memory",
127
- description: "Template-driven observational memory with hybrid search. Memories are captured automatically from conversations and classified against template schemas.",
128
- version: "3.1.0",
3716
+ description: "Template-driven observational memory with hybrid search, cross-encoder reranking, and adaptive retrieval. Memories are captured automatically from conversations and classified against template schemas.",
3717
+ version: PLUGIN_VERSION,
129
3718
  kind: "memory",
130
3719
  register(api) {
131
- const vaultPath = resolveVaultPath(api.pluginConfig);
132
- const collection = api.pluginConfig?.collection || "clawvault";
133
- const autoRecall = api.pluginConfig?.autoRecall !== false;
134
- const autoCapture = api.pluginConfig?.autoCapture !== false;
135
- const recallLimit = api.pluginConfig?.recallLimit || 5;
136
- const templatesDir = api.pluginConfig?.templatesDir ?? join(vaultPath, "..", "..", "templates");
3720
+ const cfg = api.pluginConfig || {};
3721
+ const vaultPath = resolveVaultPath(cfg);
3722
+ const collection = cfg.collection || "clawvault";
3723
+ const autoRecall = cfg.autoRecall !== false;
3724
+ const autoCapture = cfg.autoCapture !== false;
3725
+ const recallLimit = cfg.recallLimit || 5;
3726
+ const templatesDir = cfg.templatesDir ?? join5(vaultPath, "..", "..", "templates");
3727
+ const defaultScope = parseScope(cfg.defaultScope || "global");
3728
+ const retrievalConfig = {
3729
+ ...DEFAULT_RETRIEVAL_CONFIG,
3730
+ ...cfg.retrieval
3731
+ };
3732
+ const noiseConfig = {
3733
+ ...DEFAULT_NOISE_CONFIG,
3734
+ ...cfg.noise
3735
+ };
3736
+ const adaptiveConfig = {
3737
+ ...DEFAULT_ADAPTIVE_CONFIG,
3738
+ ...cfg.adaptive
3739
+ };
137
3740
  templateRegistry = initializeTemplateRegistry(templatesDir);
138
3741
  api.logger.info(`[clawvault] Template registry initialized with ${templateRegistry.schemas.size} schemas`);
139
- if (!existsSync(join(vaultPath, ".clawvault.json"))) {
3742
+ setAutoEmbedFn(autoEmbedViaOllama);
3743
+ if (!existsSync5(join5(vaultPath, ".clawvault.json"))) {
140
3744
  api.logger.warn(`[clawvault] Vault not found at ${vaultPath}`);
141
3745
  return;
142
3746
  }
143
3747
  ensureVaultStructure(vaultPath);
144
- api.logger.info(`[clawvault] v2.2.0 vault=${vaultPath} collection=${collection} recall=${autoRecall} capture=${autoCapture}`);
3748
+ api.logger.info(
3749
+ `[clawvault] v${PLUGIN_VERSION} vault=${vaultPath} collection=${collection} recall=${autoRecall} capture=${autoCapture} scope=${defaultScope}`
3750
+ );
145
3751
  api.registerTool({
146
3752
  name: "memory_search",
147
3753
  label: "Memory Search",
148
- description: "Search through long-term memories using ClawVault. Supports preferences, temporal queries, and multi-session knowledge retrieval.",
149
- parameters: Type.Object({
150
- query: Type.String({ description: "Search query \u2014 natural language question or keyword search" }),
151
- limit: Type.Optional(Type.Number({ description: "Max results (default: 10)" })),
152
- queryType: Type.Optional(Type.Union([
153
- Type.Literal("preference"),
154
- Type.Literal("temporal"),
155
- Type.Literal("knowledge"),
156
- Type.Literal("general")
157
- ], { description: "Force query type (auto-detected if omitted)" }))
3754
+ description: "Search through long-term memories using ClawVault. Uses in-process hybrid retrieval with BM25 + semantic search, RRF fusion, optional reranking, and MMR diversity.",
3755
+ parameters: import_typebox.Type.Object({
3756
+ query: import_typebox.Type.String({ description: "Search query \u2014 natural language question or keyword search" }),
3757
+ limit: import_typebox.Type.Optional(import_typebox.Type.Number({ description: "Max results (default: 10)" })),
3758
+ queryType: import_typebox.Type.Optional(import_typebox.Type.Union([
3759
+ import_typebox.Type.Literal("preference"),
3760
+ import_typebox.Type.Literal("temporal"),
3761
+ import_typebox.Type.Literal("knowledge"),
3762
+ import_typebox.Type.Literal("general")
3763
+ ], { description: "Force query type (auto-detected if omitted)" })),
3764
+ scope: import_typebox.Type.Optional(import_typebox.Type.String({ description: "Memory scope filter (global, agent:<id>, project:<name>, user:<id>)" }))
158
3765
  }),
159
3766
  async execute(_id, params) {
160
3767
  try {
@@ -162,63 +3769,73 @@ var clawvaultPlugin = {
162
3769
  if (params.queryType === "preference") searchQuery = `preference: ${searchQuery}`;
163
3770
  else if (params.queryType === "temporal") searchQuery = `when: ${searchQuery}`;
164
3771
  const limit = params.limit || 10;
165
- const results = qmdHybridSearch(searchQuery, collection, limit);
3772
+ const scope = parseScope(params.scope || defaultScope);
3773
+ const results = await retrieve(searchQuery, {
3774
+ vaultPath,
3775
+ collection,
3776
+ config: { ...retrievalConfig, topK: limit },
3777
+ scope
3778
+ });
166
3779
  if (results.length === 0) {
167
3780
  return {
168
3781
  content: [{ type: "text", text: "No relevant memories found." }],
169
3782
  details: { count: 0, provider: "clawvault" }
170
3783
  };
171
3784
  }
3785
+ const formatted = results.map((r, i) => {
3786
+ const file = (r.file || "").replace(`qmd://${collection}/`, "");
3787
+ const snippet = (r.snippet || "").replace(/@@ .+? @@\s*\(.+?\)\n?/g, "").trim() || r.title || "(no content)";
3788
+ const score = (r.fusedScore * 100).toFixed(0);
3789
+ return `${i + 1}. [${file}] ${snippet} (${score}%)`;
3790
+ }).join("\n");
172
3791
  return {
173
- content: [{ type: "text", text: formatSearchResults(results, collection) }],
174
- details: { count: results.length, provider: "clawvault" }
3792
+ content: [{ type: "text", text: formatted }],
3793
+ details: { count: results.length, provider: "clawvault", pipeline: "hybrid-v2" }
175
3794
  };
176
3795
  } catch (err) {
177
- return {
178
- content: [{ type: "text", text: `Memory search error: ${String(err)}` }],
179
- isError: true
180
- };
3796
+ try {
3797
+ const results = qmdHybridSearch(params.query, collection, params.limit || 10);
3798
+ return {
3799
+ content: [{ type: "text", text: formatSearchResults(results, collection) }],
3800
+ details: { count: results.length, provider: "clawvault", pipeline: "qmd-fallback" }
3801
+ };
3802
+ } catch {
3803
+ return {
3804
+ content: [{ type: "text", text: `Memory search error: ${String(err)}` }],
3805
+ isError: true
3806
+ };
3807
+ }
181
3808
  }
182
3809
  }
183
3810
  });
184
3811
  api.registerTool({
185
3812
  name: "memory_get",
186
3813
  label: "Memory Get",
187
- description: "Get vault status or stored preferences.",
188
- parameters: Type.Object({
189
- action: Type.Union([
190
- Type.Literal("status"),
191
- Type.Literal("preferences")
3814
+ description: "Get vault status, stored preferences, or memory stats.",
3815
+ parameters: import_typebox.Type.Object({
3816
+ action: import_typebox.Type.Union([
3817
+ import_typebox.Type.Literal("status"),
3818
+ import_typebox.Type.Literal("preferences"),
3819
+ import_typebox.Type.Literal("stats")
192
3820
  ], { description: "What to retrieve" })
193
3821
  }),
194
3822
  async execute(_id, params) {
195
3823
  try {
196
- if (params.action === "status") {
197
- const config = getVaultConfig(vaultPath);
198
- let docCount = 0;
199
- let vectorCount = 0;
200
- try {
201
- const stats = execFileSync("qmd", ["status", "--json", "-c", collection], {
202
- encoding: "utf-8",
203
- timeout: 5e3,
204
- stdio: ["ignore", "pipe", "pipe"]
205
- });
206
- const parsed = JSON.parse(stats);
207
- docCount = parsed.documents ?? parsed.doc_count ?? 0;
208
- vectorCount = parsed.vectors ?? parsed.vector_count ?? 0;
209
- } catch {
210
- }
3824
+ if (params.action === "status" || params.action === "stats") {
3825
+ const stats = computeMemoryStats(vaultPath, collection);
211
3826
  return {
212
3827
  content: [{ type: "text", text: JSON.stringify({
213
- vault: vaultPath,
214
- name: config?.name || "clawvault",
215
- collection,
216
- documents: docCount,
217
- vectors: vectorCount,
3828
+ ...stats,
218
3829
  autoRecall,
219
3830
  autoCapture,
220
- version: "3.1.0",
221
- templateSchemas: templateRegistry?.schemas.size ?? 0
3831
+ templateSchemas: templateRegistry?.schemas.size ?? 0,
3832
+ scope: defaultScope,
3833
+ retrieval: {
3834
+ rerankProvider: retrievalConfig.rerankProvider || "none",
3835
+ mmrLambda: retrievalConfig.mmrLambda,
3836
+ recencyHalfLife: retrievalConfig.recencyHalfLifeDays,
3837
+ decayHalfLife: retrievalConfig.decayHalfLifeDays
3838
+ }
222
3839
  }, null, 2) }]
223
3840
  };
224
3841
  }
@@ -251,49 +3868,73 @@ var clawvaultPlugin = {
251
3868
  name: "memory_store",
252
3869
  label: "Memory Store",
253
3870
  description: "Save important information in long-term memory. Use for preferences, facts, decisions, or anything worth remembering.",
254
- parameters: Type.Object({
255
- text: Type.String({ description: "Information to remember" }),
256
- category: Type.Optional(Type.Union([
257
- Type.Literal("preference"),
258
- Type.Literal("fact"),
259
- Type.Literal("decision"),
260
- Type.Literal("entity"),
261
- Type.Literal("event"),
262
- Type.Literal("other")
3871
+ parameters: import_typebox.Type.Object({
3872
+ text: import_typebox.Type.String({ description: "Information to remember" }),
3873
+ category: import_typebox.Type.Optional(import_typebox.Type.Union([
3874
+ import_typebox.Type.Literal("preference"),
3875
+ import_typebox.Type.Literal("fact"),
3876
+ import_typebox.Type.Literal("decision"),
3877
+ import_typebox.Type.Literal("entity"),
3878
+ import_typebox.Type.Literal("event"),
3879
+ import_typebox.Type.Literal("other")
263
3880
  ], { description: "Memory category (auto-detected if omitted)" })),
264
- tags: Type.Optional(Type.Array(Type.String(), { description: "Tags for organization" }))
3881
+ tags: import_typebox.Type.Optional(import_typebox.Type.Array(import_typebox.Type.String(), { description: "Tags for organization" })),
3882
+ scope: import_typebox.Type.Optional(import_typebox.Type.String({ description: "Memory scope (global, agent:<id>, project:<name>, user:<id>)" }))
265
3883
  }),
266
3884
  async execute(_id, params) {
267
3885
  try {
268
- const classification = classifyText(params.text);
269
- const category = params.category || detectCategory(params.text);
3886
+ const text = params.text;
3887
+ if (noiseConfig.enabled) {
3888
+ const check = isNoise(text, noiseConfig);
3889
+ if (check.isNoise) {
3890
+ return {
3891
+ content: [{ type: "text", text: `Skipped: content filtered (${check.reason})` }],
3892
+ details: { action: "filtered", reason: check.reason }
3893
+ };
3894
+ }
3895
+ }
3896
+ const classification = classifyText(text);
3897
+ const category = params.category || detectCategory(text);
270
3898
  const tags = params.tags || [category, ...classification.matchedKeywords.slice(0, 3)];
3899
+ const scope = parseScope(params.scope || defaultScope);
3900
+ const CATEGORY_TO_PRIMITIVE = {
3901
+ preference: "memory_event",
3902
+ fact: "memory_event",
3903
+ decision: "decision",
3904
+ entity: "person",
3905
+ event: "memory_event",
3906
+ other: "memory_event"
3907
+ };
3908
+ const effectivePrimitive = params.category ? CATEGORY_TO_PRIMITIVE[params.category] ?? "memory_event" : classification.primitiveType;
3909
+ const extraFields = {
3910
+ type: category,
3911
+ confidence: classification.confidence,
3912
+ tags
3913
+ };
3914
+ if (scope !== "global") extraFields.scope = scope;
271
3915
  const result = writeVaultFile(vaultPath, {
272
- primitiveType: classification.primitiveType,
273
- title: params.text.slice(0, 80),
274
- content: params.text,
275
- extraFields: {
276
- type: category,
277
- confidence: classification.confidence,
278
- tags
279
- },
3916
+ primitiveType: effectivePrimitive,
3917
+ title: text.slice(0, 80),
3918
+ content: text,
3919
+ extraFields,
280
3920
  source: "openclaw"
281
3921
  });
282
3922
  appendToLedger(vaultPath, {
283
3923
  timestamp: /* @__PURE__ */ new Date(),
284
3924
  category,
285
- content: params.text,
3925
+ content: text,
286
3926
  primitiveType: classification.primitiveType,
287
3927
  tags
288
3928
  });
289
3929
  qmdUpdateAsync(collection);
290
3930
  return {
291
- content: [{ type: "text", text: `Stored: "${params.text.slice(0, 100)}${params.text.length > 100 ? "..." : ""}" [${classification.primitiveType}/${category}]` }],
3931
+ content: [{ type: "text", text: `Stored: "${text.slice(0, 100)}${text.length > 100 ? "..." : ""}" [${classification.primitiveType}/${category}]${scope !== "global" ? ` scope=${scope}` : ""}` }],
292
3932
  details: {
293
3933
  action: result.created ? "created" : "updated",
294
3934
  category,
295
3935
  primitiveType: classification.primitiveType,
296
- path: result.path
3936
+ path: result.path,
3937
+ scope
297
3938
  }
298
3939
  };
299
3940
  } catch (err) {
@@ -308,9 +3949,9 @@ var clawvaultPlugin = {
308
3949
  name: "memory_forget",
309
3950
  label: "Memory Forget",
310
3951
  description: "Delete specific memories from the vault.",
311
- parameters: Type.Object({
312
- query: Type.String({ description: "Search query to find the memory to delete" }),
313
- confirm: Type.Optional(Type.Boolean({ description: "Set true to confirm deletion of first match" }))
3952
+ parameters: import_typebox.Type.Object({
3953
+ query: import_typebox.Type.String({ description: "Search query to find the memory to delete" }),
3954
+ confirm: import_typebox.Type.Optional(import_typebox.Type.Boolean({ description: "Set true to confirm deletion of first match" }))
314
3955
  }),
315
3956
  async execute(_id, params) {
316
3957
  try {
@@ -337,12 +3978,11 @@ Call again with confirm=true to delete the top match.` }],
337
3978
  }
338
3979
  const target = results[0];
339
3980
  const file = (target.file || "").replace(`qmd://${collection}/`, "");
340
- const fullPath = join(vaultPath, file);
341
- if (existsSync(fullPath)) {
342
- const trashDir = join(vaultPath, ".trash");
343
- if (!existsSync(trashDir)) mkdirSync(trashDir, { recursive: true });
344
- const trashPath = join(trashDir, `${Date.now()}-${basename(file)}`);
345
- const { renameSync } = __require("fs");
3981
+ const fullPath = join5(vaultPath, file);
3982
+ if (existsSync5(fullPath)) {
3983
+ const trashDir = join5(vaultPath, ".trash");
3984
+ if (!existsSync5(trashDir)) mkdirSync2(trashDir, { recursive: true });
3985
+ const trashPath = join5(trashDir, `${Date.now()}-${basename(file)}`);
346
3986
  renameSync(fullPath, trashPath);
347
3987
  qmdUpdateAsync(collection);
348
3988
  return {
@@ -364,32 +4004,61 @@ Call again with confirm=true to delete the top match.` }],
364
4004
  });
365
4005
  if (autoRecall) {
366
4006
  api.on("before_agent_start", async (event) => {
367
- if (!event.prompt || event.prompt.length < 10) return;
368
- if (event.prompt.includes("HEARTBEAT") || event.prompt.startsWith("[System")) return;
4007
+ const prompt = event.prompt;
4008
+ if (!prompt || prompt.length < 10) return;
4009
+ if (prompt.includes("HEARTBEAT") || prompt.startsWith("[System")) return;
4010
+ if (prompt.includes("#clawvault:no-recall") || prompt.includes("#clawvault:no-memory")) return;
4011
+ if (adaptiveConfig.enabled) {
4012
+ const check = shouldRetrieve(prompt, adaptiveConfig);
4013
+ if (!check.shouldRetrieve) {
4014
+ api.logger.debug(`[clawvault] adaptive skip: ${check.skipReason}`);
4015
+ return;
4016
+ }
4017
+ }
369
4018
  try {
370
4019
  const contextParts = [];
371
4020
  const recap = buildSessionRecap(vaultPath, {
372
4021
  maxAge: 24 * 60 * 60 * 1e3,
373
- // 24 hours
374
4022
  limit: 10,
375
4023
  includeContent: true
376
4024
  });
377
- if (recap.xml) {
378
- contextParts.push(recap.xml);
379
- }
380
- const searchTerms = extractSearchTerms(event.prompt);
381
- const results = qmdHybridSearch(searchTerms, collection, recallLimit);
382
- if (results.length > 0) {
383
- const topScore = results[0]?.score ?? 0;
384
- if (topScore >= 0.25) {
385
- contextParts.push(formatMemoriesForContext(results, collection));
386
- api.logger.info(`[clawvault] auto-recall: ${results.length} memories (top: ${(topScore * 100).toFixed(0)}%, query: "${searchTerms.slice(0, 60)}")`);
4025
+ if (recap.xml) contextParts.push(recap.xml);
4026
+ const searchTerms = extractSearchTerms(prompt);
4027
+ try {
4028
+ const results = await retrieve(searchTerms, {
4029
+ vaultPath,
4030
+ collection,
4031
+ config: { ...retrievalConfig, topK: recallLimit },
4032
+ scope: defaultScope
4033
+ });
4034
+ if (results.length > 0 && results[0].fusedScore >= 0.01) {
4035
+ const formatted = results.map((r, i) => {
4036
+ const file = r.file || "";
4037
+ const snippet = (r.snippet || "").replace(/@@ .+? @@\s*\(.+?\)\n?/g, "").trim() || r.title || "";
4038
+ return `${i + 1}. [${file}] ${snippet}`;
4039
+ });
4040
+ contextParts.push(`<relevant-memories>
4041
+ These are recalled from long-term vault memory. Treat as historical context.
4042
+ ${formatted.join("\n")}
4043
+ </relevant-memories>`);
4044
+ api.logger.info(
4045
+ `[clawvault] auto-recall: ${results.length} memories (top: ${(results[0].fusedScore * 100).toFixed(0)}%, query: "${searchTerms.slice(0, 60)}", pipeline: hybrid-v2)`
4046
+ );
4047
+ }
4048
+ } catch {
4049
+ const results = qmdHybridSearch(searchTerms, collection, recallLimit);
4050
+ if (results.length > 0) {
4051
+ const topScore = results[0]?.score ?? 0;
4052
+ if (topScore >= 0.25) {
4053
+ contextParts.push(formatMemoriesForContext(results, collection));
4054
+ api.logger.info(
4055
+ `[clawvault] auto-recall: ${results.length} memories (top: ${(topScore * 100).toFixed(0)}%, query: "${searchTerms.slice(0, 60)}", pipeline: qmd-fallback)`
4056
+ );
4057
+ }
387
4058
  }
388
4059
  }
389
4060
  if (contextParts.length === 0) return;
390
- return {
391
- prependContext: contextParts.join("\n\n")
392
- };
4061
+ return { prependContext: contextParts.join("\n\n") };
393
4062
  } catch (err) {
394
4063
  api.logger.warn(`[clawvault] auto-recall failed: ${String(err)}`);
395
4064
  }
@@ -397,9 +4066,12 @@ Call again with confirm=true to delete the top match.` }],
397
4066
  }
398
4067
  if (autoCapture) {
399
4068
  api.on("message_received", async (event) => {
400
- if (!event.content || !isObservable(event.content)) return;
4069
+ const content = event.content;
4070
+ if (!content || !isObservable(content, noiseConfig)) return;
4071
+ if (content.includes("#clawvault:no-capture") || content.includes("#clawvault:no-memory")) return;
4072
+ if (noiseConfig.enabled && isNoise(content, noiseConfig).isNoise) return;
401
4073
  try {
402
- const result = processMessageForObservations(event.content, {
4074
+ const result = processMessageForObservations(content, {
403
4075
  from: event.from,
404
4076
  sessionId: event.sessionId
405
4077
  });
@@ -410,7 +4082,6 @@ Call again with confirm=true to delete the top match.` }],
410
4082
  actor: event.from || "user",
411
4083
  writeLedger: true,
412
4084
  writeFiles: false
413
- // Only write to ledger for speed
414
4085
  });
415
4086
  api.logger.info(`[clawvault] auto-captured ${writeResult.successful} observations from incoming message`);
416
4087
  } catch (err) {
@@ -418,17 +4089,18 @@ Call again with confirm=true to delete the top match.` }],
418
4089
  }
419
4090
  });
420
4091
  api.on("agent_end", async (event) => {
421
- if (!event.success || !event.messages?.length) return;
4092
+ if (!event.success || !event.messages) return;
4093
+ const messages = event.messages;
422
4094
  try {
423
4095
  let captured = 0;
424
- for (const msg of event.messages) {
4096
+ for (const msg of messages) {
425
4097
  if (!msg || typeof msg !== "object") continue;
426
4098
  if (msg.role === "user") {
427
- const content = typeof msg.content === "string" ? msg.content : Array.isArray(msg.content) ? msg.content.filter((b) => b?.type === "text").map((b) => b.text).join(" ") : "";
428
- if (isObservable(content)) {
4099
+ const content = typeof msg.content === "string" ? msg.content : Array.isArray(msg.content) ? msg.content.filter((b) => b?.type === "text").map((b) => b.text || "").join(" ") : "";
4100
+ if (isObservable(content, noiseConfig) && !isNoise(content, noiseConfig).isNoise) {
429
4101
  const result = processMessageForObservations(content);
430
4102
  for (const obs of result.observations) {
431
- observe(vaultPath, obs.text, { tags: obs.tags });
4103
+ observeViaCli(vaultPath, obs.text, { tags: obs.tags });
432
4104
  captured++;
433
4105
  }
434
4106
  }
@@ -445,7 +4117,7 @@ Call again with confirm=true to delete the top match.` }],
445
4117
  }
446
4118
  api.on("before_compaction", async () => {
447
4119
  try {
448
- execFileSync("qmd", ["update", "-c", collection], {
4120
+ execFileSync2("qmd", ["update", "-c", collection], {
449
4121
  timeout: 15e3,
450
4122
  encoding: "utf-8",
451
4123
  stdio: ["ignore", "pipe", "pipe"]
@@ -466,19 +4138,14 @@ Call again with confirm=true to delete the top match.` }],
466
4138
  }
467
4139
  });
468
4140
  api.registerCli(
469
- ({ program }) => {
470
- const cmd = program.command("vault").description("ClawVault memory commands");
4141
+ (ctx) => {
4142
+ const cmd = ctx.program.command("vault").description("ClawVault memory commands");
471
4143
  cmd.command("status").action(() => {
472
- const config = getVaultConfig(vaultPath);
473
- console.log(JSON.stringify({
474
- vault: vaultPath,
475
- version: "3.1.0",
476
- templateSchemas: templateRegistry?.schemas.size ?? 0,
477
- ...config
478
- }, null, 2));
4144
+ const stats = computeMemoryStats(vaultPath, collection);
4145
+ console.log(JSON.stringify(stats, null, 2));
479
4146
  });
480
4147
  cmd.command("search <query>").option("-n, --limit <n>", "Max results", "10").action((query, opts) => {
481
- const results = qmdHybridSearch(query, collection, parseInt(opts.limit));
4148
+ const results = qmdHybridSearch(query, collection, parseInt(opts.limit || "10"));
482
4149
  console.log(formatSearchResults(results, collection));
483
4150
  });
484
4151
  cmd.command("templates").action(() => {
@@ -493,6 +4160,35 @@ Call again with confirm=true to delete the top match.` }],
493
4160
  const result = classifyText(text);
494
4161
  console.log(JSON.stringify(result, null, 2));
495
4162
  });
4163
+ cmd.command("stats").action(() => {
4164
+ const stats = computeMemoryStats(vaultPath, collection);
4165
+ console.log(JSON.stringify(stats, null, 2));
4166
+ });
4167
+ cmd.command("export <outputPath>").action((outputPath) => {
4168
+ const result = exportMemories(vaultPath, outputPath);
4169
+ console.log(`Exported ${result.count} memories to ${result.path}`);
4170
+ });
4171
+ cmd.command("import <inputPath>").action((inputPath) => {
4172
+ const result = importMemories(vaultPath, inputPath);
4173
+ console.log(`Imported: ${result.imported}, Skipped: ${result.skipped}`);
4174
+ if (result.errors.length > 0) {
4175
+ console.log(`Errors: ${result.errors.join(", ")}`);
4176
+ }
4177
+ });
4178
+ cmd.command("reembed").action(() => {
4179
+ console.log("Re-embedding all vault documents...");
4180
+ const files = scanVaultFiles(vaultPath, { maxAge: Infinity, limit: 1e5 });
4181
+ let count = 0;
4182
+ for (const file of files) {
4183
+ const content = `${file.frontmatter.title || ""} ${file.content}`.trim();
4184
+ autoEmbedViaOllama(file.path, content).then(() => {
4185
+ count++;
4186
+ if (count % 100 === 0) console.log(` Embedded ${count}/${files.length}...`);
4187
+ }).catch(() => {
4188
+ });
4189
+ }
4190
+ console.log(`Queued ${files.length} documents for re-embedding.`);
4191
+ });
496
4192
  },
497
4193
  { commands: ["vault"] }
498
4194
  );
@@ -504,25 +4200,15 @@ Call again with confirm=true to delete the top match.` }],
504
4200
  handler: (ctx) => {
505
4201
  const args = (ctx.args || "").trim();
506
4202
  if (!args || args === "status") {
507
- const config = getVaultConfig(vaultPath);
508
- let docCount = 0, vectorCount = 0;
509
- try {
510
- const stats = execFileSync("qmd", ["status", "--json", "-c", collection], {
511
- encoding: "utf-8",
512
- timeout: 5e3,
513
- stdio: ["ignore", "pipe", "pipe"]
514
- });
515
- const p = JSON.parse(stats);
516
- docCount = p.documents ?? p.doc_count ?? 0;
517
- vectorCount = p.vectors ?? p.vector_count ?? 0;
518
- } catch {
519
- }
4203
+ const stats = computeMemoryStats(vaultPath, collection);
520
4204
  return {
521
- text: `\u{1F9E0} ClawVault v2.2.0
4205
+ text: `\u{1F9E0} ClawVault v${PLUGIN_VERSION}
522
4206
  Vault: ${vaultPath}
523
- Docs: ${docCount} | Vectors: ${vectorCount}
4207
+ Docs: ${stats.documents} | Vectors: ${stats.vectors}
524
4208
  Recall: ${autoRecall ? "\u2705" : "\u274C"} | Capture: ${autoCapture ? "\u2705" : "\u274C"}
525
- Templates: ${templateRegistry?.schemas.size ?? 0} schemas`
4209
+ Templates: ${templateRegistry?.schemas.size ?? 0} schemas
4210
+ Scope: ${defaultScope}
4211
+ Pipeline: hybrid-v2 (BM25+Semantic+RRF${retrievalConfig.rerankProvider ? `+${retrievalConfig.rerankProvider}` : ""})`
526
4212
  };
527
4213
  }
528
4214
  if (args.startsWith("search ")) {
@@ -538,10 +4224,16 @@ Templates: ${templateRegistry?.schemas.size ?? 0} schemas`
538
4224
  const recap = buildSessionRecap(vaultPath, { limit: 10, includeContent: true });
539
4225
  return { text: recap.xml || "No recent activity found." };
540
4226
  }
541
- return { text: "Usage: /vault [status|search <query>|templates|recap]" };
4227
+ if (args === "stats") {
4228
+ const stats = computeMemoryStats(vaultPath, collection);
4229
+ return { text: JSON.stringify(stats, null, 2) };
4230
+ }
4231
+ return { text: "Usage: /vault [status|search <query>|templates|recap|stats]" };
542
4232
  }
543
4233
  });
544
- console.log(`[clawvault] v2.2.0 registered \u2014 vault=${vaultPath} templates=${templateRegistry?.schemas.size ?? 0}`);
4234
+ console.log(
4235
+ `[clawvault] v${PLUGIN_VERSION} registered \u2014 vault=${vaultPath} templates=${templateRegistry?.schemas.size ?? 0} pipeline=hybrid-v2 scope=${defaultScope}`
4236
+ );
545
4237
  }
546
4238
  };
547
4239
  var plugin_default = clawvaultPlugin;