@c9up/polaris 0.0.0-stage → 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 (253) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1146 -2
  3. package/dist/Polaris.d.ts +80 -0
  4. package/dist/Polaris.d.ts.map +1 -0
  5. package/dist/Polaris.js +208 -0
  6. package/dist/Polaris.js.map +1 -0
  7. package/dist/PolarisClient.d.ts +184 -0
  8. package/dist/PolarisClient.d.ts.map +1 -0
  9. package/dist/PolarisClient.js +576 -0
  10. package/dist/PolarisClient.js.map +1 -0
  11. package/dist/PolarisProvider.d.ts +40 -0
  12. package/dist/PolarisProvider.d.ts.map +1 -0
  13. package/dist/PolarisProvider.js +79 -0
  14. package/dist/PolarisProvider.js.map +1 -0
  15. package/dist/augmentations.d.ts +18 -0
  16. package/dist/augmentations.d.ts.map +1 -0
  17. package/dist/augmentations.js +9 -0
  18. package/dist/augmentations.js.map +1 -0
  19. package/dist/budget.d.ts +77 -0
  20. package/dist/budget.d.ts.map +1 -0
  21. package/dist/budget.js +134 -0
  22. package/dist/budget.js.map +1 -0
  23. package/dist/catalog/catalog.d.ts +37 -0
  24. package/dist/catalog/catalog.d.ts.map +1 -0
  25. package/dist/catalog/catalog.js +145 -0
  26. package/dist/catalog/catalog.js.map +1 -0
  27. package/dist/catalog/discover.d.ts +64 -0
  28. package/dist/catalog/discover.d.ts.map +1 -0
  29. package/dist/catalog/discover.js +148 -0
  30. package/dist/catalog/discover.js.map +1 -0
  31. package/dist/catalog/index.d.ts +8 -0
  32. package/dist/catalog/index.d.ts.map +1 -0
  33. package/dist/catalog/index.js +8 -0
  34. package/dist/catalog/index.js.map +1 -0
  35. package/dist/commands/index.d.ts +82 -0
  36. package/dist/commands/index.d.ts.map +1 -0
  37. package/dist/commands/index.js +278 -0
  38. package/dist/commands/index.js.map +1 -0
  39. package/dist/cost.d.ts +50 -0
  40. package/dist/cost.d.ts.map +1 -0
  41. package/dist/cost.js +131 -0
  42. package/dist/cost.js.map +1 -0
  43. package/dist/drivers/anthropic.d.ts +44 -0
  44. package/dist/drivers/anthropic.d.ts.map +1 -0
  45. package/dist/drivers/anthropic.js +539 -0
  46. package/dist/drivers/anthropic.js.map +1 -0
  47. package/dist/drivers/openai-compatible.d.ts +54 -0
  48. package/dist/drivers/openai-compatible.d.ts.map +1 -0
  49. package/dist/drivers/openai-compatible.js +699 -0
  50. package/dist/drivers/openai-compatible.js.map +1 -0
  51. package/dist/drivers/status.d.ts +8 -0
  52. package/dist/drivers/status.d.ts.map +1 -0
  53. package/dist/drivers/status.js +21 -0
  54. package/dist/drivers/status.js.map +1 -0
  55. package/dist/errors.d.ts +79 -0
  56. package/dist/errors.d.ts.map +1 -0
  57. package/dist/errors.js +86 -0
  58. package/dist/errors.js.map +1 -0
  59. package/dist/extract.d.ts +38 -0
  60. package/dist/extract.d.ts.map +1 -0
  61. package/dist/extract.js +144 -0
  62. package/dist/extract.js.map +1 -0
  63. package/dist/index.d.ts +21 -0
  64. package/dist/index.d.ts.map +1 -0
  65. package/dist/index.js +15 -0
  66. package/dist/index.js.map +1 -0
  67. package/dist/json-schema.d.ts +33 -0
  68. package/dist/json-schema.d.ts.map +1 -0
  69. package/dist/json-schema.js +508 -0
  70. package/dist/json-schema.js.map +1 -0
  71. package/dist/log.d.ts +47 -0
  72. package/dist/log.d.ts.map +1 -0
  73. package/dist/log.js +35 -0
  74. package/dist/log.js.map +1 -0
  75. package/dist/mcp/client.d.ts +81 -0
  76. package/dist/mcp/client.d.ts.map +1 -0
  77. package/dist/mcp/client.js +401 -0
  78. package/dist/mcp/client.js.map +1 -0
  79. package/dist/mcp/endpoint.d.ts +54 -0
  80. package/dist/mcp/endpoint.d.ts.map +1 -0
  81. package/dist/mcp/endpoint.js +158 -0
  82. package/dist/mcp/endpoint.js.map +1 -0
  83. package/dist/mcp/host.d.ts +42 -0
  84. package/dist/mcp/host.d.ts.map +1 -0
  85. package/dist/mcp/host.js +111 -0
  86. package/dist/mcp/host.js.map +1 -0
  87. package/dist/mcp/http.d.ts +32 -0
  88. package/dist/mcp/http.d.ts.map +1 -0
  89. package/dist/mcp/http.js +190 -0
  90. package/dist/mcp/http.js.map +1 -0
  91. package/dist/mcp/index.d.ts +23 -0
  92. package/dist/mcp/index.d.ts.map +1 -0
  93. package/dist/mcp/index.js +21 -0
  94. package/dist/mcp/index.js.map +1 -0
  95. package/dist/mcp/manifest.d.ts +36 -0
  96. package/dist/mcp/manifest.d.ts.map +1 -0
  97. package/dist/mcp/manifest.js +40 -0
  98. package/dist/mcp/manifest.js.map +1 -0
  99. package/dist/mcp/rpc.d.ts +39 -0
  100. package/dist/mcp/rpc.d.ts.map +1 -0
  101. package/dist/mcp/rpc.js +149 -0
  102. package/dist/mcp/rpc.js.map +1 -0
  103. package/dist/mcp/stdio.d.ts +24 -0
  104. package/dist/mcp/stdio.d.ts.map +1 -0
  105. package/dist/mcp/stdio.js +157 -0
  106. package/dist/mcp/stdio.js.map +1 -0
  107. package/dist/mcp/worker-main.d.ts +6 -0
  108. package/dist/mcp/worker-main.d.ts.map +1 -0
  109. package/dist/mcp/worker-main.js +17 -0
  110. package/dist/mcp/worker-main.js.map +1 -0
  111. package/dist/mcp/worker.d.ts +54 -0
  112. package/dist/mcp/worker.d.ts.map +1 -0
  113. package/dist/mcp/worker.js +166 -0
  114. package/dist/mcp/worker.js.map +1 -0
  115. package/dist/meter.d.ts +63 -0
  116. package/dist/meter.d.ts.map +1 -0
  117. package/dist/meter.js +94 -0
  118. package/dist/meter.js.map +1 -0
  119. package/dist/providers.d.ts +23 -0
  120. package/dist/providers.d.ts.map +1 -0
  121. package/dist/providers.js +21 -0
  122. package/dist/providers.js.map +1 -0
  123. package/dist/redact.d.ts +12 -0
  124. package/dist/redact.d.ts.map +1 -0
  125. package/dist/redact.js +77 -0
  126. package/dist/redact.js.map +1 -0
  127. package/dist/redis-budget.d.ts +45 -0
  128. package/dist/redis-budget.d.ts.map +1 -0
  129. package/dist/redis-budget.js +56 -0
  130. package/dist/redis-budget.js.map +1 -0
  131. package/dist/response.d.ts +39 -0
  132. package/dist/response.d.ts.map +1 -0
  133. package/dist/response.js +109 -0
  134. package/dist/response.js.map +1 -0
  135. package/dist/schema.d.ts +45 -0
  136. package/dist/schema.d.ts.map +1 -0
  137. package/dist/schema.js +43 -0
  138. package/dist/schema.js.map +1 -0
  139. package/dist/server-sent.d.ts +9 -0
  140. package/dist/server-sent.d.ts.map +1 -0
  141. package/dist/server-sent.js +74 -0
  142. package/dist/server-sent.js.map +1 -0
  143. package/dist/server-tools.d.ts +12 -0
  144. package/dist/server-tools.d.ts.map +1 -0
  145. package/dist/server-tools.js +71 -0
  146. package/dist/server-tools.js.map +1 -0
  147. package/dist/services/catalog.d.ts +19 -0
  148. package/dist/services/catalog.d.ts.map +1 -0
  149. package/dist/services/catalog.js +37 -0
  150. package/dist/services/catalog.js.map +1 -0
  151. package/dist/services/main.d.ts +23 -0
  152. package/dist/services/main.d.ts.map +1 -0
  153. package/dist/services/main.js +43 -0
  154. package/dist/services/main.js.map +1 -0
  155. package/dist/skills/folder.d.ts +32 -0
  156. package/dist/skills/folder.d.ts.map +1 -0
  157. package/dist/skills/folder.js +170 -0
  158. package/dist/skills/folder.js.map +1 -0
  159. package/dist/skills/frontmatter.d.ts +15 -0
  160. package/dist/skills/frontmatter.d.ts.map +1 -0
  161. package/dist/skills/frontmatter.js +92 -0
  162. package/dist/skills/frontmatter.js.map +1 -0
  163. package/dist/skills/index.d.ts +18 -0
  164. package/dist/skills/index.d.ts.map +1 -0
  165. package/dist/skills/index.js +18 -0
  166. package/dist/skills/index.js.map +1 -0
  167. package/dist/skills/kit.d.ts +39 -0
  168. package/dist/skills/kit.d.ts.map +1 -0
  169. package/dist/skills/kit.js +124 -0
  170. package/dist/skills/kit.js.map +1 -0
  171. package/dist/skills/skill.d.ts +67 -0
  172. package/dist/skills/skill.d.ts.map +1 -0
  173. package/dist/skills/skill.js +114 -0
  174. package/dist/skills/skill.js.map +1 -0
  175. package/dist/sse.d.ts +95 -0
  176. package/dist/sse.d.ts.map +1 -0
  177. package/dist/sse.js +117 -0
  178. package/dist/sse.js.map +1 -0
  179. package/dist/testing/FakeProvider.d.ts +85 -0
  180. package/dist/testing/FakeProvider.d.ts.map +1 -0
  181. package/dist/testing/FakeProvider.js +214 -0
  182. package/dist/testing/FakeProvider.js.map +1 -0
  183. package/dist/testing/index.d.ts +7 -0
  184. package/dist/testing/index.d.ts.map +1 -0
  185. package/dist/testing/index.js +7 -0
  186. package/dist/testing/index.js.map +1 -0
  187. package/dist/tool-modules.d.ts +17 -0
  188. package/dist/tool-modules.d.ts.map +1 -0
  189. package/dist/tool-modules.js +43 -0
  190. package/dist/tool-modules.js.map +1 -0
  191. package/dist/tools.d.ts +132 -0
  192. package/dist/tools.d.ts.map +1 -0
  193. package/dist/tools.js +250 -0
  194. package/dist/tools.js.map +1 -0
  195. package/dist/transport.d.ts +23 -0
  196. package/dist/transport.d.ts.map +1 -0
  197. package/dist/transport.js +83 -0
  198. package/dist/transport.js.map +1 -0
  199. package/dist/types.d.ts +397 -0
  200. package/dist/types.d.ts.map +1 -0
  201. package/dist/types.js +12 -0
  202. package/dist/types.js.map +1 -0
  203. package/package.json +97 -3
  204. package/src/Polaris.ts +304 -0
  205. package/src/PolarisClient.ts +828 -0
  206. package/src/PolarisProvider.ts +111 -0
  207. package/src/augmentations.ts +20 -0
  208. package/src/budget.ts +201 -0
  209. package/src/catalog/catalog.ts +180 -0
  210. package/src/catalog/discover.ts +234 -0
  211. package/src/catalog/index.ts +19 -0
  212. package/src/commands/index.ts +412 -0
  213. package/src/cost.ts +176 -0
  214. package/src/drivers/anthropic.ts +703 -0
  215. package/src/drivers/openai-compatible.ts +907 -0
  216. package/src/drivers/status.ts +30 -0
  217. package/src/errors.ts +202 -0
  218. package/src/extract.ts +214 -0
  219. package/src/index.ts +85 -0
  220. package/src/json-schema.ts +597 -0
  221. package/src/log.ts +86 -0
  222. package/src/mcp/client.ts +532 -0
  223. package/src/mcp/endpoint.ts +229 -0
  224. package/src/mcp/host.ts +140 -0
  225. package/src/mcp/http.ts +276 -0
  226. package/src/mcp/index.ts +51 -0
  227. package/src/mcp/manifest.ts +73 -0
  228. package/src/mcp/rpc.ts +197 -0
  229. package/src/mcp/stdio.ts +190 -0
  230. package/src/mcp/worker-main.ts +18 -0
  231. package/src/mcp/worker.ts +212 -0
  232. package/src/meter.ts +199 -0
  233. package/src/providers.ts +25 -0
  234. package/src/redact.ts +81 -0
  235. package/src/redis-budget.ts +85 -0
  236. package/src/response.ts +143 -0
  237. package/src/schema.ts +88 -0
  238. package/src/server-sent.ts +70 -0
  239. package/src/server-tools.ts +77 -0
  240. package/src/services/catalog.ts +43 -0
  241. package/src/services/main.ts +49 -0
  242. package/src/skills/folder.ts +231 -0
  243. package/src/skills/frontmatter.ts +92 -0
  244. package/src/skills/index.ts +38 -0
  245. package/src/skills/kit.ts +163 -0
  246. package/src/skills/skill.ts +171 -0
  247. package/src/sse.ts +194 -0
  248. package/src/testing/FakeProvider.ts +322 -0
  249. package/src/testing/index.ts +12 -0
  250. package/src/tool-modules.ts +49 -0
  251. package/src/tools.ts +399 -0
  252. package/src/transport.ts +107 -0
  253. package/src/types.ts +434 -0
