kcode-k1-chat-thread-web-code-open-targets 0.3.0

Pure source presentation and target classification for K1 Chat WebCode
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
#![forbid(unsafe_code)]
#![doc = include_str!("../Documentation.md")]

use kcode_k1_chat_boxes::{BoxId, ChatBox, ToolCallId};
use kcode_k1_chat_web_code_output::{WebCodeOutput, create, open};
use kcode_k1_web_code_document::{CodeDocument, Language};
use std::fmt::{Display, Formatter};

pub const NON_TEXT_SENTINEL: &str = "this file is a non-text object and cannot be modified";

#[derive(Clone, Debug, Eq, PartialEq)]
struct FileBinding {
    originating_call: BoxId,
    tool_call_id: ToolCallId,
    message_index: u64,
    path: String,
    is_text: bool,
    current: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SourceLayout {
    bindings: Vec<FileBinding>,
}

impl SourceLayout {
    pub fn originating_call(&self) -> Option<BoxId> {
        self.bindings
            .first()
            .map(|binding| binding.originating_call)
    }

    pub fn tool_call_id(&self) -> Option<ToolCallId> {
        self.bindings.first().map(|binding| binding.tool_call_id)
    }

    pub fn file_count(&self) -> usize {
        self.bindings.len()
    }

    pub fn record_file(
        &mut self,
        originating_call: BoxId,
        tool_call_id: ToolCallId,
        path: String,
        is_text: bool,
    ) -> Result<(), String> {
        if self
            .bindings
            .iter()
            .any(|binding| binding.current && binding.path == path)
        {
            return Err("source layout already contains the current path".to_owned());
        }
        self.bindings.push(FileBinding {
            originating_call,
            tool_call_id,
            message_index: 1,
            path,
            is_text,
            current: true,
        });
        Ok(())
    }

