Refactor and clean up code across multiple modules
- Simplified token type assignment in OAuth service. - Removed unused session_lock module and re-exported Session from zesdex_entities. - Cleaned up session entity by removing unnecessary comments and code. - Consolidated session handling in HTTP handlers for better readability. - Improved formatting and readability in OAuth repository tests. - Enhanced session lock repository with clearer match statements. - Streamlined session repository error handling. - Refined RNG tests for better clarity. - Adjusted module visibility and organization in lib.rs. - Updated IPC client and connection code for better error handling and clarity. - Improved frame handling in IPC for better readability. - Organized module imports and added test utilities for IPC. - Enhanced database connection error handling. - Simplified JWT token creation error handling. - Improved password verification error handling. - Cleaned up state management code for better readability. - Refactored middleware for session authentication and rate limiting. - Simplified clipboard utility for better error handling. - Enhanced logging initialization for better error reporting. - Improved pagination utility with clearer method annotations. - Cleaned up sanitization functions for filenames and paths. - Enhanced slug generation functions for better clarity and usability.
This commit is contained in:
@@ -16,8 +16,7 @@
|
||||
//! needed; optional fields use `skip_serializing_if` so unset knobs are
|
||||
//! omitted rather than sent as `null`, matching provider expectations.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
|
||||
/// Outbound chat completion request body sent to an
|
||||
/// OpenAI/Anthropic-compatible provider.
|
||||
@@ -25,57 +24,6 @@ use serde_json::Value;
|
||||
/// Flow: constructed from the current message history plus optional
|
||||
/// generation knobs (temperature, `max_tokens`, tools, etc.) and serialized
|
||||
/// directly into the HTTP request body.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChatCompletionRequest {
|
||||
pub model: String,
|
||||
pub messages: Vec<super::super::chat::message::ChatMessage>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub max_tokens: Option<u32>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub temperature: Option<f32>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub stream: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub stream_options: Option<StreamOptions>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tools: Option<Vec<ToolDef>>,
|
||||
/// Controls which (if any) function is called by the model.
|
||||
/// Can be `"none"`, `"auto"`, or `{"type": "function", "function": {"name": "..."}}`.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tool_choice: Option<Value>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub stop: Option<Vec<String>>,
|
||||
}
|
||||
pub use zesdex_entities::seaorm::common::provider::ChatRequest as ChatCompletionRequest;
|
||||
|
||||
/// Streaming options for the request; `include_usage` asks the provider to
|
||||
/// emit a final usage chunk in the SSE stream.
|
||||
///
|
||||
/// Why: usage tokens are otherwise unavailable in a streamed response since
|
||||
/// they are normally only attached to the final non-streamed completion.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct StreamOptions {
|
||||
pub include_usage: bool,
|
||||
}
|
||||
|
||||
/// Wire format for a single tool definition sent to the provider.
|
||||
///
|
||||
/// Flow: built from the harness's registered `Tool` implementations
|
||||
/// and attached to [`ChatCompletionRequest::tools`] so the model knows which
|
||||
/// functions it may call.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolDef {
|
||||
#[serde(rename = "type")]
|
||||
pub type_: String,
|
||||
pub function: ToolFunctionDef,
|
||||
}
|
||||
|
||||
/// Name, description, and JSON schema parameters for a tool definition.
|
||||
///
|
||||
/// Why: `parameters` is a raw `serde_json::Value` rather than a typed struct
|
||||
/// because each tool defines its own arbitrary JSON schema.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolFunctionDef {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub parameters: Value,
|
||||
}
|
||||
pub use zesdex_entities::seaorm::common::provider::{StreamOptions, ToolDef, ToolFunctionDef};
|
||||
|
||||
@@ -1,58 +1,5 @@
|
||||
#![allow(
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::cast_precision_loss,
|
||||
clippy::cast_possible_wrap
|
||||
)]
|
||||
|
||||
//! Inbound response DTOs for the non-streaming chat completions API.
|
||||
//!
|
||||
//! Flow: provider HTTP response body → `serde_json` deserializes into
|
||||
//! [`ChatCompletionResponse`] → caller reads `choices[0]` for the assistant
|
||||
//! reply and `usage` for token accounting.
|
||||
//! Re-exported from `zesdex_entities` for consistency.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Non-streaming chat completion response returned by the provider.
|
||||
///
|
||||
/// Flow: deserialized directly from the HTTP response body of a
|
||||
/// non-streaming completion call.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChatCompletionResponse {
|
||||
pub id: String,
|
||||
pub object: String,
|
||||
pub created: i64,
|
||||
pub model: String,
|
||||
pub choices: Vec<Choice>,
|
||||
pub usage: Option<super::usage::TokenUsage>,
|
||||
}
|
||||
|
||||
/// One completion candidate within a [`ChatCompletionResponse::choices`] list.
|
||||
///
|
||||
/// For non-streaming responses the `message` field is populated; for streaming
|
||||
/// responses the `delta` field carries the incremental token.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Choice {
|
||||
pub index: u32,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub message: Option<super::super::chat::message::ChatMessage>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub delta: Option<Delta>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub finish_reason: Option<String>,
|
||||
}
|
||||
|
||||
/// Incremental delta emitted in a streaming SSE chunk.
|
||||
///
|
||||
/// Only populated when the response is streamed; `role` typically appears
|
||||
/// only on the first chunk and `content` / `tool_calls` are appended
|
||||
/// incrementally.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Delta {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub role: Option<super::super::chat::message::Role>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub content: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tool_calls: Option<Vec<super::super::chat::tool::ToolCall>>,
|
||||
}
|
||||
pub use zesdex_entities::seaorm::common::provider::{ChatResponse as ChatCompletionResponse, Choice, Delta};
|
||||
|
||||
@@ -1,28 +1,5 @@
|
||||
#![allow(
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::cast_precision_loss,
|
||||
clippy::cast_possible_wrap
|
||||
)]
|
||||
|
||||
//! Token usage accounting DTO shared by streaming and non-streaming responses.
|
||||
//!
|
||||
//! Flow: populated from the provider's `usage` object (either the final SSE
|
||||
//! chunk when `stream_options.include_usage` is set, or the `usage` field of
|
||||
//! a non-streaming [`ChatCompletionResponse`](super::response::ChatCompletionResponse))
|
||||
//! → surfaced to the TUI for cost/token display.
|
||||
//! Re-exported from `zesdex_entities` for consistency.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Token counts for a single completion request.
|
||||
///
|
||||
/// Why: these are the standard fields reported by the OpenAI-compatible chat
|
||||
/// completions API. All fields are required when present — use `Option` at
|
||||
/// the [`ChatCompletionResponse`](super::response::ChatCompletionResponse) level
|
||||
/// if usage is absent.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
|
||||
pub struct TokenUsage {
|
||||
pub prompt_tokens: u32,
|
||||
pub completion_tokens: u32,
|
||||
pub total_tokens: u32,
|
||||
}
|
||||
pub use zesdex_entities::seaorm::common::provider::TokenUsage;
|
||||
|
||||
Reference in New Issue
Block a user