@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
package/dist/budget.js ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Spending limits per scope (a user, a tenant), enforced before a request is
3
+ * sent.
4
+ *
5
+ * A request reserves its estimated cost first: the reservation is added to
6
+ * the scope's spend, and if the total goes past the limit the reservation is
7
+ * taken back and the request refused, before anything leaves the server.
8
+ * Once the reply is in, the reservation is replaced by the real cost.
9
+ * Reserving rather than reading the spend is what keeps two requests sent at
10
+ * the same moment from passing the limit together.
11
+ */
12
+ import { add, exceeds, isZero, subtract } from "./cost.js";
13
+ import { E_POLARIS_BUDGET_EXCEEDED } from "./errors.js";
14
+ /** A spend store in memory: one process, lost on restart. For tests and a single instance. */
15
+ export function memoryBudgetStore() {
16
+ const spent = new Map();
17
+ return {
18
+ async add(scope, amount) {
19
+ const total = add(spent.get(scope) ?? "0", amount);
20
+ spent.set(scope, total);
21
+ return total;
22
+ },
23
+ };
24
+ }
25
+ const NOTHING = {
26
+ settle: async () => undefined,
27
+ release: async () => undefined,
28
+ };
29
+ /** Resolve the limit of `scope`. */
30
+ export async function limitOf(config, scope) {
31
+ return typeof config.limit === "function"
32
+ ? config.limit(scope)
33
+ : Object.hasOwn(config.limit, scope)
34
+ ? config.limit[scope]
35
+ : undefined;
36
+ }
37
+ /**
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.
40
+ */
41
+ export async function reserve(store, scope, limit, estimate) {
42
+ if (limit === undefined)
43
+ return NOTHING;
44
+ const total = await store.add(scope, estimate);
45
+ if (exceeds(total, limit)) {
46
+ await store.add(scope, `-${estimate}`);
47
+ throw new E_POLARIS_BUDGET_EXCEEDED([scope, limit]);
48
+ }
49
+ let open = true;
50
+ return {
51
+ async settle(cost) {
52
+ if (!open)
53
+ return;
54
+ open = false;
55
+ const difference = subtract(cost, estimate);
56
+ if (!isZero(difference))
57
+ await store.add(scope, difference);
58
+ },
59
+ async release() {
60
+ if (!open)
61
+ return;
62
+ open = false;
63
+ await store.add(scope, `-${estimate}`);
64
+ },
65
+ };
66
+ }
67
+ //# sourceMappingURL=budget.js.map
@@ -0,0 +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;AAC3D,OAAO,EAAE,y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CAAC,KAAK,EAAE,IAAI,QAAQ,EAAE,CAAC,CAAC;QACvC,MAAM,IAAI,yBAAyB,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IACrD,CAAC;IACD,IAAI,IAAI,GAAG,IAAI,CAAC;IAChB,OAAO;QACN,KAAK,CAAC,MAAM,CAAC,IAAI;YAChB,IAAI,CAAC,IAAI;gBAAE,OAAO;YAClB,IAAI,GAAG,KAAK,CAAC;YACb,MAAM,UAAU,GAAG,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YAC5C,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;gBAAE,MAAM,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QAC7D,CAAC;QACD,KAAK,CAAC,OAAO;YACZ,IAAI,CAAC,IAAI;gBAAE,OAAO;YAClB,IAAI,GAAG,KAAK,CAAC;YACb,MAAM,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,QAAQ,EAAE,CAAC,CAAC;QACxC,CAAC;KACD,CAAC;AACH,CAAC"}
package/dist/cost.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * What a request costs, computed exactly (atom decimals, never floats), from
3
+ * a price table the application declares.
4
+ */
5
+ import type { ChatRequest, EmbedRequest, Usage } from "./types.js";
6
+ /**
7
+ * A model's prices, per million tokens, as decimal strings (`"4"`, `"0.40"`).
8
+ * Cache prices default to the input price.
9
+ */
10
+ export interface ModelPrice {
11
+ readonly input: string;
12
+ readonly output?: string;
13
+ readonly cacheRead?: string;
14
+ readonly cacheWrite?: string;
15
+ }
16
+ export interface Pricing {
17
+ /** The currency every price is in, written on every cost: `USD`, `CHF`… */
18
+ readonly currency: string;
19
+ /** Prices by model id, as the provider names it in its responses. */
20
+ readonly models: {
21
+ readonly [model: string]: ModelPrice;
22
+ };
23
+ }
24
+ /** An amount of money, as an exact decimal string. */
25
+ export interface Cost {
26
+ readonly amount: string;
27
+ readonly currency: string;
28
+ }
29
+ /** Check a price table once, at boot: every price a non-negative decimal. */
30
+ export declare function checkPricing(pricing: Pricing): void;
31
+ /** The price of `model`, if the table has one. */
32
+ export declare function priceFor(pricing: Pricing | undefined, model: string | undefined): ModelPrice | undefined;
33
+ /** What `usage` cost at `price`. */
34
+ export declare function costOf(price: ModelPrice, usage: Usage): string;
35
+ /**
36
+ * The most a chat request can cost: its input estimated from its length (few
37
+ * characters per token, so the estimate errs high), and its output at the
38
+ * full `maxTokens`.
39
+ */
40
+ export declare function estimateChat(price: ModelPrice, request: ChatRequest, maxTokens: number): string;
41
+ /** The most an embedding request can cost. */
42
+ export declare function estimateEmbed(price: ModelPrice, request: EmbedRequest): string;
43
+ /** Whether `a` is more than `b`, both decimal strings. */
44
+ export declare function exceeds(a: string, b: string): boolean;
45
+ export declare function add(a: string, b: string): string;
46
+ export declare function subtract(a: string, b: string): string;
47
+ export declare function isZero(value: string): boolean;
48
+ //# sourceMappingURL=cost.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnE;;;GAGG;AACH,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,OAAO;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,qEAAqE;IACrE,QAAQ,CAAC,MAAM,EAAE;QAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,UAAU,CAAA;KAAE,CAAC;CAC1D;AAED,sDAAsD;AACtD,MAAM,WAAW,IAAI;IACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B;AAUD,6EAA6E;AAC7E,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAenD;AAED,kDAAkD;AAClD,wBAAgB,QAAQ,CACvB,OAAO,EAAE,OAAO,GAAG,SAAS,EAC5B,KAAK,EAAE,MAAM,GAAG,SAAS,GACvB,UAAU,GAAG,SAAS,CAKxB;AAED,oCAAoC;AACpC,wBAAgB,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,GAAG,MAAM,CAU9D;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAC3B,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,WAAW,EACpB,SAAS,EAAE,MAAM,GACf,MAAM,CAOR;AAED,8CAA8C;AAC9C,wBAAgB,aAAa,CAC5B,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,YAAY,GACnB,MAAM,CAWR;AA8BD,0DAA0D;AAC1D,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAErD;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE7C"}
package/dist/cost.js ADDED
@@ -0,0 +1,126 @@
1
+ /**
2
+ * What a request costs, computed exactly (atom decimals, never floats), from
3
+ * a price table the application declares.
4
+ */
5
+ import { decimal } from "@c9up/atom";
6
+ import { E_POLARIS_INVALID_CONFIG } from "./errors.js";
7
+ const MILLION = 1_000_000;
8
+ /** Digits kept when a price per million is brought down to a token. */
9
+ const PRECISION = 18;
10
+ /** What an image weighs in an estimate, in input tokens. */
11
+ const IMAGE_TOKENS = 1_600;
12
+ /** Characters per token in an estimate: few, so the estimate errs high. */
13
+ const CHARS_PER_TOKEN = 3;
14
+ /** Check a price table once, at boot: every price a non-negative decimal. */
15
+ export function checkPricing(pricing) {
16
+ if (typeof pricing.currency !== "string" || pricing.currency.trim() === "") {
17
+ throw new E_POLARIS_INVALID_CONFIG(["pricing.currency is required"]);
18
+ }
19
+ for (const [model, price] of Object.entries(pricing.models)) {
20
+ for (const [field, value] of Object.entries(price)) {
21
+ if (value === undefined)
22
+ continue;
23
+ const parsed = typeof value === "string" ? safe(value) : undefined;
24
+ if (parsed === undefined || parsed.isNegative()) {
25
+ throw new E_POLARIS_INVALID_CONFIG([
26
+ `pricing.models['${model}'].${field} must be a non-negative decimal string`,
27
+ ]);
28
+ }
29
+ }
30
+ }
31
+ }
32
+ /** The price of `model`, if the table has one. */
33
+ export function priceFor(pricing, model) {
34
+ if (pricing === undefined || model === undefined)
35
+ return undefined;
36
+ return Object.hasOwn(pricing.models, model)
37
+ ? pricing.models[model]
38
+ : undefined;
39
+ }
40
+ /** What `usage` cost at `price`. */
41
+ export function costOf(price, usage) {
42
+ const cacheRead = price.cacheRead ?? price.input;
43
+ const cacheWrite = price.cacheWrite ?? price.input;
44
+ return decimal(usage.inputTokens)
45
+ .times(price.input)
46
+ .plus(decimal(usage.outputTokens).times(price.output ?? "0"))
47
+ .plus(decimal(usage.cacheReadTokens).times(cacheRead))
48
+ .plus(decimal(usage.cacheWriteTokens).times(cacheWrite))
49
+ .div(MILLION, { precision: PRECISION })
50
+ .toString();
51
+ }
52
+ /**
53
+ * The most a chat request can cost: its input estimated from its length (few
54
+ * characters per token, so the estimate errs high), and its output at the
55
+ * full `maxTokens`.
56
+ */
57
+ export function estimateChat(price, request, maxTokens) {
58
+ return costOf(price, {
59
+ inputTokens: estimateTokens(request),
60
+ outputTokens: maxTokens,
61
+ cacheReadTokens: 0,
62
+ cacheWriteTokens: 0,
63
+ });
64
+ }
65
+ /** The most an embedding request can cost. */
66
+ export function estimateEmbed(price, request) {
67
+ const characters = request.inputs.reduce((total, input) => total + input.length, 0);
68
+ return costOf(price, {
69
+ inputTokens: Math.ceil(characters / CHARS_PER_TOKEN),
70
+ outputTokens: 0,
71
+ cacheReadTokens: 0,
72
+ cacheWriteTokens: 0,
73
+ });
74
+ }
75
+ function estimateTokens(request) {
76
+ let characters = (request.system ?? "").length;
77
+ let images = 0;
78
+ for (const message of request.messages) {
79
+ if (typeof message.content === "string") {
80
+ characters += message.content.length;
81
+ continue;
82
+ }
83
+ for (const part of message.content) {
84
+ if (part.type === "image")
85
+ images += 1;
86
+ else if (part.type === "text" || part.type === "reasoning") {
87
+ characters += part.text.length;
88
+ }
89
+ else if (part.type === "tool_result")
90
+ characters += part.content.length;
91
+ else if (part.type === "tool_call") {
92
+ characters += JSON.stringify(part.input ?? null).length;
93
+ }
94
+ }
95
+ }
96
+ for (const tool of request.tools ?? []) {
97
+ characters += tool.name.length + tool.description.length;
98
+ characters += JSON.stringify(tool.inputSchema).length;
99
+ }
100
+ if (request.output !== undefined) {
101
+ characters += JSON.stringify(request.output.schema).length;
102
+ }
103
+ return Math.ceil(characters / CHARS_PER_TOKEN) + images * IMAGE_TOKENS;
104
+ }
105
+ /** Whether `a` is more than `b`, both decimal strings. */
106
+ export function exceeds(a, b) {
107
+ return decimal(a).gt(b);
108
+ }
109
+ export function add(a, b) {
110
+ return decimal(a).plus(b).toString();
111
+ }
112
+ export function subtract(a, b) {
113
+ return decimal(a).minus(b).toString();
114
+ }
115
+ export function isZero(value) {
116
+ return decimal(value).isZero();
117
+ }
118
+ function safe(value) {
119
+ try {
120
+ return decimal(value);
121
+ }
122
+ catch {
123
+ return undefined;
124
+ }
125
+ }
126
+ //# sourceMappingURL=cost.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings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AAO,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,SAAS,CAAC;IAClB,CAAC;AACF,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * The Anthropic driver: Claude through the official SDK (`@anthropic-ai/sdk`,
3
+ * an optional peer, loaded on the first request).
4
+ *
5
+ * Requests go through the SDK's beta namespace, which is the general one plus
6
+ * the beta features (server-side refusal fallbacks among them): one request
7
+ * shape and one response shape whether or not a beta is turned on.
8
+ */
9
+ import type { ProviderFactory } from "../types.js";
10
+ export interface AnthropicConfig {
11
+ /** The model every request uses unless it names another. */
12
+ readonly model: string;
13
+ /**
14
+ * The API key. Without one, the SDK reads `ANTHROPIC_API_KEY` (or another
15
+ * credential it knows) from the environment. A per-call key overrides it.
16
+ */
17
+ readonly apiKey?: string;
18
+ /** Another endpoint speaking the Anthropic API. */
19
+ readonly baseURL?: string;
20
+ /** Output limit when a request sets none. Default 16 000, 64 000 for a stream. */
21
+ readonly maxTokens?: number;
22
+ /**
23
+ * Retries of a rate-limited, overloaded or failed request, honouring the
24
+ * provider's `retry-after`, all within the call's deadline. Default 2.
25
+ */
26
+ readonly maxRetries?: number;
27
+ /**
28
+ * Server-side fallback when the model declines a request: `'default'` lets
29
+ * the API pick the fallback model, or name them. Turns on its beta.
30
+ */
31
+ readonly fallbacks?: "default" | readonly {
32
+ readonly model: string;
33
+ }[];
34
+ /** Further `anthropic-beta` flags sent with every request. */
35
+ readonly betas?: readonly string[];
36
+ }
37
+ /** A provider factory for Claude: `providers: { claude: anthropic({ model }) }`. */
38
+ export declare function anthropic(config: AnthropicConfig): ProviderFactory;
39
+ //# sourceMappingURL=anthropic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/drivers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAaH,OAAO,KAAK,EAWX,eAAe,EAIf,MAAM,aAAa,CAAC;AAiBrB,MAAM,WAAW,eAAe;IAC/B,4DAA4D;IAC5D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,mDAAmD;IACnD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,kFAAkF;IAClF,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,GAAG,SAAS;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IACvE,8DAA8D;IAC9D,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAQD,oFAAoF;AACpF,wBAAgB,SAAS,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAElE"}