@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
package/src/types.ts CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
 
12
12
  import type { BudgetConfig } from "./budget.js";
13
+ import type { CatalogConfig } from "./catalog/discover.js";
13
14
  import type { Cost, Pricing } from "./cost.js";
14
15
  import type { ExchangeLog } from "./log.js";
15
16
 
@@ -138,6 +139,22 @@ export interface ChatRequest {
138
139
  readonly output?: OutputFormat;
139
140
  readonly stopSequences?: readonly string[];
140
141
  readonly effort?: Effort;
142
+ /**
143
+ * Ask the provider to cache the request's prefix (tools, system prompt,
144
+ * messages), so the next request that starts the same way reads it at the
145
+ * cache price instead of the full input price. Claude caches only when
146
+ * asked, for 5 minutes from the last read; the OpenAI-compatible services
147
+ * cache on their own, or not at all, and ignore the option. Check
148
+ * `usage.cacheReadTokens` to see it work.
149
+ */
150
+ readonly cache?: boolean;
151
+ /**
152
+ * Tools the provider runs on its own servers: code execution, its skills,
153
+ * and the MCP servers it calls itself. Only the drivers that declare the
154
+ * `serverTools` feature take it (Anthropic); the others refuse the request
155
+ * with `E_POLARIS_UNSUPPORTED`.
156
+ */
157
+ readonly server?: ServerTools;
141
158
  /**
142
159
  * Provider-specific fields, passed through by the driver that knows them
143
160
  * and ignored by the others.
@@ -145,6 +162,51 @@ export interface ChatRequest {
145
162
  readonly providerOptions?: { readonly [provider: string]: unknown };
146
163
  }
147
164
 
165
+ /**
166
+ * What a provider runs on its own side for a request. Its tool calls never
167
+ * reach the application: they come back as `provider` parts, with their
168
+ * results, and the run goes on without polaris running anything.
169
+ */
170
+ export interface ServerTools {
171
+ /**
172
+ * Let the model run code in the provider's container. Implied by `skills`,
173
+ * which need it.
174
+ */
175
+ readonly codeExecution?: boolean;
176
+ /** Skills the provider loads into the container, at most 20. */
177
+ readonly skills?: readonly ServerSkill[];
178
+ /** MCP servers the provider connects to and calls itself. */
179
+ readonly mcpServers?: readonly ServerMcpServer[];
180
+ /**
181
+ * The container to work in again: a previous response's `container.id`,
182
+ * so the files it holds are still there. `run()` carries it from one step
183
+ * to the next by itself.
184
+ */
185
+ readonly container?: string;
186
+ }
187
+
188
+ /** A skill the provider runs, as it knows it. */
189
+ export interface ServerSkill {
190
+ /** `anthropic` for the provider's own skills (`xlsx`, `pptx`…), `custom` for one uploaded to the account. */
191
+ readonly source: "anthropic" | "custom";
192
+ /** `xlsx`, or a custom skill's `skill_…` id. */
193
+ readonly id: string;
194
+ /** A version the provider names (`20251013`, `skver_…`). Default `latest`. */
195
+ readonly version?: string;
196
+ }
197
+
198
+ /** An MCP server the provider calls over the internet. */
199
+ export interface ServerMcpServer {
200
+ /** Its name in the request: unique, and how the model's calls name it. */
201
+ readonly name: string;
202
+ /** Its `https://` endpoint (Streamable HTTP or SSE). */
203
+ readonly url: string;
204
+ /** Sent to the server as its OAuth bearer token. Redacted from logs and errors. */
205
+ readonly authorizationToken?: string;
206
+ /** Only these of its tools. Default: all of them. */
207
+ readonly tools?: readonly string[];
208
+ }
209
+
148
210
  /**
149
211
  * Why the model stopped.
150
212
  *
@@ -168,8 +230,9 @@ export type StopReason =
168
230
  | "other";
169
231
 
170
232
  /**
171
- * Tokens a request used. Counts a provider does not report are 0. The three
172
- * input counts do not overlap, so each is billed at its own price.
233
+ * Tokens a request used. Counts a provider does not report are 0 (see
234
+ * `usageUnknown`). The three input counts do not overlap, so each is billed
235
+ * at its own price.
173
236
  */
174
237
  export interface Usage {
175
238
  /** Input tokens billed at the full input price: neither read from nor written to a cache. */
@@ -179,6 +242,12 @@ export interface Usage {
179
242
  readonly cacheReadTokens: number;
180
243
  /** Input tokens written to the provider's prompt cache. */
181
244
  readonly cacheWriteTokens: number;
245
+ /**
246
+ * Requests to tools the provider ran itself, by tool, as it counts them:
247
+ * `{ codeExecutionRequests: 2 }`. Absent when it reports none. Tokens those
248
+ * tools read or wrote are in the counts above.
249
+ */
250
+ readonly serverToolUse?: { readonly [counter: string]: number };
182
251
  }
183
252
 
184
253
  export interface ChatResponse {
@@ -197,8 +266,27 @@ export interface ChatResponse {
197
266
  readonly toolCalls: readonly ToolCallPart[];
198
267
  readonly stopReason: StopReason;
199
268
  readonly usage: Usage;
269
+ /**
270
+ * The provider reported no token counts, left out the input or output
271
+ * count, or reported counts that cannot be right (negative, fractional):
272
+ * `usage` holds 0 where it had nothing usable, the
273
+ * response has no `cost`, and a budget keeps the call's estimate.
274
+ */
275
+ readonly usageUnknown?: boolean;
200
276
  /** What the request cost, when the config has a price for the model. */
201
277
  readonly cost?: Cost;
278
+ /**
279
+ * The provider's container the reply ran code in, while it is kept: send
280
+ * its id back as `server.container` to work on its files again.
281
+ */
282
+ readonly container?: ServerContainer;
283
+ }
284
+
285
+ /** A provider's code execution container. */
286
+ export interface ServerContainer {
287
+ readonly id: string;
288
+ /** When the provider drops it, as it reports it (ISO 8601). */
289
+ readonly expiresAt?: string;
202
290
  }
203
291
 
204
292
  /**
@@ -227,6 +315,11 @@ export interface EmbedResponse {
227
315
  /** One vector per input, in input order. */
228
316
  readonly vectors: readonly Float32Array[];
229
317
  readonly usage: { readonly inputTokens: number };
318
+ /**
319
+ * The provider reported no token count, or one that cannot be right: the
320
+ * response has no `cost`, and a budget keeps the call's estimate.
321
+ */
322
+ readonly usageUnknown?: boolean;
230
323
  /** What the request cost, when the config has a price for the model. */
231
324
  readonly cost?: Cost;
232
325
  }
@@ -267,6 +360,8 @@ export interface DriverFeatures {
267
360
  readonly tools: boolean;
268
361
  readonly structuredOutput: boolean;
269
362
  readonly images: boolean;
363
+ /** Takes a request's `server` tools. Default false. */
364
+ readonly serverTools?: boolean;
270
365
  }
271
366
 
272
367
  /**
@@ -283,6 +378,12 @@ export interface PolarisDriver {
283
378
  * before it is sent (a budget needs it).
284
379
  */
285
380
  readonly defaults?: DriverDefaults;
381
+ /**
382
+ * The credentials the driver sends on its own: its configured key, one it
383
+ * reads from the environment, the values of headers it was given. polaris
384
+ * keeps them out of the errors it raises, as it does a per-call key.
385
+ */
386
+ readonly secrets?: readonly string[];
286
387
  chat(request: ChatRequest, call: DriverCall): Promise<ChatResponse>;
287
388
  stream?(request: ChatRequest, call: DriverCall): AsyncIterable<StreamEvent>;
288
389
  embed?(request: EmbedRequest, call: DriverCall): Promise<EmbedResponse>;
@@ -313,6 +414,11 @@ export interface PolarisConfig<
313
414
  /** The provider `use()` returns when given no name. */
314
415
  readonly default?: keyof KnownProviders & string;
315
416
  readonly providers: KnownProviders;
417
+ /**
418
+ * Where `PolarisProvider` and `ream polaris:index` find the application's
419
+ * tools and skills (see `@c9up/polaris/catalog`). `false`: none.
420
+ */
421
+ readonly catalog?: CatalogConfig | false;
316
422
  }
317
423
 
318
424
  /**