package/src/types.ts ADDED
@@ -0,0 +1,434 @@
1
+ /**
2
+ * The shapes polaris speaks: one message model, one request, one response and
3
+ * one stream for every provider, and the contract a provider driver fills.
4
+ *
5
+ * Also the module an application augments to type `polaris.use(name)`:
6
+ *
7
+ * declare module '@c9up/polaris/types' {
8
+ * interface ProvidersList extends InferProviders<typeof polarisConfig> {}
9
+ * }
10
+ */
11
+
12
+ import type { BudgetConfig } from "./budget.js";
13
+ import type { CatalogConfig } from "./catalog/discover.js";
14
+ import type { Cost, Pricing } from "./cost.js";
15
+ import type { ExchangeLog } from "./log.js";
16
+
17
+ /** A JSON Schema object, as providers take it for tools and structured output. */
18
+ export type JsonSchema = { readonly [key: string]: unknown };
19
+
20
+ /**
21
+ * The block a part was read from, as its provider sent it.
22
+ *
23
+ * A driver sends a part whose origin is its own provider back as this block,
24
+ * untouched: a provider may require the history it receives to be exactly what
25
+ * it returned (Claude ties its reasoning blocks to the conversation), and a
26
+ * neutral rewrite would drop what polaris does not model (a signature,
27
+ * citations). A driver for another provider ignores the origin and uses the
28
+ * neutral fields.
29
+ */
30
+ export interface Origin {
31
+ readonly provider: string;
32
+ readonly block: unknown;
33
+ }
34
+
35
+ /** Plain text, from either side of the conversation. */
36
+ export interface TextPart {
37
+ readonly type: "text";
38
+ readonly text: string;
39
+ readonly origin?: Origin;
40
+ }
41
+
42
+ /** An image sent by the user, base64-encoded. */
43
+ export interface ImagePart {
44
+ readonly type: "image";
45
+ readonly mediaType: "image/png" | "image/jpeg" | "image/gif" | "image/webp";
46
+ /** The image bytes, base64-encoded, without a `data:` prefix. */
47
+ readonly data: string;
48
+ }
49
+
50
+ /**
51
+ * The model's reasoning, when the provider returns it. Its text may be empty
52
+ * (hidden or encrypted reasoning). Only the provider in `origin` can take it
53
+ * back; a driver for another provider leaves it out of what it sends.
54
+ */
55
+ export interface ReasoningPart {
56
+ readonly type: "reasoning";
57
+ readonly text: string;
58
+ readonly origin?: Origin;
59
+ }
60
+
61
+ /** A tool the model asks the application to run. */
62
+ export interface ToolCallPart {
63
+ readonly type: "tool_call";
64
+ readonly id: string;
65
+ readonly name: string;
66
+ /** The arguments as the model wrote them, parsed from JSON, not yet validated. */
67
+ readonly input: unknown;
68
+ readonly origin?: Origin;
69
+ }
70
+
71
+ /**
72
+ * A block with no neutral equivalent (a server-side tool's result, a
73
+ * compaction summary), kept so the conversation can go back to its provider
74
+ * unchanged. Other providers leave it out.
75
+ */
76
+ export interface ProviderPart {
77
+ readonly type: "provider";
78
+ readonly origin: Origin;
79
+ }
80
+
81
+ /** The answer to a {@link ToolCallPart}, sent back by the user side. */
82
+ export interface ToolResultPart {
83
+ readonly type: "tool_result";
84
+ readonly toolCallId: string;
85
+ readonly content: string;
86
+ readonly isError?: boolean;
87
+ }
88
+
89
+ export type UserPart = TextPart | ImagePart | ToolResultPart;
90
+ export type AssistantPart =
91
+ | TextPart
92
+ | ReasoningPart
93
+ | ToolCallPart
94
+ | ProviderPart;
95
+ export type ContentPart = UserPart | AssistantPart;
96
+
97
+ export interface UserMessage {
98
+ readonly role: "user";
99
+ readonly content: string | readonly UserPart[];
100
+ }
101
+
102
+ export interface AssistantMessage {
103
+ readonly role: "assistant";
104
+ readonly content: string | readonly AssistantPart[];
105
+ }
106
+
107
+ export type Message = UserMessage | AssistantMessage;
108
+
109
+ /** A tool the model may call. */
110
+ export interface ToolDefinition {
111
+ readonly name: string;
112
+ readonly description: string;
113
+ readonly inputSchema: JsonSchema;
114
+ }
115
+
116
+ /** Ask for a reply that is one JSON value matching `schema`. */
117
+ export interface OutputFormat {
118
+ readonly name: string;
119
+ readonly schema: JsonSchema;
120
+ readonly description?: string;
121
+ }
122
+
123
+ /** How hard the model works on a reply, for providers that let you choose. */
124
+ export type Effort = "low" | "medium" | "high" | "xhigh" | "max";
125
+
126
+ export interface ChatRequest {
127
+ /** Overrides the provider's configured model for this request. */
128
+ readonly model?: string;
129
+ readonly system?: string;
130
+ readonly messages: readonly Message[];
131
+ readonly maxTokens?: number;
132
+ readonly tools?: readonly ToolDefinition[];
133
+ /**
134
+ * `auto` lets the model decide; `none` forbids tool calls. There is no
135
+ * "force this tool": the current Claude models refuse it, and structured
136
+ * output covers what forcing was used for.
137
+ */
138
+ readonly toolChoice?: "auto" | "none";
139
+ readonly output?: OutputFormat;
140
+ readonly stopSequences?: readonly string[];
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;
158
+ /**
159
+ * Provider-specific fields, passed through by the driver that knows them
160
+ * and ignored by the others.
161
+ */
162
+ readonly providerOptions?: { readonly [provider: string]: unknown };
163
+ }
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
+
210
+ /**
211
+ * Why the model stopped.
212
+ *
213
+ * - `end`: it finished its reply.
214
+ * - `max_tokens`: it hit the output limit; the reply is cut.
215
+ * - `tool_use`: it is waiting for tool results.
216
+ * - `stop_sequence`: it wrote one of the request's stop sequences.
217
+ * - `refusal`: the provider declined to answer; the content may be empty.
218
+ * - `pause`: the provider paused a long turn; send the reply back to resume it.
219
+ * - `content_filter`: the provider filtered the content.
220
+ * - `other`: a reason polaris does not know yet.
221
+ */
222
+ export type StopReason =
223
+ | "end"
224
+ | "max_tokens"
225
+ | "tool_use"
226
+ | "stop_sequence"
227
+ | "refusal"
228
+ | "pause"
229
+ | "content_filter"
230
+ | "other";
231
+
232
+ /**
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.
236
+ */
237
+ export interface Usage {
238
+ /** Input tokens billed at the full input price: neither read from nor written to a cache. */
239
+ readonly inputTokens: number;
240
+ readonly outputTokens: number;
241
+ /** Input tokens read from the provider's prompt cache. */
242
+ readonly cacheReadTokens: number;
243
+ /** Input tokens written to the provider's prompt cache. */
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 };
251
+ }
252
+
253
+ export interface ChatResponse {
254
+ /** The provider's id for this response. */
255
+ readonly id: string;
256
+ /** The provider that answered, as its driver names itself. */
257
+ readonly provider: string;
258
+ /** The model that answered, as the provider reports it. */
259
+ readonly model: string;
260
+ readonly message: AssistantMessage & {
261
+ readonly content: readonly AssistantPart[];
262
+ };
263
+ /** The text parts of the reply, joined. */
264
+ readonly text: string;
265
+ /** The tool calls of the reply, in order. */
266
+ readonly toolCalls: readonly ToolCallPart[];
267
+ readonly stopReason: StopReason;
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;
276
+ /** What the request cost, when the config has a price for the model. */
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;
290
+ }
291
+
292
+ /**
293
+ * What a stream yields: text and reasoning as they arrive, each tool call once
294
+ * its arguments are complete, then exactly one `done` with the whole response.
295
+ */
296
+ export type StreamEvent =
297
+ | { readonly type: "text"; readonly text: string }
298
+ | { readonly type: "reasoning"; readonly text: string }
299
+ | { readonly type: "tool_call"; readonly call: ToolCallPart }
300
+ | { readonly type: "done"; readonly response: ChatResponse };
301
+
302
+ export interface EmbedRequest {
303
+ /** Overrides the provider's configured embedding model for this request. */
304
+ readonly model?: string;
305
+ readonly inputs: readonly string[];
306
+ /** Asks for vectors of this size, for models that can shorten them. */
307
+ readonly dimensions?: number;
308
+ }
309
+
310
+ export interface EmbedResponse {
311
+ readonly provider: string;
312
+ readonly model: string;
313
+ /** The length of every vector. */
314
+ readonly dimensions: number;
315
+ /** One vector per input, in input order. */
316
+ readonly vectors: readonly Float32Array[];
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;
323
+ /** What the request cost, when the config has a price for the model. */
324
+ readonly cost?: Cost;
325
+ }
326
+
327
+ /** The `fetch` signature: what a driver sends its HTTP requests through. */
328
+ export type Transport = (
329
+ input: string | URL | Request,
330
+ init?: RequestInit,
331
+ ) => Promise<Response>;
332
+
333
+ /**
334
+ * What polaris hands a driver for one call.
335
+ *
336
+ * A driver sends every HTTP request through `fetch` and passes it `signal`:
337
+ * that is how the deadline, the response size bound and the caller's
338
+ * cancellation reach the wire.
339
+ */
340
+ export interface DriverCall {
341
+ readonly fetch: Transport;
342
+ readonly signal: AbortSignal;
343
+ /**
344
+ * A key for this call only (an account's own key), to use instead of the
345
+ * configured one. polaris keeps it out of the errors it raises.
346
+ */
347
+ readonly apiKey: string | undefined;
348
+ }
349
+
350
+ /** A driver's defaults for a request that sets none. */
351
+ export interface DriverDefaults {
352
+ readonly model?: string;
353
+ readonly embeddingModel?: string;
354
+ readonly maxTokens?: number;
355
+ readonly streamMaxTokens?: number;
356
+ }
357
+
358
+ /** The request features a driver can serve. */
359
+ export interface DriverFeatures {
360
+ readonly tools: boolean;
361
+ readonly structuredOutput: boolean;
362
+ readonly images: boolean;
363
+ /** Takes a request's `server` tools. Default false. */
364
+ readonly serverTools?: boolean;
365
+ }
366
+
367
+ /**
368
+ * A provider, as polaris drives it. Implement `stream` and `embed` only when
369
+ * the provider has them: polaris answers a call to a missing one with
370
+ * `E_POLARIS_UNSUPPORTED` before anything is sent.
371
+ */
372
+ export interface PolarisDriver {
373
+ /** The provider's name, carried by every response (`anthropic`, `openai-compatible`…). */
374
+ readonly provider: string;
375
+ readonly features: DriverFeatures;
376
+ /**
377
+ * What a request uses when it sets none, so polaris can price a request
378
+ * before it is sent (a budget needs it).
379
+ */
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[];
387
+ chat(request: ChatRequest, call: DriverCall): Promise<ChatResponse>;
388
+ stream?(request: ChatRequest, call: DriverCall): AsyncIterable<StreamEvent>;
389
+ embed?(request: EmbedRequest, call: DriverCall): Promise<EmbedResponse>;
390
+ }
391
+
392
+ /** Builds a driver the first time its provider is used. */
393
+ export type ProviderFactory = () => PolarisDriver;
394
+
395
+ /** Bounds every provider call. */
396
+ export interface PolarisOptions {
397
+ /** Prices by model, to put a cost on every response and to keep budgets. */
398
+ readonly pricing?: Pricing;
399
+ /** Spending limits per scope (a user, a tenant), checked before each request. */
400
+ readonly budget?: BudgetConfig;
401
+ /** Receives every exchange with a provider, without message content by default. */
402
+ readonly log?: ExchangeLog;
403
+ /** Deadline for one call, streams included, in ms. Default 600 000 (10 minutes). */
404
+ readonly timeout?: number;
405
+ /** Largest response body accepted from a provider, in bytes. Default 32 MiB. */
406
+ readonly maxResponseBytes?: number;
407
+ /** The `fetch` every call goes through. Default: the global `fetch`. */
408
+ readonly fetch?: Transport;
409
+ }
410
+
411
+ export interface PolarisConfig<
412
+ KnownProviders extends Record<string, ProviderFactory>,
413
+ > extends PolarisOptions {
414
+ /** The provider `use()` returns when given no name. */
415
+ readonly default?: keyof KnownProviders & string;
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;
422
+ }
423
+
424
+ /**
425
+ * The providers an application declared, by name. Empty until the
426
+ * application augments it with {@link InferProviders}.
427
+ */
428
+ // biome-ignore lint/suspicious/noEmptyInterface: an application extends this interface by declaration merging; a type alias cannot be merged.
429
+ export interface ProvidersList {}
430
+
431
+ /** The providers a config declares, to augment {@link ProvidersList} with. */
432
+ export type InferProviders<
433
+ T extends { readonly providers: Record<string, ProviderFactory> },
434
+ > = T["providers"];