@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 @@
1
+ {"version":3,"file":"meter.d.ts","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,WAAW,EAIhB,MAAM,aAAa,CAAC;AACrB,OAAO,EAEN,KAAK,IAAI,EAGT,KAAK,UAAU,EACf,KAAK,OAAO,EAEZ,MAAM,WAAW,CAAC;AAMnB,OAAO,EAEN,KAAK,a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,CAAC,IAAI,GAAG,SAAS,CAAC,CAAC;IACzD;;;OAGG;IACH,IAAI,CACH,KAAK,EAAE,OAAO,EACd,OAAO,EAAE;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,GAC7C,OAAO,CAAC,IAAI,CAAC,CAAC;CACjB;AAED,+FAA+F;AAC/F,wBAAsB,UAAU,CAC/B,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,UAAU,GACf,OAAO,CAAC,MAAM,CAAC,CAsFjB;AAED,4EAA4E;AAC5E,wBAAgB,OAAO,CACtB,CAAC,EAAE,IAAI,GAAG,SAAS,EACnB,CAAC,EAAE,IAAI,GAAG,SAAS,GACjB,IAAI,GAAG,SAAS,CAIlB"}
package/dist/meter.js ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Prices, budgets and the exchange log, around each provider call.
3
+ *
4
+ * Before the call: its price is found, the request's own ceiling (`maxCost`)
5
+ * checked, and its estimated cost reserved on the scope's budget; a refusal
6
+ * sends nothing. After it: the reservation is replaced by the real cost, the
7
+ * cost is put on the response, and the exchange is logged.
8
+ */
9
+ import { limitOf, reserve, } from "./budget.js";
10
+ import { add, costOf, exceeds, priceFor, } from "./cost.js";
11
+ import { E_POLARIS_BUDGET_EXCEEDED, E_POLARIS_INVALID_REQUEST, E_POLARIS_UNKNOWN_PRICE, } from "./errors.js";
12
+ import { codeOf, writeLog, } from "./log.js";
13
+ /** Open the meter of one call. Throws, before anything is sent, when its budget refuses it. */
14
+ export async function openTicket(config, input) {
15
+ const started = Date.now();
16
+ const price = priceFor(config.pricing, input.model);
17
+ const limit = input.scope !== undefined && config.budget !== undefined
18
+ ? await limitOf(config.budget, input.scope)
19
+ : undefined;
20
+ if (input.scope !== undefined && config.budget === undefined) {
21
+ throw new E_POLARIS_INVALID_REQUEST([
22
+ "`scope` was given, but the config declares no `budget`",
23
+ ]);
24
+ }
25
+ let reservation;
26
+ if (limit !== undefined || input.maxCost !== undefined) {
27
+ // A budget cannot be kept without knowing what the model costs: refuse
28
+ // rather than let the call through unmetered.
29
+ if (price === undefined) {
30
+ throw new E_POLARIS_UNKNOWN_PRICE([input.model ?? "(no model)"]);
31
+ }
32
+ const estimate = input.estimate(price);
33
+ if (input.maxCost !== undefined && exceeds(estimate, input.maxCost)) {
34
+ throw new E_POLARIS_BUDGET_EXCEEDED(["this request", input.maxCost]);
35
+ }
36
+ if (input.scope !== undefined && config.budget !== undefined) {
37
+ reservation = await reserve(config.budget.store, input.scope, limit, estimate);
38
+ }
39
+ }
40
+ const entry = (status) => ({
41
+ provider: input.provider,
42
+ model: input.model,
43
+ operation: input.operation,
44
+ status,
45
+ durationMs: Date.now() - started,
46
+ ...(input.scope !== undefined ? { scope: input.scope } : {}),
47
+ });
48
+ return {
49
+ async finish(outcome) {
50
+ const answered = priceFor(config.pricing, outcome.model) ?? price;
51
+ const amount = answered ? costOf(answered, outcome.usage) : undefined;
52
+ const cost = amount !== undefined && config.pricing !== undefined
53
+ ? { amount, currency: config.pricing.currency }
54
+ : undefined;
55
+ // Without a price for the model that answered, the estimate stays.
56
+ if (reservation !== undefined && amount !== undefined) {
57
+ await reservation.settle(amount);
58
+ }
59
+ await writeLog(config.log, {
60
+ ...entry("ok"),
61
+ model: outcome.model,
62
+ usage: outcome.usage,
63
+ ...(cost !== undefined ? { cost } : {}),
64
+ ...(outcome.responseId !== undefined
65
+ ? { responseId: outcome.responseId }
66
+ : {}),
67
+ ...(outcome.stopReason !== undefined
68
+ ? { stopReason: outcome.stopReason }
69
+ : {}),
70
+ }, { request: input.request, response: outcome.response });
71
+ return cost;
72
+ },
73
+ async fail(error, options) {
74
+ if (reservation !== undefined && !options.spent)
75
+ await reservation.release();
76
+ await writeLog(config.log, {
77
+ ...entry(options.cancelled ? "cancelled" : "error"),
78
+ errorCode: codeOf(error),
79
+ }, { request: input.request });
80
+ },
81
+ };
82
+ }
83
+ /** Two costs added up, exactly, when both are known and in one currency. */
84
+ export function addCost(a, b) {
85
+ if (a === undefined)
86
+ return b;
87
+ if (b === undefined || a.currency !== b.currency)
88
+ return a;
89
+ return { amount: add(a.amount, b.amount), currency: a.currency };
90
+ }
91
+ //# sourceMappingURL=meter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"meter.js","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAGN,OAAO,EAEP,OAAO,GACP,MAAM,aAAa,CAAC;AACrB,OAAO,EACN,GAAG,EAEH,MAAM,EACN,OAAO,EAGP,QAAQ,GACR,MAAM,WAAW,CAAC;AACnB,OAAO,EACN,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,GACvB,MAAM,aAAa,CAAC;AACrB,OAAO,EACN,MAAM,EAGN,QAAQ,GACR,MAAM,UAAU,CAAC;AAoDlB,+FAA+F;AAC/F,MAAM,CAAC,KAAK,UAAU,UAAU,CAC/B,MAAmB,EACnB,KAAiB;IAEjB,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC3B,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IACpD,MAAM,KAAK,GACV,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS;QACvD,CAAC,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC;QAC3C,CAAC,CAAC,SAAS,CAAC;IACd,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;QAC9D,MAAM,IAAI,yBAAyB,CAAC;YACnC,wDAAwD;SACxD,CAAC,CAAC;IACJ,CAAC;IAED,IAAI,WAAoC,CAAC;IACzC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QACxD,uEAAuE;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AC,KAAK;gBAC9C,MAAM,WAAW,CAAC,OAAO,EAAE,CAAC;YAC7B,MAAM,QAAQ,CACb,MAAM,CAAC,GAAG,EACV;gBACC,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC;gBACnD,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC;aACxB,EACD,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAC1B,CAAC;QACH,CAAC;KACD,CAAC;AACH,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,OAAO,CACtB,CAAmB,EACnB,CAAmB;IAEnB,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IAC9B,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,CAAC,CAAC,QAAQ;QAAE,OAAO,CAAC,CAAC;IAC3D,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;AAClE,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * One helper per provider polaris ships, as @adonisjs/mail's `transports`:
3
+ *
4
+ * defineConfig({
5
+ * default: 'claude',
6
+ * providers: {
7
+ * claude: providers.anthropic({ model: 'claude-opus-5-5' }),
8
+ * local: providers.openaiCompatible({ baseURL: 'http://localhost:11434/v1', model: 'qwen3' }),
9
+ * },
10
+ * })
11
+ *
12
+ * A helper returns a factory; the driver is built, and its SDK loaded, the
13
+ * first time the provider is used.
14
+ */
15
+ import { anthropic } from "./drivers/anthropic.js";
16
+ import { openaiCompatible } from "./drivers/openai-compatible.js";
17
+ export declare const providers: {
18
+ anthropic: typeof anthropic;
19
+ openaiCompatible: typeof openaiCompatible;
20
+ };
21
+ export type { AnthropicConfig } from "./drivers/anthropic.js";
22
+ export type { OpenAICompatibleConfig } from "./drivers/openai-compatible.js";
23
+ //# sourceMappingURL=providers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAElE,eAAO,MAAM,SAAS;;;CAGrB,CAAC;AAEF,YAAY,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,YAAY,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * One helper per provider polaris ships, as @adonisjs/mail's `transports`:
3
+ *
4
+ * defineConfig({
5
+ * default: 'claude',
6
+ * providers: {
7
+ * claude: providers.anthropic({ model: 'claude-opus-5-5' }),
8
+ * local: providers.openaiCompatible({ baseURL: 'http://localhost:11434/v1', model: 'qwen3' }),
9
+ * },
10
+ * })
11
+ *
12
+ * A helper returns a factory; the driver is built, and its SDK loaded, the
13
+ * first time the provider is used.
14
+ */
15
+ import { anthropic } from "./drivers/anthropic.js";
16
+ import { openaiCompatible } from "./drivers/openai-compatible.js";
17
+ export const providers = {
18
+ anthropic,
19
+ openaiCompatible,
20
+ };
21
+ //# sourceMappingURL=providers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.js","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAElE,MAAM,CAAC,MAAM,SAAS,GAAG;IACxB,SAAS;IACT,gBAAgB;CAChB,CAAC"}
@@ -0,0 +1,11 @@
1
+ /** Replace every occurrence of each secret in `text`. */
2
+ export declare function redact(text: string, secrets: readonly string[]): string;
3
+ /**
4
+ * The error to raise for a failed call that was given `secrets`.
5
+ *
6
+ * An error that holds none of them is returned as it is. One that holds any is
7
+ * replaced by `E_POLARIS_PROVIDER_FAILED`, whose message is the original one
8
+ * with the secrets taken out, and which keeps no reference to the original.
9
+ */
10
+ export declare function scrub(error: unknown, provider: string, secrets: readonly (string | undefined)[]): unknown;
11
+ //# sourceMappingURL=redact.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAOA,yDAAyD;AACzD,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAIvE;AA+CD;;;;;;GAMG;AACH,wBAAgB,KAAK,CACpB,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,SAAS,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE,GACtC,OAAO,CAOT"}
package/dist/redact.js ADDED
@@ -0,0 +1,76 @@
1
+ import { E_POLARIS_PROVIDER_FAILED } from "./errors.js";
2
+ const REDACTED = "[redacted]";
3
+ /** How many values the scan visits before it assumes the worst. */
4
+ const MAX_VISITS = 2_000;
5
+ const MAX_DEPTH = 8;
6
+ /** Replace every occurrence of each secret in `text`. */
7
+ export function redact(text, secrets) {
8
+ let out = text;
9
+ for (const secret of secrets)
10
+ out = out.split(secret).join(REDACTED);
11
+ return out;
12
+ }
13
+ /**
14
+ * Whether `value` holds one of `secrets` anywhere a logger or an error report
15
+ * could reach: its message, stack, cause, own properties, nested objects,
16
+ * arrays and `Headers`.
17
+ *
18
+ * A value too large or too deep to scan in full counts as holding one: the
19
+ * error it came from is then replaced, which loses detail but leaks nothing.
20
+ */
21
+ function holdsSecret(value, secrets) {
22
+ const seen = new Set();
23
+ let visits = 0;
24
+ const visit = (current, depth) => {
25
+ visits += 1;
26
+ if (visits > MAX_VISITS || depth > MAX_DEPTH)
27
+ return true;
28
+ if (typeof current === "string") {
29
+ return secrets.some((secret) => current.includes(secret));
30
+ }
31
+ if (typeof current !== "object" || current === null)
32
+ return false;
33
+ if (seen.has(current))
34
+ return false;
35
+ seen.add(current);
36
+ if (current instanceof Headers) {
37
+ for (const [, header] of current) {
38
+ if (visit(header, depth + 1))
39
+ return true;
40
+ }
41
+ return false;
42
+ }
43
+ if (current instanceof Error) {
44
+ if (visit(current.message, depth + 1))
45
+ return true;
46
+ if (visit(current.stack, depth + 1))
47
+ return true;
48
+ if (visit(current.cause, depth + 1))
49
+ return true;
50
+ }
51
+ if (Array.isArray(current)) {
52
+ return current.some((item) => visit(item, depth + 1));
53
+ }
54
+ for (const key of Object.keys(current)) {
55
+ if (visit(Reflect.get(current, key), depth + 1))
56
+ return true;
57
+ }
58
+ return false;
59
+ };
60
+ return visit(value, 0);
61
+ }
62
+ /**
63
+ * The error to raise for a failed call that was given `secrets`.
64
+ *
65
+ * An error that holds none of them is returned as it is. One that holds any is
66
+ * replaced by `E_POLARIS_PROVIDER_FAILED`, whose message is the original one
67
+ * with the secrets taken out, and which keeps no reference to the original.
68
+ */
69
+ export function scrub(error, provider, secrets) {
70
+ const live = secrets.filter((secret) => typeof secret === "string" && secret !== "");
71
+ if (live.length === 0 || !holdsSecret(error, live))
72
+ return error;
73
+ const message = error instanceof Error ? error.message : String(error);
74
+ return new E_POLARIS_PROVIDER_FAILED([provider, redact(message, live)]);
75
+ }
76
+ //# sourceMappingURL=redact.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redact.js","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAExD,MAAM,QAAQ,GAAG,YAAY,CAAC;AAC9B,mEAAmE;AACnE,MAAM,UAAU,GAAG,KAAK,CAAC;AACzB,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB,yDAAyD;AACzD,MAAM,UAAU,MAAM,CAAC,IAAY,EAAE,OAA0B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,CAAC;IACvE,OAAO,IAAI,yBAAyB,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AACzE,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { AssistantPart, ChatResponse, StopReason, Usage } from "./types.js";
2
+ export interface ResponseInput {
3
+ readonly id: string;
4
+ readonly provider: string;
5
+ readonly model: string;
6
+ readonly content: readonly AssistantPart[];
7
+ readonly stopReason: StopReason;
8
+ readonly usage?: Partial<Usage>;
9
+ }
10
+ /** Usage of nothing yet: what a sum of usages starts from. */
11
+ export declare const NO_USAGE: Usage;
12
+ /** Two usages added up: the cost of a run or of several attempts. */
13
+ export declare function addUsage(a: Usage, b: Usage): Usage;
14
+ /**
15
+ * Build a {@link ChatResponse} from what a provider returned: `text` and
16
+ * `toolCalls` are derived from `content`, and missing usage counts are 0.
17
+ * Drivers build their responses with it, so the derived fields cannot drift
18
+ * from the content they summarize.
19
+ */
20
+ export declare function createResponse(input: ResponseInput): ChatResponse;
21
+ //# sourceMappingURL=response.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,aAAa,EACb,YAAY,EACZ,UAAU,EAEV,KAAK,EACL,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE,CAAC;IAC3C,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;CAChC;AAED,8DAA8D;AAC9D,eAAO,MAAM,QAAQ,EAAE,KAKtB,CAAC;AAEF,qEAAqE;AACrE,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG,KAAK,CAOlD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,GAAG,YAAY,CAuBjE"}
@@ -0,0 +1,45 @@
1
+ /** Usage of nothing yet: what a sum of usages starts from. */
2
+ export const NO_USAGE = {
3
+ inputTokens: 0,
4
+ outputTokens: 0,
5
+ cacheReadTokens: 0,
6
+ cacheWriteTokens: 0,
7
+ };
8
+ /** Two usages added up: the cost of a run or of several attempts. */
9
+ export function addUsage(a, b) {
10
+ return {
11
+ inputTokens: a.inputTokens + b.inputTokens,
12
+ outputTokens: a.outputTokens + b.outputTokens,
13
+ cacheReadTokens: a.cacheReadTokens + b.cacheReadTokens,
14
+ cacheWriteTokens: a.cacheWriteTokens + b.cacheWriteTokens,
15
+ };
16
+ }
17
+ /**
18
+ * Build a {@link ChatResponse} from what a provider returned: `text` and
19
+ * `toolCalls` are derived from `content`, and missing usage counts are 0.
20
+ * Drivers build their responses with it, so the derived fields cannot drift
21
+ * from the content they summarize.
22
+ */
23
+ export function createResponse(input) {
24
+ const text = input.content
25
+ .filter((part) => part.type === "text")
26
+ .map((part) => part.text)
27
+ .join("");
28
+ const toolCalls = input.content.filter((part) => part.type === "tool_call");
29
+ return {
30
+ id: input.id,
31
+ provider: input.provider,
32
+ model: input.model,
33
+ message: { role: "assistant", content: input.content },
34
+ text,
35
+ toolCalls,
36
+ stopReason: input.stopReason,
37
+ usage: {
38
+ inputTokens: input.usage?.inputTokens ?? 0,
39
+ outputTokens: input.usage?.outputTokens ?? 0,
40
+ cacheReadTokens: input.usage?.cacheReadTokens ?? 0,
41
+ cacheWriteTokens: input.usage?.cacheWriteTokens ?? 0,
42
+ },
43
+ };
44
+ }
45
+ //# sourceMappingURL=response.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":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AAW,EAAE,KAAK,CAAC,KAAK,EAAE,WAAW,IAAI,CAAC;YAC1C,YAAY,EAAE,KAAK,CAAC,KAAK,EAAE,YAAY,IAAI,CAAC;YAC5C,eAAe,EAAE,KAAK,CAAC,KAAK,EAAE,eAAe,IAAI,CAAC;YAClD,gBAAgB,EAAE,KAAK,CAAC,KAAK,EAAE,gBAAgB,IAAI,CAAC;SACpD;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * The schemas polaris takes, for a tool's input and for a structured answer:
3
+ * anything that implements Standard Schema v1 (validation) and Standard JSON
4
+ * Schema v1 (its JSON Schema), as rune's validators do. polaris imports no
5
+ * validation library.
6
+ */
7
+ import type { JsonSchema } from "./types.js";
8
+ /** One problem a schema found in a value. */
9
+ export interface SchemaIssue {
10
+ readonly message: string;
11
+ readonly path?: readonly unknown[];
12
+ }
13
+ /** What a Standard Schema's `validate` answers. */
14
+ export type SchemaResult<Output> = {
15
+ readonly value: Output;
16
+ readonly issues?: undefined;
17
+ } | {
18
+ readonly issues: readonly SchemaIssue[];
19
+ };
20
+ /** The slice of Standard Schema v1 and Standard JSON Schema v1 polaris uses. */
21
+ export interface StandardSchema<Output = unknown> {
22
+ readonly "~standard": {
23
+ readonly version: 1;
24
+ readonly vendor: string;
25
+ validate(value: unknown): SchemaResult<Output> | Promise<SchemaResult<Output>>;
26
+ readonly jsonSchema: {
27
+ input(options?: {
28
+ readonly target?: string;
29
+ }): Record<string, unknown>;
30
+ };
31
+ };
32
+ }
33
+ /** The value a schema hands back once it accepted the input. */
34
+ export type SchemaOutput<S extends StandardSchema> = Extract<Awaited<ReturnType<S["~standard"]["validate"]>>, {
35
+ readonly value: unknown;
36
+ }>["value"];
37
+ /**
38
+ * The JSON Schema of what `schema` accepts, checked to describe an object
39
+ * (what providers take for a tool's input and a structured answer), without
40
+ * its `$schema` key, which some providers refuse.
41
+ */
42
+ export declare function objectJsonSchema(schema: StandardSchema, label: string): JsonSchema;
43
+ /** `canton: too short; age: not a number`: issues as one line a model can act on. */
44
+ export declare function describeIssues(issues: readonly SchemaIssue[]): string;
45
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,6CAA6C;AAC7C,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,OAAO,EAAE,CAAC;CACnC;AAED,mDAAmD;AACnD,MAAM,MAAM,YAAY,CAAC,MAAM,IAC5B;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAA;CAAE,GACvD;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,WAAW,EAAE,CAAA;CAAE,CAAC;AAE/C,gFAAgF;AAChF,MAAM,WAAW,cAAc,CAAC,MAAM,GAAG,OAAO;IAC/C,QAAQ,CAAC,WAAW,EAAE;QACrB,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CACP,KAAK,EAAE,OAAO,GACZ,YAAY,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;QACxD,QAAQ,CAAC,UAAU,EAAE;YACpB,KAAK,CAAC,OAAO,CAAC,EAAE;gBAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;aAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;SACvE,CAAC;KACF,CAAC;CACF;AAED,gEAAgE;AAChE,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,cAAc,IAAI,OAAO,CAC3D,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAC/C;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAC3B,CAAC,OAAO,CAAC,CAAC;AAEX;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,cAAc,EACtB,KAAK,EAAE,MAAM,GACX,UAAU,CAsBZ;AAED,qFAAqF;AACrF,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAarE"}
package/dist/schema.js ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The schemas polaris takes, for a tool's input and for a structured answer:
3
+ * anything that implements Standard Schema v1 (validation) and Standard JSON
4
+ * Schema v1 (its JSON Schema), as rune's validators do. polaris imports no
5
+ * validation library.
6
+ */
7
+ import { E_POLARIS_INVALID_REQUEST } from "./errors.js";
8
+ /**
9
+ * The JSON Schema of what `schema` accepts, checked to describe an object
10
+ * (what providers take for a tool's input and a structured answer), without
11
+ * its `$schema` key, which some providers refuse.
12
+ */
13
+ export function objectJsonSchema(schema, label) {
14
+ const standard = schema["~standard"];
15
+ if (standard?.version !== 1 ||
16
+ typeof standard.validate !== "function" ||
17
+ typeof standard.jsonSchema?.input !== "function") {
18
+ throw new E_POLARIS_INVALID_REQUEST([
19
+ `the schema of ${label} must implement Standard Schema v1 and Standard JSON Schema v1 (a rune schema does)`,
20
+ ]);
21
+ }
22
+ const jsonSchema = Object.fromEntries(Object.entries(standard.jsonSchema.input()).filter(([key]) => key !== "$schema"));
23
+ if (jsonSchema.type !== "object") {
24
+ throw new E_POLARIS_INVALID_REQUEST([
25
+ `the schema of ${label} must describe an object`,
26
+ ]);
27
+ }
28
+ return jsonSchema;
29
+ }
30
+ /** `canton: too short; age: not a number`: issues as one line a model can act on. */
31
+ export function describeIssues(issues) {
32
+ return issues
33
+ .map((issue) => {
34
+ const path = (issue.path ?? [])
35
+ .map((segment) => typeof segment === "object" && segment !== null && "key" in segment
36
+ ? String(segment.key)
37
+ : String(segment))
38
+ .join(".");
39
+ return path === "" ? issue.message : `${path}: ${issue.message}`;
40
+ })
41
+ .join("; ");
42
+ }
43
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAkCxD;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC/B,MAAsB,EACtB,KAAa;IAEb,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IACrC,IACC,QAAQ,EAAE,OAAO,KAAK,CAAC;QACvB,OAAO,QAAQ,CAAC,QAAQ,KAAK,UAAU;QACvC,OAAO,QAAQ,CAAC,UAAU,EAAE,KAAK,KAAK,UAAU,EAC/C,CAAC;QACF,MAAM,IAAI,yBAAyB,CAAC;YACnC,iBAAiB,KAAK,qFAAqF;SAC3G,CAAC,CAAC;IACJ,CAAC;IACD,MAAM,UAAU,GAAe,MAAM,CAAC,WAAW,CAChD,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CACjD,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,SAAS,CAC5B,CACD,CAAC;IACF,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAClC,MAAM,IAAI,yBAAyB,CAAC;YACnC,iBAAiB,KAAK,0BAA0B;SAChD,CAAC,CAAC;IACJ,CAAC;IACD,OAAO,UAAU,CAAC;AACnB,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,cAAc,CAAC,MAA8B;IAC5D,OAAO,MAAM;SACX,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACd,MAAM,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC;aAC7B,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAChB,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,IAAI,KAAK,IAAI,OAAO;YAClE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;YACrB,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAClB;aACA,IAAI,CAAC,GAAG,CAAC,CAAC;QACZ,OAAO,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;IAClE,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;AACd,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The application's polaris manager, ready to import:
3
+ *
4
+ * import polaris from '@c9up/polaris/services/main'
5
+ *
6
+ * const reply = await polaris.use('claude').chat({ messages })
7
+ *
8
+ * Bound by `PolarisProvider.boot()`.
9
+ */
10
+ import type { PolarisService } from "../Polaris.js";
11
+ /** @internal Bind the singleton (called by PolarisProvider). */
12
+ export declare function setPolaris(value: PolarisService): void;
13
+ /** @internal Read the singleton, or `undefined` before boot. */
14
+ export declare function getPolaris(): PolarisService | undefined;
15
+ /**
16
+ * @internal Release the singleton, so a stopped application leaves no manager
17
+ * reachable here. The caller checks it still owns it: two applications share
18
+ * this module in one process.
19
+ */
20
+ export declare function clearPolaris(): void;
21
+ declare const polaris: PolarisService;
22
+ export default polaris;
23
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/services/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAIpD,gEAAgE;AAChE,wBAAgB,UAAU,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,CAEtD;AAED,gEAAgE;AAChE,wBAAgB,UAAU,IAAI,cAAc,GAAG,SAAS,CAEvD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,IAAI,CAEnC;AAKD,QAAA,MAAM,OAAO,EAAE,cASb,CAAC;AAEH,eAAe,OAAO,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The application's polaris manager, ready to import:
3
+ *
4
+ * import polaris from '@c9up/polaris/services/main'
5
+ *
6
+ * const reply = await polaris.use('claude').chat({ messages })
7
+ *
8
+ * Bound by `PolarisProvider.boot()`.
9
+ */
10
+ import { E_POLARIS_NOT_BOOTED } from "../errors.js";
11
+ let instance;
12
+ /** @internal Bind the singleton (called by PolarisProvider). */
13
+ export function setPolaris(value) {
14
+ instance = value;
15
+ }
16
+ /** @internal Read the singleton, or `undefined` before boot. */
17
+ export function getPolaris() {
18
+ return instance;
19
+ }
20
+ /**
21
+ * @internal Release the singleton, so a stopped application leaves no manager
22
+ * reachable here. The caller checks it still owns it: two applications share
23
+ * this module in one process.
24
+ */
25
+ export function clearPolaris() {
26
+ instance = undefined;
27
+ }
28
+ // Never read through: every trap forwards to `instance`.
29
+ const target = Object.create(null);
30
+ const polaris = new Proxy(target, {
31
+ get(_target, prop) {
32
+ // A module loader reads `then` and a few symbols on what it imports,
33
+ // before anyone uses it. Throwing there would crash the import itself.
34
+ if (typeof prop === "symbol" || prop === "then")
35
+ return undefined;
36
+ if (!instance)
37
+ throw new E_POLARIS_NOT_BOOTED();
38
+ const value = Reflect.get(instance, prop, instance);
39
+ return typeof value === "function" ? value.bind(instance) : value;
40
+ },
41
+ });
42
+ export default polaris;
43
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/services/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAGpD,IAAI,QAAoC,CAAC;AAEzC,gEAAgE;AAChE,MAAM,UAAU,UAAU,CAAC,KAAqB;IAC/C,QAAQ,GAAG,KAAK,CAAC;AAClB,CAAC;AAED,gEAAgE;AAChE,MAAM,UAAU,UAAU;IACzB,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY;IAC3B,QAAQ,GAAG,SAAS,CAAC;AACtB,CAAC;AAED,yDAAyD;AACzD,MAAM,MAAM,GAAmB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAEnD,MAAM,OAAO,GAAmB,IAAI,KAAK,CAAC,MAAM,EAAE;IACjD,GAAG,CAAC,OAAO,EAAE,IAAI;QAChB,qEAAqE;QACrE,uEAAuE;QACvE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,MAAM;YAAE,OAAO,SAAS,CAAC;QAClE,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,oBAAoB,EAAE,CAAC;QAChD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QACpD,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACnE,CAAC;CACD,CAAC,CAAC;AAEH,eAAe,OAAO,CAAC"}
package/dist/sse.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * A reply streamed to the browser as Server-Sent Events, over the response of
3
+ * the request that asked for it (ream's `ctx.response.sse()`).
4
+ *
5
+ * That stream, not a relay channel, is the transport here: it belongs to the
6
+ * one client waiting for this reply, and its close is a sure sign the client
7
+ * left, on every instance, which is what lets the provider request be
8
+ * cancelled. A relay channel reaches whoever subscribed, wherever they are,
9
+ * but cannot tell polaris that nobody listens any more.
10
+ */
11
+ import type { ChatResponse, StreamEvent, Usage } from "./types.js";
12
+ /**
13
+ * What polaris needs of an SSE stream: ream's `SseStream` fits as it is, and
14
+ * so does relay's.
15
+ */
16
+ export interface SseSink {
17
+ /** Send one named event. Answers `false` once the stream is closed. */
18
+ send(event: string, data: unknown): Promise<boolean> | boolean;
19
+ /** Called once when the stream closes, whoever closed it. */
20
+ onClose(callback: () => void): void;
21
+ /** Close the stream from the server. */
22
+ end(): Promise<void> | void;
23
+ }
24
+ /** How a streamed reply ended. */
25
+ export type StreamToResult = {
26
+ readonly status: "done";
27
+ readonly response: ChatResponse;
28
+ }
29
+ /** The client went away; the provider request was cancelled. */
30
+ | {
31
+ readonly status: "client_left";
32
+ }
33
+ /** The call failed; the client was sent an `error` event with its code. */
34
+ | {
35
+ readonly status: "failed";
36
+ readonly error: unknown;
37
+ };
38
+ /** The `done` event's data: what a page needs, none of the provider's raw blocks. */
39
+ export interface DoneEventData {
40
+ readonly id: string;
41
+ readonly provider: string;
42
+ readonly model: string;
43
+ readonly text: string;
44
+ readonly stopReason: ChatResponse["stopReason"];
45
+ readonly usage: Usage;
46
+ }
47
+ /** Why polaris cancelled the provider request: the client closed the stream. */
48
+ export declare class ClientLeft extends Error {
49
+ constructor();
50
+ }
51
+ /**
52
+ * Forward `events` to `sink` as named events (`text`, `reasoning`,
53
+ * `tool_call`, `done`, `error`, after `prefix`), and end the stream when the
54
+ * reply is complete. `leave` is aborted when the client closes the stream.
55
+ * Never rejects: every outcome is in the result.
56
+ */
57
+ export declare function pipeToSse(events: AsyncIterable<StreamEvent>, sink: SseSink, leave: AbortController, prefix: string): Promise<StreamToResult>;
58
+ //# sourceMappingURL=sse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnE;;;GAGG;AACH,MAAM,WAAW,OAAO;IACvB,uEAAuE;IACvE,IAAI,CAAC,KAAK,EAAE,MAAM,EA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