@@ -19,70 +19,28 @@ export use ./mcp.nu
1919export use ./providers
2020export use ./provider .nu
2121export use ./prep .nu
22+ export use ./schema .nu
2223
2324export def document [
2425 path : string # Path to the document file
2526 -- name (- n ): string # Optional name for the document (defaults to filename)
2627 -- cache # Enable caching for this document
2728 -- bookmark (- b ): string # Bookmark this document registration
2829] {
29- # Validate file exists
30- if not ($path | path exists ) {
31- error make {
32- msg : $" File does not exist: ($path )"
33- label : {
34- text : " this path"
35- span : (metadata $path ).span
36- }
37- }
38- }
30+ # Generate normalized document turn using schema layer
31+ let normalized_turn = schema document-turn $path {name : $name cache : $cache }
3932
40- # Detect content type from file extension
41- let content_type = match ($path | path parse | get extension | str downcase ) {
42- " pdf" => " application/pdf"
43- " txt" => " text/plain"
44- " md" => " text/markdown"
45- " json" => " application/json"
46- " csv" => " text/csv"
47- " jpg" | " jpeg" => " image/jpeg"
48- " png" => " image/png"
49- " webp" => " image/webp"
50- " gif" => " image/gif"
51- " docx" => " application/vnd.openxmlformats-officedocument.wordprocessingml.document"
52- " xlsx" => " application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
53- _ => {
54- let detected = (file -- mime-type $path | split row " :" | get 1 | str trim )
55- print $" Warning: Unknown extension, detected MIME type: ($detected )"
56- $detected
57- }
58- }
33+ # Extract metadata for storage
34+ let metadata = $normalized_turn._metadata
35+ let clean_turn = $normalized_turn | reject _metadata
5936
60- # Check file size (Anthropic has limits)
61- let file_size = ($path | path expand | ls $in | get size | first )
62- if $file_size > 100MB {
63- # rough limit
64- error make {
65- msg : $" File too large: ($file_size ). Consider splitting or compressing."
66- }
67- }
68-
69- let document_name = $name | default ($path | path basename )
70- let content = open $path -- raw
71-
72- let meta = {
73- type : " document"
74- content_type : $content_type
75- role : " user"
76- document_name : $document_name
77- original_path : ($path | path expand )
78- file_size : $file_size
79- } | conditional-pipe $cache {
80- insert cache true
81- } | conditional-pipe ($bookmark | is-not-empty ) {
37+ # Build storage metadata
38+ let meta = $metadata | insert role " user" | conditional-pipe ($bookmark | is-not-empty ) {
8239 insert head $bookmark
8340 }
8441
85- let turn = $content | .append gpt.turn -- meta $meta
42+ # Store the clean normalized content
43+ let turn = $clean_turn.content | to json | .append gpt.turn -- meta $meta
8644
8745 $turn
8846}
@@ -100,11 +58,13 @@ export def main [
10058] {
10159 let content = if $in == null {
10260 input " Enter prompt: "
103- } else if ($in | describe ) == " list<string>" {
104- $in | str join $separator
10561 } else {
10662 $in
10763 }
64+
65+ # Generate normalized user turn using schema layer
66+ let normalized_turn = schema user-turn $content {json : $json cache : $cache separator : $separator }
67+
10868 let continues = $continues | append [] | each { ctx headish-to-id $in }
10969 let continues = $continues | conditional-pipe $respond { append (.head gpt.turn ).id }
11070
@@ -114,7 +74,7 @@ export def main [
11474
11575 let meta = (
11676 {
117- role : user
77+ role : $normalized_turn.role
11878 # options should be renamed to "inherited"
11979 options : (
12080 {}
@@ -124,12 +84,13 @@ export def main [
12484 )
12585 }
12686 | conditional-pipe ($head | is-not-empty ) { insert head $head }
127- | conditional-pipe $ cache { insert cache true }
87+ | conditional-pipe ( $normalized_turn. cache? == true ) { insert cache true }
12888 | if $continues != null { insert continues $continues } else { }
12989 | if $json { insert content_type " application/json" } else { }
13090 )
13191
132- let turn = $content | .append gpt.turn -- meta $meta
92+ # Store the clean normalized content
93+ let turn = $normalized_turn.content | to json | .append gpt.turn -- meta $meta
13394
13495 process-turn $turn
13596}
@@ -239,7 +200,7 @@ export def process-turn [turn: record] {
239200
240201 # save the assistance response
241202 let turn = $content | to json | .append gpt.turn -- meta $meta
242- print $" THIS SHOULND'T BE POSSIBLE: ($turn )"
203+ print $" THIS SHOULND'T BE POSSIBLE: ($turn )"
243204 $content | process-turn-response $turn
244205}
245206
0 commit comments