Skip to main content

weft_core/layers/
transform.rs

1use crate::config::ProviderConfig;
2use crate::types::{ChatRequest, ChatResponse, StreamChunk};
3use anyhow::Result;
4use async_trait::async_trait;
5use bytes::Bytes;
6
7/// Describes an outgoing HTTP request to a provider.
8#[derive(Debug)]
9pub struct ProviderRequest {
10    pub url: String,
11    pub method: String,
12    pub headers: Vec<(String, String)>,
13    pub body: Bytes,
14}
15
16#[async_trait]
17pub trait TransformLayer: Send + Sync {
18    /// Convert a ChatRequest into the provider's HTTP format.
19    async fn transform_request(
20        &self,
21        request: &ChatRequest,
22        api_key: &str,
23        provider: &ProviderConfig,
24    ) -> Result<ProviderRequest>;
25
26    /// Parse a provider's HTTP response into ChatResponse.
27    async fn transform_response(
28        &self,
29        status: u16,
30        body: Bytes,
31        provider: &ProviderConfig,
32    ) -> Result<ChatResponse>;
33
34    /// Parse one SSE chunk from the provider into our StreamChunk.
35    /// Returns None if the chunk is a keep-alive or should be skipped.
36    async fn transform_stream_chunk(
37        &self,
38        chunk: &str,
39        provider: &ProviderConfig,
40    ) -> Result<Option<StreamChunk>>;
41}