poe-code 3.0.184 → 3.0.185

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 (228) hide show
  1. package/dist/cli/commands/configure-payload.d.ts +2 -1
  2. package/dist/cli/commands/configure-payload.js +4 -2
  3. package/dist/cli/commands/configure-payload.js.map +1 -1
  4. package/dist/cli/commands/configure.d.ts +1 -0
  5. package/dist/cli/commands/configure.js +50 -11
  6. package/dist/cli/commands/configure.js.map +1 -1
  7. package/dist/cli/commands/ensure-isolated-config.js +24 -2
  8. package/dist/cli/commands/ensure-isolated-config.js.map +1 -1
  9. package/dist/cli/commands/experiment.js +15 -2
  10. package/dist/cli/commands/experiment.js.map +1 -1
  11. package/dist/cli/commands/login.js +8 -4
  12. package/dist/cli/commands/login.js.map +1 -1
  13. package/dist/cli/commands/memory.js +16 -7
  14. package/dist/cli/commands/memory.js.map +1 -1
  15. package/dist/cli/commands/pipeline-init.js +32 -48
  16. package/dist/cli/commands/pipeline-init.js.map +1 -1
  17. package/dist/cli/commands/pipeline.js +89 -77
  18. package/dist/cli/commands/pipeline.js.map +1 -1
  19. package/dist/cli/commands/provider.d.ts +6 -0
  20. package/dist/cli/commands/provider.js +100 -0
  21. package/dist/cli/commands/provider.js.map +1 -0
  22. package/dist/cli/commands/shared.d.ts +7 -0
  23. package/dist/cli/commands/shared.js +3 -0
  24. package/dist/cli/commands/shared.js.map +1 -1
  25. package/dist/cli/commands/test.js +1 -1
  26. package/dist/cli/commands/test.js.map +1 -1
  27. package/dist/cli/commands/unconfigure.js +12 -3
  28. package/dist/cli/commands/unconfigure.js.map +1 -1
  29. package/dist/cli/container.d.ts +2 -0
  30. package/dist/cli/container.js +3 -0
  31. package/dist/cli/container.js.map +1 -1
  32. package/dist/cli/isolated-env-runner.js +2 -2
  33. package/dist/cli/isolated-env-runner.js.map +1 -1
  34. package/dist/cli/isolated-env.d.ts +3 -2
  35. package/dist/cli/isolated-env.js +31 -40
  36. package/dist/cli/isolated-env.js.map +1 -1
  37. package/dist/cli/poe-code-command-runner.js +9 -2
  38. package/dist/cli/poe-code-command-runner.js.map +1 -1
  39. package/dist/cli/program.js +5 -0
  40. package/dist/cli/program.js.map +1 -1
  41. package/dist/cli/service-registry.d.ts +7 -7
  42. package/dist/cli/service-registry.js.map +1 -1
  43. package/dist/index.js +2491 -1911
  44. package/dist/index.js.map +4 -4
  45. package/dist/providers/claude-code.d.ts +2 -1
  46. package/dist/providers/claude-code.js +5 -5
  47. package/dist/providers/claude-code.js.map +2 -2
  48. package/dist/providers/codex.d.ts +5 -1
  49. package/dist/providers/codex.js +39 -12
  50. package/dist/providers/codex.js.map +2 -2
  51. package/dist/providers/goose.d.ts +2 -1
  52. package/dist/providers/goose.js +24 -8
  53. package/dist/providers/goose.js.map +3 -3
  54. package/dist/providers/kimi.js +3 -3
  55. package/dist/providers/kimi.js.map +3 -3
  56. package/dist/providers/opencode.js +2 -2
  57. package/dist/providers/opencode.js.map +3 -3
  58. package/dist/providers/poe-agent.js +753 -649
  59. package/dist/providers/poe-agent.js.map +4 -4
  60. package/dist/sdk/container.js +3 -0
  61. package/dist/sdk/container.js.map +1 -1
  62. package/dist/sdk/pipeline.d.ts +1 -2
  63. package/dist/sdk/pipeline.js +51 -119
  64. package/dist/sdk/pipeline.js.map +1 -1
  65. package/dist/services/config.d.ts +1 -0
  66. package/dist/services/config.js +27 -2
  67. package/dist/services/config.js.map +1 -1
  68. package/dist/templates/pipeline/SKILL_plan.md +16 -42
  69. package/package.json +10 -1
  70. package/packages/agent-mcp-config/dist/apply.d.ts +6 -0
  71. package/packages/agent-mcp-config/dist/apply.js +175 -0
  72. package/packages/agent-mcp-config/dist/configs.d.ts +22 -0
  73. package/packages/agent-mcp-config/dist/configs.js +74 -0
  74. package/packages/agent-mcp-config/dist/index.d.ts +3 -0
  75. package/packages/agent-mcp-config/dist/index.js +2 -0
  76. package/packages/agent-mcp-config/dist/shapes.d.ts +31 -0
  77. package/packages/agent-mcp-config/dist/shapes.js +87 -0
  78. package/packages/agent-mcp-config/dist/types.d.ts +25 -0
  79. package/packages/agent-mcp-config/dist/types.js +1 -0
  80. package/packages/agent-skill-config/dist/apply.d.ts +25 -0
  81. package/packages/agent-skill-config/dist/apply.js +109 -0
  82. package/packages/agent-skill-config/dist/configs.d.ts +16 -0
  83. package/packages/agent-skill-config/dist/configs.js +66 -0
  84. package/packages/agent-skill-config/dist/exports.compile-check.d.ts +1 -0
  85. package/packages/agent-skill-config/dist/exports.compile-check.js +1 -0
  86. package/packages/agent-skill-config/dist/index.d.ts +5 -0
  87. package/packages/agent-skill-config/dist/index.js +2 -0
  88. package/packages/agent-skill-config/dist/templates/poe-generate.md +47 -0
  89. package/packages/agent-skill-config/dist/templates/terminal-pilot.md +45 -0
  90. package/packages/agent-skill-config/dist/templates.d.ts +3 -0
  91. package/packages/agent-skill-config/dist/templates.js +63 -0
  92. package/packages/agent-skill-config/dist/types.d.ts +16 -0
  93. package/packages/agent-skill-config/dist/types.js +1 -0
  94. package/packages/cmdkit/dist/cli.js +7 -2
  95. package/packages/cmdkit/dist/cli.js.map +2 -2
  96. package/packages/config-mutations/dist/execution/apply-mutation.d.ts +5 -0
  97. package/packages/config-mutations/dist/execution/apply-mutation.js +552 -0
  98. package/packages/config-mutations/dist/execution/path-utils.d.ts +17 -0
  99. package/packages/config-mutations/dist/execution/path-utils.js +58 -0
  100. package/packages/config-mutations/dist/execution/run-mutations.d.ts +7 -0
  101. package/packages/config-mutations/dist/execution/run-mutations.js +46 -0
  102. package/packages/config-mutations/dist/formats/index.d.ts +13 -0
  103. package/packages/config-mutations/dist/formats/index.js +49 -0
  104. package/packages/config-mutations/dist/formats/json.d.ts +31 -0
  105. package/packages/config-mutations/dist/formats/json.js +140 -0
  106. package/packages/config-mutations/dist/formats/toml.d.ts +2 -0
  107. package/packages/config-mutations/dist/formats/toml.js +72 -0
  108. package/packages/config-mutations/dist/formats/yaml.d.ts +2 -0
  109. package/packages/config-mutations/dist/formats/yaml.js +73 -0
  110. package/packages/config-mutations/dist/fs-utils.d.ts +18 -0
  111. package/packages/config-mutations/dist/fs-utils.js +45 -0
  112. package/packages/config-mutations/dist/index.d.ts +8 -0
  113. package/packages/config-mutations/dist/index.js +8 -0
  114. package/packages/config-mutations/dist/mutations/config-mutation.d.ts +47 -0
  115. package/packages/config-mutations/dist/mutations/config-mutation.js +34 -0
  116. package/packages/config-mutations/dist/mutations/file-mutation.d.ts +52 -0
  117. package/packages/config-mutations/dist/mutations/file-mutation.js +46 -0
  118. package/packages/config-mutations/dist/mutations/template-mutation.d.ts +40 -0
  119. package/packages/config-mutations/dist/mutations/template-mutation.js +32 -0
  120. package/packages/config-mutations/dist/template/render.d.ts +7 -0
  121. package/packages/config-mutations/dist/template/render.js +28 -0
  122. package/packages/config-mutations/dist/testing/format-utils.d.ts +7 -0
  123. package/packages/config-mutations/dist/testing/format-utils.js +21 -0
  124. package/packages/config-mutations/dist/testing/index.d.ts +3 -0
  125. package/packages/config-mutations/dist/testing/index.js +2 -0
  126. package/packages/config-mutations/dist/testing/mock-fs.d.ts +25 -0
  127. package/packages/config-mutations/dist/testing/mock-fs.js +170 -0
  128. package/packages/config-mutations/dist/types.d.ts +156 -0
  129. package/packages/config-mutations/dist/types.js +6 -0
  130. package/packages/memory/dist/audit.d.ts +11 -0
  131. package/packages/memory/dist/audit.js +131 -0
  132. package/packages/memory/dist/cache.cli.d.ts +9 -0
  133. package/packages/memory/dist/cache.cli.js +24 -0
  134. package/packages/memory/dist/cache.d.ts +14 -0
  135. package/packages/memory/dist/cache.js +149 -0
  136. package/packages/memory/dist/confidence.d.ts +4 -0
  137. package/packages/memory/dist/confidence.js +201 -0
  138. package/packages/memory/dist/corpus/001-archaeoastronomy.md +479 -0
  139. package/packages/memory/dist/corpus/002-magnetohydrodynamics.md +475 -0
  140. package/packages/memory/dist/corpus/003-biosemiotics.md +483 -0
  141. package/packages/memory/dist/corpus/004-cryopedology.md +483 -0
  142. package/packages/memory/dist/corpus/005-geomicrobiology.md +479 -0
  143. package/packages/memory/dist/corpus/006-aeronomy.md +487 -0
  144. package/packages/memory/dist/corpus/007-paleoclimatology.md +479 -0
  145. package/packages/memory/dist/corpus/008-hydrogeophysics.md +479 -0
  146. package/packages/memory/dist/corpus/009-magnetostratigraphy.md +475 -0
  147. package/packages/memory/dist/corpus/010-isotope-hydrology.md +481 -0
  148. package/packages/memory/dist/corpus/011-speleothem-geochemistry.md +474 -0
  149. package/packages/memory/dist/corpus/012-astrobiogeochemistry.md +475 -0
  150. package/packages/memory/dist/corpus/013-neuroethology.md +483 -0
  151. package/packages/memory/dist/corpus/014-chronophysiology.md +483 -0
  152. package/packages/memory/dist/corpus/015-limnogeochemistry.md +475 -0
  153. package/packages/memory/dist/corpus/016-palynology.md +483 -0
  154. package/packages/memory/dist/corpus/017-volcanotectonics.md +473 -0
  155. package/packages/memory/dist/corpus/018-seismotectonics.md +473 -0
  156. package/packages/memory/dist/corpus/019-biogeomorphology.md +475 -0
  157. package/packages/memory/dist/corpus/020-geobiophysics.md +479 -0
  158. package/packages/memory/dist/corpus/021-phytolith-analysis.md +481 -0
  159. package/packages/memory/dist/corpus/022-archaeometallurgy.md +479 -0
  160. package/packages/memory/dist/corpus/023-paleomagnetism.md +479 -0
  161. package/packages/memory/dist/corpus/024-biocalorimetry.md +475 -0
  162. package/packages/memory/dist/corpus/025-atmospheric-chemiluminescence.md +473 -0
  163. package/packages/memory/dist/corpus/026-cryoseismology.md +479 -0
  164. package/packages/memory/dist/corpus/027-extremophile-radiobiology.md +475 -0
  165. package/packages/memory/dist/corpus/028-heliophysics.md +479 -0
  166. package/packages/memory/dist/corpus/029-astroparticle-geophysics.md +474 -0
  167. package/packages/memory/dist/corpus/030-glaciohydrology.md +479 -0
  168. package/packages/memory/dist/corpus/031-permafrost-microbiology.md +477 -0
  169. package/packages/memory/dist/corpus/032-ecoacoustics.md +479 -0
  170. package/packages/memory/dist/corpus/033-dendroclimatology.md +473 -0
  171. package/packages/memory/dist/corpus/034-ionospheric-tomography.md +477 -0
  172. package/packages/memory/dist/corpus/035-marine-geodesy.md +481 -0
  173. package/packages/memory/dist/corpus/036-sedimentary-ancient-dna.md +481 -0
  174. package/packages/memory/dist/corpus/037-myrmecochory-dynamics.md +474 -0
  175. package/packages/memory/dist/corpus/038-chemosensory-ecology.md +477 -0
  176. package/packages/memory/dist/corpus/039-spintronics-materials.md +479 -0
  177. package/packages/memory/dist/corpus/040-nanotoxicology.md +483 -0
  178. package/packages/memory/dist/corpus/041-cosmochemistry.md +483 -0
  179. package/packages/memory/dist/corpus/042-quaternary-geochronology.md +471 -0
  180. package/packages/memory/dist/corpus/043-biophotonics.md +479 -0
  181. package/packages/memory/dist/corpus/044-evolutionary-morphometrics.md +481 -0
  182. package/packages/memory/dist/corpus/045-cryovolcanology.md +475 -0
  183. package/packages/memory/dist/corpus/046-exoplanet-atmospheric-dynamics.md +479 -0
  184. package/packages/memory/dist/corpus/047-microbial-electrosynthesis.md +477 -0
  185. package/packages/memory/dist/corpus/048-paleoseismology.md +479 -0
  186. package/packages/memory/dist/corpus/049-actinide-geochemistry.md +477 -0
  187. package/packages/memory/dist/corpus/050-quantum-biology.md +489 -0
  188. package/packages/memory/dist/edit.d.ts +10 -0
  189. package/packages/memory/dist/edit.js +43 -0
  190. package/packages/memory/dist/explain.cli.d.ts +8 -0
  191. package/packages/memory/dist/explain.cli.js +9 -0
  192. package/packages/memory/dist/explain.d.ts +8 -0
  193. package/packages/memory/dist/explain.js +77 -0
  194. package/packages/memory/dist/frontmatter.d.ts +9 -0
  195. package/packages/memory/dist/frontmatter.js +217 -0
  196. package/packages/memory/dist/index.d.ts +21 -0
  197. package/packages/memory/dist/index.js +4807 -0
  198. package/packages/memory/dist/index.js.map +7 -0
  199. package/packages/memory/dist/ingest.d.ts +3 -0
  200. package/packages/memory/dist/ingest.js +118 -0
  201. package/packages/memory/dist/init.d.ts +2 -0
  202. package/packages/memory/dist/init.js +24 -0
  203. package/packages/memory/dist/install.d.ts +18 -0
  204. package/packages/memory/dist/install.js +50 -0
  205. package/packages/memory/dist/lock.d.ts +19 -0
  206. package/packages/memory/dist/lock.js +102 -0
  207. package/packages/memory/dist/mcp.d.ts +7 -0
  208. package/packages/memory/dist/mcp.js +58 -0
  209. package/packages/memory/dist/pages.d.ts +4 -0
  210. package/packages/memory/dist/pages.js +92 -0
  211. package/packages/memory/dist/paths.d.ts +12 -0
  212. package/packages/memory/dist/paths.js +34 -0
  213. package/packages/memory/dist/query.d.ts +10 -0
  214. package/packages/memory/dist/query.js +130 -0
  215. package/packages/memory/dist/reconcile.d.ts +9 -0
  216. package/packages/memory/dist/reconcile.js +138 -0
  217. package/packages/memory/dist/resolve-root.d.ts +11 -0
  218. package/packages/memory/dist/resolve-root.js +22 -0
  219. package/packages/memory/dist/search.d.ts +2 -0
  220. package/packages/memory/dist/search.js +29 -0
  221. package/packages/memory/dist/status.d.ts +7 -0
  222. package/packages/memory/dist/status.js +46 -0
  223. package/packages/memory/dist/tokens.d.ts +2 -0
  224. package/packages/memory/dist/tokens.js +71 -0
  225. package/packages/memory/dist/types.d.ts +155 -0
  226. package/packages/memory/dist/types.js +1 -0
  227. package/packages/memory/dist/write.d.ts +9 -0
  228. package/packages/memory/dist/write.js +76 -0
