Skip to content

Commit ad0f876

Browse files
committed
feat: support setting llm extra
1 parent e63c906 commit ad0f876

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/ai/mod.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ async fn test_groq_asr() {
128128
#[derive(Debug, Clone, serde::Serialize)]
129129
pub struct StableLlmRequest {
130130
stream: bool,
131-
#[serde(rename = "chatId")]
132-
#[serde(skip_serializing_if = "String::is_empty")]
133-
chat_id: String,
131+
#[serde(flatten, skip_serializing_if = "Option::is_none")]
132+
extra: Option<serde_json::Value>,
134133
messages: Vec<llm::Content>,
135134
#[serde(skip_serializing_if = "String::is_empty")]
136135
model: String,
@@ -140,6 +139,23 @@ pub struct StableLlmRequest {
140139
tool_choice: &'static str,
141140
}
142141

142+
#[test]
143+
fn test_stable_llm_request_json() {
144+
let request = StableLlmRequest {
145+
stream: true,
146+
extra: Some(serde_json::json!({
147+
"chat_id": "test-chat-id",
148+
})),
149+
messages: vec![],
150+
model: "test-model".to_string(),
151+
tools: vec![],
152+
tool_choice: "",
153+
};
154+
155+
let json_str = serde_json::to_string_pretty(&request).unwrap();
156+
println!("StableLlmRequest json: {}", json_str);
157+
}
158+
143159
pub enum StableLLMResponseChunk {
144160
Functions(Vec<llm::ToolCall>),
145161
Text(String),
@@ -417,7 +433,7 @@ pub async fn llm_stable<'p, I: IntoIterator<Item = C>, C: AsRef<llm::Content>>(
417433
llm_url: &str,
418434
token: &str,
419435
model: &str,
420-
chat_id: Option<String>,
436+
extra: Option<serde_json::Value>,
421437
prompts: I,
422438
tools: Vec<llm::Tool>,
423439
) -> anyhow::Result<StableLlmResponse> {
@@ -443,22 +459,22 @@ pub async fn llm_stable<'p, I: IntoIterator<Item = C>, C: AsRef<llm::Content>>(
443459
serde_json::to_string_pretty(&serde_json::json!(
444460
{
445461
"stream": true,
446-
"chat_id": chat_id,
447462
"messages": messages,
448463
"model": model.to_string(),
449464
"tools": tool_name,
465+
"extra": extra,
450466
"tool_choice": tool_choice,
451467
}
452468
))?
453469
);
454470

455471
let request = StableLlmRequest {
456472
stream: true,
457-
chat_id: chat_id.unwrap_or_default(),
458473
messages,
459474
model: model.to_string(),
460475
tools,
461476
tool_choice,
477+
extra,
462478
};
463479

464480
let response = response_builder
@@ -549,7 +565,7 @@ async fn test_stable_llm() {
549565
pub struct ChatSession {
550566
pub api_key: String,
551567
pub model: String,
552-
pub chat_id: Option<String>,
568+
pub extra: Option<serde_json::Value>,
553569
pub url: String,
554570

555571
pub history: usize,
@@ -564,15 +580,15 @@ impl ChatSession {
564580
url: String,
565581
api_key: String,
566582
model: String,
567-
chat_id: Option<String>,
583+
extra: Option<serde_json::Value>,
568584
history: usize,
569585
tools: ToolSet<McpToolAdapter>,
570586
) -> Self {
571587
Self {
572588
api_key,
573589
model,
574590
url,
575-
chat_id,
591+
extra,
576592
history,
577593
system_prompts: Vec::new(),
578594
messages: LinkedList::new(),
@@ -673,7 +689,7 @@ impl ChatSession {
673689
self.url.as_str(),
674690
&self.api_key,
675691
&self.model,
676-
self.chat_id.clone(),
692+
self.extra.clone(),
677693
prompts,
678694
tools,
679695
)

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub struct LLMConfig {
4242
pub history: usize,
4343
#[serde(default)]
4444
pub mcp_server: Vec<MCPServerConfig>,
45+
#[serde(default)]
46+
pub extra: Option<serde_json::Value>,
4547
}
4648

4749
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

src/services/ws/stable/llm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ async fn test_load_prompts() {
131131
llm_chat_url: "".to_string(),
132132
history: 5,
133133
mcp_server: vec![],
134+
extra: None,
134135
};
135136

136137
let now = tokio::time::Instant::now();

src/services/ws/stable/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ pub async fn run_session_manager(
302302
llm.llm_chat_url.clone(),
303303
llm.api_key.clone().unwrap_or_default(),
304304
llm.model.clone(),
305-
None,
305+
llm.extra.clone(),
306306
llm.history,
307307
tools.clone(),
308308
);

0 commit comments

Comments
 (0)