baml-sap-ts 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.
- package/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +355 -0
- package/dist/index.d.ts +180 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +274 -0
- package/dist/index.js.map +1 -0
- package/dist/json-extractor.d.ts +57 -0
- package/dist/json-extractor.d.ts.map +1 -0
- package/dist/json-extractor.js +382 -0
- package/dist/json-extractor.js.map +1 -0
- package/dist/schema-renderer.d.ts +34 -0
- package/dist/schema-renderer.d.ts.map +1 -0
- package/dist/schema-renderer.js +329 -0
- package/dist/schema-renderer.js.map +1 -0
- package/dist/type-coercer.d.ts +59 -0
- package/dist/type-coercer.d.ts.map +1 -0
- package/dist/type-coercer.js +589 -0
- package/dist/type-coercer.js.map +1 -0
- package/package.json +79 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* JSON Extractor - Extracts JSON from LLM responses
|
|
4
|
+
*
|
|
5
|
+
* Handles:
|
|
6
|
+
* - Markdown code block extraction
|
|
7
|
+
* - Multiple JSON objects
|
|
8
|
+
* - JSON fixing (trailing commas, missing quotes)
|
|
9
|
+
* - Chain-of-thought text filtering
|
|
10
|
+
*
|
|
11
|
+
* Based on BAML's jsonish parser
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.extractJson = extractJson;
|
|
15
|
+
exports.filterChainOfThought = filterChainOfThought;
|
|
16
|
+
exports.extractAllCandidates = extractAllCandidates;
|
|
17
|
+
exports.hasChainOfThought = hasChainOfThought;
|
|
18
|
+
const defaultOptions = {
|
|
19
|
+
allowMarkdownJson: true,
|
|
20
|
+
allowFixes: true,
|
|
21
|
+
allowAsString: true,
|
|
22
|
+
findAllJsonObjects: true,
|
|
23
|
+
maxDepth: 100,
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Extract JSON from an LLM response string
|
|
27
|
+
*/
|
|
28
|
+
function extractJson(text, options = {}, isDone = true, depth = 0) {
|
|
29
|
+
const opts = { ...defaultOptions, ...options };
|
|
30
|
+
// Depth limit check
|
|
31
|
+
if (depth > opts.maxDepth) {
|
|
32
|
+
throw new Error("Depth limit reached. Likely a circular reference.");
|
|
33
|
+
}
|
|
34
|
+
// Try direct JSON parsing first
|
|
35
|
+
const directResult = tryParseDirect(text, isDone);
|
|
36
|
+
if (directResult) {
|
|
37
|
+
return directResult;
|
|
38
|
+
}
|
|
39
|
+
// Try markdown extraction
|
|
40
|
+
if (opts.allowMarkdownJson) {
|
|
41
|
+
const markdownResult = tryExtractFromMarkdown(text, opts, isDone, depth + 1);
|
|
42
|
+
if (markdownResult) {
|
|
43
|
+
return markdownResult;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Try finding all JSON objects
|
|
47
|
+
if (opts.findAllJsonObjects) {
|
|
48
|
+
const multiResult = tryExtractMultipleJson(text, opts, isDone, depth + 1);
|
|
49
|
+
if (multiResult) {
|
|
50
|
+
return multiResult;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Try fixing malformed JSON
|
|
54
|
+
if (opts.allowFixes) {
|
|
55
|
+
const fixedResult = tryFixJson(text, opts, isDone, depth + 1);
|
|
56
|
+
if (fixedResult) {
|
|
57
|
+
return fixedResult;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Return as string if allowed
|
|
61
|
+
if (opts.allowAsString) {
|
|
62
|
+
return {
|
|
63
|
+
value: text,
|
|
64
|
+
raw: text,
|
|
65
|
+
isPartial: !isDone,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
throw new Error("Failed to extract JSON from response");
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Try to parse the text directly as JSON
|
|
72
|
+
*/
|
|
73
|
+
function tryParseDirect(text, isDone) {
|
|
74
|
+
const trimmed = text.trim();
|
|
75
|
+
try {
|
|
76
|
+
// Check if it looks like JSON before trying to parse
|
|
77
|
+
if (!looksLikeJson(trimmed)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const parsed = JSON.parse(trimmed);
|
|
81
|
+
return {
|
|
82
|
+
value: parsed,
|
|
83
|
+
raw: text,
|
|
84
|
+
isPartial: !isDone,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Check if text looks like it might be JSON
|
|
93
|
+
*/
|
|
94
|
+
function looksLikeJson(text) {
|
|
95
|
+
const trimmed = text.trim();
|
|
96
|
+
if (trimmed.length === 0)
|
|
97
|
+
return false;
|
|
98
|
+
const firstChar = trimmed[0];
|
|
99
|
+
const lastChar = trimmed[trimmed.length - 1];
|
|
100
|
+
// Object or array
|
|
101
|
+
if ((firstChar === "{" && lastChar === "}") || (firstChar === "[" && lastChar === "]")) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
// String (quoted)
|
|
105
|
+
if (firstChar === '"' && lastChar === '"') {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
// Number, boolean, null
|
|
109
|
+
if (/^-?\d/.test(trimmed) || trimmed === "true" || trimmed === "false" || trimmed === "null") {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Extract JSON from markdown code blocks
|
|
116
|
+
*/
|
|
117
|
+
function tryExtractFromMarkdown(text, options, isDone, _depth) {
|
|
118
|
+
// Regex for markdown code blocks
|
|
119
|
+
const codeBlockRegex = /^(\s*)```(\w*)\s*\n?([\s\S]*?)```/gm;
|
|
120
|
+
const matches = [];
|
|
121
|
+
let match;
|
|
122
|
+
while (true) {
|
|
123
|
+
match = codeBlockRegex.exec(text);
|
|
124
|
+
if (match === null)
|
|
125
|
+
break;
|
|
126
|
+
matches.push({
|
|
127
|
+
lang: match[2].trim().toLowerCase(),
|
|
128
|
+
content: match[3].trim(),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (matches.length === 0) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
// Filter for JSON-like blocks
|
|
135
|
+
const jsonBlocks = matches.filter((m) => m.lang === "json" || m.lang === "" || m.lang === "javascript" || m.lang === "js" || looksLikeJson(m.content));
|
|
136
|
+
if (jsonBlocks.length === 0) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
// If only one block, try to parse it directly
|
|
140
|
+
if (jsonBlocks.length === 1) {
|
|
141
|
+
try {
|
|
142
|
+
const content = jsonBlocks[0].content;
|
|
143
|
+
const parsed = extractJson(content, options, isDone, _depth);
|
|
144
|
+
return {
|
|
145
|
+
...parsed,
|
|
146
|
+
fromMarkdown: true,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// Continue to try other blocks
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Multiple blocks - try to parse each and return the first valid one
|
|
154
|
+
// or return all as an array
|
|
155
|
+
const results = [];
|
|
156
|
+
const errors = [];
|
|
157
|
+
for (const block of jsonBlocks) {
|
|
158
|
+
try {
|
|
159
|
+
const parsed = JSON.parse(block.content);
|
|
160
|
+
results.push(parsed);
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
errors.push(String(e));
|
|
164
|
+
// Try with fixes
|
|
165
|
+
try {
|
|
166
|
+
const fixed = tryFixJson(block.content, options, isDone, _depth);
|
|
167
|
+
if (fixed) {
|
|
168
|
+
results.push(fixed.value);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
// Ignore fix errors
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (results.length === 1) {
|
|
177
|
+
return {
|
|
178
|
+
value: results[0],
|
|
179
|
+
raw: text,
|
|
180
|
+
fromMarkdown: true,
|
|
181
|
+
isPartial: !isDone,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
if (results.length > 1) {
|
|
185
|
+
return {
|
|
186
|
+
value: results,
|
|
187
|
+
raw: text,
|
|
188
|
+
fromMarkdown: true,
|
|
189
|
+
isPartial: !isDone,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Try to extract multiple JSON objects from text
|
|
196
|
+
*/
|
|
197
|
+
function tryExtractMultipleJson(text, _options, isDone, _depth) {
|
|
198
|
+
const objects = [];
|
|
199
|
+
const jsonRegex = /\{[\s\S]*?\}|\[[\s\S]*?\]/g;
|
|
200
|
+
let match;
|
|
201
|
+
while (true) {
|
|
202
|
+
match = jsonRegex.exec(text);
|
|
203
|
+
if (match === null)
|
|
204
|
+
break;
|
|
205
|
+
try {
|
|
206
|
+
const candidate = match[0];
|
|
207
|
+
const parsed = JSON.parse(candidate);
|
|
208
|
+
objects.push(parsed);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// Try with fixes
|
|
212
|
+
try {
|
|
213
|
+
const fixed = applyJsonFixes(match[0]);
|
|
214
|
+
const parsed = JSON.parse(fixed);
|
|
215
|
+
objects.push(parsed);
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// Not valid JSON
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (objects.length === 0) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
if (objects.length === 1) {
|
|
226
|
+
return {
|
|
227
|
+
value: objects[0],
|
|
228
|
+
raw: text,
|
|
229
|
+
isPartial: !isDone,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
value: objects,
|
|
234
|
+
raw: text,
|
|
235
|
+
isPartial: !isDone,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Try to fix malformed JSON
|
|
240
|
+
*/
|
|
241
|
+
function tryFixJson(text, _options, isDone, _depth) {
|
|
242
|
+
const fixes = [];
|
|
243
|
+
try {
|
|
244
|
+
let fixed = text;
|
|
245
|
+
// Apply fixes
|
|
246
|
+
const fixedResult = applyJsonFixes(fixed);
|
|
247
|
+
if (fixedResult !== fixed) {
|
|
248
|
+
fixes.push("applied_auto_fixes");
|
|
249
|
+
fixed = fixedResult;
|
|
250
|
+
}
|
|
251
|
+
// Try parsing the fixed text
|
|
252
|
+
const parsed = JSON.parse(fixed);
|
|
253
|
+
return {
|
|
254
|
+
value: parsed,
|
|
255
|
+
raw: text,
|
|
256
|
+
fixes,
|
|
257
|
+
isPartial: !isDone,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
// Try extracting partial JSON
|
|
262
|
+
const partial = tryExtractPartialJson(text);
|
|
263
|
+
if (partial) {
|
|
264
|
+
fixes.push("extracted_partial");
|
|
265
|
+
return {
|
|
266
|
+
value: partial,
|
|
267
|
+
raw: text,
|
|
268
|
+
fixes,
|
|
269
|
+
isPartial: true,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Apply common JSON fixes
|
|
277
|
+
*/
|
|
278
|
+
function applyJsonFixes(text) {
|
|
279
|
+
let fixed = text.trim();
|
|
280
|
+
// Remove trailing commas before } or ]
|
|
281
|
+
fixed = fixed.replace(/,(\s*[}\]])/g, "$1");
|
|
282
|
+
// Fix single quotes to double quotes (carefully)
|
|
283
|
+
// Only fix property keys and simple string values, not content inside strings
|
|
284
|
+
fixed = fixed.replace(/([{,]\s*)'([^']+)'(\s*:)/g, '$1"$2"$3');
|
|
285
|
+
// Fix unquoted keys (simple cases only)
|
|
286
|
+
fixed = fixed.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g, '$1"$2":');
|
|
287
|
+
// Fix missing quotes around string values (simple cases)
|
|
288
|
+
// This is risky but common with LLMs
|
|
289
|
+
// fixed = fixed.replace(/: \s*([a-zA-Z][a-zA-Z0-9_]*)\s*([,}])/g, ': "$1"$2');
|
|
290
|
+
// Fix line breaks in strings (replace with \n)
|
|
291
|
+
// This is a simplified approach - real implementation would be more careful
|
|
292
|
+
// fixed = fixed.replace(/"([^"]*)\n([^"]*)"/g, '"$1\\n$2"');
|
|
293
|
+
return fixed;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Try to extract partial/incomplete JSON (for streaming)
|
|
297
|
+
*/
|
|
298
|
+
function tryExtractPartialJson(text) {
|
|
299
|
+
// Try to complete an incomplete object
|
|
300
|
+
let fixed = text.trim();
|
|
301
|
+
// Count braces
|
|
302
|
+
const openBraces = (fixed.match(/\{/g) || []).length;
|
|
303
|
+
const closeBraces = (fixed.match(/\}/g) || []).length;
|
|
304
|
+
const openBrackets = (fixed.match(/\[/g) || []).length;
|
|
305
|
+
const closeBrackets = (fixed.match(/\]/g) || []).length;
|
|
306
|
+
// Add missing closing braces/brackets
|
|
307
|
+
fixed += "}".repeat(Math.max(0, openBraces - closeBraces));
|
|
308
|
+
fixed += "]".repeat(Math.max(0, openBrackets - closeBrackets));
|
|
309
|
+
try {
|
|
310
|
+
return JSON.parse(fixed);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
// Try without the added closings if that failed
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Filter out chain-of-thought reasoning text
|
|
319
|
+
* Returns the text that appears after reasoning markers
|
|
320
|
+
*/
|
|
321
|
+
function filterChainOfThought(text) {
|
|
322
|
+
// Common patterns that indicate the start of actual output
|
|
323
|
+
const patterns = [
|
|
324
|
+
/here is the json[\s\S]*?:\s*/i,
|
|
325
|
+
/output json[\s\S]*?:\s*/i,
|
|
326
|
+
/therefore the output json is[\s\S]*?:\s*/i,
|
|
327
|
+
/final answer[\s\S]*?:\s*/i,
|
|
328
|
+
/answer:[\s\S]*?\n\s*/i,
|
|
329
|
+
/```json\s*/,
|
|
330
|
+
/\{[\s\S]*/, // First opening brace
|
|
331
|
+
];
|
|
332
|
+
for (const pattern of patterns) {
|
|
333
|
+
const match = text.match(pattern);
|
|
334
|
+
if (match) {
|
|
335
|
+
const index = match.index ?? 0;
|
|
336
|
+
return text.substring(index).trim();
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return text;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Extract all candidate JSON strings from text
|
|
343
|
+
* Useful for debugging and multiple-choice scenarios
|
|
344
|
+
*/
|
|
345
|
+
function extractAllCandidates(text) {
|
|
346
|
+
const candidates = [];
|
|
347
|
+
// Direct JSON-like strings
|
|
348
|
+
const jsonRegex = /\{[\s\S]*?\}|\[[\s\S]*?\]|"[^"]*"|-?\d+(?:\.\d+)?|true|false|null/g;
|
|
349
|
+
let match;
|
|
350
|
+
while (true) {
|
|
351
|
+
match = jsonRegex.exec(text);
|
|
352
|
+
if (match === null)
|
|
353
|
+
break;
|
|
354
|
+
candidates.push(match[0]);
|
|
355
|
+
}
|
|
356
|
+
// Markdown code blocks
|
|
357
|
+
const codeBlockRegex = /```(?:json)?\s*\n?([\s\S]*?)```/g;
|
|
358
|
+
while (true) {
|
|
359
|
+
match = codeBlockRegex.exec(text);
|
|
360
|
+
if (match === null)
|
|
361
|
+
break;
|
|
362
|
+
candidates.push(match[1].trim());
|
|
363
|
+
}
|
|
364
|
+
return [...new Set(candidates)]; // Remove duplicates
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Check if a response looks like it contains chain-of-thought reasoning
|
|
368
|
+
*/
|
|
369
|
+
function hasChainOfThought(text) {
|
|
370
|
+
const patterns = [
|
|
371
|
+
/let me think/i,
|
|
372
|
+
/step by step/i,
|
|
373
|
+
/first,? /i,
|
|
374
|
+
/reasoning:/i,
|
|
375
|
+
/thinking:/i,
|
|
376
|
+
/analysis:/i,
|
|
377
|
+
/therefore,? /i,
|
|
378
|
+
/in conclusion/i,
|
|
379
|
+
];
|
|
380
|
+
return patterns.some((p) => p.test(text));
|
|
381
|
+
}
|
|
382
|
+
//# sourceMappingURL=json-extractor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-extractor.js","sourceRoot":"","sources":["../src/json-extractor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AA0CH,kCAqDC;AA+SD,oDAqBC;AAMD,oDAqBC;AAKD,8CAaC;AAjcD,MAAM,cAAc,GAAsB;IACzC,iBAAiB,EAAE,IAAI;IACvB,UAAU,EAAE,IAAI;IAChB,aAAa,EAAE,IAAI;IACnB,kBAAkB,EAAE,IAAI;IACxB,QAAQ,EAAE,GAAG;CACb,CAAC;AAkBF;;GAEG;AACH,SAAgB,WAAW,CAC1B,IAAY,EACZ,UAA6B,EAAE,EAC/B,SAAkB,IAAI,EACtB,QAAgB,CAAC;IAEjB,MAAM,IAAI,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC;IAE/C,oBAAoB;IACpB,IAAI,KAAK,GAAG,IAAI,CAAC,QAAS,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACtE,CAAC;IAED,gCAAgC;IAChC,MAAM,YAAY,GAAG,cAAc,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAClD,IAAI,YAAY,EAAE,CAAC;QAClB,OAAO,YAAY,CAAC;IACrB,CAAC;IAED,0BAA0B;IAC1B,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAC5B,MAAM,cAAc,GAAG,sBAAsB,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QAC7E,IAAI,cAAc,EAAE,CAAC;YACpB,OAAO,cAAc,C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ML's render_output_format.rs
|
|
5
|
+
* It generates schema instructions that guide the LLM to output correctly
|
|
6
|
+
* structured data.
|
|
7
|
+
*/
|
|
8
|
+
import { type TSchema } from "@sinclair/typebox";
|
|
9
|
+
export interface SchemaRenderOptions {
|
|
10
|
+
/** Include field descriptions in output */
|
|
11
|
+
includeDescriptions?: boolean;
|
|
12
|
+
/** Indentation level for formatting */
|
|
13
|
+
indent?: number;
|
|
14
|
+
/** Maximum depth for nested structures */
|
|
15
|
+
maxDepth?: number;
|
|
16
|
+
/** Whether to allow partial outputs (for streaming) */
|
|
17
|
+
allowPartials?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Render a TypeBox schema as prompt instructions
|
|
21
|
+
*/
|
|
22
|
+
export declare function renderSchema(schema: TSchema, options?: SchemaRenderOptions): string;
|
|
23
|
+
/**
|
|
24
|
+
* Create a prompt with schema instructions appended
|
|
25
|
+
*/
|
|
26
|
+
export declare function createPromptWithSchema(basePrompt: string, schema: TSchema, options?: SchemaRenderOptions): string;
|
|
27
|
+
/**
|
|
28
|
+
* Create a compact JSON schema representation for the prompt
|
|
29
|
+
* This is an alternative to the human-readable format above
|
|
30
|
+
*/
|
|
31
|
+
export declare function createJsonSchemaPrompt(basePrompt: string, schema: TSchema, _options?: {
|
|
32
|
+
includeExamples?: boolean;
|
|
33
|
+
}): string;
|
|
34
|
+
//# sourceMappingURL=schema-renderer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-renderer.d.ts","sourceRoot":"","sources":["../src/schema-renderer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAaN,KAAK,OAAO,EAIZ,MAAM,mBAAmB,CAAC;AAE3B,MAAM,WAAW,mBAAmB;IACnC,2CAA2C;IAC3C,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,uCAAuC;IACvC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB;AASD;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,GAAE,mBAAwB,GAAG,MAAM,CAWvF;AAoWD;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CAIjH;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CACrC,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,OAAO,EACf,QAAQ,GAAE;IAAE,eAAe,CAAC,EAAE,OAAO,CAAA;CAAO,GAC1C,MAAM,CASR"}
|