forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.rs
More file actions
733 lines (671 loc) · 23.3 KB
/
Copy pathmessage.rs
File metadata and controls
733 lines (671 loc) · 23.3 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
use crate::bus::{BackgroundTaskCompleted, BackgroundTaskProgressEvent, BackgroundTaskStatus};
use chrono::Utc;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::sync::OnceLock;
mod notifications;
pub use notifications::{
InputShellResult, ParsedBackgroundTaskNotification, ParsedBackgroundTaskProgressNotification,
background_task_display_label, background_task_status_notice,
format_background_task_notification_markdown, format_background_task_progress_markdown,
format_input_shell_result_markdown, input_shell_status_notice,
parse_background_task_notification_markdown,
parse_background_task_progress_notification_markdown,
};
fn compile_static_regex(pattern: &str) -> Option<Regex> {
match Regex::new(pattern) {
Ok(regex) => Some(regex),
Err(err) => {
eprintln!("jcode: failed to compile static regex: {err}");
None
}
}
}
fn compile_static_regexes(patterns: &[&str]) -> Vec<Regex> {
patterns
.iter()
.filter_map(|pattern| compile_static_regex(pattern))
.collect()
}
/// Role in conversation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Role {
User,
Assistant,
}
/// Plain-text tool output placeholder when execution was interrupted.
pub const TOOL_OUTPUT_MISSING_TEXT: &str =
"Tool output missing (session interrupted before tool execution completed)";
/// A message in the conversation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
pub content: Vec<ContentBlock>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub timestamp: Option<chrono::DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_duration_ms: Option<u64>,
}
/// Cache control metadata for prompt caching
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheControl {
#[serde(rename = "type")]
pub kind: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub ttl: Option<String>,
}
impl CacheControl {
pub fn ephemeral(ttl: Option<String>) -> Self {
Self {
kind: "ephemeral".to_string(),
ttl,
}
}
}
/// Content block within a message
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlock {
Text {
text: String,
#[serde(skip_serializing_if = "Option::is_none")]
cache_control: Option<CacheControl>,
},
/// Hidden reasoning content used for providers that require it (not displayed)
Reasoning {
text: String,
},
ToolUse {
id: String,
name: String,
input: serde_json::Value,
},
ToolResult {
tool_use_id: String,
content: String,
#[serde(skip_serializing_if = "Option::is_none")]
is_error: Option<bool>,
},
Image {
media_type: String,
data: String,
},
/// Hidden OpenAI Responses compaction item used to preserve native
/// compaction state across turns/saves when jcode explicitly triggers it.
OpenAICompaction {
encrypted_content: String,
},
}
impl Message {
pub fn user(text: &str) -> Self {
Self {
role: Role::User,
content: vec![ContentBlock::Text {
text: text.to_string(),
cache_control: None,
}],
timestamp: Some(Utc::now()),
tool_duration_ms: None,
}
}
pub fn user_with_images(text: &str, images: Vec<(String, String)>) -> Self {
let mut content: Vec<ContentBlock> = images
.into_iter()
.map(|(media_type, data)| ContentBlock::Image { media_type, data })
.collect();
content.push(ContentBlock::Text {
text: text.to_string(),
cache_control: None,
});
Self {
role: Role::User,
content,
timestamp: Some(Utc::now()),
tool_duration_ms: None,
}
}
pub fn assistant_text(text: &str) -> Self {
Self {
role: Role::Assistant,
content: vec![ContentBlock::Text {
text: text.to_string(),
cache_control: None,
}],
timestamp: Some(Utc::now()),
tool_duration_ms: None,
}
}
pub fn tool_result(tool_use_id: &str, content: &str, is_error: bool) -> Self {
Self::tool_result_with_duration(tool_use_id, content, is_error, None)
}
pub fn tool_result_with_duration(
tool_use_id: &str,
content: &str,
is_error: bool,
tool_duration_ms: Option<u64>,
) -> Self {
Self {
role: Role::User,
content: vec![ContentBlock::ToolResult {
tool_use_id: tool_use_id.to_string(),
content: content.to_string(),
is_error: if is_error { Some(true) } else { None },
}],
timestamp: Some(Utc::now()),
tool_duration_ms,
}
}
/// Format a timestamp deterministically in UTC for injection into model-visible content.
pub fn format_timestamp(ts: &chrono::DateTime<Utc>) -> String {
ts.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)
}
pub fn format_duration(duration_ms: u64) -> String {
match duration_ms {
0..=999 => format!("{}ms", duration_ms),
1_000..=9_999 => format!("{:.1}s", duration_ms as f64 / 1000.0),
10_000..=59_999 => format!("{}s", duration_ms / 1000),
_ => {
let total_seconds = duration_ms / 1000;
let minutes = total_seconds / 60;
let seconds = total_seconds % 60;
if seconds == 0 {
format!("{}m", minutes)
} else {
format!("{}m {}s", minutes, seconds)
}
}
}
}
pub fn is_internal_system_reminder(&self) -> bool {
self.content
.iter()
.find_map(|block| match block {
ContentBlock::Text { text, .. } => Some(text.trim_start()),
_ => None,
})
.is_some_and(|text| text.starts_with("<system-reminder>"))
}
fn should_skip_timestamp_injection(&self) -> bool {
self.is_internal_system_reminder()
}
fn tool_result_tag(&self, ts: &chrono::DateTime<Utc>) -> String {
match self.tool_duration_ms {
Some(duration_ms) => {
let duration_ms_i64 = i64::try_from(duration_ms).unwrap_or(i64::MAX);
let start_ts = ts
.checked_sub_signed(chrono::Duration::milliseconds(duration_ms_i64))
.unwrap_or(*ts);
format!(
"[tool timing: start={} finish={} duration={}]",
Self::format_timestamp(&start_ts),
Self::format_timestamp(ts),
Self::format_duration(duration_ms)
)
}
None => format!("[{}]", Self::format_timestamp(ts)),
}
}
/// Return a copy of messages with timestamps injected into user-role text content.
/// Tool results get a stable UTC timing header prepended to content.
/// User text messages get a stable UTC timestamp prepended to the first text block.
pub fn with_timestamps(messages: &[Message]) -> Vec<Message> {
messages
.iter()
.map(|msg| {
let Some(ts) = msg.timestamp else {
return msg.clone();
};
if msg.role != Role::User || msg.should_skip_timestamp_injection() {
return msg.clone();
}
let text_tag = format!("[{}]", Self::format_timestamp(&ts));
let tool_result_tag = msg.tool_result_tag(&ts);
let mut msg = msg.clone();
let mut tagged = false;
for block in &mut msg.content {
match block {
ContentBlock::Text { text, .. } if !tagged => {
*text = format!("{} {}", text_tag, text);
tagged = true;
}
ContentBlock::ToolResult { content, .. } if !tagged => {
*content = format!("{} {}", tool_result_tag, content);
tagged = true;
}
_ => {}
}
}
msg
})
.collect()
}
}
const STABLE_HASH_SEED: u64 = 0xcbf29ce484222325;
const STABLE_HASH_PRIME: u64 = 0x100000001b3;
fn stable_hash_bytes(bytes: &[u8]) -> u64 {
let mut hash = STABLE_HASH_SEED;
for byte in bytes {
hash ^= *byte as u64;
hash = hash.wrapping_mul(STABLE_HASH_PRIME);
}
hash
}
pub(crate) fn extend_stable_hash(acc: u64, next: u64) -> u64 {
stable_hash_bytes(&[acc.to_le_bytes().as_slice(), next.to_le_bytes().as_slice()].concat())
}
pub(crate) fn stable_message_hash(message: &Message) -> u64 {
match serde_json::to_vec(message) {
Ok(bytes) => stable_hash_bytes(&bytes),
Err(_) => stable_hash_bytes(format!("{:?}", message).as_bytes()),
}
}
pub fn ends_with_fresh_user_turn(messages: &[Message]) -> bool {
for msg in messages.iter().rev() {
if msg.role != Role::User {
return false;
}
if msg
.content
.iter()
.any(|block| matches!(block, ContentBlock::ToolResult { .. }))
{
return false;
}
if msg.content.is_empty() {
return false;
}
let mut saw_user_text = false;
for block in &msg.content {
match block {
ContentBlock::Text { text, .. } => {
let trimmed = text.trim();
if !trimmed.is_empty() && !trimmed.starts_with("<system-reminder>") {
saw_user_text = true;
}
}
_ => return false,
}
}
if saw_user_text {
return true;
}
if msg.is_internal_system_reminder() {
continue;
}
return false;
}
false
}
/// Sanitize a tool ID so it matches the pattern `^[a-zA-Z0-9_-]+$`.
///
/// Different providers generate tool IDs in different formats. When switching
/// from one provider to another mid-conversation, the historical tool IDs may
/// contain characters that the new provider rejects (e.g., dots in Copilot IDs
/// sent to Anthropic). This function replaces any invalid characters with
/// underscores.
pub fn sanitize_tool_id(id: &str) -> String {
if id.is_empty() {
return "unknown".to_string();
}
let sanitized: String = id
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '_' || c == '-' {
c
} else {
'_'
}
})
.collect();
if sanitized.is_empty() {
"unknown".to_string()
} else {
sanitized
}
}
/// Redact likely secrets from persisted tool output.
///
/// This is a best-effort safeguard for local session history files. It targets
/// high-confidence token/key patterns and common `KEY=VALUE` assignments used by
/// auth flows.
pub fn redact_secrets(text: &str) -> String {
// Fast path to avoid regex work for most tool outputs.
let lower = text.to_ascii_lowercase();
if !text.contains("sk-")
&& !text.contains("ghp_")
&& !text.contains("github_pat_")
&& !text.contains("AIza")
&& !text.contains("ya29.")
&& !text.contains("xox")
&& !lower.contains("api_key")
&& !lower.contains("token")
{
return text.to_string();
}
static DIRECT_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();
static ASSIGNMENT_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();
let direct_patterns = DIRECT_PATTERNS.get_or_init(|| {
compile_static_regexes(&[
r"sk-ant-(?:oat|ort)01-[A-Za-z0-9_-]{20,}",
r"sk-or-v1-[A-Za-z0-9_-]{20,}",
r"ghp_[A-Za-z0-9]{20,}",
r"github_pat_[A-Za-z0-9_]{20,}",
r"ya29\.[A-Za-z0-9._-]{20,}",
r"AIza[0-9A-Za-z_-]{20,}",
r"xox[baprs]-[A-Za-z0-9-]{10,}",
])
});
let assignment_patterns = ASSIGNMENT_PATTERNS.get_or_init(|| {
compile_static_regexes(&[
r"(?m)^\s*(OPENROUTER_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(OPENCODE_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(OPENCODE_GO_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(ZHIPU_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(ZAI_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(302AI_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(BASETEN_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(CORTECS_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(DEEPSEEK_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(FIRMWARE_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(HF_TOKEN\s*=\s*)[^\r\n]+",
r"(?m)^\s*(MOONSHOT_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(NEBIUS_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(SCALEWAY_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(STACKIT_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(GROQ_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(MISTRAL_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(PERPLEXITY_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(TOGETHER_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(DEEPINFRA_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(XAI_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(LMSTUDIO_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(OLLAMA_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(CHUTES_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(CEREBRAS_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(OPENAI_COMPAT_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(ANTHROPIC_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(OPENAI_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(AZURE_OPENAI_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(CURSOR_API_KEY\s*=\s*)[^\r\n]+",
r"(?m)^\s*(GITHUB_TOKEN\s*=\s*)[^\r\n]+",
])
});
let mut redacted = text.to_string();
let mut redacted_keys: HashSet<String> = [
"OPENROUTER_API_KEY",
"OPENCODE_API_KEY",
"OPENCODE_GO_API_KEY",
"ZHIPU_API_KEY",
"ZAI_API_KEY",
"302AI_API_KEY",
"BASETEN_API_KEY",
"CORTECS_API_KEY",
"DEEPSEEK_API_KEY",
"FIRMWARE_API_KEY",
"HF_TOKEN",
"MOONSHOT_API_KEY",
"NEBIUS_API_KEY",
"SCALEWAY_API_KEY",
"STACKIT_API_KEY",
"GROQ_API_KEY",
"MISTRAL_API_KEY",
"PERPLEXITY_API_KEY",
"TOGETHER_API_KEY",
"DEEPINFRA_API_KEY",
"XAI_API_KEY",
"LMSTUDIO_API_KEY",
"OLLAMA_API_KEY",
"CHUTES_API_KEY",
"CEREBRAS_API_KEY",
"OPENAI_COMPAT_API_KEY",
"ANTHROPIC_API_KEY",
"OPENAI_API_KEY",
"AZURE_OPENAI_API_KEY",
"CURSOR_API_KEY",
"GITHUB_TOKEN",
]
.iter()
.map(|k| (*k).to_string())
.collect();
for re in direct_patterns {
redacted = re.replace_all(&redacted, "[REDACTED_SECRET]").into_owned();
}
for re in assignment_patterns {
redacted = re
.replace_all(&redacted, "${1}[REDACTED_SECRET]")
.into_owned();
}
// Also redact custom API key variable names configured at runtime.
for source in [
"JCODE_OPENROUTER_API_KEY_NAME",
"JCODE_OPENAI_COMPAT_API_KEY_NAME",
] {
let Some(key_name) = std::env::var(source)
.ok()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
else {
continue;
};
if !key_name
.chars()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
{
continue;
}
if !redacted_keys.insert(key_name.clone()) {
continue;
}
let pattern = format!(r"(?m)^\s*({}\s*=\s*)[^\r\n]+", regex::escape(&key_name));
if let Ok(re) = Regex::new(&pattern) {
redacted = re
.replace_all(&redacted, "${1}[REDACTED_SECRET]")
.into_owned();
}
}
redacted
}
/// Tool definition for the API
#[derive(Debug, Clone, Serialize)]
pub struct ToolDefinition {
pub name: String,
// Prompt-visible text sent to the model by provider adapters.
// Approximate prompt cost: description.len() / 4. Use
// ToolDefinition::description_token_estimate() when reviewing tool bloat.
pub description: String,
pub input_schema: serde_json::Value,
}
impl ToolDefinition {
/// Serialized size of the full tool definition payload sent to providers.
pub fn prompt_chars(&self) -> usize {
serde_json::json!({
"name": self.name,
"description": self.description,
"input_schema": self.input_schema,
})
.to_string()
.len()
}
/// Approximate prompt-token cost of this tool's top-level description.
///
/// This uses jcode's standard chars/4 heuristic, matching other token
/// budget estimates in the codebase.
pub fn description_token_estimate(&self) -> usize {
crate::util::estimate_tokens(&self.description)
}
/// Approximate prompt-token cost of the full tool definition payload.
pub fn prompt_token_estimate(&self) -> usize {
crate::util::estimate_tokens(
&serde_json::json!({
"name": self.name,
"description": self.description,
"input_schema": self.input_schema,
})
.to_string(),
)
}
pub fn aggregate_prompt_chars(defs: &[ToolDefinition]) -> usize {
defs.iter().map(Self::prompt_chars).sum()
}
pub fn aggregate_prompt_token_estimate(defs: &[ToolDefinition]) -> usize {
defs.iter().map(Self::prompt_token_estimate).sum()
}
}
/// A tool call from the model
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ToolCall {
#[serde(default)]
pub id: String,
#[serde(default)]
pub name: String,
#[serde(default)]
pub input: serde_json::Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub intent: Option<String>,
}
impl ToolCall {
pub fn intent_from_input(input: &serde_json::Value) -> Option<String> {
input
.get("intent")
.and_then(|value| value.as_str())
.map(str::trim)
.filter(|intent| !intent.is_empty())
.map(ToString::to_string)
}
pub fn refresh_intent_from_input(&mut self) {
self.intent = Self::intent_from_input(&self.input);
}
}
pub const GENERATED_IMAGE_TOOL_NAME: &str = "image_generation";
pub fn generated_image_tool_input(
path: &str,
metadata_path: Option<&str>,
output_format: &str,
revised_prompt: Option<&str>,
) -> serde_json::Value {
serde_json::json!({
"path": path,
"metadata_path": metadata_path,
"output_format": output_format,
"revised_prompt": revised_prompt,
})
}
pub fn generated_image_summary(
path: &str,
metadata_path: Option<&str>,
output_format: &str,
revised_prompt: Option<&str>,
) -> String {
let mut summary = format!("Generated image ({}) saved to `{}`.", output_format, path);
if let Some(metadata_path) = metadata_path {
summary.push_str(&format!("\nMetadata saved to `{}`.", metadata_path));
}
if let Some(revised_prompt) = revised_prompt.filter(|prompt| !prompt.trim().is_empty()) {
summary.push_str("\n\nRevised prompt:\n");
summary.push_str(revised_prompt.trim());
}
summary
}
/// Connection phase for status bar transparency
#[derive(Debug, Clone, PartialEq)]
pub enum ConnectionPhase {
/// Refreshing OAuth token
Authenticating,
/// TCP + TLS connection to API
Connecting,
/// HTTP request sent, waiting for first response byte
WaitingForResponse,
/// First byte received, stream is active
Streaming,
/// Retrying after a transient error
Retrying { attempt: u32, max: u32 },
}
impl std::fmt::Display for ConnectionPhase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConnectionPhase::Authenticating => write!(f, "authenticating"),
ConnectionPhase::Connecting => write!(f, "connecting"),
ConnectionPhase::WaitingForResponse => write!(f, "waiting for response"),
ConnectionPhase::Streaming => write!(f, "streaming"),
ConnectionPhase::Retrying { attempt, max } => {
write!(f, "retrying ({}/{})", attempt, max)
}
}
}
}
/// Streaming event from provider
#[derive(Debug, Clone)]
pub enum StreamEvent {
/// Text content delta
TextDelta(String),
/// Tool use started
ToolUseStart { id: String, name: String },
/// Tool input delta (JSON fragment)
ToolInputDelta(String),
/// Tool use complete
ToolUseEnd,
/// Tool result from provider (provider already executed the tool)
ToolResult {
tool_use_id: String,
content: String,
is_error: bool,
},
/// Image generated by a provider-native image generation tool.
GeneratedImage {
id: String,
path: String,
metadata_path: Option<String>,
output_format: String,
revised_prompt: Option<String>,
},
/// Extended thinking started
ThinkingStart,
/// Extended thinking delta (reasoning content)
ThinkingDelta(String),
/// Extended thinking ended
ThinkingEnd,
/// Extended thinking completed with duration
ThinkingDone { duration_secs: f64 },
/// Message complete (may have stop reason)
MessageEnd { stop_reason: Option<String> },
/// Token usage update
TokenUsage {
input_tokens: Option<u64>,
output_tokens: Option<u64>,
cache_read_input_tokens: Option<u64>,
cache_creation_input_tokens: Option<u64>,
},
/// Active transport/connection type for this stream
ConnectionType { connection: String },
/// Connection phase update (for status bar transparency)
ConnectionPhase { phase: ConnectionPhase },
/// Provider-supplied human-readable transport detail for the status line
StatusDetail { detail: String },
/// Error occurred
Error {
message: String,
/// Seconds until rate limit resets (if this is a rate limit error)
retry_after_secs: Option<u64>,
},
/// Provider session ID (for conversation resume)
SessionId(String),
/// Compaction occurred (context was summarized)
Compaction {
trigger: String,
pre_tokens: Option<u64>,
/// Provider-native compaction artifact, if one was emitted.
openai_encrypted_content: Option<String>,
},
/// Upstream provider info (e.g., which provider OpenRouter routed to)
UpstreamProvider { provider: String },
/// Native tool call from a provider bridge that needs execution by jcode
NativeToolCall {
request_id: String,
tool_name: String,
input: serde_json::Value,
},
}
#[cfg(test)]
mod tests;