@c9up/polaris 0.1.0 → 0.1.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 (217) hide show
  1. package/README.md +614 -10
  2. package/dist/Polaris.d.ts +6 -2
  3. package/dist/Polaris.d.ts.map +1 -1
  4. package/dist/Polaris.js +14 -0
  5. package/dist/Polaris.js.map +1 -1
  6. package/dist/PolarisClient.d.ts +51 -3
  7. package/dist/PolarisClient.d.ts.map +1 -1
  8. package/dist/PolarisClient.js +213 -45
  9. package/dist/PolarisClient.js.map +1 -1
  10. package/dist/PolarisProvider.d.ts +7 -2
  11. package/dist/PolarisProvider.d.ts.map +1 -1
  12. package/dist/PolarisProvider.js +34 -2
  13. package/dist/PolarisProvider.js.map +1 -1
  14. package/dist/augmentations.d.ts +4 -1
  15. package/dist/augmentations.d.ts.map +1 -1
  16. package/dist/augmentations.js +1 -1
  17. package/dist/budget.d.ts +39 -6
  18. package/dist/budget.d.ts.map +1 -1
  19. package/dist/budget.js +79 -12
  20. package/dist/budget.js.map +1 -1
  21. package/dist/catalog/catalog.d.ts +37 -0
  22. package/dist/catalog/catalog.d.ts.map +1 -0
  23. package/dist/catalog/catalog.js +145 -0
  24. package/dist/catalog/catalog.js.map +1 -0
  25. package/dist/catalog/discover.d.ts +64 -0
  26. package/dist/catalog/discover.d.ts.map +1 -0
  27. package/dist/catalog/discover.js +148 -0
  28. package/dist/catalog/discover.js.map +1 -0
  29. package/dist/catalog/index.d.ts +8 -0
  30. package/dist/catalog/index.d.ts.map +1 -0
  31. package/dist/catalog/index.js +8 -0
  32. package/dist/catalog/index.js.map +1 -0
  33. package/dist/commands/index.d.ts +82 -0
  34. package/dist/commands/index.d.ts.map +1 -0
  35. package/dist/commands/index.js +278 -0
  36. package/dist/commands/index.js.map +1 -0
  37. package/dist/cost.d.ts +3 -1
  38. package/dist/cost.d.ts.map +1 -1
  39. package/dist/cost.js +8 -3
  40. package/dist/cost.js.map +1 -1
  41. package/dist/drivers/anthropic.d.ts +6 -1
  42. package/dist/drivers/anthropic.d.ts.map +1 -1
  43. package/dist/drivers/anthropic.js +166 -21
  44. package/dist/drivers/anthropic.js.map +1 -1
  45. package/dist/drivers/openai-compatible.d.ts +1 -1
  46. package/dist/drivers/openai-compatible.d.ts.map +1 -1
  47. package/dist/drivers/openai-compatible.js +106 -70
  48. package/dist/drivers/openai-compatible.js.map +1 -1
  49. package/dist/errors.d.ts +24 -0
  50. package/dist/errors.d.ts.map +1 -1
  51. package/dist/errors.js +24 -0
  52. package/dist/errors.js.map +1 -1
  53. package/dist/index.d.ts +6 -4
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +3 -1
  56. package/dist/index.js.map +1 -1
  57. package/dist/json-schema.d.ts +33 -0
  58. package/dist/json-schema.d.ts.map +1 -0
  59. package/dist/json-schema.js +508 -0
  60. package/dist/json-schema.js.map +1 -0
  61. package/dist/log.d.ts.map +1 -1
  62. package/dist/log.js +3 -1
  63. package/dist/log.js.map +1 -1
  64. package/dist/mcp/client.d.ts +81 -0
  65. package/dist/mcp/client.d.ts.map +1 -0
  66. package/dist/mcp/client.js +401 -0
  67. package/dist/mcp/client.js.map +1 -0
  68. package/dist/mcp/endpoint.d.ts +54 -0
  69. package/dist/mcp/endpoint.d.ts.map +1 -0
  70. package/dist/mcp/endpoint.js +158 -0
  71. package/dist/mcp/endpoint.js.map +1 -0
  72. package/dist/mcp/host.d.ts +42 -0
  73. package/dist/mcp/host.d.ts.map +1 -0
  74. package/dist/mcp/host.js +111 -0
  75. package/dist/mcp/host.js.map +1 -0
  76. package/dist/mcp/http.d.ts +32 -0
  77. package/dist/mcp/http.d.ts.map +1 -0
  78. package/dist/mcp/http.js +190 -0
  79. package/dist/mcp/http.js.map +1 -0
  80. package/dist/mcp/index.d.ts +23 -0
  81. package/dist/mcp/index.d.ts.map +1 -0
  82. package/dist/mcp/index.js +21 -0
  83. package/dist/mcp/index.js.map +1 -0
  84. package/dist/mcp/manifest.d.ts +36 -0
  85. package/dist/mcp/manifest.d.ts.map +1 -0
  86. package/dist/mcp/manifest.js +40 -0
  87. package/dist/mcp/manifest.js.map +1 -0
  88. package/dist/mcp/rpc.d.ts +39 -0
  89. package/dist/mcp/rpc.d.ts.map +1 -0
  90. package/dist/mcp/rpc.js +149 -0
  91. package/dist/mcp/rpc.js.map +1 -0
  92. package/dist/mcp/stdio.d.ts +24 -0
  93. package/dist/mcp/stdio.d.ts.map +1 -0
  94. package/dist/mcp/stdio.js +157 -0
  95. package/dist/mcp/stdio.js.map +1 -0
  96. package/dist/mcp/worker-main.d.ts +6 -0
  97. package/dist/mcp/worker-main.d.ts.map +1 -0
  98. package/dist/mcp/worker-main.js +17 -0
  99. package/dist/mcp/worker-main.js.map +1 -0
  100. package/dist/mcp/worker.d.ts +54 -0
  101. package/dist/mcp/worker.d.ts.map +1 -0
  102. package/dist/mcp/worker.js +166 -0
  103. package/dist/mcp/worker.js.map +1 -0
  104. package/dist/meter.d.ts +6 -0
  105. package/dist/meter.d.ts.map +1 -1
  106. package/dist/meter.js +6 -3
  107. package/dist/meter.js.map +1 -1
  108. package/dist/redact.d.ts +4 -3
  109. package/dist/redact.d.ts.map +1 -1
  110. package/dist/redact.js +5 -4
  111. package/dist/redact.js.map +1 -1
  112. package/dist/redis-budget.d.ts +45 -0
  113. package/dist/redis-budget.d.ts.map +1 -0
  114. package/dist/redis-budget.js +56 -0
  115. package/dist/redis-budget.js.map +1 -0
  116. package/dist/response.d.ts +21 -3
  117. package/dist/response.d.ts.map +1 -1
  118. package/dist/response.js +71 -7
  119. package/dist/response.js.map +1 -1
  120. package/dist/server-sent.d.ts +9 -0
  121. package/dist/server-sent.d.ts.map +1 -0
  122. package/dist/server-sent.js +74 -0
  123. package/dist/server-sent.js.map +1 -0
  124. package/dist/server-tools.d.ts +12 -0
  125. package/dist/server-tools.d.ts.map +1 -0
  126. package/dist/server-tools.js +71 -0
  127. package/dist/server-tools.js.map +1 -0
  128. package/dist/services/catalog.d.ts +19 -0
  129. package/dist/services/catalog.d.ts.map +1 -0
  130. package/dist/services/catalog.js +37 -0
  131. package/dist/services/catalog.js.map +1 -0
  132. package/dist/skills/folder.d.ts +32 -0
  133. package/dist/skills/folder.d.ts.map +1 -0
  134. package/dist/skills/folder.js +170 -0
  135. package/dist/skills/folder.js.map +1 -0
  136. package/dist/skills/frontmatter.d.ts +15 -0
  137. package/dist/skills/frontmatter.d.ts.map +1 -0
  138. package/dist/skills/frontmatter.js +92 -0
  139. package/dist/skills/frontmatter.js.map +1 -0
  140. package/dist/skills/index.d.ts +18 -0
  141. package/dist/skills/index.d.ts.map +1 -0
  142. package/dist/skills/index.js +18 -0
  143. package/dist/skills/index.js.map +1 -0
  144. package/dist/skills/kit.d.ts +39 -0
  145. package/dist/skills/kit.d.ts.map +1 -0
  146. package/dist/skills/kit.js +124 -0
  147. package/dist/skills/kit.js.map +1 -0
  148. package/dist/skills/skill.d.ts +67 -0
  149. package/dist/skills/skill.d.ts.map +1 -0
  150. package/dist/skills/skill.js +114 -0
  151. package/dist/skills/skill.js.map +1 -0
  152. package/dist/sse.d.ts +43 -6
  153. package/dist/sse.d.ts.map +1 -1
  154. package/dist/sse.js +44 -18
  155. package/dist/sse.js.map +1 -1
  156. package/dist/testing/FakeProvider.d.ts.map +1 -1
  157. package/dist/testing/FakeProvider.js +1 -0
  158. package/dist/testing/FakeProvider.js.map +1 -1
  159. package/dist/tool-modules.d.ts +17 -0
  160. package/dist/tool-modules.d.ts.map +1 -0
  161. package/dist/tool-modules.js +43 -0
  162. package/dist/tool-modules.js.map +1 -0
  163. package/dist/tools.d.ts +69 -5
  164. package/dist/tools.d.ts.map +1 -1
  165. package/dist/tools.js +174 -31
  166. package/dist/tools.js.map +1 -1
  167. package/dist/transport.d.ts +8 -3
  168. package/dist/transport.d.ts.map +1 -1
  169. package/dist/transport.js +19 -4
  170. package/dist/transport.js.map +1 -1
  171. package/dist/types.d.ts +106 -2
  172. package/dist/types.d.ts.map +1 -1
  173. package/package.json +22 -1
  174. package/src/Polaris.ts +25 -1
  175. package/src/PolarisClient.ts +288 -51
  176. package/src/PolarisProvider.ts +40 -2
  177. package/src/augmentations.ts +4 -1
  178. package/src/budget.ts +115 -15
  179. package/src/catalog/catalog.ts +180 -0
  180. package/src/catalog/discover.ts +234 -0
  181. package/src/catalog/index.ts +19 -0
  182. package/src/commands/index.ts +412 -0
  183. package/src/cost.ts +9 -3
  184. package/src/drivers/anthropic.ts +203 -20
  185. package/src/drivers/openai-compatible.ts +143 -76
  186. package/src/errors.ts +41 -0
  187. package/src/index.ts +16 -0
  188. package/src/json-schema.ts +597 -0
  189. package/src/log.ts +3 -1
  190. package/src/mcp/client.ts +532 -0
  191. package/src/mcp/endpoint.ts +229 -0
  192. package/src/mcp/host.ts +140 -0
  193. package/src/mcp/http.ts +276 -0
  194. package/src/mcp/index.ts +51 -0
  195. package/src/mcp/manifest.ts +73 -0
  196. package/src/mcp/rpc.ts +197 -0
  197. package/src/mcp/stdio.ts +190 -0
  198. package/src/mcp/worker-main.ts +18 -0
  199. package/src/mcp/worker.ts +212 -0
  200. package/src/meter.ts +13 -2
  201. package/src/redact.ts +6 -3
  202. package/src/redis-budget.ts +85 -0
  203. package/src/response.ts +86 -8
  204. package/src/server-sent.ts +70 -0
  205. package/src/server-tools.ts +77 -0
  206. package/src/services/catalog.ts +43 -0
  207. package/src/skills/folder.ts +231 -0
  208. package/src/skills/frontmatter.ts +92 -0
  209. package/src/skills/index.ts +38 -0
  210. package/src/skills/kit.ts +163 -0
  211. package/src/skills/skill.ts +171 -0
  212. package/src/sse.ts +88 -22
  213. package/src/testing/FakeProvider.ts +1 -0
  214. package/src/tool-modules.ts +49 -0
  215. package/src/tools.ts +260 -27
  216. package/src/transport.ts +25 -3
  217. package/src/types.ts +108 -2
