logiguard fork v3: full patch set on verified 8c74db0 tree
Includes prior-session patches (carry forward so the app compiles): - crates/gpui/build.rs: cross-compile manifest fix - crates/gpui/src/platform.rs: PlatformWindow::activate_with_token trait method - crates/gpui/src/window.rs: Window::activate_with_token public API - crates/gpui_linux/src/linux/wayland/window.rs: WaylandWindow::activate_with_token + activate() keyboard-serial fix Plus the focus-serial fix: - serial.rs: SerialKind::KeyboardEnter - client.rs: store wl_keyboard.enter serial; latest_serial_of() Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
27
crates/language_model_core/Cargo.toml
Normal file
27
crates/language_model_core/Cargo.toml
Normal file
@@ -0,0 +1,27 @@
|
||||
[package]
|
||||
name = "language_model_core"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
publish.workspace = true
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/language_model_core.rs"
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
async-lock.workspace = true
|
||||
cloud_llm_client.workspace = true
|
||||
futures.workspace = true
|
||||
gpui_shared_string.workspace = true
|
||||
http_client.workspace = true
|
||||
partial-json-fixer.workspace = true
|
||||
schemars.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
strum.workspace = true
|
||||
thiserror.workspace = true
|
||||
1
crates/language_model_core/LICENSE-GPL
Symbolic link
1
crates/language_model_core/LICENSE-GPL
Symbolic link
@@ -0,0 +1 @@
|
||||
../../LICENSE-GPL
|
||||
659
crates/language_model_core/src/language_model_core.rs
Normal file
659
crates/language_model_core/src/language_model_core.rs
Normal file
@@ -0,0 +1,659 @@
|
||||
mod provider;
|
||||
mod rate_limiter;
|
||||
mod request;
|
||||
mod role;
|
||||
pub mod tool_schema;
|
||||
pub mod util;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use cloud_llm_client::CompletionRequestStatus;
|
||||
use http_client::{StatusCode, http};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::ops::{Add, Sub};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::{fmt, io};
|
||||
use thiserror::Error;
|
||||
fn is_default<T: Default + PartialEq>(value: &T) -> bool {
|
||||
*value == T::default()
|
||||
}
|
||||
|
||||
pub use crate::provider::*;
|
||||
pub use crate::rate_limiter::*;
|
||||
pub use crate::request::*;
|
||||
pub use crate::role::*;
|
||||
pub use crate::tool_schema::LanguageModelToolSchemaFormat;
|
||||
pub use crate::util::{fix_streamed_json, parse_prompt_too_long, parse_tool_arguments};
|
||||
pub use gpui_shared_string::SharedString;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LanguageModelCacheConfiguration {
|
||||
pub max_cache_anchors: usize,
|
||||
pub should_speculate: bool,
|
||||
pub min_total_token: u64,
|
||||
}
|
||||
|
||||
/// A completion event from a language model.
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
||||
pub enum LanguageModelCompletionEvent {
|
||||
Queued {
|
||||
position: usize,
|
||||
},
|
||||
Started,
|
||||
Stop(StopReason),
|
||||
Text(String),
|
||||
Thinking {
|
||||
text: String,
|
||||
signature: Option<String>,
|
||||
},
|
||||
RedactedThinking {
|
||||
data: String,
|
||||
},
|
||||
ToolUse(LanguageModelToolUse),
|
||||
ToolUseJsonParseError {
|
||||
id: LanguageModelToolUseId,
|
||||
tool_name: Arc<str>,
|
||||
raw_input: Arc<str>,
|
||||
json_parse_error: String,
|
||||
},
|
||||
StartMessage {
|
||||
message_id: String,
|
||||
},
|
||||
ReasoningDetails(serde_json::Value),
|
||||
UsageUpdate(TokenUsage),
|
||||
}
|
||||
|
||||
impl LanguageModelCompletionEvent {
|
||||
pub fn from_completion_request_status(
|
||||
status: CompletionRequestStatus,
|
||||
upstream_provider: LanguageModelProviderName,
|
||||
) -> Result<Option<Self>, LanguageModelCompletionError> {
|
||||
match status {
|
||||
CompletionRequestStatus::Queued { position } => {
|
||||
Ok(Some(LanguageModelCompletionEvent::Queued { position }))
|
||||
}
|
||||
CompletionRequestStatus::Started => Ok(Some(LanguageModelCompletionEvent::Started)),
|
||||
CompletionRequestStatus::Unknown | CompletionRequestStatus::StreamEnded => Ok(None),
|
||||
CompletionRequestStatus::Failed {
|
||||
code,
|
||||
message,
|
||||
request_id: _,
|
||||
retry_after,
|
||||
} => Err(LanguageModelCompletionError::from_cloud_failure(
|
||||
upstream_provider,
|
||||
code,
|
||||
message,
|
||||
retry_after.map(Duration::from_secs_f64),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum LanguageModelCompletionError {
|
||||
#[error("prompt too large for context window")]
|
||||
PromptTooLarge { tokens: Option<u64> },
|
||||
#[error("missing {provider} API key")]
|
||||
NoApiKey { provider: LanguageModelProviderName },
|
||||
#[error("{provider}'s API rate limit exceeded")]
|
||||
RateLimitExceeded {
|
||||
provider: LanguageModelProviderName,
|
||||
retry_after: Option<Duration>,
|
||||
},
|
||||
#[error("{provider}'s API servers are overloaded right now")]
|
||||
ServerOverloaded {
|
||||
provider: LanguageModelProviderName,
|
||||
retry_after: Option<Duration>,
|
||||
},
|
||||
#[error("{provider}'s API server reported an internal server error: {message}")]
|
||||
ApiInternalServerError {
|
||||
provider: LanguageModelProviderName,
|
||||
message: String,
|
||||
},
|
||||
#[error("{message}")]
|
||||
UpstreamProviderError {
|
||||
message: String,
|
||||
status: StatusCode,
|
||||
retry_after: Option<Duration>,
|
||||
},
|
||||
#[error("HTTP response error from {provider}'s API: status {status_code} - {message:?}")]
|
||||
HttpResponseError {
|
||||
provider: LanguageModelProviderName,
|
||||
status_code: StatusCode,
|
||||
message: String,
|
||||
},
|
||||
#[error("invalid request format to {provider}'s API: {message}")]
|
||||
BadRequestFormat {
|
||||
provider: LanguageModelProviderName,
|
||||
message: String,
|
||||
},
|
||||
#[error("authentication error with {provider}'s API: {message}")]
|
||||
AuthenticationError {
|
||||
provider: LanguageModelProviderName,
|
||||
message: String,
|
||||
},
|
||||
#[error("Permission error with {provider}'s API: {message}")]
|
||||
PermissionError {
|
||||
provider: LanguageModelProviderName,
|
||||
message: String,
|
||||
},
|
||||
#[error("language model provider API endpoint not found")]
|
||||
ApiEndpointNotFound { provider: LanguageModelProviderName },
|
||||
#[error("I/O error reading response from {provider}'s API")]
|
||||
ApiReadResponseError {
|
||||
provider: LanguageModelProviderName,
|
||||
#[source]
|
||||
error: io::Error,
|
||||
},
|
||||
#[error("error serializing request to {provider} API")]
|
||||
SerializeRequest {
|
||||
provider: LanguageModelProviderName,
|
||||
#[source]
|
||||
error: serde_json::Error,
|
||||
},
|
||||
#[error("error building request body to {provider} API")]
|
||||
BuildRequestBody {
|
||||
provider: LanguageModelProviderName,
|
||||
#[source]
|
||||
error: http::Error,
|
||||
},
|
||||
#[error("error sending HTTP request to {provider} API")]
|
||||
HttpSend {
|
||||
provider: LanguageModelProviderName,
|
||||
#[source]
|
||||
error: anyhow::Error,
|
||||
},
|
||||
#[error("error deserializing {provider} API response")]
|
||||
DeserializeResponse {
|
||||
provider: LanguageModelProviderName,
|
||||
#[source]
|
||||
error: serde_json::Error,
|
||||
},
|
||||
#[error("stream from {provider} ended unexpectedly")]
|
||||
StreamEndedUnexpectedly { provider: LanguageModelProviderName },
|
||||
#[error(transparent)]
|
||||
Other(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
impl LanguageModelCompletionError {
|
||||
fn parse_upstream_error_json(message: &str) -> Option<(StatusCode, String)> {
|
||||
let error_json = serde_json::from_str::<serde_json::Value>(message).ok()?;
|
||||
let upstream_status = error_json
|
||||
.get("upstream_status")
|
||||
.and_then(|v| v.as_u64())
|
||||
.and_then(|status| u16::try_from(status).ok())
|
||||
.and_then(|status| StatusCode::from_u16(status).ok())?;
|
||||
let inner_message = error_json
|
||||
.get("message")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or(message)
|
||||
.to_string();
|
||||
Some((upstream_status, inner_message))
|
||||
}
|
||||
|
||||
pub fn from_cloud_failure(
|
||||
upstream_provider: LanguageModelProviderName,
|
||||
code: String,
|
||||
message: String,
|
||||
retry_after: Option<Duration>,
|
||||
) -> Self {
|
||||
if let Some(tokens) = parse_prompt_too_long(&message) {
|
||||
Self::PromptTooLarge {
|
||||
tokens: Some(tokens),
|
||||
}
|
||||
} else if code == "upstream_http_error" {
|
||||
if let Some((upstream_status, inner_message)) =
|
||||
Self::parse_upstream_error_json(&message)
|
||||
{
|
||||
return Self::from_http_status(
|
||||
upstream_provider,
|
||||
upstream_status,
|
||||
inner_message,
|
||||
retry_after,
|
||||
);
|
||||
}
|
||||
anyhow!("completion request failed, code: {code}, message: {message}").into()
|
||||
} else if let Some(status_code) = code
|
||||
.strip_prefix("upstream_http_")
|
||||
.and_then(|code| StatusCode::from_str(code).ok())
|
||||
{
|
||||
Self::from_http_status(upstream_provider, status_code, message, retry_after)
|
||||
} else if let Some(status_code) = code
|
||||
.strip_prefix("http_")
|
||||
.and_then(|code| StatusCode::from_str(code).ok())
|
||||
{
|
||||
Self::from_http_status(ZED_CLOUD_PROVIDER_NAME, status_code, message, retry_after)
|
||||
} else {
|
||||
anyhow!("completion request failed, code: {code}, message: {message}").into()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_http_status(
|
||||
provider: LanguageModelProviderName,
|
||||
status_code: StatusCode,
|
||||
message: String,
|
||||
retry_after: Option<Duration>,
|
||||
) -> Self {
|
||||
match status_code {
|
||||
StatusCode::BAD_REQUEST => Self::BadRequestFormat { provider, message },
|
||||
StatusCode::UNAUTHORIZED => Self::AuthenticationError { provider, message },
|
||||
StatusCode::FORBIDDEN => Self::PermissionError { provider, message },
|
||||
StatusCode::NOT_FOUND => Self::ApiEndpointNotFound { provider },
|
||||
StatusCode::PAYLOAD_TOO_LARGE => Self::PromptTooLarge {
|
||||
tokens: parse_prompt_too_long(&message),
|
||||
},
|
||||
StatusCode::TOO_MANY_REQUESTS => Self::RateLimitExceeded {
|
||||
provider,
|
||||
retry_after,
|
||||
},
|
||||
StatusCode::INTERNAL_SERVER_ERROR => Self::ApiInternalServerError { provider, message },
|
||||
StatusCode::SERVICE_UNAVAILABLE => Self::ServerOverloaded {
|
||||
provider,
|
||||
retry_after,
|
||||
},
|
||||
_ if status_code.as_u16() == 529 => Self::ServerOverloaded {
|
||||
provider,
|
||||
retry_after,
|
||||
},
|
||||
_ => Self::HttpResponseError {
|
||||
provider,
|
||||
status_code,
|
||||
message,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum StopReason {
|
||||
EndTurn,
|
||||
MaxTokens,
|
||||
ToolUse,
|
||||
Refusal,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize, Default)]
|
||||
pub struct TokenUsage {
|
||||
#[serde(default, skip_serializing_if = "is_default")]
|
||||
pub input_tokens: u64,
|
||||
#[serde(default, skip_serializing_if = "is_default")]
|
||||
pub output_tokens: u64,
|
||||
#[serde(default, skip_serializing_if = "is_default")]
|
||||
pub cache_creation_input_tokens: u64,
|
||||
#[serde(default, skip_serializing_if = "is_default")]
|
||||
pub cache_read_input_tokens: u64,
|
||||
}
|
||||
|
||||
impl TokenUsage {
|
||||
pub fn total_tokens(&self) -> u64 {
|
||||
self.input_tokens
|
||||
+ self.output_tokens
|
||||
+ self.cache_read_input_tokens
|
||||
+ self.cache_creation_input_tokens
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<TokenUsage> for TokenUsage {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
input_tokens: self.input_tokens + other.input_tokens,
|
||||
output_tokens: self.output_tokens + other.output_tokens,
|
||||
cache_creation_input_tokens: self.cache_creation_input_tokens
|
||||
+ other.cache_creation_input_tokens,
|
||||
cache_read_input_tokens: self.cache_read_input_tokens + other.cache_read_input_tokens,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<TokenUsage> for TokenUsage {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: Self) -> Self {
|
||||
Self {
|
||||
input_tokens: self.input_tokens - other.input_tokens,
|
||||
output_tokens: self.output_tokens - other.output_tokens,
|
||||
cache_creation_input_tokens: self.cache_creation_input_tokens
|
||||
- other.cache_creation_input_tokens,
|
||||
cache_read_input_tokens: self.cache_read_input_tokens - other.cache_read_input_tokens,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
|
||||
pub struct LanguageModelToolUseId(Arc<str>);
|
||||
|
||||
impl fmt::Display for LanguageModelToolUseId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for LanguageModelToolUseId
|
||||
where
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
fn from(value: T) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
|
||||
pub struct LanguageModelToolUse {
|
||||
pub id: LanguageModelToolUseId,
|
||||
pub name: Arc<str>,
|
||||
pub raw_input: String,
|
||||
pub input: serde_json::Value,
|
||||
pub is_input_complete: bool,
|
||||
/// Thought signature the model sent us. Some models require that this
|
||||
/// signature be preserved and sent back in conversation history for validation.
|
||||
pub thought_signature: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LanguageModelEffortLevel {
|
||||
pub name: SharedString,
|
||||
pub value: SharedString,
|
||||
pub is_default: bool,
|
||||
}
|
||||
|
||||
/// An error that occurred when trying to authenticate the language model provider.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AuthenticateError {
|
||||
#[error("connection refused")]
|
||||
ConnectionRefused,
|
||||
#[error("credentials not found")]
|
||||
CredentialsNotFound,
|
||||
#[error(transparent)]
|
||||
Other(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd, Serialize, Deserialize)]
|
||||
pub struct LanguageModelId(pub SharedString);
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
|
||||
pub struct LanguageModelName(pub SharedString);
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
|
||||
pub struct LanguageModelProviderId(pub SharedString);
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
|
||||
pub struct LanguageModelProviderName(pub SharedString);
|
||||
|
||||
impl LanguageModelProviderId {
|
||||
pub const fn new(id: &'static str) -> Self {
|
||||
Self(SharedString::new_static(id))
|
||||
}
|
||||
}
|
||||
|
||||
impl LanguageModelProviderName {
|
||||
pub const fn new(id: &'static str) -> Self {
|
||||
Self(SharedString::new_static(id))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LanguageModelProviderId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LanguageModelProviderName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for LanguageModelId {
|
||||
fn from(value: String) -> Self {
|
||||
Self(SharedString::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for LanguageModelName {
|
||||
fn from(value: String) -> Self {
|
||||
Self(SharedString::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for LanguageModelProviderId {
|
||||
fn from(value: String) -> Self {
|
||||
Self(SharedString::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for LanguageModelProviderName {
|
||||
fn from(value: String) -> Self {
|
||||
Self(SharedString::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Arc<str>> for LanguageModelProviderId {
|
||||
fn from(value: Arc<str>) -> Self {
|
||||
Self(SharedString::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Arc<str>> for LanguageModelProviderName {
|
||||
fn from(value: Arc<str>) -> Self {
|
||||
Self(SharedString::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings-layer–free model mode enum.
|
||||
///
|
||||
/// Mirrors the shape of `settings_content::ModelMode` but lives here so that
|
||||
/// crates below the settings layer can reference it.
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
||||
#[serde(tag = "type", rename_all = "lowercase")]
|
||||
pub enum ModelMode {
|
||||
#[default]
|
||||
Default,
|
||||
Thinking {
|
||||
budget_tokens: Option<u32>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Settings-layer–free reasoning-effort enum.
|
||||
///
|
||||
/// Mirrors the shape of `settings_content::OpenAiReasoningEffort` but lives
|
||||
/// here so that crates below the settings layer can reference it.
|
||||
#[derive(
|
||||
Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, strum::EnumString,
|
||||
)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
#[strum(serialize_all = "lowercase")]
|
||||
pub enum ReasoningEffort {
|
||||
None,
|
||||
Minimal,
|
||||
Low,
|
||||
Medium,
|
||||
High,
|
||||
XHigh,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_from_cloud_failure_with_upstream_http_error() {
|
||||
let error = LanguageModelCompletionError::from_cloud_failure(
|
||||
String::from("anthropic").into(),
|
||||
"upstream_http_error".to_string(),
|
||||
r#"{"code":"upstream_http_error","message":"Received an error from the Anthropic API: upstream connect error or disconnect/reset before headers. reset reason: connection timeout","upstream_status":503}"#.to_string(),
|
||||
None,
|
||||
);
|
||||
|
||||
match error {
|
||||
LanguageModelCompletionError::ServerOverloaded { provider, .. } => {
|
||||
assert_eq!(provider.0, "anthropic");
|
||||
}
|
||||
_ => panic!(
|
||||
"Expected ServerOverloaded error for 503 status, got: {:?}",
|
||||
error
|
||||
),
|
||||
}
|
||||
|
||||
let error = LanguageModelCompletionError::from_cloud_failure(
|
||||
String::from("anthropic").into(),
|
||||
"upstream_http_error".to_string(),
|
||||
r#"{"code":"upstream_http_error","message":"Internal server error","upstream_status":500}"#.to_string(),
|
||||
None,
|
||||
);
|
||||
|
||||
match error {
|
||||
LanguageModelCompletionError::ApiInternalServerError { provider, message } => {
|
||||
assert_eq!(provider.0, "anthropic");
|
||||
assert_eq!(message, "Internal server error");
|
||||
}
|
||||
_ => panic!(
|
||||
"Expected ApiInternalServerError for 500 status, got: {:?}",
|
||||
error
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_cloud_failure_with_standard_format() {
|
||||
let error = LanguageModelCompletionError::from_cloud_failure(
|
||||
String::from("anthropic").into(),
|
||||
"upstream_http_503".to_string(),
|
||||
"Service unavailable".to_string(),
|
||||
None,
|
||||
);
|
||||
|
||||
match error {
|
||||
LanguageModelCompletionError::ServerOverloaded { provider, .. } => {
|
||||
assert_eq!(provider.0, "anthropic");
|
||||
}
|
||||
_ => panic!("Expected ServerOverloaded error for upstream_http_503"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_upstream_http_error_connection_timeout() {
|
||||
let error = LanguageModelCompletionError::from_cloud_failure(
|
||||
String::from("anthropic").into(),
|
||||
"upstream_http_error".to_string(),
|
||||
r#"{"code":"upstream_http_error","message":"Received an error from the Anthropic API: upstream connect error or disconnect/reset before headers. reset reason: connection timeout","upstream_status":503}"#.to_string(),
|
||||
None,
|
||||
);
|
||||
|
||||
match error {
|
||||
LanguageModelCompletionError::ServerOverloaded { provider, .. } => {
|
||||
assert_eq!(provider.0, "anthropic");
|
||||
}
|
||||
_ => panic!(
|
||||
"Expected ServerOverloaded error for connection timeout with 503 status, got: {:?}",
|
||||
error
|
||||
),
|
||||
}
|
||||
|
||||
let error = LanguageModelCompletionError::from_cloud_failure(
|
||||
String::from("anthropic").into(),
|
||||
"upstream_http_error".to_string(),
|
||||
r#"{"code":"upstream_http_error","message":"Received an error from the Anthropic API: upstream connect error or disconnect/reset before headers. reset reason: connection timeout","upstream_status":500}"#.to_string(),
|
||||
None,
|
||||
);
|
||||
|
||||
match error {
|
||||
LanguageModelCompletionError::ApiInternalServerError { provider, message } => {
|
||||
assert_eq!(provider.0, "anthropic");
|
||||
assert_eq!(
|
||||
message,
|
||||
"Received an error from the Anthropic API: upstream connect error or disconnect/reset before headers. reset reason: connection timeout"
|
||||
);
|
||||
}
|
||||
_ => panic!(
|
||||
"Expected ApiInternalServerError for connection timeout with 500 status, got: {:?}",
|
||||
error
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_language_model_tool_use_serializes_with_signature() {
|
||||
use serde_json::json;
|
||||
|
||||
let tool_use = LanguageModelToolUse {
|
||||
id: LanguageModelToolUseId::from("test_id"),
|
||||
name: "test_tool".into(),
|
||||
raw_input: json!({"arg": "value"}).to_string(),
|
||||
input: json!({"arg": "value"}),
|
||||
is_input_complete: true,
|
||||
thought_signature: Some("test_signature".to_string()),
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_value(&tool_use).unwrap();
|
||||
|
||||
assert_eq!(serialized["id"], "test_id");
|
||||
assert_eq!(serialized["name"], "test_tool");
|
||||
assert_eq!(serialized["thought_signature"], "test_signature");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_language_model_tool_use_deserializes_with_missing_signature() {
|
||||
use serde_json::json;
|
||||
|
||||
let json = json!({
|
||||
"id": "test_id",
|
||||
"name": "test_tool",
|
||||
"raw_input": "{\"arg\":\"value\"}",
|
||||
"input": {"arg": "value"},
|
||||
"is_input_complete": true
|
||||
});
|
||||
|
||||
let tool_use: LanguageModelToolUse = serde_json::from_value(json).unwrap();
|
||||
|
||||
assert_eq!(tool_use.id, LanguageModelToolUseId::from("test_id"));
|
||||
assert_eq!(tool_use.name.as_ref(), "test_tool");
|
||||
assert_eq!(tool_use.thought_signature, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_language_model_tool_use_round_trip_with_signature() {
|
||||
use serde_json::json;
|
||||
|
||||
let original = LanguageModelToolUse {
|
||||
id: LanguageModelToolUseId::from("round_trip_id"),
|
||||
name: "round_trip_tool".into(),
|
||||
raw_input: json!({"key": "value"}).to_string(),
|
||||
input: json!({"key": "value"}),
|
||||
is_input_complete: true,
|
||||
thought_signature: Some("round_trip_sig".to_string()),
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_value(&original).unwrap();
|
||||
let deserialized: LanguageModelToolUse = serde_json::from_value(serialized).unwrap();
|
||||
|
||||
assert_eq!(deserialized.id, original.id);
|
||||
assert_eq!(deserialized.name, original.name);
|
||||
assert_eq!(deserialized.thought_signature, original.thought_signature);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_language_model_tool_use_round_trip_without_signature() {
|
||||
use serde_json::json;
|
||||
|
||||
let original = LanguageModelToolUse {
|
||||
id: LanguageModelToolUseId::from("no_sig_id"),
|
||||
name: "no_sig_tool".into(),
|
||||
raw_input: json!({"arg": "value"}).to_string(),
|
||||
input: json!({"arg": "value"}),
|
||||
is_input_complete: true,
|
||||
thought_signature: None,
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_value(&original).unwrap();
|
||||
let deserialized: LanguageModelToolUse = serde_json::from_value(serialized).unwrap();
|
||||
|
||||
assert_eq!(deserialized.id, original.id);
|
||||
assert_eq!(deserialized.name, original.name);
|
||||
assert_eq!(deserialized.thought_signature, None);
|
||||
}
|
||||
}
|
||||
21
crates/language_model_core/src/provider.rs
Normal file
21
crates/language_model_core/src/provider.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::{LanguageModelProviderId, LanguageModelProviderName};
|
||||
|
||||
pub const ANTHROPIC_PROVIDER_ID: LanguageModelProviderId =
|
||||
LanguageModelProviderId::new("anthropic");
|
||||
pub const ANTHROPIC_PROVIDER_NAME: LanguageModelProviderName =
|
||||
LanguageModelProviderName::new("Anthropic");
|
||||
|
||||
pub const OPEN_AI_PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("openai");
|
||||
pub const OPEN_AI_PROVIDER_NAME: LanguageModelProviderName =
|
||||
LanguageModelProviderName::new("OpenAI");
|
||||
|
||||
pub const GOOGLE_PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("google");
|
||||
pub const GOOGLE_PROVIDER_NAME: LanguageModelProviderName =
|
||||
LanguageModelProviderName::new("Google AI");
|
||||
|
||||
pub const X_AI_PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("x_ai");
|
||||
pub const X_AI_PROVIDER_NAME: LanguageModelProviderName = LanguageModelProviderName::new("xAI");
|
||||
|
||||
pub const ZED_CLOUD_PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("zed.dev");
|
||||
pub const ZED_CLOUD_PROVIDER_NAME: LanguageModelProviderName =
|
||||
LanguageModelProviderName::new("Zed");
|
||||
77
crates/language_model_core/src/rate_limiter.rs
Normal file
77
crates/language_model_core/src/rate_limiter.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use async_lock::{Semaphore, SemaphoreGuardArc};
|
||||
use futures::Stream;
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
sync::Arc,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use crate::LanguageModelCompletionError;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RateLimiter {
|
||||
semaphore: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
pub struct RateLimitGuard<T> {
|
||||
inner: T,
|
||||
_guard: SemaphoreGuardArc,
|
||||
}
|
||||
|
||||
impl<T> Stream for RateLimitGuard<T>
|
||||
where
|
||||
T: Stream,
|
||||
{
|
||||
type Item = T::Item;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||
unsafe { Pin::map_unchecked_mut(self, |this| &mut this.inner).poll_next(cx) }
|
||||
}
|
||||
}
|
||||
|
||||
impl RateLimiter {
|
||||
pub fn new(limit: usize) -> Self {
|
||||
Self {
|
||||
semaphore: Arc::new(Semaphore::new(limit)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run<'a, Fut, T>(
|
||||
&self,
|
||||
future: Fut,
|
||||
) -> impl 'a + Future<Output = Result<T, LanguageModelCompletionError>>
|
||||
where
|
||||
Fut: 'a + Future<Output = Result<T, LanguageModelCompletionError>>,
|
||||
{
|
||||
let guard = self.semaphore.acquire_arc();
|
||||
async move {
|
||||
let guard = guard.await;
|
||||
let result = future.await?;
|
||||
drop(guard);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stream<'a, Fut, T>(
|
||||
&self,
|
||||
future: Fut,
|
||||
) -> impl 'a
|
||||
+ Future<
|
||||
Output = Result<impl Stream<Item = T::Item> + use<Fut, T>, LanguageModelCompletionError>,
|
||||
>
|
||||
where
|
||||
Fut: 'a + Future<Output = Result<T, LanguageModelCompletionError>>,
|
||||
T: Stream,
|
||||
{
|
||||
let guard = self.semaphore.acquire_arc();
|
||||
async move {
|
||||
let guard = guard.await;
|
||||
let inner = future.await?;
|
||||
Ok(RateLimitGuard {
|
||||
inner,
|
||||
_guard: guard,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
626
crates/language_model_core/src/request.rs
Normal file
626
crates/language_model_core/src/request.rs
Normal file
@@ -0,0 +1,626 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::role::Role;
|
||||
use crate::{LanguageModelToolUse, LanguageModelToolUseId, SharedString};
|
||||
|
||||
/// Dimensions of a `LanguageModelImage`
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct ImageSize {
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
|
||||
pub struct LanguageModelImage {
|
||||
/// A base64-encoded PNG image.
|
||||
pub source: SharedString,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub size: Option<ImageSize>,
|
||||
}
|
||||
|
||||
impl LanguageModelImage {
|
||||
pub fn len(&self) -> usize {
|
||||
self.source.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.source.is_empty()
|
||||
}
|
||||
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
source: "".into(),
|
||||
size: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse Self from a JSON object with case-insensitive field names
|
||||
pub fn from_json(obj: &serde_json::Map<String, serde_json::Value>) -> Option<Self> {
|
||||
let mut source = None;
|
||||
let mut size_obj = None;
|
||||
|
||||
for (k, v) in obj.iter() {
|
||||
match k.to_lowercase().as_str() {
|
||||
"source" => source = v.as_str(),
|
||||
"size" => size_obj = v.as_object(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let source = source?;
|
||||
let size_obj = size_obj?;
|
||||
|
||||
let mut width = None;
|
||||
let mut height = None;
|
||||
|
||||
for (k, v) in size_obj.iter() {
|
||||
match k.to_lowercase().as_str() {
|
||||
"width" => width = v.as_i64().map(|w| w as i32),
|
||||
"height" => height = v.as_i64().map(|h| h as i32),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Some(Self {
|
||||
size: Some(ImageSize {
|
||||
width: width?,
|
||||
height: height?,
|
||||
}),
|
||||
source: SharedString::from(source.to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn estimate_tokens(&self) -> usize {
|
||||
let Some(size) = self.size.as_ref() else {
|
||||
return 0;
|
||||
};
|
||||
let width = size.width.unsigned_abs() as usize;
|
||||
let height = size.height.unsigned_abs() as usize;
|
||||
|
||||
// From: https://docs.anthropic.com/en/docs/build-with-claude/vision#calculate-image-costs
|
||||
(width * height) / 750
|
||||
}
|
||||
|
||||
pub fn to_base64_url(&self) -> String {
|
||||
format!("data:image/png;base64,{}", self.source)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for LanguageModelImage {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("LanguageModelImage")
|
||||
.field("source", &format!("<{} bytes>", self.source.len()))
|
||||
.field("size", &self.size)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
|
||||
pub struct LanguageModelToolResult {
|
||||
pub tool_use_id: LanguageModelToolUseId,
|
||||
pub tool_name: Arc<str>,
|
||||
pub is_error: bool,
|
||||
#[serde(with = "tool_result_content_vec")]
|
||||
pub content: Vec<LanguageModelToolResultContent>,
|
||||
/// The raw tool output, if available, often for debugging or extra state for replay
|
||||
pub output: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl LanguageModelToolResult {
|
||||
/// Concatenates all `Text` parts of the content, ignoring non-text parts.
|
||||
pub fn text_contents(&self) -> String {
|
||||
let mut buffer = String::new();
|
||||
for part in &self.content {
|
||||
if let LanguageModelToolResultContent::Text(text) = part {
|
||||
buffer.push_str(text);
|
||||
}
|
||||
}
|
||||
buffer
|
||||
}
|
||||
|
||||
/// Returns true when there are no content parts, or every part is empty.
|
||||
pub fn is_content_empty(&self) -> bool {
|
||||
self.content.iter().all(|part| part.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
/// Serde helper that accepts both the legacy single-value shape and the new
|
||||
/// array shape for `LanguageModelToolResult::content`, and normalizes both to
|
||||
/// `Vec<LanguageModelToolResultContent>`.
|
||||
mod tool_result_content_vec {
|
||||
use super::LanguageModelToolResultContent;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
pub fn serialize<S>(
|
||||
value: &Vec<LanguageModelToolResultContent>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
value.serialize(serializer)
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Vec<LanguageModelToolResultContent>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = serde_json::Value::deserialize(deserializer)?;
|
||||
match value {
|
||||
serde_json::Value::Array(items) => {
|
||||
let mut out = Vec::with_capacity(items.len());
|
||||
for item in items {
|
||||
out.push(
|
||||
serde_json::from_value::<LanguageModelToolResultContent>(item)
|
||||
.map_err(serde::de::Error::custom)?,
|
||||
);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
other => {
|
||||
let single = serde_json::from_value::<LanguageModelToolResultContent>(other)
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
Ok(vec![single])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash)]
|
||||
pub enum LanguageModelToolResultContent {
|
||||
Text(Arc<str>),
|
||||
Image(LanguageModelImage),
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for LanguageModelToolResultContent {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
use serde::de::Error;
|
||||
|
||||
let value = serde_json::Value::deserialize(deserializer)?;
|
||||
|
||||
// 1. Try as plain string
|
||||
if let Ok(text) = serde_json::from_value::<String>(value.clone()) {
|
||||
return Ok(Self::Text(Arc::from(text)));
|
||||
}
|
||||
|
||||
// 2. Try as object
|
||||
if let Some(obj) = value.as_object() {
|
||||
fn get_field<'a>(
|
||||
obj: &'a serde_json::Map<String, serde_json::Value>,
|
||||
field: &str,
|
||||
) -> Option<&'a serde_json::Value> {
|
||||
obj.iter()
|
||||
.find(|(k, _)| k.to_lowercase() == field.to_lowercase())
|
||||
.map(|(_, v)| v)
|
||||
}
|
||||
|
||||
// Accept wrapped text format: { "type": "text", "text": "..." }
|
||||
if let (Some(type_value), Some(text_value)) =
|
||||
(get_field(obj, "type"), get_field(obj, "text"))
|
||||
&& let Some(type_str) = type_value.as_str()
|
||||
&& type_str.to_lowercase() == "text"
|
||||
&& let Some(text) = text_value.as_str()
|
||||
{
|
||||
return Ok(Self::Text(Arc::from(text)));
|
||||
}
|
||||
|
||||
// Check for wrapped Text variant: { "text": "..." }
|
||||
if let Some((_key, value)) = obj.iter().find(|(k, _)| k.to_lowercase() == "text")
|
||||
&& obj.len() == 1
|
||||
{
|
||||
if let Some(text) = value.as_str() {
|
||||
return Ok(Self::Text(Arc::from(text)));
|
||||
}
|
||||
}
|
||||
|
||||
// Check for wrapped Image variant: { "image": { "source": "...", "size": ... } }
|
||||
if let Some((_key, value)) = obj.iter().find(|(k, _)| k.to_lowercase() == "image")
|
||||
&& obj.len() == 1
|
||||
{
|
||||
if let Some(image_obj) = value.as_object()
|
||||
&& let Some(image) = LanguageModelImage::from_json(image_obj)
|
||||
{
|
||||
return Ok(Self::Image(image));
|
||||
}
|
||||
}
|
||||
|
||||
// Try as direct Image
|
||||
if let Some(image) = LanguageModelImage::from_json(obj) {
|
||||
return Ok(Self::Image(image));
|
||||
}
|
||||
}
|
||||
|
||||
Err(D::Error::custom(format!(
|
||||
"data did not match any variant of LanguageModelToolResultContent. Expected either a string, \
|
||||
an object with 'type': 'text', a wrapped variant like {{\"Text\": \"...\"}}, or an image object. Got: {}",
|
||||
serde_json::to_string_pretty(&value).unwrap_or_else(|_| value.to_string())
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
impl LanguageModelToolResultContent {
|
||||
pub fn to_str(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::Text(text) => Some(text),
|
||||
Self::Image(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
match self {
|
||||
Self::Text(text) => text.chars().all(|c| c.is_whitespace()),
|
||||
Self::Image(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for LanguageModelToolResultContent {
|
||||
fn from(value: &str) -> Self {
|
||||
Self::Text(Arc::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for LanguageModelToolResultContent {
|
||||
fn from(value: String) -> Self {
|
||||
Self::Text(Arc::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<anyhow::Error> for LanguageModelToolResultContent {
|
||||
fn from(error: anyhow::Error) -> Self {
|
||||
Self::Text(Arc::from(error.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LanguageModelImage> for LanguageModelToolResultContent {
|
||||
fn from(image: LanguageModelImage) -> Self {
|
||||
Self::Image(image)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
|
||||
pub enum MessageContent {
|
||||
Text(String),
|
||||
Thinking {
|
||||
text: String,
|
||||
signature: Option<String>,
|
||||
},
|
||||
RedactedThinking(String),
|
||||
Image(LanguageModelImage),
|
||||
ToolUse(LanguageModelToolUse),
|
||||
ToolResult(LanguageModelToolResult),
|
||||
}
|
||||
|
||||
impl MessageContent {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
match self {
|
||||
MessageContent::Text(text) => text.chars().all(|c| c.is_whitespace()),
|
||||
MessageContent::Thinking { text, .. } => text.chars().all(|c| c.is_whitespace()),
|
||||
MessageContent::ToolResult(tool_result) => tool_result.is_content_empty(),
|
||||
MessageContent::RedactedThinking(_)
|
||||
| MessageContent::ToolUse(_)
|
||||
| MessageContent::Image(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for MessageContent {
|
||||
fn from(value: String) -> Self {
|
||||
MessageContent::Text(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for MessageContent {
|
||||
fn from(value: &str) -> Self {
|
||||
MessageContent::Text(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Hash)]
|
||||
pub struct LanguageModelRequestMessage {
|
||||
pub role: Role,
|
||||
pub content: Vec<MessageContent>,
|
||||
pub cache: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub reasoning_details: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl LanguageModelRequestMessage {
|
||||
pub fn string_contents(&self) -> String {
|
||||
let mut buffer = String::new();
|
||||
for content in &self.content {
|
||||
match content {
|
||||
MessageContent::Text(text) => {
|
||||
buffer.push_str(text);
|
||||
}
|
||||
MessageContent::Thinking { text, .. } => {
|
||||
buffer.push_str(text);
|
||||
}
|
||||
MessageContent::ToolResult(tool_result) => {
|
||||
for part in &tool_result.content {
|
||||
if let LanguageModelToolResultContent::Text(text) = part {
|
||||
buffer.push_str(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
MessageContent::RedactedThinking(_)
|
||||
| MessageContent::ToolUse(_)
|
||||
| MessageContent::Image(_) => {}
|
||||
}
|
||||
}
|
||||
buffer
|
||||
}
|
||||
|
||||
pub fn contents_empty(&self) -> bool {
|
||||
self.content.iter().all(|content| content.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Hash, Clone, Serialize, Deserialize)]
|
||||
pub struct LanguageModelRequestTool {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub input_schema: serde_json::Value,
|
||||
pub use_input_streaming: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Hash, Clone, Serialize, Deserialize)]
|
||||
pub enum LanguageModelToolChoice {
|
||||
Auto,
|
||||
Any,
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum CompletionIntent {
|
||||
UserPrompt,
|
||||
Subagent,
|
||||
ToolResults,
|
||||
ThreadSummarization,
|
||||
ThreadContextSummarization,
|
||||
CreateFile,
|
||||
EditFile,
|
||||
InlineAssist,
|
||||
TerminalInlineAssist,
|
||||
GenerateGitCommitMessage,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct LanguageModelRequest {
|
||||
pub thread_id: Option<String>,
|
||||
pub prompt_id: Option<String>,
|
||||
pub intent: Option<CompletionIntent>,
|
||||
pub messages: Vec<LanguageModelRequestMessage>,
|
||||
pub tools: Vec<LanguageModelRequestTool>,
|
||||
pub tool_choice: Option<LanguageModelToolChoice>,
|
||||
pub stop: Vec<String>,
|
||||
pub temperature: Option<f32>,
|
||||
pub thinking_allowed: bool,
|
||||
pub thinking_effort: Option<String>,
|
||||
pub speed: Option<Speed>,
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, Copy, Default, Debug, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema,
|
||||
)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Speed {
|
||||
#[default]
|
||||
Standard,
|
||||
Fast,
|
||||
}
|
||||
|
||||
impl Speed {
|
||||
pub fn toggle(self) -> Self {
|
||||
match self {
|
||||
Speed::Standard => Speed::Fast,
|
||||
Speed::Fast => Speed::Standard,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||
pub struct LanguageModelResponseMessage {
|
||||
pub role: Option<Role>,
|
||||
pub content: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_language_model_tool_result_content_deserialization() {
|
||||
// Test plain string
|
||||
let json = serde_json::json!("hello world");
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(
|
||||
content,
|
||||
LanguageModelToolResultContent::Text(Arc::from("hello world"))
|
||||
);
|
||||
|
||||
// Test wrapped text format: { "type": "text", "text": "..." }
|
||||
let json = serde_json::json!({"type": "text", "text": "hello"});
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(
|
||||
content,
|
||||
LanguageModelToolResultContent::Text(Arc::from("hello"))
|
||||
);
|
||||
|
||||
// Test single-field text object: { "text": "..." }
|
||||
let json = serde_json::json!({"text": "hello"});
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(
|
||||
content,
|
||||
LanguageModelToolResultContent::Text(Arc::from("hello"))
|
||||
);
|
||||
|
||||
// Test case-insensitive type field
|
||||
let json = serde_json::json!({"Type": "Text", "Text": "hello"});
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(
|
||||
content,
|
||||
LanguageModelToolResultContent::Text(Arc::from("hello"))
|
||||
);
|
||||
|
||||
// Test image object
|
||||
let json = serde_json::json!({
|
||||
"source": "base64encodedimagedata",
|
||||
"size": {"width": 100, "height": 200}
|
||||
});
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
match content {
|
||||
LanguageModelToolResultContent::Image(image) => {
|
||||
assert_eq!(image.source.as_ref(), "base64encodedimagedata");
|
||||
let size = image.size.expect("size");
|
||||
assert_eq!(size.width, 100);
|
||||
assert_eq!(size.height, 200);
|
||||
}
|
||||
_ => panic!("Expected Image variant"),
|
||||
}
|
||||
|
||||
// Test wrapped image: { "image": { "source": "...", "size": ... } }
|
||||
let json = serde_json::json!({
|
||||
"image": {
|
||||
"source": "wrappedimagedata",
|
||||
"size": {"width": 50, "height": 75}
|
||||
}
|
||||
});
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
match content {
|
||||
LanguageModelToolResultContent::Image(image) => {
|
||||
assert_eq!(image.source.as_ref(), "wrappedimagedata");
|
||||
let size = image.size.expect("size");
|
||||
assert_eq!(size.width, 50);
|
||||
assert_eq!(size.height, 75);
|
||||
}
|
||||
_ => panic!("Expected Image variant"),
|
||||
}
|
||||
|
||||
// Test case insensitive
|
||||
let json = serde_json::json!({
|
||||
"Source": "caseinsensitive",
|
||||
"Size": {"Width": 30, "Height": 40}
|
||||
});
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
match content {
|
||||
LanguageModelToolResultContent::Image(image) => {
|
||||
assert_eq!(image.source.as_ref(), "caseinsensitive");
|
||||
let size = image.size.expect("size");
|
||||
assert_eq!(size.width, 30);
|
||||
assert_eq!(size.height, 40);
|
||||
}
|
||||
_ => panic!("Expected Image variant"),
|
||||
}
|
||||
|
||||
// Test direct image object
|
||||
let json = serde_json::json!({
|
||||
"source": "directimage",
|
||||
"size": {"width": 200, "height": 300}
|
||||
});
|
||||
let content: LanguageModelToolResultContent = serde_json::from_value(json).unwrap();
|
||||
match content {
|
||||
LanguageModelToolResultContent::Image(image) => {
|
||||
assert_eq!(image.source.as_ref(), "directimage");
|
||||
let size = image.size.expect("size");
|
||||
assert_eq!(size.width, 200);
|
||||
assert_eq!(size.height, 300);
|
||||
}
|
||||
_ => panic!("Expected Image variant"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_language_model_tool_result_content_vec_deserialization() {
|
||||
// Legacy single-value shape is normalized to a Vec.
|
||||
let json = serde_json::json!({
|
||||
"tool_use_id": "abc",
|
||||
"tool_name": "echo",
|
||||
"is_error": false,
|
||||
"content": "hello",
|
||||
"output": null,
|
||||
});
|
||||
let result: LanguageModelToolResult = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(
|
||||
result.content,
|
||||
vec![LanguageModelToolResultContent::Text(Arc::from("hello"))]
|
||||
);
|
||||
|
||||
// Legacy wrapped single-value shape also works.
|
||||
let json = serde_json::json!({
|
||||
"tool_use_id": "abc",
|
||||
"tool_name": "echo",
|
||||
"is_error": false,
|
||||
"content": {"type": "text", "text": "hello"},
|
||||
"output": null,
|
||||
});
|
||||
let result: LanguageModelToolResult = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(
|
||||
result.content,
|
||||
vec![LanguageModelToolResultContent::Text(Arc::from("hello"))]
|
||||
);
|
||||
|
||||
// New array shape with text + image deserializes into a Vec.
|
||||
let json = serde_json::json!({
|
||||
"tool_use_id": "abc",
|
||||
"tool_name": "echo",
|
||||
"is_error": false,
|
||||
"content": [
|
||||
{"type": "text", "text": "foo"},
|
||||
{"source": "data", "size": {"width": 1, "height": 2}}
|
||||
],
|
||||
"output": null,
|
||||
});
|
||||
let result: LanguageModelToolResult = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(result.content.len(), 2);
|
||||
assert_eq!(
|
||||
result.content[0],
|
||||
LanguageModelToolResultContent::Text(Arc::from("foo"))
|
||||
);
|
||||
match &result.content[1] {
|
||||
LanguageModelToolResultContent::Image(image) => {
|
||||
assert_eq!(image.source.as_ref(), "data");
|
||||
}
|
||||
_ => panic!("Expected Image variant"),
|
||||
}
|
||||
|
||||
// Round-tripping preserves multi-part content.
|
||||
let roundtripped: LanguageModelToolResult =
|
||||
serde_json::from_value(serde_json::to_value(&result).unwrap()).unwrap();
|
||||
assert_eq!(roundtripped, result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_contents_includes_all_tool_result_text_parts() {
|
||||
let tool_result = LanguageModelToolResult {
|
||||
tool_use_id: LanguageModelToolUseId::from("id".to_string()),
|
||||
tool_name: Arc::from("tool"),
|
||||
is_error: false,
|
||||
content: vec![
|
||||
LanguageModelToolResultContent::Text(Arc::from("first ")),
|
||||
LanguageModelToolResultContent::Image(LanguageModelImage::empty()),
|
||||
LanguageModelToolResultContent::Text(Arc::from("second")),
|
||||
],
|
||||
output: None,
|
||||
};
|
||||
let message = LanguageModelRequestMessage {
|
||||
role: Role::User,
|
||||
content: vec![
|
||||
MessageContent::Text("prefix ".to_string()),
|
||||
MessageContent::ToolResult(tool_result),
|
||||
MessageContent::Text(" suffix".to_string()),
|
||||
],
|
||||
cache: false,
|
||||
reasoning_details: None,
|
||||
};
|
||||
assert_eq!(message.string_contents(), "prefix first second suffix");
|
||||
}
|
||||
}
|
||||
30
crates/language_model_core/src/role.rs
Normal file
30
crates/language_model_core/src/role.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq, Hash)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Role {
|
||||
User,
|
||||
Assistant,
|
||||
System,
|
||||
}
|
||||
|
||||
impl Role {
|
||||
pub fn cycle(self) -> Role {
|
||||
match self {
|
||||
Role::User => Role::Assistant,
|
||||
Role::Assistant => Role::System,
|
||||
Role::System => Role::User,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Role {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Role::User => write!(f, "user"),
|
||||
Role::Assistant => write!(f, "assistant"),
|
||||
Role::System => write!(f, "system"),
|
||||
}
|
||||
}
|
||||
}
|
||||
448
crates/language_model_core/src/tool_schema.rs
Normal file
448
crates/language_model_core/src/tool_schema.rs
Normal file
@@ -0,0 +1,448 @@
|
||||
use anyhow::Result;
|
||||
use schemars::{
|
||||
JsonSchema, Schema,
|
||||
generate::SchemaSettings,
|
||||
transform::{Transform, transform_subschemas},
|
||||
};
|
||||
use serde_json::Value;
|
||||
|
||||
/// Indicates the format used to define the input schema for a language model tool.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
||||
pub enum LanguageModelToolSchemaFormat {
|
||||
/// A JSON schema, see https://json-schema.org
|
||||
JsonSchema,
|
||||
/// A subset of an OpenAPI 3.0 schema object supported by Google AI, see https://ai.google.dev/api/caching#Schema
|
||||
JsonSchemaSubset,
|
||||
}
|
||||
|
||||
pub fn root_schema_for<T: JsonSchema>(format: LanguageModelToolSchemaFormat) -> Schema {
|
||||
let mut generator = match format {
|
||||
LanguageModelToolSchemaFormat::JsonSchema => SchemaSettings::draft07()
|
||||
.with(|settings| {
|
||||
settings.meta_schema = None;
|
||||
settings.inline_subschemas = true;
|
||||
})
|
||||
.into_generator(),
|
||||
LanguageModelToolSchemaFormat::JsonSchemaSubset => SchemaSettings::openapi3()
|
||||
.with(|settings| {
|
||||
settings.meta_schema = None;
|
||||
settings.inline_subschemas = true;
|
||||
})
|
||||
.with_transform(ToJsonSchemaSubsetTransform)
|
||||
.into_generator(),
|
||||
};
|
||||
generator.root_schema_for::<T>()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ToJsonSchemaSubsetTransform;
|
||||
|
||||
impl Transform for ToJsonSchemaSubsetTransform {
|
||||
fn transform(&mut self, schema: &mut Schema) {
|
||||
// Ensure that the type field is not an array, this happens when we use
|
||||
// Option<T>, the type will be [T, "null"].
|
||||
if let Some(type_field) = schema.get_mut("type")
|
||||
&& let Some(types) = type_field.as_array()
|
||||
&& let Some(first_type) = types.first()
|
||||
{
|
||||
*type_field = first_type.clone();
|
||||
}
|
||||
|
||||
// oneOf is not supported, use anyOf instead
|
||||
if let Some(one_of) = schema.remove("oneOf") {
|
||||
schema.insert("anyOf".to_string(), one_of);
|
||||
}
|
||||
|
||||
transform_subschemas(self, schema);
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to adapt a JSON schema representation to be compatible with the specified format.
|
||||
///
|
||||
/// If the json cannot be made compatible with the specified format, an error is returned.
|
||||
pub fn adapt_schema_to_format(
|
||||
json: &mut Value,
|
||||
format: LanguageModelToolSchemaFormat,
|
||||
) -> Result<()> {
|
||||
if let Value::Object(obj) = json {
|
||||
obj.remove("$schema");
|
||||
obj.remove("title");
|
||||
obj.remove("description");
|
||||
}
|
||||
|
||||
match format {
|
||||
LanguageModelToolSchemaFormat::JsonSchema => preprocess_json_schema(json),
|
||||
LanguageModelToolSchemaFormat::JsonSchemaSubset => adapt_to_json_schema_subset(json),
|
||||
}
|
||||
}
|
||||
|
||||
fn preprocess_json_schema(json: &mut Value) -> Result<()> {
|
||||
if let Value::Object(obj) = json
|
||||
&& matches!(obj.get("type"), Some(Value::String(s)) if s == "object")
|
||||
{
|
||||
if !obj.contains_key("additionalProperties") {
|
||||
obj.insert("additionalProperties".to_string(), Value::Bool(false));
|
||||
}
|
||||
|
||||
if !obj.contains_key("properties") {
|
||||
obj.insert("properties".to_string(), Value::Object(Default::default()));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn adapt_to_json_schema_subset(json: &mut Value) -> Result<()> {
|
||||
if let Value::Object(obj) = json {
|
||||
const UNSUPPORTED_KEYS: [&str; 4] = ["if", "then", "else", "$ref"];
|
||||
|
||||
for key in UNSUPPORTED_KEYS {
|
||||
anyhow::ensure!(
|
||||
!obj.contains_key(key),
|
||||
"Schema cannot be made compatible because it contains \"{key}\""
|
||||
);
|
||||
}
|
||||
|
||||
const KEYS_TO_REMOVE: [(&str, fn(&Value) -> bool); 6] = [
|
||||
("format", |value| value.is_string()),
|
||||
("additionalProperties", |_| true),
|
||||
("propertyNames", |_| true),
|
||||
("exclusiveMinimum", |value| value.is_number()),
|
||||
("exclusiveMaximum", |value| value.is_number()),
|
||||
("optional", |value| value.is_boolean()),
|
||||
];
|
||||
for (key, predicate) in KEYS_TO_REMOVE {
|
||||
if let Some(value) = obj.get(key)
|
||||
&& predicate(value)
|
||||
{
|
||||
obj.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure that the type field is not an array. This can happen with MCP tool
|
||||
// schemas that use multiple types (e.g. `["string", "number"]` or `["string", "null"]`).
|
||||
if let Some(type_value) = obj.get_mut("type")
|
||||
&& let Some(types) = type_value.as_array()
|
||||
&& let Some(first_type) = types.first().cloned()
|
||||
{
|
||||
*type_value = first_type;
|
||||
}
|
||||
|
||||
if matches!(obj.get("description"), Some(Value::String(_)))
|
||||
&& !obj.contains_key("type")
|
||||
&& !(obj.contains_key("anyOf")
|
||||
|| obj.contains_key("oneOf")
|
||||
|| obj.contains_key("allOf"))
|
||||
{
|
||||
obj.insert("type".to_string(), Value::String("string".to_string()));
|
||||
}
|
||||
|
||||
if let Some(subschemas) = obj.get_mut("oneOf")
|
||||
&& subschemas.is_array()
|
||||
{
|
||||
let subschemas_clone = subschemas.clone();
|
||||
obj.remove("oneOf");
|
||||
obj.insert("anyOf".to_string(), subschemas_clone);
|
||||
}
|
||||
|
||||
for (_, value) in obj.iter_mut() {
|
||||
if let Value::Object(_) | Value::Array(_) = value {
|
||||
adapt_to_json_schema_subset(value)?;
|
||||
}
|
||||
}
|
||||
} else if let Value::Array(arr) = json {
|
||||
for item in arr.iter_mut() {
|
||||
adapt_to_json_schema_subset(item)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn test_transform_adds_type_when_missing() {
|
||||
let mut json = json!({
|
||||
"description": "A test field without type"
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"description": "A test field without type",
|
||||
"type": "string"
|
||||
})
|
||||
);
|
||||
|
||||
let mut json = json!({
|
||||
"description": {
|
||||
"value": "abc",
|
||||
"type": "string"
|
||||
}
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"description": {
|
||||
"value": "abc",
|
||||
"type": "string"
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_removes_unsupported_keys() {
|
||||
let mut json = json!({
|
||||
"description": "A test field",
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"exclusiveMinimum": 0,
|
||||
"exclusiveMaximum": 100,
|
||||
"additionalProperties": false,
|
||||
"optional": true
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"description": "A test field",
|
||||
"type": "integer"
|
||||
})
|
||||
);
|
||||
|
||||
let mut json = json!({
|
||||
"description": "A test field",
|
||||
"type": "integer",
|
||||
"format": {},
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"description": "A test field",
|
||||
"type": "integer",
|
||||
"format": {},
|
||||
})
|
||||
);
|
||||
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "type": "string" }
|
||||
},
|
||||
"additionalProperties": { "type": "string" },
|
||||
"propertyNames": { "pattern": "^[A-Za-z]+$" }
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "type": "string" }
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_one_of_to_any_of() {
|
||||
let mut json = json!({
|
||||
"description": "A test field",
|
||||
"oneOf": [
|
||||
{ "type": "string" },
|
||||
{ "type": "integer" }
|
||||
]
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"description": "A test field",
|
||||
"anyOf": [
|
||||
{ "type": "string" },
|
||||
{ "type": "integer" }
|
||||
]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_nested_objects() {
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"nested": {
|
||||
"oneOf": [
|
||||
{ "type": "string" },
|
||||
{ "type": "null" }
|
||||
],
|
||||
"format": "email"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"nested": {
|
||||
"anyOf": [
|
||||
{ "type": "string" },
|
||||
{ "type": "null" }
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_array_type_to_single_type() {
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"projectSlugOrId": {
|
||||
"type": ["string", "number"],
|
||||
"description": "Project slug or numeric ID"
|
||||
},
|
||||
"optionalName": {
|
||||
"type": ["string", "null"],
|
||||
"description": "An optional name"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
adapt_to_json_schema_subset(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"projectSlugOrId": {
|
||||
"type": "string",
|
||||
"description": "Project slug or numeric ID"
|
||||
},
|
||||
"optionalName": {
|
||||
"type": "string",
|
||||
"description": "An optional name"
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform_fails_if_unsupported_keys_exist() {
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"$ref": "#/definitions/User",
|
||||
}
|
||||
});
|
||||
|
||||
assert!(adapt_to_json_schema_subset(&mut json).is_err());
|
||||
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"if": "...",
|
||||
}
|
||||
});
|
||||
|
||||
assert!(adapt_to_json_schema_subset(&mut json).is_err());
|
||||
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"then": "...",
|
||||
}
|
||||
});
|
||||
|
||||
assert!(adapt_to_json_schema_subset(&mut json).is_err());
|
||||
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"else": "...",
|
||||
}
|
||||
});
|
||||
|
||||
assert!(adapt_to_json_schema_subset(&mut json).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_preprocess_json_schema_adds_additional_properties() {
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
preprocess_json_schema(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_preprocess_json_schema_preserves_additional_properties() {
|
||||
let mut json = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": true
|
||||
});
|
||||
|
||||
preprocess_json_schema(&mut json).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
json,
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": true
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
111
crates/language_model_core/src/util.rs
Normal file
111
crates/language_model_core/src/util.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Parses tool call arguments JSON, treating empty strings as empty objects.
|
||||
///
|
||||
/// Many LLM providers return empty strings for tool calls with no arguments.
|
||||
/// This helper normalizes that behavior by converting empty strings to `{}`.
|
||||
pub fn parse_tool_arguments(arguments: &str) -> Result<serde_json::Value, serde_json::Error> {
|
||||
if arguments.is_empty() {
|
||||
Ok(serde_json::Value::Object(Default::default()))
|
||||
} else {
|
||||
serde_json::Value::from_str(arguments)
|
||||
}
|
||||
}
|
||||
|
||||
/// `partial_json_fixer::fix_json` converts a trailing `\` inside a string into `\\`
|
||||
/// (a literal backslash). When used for incremental parsing (comparing successive
|
||||
/// parses to extract deltas), this produces a spurious backslash character that
|
||||
/// doesn't exist in the final text, corrupting the output.
|
||||
///
|
||||
/// This function strips any trailing incomplete escape sequence before fixing,
|
||||
/// so each intermediate parse produces a true prefix of the final string value.
|
||||
pub fn fix_streamed_json(partial_json: &str) -> String {
|
||||
let json = strip_trailing_incomplete_escape(partial_json);
|
||||
partial_json_fixer::fix_json(json)
|
||||
}
|
||||
|
||||
fn strip_trailing_incomplete_escape(json: &str) -> &str {
|
||||
let trailing_backslashes = json
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|&&b| b == b'\\')
|
||||
.count();
|
||||
if trailing_backslashes % 2 == 1 {
|
||||
&json[..json.len() - 1]
|
||||
} else {
|
||||
json
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a "prompt is too long: N tokens ..." message and extracts the token count.
|
||||
pub fn parse_prompt_too_long(message: &str) -> Option<u64> {
|
||||
message
|
||||
.strip_prefix("prompt is too long: ")?
|
||||
.split_once(" tokens")?
|
||||
.0
|
||||
.parse()
|
||||
.ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_fix_streamed_json_strips_incomplete_escape() {
|
||||
let fixed = fix_streamed_json(r#"{"text": "hello\"#);
|
||||
let parsed: serde_json::Value = serde_json::from_str(&fixed).expect("valid json");
|
||||
assert_eq!(parsed["text"], "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fix_streamed_json_preserves_complete_escape() {
|
||||
let fixed = fix_streamed_json(r#"{"text": "hello\\"#);
|
||||
let parsed: serde_json::Value = serde_json::from_str(&fixed).expect("valid json");
|
||||
assert_eq!(parsed["text"], "hello\\");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fix_streamed_json_strips_escape_after_complete_escape() {
|
||||
let fixed = fix_streamed_json(r#"{"text": "hello\\\"#);
|
||||
let parsed: serde_json::Value = serde_json::from_str(&fixed).expect("valid json");
|
||||
assert_eq!(parsed["text"], "hello\\");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fix_streamed_json_no_escape_at_end() {
|
||||
let fixed = fix_streamed_json(r#"{"text": "hello"#);
|
||||
let parsed: serde_json::Value = serde_json::from_str(&fixed).expect("valid json");
|
||||
assert_eq!(parsed["text"], "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fix_streamed_json_newline_escape_boundary() {
|
||||
let fixed = fix_streamed_json(r#"{"text": "line1\"#);
|
||||
let parsed: serde_json::Value = serde_json::from_str(&fixed).expect("valid json");
|
||||
assert_eq!(parsed["text"], "line1");
|
||||
|
||||
let fixed = fix_streamed_json(r#"{"text": "line1\nline2"#);
|
||||
let parsed: serde_json::Value = serde_json::from_str(&fixed).expect("valid json");
|
||||
assert_eq!(parsed["text"], "line1\nline2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fix_streamed_json_incremental_delta_correctness() {
|
||||
let chunk1 = r#"{"replacement_text": "fn foo() {\"#;
|
||||
let fixed1 = fix_streamed_json(chunk1);
|
||||
let parsed1: serde_json::Value = serde_json::from_str(&fixed1).expect("valid json");
|
||||
let text1 = parsed1["replacement_text"].as_str().expect("string");
|
||||
assert_eq!(text1, "fn foo() {");
|
||||
|
||||
let chunk2 = r#"{"replacement_text": "fn foo() {\n return bar;\n}"}"#;
|
||||
let fixed2 = fix_streamed_json(chunk2);
|
||||
let parsed2: serde_json::Value = serde_json::from_str(&fixed2).expect("valid json");
|
||||
let text2 = parsed2["replacement_text"].as_str().expect("string");
|
||||
assert_eq!(text2, "fn foo() {\n return bar;\n}");
|
||||
|
||||
let delta = &text2[text1.len()..];
|
||||
assert_eq!(delta, "\n return bar;\n}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user