amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Amber Streaming Structured JSON & LLM Token Grammar Engine (amber:grammar)
// Real-time incremental partial JSON repair, SSE parser, and constrained token grammars.

use rusty_v8 as v8;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SSEMessage {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event: Option<String>,
    pub data: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retry: Option<u64>,
}

/// Repairs an incomplete/partial JSON string so it can be parsed into a valid JSON object.
/// Handles unclosed strings, trailing commas, open arrays `[` and open objects `{`.
pub fn repair_partial_json(input: &str) -> String {
    let text = input.trim();
    if text.is_empty() {
        return "null".to_string();
    }

    // If it's already valid JSON, return directly
    if serde_json::from_str::<serde_json::Value>(text).is_ok() {
        return text.to_string();
    }

    let mut result = String::with_capacity(text.len() + 16);
    let mut in_string = false;
    let mut escape = false;
    let mut stack = Vec::new(); // tracks open '{' and '['

    let chars: Vec<char> = text.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        let ch = chars[i];

        if in_string {
            result.push(ch);
            if escape {
                escape = false;
            } else if ch == '\\' {
                escape = true;
            } else if ch == '"' {
                in_string = false;
            }
            i += 1;
            continue;
        }

        match ch {
            '"' => {
                in_string = true;
                result.push(ch);
            }
            '{' => {
                stack.push('}');
                result.push(ch);
            }
            '[' => {
                stack.push(']');
                result.push(ch);
            }
            '}' => {
                if let Some(&expected) = stack.last() {
                    if expected == '}' {
                        stack.pop();
                    }
                }
                result.push(ch);
            }
            ']' => {
                if let Some(&expected) = stack.last() {
                    if expected == ']' {
                        stack.pop();
                    }
                }
                result.push(ch);
            }
            _ => {
                result.push(ch);
            }
        }
        i += 1;
    }

    // 1. If inside an unclosed string, close the quote
    if in_string {
        if escape {
            result.pop(); // drop trailing solitary backslash
        }
        result.push('"');
    }

    // 2. Clean up trailing separators like trailing commas or uncompleted key-value colons
    let mut trimmed = result.trim_end().to_string();

    loop {
        if trimmed.ends_with(',') {
            trimmed.pop();
            trimmed = trimmed.trim_end().to_string();
        } else if trimmed.ends_with(':') {
            // E.g. {"key": -> supply null
            trimmed.push_str(" null");
            break;
        } else {
            break;
        }
    }

    // 3. Balance unclosed braces and brackets in reverse order
    while let Some(closing) = stack.pop() {
        trimmed.push(closing);
    }

    // 4. Verify if repaired JSON is valid; if not, fallback to object or array if started with them
    if serde_json::from_str::<serde_json::Value>(&trimmed).is_ok() {
        trimmed
    } else if text.starts_with('{') {
        "{}".to_string()
    } else if text.starts_with('[') {
        "[]".to_string()
    } else {
        serde_json::to_string(text).unwrap_or_else(|_| "\"\"".to_string())
    }
}

/// Parses a raw Server-Sent Events (SSE) chunk into structured SSE messages.
pub fn parse_sse_chunk(chunk: &str) -> Vec<SSEMessage> {
    let mut messages = Vec::new();
    let mut current_event: Option<String> = None;
    let mut current_data_lines = Vec::new();
    let mut current_id: Option<String> = None;
    let mut current_retry: Option<u64> = None;

    for line in chunk.lines() {
        let line = line.trim_end_matches('\r');

        if line.is_empty() {
            // Empty line indicates dispatch of event
            if !current_data_lines.is_empty() || current_event.is_some() {
                messages.push(SSEMessage {
                    event: current_event.take(),
                    data: current_data_lines.join("\n"),
                    id: current_id.take(),
                    retry: current_retry.take(),
                });
                current_data_lines.clear();
            }
            continue;
        }

        if line.starts_with(':') {
            // SSE comment/ping line, ignore
            continue;
        }

        if let Some((field, value)) = line.split_once(':') {
            let val = value.strip_prefix(' ').unwrap_or(value);
            match field {
                "event" => current_event = Some(val.to_string()),
                "data" => current_data_lines.push(val.to_string()),
                "id" => current_id = Some(val.to_string()),
                "retry" => {
                    if let Ok(ms) = val.parse::<u64>() {
                        current_retry = Some(ms);
                    }
                }
                _ => {}
            }
        } else {
            // Field without colon
            if line == "data" {
                current_data_lines.push(String::new());
            }
        }
    }

    // If chunk ended without final empty line, dispatch remaining
    if !current_data_lines.is_empty() || current_event.is_some() {
        messages.push(SSEMessage {
            event: current_event,
            data: current_data_lines.join("\n"),
            id: current_id,
            retry: current_retry,
        });
    }

    messages
}

