Skip to main content

pi_core/
events.rs

1use serde::{Deserialize, Serialize};
2use ts_rs::TS;
3
4use crate::message::{AgentMessage, ToolCall, ToolResultMessage};
5use crate::tool::ToolResult as ToolExecResult;
6use crate::types::{SessionId, ToolArguments, ToolCallId, ToolName};
7
8// ---------------------------------------------------------------------------
9// Tool execution lifecycle types
10// ---------------------------------------------------------------------------
11
12/// Which output stream a tool update chunk came from.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
14#[serde(rename_all = "snake_case")]
15#[ts(rename_all = "snake_case")]
16pub enum ToolOutputStream {
17    Stdout,
18    Stderr,
19    Status,
20}
21
22/// Reason a tool was cancelled.
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
24#[serde(tag = "type", rename_all = "snake_case")]
25#[ts(tag = "type", rename_all = "snake_case")]
26pub enum CancelReason {
27    UserRequested,
28    Timeout,
29    AgentAborted,
30    DependencyFailed { cause_tool_call_id: ToolCallId },
31}
32
33/// A streaming update from a running tool execution.
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
35pub struct ToolExecutionUpdate {
36    pub tool_call_id: ToolCallId,
37    pub stream: ToolOutputStream,
38    pub chunk: String,
39    pub sequence: u64,
40    pub timestamp: u64,
41}
42
43/// Reference to a background job started by a tool.
44#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
45pub struct BackgroundJobRef {
46    pub job_id: String,
47    pub tool_call_id: ToolCallId,
48    pub command_label: String,
49}
50
51// ---------------------------------------------------------------------------
52// Actions and events
53// ---------------------------------------------------------------------------
54
55/// Actions that the core requests the host to perform.
56#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
57#[serde(tag = "type", rename_all = "snake_case")]
58#[ts(tag = "type", rename_all = "snake_case")]
59#[ts(export)]
60pub enum AgentAction {
61    StreamLlm {
62        context: crate::context::LlmContext,
63        #[serde(skip_serializing_if = "Option::is_none")]
64        session_id: Option<SessionId>,
65    },
66    ExecuteTools {
67        calls: Vec<ToolCall>,
68    },
69    CancelTools {
70        tool_call_ids: Vec<ToolCallId>,
71        reason: CancelReason,
72    },
73    WaitForInput {
74        mode: WaitMode,
75    },
76    Finished {
77        messages: Vec<AgentMessage>,
78    },
79}
80
81/// Events emitted by the core to notify the host of state changes.
82#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
83#[serde(tag = "type", rename_all = "snake_case")]
84#[ts(tag = "type", rename_all = "snake_case")]
85#[ts(export)]
86pub enum AgentEvent {
87    AgentStart,
88    AgentEnd {
89        messages: Vec<AgentMessage>,
90    },
91    TurnStart,
92    TurnEnd {
93        message: AgentMessage,
94        tool_results: Vec<ToolResultMessage>,
95    },
96    MessageStart {
97        message: AgentMessage,
98    },
99    MessageUpdate {
100        message: AgentMessage,
101        delta: ContentDelta,
102    },
103    MessageEnd {
104        message: AgentMessage,
105    },
106    ToolExecutionStart {
107        tool_call_id: ToolCallId,
108        tool_name: ToolName,
109        #[serde(skip_serializing_if = "Option::is_none")]
110        #[ts(type = "object | undefined")]
111        args: Option<ToolArguments>,
112    },
113    ToolExecutionUpdate {
114        tool_call_id: ToolCallId,
115        stream: ToolOutputStream,
116        chunk: String,
117        sequence: u64,
118        timestamp: u64,
119    },
120    ToolExecutionEnd {
121        tool_call_id: ToolCallId,
122        result: ToolExecResult,
123        is_error: bool,
124    },
125    ToolExecutionCancelled {
126        tool_call_id: ToolCallId,
127        reason: CancelReason,
128    },
129    QueueUpdate {
130        steer: Vec<AgentMessage>,
131        follow_up: Vec<AgentMessage>,
132    },
133    SavePoint {
134        had_pending_writes: bool,
135    },
136    Settled,
137}
138
139/// A delta describing how a streaming assistant message changed.
140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
141#[serde(tag = "kind", rename_all = "snake_case")]
142#[ts(tag = "kind", rename_all = "snake_case")]
143pub enum ContentDelta {
144    TextStart,
145    TextDelta {
146        text: String,
147    },
148    TextEnd,
149    ThinkingStart,
150    ThinkingDelta {
151        text: String,
152    },
153    ThinkingEnd,
154    ToolCallStart {
155        tool_call: ToolCall,
156    },
157    ToolCallDelta {
158        tool_call_id: ToolCallId,
159        #[ts(type = "object")]
160        delta: serde_json::Value,
161    },
162    ToolCallEnd {
163        tool_call_id: ToolCallId,
164    },
165}
166
167/// Controls how queued messages are drained.
168#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default, TS)]
169#[serde(rename_all = "snake_case")]
170#[ts(rename_all = "snake_case")]
171pub enum QueueMode {
172    #[default]
173    OneAtATime,
174    All,
175}
176
177/// Reasoning / thinking level for models that support it.
178#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default, TS)]
179#[serde(rename_all = "snake_case")]
180#[ts(rename_all = "snake_case")]
181pub enum ThinkingLevel {
182    #[default]
183    Off,
184    Minimal,
185    Low,
186    Medium,
187    High,
188    Xhigh,
189}
190
191/// What kind of input the core is waiting for.
192#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, TS)]
193#[serde(rename_all = "snake_case")]
194#[ts(rename_all = "snake_case")]
195pub enum WaitMode {
196    Steering,
197    FollowUp,
198    Any,
199}