File size: 14,253 Bytes
d90101d | 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 | use derive_more::Display;
use derive_setters::Setters;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::WorkspaceId;
/// Progress events emitted during workspace indexing
#[derive(Debug, Clone, PartialEq)]
pub enum SyncProgress {
/// Sync operation is starting
Starting,
/// A new workspace was created on the server
WorkspaceCreated {
/// The ID of the newly created workspace
workspace_id: WorkspaceId,
},
/// Discovering files in the directory
DiscoveringFiles {
/// ID of the current workspace
workspace_id: WorkspaceId,
/// Path being scanned
path: std::path::PathBuf,
},
/// Files have been discovered in the directory
FilesDiscovered {
/// Total number of files found
count: usize,
},
/// Comparing local files with server state
ComparingFiles {
/// Number of remote files in the workspace
remote_files: usize,
/// Number of local files being compared
local_files: usize,
},
/// Diff computed showing breakdown of changes
DiffComputed {
/// Number of files added (new files)
added: usize,
/// Number of files deleted (orphaned on server)
deleted: usize,
/// Number of files modified (changed files)
modified: usize,
},
/// Syncing files (deleting outdated + uploading new/changed)
Syncing {
/// Current progress
current: usize,
/// Total number of files to sync
total: usize,
},
/// Sync operation completed successfully
Completed {
/// Total number of files in the workspace
total_files: usize,
/// Number of files that were uploaded (changed or new)
uploaded_files: usize,
/// Number of files that failed to sync
failed_files: usize,
},
}
impl SyncProgress {
/// Returns the progress weight (0-100) for this event.
pub fn weight(&self) -> Option<u64> {
match self {
Self::Syncing { current, total } => {
let sync_progress = if *total > 0 {
(*current as f64) / (*total as f64) * 100.0
} else {
0.0
};
Some(sync_progress as u64)
}
_ => None,
}
}
}
/// Stored authentication token for the indexing service (no expiry)
///
/// Associates a user with their indexing service authentication token
/// obtained from the remote authentication API.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkspaceAuth {
/// User ID that owns this authentication
pub user_id: UserId,
/// Authentication token (obtained from HTTP API)
pub token: crate::ApiKey,
/// When this token was stored locally
pub created_at: chrono::DateTime<chrono::Utc>,
}
impl From<WorkspaceAuth> for crate::AuthDetails {
fn from(auth: WorkspaceAuth) -> Self {
crate::AuthDetails::ApiKey(auth.token)
}
}
impl WorkspaceAuth {
/// Create a new indexing auth record
pub fn new(user_id: UserId, token: crate::ApiKey) -> Self {
Self { user_id, token, created_at: chrono::Utc::now() }
}
}
/// File content for upload to workspace server
///
/// Contains the file path (relative to workspace root) and its textual content
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FileRead {
/// File path (relative to workspace root)
pub path: String,
/// File content as UTF-8 text
pub content: String,
}
impl FileRead {
/// Create a new file read entry
pub fn new(path: String, content: String) -> Self {
Self { path, content }
}
}
/// Generic wrapper for workspace operations
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeBase<T> {
pub user_id: UserId,
pub workspace_id: WorkspaceId,
pub data: T,
}
impl<T> CodeBase<T> {
pub fn new(user_id: UserId, workspace_id: WorkspaceId, data: T) -> Self {
Self { user_id, workspace_id, data }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Setters)]
#[setters(strip_option, into)]
pub struct SearchParams<'a> {
pub query: &'a str,
pub limit: Option<usize>,
pub top_k: Option<u32>,
pub use_case: String,
pub starts_with: Option<String>,
pub ends_with: Option<Vec<String>>,
}
impl<'a> SearchParams<'a> {
pub fn new(query: &'a str, use_case: &str) -> Self {
Self {
query,
limit: None,
top_k: None,
use_case: use_case.to_string(),
starts_with: None,
ends_with: None,
}
}
}
pub type CodeSearchQuery<'a> = CodeBase<SearchParams<'a>>;
pub type FileUpload = CodeBase<Vec<FileRead>>;
pub type FileDeletion = CodeBase<Vec<String>>;
pub type WorkspaceFiles = CodeBase<()>;
/// User identifier for codebase operations.
///
/// Unique per machine, generated once and stored in database.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
#[display("{}", _0)]
pub struct UserId(Uuid);
impl UserId {
/// Generate a new random user ID
pub fn generate() -> Self {
Self(Uuid::new_v4())
}
/// Parse a user ID from a string
///
/// # Errors
/// Returns an error if the string is not a valid UUID
pub fn from_string(s: &str) -> anyhow::Result<Self> {
Ok(Self(Uuid::parse_str(s)?))
}
}
/// Node identifier for code graph nodes.
///
/// Uniquely identifies a node in the codebase graph (file chunks, files,
/// notes, tasks, etc.).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
#[display("{}", _0)]
pub struct NodeId(String);
impl NodeId {
/// Create a new node ID from a string
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
/// Get the node ID as a string slice
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for NodeId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for NodeId {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl AsRef<str> for NodeId {
fn as_ref(&self) -> &str {
&self.0
}
}
/// Git repository information for a workspace
///
/// Contains commit hash and branch name for version tracking
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GitInfo {
/// Git commit hash (e.g., "abc123...")
pub commit: String,
/// Git branch name (e.g., "main", "develop")
pub branch: String,
}
/// Information about a workspace from the server
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkspaceInfo {
/// Workspace ID
pub workspace_id: WorkspaceId,
/// Working directory path
pub working_dir: String,
/// Number of nodes created
pub node_count: Option<u64>,
/// Number of relations between nodes
pub relation_count: Option<u64>,
/// Last updated timestamp
pub last_updated: Option<chrono::DateTime<chrono::Utc>>,
/// Workspace created time.
pub created_at: chrono::DateTime<chrono::Utc>,
}
/// Result of a codebase sync operation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Setters)]
pub struct FileUploadResponse {
/// Workspace ID that was synced
pub workspace_id: WorkspaceId,
/// Number of files processed
pub files_processed: usize,
/// Upload statistics
pub upload_stats: FileUploadInfo,
/// Whether a new workspace was created (vs using existing)
pub is_new_workspace: bool,
}
impl FileUploadResponse {
/// Create new sync statistics
pub fn new(
workspace_id: WorkspaceId,
files_processed: usize,
upload_stats: FileUploadInfo,
) -> Self {
Self {
workspace_id,
files_processed,
upload_stats,
is_new_workspace: false,
}
}
}
/// Statistics from uploading files to the codebase server
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FileUploadInfo {
/// Number of code nodes created
pub nodes_created: usize,
/// Number of relations created
pub relations_created: usize,
}
impl std::ops::Add for FileUploadInfo {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
nodes_created: self.nodes_created + other.nodes_created,
relations_created: self.relations_created + other.relations_created,
}
}
}
impl FileUploadInfo {
/// Create new upload statistics
pub fn new(nodes_created: usize, relations_created: usize) -> Self {
Self { nodes_created, relations_created }
}
}
/// Results for a single codebase search query
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CodebaseQueryResult {
/// The query string that was executed
pub query: String,
/// Relevance query used for re-ranking
pub use_case: String,
/// The search results for this query
pub results: Vec<Node>,
}
/// Results for multiple codebase search queries
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CodebaseSearchResults {
/// Results for each query/use_case pair
pub queries: Vec<CodebaseQueryResult>,
}
/// A search result with its similarity score
///
/// Wraps a code node with its semantic search scores,
/// keeping the scores separate from the node data itself.
#[derive(
Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, derive_setters::Setters,
)]
#[setters(strip_option)]
pub struct Node {
/// Node identifier
pub node_id: NodeId,
/// The node data (file, chunk, note, etc.)
#[serde(flatten)]
pub node: NodeData,
/// Relevance score (most important ranking metric)
pub relevance: Option<f32>,
/// Distance score (second ranking metric, lower is better)
pub distance: Option<f32>,
}
/// File chunk with precise line numbers
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct FileChunk {
/// File path
pub file_path: String,
/// Code content
pub content: String,
/// Start line in the file
pub start_line: u32,
/// End line in the file
pub end_line: u32,
}
/// Full file content
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct FileNode {
/// File path
pub file_path: String,
/// File content
pub content: String,
/// SHA-256 hash of the file content
pub hash: String,
}
/// File reference (path only, no content)
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct FileRef {
/// File path
pub file_path: String,
/// SHA-256 hash of the file content
pub file_hash: String,
}
/// Note content
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Note {
/// Note content
pub content: String,
}
/// Task description
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Task {
/// Task description
pub task: String,
}
/// Result of a semantic search query
///
/// Represents different types of nodes returned from the codebase service.
/// Each variant contains only the fields relevant to that node type.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, derive_more::From)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum NodeData {
/// File chunk with precise line numbers
#[from]
FileChunk(FileChunk),
/// Full file content
#[from]
File(FileNode),
/// File reference (path only, no content)
#[from]
FileRef(FileRef),
/// Note content
#[from]
Note(Note),
/// Task description
#[from]
Task(Task),
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn test_user_id_roundtrip() {
let user_id = UserId::generate();
let s = user_id.to_string();
let parsed = UserId::from_string(&s).unwrap();
assert_eq!(user_id, parsed);
}
#[test]
fn test_workspace_id_roundtrip() {
let workspace_id = WorkspaceId::generate();
let s = workspace_id.to_string();
let parsed = WorkspaceId::from_string(&s).unwrap();
assert_eq!(workspace_id, parsed);
}
#[test]
fn test_search_params_with_file_extension() {
let actual = SearchParams::new("retry mechanism", "find retry logic")
.limit(10usize)
.top_k(20u32)
.ends_with(vec![".rs".to_string()]);
let expected = SearchParams {
query: "retry mechanism",
limit: Some(10),
top_k: Some(20),
use_case: "find retry logic".to_string(),
starts_with: None,
ends_with: Some(vec![".rs".to_string()]),
};
assert_eq!(actual, expected);
}
#[test]
fn test_search_params_with_multiple_file_extensions() {
let actual = SearchParams::new("retry mechanism", "find retry logic")
.limit(10usize)
.top_k(20u32)
.ends_with(vec![
".rs".to_string(),
".ts".to_string(),
".py".to_string(),
]);
let expected = SearchParams {
query: "retry mechanism",
limit: Some(10),
top_k: Some(20),
use_case: "find retry logic".to_string(),
starts_with: None,
ends_with: Some(vec![
".rs".to_string(),
".ts".to_string(),
".py".to_string(),
]),
};
assert_eq!(actual, expected);
}
#[test]
fn test_search_params_without_file_extension() {
let actual = SearchParams::new("auth logic", "authentication implementation").limit(5usize);
let expected = SearchParams {
query: "auth logic",
limit: Some(5),
top_k: None,
use_case: "authentication implementation".to_string(),
starts_with: None,
ends_with: None,
};
assert_eq!(actual, expected);
}
}
|