    pub fn retire_file(&mut self, path: &str) -> Result<(), String> {
        let binding = self
            .bindings
            .iter_mut()
            .rev()
            .find(|binding| binding.current && binding.path == path)
            .ok_or_else(|| "source layout does not contain the current path".to_owned())?;
        binding.current = false;
        Ok(())
    }
}

pub type OpenLayout = SourceLayout;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SourcePresentation {
    pub output: WebCodeOutput,
    pub layout: SourceLayout,
}

pub type OpenPresentation = SourcePresentation;

pub fn present_create(
    originating_call: BoxId,
    tool_call_id: ToolCallId,
    document: &CodeDocument,
    version: &str,
) -> SourcePresentation {
    present(originating_call, tool_call_id, document, version, true)
}

pub fn present_open(
    originating_call: BoxId,
    tool_call_id: ToolCallId,
    document: &CodeDocument,
    version: &str,
) -> SourcePresentation {
    present(originating_call, tool_call_id, document, version, false)
}

fn present(
    originating_call: BoxId,
    tool_call_id: ToolCallId,
    document: &CodeDocument,
    version: &str,
    creating: bool,
) -> SourcePresentation {
    let mut messages = Vec::with_capacity(document.files().len());
    let mut paths = Vec::with_capacity(document.files().len());
    let mut bindings = Vec::with_capacity(document.files().len());

    for (offset, file) in document.files().iter().enumerate() {
        let message_index = u64::try_from(offset + 1).expect("file message index fits u64");
        let is_text = std::str::from_utf8(file.bytes()).is_ok();
        messages.push(file_message(file.bytes()));
        paths.push(file.path().to_owned());
        bindings.push(FileBinding {
            originating_call,
            tool_call_id,
            message_index,
            path: file.path().to_owned(),
            is_text,
            current: true,
        });
    }

    let name = document.family().logical_name();
    let language = presentation_language(document);
    let result = if creating {
        Ok(format!("Created {version}."))
    } else {
        Ok(format!("Opened {version}."))
    };
    let output = if creating {
        create(
            tool_call_id,
            messages,
            result,
            name,
            version,
            language,
            &paths,
        )
    } else {
        open(
            tool_call_id,
            messages,
            result,
            name,
            version,
            language,
            &paths,
        )
    };

    SourcePresentation {
        output,
        layout: SourceLayout { bindings },
    }
}

fn presentation_language(document: &CodeDocument) -> Language {
    let javascript = document.file("index.js").is_some();
    let html = document.file("index.html").is_some();
    let css = document.file("index.css").is_some();
    match (javascript, html, css) {
        (false, true, false) => Language::Html,
        (false, false, true) => Language::Css,
        _ => Language::JavaScript,
    }
}

pub fn file_message(bytes: &[u8]) -> String {
    std::str::from_utf8(bytes)
        .map(str::to_owned)
        .unwrap_or_else(|_| NON_TEXT_SENTINEL.to_owned())
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FileTarget {
    path: String,
    is_text: bool,
}

impl FileTarget {
    pub fn path(&self) -> &str {
        &self.path
    }

    pub const fn is_text(&self) -> bool {
        self.is_text
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TargetRejection {
    HistoryAbsent,
    HistoryDuplicate,
    MalformedPayload,
    DifferentOpen,
    MessageOutOfLayout,
    NotSourceMessage,
    RetiredFile,
}

impl TargetRejection {
    pub const fn code(self) -> &'static str {
        match self {
            Self::HistoryAbsent => "history_absent",
            Self::HistoryDuplicate => "history_duplicate",
            Self::MalformedPayload => "malformed_payload",
            Self::DifferentOpen => "different_open",
            Self::MessageOutOfLayout => "message_out_of_layout",
            Self::NotSourceMessage => "not_source_message",
            Self::RetiredFile => "retired_file",
        }
    }
}

impl Display for TargetRejection {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(match self {
            Self::HistoryAbsent => "target box is absent from history",
            Self::HistoryDuplicate => "target box ID occurs more than once in history",
            Self::MalformedPayload => "target has malformed tool metadata",
            Self::DifferentOpen => "target belongs to a different source generation",
            Self::MessageOutOfLayout => "target message index is outside the source layout",
            Self::NotSourceMessage => "target is not a source file message",
            Self::RetiredFile => "target no longer represents a current file",
        })
    }
}

impl std::error::Error for TargetRejection {}

pub fn classify_anchor(
    history: &[ChatBox],
    target_id: BoxId,
    layout: &SourceLayout,
) -> Result<FileTarget, TargetRejection> {
    classify(history, target_id, layout, false)
}

pub fn classify_file_target(
    history: &[ChatBox],
    target_id: BoxId,
    layout: &SourceLayout,
) -> Result<FileTarget, TargetRejection> {
    classify(history, target_id, layout, true)
}

fn classify(
    history: &[ChatBox],
    target_id: BoxId,
    layout: &SourceLayout,
    require_current: bool,
) -> Result<FileTarget, TargetRejection> {
    let mut matching = history.iter().filter(|value| value.id() == target_id);
    let target = matching.next().ok_or(TargetRejection::HistoryAbsent)?;
    if matching.next().is_some() {
        return Err(TargetRejection::HistoryDuplicate);
    }
    let message = target
        .tool_message_metadata()
        .map_err(|_| TargetRejection::MalformedPayload)?
        .ok_or(TargetRejection::NotSourceMessage)?;

    let same_call = layout.bindings.iter().any(|binding| {
        binding.tool_call_id == message.tool_call_id
            && binding.originating_call == message.originating_call
    });
    if !same_call {
        return Err(TargetRejection::DifferentOpen);
    }
    let binding = layout
        .bindings
        .iter()
        .find(|binding| {
            binding.tool_call_id == message.tool_call_id
                && binding.originating_call == message.originating_call
                && binding.message_index == message.message_index
        })
        .ok_or(TargetRejection::MessageOutOfLayout)?;
    if require_current && !binding.current {
        return Err(TargetRejection::RetiredFile);
    }
    Ok(FileTarget {
        path: binding.path.clone(),
        is_text: binding.is_text,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use kcode_k1_chat_boxes::{ToolMessageMetadata, named_tool_message_box};
    use kcode_k1_transaction_id::TxId;
    use kcode_k1_web_package::{AuthorityId, SourceFile, WebFamily};

    fn call(byte: u8) -> ToolCallId {
        ToolCallId::new([byte; 12], 4)
    }

    fn document() -> CodeDocument {
        CodeDocument::new(
            WebFamily::new(AuthorityId::new(TxId::from_bytes([1; 12])), "alpha").unwrap(),
            vec![
                SourceFile::new("z/binary.dat", vec![0xff, 0x00]),
                SourceFile::new("Documentation.md", b"Docs\r\n".to_vec()),
                SourceFile::new("src/a.odd", b"export const value = 1;\n".to_vec()),
            ],
        )
        .unwrap()
    }

    fn assign(id: u64, value: ChatBox) -> ChatBox {
        ChatBox::new(
            BoxId::new(id),
            value.box_type().to_owned(),
            value.contents().to_owned(),
            value.hidden_type().to_owned(),
            value.hidden_contents().to_owned(),
        )
    }

    fn message(
        id: u64,
        originating_call: BoxId,
        tool_call_id: ToolCallId,
        index: u64,
        title: &str,
        body: &str,
    ) -> ChatBox {
        assign(
            id,
            named_tool_message_box(
                &ToolMessageMetadata {
                    tool_call_id,
                    originating_call,
                    message_index: index,
                    message: body.to_owned(),
                },
                title,
            )
            .unwrap(),
        )
    }

    #[test]
    fn complete_files_are_sorted_named_and_binary_safe() {
        let opened = present_open(BoxId::new(7), call(2), &document(), "1.2.3");
        assert_eq!(
            opened.output.message_titles,
            [
                Some("File: Documentation.md".to_owned()),
                Some("File: src/a.odd".to_owned()),
                Some("File: z/binary.dat".to_owned()),
            ]
        );
        assert_eq!(opened.output.messages[0], "Docs\r\n");
        assert_eq!(opened.output.messages[1], "export const value = 1;\n");
        assert_eq!(opened.output.messages[2], NON_TEXT_SENTINEL);
        assert_eq!(opened.layout.file_count(), 3);
    }

    #[test]
    fn retired_and_incremental_messages_keep_anchor_semantics() {
        let mut layout = present_create(BoxId::new(7), call(2), &document(), "draft").layout;
        let docs = message(
            10,
            BoxId::new(7),
            call(2),
            1,
            "File: Documentation.md",
            "Docs\r\n",
        );
        let binary = message(
            11,
            BoxId::new(7),
            call(2),
            3,
            "File: z/binary.dat",
            NON_TEXT_SENTINEL,
        );
        layout.retire_file("z/binary.dat").unwrap();
        assert_eq!(
            classify_anchor(std::slice::from_ref(&binary), BoxId::new(11), &layout,)
                .unwrap()
                .path(),
            "z/binary.dat"
        );
        assert_eq!(
            classify_file_target(std::slice::from_ref(&binary), BoxId::new(11), &layout,),
            Err(TargetRejection::RetiredFile)
        );
        assert!(
            classify_file_target(std::slice::from_ref(&docs), BoxId::new(10), &layout,)
                .unwrap()
                .is_text()
        );

        layout
            .record_file(BoxId::new(20), call(3), "new/file.txt".to_owned(), true)
            .unwrap();
        let added = message(21, BoxId::new(20), call(3), 1, "File: new/file.txt", "new");
        assert_eq!(
            classify_file_target(std::slice::from_ref(&added), BoxId::new(21), &layout,)
                .unwrap()
                .path(),
            "new/file.txt"
        );
    }
}