Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(proxy): converter seam foundation (api_type + SSE accumulator)

Akarshan Biswas6123cc03fbec
diff --git a/src-tauri/src/core/server/converters.rs b/src-tauri/src/core/server/converters.rs
new file mode 100644
index 000000000..e4c455219
--- /dev/null
+++ b/src-tauri/src/core/server/converters.rs
@@ -0,0 +1,162 @@
+//! Translating-gateway support for the proxy.
+//!
+//! Jan's proxy accepts OpenAI chat/completions and, for OpenAI-compatible
+//! upstreams, forwards verbatim. To also front providers with a different
+//! native wire API (OpenAI `/v1/responses`, Google `generateContent`,
+//! Anthropic `/v1/messages`), each such provider gets a converter that rewrites
+//! the request and translates the response back to chat/completions.
+//!
+//! This module currently provides the shared primitive every converter needs:
+//! an [`SseAccumulator`] that reassembles complete Server-Sent Events across
+//! network chunk boundaries. The existing inline proxy parser splits each chunk
+//! on lines and drops partial lines, which only survives because upstreams tend
+//! to flush whole events; a real translator cannot rely on that.
+
+/// One parsed Server-Sent Event.
+#[derive(Debug, Clone, Default, PartialEq, Eq)]
+pub struct SseEvent {
+ /// The `event:` field value, empty when absent (chat/completions omits it;
+ /// OpenAI `/responses` and Anthropic set it).
+ pub event: String,
+ /// Concatenated `data:` payload (multiple `data:` lines joined by `\n`),
+ /// with sentinels like `[DONE]` preserved verbatim.
+ pub data: String,
+}
+
+/// Accumulates raw response text and yields complete SSE events, buffering any
+/// partial trailing event until its terminating blank line arrives in a later
+/// chunk.
+#[derive(Debug, Default)]
+pub struct SseAccumulator {
+ buf: String,
+}
+
+impl SseAccumulator {
+ pub fn new() -> Self {
+ Self { buf: String::new() }
+ }
+
+ /// Feed a chunk of the response body; returns every event completed by it.
+ pub fn push(&mut self, chunk: &str) -> Vec<SseEvent> {
+ // Normalize CRLF so boundary detection only has to look for "\n\n".
+ self.buf.push_str(&chunk.replace("\r\n", "\n"));
+ let mut events = Vec::new();
+ while let Some(idx) = self.buf.find("\n\n") {
+ let raw: String = self.buf.drain(..idx + 2).collect();
+ if let Some(ev) = parse_event(&raw) {
+ events.push(ev);
+ }
+ }
+ events
+ }
+
+ /// Flush a final event that arrived without a terminating blank line (some
+ /// servers omit it before closing the connection).
+ pub fn finish(&mut self) -> Option<SseEvent> {
+ let raw = std::mem::take(&mut self.buf);
+ parse_event(&raw)
+ }
+}
+
+fn parse_event(raw: &str) -> Option<SseEvent> {
+ let mut ev = SseEvent::default();
+ let mut data_lines: Vec<&str> = Vec::new();
+ let mut has_field = false;
+ for line in raw.lines() {
+ // Blank lines separate events; lines starting with ':' are comments.
+ if line.is_empty() || line.starts_with(':') {
+ continue;
+ }
+ if let Some(rest) = line.strip_prefix("data:") {
+ data_lines.push(rest.strip_prefix(' ').unwrap_or(rest));
+ has_field = true;
+ } else if let Some(rest) = line.strip_prefix("event:") {
+ ev.event = rest.strip_prefix(' ').unwrap_or(rest).to_string();
+ has_field = true;
+ }
+ // `id:`, `retry:`, and unknown fields are irrelevant to translation.
+ }
+ if !has_field {
+ return None;
+ }
+ ev.data = data_lines.join("\n");
+ Some(ev)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parses_a_single_event_in_one_chunk() {
+ let mut acc = SseAccumulator::new();
+ let events = acc.push("data: {\"a\":1}\n\n");
+ assert_eq!(
+ events,
+ vec![SseEvent {
+ event: String::new(),
+ data: "{\"a\":1}".to_string()
+ }]
+ );
+ }
+
+ #[test]
+ fn buffers_an_event_split_across_chunks() {
+ let mut acc = SseAccumulator::new();
+ assert!(acc.push("data: {\"a\":").is_empty());
+ assert!(acc.push("1}").is_empty());
+ let events = acc.push("\n\n");
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].data, "{\"a\":1}");
+ }
+
+ #[test]
+ fn yields_multiple_events_from_one_chunk() {
+ let mut acc = SseAccumulator::new();
+ let events = acc.push("data: 1\n\ndata: 2\n\n");
+ assert_eq!(events.len(), 2);
+ assert_eq!(events[0].data, "1");
+ assert_eq!(events[1].data, "2");
+ }
+
+ #[test]
+ fn joins_multiline_data_and_reads_event_field() {
+ let mut acc = SseAccumulator::new();
+ let events = acc.push("event: response.delta\ndata: line1\ndata: line2\n\n");
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].event, "response.delta");
+ assert_eq!(events[0].data, "line1\nline2");
+ }
+
+ #[test]
+ fn normalizes_crlf_boundaries() {
+ let mut acc = SseAccumulator::new();
+ let events = acc.push("data: x\r\n\r\n");
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].data, "x");
+ }
+
+ #[test]
+ fn ignores_comment_lines_and_preserves_done_sentinel() {
+ let mut acc = SseAccumulator::new();
+ let events = acc.push(": keep-alive\n\ndata: [DONE]\n\n");
+ // The comment-only block yields no event; [DONE] is preserved verbatim.
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].data, "[DONE]");
+ }
+
+ #[test]
+ fn finish_flushes_a_trailing_event_without_blank_line() {
+ let mut acc = SseAccumulator::new();
+ assert!(acc.push("data: tail").is_empty());
+ let last = acc.finish();
+ assert_eq!(last.map(|e| e.data), Some("tail".to_string()));
+ }
+
+ #[test]
+ fn finish_is_empty_when_buffer_has_no_fields() {
+ let mut acc = SseAccumulator::new();
+ acc.push("data: done\n\n");
+ assert_eq!(acc.finish(), None);
+ }
+}
diff --git a/src-tauri/src/core/server/mod.rs b/src-tauri/src/core/server/mod.rs
index f17aaf4a5..46f448c9b 100644
--- a/src-tauri/src/core/server/mod.rs
+++ b/src-tauri/src/core/server/mod.rs
@@ -1,4 +1,5 @@
pub mod commands;
+pub mod converters;
pub mod provider_secrets;
pub mod proxy;
pub mod remote_provider_commands;
diff --git a/src-tauri/src/core/server/remote_provider_commands.rs b/src-tauri/src/core/server/remote_provider_commands.rs
index 099bf8117..44a95b706 100644
--- a/src-tauri/src/core/server/remote_provider_commands.rs
+++ b/src-tauri/src/core/server/remote_provider_commands.rs
@@ -23,6 +23,10 @@ pub struct RegisterProviderRequest {
pub base_url: Option<String>,
pub custom_headers: Vec<ProviderCustomHeader>,
pub models: Vec<String>,
+ /// Upstream wire API (`"openai"` default, or `"openai-responses"` /
+ /// `"google"` / `"anthropic"` to engage a translating converter).
+ #[serde(default)]
+ pub api_type: Option<String>,
}
fn merge_register_api_keys(api_key: Option<String>, api_keys: Vec<String>) -> Vec<String> {
@@ -68,6 +72,7 @@ pub async fn register_provider_config(
})
.collect(),
models: request.models, // Models will be added when they are configured
+ api_type: request.api_type,
};
// Persist the key chain to the OS keyring so it survives webview storage
@@ -201,6 +206,7 @@ mod tests {
base_url: None,
custom_headers: vec![],
models: vec![],
+ api_type: None,
}
}
diff --git a/src-tauri/src/core/state.rs b/src-tauri/src/core/state.rs
index a2633b01a..8915e05c0 100644
--- a/src-tauri/src/core/state.rs
+++ b/src-tauri/src/core/state.rs
@@ -27,6 +27,13 @@ pub struct ProviderConfig {
pub base_url: Option<String>,
pub custom_headers: Vec<ProviderCustomHeader>,
pub models: Vec<String>,
+ /// Upstream wire API this provider speaks. `None`/`"openai"` = OpenAI
+ /// chat/completions (verbatim passthrough). Other values select a
+ /// translating converter (e.g. `"openai-responses"`, `"google"`,
+ /// `"anthropic"`) so the proxy can accept OpenAI-shaped requests and talk
+ /// the provider's native API.
+ #[serde(default)]
+ pub api_type: Option<String>,
}
impl ProviderConfig {