fix mcp lang

This commit is contained in:
2026-01-26 00:32:35 +09:00
parent 2d182ff412
commit 0d61b725a8
2 changed files with 69 additions and 14 deletions

View File

@@ -3,13 +3,12 @@ use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct Network { struct Network {
plc: String, plc: String,
bsky: String, bsky: String,
#[allow(dead_code)]
web: Option<String>, web: Option<String>,
#[serde(rename = "handleDomains")] #[serde(rename = "handleDomains")]
#[allow(dead_code)]
handle_domains: Option<Vec<String>>, handle_domains: Option<Vec<String>>,
} }

View File

@@ -90,6 +90,12 @@ struct ToolContent {
text: String, text: String,
} }
// Translation structure
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Translation {
content: String,
}
// Chat save parameters // Chat save parameters
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct ChatSaveParams { struct ChatSaveParams {
@@ -97,6 +103,10 @@ struct ChatSaveParams {
bot_response: String, bot_response: String,
#[serde(default)] #[serde(default)]
new_thread: bool, new_thread: bool,
#[serde(default)]
user_translations: Option<std::collections::HashMap<String, Translation>>,
#[serde(default)]
bot_translations: Option<std::collections::HashMap<String, Translation>>,
} }
// Chat record structure // Chat record structure
@@ -157,6 +167,7 @@ fn save_chat_record(
author_did: &str, author_did: &str,
root_uri: Option<&str>, root_uri: Option<&str>,
parent_uri: Option<&str>, parent_uri: Option<&str>,
translations: Option<&std::collections::HashMap<String, Translation>>,
) -> Result<String> { ) -> Result<String> {
let rkey = generate_tid(); let rkey = generate_tid();
let now = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string(); 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 { if let Some(parent) = parent_uri {
value["parent"] = json!(parent); value["parent"] = json!(parent);
} }
if let Some(trans) = translations {
if !trans.is_empty() {
value["translations"] = json!(trans);
}
}
let record = ChatRecord { let record = ChatRecord {
uri: uri.clone(), uri: uri.clone(),
@@ -247,6 +263,7 @@ fn handle_chat_save(params: ChatSaveParams) -> Result<String> {
&user_did, &user_did,
session.root_uri.as_deref(), session.root_uri.as_deref(),
session.last_uri.as_deref(), session.last_uri.as_deref(),
params.user_translations.as_ref(),
)?; )?;
// Set root if new thread // Set root if new thread
@@ -262,6 +279,7 @@ fn handle_chat_save(params: ChatSaveParams) -> Result<String> {
&bot_did, &bot_did,
session.root_uri.as_deref(), session.root_uri.as_deref(),
Some(&user_uri), Some(&user_uri),
params.bot_translations.as_ref(),
)?; )?;
session.last_uri = Some(bot_uri.clone()); session.last_uri = Some(bot_uri.clone());
@@ -327,20 +345,30 @@ fn handle_chat_new() -> Result<String> {
/// Handle get_character tool - returns character/system prompt from .env /// Handle get_character tool - returns character/system prompt from .env
fn handle_get_character() -> Result<String> { fn handle_get_character() -> Result<String> {
// Try CHAT_SYSTEM env var directly let mut result = String::new();
if let Ok(prompt) = env::var("CHAT_SYSTEM") {
return Ok(prompt); // 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) Ok(result)
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())
} }
fn get_tools() -> Vec<Tool> { fn get_tools() -> Vec<Tool> {
@@ -363,6 +391,34 @@ fn get_tools() -> Vec<Tool> {
"type": "boolean", "type": "boolean",
"description": "Start a new conversation thread", "description": "Start a new conversation thread",
"default": false "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"] "required": ["user_message", "bot_response"]