kkachi 0.1.8

High-performance, zero-copy library for optimizing language model prompts and programs
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright © 2025 lituus-io <spicyzhug@gmail.com>
// All Rights Reserved.
// Licensed under PolyForm Noncommercial 1.0.0

//! JSON Adapter
//!
//! Formats prompts for JSON output and parses JSON responses.
//! Uses streaming parsing for efficiency.

use crate::adapter::{Adapter, DemoData};
use crate::error::Result;
use crate::intern::{sym, Sym};
use crate::predict::FieldRange;
use crate::signature::Signature;
use crate::str_view::StrView;
use crate::types::Inputs;
use smallvec::SmallVec;

/// JSON adapter configuration.
#[derive(Clone, Copy)]
pub struct JSONConfig {
    /// Pretty print JSON in prompts
    pub pretty: bool,
    /// Include schema in prompt
    pub include_schema: bool,
    /// Allow partial/streaming JSON
    pub allow_partial: bool,
}

impl Default for JSONConfig {
    fn default() -> Self {
        Self {
            pretty: true,
            include_schema: true,
            allow_partial: true,
        }
    }
}

impl JSONConfig {
    /// Create new config.
    pub const fn new() -> Self {
        Self {
            pretty: true,
            include_schema: true,
            allow_partial: true,
        }
    }

    /// Set pretty printing.
    pub const fn with_pretty(mut self, pretty: bool) -> Self {
        self.pretty = pretty;
        self
    }

    /// Set schema inclusion.
    pub const fn with_schema(mut self, include: bool) -> Self {
        self.include_schema = include;
        self
    }

    /// Set partial JSON allowance.
    pub const fn with_partial(mut self, allow: bool) -> Self {
        self.allow_partial = allow;
        self
    }
}

/// JSON adapter for LM prompts.
///
/// Formats prompts to request JSON output:
/// ```text
/// [Instructions]
///
/// Respond with JSON in this format:
/// {
///   "field1": "...",
///   "field2": "..."
/// }
///
/// Input:
/// {
///   "question": "..."
/// }
///
/// Output:
/// ```
#[derive(Clone, Copy)]
pub struct JSONAdapter {
    config: JSONConfig,
}

impl JSONAdapter {
    /// Create a new JSON adapter.
    pub const fn new(config: JSONConfig) -> Self {
        Self { config }
    }

    /// Create with default config.
    pub const fn default() -> Self {
        Self::new(JSONConfig::new())
    }

    /// Get the configuration.
    pub const fn config(&self) -> &JSONConfig {
        &self.config
    }

    /// Write JSON schema for output fields.
    fn write_schema(&self, buffer: &mut Vec<u8>, signature: &Signature<'_>) {
        buffer.extend_from_slice(b"{\n");

        for (i, field) in signature.output_fields.iter().enumerate() {
            let name = &field.name;
            if self.config.pretty {
                buffer.extend_from_slice(b"  ");
            }
            buffer.push(b'"');
            buffer.extend_from_slice(name.as_bytes());
            buffer.extend_from_slice(b"\": ");

            // Type hint
            buffer.extend_from_slice(b"\"<");
            if !field.desc.is_empty() {
                buffer.extend_from_slice(field.desc.as_bytes());
            } else {
                buffer.extend_from_slice(b"string");
            }
            buffer.extend_from_slice(b">\"");

            if i < signature.output_fields.len() - 1 {
                buffer.push(b',');
            }
            buffer.push(b'\n');
        }

        buffer.push(b'}');
    }

    /// Write JSON object for inputs.
    fn write_input_json(
        &self,
        buffer: &mut Vec<u8>,
        inputs: &Inputs<'_>,
        signature: &Signature<'_>,
    ) {
        buffer.extend_from_slice(b"{\n");

        let mut first = true;
        for field in &signature.input_fields {
            let name = &field.name;
            if let Some(value) = inputs.get(name.as_ref()) {
                if !first {
                    buffer.extend_from_slice(b",\n");
                }
                first = false;

                if self.config.pretty {
                    buffer.extend_from_slice(b"  ");
                }
                buffer.push(b'"');
                buffer.extend_from_slice(name.as_bytes());
                buffer.extend_from_slice(b"\": \"");
                // Escape JSON string
                write_escaped_json_string(buffer, value);
                buffer.push(b'"');
            }
        }

        buffer.push(b'\n');
        buffer.push(b'}');
    }

