@swissspidy/belay-core 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.
- package/LICENSE +201 -0
- package/README.md +47 -2
- package/dist/calibration.d.ts +11 -0
- package/dist/calibration.d.ts.map +1 -0
- package/dist/calibration.js +68 -0
- package/dist/calibration.js.map +1 -0
- package/dist/cloud.d.ts +37 -0
- package/dist/cloud.d.ts.map +1 -0
- package/dist/cloud.js +43 -0
- package/dist/cloud.js.map +1 -0
- package/dist/confidence.d.ts +22 -0
- package/dist/confidence.d.ts.map +1 -0
- package/dist/confidence.js +30 -0
- package/dist/confidence.js.map +1 -0
- package/dist/cost.d.ts +60 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +99 -0
- package/dist/cost.js.map +1 -0
- package/dist/errors.d.ts +24 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +16 -0
- package/dist/errors.js.map +1 -0
- package/dist/hash.d.ts +5 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +19 -0
- package/dist/hash.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/schema.d.ts +33 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +174 -0
- package/dist/schema.js.map +1 -0
- package/dist/task.d.ts +7 -0
- package/dist/task.d.ts.map +1 -0
- package/dist/task.js +288 -0
- package/dist/task.js.map +1 -0
- package/dist/types.d.ts +396 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -4
- package/src/calibration.ts +65 -0
- package/src/cloud.ts +72 -0
- package/src/confidence.ts +37 -0
- package/src/cost.ts +148 -0
- package/src/errors.ts +28 -0
- package/src/hash.ts +21 -0
- package/src/index.ts +23 -0
- package/src/schema.ts +170 -0
- package/src/task.ts +328 -0
- package/src/types.ts +417 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @swissspidy/belay-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Answer AI tasks with a small on-device model, and call a cloud model only for the inputs it is
|
|
4
|
+
unsure about. `@swissspidy/belay-core` has the task, the cascade, confidence handling, calibration files,
|
|
5
|
+
cloud adapters and savings telemetry. It has no runtime dependencies.
|
|
6
|
+
|
|
7
|
+
Pair it with [`@swissspidy/belay-web`](https://www.npmjs.com/package/@swissspidy/belay-web) for the on-device runners
|
|
8
|
+
(Chrome's Classifier and Prompt APIs), and calibrate the threshold on your own data with
|
|
9
|
+
[`@swissspidy/belay-calibrate`](https://www.npmjs.com/package/@swissspidy/belay-calibrate).
|
|
10
|
+
|
|
11
|
+
> **Status:** experimental (0.x). The API may change between minor versions.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @swissspidy/belay-core @swissspidy/belay-web
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { task, fetchAdapter } from '@swissspidy/belay-core';
|
|
23
|
+
import { classifierApi } from '@swissspidy/belay-web';
|
|
24
|
+
|
|
25
|
+
const triage = task({
|
|
26
|
+
name: 'ticket-triage',
|
|
27
|
+
schema: { type: 'categorical', options: ['bug', 'billing', 'feature', 'other'] as const },
|
|
28
|
+
local: classifierApi(),
|
|
29
|
+
cloud: fetchAdapter({ url: '/api/belay' }), // your backend calls the LLM
|
|
30
|
+
threshold: 0.8, // or: calibration: '/belay.calibration.json'
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const result = await triage.run(ticketText, { signal });
|
|
34
|
+
// { value: 'billing', confidence: 0.93, source: 'local', escalationReason: null, ... }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The local answer is accepted if and only if `confidence >= threshold`. Otherwise the task
|
|
38
|
+
escalates to the cloud, subject to your privacy policy (`redact`, `consent`, or `never`).
|
|
39
|
+
|
|
40
|
+
## Documentation
|
|
41
|
+
|
|
42
|
+
See the [project README](https://github.com/swissspidy/belay#readme) for schemas, confidence,
|
|
43
|
+
privacy, cloud adapters, calibration and telemetry, and the
|
|
44
|
+
[ADRs](https://github.com/swissspidy/belay/tree/main/docs/adr) for the design decisions.
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
Apache-2.0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CalibrationFile } from './types.js';
|
|
2
|
+
export declare const CALIBRATION_VERSION = 1;
|
|
3
|
+
/**
|
|
4
|
+
* Validates an unknown value as a v1 calibration file. When `task` is given, also checks
|
|
5
|
+
* that the file belongs to that task. Throws `BelayError('invalid-calibration')`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseCalibration(data: unknown, task?: string): CalibrationFile;
|
|
8
|
+
/** Threshold for a local top label: the per-label override if present, else the global one. */
|
|
9
|
+
export declare function thresholdFor(calibration: Pick<CalibrationFile, 'threshold' | 'thresholds'>, label?: string): number;
|
|
10
|
+
export declare function loadCalibration(source: CalibrationFile | string | URL | (() => Promise<CalibrationFile>), task?: string): Promise<CalibrationFile>;
|
|
11
|
+
//# sourceMappingURL=calibration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calibration.d.ts","sourceRoot":"","sources":["../src/calibration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAOrC;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,eAAe,CA2B9E;AAED,+FAA+F;AAC/F,wBAAgB,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC,eAAe,EAAE,WAAW,GAAG,YAAY,CAAC,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAKnH;AAED,wBAAsB,eAAe,CACnC,MAAM,EAAE,eAAe,GAAG,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,OAAO,CAAC,eAAe,CAAC,CAAC,EACzE,IAAI,CAAC,EAAE,MAAM,GACZ,OAAO,CAAC,eAAe,CAAC,CAU1B"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { BelayError } from './errors.js';
|
|
2
|
+
export const CALIBRATION_VERSION = 1;
|
|
3
|
+
const isNum = (v) => typeof v === 'number' && Number.isFinite(v);
|
|
4
|
+
const isProb = (v) => isNum(v) && v >= 0 && v <= 1;
|
|
5
|
+
/** A threshold above 1 keeps nothing local: every run escalates. */
|
|
6
|
+
const isThreshold = (v) => isNum(v) && v >= 0;
|
|
7
|
+
/**
|
|
8
|
+
* Validates an unknown value as a v1 calibration file. When `task` is given, also checks
|
|
9
|
+
* that the file belongs to that task. Throws `BelayError('invalid-calibration')`.
|
|
10
|
+
*/
|
|
11
|
+
export function parseCalibration(data, task) {
|
|
12
|
+
const fail = (msg) => {
|
|
13
|
+
throw new BelayError('invalid-calibration', `Invalid calibration file: ${msg}`);
|
|
14
|
+
};
|
|
15
|
+
if (!data || typeof data !== 'object')
|
|
16
|
+
fail('not an object');
|
|
17
|
+
const c = data;
|
|
18
|
+
if (c.version !== CALIBRATION_VERSION)
|
|
19
|
+
fail(`unsupported version ${String(c.version)}`);
|
|
20
|
+
if (typeof c.task !== 'string')
|
|
21
|
+
fail('missing task');
|
|
22
|
+
if (task !== undefined && c.task !== task)
|
|
23
|
+
fail(`file is for task "${c.task}", not "${task}"`);
|
|
24
|
+
if (typeof c.schemaFingerprint !== 'string')
|
|
25
|
+
fail('missing schemaFingerprint');
|
|
26
|
+
if (!isThreshold(c.threshold))
|
|
27
|
+
fail('threshold must be a number ≥ 0');
|
|
28
|
+
if (c.thresholds !== undefined) {
|
|
29
|
+
if (typeof c.thresholds !== 'object' || c.thresholds === null)
|
|
30
|
+
fail('thresholds must be an object');
|
|
31
|
+
for (const [label, t] of Object.entries(c.thresholds)) {
|
|
32
|
+
if (!isThreshold(t))
|
|
33
|
+
fail(`thresholds["${label}"] must be a number ≥ 0`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (!c.expected || !isProb(c.expected.localShare) || !isProb(c.expected.accuracy)) {
|
|
37
|
+
fail('expected.accuracy and expected.localShare must be numbers in [0, 1]');
|
|
38
|
+
}
|
|
39
|
+
if (!Array.isArray(c.curve))
|
|
40
|
+
fail('curve must be an array');
|
|
41
|
+
if (!c.confidenceHistogram || !Array.isArray(c.confidenceHistogram.edges) || !Array.isArray(c.confidenceHistogram.counts)) {
|
|
42
|
+
fail('confidenceHistogram must have edges and counts');
|
|
43
|
+
}
|
|
44
|
+
else if (c.confidenceHistogram.edges.length !== c.confidenceHistogram.counts.length + 1) {
|
|
45
|
+
fail('confidenceHistogram.edges must have one more entry than counts');
|
|
46
|
+
}
|
|
47
|
+
return c;
|
|
48
|
+
}
|
|
49
|
+
/** Threshold for a local top label: the per-label override if present, else the global one. */
|
|
50
|
+
export function thresholdFor(calibration, label) {
|
|
51
|
+
if (label !== undefined && calibration.thresholds && Object.hasOwn(calibration.thresholds, label)) {
|
|
52
|
+
return calibration.thresholds[label];
|
|
53
|
+
}
|
|
54
|
+
return calibration.threshold;
|
|
55
|
+
}
|
|
56
|
+
export async function loadCalibration(source, task) {
|
|
57
|
+
if (typeof source === 'function')
|
|
58
|
+
return parseCalibration(await source(), task);
|
|
59
|
+
if (typeof source === 'string' || source instanceof URL) {
|
|
60
|
+
const res = await fetch(source);
|
|
61
|
+
if (!res.ok) {
|
|
62
|
+
throw new BelayError('invalid-calibration', `Failed to fetch calibration ${String(source)}: HTTP ${res.status}`);
|
|
63
|
+
}
|
|
64
|
+
return parseCalibration(await res.json(), task);
|
|
65
|
+
}
|
|
66
|
+
return parseCalibration(source, task);
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=calibration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calibration.js","sourceRoot":"","sources":["../src/calibration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC,MAAM,KAAK,GAAG,CAAC,CAAU,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AACvF,MAAM,MAAM,GAAG,CAAC,CAAU,EAAe,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACzE,oEAAoE;AACpE,MAAM,WAAW,GAAG,CAAC,CAAU,EAAe,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEpE;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAa,EAAE,IAAa;IAC3D,MAAM,IAAI,GAAG,CAAC,GAAW,EAAS,EAAE;QAClC,MAAM,IAAI,UAAU,CAAC,qBAAqB,EAAE,6BAA6B,GAAG,EAAE,CAAC,CAAC;IAClF,CAAC,CAAC;IACF,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,IAAI,CAAC,eAAe,CAAC,CAAC;IAC7D,MAAM,CAAC,GAAG,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,+BAA+B,MAAM,CAAC,MAAM,CAAC,UAAU,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QACnH,CAAC;QACD,OAAO,gBAAgB,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AACxC,CAAC"}
|
package/dist/cloud.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CloudRequest, CloudRunner, CloudUsage, TaskSchema } from './types.js';
|
|
2
|
+
export type CloudFn<S extends TaskSchema = TaskSchema> = (request: CloudRequest<S>, options: {
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
/** Report the call's token usage, so calibration and `savingsMeter()` can measure its cost. */
|
|
5
|
+
reportUsage: (usage: CloudUsage | CloudUsage[]) => void;
|
|
6
|
+
}) => Promise<unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Wraps a plain async function as a cloud runner. The function returns the raw output:
|
|
9
|
+
* the bare value, the `{ value }` wrapper, or a JSON string of either. The task validates it.
|
|
10
|
+
* Token usage goes through `reportUsage`, since the output itself may be a `{ value }` object.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* cloudAdapter(async ({ instruction, input, jsonSchema }, { signal }) => {
|
|
14
|
+
* const res = await fetch('/api/classify', { method: 'POST', body: JSON.stringify({ instruction, input, jsonSchema }), signal });
|
|
15
|
+
* return res.json();
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function cloudAdapter<S extends TaskSchema = TaskSchema>(fn: CloudFn<S>, options?: {
|
|
20
|
+
id?: string;
|
|
21
|
+
}): CloudRunner<S>;
|
|
22
|
+
export interface FetchAdapterOptions<S extends TaskSchema = TaskSchema> {
|
|
23
|
+
/** Your backend endpoint. Never put provider API keys in the browser. */
|
|
24
|
+
url: string | URL;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
/** Builds the request body. Defaults to the JSON-serialized request (functions dropped). */
|
|
27
|
+
body?: (request: CloudRequest<S>) => unknown;
|
|
28
|
+
/** Extracts the output from the parsed JSON response. Defaults to the whole response. */
|
|
29
|
+
select?: (json: unknown) => unknown;
|
|
30
|
+
/** Extracts token usage from the parsed JSON response, if your backend passes it through. */
|
|
31
|
+
usage?: (json: unknown) => CloudUsage | CloudUsage[] | undefined;
|
|
32
|
+
id?: string;
|
|
33
|
+
fetch?: typeof fetch;
|
|
34
|
+
}
|
|
35
|
+
/** A cloud runner that POSTs the request as JSON to your endpoint. */
|
|
36
|
+
export declare function fetchAdapter<S extends TaskSchema = TaskSchema>(options: FetchAdapterOptions<S>): CloudRunner<S>;
|
|
37
|
+
//# sourceMappingURL=cloud.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloud.d.ts","sourceRoot":"","sources":["../src/cloud.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEjG,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,IAAI,CACvD,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,EACxB,OAAO,EAAE;IACP,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,+FAA+F;IAC/F,WAAW,EAAE,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,EAAE,KAAK,IAAI,CAAC;CACzD,KACE,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAC5D,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC,EACd,OAAO,GAAE;IAAE,EAAE,CAAC,EAAE,MAAM,CAAA;CAAO,GAC5B,WAAW,CAAC,CAAC,CAAC,CAShB;AAED,MAAM,WAAW,mBAAmB,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IACpE,yEAAyE;IACzE,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,4FAA4F;IAC5F,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC;IAC7C,yFAAyF;IACzF,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC;IACpC,6FAA6F;IAC7F,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,UAAU,GAAG,UAAU,EAAE,GAAG,SAAS,CAAC;IACjE,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,sEAAsE;AACtE,wBAAgB,YAAY,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAmB/G"}
|
package/dist/cloud.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a plain async function as a cloud runner. The function returns the raw output:
|
|
3
|
+
* the bare value, the `{ value }` wrapper, or a JSON string of either. The task validates it.
|
|
4
|
+
* Token usage goes through `reportUsage`, since the output itself may be a `{ value }` object.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* cloudAdapter(async ({ instruction, input, jsonSchema }, { signal }) => {
|
|
8
|
+
* const res = await fetch('/api/classify', { method: 'POST', body: JSON.stringify({ instruction, input, jsonSchema }), signal });
|
|
9
|
+
* return res.json();
|
|
10
|
+
* });
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export function cloudAdapter(fn, options = {}) {
|
|
14
|
+
return {
|
|
15
|
+
id: options.id ?? 'cloud',
|
|
16
|
+
async run(request, opts) {
|
|
17
|
+
let usage;
|
|
18
|
+
const value = await fn(request, { ...opts, reportUsage: (u) => void (usage = u) });
|
|
19
|
+
return usage ? { value, usage } : { value };
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** A cloud runner that POSTs the request as JSON to your endpoint. */
|
|
24
|
+
export function fetchAdapter(options) {
|
|
25
|
+
const doFetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
26
|
+
return cloudAdapter(async (request, { signal, reportUsage }) => {
|
|
27
|
+
const body = options.body ? options.body(request) : request;
|
|
28
|
+
const res = await doFetch(options.url, {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: { 'content-type': 'application/json', ...options.headers },
|
|
31
|
+
body: JSON.stringify(body),
|
|
32
|
+
...(signal ? { signal } : {}),
|
|
33
|
+
});
|
|
34
|
+
if (!res.ok)
|
|
35
|
+
throw new Error(`Cloud endpoint responded with HTTP ${res.status}`);
|
|
36
|
+
const json = await res.json();
|
|
37
|
+
const usage = options.usage?.(json);
|
|
38
|
+
if (usage)
|
|
39
|
+
reportUsage(usage);
|
|
40
|
+
return options.select ? options.select(json) : json;
|
|
41
|
+
}, { id: options.id ?? 'fetch' });
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=cloud.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloud.js","sourceRoot":"","sources":["../src/cloud.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAC1B,EAAc,EACd,OAAO,GAAoB,EAAE;IAE7B,OAAO;QACL,EAAE,EAAE,OAAO,CAAC,EAAE,IAAI,OAAO;QACzB,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI;YACrB,IAAI,KAA4C,CAAC;YACjD,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE,GAAG,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,EAAE,EAAE,C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C,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACtD,CAAC,EACD,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,IAAI,OAAO,EAAE,CAC9B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ProbabilityEntry } from './types.js';
|
|
2
|
+
/** Probability of the top option: the confidence signal for classifier tasks. */
|
|
3
|
+
export declare function topProbability(probabilities: readonly ProbabilityEntry[]): number;
|
|
4
|
+
export interface ConfidenceSignals {
|
|
5
|
+
/** Whether the output passed schema validation. */
|
|
6
|
+
valid: boolean;
|
|
7
|
+
/** Confidence reported by the local runner itself, if any. */
|
|
8
|
+
runner?: number | undefined;
|
|
9
|
+
/** P(correct) from the classifier-as-judge, if any. */
|
|
10
|
+
judge?: number | undefined;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Combines the signals into one confidence (ADR 0001, "Confidence signal"):
|
|
14
|
+
*
|
|
15
|
+
* - schema validation is a hard gate: an invalid output has confidence 0;
|
|
16
|
+
* - with only one of `runner` / `judge`, that value is the confidence;
|
|
17
|
+
* - with both, the minimum (the more pessimistic signal wins);
|
|
18
|
+
* - with neither, `undefined`: the task cannot decide and reports a configuration error.
|
|
19
|
+
*/
|
|
20
|
+
export declare function combineConfidence(signals: ConfidenceSignals): number | undefined;
|
|
21
|
+
export declare function clamp01(n: number): number;
|
|
22
|
+
//# sourceMappingURL=confidence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"confidence.d.ts","sourceRoot":"","sources":["../src/confidence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,iFAAiF;AACjF,wBAAgB,cAAc,CAAC,aAAa,EAAE,SAAS,gBAAgB,EAAE,GAAG,MAAM,CAIjF;AAED,MAAM,WAAW,iBAAiB;IAChC,mDAAmD;IACnD,KAAK,EAAE,OAAO,CAAC;IACf,8DAA8D;IAC9D,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,uDAAuD;IACvD,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC5B;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,iBAAiB,GAAG,MAAM,GAAG,SAAS,CAKhF;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAGzC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Probability of the top option: the confidence signal for classifier tasks. */
|
|
2
|
+
export function topProbability(probabilities) {
|
|
3
|
+
let top = 0;
|
|
4
|
+
for (const p of probabilities)
|
|
5
|
+
if (p.probability > top)
|
|
6
|
+
top = p.probability;
|
|
7
|
+
return clamp01(top);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Combines the signals into one confidence (ADR 0001, "Confidence signal"):
|
|
11
|
+
*
|
|
12
|
+
* - schema validation is a hard gate: an invalid output has confidence 0;
|
|
13
|
+
* - with only one of `runner` / `judge`, that value is the confidence;
|
|
14
|
+
* - with both, the minimum (the more pessimistic signal wins);
|
|
15
|
+
* - with neither, `undefined`: the task cannot decide and reports a configuration error.
|
|
16
|
+
*/
|
|
17
|
+
export function combineConfidence(signals) {
|
|
18
|
+
if (!signals.valid)
|
|
19
|
+
return 0;
|
|
20
|
+
const values = [signals.runner, signals.judge].filter((v) => typeof v === 'number');
|
|
21
|
+
if (values.length === 0)
|
|
22
|
+
return undefined;
|
|
23
|
+
return clamp01(Math.min(...values));
|
|
24
|
+
}
|
|
25
|
+
export function clamp01(n) {
|
|
26
|
+
if (Number.isNaN(n))
|
|
27
|
+
return 0;
|
|
28
|
+
return n < 0 ? 0 : n > 1 ? 1 : n;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=confidence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"confidence.js","sourceRoot":"","sources":["../src/confidence.ts"],"names":[],"mappings":"AAEA,iFAAiF;AACjF,MAAM,UAAU,cAAc,CAAC,aAA0C;IACvE,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,MAAM,CAAC,IAAI,aAAa;QAAE,IAAI,CAAC,CAAC,WAAW,GAAG,GAAG;YAAE,GAAG,GAAG,CAAC,CAAC,WAAW,CAAC;IAC5E,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC;AACtB,CAAC;AAWD;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAA0B;IAC1D,IAAI,CAAC,OAAO,CAAC,KAAK;QAAE,OAAO,CAAC,CAAC;IAC7B,MAAM,MAAM,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC;IACjG,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAC1C,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,CAAS;IAC/B,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC"}
|
package/dist/cost.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { BelayEvent, CloudUsage, PriceTable, TokenPrices } from './types.js';
|
|
2
|
+
/** Normalizes a runner's usage report to a list of per-model parts. */
|
|
3
|
+
export declare function usageParts(usage: CloudUsage | readonly CloudUsage[] | undefined): CloudUsage[];
|
|
4
|
+
/**
|
|
5
|
+
* Cost of one cloud call from its token usage. Prices are per million tokens. A price table is
|
|
6
|
+
* keyed by the model id the provider reports, so a call that a fallback model served is priced
|
|
7
|
+
* at that model's rates. Throws when a model has no price: guessing would make every saving wrong.
|
|
8
|
+
*/
|
|
9
|
+
export declare function costOf(usage: CloudUsage | readonly CloudUsage[], prices: PriceTable | TokenPrices): number;
|
|
10
|
+
export interface SavingsMeterOptions {
|
|
11
|
+
/** Per-token prices, to measure the cost of each cloud call from the usage its runner reports. */
|
|
12
|
+
prices?: PriceTable | TokenPrices;
|
|
13
|
+
/**
|
|
14
|
+
* Cost of one cloud call, used when calls report no usage, and to value the calls the cascade
|
|
15
|
+
* avoided before any cloud call has been measured. A calibration file's `cost.cloudPerRun` fits.
|
|
16
|
+
*/
|
|
17
|
+
cloudPerRun?: number;
|
|
18
|
+
currency?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface SavingsSummary {
|
|
21
|
+
currency: string | null;
|
|
22
|
+
runs: number;
|
|
23
|
+
/** Answered on device because the local confidence met the threshold. */
|
|
24
|
+
local: number;
|
|
25
|
+
/** Answered by the cloud runner. */
|
|
26
|
+
cloud: number;
|
|
27
|
+
/** Answered on device only because escalation was blocked (policy, consent, cloud error). */
|
|
28
|
+
blocked: number;
|
|
29
|
+
/** Spent on cloud calls: measured where usage was reported, otherwise `cloudPerRun` each. */
|
|
30
|
+
cloudSpend: number | null;
|
|
31
|
+
/** Mean cost of one cloud call (measured when possible). */
|
|
32
|
+
costPerCloudRun: number | null;
|
|
33
|
+
/** What the `local` runs would have cost in the cloud: `local × costPerCloudRun`. */
|
|
34
|
+
saved: number | null;
|
|
35
|
+
/** `saved / (saved + cloudSpend)`: the share of the cloud-only bill the cascade avoided. */
|
|
36
|
+
savedShare: number | null;
|
|
37
|
+
/** How many cloud calls were priced from reported usage. */
|
|
38
|
+
measuredCalls: number;
|
|
39
|
+
}
|
|
40
|
+
export interface SavingsMeter {
|
|
41
|
+
/** Pass as the task's `onEvent` (or call it from yours). */
|
|
42
|
+
readonly onEvent: (event: BelayEvent) => void;
|
|
43
|
+
/** Totals so far, for all tasks or one. */
|
|
44
|
+
summary(task?: string): SavingsSummary;
|
|
45
|
+
reset(): void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Counts what the cascade saves in production. Only runs that stayed on device because the
|
|
49
|
+
* local answer was confident count as saved; runs kept local because escalation was blocked
|
|
50
|
+
* are reported separately. Unmeasured calls, and the value of avoided ones, use the mean
|
|
51
|
+
* measured cost per call, falling back to `cloudPerRun`.
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* const meter = savingsMeter({ prices, cloudPerRun: calibration.cost?.cloudPerRun });
|
|
55
|
+
* const triage = task({ ..., onEvent: meter.onEvent });
|
|
56
|
+
* meter.summary(); // { runs, local, cloud, cloudSpend, saved, savedShare, ... }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function savingsMeter(options?: SavingsMeterOptions): SavingsMeter;
|
|
60
|
+
//# sourceMappingURL=cost.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAElF,uEAAuE;AACvE,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,UAAU,EAAE,GAAG,SAAS,GAAG,UAAU,EAAE,CAG9F;AAUD;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,UAAU,EAAE,EAAE,MAAM,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,CAW1G;AAED,MAAM,WAAW,mBAAmB;IAClC,kGAAkG;IAClG,MAAM,CAAC,EAAE,UAAU,GAAG,WAAW,CA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