mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-09-07 00:01:49 +00:00
* feat: send prompt_cache_key on the openai responses api * fix: bound prompt_cache_key to the provider limit and scope it to retryable paths * fix: keep a digest suffix when bounding long frontend cache keys * docs: attach the cache-key doc block to the function it describes
125 lines
4.4 KiB
Rust
125 lines
4.4 KiB
Rust
use async_trait::async_trait;
|
|
use windmill_common::{client::AuthedClient, error::Error};
|
|
use windmill_types::s3::S3Object;
|
|
|
|
use crate::ai_types::OpenAIToolCall;
|
|
use crate::proxy::{ProxyBuildArgs, ProxyRequest};
|
|
use crate::types::*;
|
|
|
|
/// Arguments for building an AI request
|
|
pub struct BuildRequestArgs<'a> {
|
|
pub messages: &'a [OpenAIMessage],
|
|
pub tools: Option<&'a [ToolDef]>,
|
|
pub model: &'a str,
|
|
pub temperature: Option<f32>,
|
|
/// Provider-native reasoning effort token (e.g. `low`, `high`, `none`).
|
|
/// Each `build_request` maps it onto the provider's thinking config.
|
|
pub reasoning_effort: Option<&'a str>,
|
|
pub max_tokens: Option<u32>,
|
|
pub output_schema: Option<&'a OpenAPISchema>,
|
|
pub output_type: &'a OutputType,
|
|
pub system_prompt: Option<&'a str>,
|
|
pub user_message: &'a str,
|
|
pub attachments: Option<&'a [S3Object]>,
|
|
pub has_websearch: bool,
|
|
/// Routing hint for the provider's prompt cache. Requests sharing a prompt prefix
|
|
/// must reuse one key to land on the same cache, so it is derived from what pins
|
|
/// the prefix (the step), never from the request. `None` retries a key the
|
|
/// endpoint rejected.
|
|
pub prompt_cache_key: Option<&'a str>,
|
|
}
|
|
|
|
/// Response from AI provider
|
|
pub enum ParsedResponse {
|
|
Text {
|
|
content: Option<String>,
|
|
tool_calls: Vec<OpenAIToolCall>,
|
|
events_str: Option<String>,
|
|
annotations: Vec<UrlCitation>,
|
|
used_websearch: bool,
|
|
usage: Option<TokenUsage>,
|
|
},
|
|
Image {
|
|
base64_data: String,
|
|
},
|
|
}
|
|
|
|
/// Trait for streaming AI events to a sink (e.g., database persistence).
|
|
/// Implemented by the worker's StreamEventProcessor.
|
|
#[async_trait]
|
|
pub trait StreamEventSink: Send + Sync {
|
|
async fn send(&self, event: StreamingEvent, events_str: &mut String) -> Result<(), Error>;
|
|
}
|
|
|
|
/// Trait for building provider-specific AI requests
|
|
#[async_trait]
|
|
pub trait QueryBuilder: Send + Sync {
|
|
/// Check if this provider supports tools with the given output type
|
|
fn supports_tools_with_output_type(&self, output_type: &OutputType) -> bool;
|
|
|
|
/// Build the request body for the provider
|
|
async fn build_request(
|
|
&self,
|
|
args: &BuildRequestArgs<'_>,
|
|
client: &AuthedClient,
|
|
workspace_id: &str,
|
|
) -> Result<String, Error>;
|
|
|
|
/// Build the request body without usage tracking (for retry on incompatible providers)
|
|
/// Default implementation just calls build_request (most providers don't need this)
|
|
async fn build_request_without_usage(
|
|
&self,
|
|
args: &BuildRequestArgs<'_>,
|
|
client: &AuthedClient,
|
|
workspace_id: &str,
|
|
) -> Result<String, Error> {
|
|
self.build_request(args, client, workspace_id).await
|
|
}
|
|
|
|
/// Whether this provider supports retry without usage tracking
|
|
/// Only OtherQueryBuilder (OpenAI-compatible providers) needs this
|
|
fn supports_retry_without_usage(&self) -> bool {
|
|
false
|
|
}
|
|
|
|
/// Whether a rejected request may be retried on the `/chat/completions` surface of
|
|
/// the same resource. Azure serves the Responses API for only a subset of models and
|
|
/// regions, and an Azure model name is a user-chosen deployment name, so such a
|
|
/// resource can only find out by being asked.
|
|
fn supports_chat_completions_fallback(&self, _base_url: &str) -> bool {
|
|
false
|
|
}
|
|
|
|
/// Build a provider-specific request from an OpenAI-compatible proxy request.
|
|
fn build_proxy_request(&self, args: &ProxyBuildArgs<'_>) -> Result<ProxyRequest, Error> {
|
|
Err(Error::BadRequest(format!(
|
|
"Proxy request building is not supported for provider {:?}",
|
|
args.credentials.provider
|
|
)))
|
|
}
|
|
|
|
/// Parse the image response from the provider
|
|
async fn parse_image_response(
|
|
&self,
|
|
response: reqwest::Response,
|
|
) -> Result<ParsedResponse, Error>;
|
|
|
|
/// Parse streaming response from the provider
|
|
async fn parse_streaming_response(
|
|
&self,
|
|
response: reqwest::Response,
|
|
stream_event_sink: Box<dyn StreamEventSink>,
|
|
) -> Result<ParsedResponse, Error>;
|
|
|
|
/// Get the API endpoint for this provider
|
|
fn get_endpoint(&self, base_url: &str, model: &str, output_type: &OutputType) -> String;
|
|
|
|
/// Get the authentication headers for this provider
|
|
fn get_auth_headers(
|
|
&self,
|
|
api_key: &str,
|
|
base_url: &str,
|
|
output_type: &OutputType,
|
|
) -> Vec<(&'static str, String)>;
|
|
}
|