    /// Write demo as JSON.
    fn write_demo_json(
        &self,
        buffer: &mut Vec<u8>,
        demo: &DemoData<'_>,
        signature: &Signature<'_>,
    ) {
        use crate::intern::sym;

        // Input
        buffer.extend_from_slice(b"Input:\n{\n");
        let mut first = true;
        for field in &signature.input_fields {
            let field_sym = sym(&field.name);
            if let Some(value) = demo.get_input(field_sym) {
                if !first {
                    buffer.extend_from_slice(b",\n");
                }
                first = false;

                if self.config.pretty {
                    buffer.extend_from_slice(b"  ");
                }
                buffer.push(b'"');
                buffer.extend_from_slice(field.name.as_bytes());
                buffer.extend_from_slice(b"\": \"");
                write_escaped_json_string(buffer, value.as_str());
                buffer.push(b'"');
            }
        }
        buffer.extend_from_slice(b"\n}\n\n");

        // Output
        buffer.extend_from_slice(b"Output:\n{\n");
        first = true;
        for field in &signature.output_fields {
            let field_sym = sym(&field.name);
            if let Some(value) = demo.get_output(field_sym) {
                if !first {
                    buffer.extend_from_slice(b",\n");
                }
                first = false;

                if self.config.pretty {
                    buffer.extend_from_slice(b"  ");
                }
                buffer.push(b'"');
                buffer.extend_from_slice(field.name.as_bytes());
                buffer.extend_from_slice(b"\": \"");
                write_escaped_json_string(buffer, value.as_str());
                buffer.push(b'"');
            }
        }
        buffer.extend_from_slice(b"\n}\n");
    }
}

/// Write an escaped JSON string value.
fn write_escaped_json_string(buffer: &mut Vec<u8>, s: &str) {
    for c in s.chars() {
        match c {
            '"' => buffer.extend_from_slice(b"\\\""),
            '\\' => buffer.extend_from_slice(b"\\\\"),
            '\n' => buffer.extend_from_slice(b"\\n"),
            '\r' => buffer.extend_from_slice(b"\\r"),
            '\t' => buffer.extend_from_slice(b"\\t"),
            c if c.is_control() => {
                buffer.extend_from_slice(format!("\\u{:04x}", c as u32).as_bytes());
            }
            c => {
                let mut buf = [0u8; 4];
                buffer.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
            }
        }
    }
}

/// Parse a JSON string value, handling escapes.
#[allow(dead_code)] // Reserved for future use in response parsing
fn parse_json_string(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('"') => result.push('"'),
                Some('\\') => result.push('\\'),
                Some('n') => result.push('\n'),
                Some('r') => result.push('\r'),
                Some('t') => result.push('\t'),
                Some('u') => {
                    // Unicode escape
                    let hex: String = chars.by_ref().take(4).collect();
                    if let Ok(code) = u32::from_str_radix(&hex, 16) {
                        if let Some(c) = char::from_u32(code) {
                            result.push(c);
                        }
                    }
                }
                Some(c) => {
                    result.push('\\');
                    result.push(c);
                }
                None => result.push('\\'),
            }
        } else {
            result.push(c);
        }
    }

    result
}

/// Find a JSON field value in text.
/// Returns the range of the value (without quotes for strings).
fn find_json_field(text: &str, field_name: &str) -> Option<std::ops::Range<usize>> {
    let pattern = format!("\"{}\"", field_name);
    let field_start = text.find(&pattern)?;
    let after_field = field_start + pattern.len();

    // Skip whitespace and colon
    let rest = &text[after_field..];
    let colon_pos = rest.find(':')?;
    let after_colon = after_field + colon_pos + 1;

    let rest = &text[after_colon..];
    let value_start = rest.find(|c: char| !c.is_whitespace())?;
    let value_start = after_colon + value_start;

    // Determine value type
    let rest = &text[value_start..];
    let first_char = rest.chars().next()?;

    match first_char {
        '"' => {
            // String value
            let content_start = value_start + 1;
            let mut i = 0;
            let mut escaped = false;
            for c in rest[1..].chars() {
                if escaped {
                    escaped = false;
                    i += c.len_utf8();
                } else if c == '\\' {
                    escaped = true;
                    i += 1;
                } else if c == '"' {
                    return Some(content_start..content_start + i);
                } else {
                    i += c.len_utf8();
                }
            }
            None
        }
        '{' | '[' => {
            // Object or array - find matching bracket
            let open = first_char;
            let close = if open == '{' { '}' } else { ']' };
            let mut depth = 1;
            let mut i = 1;
            let mut in_string = false;
            let mut escaped = false;

            for c in rest[1..].chars() {
                if escaped {
                    escaped = false;
                } else if c == '\\' && in_string {
                    escaped = true;
                } else if c == '"' {
                    in_string = !in_string;
                } else if !in_string {
                    if c == open {
                        depth += 1;
                    } else if c == close {
                        depth -= 1;
                        if depth == 0 {
                            return Some(value_start..value_start + i + 1);
                        }
                    }
                }
                i += c.len_utf8();
            }
            None
        }
        _ => {
            // Number, boolean, or null
            let end = rest
                .find(|c: char| c == ',' || c == '}' || c == ']' || c.is_whitespace())
                .unwrap_or(rest.len());
            Some(value_start..value_start + end)
        }
    }
}

