use kcode_k1_chat_boxes::{
BoxId, ChatBox, ToolCallId, ToolMessageMetadata, ToolResultV2Metadata, USER_MESSAGE_TYPE,
tool_message_box, tool_result_v2_box,
};
use kcode_k1_chat_thread_web_code_open_targets::{
OpenLayout, OpenPresentation, TargetRejection, classify_original_target, present_open,
};
use kcode_k1_chat_web_code_output::{DocumentPart, WEB_CODE_OPEN_METADATA_TYPE};
use kcode_k1_transaction_id::TxId;
use kcode_k1_web_code_document::{CodeDocument, Language};
use kcode_k1_web_package::{AuthorityId, WebFamily, WebId};
use semver::Version;
fn tool_call_id() -> ToolCallId {
ToolCallId::new([0xab; 12], 42)
}
fn document() -> CodeDocument {
let family = WebFamily::new(AuthorityId::new(TxId::from_bytes([1; 12])), "alpha").unwrap();
let id = WebId::new(family, Version::new(1, 2, 3)).unwrap();
let documentation = b"<!-- k1-web/v1\n{\"dependencies\":[]}\n-->\nExact docs\r\n".to_vec();
let code = format!(
"export const first = \"{}\";\nexport const second = \"{}\";\n",
"a".repeat(850),
"b".repeat(850)
)
.into_bytes();
CodeDocument::new(id, documentation, Language::JavaScript, code).unwrap()
}
fn assign_id(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_box(id: u64, layout: OpenLayout, message_index: u64, message: &str) -> ChatBox {
assign_id(
id,
tool_message_box(&ToolMessageMetadata {
tool_call_id: layout.tool_call_id(),
originating_call: layout.originating_call(),
message_index,
message: message.to_owned(),
})
.unwrap(),
)
}
fn result_box(id: u64, presentation: &OpenPresentation) -> ChatBox {
assign_id(
id,
tool_result_v2_box(&ToolResultV2Metadata {
tool_call_id: presentation.layout.tool_call_id(),
originating_call: presentation.layout.originating_call(),
result: presentation.output.result.clone(),
metadata_type: presentation.output.metadata_type.clone(),
metadata_contents: presentation.output.metadata_contents.clone(),
}),
)
}
#[test]
fn present_open_preserves_exact_document_and_layout() {
let document = document();
let ranges = document.chunk_ranges();
assert_eq!(ranges.len(), 2);
let presentation = present_open(
BoxId::new(7),
tool_call_id(),
&document,
"authority",
"alpha",
"1.2.3",
);
assert_eq!(presentation.layout.originating_call(), BoxId::new(7));
assert_eq!(presentation.layout.tool_call_id(), tool_call_id());
assert_eq!(presentation.layout.code_message_count(), 2);
assert_eq!(presentation.output.tool_call_id, tool_call_id());
assert_eq!(presentation.output.messages.len(), 2);
assert_eq!(
presentation.output.messages[0].as_bytes(),
document.documentation()
);
assert_eq!(
presentation.output.messages[1].as_bytes(),
&document.code()[ranges[0].clone()]
);
assert_eq!(
presentation.output.result.as_ref().unwrap().as_bytes(),
&document.code()[ranges[1].clone()]
);
let reconstructed_code = presentation
.output
.messages
.iter()
.skip(1)
.map(String::as_str)
.chain(std::iter::once(
presentation.output.result.as_ref().unwrap().as_str(),
))
.collect::<String>();
assert_eq!(reconstructed_code.as_bytes(), document.code());
assert_eq!(
presentation.output.metadata_type,
WEB_CODE_OPEN_METADATA_TYPE
);
assert_eq!(
presentation.output.metadata_contents,
r#"{"status":"opened","authority":"authority","name":"alpha","version":"1.2.3","language":"javascript","documentation_message_index":1,"code_message_start":2,"code_message_count":2}"#
);
}
#[test]
fn classifies_each_original_open_output() {
let presentation = present_open(
BoxId::new(7),
tool_call_id(),
&document(),
"authority",
"alpha",
"1.2.3",
);
assert_eq!(presentation.layout.code_message_count(), 2);
let history = vec![
message_box(
101,
presentation.layout,
1,
&presentation.output.messages[0],
),
message_box(
102,
presentation.layout,
2,
&presentation.output.messages[1],
),
result_box(103, &presentation),
];
assert_eq!(
classify_original_target(&history, BoxId::new(101), presentation.layout),
Ok(DocumentPart::Documentation)
);
assert_eq!(
classify_original_target(&history, BoxId::new(102), presentation.layout),
Ok(DocumentPart::Code)
);
assert_eq!(
classify_original_target(&history, BoxId::new(103), presentation.layout),
Ok(DocumentPart::Code)
);
}
#[test]
fn rejects_absent_foreign_open_and_non_open_targets() {
let presentation = present_open(
BoxId::new(7),
tool_call_id(),
&document(),
"authority",
"alpha",
"1.2.3",
);
let foreign = assign_id(
201,
tool_message_box(&ToolMessageMetadata {
tool_call_id: ToolCallId::new([0xcd; 12], 43),
originating_call: presentation.layout.originating_call(),
message_index: 1,
message: "foreign documentation".to_owned(),
})
.unwrap(),
);
let non_open = ChatBox::new(
BoxId::new(202),
USER_MESSAGE_TYPE.to_owned(),
"ordinary message".to_owned(),
String::new(),
String::new(),
);
let history = vec![foreign, non_open];
assert_eq!(
classify_original_target(&history, BoxId::new(999), presentation.layout),
Err(TargetRejection::HistoryAbsent)
);
assert_eq!(
classify_original_target(&history, BoxId::new(201), presentation.layout),
Err(TargetRejection::DifferentOpen)
);
assert_eq!(
classify_original_target(&history, BoxId::new(202), presentation.layout),
Err(TargetRejection::NotOpenPayload)
);
}