@@ -0,0 +1,24 @@
1
+ import parseDuration from "parse-duration";
2
+ import { clearCache } from "./cache.js";
3
+ export async function runMemoryCacheStatus() {
4
+ console.log("cache status not implemented yet");
5
+ }
6
+ export async function runMemoryCacheClear(input) {
7
+ if (!input.yes) {
8
+ throw new Error("Refusing to clear cache without --yes.");
9
+ }
10
+ const olderThanMs = parseOlderThan(input.olderThan);
11
+ const result = await clearCache(input.root, olderThanMs === undefined ? {} : { olderThanMs });
12
+ console.log(`removed ${result.removed} cache ${result.removed === 1 ? "entry" : "entries"}`);
13
+ return result;
14
+ }
15
+ function parseOlderThan(value) {
16
+ if (value === undefined) {
17
+ return undefined;
18
+ }
19
+ const duration = parseDuration(value);
20
+ if (duration === null || Number.isNaN(duration) || duration < 0) {
21
+ throw new Error(`Invalid duration for --older-than: "${value}".`);
22
+ }
23
+ return duration;
24
+ }
@@ -0,0 +1,14 @@
1
+ import type { IngestCacheEntry, IngestCacheKey, MemoryRoot } from "./types.js";
2
+ export declare function computeIngestKey(input: {
3
+ sourceBytes: Buffer;
4
+ indexMdBytes: Buffer;
5
+ promptTemplateVersion: string;
6
+ agentId: string;
7
+ }): IngestCacheKey;
8
+ export declare function readCacheEntry(root: MemoryRoot, key: IngestCacheKey): Promise<IngestCacheEntry | null>;
9
+ export declare function writeCacheEntry(root: MemoryRoot, entry: IngestCacheEntry): Promise<void>;
10
+ export declare function clearCache(root: MemoryRoot, opts?: {
11
+ olderThanMs?: number;
12
+ }): Promise<{
13
+ removed: number;
14
+ }>;
@@ -0,0 +1,149 @@
1
+ import { createHash } from "node:crypto";
2
+ import * as fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { MEMORY_CACHE_DIR_RELPATH, MEMORY_INGEST_CACHE_DIR_RELPATH } from "./paths.js";
5
+ export function computeIngestKey(input) {
6
+ const hash = createHash("sha256");
7
+ hash.update(input.sourceBytes);
8
+ hash.update("\0");
9
+ hash.update(input.indexMdBytes);
10
+ hash.update("\0");
11
+ hash.update(input.promptTemplateVersion);
12
+ hash.update("\0");
13
+ hash.update(input.agentId);
14
+ return hash.digest("hex");
15
+ }
16
+ export async function readCacheEntry(root, key) {
17
+ const cachePath = path.join(root, MEMORY_INGEST_CACHE_DIR_RELPATH, `${key}.json`);
18
+ let raw;
19
+ try {
20
+ raw = await fs.readFile(cachePath, "utf8");
21
+ }
22
+ catch (error) {
23
+ if (isMissing(error)) {
24
+ return null;
25
+ }
26
+ throw error;
27
+ }
28
+ try {
29
+ return parseCacheEntry(JSON.parse(raw), key);
30
+ }
31
+ catch (error) {
32
+ const message = error instanceof Error ? error.message : String(error);
33
+ console.warn(`Ignoring ingest cache entry "${key}": ${message}`);
34
+ return null;
35
+ }
36
+ }
37
+ export async function writeCacheEntry(root, entry) {
38
+ await fs.mkdir(path.join(root, MEMORY_INGEST_CACHE_DIR_RELPATH), { recursive: true });
39
+ await fs.writeFile(path.join(root, MEMORY_INGEST_CACHE_DIR_RELPATH, `${entry.key}.json`), `${JSON.stringify(entry)}\n`, "utf8");
40
+ }
41
+ export async function clearCache(root, opts = {}) {
42
+ const ingestDir = path.join(root, MEMORY_INGEST_CACHE_DIR_RELPATH);
43
+ const cacheDir = path.join(root, MEMORY_CACHE_DIR_RELPATH);
44
+ const fileNames = await readCacheFileNames(ingestDir);
45
+ if (fileNames.length === 0) {
46
+ if (opts.olderThanMs === undefined) {
47
+ await fs.rm(cacheDir, { recursive: true, force: true });
48
+ }
49
+ return { removed: 0 };
50
+ }
51
+ if (opts.olderThanMs === undefined) {
52
+ await fs.rm(cacheDir, { recursive: true, force: true });
53
+ return { removed: fileNames.length };
54
+ }
55
+ const cutoff = Date.now() - opts.olderThanMs;
56
+ let removed = 0;
57
+ for (const fileName of fileNames) {
58
+ const key = fileName.slice(0, -".json".length);
59
+ const entry = await readCacheEntry(root, key);
60
+ if (entry === null || Date.parse(entry.ingestedAt) > cutoff) {
61
+ continue;
62
+ }
63
+ await fs.rm(path.join(ingestDir, fileName), { force: true });
64
+ removed += 1;
65
+ }
66
+ await removeEmptyDirectory(ingestDir);
67
+ await removeEmptyDirectory(cacheDir);
68
+ return { removed };
69
+ }
70
+ function parseCacheEntry(value, key) {
71
+ const object = expectRecord(value);
72
+ return {
73
+ key: expectString(object.key, "key"),
74
+ ingestedAt: expectString(object.ingestedAt, "ingestedAt"),
75
+ sourceLabel: expectString(object.sourceLabel, "sourceLabel"),
76
+ diff: parseMemoryDiff(object.diff),
77
+ exitCode: expectNumber(object.exitCode, "exitCode"),
78
+ durationMs: expectNumber(object.durationMs, "durationMs"),
79
+ memoryTokens: expectNumber(object.memoryTokens, "memoryTokens"),
80
+ sourceTokens: expectNumber(object.sourceTokens, "sourceTokens"),
81
+ promptTemplateVersion: expectString(object.promptTemplateVersion, "promptTemplateVersion"),
82
+ agentId: expectString(object.agentId, "agentId")
83
+ };
84
+ }
85
+ function parseMemoryDiff(value) {
86
+ const object = expectRecord(value);
87
+ return {
88
+ created: expectStringArray(object.created, "diff.created"),
89
+ updated: expectStringArray(object.updated, "diff.updated"),
90
+ deleted: expectStringArray(object.deleted, "diff.deleted")
91
+ };
92
+ }
93
+ function expectRecord(value) {
94
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
95
+ throw new Error("Expected a JSON object.");
96
+ }
97
+ return value;
98
+ }
99
+ function expectString(value, field) {
100
+ if (typeof value !== "string") {
101
+ throw new Error(`Expected string at "${field}".`);
102
+ }
103
+ return value;
104
+ }
105
+ function expectNumber(value, field) {
106
+ if (typeof value !== "number" || Number.isNaN(value)) {
107
+ throw new Error(`Expected number at "${field}".`);
108
+ }
109
+ return value;
110
+ }
111
+ function expectStringArray(value, field) {
112
+ if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string")) {
113
+ throw new Error(`Expected string[] at "${field}".`);
114
+ }
115
+ return value;
116
+ }
117
+ async function readCacheFileNames(ingestDir) {
118
+ try {
119
+ return (await fs.readdir(ingestDir))
120
+ .filter((fileName) => path.posix.extname(fileName).toLowerCase() === ".json")
121
+ .sort((left, right) => left.localeCompare(right));
122
+ }
123
+ catch (error) {
124
+ if (isMissing(error)) {
125
+ return [];
126
+ }
127
+ throw error;
128
+ }
129
+ }
130
+ async function removeEmptyDirectory(directoryPath) {
131
+ try {
132
+ const remainingEntries = await fs.readdir(directoryPath);
133
+ if (remainingEntries.length === 0) {
134
+ await fs.rmdir(directoryPath);
135
+ }
136
+ }
137
+ catch (error) {
138
+ if (isMissing(error)) {
139
+ return;
140
+ }
141
+ throw error;
142
+ }
143
+ }
144
+ function isMissing(error) {
145
+ return (typeof error === "object" &&
146
+ error !== null &&
147
+ "code" in error &&
148
+ error.code === "ENOENT");
149
+ }
@@ -0,0 +1,4 @@
1
+ import type { ConfidenceTag, TaggedClaim } from "./types.js";
2
+ export declare const TAG_RE: RegExp;
3
+ export declare function parseClaims(body: string): TaggedClaim[];
4
+ export declare function serializeTag(tag: ConfidenceTag): string;
@@ -0,0 +1,201 @@
1
+ import { parseSourceRef, serializeSourceRef } from "./frontmatter.js";
2
+ export const TAG_RE = /^<!--\s*memory:(?<verb>extracted|inferred|ambiguous)(?<rest>[^>]*?)-->\s*$/;
3
+ export function parseClaims(body) {
4
+ const lines = normalizeNewlines(body).split("\n");
5
+ const claims = [];
6
+ for (let index = 0; index < lines.length; index += 1) {
7
+ const tagLine = lines[index] ?? "";
8
+ const match = TAG_RE.exec(tagLine);
9
+ if (match?.groups?.verb === undefined) {
10
+ continue;
11
+ }
12
+ const claimLines = [];
13
+ for (let claimIndex = index + 1; claimIndex < lines.length; claimIndex += 1) {
14
+ const line = lines[claimIndex] ?? "";
15
+ if (line.trim().length === 0 || TAG_RE.test(line)) {
16
+ break;
17
+ }
18
+ claimLines.push(line);
19
+ }
20
+ if (claimLines.length === 0) {
21
+ throw new Error(`Confidence tag on line ${index + 1} is not followed by a claim paragraph.`);
22
+ }
23
+ claims.push({
24
+ tag: parseTag(match.groups.verb, match.groups.rest ?? ""),
25
+ body: claimLines.join("\n"),
26
+ lineNumber: index + 1
27
+ });
28
+ }
29
+ return claims;
30
+ }
31
+ export function serializeTag(tag) {
32
+ switch (tag.verb) {
33
+ case "extracted":
34
+ return serializeComment("extracted", {
35
+ source: serializeSourceRef(tag.source),
36
+ ...(tag.note === undefined ? {} : { note: tag.note })
37
+ });
38
+ case "inferred":
39
+ return serializeComment("inferred", {
40
+ confidence: serializeConfidence(tag.confidence),
41
+ ...(tag.source === undefined ? {} : { source: serializeSourceRef(tag.source) }),
42
+ ...(tag.note === undefined ? {} : { note: tag.note })
43
+ });
44
+ case "ambiguous": {
45
+ const reason = tag.reason.trim();
46
+ if (reason.length === 0) {
47
+ throw new Error('Ambiguous confidence tags require a non-empty "reason".');
48
+ }
49
+ return serializeComment("ambiguous", { reason });
50
+ }
51
+ }
52
+ }
53
+ function parseTag(verb, rest) {
54
+ const attrs = parseAttributes(rest);
55
+ switch (verb) {
56
+ case "extracted": {
57
+ const source = attrs.source;
58
+ if (source === undefined) {
59
+ throw new Error('Extracted confidence tags require "source".');
60
+ }
61
+ assertOnlyKeys(attrs, verb, ["source", "note"]);
62
+ return {
63
+ verb,
64
+ source: parseSourceRef(source),
65
+ ...(attrs.note === undefined ? {} : { note: attrs.note })
66
+ };
67
+ }
68
+ case "inferred": {
69
+ const confidence = attrs.confidence;
70
+ if (confidence === undefined) {
71
+ throw new Error('Inferred confidence tags require "confidence".');
72
+ }
73
+ assertOnlyKeys(attrs, verb, ["confidence", "source", "note"]);
74
+ return {
75
+ verb,
76
+ confidence: parseConfidence(confidence),
77
+ ...(attrs.source === undefined ? {} : { source: parseSourceRef(attrs.source) }),
78
+ ...(attrs.note === undefined ? {} : { note: attrs.note })
79
+ };
80
+ }
81
+ case "ambiguous": {
82
+ const reason = attrs.reason?.trim();
83
+ if (reason === undefined || reason.length === 0) {
84
+ throw new Error('Ambiguous confidence tags require a non-empty "reason".');
85
+ }
86
+ assertOnlyKeys(attrs, verb, ["reason"]);
87
+ return {
88
+ verb,
89
+ reason
90
+ };
91
+ }
92
+ }
93
+ }
94
+ function parseAttributes(rest) {
95
+ const attrs = {};
96
+ let index = 0;
97
+ while (index < rest.length) {
98
+ index = skipWhitespace(rest, index);
99
+ if (index >= rest.length) {
100
+ break;
101
+ }
102
+ const keyStart = index;
103
+ while (index < rest.length && isKeyCharacter(rest[index] ?? "")) {
104
+ index += 1;
105
+ }
106
+ if (keyStart === index) {
107
+ throw new Error(`Invalid confidence tag attribute near "${rest.slice(index).trim()}".`);
108
+ }
109
+ const key = rest.slice(keyStart, index);
110
+ if ((rest[index] ?? "") !== "=") {
111
+ throw new Error(`Invalid confidence tag attribute "${key}".`);
112
+ }
113
+ index += 1;
114
+ const { value, nextIndex } = readAttributeValue(rest, index);
115
+ if (attrs[key] !== undefined) {
116
+ throw new Error(`Duplicate confidence tag attribute "${key}".`);
117
+ }
118
+ attrs[key] = value;
119
+ index = nextIndex;
120
+ }
121
+ return attrs;
122
+ }
123
+ function readAttributeValue(input, index) {
124
+ if (index >= input.length) {
125
+ throw new Error("Missing confidence tag attribute value.");
126
+ }
127
+ if (input[index] === '"') {
128
+ const endQuote = findClosingQuote(input, index + 1);
129
+ return {
130
+ value: JSON.parse(input.slice(index, endQuote + 1)),
131
+ nextIndex: endQuote + 1
132
+ };
133
+ }
134
+ let nextIndex = index;
135
+ while (nextIndex < input.length && !isWhitespace(input[nextIndex] ?? "")) {
136
+ nextIndex += 1;
137
+ }
138
+ if (nextIndex === index) {
139
+ throw new Error("Missing confidence tag attribute value.");
140
+ }
141
+ return {
142
+ value: input.slice(index, nextIndex),
143
+ nextIndex
144
+ };
145
+ }
146
+ function findClosingQuote(input, start) {
147
+ let escaped = false;
148
+ for (let index = start; index < input.length; index += 1) {
149
+ const char = input[index] ?? "";
150
+ if (escaped) {
151
+ escaped = false;
152
+ continue;
153
+ }
154
+ if (char === "\\") {
155
+ escaped = true;
156
+ continue;
157
+ }
158
+ if (char === '"') {
159
+ return index;
160
+ }
161
+ }
162
+ throw new Error("Unterminated quoted confidence tag attribute.");
163
+ }
164
+ function assertOnlyKeys(attrs, verb, allowedKeys) {
165
+ const disallowedKeys = Object.keys(attrs).filter((key) => !allowedKeys.includes(key));
166
+ if (disallowedKeys.length > 0) {
167
+ throw new Error(`${verb} confidence tags do not support: ${disallowedKeys.map((key) => `"${key}"`).join(", ")}.`);
168
+ }
169
+ }
170
+ function parseConfidence(rawConfidence) {
171
+ const confidence = Number(rawConfidence);
172
+ if (!Number.isFinite(confidence) || confidence <= 0 || confidence > 1) {
173
+ throw new Error(`Invalid confidence value "${rawConfidence}". Expected a number in (0, 1].`);
174
+ }
175
+ return confidence;
176
+ }
177
+ function serializeComment(verb, attrs) {
178
+ const parts = Object.entries(attrs).map(([key, value]) => `${key}=${serializeAttributeValue(key, value)}`);
179
+ return `<!-- memory:${verb}${parts.length === 0 ? "" : ` ${parts.join(" ")}`} -->`;
180
+ }
181
+ function serializeAttributeValue(key, value) {
182
+ return key === "source" || key === "confidence" ? value : JSON.stringify(value);
183
+ }
184
+ function serializeConfidence(confidence) {
185
+ return String(parseConfidence(String(confidence)));
186
+ }
187
+ function normalizeNewlines(value) {
188
+ return value.replaceAll("\r\n", "\n").replaceAll("\r", "\n");
189
+ }
190
+ function skipWhitespace(input, index) {
191
+ while (index < input.length && isWhitespace(input[index] ?? "")) {
192
+ index += 1;
193
+ }
194
+ return index;
195
+ }
196
+ function isKeyCharacter(char) {
197
+ return (char >= "a" && char <= "z") || (char >= "A" && char <= "Z");
198
+ }
199
+ function isWhitespace(char) {
200
+ return char === " " || char === "\t" || char === "\n" || char === "\r";
201
+ }