@c9up/polaris 0.0.0-stage → 0.1.0

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 (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +542 -2
  3. package/dist/Polaris.d.ts +76 -0
  4. package/dist/Polaris.d.ts.map +1 -0
  5. package/dist/Polaris.js +194 -0
  6. package/dist/Polaris.js.map +1 -0
  7. package/dist/PolarisClient.d.ts +136 -0
  8. package/dist/PolarisClient.d.ts.map +1 -0
  9. package/dist/PolarisClient.js +408 -0
  10. package/dist/PolarisClient.js.map +1 -0
  11. package/dist/PolarisProvider.d.ts +35 -0
  12. package/dist/PolarisProvider.d.ts.map +1 -0
  13. package/dist/PolarisProvider.js +47 -0
  14. package/dist/PolarisProvider.js.map +1 -0
  15. package/dist/augmentations.d.ts +15 -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 +44 -0
  20. package/dist/budget.d.ts.map +1 -0
  21. package/dist/budget.js +67 -0
  22. package/dist/budget.js.map +1 -0
  23. package/dist/cost.d.ts +48 -0
  24. package/dist/cost.d.ts.map +1 -0
  25. package/dist/cost.js +126 -0
  26. package/dist/cost.js.map +1 -0
  27. package/dist/drivers/anthropic.d.ts +39 -0
  28. package/dist/drivers/anthropic.d.ts.map +1 -0
  29. package/dist/drivers/anthropic.js +394 -0
  30. package/dist/drivers/anthropic.js.map +1 -0
  31. package/dist/drivers/openai-compatible.d.ts +54 -0
  32. package/dist/drivers/openai-compatible.d.ts.map +1 -0
  33. package/dist/drivers/openai-compatible.js +663 -0
  34. package/dist/drivers/openai-compatible.js.map +1 -0
  35. package/dist/drivers/status.d.ts +8 -0
  36. package/dist/drivers/status.d.ts.map +1 -0
  37. package/dist/drivers/status.js +21 -0
  38. package/dist/drivers/status.js.map +1 -0
  39. package/dist/errors.d.ts +55 -0
  40. package/dist/errors.d.ts.map +1 -0
  41. package/dist/errors.js +62 -0
  42. package/dist/errors.js.map +1 -0
  43. package/dist/extract.d.ts +38 -0
  44. package/dist/extract.d.ts.map +1 -0
  45. package/dist/extract.js +144 -0
  46. package/dist/extract.js.map +1 -0
  47. package/dist/index.d.ts +19 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +13 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/log.d.ts +47 -0
  52. package/dist/log.d.ts.map +1 -0
  53. package/dist/log.js +33 -0
  54. package/dist/log.js.map +1 -0
  55. package/dist/meter.d.ts +57 -0
  56. package/dist/meter.d.ts.map +1 -0
  57. package/dist/meter.js +91 -0
  58. package/dist/meter.js.map +1 -0
  59. package/dist/providers.d.ts +23 -0
  60. package/dist/providers.d.ts.map +1 -0
  61. package/dist/providers.js +21 -0
  62. package/dist/providers.js.map +1 -0
  63. package/dist/redact.d.ts +11 -0
  64. package/dist/redact.d.ts.map +1 -0
  65. package/dist/redact.js +76 -0
  66. package/dist/redact.js.map +1 -0
  67. package/dist/response.d.ts +21 -0
  68. package/dist/response.d.ts.map +1 -0
  69. package/dist/response.js +45 -0
  70. package/dist/response.js.map +1 -0
  71. package/dist/schema.d.ts +45 -0
  72. package/dist/schema.d.ts.map +1 -0
  73. package/dist/schema.js +43 -0
  74. package/dist/schema.js.map +1 -0
  75. package/dist/services/main.d.ts +23 -0
  76. package/dist/services/main.d.ts.map +1 -0
  77. package/dist/services/main.js +43 -0
  78. package/dist/services/main.js.map +1 -0
  79. package/dist/sse.d.ts +58 -0
  80. package/dist/sse.d.ts.map +1 -0
  81. package/dist/sse.js +91 -0
  82. package/dist/sse.js.map +1 -0
  83. package/dist/testing/FakeProvider.d.ts +85 -0
  84. package/dist/testing/FakeProvider.d.ts.map +1 -0
  85. package/dist/testing/FakeProvider.js +213 -0
  86. package/dist/testing/FakeProvider.js.map +1 -0
  87. package/dist/testing/index.d.ts +7 -0
  88. package/dist/testing/index.d.ts.map +1 -0
  89. package/dist/testing/index.js +7 -0
  90. package/dist/testing/index.js.map +1 -0
  91. package/dist/tools.d.ts +68 -0
  92. package/dist/tools.d.ts.map +1 -0
  93. package/dist/tools.js +107 -0
  94. package/dist/tools.js.map +1 -0
  95. package/dist/transport.d.ts +18 -0
  96. package/dist/transport.d.ts.map +1 -0
  97. package/dist/transport.js +68 -0
  98. package/dist/transport.js.map +1 -0
  99. package/dist/types.d.ts +293 -0
  100. package/dist/types.d.ts.map +1 -0
  101. package/dist/types.js +12 -0
  102. package/dist/types.js.map +1 -0
  103. package/package.json +76 -3
  104. package/src/Polaris.ts +280 -0
  105. package/src/PolarisClient.ts +591 -0
  106. package/src/PolarisProvider.ts +73 -0
  107. package/src/augmentations.ts +17 -0
  108. package/src/budget.ts +101 -0
  109. package/src/cost.ts +170 -0
  110. package/src/drivers/anthropic.ts +520 -0
  111. package/src/drivers/openai-compatible.ts +840 -0
  112. package/src/drivers/status.ts +30 -0
  113. package/src/errors.ts +161 -0
  114. package/src/extract.ts +214 -0
  115. package/src/index.ts +69 -0
  116. package/src/log.ts +84 -0
  117. package/src/meter.ts +188 -0
  118. package/src/providers.ts +25 -0
  119. package/src/redact.ts +78 -0
  120. package/src/response.ts +65 -0
  121. package/src/schema.ts +88 -0
  122. package/src/services/main.ts +49 -0
  123. package/src/sse.ts +128 -0
  124. package/src/testing/FakeProvider.ts +321 -0
  125. package/src/testing/index.ts +12 -0
  126. package/src/tools.ts +166 -0
  127. package/src/transport.ts +85 -0
  128. package/src/types.ts +328 -0
@@ -0,0 +1,68 @@
1
+ import { E_POLARIS_RESPONSE_TOO_LARGE } from "./errors.js";
2
+ /** Statuses whose response has no body by definition. */
3
+ const NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
4
+ /**
5
+ * Wrap `base` so every request it sends carries `signal`, and no response body
6
+ * longer than `maxBytes` is read.
7
+ *
8
+ * The size is counted on the bytes as they arrive, not taken from
9
+ * `Content-Length` alone: a compressed or chunked body announces nothing true
10
+ * about what it will decode to. A declared length over the bound is refused
11
+ * before reading anything.
12
+ */
13
+ export function boundFetch(base, signal, maxBytes) {
14
+ return async (input, init) => {
15
+ const own = init?.signal;
16
+ const response = await base(input, {
17
+ ...init,
18
+ signal: own ? AbortSignal.any([signal, own]) : signal,
19
+ });
20
+ const declared = Number(response.headers.get("content-length"));
21
+ if (Number.isFinite(declared) && declared > maxBytes) {
22
+ await response.body?.cancel().catch(() => undefined);
23
+ throw new E_POLARIS_RESPONSE_TOO_LARGE([maxBytes]);
24
+ }
25
+ if (response.body === null || NULL_BODY_STATUSES.has(response.status)) {
26
+ return response;
27
+ }
28
+ let seen = 0;
29
+ const limited = response.body.pipeThrough(new TransformStream({
30
+ transform(chunk, controller) {
31
+ seen += chunk.byteLength;
32
+ if (seen > maxBytes) {
33
+ controller.error(new E_POLARIS_RESPONSE_TOO_LARGE([maxBytes]));
34
+ return;
35
+ }
36
+ controller.enqueue(chunk);
37
+ },
38
+ }));
39
+ return new Response(limited, {
40
+ status: response.status,
41
+ statusText: response.statusText,
42
+ headers: response.headers,
43
+ });
44
+ };
45
+ }
46
+ /**
47
+ * Settle with `promise`, or reject with `signal.reason` as soon as `signal`
48
+ * aborts — so a driver that ignores its signal still cannot outlive the
49
+ * deadline.
50
+ */
51
+ export function untilAborted(promise, signal) {
52
+ if (signal.aborted) {
53
+ promise.catch(() => undefined);
54
+ return Promise.reject(signal.reason);
55
+ }
56
+ return new Promise((resolve, reject) => {
57
+ const onAbort = () => reject(signal.reason);
58
+ signal.addEventListener("abort", onAbort, { once: true });
59
+ promise.then((value) => {
60
+ signal.removeEventListener("abort", onAbort);
61
+ resolve(value);
62
+ }, (error) => {
63
+ signal.removeEventListener("abort", onAbort);
64
+ reject(error);
65
+ });
66
+ });
67
+ }
68
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,MA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,CAAC,CAAC;YAC7C,MAAM,CAAC,KAAK,CAAC,CAAC;QACf,CAAC,CACD,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,293 @@
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
+ import type { BudgetConfig } from "./budget.js";
12
+ import type { Cost, Pricing } from "./cost.js";
13
+ import type { ExchangeLog } from "./log.js";
14
+ /** A JSON Schema object, as providers take it for tools and structured output. */
15
+ export type JsonSchema = {
16
+ readonly [key: string]: unknown;
17
+ };
18
+ /**
19
+ * The block a part was read from, as its provider sent it.
20
+ *
21
+ * A driver sends a part whose origin is its own provider back as this block,
22
+ * untouched: a provider may require the history it receives to be exactly what
23
+ * it returned (Claude ties its reasoning blocks to the conversation), and a
24
+ * neutral rewrite would drop what polaris does not model (a signature,
25
+ * citations). A driver for another provider ignores the origin and uses the
26
+ * neutral fields.
27
+ */
28
+ export interface Origin {
29
+ readonly provider: string;
30
+ readonly block: unknown;
31
+ }
32
+ /** Plain text, from either side of the conversation. */
33
+ export interface TextPart {
34
+ readonly type: "text";
35
+ readonly text: string;
36
+ readonly origin?: Origin;
37
+ }
38
+ /** An image sent by the user, base64-encoded. */
39
+ export interface ImagePart {
40
+ readonly type: "image";
41
+ readonly mediaType: "image/png" | "image/jpeg" | "image/gif" | "image/webp";
42
+ /** The image bytes, base64-encoded, without a `data:` prefix. */
43
+ readonly data: string;
44
+ }
45
+ /**
46
+ * The model's reasoning, when the provider returns it. Its text may be empty
47
+ * (hidden or encrypted reasoning). Only the provider in `origin` can take it
48
+ * back; a driver for another provider leaves it out of what it sends.
49
+ */
50
+ export interface ReasoningPart {
51
+ readonly type: "reasoning";
52
+ readonly text: string;
53
+ readonly origin?: Origin;
54
+ }
55
+ /** A tool the model asks the application to run. */
56
+ export interface ToolCallPart {
57
+ readonly type: "tool_call";
58
+ readonly id: string;
59
+ readonly name: string;
60
+ /** The arguments as the model wrote them, parsed from JSON, not yet validated. */
61
+ readonly input: unknown;
62
+ readonly origin?: Origin;
63
+ }
64
+ /**
65
+ * A block with no neutral equivalent (a server-side tool's result, a
66
+ * compaction summary), kept so the conversation can go back to its provider
67
+ * unchanged. Other providers leave it out.
68
+ */
69
+ export interface ProviderPart {
70
+ readonly type: "provider";
71
+ readonly origin: Origin;
72
+ }
73
+ /** The answer to a {@link ToolCallPart}, sent back by the user side. */
74
+ export interface ToolResultPart {
75
+ readonly type: "tool_result";
76
+ readonly toolCallId: string;
77
+ readonly content: string;
78
+ readonly isError?: boolean;
79
+ }
80
+ export type UserPart = TextPart | ImagePart | ToolResultPart;
81
+ export type AssistantPart = TextPart | ReasoningPart | ToolCallPart | ProviderPart;
82
+ export type ContentPart = UserPart | AssistantPart;
83
+ export interface UserMessage {
84
+ readonly role: "user";
85
+ readonly content: string | readonly UserPart[];
86
+ }
87
+ export interface AssistantMessage {
88
+ readonly role: "assistant";
89
+ readonly content: string | readonly AssistantPart[];
90
+ }
91
+ export type Message = UserMessage | AssistantMessage;
92
+ /** A tool the model may call. */
93
+ export interface ToolDefinition {
94
+ readonly name: string;
95
+ readonly description: string;
96
+ readonly inputSchema: JsonSchema;
97
+ }
98
+ /** Ask for a reply that is one JSON value matching `schema`. */
99
+ export interface OutputFormat {
100
+ readonly name: string;
101
+ readonly schema: JsonSchema;
102
+ readonly description?: string;
103
+ }
104
+ /** How hard the model works on a reply, for providers that let you choose. */
105
+ export type Effort = "low" | "medium" | "high" | "xhigh" | "max";
106
+ export interface ChatRequest {
107
+ /** Overrides the provider's configured model for this request. */
108
+ readonly model?: string;
109
+ readonly system?: string;
110
+ readonly messages: readonly Message[];
111
+ readonly maxTokens?: number;
112
+ readonly tools?: readonly ToolDefinition[];
113
+ /**
114
+ * `auto` lets the model decide; `none` forbids tool calls. There is no
115
+ * "force this tool": the current Claude models refuse it, and structured
116
+ * output covers what forcing was used for.
117
+ */
118
+ readonly toolChoice?: "auto" | "none";
119
+ readonly output?: OutputFormat;
120
+ readonly stopSequences?: readonly string[];
121
+ readonly effort?: Effort;
122
+ /**
123
+ * Provider-specific fields, passed through by the driver that knows them
124
+ * and ignored by the others.
125
+ */
126
+ readonly providerOptions?: {
127
+ readonly [provider: string]: unknown;
128
+ };
129
+ }
130
+ /**
131
+ * Why the model stopped.
132
+ *
133
+ * - `end`: it finished its reply.
134
+ * - `max_tokens`: it hit the output limit; the reply is cut.
135
+ * - `tool_use`: it is waiting for tool results.
136
+ * - `stop_sequence`: it wrote one of the request's stop sequences.
137
+ * - `refusal`: the provider declined to answer; the content may be empty.
138
+ * - `pause`: the provider paused a long turn; send the reply back to resume it.
139
+ * - `content_filter`: the provider filtered the content.
140
+ * - `other`: a reason polaris does not know yet.
141
+ */
142
+ export type StopReason = "end" | "max_tokens" | "tool_use" | "stop_sequence" | "refusal" | "pause" | "content_filter" | "other";
143
+ /**
144
+ * Tokens a request used. Counts a provider does not report are 0. The three
145
+ * input counts do not overlap, so each is billed at its own price.
146
+ */
147
+ export interface Usage {
148
+ /** Input tokens billed at the full input price: neither read from nor written to a cache. */
149
+ readonly inputTokens: number;
150
+ readonly outputTokens: number;
151
+ /** Input tokens read from the provider's prompt cache. */
152
+ readonly cacheReadTokens: number;
153
+ /** Input tokens written to the provider's prompt cache. */
154
+ readonly cacheWriteTokens: number;
155
+ }
156
+ export interface ChatResponse {
157
+ /** The provider's id for this response. */
158
+ readonly id: string;
159
+ /** The provider that answered, as its driver names itself. */
160
+ readonly provider: string;
161
+ /** The model that answered, as the provider reports it. */
162
+ readonly model: string;
163
+ readonly message: AssistantMessage & {
164
+ readonly content: readonly AssistantPart[];
165
+ };
166
+ /** The text parts of the reply, joined. */
167
+ readonly text: string;
168
+ /** The tool calls of the reply, in order. */
169
+ readonly toolCalls: readonly ToolCallPart[];
170
+ readonly stopReason: StopReason;
171
+ readonly usage: Usage;
172
+ /** What the request cost, when the config has a price for the model. */
173
+ readonly cost?: Cost;
174
+ }
175
+ /**
176
+ * What a stream yields: text and reasoning as they arrive, each tool call once
177
+ * its arguments are complete, then exactly one `done` with the whole response.
178
+ */
179
+ export type StreamEvent = {
180
+ readonly type: "text";
181
+ readonly text: string;
182
+ } | {
183
+ readonly type: "reasoning";
184
+ readonly text: string;
185
+ } | {
186
+ readonly type: "tool_call";
187
+ readonly call: ToolCallPart;
188
+ } | {
189
+ readonly type: "done";
190
+ readonly response: ChatResponse;
191
+ };
192
+ export interface EmbedRequest {
193
+ /** Overrides the provider's configured embedding model for this request. */
194
+ readonly model?: string;
195
+ readonly inputs: readonly string[];
196
+ /** Asks for vectors of this size, for models that can shorten them. */
197
+ readonly dimensions?: number;
198
+ }
199
+ export interface EmbedResponse {
200
+ readonly provider: string;
201
+ readonly model: string;
202
+ /** The length of every vector. */
203
+ readonly dimensions: number;
204
+ /** One vector per input, in input order. */
205
+ readonly vectors: readonly Float32Array[];
206
+ readonly usage: {
207
+ readonly inputTokens: number;
208
+ };
209
+ /** What the request cost, when the config has a price for the model. */
210
+ readonly cost?: Cost;
211
+ }
212
+ /** The `fetch` signature: what a driver sends its HTTP requests through. */
213
+ export type Transport = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
214
+ /**
215
+ * What polaris hands a driver for one call.
216
+ *
217
+ * A driver sends every HTTP request through `fetch` and passes it `signal`:
218
+ * that is how the deadline, the response size bound and the caller's
219
+ * cancellation reach the wire.
220
+ */
221
+ export interface DriverCall {
222
+ readonly fetch: Transport;
223
+ readonly signal: AbortSignal;
224
+ /**
225
+ * A key for this call only (an account's own key), to use instead of the
226
+ * configured one. polaris keeps it out of the errors it raises.
227
+ */
228
+ readonly apiKey: string | undefined;
229
+ }
230
+ /** A driver's defaults for a request that sets none. */
231
+ export interface DriverDefaults {
232
+ readonly model?: string;
233
+ readonly embeddingModel?: string;
234
+ readonly maxTokens?: number;
235
+ readonly streamMaxTokens?: number;
236
+ }
237
+ /** The request features a driver can serve. */
238
+ export interface DriverFeatures {
239
+ readonly tools: boolean;
240
+ readonly structuredOutput: boolean;
241
+ readonly images: boolean;
242
+ }
243
+ /**
244
+ * A provider, as polaris drives it. Implement `stream` and `embed` only when
245
+ * the provider has them: polaris answers a call to a missing one with
246
+ * `E_POLARIS_UNSUPPORTED` before anything is sent.
247
+ */
248
+ export interface PolarisDriver {
249
+ /** The provider's name, carried by every response (`anthropic`, `openai-compatible`…). */
250
+ readonly provider: string;
251
+ readonly features: DriverFeatures;
252
+ /**
253
+ * What a request uses when it sets none, so polaris can price a request
254
+ * before it is sent (a budget needs it).
255
+ */
256
+ readonly defaults?: DriverDefaults;
257
+ chat(request: ChatRequest, call: DriverCall): Promise<ChatResponse>;
258
+ stream?(request: ChatRequest, call: DriverCall): AsyncIterable<StreamEvent>;
259
+ embed?(request: EmbedRequest, call: DriverCall): Promise<EmbedResponse>;
260
+ }
261
+ /** Builds a driver the first time its provider is used. */
262
+ export type ProviderFactory = () => PolarisDriver;
263
+ /** Bounds every provider call. */
264
+ export interface PolarisOptions {
265
+ /** Prices by model, to put a cost on every response and to keep budgets. */
266
+ readonly pricing?: Pricing;
267
+ /** Spending limits per scope (a user, a tenant), checked before each request. */
268
+ readonly budget?: BudgetConfig;
269
+ /** Receives every exchange with a provider, without message content by default. */
270
+ readonly log?: ExchangeLog;
271
+ /** Deadline for one call, streams included, in ms. Default 600 000 (10 minutes). */
272
+ readonly timeout?: number;
273
+ /** Largest response body accepted from a provider, in bytes. Default 32 MiB. */
274
+ readonly maxResponseBytes?: number;
275
+ /** The `fetch` every call goes through. Default: the global `fetch`. */
276
+ readonly fetch?: Transport;
277
+ }
278
+ export interface PolarisConfig<KnownProviders extends Record<string, ProviderFactory>> extends PolarisOptions {
279
+ /** The provider `use()` returns when given no name. */
280
+ readonly default?: keyof KnownProviders & string;
281
+ readonly providers: KnownProviders;
282
+ }
283
+ /**
284
+ * The providers an application declared, by name. Empty until the
285
+ * application augments it with {@link InferProviders}.
286
+ */
287
+ export interface ProvidersList {
288
+ }
289
+ /** The providers a config declares, to augment {@link ProvidersList} with. */
290
+ export type InferProviders<T extends {
291
+ readonly providers: Record<string, ProviderFactory>;
292
+ }> = T["providers"];
293
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,kFAAkF;AAClF,MAAM,MAAM,UAAU,GAAG;IAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAE7D;;;;;;;;;GASG;AACH,MAAM,WAAW,MAAM;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACxB;AAED,wDAAwD;AACxD,MAAM,WAAW,QAAQ;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,iDAAiD;AACjD,MAAM,WAAW,SAAS;IACzB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,WAAW,GAAG,YAAY,GAAG,WAAW,GAAG,YAAY,CAAC;IAC5E,iEAAiE;IACjE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,IAAI,EAA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C,EAAE,MAAM,CAAC;IAC1B,gFAAgF;IAChF,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,wEAAwE;IACxE,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC;CAC3B;AAED,MAAM,WAAW,aAAa,CAC7B,cAAc,SAAS,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CACrD,SAAQ,cAAc;IACvB,uDAAuD;IACvD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,cAAc,GAAG,MAAM,CAAC;IACjD,QAAQ,CAAC,SAAS,EAAE,cAAc,CAAC;CACnC;AAED;;;GAGG;AAEH,MAAM,WAAW,aAAa;CAAG;AAEjC,8EAA8E;AAC9E,MAAM,MAAM,cAAc,CACzB,CAAC,SAAS;IAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;CAAE,IAC9D,CAAC,CAAC,WAAW,CAAC,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,12 @@
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
+ export {};
12
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG"}
package/package.json CHANGED
@@ -1,6 +1,79 @@
1
1
  {
2
2
  "name": "@c9up/polaris",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Polaris — one API for language models, whatever the provider: streamed replies, tool calls, structured output, embeddings and the cost of every request",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "src"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ },
18
+ "./types": {
19
+ "types": "./dist/types.d.ts",
20
+ "import": "./dist/types.js"
21
+ },
22
+ "./provider": {
23
+ "types": "./dist/PolarisProvider.d.ts",
24
+ "import": "./dist/PolarisProvider.js"
25
+ },
26
+ "./services/main": {
27
+ "types": "./dist/services/main.d.ts",
28
+ "import": "./dist/services/main.js"
29
+ },
30
+ "./testing": {
31
+ "types": "./dist/testing/index.d.ts",
32
+ "import": "./dist/testing/index.js"
33
+ }
34
+ },
35
+ "//engines": "Node 24, not 22, because AsyncLocalStorage sits on the request hot path — the container resolves through it, HttpContext is it, and the emitter reads it on every event. Node 24 backs it with AsyncContextFrame by default, and measured on that exact pattern (one context per request, six async boundaries, several reads) it costs 0.61 us per request instead of 1.55 us: 2.5x, on every request, for free. Before 24 the same mechanism existed only behind an experimental flag, and a framework cannot base its performance on a flag the application has to remember to pass. This is NOT 'track the current LTS' — security fixes still land in 22 until 2027, npm 11 is irrelevant under pnpm, and node:sqlite is not what atlas uses.",
36
+ "engines": {
37
+ "node": ">=24.0.0"
38
+ },
39
+ "dependencies": {
40
+ "@c9up/atom": "^0.1.17",
41
+ "@c9up/flare": "^0.1.0"
42
+ },
43
+ "devDependencies": {
44
+ "@anthropic-ai/sdk": "^1.11.0",
45
+ "@biomejs/biome": "^2.6.8",
46
+ "@c9up/ream": "^0.2.32",
47
+ "@c9up/rune": "^0.2.5",
48
+ "@types/node": "^22.20.1",
49
+ "@vitest/coverage-v8": "4.1.12",
50
+ "typescript": "^6.0.3",
51
+ "vitest": "4.1.12"
52
+ },
53
+ "peerDependencies": {
54
+ "@anthropic-ai/sdk": "^1.11.0",
55
+ "@c9up/ream": "^0.2.32"
56
+ },
57
+ "peerDependenciesMeta": {
58
+ "@anthropic-ai/sdk": {
59
+ "optional": true
60
+ },
61
+ "@c9up/ream": {
62
+ "optional": true
63
+ }
64
+ },
65
+ "publishConfig": {
66
+ "access": "public"
67
+ },
68
+ "repository": {
69
+ "type": "git",
70
+ "url": "git+https://github.com/C9up/polaris.git"
71
+ },
72
+ "scripts": {
73
+ "build": "tsc -p tsconfig.build.json",
74
+ "test": "vitest run",
75
+ "lint": "biome check src/ tests/",
76
+ "test:coverage": "vitest run --coverage",
77
+ "typecheck": "tsc --noEmit"
78
+ }
6
79
  }