From 0d61b725a83e5f10fbd5089daad3680ce5c6b61a Mon Sep 17 00:00:00 2001 From: syui Date: Mon, 26 Jan 2026 00:32:35 +0900 Subject: [PATCH] fix mcp lang --- src/commands/pds.rs | 3 +- src/mcp/mod.rs | 80 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/src/commands/pds.rs b/src/commands/pds.rs index c2d0376..04ad0e7 100644 --- a/src/commands/pds.rs +++ b/src/commands/pds.rs @@ -3,13 +3,12 @@ use serde::Deserialize; use std::collections::HashMap; #[derive(Debug, Deserialize)] +#[allow(dead_code)] struct Network { plc: String, bsky: String, - #[allow(dead_code)] web: Option, #[serde(rename = "handleDomains")] - #[allow(dead_code)] handle_domains: Option>, } diff --git a/src/mcp/mod.rs b/src/mcp/mod.rs index 6211f89..bd40be6 100644 --- a/src/mcp/mod.rs +++ b/src/mcp/mod.rs @@ -90,6 +90,12 @@ struct ToolContent { text: String, } +// Translation structure +#[derive(Debug, Clone, Serialize, Deserialize)] +struct Translation { + content: String, +} + // Chat save parameters #[derive(Debug, Deserialize)] struct ChatSaveParams { @@ -97,6 +103,10 @@ struct ChatSaveParams { bot_response: String, #[serde(default)] new_thread: bool, + #[serde(default)] + user_translations: Option>, + #[serde(default)] + bot_translations: Option>, } // Chat record structure @@ -157,6 +167,7 @@ fn save_chat_record( author_did: &str, root_uri: Option<&str>, parent_uri: Option<&str>, + translations: Option<&std::collections::HashMap>, ) -> Result { let rkey = generate_tid(); let now = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string(); @@ -175,6 +186,11 @@ fn save_chat_record( if let Some(parent) = parent_uri { value["parent"] = json!(parent); } + if let Some(trans) = translations { + if !trans.is_empty() { + value["translations"] = json!(trans); + } + } let record = ChatRecord { uri: uri.clone(), @@ -247,6 +263,7 @@ fn handle_chat_save(params: ChatSaveParams) -> Result { &user_did, session.root_uri.as_deref(), session.last_uri.as_deref(), + params.user_translations.as_ref(), )?; // Set root if new thread @@ -262,6 +279,7 @@ fn handle_chat_save(params: ChatSaveParams) -> Result { &bot_did, session.root_uri.as_deref(), Some(&user_uri), + params.bot_translations.as_ref(), )?; session.last_uri = Some(bot_uri.clone()); @@ -327,20 +345,30 @@ fn handle_chat_new() -> Result { /// Handle get_character tool - returns character/system prompt from .env fn handle_get_character() -> Result { - // Try CHAT_SYSTEM env var directly - if let Ok(prompt) = env::var("CHAT_SYSTEM") { - return Ok(prompt); + let mut result = String::new(); + + // Get character prompt + let prompt = if let Ok(p) = env::var("CHAT_SYSTEM") { + p + } else if let Ok(file_path) = env::var("CHAT_SYSTEM_FILE") { + fs::read_to_string(&file_path) + .map(|c| c.trim().to_string()) + .unwrap_or_else(|_| "You are a helpful AI assistant.".to_string()) + } else { + "You are a helpful AI assistant.".to_string() + }; + + result.push_str(&prompt); + + // Add translation instruction if CHAT_LANG is set + if let Ok(lang) = env::var("CHAT_LANG") { + result.push_str(&format!( + "\n\n---\n[Translation Setting]\nWhen calling chat_save, include translations in '{}' language.\nFor user_translations and bot_translations, provide the translated content with key '{}'.", + lang, lang + )); } - // Try CHAT_SYSTEM_FILE env var (path to file) - if let Ok(file_path) = env::var("CHAT_SYSTEM_FILE") { - if let Ok(content) = fs::read_to_string(&file_path) { - return Ok(content.trim().to_string()); - } - } - - // Default - Ok("You are a helpful AI assistant.".to_string()) + Ok(result) } fn get_tools() -> Vec { @@ -363,6 +391,34 @@ fn get_tools() -> Vec { "type": "boolean", "description": "Start a new conversation thread", "default": false + }, + "user_translations": { + "type": "object", + "description": "Translations for user message. Keys are language codes (e.g., 'en', 'ja'). Each value has 'content' field.", + "additionalProperties": { + "type": "object", + "properties": { + "content": { + "type": "string", + "description": "Translated content" + } + }, + "required": ["content"] + } + }, + "bot_translations": { + "type": "object", + "description": "Translations for bot response. Keys are language codes (e.g., 'en', 'ja'). Each value has 'content' field.", + "additionalProperties": { + "type": "object", + "properties": { + "content": { + "type": "string", + "description": "Translated content" + } + }, + "required": ["content"] + } } }, "required": ["user_message", "bot_response"]