predict_edits_v3.rs

  1use chrono::Duration;
  2use serde::{Deserialize, Serialize};
  3use std::{ops::Range, path::PathBuf};
  4use uuid::Uuid;
  5
  6use crate::PredictEditsGitInfo;
  7
  8// TODO: snippet ordering within file / relative to excerpt
  9
 10#[derive(Debug, Clone, Serialize, Deserialize)]
 11pub struct PredictEditsRequest {
 12    pub excerpt: String,
 13    pub excerpt_path: PathBuf,
 14    /// Within file
 15    pub excerpt_range: Range<usize>,
 16    /// Within `excerpt`
 17    pub cursor_offset: usize,
 18    /// Within `signatures`
 19    pub excerpt_parent: Option<usize>,
 20    pub signatures: Vec<Signature>,
 21    pub referenced_declarations: Vec<ReferencedDeclaration>,
 22    pub events: Vec<Event>,
 23    #[serde(default)]
 24    pub can_collect_data: bool,
 25    #[serde(skip_serializing_if = "Vec::is_empty", default)]
 26    pub diagnostic_groups: Vec<DiagnosticGroup>,
 27    #[serde(skip_serializing_if = "is_default", default)]
 28    pub diagnostic_groups_truncated: bool,
 29    /// Info about the git repository state, only present when can_collect_data is true.
 30    #[serde(skip_serializing_if = "Option::is_none", default)]
 31    pub git_info: Option<PredictEditsGitInfo>,
 32    #[serde(default)]
 33    pub debug_info: bool,
 34}
 35
 36#[derive(Debug, Clone, Serialize, Deserialize)]
 37#[serde(tag = "event")]
 38pub enum Event {
 39    BufferChange {
 40        path: Option<PathBuf>,
 41        old_path: Option<PathBuf>,
 42        diff: String,
 43        predicted: bool,
 44    },
 45}
 46
 47#[derive(Debug, Clone, Serialize, Deserialize)]
 48pub struct Signature {
 49    pub text: String,
 50    pub text_is_truncated: bool,
 51    #[serde(skip_serializing_if = "Option::is_none", default)]
 52    pub parent_index: Option<usize>,
 53    /// Range of `text` within the file, possibly truncated according to `text_is_truncated`. The
 54    /// file is implicitly the file that contains the descendant declaration or excerpt.
 55    pub range: Range<usize>,
 56}
 57
 58#[derive(Debug, Clone, Serialize, Deserialize)]
 59pub struct ReferencedDeclaration {
 60    pub path: PathBuf,
 61    pub text: String,
 62    pub text_is_truncated: bool,
 63    /// Range of `text` within file, possibly truncated according to `text_is_truncated`
 64    pub range: Range<usize>,
 65    /// Range within `text`
 66    pub signature_range: Range<usize>,
 67    /// Index within `signatures`.
 68    #[serde(skip_serializing_if = "Option::is_none", default)]
 69    pub parent_index: Option<usize>,
 70    pub score_components: ScoreComponents,
 71    pub signature_score: f32,
 72    pub declaration_score: f32,
 73}
 74
 75#[derive(Debug, Clone, Serialize, Deserialize)]
 76pub struct ScoreComponents {
 77    pub is_same_file: bool,
 78    pub is_referenced_nearby: bool,
 79    pub is_referenced_in_breadcrumb: bool,
 80    pub reference_count: usize,
 81    pub same_file_declaration_count: usize,
 82    pub declaration_count: usize,
 83    pub reference_line_distance: u32,
 84    pub declaration_line_distance: u32,
 85    pub declaration_line_distance_rank: usize,
 86    pub containing_range_vs_item_jaccard: f32,
 87    pub containing_range_vs_signature_jaccard: f32,
 88    pub adjacent_vs_item_jaccard: f32,
 89    pub adjacent_vs_signature_jaccard: f32,
 90    pub containing_range_vs_item_weighted_overlap: f32,
 91    pub containing_range_vs_signature_weighted_overlap: f32,
 92    pub adjacent_vs_item_weighted_overlap: f32,
 93    pub adjacent_vs_signature_weighted_overlap: f32,
 94}
 95
 96#[derive(Debug, Clone, Serialize, Deserialize)]
 97#[serde(transparent)]
 98pub struct DiagnosticGroup(pub Box<serde_json::value::RawValue>);
 99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct PredictEditsResponse {
102    pub request_id: Uuid,
103    pub edits: Vec<Edit>,
104    pub debug_info: Option<DebugInfo>,
105}
106
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct DebugInfo {
109    pub prompt: String,
110    pub prompt_planning_time: Duration,
111    pub model_response: String,
112    pub inference_time: Duration,
113    pub parsing_time: Duration,
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct Edit {
118    pub path: PathBuf,
119    pub range: Range<usize>,
120    pub content: String,
121}
122
123fn is_default<T: Default + PartialEq>(value: &T) -> bool {
124    *value == T::default()
125}