impl Adapter for JSONAdapter {
    fn format<'a>(
        &self,
        buffer: &'a mut Vec<u8>,
        signature: &Signature<'_>,
        inputs: &Inputs<'_>,
        demos: &[DemoData<'_>],
    ) -> StrView<'a> {
        buffer.clear();

        // Instructions
        if !signature.instructions.is_empty() {
            buffer.extend_from_slice(signature.instructions.as_bytes());
            buffer.extend_from_slice(b"\n\n");
        }

        // Schema
        if self.config.include_schema {
            buffer.extend_from_slice(b"Respond with JSON in this format:\n");
            self.write_schema(buffer, signature);
            buffer.extend_from_slice(b"\n\n");
        }

        // Demos
        for (i, demo) in demos.iter().enumerate() {
            if i > 0 {
                buffer.extend_from_slice(b"\n---\n\n");
            }
            self.write_demo_json(buffer, demo, signature);
        }

        // Separator
        if !demos.is_empty() {
            buffer.extend_from_slice(b"\n---\n\n");
        }

        // Input
        buffer.extend_from_slice(b"Input:\n");
        self.write_input_json(buffer, inputs, signature);
        buffer.extend_from_slice(b"\n\nOutput:\n");

        // SAFETY: We only write valid UTF-8
        unsafe { StrView::from_raw_parts(buffer.as_ptr(), buffer.len()) }
    }

    fn parse<'a>(
        &self,
        response: StrView<'a>,
        signature: &Signature<'_>,
    ) -> Result<SmallVec<[(Sym, FieldRange); 4]>> {
        let text = response.as_str();
        let mut fields = SmallVec::new();

        for field in &signature.output_fields {
            let field_name = &field.name;
            if let Some(range) = find_json_field(text, field_name) {
                fields.push((
                    sym(field_name),
                    FieldRange::new(range.start as u32, range.end as u32),
                ));
            }
        }

        Ok(fields)
    }

    fn name(&self) -> &'static str {
        "JSON"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::intern::sym;

    #[test]
    fn test_json_adapter_creation() {
        let adapter = JSONAdapter::default();
        assert_eq!(adapter.name(), "JSON");
        assert!(adapter.config().pretty);
    }

    #[test]
    fn test_json_config() {
        let config = JSONConfig::new().with_pretty(false).with_schema(false);
        assert!(!config.pretty);
        assert!(!config.include_schema);
    }

    #[test]
    fn test_escape_json_string() {
        let mut buffer = Vec::new();
        write_escaped_json_string(&mut buffer, "Hello\nWorld");
        assert_eq!(String::from_utf8(buffer).unwrap(), "Hello\\nWorld");

        let mut buffer = Vec::new();
        write_escaped_json_string(&mut buffer, "Say \"Hi\"");
        assert_eq!(String::from_utf8(buffer).unwrap(), "Say \\\"Hi\\\"");
    }

    #[test]
    fn test_parse_json_string() {
        assert_eq!(parse_json_string("Hello\\nWorld"), "Hello\nWorld");
        assert_eq!(parse_json_string("Say \\\"Hi\\\""), "Say \"Hi\"");
        assert_eq!(parse_json_string("Tab\\there"), "Tab\there");
    }

    #[test]
    fn test_find_json_field_string() {
        let json = r#"{"answer": "42", "other": "foo"}"#;
        let range = find_json_field(json, "answer");
        assert!(range.is_some());
        assert_eq!(&json[range.unwrap()], "42");
    }

    #[test]
    fn test_find_json_field_number() {
        let json = r#"{"count": 42, "name": "test"}"#;
        let range = find_json_field(json, "count");
        assert!(range.is_some());
        assert_eq!(&json[range.unwrap()], "42");
    }

    #[test]
    fn test_find_json_field_object() {
        let json = r#"{"data": {"a": 1, "b": 2}, "name": "test"}"#;
        let range = find_json_field(json, "data");
        assert!(range.is_some());
        assert_eq!(&json[range.unwrap()], r#"{"a": 1, "b": 2}"#);
    }

    #[test]
    fn test_format_basic() {
        let adapter = JSONAdapter::new(JSONConfig::new().with_schema(false));
        let sig = Signature::parse("question -> answer").unwrap();

        let mut buffer = Vec::new();
        let mut inputs = Inputs::new();
        inputs.insert("question", "What is 2+2?");

        let prompt = adapter.format(&mut buffer, &sig, &inputs, &[]);

        assert!(prompt.as_str().contains("\"question\": \"What is 2+2?\""));
        assert!(prompt.as_str().contains("Output:"));
    }

    #[test]
    fn test_parse_response() {
        let adapter = JSONAdapter::default();
        let sig = Signature::parse("question -> answer").unwrap();

        let response = StrView::new(r#"{"answer": "4"}"#);
        let fields = adapter.parse(response, &sig).unwrap();

        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].0, sym("answer"));

        let range = fields[0].1.as_range();
        assert_eq!(&response.as_str()[range], "4");
    }
}