@@ -4,8 +4,11 @@
4
4
  * register() binds `polaris.manager`, built on first resolve, unless the
5
5
  * application bound its own first.
6
6
  * boot() builds it and publishes it to `@c9up/polaris/services/main`;
7
- * a missing or invalid config fails the boot here.
8
- * shutdown() releases the published manager, if it is still this one.
7
+ * a missing or invalid config fails the boot here. Reads the
8
+ * tool catalog (`polaris.json`, written by `ream polaris:index`),
9
+ * binds it as `polaris.catalog` and publishes it to
10
+ * `@c9up/polaris/services/catalog`.
11
+ * shutdown() releases what it published, if it is still its own.
9
12
  *
10
13
  * Typed against the slice of ream's `AppContext` it uses rather than importing
11
14
  * it, so ream stays an optional peer.
@@ -23,6 +26,8 @@ interface ConfigStoreLike {
23
26
  export interface PolarisAppContext {
24
27
  container: ContainerLike;
25
28
  config: ConfigStoreLike;
29
+ /** The application's root; the manifest's path is resolved from it. */
30
+ appRoot?: URL | undefined;
26
31
  }
27
32
  export default class PolarisProvider {
28
33
  #private;
@@ -1 +1 @@
1
- {"version":3,"file":"PolarisProvider.d.ts","sourceRoot":"","sources":["../src/PolarisProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,oBAAoB,CAAC;AAY5B,UAAU,aAAa;IACtB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,OAAO,GAAG,IAAI,CAAC;IAChE,iDAAiD;IACjD,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACzD,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CACtC;AAED,UAAU,eAAe;IACxB,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;CAC7C;AAED,MAAM,WAAW,iBAAiB;IACjC,SAAS,EAAE,aAAa,CAAC;IACzB,MAAM,EAAE,eAAe,CAAC;CACxB;AAED,MAAM,CAAC,OAAO,OAAO,eAAe;;gBAIvB,GAAG,EAAE,iBAAiB;IAIlC,QAAQ,IAAI,IAAI;IAWV,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAMrB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAM/B"}
1
+ {"version":3,"file":"PolarisProvider.d.ts","sourceRoot":"","sources":["../src/PolarisProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,oBAAoB,CAAC;AAgB5B,UAAU,aAAa;IACtB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,OAAO,GAAG,IAAI,CAAC;IAChE,iDAAiD;IACjD,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACzD,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CACtC;AAED,UAAU,eAAe;IACxB,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;CAC7C;AAED,MAAM,WAAW,iBAAiB;IACjC,SAAS,EAAE,aAAa,CAAC;IACzB,MAAM,EAAE,eAAe,CAAC;IACxB,uEAAuE;IACvE,OAAO,CAAC,EAAE,GAAG,GAAG,SAAS,CAAC;CAC1B;AAED,MAAM,CAAC,OAAO,OAAO,eAAe;;gBAKvB,GAAG,EAAE,iBAAiB;IAIlC,QAAQ,IAAI,IAAI;IAWV,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAyBrB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAa/B"}
@@ -4,20 +4,30 @@
4
4
  * register() binds `polaris.manager`, built on first resolve, unless the
5
5
  * application bound its own first.
6
6
  * boot() builds it and publishes it to `@c9up/polaris/services/main`;
7
- * a missing or invalid config fails the boot here.
8
- * shutdown() releases the published manager, if it is still this one.
7
+ * a missing or invalid config fails the boot here. Reads the
8
+ * tool catalog (`polaris.json`, written by `ream polaris:index`),
9
+ * binds it as `polaris.catalog` and publishes it to
10
+ * `@c9up/polaris/services/catalog`.
11
+ * shutdown() releases what it published, if it is still its own.
9
12
  *
10
13
  * Typed against the slice of ream's `AppContext` it uses rather than importing
11
14
  * it, so ream stays an optional peer.
12
15
  */
16
+ import { resolve } from "node:path";
17
+ import { fileURLToPath } from "node:url";
13
18
  import "./augmentations.js";
19
+ import { Catalog } from "./catalog/catalog.js";
20
+ import { DEFAULT_MANIFEST } from "./catalog/discover.js";
14
21
  import { E_POLARIS_MISSING_CONFIG } from "./errors.js";
15
22
  import { Polaris, } from "./Polaris.js";
23
+ import { clearCatalog, getCatalog, setCatalog } from "./services/catalog.js";
16
24
  import { clearPolaris, getPolaris, setPolaris } from "./services/main.js";
17
25
  const TOKEN = "polaris.manager";
26
+ const CATALOG_TOKEN = "polaris.catalog";
18
27
  export default class PolarisProvider {
19
28
  #app;
20
29
  #owned;
30
+ #ownedCatalog;
21
31
  constructor(app) {
22
32
  this.#app = app;
23
33
  }
@@ -36,12 +46,34 @@ export default class PolarisProvider {
36
46
  const manager = await this.#app.container.resolve(TOKEN);
37
47
  this.#owned = manager;
38
48
  setPolaris(manager);
49
+ const config = this.#app.config.get("polaris");
50
+ const options = config?.catalog;
51
+ if (options === false)
52
+ return;
53
+ const manifest = typeof options === "object" &&
54
+ options !== null &&
55
+ "manifest" in options &&
56
+ typeof options.manifest === "string"
57
+ ? options.manifest
58
+ : DEFAULT_MANIFEST;
59
+ const root = this.#app.appRoot === undefined
60
+ ? process.cwd()
61
+ : fileURLToPath(this.#app.appRoot);
62
+ const catalog = await Catalog.read(resolve(root, manifest));
63
+ this.#ownedCatalog = catalog;
64
+ this.#app.container.singleton(CATALOG_TOKEN, () => catalog);
65
+ setCatalog(catalog);
39
66
  }
40
67
  async shutdown() {
41
68
  if (this.#owned !== undefined && getPolaris() === this.#owned) {
42
69
  clearPolaris();
43
70
  }
44
71
  this.#owned = undefined;
72
+ if (this.#ownedCatalog !== undefined &&
73
+ getCatalog() === this.#ownedCatalog) {
74
+ clearCatalog();
75
+ }
76
+ this.#ownedCatalog = undefined;
45
77
  }
46
78
  }
47
79
  //# sourceMappingURL=PolarisProvider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PolarisProvider.js","sourceRoot":"","sources":["../src/PolarisProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,oBAAoB,CAAC;AAC5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAEN,OAAO,GAEP,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAG1E,MAAM,KAAK,GAAG,iBAAiB,CAAC;AAkBhC,MAAM,CAAC,OAAO,OAAO,eAAe;IAC1B,IAAI,CAAoB;IACjC,MAAM,CAA6B;IAEnC,YAAY,GAAsB;QACjC,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;IACjB,CAAC;IAED,QAAQ;QACP,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;QACtC,IAAI,SAAS,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC;YAAE,OAAO;QACnC,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE;YAC/B,MAAM,MAAM,GACX,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAmC,SAAS,CAAC,CAAC;YACnE,IAAI,MAAM,KAAK,SAAS;gBAAE,MAAM,IAAI,wBAAwB,EAAE,CAAC;YAC/D,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,IAAI;QACT,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAiB,KAAK,CAAC,CAAC;QACzE,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;QACtB,UAAU,CAAC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CAAC,SAAS,CAAC,SAAS,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC;QAC5D,UAAU,CAAC,OAAO,CAAC,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,QAAQ;QACb,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,UAAU,EAAE,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YAC/D,YAAY,EAAE,CAAC;QAChB,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACxB,IACC,IAAI,CAAC,aAAa,KAAK,SAAS;YAChC,UAAU,EAAE,KAAK,IAAI,CAAC,aAAa,EAClC,CAAC;YACF,YAAY,EAAE,CAAC;QAChB,CAAC;QACD,IAAI,CAAC,aAAa,GAAG,SAAS,CAAC;IAChC,CAAC;CACD"}
@@ -1,15 +1,18 @@
1
1
  /**
2
2
  * Tell ream's `ContainerBindings` what `container.resolve('polaris.manager')`
3
- * returns. Loaded from the package barrel and from the provider, so an
3
+ * and `container.resolve('polaris.catalog')` return. Loaded from the package barrel and from the provider, so an
4
4
  * application writes no `declare module` of its own for it.
5
5
  *
6
6
  * Type-only: ream stays an optional peer.
7
7
  */
8
+ import type { Catalog } from "./catalog/catalog.js";
8
9
  import type { PolarisService } from "./Polaris.js";
9
10
  declare module "@c9up/ream/types" {
10
11
  interface ContainerBindings {
11
12
  /** The provider manager, bound by `PolarisProvider`. */
12
13
  "polaris.manager": PolarisService;
14
+ /** The tool and skill catalog, read from `polaris.json` by `PolarisProvider`. */
15
+ "polaris.catalog": Catalog;
13
16
  }
14
17
  }
15
18
  //# sourceMappingURL=augmentations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"augmentations.d.ts","sourceRoot":"","sources":["../src/augmentations.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,QAAQ,kBAAkB,CAAC;IACjC,UAAU,iBAAiB;QAC1B,wDAAwD;QACxD,iBAAiB,EAAE,cAAc,CAAC;KAClC;CACD"}
1
+ {"version":3,"file":"augmentations.d.ts","sourceRoot":"","sources":["../src/augmentations.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,QAAQ,kBAAkB,CAAC;IACjC,UAAU,iBAAiB;QAC1B,wDAAwD;QACxD,iBAAiB,EAAE,cAAc,CAAC;QAClC,iFAAiF;QACjF,iBAAiB,EAAE,OAAO,CAAC;KAC3B;CACD"}
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Tell ream's `ContainerBindings` what `container.resolve('polaris.manager')`
3
- * returns. Loaded from the package barrel and from the provider, so an
3
+ * and `container.resolve('polaris.catalog')` return. Loaded from the package barrel and from the provider, so an
4
4
  * application writes no `declare module` of its own for it.
5
5
  *
6
6
  * Type-only: ream stays an optional peer.
package/dist/budget.d.ts CHANGED
@@ -10,23 +10,51 @@
10
10
  * the same moment from passing the limit together.
11
11
  */
12
12
  /**
13
- * Where spend is kept. `add` must be atomic (a Redis `INCRBYFLOAT`, an SQL
13
+ * Where spend is kept. `add` must be atomic (a Redis `INCRBY`, an SQL
14
14
  * `UPDATE … SET spent = spent + ?`): it is what reservations rely on.
15
15
  */
16
16
  export interface BudgetStore {
17
- /** Add `amount` (a decimal string, possibly negative) to `scope`, and answer the new total. */
18
- add(scope: string, amount: string): Promise<string>;
17
+ /**
18
+ * Add `amount` (a decimal string, possibly negative) to `key`, and answer
19
+ * the new total. `key` is the scope, followed by its period when the
20
+ * budget has one (`user:42@2026-12`). `expiresAt` (epoch milliseconds),
21
+ * given with a period, is when the key may be dropped: a store that keeps
22
+ * it for ever counts right, but grows with every period.
23
+ */
24
+ add(key: string, amount: string, expiresAt?: number): Promise<string>;
25
+ /**
26
+ * The decimals the store keeps, when it rounds what it adds (the Redis
27
+ * store keeps 9). A reservation then rounds its estimate and the final
28
+ * cost once each, half up, and moves the spend by their difference: what
29
+ * a request leaves in the store is its cost rounded once, whatever its
30
+ * estimate was. Rounding the estimate and the correction separately would
31
+ * not add up, and could undercount the spend. Leave it out for a store
32
+ * that keeps every decimal.
33
+ */
34
+ readonly decimals?: number;
19
35
  }
36
+ /** How long spend counts before it starts again at zero, in UTC. */
37
+ export type BudgetPeriod = "day" | "month";
20
38
  export interface BudgetConfig {
21
39
  /** The limit of a scope, as a decimal string in the pricing currency; `undefined` for none. */
22
40
  readonly limit: {
23
41
  readonly [scope: string]: string;
24
42
  } | ((scope: string) => string | undefined | Promise<string | undefined>);
43
+ /**
44
+ * Count spend per day or per month (UTC), from zero at the start of each.
45
+ * Default: no period, spend counts for as long as the store keeps it.
46
+ */
47
+ readonly period?: BudgetPeriod;
25
48
  /** Where spend is kept. Default: in memory, for one process and its lifetime. */
26
49
  readonly store?: BudgetStore;
27
50
  }
28
51
  /** A spend store in memory: one process, lost on restart. For tests and a single instance. */
29
52
  export declare function memoryBudgetStore(): BudgetStore;
53
+ /** The key a scope's spend is kept under at `now`, and when that key may go. */
54
+ export declare function periodKey(scope: string, period: BudgetPeriod | undefined, now?: number): {
55
+ readonly key: string;
56
+ readonly expiresAt?: number;
57
+ };
30
58
  /** What a reservation hands back to settle it. */
31
59
  export interface Reservation {
32
60
  /** Replace the reservation by what the request really cost. */
@@ -37,8 +65,13 @@ export interface Reservation {
37
65
  /** Resolve the limit of `scope`. */
38
66
  export declare function limitOf(config: BudgetConfig, scope: string): Promise<string | undefined>;
39
67
  /**
40
- * Reserve `estimate` on `scope`, or refuse with `E_POLARIS_BUDGET_EXCEEDED`
41
- * when that would go past `limit`. With no limit, nothing is reserved.
68
+ * Reserve `estimate` on `scope` for the current `period`, or refuse with
69
+ * `E_POLARIS_BUDGET_EXCEEDED` when that would go past `limit`. With no limit,
70
+ * nothing is reserved.
71
+ *
72
+ * The reservation keeps the key of the period it was made in: settled after
73
+ * midnight, it still lands on the day it counted against, and a reservation
74
+ * whose process died never settles but goes with its period's key.
42
75
  */
43
- export declare function reserve(store: BudgetStore, scope: string, limit: string | undefined, estimate: string): Promise<Reservation>;
76
+ export declare function reserve(store: BudgetStore, scope: string, limit: string | undefined, estimate: string, period?: BudgetPeriod): Promise<Reservation>;
44
77
  //# sourceMappingURL=budget.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"budget.d.ts","sourceRoot":"","sources":["../src/budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC3B,+FAA+F;IAC/F,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACpD;AAED,MAAM,WAAW,YAAY;IAC5B,+FAA+F;IAC/F,QAAQ,CAAC,KAAK,EACX;QAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,GACpC,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IACzE,iFAAiF;IACjF,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,CAAC;CAC7B;AAED,8FAA8F;AAC9F,wBAAgB,iBAAiB,IAAI,WAAW,CAS/C;AAED,kDAAkD;AAClD,MAAM,WAAW,WAAW;IAC3B,+DAA+D;IAC/D,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,wEAAwE;IACxE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAOD,oCAAoC;AACpC,wBAAsB,OAAO,CAC5B,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,MAAM,GACX,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAM7B;AAED;;;GAGG;AACH,w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AA;CAAE,CAgBvD;AAED,kDAAkD;AAClD,MAAM,WAAW,WAAW;IAC3B,+DAA+D;IAC/D,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,wEAAwE;IACxE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAOD,oCAAoC;AACpC,wBAAsB,OAAO,CAC5B,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,MAAM,GACX,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAM7B;AAED;;;;;;;;GAQG;AACH,wBAAsB,OAAO,CAC5B,KAAK,EAAE,WAAW,EAClB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,YAAY,GACnB,OAAO,CAAC,WAAW,CAAC,CA4BtB"}
package/dist/budget.js CHANGED
@@ -9,19 +9,61 @@
9
9
  * Reserving rather than reading the spend is what keeps two requests sent at
10
10
  * the same moment from passing the limit together.
11
11
  */
12
+ import { decimal } from "@c9up/atom";
12
13
  import { add, exceeds, isZero, subtract } from "./cost.js";
13
- import { E_POLARIS_BUDGET_EXCEEDED } from "./errors.js";
14
+ import { E_POLARIS_BUDGET_EXCEEDED, E_POLARIS_INVALID_CONFIG, } from "./errors.js";
14
15
  /** A spend store in memory: one process, lost on restart. For tests and a single instance. */
15
16
  export function memoryBudgetStore() {
16
17
  const spent = new Map();
18
+ // Expired keys are swept at most once per expiry passed, not on every add.
19
+ let nextSweep = Number.POSITIVE_INFINITY;
17
20
  return {
18
- async add(scope, amount) {
19
- const total = add(spent.get(scope) ?? "0", amount);
20
- spent.set(scope, total);
21
+ async add(key, amount, expiresAt) {
22
+ const now = Date.now();
23
+ if (now >= nextSweep) {
24
+ nextSweep = Number.POSITIVE_INFINITY;
25
+ for (const [name, entry] of spent) {
26
+ if (entry.expiresAt === undefined)
27
+ continue;
28
+ if (entry.expiresAt <= now)
29
+ spent.delete(name);
30
+ else
31
+ nextSweep = Math.min(nextSweep, entry.expiresAt);
32
+ }
33
+ }
34
+ const total = add(spent.get(key)?.total ?? "0", amount);
35
+ spent.set(key, {
36
+ total,
37
+ ...(expiresAt !== undefined ? { expiresAt } : {}),
38
+ });
39
+ if (expiresAt !== undefined)
40
+ nextSweep = Math.min(nextSweep, expiresAt);
21
41
  return total;
22
42
  },
23
43
  };
24
44
  }
45
+ /**
46
+ * How long a period's spend is kept after the period ends: long enough for a
47
+ * request sent just before midnight to settle into the period it reserved on.
48
+ */
49
+ const GRACE_MS = 24 * 60 * 60 * 1000;
50
+ /** The key a scope's spend is kept under at `now`, and when that key may go. */
51
+ export function periodKey(scope, period, now = Date.now()) {
52
+ if (period === undefined)
53
+ return { key: scope };
54
+ const date = new Date(now);
55
+ const year = date.getUTCFullYear();
56
+ const month = date.getUTCMonth();
57
+ const day = date.getUTCDate();
58
+ const pad = (value) => String(value).padStart(2, "0");
59
+ const label = period === "month"
60
+ ? `${year}-${pad(month + 1)}`
61
+ : `${year}-${pad(month + 1)}-${pad(day)}`;
62
+ const end = period === "month"
63
+ ? Date.UTC(year, month + 1, 1)
64
+ : Date.UTC(year, month, day + 1);
65
+ return { key: `${scope}@${label}`, expiresAt: end + GRACE_MS };
66
+ }
25
67
  const NOTHING = {
26
68
  settle: async () => undefined,
27
69
  release: async () => undefined,
@@ -35,15 +77,26 @@ export async function limitOf(config, scope) {
35
77
  : undefined;
36
78
  }
37
79
  /**
38
- * Reserve `estimate` on `scope`, or refuse with `E_POLARIS_BUDGET_EXCEEDED`
39
- * when that would go past `limit`. With no limit, nothing is reserved.
80
+ * Reserve `estimate` on `scope` for the current `period`, or refuse with
81
+ * `E_POLARIS_BUDGET_EXCEEDED` when that would go past `limit`. With no limit,
82
+ * nothing is reserved.
83
+ *
84
+ * The reservation keeps the key of the period it was made in: settled after
85
+ * midnight, it still lands on the day it counted against, and a reservation
86
+ * whose process died never settles but goes with its period's key.
40
87
  */
41
- export async function reserve(store, scope, limit, estimate) {
88
+ export async function reserve(store, scope, limit, estimate, period) {
42
89
  if (limit === undefined)
43
90
  return NOTHING;
44
- const total = await store.add(scope, estimate);
91
+ const round = rounding(store.decimals);
92
+ const reserved = round(estimate);
93
+ const { key, expiresAt } = periodKey(scope, period);
94
+ const move = (amount) => expiresAt === undefined
95
+ ? store.add(key, amount)
96
+ : store.add(key, amount, expiresAt);
97
+ const total = await move(reserved);
45
98
  if (exceeds(total, limit)) {
46
- await store.add(scope, `-${estimate}`);
99
+ await move(negate(reserved));
47
100
  throw new E_POLARIS_BUDGET_EXCEEDED([scope, limit]);
48
101
  }
49
102
  let open = true;
@@ -52,16 +105,30 @@ export async function reserve(store, scope, limit, estimate) {
52
105
  if (!open)
53
106
  return;
54
107
  open = false;
55
- const difference = subtract(cost, estimate);
108
+ const difference = subtract(round(cost), reserved);
56
109
  if (!isZero(difference))
57
- await store.add(scope, difference);
110
+ await move(difference);
58
111
  },
59
112
  async release() {
60
113
  if (!open)
61
114
  return;
62
115
  open = false;
63
- await store.add(scope, `-${estimate}`);
116
+ await move(negate(reserved));
64
117
  },
65
118
  };
66
119
  }
120
+ /** Round an amount to the decimals a store keeps, or leave it as it is. */
121
+ function rounding(decimals) {
122
+ if (decimals === undefined)
123
+ return (amount) => amount;
124
+ if (!Number.isSafeInteger(decimals) || decimals < 0) {
125
+ throw new E_POLARIS_INVALID_CONFIG([
126
+ "budget.store.decimals must be a non-negative integer",
127
+ ]);
128
+ }
129
+ return (amount) => decimal(amount).toFixed(decimals, "half-up");
130
+ }
131
+ function negate(amount) {
132
+ return subtract("0", amount);
133
+ }
67
134
  //# sourceMappingURL=budget.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"budget.js","sourceRoot":"","sources":["../src/budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AA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AC,EAAE,QAAQ,CAAC,CAAC;YACnD,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;gBAAE,MAAM,IAAI,CAAC,UAAU,CAAC,CAAC;QACjD,CAAC;QACD,KAAK,CAAC,OAAO;YACZ,IAAI,CAAC,IAAI;gBAAE,OAAO;YAClB,IAAI,GAAG,KAAK,CAAC;YACb,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC9B,CAAC;KACD,CAAC;AACH,CAAC;AAED,2EAA2E;AAC3E,SAAS,QAAQ,CAAC,QAA4B;IAC7C,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC;IACtD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;QACrD,MAAM,IAAI,wBAAwB,CAAC;YAClC,sDAAsD;SACtD,CAAC,CAAC;IACJ,CAAC;IACD,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;AACjE,CAAC;AAED,SAAS,MAAM,CAAC,MAAc;IAC7B,OAAO,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The application's tools and skills, read from `polaris.json`. A run is
3
+ * given them by name; only the modules of the tools asked for are imported.
4
+ *
5
+ * const tools = await catalog.tools(['canton_rate'])
6
+ * const skills = useSkills(await catalog.skills(['payroll']))
7
+ */
8
+ import { type ReadSkillOptions } from "../skills/folder.js";
9
+ import type { PolarisSkill } from "../skills/skill.js";
10
+ import type { PolarisTool } from "../tools.js";
11
+ import type { CatalogManifest, CatalogSkill, CatalogTool } from "./discover.js";
12
+ export declare class Catalog {
13
+ #private;
14
+ /** Paths in `manifest` are relative to `base`. */
15
+ constructor(manifest: CatalogManifest, base: string, path?: string);
16
+ /**
17
+ * Read a manifest. A file that does not exist gives an empty catalog,
18
+ * which says so when a name is asked of it; one that is not a manifest
19
+ * throws `E_POLARIS_INVALID_CATALOG`.
20
+ */
21
+ static read(path: string): Promise<Catalog>;
22
+ /** The tools found, by name and description, without importing them. */
23
+ get toolList(): readonly CatalogTool[];
24
+ /** The skills found, by name and description, without reading them. */
25
+ get skillList(): readonly CatalogSkill[];
26
+ /**
27
+ * The tools named, in that order; all of them when no name is given. Each
28
+ * module is imported once, on the first call that needs it.
29
+ */
30
+ tools(names?: readonly string[]): Promise<PolarisTool[]>;
31
+ /**
32
+ * The skills named, in that order; all of them when no name is given.
33
+ * Each is read from its folder on every call (see `readSkill`).
34
+ */
35
+ skills(names?: readonly string[], options?: ReadSkillOptions): Promise<PolarisSkill[]>;
36
+ }
37
+ //# sourceMappingURL=catalog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/catalog/catalog.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,OAAO,EAAE,KAAK,gBAAgB,EAAa,MAAM,qBAAqB,CAAC;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AA+BhF,qBAAa,OAAO;;IAOnB,kDAAkD;gBACtC,QAAQ,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;IAMlE;;;;OAIG;WACU,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAyBjD,wEAAwE;IACxE,IAAI,QAAQ,IAAI,SAAS,WAAW,EAAE,CAErC;IAED,uEAAuE;IACvE,IAAI,SAAS,IAAI,SAAS,YAAY,EAAE,CAEvC;IAYD;;;OAGG;IACG,KAAK,CAAC,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAwC9D;;;OAGG;IACG,MAAM,CACX,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,EACzB,OAAO,CAAC,EAAE,gBAAgB,GACxB,OAAO,CAAC,YAAY,EAAE,CAAC;CAe1B"}
@@ -0,0 +1,145 @@
1
+ /**
2
+ * The application's tools and skills, read from `polaris.json`. A run is
3
+ * given them by name; only the modules of the tools asked for are imported.
4
+ *
5
+ * const tools = await catalog.tools(['canton_rate'])
6
+ * const skills = useSkills(await catalog.skills(['payroll']))
7
+ */
8
+ import { existsSync } from "node:fs";
9
+ import { readFile } from "node:fs/promises";
10
+ import { dirname, resolve } from "node:path";
11
+ import { pathToFileURL } from "node:url";
12
+ import { E_POLARIS_INVALID_CATALOG } from "../errors.js";
13
+ import { isObject } from "../mcp/rpc.js";
14
+ import { readSkill } from "../skills/folder.js";
15
+ import { importToolModule } from "../tool-modules.js";
16
+ const REINDEX = "run `ream polaris:index`";
17
+ const isEntry = (value, path) => isObject(value) &&
18
+ typeof value.name === "string" &&
19
+ typeof value.description === "string" &&
20
+ typeof value[path] === "string";
21
+ function isManifest(value) {
22
+ return (isObject(value) &&
23
+ value.version === 1 &&
24
+ Array.isArray(value.tools) &&
25
+ value.tools.every((entry) => isEntry(entry, "module")) &&
26
+ Array.isArray(value.skills) &&
27
+ value.skills.every((entry) => isEntry(entry, "folder")));
28
+ }
29
+ /**
30
+ * A module's path as written, or its build output: in a build, `polaris.json`
31
+ * is copied beside the compiled code, where `x.ts` became `x.js`.
32
+ */
33
+ function compiled(path) {
34
+ if (existsSync(path))
35
+ return path;
36
+ const built = path.replace(/\.ts$/, ".js").replace(/\.mts$/, ".mjs");
37
+ return existsSync(built) ? built : path;
38
+ }
39
+ export class Catalog {
40
+ #manifest;
41
+ #base;
42
+ /** The manifest's path, for messages; `undefined` when there was none. */
43
+ #path;
44
+ #modules = new Map();
45
+ /** Paths in `manifest` are relative to `base`. */
46
+ constructor(manifest, base, path) {
47
+ this.#manifest = manifest;
48
+ this.#base = base;
49
+ this.#path = path;
50
+ }
51
+ /**
52
+ * Read a manifest. A file that does not exist gives an empty catalog,
53
+ * which says so when a name is asked of it; one that is not a manifest
54
+ * throws `E_POLARIS_INVALID_CATALOG`.
55
+ */
56
+ static async read(path) {
57
+ const absolute = resolve(path);
58
+ let text;
59
+ try {
60
+ text = await readFile(absolute, "utf8");
61
+ }
62
+ catch {
63
+ return new Catalog({ version: 1, tools: [], skills: [] }, dirname(absolute));
64
+ }
65
+ let manifest;
66
+ try {
67
+ manifest = JSON.parse(text);
68
+ }
69
+ catch {
70
+ manifest = undefined;
71
+ }
72
+ if (!isManifest(manifest)) {
73
+ throw new E_POLARIS_INVALID_CATALOG([
74
+ `'${path}' is not a polaris manifest; ${REINDEX}`,
75
+ ]);
76
+ }
77
+ return new Catalog(manifest, dirname(absolute), path);
78
+ }
79
+ /** The tools found, by name and description, without importing them. */
80
+ get toolList() {
81
+ return this.#manifest.tools;
82
+ }
83
+ /** The skills found, by name and description, without reading them. */
84
+ get skillList() {
85
+ return this.#manifest.skills;
86
+ }
87
+ #missing(kind, name) {
88
+ const where = this.#path === undefined
89
+ ? `no manifest was found; ${REINDEX}`
90
+ : `'${this.#path}' lists none; ${REINDEX} after adding one`;
91
+ throw new E_POLARIS_INVALID_CATALOG([
92
+ `no ${kind} is named '${name.slice(0, 64)}': ${where}`,
93
+ ]);
94
+ }
95
+ /**
96
+ * The tools named, in that order; all of them when no name is given. Each
97
+ * module is imported once, on the first call that needs it.
98
+ */
99
+ async tools(names) {
100
+ const entries = names === undefined
101
+ ? this.#manifest.tools
102
+ : names.map((name) => this.#manifest.tools.find((tool) => tool.name === name) ??
103
+ this.#missing("tool", name));
104
+ const tools = [];
105
+ for (const entry of entries) {
106
+ const exported = await this.#module(entry.module);
107
+ const tool = exported.find((item) => item.name === entry.name);
108
+ if (tool === undefined) {
109
+ throw new E_POLARIS_INVALID_CATALOG([
110
+ `'${entry.module}' no longer exports the tool '${entry.name}'; ${REINDEX}`,
111
+ ]);
112
+ }
113
+ tools.push(tool);
114
+ }
115
+ return tools;
116
+ }
117
+ #module(module) {
118
+ let loading = this.#modules.get(module);
119
+ if (loading === undefined) {
120
+ const url = pathToFileURL(compiled(resolve(this.#base, module))).href;
121
+ loading = importToolModule(url).then((found) => {
122
+ if (typeof found === "string") {
123
+ throw new E_POLARIS_INVALID_CATALOG([`'${module}' ${found}`]);
124
+ }
125
+ return found;
126
+ });
127
+ // A failed import is tried again on the next call, not remembered.
128
+ loading.catch(() => this.#modules.delete(module));
129
+ this.#modules.set(module, loading);
130
+ }
131
+ return loading;
132
+ }
133
+ /**
134
+ * The skills named, in that order; all of them when no name is given.
135
+ * Each is read from its folder on every call (see `readSkill`).
136
+ */
137
+ async skills(names, options) {
138
+ const entries = names === undefined
139
+ ? this.#manifest.skills
140
+ : names.map((name) => this.#manifest.skills.find((skill) => skill.name === name) ??
141
+ this.#missing("skill", name));
142
+ return Promise.all(entries.map((entry) => readSkill(resolve(this.#base, entry.folder), options)));
143
+ }
144
+ }
145
+ //# sourceMappingURL=catalog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.js","sourceRoot":"","sources":["../../src/catalog/catalog.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAyB,SAAS,EAAE,MAAM,qBAAqB,CAAC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AAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;gBAC1D,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAC7B,CAAC;QACL,OAAO,OAAO,CAAC,GAAG,CACjB,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACrB,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CACrD,CACD,CAAC;IACH,CAAC;CACD"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Finding the application's tools and skills by convention: the folders and
3
+ * files its glob patterns match. `ream polaris:index` runs this at build and
4
+ * writes the result, `polaris.json`, so the running application reads a list
5
+ * instead of importing every tool module to learn its name.
6
+ */
7
+ /** `catalog` in `config/polaris.ts`. Paths are relative to the application. */
8
+ export interface CatalogConfig {
9
+ /**
10
+ * Glob patterns of tool folders or tool files. A folder holds tool
11
+ * modules; a file is one. Default `['app/tools']`; a modular application
12
+ * names its modules: `['app/modules/*\/tools', 'app/modules/*\/http/*_tool.ts']`.
13
+ */
14
+ readonly tools?: readonly string[];
15
+ /**
16
+ * Glob patterns of skill folders: a folder with a `SKILL.md` is a skill,
17
+ * another holds skills, one per sub-folder. Default `['resources/skills']`.
18
+ */
19
+ readonly skills?: readonly string[];
20
+ /** Where `ream polaris:index` writes the list. Default `polaris.json`. */
21
+ readonly manifest?: string;
22
+ }
23
+ export declare const DEFAULT_TOOL_PATTERNS: readonly string[];
24
+ export declare const DEFAULT_SKILL_PATTERNS: readonly string[];
25
+ export declare const DEFAULT_MANIFEST = "polaris.json";
26
+ /** What `polaris.json` holds. Paths are relative to it, with `/`. */
27
+ export interface CatalogManifest {
28
+ readonly version: 1;
29
+ readonly tools: readonly CatalogTool[];
30
+ readonly skills: readonly CatalogSkill[];
31
+ }
32
+ export interface CatalogTool {
33
+ readonly name: string;
34
+ readonly description: string;
35
+ /** The module that exports it. */
36
+ readonly module: string;
37
+ }
38
+ export interface CatalogSkill {
39
+ readonly name: string;
40
+ readonly description: string;
41
+ /** The folder that holds its `SKILL.md`. */
42
+ readonly folder: string;
43
+ }
44
+ export interface IndexCatalogOptions extends CatalogConfig {
45
+ /** The application's root. Default the current directory. */
46
+ readonly root?: string;
47
+ }
48
+ /**
49
+ * Find the tools and skills the patterns lead to, importing each tool module
50
+ * and reading each `SKILL.md` once. Throws `E_POLARIS_INVALID_CATALOG` when a
51
+ * module exports no tools, or when two tools or two skills share a name,
52
+ * naming both files; `E_POLARIS_INVALID_SKILL` for a skill folder that
53
+ * cannot be read. Paths in the result are relative to `base`.
54
+ */
55
+ export declare function indexCatalog(options?: IndexCatalogOptions, base?: string): Promise<CatalogManifest>;
56
+ /**
57
+ * {@link indexCatalog}, written to the manifest (`polaris.json` at the root
58
+ * unless `manifest` says otherwise). Returns the manifest and its path.
59
+ */
60
+ export declare function writeCatalog(options?: IndexCatalogOptions): Promise<{
61
+ manifest: CatalogManifest;
62
+ path: string;
63
+ }>;
64
+ //# sourceMappingURL=discover.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discover.d.ts","sourceRoot":"","sources":["../../src/catalog/discover.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAkBH,+EAA+E;AAC/E,MAAM,WAAW,aAAa;IAC7B;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,0EAA0E;IAC1E,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,eAAO,MAAM,qBAAqB,EAAE,SAAS,MAAM,EAAkB,CAAC;AACtE,eAAO,MAAM,sBAAsB,EAAE,SAAS,MAAM,EAAyB,CAAC;AAC9E,eAAO,MAAM,gBAAgB,iBAAiB,CAAC;AAE/C,qEAAqE;AACrE,MAAM,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