This commit is contained in:
@ -28,6 +28,7 @@ Only return the enhanced content without explanations.";
|
||||
self.client.chat(system_prompt, &user_prompt).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn suggest_improvements(&self, content: &str) -> Result<Vec<String>> {
|
||||
let system_prompt = "You are a content analyzer. Analyze the given content and provide:
|
||||
1. Suggestions for improving the content
|
||||
|
@ -21,6 +21,7 @@ impl<'a> Translator<'a> {
|
||||
self.client.chat(&system_prompt, content).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn translate_post(&self, title: &str, content: &str, from: &str, to: &str) -> Result<(String, String)> {
|
||||
// Translate title
|
||||
let translated_title = self.translate(title, from, to).await?;
|
||||
|
@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub struct AtprotoClient {
|
||||
client: reqwest::Client,
|
||||
handle_resolver: String,
|
||||
@ -27,8 +28,10 @@ pub struct CommentRecord {
|
||||
#[serde(rename = "$type")]
|
||||
pub record_type: String,
|
||||
pub text: String,
|
||||
pub createdAt: String,
|
||||
pub postUri: String,
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: String,
|
||||
#[serde(rename = "postUri")]
|
||||
pub post_uri: String,
|
||||
pub author: AuthorInfo,
|
||||
}
|
||||
|
||||
@ -38,6 +41,7 @@ pub struct AuthorInfo {
|
||||
pub handle: String,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl AtprotoClient {
|
||||
pub fn new(handle_resolver: String) -> Self {
|
||||
Self {
|
||||
@ -59,8 +63,8 @@ impl AtprotoClient {
|
||||
let record = CommentRecord {
|
||||
record_type: "app.bsky.feed.post".to_string(),
|
||||
text: text.to_string(),
|
||||
createdAt: chrono::Utc::now().to_rfc3339(),
|
||||
postUri: post_uri.to_string(),
|
||||
created_at: chrono::Utc::now().to_rfc3339(),
|
||||
post_uri: post_uri.to_string(),
|
||||
author: AuthorInfo {
|
||||
did: did.to_string(),
|
||||
handle: "".to_string(), // Will be resolved by the server
|
||||
|
@ -20,11 +20,13 @@ pub struct CommentStorage {
|
||||
pub comments: Vec<Comment>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct CommentSync {
|
||||
client: AtprotoClient,
|
||||
storage_path: PathBuf,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl CommentSync {
|
||||
pub fn new(client: AtprotoClient, base_path: PathBuf) -> Self {
|
||||
let storage_path = base_path.join("data/comments.json");
|
||||
@ -91,6 +93,7 @@ impl CommentSync {
|
||||
}
|
||||
|
||||
// Helper to generate comment HTML
|
||||
#[allow(dead_code)]
|
||||
pub fn render_comments_html(comments: &[Comment]) -> String {
|
||||
let mut html = String::from("<div class=\"comments\">\n");
|
||||
html.push_str(" <h3>コメント</h3>\n");
|
||||
|
@ -1,7 +1,3 @@
|
||||
pub mod oauth;
|
||||
pub mod client;
|
||||
pub mod comment_sync;
|
||||
|
||||
pub use oauth::OAuthHandler;
|
||||
pub use client::AtprotoClient;
|
||||
pub use comment_sync::CommentSync;
|
||||
pub mod comment_sync;
|
@ -21,6 +21,7 @@ pub struct ClientMetadata {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub struct OAuthHandler {
|
||||
config: AtprotoConfig,
|
||||
client: reqwest::Client,
|
||||
@ -46,6 +47,7 @@ pub struct TokenResponse {
|
||||
pub scope: String,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl OAuthHandler {
|
||||
pub fn new(config: AtprotoConfig) -> Self {
|
||||
Self {
|
||||
@ -137,6 +139,7 @@ impl OAuthHandler {
|
||||
}
|
||||
|
||||
// PKCE helpers
|
||||
#[allow(dead_code)]
|
||||
pub fn generate_code_verifier() -> String {
|
||||
use rand::Rng;
|
||||
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
||||
@ -150,6 +153,7 @@ pub fn generate_code_verifier() -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn generate_code_challenge(verifier: &str) -> String {
|
||||
use sha2::{Sha256, Digest};
|
||||
use base64::{Engine as _, engine::general_purpose};
|
||||
|
@ -8,6 +8,7 @@ use std::path::PathBuf;
|
||||
pub struct AuthConfig {
|
||||
pub admin: AdminConfig,
|
||||
pub jetstream: JetstreamConfig,
|
||||
pub collections: CollectionConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@ -25,6 +26,12 @@ pub struct JetstreamConfig {
|
||||
pub collections: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CollectionConfig {
|
||||
pub comment: String,
|
||||
pub user: String,
|
||||
}
|
||||
|
||||
impl Default for AuthConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@ -39,6 +46,10 @@ impl Default for AuthConfig {
|
||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
||||
collections: vec!["ai.syui.log".to_string()],
|
||||
},
|
||||
collections: CollectionConfig {
|
||||
comment: "ai.syui.log".to_string(),
|
||||
user: "ai.syui.log.user".to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -105,6 +116,7 @@ pub async fn init() -> Result<()> {
|
||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
||||
collections: vec!["ai.syui.log".to_string()],
|
||||
},
|
||||
collections: generate_collection_config(),
|
||||
};
|
||||
|
||||
// Save config
|
||||
@ -208,7 +220,10 @@ pub fn load_config() -> Result<AuthConfig> {
|
||||
}
|
||||
|
||||
let config_json = fs::read_to_string(&config_path)?;
|
||||
let config: AuthConfig = serde_json::from_str(&config_json)?;
|
||||
let mut config: AuthConfig = serde_json::from_str(&config_json)?;
|
||||
|
||||
// Update collection configuration
|
||||
update_config_collections(&mut config);
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
@ -233,14 +248,18 @@ pub async fn load_config_with_refresh() -> Result<AuthConfig> {
|
||||
}
|
||||
}
|
||||
|
||||
// Update collection configuration
|
||||
update_config_collections(&mut config);
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn test_api_access_with_auth(config: &AuthConfig) -> Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log&limit=1",
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=1",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
urlencoding::encode(&config.admin.did),
|
||||
urlencoding::encode(&config.collections.comment));
|
||||
|
||||
let response = client
|
||||
.get(&url)
|
||||
@ -290,4 +309,31 @@ fn save_config(config: &AuthConfig) -> Result<()> {
|
||||
let config_json = serde_json::to_string_pretty(config)?;
|
||||
fs::write(&config_path, config_json)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Generate collection names from admin DID or environment
|
||||
fn generate_collection_config() -> CollectionConfig {
|
||||
// Check environment variables first
|
||||
if let (Ok(comment), Ok(user)) = (
|
||||
std::env::var("AILOG_COLLECTION_COMMENT"),
|
||||
std::env::var("AILOG_COLLECTION_USER")
|
||||
) {
|
||||
return CollectionConfig {
|
||||
comment,
|
||||
user,
|
||||
};
|
||||
}
|
||||
|
||||
// Default collections
|
||||
CollectionConfig {
|
||||
comment: "ai.syui.log".to_string(),
|
||||
user: "ai.syui.log.user".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
// Update existing config with collection settings
|
||||
pub fn update_config_collections(config: &mut AuthConfig) {
|
||||
config.collections = generate_collection_config();
|
||||
// Also update jetstream collections to monitor the comment collection
|
||||
config.jetstream.collections = vec![config.collections.comment.clone()];
|
||||
}
|
@ -267,7 +267,7 @@ async fn handle_message(text: &str, config: &mut AuthConfig) -> Result<()> {
|
||||
if let (Some(collection), Some(commit), Some(did)) =
|
||||
(&message.collection, &message.commit, &message.did) {
|
||||
|
||||
if collection == "ai.syui.log" && commit.operation.as_deref() == Some("create") {
|
||||
if collection == &config.collections.comment && commit.operation.as_deref() == Some("create") {
|
||||
let unknown_uri = "unknown".to_string();
|
||||
let uri = commit.uri.as_ref().unwrap_or(&unknown_uri);
|
||||
|
||||
@ -358,9 +358,10 @@ async fn update_user_list(config: &mut AuthConfig, did: &str, handle: &str) -> R
|
||||
|
||||
async fn get_current_user_list(config: &mut AuthConfig) -> Result<Vec<UserRecord>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log.user&limit=10",
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=10",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
urlencoding::encode(&config.admin.did),
|
||||
urlencoding::encode(&config.collections.user));
|
||||
|
||||
let response = client
|
||||
.get(&url)
|
||||
@ -416,10 +417,14 @@ async fn post_user_list(config: &mut AuthConfig, users: &[UserRecord], metadata:
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let rkey = now.format("%Y-%m-%dT%H-%M-%S-%3fZ").to_string().replace(".", "-");
|
||||
// Extract short ID from DID (did:plc:xxx -> xxx) for rkey
|
||||
let short_did = config.admin.did
|
||||
.strip_prefix("did:plc:")
|
||||
.unwrap_or(&config.admin.did);
|
||||
let rkey = format!("{}-{}", short_did, now.format("%Y-%m-%dT%H-%M-%S-%3fZ").to_string().replace(".", "-"));
|
||||
|
||||
let record = UserListRecord {
|
||||
record_type: "ai.syui.log.user".to_string(),
|
||||
record_type: config.collections.user.clone(),
|
||||
users: users.to_vec(),
|
||||
created_at: now.to_rfc3339(),
|
||||
updated_by: UserInfo {
|
||||
@ -433,7 +438,7 @@ async fn post_user_list(config: &mut AuthConfig, users: &[UserRecord], metadata:
|
||||
|
||||
let request_body = json!({
|
||||
"repo": config.admin.did,
|
||||
"collection": "ai.syui.log.user",
|
||||
"collection": config.collections.user,
|
||||
"rkey": rkey,
|
||||
"record": record
|
||||
});
|
||||
@ -674,9 +679,10 @@ async fn poll_comments_periodically(mut config: AuthConfig) -> Result<()> {
|
||||
|
||||
async fn get_recent_comments(config: &mut AuthConfig) -> Result<Vec<Value>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log&limit=20",
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=20",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
urlencoding::encode(&config.admin.did),
|
||||
urlencoding::encode(&config.collections.comment));
|
||||
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", format!("🌐 API Request URL: {}", url).yellow());
|
||||
@ -757,7 +763,7 @@ pub async fn test_api() -> Result<()> {
|
||||
println!("{}", format!("✅ Successfully retrieved {} comments", comments.len()).green());
|
||||
|
||||
if comments.is_empty() {
|
||||
println!("{}", "ℹ️ No comments found in ai.syui.log collection".blue());
|
||||
println!("{}", format!("ℹ️ No comments found in {} collection", config.collections.comment).blue());
|
||||
println!("💡 Try posting a comment first using the web interface");
|
||||
} else {
|
||||
println!("{}", "📝 Comment details:".cyan());
|
||||
|
@ -114,6 +114,7 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn save_global(&self) -> Result<()> {
|
||||
let config_dir = Self::global_config_dir();
|
||||
fs::create_dir_all(&config_dir)?;
|
||||
|
@ -3,6 +3,7 @@ use std::path::PathBuf;
|
||||
use crate::analyzer::{ProjectInfo, ApiInfo, ProjectStructure};
|
||||
use crate::ai::gpt_client::GptClient;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct DocGenerator {
|
||||
base_path: PathBuf,
|
||||
ai_enabled: bool,
|
||||
@ -201,6 +202,7 @@ impl DocGenerator {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct GitCommit {
|
||||
pub hash: String,
|
||||
pub message: String,
|
||||
|
@ -2,5 +2,4 @@ pub mod server;
|
||||
pub mod tools;
|
||||
pub mod types;
|
||||
|
||||
pub use server::McpServer;
|
||||
pub use types::*;
|
||||
pub use server::McpServer;
|
@ -1,18 +1,17 @@
|
||||
use anyhow::Result;
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
use serde_json::{json, Value};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tower_http::cors::CorsLayer;
|
||||
use crate::mcp::types::*;
|
||||
use crate::mcp::tools::BlogTools;
|
||||
use crate::mcp::types::{McpRequest, McpResponse, McpError, CreatePostRequest, ListPostsRequest, BuildRequest};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
|
@ -1,5 +1,4 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct McpRequest {
|
||||
|
16
src/oauth.rs
16
src/oauth.rs
@ -1,11 +1,10 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tower_sessions::Session;
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
response::{Html, Redirect},
|
||||
extract::Query,
|
||||
response::Html,
|
||||
Json,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use jsonwebtoken::{encode, decode, Header, Algorithm, Validation, EncodingKey, DecodingKey};
|
||||
use anyhow::Result;
|
||||
|
||||
@ -38,8 +37,9 @@ pub struct Claims {
|
||||
pub iat: usize,
|
||||
}
|
||||
|
||||
const JWT_SECRET: &[u8] = b"ailog-oauth-secret-key-2025";
|
||||
const _JWT_SECRET: &[u8] = b"ailog-oauth-secret-key-2025";
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn create_jwt(oauth_data: &OAuthData) -> Result<String> {
|
||||
let now = chrono::Utc::now().timestamp() as usize;
|
||||
let claims = Claims {
|
||||
@ -54,22 +54,24 @@ pub fn create_jwt(oauth_data: &OAuthData) -> Result<String> {
|
||||
let token = encode(
|
||||
&Header::default(),
|
||||
&claims,
|
||||
&EncodingKey::from_secret(JWT_SECRET),
|
||||
&EncodingKey::from_secret(_JWT_SECRET),
|
||||
)?;
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn verify_jwt(token: &str) -> Result<Claims> {
|
||||
let token_data = decode::<Claims>(
|
||||
token,
|
||||
&DecodingKey::from_secret(JWT_SECRET),
|
||||
&DecodingKey::from_secret(_JWT_SECRET),
|
||||
&Validation::new(Algorithm::HS256),
|
||||
)?;
|
||||
|
||||
Ok(token_data.claims)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn oauth_callback_handler(
|
||||
Query(params): Query<OAuthCallback>,
|
||||
session: Session,
|
||||
@ -179,6 +181,7 @@ pub async fn oauth_callback_handler(
|
||||
Err("No authorization code received".to_string())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn oauth_session_handler(session: Session) -> Json<serde_json::Value> {
|
||||
if let Ok(Some(oauth_data)) = session.get::<OAuthData>("oauth_data").await {
|
||||
if let Ok(Some(jwt_token)) = session.get::<String>("jwt_token").await {
|
||||
@ -195,6 +198,7 @@ pub async fn oauth_session_handler(session: Session) -> Json<serde_json::Value>
|
||||
}))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn oauth_logout_handler(session: Session) -> Json<serde_json::Value> {
|
||||
let _ = session.remove::<OAuthData>("oauth_data").await;
|
||||
let _ = session.remove::<String>("jwt_token").await;
|
||||
|
@ -16,6 +16,7 @@ impl TemplateEngine {
|
||||
Ok(Self { tera })
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn create_context(&self, config: &Config, posts: &[Post]) -> Result<Context> {
|
||||
let mut context = Context::new();
|
||||
context.insert("config", &config.site);
|
||||
|
@ -3,7 +3,7 @@ use regex::Regex;
|
||||
use super::MarkdownSection;
|
||||
|
||||
pub struct MarkdownParser {
|
||||
code_block_regex: Regex,
|
||||
_code_block_regex: Regex,
|
||||
header_regex: Regex,
|
||||
link_regex: Regex,
|
||||
image_regex: Regex,
|
||||
@ -15,7 +15,7 @@ pub struct MarkdownParser {
|
||||
impl MarkdownParser {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
code_block_regex: Regex::new(r"```([a-zA-Z0-9]*)\n([\s\S]*?)\n```").unwrap(),
|
||||
_code_block_regex: Regex::new(r"```([a-zA-Z0-9]*)\n([\s\S]*?)\n```").unwrap(),
|
||||
header_regex: Regex::new(r"^(#{1,6})\s+(.+)$").unwrap(),
|
||||
link_regex: Regex::new(r"\[([^\]]+)\]\(([^)]+)\)").unwrap(),
|
||||
image_regex: Regex::new(r"!\[([^\]]*)\]\(([^)]+)\)").unwrap(),
|
||||
|
@ -41,11 +41,13 @@ pub enum MarkdownSection {
|
||||
}
|
||||
|
||||
pub trait Translator {
|
||||
#[allow(dead_code)]
|
||||
async fn translate(&self, content: &str, config: &TranslationConfig) -> Result<String>;
|
||||
async fn translate_markdown(&self, content: &str, config: &TranslationConfig) -> Result<String>;
|
||||
async fn translate_sections(&self, sections: Vec<MarkdownSection>, config: &TranslationConfig) -> Result<Vec<MarkdownSection>>;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct TranslationResult {
|
||||
pub original: String,
|
||||
pub translated: String,
|
||||
@ -56,6 +58,7 @@ pub struct TranslationResult {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[allow(dead_code)]
|
||||
pub struct TranslationMetrics {
|
||||
pub character_count: usize,
|
||||
pub word_count: usize,
|
||||
@ -117,6 +120,7 @@ impl LanguageMapping {
|
||||
self.mappings.get(code)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_supported_languages(&self) -> Vec<String> {
|
||||
self.mappings.keys().cloned().collect()
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ impl Translator for OllamaTranslator {
|
||||
println!(" 🔤 Processing section {}", index + 1);
|
||||
|
||||
let translated_section = match §ion {
|
||||
MarkdownSection::Code(content, lang) => {
|
||||
MarkdownSection::Code(_content, _lang) => {
|
||||
if config.preserve_code {
|
||||
println!(" ⏭️ Preserving code block");
|
||||
section // Preserve code blocks
|
||||
@ -174,7 +174,7 @@ impl Translator for OllamaTranslator {
|
||||
MarkdownSection::Link(translated_text.trim().to_string(), url.clone())
|
||||
}
|
||||
}
|
||||
MarkdownSection::Image(alt, url) => {
|
||||
MarkdownSection::Image(_alt, _url) => {
|
||||
println!(" 🖼️ Preserving image");
|
||||
section // Preserve images
|
||||
}
|
||||
|
Reference in New Issue
Block a user