fn grammar_native_dispatch(
    scope: &mut v8::PinScope,
    args: v8::FunctionCallbackArguments,
    mut rv: v8::ReturnValue,
) {
    if args.length() < 1 {
        return;
    }

    let action = args.get(0).to_rust_string_lossy(scope);

    match action.as_str() {
        "repair_partial_json" => {
            let input = args.get(1).to_rust_string_lossy(scope);
            let repaired = repair_partial_json(&input);
            let s = v8::String::new(scope, &repaired).unwrap();
            rv.set(s.into());
        }
        "parse_sse" => {
            let input = args.get(1).to_rust_string_lossy(scope);
            let events = parse_sse_chunk(&input);
            let json = serde_json::to_string(&events).unwrap_or_else(|_| "[]".to_string());
            let s = v8::String::new(scope, &json).unwrap();
            rv.set(s.into());
        }
        _ => {
            let msg =
                v8::String::new(scope, &format!("Unknown grammar action '{action}'")).unwrap();
            let exc = v8::Exception::type_error(scope, msg);
            scope.throw_exception(exc);
        }
    }
}

/// Sets up the `amber:grammar` API in V8 context
pub fn setup_grammar_api(
    scope: &mut v8::PinScope,
    context: &v8::Local<v8::Context>,
) -> anyhow::Result<()> {
    let global = context.global(scope);

    // Register native dispatcher callback
    let native_fn = v8::Function::new(scope, grammar_native_dispatch).unwrap();
    let k_native = v8::String::new(scope, "__amber_grammar_native").unwrap();
    global.set(scope, k_native.into(), native_fn.into());

    let grammar_js_bootstrap = r#"
    (function() {
        const native = globalThis.__amber_grammar_native;

        // --- 1. Partial JSON Parsing ---
        function parsePartialJSON(input) {
            if (typeof input !== 'string') {
                throw new TypeError('parsePartialJSON expects a string input');
            }
            const repaired = native('repair_partial_json', input);
            try {
                return JSON.parse(repaired);
            } catch (err) {
                return null;
            }
        }

        // --- 2. Stream Decoder ---
        function createStreamDecoder(options = {}) {
            let buffer = '';
            let lastParsed = null;
            const onChunk = options.onChunk || null;

            return {
                push(chunk) {
                    if (typeof chunk === 'string') {
                        buffer += chunk;
                    }
                    const parsed = parsePartialJSON(buffer);
                    lastParsed = parsed;
                    if (typeof onChunk === 'function') {
                        onChunk(parsed, false);
                    }
                    return parsed;
                },
                finish() {
                    const parsed = parsePartialJSON(buffer);
                    lastParsed = parsed;
                    if (typeof onChunk === 'function') {
                        onChunk(parsed, true);
                    }
                    return parsed;
                },
                get current() {
                    return lastParsed;
                },
                get raw() {
                    return buffer;
                },
                reset() {
                    buffer = '';
                    lastParsed = null;
                }
            };
        }

        // --- 3. Server-Sent Events (SSE) Parser ---
        function parseSSEChunk(chunk) {
            if (typeof chunk !== 'string') {
                throw new TypeError('parseSSEChunk expects a string chunk');
            }
            const raw = native('parse_sse', chunk);
            const list = JSON.parse(raw);
            return list.map(item => ({
                event: item.event || 'message',
                data: item.data,
                id: item.id,
                retry: item.retry,
                json() {
                    try {
                        return JSON.parse(item.data);
                    } catch {
                        return parsePartialJSON(item.data);
                    }
                }
            }));
        }

        // --- 4. Constrained Grammars ---
        class Grammar {
            #type;
            #validator;
            #acceptor;

            constructor(type, { validate, accept }) {
                this.#type = type;
                this.#validator = validate;
                this.#acceptor = accept;
            }

            get type() {
                return this.#type;
            }

            validate(text) {
                return this.#validator(String(text));
            }

            accept(prefix, nextToken) {
                if (typeof this.#acceptor === 'function') {
                    return this.#acceptor(String(prefix), String(nextToken));
                }
                const candidate = String(prefix) + String(nextToken);
                const res = this.validate(candidate);
                return res.valid;
            }
        }

        function createChoiceGrammar(choices) {
            if (!Array.isArray(choices) || choices.length === 0) {
                throw new TypeError('createChoiceGrammar requires a non-empty array of strings');
            }
            const choiceList = choices.map(String);

            return new Grammar('choices', {
                validate(text) {
                    const completed = choiceList.includes(text);
                    const valid = completed || choiceList.some(c => c.startsWith(text));
                    return { valid, completed, choices: choiceList };
                },
                accept(prefix, nextToken) {
                    const candidate = prefix + nextToken;
                    return choiceList.some(c => c.startsWith(candidate) || candidate.startsWith(c));
                }
            });
        }

        function createRegexGrammar(pattern) {
            const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;

            return new Grammar('regex', {
                validate(text) {
                    const valid = regex.test(text);
                    return { valid, completed: valid, pattern: regex.source };
                },
                accept(prefix, nextToken) {
                    const candidate = prefix + nextToken;
                    return regex.test(candidate);
                }
            });
        }

        function createJSONGrammar(schema = null) {
            return new Grammar('json', {
                validate(text) {
                    const parsed = parsePartialJSON(text);
                    const isComplete = Boolean(parsed && (text.trim().endsWith('}') || text.trim().endsWith(']')));
                    return {
                        valid: parsed !== null,
                        completed: isComplete,
                        parsed
                    };
                },
                accept(prefix, nextToken) {
                    const candidate = prefix + nextToken;
                    return parsePartialJSON(candidate) !== null;
                }
            });
        }

        function createGrammar(spec) {
            if (!spec || typeof spec !== 'object') {
                throw new TypeError('createGrammar requires a grammar specification object');
            }
            if (spec.choices) return createChoiceGrammar(spec.choices);
            if (spec.regex || spec.pattern) return createRegexGrammar(spec.regex || spec.pattern);
            if (spec.schema || spec.type === 'json') return createJSONGrammar(spec.schema);
            throw new TypeError("Grammar specification must have 'choices', 'regex', or 'schema'");
        }

        const grammarModule = {
            parsePartialJSON,
            createStreamDecoder,
            parseSSEChunk,
            Grammar,
            createGrammar,
            createChoiceGrammar,
            createRegexGrammar,
            createJSONGrammar,
            default: {
                parsePartialJSON,
                createStreamDecoder,
                parseSSEChunk,
                createGrammar,
                createChoiceGrammar,
                createRegexGrammar,
                createJSONGrammar
            }
        };

        globalThis.__amber_grammar = grammarModule;
        globalThis.grammar = grammarModule;
    })();
    "#;

    if let Some(code) = v8::String::new(scope, grammar_js_bootstrap) {
        if let Some(script) = v8::Script::compile(scope, code, None) {
            let _ = script.run(scope);
        }
    }

    Ok(())
}