pi_core/
context_metadata.rs1use serde::{Deserialize, Serialize};
7use ts_rs::TS;
8
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
11#[serde(rename_all = "snake_case")]
12pub enum ContentKind {
13 FileRead,
14 CommandOutput,
15 Diff,
16 SearchResults,
17 DirectoryListing,
18 GenericText,
19}
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
23#[serde(rename_all = "snake_case", tag = "type")]
24pub enum ContextStrategy {
25 #[serde(rename = "keep_full")]
26 KeepFull,
27 #[serde(rename = "head")]
28 Head { max_chars: usize },
29 #[serde(rename = "tail")]
30 Tail { max_chars: usize },
31 #[serde(rename = "head_tail")]
32 HeadTail { head_chars: usize, tail_chars: usize },
33 #[serde(rename = "drop_if_old")]
34 DropIfOld,
35 #[serde(rename = "microcompacted")]
37 Microcompacted,
38}
39
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
42pub struct ToolResultContext {
43 pub content_kind: ContentKind,
44 pub strategy: ContextStrategy,
45 pub original_chars: usize,
46 pub truncated_by_tool: bool,
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub path: Option<String>,
49 #[serde(skip_serializing_if = "Option::is_none")]
50 pub exit_code: Option<i32>,
51}
52
53pub fn fallback_strategy(tool_name: &str) -> ContextStrategy {
55 match tool_name {
56 "read" => ContextStrategy::Head { max_chars: 2000 },
57 "bash" => ContextStrategy::Tail { max_chars: 2000 },
58 "edit" | "write" => ContextStrategy::KeepFull,
59 "grep" | "find" | "ls" => ContextStrategy::Head { max_chars: 2000 },
60 _ => ContextStrategy::Head { max_chars: 2000 },
61 }
62}