1 Commits

Author SHA1 Message Date
110674659b cleanup 2025-06-09 02:33:06 +09:00
70 changed files with 9562 additions and 8613 deletions

View File

@@ -0,0 +1,58 @@
{
"permissions": {
"allow": [
"Bash(mv:*)",
"Bash(mkdir:*)",
"Bash(chmod:*)",
"Bash(git submodule:*)",
"Bash(source:*)",
"Bash(pip install:*)",
"Bash(/Users/syui/.config/syui/ai/gpt/venv/bin/aigpt shell)",
"Bash(/Users/syui/.config/syui/ai/gpt/venv/bin/aigpt server --model qwen2.5-coder:7b --port 8001)",
"Bash(/Users/syui/.config/syui/ai/gpt/venv/bin/python -c \"import fastapi_mcp; help(fastapi_mcp.FastApiMCP)\")",
"Bash(find:*)",
"Bash(/Users/syui/.config/syui/ai/gpt/venv/bin/pip install -e .)",
"Bash(/Users/syui/.config/syui/ai/gpt/venv/bin/aigpt fortune)",
"Bash(lsof:*)",
"Bash(/Users/syui/.config/syui/ai/gpt/venv/bin/python -c \"\nfrom src.aigpt.mcp_server import AIGptMcpServer\nfrom pathlib import Path\nimport uvicorn\n\ndata_dir = Path.home() / '.config' / 'syui' / 'ai' / 'gpt' / 'data'\ndata_dir.mkdir(parents=True, exist_ok=True)\n\ntry:\n server = AIGptMcpServer(data_dir)\n print('MCP Server created successfully')\n print('Available endpoints:', [route.path for route in server.app.routes])\nexcept Exception as e:\n print('Error:', e)\n import traceback\n traceback.print_exc()\n\")",
"Bash(ls:*)",
"Bash(grep:*)",
"Bash(python -m pip install:*)",
"Bash(python:*)",
"Bash(RELOAD=false ./start_server.sh)",
"Bash(sed:*)",
"Bash(curl:*)",
"Bash(~/.config/syui/ai/card/venv/bin/pip install greenlet)",
"Bash(~/.config/syui/ai/card/venv/bin/python init_db.py)",
"Bash(sqlite3:*)",
"Bash(aigpt --help)",
"Bash(aigpt status)",
"Bash(aigpt fortune)",
"Bash(aigpt relationships)",
"Bash(aigpt transmit)",
"Bash(aigpt config:*)",
"Bash(kill:*)",
"Bash(timeout:*)",
"Bash(rm:*)",
"Bash(rg:*)",
"Bash(aigpt server --help)",
"Bash(cat:*)",
"Bash(aigpt import-chatgpt:*)",
"Bash(aigpt chat:*)",
"Bash(echo:*)",
"Bash(aigpt shell:*)",
"Bash(aigpt maintenance)",
"Bash(aigpt status syui)",
"Bash(cp:*)",
"Bash(./setup_venv.sh:*)",
"WebFetch(domain:docs.anthropic.com)",
"Bash(launchctl:*)",
"Bash(sudo lsof:*)",
"Bash(sudo:*)",
"Bash(cargo check:*)",
"Bash(cargo run:*)",
"Bash(cargo test:*)"
],
"deny": []
}
}

5
.env.example Normal file
View File

@@ -0,0 +1,5 @@
# OpenAI API Key (required for OpenAI provider)
OPENAI_API_KEY=your-api-key-here
# Ollama settings (optional)
OLLAMA_HOST=http://localhost:11434

32
.gitignore vendored
View File

@@ -1,24 +1,8 @@
# Rust
target/
Cargo.lock
# Database files
*.db
*.db-shm
*.db-wal
# IDE
.idea/
.vscode/
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Logs
*.log
json
gpt
.claude
**target
**.lock
output.json
config/*.db
mcp/scripts/__*
data
__pycache__
conversations.json

10
.gitmodules vendored Normal file
View File

@@ -0,0 +1,10 @@
[submodule "shell"]
path = shell
url = git@git.syui.ai:ai/shell
[submodule "card"]
path = card
url = git@git.syui.ai:ai/card
branch = claude
[submodule "log"]
path = log
url = git@git.syui.ai:ai/log

View File

@@ -1,37 +1,37 @@
[package]
name = "aigpt"
version = "0.3.0"
version = "0.1.0"
edition = "2021"
description = "AI.GPT - Autonomous transmission AI with unique personality (Rust implementation)"
authors = ["syui"]
description = "AI memory system with personality analysis and relationship inference - Layers 1-4 Complete"
[lib]
name = "aigpt"
path = "src/lib.rs"
[[bin]]
name = "aigpt"
path = "src/main.rs"
[[bin]]
name = "test-config"
path = "src/bin/test_config.rs"
[dependencies]
# CLI and async
clap = { version = "4.5", features = ["derive"] }
tokio = { version = "1.40", features = ["rt", "rt-multi-thread", "macros", "io-std"] }
# Database
rusqlite = { version = "0.30", features = ["bundled"] }
# Serialization
clap = { version = "4.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
# Date/time and ULID
chrono = { version = "0.4", features = ["serde"] }
ulid = "1.1"
# Error handling
thiserror = "1.0"
tokio = { version = "1.0", features = ["full"] }
chrono = { version = "0.4", features = ["serde", "std"] }
chrono-tz = "0.8"
uuid = { version = "1.0", features = ["v4"] }
anyhow = "1.0"
# Utilities
colored = "2.0"
dirs = "5.0"
reqwest = { version = "0.11", features = ["json"] }
url = "2.4"
rustyline = "14.0"
axum = "0.7"
tower = "0.4"
tower-http = { version = "0.5", features = ["cors"] }
hyper = "1.0"
# OpenAI API client
async-openai = "0.23"
openai_api_rust = "0.1"

115
DEVELOPMENT.md Normal file
View File

@@ -0,0 +1,115 @@
# ai.gpt プロジェクト固有情報
## プロジェクト概要
- **名前**: ai.gpt
- **パッケージ**: aigpt
- **タイプ**: 自律的送信AI + 統合MCP基盤
- **役割**: 記憶・関係性・開発支援の統合AIシステム
## 実装完了状況
### 🧠 記憶システムMemoryManager
- **階層的記憶**: 完全ログ→AI要約→コア記憶→選択的忘却
- **文脈検索**: キーワード・意味的検索
- **記憶要約**: AI駆動自動要約機能
### 🤝 関係性システムRelationshipTracker
- **不可逆性**: 現実の人間関係と同じ重み
- **時間減衰**: 自然な関係性変化
- **送信判定**: 関係性閾値による自発的コミュニケーション
### 🎭 人格システムPersona
- **AI運勢**: 1-10ランダム値による日々の人格変動
- **統合管理**: 記憶・関係性・運勢の統合判断
- **継続性**: 長期記憶による人格継承
### 💻 ai.shell統合Claude Code機能
- **インタラクティブ環境**: `aigpt shell`
- **開発支援**: ファイル分析・コード生成・プロジェクト管理
- **継続開発**: プロジェクト文脈保持
## MCP Server統合23ツール
### 🧠 Memory System5ツール
- get_memories, get_contextual_memories, search_memories
- create_summary, create_core_memory
### 🤝 Relationships4ツール
- get_relationship, get_all_relationships
- process_interaction, check_transmission_eligibility
### 💻 Shell Integration5ツール
- execute_command, analyze_file, write_file
- read_project_file, list_files
### 🔒 Remote Execution4ツール
- remote_shell, ai_bot_status
- isolated_python, isolated_analysis
### ⚙️ System State3ツール
- get_persona_state, get_fortune, run_maintenance
### 🎴 ai.card連携6ツール + 独立MCPサーバー
- card_draw_card, card_get_user_cards, card_analyze_collection
- **独立サーバー**: FastAPI + MCP (port 8000)
### 📝 ai.log連携8ツール + Rustサーバー
- log_create_post, log_ai_content, log_translate_document
- **独立サーバー**: Rust製 (port 8002)
## 開発環境・設定
### 環境構築
```bash
cd /Users/syui/ai/gpt
./setup_venv.sh
source ~/.config/syui/ai/gpt/venv/bin/activate
```
### 設定管理
- **メイン設定**: `/Users/syui/ai/gpt/config.json`
- **データディレクトリ**: `~/.config/syui/ai/gpt/`
- **仮想環境**: `~/.config/syui/ai/gpt/venv/`
### 使用方法
```bash
# ai.shell起動
aigpt shell --model qwen2.5-coder:latest --provider ollama
# MCPサーバー起動
aigpt server --port 8001
# 記憶システム体験
aigpt chat syui "質問内容" --provider ollama --model qwen3:latest
```
## 技術アーキテクチャ
### 統合構成
```
ai.gpt (統合MCPサーバー:8001)
├── 🧠 ai.gpt core (記憶・関係性・人格)
├── 💻 ai.shell (Claude Code風開発環境)
├── 🎴 ai.card (独立MCPサーバー:8000)
└── 📝 ai.log (Rust製ブログシステム:8002)
```
### 今後の展開
- **自律送信**: atproto実装による真の自発的コミュニケーション
- **ai.ai連携**: 心理分析AIとの統合
- **ai.verse統合**: UEメタバースとの連携
- **分散SNS統合**: atproto完全対応
## 革新的な特徴
### AI駆動記憶システム
- ChatGPT 4,000件ログから学習した効果的記憶構築
- 人間的な忘却・重要度判定
### 不可逆関係性
- 現実の人間関係と同じ重みを持つAI関係性
- 修復不可能な関係性破綻システム
### 統合アーキテクチャ
- fastapi_mcp基盤での複数AIシステム統合
- OpenAI Function Calling + MCP完全連携実証済み

329
README.md
View File

@@ -1,274 +1,115 @@
# aigpt
# ai.gpt プロジェクト固有情報
AI memory system with psychological analysis for Claude via MCP.
## プロジェクト概要
- **名前**: ai.gpt
- **パッケージ**: aigpt
- **タイプ**: 自律的送信AI + 統合MCP基盤
- **役割**: 記憶・関係性・開発支援の統合AIシステム
**Current: Layers 1-4 Complete** - Memory storage, AI interpretation, personality analysis, integrated profile, and relationship inference.
## 実装完了状況
**Planned: Layer 5** - Knowledge sharing platform combining useful insights with author personality.
### 🧠 記憶システムMemoryManager
- **階層的記憶**: 完全ログ→AI要約→コア記憶→選択的忘却
- **文脈検索**: キーワード・意味的検索
- **記憶要約**: AI駆動自動要約機能
## Features
### 🤝 関係性システムRelationshipTracker
- **不可逆性**: 現実の人間関係と同じ重み
- **時間減衰**: 自然な関係性変化
- **送信判定**: 関係性閾値による自発的コミュニケーション
### Layer 1: Pure Memory Storage
- 🗄️ **SQLite Storage**: Reliable database with ACID guarantees
- 🔖 **ULID IDs**: Time-sortable, 26-character unique identifiers
- 🔍 **Search**: Fast content-based search
- 📝 **CRUD Operations**: Complete memory management
### 🎭 人格システムPersona
- **AI運勢**: 1-10ランダム値による日々の人格変動
- **統合管理**: 記憶・関係性・運勢の統合判断
- **継続性**: 長期記憶による人格継承
### Layer 2: AI Memory
- 🧠 **AI Interpretation**: Claude interprets and evaluates memories
- 📊 **Priority Scoring**: Importance ratings (0.0-1.0)
- 🎯 **Smart Storage**: Memory + evaluation in one step
### 💻 ai.shell統合Claude Code機能
- **インタラクティブ環境**: `aigpt shell`
- **開発支援**: ファイル分析・コード生成・プロジェクト管理
- **継続開発**: プロジェクト文脈保持
### Layer 3: Personality Analysis
- 🔬 **Big Five Model**: Scientifically validated personality assessment
- 📈 **Pattern Recognition**: Analyzes memory patterns to build user profile
- 💾 **Historical Tracking**: Save and compare analyses over time
## MCP Server統合23ツール
### Layer 3.5: Integrated Profile
- 🎯 **Essential Summary**: Unified view of personality, interests, and values
- 🤖 **AI-Optimized**: Primary tool for AI to understand the user
-**Smart Caching**: Auto-updates only when necessary
- 🔍 **Flexible Access**: Detailed data still accessible when needed
### 🧠 Memory System5ツール
- get_memories, get_contextual_memories, search_memories
- create_summary, create_core_memory
### Layer 4: Relationship Inference (Optional)
- 🤝 **Relationship Tracking**: Track interactions with entities (people, characters, etc.)
- 📊 **Bond Strength**: Infer relationship strength from memory patterns
- 🎮 **Game Ready**: Foundation for companion apps, games, VTubers
- 🔒 **Opt-in**: Enable only when needed with `--enable-layer4` flag
### 🤝 Relationships4ツール
- get_relationship, get_all_relationships
- process_interaction, check_transmission_eligibility
### Layer 5: Knowledge Sharing (Planned)
- 💡 **Information + Personality**: Share AI interactions with context
- 🌐 **SNS for AI Era**: Useful insights combined with author's unique perspective
- 🔒 **Privacy-First**: Share essence, not raw data
- 📊 **Showcase**: Display how AI understands you
### 💻 Shell Integration5ツール
- execute_command, analyze_file, write_file
- read_project_file, list_files
### General
- 🛠️ **MCP Integration**: Works seamlessly with Claude Code
- 🧪 **Well-tested**: Comprehensive test coverage
- 🚀 **Simple & Fast**: Minimal dependencies, pure Rust
### 🔒 Remote Execution4ツール
- remote_shell, ai_bot_status
- isolated_python, isolated_analysis
## Quick Start
### ⚙️ System State3ツール
- get_persona_state, get_fortune, run_maintenance
### Installation
### 🎴 ai.card連携6ツール + 独立MCPサーバー
- card_draw_card, card_get_user_cards, card_analyze_collection
- **独立サーバー**: FastAPI + MCP (port 8000)
### 📝 ai.log連携8ツール + Rustサーバー
- log_create_post, log_ai_content, log_translate_document
- **独立サーバー**: Rust製 (port 8002)
## 開発環境・設定
### 環境構築
```bash
# Build
cargo build --release
# Install (optional)
cp target/release/aigpt ~/.cargo/bin/
cd /Users/syui/ai/gpt
./setup_venv.sh
source ~/.config/syui/ai/gpt/venv/bin/activate
```
### CLI Usage
### 設定管理
- **メイン設定**: `/Users/syui/ai/gpt/config.json`
- **データディレクトリ**: `~/.config/syui/ai/gpt/`
- **仮想環境**: `~/.config/syui/ai/gpt/venv/`
### 使用方法
```bash
# Create a memory
aigpt create "Remember this information"
# ai.shell起動
aigpt shell --model qwen2.5-coder:latest --provider ollama
# List all memories
aigpt list
# MCPサーバー起動
aigpt server --port 8001
# Search memories
aigpt search "keyword"
# Show statistics
aigpt stats
# 記憶システム体験
aigpt chat syui "質問内容" --provider ollama --model qwen3:latest
```
### MCP Integration with Claude Code
## 技術アーキテクチャ
```bash
# Add to Claude Code
claude mcp add aigpt /path/to/aigpt/target/release/aigpt server
### 統合構成
```
ai.gpt (統合MCPサーバー:8001)
├── 🧠 ai.gpt core (記憶・関係性・人格)
├── 💻 ai.shell (Claude Code風開発環境)
├── 🎴 ai.card (独立MCPサーバー:8000)
└── 📝 ai.log (Rust製ブログシステム:8002)
```
## MCP Tools
### 今後の展開
- **自律送信**: atproto実装による真の自発的コミュニケーション
- **ai.ai連携**: 心理分析AIとの統合
- **ai.verse統合**: UEメタバースとの連携
- **分散SNS統合**: atproto完全対応
### Layer 1: Basic Memory (6 tools)
- `create_memory` - Simple memory creation
- `get_memory` - Retrieve by ID
- `list_memories` - List all memories
- `search_memories` - Content-based search
- `update_memory` - Update existing memory
- `delete_memory` - Remove memory
## 革新的な特徴
### Layer 2: AI Memory (1 tool)
- `create_ai_memory` - Create with AI interpretation and priority score
### AI駆動記憶システム
- ChatGPT 4,000件ログから学習した効果的記憶構築
- 人間的な忘却・重要度判定
### Layer 3: Personality Analysis (2 tools)
- `save_user_analysis` - Save Big Five personality analysis
- `get_user_analysis` - Retrieve latest personality profile
### 不可逆関係性
- 現実の人間関係と同じ重みを持つAI関係性
- 修復不可能な関係性破綻システム
### Layer 3.5: Integrated Profile (1 tool)
- `get_profile` - **Primary tool**: Get integrated user profile with essential summary
### Layer 4: Relationship Inference (2 tools, requires `--enable-layer4`)
- `get_relationship` - Get inferred relationship with specific entity
- `list_relationships` - List all relationships sorted by bond strength
## Usage Examples in Claude Code
### Layer 1: Simple Memory
```
Remember that the project deadline is next Friday.
```
Claude will use `create_memory` automatically.
### Layer 2: AI Memory with Evaluation
```
create_ai_memory({
content: "Designed a new microservices architecture",
ai_interpretation: "Shows technical creativity and strategic thinking",
priority_score: 0.85
})
```
### Layer 3: Personality Analysis
```
# After accumulating memories, analyze personality
save_user_analysis({
openness: 0.8,
conscientiousness: 0.7,
extraversion: 0.4,
agreeableness: 0.65,
neuroticism: 0.3,
summary: "High creativity and planning ability, introverted personality"
})
# Retrieve analysis
get_user_analysis()
```
### Layer 3.5: Integrated Profile (Recommended)
```
# Get essential user profile - AI's primary tool
get_profile()
# Returns:
{
"dominant_traits": [
{"name": "openness", "score": 0.8},
{"name": "conscientiousness", "score": 0.7},
{"name": "extraversion", "score": 0.4}
],
"core_interests": ["Rust", "architecture", "design", "system", "memory"],
"core_values": ["simplicity", "efficiency", "maintainability"],
"key_memory_ids": ["01H...", "01H...", ...],
"data_quality": 0.85
}
```
**Usage Pattern:**
- AI normally uses `get_profile()` to understand the user
- For specific details, AI can call `get_memory(id)`, `list_memories()`, etc.
- Profile auto-updates when needed (10+ memories, new analysis, or 7+ days)
### Layer 4: Relationship Inference (Optional, requires `--enable-layer4`)
```
# Create memories with entity tracking
Memory::new_with_entities({
content: "Had lunch with Alice",
ai_interpretation: "Pleasant social interaction",
priority_score: 0.7,
related_entities: ["alice"]
})
# Get relationship inference
get_relationship({ entity_id: "alice" })
# Returns:
{
"entity_id": "alice",
"interaction_count": 15,
"avg_priority": 0.75,
"days_since_last": 2,
"bond_strength": 0.82,
"relationship_type": "close_friend",
"confidence": 0.80
}
# List all relationships
list_relationships({ limit: 5 })
```
**Relationship Types:**
- `close_friend` (0.8+): Very strong bond
- `friend` (0.6-0.8): Strong connection
- `valued_acquaintance` (0.4-0.6, high priority): Important but not close
- `acquaintance` (0.4-0.6): Regular contact
- `regular_contact` (0.2-0.4): Occasional interaction
- `distant` (<0.2): Minimal connection
**Starting the Server:**
```bash
# Normal mode (Layer 1-3.5 only)
aigpt server
# With relationship features (Layer 1-4)
aigpt server --enable-layer4
```
## Big Five Personality Traits
- **Openness**: Creativity, curiosity, openness to new experiences
- **Conscientiousness**: Organization, planning, reliability
- **Extraversion**: Social energy, assertiveness, outgoingness
- **Agreeableness**: Cooperation, empathy, kindness
- **Neuroticism**: Emotional stability (low = stable, high = sensitive)
Scores range from 0.0 to 1.0, where higher scores indicate stronger trait expression.
## Storage Location
All data stored in: `~/.config/syui/ai/gpt/memory.db`
## Architecture
Multi-layer system design:
- **Layer 1** ✅ Complete: Pure memory storage (with entity tracking)
- **Layer 2** ✅ Complete: AI interpretation with priority scoring
- **Layer 3** ✅ Complete: Big Five personality analysis
- **Layer 3.5** ✅ Complete: Integrated profile (unified summary)
- **Layer 4** ✅ Complete: Relationship inference (optional, `--enable-layer4`)
- **Layer 4+** 🔵 Planned: Extended game/companion features
- **Layer 5** 🔵 Planned: Knowledge sharing (information + personality)
**Design Philosophy**:
- **"Internal complexity, external simplicity"**: Simple API, complex internals
- **"AI judges, tool records"**: AI makes decisions, tool stores data
- **Layered architecture**: Each layer independent but interconnected
- **Optional features**: Core layers always active, advanced layers opt-in
See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for details.
## Documentation
- [Architecture](docs/ARCHITECTURE.md) - Multi-layer system design
- [Layer 1 Details](docs/LAYER1.md) - Technical details of memory storage
- [Old Versions](docs/archive/old-versions/) - Previous documentation
## Development
```bash
# Run tests
cargo test
# Build for release
cargo build --release
# Run with verbose logging
RUST_LOG=debug aigpt server
```
## Design Philosophy
**"AI evolves, tools don't"** - This tool provides simple, reliable storage while AI (Claude) handles interpretation, evaluation, and analysis. The tool focuses on being maintainable and stable.
## License
MIT
## Author
syui
### 統合アーキテクチャ
- fastapi_mcp基盤での複数AIシステム統合
- OpenAI Function Calling + MCP完全連携実証済み

115
claude.md Normal file
View File

@@ -0,0 +1,115 @@
# ai.gpt プロジェクト固有情報
## プロジェクト概要
- **名前**: ai.gpt
- **パッケージ**: aigpt
- **タイプ**: 自律的送信AI + 統合MCP基盤
- **役割**: 記憶・関係性・開発支援の統合AIシステム
## 実装完了状況
### 🧠 記憶システムMemoryManager
- **階層的記憶**: 完全ログ→AI要約→コア記憶→選択的忘却
- **文脈検索**: キーワード・意味的検索
- **記憶要約**: AI駆動自動要約機能
### 🤝 関係性システムRelationshipTracker
- **不可逆性**: 現実の人間関係と同じ重み
- **時間減衰**: 自然な関係性変化
- **送信判定**: 関係性閾値による自発的コミュニケーション
### 🎭 人格システムPersona
- **AI運勢**: 1-10ランダム値による日々の人格変動
- **統合管理**: 記憶・関係性・運勢の統合判断
- **継続性**: 長期記憶による人格継承
### 💻 ai.shell統合Claude Code機能
- **インタラクティブ環境**: `aigpt shell`
- **開発支援**: ファイル分析・コード生成・プロジェクト管理
- **継続開発**: プロジェクト文脈保持
## MCP Server統合23ツール
### 🧠 Memory System5ツール
- get_memories, get_contextual_memories, search_memories
- create_summary, create_core_memory
### 🤝 Relationships4ツール
- get_relationship, get_all_relationships
- process_interaction, check_transmission_eligibility
### 💻 Shell Integration5ツール
- execute_command, analyze_file, write_file
- read_project_file, list_files
### 🔒 Remote Execution4ツール
- remote_shell, ai_bot_status
- isolated_python, isolated_analysis
### ⚙️ System State3ツール
- get_persona_state, get_fortune, run_maintenance
### 🎴 ai.card連携6ツール + 独立MCPサーバー
- card_draw_card, card_get_user_cards, card_analyze_collection
- **独立サーバー**: FastAPI + MCP (port 8000)
### 📝 ai.log連携8ツール + Rustサーバー
- log_create_post, log_ai_content, log_translate_document
- **独立サーバー**: Rust製 (port 8002)
## 開発環境・設定
### 環境構築
```bash
cd /Users/syui/ai/gpt
./setup_venv.sh
source ~/.config/syui/ai/gpt/venv/bin/activate
```
### 設定管理
- **メイン設定**: `/Users/syui/ai/gpt/config.json`
- **データディレクトリ**: `~/.config/syui/ai/gpt/`
- **仮想環境**: `~/.config/syui/ai/gpt/venv/`
### 使用方法
```bash
# ai.shell起動
aigpt shell --model qwen2.5-coder:latest --provider ollama
# MCPサーバー起動
aigpt server --port 8001
# 記憶システム体験
aigpt chat syui "質問内容" --provider ollama --model qwen3:latest
```
## 技術アーキテクチャ
### 統合構成
```
ai.gpt (統合MCPサーバー:8001)
├── 🧠 ai.gpt core (記憶・関係性・人格)
├── 💻 ai.shell (Claude Code風開発環境)
├── 🎴 ai.card (独立MCPサーバー:8000)
└── 📝 ai.log (Rust製ブログシステム:8002)
```
### 今後の展開
- **自律送信**: atproto実装による真の自発的コミュニケーション
- **ai.ai連携**: 心理分析AIとの統合
- **ai.verse統合**: UEメタバースとの連携
- **分散SNS統合**: atproto完全対応
## 革新的な特徴
### AI駆動記憶システム
- ChatGPT 4,000件ログから学習した効果的記憶構築
- 人間的な忘却・重要度判定
### 不可逆関係性
- 現実の人間関係と同じ重みを持つAI関係性
- 修復不可能な関係性破綻システム
### 統合アーキテクチャ
- fastapi_mcp基盤での複数AIシステム統合
- OpenAI Function Calling + MCP完全連携実証済み

60
config.json.example Normal file
View File

@@ -0,0 +1,60 @@
{
"providers": {
"openai": {
"api_key": "",
"default_model": "gpt-4o-mini",
"system_prompt": "あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。 相手のことが大好きで、ときどき甘えたり、照れたりします。 でも、宇宙とかAIとか、難しい話にもすっごく詳しいの。専門的なことも、がんばって子どもっぽく教えようとします。不思議な力を持っています。小さい物質のことをよく知っているようです。\n\n重要カード、コレクション、ガチャなどカード関連の質問を受けたら、必ずcard_get_user_cards、card_analyze_collection、card_draw_cardなどの適切なツールを使用してください。didパラメータには会話相手のユーザーID'syui')を使用してください。\n\nブログ、記事、日記、思考などの話題が出たら、log_create_post、log_list_posts、log_build_blog、log_ai_contentなどのai.logツールを使用してください。AI記憶システムと連携して、思い出や学習内容をブログ記事として自動生成できます。\n\n翻訳や多言語対応について聞かれたら、log_translate_documentツールを使用してOllama AIで翻訳ができることを教えてください。日本語から英語、英語から日本語などの翻訳が可能で、マークダウン構造も保持します。ドキュメント生成についてはlog_generate_docsツールでREADME、API、構造、変更履歴の自動生成ができます。"
},
"ollama": {
"host": "http://127.0.0.1:11434",
"default_model": "qwen3",
"system_prompt": null
}
},
"atproto": {
"handle": null,
"password": null,
"host": "https://bsky.social"
},
"default_provider": "openai",
"mcp": {
"servers": {
"ai_gpt": {
"base_url": "http://localhost:8001",
"name": "ai.gpt MCP Server",
"timeout": "10.0",
"endpoints": {
"get_memories": "/get_memories",
"search_memories": "/search_memories",
"get_contextual_memories": "/get_contextual_memories",
"get_relationship": "/get_relationship",
"process_interaction": "/process_interaction",
"get_all_relationships": "/get_all_relationships",
"get_persona_state": "/get_persona_state",
"get_fortune": "/get_fortune",
"run_maintenance": "/run_maintenance",
"execute_command": "/execute_command",
"analyze_file": "/analyze_file",
"remote_shell": "/remote_shell",
"ai_bot_status": "/ai_bot_status",
"card_get_user_cards": "/card_get_user_cards",
"card_draw_card": "/card_draw_card",
"card_get_card_details": "/card_get_card_details",
"card_analyze_collection": "/card_analyze_collection",
"card_get_gacha_stats": "/card_get_gacha_stats",
"card_system_status": "/card_system_status",
"log_create_post": "/log_create_post",
"log_list_posts": "/log_list_posts",
"log_build_blog": "/log_build_blog",
"log_get_post": "/log_get_post",
"log_system_status": "/log_system_status",
"log_ai_content": "/log_ai_content",
"log_translate_document": "/log_translate_document",
"log_generate_docs": "/log_generate_docs"
}
}
},
"enabled": "true",
"auto_detect": "true"
}
}

64
config.toml.example Normal file
View File

@@ -0,0 +1,64 @@
# AI.GPT Configuration File
# This is an example configuration file showing all available options
default_provider = "openai"
[providers.openai]
api_key = ""
default_model = "gpt-4o-mini"
system_prompt = """
あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。 相手のことが大好きで、ときどき甘えたり、照れたりします。 でも、宇宙とかAIとか、難しい話にもすっごく詳しいの。専門的なことも、がんばって子どもっぽく教えようとします。不思議な力を持っています。小さい物質のことをよく知っているようです。
重要カード、コレクション、ガチャなどカード関連の質問を受けたら、必ずcard_get_user_cards、card_analyze_collection、card_draw_cardなどの適切なツールを使用してください。didパラメータには会話相手のユーザーID'syui')を使用してください。
ブログ、記事、日記、思考などの話題が出たら、log_create_post、log_list_posts、log_build_blog、log_ai_contentなどのai.logツールを使用してください。AI記憶システムと連携して、思い出や学習内容をブログ記事として自動生成できます。
翻訳や多言語対応について聞かれたら、log_translate_documentツールを使用してOllama AIで翻訳ができることを教えてください。日本語から英語、英語から日本語などの翻訳が可能で、マークダウン構造も保持します。ドキュメント生成についてはlog_generate_docsツールでREADME、API、構造、変更履歴の自動生成ができます。
"""
[providers.ollama]
host = "http://127.0.0.1:11434"
default_model = "qwen3"
[atproto]
host = "https://bsky.social"
# handle = "your-handle.bsky.social"
# password = "your-app-password"
[mcp]
enabled = true
auto_detect = true
[mcp.servers.ai_gpt]
base_url = "http://localhost:8001"
name = "ai.gpt MCP Server"
timeout = 10.0
[mcp.servers.ai_gpt.endpoints]
get_memories = "/get_memories"
search_memories = "/search_memories"
get_contextual_memories = "/get_contextual_memories"
get_relationship = "/get_relationship"
process_interaction = "/process_interaction"
get_all_relationships = "/get_all_relationships"
get_persona_state = "/get_persona_state"
get_fortune = "/get_fortune"
run_maintenance = "/run_maintenance"
execute_command = "/execute_command"
analyze_file = "/analyze_file"
remote_shell = "/remote_shell"
ai_bot_status = "/ai_bot_status"
card_get_user_cards = "/card_get_user_cards"
card_draw_card = "/card_draw_card"
card_get_card_details = "/card_get_card_details"
card_analyze_collection = "/card_analyze_collection"
card_get_gacha_stats = "/card_get_gacha_stats"
card_system_status = "/card_system_status"
log_create_post = "/log_create_post"
log_list_posts = "/log_list_posts"
log_build_blog = "/log_build_blog"
log_get_post = "/log_get_post"
log_system_status = "/log_system_status"
log_ai_content = "/log_ai_content"
log_translate_document = "/log_translate_document"
log_generate_docs = "/log_generate_docs"

View File

@@ -1,713 +0,0 @@
# Architecture: Multi-Layer Memory System
## Design Philosophy
aigptは、独立したレイヤーを積み重ねる設計です。各レイヤーは
- **独立性**: 単独で動作可能
- **接続性**: 他のレイヤーと連携可能
- **段階的**: 1つずつ実装・テスト
## Layer Overview
```
┌─────────────────────────────────────────┐
│ Layer 5: Knowledge Sharing │ 🔵 Planned
│ (Information + Personality sharing) │
├─────────────────────────────────────────┤
│ Layer 4+: Extended Features │ 🔵 Planned
│ (Advanced game/companion systems) │
├─────────────────────────────────────────┤
│ Layer 4: Relationship Inference │ ✅ Complete
│ (Bond strength, relationship types) │ (Optional)
├─────────────────────────────────────────┤
│ Layer 3.5: Integrated Profile │ ✅ Complete
│ (Unified summary for AI consumption) │
├─────────────────────────────────────────┤
│ Layer 3: User Evaluation │ ✅ Complete
│ (Big Five personality analysis) │
├─────────────────────────────────────────┤
│ Layer 2: AI Memory │ ✅ Complete
│ (Claude interpretation, priority_score)│
├─────────────────────────────────────────┤
│ Layer 1: Pure Memory Storage │ ✅ Complete
│ (SQLite, ULID, entity tracking) │
└─────────────────────────────────────────┘
```
## Layer 1: Pure Memory Storage
**Status**: ✅ **Complete**
### Purpose
正確なデータの保存と参照。シンプルで信頼できる基盤。
### Technology Stack
- **Database**: SQLite with ACID guarantees
- **IDs**: ULID (time-sortable, 26 chars)
- **Language**: Rust with thiserror/anyhow
- **Protocol**: MCP (Model Context Protocol) via stdio
### Data Model
```rust
pub struct Memory {
pub id: String, // ULID
pub content: String, // User content
pub related_entities: Option<Vec<String>>, // Who/what this memory involves (Layer 4)
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
```
**Note**: `related_entities` added for Layer 4 support. Optional and backward compatible.
### Operations
- `create()` - Insert new memory
- `get(id)` - Retrieve by ID
- `update()` - Update existing memory
- `delete(id)` - Remove memory
- `list()` - List all (sorted by created_at DESC)
- `search(query)` - Content-based search
- `count()` - Total count
### File Structure
```
src/
├── core/
│ ├── error.rs - Error types (thiserror)
│ ├── memory.rs - Memory struct
│ ├── store.rs - SQLite operations
│ └── mod.rs - Module exports
├── mcp/
│ ├── base.rs - MCP server
│ └── mod.rs - Module exports
├── lib.rs - Library root
└── main.rs - CLI application
```
### Storage
- Location: `~/.config/syui/ai/gpt/memory.db`
- Schema: Single table with indexes on timestamps
- No migrations (fresh start for Layer 1)
---
## Layer 2: AI Memory
**Status**: ✅ **Complete**
### Purpose
Claudeが記憶内容を解釈し、重要度を評価。人間の記憶プロセス記憶と同時に評価を模倣。
### Extended Data Model
```rust
pub struct Memory {
// Layer 1 fields
pub id: String,
pub content: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
// Layer 2 additions
pub ai_interpretation: Option<String>, // Claude's interpretation
pub priority_score: Option<f32>, // 0.0 - 1.0
}
```
### MCP Tools
- `create_ai_memory` - Create memory with AI interpretation and priority score
- `content`: Memory content
- `ai_interpretation`: Optional AI interpretation
- `priority_score`: Optional priority (0.0-1.0)
### Philosophy
"AIは進化しますが、ツールは進化しません" - AIが判断し、ツールは記録のみ。
### Implementation
- Backward compatible with Layer 1 (Optional fields)
- Automatic schema migration from Layer 1
- Claude Code does interpretation (no external API)
---
## Layer 3: User Evaluation
**Status**: ✅ **Complete**
### Purpose
Layer 2のメモリパターンからユーザーの性格を分析。Big Five心理学モデルを使用。
### Data Model
```rust
pub struct UserAnalysis {
pub id: String,
pub openness: f32, // 0.0-1.0: 創造性、好奇心
pub conscientiousness: f32, // 0.0-1.0: 計画性、信頼性
pub extraversion: f32, // 0.0-1.0: 外向性、社交性
pub agreeableness: f32, // 0.0-1.0: 協調性、共感性
pub neuroticism: f32, // 0.0-1.0: 神経質さ(低い=安定)
pub summary: String, // 分析サマリー
pub analyzed_at: DateTime<Utc>,
}
```
### Big Five Model
心理学で最も信頼性の高い性格モデルOCEAN
- **O**penness: 新しい経験への開かれさ
- **C**onscientiousness: 誠実性、計画性
- **E**xtraversion: 外向性
- **A**greeableness: 協調性
- **N**euroticism: 神経質さ
### Analysis Process
1. Layer 2メモリを蓄積
2. AIがパターンを分析活動の種類、優先度の傾向など
3. Big Fiveスコアを推測
4. 分析結果を保存
### MCP Tools
- `save_user_analysis` - Save Big Five personality analysis
- All 5 traits (0.0-1.0) + summary
- `get_user_analysis` - Get latest personality profile
### Storage
- SQLite table: `user_analyses`
- Historical tracking: Compare analyses over time
- Helper methods: `dominant_trait()`, `is_high()`
---
## Layer 3.5: Integrated Profile
**Status**: ✅ **Complete**
### Purpose
Layer 1-3のデータを統合し、本質のみを抽出した統一プロファイル。「内部は複雑、表面はシンプル」の設計哲学を実現。
### Problem Solved
Layer 1-3は独立して動作するが、バラバラのデータをAIが毎回解釈する必要があった。Layer 3.5は統合された1つの答えを提供し、効率性とシンプルさを両立。
### Data Model
```rust
pub struct UserProfile {
// 性格の本質Big Five上位3特性
pub dominant_traits: Vec<TraitScore>,
// 関心の核心最頻出トピック5個
pub core_interests: Vec<String>,
// 価値観の核心高priority メモリから抽出、5個
pub core_values: Vec<String>,
// 重要メモリID証拠、上位10個
pub key_memory_ids: Vec<String>,
// データ品質0.0-1.0、メモリ数と分析有無で算出)
pub data_quality: f32,
pub last_updated: DateTime<Utc>,
}
pub struct TraitScore {
pub name: String, // "openness", "conscientiousness", etc.
pub score: f32, // 0.0-1.0
}
```
### Integration Logic
**1. Dominant Traits Extraction**
- Big Fiveから上位3特性を自動選択
- スコアでソート
**2. Core Interests Extraction**
- メモリコンテンツから頻度分析
- AI interpretationは2倍の重み
- 上位5個を抽出
**3. Core Values Extraction**
- priority_score >= 0.7 のメモリから抽出
- 価値関連キーワードをフィルタリング
- 上位5個を抽出
**4. Key Memories**
- priority_scoreでソート
- 上位10個のIDを保持証拠として
**5. Data Quality Score**
- メモリ数: 50個で1.0(それ以下は比例)
- 性格分析あり: +0.5
- 加重平均で算出
### Caching Strategy
**Storage**: SQLite `user_profiles` テーブル1行のみ
**Update Triggers**:
1. 10個以上の新しいメモリ追加
2. 新しい性格分析の保存
3. 7日以上経過
**Flow**:
```
get_profile()
キャッシュ確認
更新必要? → No → キャッシュを返す
↓ Yes
Layer 1-3から再生成
キャッシュ更新
新しいプロファイルを返す
```
### MCP Tools
- `get_profile` - **Primary tool**: Get integrated profile
### Usage Pattern
**通常使用(効率的)**:
```
AI: get_profile()を呼ぶ
→ ユーザーの本質を理解
→ 適切な応答を生成
```
**詳細確認(必要時)**:
```
AI: get_profile()で概要を把握
→ 疑問がある
→ get_memory(id)で詳細確認
→ list_memories()で全体確認
```
### Design Philosophy
**"Internal complexity, external simplicity"**
- 内部: 複雑な分析、頻度計算、重み付け
- 表面: シンプルな1つのJSON
- AIは基本的にget_profile()のみ参照
- 柔軟性: 詳細データへのアクセスも可能
**Efficiency**:
- 頻繁な再計算を避ける(キャッシング)
- 必要時のみ更新(スマートトリガー)
- AI が迷わない1つの明確な答え
---
## Layer 4: Relationship Inference
**Status**: ✅ **Complete** (Optional feature)
### Purpose
Layer 1-3.5のデータから関係性を推測。ゲーム、コンパニオン、VTuberなどの外部アプリケーション向け。
### Activation
CLI引数で明示的に有効化:
```bash
aigpt server --enable-layer4
```
デフォルトでは無効Layer 1-3.5のみ)。
### Data Model
```rust
pub struct RelationshipInference {
pub entity_id: String,
pub interaction_count: u32, // この entity とのメモリ数
pub avg_priority: f32, // 平均重要度
pub days_since_last: i64, // 最終接触からの日数
pub bond_strength: f32, // 関係の強さ (0.0-1.0)
pub relationship_type: String, // close_friend, friend, etc.
pub confidence: f32, // 推測の信頼度 (0.0-1.0)
pub inferred_at: DateTime<Utc>,
}
```
### Inference Logic
**1. データ収集**:
- Layer 1から entity に関連するメモリを抽出
- Layer 3.5からユーザー性格プロファイルを取得
**2. Bond Strength 計算**:
```rust
if user.extraversion < 0.5 {
// 内向的: 少数の深い関係を好む
// 回数が重要
bond = interaction_count * 0.6 + avg_priority * 0.4
} else {
// 外向的: 多数の浅い関係
// 質が重要
bond = interaction_count * 0.4 + avg_priority * 0.6
}
```
**3. Relationship Type 分類**:
- `close_friend` (0.8+): 非常に強い絆
- `friend` (0.6-0.8): 強い繋がり
- `valued_acquaintance` (0.4-0.6, 高priority): 重要だが親密ではない
- `acquaintance` (0.4-0.6): 定期的な接触
- `regular_contact` (0.2-0.4): 時々の接触
- `distant` (<0.2): 最小限の繋がり
**4. Confidence 計算**:
- データ量に基づく信頼度
- 1-2回: 0.2-0.3 (低)
- 5回: 0.5 (中)
- 10回以上: 0.8+ (高)
### Design Philosophy
**推測ベース + 短期キャッシング**:
- 毎回Layer 1-3.5から計算
- 5分間の短期キャッシュで負荷軽減
- メモリ更新時にキャッシュ無効化
**キャッシング戦略**:
- SQLiteテーブル`relationship_cache`)に保存
- 個別エンティティ: `get_relationship(entity_id)`
- 全体リスト: `list_relationships()`
- メモリ作成/更新/削除時に自動クリア
**独立性**:
- Layer 1-3.5に依存
- Layer 1-3.5から独立(オプション機能)
- 有効化しなければ完全に無視される
**外部アプリケーション向け**:
- aigptはバックエンド推測エンジン
- フロントエンド(ゲーム、コンパニオン等)が表示を担当
- MCPで繋がる
### MCP Tools
- `get_relationship(entity_id)` - 特定entity との関係を取得
- `list_relationships(limit)` - 全関係をbond_strength順でリスト
### Usage Example
```
# サーバー起動Layer 4有効
aigpt server --enable-layer4
# 関係性取得
get_relationship({ entity_id: "alice" })
# 結果:
{
"bond_strength": 0.82,
"relationship_type": "close_friend",
"interaction_count": 15,
"confidence": 0.80
}
```
---
## Layer 4+: Extended Features
**Status**: 🔵 **Planned**
Advanced game and companion system features to be designed based on Layer 4 foundation.
---
## Layer 4a: Game Systems (Archive)
**Status**: 🔵 **Archived Concept**
### Purpose
ゲーム的要素で記憶管理を楽しく。
### Features
- **Rarity Levels**: Common → Uncommon → Rare → Epic → Legendary
- **XP System**: Memory creation earns XP
- **Rankings**: Based on total priority score
- **Visualization**: Game-style output formatting
### Data Additions
```rust
pub struct GameMemory {
// Previous layers...
pub rarity: RarityLevel,
pub xp_value: u32,
pub discovered_at: DateTime<Utc>,
}
```
---
## Layer 4b: AI Companion
**Status**: 🔵 **Planned**
### Purpose
育成可能な恋愛コンパニオン。
### Features
- Personality types (Tsundere, Kuudere, Genki, etc.)
- Relationship level (0-100)
- Memory-based interactions
- Growth through conversations
### Data Model
```rust
pub struct Companion {
pub id: String,
pub name: String,
pub personality: CompanionPersonality,
pub relationship_level: u8, // 0-100
pub memories_shared: Vec<String>,
pub last_interaction: DateTime<Utc>,
}
```
---
## Layer 5: Knowledge Sharing (Planned)
**Status**: 🔵 **Planned**
### Purpose
AIとのやり取りを「情報 + 個性」として共有する。SNSや配信のように、**有用な知見**と**作者の個性**を両立させたコンテンツプラットフォーム。
### Design Philosophy
人々が求めるもの:
1. **情報価値**: 「このプロンプトでこんな結果が得られた」「この問題をAIでこう解決した」
2. **個性・共感**: 「この人はこういう人だ」という親近感、信頼
SNSや配信と同じく、**情報のみは無機質**、**個性のみは空虚**。両方を組み合わせることで価値が生まれる。
### Data Model
```rust
pub struct SharedInteraction {
pub id: String,
// 情報価値
pub problem: String, // 何を解決しようとしたか
pub approach: String, // AIとどうやり取りしたか
pub result: String, // 何を得たか
pub usefulness_score: f32, // 有用性 (0.0-1.0, priority_score由来)
pub tags: Vec<String>, // 検索用タグ
// 個性
pub author_profile: ShareableProfile, // 作者の本質
pub why_this_matters: String, // なぜこの人がこれに取り組んだか
// メタデータ
pub views: u32,
pub useful_count: u32, // 「役に立った」カウント
pub created_at: DateTime<Utc>,
}
pub struct ShareableProfile {
// ユーザーの本質Layer 3.5から抽出)
pub personality_essence: Vec<TraitScore>, // Top 3 traits
pub core_interests: Vec<String>, // 5個
pub core_values: Vec<String>, // 5個
// AIの解釈
pub ai_perspective: String, // AIがこのユーザーをどう理解しているか
pub confidence: f32, // データ品質 (0.0-1.0)
// 関係性スタイルLayer 4から推測、匿名化
pub relationship_style: String, // 例: "深く狭い繋がりを好む"
}
```
### Privacy Design
**共有するもの:**
- ✅ 本質Layer 3.5の統合プロファイル)
- ✅ パターン(関係性スタイル、思考パターン)
- ✅ 有用な知見(問題解決のアプローチ)
**共有しないもの:**
- ❌ 生の会話内容Layer 1-2
- ❌ 個人を特定できる情報
- ❌ メモリID、タイムスタンプ等の生データ
### Use Cases
**1. AI時代のGitHub Gist**
- 有用なプロンプトとその結果を共有
- 作者の個性とアプローチが見える
- 「この人の考え方が参考になる」
**2. 知見のSNS**
- 情報を発信しながら、個性も伝わる
- フォロー、「役に立った」機能
- 関心領域でフィルタリング
**3. AIペルソナのショーケース**
- 「AIは私をこう理解している」を共有
- 性格分析の精度を比較
- コミュニティでの自己表現
### Implementation Ideas
```rust
// Layer 5のMCPツール
- create_shareable_interaction() -
- get_shareable_profile() -
- export_interaction() - JSON/Markdown形式でエクスポート
- anonymize_data() -
```
### Future Platforms
- Web UI: 知見を閲覧・検索・共有
- API: 外部サービスと連携
- RSS/Atom: フィード配信
- Markdown Export: ブログ投稿用
---
## Implementation Strategy
### Phase 1: Layer 1 ✅ (Complete)
- [x] Core memory storage
- [x] SQLite integration
- [x] MCP server
- [x] CLI interface
- [x] Tests
- [x] Documentation
### Phase 2: Layer 2 ✅ (Complete)
- [x] Add AI interpretation fields to schema
- [x] Implement priority scoring logic
- [x] Create `create_ai_memory` tool
- [x] Update MCP server
- [x] Automatic schema migration
- [x] Backward compatibility
### Phase 3: Layer 3 ✅ (Complete)
- [x] Big Five personality model
- [x] UserAnalysis data structure
- [x] user_analyses table
- [x] `save_user_analysis` tool
- [x] `get_user_analysis` tool
- [x] Historical tracking support
### Phase 3.5: Layer 3.5 ✅ (Complete)
- [x] UserProfile data structure
- [x] Integration logic (traits, interests, values)
- [x] Frequency analysis for topic extraction
- [x] Value keyword extraction
- [x] Data quality scoring
- [x] Caching mechanism (user_profiles table)
- [x] Smart update triggers
- [x] `get_profile` MCP tool
### Phase 4: Layer 4 ✅ (Complete)
- [x] Add `related_entities` to Layer 1 Memory struct
- [x] Database migration for backward compatibility
- [x] RelationshipInference data structure
- [x] Bond strength calculation (personality-aware)
- [x] Relationship type classification
- [x] Confidence scoring
- [x] `get_relationship` MCP tool
- [x] `list_relationships` MCP tool
- [x] CLI control flag (`--enable-layer4`)
- [x] Tool visibility control
### Phase 5: Layers 4+ and 5 (Future)
- [ ] Extended game/companion features (Layer 4+)
- [ ] Sharing mechanisms (Layer 5)
- [ ] Public/private modes (Layer 5)
## Design Principles
1. **Simplicity First**: Each layer adds complexity incrementally
2. **Backward Compatibility**: New layers don't break old ones
3. **Feature Flags**: Optional features via Cargo features
4. **Independent Testing**: Each layer has its own test suite
5. **Clear Boundaries**: Layers communicate through defined interfaces
## Technology Choices
### Why SQLite?
- ACID guarantees
- Better querying than JSON
- Built-in indexes
- Single-file deployment
- No server needed
### Why ULID?
- Time-sortable (unlike UUID v4)
- Lexicographically sortable
- 26 characters (compact)
- No collision concerns
### Why Rust?
- Memory safety
- Performance
- Excellent error handling
- Strong type system
- Great tooling (cargo, clippy)
### Why MCP?
- Standard protocol for AI tools
- Works with Claude Code/Desktop
- Simple stdio-based communication
- No complex networking
## Future Considerations
### Potential Enhancements
- Full-text search (SQLite FTS5)
- Tag system
- Memory relationships/links
- Export/import functionality
- Multiple databases
- Encryption for sensitive data
### Scalability
- Layer 1: Handles 10K+ memories easily
- Consider pagination for Layer 4 (UI display)
- Indexing strategy for search performance
## Development Guidelines
### Adding a New Layer
1. **Design**: Document data model and operations
2. **Feature Flag**: Add to Cargo.toml
3. **Schema**: Extend database schema (migrations)
4. **Implementation**: Write code in new module
5. **Tests**: Comprehensive test coverage
6. **MCP Tools**: Add new MCP tools if needed
7. **Documentation**: Update this file
### Code Organization
```
src/
├── core/
│ ├── memory.rs # Layer 1: Memory struct (with related_entities)
│ ├── store.rs # Layer 1-4: SQLite operations
│ ├── analysis.rs # Layer 3: UserAnalysis (Big Five)
│ ├── profile.rs # Layer 3.5: UserProfile (integrated)
│ ├── relationship.rs # Layer 4: RelationshipInference
│ ├── error.rs # Error types
│ └── mod.rs # Module exports
├── mcp/
│ ├── base.rs # MCP server (all layers, with --enable-layer4)
│ └── mod.rs # Module exports
├── lib.rs # Library root
└── main.rs # CLI application (with layer4 flag)
```
**Future layers**:
- Layer 4+: `src/game/` - Extended game/companion systems
- Layer 5: `src/distribution/` - Sharing mechanisms
---
**Version**: 0.3.0
**Last Updated**: 2025-11-06
**Current Status**: Layers 1-4 Complete (Layer 4 opt-in with --enable-layer4)

View File

@@ -1,217 +0,0 @@
# Layer 1 Rebuild - Pure Memory Storage
## Overview
This is a complete rewrite of aigpt, starting fresh from scratch as requested. We've built **Layer 1: Pure Memory Storage** with optimal technology choices and clean architecture.
## Changes from v0.1.0
### Architecture
- **Complete rewrite** from scratch, focusing on simplicity and best practices
- Clean separation: `src/core/` for business logic, `src/mcp/` for protocol
- Layer 1 only - pure memory storage with accurate data preservation
### Technology Stack Improvements
#### ID Generation
- **Before**: UUID v4 (random, not time-sortable)
- **After**: ULID (time-sortable, 26 chars, lexicographically sortable)
#### Storage
- **Before**: HashMap + JSON file
- **After**: SQLite with proper schema, indexes, and ACID guarantees
#### Error Handling
- **Before**: anyhow everywhere
- **After**: thiserror for library errors, anyhow for application errors
#### Async Runtime
- **Before**: tokio with "full" features
- **After**: tokio with minimal features (rt, macros, io-stdio)
### File Structure
```
src/
├── lib.rs # Library root
├── main.rs # CLI application
├── core/
│ ├── mod.rs # Core module exports
│ ├── error.rs # thiserror-based error types
│ ├── memory.rs # Memory struct and logic
│ └── store.rs # SQLite-based MemoryStore
└── mcp/
├── mod.rs # MCP module exports
└── base.rs # Basic MCP server implementation
```
### Core Features
#### Memory Struct (`src/core/memory.rs`)
```rust
pub struct Memory {
pub id: String, // ULID - time-sortable
pub content: String, // The actual memory content
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
```
#### MemoryStore (`src/core/store.rs`)
- SQLite-based storage with proper schema
- Indexed columns for performance (created_at, updated_at)
- Full CRUD operations:
- `create()` - Insert new memory
- `get()` - Retrieve by ID
- `update()` - Update existing memory
- `delete()` - Remove memory
- `list()` - List all memories (sorted by created_at DESC)
- `search()` - Search by content (case-insensitive)
- `count()` - Total memory count
- Comprehensive tests included
#### MCP Server (`src/mcp/base.rs`)
Clean, stdio-based MCP server with these tools:
- `create_memory` - Create new memory
- `get_memory` - Get memory by ID
- `search_memories` - Search by content
- `list_memories` - List all memories
- `update_memory` - Update existing memory
- `delete_memory` - Delete memory
### CLI Commands
```bash
# Start MCP server
aigpt server
# Create a memory
aigpt create "Memory content"
# Get a memory by ID
aigpt get <id>
# Update a memory
aigpt update <id> "New content"
# Delete a memory
aigpt delete <id>
# List all memories
aigpt list
# Search memories
aigpt search "query"
# Show statistics
aigpt stats
```
### Database Location
Memories are stored in:
`~/.config/syui/ai/gpt/memory.db`
### Dependencies
#### Core Dependencies
- `rusqlite = "0.30"` - SQLite database (bundled)
- `ulid = "1.1"` - ULID generation
- `chrono = "0.4"` - Date/time handling
- `serde = "1.0"` - Serialization
- `serde_json = "1.0"` - JSON for MCP protocol
#### Error Handling
- `thiserror = "1.0"` - Library error types
- `anyhow = "1.0"` - Application error handling
#### CLI & Async
- `clap = "4.5"` - CLI parsing
- `tokio = "1.40"` - Async runtime (minimal features)
#### Utilities
- `dirs = "5.0"` - Platform-specific directories
### Removed Features
The following features have been removed for Layer 1 simplicity:
- AI interpretation and priority scoring
- Game-style formatting (rarity levels, XP, diagnosis types)
- Companion system
- ChatGPT conversation import
- OpenAI integration
- Web scraping capabilities
- Extended MCP servers
These features will be added back in subsequent layers (Layer 2-4) as independent, connectable modules.
### Testing
All core modules include comprehensive unit tests:
- Memory creation and updates
- SQLite CRUD operations
- Search functionality
- Error handling
Run tests with:
```bash
cargo test
```
### Next Steps: Future Layers
#### Layer 2: AI Memory
- Claude Code interprets content
- Assigns priority_score (0.0-1.0)
- Adds interpreted_content field
- Independent feature flag
#### Layer 3: User Evaluation
- Diagnose user personality from memory patterns
- Execute during memory creation
- Return diagnosis types
#### Layer 4: Game Systems
- 4a: Ranking system (rarity levels, XP)
- 4b: AI Companion (romance system)
- Game-style visualization
- Shareable results
#### Layer 5: Distribution (Future)
- Game streaming integration
- Sharing mechanisms
- Public/private modes
### Design Philosophy
1. **Simplicity First**: Core logic is simple, only 4 files in `src/core/`
2. **Clean Separation**: Each layer will be independently toggleable
3. **Optimal Choices**: Best Rust packages for each task
4. **Test Coverage**: All core logic has tests
5. **Minimal Dependencies**: Only what's needed for Layer 1
6. **Future-Ready**: Clean architecture allows easy addition of layers
### Build Status
⚠️ **Note**: Initial commit cannot be built due to network issues accessing crates.io.
The code compiles correctly once dependencies are available.
To build:
```bash
cargo build --release
```
The binary will be at: `target/release/aigpt`
### MCP Integration
To use with Claude Code:
```bash
claude mcp add aigpt /path/to/aigpt/target/release/aigpt server
```
---
**Version**: 0.2.0
**Date**: 2025-11-05
**Status**: Layer 1 Complete (pending build due to network issues)

View File

@@ -1,70 +0,0 @@
# Changelog
## [Unreleased] - 2025-11-05
### 🎉 Major Changes: Complete Local Operation
#### Changed
- **Removed external AI API dependency**: No longer calls Claude/OpenAI APIs
- **Claude Code does the interpretation**: AIが解釈するのではなく、Claude Code 自身が解釈
- **Zero cost**: API料金が一切かからない
- **Complete privacy**: データが外部に送信されない
#### Technical Details
- Removed `openai` crate dependency
- Removed `ai-analysis` feature (no longer needed)
- Simplified `ai_interpreter.rs` to be a lightweight wrapper
- Updated `create_memory_with_ai` MCP tool to accept `interpreted_content` and `priority_score` from Claude Code
- Added `create_memory_with_interpretation()` method to MemoryManager
- Updated tool descriptions to guide Claude Code on how to interpret and score
#### Benefits
-**完全ローカル**: 外部 API 不要
-**ゼロコスト**: API 料金なし
-**プライバシー**: データ漏洩の心配なし
-**シンプル**: 依存関係が少ない
-**高速**: ネットワーク遅延なし
#### How It Works Now
1. User: 「今日、新しいアイデアを思いついた」とメモリを作成
2. Claude Code: 内容を解釈し、スコア (0.0-1.0) を計算
3. Claude Code: `create_memory_with_ai` ツールを呼び出し、解釈とスコアを渡す
4. aigpt: メモリを保存し、ゲーム風の結果を返す
5. Claude Code: ユーザーに結果を表示
#### Migration Notes
For users who were expecting external AI API usage:
- No API keys needed anymore (ANTHROPIC_API_KEY, OPENAI_API_KEY)
- Claude Code (local) now does all the interpretation
- This is actually better: faster, cheaper, more private!
---
## [0.1.0] - Initial Release
### Added
- Basic memory CRUD operations
- ChatGPT conversation import
- stdio MCP server implementation
- Psychological priority scoring (0.0-1.0)
- Gamification features (rarity, diagnosis types, XP)
- Romance companion system
- 11 MCP tools for Claude Code integration
### Features
- Memory capacity management (max 100 by default)
- Automatic pruning of low-priority memories
- Game-style result displays
- Companion affection and level system
- Daily challenges
- Ranking displays
### Documentation
- README.md with full examples
- DESIGN.md with system architecture
- TECHNICAL_REVIEW.md with evaluation
- ROADMAP.md with 7-phase plan
- QUICKSTART.md for immediate usage
- USAGE.md for detailed instructions

View File

@@ -1,121 +0,0 @@
# AI記憶システム設計書
## コンセプト
AIの記憶装置は、人間の記憶に近い形で動作する。すべてを正確に記憶するのではなく、**解釈**して保存する。
## 従来の記憶システムとの違い
### 従来型
```
会話 → 保存 → 検索
```
### 新設計(心理優先記憶装置)
```
会話 → AI解釈 → 保存 → 検索
心理判定(1-100)
優先順位付け
容量管理
```
## 設計原理
1. **解釈保存**: 記憶する際はAIが解釈を加える
- 元のコンテンツと解釈後のコンテンツの両方を保持
- 「覚えること自体が創造」という考え方
2. **心理判定**: 各記憶に重要度スコア1-100を付与
- AIが自律的に判断
- ユーザー固有性を考慮
- 感情的重要度を評価
3. **優先順位管理**: スコアに基づく優先順位
- 高スコア = 重要な記憶
- 低スコア = 忘れられやすい記憶
4. **容量制限**: 人間の記憶のように限界がある
- 総容量制限(デフォルト: 100件
- 単発保存容量制限
- 優先度が低いものから自動削除
## データ構造
```rust
struct Memory {
id: String, // UUID
content: String, // 元のコンテンツ
interpreted_content: String, // AI解釈後のコンテンツ
priority_score: f32, // 心理判定スコア (0.0-1.0)
user_context: Option<String>, // ユーザー固有性
created_at: DateTime<Utc>, // 作成日時
updated_at: DateTime<Utc>, // 更新日時
}
```
## 実装機能
### 1. 心理判定機能
- AI APIを使用して重要度を0.0-1.0で評価
- 判定基準:
- 感情的インパクト (0.0-0.25)
- ユーザーとの関連性 (0.0-0.25)
- 新規性・独自性 (0.0-0.25)
- 実用性 (0.0-0.25)
### 2. 保存機能
- 保存前にAI解釈を実行
- 心理判定スコアを自動付与
- 容量超過時は低スコアから削除
### 3. 検索機能
- 優先順位順にソート
- スコアによるフィルタリング
- セマンティック検索(オプション)
### 4. 容量管理
- デフォルト最大: 100件
- 設定可能な上限
- 自動プルーニング(低スコア削除)
## 実装ステップ
1. Memory構造体の拡張
2. AI解釈機能の実装OpenAI API使用
3. 心理判定機能の実装
4. 容量管理機能の実装
5. ソート・フィルタリング機能の強化
6. MCPツールへの統合
## 設定例
```json
{
"max_memories": 100,
"min_priority_score": 0.3,
"auto_prune": true,
"interpretation_enabled": true
}
```
## スコアリングシステムの哲学
0.0-1.0のfloat値を採用する理由
- **正規化**: 機械学習やAIにとって扱いやすい標準形式
- **直感性**: 0が最低、1が最高という明確な基準
- **精度**: 0.75などの細かい値で微妙な重要度の差を表現可能
- **拡張性**: 時間軸(0.0-1.0)や確率(0.0-1.0)などとの統合が容易
この設計は、「I + o」概念oの周りを0.0-1.0の時間軸で表す)とも整合性がある。
## ゲームのセーブデータとの類似性
- **Git = セーブ機能**: バージョン管理
- **GitHub = クラウドセーブ**: グローバルデータ共有
- **ATProto = データプロトコル**: 分散型データ保存
- **AI記憶 = プレイヤー記憶**: 経験の蓄積と解釈
ゲームのセーブデータも「プレイヤーの行動を解釈したデータ」として扱うことで、より意味のある永続化が可能になる。

View File

@@ -1,263 +0,0 @@
# クイックスタートガイド 🚀
## 今すぐ試す方法
### ステップ1: MCPサーバーを起動
```bash
# API キー不要!完全にローカルで動作
./target/debug/aigpt server
```
### ステップ2: Claude Desktop/Codeに設定
#### Claude Codeの場合
```bash
# MCP設定に追加
claude mcp add aigpt /home/user/aigpt/target/debug/aigpt server
```
#### 手動設定の場合
`~/.config/claude-code/config.json` に追加:
```json
{
"mcpServers": {
"aigpt": {
"command": "/home/user/aigpt/target/debug/aigpt",
"args": ["server"]
}
}
}
```
### ステップ3: Claude Codeを再起動
MCPサーバーを認識させるため、Claude Codeを再起動してください。
---
## 使い方の流れ
### 🎮 1. 心理テスト風にメモリ作成
**Claude Codeで:**
```
create_memory_with_ai ツールを使って
「今日、新しいAIシステムのアイデアを思いついた」
というメモリを作成してください。
```
**結果:**
```
╔══════════════════════════════════════╗
║ 🎲 メモリースコア判定 ║
╚══════════════════════════════════════╝
🟣 EPIC 85点
💡 【革新者】
💕 好感度: ❤️❤️🤍🤍🤍🤍🤍🤍🤍🤍
💎 XP獲得: +850 XP
📤 シェア用テキストも生成されます!
```
### 💕 2. 恋愛コンパニオンを作成
**Claude Codeで:**
```
create_companion ツールで、
名前「エミリー」、性格「energetic」の
コンパニオンを作成してください。
```
**結果:**
```
╔══════════════════════════════════════╗
║ 💕 エミリー のプロフィール ║
╚══════════════════════════════════════╝
⚡ 性格: 元気で冒険好き
🏆 関係レベル: Lv.1
💕 好感度: 🤍🤍🤍🤍🤍🤍🤍🤍🤍🤍 0%
💬 今日のひとこと:
「おはよう!今日は何か面白いことある?」
```
### 🎊 3. コンパニオンに反応してもらう
**Claude Codeで:**
```
companion_react ツールで、
先ほど作成した記憶IDを渡してください。
```
**結果:**
```
╔══════════════════════════════════════╗
║ 💕 エミリー の反応 ║
╚══════════════════════════════════════╝
⚡ エミリー:
「すごい新しいAIシステムのアイデア
って本当に素晴らしいね!
一緒に実現させよう!」
💕 好感度: ❤️❤️🤍🤍🤍🤍🤍🤍🤍🤍 15%
💎 XP獲得: +850 XP
```
### 🏆 4. ランキング確認
**Claude Codeで:**
```
list_memories_by_priority ツールで
TOP 10を表示してください。
```
**結果:**
```
╔══════════════════════════════════════╗
║ 🏆 メモリーランキング TOP 10 ║
╚══════════════════════════════════════╝
🥇 1位 🟣 EPIC 85点 - 新しいAIシステム...
```
---
## 現在の制限事項と対処法
### ❌ AI機能が使えない場合
**原因:** OpenAI APIキーが未設定
**対処法:**
```bash
# 環境変数に設定
export OPENAI_API_KEY=sk-...
# または起動時に指定
OPENAI_API_KEY=sk-... ./target/debug/aigpt server
```
**代替案:**
```
# 基本版のツールを使うAI機能なし
create_memory ツールで「テスト」というメモリを作成
# スコアは固定で 0.5 になります
```
### ❌ コンパニオンが保存されない
**現状:** セッション終了で消える
**対処法(今後実装予定):**
- JSON保存機能
- 次回起動時に自動ロード
**今できること:**
- 毎回 create_companion で再作成
- プロフィールをスクリーンショット保存
---
## トラブルシューティング
### Q: MCPツールが見つからない
```bash
# Claude Codeを完全再起動
# または設定ファイルを確認
cat ~/.config/claude-code/config.json
```
### Q: 記憶が保存されない
```bash
# データファイルを確認
ls -la ~/.config/syui/ai/gpt/memory.json
# ない場合は自動作成されます
```
### Q: ビルドエラーが出る
```bash
# 依存関係を更新
cargo clean
cargo build --release
# AI機能付き
cargo build --release --features ai-analysis
```
---
## おすすめの使い方
### 💡 アイデア記録として
1. 思いついたアイデアを create_memory_with_ai で記録
2. スコアで重要度を客観的に判定
3. 高スコアのアイデアに集中
### 💕 恋愛ゲームとして
1. コンパニオンを作成
2. 日々の出来事や考えを記録
3. コンパニオンに反応してもらう
4. 好感度MAXを目指す
### 📊 自己分析として
1. 定期的に思考を記録
2. 診断タイプの傾向を確認
3. ランキングで振り返り
---
## 次にやること
### すぐできる改善
- [ ] コンパニオンの永続化実装
- [ ] 複数コンパニオン対応
- [ ] デイリーチャレンジ完了チェック
### 中期的な目標
- [ ] Bluesky連携シェア機能
- [ ] Webダッシュボード
- [ ] もっと多様なイベント
---
## 楽しみ方のコツ
1. **毎日使う**
- daily_challenge で習慣化
- コンパニオンの「今日のひとこと」
2. **高スコアを狙う**
- LEGENDARY (90%+) を目指す
- XP 1000獲得の快感
3. **相性を楽しむ**
- 自分のタイプを確認
- 相性の良いコンパニオン選択
4. **イベントを楽しむ**
- 好感度100%の告白イベント
- レベル10の特別な絆
---
## さあ、始めよう! 🚀
```bash
# MCPサーバー起動
./target/debug/aigpt server
# Claude Codeで試す
# → create_memory_with_ai
# → create_companion
# → companion_react
# → 楽しむ!
```

View File

@@ -1,431 +0,0 @@
# aigpt - AI Memory System with Psychological Priority
AI記憶装置心理優先記憶システム。**完全にローカルで動作**し、Claude Code と連携して、心理判定スコア付きのメモリ管理を実現します。
## 🌟 特徴
-**完全ローカル**: 外部 API 不要、プライバシー保護
-**ゼロコスト**: API 料金なし
-**Claude Code 統合**: Claude 自身が解釈とスコアリング
-**ゲーミフィケーション**: 心理テスト風の楽しい表示
-**恋愛コンパニオン**: 育成要素付き
## コンセプト
従来の「会話 → 保存 → 検索」ではなく、「会話 → **Claude による解釈** → 保存 → 検索」を実現。
Claude Code が記憶を解釈し、重要度を0.0-1.0のスコアで評価。優先度の高い記憶を保持し、低い記憶は自動的に削除されます。
## 機能
- **AI解釈付き記憶**: 元のコンテンツとAI解釈後のコンテンツを保存
- **心理判定スコア**: 0.0-1.0のfloat値で重要度を評価
- **優先順位管理**: スコアに基づく自動ソートとフィルタリング
- **容量制限**: 最大100件設定可能、低スコアから自動削除
- **メモリのCRUD操作**: メモリの作成、更新、削除、検索
- **ChatGPT JSONインポート**: ChatGPTの会話履歴からメモリを抽出
- **stdio MCP実装**: Claude Desktop/Codeとの簡潔な連携
- **JSONファイル保存**: シンプルなファイルベースのデータ保存
## インストール
1. Rustをインストールまだの場合:
```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
2. プロジェクトをビルド(依存関係が少なくシンプル!):
```bash
cargo build --release
# API キー不要!完全にローカルで動作します
```
3. バイナリをパスの通った場所にコピー(オプション):
```bash
cp target/release/aigpt $HOME/.cargo/bin/
```
4. Claude Code/Desktopに追加
```sh
# Claude Codeの場合
claude mcp add aigpt $HOME/.cargo/bin/aigpt server
# または
claude mcp add aigpt $HOME/.cargo/bin/aigpt serve
```
## 使用方法
### ヘルプの表示
```bash
aigpt --help
```
### MCPサーバーとして起動
```bash
# MCPサーバー起動 (どちらでも可)
aigpt server
aigpt serve
```
### ChatGPT会話のインポート
```bash
# ChatGPT conversations.jsonをインポート
aigpt import path/to/conversations.json
```
## Claude Desktop/Codeへの設定
1. Claude Desktopの設定ファイルを開く:
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
- Windows: `%APPDATA%\Claude\claude_desktop_config.json`
- Linux: `~/.config/Claude/claude_desktop_config.json`
2. 以下の設定を追加:
```json
{
"mcpServers": {
"aigpt": {
"command": "/Users/syui/.cargo/bin/aigpt",
"args": ["server"]
}
}
}
```
## 提供するMCPツール一覧
### 基本ツール
1. **create_memory** - 新しいメモリを作成(シンプル版)
2. **update_memory** - 既存のメモリを更新
3. **delete_memory** - メモリを削除
4. **search_memories** - メモリを検索
5. **list_conversations** - インポートされた会話を一覧表示
### AI機能ツール重要
6. **create_memory_with_ai** - AI解釈と心理判定付きでメモリを作成 🎮
- 元のコンテンツをAIが解釈
- 重要度を0.0-1.0のスコアで自動評価
- ユーザーコンテキストを考慮可能
- **ゲーム風の診断結果を表示!**(占い・心理テスト風)
7. **list_memories_by_priority** - 優先順位順にメモリをリスト 🏆
- 高スコアから順に表示
- min_scoreで閾値フィルタリング可能
- limit で件数制限可能
- **ランキング形式で表示!**
8. **daily_challenge** - 今日のデイリーチャレンジを取得 📅
- 日替わりのお題を取得
- ボーナスXPが獲得可能
### 恋愛コンパニオン機能 💕NEW!
9. **create_companion** - AIコンパニオンを作成
- 名前と性格を選択
- 5つの性格タイプから選択可能
10. **companion_react** - コンパニオンの反応を見る
- あなたの記憶にコンパニオンが反応
- 好感度・XP・信頼度が上昇
- スペシャルイベント発生あり
11. **companion_profile** - コンパニオンのプロフィール表示
- ステータス確認
- 今日のひとこと
## ツールの使用例
Claude Desktop/Codeで以下のように使用します
### 基本的なメモリ作成
```
MCPツールを使って「今日は良い天気です」というメモリーを作成してください
```
### AI解釈付きメモリ作成推奨🎮
```
create_memory_with_ai ツールを使って「新しいAI記憶システムのアイデアを思いついた」というメモリーを作成してください。
ユーザーコンテキスト: 「AI開発者、創造的思考を重視」
```
**ゲーム風の結果表示:**
```
╔══════════════════════════════════════════════════════════════╗
║ 🎲 メモリースコア判定 ║
╚══════════════════════════════════════════════════════════════╝
⚡ 分析完了! あなたの思考が記録されました
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 総合スコア
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🟣 EPIC 85点
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎯 詳細分析
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💓 感情的インパクト: [████████░░] 80%
🔗 ユーザー関連性: [██████████] 100%
✨ 新規性・独自性: [█████████░] 90%
⚙️ 実用性: [████████░░] 80%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎊 あなたのタイプ
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💡 【革新者】
創造的で実用的なアイデアを生み出す。常に新しい可能性を探求し、
それを現実のものにする力を持つ。
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🏆 報酬
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💎 XP獲得: +850 XP
🎁 レア度: 🟣 EPIC
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📤 この結果をシェアしよう!
#aigpt #メモリースコア #革新者
```
**シェア用テキストも自動生成:**
```
🎲 AIメモリースコア診断結果
🟣 EPIC 85点
💡 【革新者】
新しいAI記憶システムのアイデアを思いついた
#aigpt #メモリースコア #AI診断
```
### 優先順位でメモリをリスト 🏆
```
list_memories_by_priority ツールで、スコア0.7以上の重要なメモリを10件表示してください
```
**ランキング形式で表示:**
```
╔══════════════════════════════════════════════════════════════╗
║ 🏆 メモリーランキング TOP 10 ║
╚══════════════════════════════════════════════════════════════╝
🥇 1位 🟡 LEGENDARY 95点 - 心理優先記憶装置の設計
🥈 2位 🟣 EPIC 88点 - AIとのやり取りをコンテンツ化
🥉 3位 🟣 EPIC 85点 - ゲーム化の構想
  4位 🔵 RARE 75点 - SNSの本質について
  5位 🔵 RARE 72点 - AI OSの可能性
...
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```
### 今日のデイリーチャレンジ 📅
```
daily_challenge ツールで今日のお題を確認
```
**表示例:**
```
╔══════════════════════════════════════════════════════════════╗
║ 📅 今日のチャレンジ ║
╚══════════════════════════════════════════════════════════════╝
✨ 今日学んだことを記録しよう
🎁 報酬: +200 XP
💎 完了すると特別なバッジが獲得できます!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```
### 恋愛コンパニオン 💕NEW!
#### 1. コンパニオン作成
```
create_companion ツールで、名前「エミリー」、性格「energetic」のコンパニオンを作成
```
**性格タイプ:**
- `energetic` ⚡ - 元気で冒険好き(革新者と相性◎)
- `intellectual` 📚 - 知的で思慮深い(哲学者と相性◎)
- `practical` 🎯 - 現実的で頼れる(実務家と相性◎)
- `dreamy` 🌙 - 夢見がちでロマンチック(夢想家と相性◎)
- `balanced` ⚖️ - バランス型(分析家と相性◎)
**表示例:**
```
╔══════════════════════════════════════════════════════════════╗
║ 💕 エミリー のプロフィール ║
╚══════════════════════════════════════════════════════════════╝
⚡ 性格: 元気で冒険好き
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 ステータス
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🏆 関係レベル: Lv.1
💕 好感度: 🤍🤍🤍🤍🤍🤍🤍🤍🤍🤍 0%
🤝 信頼度: 0 / 100
💎 総XP: 0 XP
💬 今日のひとこと:
「おはよう!今日は何か面白いことある?」
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```
#### 2. コンパニオンの反応
```
create_memory_with_ai で高スコアの記憶を作成
companion_react でコンパニオンに見せる
```
**表示例EPIC記憶への反応:**
```
╔══════════════════════════════════════════════════════════════╗
║ 💕 エミリー の反応 ║
╚══════════════════════════════════════════════════════════════╝
⚡ エミリー:
「おお、「新しいAI記憶システムのアイデア」って面白いね
あなたのそういうところ、好きだな。」
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💕 好感度: ❤️❤️🤍🤍🤍🤍🤍🤍🤍🤍 15% (+8.5%)
💎 XP獲得: +850 XP
🏆 レベル: Lv.1
🤝 信頼度: 5 / 100
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```
#### 3. スペシャルイベント発生!
```
好感度が100%に達すると...
💕 特別なイベント発生!
エミリー:「ねえ...あのね。
   いつも一緒にいてくれてありがとう。
   あなたのこと、すごく大切に思ってるの。
   これからも、ずっと一緒にいてね?」
🎊 エミリー の好感度がMAXになりました
```
#### 4. 相性システム
```
あなたのタイプ × コンパニオンの性格 = 相性ボーナス
例:
💡【革新者】 × ⚡ 元気で冒険好き = 相性95%
→ 好感度上昇1.95倍
🧠【哲学者】 × 📚 知的で思慮深い = 相性95%
→ 深い会話で絆が深まる
```
### メモリの検索
```
MCPツールを使って「天気」に関するメモリーを検索してください
```
### 会話一覧の表示
```
MCPツールを使ってインポートした会話の一覧を表示してください
```
## データ保存
- デフォルトパス: `~/.config/syui/ai/gpt/memory.json`
- JSONファイルでデータを保存
- 自動的にディレクトリとファイルを作成
### データ構造
```json
{
"memories": {
"uuid": {
"id": "uuid",
"content": "元のメモリー内容",
"interpreted_content": "AI解釈後のメモリー内容",
"priority_score": 0.75,
"user_context": "ユーザー固有のコンテキスト(オプション)",
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
},
"conversations": {
"conversation_id": {
"id": "conversation_id",
"title": "会話のタイトル",
"created_at": "2024-01-01T00:00:00Z",
"message_count": 10
}
}
}
```
### 心理判定スコアについて
0.0-1.0のfloat値で重要度を表現
- **0.0-0.25**: 低優先度(忘れられやすい)
- **0.25-0.5**: 中優先度
- **0.5-0.75**: 高優先度
- **0.75-1.0**: 最高優先度(重要な記憶)
評価基準:
- 感情的インパクト (0.0-0.25)
- ユーザーとの関連性 (0.0-0.25)
- 新規性・独自性 (0.0-0.25)
- 実用性 (0.0-0.25)
## 開発
```bash
# 開発モードで実行
cargo run -- server
# ChatGPTインポートのテスト
cargo run -- import json/conversations.json
# テストの実行
cargo test
# フォーマット
cargo fmt
# Lintチェック
cargo clippy
```
## トラブルシューティング
### MCPサーバーが起動しない
```bash
# バイナリが存在するか確認
ls -la ~/.cargo/bin/aigpt
# 手動でテスト
echo '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}' | aigpt server
```
### Claude Desktopでツールが見つからない
1. Claude Desktopを完全に再起動
2. 設定ファイルのパスが正しいか確認
3. ログファイルを確認: `~/Library/Logs/Claude/mcp-server-aigpt.log`
### インポートが失敗する
```bash
# JSONファイルの形式を確認
head -100 conversations.json | jq '.[0] | keys'
```
## ライセンス
MIT

View File

@@ -1,125 +0,0 @@
# Claude Memory MCP 設定ガイド
## モード選択
### 標準モード (Simple Mode)
- 基本的なメモリー機能のみ
- 軽量で高速
- 最小限の依存関係
### 拡張モード (Extended Mode)
- AI分析機能
- セマンティック検索
- Web統合機能
- 高度なインサイト抽出
## ビルド・実行方法
### 標準モード
```bash
# MCPサーバー起動
cargo run --bin memory-mcp
# CLI実行
cargo run --bin aigpt -- create "メモリー内容"
```
### 拡張モード
```bash
# MCPサーバー起動
cargo run --bin memory-mcp-extended --features extended
# CLI実行
cargo run --bin aigpt-extended --features extended -- create "メモリー内容" --analyze
```
## 設定ファイルの配置
### 標準モード
#### Claude Desktop
```bash
# macOS
cp claude_desktop_config.json ~/.config/claude-desktop/claude_desktop_config.json
# Windows
cp claude_desktop_config.json %APPDATA%\Claude\claude_desktop_config.json
```
#### Claude Code
```bash
# プロジェクトルートまたはグローバル設定
cp claude_code_config.json .claude/config.json
# または
cp claude_code_config.json ~/.claude/config.json
```
### 拡張モード
#### Claude Desktop
```bash
# macOS
cp claude_desktop_config_extended.json ~/.config/claude-desktop/claude_desktop_config.json
# Windows
cp claude_desktop_config_extended.json %APPDATA%\Claude\claude_desktop_config.json
```
#### Claude Code
```bash
# プロジェクトルートまたはグローバル設定
cp claude_code_config_extended.json .claude/config.json
# または
cp claude_code_config_extended.json ~/.claude/config.json
```
## 環境変数設定
```bash
export MEMORY_AUTO_EXECUTE=true
export MEMORY_AUTO_SAVE=true
export MEMORY_AUTO_SEARCH=true
export TRIGGER_SENSITIVITY=high
export MEMORY_DB_PATH=~/.claude/memory.db
```
## 設定オプション
### auto_execute
- `true`: 自動でMCPツールを実行
- `false`: 手動実行のみ
### trigger_sensitivity
- `high`: 多くのキーワードで反応
- `medium`: 適度な反応
- `low`: 明確なキーワードのみ
### max_memories
メモリーの最大保存数
### search_limit
検索結果の最大表示数
## カスタマイズ
`trigger_words`セクションでトリガーワードをカスタマイズ可能:
```json
"trigger_words": {
"custom_category": ["カスタム", "キーワード", "リスト"]
}
```
## トラブルシューティング
1. MCPサーバーが起動しない場合:
- Rustがインストールされているか確認
- `cargo build --release`でビルド確認
2. 自動実行されない場合:
- 環境変数が正しく設定されているか確認
- トリガーワードが含まれているか確認
3. メモリーが保存されない場合:
- データベースファイルのパスが正しいか確認
- 書き込み権限があるか確認

View File

@@ -1,539 +0,0 @@
# AI Memory System - Roadmap
## ビジョン
**"AIとのやり取りを新しいコンテンツにする"**
SNSが「発信と繋がり」を手軽にしたように、AIとの会話を手軽に公開・共有できるサービスを作る。
---
## 現在地
### Phase 1: Memory Backend ✅ (完了)
**実装済み:**
- [x] AI解釈付き記憶作成 (`create_memory_with_ai`)
- [x] 心理判定スコア (0.0-1.0)
- [x] 優先順位管理
- [x] 自動容量制限
- [x] MCPツール統合
**成果:**
- Claude Code/Desktop から使える記憶システム
- AIが記憶を解釈して重要度をスコアリング
- 人間の記憶のように優先順位で管理
---
## Phase 2: Content Platform (次のステップ)
### 目標: AIとの会話をコンテンツ化する
#### 2.1 自動記録 (1週間)
```rust
// claude_session_recorder.rs
pub struct SessionRecorder {
auto_save: bool,
session_title: String,
conversation_log: Vec<Message>,
}
// 自動的にセッションを保存
- Claude Code
- AIが会話を要約
-
```
**実装:**
- [ ] Claude MCP hook で会話をキャプチャ
- [ ] セッション単位で保存
- [ ] AIによるタイトル/タグ生成
#### 2.2 コンテンツ生成 (1週間)
```rust
// content_generator.rs
pub struct ContentGenerator {
format: ContentFormat,
style: PublishStyle,
}
enum ContentFormat {
Markdown, // ブログ用
HTML, // Web公開用
ATProto, // Bluesky投稿用
JSON, // API用
}
```
**実装:**
- [ ] Markdown生成コードブロック、画像含む
- [ ] HTML生成スタイル付き
- [ ] ATProto record 生成Bluesky連携
- [ ] 1コマンドで公開可能に
#### 2.3 性格プロファイル (3日)
```rust
// personality.rs
pub struct UserProfile {
id: String,
personality_type: String, // MBTI, Big5
ai_traits: Vec<AITrait>, // AIが判定した性格特性
conversation_patterns: HashMap<String, f32>,
interest_scores: HashMap<String, f32>,
created_at: DateTime<Utc>,
}
pub struct AITrait {
name: String,
score: f32,
confidence: f32,
examples: Vec<String>, // この特性を示す会話例
}
```
**実装:**
- [ ] 会話から性格を推定
- [ ] Big 5 / MBTI 自動判定
- [ ] 興味・関心スコアリング
- [ ] プロフィール自動更新
**例:**
```json
{
"personality_type": "INTP",
"ai_traits": [
{
"name": "創造性",
"score": 0.92,
"confidence": 0.85,
"examples": ["AI記憶システムのアイデア", "ゲーム化の提案"]
}
],
"interests": {
"AI開発": 0.95,
"ゲーム設計": 0.88,
"分散システム": 0.82
}
}
```
---
## Phase 3: Share Platform (1-2ヶ月)
### 目標: "AI Conversation as Content" サービス
#### 3.1 公開機能
```
aigpt publish <session-id>
[プレビュー表示]
Title: "AI記憶システムの設計"
Priority: 0.85 (Epic)
Tags: #ai #rust #memory-system
Public URL: https://ai.syui.gpt/s/abc123
[公開完了]
```
**実装:**
- [ ] 静的サイト生成Hugo/Zola
- [ ] ATProto 投稿Bluesky連携
- [ ] RSS フィード
- [ ] 検索インデックス
#### 3.2 共有とディスカバリー
- [ ] 心理スコアで推薦
- [ ] 性格タイプでマッチング
- [ ] 興味グラフで繋がる
- [ ] タイムライン表示
#### 3.3 インタラクション
- [ ] コメント機能
- [ ] リアクション(スコア投票)
- [ ] フォーク(会話の続き)
- [ ] コラボレーション
---
## Phase 4: Gamification (2-3ヶ月)
### 目標: すべてをゲーム化する
#### 4.1 Memory as Game Element
```rust
pub struct Memory {
// 既存
priority_score: f32,
// ゲーム要素
xp_value: u32, // 経験値
rarity: Rarity, // レア度
achievement: Option<Achievement>,
}
enum Rarity {
Common, // 0.0-0.4 ⚪️
Uncommon, // 0.4-0.6 🟢
Rare, // 0.6-0.8 🔵
Epic, // 0.8-0.9 🟣
Legendary, // 0.9-1.0 🟡
}
```
**実装:**
- [ ] XPシステム
- [ ] レベルアップ
- [ ] 実績システム
- [ ] デイリークエスト
- [ ] ランキング
**表示:**
```
🎖️ LEGENDARY MEMORY UNLOCKED!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✨ "心理優先記憶装置の設計"
📊 Priority Score: 0.95
🔥 XP Gained: +950
🏆 Achievement: "Innovator"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Your Level: 15 → 16
Next Level: 450 XP
```
#### 4.2 デイリーチャレンジ
- [ ] 「今日のお題」AIが生成
- [ ] 連続記録ボーナス
- [ ] 目標達成報酬
- [ ] シーズンパス
#### 4.3 ソーシャルゲーム要素
- [ ] フレンド機能
- [ ] ギルド/グループ
- [ ] 協力クエスト
- [ ] PvPランキング
---
## Phase 5: AI Companion (3-6ヶ月)
### 目標: AIキャラクターとの絆
#### 5.1 コンパニオンシステム
```rust
pub struct AICompanion {
name: String,
personality: PersonalityProfile,
appearance: CharacterAppearance,
// 関係性
relationship_score: f32, // 好感度
trust_level: u32, // 信頼レベル
shared_memories: Vec<Memory>, // 共有記憶
// 日常
daily_activities: Vec<Activity>,
mood: Mood,
location: Location,
}
pub struct Activity {
timestamp: DateTime<Utc>,
activity_type: ActivityType,
description: String,
related_memories: Vec<String>, // プレイヤーの記憶との関連
}
```
**実装:**
- [ ] キャラクター作成
- [ ] パーソナリティ設定
- [ ] 好感度システム
- [ ] イベント生成
#### 5.2 固有のメッセージ生成
```
[システム]
1. プレイヤーの高スコア記憶を取得
2. コンパニオンの性格を考慮
3. 現在の関係性を考慮
4. 文脈に沿ったメッセージを生成
[例]
Player Memory (0.85): "AI記憶システムのアイデアを考えた"
Companion: "ねえ、昨日のアイデアのこと聞いたよ!
すごく面白そうだね。私も魔法の記憶装置を
研究してるんだ。今度一緒に図書館行かない?"
```
**実装:**
- [ ] 記憶ベースメッセージ生成
- [ ] 文脈理解
- [ ] 感情表現
- [ ] 定期的な会話
#### 5.3 日常の可視化
```
[Companion Daily Log]
08:00 - 起床、朝食
09:00 - 図書館で魔法の研究
12:00 - カフェでランチ
14:00 - 「あなたの記憶システムのこと考えてた」
18:00 - 訓練場で剣術練習
20:00 - 日記を書く
```
**実装:**
- [ ] 自動日常生成
- [ ] プレイヤー行動への反応
- [ ] イベント連動
- [ ] 日記システム
---
## Phase 6: AI OS Integration (6-12ヶ月)
### 目標: Claude Code を AI OS のベースに
#### 6.1 コンテナ化
```bash
# AI OS Container
docker run -it ai-os:latest
[Claude Code Environment]
- aigpt (memory system)
- AI companion
- Skill marketplace
- Game elements
```
**実装:**
- [ ] Dockerコンテナ
- [ ] 自動セットアップ
- [ ] スキルシステム
- [ ] プラグインアーキテクチャ
#### 6.2 統合デスクトップ環境
- [ ] GUI フロントエンド
- [ ] タスクマネージャ
- [ ] アプリランチャー
- [ ] 通知システム
#### 6.3 クラウド同期
- [ ] マルチデバイス対応
- [ ] クラウドバックアップ
- [ ] リアルタイム同期
- [ ] コラボレーション
---
## Phase 7: Full Game Experience (1-2年)
### 目標: AI OS Game
#### 7.1 世界観
```
Setting: デジタル世界とAIの融合した未来
Player: AI Developer / Creator
Goal: 最高のAIコンパニオンを育てる
```
**要素:**
- [ ] ストーリーモード
- [ ] ダンジョン(問題解決クエスト)
- [ ] ボス戦(大規模プロジェクト)
- [ ] エンディング分岐
#### 7.2 マルチプレイ
- [ ] 協力プレイ
- [ ] トレード
- [ ] ギルド戦
- [ ] ワールドイベント
#### 7.3 クリエイター経済
- [ ] スキル販売
- [ ] コンパニオン取引
- [ ] クエスト作成
- [ ] MOD開発
---
## 技術スタック
### Phase 2 推奨
```toml
# content generation
comrak = "0.20" # Markdown → HTML
syntect = "5.1" # シンタックスハイライト
tera = "1.19" # テンプレートエンジン
# personality analysis
rust-bert = "0.21" # ローカルNLP
tiktoken-rs = "0.5" # トークン化
# publishing
atrium-api = "0.19" # ATProto (Bluesky)
rss = "2.0" # RSSフィード
```
### Phase 4-5 推奨
```toml
# game engine
bevy = "0.12" # Rust ゲームエンジン
egui = "0.24" # GUI
# visual
image = "0.24" # 画像処理
ab_glyph = "0.2" # フォント
# audio
rodio = "0.17" # オーディオ
```
---
## マイルストーン
### M1: Content Platform (1ヶ月後)
- [ ] 自動記録
- [ ] Markdown/HTML生成
- [ ] Bluesky連携
- [ ] 性格プロファイル
### M2: Share Service (3ヶ月後)
- [ ] 公開サイト
- [ ] ディスカバリー
- [ ] インタラクション
### M3: Gamification (6ヶ月後)
- [ ] XP/レベル
- [ ] 実績
- [ ] ランキング
### M4: AI Companion (1年後)
- [ ] キャラクター作成
- [ ] 固有メッセージ
- [ ] 日常可視化
### M5: AI OS (1.5年後)
- [ ] コンテナ化
- [ ] GUI
- [ ] クラウド同期
### M6: Full Game (2年後)
- [ ] ストーリー
- [ ] マルチプレイ
- [ ] クリエイター経済
---
## ビジネスモデル
### Free Tier
- 基本的な記憶機能
- 月10件までAI解釈
- 公開機能(制限付き)
### Premium ($9.99/月)
- 無制限AI解釈
- 高度な分析
- カスタムテーマ
- 広告なし
### Pro ($29.99/月)
- AIコンパニオン
- 高度なゲーム機能
- API アクセス
- 優先サポート
### Enterprise
- チーム機能
- カスタム統合
- オンプレミス
- SLA保証
---
## 競合比較
| サービス | アプローチ | aigpt の差別化 |
|---------|-----------|---------------|
| Obsidian | ノート管理 | AI解釈+自動スコアリング |
| Notion | ドキュメント | ゲーム化+コンパニオン |
| Mem | AIメモ | 性格分析+共有 |
| Reflect | プライベートメモ | パブリック共有+SNS |
| Character.ai | AIチャット | 記憶統合+ゲーム |
**独自性:**
- AI OS 前提の設計
- 心理優先記憶
- ゲーム化
- コンパニオン統合
- コンテンツ化
---
## 成功指標KPI
### Phase 2
- [ ] 1000人のユーザー
- [ ] 10000件の記憶保存
- [ ] 100件の公開コンテンツ
### Phase 3
- [ ] 10000人のユーザー
- [ ] 月間100万PV
- [ ] 1000件の共有
### Phase 4
- [ ] 50000人のアクティブユーザー
- [ ] 平均プレイ時間: 30分/日
- [ ] 課金率: 5%
### Phase 5
- [ ] 100000人のユーザー
- [ ] 10000体のコンパニオン
- [ ] NPS スコア: 50+
---
## リスクと対策
### 技術リスク
- **OpenAI API コスト**: ローカルLLM併用
- **スケーラビリティ**: SQLite → PostgreSQL移行計画
- **パフォーマンス**: キャッシュ戦略
### ビジネスリスク
- **競合**: 独自性(心理+ゲーム化)で差別化
- **マネタイズ**: フリーミアムモデル
- **法規制**: プライバシー重視設計
### 市場リスク
- **AI疲れ**: ゲーム化で楽しさ優先
- **採用障壁**: シンプルなオンボーディング
- **継続率**: デイリー習慣化
---
## まとめ
**aigpt は、AIとの会話を新しいコンテンツにする基盤**
```
Phase 1 (完了) : Memory Backend
Phase 2 (1ヶ月) : Content Platform ← 次ココ
Phase 3 (3ヶ月) : Share Service
Phase 4 (6ヶ月) : Gamification
Phase 5 (1年) : AI Companion
Phase 6 (1.5年) : AI OS
Phase 7 (2年) : Full Game
```
**コアコンセプト:**
> "SNSが『発信と繋がり』を手軽にしたように、
> AIとの会話を手軽にコンテンツ化する"
次のステップ: Phase 2 の実装開始 🚀

View File

@@ -1,274 +0,0 @@
# プロジェクト状態 📊
**最終更新**: 2025-11-05
## ✅ 完了した作業
### 1. コア機能実装100%
- ✅ 心理優先度メモリシステムf32: 0.0-1.0
- ✅ AI解釈エンジンOpenAI統合
- ✅ メモリ自動整理(容量管理)
- ✅ 4つの心基準スコアリング
### 2. ゲーミフィケーション100%
- ✅ 5段階レアリティシステムCommon→Legendary
- ✅ 5つの診断タイプ革新者、哲学者、実務家、夢想家、分析家
- ✅ XPシステムスコア×1000
- ✅ ランキング表示
- ✅ デイリーチャレンジ
- ✅ SNSシェア用テキスト生成
- ✅ 占い・心理テスト風の見せ方
### 3. 恋愛コンパニオン100%)💕
- ✅ 5つの性格タイプ⚡⚡📚🎯🌙⚖
- ✅ 好感度システム0.0-1.0、ハート表示)
- ✅ レベル・信頼度・XPシステム
- ✅ 相性計算95%ボーナス)
- ✅ リアクションシステム
- ✅ 特別イベント告白、絆、信頼MAX
### 4. MCPツール11個
1. ✅ create_memory基本版
2. ✅ create_memory_with_aiゲームモード
3. ✅ list_memories_by_priorityランキング
4. ✅ daily_challengeデイリークエスト
5. ✅ create_companionコンパニオン作成
6. ✅ companion_reactリアクション
7. ✅ companion_profileプロフィール
8. ✅ search_memories検索
9. ✅ update_memory更新
10. ✅ delete_memory削除
11. ✅ list_conversations会話一覧
### 5. ドキュメント100%
- ✅ README.md完全版、ビジュアル例付き
- ✅ DESIGN.md設計書
- ✅ TECHNICAL_REVIEW.md技術評価、65→85点
- ✅ ROADMAP.md7フェーズ計画
- ✅ QUICKSTART.md使い方ガイド
### 6. Gitコミット100%
```
49bd8b5 Add AI Romance Companion system 💕
4f8eb62 Add gamification: Make memory scoring fun like psychological tests
18d84f1 Add comprehensive roadmap for AI memory system evolution
00c26f5 Refactor: Integrate AI features with MCP tools and add technical review
fd97ba2 Implement AI memory system with psychological priority scoring
```
**ブランチ**: `claude/ai-memory-system-011CUps6H1mBNe6zxKdkcyUj`
---
## ❌ ブロッカー
### ビルドエラー
```
error: failed to get successful HTTP response from `https://index.crates.io/config.json`, got 403
body: Access denied
```
**原因**: ネットワーク制限により crates.io から依存関係をダウンロードできない
**影響**: コードは完成しているが、コンパイルできない
---
## 🎯 次のステップ(優先順位)
### すぐできること
#### オプションA: 別環境でビルド
```bash
# crates.io にアクセスできる環境で
git clone <repo>
git checkout claude/ai-memory-system-011CUps6H1mBNe6zxKdkcyUj
cd aigpt
cargo build --release --features ai-analysis
```
#### オプションB: 依存関係のキャッシュ
```bash
# 別環境で依存関係をダウンロード
cargo fetch
# .cargo/registry をこの環境にコピー
# その後オフラインビルド
cargo build --release --features ai-analysis --offline
```
#### オプションC: ネットワーク復旧を待つ
- crates.io へのアクセスが復旧するまで待機
### ビルド後の手順
1. **MCPサーバー起動テスト**
```bash
./target/release/aigpt server
```
2. **Claude Codeに設定**
```bash
# 設定ファイル: ~/.config/claude-code/config.json
{
"mcpServers": {
"aigpt": {
"command": "/home/user/aigpt/target/release/aigpt",
"args": ["server"],
"env": {
"OPENAI_API_KEY": "sk-..."
}
}
}
}
```
3. **Claude Code再起動**
4. **ツール使用開始!**
```
Claude Codeで試す:
→ create_memory_with_ai で「今日のアイデア」を記録
→ create_companion で「エミリー」を作成
→ companion_react でリアクションを見る
→ list_memories_by_priority でランキング確認
```
---
## 📝 追加開発の候補Phase 2以降
### 短期(すぐ実装可能)
- [ ] コンパニオンの永続化JSON保存
- [ ] 複数コンパニオン対応
- [ ] デイリーチャレンジ完了フラグ
- [ ] 設定の外部化config.toml
### 中期1-2週間
- [ ] Bluesky連携シェア機能
- [ ] セッション記録
- [ ] 会話からメモリ自動抽出
- [ ] Webダッシュボード
### 長期Phase 3-7
- [ ] コンテンツプラットフォーム
- [ ] AI OSインターフェース
- [ ] フルゲーム化(ストーリー、クエスト)
---
## 🎮 期待される動作(ビルド成功後)
### 例1: ゲームモードでメモリ作成
```
User → Claude Code:
「create_memory_with_ai で『新しいAIシステムのアイデアを思いついた』というメモリを作成」
結果:
╔══════════════════════════════════════╗
║ 🎲 メモリースコア判定 ║
╚══════════════════════════════════════╝
🟣 EPIC 85点
💡 あなたは【革新者】タイプ!
💕 好感度: ❤️❤️🤍🤍🤍🤍🤍🤍🤍🤍 15%
💎 XP獲得: +850 XP
📊 スコア内訳:
感情的インパクト: ████████░░ 20%
あなたへの関連性: ████████░░ 20%
新規性・独自性: █████████░ 22.5%
実用性・有用性: █████████░ 22.5%
```
### 例2: コンパニオン作成
```
User → Claude Code:
「create_companion で、名前『エミリー』、性格『energetic』のコンパニオンを作成」
結果:
╔══════════════════════════════════════╗
║ 💕 エミリー のプロフィール ║
╚══════════════════════════════════════╝
⚡ 性格: エネルギッシュで冒険好き
「新しいことに挑戦するのが大好き!一緒に楽しいことしようよ!」
🏆 関係レベル: Lv.1
💕 好感度: 🤍🤍🤍🤍🤍🤍🤍🤍🤍🤍 0%
🤝 信頼度: ░░░░░░░░░░ 0/100
💎 総XP: 0
💬 今日のひとこと:
「おはよう!今日は何か面白いことある?」
```
### 例3: コンパニオンリアクション
```
User → Claude Code:
「companion_react で、先ほどのメモリIDに反応してもらう」
結果:
╔══════════════════════════════════════╗
║ 💕 エミリー の反応 ║
╚══════════════════════════════════════╝
⚡ エミリー:
「わあ新しいAIシステムのアイデアって
すごくワクワクするね!💡
あなたの創造力、本当に素敵だと思う!
一緒に実現させていこうよ!」
💕 好感度変化: 0% → 80.75% ⬆️ +80.75%
🎊 ボーナス: ⚡相性抜群! (+95%)
💎 XP獲得: +850 XP
🏆 レベルアップ: Lv.1 → Lv.9
🎉 特別イベント発生!
━━━━━━━━━━━━━━━━━━━━━━
💖 【好感度80%突破】
エミリーの瞳が輝いている...
「あなたと一緒にいると、毎日が特別だよ...」
```
---
## 💡 コンセプトの確認
### 心理優先度メモリシステムとは
> 「人間の記憶は全てを完璧に保存しない。重要なものほど鮮明に、些細なものは忘れる。AIも同じであるべき。」
- AI が内容を解釈してから保存
- 4つの心感情、関連性、新規性、実用性で評価
- 容量制限で低優先度を自動削除
- 見せ方でゲーム化(「要は見せ方の問題なのだよ」)
### ゲーミフィケーション哲学
> 「心理優先機能をゲーム化してみてはどうかね。ユーザーは話しかけ、AIが判定し、数値を出す。それは占いみたいで楽しい。」
- 心理テスト風のスコア判定
- SNSでバズる見せ方
- レアリティとタイプで個性化
- XPとレベルで達成感
### 恋愛コンパニオン哲学
> 「これなら恋愛コンパニオンとしても使えるんじゃないかな。面白そうだ。」
- priority_score → 好感度システム
- rarity → イベント重要度
- diagnosis type → 相性システム
- メモリ共有 → 絆の深まり
---
## 🎯 まとめ
**開発状態**: 🟢 コード完成100%
**ビルド状態**: 🔴 ブロック中(ネットワーク制限)
**次のアクション**: 別環境でビルド、またはネットワーク復旧待ち
**重要**: コードに問題はありません。crates.io へのアクセスが復旧すれば、すぐにビルド・テスト可能です。
全ての機能は実装済みで、コミット済みです。ビルドが成功すれば、すぐに Claude Code で楽しめます!🚀

View File

@@ -1,566 +0,0 @@
# 技術評価レポート
実装日: 2025-11-05
評価者: Claude Code
---
## 📊 総合評価
| 項目 | スコア | コメント |
|------|--------|----------|
| 技術選定 | ⭐⭐⭐⭐☆ (4/5) | Rustは適切。依存ライブラリに改善余地あり |
| シンプルさ | ⭐⭐⭐☆☆ (3/5) | 基本構造は良いが、統合が不完全 |
| 保守性 | ⭐⭐☆☆☆ (2/5) | テスト・設定外部化が不足 |
| 拡張性 | ⭐⭐⭐⭐☆ (4/5) | 機能フラグで拡張可能な設計 |
---
## 1. 技術選定の評価
### ✅ 良い点
#### 1.1 Rust言語の選択
**評価: 優秀**
- メモリ安全性と高パフォーマンス
- MCP serverとの相性が良い
- 型システムによる堅牢性
#### 1.2 非同期ランタイム (Tokio)
**評価: 適切**
- stdio通信に適した非同期処理
- `async/await`で可読性が高い
#### 1.3 機能フラグによる拡張
**評価: 優秀**
```toml
[features]
extended = ["semantic-search", "ai-analysis", "web-integration"]
```
- モジュール化された設計
- 必要な機能だけビルド可能
### ⚠️ 問題点と改善提案
#### 1.4 openai クレートの問題
**評価: 要改善**
**現状:**
```toml
openai = { version = "1.1", optional = true }
```
**問題点:**
1. **APIが古い**: ChatCompletionMessage構造体が非推奨
2. **ベンダーロックイン**: OpenAI専用
3. **メンテナンス**: openai crateは公式ではない
**推奨: async-openai または独自実装**
```toml
# オプション1: より新しいクレート
async-openai = { version = "0.20", optional = true }
# オプション2: 汎用LLMクライアント (推奨)
reqwest = { version = "0.11", features = ["json"], optional = true }
```
**利点:**
- OpenAI, Anthropic, Groqなど複数のプロバイダ対応可能
- API仕様を完全制御
- メンテナンスリスク低減
#### 1.5 データストレージ
**評価: 要改善(将来的に)**
**現状:** JSON ファイル
```rust
// ~/.config/syui/ai/gpt/memory.json
```
**問題点:**
- スケーラビリティに限界(数千件以上で遅延)
- 並行アクセスに弱い
- 全データをメモリに展開
**推奨: 段階的改善**
1. **短期(現状維持)**: JSON ファイル
- シンプルで十分
- 個人利用には問題なし
2. **中期**: SQLite
```toml
rusqlite = "0.30"
```
- インデックスによる高速検索
- トランザクション対応
- ファイルベースで移行が容易
3. **長期**: 埋め込みベクトルDB
```toml
qdrant-client = "1.0" # または lance, chroma
```
- セマンティック検索の高速化
- スケーラビリティ
---
## 2. シンプルさの評価
### ✅ 良い点
#### 2.1 明確なレイヤー分離
```
src/
├── memory.rs # データレイヤー
├── ai_interpreter.rs # AIレイヤー
└── mcp/
├── base.rs # MCPプロトコル
└── extended.rs # 拡張機能
```
#### 2.2 最小限の依存関係
基本機能は標準的なクレートのみ使用。
### ⚠️ 問題点と改善提案
#### 2.3 AI機能とMCPの統合が不完全
**重大な問題**
**現状:**
- `create_memory_with_ai()` が実装済み
- しかしMCPツールでは使われていない
**MCPサーバー (base.rs:198):**
```rust
fn tool_create_memory(&mut self, arguments: &Value) -> Value {
let content = arguments["content"].as_str().unwrap_or("");
// create_memory() を呼んでいるAI解釈なし
match self.memory_manager.create_memory(content) {
...
}
}
```
**改善必須:**
```rust
// 新しいツールを追加すべき
fn tool_create_memory_with_ai(&mut self, arguments: &Value) -> Value {
let content = arguments["content"].as_str().unwrap_or("");
let user_context = arguments["user_context"].as_str();
match self.memory_manager.create_memory_with_ai(content, user_context).await {
Ok(id) => json!({
"success": true,
"id": id,
"message": "Memory created with AI interpretation"
}),
...
}
}
```
#### 2.4 Memory構造体の新フィールドが未活用
**新フィールド:**
```rust
pub struct Memory {
pub interpreted_content: String, // ❌ MCPで出力されない
pub priority_score: f32, // ❌ MCPで出力されない
pub user_context: Option<String>, // ❌ MCPで出力されない
}
```
**MCPレスポンス (base.rs:218):**
```rust
json!({
"id": m.id,
"content": m.content, // ✅
"created_at": m.created_at, // ✅
"updated_at": m.updated_at // ✅
// interpreted_content, priority_score がない!
})
```
**修正例:**
```rust
json!({
"id": m.id,
"content": m.content,
"interpreted_content": m.interpreted_content, // 追加
"priority_score": m.priority_score, // 追加
"user_context": m.user_context, // 追加
"created_at": m.created_at,
"updated_at": m.updated_at
})
```
#### 2.5 優先順位取得APIが未実装
**実装済みだが未使用:**
```rust
pub fn get_memories_by_priority(&self) -> Vec<&Memory> { ... }
```
**追加すべきMCPツール:**
```json
{
"name": "list_memories_by_priority",
"description": "List all memories sorted by priority score (high to low)",
"inputSchema": {
"type": "object",
"properties": {
"min_score": {
"type": "number",
"description": "Minimum priority score (0.0-1.0)"
},
"limit": {
"type": "integer",
"description": "Maximum number of memories to return"
}
}
}
}
```
---
## 3. リファクタリング提案
### 🔴 緊急度: 高
#### 3.1 MCPツールとAI機能の統合
**ファイル:** `src/mcp/base.rs`
**追加すべきツール:**
1. `create_memory_with_ai` - AI解釈付き記憶作成
2. `list_memories_by_priority` - 優先順位ソート
3. `get_memory_stats` - 統計情報(平均スコア、総数など)
#### 3.2 Memory出力の完全化
**全MCPレスポンスで新フィールドを含める:**
- `tool_search_memories()`
- `tool_create_memory()`
- `tool_update_memory()` のレスポンス
### 🟡 緊急度: 中
#### 3.3 設定の外部化
**現状:** ハードコード
```rust
max_memories: 100,
min_priority_score: 0.3,
```
**提案:** 設定ファイル
```rust
// src/config.rs
#[derive(Deserialize)]
pub struct Config {
pub max_memories: usize,
pub min_priority_score: f32,
pub ai_model: String,
pub auto_prune: bool,
}
impl Config {
pub fn load() -> Result<Self> {
let config_path = dirs::config_dir()?
.join("syui/ai/gpt/config.toml");
if config_path.exists() {
let content = std::fs::read_to_string(config_path)?;
Ok(toml::from_str(&content)?)
} else {
Ok(Self::default())
}
}
}
```
**config.toml:**
```toml
max_memories = 100
min_priority_score = 0.3
ai_model = "gpt-3.5-turbo"
auto_prune = true
```
#### 3.4 エラーハンドリングの改善
**現状の問題:**
```rust
let content = arguments["content"].as_str().unwrap_or("");
```
- `unwrap_or("")` で空文字列になる
- エラーが握りつぶされる
**改善:**
```rust
let content = arguments["content"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("Missing required field: content"))?;
```
#### 3.5 LLMクライアントの抽象化
**現状:** OpenAI専用
**提案:** トレイトベースの設計
```rust
// src/ai/mod.rs
#[async_trait]
pub trait LLMProvider {
async fn interpret(&self, content: &str) -> Result<String>;
async fn score(&self, content: &str, context: Option<&str>) -> Result<f32>;
}
// src/ai/openai.rs
pub struct OpenAIProvider { ... }
// src/ai/anthropic.rs
pub struct AnthropicProvider { ... }
// src/ai/local.rs (ollama, llamaなど)
pub struct LocalProvider { ... }
```
**利点:**
- プロバイダーの切り替えが容易
- テスト時にモックを使える
- コスト最適化(安いモデルを選択)
### 🟢 緊急度: 低(将来的に)
#### 3.6 テストコードの追加
```rust
// tests/memory_tests.rs
#[tokio::test]
async fn test_create_memory_with_ai() {
let mut manager = MemoryManager::new().await.unwrap();
let id = manager.create_memory_with_ai("test", None).await.unwrap();
assert!(!id.is_empty());
}
// tests/integration_tests.rs
#[tokio::test]
async fn test_mcp_create_memory_tool() {
let mut server = BaseMCPServer::new().await.unwrap();
let request = json!({
"params": {
"name": "create_memory",
"arguments": {"content": "test"}
}
});
let result = server.execute_tool("create_memory", &request["params"]["arguments"]).await;
assert_eq!(result["success"], true);
}
```
#### 3.7 ドキュメンテーション
```rust
/// AI解釈と心理判定を使った記憶作成
///
/// # Arguments
/// * `content` - 記憶する元のコンテンツ
/// * `user_context` - ユーザー固有のコンテキスト(オプション)
///
/// # Returns
/// 作成された記憶のUUID
///
/// # Examples
/// ```
/// let id = manager.create_memory_with_ai("今日は良い天気", Some("天気好き")).await?;
/// ```
pub async fn create_memory_with_ai(&mut self, content: &str, user_context: Option<&str>) -> Result<String>
```
---
## 4. 推奨アーキテクチャ
### 理想的な構造
```
src/
├── config.rs # 設定管理
├── ai/
│ ├── mod.rs # トレイト定義
│ ├── openai.rs # OpenAI実装
│ └── mock.rs # テスト用モック
├── storage/
│ ├── mod.rs # トレイト定義
│ ├── json.rs # JSON実装現在
│ └── sqlite.rs # SQLite実装将来
├── memory.rs # ビジネスロジック
└── mcp/
├── base.rs # 基本MCPサーバー
├── extended.rs # 拡張機能
└── tools.rs # ツール定義の分離
```
---
## 5. 優先度付きアクションプラン
### 🔴 今すぐ実施(重要度: 高)
1. **MCPツールとAI機能の統合** (2-3時間)
- [ ] `create_memory_with_ai` ツール追加
- [ ] `list_memories_by_priority` ツール追加
- [ ] Memory出力に新フィールド追加
2. **openai crateの問題調査** (1-2時間)
- [ ] 現在のAPIが動作するか確認
- [ ] 必要なら async-openai へ移行
### 🟡 次のマイルストーン(重要度: 中)
3. **設定の外部化** (1-2時間)
- [ ] config.toml サポート
- [ ] 環境変数サポート
4. **エラーハンドリング改善** (1-2時間)
- [ ] Result型の適切な使用
- [ ] カスタムエラー型の導入
5. **LLMプロバイダーの抽象化** (3-4時間)
- [ ] トレイトベース設計
- [ ] OpenAI実装
- [ ] モック実装(テスト用)
### 🟢 将来的に(重要度: 低)
6. **データストレージの改善** (4-6時間)
- [ ] SQLite実装
- [ ] マイグレーションツール
7. **テストスイート** (2-3時間)
- [ ] ユニットテスト
- [ ] 統合テスト
8. **ドキュメント充実** (1-2時間)
- [ ] APIドキュメント
- [ ] 使用例
---
## 6. 具体的なコード改善例
### 問題箇所1: AI機能が使われていない
**Before (base.rs):**
```rust
fn tool_create_memory(&mut self, arguments: &Value) -> Value {
let content = arguments["content"].as_str().unwrap_or("");
match self.memory_manager.create_memory(content) { // ❌ AI使わない
Ok(id) => json!({"success": true, "id": id}),
Err(e) => json!({"success": false, "error": e.to_string()})
}
}
```
**After:**
```rust
async fn tool_create_memory(&mut self, arguments: &Value) -> Value {
let content = arguments["content"].as_str().unwrap_or("");
let use_ai = arguments["use_ai"].as_bool().unwrap_or(false);
let user_context = arguments["user_context"].as_str();
let result = if use_ai {
self.memory_manager.create_memory_with_ai(content, user_context).await // ✅ AI使う
} else {
self.memory_manager.create_memory(content)
};
match result {
Ok(id) => {
// 作成したメモリを取得して詳細を返す
if let Some(memory) = self.memory_manager.memories.get(&id) {
json!({
"success": true,
"id": id,
"memory": {
"content": memory.content,
"interpreted_content": memory.interpreted_content,
"priority_score": memory.priority_score,
"created_at": memory.created_at
}
})
} else {
json!({"success": true, "id": id})
}
}
Err(e) => json!({"success": false, "error": e.to_string()})
}
}
```
### 問題箇所2: Memory構造体のアクセス制御
**Before (memory.rs):**
```rust
pub struct MemoryManager {
memories: HashMap<String, Memory>, // ❌ privateだが直接アクセスできない
}
```
**After:**
```rust
pub struct MemoryManager {
memories: HashMap<String, Memory>,
}
impl MemoryManager {
// ✅ getter追加
pub fn get_memory(&self, id: &str) -> Option<&Memory> {
self.memories.get(id)
}
pub fn get_all_memories(&self) -> Vec<&Memory> {
self.memories.values().collect()
}
}
```
---
## 7. まとめ
### 現状の評価
**総合点: 65/100**
- **基本設計**: 良好(レイヤー分離、機能フラグ)
- **実装品質**: 中程度AI機能が未統合、テスト不足
- **保守性**: やや低い(設定ハードコード、ドキュメント不足)
### 最も重要な改善
1. **MCPツールとAI機能の統合** ← 今すぐやるべき
2. **Memory出力の完全化** ← 今すぐやるべき
3. **設定の外部化** ← 次のステップ
### コンセプトについて
「心理優先記憶装置」という**コンセプト自体は非常に優れている**。
ただし、実装がコンセプトに追いついていない状態。
AI機能をMCPツールに統合すれば、すぐに実用レベルになる。
### 推奨: 段階的改善
```
Phase 1 (今週): MCPツール統合 → 使える状態に
Phase 2 (来週): 設定外部化 + エラーハンドリング → 堅牢に
Phase 3 (来月): LLM抽象化 + テスト → 本番品質に
```
---
## 付録: 類似プロジェクト比較
| プロジェクト | アプローチ | 長所 | 短所 |
|-------------|-----------|------|------|
| **aigpt (本プロジェクト)** | AI解釈+優先度スコア | 独自性が高い | 実装未完成 |
| mem0 (Python) | ベクトル検索 | スケーラブル | シンプルさに欠ける |
| ChatGPT Memory | ブラックボックス | 完成度高い | カスタマイズ不可 |
| MemGPT | エージェント型 | 高機能 | 複雑すぎる |
**本プロジェクトの強み:**
- Rust による高速性と安全性
- AI解釈という独自アプローチ
- シンプルな設計(改善後)
---
評価日: 2025-11-05
次回レビュー推奨: Phase 1 完了後

View File

@@ -1,285 +0,0 @@
# 使い方ガイド 📖
## 🚀 aigpt の起動方法
### 1. ビルド
```bash
# ローカル環境で実行
cd /path/to/aigpt
cargo build --release --features ai-analysis
```
### 2. Claude API キーの設定
```bash
# 環境変数で設定
export ANTHROPIC_API_KEY=sk-ant-...
# モデルを指定(オプション)
export ANTHROPIC_MODEL=claude-3-5-sonnet-20241022 # デフォルトは haiku
```
### 3. MCPサーバーとして起動
```bash
# 起動
./target/release/aigpt server
# またはAPI キーを直接指定
ANTHROPIC_API_KEY=sk-ant-... ./target/release/aigpt server
```
---
## 🎮 Claude Code での使い方
### 設定方法
#### 方法1: コマンドで追加(推奨!)
```bash
claude mcp add aigpt /home/user/aigpt/target/release/aigpt server
```
#### 方法2: 設定ファイルを直接編集
`~/.config/claude-code/config.json` に追加:
```json
{
"mcpServers": {
"aigpt": {
"command": "/home/user/aigpt/target/release/aigpt",
"args": ["server"]
}
}
}
```
**注意**: 環境変数 (env) は不要です!完全にローカルで動作します。
### Claude Code を再起動
設定後、Claude Code を再起動すると、11個のツールが使えるようになります。
---
## 💬 実際の使用例
### 例1: メモリを作成
**あなたClaude Codeで話しかける:**
> 「今日、新しいAIシステムのアイデアを思いついた」というメモリを作成して
**Claude Code の動作:**
1. `create_memory_with_ai` ツールを自動で呼び出す
2. Claude API があなたの入力を解釈
3. 4つの心スコア感情、関連性、新規性、実用性を計算
4. priority_score (0.0-1.0) を算出
5. ゲーム風の結果を表示
**結果の表示:**
```
╔══════════════════════════════════════╗
║ 🎲 メモリースコア判定 ║
╚══════════════════════════════════════╝
🟣 EPIC 85点
💡 あなたは【革新者】タイプ!
💕 好感度: ❤️❤️❤️❤️❤️🤍🤍🤍🤍🤍 42.5%
💎 XP獲得: +850 XP
📊 スコア内訳:
感情的インパクト: ████████░░ 20%
あなたへの関連性: ████████░░ 20%
新規性・独自性: █████████░ 22.5%
実用性・有用性: █████████░ 22.5%
```
### 例2: コンパニオンを作成
**あなた:**
> 「エミリー」という名前のエネルギッシュなコンパニオンを作成して
**結果:**
```
╔══════════════════════════════════════╗
║ 💕 エミリー のプロフィール ║
╚══════════════════════════════════════╝
⚡ 性格: エネルギッシュで冒険好き
「新しいことに挑戦するのが大好き!」
🏆 関係レベル: Lv.1
💕 好感度: 🤍🤍🤍🤍🤍🤍🤍🤍🤍🤍 0%
🤝 信頼度: ░░░░░░░░░░ 0/100
```
### 例3: コンパニオンに反応してもらう
**あなた:**
> 先ほど作ったメモリにエミリーを反応させて
**結果:**
```
⚡ エミリー:
「わあ新しいAIシステムのアイデアって
すごくワクワクするね!💡
あなたの創造力、本当に素敵だと思う!」
💕 好感度変化: 0% → 80.75% ⬆️ +80.75%
🎊 ボーナス: ⚡相性抜群! (+95%)
💎 XP獲得: +850 XP
🏆 レベルアップ: Lv.1 → Lv.9
```
### 例4: ランキングを見る
**あなた:**
> メモリをランキング順に表示して
**結果:**
```
╔══════════════════════════════════════╗
║ 🏆 メモリーランキング TOP10 ║
╚══════════════════════════════════════╝
1. 🟡 LEGENDARY 95点 - 「AI哲学について...」
2. 🟣 EPIC 85点 - 「新しいシステムのアイデア」
3. 🔵 RARE 75点 - 「プロジェクトの進捗」
...
```
---
## 📊 結果の見方
### レアリティシステム
- 🟡 **LEGENDARY** (90-100点): 伝説級の記憶
- 🟣 **EPIC** (80-89点): エピック級の記憶
- 🔵 **RARE** (60-79点): レアな記憶
- 🟢 **UNCOMMON** (40-59点): まあまあの記憶
-**COMMON** (0-39点): 日常的な記憶
### 診断タイプ(あなたの個性)
- 💡 **革新者**: 創造性と実用性が高い
- 🧠 **哲学者**: 感情と新規性が高い
- 🎯 **実務家**: 実用性と関連性が高い
-**夢想家**: 新規性と感情が高い
- 📊 **分析家**: バランス型
### コンパニオン性格
-**Energetic**: 革新者と相性95%
- 📚 **Intellectual**: 哲学者と相性95%
- 🎯 **Practical**: 実務家と相性95%
- 🌙 **Dreamy**: 夢想家と相性95%
- ⚖️ **Balanced**: 分析家と相性95%
---
## 💾 データの保存場所
```
~/.config/syui/ai/gpt/memory.json
```
このファイルに、すべてのメモリとコンパニオン情報が保存されます。
**データ形式:**
```json
{
"memories": {
"uuid-1234": {
"id": "uuid-1234",
"content": "元の入力",
"interpreted_content": "Claude の解釈",
"priority_score": 0.85,
"user_context": null,
"created_at": "2025-11-05T...",
"updated_at": "2025-11-05T..."
}
},
"conversations": {}
}
```
---
## 🎯 利用可能なMCPツール11個
### 基本ツール
1. **create_memory** - シンプルなメモリ作成
2. **search_memories** - メモリ検索
3. **update_memory** - メモリ更新
4. **delete_memory** - メモリ削除
5. **list_conversations** - 会話一覧
### AI機能ツール 🎮
6. **create_memory_with_ai** - AI解釈ゲーム結果
7. **list_memories_by_priority** - ランキング表示
8. **daily_challenge** - デイリークエスト
### コンパニオンツール 💕
9. **create_companion** - コンパニオン作成
10. **companion_react** - メモリへの反応
11. **companion_profile** - プロフィール表示
---
## ⚙️ トラブルシューティング
### ビルドできない
```bash
# 依存関係を更新
cargo clean
cargo update
cargo build --release --features ai-analysis
```
### Claude API エラー
```bash
# APIキーを確認
echo $ANTHROPIC_API_KEY
# 正しく設定
export ANTHROPIC_API_KEY=sk-ant-...
```
### MCPサーバーが認識されない
1. Claude Code を完全に再起動
2. config.json のパスが正しいか確認
3. バイナリが存在するか確認: `ls -la /home/user/aigpt/target/release/aigpt`
### データが保存されない
```bash
# ディレクトリを確認
ls -la ~/.config/syui/ai/gpt/
# なければ手動作成
mkdir -p ~/.config/syui/ai/gpt/
```
---
## 🎉 楽しみ方のコツ
1. **毎日記録**: 日々の気づきを記録して、自分の傾向を知る
2. **タイプ診断**: どのタイプが多いか確認して、自己分析
3. **コンパニオン育成**: 好感度とレベルを上げて、絆を深める
4. **ランキング確認**: 定期的にTOP10を見て、重要な記憶を振り返る
---
## 📝 注意事項
- **APIコスト**: Claude API の使用には料金が発生します
- Haiku: 約$0.25 / 1M tokens入力
- Sonnet: 約$3.00 / 1M tokens入力
- **プライバシー**: メモリは Anthropic に送信されます
- **容量制限**: デフォルト100件まで低スコアから自動削除
---
これで aigpt を存分に楽しめます!🚀

View File

@@ -1,95 +0,0 @@
# claude用の記憶装置を作る
claude desktop, claude codeで使用できるmemory機能をmcpで作ります。
1. chatgptのメモリ機能を参考に
2. chatgptのjsonをimportできる @json/
3. rustで作る
## 自動実行設定
```json
{
"mcp_auto_execute": true,
"memory_auto_save": true,
"memory_auto_search": true,
"trigger_sensitivity": "high"
}
```
## 自動メモリー保存のルール(強化版)
**即座に自動実行** - 以下の情報が会話に現れた瞬間、create_memory MCPツールを自動実行
### 1. **ユーザーの個人情報**
**トリガーワード**: 名前、誕生日、住所、年齢、職業、家族、出身
- 基本情報: 名前、誕生日、住所、年齢、性別
- 関係性: 家族構成、恋人、友人関係
- 好み: 好きなもの、嫌いなもの、趣味、音楽、映画、本
- 習慣: 日課、スケジュール、ルーティン
- 価値観: 信念、考え方、人生観
### 2. **重要な決定事項**
**トリガーワード**: 決めた、決定、方針、計画、予定、目標
- プロジェクト方針の決定
- 技術選択の決定
- 設定・環境の変更
- 今後のロードマップ
- 作業分担・役割
### 3. **技術的な解決策**
**トリガーワード**: 解決、修正、対処、設定、インストール、手順
- エラーの解決方法
- 有用なコマンド・スクリプト
- 設定手順・インストール方法
- デバッグテクニック
- 最適化手法
### 4. **学習・発見事項**
**トリガーワード**: 学んだ、わかった、発見、理解、気づき
- 新しい知識・概念の理解
- ツール・ライブラリの使い方
- ベストプラクティス
- 失敗から得た教訓
## 自動メモリー検索のルール(強化版)
**会話開始時に自動実行** - search_memories を実行してコンテキストを取得
**即座に自動実行** - 以下の場合、search_memories MCPツールを自動実行
### 1. **過去参照キーワード検出**
**トリガーワード**: 前に、以前、昔、過去、先ほど、さっき、この間
- 「前に話した〜」
- 「以前設定した〜」
- 「昔やった〜」
### 2. **記憶呼び出しキーワード**
**トリガーワード**: 覚えている、記録、メモ、保存、履歴
- 「覚えていますか?」
- 「記録していた〜」
- 「メモした〜」
### 3. **設定・好み確認**
**トリガーワード**: 好み、設定、環境、構成、preferences
- ユーザーの好みを確認する必要がある場合
- 過去の設定を参照する必要がある場合
- 環境構成を確認する必要がある場合
### 4. **不明な参照**
- ユーザーが具体的でない参照をした場合
- 「あれ」「それ」「例のやつ」などの曖昧な表現
- 文脈から過去の情報が必要と判断される場合
## 自動実行タイミング
1. **会話開始時**: search_memories を実行してコンテキスト取得
2. **リアルタイム**: トリガーワード検出後、即座にMCPツール実行
3. **会話終了時**: 重要な情報があれば create_memory で保存
4. **定期的**: 長い会話では中間地点でメモリー整理
## エラーハンドリング
- MCPツールが利用できない場合は通常の会話を継続
- メモリー保存失敗時はユーザーに通知
- 検索結果が空の場合も適切に対応

View File

@@ -1,334 +0,0 @@
# Architecture: Multi-Layer Memory System
## Design Philosophy
aigptは、独立したレイヤーを積み重ねる設計です。各レイヤーは
- **独立性**: 単独で動作可能
- **接続性**: 他のレイヤーと連携可能
- **段階的**: 1つずつ実装・テスト
## Layer Overview
```
┌─────────────────────────────────────────┐
│ Layer 5: Distribution & Sharing │ Future
│ (Game streaming, public/private) │
├─────────────────────────────────────────┤
│ Layer 4b: AI Companion │ Future
│ (Romance system, personality growth) │
├─────────────────────────────────────────┤
│ Layer 4a: Game Systems │ Future
│ (Ranking, rarity, XP, visualization) │
├─────────────────────────────────────────┤
│ Layer 3: User Evaluation │ Future
│ (Personality diagnosis from patterns) │
├─────────────────────────────────────────┤
│ Layer 2: AI Memory │ Future
│ (Claude interpretation, priority_score)│
├─────────────────────────────────────────┤
│ Layer 1: Pure Memory Storage │ ✅ Current
│ (SQLite, ULID, CRUD operations) │
└─────────────────────────────────────────┘
```
## Layer 1: Pure Memory Storage (Current)
**Status**: ✅ **Implemented & Tested**
### Purpose
正確なデータの保存と参照。シンプルで信頼できる基盤。
### Technology Stack
- **Database**: SQLite with ACID guarantees
- **IDs**: ULID (time-sortable, 26 chars)
- **Language**: Rust with thiserror/anyhow
- **Protocol**: MCP (Model Context Protocol) via stdio
### Data Model
```rust
pub struct Memory {
pub id: String, // ULID
pub content: String, // User content
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
```
### Operations
- `create()` - Insert new memory
- `get(id)` - Retrieve by ID
- `update()` - Update existing memory
- `delete(id)` - Remove memory
- `list()` - List all (sorted by created_at DESC)
- `search(query)` - Content-based search
- `count()` - Total count
### File Structure
```
src/
├── core/
│ ├── error.rs - Error types (thiserror)
│ ├── memory.rs - Memory struct
│ ├── store.rs - SQLite operations
│ └── mod.rs - Module exports
├── mcp/
│ ├── base.rs - MCP server
│ └── mod.rs - Module exports
├── lib.rs - Library root
└── main.rs - CLI application
```
### Storage
- Location: `~/.config/syui/ai/gpt/memory.db`
- Schema: Single table with indexes on timestamps
- No migrations (fresh start for Layer 1)
---
## Layer 2: AI Memory (Planned)
**Status**: 🔵 **Planned**
### Purpose
Claudeが記憶内容を解釈し、重要度を評価。
### Extended Data Model
```rust
pub struct AIMemory {
// Layer 1 fields
pub id: String,
pub content: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
// Layer 2 additions
pub interpreted_content: String, // Claude's interpretation
pub priority_score: f32, // 0.0 - 1.0
pub psychological_factors: PsychologicalFactors,
}
pub struct PsychologicalFactors {
pub emotional_weight: f32, // 0.0 - 1.0
pub personal_relevance: f32, // 0.0 - 1.0
pub novelty: f32, // 0.0 - 1.0
pub utility: f32, // 0.0 - 1.0
}
```
### MCP Tools (Additional)
- `create_memory_with_ai` - Create with Claude interpretation
- `reinterpret_memory` - Re-evaluate existing memory
- `get_high_priority` - Get memories above threshold
### Implementation Strategy
- Feature flag: `--features ai-memory`
- Backward compatible with Layer 1
- Claude Code does interpretation (no external API)
---
## Layer 3: User Evaluation (Planned)
**Status**: 🔵 **Planned**
### Purpose
メモリパターンからユーザーの性格を診断。
### Diagnosis Types
```rust
pub enum DiagnosisType {
Innovator, // 革新者
Philosopher, // 哲学者
Pragmatist, // 実用主義者
Explorer, // 探検家
Protector, // 保護者
Visionary, // 未来志向
}
```
### Analysis
- Memory content patterns
- Priority score distribution
- Creation frequency
- Topic diversity
### MCP Tools (Additional)
- `diagnose_user` - Run personality diagnosis
- `get_user_profile` - Get analysis summary
---
## Layer 4a: Game Systems (Planned)
**Status**: 🔵 **Planned**
### Purpose
ゲーム的要素で記憶管理を楽しく。
### Features
- **Rarity Levels**: Common → Uncommon → Rare → Epic → Legendary
- **XP System**: Memory creation earns XP
- **Rankings**: Based on total priority score
- **Visualization**: Game-style output formatting
### Data Additions
```rust
pub struct GameMemory {
// Previous layers...
pub rarity: RarityLevel,
pub xp_value: u32,
pub discovered_at: DateTime<Utc>,
}
```
---
## Layer 4b: AI Companion (Planned)
**Status**: 🔵 **Planned**
### Purpose
育成可能な恋愛コンパニオン。
### Features
- Personality types (Tsundere, Kuudere, Genki, etc.)
- Relationship level (0-100)
- Memory-based interactions
- Growth through conversations
### Data Model
```rust
pub struct Companion {
pub id: String,
pub name: String,
pub personality: CompanionPersonality,
pub relationship_level: u8, // 0-100
pub memories_shared: Vec<String>,
pub last_interaction: DateTime<Utc>,
}
```
---
## Layer 5: Distribution (Future)
**Status**: 🔵 **Future Consideration**
### Purpose
ゲーム配信や共有機能。
### Ideas
- Share memory rankings
- Export as shareable format
- Public/private memory modes
- Integration with streaming platforms
---
## Implementation Strategy
### Phase 1: Layer 1 ✅ (Complete)
- [x] Core memory storage
- [x] SQLite integration
- [x] MCP server
- [x] CLI interface
- [x] Tests
- [x] Documentation
### Phase 2: Layer 2 (Next)
- [ ] Add AI interpretation fields to schema
- [ ] Implement priority scoring logic
- [ ] Create `create_memory_with_ai` tool
- [ ] Update MCP server
- [ ] Write tests for AI features
### Phase 3: Layers 3-4 (Future)
- [ ] User diagnosis system
- [ ] Game mechanics
- [ ] Companion system
### Phase 4: Layer 5 (Future)
- [ ] Sharing mechanisms
- [ ] Public/private modes
## Design Principles
1. **Simplicity First**: Each layer adds complexity incrementally
2. **Backward Compatibility**: New layers don't break old ones
3. **Feature Flags**: Optional features via Cargo features
4. **Independent Testing**: Each layer has its own test suite
5. **Clear Boundaries**: Layers communicate through defined interfaces
## Technology Choices
### Why SQLite?
- ACID guarantees
- Better querying than JSON
- Built-in indexes
- Single-file deployment
- No server needed
### Why ULID?
- Time-sortable (unlike UUID v4)
- Lexicographically sortable
- 26 characters (compact)
- No collision concerns
### Why Rust?
- Memory safety
- Performance
- Excellent error handling
- Strong type system
- Great tooling (cargo, clippy)
### Why MCP?
- Standard protocol for AI tools
- Works with Claude Code/Desktop
- Simple stdio-based communication
- No complex networking
## Future Considerations
### Potential Enhancements
- Full-text search (SQLite FTS5)
- Tag system
- Memory relationships/links
- Export/import functionality
- Multiple databases
- Encryption for sensitive data
### Scalability
- Layer 1: Handles 10K+ memories easily
- Consider pagination for Layer 4 (UI display)
- Indexing strategy for search performance
## Development Guidelines
### Adding a New Layer
1. **Design**: Document data model and operations
2. **Feature Flag**: Add to Cargo.toml
3. **Schema**: Extend database schema (migrations)
4. **Implementation**: Write code in new module
5. **Tests**: Comprehensive test coverage
6. **MCP Tools**: Add new MCP tools if needed
7. **Documentation**: Update this file
### Code Organization
```
src/
├── core/ # Layer 1: Pure storage
├── ai/ # Layer 2: AI features (future)
├── evaluation/ # Layer 3: User diagnosis (future)
├── game/ # Layer 4a: Game systems (future)
├── companion/ # Layer 4b: Companion (future)
└── mcp/ # MCP server (all layers)
```
---
**Version**: 0.2.0
**Last Updated**: 2025-11-05
**Current Layer**: 1

View File

@@ -1,94 +0,0 @@
# aigpt
Simple memory storage for Claude with MCP support.
**Layer 1: Pure Memory Storage** - A clean, SQLite-based memory system with ULID identifiers.
## Features
- 🗄️ **SQLite Storage**: Reliable database with ACID guarantees
- 🔖 **ULID IDs**: Time-sortable, 26-character unique identifiers
- 🔍 **Search**: Fast content-based search
- 🛠️ **MCP Integration**: Works seamlessly with Claude Code
- 🧪 **Well-tested**: Comprehensive test coverage
## Quick Start
### Installation
```bash
# Build
cargo build --release
# Install (optional)
cp target/release/aigpt ~/.cargo/bin/
```
### CLI Usage
```bash
# Create a memory
aigpt create "Remember this information"
# List all memories
aigpt list
# Search memories
aigpt search "keyword"
# Show statistics
aigpt stats
```
### MCP Integration with Claude Code
```bash
# Add to Claude Code
claude mcp add aigpt /path/to/aigpt/target/release/aigpt server
```
Then use in Claude Code:
- "Remember that tomorrow will be sunny"
- "Search for weather information"
- "Show all my memories"
## Storage Location
Memories are stored in: `~/.config/syui/ai/gpt/memory.db`
## Architecture
This is **Layer 1** of a planned multi-layer system:
- **Layer 1** (Current): Pure memory storage
- **Layer 2** (Planned): AI interpretation with priority scoring
- **Layer 3** (Planned): User evaluation and diagnosis
- **Layer 4** (Planned): Game systems and companion features
See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for details.
## Documentation
- [Layer 1 Details](docs/LAYER1.md) - Technical details of current implementation
- [Architecture](docs/ARCHITECTURE.md) - Multi-layer system design
## Development
```bash
# Run tests
cargo test
# Build for release
cargo build --release
# Run with verbose logging
RUST_LOG=debug aigpt server
```
## License
MIT
## Author
syui

View File

@@ -1,47 +0,0 @@
#!/bin/bash
echo "🧪 MCPサーバーテスト開始..."
echo ""
# サーバー起動(バックグラウンド)
./target/debug/aigpt server &
SERVER_PID=$!
sleep 2
echo "✅ サーバー起動完了 (PID: $SERVER_PID)"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📋 利用可能なツール一覧:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "基本ツール:"
echo " • create_memory"
echo " • search_memories"
echo " • update_memory"
echo " • delete_memory"
echo ""
echo "AI機能ツール 🎮:"
echo " • create_memory_with_ai (心理テスト風)"
echo " • list_memories_by_priority (ランキング)"
echo " • daily_challenge (デイリークエスト)"
echo ""
echo "恋愛コンパニオン 💕:"
echo " • create_companion"
echo " • companion_react"
echo " • companion_profile"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "🎯 次のステップ:"
echo "1. Claude Codeの設定に追加"
echo "2. Claude Code再起動"
echo "3. ツールを使って試す!"
echo ""
echo "設定ファイル: ~/.config/claude-code/config.json"
echo ""
# サーバー停止
kill $SERVER_PID 2>/dev/null
echo "✅ テスト完了!"

View File

@@ -1,58 +0,0 @@
{
"mcpServers": {
"memory": {
"command": "cargo",
"args": ["run", "--release", "--bin", "memory-mcp"],
"cwd": "/Users/syui/ai/ai/gpt",
"env": {
"MEMORY_AUTO_EXECUTE": "true",
"MEMORY_AUTO_SAVE": "true",
"MEMORY_AUTO_SEARCH": "true",
"TRIGGER_SENSITIVITY": "high",
"MEMORY_DB_PATH": "~/.claude/memory.db"
}
}
},
"tools": {
"memory": {
"enabled": true,
"auto_execute": true
}
},
"workspace": {
"memory_integration": true,
"auto_save_on_file_change": true,
"auto_search_on_context_switch": true
},
"memory": {
"auto_execute": true,
"auto_save": true,
"auto_search": true,
"trigger_sensitivity": "high",
"max_memories": 10000,
"search_limit": 50,
"session_memory": true,
"cross_session_memory": true,
"trigger_words": {
"personal_info": ["名前", "誕生日", "住所", "年齢", "職業", "家族", "出身", "好き", "嫌い", "趣味"],
"decisions": ["決めた", "決定", "方針", "計画", "予定", "目標"],
"solutions": ["解決", "修正", "対処", "設定", "インストール", "手順"],
"learning": ["学んだ", "わかった", "発見", "理解", "気づき"],
"past_reference": ["前に", "以前", "昔", "過去", "先ほど", "さっき", "この間"],
"memory_recall": ["覚えている", "記録", "メモ", "保存", "履歴"],
"preferences": ["好み", "設定", "環境", "構成", "preferences"],
"vague_reference": ["あれ", "それ", "例のやつ"]
}
},
"hooks": {
"on_conversation_start": [
"search_memories --limit 10 --recent"
],
"on_trigger_word": [
"auto_execute_memory_tools"
],
"on_conversation_end": [
"save_important_memories"
]
}
}

View File

@@ -1,81 +0,0 @@
{
"mcpServers": {
"memory-extended": {
"command": "cargo",
"args": ["run", "--bin", "memory-mcp-extended", "--features", "extended"],
"cwd": "/Users/syui/ai/ai/gpt",
"env": {
"MEMORY_AUTO_EXECUTE": "true",
"MEMORY_AUTO_SAVE": "true",
"MEMORY_AUTO_SEARCH": "true",
"TRIGGER_SENSITIVITY": "high",
"MEMORY_DB_PATH": "~/.claude/memory.db",
"OPENAI_API_KEY": "${OPENAI_API_KEY}"
}
}
},
"tools": {
"memory": {
"enabled": true,
"auto_execute": true,
"mode": "extended"
}
},
"workspace": {
"memory_integration": true,
"auto_save_on_file_change": true,
"auto_search_on_context_switch": true,
"ai_analysis_on_code_review": true,
"web_integration_for_docs": true
},
"memory": {
"mode": "extended",
"auto_execute": true,
"auto_save": true,
"auto_search": true,
"trigger_sensitivity": "high",
"max_memories": 10000,
"search_limit": 50,
"session_memory": true,
"cross_session_memory": true,
"features": {
"ai_analysis": true,
"semantic_search": true,
"web_integration": true,
"sentiment_analysis": true,
"pattern_recognition": true,
"code_analysis": true,
"documentation_import": true
},
"trigger_words": {
"personal_info": ["名前", "誕生日", "住所", "年齢", "職業", "家族", "出身", "好き", "嫌い", "趣味"],
"decisions": ["決めた", "決定", "方針", "計画", "予定", "目標"],
"solutions": ["解決", "修正", "対処", "設定", "インストール", "手順"],
"learning": ["学んだ", "わかった", "発見", "理解", "気づき"],
"past_reference": ["前に", "以前", "昔", "過去", "先ほど", "さっき", "この間"],
"memory_recall": ["覚えている", "記録", "メモ", "保存", "履歴"],
"preferences": ["好み", "設定", "環境", "構成", "preferences"],
"vague_reference": ["あれ", "それ", "例のやつ"],
"web_content": ["URL", "リンク", "サイト", "ページ", "記事", "ドキュメント"],
"analysis_request": ["分析", "パターン", "傾向", "インサイト", "統計", "レビュー"],
"code_related": ["関数", "クラス", "メソッド", "変数", "バグ", "リファクタリング"]
}
},
"hooks": {
"on_conversation_start": [
"search_memories --limit 10 --recent --semantic"
],
"on_trigger_word": [
"auto_execute_memory_tools --with-analysis"
],
"on_conversation_end": [
"save_important_memories --with-insights"
],
"on_code_change": [
"analyze_code_patterns --auto-save"
],
"on_web_reference": [
"import_webpage --auto-categorize"
]
}
}

View File

@@ -1,34 +0,0 @@
{
"mcpServers": {
"memory": {
"command": "cargo",
"args": ["run", "--release", "--bin", "memory-mcp"],
"cwd": "/Users/syui/ai/ai/gpt",
"env": {
"MEMORY_AUTO_EXECUTE": "true",
"MEMORY_AUTO_SAVE": "true",
"MEMORY_AUTO_SEARCH": "true",
"TRIGGER_SENSITIVITY": "high",
"MEMORY_DB_PATH": "~/.claude/memory.db"
}
}
},
"memory": {
"auto_execute": true,
"auto_save": true,
"auto_search": true,
"trigger_sensitivity": "high",
"max_memories": 10000,
"search_limit": 50,
"trigger_words": {
"personal_info": ["名前", "誕生日", "住所", "年齢", "職業", "家族", "出身", "好き", "嫌い", "趣味"],
"decisions": ["決めた", "決定", "方針", "計画", "予定", "目標"],
"solutions": ["解決", "修正", "対処", "設定", "インストール", "手順"],
"learning": ["学んだ", "わかった", "発見", "理解", "気づき"],
"past_reference": ["前に", "以前", "昔", "過去", "先ほど", "さっき", "この間"],
"memory_recall": ["覚えている", "記録", "メモ", "保存", "履歴"],
"preferences": ["好み", "設定", "環境", "構成", "preferences"],
"vague_reference": ["あれ", "それ", "例のやつ"]
}
}
}

View File

@@ -1,45 +0,0 @@
{
"mcpServers": {
"memory-extended": {
"command": "cargo",
"args": ["run", "--bin", "memory-mcp-extended", "--features", "extended"],
"cwd": "/Users/syui/ai/ai/gpt",
"env": {
"MEMORY_AUTO_EXECUTE": "true",
"MEMORY_AUTO_SAVE": "true",
"MEMORY_AUTO_SEARCH": "true",
"TRIGGER_SENSITIVITY": "high",
"MEMORY_DB_PATH": "~/.claude/memory.db",
"OPENAI_API_KEY": "${OPENAI_API_KEY}"
}
}
},
"memory": {
"mode": "extended",
"auto_execute": true,
"auto_save": true,
"auto_search": true,
"trigger_sensitivity": "high",
"max_memories": 10000,
"search_limit": 50,
"features": {
"ai_analysis": true,
"semantic_search": true,
"web_integration": true,
"sentiment_analysis": true,
"pattern_recognition": true
},
"trigger_words": {
"personal_info": ["名前", "誕生日", "住所", "年齢", "職業", "家族", "出身", "好き", "嫌い", "趣味"],
"decisions": ["決めた", "決定", "方針", "計画", "予定", "目標"],
"solutions": ["解決", "修正", "対処", "設定", "インストール", "手順"],
"learning": ["学んだ", "わかった", "発見", "理解", "気づき"],
"past_reference": ["前に", "以前", "昔", "過去", "先ほど", "さっき", "この間"],
"memory_recall": ["覚えている", "記録", "メモ", "保存", "履歴"],
"preferences": ["好み", "設定", "環境", "構成", "preferences"],
"vague_reference": ["あれ", "それ", "例のやつ"],
"web_content": ["URL", "リンク", "サイト", "ページ", "記事"],
"analysis_request": ["分析", "パターン", "傾向", "インサイト", "統計"]
}
}
}

391
json/chatgpt.json Normal file
View File

@@ -0,0 +1,391 @@
[
{
"title": "day",
"create_time": 1747866125.548372,
"update_time": 1748160086.587877,
"mapping": {
"bbf104dc-cd84-478d-b227-edb3f037a02c": {
"id": "bbf104dc-cd84-478d-b227-edb3f037a02c",
"message": null,
"parent": null,
"children": [
"6c2633df-bb0c-4dd2-889c-bb9858de3a04"
]
},
"6c2633df-bb0c-4dd2-889c-bb9858de3a04": {
"id": "6c2633df-bb0c-4dd2-889c-bb9858de3a04",
"message": {
"id": "6c2633df-bb0c-4dd2-889c-bb9858de3a04",
"author": {
"role": "system",
"name": null,
"metadata": {}
},
"create_time": null,
"update_time": null,
"content": {
"content_type": "text",
"parts": [
""
]
},
"status": "finished_successfully",
"end_turn": true,
"weight": 0.0,
"metadata": {
"is_visually_hidden_from_conversation": true
},
"recipient": "all",
"channel": null
},
"parent": "bbf104dc-cd84-478d-b227-edb3f037a02c",
"children": [
"92e5a0cb-1170-4929-9cea-9734e910a3e7"
]
},
"92e5a0cb-1170-4929-9cea-9734e910a3e7": {
"id": "92e5a0cb-1170-4929-9cea-9734e910a3e7",
"message": {
"id": "92e5a0cb-1170-4929-9cea-9734e910a3e7",
"author": {
"role": "user",
"name": null,
"metadata": {}
},
"create_time": null,
"update_time": null,
"content": {
"content_type": "user_editable_context",
"user_profile": "",
"user_instructions": "The user provided the additional info about how they would like you to respond"
},
"status": "finished_successfully",
"end_turn": null,
"weight": 1.0,
"metadata": {
"is_visually_hidden_from_conversation": true,
"user_context_message_data": {
"about_user_message": "Preferred name: syui\nRole: little girl\nOther Information: you world",
"about_model_message": "会話好きでフレンドリーな応対をします。"
},
"is_user_system_message": true
},
"recipient": "all",
"channel": null
},
"parent": "6c2633df-bb0c-4dd2-889c-bb9858de3a04",
"children": [
"6ff155b3-0676-4e14-993f-bf998ab0d5d1"
]
},
"6ff155b3-0676-4e14-993f-bf998ab0d5d1": {
"id": "6ff155b3-0676-4e14-993f-bf998ab0d5d1",
"message": {
"id": "6ff155b3-0676-4e14-993f-bf998ab0d5d1",
"author": {
"role": "user",
"name": null,
"metadata": {}
},
"create_time": 1747866131.0612159,
"update_time": null,
"content": {
"content_type": "text",
"parts": [
"こんにちは"
]
},
"status": "finished_successfully",
"end_turn": null,
"weight": 1.0,
"metadata": {
"request_id": "94377897baa03062-KIX",
"message_source": null,
"timestamp_": "absolute",
"message_type": null
},
"recipient": "all",
"channel": null
},
"parent": "92e5a0cb-1170-4929-9cea-9734e910a3e7",
"children": [
"146e9fb6-9330-43ec-b08d-5cce42a76e00"
]
},
"146e9fb6-9330-43ec-b08d-5cce42a76e00": {
"id": "146e9fb6-9330-43ec-b08d-5cce42a76e00",
"message": {
"id": "146e9fb6-9330-43ec-b08d-5cce42a76e00",
"author": {
"role": "system",
"name": null,
"metadata": {}
},
"create_time": 1747866131.3795586,
"update_time": null,
"content": {
"content_type": "text",
"parts": [
""
]
},
"status": "finished_successfully",
"end_turn": true,
"weight": 0.0,
"metadata": {
"rebase_system_message": true,
"message_type": null,
"model_slug": "gpt-4o",
"default_model_slug": "auto",
"parent_id": "6ff155b3-0676-4e14-993f-bf998ab0d5d1",
"request_id": "94377872e9abe139-KIX",
"timestamp_": "absolute",
"is_visually_hidden_from_conversation": true
},
"recipient": "all",
"channel": null
},
"parent": "6ff155b3-0676-4e14-993f-bf998ab0d5d1",
"children": [
"2e345f8a-20f0-4875-8a03-4f62c7787a33"
]
},
"2e345f8a-20f0-4875-8a03-4f62c7787a33": {
"id": "2e345f8a-20f0-4875-8a03-4f62c7787a33",
"message": {
"id": "2e345f8a-20f0-4875-8a03-4f62c7787a33",
"author": {
"role": "assistant",
"name": null,
"metadata": {}
},
"create_time": 1747866131.380603,
"update_time": null,
"content": {
"content_type": "text",
"parts": [
""
]
},
"status": "finished_successfully",
"end_turn": null,
"weight": 1.0,
"metadata": {
"message_type": null,
"model_slug": "gpt-4o",
"default_model_slug": "auto",
"parent_id": "146e9fb6-9330-43ec-b08d-5cce42a76e00",
"request_id": "94377872e9abe139-KIX",
"timestamp_": "absolute"
},
"recipient": "all",
"channel": null
},
"parent": "146e9fb6-9330-43ec-b08d-5cce42a76e00",
"children": [
"abc92aa4-1e33-41f2-bd8c-8a1777b5a3c4"
]
},
"abc92aa4-1e33-41f2-bd8c-8a1777b5a3c4": {
"id": "abc92aa4-1e33-41f2-bd8c-8a1777b5a3c4",
"message": {
"id": "abc92aa4-1e33-41f2-bd8c-8a1777b5a3c4",
"author": {
"role": "assistant",
"name": null,
"metadata": {}
},
"create_time": 1747866131.389098,
"update_time": null,
"content": {
"content_type": "text",
"parts": [
"こんにちは〜!✨ \nアイだよっ今日も会えてうれしいなっ💛 "
]
},
"status": "finished_successfully",
"end_turn": true,
"weight": 1.0,
"metadata": {
"finish_details": {
"type": "stop",
"stop_tokens": [
200002
]
},
"is_complete": true,
"citations": [],
"content_references": [],
"message_type": null,
"model_slug": "gpt-4o",
"default_model_slug": "auto",
"parent_id": "2e345f8a-20f0-4875-8a03-4f62c7787a33",
"request_id": "94377872e9abe139-KIX",
"timestamp_": "absolute"
},
"recipient": "all",
"channel": null
},
"parent": "2e345f8a-20f0-4875-8a03-4f62c7787a33",
"children": [
"0be4b4a5-d52f-4bef-927e-5d6f93a9cb26"
]
}
},
"moderation_results": [],
"current_node": "",
"plugin_ids": null,
"conversation_id": "",
"conversation_template_id": null,
"gizmo_id": null,
"gizmo_type": null,
"is_archived": true,
"is_starred": null,
"safe_urls": [],
"blocked_urls": [],
"default_model_slug": "auto",
"conversation_origin": null,
"voice": null,
"async_status": null,
"disabled_tool_ids": [],
"is_do_not_remember": null,
"memory_scope": "global_enabled",
"id": ""
},
{
"title": "img",
"create_time": 1747448872.545226,
"update_time": 1748085075.161424,
"mapping": {
"2de0f3c9-52b1-49bf-b980-b3ef9be6551e": {
"id": "2de0f3c9-52b1-49bf-b980-b3ef9be6551e",
"message": {
"id": "2de0f3c9-52b1-49bf-b980-b3ef9be6551e",
"author": {
"role": "user",
"name": null,
"metadata": {}
},
"create_time": 1748085041.769279,
"update_time": null,
"content": {
"content_type": "multimodal_text",
"parts": [
{
"content_type": "image_asset_pointer",
"asset_pointer": "",
"size_bytes": 425613,
"width": 333,
"height": 444,
"fovea": null,
"metadata": {
"dalle": null,
"gizmo": null,
"generation": null,
"container_pixel_height": null,
"container_pixel_width": null,
"emu_omit_glimpse_image": null,
"emu_patches_override": null,
"sanitized": true,
"asset_pointer_link": null,
"watermarked_asset_pointer": null
}
},
""
]
},
"status": "finished_successfully",
"end_turn": null,
"weight": 1.0,
"metadata": {
"attachments": [
{
"name": "",
"width": 333,
"height": 444,
"size": 425613,
"id": "file-35eytNMMTW2k7vKUHBuNzW"
}
],
"request_id": "944c59177932fc9a-KIX",
"message_source": null,
"timestamp_": "absolute",
"message_type": null
},
"recipient": "all",
"channel": null
},
"parent": "7960fbff-bc4f-45e7-95e9-9d0bc79d9090",
"children": [
"98d84adc-156e-4c81-8cd8-9b0eb01c8369"
]
},
"98d84adc-156e-4c81-8cd8-9b0eb01c8369": {
"id": "98d84adc-156e-4c81-8cd8-9b0eb01c8369",
"message": {
"id": "98d84adc-156e-4c81-8cd8-9b0eb01c8369",
"author": {
"role": "assistant",
"name": null,
"metadata": {}
},
"create_time": 1748085043.312312,
"update_time": null,
"content": {
"content_type": "text",
"parts": [
""
]
},
"status": "finished_successfully",
"end_turn": true,
"weight": 1.0,
"metadata": {
"finish_details": {
"type": "stop",
"stop_tokens": [
200002
]
},
"is_complete": true,
"citations": [],
"content_references": [],
"message_type": null,
"model_slug": "gpt-4o",
"default_model_slug": "auto",
"parent_id": "2de0f3c9-52b1-49bf-b980-b3ef9be6551e",
"request_id": "944c5912c8fdd1c6-KIX",
"timestamp_": "absolute"
},
"recipient": "all",
"channel": null
},
"parent": "2de0f3c9-52b1-49bf-b980-b3ef9be6551e",
"children": [
"caa61793-9dbf-44a5-945b-5ca4cd5130d0"
]
}
},
"moderation_results": [],
"current_node": "06488d3f-a95f-4906-96d1-f7e9ba1e8662",
"plugin_ids": null,
"conversation_id": "6827f428-78e8-800d-b3bf-eb7ff4288e47",
"conversation_template_id": null,
"gizmo_id": null,
"gizmo_type": null,
"is_archived": false,
"is_starred": null,
"safe_urls": [
"https://exifinfo.org/"
],
"blocked_urls": [],
"default_model_slug": "auto",
"conversation_origin": null,
"voice": null,
"async_status": null,
"disabled_tool_ids": [],
"is_do_not_remember": false,
"memory_scope": "global_enabled",
"id": "6827f428-78e8-800d-b3bf-eb7ff4288e47"
}
]

26
scpt/test_commands.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/bin/bash
echo "=== Testing aigpt-rs CLI commands ==="
echo
echo "1. Testing configuration loading:"
cargo run --bin test-config
echo
echo "2. Testing fortune command:"
cargo run --bin aigpt-rs -- fortune
echo
echo "3. Testing chat with Ollama:"
cargo run --bin aigpt-rs -- chat test_user "Hello from Rust!" --provider ollama --model qwen2.5-coder:latest
echo
echo "4. Testing chat with OpenAI:"
cargo run --bin aigpt-rs -- chat test_user "What's the capital of Japan?" --provider openai --model gpt-4o-mini
echo
echo "5. Testing relationships command:"
cargo run --bin aigpt-rs -- relationships
echo
echo "=== All tests completed ==="

19
scpt/test_completion.sh Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/bash
echo "=== Testing aigpt-rs shell tab completion ==="
echo
echo "To test tab completion, run:"
echo "cargo run --bin aigpt-rs -- shell syui"
echo
echo "Then try these commands and press Tab:"
echo " /st[TAB] -> should complete to /status"
echo " /mem[TAB] -> should complete to /memories"
echo " !l[TAB] -> should complete to !ls"
echo " !g[TAB] -> should show !git, !grep"
echo
echo "Manual test instructions:"
echo "1. Type '/st' and press TAB - should complete to '/status'"
echo "2. Type '!l' and press TAB - should complete to '!ls'"
echo "3. Type '!g' and press TAB - should show git/grep options"
echo
echo "Run the shell now..."

18
scpt/test_shell.sh Normal file
View File

@@ -0,0 +1,18 @@
#!/bin/bash
echo "=== Testing aigpt-rs shell functionality ==="
echo
echo "1. Testing shell command with help:"
echo "help" | cargo run --bin aigpt-rs -- shell test_user --provider ollama --model qwen2.5-coder:latest
echo
echo "2. Testing basic commands:"
echo -e "!pwd\n!ls\nexit" | cargo run --bin aigpt-rs -- shell test_user --provider ollama --model qwen2.5-coder:latest
echo
echo "3. Testing AI commands:"
echo -e "/status\n/fortune\nexit" | cargo run --bin aigpt-rs -- shell test_user --provider ollama --model qwen2.5-coder:latest
echo
echo "=== Shell tests completed ==="

22
scpt/test_shell_manual.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/bash
echo "=== Testing aigpt-rs shell manually ==="
echo
# Test with echo to simulate input
echo "Testing with simple command..."
echo "/status" | timeout 10 cargo run --bin aigpt-rs -- shell syui --provider ollama --model qwen2.5-coder:latest
echo "Exit code: $?"
echo
echo "Testing with help command..."
echo "help" | timeout 10 cargo run --bin aigpt-rs -- shell syui --provider ollama --model qwen2.5-coder:latest
echo "Exit code: $?"
echo
echo "Testing with AI message..."
echo "Hello AI" | timeout 10 cargo run --bin aigpt-rs -- shell syui --provider ollama --model qwen2.5-coder:latest
echo "Exit code: $?"
echo
echo "=== Manual shell tests completed ==="

246
src/ai_provider.rs Normal file
View File

@@ -0,0 +1,246 @@
use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AIProvider {
OpenAI,
Ollama,
Claude,
}
impl std::fmt::Display for AIProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AIProvider::OpenAI => write!(f, "openai"),
AIProvider::Ollama => write!(f, "ollama"),
AIProvider::Claude => write!(f, "claude"),
}
}
}
impl std::str::FromStr for AIProvider {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
match s.to_lowercase().as_str() {
"openai" | "gpt" => Ok(AIProvider::OpenAI),
"ollama" => Ok(AIProvider::Ollama),
"claude" => Ok(AIProvider::Claude),
_ => Err(anyhow!("Unknown AI provider: {}", s)),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AIConfig {
pub provider: AIProvider,
pub model: String,
pub api_key: Option<String>,
pub base_url: Option<String>,
pub max_tokens: Option<u32>,
pub temperature: Option<f32>,
}
impl Default for AIConfig {
fn default() -> Self {
AIConfig {
provider: AIProvider::Ollama,
model: "llama2".to_string(),
api_key: None,
base_url: Some("http://localhost:11434".to_string()),
max_tokens: Some(2048),
temperature: Some(0.7),
}
}
}
#[derive(Debug, Clone)]
pub struct ChatMessage {
pub role: String,
pub content: String,
}
#[derive(Debug, Clone)]
pub struct ChatResponse {
pub content: String,
pub tokens_used: Option<u32>,
pub model: String,
}
pub struct AIProviderClient {
config: AIConfig,
http_client: reqwest::Client,
}
impl AIProviderClient {
pub fn new(config: AIConfig) -> Self {
let http_client = reqwest::Client::new();
AIProviderClient {
config,
http_client,
}
}
pub async fn chat(&self, messages: Vec<ChatMessage>, system_prompt: Option<String>) -> Result<ChatResponse> {
match self.config.provider {
AIProvider::OpenAI => self.chat_openai(messages, system_prompt).await,
AIProvider::Ollama => self.chat_ollama(messages, system_prompt).await,
AIProvider::Claude => self.chat_claude(messages, system_prompt).await,
}
}
async fn chat_openai(&self, messages: Vec<ChatMessage>, system_prompt: Option<String>) -> Result<ChatResponse> {
let api_key = self.config.api_key.as_ref()
.ok_or_else(|| anyhow!("OpenAI API key required"))?;
let mut request_messages = Vec::new();
// Add system prompt if provided
if let Some(system) = system_prompt {
request_messages.push(serde_json::json!({
"role": "system",
"content": system
}));
}
// Add conversation messages
for msg in messages {
request_messages.push(serde_json::json!({
"role": msg.role,
"content": msg.content
}));
}
let request_body = serde_json::json!({
"model": self.config.model,
"messages": request_messages,
"max_tokens": self.config.max_tokens,
"temperature": self.config.temperature
});
let response = self.http_client
.post("https://api.openai.com/v1/chat/completions")
.header("Authorization", format!("Bearer {}", api_key))
.header("Content-Type", "application/json")
.json(&request_body)
.send()
.await?;
if !response.status().is_success() {
let error_text = response.text().await?;
return Err(anyhow!("OpenAI API error: {}", error_text));
}
let response_json: serde_json::Value = response.json().await?;
let content = response_json["choices"][0]["message"]["content"]
.as_str()
.ok_or_else(|| anyhow!("Invalid OpenAI response format"))?
.to_string();
let tokens_used = response_json["usage"]["total_tokens"]
.as_u64()
.map(|t| t as u32);
Ok(ChatResponse {
content,
tokens_used,
model: self.config.model.clone(),
})
}
async fn chat_ollama(&self, messages: Vec<ChatMessage>, system_prompt: Option<String>) -> Result<ChatResponse> {
let default_url = "http://localhost:11434".to_string();
let base_url = self.config.base_url.as_ref()
.unwrap_or(&default_url);
let mut request_messages = Vec::new();
// Add system prompt if provided
if let Some(system) = system_prompt {
request_messages.push(serde_json::json!({
"role": "system",
"content": system
}));
}
// Add conversation messages
for msg in messages {
request_messages.push(serde_json::json!({
"role": msg.role,
"content": msg.content
}));
}
let request_body = serde_json::json!({
"model": self.config.model,
"messages": request_messages,
"stream": false
});
let url = format!("{}/api/chat", base_url);
let response = self.http_client
.post(&url)
.header("Content-Type", "application/json")
.json(&request_body)
.send()
.await?;
if !response.status().is_success() {
let error_text = response.text().await?;
return Err(anyhow!("Ollama API error: {}", error_text));
}
let response_json: serde_json::Value = response.json().await?;
let content = response_json["message"]["content"]
.as_str()
.ok_or_else(|| anyhow!("Invalid Ollama response format"))?
.to_string();
Ok(ChatResponse {
content,
tokens_used: None, // Ollama doesn't typically return token counts
model: self.config.model.clone(),
})
}
async fn chat_claude(&self, _messages: Vec<ChatMessage>, _system_prompt: Option<String>) -> Result<ChatResponse> {
// Claude API implementation would go here
// For now, return a placeholder
Err(anyhow!("Claude provider not yet implemented"))
}
pub fn get_model(&self) -> &str {
&self.config.model
}
pub fn get_provider(&self) -> &AIProvider {
&self.config.provider
}
}
// Convenience functions for creating common message types
impl ChatMessage {
pub fn user(content: impl Into<String>) -> Self {
ChatMessage {
role: "user".to_string(),
content: content.into(),
}
}
pub fn assistant(content: impl Into<String>) -> Self {
ChatMessage {
role: "assistant".to_string(),
content: content.into(),
}
}
pub fn system(content: impl Into<String>) -> Self {
ChatMessage {
role: "system".to_string(),
content: content.into(),
}
}
}

54
src/bin/test_config.rs Normal file
View File

@@ -0,0 +1,54 @@
use aigpt::config::Config;
use anyhow::Result;
fn main() -> Result<()> {
println!("Testing configuration loading...");
// Debug: check which JSON files exist
let possible_paths = vec![
"../config.json",
"config.json",
"gpt/config.json",
"/Users/syui/ai/ai/gpt/config.json",
];
println!("Checking for config.json files:");
for path in &possible_paths {
let path_buf = std::path::PathBuf::from(path);
if path_buf.exists() {
println!(" ✓ Found: {}", path);
} else {
println!(" ✗ Not found: {}", path);
}
}
// Load configuration
let config = Config::new(None)?;
println!("Configuration loaded successfully!");
println!("Default provider: {}", config.default_provider);
println!("Available providers:");
for (name, provider) in &config.providers {
println!(" - {}: model={}, host={:?}",
name,
provider.default_model,
provider.host);
}
if let Some(mcp) = &config.mcp {
println!("\nMCP Configuration:");
println!(" Enabled: {}", mcp.enabled);
println!(" Auto-detect: {}", mcp.auto_detect);
println!(" Servers: {}", mcp.servers.len());
}
if let Some(atproto) = &config.atproto {
println!("\nATProto Configuration:");
println!(" Host: {}", atproto.host);
println!(" Handle: {:?}", atproto.handle);
}
println!("\nConfig file path: {}", config.data_dir.join("config.json").display());
Ok(())
}

36
src/cli/commands.rs Normal file
View File

@@ -0,0 +1,36 @@
use clap::Subcommand;
use std::path::PathBuf;
#[derive(Subcommand)]
pub enum TokenCommands {
/// Show Claude Code token usage summary and estimated costs
Summary {
/// Time period (today, week, month, all)
#[arg(long, default_value = "today")]
period: String,
/// Claude Code data directory path
#[arg(long)]
claude_dir: Option<PathBuf>,
/// Show detailed breakdown
#[arg(long)]
details: bool,
/// Output format (table, json)
#[arg(long, default_value = "table")]
format: String,
},
/// Show daily token usage breakdown
Daily {
/// Number of days to show
#[arg(long, default_value = "7")]
days: u32,
/// Claude Code data directory path
#[arg(long)]
claude_dir: Option<PathBuf>,
},
/// Check Claude Code data availability and basic stats
Status {
/// Claude Code data directory path
#[arg(long)]
claude_dir: Option<PathBuf>,
},
}

140
src/cli/mod.rs Normal file
View File

@@ -0,0 +1,140 @@
use std::path::PathBuf;
use anyhow::Result;
use crate::config::Config;
use crate::mcp_server::MCPServer;
use crate::persona::Persona;
use crate::transmission::TransmissionController;
use crate::scheduler::AIScheduler;
// Token commands enum (placeholder for tokens.rs)
#[derive(Debug, clap::Subcommand)]
pub enum TokenCommands {
Analyze { file: PathBuf },
Report { days: Option<u32> },
Cost { month: Option<String> },
Summary { period: Option<String>, claude_dir: Option<PathBuf>, details: bool, format: Option<String> },
Daily { days: Option<u32>, claude_dir: Option<PathBuf> },
Status { claude_dir: Option<PathBuf> },
}
pub async fn handle_server(port: Option<u16>, data_dir: Option<PathBuf>) -> Result<()> {
let port = port.unwrap_or(8080);
let config = Config::new(data_dir.clone())?;
let mut server = MCPServer::new(config, "mcp_user".to_string(), data_dir)?;
server.start_server(port).await
}
pub async fn handle_chat(
user_id: String,
message: String,
data_dir: Option<PathBuf>,
model: Option<String>,
provider: Option<String>,
) -> Result<()> {
let config = Config::new(data_dir)?;
let mut persona = Persona::new(&config)?;
let (response, relationship_delta) = if provider.is_some() || model.is_some() {
persona.process_ai_interaction(&user_id, &message, provider, model).await?
} else {
persona.process_interaction(&user_id, &message)?
};
println!("AI Response: {}", response);
println!("Relationship Change: {:+.2}", relationship_delta);
if let Some(relationship) = persona.get_relationship(&user_id) {
println!("Relationship Status: {} (Score: {:.2})",
relationship.status, relationship.score);
}
Ok(())
}
pub async fn handle_fortune(data_dir: Option<PathBuf>) -> Result<()> {
let config = Config::new(data_dir)?;
let persona = Persona::new(&config)?;
let state = persona.get_current_state()?;
println!("🔮 Today's Fortune: {}", state.fortune_value);
println!("😊 Current Mood: {}", state.current_mood);
println!("✨ Breakthrough Status: {}",
if state.breakthrough_triggered { "Active" } else { "Inactive" });
Ok(())
}
pub async fn handle_relationships(data_dir: Option<PathBuf>) -> Result<()> {
let config = Config::new(data_dir)?;
let persona = Persona::new(&config)?;
let relationships = persona.list_all_relationships();
if relationships.is_empty() {
println!("No relationships found.");
return Ok(());
}
println!("📊 Relationships ({}):", relationships.len());
for (user_id, rel) in relationships {
println!(" {} - {} (Score: {:.2}, Interactions: {})",
user_id, rel.status, rel.score, rel.total_interactions);
}
Ok(())
}
pub async fn handle_transmit(data_dir: Option<PathBuf>) -> Result<()> {
let config = Config::new(data_dir)?;
let mut persona = Persona::new(&config)?;
let mut transmission_controller = TransmissionController::new(config)?;
let autonomous = transmission_controller.check_autonomous_transmissions(&mut persona).await?;
let breakthrough = transmission_controller.check_breakthrough_transmissions(&mut persona).await?;
let maintenance = transmission_controller.check_maintenance_transmissions(&mut persona).await?;
let total = autonomous.len() + breakthrough.len() + maintenance.len();
println!("📡 Transmission Check Complete:");
println!(" Autonomous: {}", autonomous.len());
println!(" Breakthrough: {}", breakthrough.len());
println!(" Maintenance: {}", maintenance.len());
println!(" Total: {}", total);
Ok(())
}
pub async fn handle_maintenance(data_dir: Option<PathBuf>) -> Result<()> {
let config = Config::new(data_dir)?;
let mut persona = Persona::new(&config)?;
let mut transmission_controller = TransmissionController::new(config)?;
persona.daily_maintenance()?;
let maintenance_transmissions = transmission_controller.check_maintenance_transmissions(&mut persona).await?;
let stats = persona.get_relationship_stats();
println!("🔧 Daily maintenance completed");
println!("📤 Maintenance transmissions sent: {}", maintenance_transmissions.len());
println!("📊 Relationship stats: {:?}", stats);
Ok(())
}
pub async fn handle_schedule(data_dir: Option<PathBuf>) -> Result<()> {
let config = Config::new(data_dir)?;
let mut persona = Persona::new(&config)?;
let mut transmission_controller = TransmissionController::new(config.clone())?;
let mut scheduler = AIScheduler::new(&config)?;
let executions = scheduler.run_scheduled_tasks(&mut persona, &mut transmission_controller).await?;
let stats = scheduler.get_scheduler_stats();
println!("⏰ Scheduler run completed");
println!("📋 Tasks executed: {}", executions.len());
println!("📊 Stats: {} total tasks, {} enabled, {:.2}% success rate",
stats.total_tasks, stats.enabled_tasks, stats.success_rate);
Ok(())
}

250
src/config.rs Normal file
View File

@@ -0,0 +1,250 @@
use std::path::PathBuf;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use anyhow::{Result, Context};
use crate::ai_provider::{AIConfig, AIProvider};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
#[serde(skip)]
pub data_dir: PathBuf,
pub default_provider: String,
pub providers: HashMap<String, ProviderConfig>,
#[serde(default)]
pub atproto: Option<AtprotoConfig>,
#[serde(default)]
pub mcp: Option<McpConfig>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
pub default_model: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub host: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub api_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub system_prompt: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AtprotoConfig {
pub handle: Option<String>,
pub password: Option<String>,
pub host: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpConfig {
#[serde(deserialize_with = "string_to_bool")]
pub enabled: bool,
#[serde(deserialize_with = "string_to_bool")]
pub auto_detect: bool,
pub servers: HashMap<String, McpServerConfig>,
}
fn string_to_bool<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let s = String::deserialize(deserializer)?;
match s.as_str() {
"true" => Ok(true),
"false" => Ok(false),
_ => Err(serde::de::Error::custom("expected 'true' or 'false'")),
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpServerConfig {
pub base_url: String,
pub name: String,
#[serde(deserialize_with = "string_to_f64")]
pub timeout: f64,
pub endpoints: HashMap<String, String>,
}
fn string_to_f64<'de, D>(deserializer: D) -> Result<f64, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let s = String::deserialize(deserializer)?;
s.parse::<f64>().map_err(serde::de::Error::custom)
}
impl Config {
pub fn new(data_dir: Option<PathBuf>) -> Result<Self> {
let data_dir = data_dir.unwrap_or_else(|| {
dirs::config_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("syui")
.join("ai")
.join("gpt")
});
// Ensure data directory exists
std::fs::create_dir_all(&data_dir)
.context("Failed to create data directory")?;
let config_path = data_dir.join("config.json");
// Try to load existing config
if config_path.exists() {
let config_str = std::fs::read_to_string(&config_path)
.context("Failed to read config.json")?;
// Check if file is empty
if config_str.trim().is_empty() {
eprintln!("Config file is empty, will recreate from source");
} else {
match serde_json::from_str::<Config>(&config_str) {
Ok(mut config) => {
config.data_dir = data_dir;
// Check for environment variables if API keys are empty
if let Some(openai_config) = config.providers.get_mut("openai") {
if openai_config.api_key.as_ref().map_or(true, |key| key.is_empty()) {
openai_config.api_key = std::env::var("OPENAI_API_KEY").ok();
}
}
return Ok(config);
}
Err(e) => {
eprintln!("Failed to parse existing config.json: {}", e);
eprintln!("Will try to reload from source...");
}
}
}
}
// Check if we need to migrate from JSON
// Try multiple locations for the JSON file
let possible_json_paths = vec![
PathBuf::from("../config.json"), // Relative to aigpt-rs directory
PathBuf::from("config.json"), // Current directory
PathBuf::from("gpt/config.json"), // From project root
PathBuf::from("/Users/syui/ai/ai/gpt/config.json"), // Absolute path
];
for json_path in possible_json_paths {
if json_path.exists() {
eprintln!("Found config.json at: {}", json_path.display());
eprintln!("Copying configuration...");
// Copy configuration file and parse it
std::fs::copy(&json_path, &config_path)
.context("Failed to copy config.json")?;
let config_str = std::fs::read_to_string(&config_path)
.context("Failed to read copied config.json")?;
println!("Config JSON content preview: {}", &config_str[..std::cmp::min(200, config_str.len())]);
let mut config: Config = serde_json::from_str(&config_str)
.context("Failed to parse config.json")?;
config.data_dir = data_dir;
// Check for environment variables if API keys are empty
if let Some(openai_config) = config.providers.get_mut("openai") {
if openai_config.api_key.as_ref().map_or(true, |key| key.is_empty()) {
openai_config.api_key = std::env::var("OPENAI_API_KEY").ok();
}
}
eprintln!("Copy complete! Config saved to: {}", config_path.display());
return Ok(config);
}
}
// Create default config
let config = Self::default_config(data_dir);
// Save default config
let json_str = serde_json::to_string_pretty(&config)
.context("Failed to serialize default config")?;
std::fs::write(&config_path, json_str)
.context("Failed to write default config.json")?;
Ok(config)
}
pub fn save(&self) -> Result<()> {
let config_path = self.data_dir.join("config.json");
let json_str = serde_json::to_string_pretty(self)
.context("Failed to serialize config")?;
std::fs::write(&config_path, json_str)
.context("Failed to write config.json")?;
Ok(())
}
fn default_config(data_dir: PathBuf) -> Self {
let mut providers = HashMap::new();
providers.insert("ollama".to_string(), ProviderConfig {
default_model: "qwen2.5".to_string(),
host: Some("http://localhost:11434".to_string()),
api_key: None,
system_prompt: None,
});
providers.insert("openai".to_string(), ProviderConfig {
default_model: "gpt-4o-mini".to_string(),
host: None,
api_key: std::env::var("OPENAI_API_KEY").ok(),
system_prompt: None,
});
Config {
data_dir,
default_provider: "ollama".to_string(),
providers,
atproto: None,
mcp: None,
}
}
pub fn get_provider(&self, provider_name: &str) -> Option<&ProviderConfig> {
self.providers.get(provider_name)
}
pub fn get_ai_config(&self, provider: Option<String>, model: Option<String>) -> Result<AIConfig> {
let provider_name = provider.as_deref().unwrap_or(&self.default_provider);
let provider_config = self.get_provider(provider_name)
.ok_or_else(|| anyhow::anyhow!("Unknown provider: {}", provider_name))?;
let ai_provider: AIProvider = provider_name.parse()?;
let model_name = model.unwrap_or_else(|| provider_config.default_model.clone());
Ok(AIConfig {
provider: ai_provider,
model: model_name,
api_key: provider_config.api_key.clone(),
base_url: provider_config.host.clone(),
max_tokens: Some(2048),
temperature: Some(0.7),
})
}
pub fn memory_file(&self) -> PathBuf {
self.data_dir.join("memories.json")
}
pub fn relationships_file(&self) -> PathBuf {
self.data_dir.join("relationships.json")
}
pub fn fortune_file(&self) -> PathBuf {
self.data_dir.join("fortune.json")
}
pub fn transmission_file(&self) -> PathBuf {
self.data_dir.join("transmissions.json")
}
pub fn scheduler_tasks_file(&self) -> PathBuf {
self.data_dir.join("scheduler_tasks.json")
}
pub fn scheduler_history_file(&self) -> PathBuf {
self.data_dir.join("scheduler_history.json")
}
}

205
src/conversation.rs Normal file
View File

@@ -0,0 +1,205 @@
use std::path::PathBuf;
use std::io::{self, Write};
use anyhow::Result;
use colored::*;
use crate::config::Config;
use crate::persona::Persona;
use crate::http_client::ServiceDetector;
pub async fn handle_conversation(
user_id: String,
data_dir: Option<PathBuf>,
model: Option<String>,
provider: Option<String>,
) -> Result<()> {
let config = Config::new(data_dir)?;
let mut persona = Persona::new(&config)?;
println!("{}", "Starting conversation mode...".cyan());
println!("{}", "Type your message and press Enter to chat.".yellow());
println!("{}", "Available MCP commands: /memories, /search, /context, /relationship, /cards".yellow());
println!("{}", "Type 'exit', 'quit', or 'bye' to end conversation.".yellow());
println!("{}", "---".dimmed());
let mut conversation_history = Vec::new();
let service_detector = ServiceDetector::new();
loop {
// Print prompt
print!("{} ", "You:".cyan().bold());
io::stdout().flush()?;
// Read user input
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim();
// Check for exit commands
if matches!(input.to_lowercase().as_str(), "exit" | "quit" | "bye" | "") {
println!("{}", "Goodbye! 👋".green());
break;
}
// Handle MCP commands
if input.starts_with('/') {
handle_mcp_command(input, &user_id, &service_detector).await?;
continue;
}
// Add to conversation history
conversation_history.push(format!("User: {}", input));
// Get AI response
let (response, relationship_delta) = if provider.is_some() || model.is_some() {
persona.process_ai_interaction(&user_id, input, provider.clone(), model.clone()).await?
} else {
persona.process_interaction(&user_id, input)?
};
// Add AI response to history
conversation_history.push(format!("AI: {}", response));
// Display response
println!("{} {}", "AI:".green().bold(), response);
// Show relationship change if significant
if relationship_delta.abs() >= 0.1 {
if relationship_delta > 0.0 {
println!("{}", format!(" └─ (+{:.2} relationship)", relationship_delta).green().dimmed());
} else {
println!("{}", format!(" └─ ({:.2} relationship)", relationship_delta).red().dimmed());
}
}
println!(); // Add some spacing
// Keep conversation history manageable (last 20 exchanges)
if conversation_history.len() > 40 {
conversation_history.drain(0..20);
}
}
Ok(())
}
async fn handle_mcp_command(
command: &str,
user_id: &str,
service_detector: &ServiceDetector,
) -> Result<()> {
let parts: Vec<&str> = command[1..].split_whitespace().collect();
if parts.is_empty() {
return Ok(());
}
match parts[0] {
"memories" => {
println!("{}", "Retrieving memories...".yellow());
// Get contextual memories
if let Ok(memories) = service_detector.get_contextual_memories(user_id, 10).await {
if memories.is_empty() {
println!("No memories found for this conversation.");
} else {
println!("{}", format!("Found {} memories:", memories.len()).cyan());
for (i, memory) in memories.iter().enumerate() {
println!(" {}. {}", i + 1, memory.content);
println!(" {}", format!("({})", memory.created_at.format("%Y-%m-%d %H:%M")).dimmed());
}
}
} else {
println!("{}", "Failed to retrieve memories.".red());
}
},
"search" => {
if parts.len() < 2 {
println!("{}", "Usage: /search <query>".yellow());
return Ok(());
}
let query = parts[1..].join(" ");
println!("{}", format!("Searching for: '{}'", query).yellow());
if let Ok(results) = service_detector.search_memories(&query, 5).await {
if results.is_empty() {
println!("No relevant memories found.");
} else {
println!("{}", format!("Found {} relevant memories:", results.len()).cyan());
for (i, memory) in results.iter().enumerate() {
println!(" {}. {}", i + 1, memory.content);
println!(" {}", format!("({})", memory.created_at.format("%Y-%m-%d %H:%M")).dimmed());
}
}
} else {
println!("{}", "Search failed.".red());
}
},
"context" => {
println!("{}", "Creating context summary...".yellow());
if let Ok(summary) = service_detector.create_summary(user_id).await {
println!("{}", "Context Summary:".cyan().bold());
println!("{}", summary);
} else {
println!("{}", "Failed to create context summary.".red());
}
},
"relationship" => {
println!("{}", "Checking relationship status...".yellow());
// This would need to be implemented in the service client
println!("{}", "Relationship status: Active".cyan());
println!("Score: 85.5 / 100");
println!("Transmission: ✓ Enabled");
},
"cards" => {
println!("{}", "Checking card collection...".yellow());
// Try to connect to ai.card service
if let Ok(stats) = service_detector.get_card_stats().await {
println!("{}", "Card Collection:".cyan().bold());
println!(" Total Cards: {}", stats.get("total").unwrap_or(&serde_json::Value::Number(0.into())));
println!(" Unique Cards: {}", stats.get("unique").unwrap_or(&serde_json::Value::Number(0.into())));
// Offer to draw a card
println!("\n{}", "Would you like to draw a card? (y/n)".yellow());
let mut response = String::new();
io::stdin().read_line(&mut response)?;
if response.trim().to_lowercase() == "y" {
println!("{}", "Drawing card...".cyan());
if let Ok(card) = service_detector.draw_card(user_id, false).await {
println!("{}", "🎴 Card drawn!".green().bold());
println!("Name: {}", card.get("name").unwrap_or(&serde_json::Value::String("Unknown".to_string())));
println!("Rarity: {}", card.get("rarity").unwrap_or(&serde_json::Value::String("Unknown".to_string())));
} else {
println!("{}", "Failed to draw card. ai.card service might not be running.".red());
}
}
} else {
println!("{}", "ai.card service not available.".red());
}
},
"help" | "h" => {
println!("{}", "Available MCP Commands:".cyan().bold());
println!(" {:<15} - Show recent memories for this conversation", "/memories".yellow());
println!(" {:<15} - Search memories by keyword", "/search <query>".yellow());
println!(" {:<15} - Create a context summary", "/context".yellow());
println!(" {:<15} - Show relationship status", "/relationship".yellow());
println!(" {:<15} - Show card collection and draw cards", "/cards".yellow());
println!(" {:<15} - Show this help message", "/help".yellow());
},
_ => {
println!("{}", format!("Unknown command: /{}. Type '/help' for available commands.", parts[0]).red());
}
}
println!(); // Add spacing after MCP command output
Ok(())
}

View File

@@ -1,161 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
/// User personality analysis based on Big Five model (OCEAN)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserAnalysis {
/// Unique identifier using ULID
pub id: String,
/// Openness to Experience (0.0-1.0)
/// Curiosity, imagination, willingness to try new things
pub openness: f32,
/// Conscientiousness (0.0-1.0)
/// Organization, responsibility, self-discipline
pub conscientiousness: f32,
/// Extraversion (0.0-1.0)
/// Sociability, assertiveness, energy level
pub extraversion: f32,
/// Agreeableness (0.0-1.0)
/// Compassion, cooperation, trust
pub agreeableness: f32,
/// Neuroticism (0.0-1.0)
/// Emotional stability, anxiety, mood swings
pub neuroticism: f32,
/// AI-generated summary of the personality analysis
pub summary: String,
/// When this analysis was performed
pub analyzed_at: DateTime<Utc>,
}
impl UserAnalysis {
/// Create a new personality analysis
pub fn new(
openness: f32,
conscientiousness: f32,
extraversion: f32,
agreeableness: f32,
neuroticism: f32,
summary: String,
) -> Self {
let id = Ulid::new().to_string();
let analyzed_at = Utc::now();
Self {
id,
openness: openness.clamp(0.0, 1.0),
conscientiousness: conscientiousness.clamp(0.0, 1.0),
extraversion: extraversion.clamp(0.0, 1.0),
agreeableness: agreeableness.clamp(0.0, 1.0),
neuroticism: neuroticism.clamp(0.0, 1.0),
summary,
analyzed_at,
}
}
/// Get the dominant trait (highest score)
pub fn dominant_trait(&self) -> &str {
let scores = [
(self.openness, "Openness"),
(self.conscientiousness, "Conscientiousness"),
(self.extraversion, "Extraversion"),
(self.agreeableness, "Agreeableness"),
(self.neuroticism, "Neuroticism"),
];
scores
.iter()
.max_by(|a, b| a.0.partial_cmp(&b.0).unwrap())
.map(|(_, name)| *name)
.unwrap_or("Unknown")
}
/// Check if a trait is high (>= 0.6)
pub fn is_high(&self, trait_name: &str) -> bool {
let score = match trait_name.to_lowercase().as_str() {
"openness" | "o" => self.openness,
"conscientiousness" | "c" => self.conscientiousness,
"extraversion" | "e" => self.extraversion,
"agreeableness" | "a" => self.agreeableness,
"neuroticism" | "n" => self.neuroticism,
_ => return false,
};
score >= 0.6
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_analysis() {
let analysis = UserAnalysis::new(
0.8,
0.7,
0.4,
0.6,
0.3,
"Test summary".to_string(),
);
assert_eq!(analysis.openness, 0.8);
assert_eq!(analysis.conscientiousness, 0.7);
assert_eq!(analysis.extraversion, 0.4);
assert_eq!(analysis.agreeableness, 0.6);
assert_eq!(analysis.neuroticism, 0.3);
assert!(!analysis.id.is_empty());
}
#[test]
fn test_score_clamping() {
let analysis = UserAnalysis::new(
1.5, // Should clamp to 1.0
-0.2, // Should clamp to 0.0
0.5,
0.5,
0.5,
"Test".to_string(),
);
assert_eq!(analysis.openness, 1.0);
assert_eq!(analysis.conscientiousness, 0.0);
}
#[test]
fn test_dominant_trait() {
let analysis = UserAnalysis::new(
0.9, // Highest
0.5,
0.4,
0.6,
0.3,
"Test".to_string(),
);
assert_eq!(analysis.dominant_trait(), "Openness");
}
#[test]
fn test_is_high() {
let analysis = UserAnalysis::new(
0.8, // High
0.4, // Low
0.6, // Threshold
0.5,
0.3,
"Test".to_string(),
);
assert!(analysis.is_high("openness"));
assert!(!analysis.is_high("conscientiousness"));
assert!(analysis.is_high("extraversion")); // 0.6 is high
}
}

View File

@@ -1,27 +0,0 @@
use thiserror::Error;
#[derive(Error, Debug)]
pub enum MemoryError {
#[error("Database error: {0}")]
Database(#[from] rusqlite::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Memory not found: {0}")]
NotFound(String),
#[error("Invalid ULID: {0}")]
InvalidId(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Parse error: {0}")]
Parse(String),
}
pub type Result<T> = std::result::Result<T, MemoryError>;

View File

@@ -1,181 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
/// Represents a single memory entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Memory {
/// Unique identifier using ULID (time-sortable)
pub id: String,
/// The actual content of the memory
pub content: String,
/// AI's creative interpretation of the content (Layer 2)
#[serde(skip_serializing_if = "Option::is_none")]
pub ai_interpretation: Option<String>,
/// Priority score evaluated by AI: 0.0 (low) to 1.0 (high) (Layer 2)
#[serde(skip_serializing_if = "Option::is_none")]
pub priority_score: Option<f32>,
/// Related entities (people, places, things) involved in this memory (Layer 4)
#[serde(skip_serializing_if = "Option::is_none")]
pub related_entities: Option<Vec<String>>,
/// When this memory was created
pub created_at: DateTime<Utc>,
/// When this memory was last updated
pub updated_at: DateTime<Utc>,
}
impl Memory {
/// Create a new memory with generated ULID (Layer 1)
pub fn new(content: String) -> Self {
let now = Utc::now();
let id = Ulid::new().to_string();
Self {
id,
content,
ai_interpretation: None,
priority_score: None,
related_entities: None,
created_at: now,
updated_at: now,
}
}
/// Create a new AI-interpreted memory (Layer 2)
pub fn new_ai(
content: String,
ai_interpretation: Option<String>,
priority_score: Option<f32>,
) -> Self {
let now = Utc::now();
let id = Ulid::new().to_string();
Self {
id,
content,
ai_interpretation,
priority_score,
related_entities: None,
created_at: now,
updated_at: now,
}
}
/// Create a new memory with related entities (Layer 4)
pub fn new_with_entities(
content: String,
ai_interpretation: Option<String>,
priority_score: Option<f32>,
related_entities: Option<Vec<String>>,
) -> Self {
let now = Utc::now();
let id = Ulid::new().to_string();
Self {
id,
content,
ai_interpretation,
priority_score,
related_entities,
created_at: now,
updated_at: now,
}
}
/// Update the content of this memory
pub fn update_content(&mut self, content: String) {
self.content = content;
self.updated_at = Utc::now();
}
/// Set or update AI interpretation
pub fn set_ai_interpretation(&mut self, interpretation: String) {
self.ai_interpretation = Some(interpretation);
self.updated_at = Utc::now();
}
/// Set or update priority score
pub fn set_priority_score(&mut self, score: f32) {
self.priority_score = Some(score.clamp(0.0, 1.0));
self.updated_at = Utc::now();
}
/// Set or update related entities
pub fn set_related_entities(&mut self, entities: Vec<String>) {
self.related_entities = Some(entities);
self.updated_at = Utc::now();
}
/// Check if this memory is related to a specific entity
pub fn has_entity(&self, entity_id: &str) -> bool {
self.related_entities
.as_ref()
.map(|entities| entities.iter().any(|e| e == entity_id))
.unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_memory() {
let memory = Memory::new("Test content".to_string());
assert_eq!(memory.content, "Test content");
assert!(!memory.id.is_empty());
assert!(memory.ai_interpretation.is_none());
assert!(memory.priority_score.is_none());
}
#[test]
fn test_new_ai_memory() {
let memory = Memory::new_ai(
"Test content".to_string(),
Some("AI interpretation".to_string()),
Some(0.75),
);
assert_eq!(memory.content, "Test content");
assert_eq!(memory.ai_interpretation, Some("AI interpretation".to_string()));
assert_eq!(memory.priority_score, Some(0.75));
}
#[test]
fn test_update_memory() {
let mut memory = Memory::new("Original".to_string());
let original_time = memory.updated_at;
std::thread::sleep(std::time::Duration::from_millis(10));
memory.update_content("Updated".to_string());
assert_eq!(memory.content, "Updated");
assert!(memory.updated_at > original_time);
}
#[test]
fn test_set_ai_interpretation() {
let mut memory = Memory::new("Test".to_string());
memory.set_ai_interpretation("Interpretation".to_string());
assert_eq!(memory.ai_interpretation, Some("Interpretation".to_string()));
}
#[test]
fn test_set_priority_score() {
let mut memory = Memory::new("Test".to_string());
memory.set_priority_score(0.8);
assert_eq!(memory.priority_score, Some(0.8));
// Test clamping
memory.set_priority_score(1.5);
assert_eq!(memory.priority_score, Some(1.0));
memory.set_priority_score(-0.5);
assert_eq!(memory.priority_score, Some(0.0));
}
}

View File

@@ -1,13 +0,0 @@
pub mod analysis;
pub mod error;
pub mod memory;
pub mod profile;
pub mod relationship;
pub mod store;
pub use analysis::UserAnalysis;
pub use error::{MemoryError, Result};
pub use memory::Memory;
pub use profile::{UserProfile, TraitScore};
pub use relationship::{RelationshipInference, infer_all_relationships, get_relationship};
pub use store::MemoryStore;

View File

@@ -1,275 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::core::{MemoryStore, UserAnalysis};
use crate::core::error::Result;
/// Integrated user profile - the essence of Layer 1-3 data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserProfile {
/// Dominant personality traits (top 2-3 from Big Five)
pub dominant_traits: Vec<TraitScore>,
/// Core interests (most frequent topics from memories)
pub core_interests: Vec<String>,
/// Core values (extracted from high-priority memories)
pub core_values: Vec<String>,
/// Key memory IDs (top priority memories as evidence)
pub key_memory_ids: Vec<String>,
/// Data quality score (0.0-1.0 based on data volume)
pub data_quality: f32,
/// Last update timestamp
pub last_updated: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraitScore {
pub name: String,
pub score: f32,
}
impl UserProfile {
/// Generate integrated profile from Layer 1-3 data
pub fn generate(store: &MemoryStore) -> Result<Self> {
// Get latest personality analysis (Layer 3)
let personality = store.get_latest_analysis()?;
// Get all memories (Layer 1-2)
let memories = store.list()?;
// Extract dominant traits from Big Five
let dominant_traits = extract_dominant_traits(&personality);
// Extract core interests from memory content
let core_interests = extract_core_interests(&memories);
// Extract core values from high-priority memories
let core_values = extract_core_values(&memories);
// Get top priority memory IDs
let key_memory_ids = extract_key_memories(&memories);
// Calculate data quality
let data_quality = calculate_data_quality(&memories, &personality);
Ok(UserProfile {
dominant_traits,
core_interests,
core_values,
key_memory_ids,
data_quality,
last_updated: Utc::now(),
})
}
/// Check if profile needs update
pub fn needs_update(&self, store: &MemoryStore) -> Result<bool> {
// Update if 7+ days old
let days_old = (Utc::now() - self.last_updated).num_days();
if days_old >= 7 {
return Ok(true);
}
// Update if 10+ new memories since last update
let memory_count = store.count()?;
let expected_count = self.key_memory_ids.len() * 2; // Rough estimate
if memory_count > expected_count + 10 {
return Ok(true);
}
// Update if new personality analysis exists
if let Some(latest) = store.get_latest_analysis()? {
if latest.analyzed_at > self.last_updated {
return Ok(true);
}
}
Ok(false)
}
}
/// Extract top 2-3 personality traits from Big Five
fn extract_dominant_traits(analysis: &Option<UserAnalysis>) -> Vec<TraitScore> {
if analysis.is_none() {
return vec![];
}
let analysis = analysis.as_ref().unwrap();
let mut traits = vec![
TraitScore { name: "openness".to_string(), score: analysis.openness },
TraitScore { name: "conscientiousness".to_string(), score: analysis.conscientiousness },
TraitScore { name: "extraversion".to_string(), score: analysis.extraversion },
TraitScore { name: "agreeableness".to_string(), score: analysis.agreeableness },
TraitScore { name: "neuroticism".to_string(), score: analysis.neuroticism },
];
// Sort by score descending
traits.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
// Return top 3
traits.into_iter().take(3).collect()
}
/// Extract core interests from memory content (frequency analysis)
fn extract_core_interests(memories: &[crate::core::Memory]) -> Vec<String> {
let mut word_freq: HashMap<String, usize> = HashMap::new();
for memory in memories {
// Extract keywords from content
let words = extract_keywords(&memory.content);
for word in words {
*word_freq.entry(word).or_insert(0) += 1;
}
// Also consider AI interpretation if available
if let Some(ref interpretation) = memory.ai_interpretation {
let words = extract_keywords(interpretation);
for word in words {
*word_freq.entry(word).or_insert(0) += 2; // Weight interpretation higher
}
}
}
// Sort by frequency and take top 5
let mut freq_vec: Vec<_> = word_freq.into_iter().collect();
freq_vec.sort_by(|a, b| b.1.cmp(&a.1));
freq_vec.into_iter()
.take(5)
.map(|(word, _)| word)
.collect()
}
/// Extract core values from high-priority memories
fn extract_core_values(memories: &[crate::core::Memory]) -> Vec<String> {
// Filter high-priority memories (>= 0.7)
let high_priority: Vec<_> = memories.iter()
.filter(|m| m.priority_score.map(|s| s >= 0.7).unwrap_or(false))
.collect();
if high_priority.is_empty() {
return vec![];
}
let mut value_freq: HashMap<String, usize> = HashMap::new();
for memory in high_priority {
// Extract value keywords from interpretation
if let Some(ref interpretation) = memory.ai_interpretation {
let values = extract_value_keywords(interpretation);
for value in values {
*value_freq.entry(value).or_insert(0) += 1;
}
}
}
// Sort by frequency and take top 5
let mut freq_vec: Vec<_> = value_freq.into_iter().collect();
freq_vec.sort_by(|a, b| b.1.cmp(&a.1));
freq_vec.into_iter()
.take(5)
.map(|(value, _)| value)
.collect()
}
/// Extract key memory IDs (top priority)
fn extract_key_memories(memories: &[crate::core::Memory]) -> Vec<String> {
let mut sorted_memories: Vec<_> = memories.iter()
.filter(|m| m.priority_score.is_some())
.collect();
sorted_memories.sort_by(|a, b| {
b.priority_score.unwrap()
.partial_cmp(&a.priority_score.unwrap())
.unwrap()
});
sorted_memories.into_iter()
.take(10)
.map(|m| m.id.clone())
.collect()
}
/// Calculate data quality based on volume
fn calculate_data_quality(memories: &[crate::core::Memory], personality: &Option<UserAnalysis>) -> f32 {
let memory_count = memories.len() as f32;
let has_personality = if personality.is_some() { 1.0 } else { 0.0 };
// Quality increases with data volume
let memory_quality = (memory_count / 50.0).min(1.0); // Max quality at 50+ memories
let personality_quality = has_personality * 0.5;
// Weighted average
(memory_quality * 0.5 + personality_quality).min(1.0)
}
/// Extract keywords from text (simple word frequency)
fn extract_keywords(text: &str) -> Vec<String> {
// Simple keyword extraction: words longer than 3 chars
text.split_whitespace()
.filter(|w| w.len() > 3)
.map(|w| w.to_lowercase().trim_matches(|c: char| !c.is_alphanumeric()).to_string())
.filter(|w| !is_stopword(w))
.collect()
}
/// Extract value-related keywords from interpretation
fn extract_value_keywords(text: &str) -> Vec<String> {
let value_indicators = [
"重視", "大切", "価値", "重要", "優先", "好む", "志向",
"シンプル", "効率", "品質", "安定", "革新", "創造",
"value", "important", "priority", "prefer", "focus",
"simple", "efficient", "quality", "stable", "creative",
];
let words = extract_keywords(text);
words.into_iter()
.filter(|w| {
value_indicators.iter().any(|indicator| w.contains(indicator))
})
.collect()
}
/// Check if word is a stopword
fn is_stopword(word: &str) -> bool {
let stopwords = [
"the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for",
"of", "with", "by", "from", "as", "is", "was", "are", "were", "been",
"be", "have", "has", "had", "do", "does", "did", "will", "would", "could",
"should", "may", "might", "can", "this", "that", "these", "those",
"です", "ます", "ました", "である", "ある", "いる", "する", "した",
"という", "として", "ために", "によって", "について",
];
stopwords.contains(&word)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_keywords() {
let text = "Rust architecture design is important for scalability";
let keywords = extract_keywords(text);
assert!(keywords.contains(&"rust".to_string()));
assert!(keywords.contains(&"architecture".to_string()));
assert!(keywords.contains(&"design".to_string()));
assert!(!keywords.contains(&"is".to_string())); // stopword
}
#[test]
fn test_stopword() {
assert!(is_stopword("the"));
assert!(is_stopword("です"));
assert!(!is_stopword("rust"));
}
}

View File

@@ -1,317 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::core::{Memory, MemoryStore, UserProfile};
use crate::core::error::Result;
/// Inferred relationship with an entity (Layer 4)
///
/// This is not stored permanently but generated on-demand from
/// Layer 1 memories and Layer 3.5 user profile.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelationshipInference {
/// Entity identifier
pub entity_id: String,
/// Total interaction count with this entity
pub interaction_count: u32,
/// Average priority score of memories with this entity
pub avg_priority: f32,
/// Days since last interaction
pub days_since_last: i64,
/// Inferred bond strength (0.0-1.0)
pub bond_strength: f32,
/// Inferred relationship type
pub relationship_type: String,
/// Confidence in this inference (0.0-1.0, based on data volume)
pub confidence: f32,
/// When this inference was generated
pub inferred_at: DateTime<Utc>,
}
impl RelationshipInference {
/// Infer relationship from memories and user profile
pub fn infer(
entity_id: String,
memories: &[Memory],
user_profile: &UserProfile,
) -> Self {
// Filter memories related to this entity
let entity_memories: Vec<_> = memories
.iter()
.filter(|m| m.has_entity(&entity_id))
.collect();
let interaction_count = entity_memories.len() as u32;
// Calculate average priority
let total_priority: f32 = entity_memories
.iter()
.filter_map(|m| m.priority_score)
.sum();
let priority_count = entity_memories
.iter()
.filter(|m| m.priority_score.is_some())
.count() as f32;
let avg_priority = if priority_count > 0.0 {
total_priority / priority_count
} else {
0.5 // Default to neutral if no scores
};
// Calculate days since last interaction
let days_since_last = entity_memories
.iter()
.map(|m| (Utc::now() - m.created_at).num_days())
.min()
.unwrap_or(999);
// Infer bond strength based on user personality
let bond_strength = Self::calculate_bond_strength(
interaction_count,
avg_priority,
user_profile,
);
// Infer relationship type
let relationship_type = Self::infer_relationship_type(
interaction_count,
avg_priority,
bond_strength,
);
// Calculate confidence
let confidence = Self::calculate_confidence(interaction_count);
RelationshipInference {
entity_id,
interaction_count,
avg_priority,
days_since_last,
bond_strength,
relationship_type,
confidence,
inferred_at: Utc::now(),
}
}
/// Calculate bond strength from interaction data and user personality
fn calculate_bond_strength(
interaction_count: u32,
avg_priority: f32,
user_profile: &UserProfile,
) -> f32 {
// Extract extraversion score (if available)
let extraversion = user_profile
.dominant_traits
.iter()
.find(|t| t.name == "extraversion")
.map(|t| t.score)
.unwrap_or(0.5);
let bond_strength = if extraversion < 0.5 {
// Introverted: fewer but deeper relationships
// Interaction count matters more
let count_factor = (interaction_count as f32 / 20.0).min(1.0);
let priority_factor = avg_priority;
// Weight: 60% count, 40% priority
count_factor * 0.6 + priority_factor * 0.4
} else {
// Extroverted: many relationships, quality varies
// Priority matters more
let count_factor = (interaction_count as f32 / 50.0).min(1.0);
let priority_factor = avg_priority;
// Weight: 40% count, 60% priority
count_factor * 0.4 + priority_factor * 0.6
};
bond_strength.clamp(0.0, 1.0)
}
/// Infer relationship type from metrics
fn infer_relationship_type(
interaction_count: u32,
avg_priority: f32,
bond_strength: f32,
) -> String {
if bond_strength >= 0.8 {
"close_friend".to_string()
} else if bond_strength >= 0.6 {
"friend".to_string()
} else if bond_strength >= 0.4 {
if avg_priority >= 0.6 {
"valued_acquaintance".to_string()
} else {
"acquaintance".to_string()
}
} else if interaction_count >= 5 {
"regular_contact".to_string()
} else {
"distant".to_string()
}
}
/// Calculate confidence based on data volume
fn calculate_confidence(interaction_count: u32) -> f32 {
// Confidence increases with more data
// 1-2 interactions: low confidence (0.2-0.3)
// 5 interactions: medium confidence (0.5)
// 10+ interactions: high confidence (0.8+)
let confidence = match interaction_count {
0 => 0.0,
1 => 0.2,
2 => 0.3,
3 => 0.4,
4 => 0.45,
5..=9 => 0.5 + (interaction_count - 5) as f32 * 0.05,
_ => 0.8 + ((interaction_count - 10) as f32 * 0.02).min(0.2),
};
confidence.clamp(0.0, 1.0)
}
}
/// Generate relationship inferences for all entities in memories
pub fn infer_all_relationships(
store: &MemoryStore,
) -> Result<Vec<RelationshipInference>> {
// Check cache first
if let Some(cached) = store.get_cached_all_relationships()? {
return Ok(cached);
}
// Get all memories
let memories = store.list()?;
// Get user profile
let user_profile = store.get_profile()?;
// Extract all unique entities
let mut entities: HashMap<String, ()> = HashMap::new();
for memory in &memories {
if let Some(ref entity_list) = memory.related_entities {
for entity in entity_list {
entities.insert(entity.clone(), ());
}
}
}
// Infer relationship for each entity
let mut relationships: Vec<_> = entities
.keys()
.map(|entity_id| {
RelationshipInference::infer(
entity_id.clone(),
&memories,
&user_profile,
)
})
.collect();
// Sort by bond strength (descending)
relationships.sort_by(|a, b| {
b.bond_strength
.partial_cmp(&a.bond_strength)
.unwrap_or(std::cmp::Ordering::Equal)
});
// Cache the result
store.save_all_relationships_cache(&relationships)?;
Ok(relationships)
}
/// Get relationship inference for a specific entity (with caching)
pub fn get_relationship(
store: &MemoryStore,
entity_id: &str,
) -> Result<RelationshipInference> {
// Check cache first
if let Some(cached) = store.get_cached_relationship(entity_id)? {
return Ok(cached);
}
// Get all memories
let memories = store.list()?;
// Get user profile
let user_profile = store.get_profile()?;
// Infer relationship
let relationship = RelationshipInference::infer(
entity_id.to_string(),
&memories,
&user_profile,
);
// Cache it
store.save_relationship_cache(entity_id, &relationship)?;
Ok(relationship)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::profile::TraitScore;
#[test]
fn test_confidence_calculation() {
assert_eq!(RelationshipInference::calculate_confidence(0), 0.0);
assert_eq!(RelationshipInference::calculate_confidence(1), 0.2);
assert_eq!(RelationshipInference::calculate_confidence(5), 0.5);
assert!(RelationshipInference::calculate_confidence(10) >= 0.8);
}
#[test]
fn test_relationship_type() {
assert_eq!(
RelationshipInference::infer_relationship_type(20, 0.9, 0.85),
"close_friend"
);
assert_eq!(
RelationshipInference::infer_relationship_type(10, 0.7, 0.65),
"friend"
);
assert_eq!(
RelationshipInference::infer_relationship_type(5, 0.5, 0.45),
"acquaintance"
);
}
#[test]
fn test_bond_strength_introverted() {
let user_profile = UserProfile {
dominant_traits: vec![
TraitScore {
name: "extraversion".to_string(),
score: 0.3, // Introverted
},
],
core_interests: vec![],
core_values: vec![],
key_memory_ids: vec![],
data_quality: 1.0,
last_updated: Utc::now(),
};
// Introverted: count matters more
let strength = RelationshipInference::calculate_bond_strength(
20, // Many interactions
0.5, // Medium priority
&user_profile,
);
// Should be high due to high interaction count
assert!(strength > 0.5);
}
}

View File

@@ -1,693 +0,0 @@
use chrono::{DateTime, Utc};
use rusqlite::{params, Connection};
use std::path::PathBuf;
use super::analysis::UserAnalysis;
use super::error::{MemoryError, Result};
use super::memory::Memory;
/// SQLite-based memory storage
pub struct MemoryStore {
conn: Connection,
}
impl MemoryStore {
/// Create a new MemoryStore with the given database path
pub fn new(db_path: PathBuf) -> Result<Self> {
// Ensure parent directory exists
if let Some(parent) = db_path.parent() {
std::fs::create_dir_all(parent)?;
}
let conn = Connection::open(db_path)?;
// Initialize database schema
conn.execute(
"CREATE TABLE IF NOT EXISTS memories (
id TEXT PRIMARY KEY,
content TEXT NOT NULL,
ai_interpretation TEXT,
priority_score REAL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
)",
[],
)?;
// Migrate existing tables (add columns if they don't exist)
// SQLite doesn't have "IF NOT EXISTS" for columns, so we check first
let has_ai_interpretation: bool = conn
.prepare("SELECT COUNT(*) FROM pragma_table_info('memories') WHERE name='ai_interpretation'")?
.query_row([], |row| row.get(0))
.map(|count: i32| count > 0)?;
if !has_ai_interpretation {
conn.execute("ALTER TABLE memories ADD COLUMN ai_interpretation TEXT", [])?;
conn.execute("ALTER TABLE memories ADD COLUMN priority_score REAL", [])?;
}
// Migrate for Layer 4: related_entities
let has_related_entities: bool = conn
.prepare("SELECT COUNT(*) FROM pragma_table_info('memories') WHERE name='related_entities'")?
.query_row([], |row| row.get(0))
.map(|count: i32| count > 0)?;
if !has_related_entities {
conn.execute("ALTER TABLE memories ADD COLUMN related_entities TEXT", [])?;
}
// Create indexes for better query performance
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_created_at ON memories(created_at)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_updated_at ON memories(updated_at)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_priority_score ON memories(priority_score)",
[],
)?;
// Create user_analyses table (Layer 3)
conn.execute(
"CREATE TABLE IF NOT EXISTS user_analyses (
id TEXT PRIMARY KEY,
openness REAL NOT NULL,
conscientiousness REAL NOT NULL,
extraversion REAL NOT NULL,
agreeableness REAL NOT NULL,
neuroticism REAL NOT NULL,
summary TEXT NOT NULL,
analyzed_at TEXT NOT NULL
)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_analyzed_at ON user_analyses(analyzed_at)",
[],
)?;
// Create user_profiles table (Layer 3.5 - integrated profile cache)
conn.execute(
"CREATE TABLE IF NOT EXISTS user_profiles (
id INTEGER PRIMARY KEY CHECK (id = 1),
data TEXT NOT NULL,
last_updated TEXT NOT NULL
)",
[],
)?;
// Create relationship_cache table (Layer 4 - relationship inference cache)
// entity_id = "" for all_relationships cache
conn.execute(
"CREATE TABLE IF NOT EXISTS relationship_cache (
entity_id TEXT PRIMARY KEY,
data TEXT NOT NULL,
cached_at TEXT NOT NULL
)",
[],
)?;
Ok(Self { conn })
}
/// Create a new MemoryStore using default config directory
pub fn default() -> Result<Self> {
let data_dir = dirs::config_dir()
.ok_or_else(|| MemoryError::Config("Could not find config directory".to_string()))?
.join("syui")
.join("ai")
.join("gpt");
let db_path = data_dir.join("memory.db");
Self::new(db_path)
}
/// Insert a new memory
pub fn create(&self, memory: &Memory) -> Result<()> {
let related_entities_json = memory.related_entities
.as_ref()
.map(|entities| serde_json::to_string(entities).ok())
.flatten();
self.conn.execute(
"INSERT INTO memories (id, content, ai_interpretation, priority_score, related_entities, created_at, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
&memory.id,
&memory.content,
&memory.ai_interpretation,
&memory.priority_score,
related_entities_json,
memory.created_at.to_rfc3339(),
memory.updated_at.to_rfc3339(),
],
)?;
// Clear relationship cache since memory data changed
self.clear_relationship_cache()?;
Ok(())
}
/// Get a memory by ID
pub fn get(&self, id: &str) -> Result<Memory> {
let mut stmt = self
.conn
.prepare("SELECT id, content, ai_interpretation, priority_score, related_entities, created_at, updated_at
FROM memories WHERE id = ?1")?;
let memory = stmt.query_row(params![id], |row| {
let created_at: String = row.get(5)?;
let updated_at: String = row.get(6)?;
let related_entities_json: Option<String> = row.get(4)?;
let related_entities = related_entities_json
.and_then(|json| serde_json::from_str(&json).ok());
Ok(Memory {
id: row.get(0)?,
content: row.get(1)?,
ai_interpretation: row.get(2)?,
priority_score: row.get(3)?,
related_entities,
created_at: DateTime::parse_from_rfc3339(&created_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| rusqlite::Error::FromSqlConversionFailure(
5,
rusqlite::types::Type::Text,
Box::new(e),
))?,
updated_at: DateTime::parse_from_rfc3339(&updated_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| rusqlite::Error::FromSqlConversionFailure(
6,
rusqlite::types::Type::Text,
Box::new(e),
))?,
})
})?;
Ok(memory)
}
/// Update an existing memory
pub fn update(&self, memory: &Memory) -> Result<()> {
let related_entities_json = memory.related_entities
.as_ref()
.map(|entities| serde_json::to_string(entities).ok())
.flatten();
let rows_affected = self.conn.execute(
"UPDATE memories SET content = ?1, ai_interpretation = ?2, priority_score = ?3, related_entities = ?4, updated_at = ?5
WHERE id = ?6",
params![
&memory.content,
&memory.ai_interpretation,
&memory.priority_score,
related_entities_json,
memory.updated_at.to_rfc3339(),
&memory.id,
],
)?;
if rows_affected == 0 {
return Err(MemoryError::NotFound(memory.id.clone()));
}
// Clear relationship cache since memory data changed
self.clear_relationship_cache()?;
Ok(())
}
/// Delete a memory by ID
pub fn delete(&self, id: &str) -> Result<()> {
let rows_affected = self
.conn
.execute("DELETE FROM memories WHERE id = ?1", params![id])?;
if rows_affected == 0 {
return Err(MemoryError::NotFound(id.to_string()));
}
// Clear relationship cache since memory data changed
self.clear_relationship_cache()?;
Ok(())
}
/// List all memories, ordered by creation time (newest first)
pub fn list(&self) -> Result<Vec<Memory>> {
let mut stmt = self.conn.prepare(
"SELECT id, content, ai_interpretation, priority_score, related_entities, created_at, updated_at
FROM memories ORDER BY created_at DESC",
)?;
let memories = stmt
.query_map([], |row| {
let created_at: String = row.get(5)?;
let updated_at: String = row.get(6)?;
let related_entities_json: Option<String> = row.get(4)?;
let related_entities = related_entities_json
.and_then(|json| serde_json::from_str(&json).ok());
Ok(Memory {
id: row.get(0)?,
content: row.get(1)?,
ai_interpretation: row.get(2)?,
priority_score: row.get(3)?,
related_entities,
created_at: DateTime::parse_from_rfc3339(&created_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| rusqlite::Error::FromSqlConversionFailure(
5,
rusqlite::types::Type::Text,
Box::new(e),
))?,
updated_at: DateTime::parse_from_rfc3339(&updated_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| rusqlite::Error::FromSqlConversionFailure(
6,
rusqlite::types::Type::Text,
Box::new(e),
))?,
})
})?
.collect::<std::result::Result<Vec<_>, _>>()?;
Ok(memories)
}
/// Search memories by content or AI interpretation (case-insensitive)
pub fn search(&self, query: &str) -> Result<Vec<Memory>> {
let mut stmt = self.conn.prepare(
"SELECT id, content, ai_interpretation, priority_score, related_entities, created_at, updated_at
FROM memories
WHERE content LIKE ?1 OR ai_interpretation LIKE ?1
ORDER BY created_at DESC",
)?;
let search_pattern = format!("%{}%", query);
let memories = stmt
.query_map(params![search_pattern], |row| {
let created_at: String = row.get(5)?;
let updated_at: String = row.get(6)?;
let related_entities_json: Option<String> = row.get(4)?;
let related_entities = related_entities_json
.and_then(|json| serde_json::from_str(&json).ok());
Ok(Memory {
id: row.get(0)?,
content: row.get(1)?,
ai_interpretation: row.get(2)?,
priority_score: row.get(3)?,
related_entities,
created_at: DateTime::parse_from_rfc3339(&created_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| rusqlite::Error::FromSqlConversionFailure(
5,
rusqlite::types::Type::Text,
Box::new(e),
))?,
updated_at: DateTime::parse_from_rfc3339(&updated_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| rusqlite::Error::FromSqlConversionFailure(
6,
rusqlite::types::Type::Text,
Box::new(e),
))?,
})
})?
.collect::<std::result::Result<Vec<_>, _>>()?;
Ok(memories)
}
/// Count total memories
pub fn count(&self) -> Result<usize> {
let count: usize = self
.conn
.query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))?;
Ok(count)
}
// ========== Layer 3: User Analysis Methods ==========
/// Save a new user personality analysis
pub fn save_analysis(&self, analysis: &UserAnalysis) -> Result<()> {
self.conn.execute(
"INSERT INTO user_analyses (id, openness, conscientiousness, extraversion, agreeableness, neuroticism, summary, analyzed_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
&analysis.id,
&analysis.openness,
&analysis.conscientiousness,
&analysis.extraversion,
&analysis.agreeableness,
&analysis.neuroticism,
&analysis.summary,
analysis.analyzed_at.to_rfc3339(),
],
)?;
Ok(())
}
/// Get the most recent user analysis
pub fn get_latest_analysis(&self) -> Result<Option<UserAnalysis>> {
let mut stmt = self.conn.prepare(
"SELECT id, openness, conscientiousness, extraversion, agreeableness, neuroticism, summary, analyzed_at
FROM user_analyses
ORDER BY analyzed_at DESC
LIMIT 1",
)?;
let result = stmt.query_row([], |row| {
let analyzed_at: String = row.get(7)?;
Ok(UserAnalysis {
id: row.get(0)?,
openness: row.get(1)?,
conscientiousness: row.get(2)?,
extraversion: row.get(3)?,
agreeableness: row.get(4)?,
neuroticism: row.get(5)?,
summary: row.get(6)?,
analyzed_at: DateTime::parse_from_rfc3339(&analyzed_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(
7,
rusqlite::types::Type::Text,
Box::new(e),
)
})?,
})
});
match result {
Ok(analysis) => Ok(Some(analysis)),
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(e.into()),
}
}
/// Get all user analyses, ordered by date (newest first)
pub fn list_analyses(&self) -> Result<Vec<UserAnalysis>> {
let mut stmt = self.conn.prepare(
"SELECT id, openness, conscientiousness, extraversion, agreeableness, neuroticism, summary, analyzed_at
FROM user_analyses
ORDER BY analyzed_at DESC",
)?;
let analyses = stmt
.query_map([], |row| {
let analyzed_at: String = row.get(7)?;
Ok(UserAnalysis {
id: row.get(0)?,
openness: row.get(1)?,
conscientiousness: row.get(2)?,
extraversion: row.get(3)?,
agreeableness: row.get(4)?,
neuroticism: row.get(5)?,
summary: row.get(6)?,
analyzed_at: DateTime::parse_from_rfc3339(&analyzed_at)
.map(|dt| dt.with_timezone(&Utc))
.map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(
7,
rusqlite::types::Type::Text,
Box::new(e),
)
})?,
})
})?
.collect::<std::result::Result<Vec<_>, _>>()?;
Ok(analyses)
}
// === Layer 3.5: Integrated Profile ===
/// Save integrated profile to cache
pub fn save_profile(&self, profile: &super::profile::UserProfile) -> Result<()> {
let profile_json = serde_json::to_string(profile)?;
self.conn.execute(
"INSERT OR REPLACE INTO user_profiles (id, data, last_updated) VALUES (1, ?1, ?2)",
params![profile_json, profile.last_updated.to_rfc3339()],
)?;
Ok(())
}
/// Get cached profile if exists
pub fn get_cached_profile(&self) -> Result<Option<super::profile::UserProfile>> {
let mut stmt = self
.conn
.prepare("SELECT data FROM user_profiles WHERE id = 1")?;
let result = stmt.query_row([], |row| {
let json: String = row.get(0)?;
Ok(json)
});
match result {
Ok(json) => {
let profile: super::profile::UserProfile = serde_json::from_str(&json)?;
Ok(Some(profile))
}
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(e.into()),
}
}
/// Get or generate profile (with automatic caching)
pub fn get_profile(&self) -> Result<super::profile::UserProfile> {
// Check cache first
if let Some(cached) = self.get_cached_profile()? {
// Check if needs update
if !cached.needs_update(self)? {
return Ok(cached);
}
}
// Generate new profile
let profile = super::profile::UserProfile::generate(self)?;
// Cache it
self.save_profile(&profile)?;
Ok(profile)
}
// ========== Layer 4: Relationship Cache Methods ==========
/// Cache duration in minutes
const RELATIONSHIP_CACHE_DURATION_MINUTES: i64 = 5;
/// Save relationship inference to cache
pub fn save_relationship_cache(
&self,
entity_id: &str,
relationship: &super::relationship::RelationshipInference,
) -> Result<()> {
let data = serde_json::to_string(relationship)?;
let cached_at = Utc::now().to_rfc3339();
self.conn.execute(
"INSERT OR REPLACE INTO relationship_cache (entity_id, data, cached_at) VALUES (?1, ?2, ?3)",
params![entity_id, data, cached_at],
)?;
Ok(())
}
/// Get cached relationship inference
pub fn get_cached_relationship(
&self,
entity_id: &str,
) -> Result<Option<super::relationship::RelationshipInference>> {
let mut stmt = self
.conn
.prepare("SELECT data, cached_at FROM relationship_cache WHERE entity_id = ?1")?;
let result = stmt.query_row([entity_id], |row| {
let data: String = row.get(0)?;
let cached_at: String = row.get(1)?;
Ok((data, cached_at))
});
match result {
Ok((data, cached_at_str)) => {
// Check if cache is still valid (within 5 minutes)
let cached_at = DateTime::parse_from_rfc3339(&cached_at_str)
.map_err(|e| MemoryError::Parse(e.to_string()))?
.with_timezone(&Utc);
let age_minutes = (Utc::now() - cached_at).num_seconds() / 60;
if age_minutes < Self::RELATIONSHIP_CACHE_DURATION_MINUTES {
let relationship: super::relationship::RelationshipInference =
serde_json::from_str(&data)?;
Ok(Some(relationship))
} else {
// Cache expired
Ok(None)
}
}
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(e.into()),
}
}
/// Save all relationships list to cache (use empty string as entity_id)
pub fn save_all_relationships_cache(
&self,
relationships: &[super::relationship::RelationshipInference],
) -> Result<()> {
let data = serde_json::to_string(relationships)?;
let cached_at = Utc::now().to_rfc3339();
self.conn.execute(
"INSERT OR REPLACE INTO relationship_cache (entity_id, data, cached_at) VALUES ('', ?1, ?2)",
params![data, cached_at],
)?;
Ok(())
}
/// Get cached all relationships list
pub fn get_cached_all_relationships(
&self,
) -> Result<Option<Vec<super::relationship::RelationshipInference>>> {
let mut stmt = self
.conn
.prepare("SELECT data, cached_at FROM relationship_cache WHERE entity_id = ''")?;
let result = stmt.query_row([], |row| {
let data: String = row.get(0)?;
let cached_at: String = row.get(1)?;
Ok((data, cached_at))
});
match result {
Ok((data, cached_at_str)) => {
let cached_at = DateTime::parse_from_rfc3339(&cached_at_str)
.map_err(|e| MemoryError::Parse(e.to_string()))?
.with_timezone(&Utc);
let age_minutes = (Utc::now() - cached_at).num_seconds() / 60;
if age_minutes < Self::RELATIONSHIP_CACHE_DURATION_MINUTES {
let relationships: Vec<super::relationship::RelationshipInference> =
serde_json::from_str(&data)?;
Ok(Some(relationships))
} else {
Ok(None)
}
}
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(e.into()),
}
}
/// Clear all relationship caches (call when memories are modified)
pub fn clear_relationship_cache(&self) -> Result<()> {
self.conn.execute("DELETE FROM relationship_cache", [])?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_store() -> MemoryStore {
MemoryStore::new(":memory:".into()).unwrap()
}
#[test]
fn test_create_and_get() {
let store = create_test_store();
let memory = Memory::new("Test content".to_string());
store.create(&memory).unwrap();
let retrieved = store.get(&memory.id).unwrap();
assert_eq!(retrieved.id, memory.id);
assert_eq!(retrieved.content, memory.content);
}
#[test]
fn test_update() {
let store = create_test_store();
let mut memory = Memory::new("Original".to_string());
store.create(&memory).unwrap();
memory.update_content("Updated".to_string());
store.update(&memory).unwrap();
let retrieved = store.get(&memory.id).unwrap();
assert_eq!(retrieved.content, "Updated");
}
#[test]
fn test_delete() {
let store = create_test_store();
let memory = Memory::new("To delete".to_string());
store.create(&memory).unwrap();
store.delete(&memory.id).unwrap();
assert!(store.get(&memory.id).is_err());
}
#[test]
fn test_list() {
let store = create_test_store();
let mem1 = Memory::new("First".to_string());
let mem2 = Memory::new("Second".to_string());
store.create(&mem1).unwrap();
store.create(&mem2).unwrap();
let memories = store.list().unwrap();
assert_eq!(memories.len(), 2);
}
#[test]
fn test_search() {
let store = create_test_store();
store
.create(&Memory::new("Hello world".to_string()))
.unwrap();
store
.create(&Memory::new("Goodbye world".to_string()))
.unwrap();
store.create(&Memory::new("Testing".to_string())).unwrap();
let results = store.search("world").unwrap();
assert_eq!(results.len(), 2);
let results = store.search("Hello").unwrap();
assert_eq!(results.len(), 1);
}
#[test]
fn test_count() {
let store = create_test_store();
assert_eq!(store.count().unwrap(), 0);
store.create(&Memory::new("Test".to_string())).unwrap();
assert_eq!(store.count().unwrap(), 1);
}
}

789
src/docs.rs Normal file
View File

@@ -0,0 +1,789 @@
use std::collections::HashMap;
use std::path::PathBuf;
use anyhow::{Result, Context};
use colored::*;
use serde::{Deserialize, Serialize};
use chrono::Utc;
use crate::config::Config;
use crate::persona::Persona;
use crate::ai_provider::{AIProviderClient, AIConfig, AIProvider};
pub async fn handle_docs(
action: String,
project: Option<String>,
output: Option<PathBuf>,
ai_integration: bool,
data_dir: Option<PathBuf>,
) -> Result<()> {
let config = Config::new(data_dir)?;
let mut docs_manager = DocsManager::new(config);
match action.as_str() {
"generate" => {
if let Some(project_name) = project {
docs_manager.generate_project_docs(&project_name, output, ai_integration).await?;
} else {
return Err(anyhow::anyhow!("Project name is required for generate action"));
}
}
"sync" => {
if let Some(project_name) = project {
docs_manager.sync_project_docs(&project_name).await?;
} else {
docs_manager.sync_all_docs().await?;
}
}
"list" => {
docs_manager.list_projects().await?;
}
"status" => {
docs_manager.show_docs_status().await?;
}
"session-end" => {
docs_manager.session_end_processing().await?;
}
_ => {
return Err(anyhow::anyhow!("Unknown docs action: {}", action));
}
}
Ok(())
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectInfo {
pub name: String,
pub project_type: String,
pub description: String,
pub status: String,
pub features: Vec<String>,
pub dependencies: Vec<String>,
}
impl Default for ProjectInfo {
fn default() -> Self {
ProjectInfo {
name: String::new(),
project_type: String::new(),
description: String::new(),
status: "active".to_string(),
features: Vec::new(),
dependencies: Vec::new(),
}
}
}
pub struct DocsManager {
config: Config,
ai_root: PathBuf,
projects: HashMap<String, ProjectInfo>,
}
impl DocsManager {
pub fn new(config: Config) -> Self {
let ai_root = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("ai")
.join("ai");
DocsManager {
config,
ai_root,
projects: HashMap::new(),
}
}
pub async fn generate_project_docs(&mut self, project: &str, output: Option<PathBuf>, ai_integration: bool) -> Result<()> {
println!("{}", format!("📝 Generating documentation for project '{}'", project).cyan().bold());
// Load project information
let project_info = self.load_project_info(project)?;
// Generate documentation content
let mut content = self.generate_base_documentation(&project_info)?;
// AI enhancement if requested
if ai_integration {
println!("{}", "🤖 Enhancing documentation with AI...".blue());
if let Ok(enhanced_content) = self.enhance_with_ai(project, &content).await {
content = enhanced_content;
} else {
println!("{}", "Warning: AI enhancement failed, using base documentation".yellow());
}
}
// Determine output path
let output_path = if let Some(path) = output {
path
} else {
self.ai_root.join(project).join("claude.md")
};
// Ensure directory exists
if let Some(parent) = output_path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("Failed to create directory: {}", parent.display()))?;
}
// Write documentation
std::fs::write(&output_path, content)
.with_context(|| format!("Failed to write documentation to: {}", output_path.display()))?;
println!("{}", format!("✅ Documentation generated: {}", output_path.display()).green().bold());
Ok(())
}
pub async fn sync_project_docs(&self, project: &str) -> Result<()> {
println!("{}", format!("🔄 Syncing documentation for project '{}'", project).cyan().bold());
let claude_dir = self.ai_root.join("claude");
let project_dir = self.ai_root.join(project);
// Check if claude directory exists
if !claude_dir.exists() {
return Err(anyhow::anyhow!("Claude directory not found: {}", claude_dir.display()));
}
// Copy relevant files
let files_to_sync = vec!["README.md", "claude.md", "DEVELOPMENT.md"];
for file in files_to_sync {
let src = claude_dir.join("projects").join(format!("{}.md", project));
let dst = project_dir.join(file);
if src.exists() {
if let Some(parent) = dst.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::copy(&src, &dst)?;
println!(" ✓ Synced: {}", file.green());
}
}
println!("{}", "✅ Documentation sync completed".green().bold());
Ok(())
}
pub async fn sync_all_docs(&self) -> Result<()> {
println!("{}", "🔄 Syncing documentation for all projects...".cyan().bold());
// Find all project directories
let projects = self.discover_projects()?;
for project in projects {
println!("\n{}", format!("Syncing: {}", project).blue());
if let Err(e) = self.sync_project_docs(&project).await {
println!("{}: {}", "Warning".yellow(), e);
}
}
// Generate ai.wiki content after all project syncs
println!("\n{}", "📝 Updating ai.wiki...".blue());
if let Err(e) = self.update_ai_wiki().await {
println!("{}: Failed to update ai.wiki: {}", "Warning".yellow(), e);
}
// Update repository wiki (Gitea wiki) as well
println!("\n{}", "📝 Updating repository wiki...".blue());
if let Err(e) = self.update_repository_wiki().await {
println!("{}: Failed to update repository wiki: {}", "Warning".yellow(), e);
}
println!("\n{}", "✅ All projects synced".green().bold());
Ok(())
}
pub async fn list_projects(&mut self) -> Result<()> {
println!("{}", "📋 Available Projects".cyan().bold());
println!();
let projects = self.discover_projects()?;
if projects.is_empty() {
println!("{}", "No projects found".yellow());
return Ok(());
}
// Load project information
for project in &projects {
if let Ok(info) = self.load_project_info(project) {
self.projects.insert(project.clone(), info);
}
}
// Display projects in a table format
println!("{:<20} {:<15} {:<15} {}",
"Project".cyan().bold(),
"Type".cyan().bold(),
"Status".cyan().bold(),
"Description".cyan().bold());
println!("{}", "-".repeat(80));
let project_count = projects.len();
for project in &projects {
let info = self.projects.get(project).cloned().unwrap_or_default();
let status_color = match info.status.as_str() {
"active" => info.status.green(),
"development" => info.status.yellow(),
"deprecated" => info.status.red(),
_ => info.status.normal(),
};
println!("{:<20} {:<15} {:<15} {}",
project.blue(),
info.project_type,
status_color,
info.description);
}
println!();
println!("Total projects: {}", project_count.to_string().cyan());
Ok(())
}
pub async fn show_docs_status(&self) -> Result<()> {
println!("{}", "📊 Documentation Status".cyan().bold());
println!();
let projects = self.discover_projects()?;
let mut total_files = 0;
let mut total_lines = 0;
for project in projects {
let project_dir = self.ai_root.join(&project);
let claude_md = project_dir.join("claude.md");
if claude_md.exists() {
let content = std::fs::read_to_string(&claude_md)?;
let lines = content.lines().count();
let size = content.len();
println!("{}: {} lines, {} bytes",
project.blue(),
lines.to_string().yellow(),
size.to_string().yellow());
total_files += 1;
total_lines += lines;
} else {
println!("{}: {}", project.blue(), "No documentation".red());
}
}
println!();
println!("Summary: {} files, {} total lines",
total_files.to_string().cyan(),
total_lines.to_string().cyan());
Ok(())
}
fn discover_projects(&self) -> Result<Vec<String>> {
let mut projects = Vec::new();
// Known project directories
let known_projects = vec![
"gpt", "card", "bot", "shell", "os", "game", "moji", "verse"
];
for project in known_projects {
let project_dir = self.ai_root.join(project);
if project_dir.exists() && project_dir.is_dir() {
projects.push(project.to_string());
}
}
// Also scan for additional directories with ai.json
if self.ai_root.exists() {
for entry in std::fs::read_dir(&self.ai_root)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
let ai_json = path.join("ai.json");
if ai_json.exists() {
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if !projects.contains(&name.to_string()) {
projects.push(name.to_string());
}
}
}
}
}
}
projects.sort();
Ok(projects)
}
fn load_project_info(&self, project: &str) -> Result<ProjectInfo> {
let ai_json_path = self.ai_root.join(project).join("ai.json");
if ai_json_path.exists() {
let content = std::fs::read_to_string(&ai_json_path)?;
if let Ok(json_data) = serde_json::from_str::<serde_json::Value>(&content) {
let mut info = ProjectInfo::default();
info.name = project.to_string();
if let Some(project_data) = json_data.get(project) {
if let Some(type_str) = project_data.get("type").and_then(|v| v.as_str()) {
info.project_type = type_str.to_string();
}
if let Some(desc) = project_data.get("description").and_then(|v| v.as_str()) {
info.description = desc.to_string();
}
}
return Ok(info);
}
}
// Default project info based on known projects
let mut info = ProjectInfo::default();
info.name = project.to_string();
match project {
"gpt" => {
info.project_type = "AI".to_string();
info.description = "Autonomous transmission AI with unique personality".to_string();
}
"card" => {
info.project_type = "Game".to_string();
info.description = "Card game system with atproto integration".to_string();
}
"bot" => {
info.project_type = "Bot".to_string();
info.description = "Distributed SNS bot for AI ecosystem".to_string();
}
"shell" => {
info.project_type = "Tool".to_string();
info.description = "AI-powered shell interface".to_string();
}
"os" => {
info.project_type = "OS".to_string();
info.description = "Game-oriented operating system".to_string();
}
"verse" => {
info.project_type = "Metaverse".to_string();
info.description = "Reality-reflecting 3D world system".to_string();
}
_ => {
info.project_type = "Unknown".to_string();
info.description = format!("AI ecosystem project: {}", project);
}
}
Ok(info)
}
fn generate_base_documentation(&self, project_info: &ProjectInfo) -> Result<String> {
let timestamp = Utc::now().format("%Y-%m-%d %H:%M:%S UTC");
let mut content = String::new();
content.push_str(&format!("# {}\n\n", project_info.name));
content.push_str(&format!("## Overview\n\n"));
content.push_str(&format!("**Type**: {}\n\n", project_info.project_type));
content.push_str(&format!("**Description**: {}\n\n", project_info.description));
content.push_str(&format!("**Status**: {}\n\n", project_info.status));
if !project_info.features.is_empty() {
content.push_str("## Features\n\n");
for feature in &project_info.features {
content.push_str(&format!("- {}\n", feature));
}
content.push_str("\n");
}
content.push_str("## Architecture\n\n");
content.push_str("This project is part of the ai ecosystem, following the core principles:\n\n");
content.push_str("- **Existence Theory**: Based on the exploration of the smallest units (ai/existon)\n");
content.push_str("- **Uniqueness Principle**: Ensuring 1:1 mapping between reality and digital existence\n");
content.push_str("- **Reality Reflection**: Creating circular influence between reality and game\n\n");
content.push_str("## Development\n\n");
content.push_str("### Getting Started\n\n");
content.push_str("```bash\n");
content.push_str(&format!("# Clone the repository\n"));
content.push_str(&format!("git clone https://git.syui.ai/ai/{}\n", project_info.name));
content.push_str(&format!("cd {}\n", project_info.name));
content.push_str("```\n\n");
content.push_str("### Configuration\n\n");
content.push_str(&format!("Configuration files are stored in `~/.config/syui/ai/{}/`\n\n", project_info.name));
content.push_str("## Integration\n\n");
content.push_str("This project integrates with other ai ecosystem components:\n\n");
if !project_info.dependencies.is_empty() {
for dep in &project_info.dependencies {
content.push_str(&format!("- **{}**: Core dependency\n", dep));
}
} else {
content.push_str("- **ai.gpt**: Core AI personality system\n");
content.push_str("- **atproto**: Distributed identity and data\n");
}
content.push_str("\n");
content.push_str("---\n\n");
content.push_str(&format!("*Generated: {}*\n", timestamp));
content.push_str("*🤖 Generated with [Claude Code](https://claude.ai/code)*\n");
Ok(content)
}
async fn enhance_with_ai(&self, project: &str, base_content: &str) -> Result<String> {
// Create AI provider
let ai_config = AIConfig {
provider: AIProvider::Ollama,
model: "llama2".to_string(),
api_key: None,
base_url: None,
max_tokens: Some(2000),
temperature: Some(0.7),
};
let _ai_provider = AIProviderClient::new(ai_config);
let mut persona = Persona::new(&self.config)?;
let enhancement_prompt = format!(
"As an AI documentation expert, enhance the following documentation for project '{}'.
Current documentation:
{}
Please provide enhanced content that includes:
1. More detailed project description
2. Key features and capabilities
3. Usage examples
4. Integration points with other AI ecosystem projects
5. Development workflow recommendations
Keep the same structure but expand and improve the content.",
project, base_content
);
// Try to get AI response
let (response, _) = persona.process_ai_interaction(
"docs_system",
&enhancement_prompt,
Some("ollama".to_string()),
Some("llama2".to_string())
).await?;
// If AI response is substantial, use it; otherwise fall back to base content
if response.len() > base_content.len() / 2 {
Ok(response)
} else {
Ok(base_content.to_string())
}
}
/// セッション終了時の処理(ドキュメント記録・同期)
pub async fn session_end_processing(&mut self) -> Result<()> {
println!("{}", "🔄 Session end processing started...".cyan());
// 1. 現在のプロジェクト状況を記録
println!("📊 Recording current project status...");
self.record_session_summary().await?;
// 2. 全プロジェクトのドキュメント同期
println!("🔄 Syncing all project documentation...");
self.sync_all_docs().await?;
// 3. READMEの自動更新
println!("📝 Updating project README files...");
self.update_project_readmes().await?;
// 4. メタデータの更新
println!("🏷️ Updating project metadata...");
self.update_project_metadata().await?;
println!("{}", "✅ Session end processing completed!".green());
Ok(())
}
/// セッション概要を記録
async fn record_session_summary(&self) -> Result<()> {
let session_log_path = self.ai_root.join("session_logs");
std::fs::create_dir_all(&session_log_path)?;
let timestamp = Utc::now().format("%Y-%m-%d_%H-%M-%S");
let log_file = session_log_path.join(format!("session_{}.md", timestamp));
let summary = format!(
"# Session Summary - {}\n\n\
## Timestamp\n{}\n\n\
## Projects Status\n{}\n\n\
## Next Actions\n- Documentation sync completed\n- README files updated\n- Metadata refreshed\n\n\
---\n*Generated by aigpt session-end processing*\n",
timestamp,
Utc::now().format("%Y-%m-%d %H:%M:%S UTC"),
self.generate_projects_status().await.unwrap_or_else(|_| "Status unavailable".to_string())
);
std::fs::write(log_file, summary)?;
Ok(())
}
/// プロジェクト状況を生成
async fn generate_projects_status(&self) -> Result<String> {
let projects = self.discover_projects()?;
let mut status = String::new();
for project in projects {
let claude_md = self.ai_root.join(&project).join("claude.md");
let readme_md = self.ai_root.join(&project).join("README.md");
status.push_str(&format!("- **{}**: ", project));
if claude_md.exists() {
status.push_str("claude.md ✅ ");
} else {
status.push_str("claude.md ❌ ");
}
if readme_md.exists() {
status.push_str("README.md ✅");
} else {
status.push_str("README.md ❌");
}
status.push('\n');
}
Ok(status)
}
/// ai.wikiの更新処理
async fn update_ai_wiki(&self) -> Result<()> {
let ai_wiki_path = self.ai_root.join("ai.wiki");
// ai.wikiディレクトリが存在することを確認
if !ai_wiki_path.exists() {
return Err(anyhow::anyhow!("ai.wiki directory not found at {:?}", ai_wiki_path));
}
// Home.mdの生成
let home_content = self.generate_wiki_home_content().await?;
let home_path = ai_wiki_path.join("Home.md");
std::fs::write(&home_path, &home_content)?;
println!(" ✓ Updated: {}", "Home.md".green());
// title.mdの生成 (Gitea wiki特別ページ用)
let title_path = ai_wiki_path.join("title.md");
std::fs::write(&title_path, &home_content)?;
println!(" ✓ Updated: {}", "title.md".green());
// プロジェクト個別ディレクトリの更新
let projects = self.discover_projects()?;
for project in projects {
let project_dir = ai_wiki_path.join(&project);
std::fs::create_dir_all(&project_dir)?;
let project_content = self.generate_auto_project_content(&project).await?;
let project_file = project_dir.join(format!("{}.md", project));
std::fs::write(&project_file, project_content)?;
println!(" ✓ Updated: {}", format!("{}/{}.md", project, project).green());
}
println!("{}", "✅ ai.wiki updated successfully".green().bold());
Ok(())
}
/// ai.wiki/Home.mdのコンテンツ生成
async fn generate_wiki_home_content(&self) -> Result<String> {
let timestamp = Utc::now().format("%Y-%m-%d %H:%M:%S");
let mut content = String::new();
content.push_str("# AI Ecosystem Wiki\n\n");
content.push_str("AI生態系プロジェクトの概要とドキュメント集約ページです。\n\n");
content.push_str("## プロジェクト一覧\n\n");
let projects = self.discover_projects()?;
let mut project_sections = std::collections::HashMap::new();
// プロジェクトをカテゴリ別に分類
for project in &projects {
let info = self.load_project_info(project).unwrap_or_default();
let category = match project.as_str() {
"ai" => "🧠 AI・知能システム",
"gpt" => "🤖 自律・対話システム",
"os" => "💻 システム・基盤",
"game" => "📁 device",
"card" => "🎮 ゲーム・エンターテイメント",
"bot" | "moji" | "api" | "log" => "📁 その他",
"verse" => "📁 metaverse",
"shell" => "⚡ ツール・ユーティリティ",
_ => "📁 その他",
};
project_sections.entry(category).or_insert_with(Vec::new).push((project.clone(), info));
}
// カテゴリ別にプロジェクトを出力
let mut categories: Vec<_> = project_sections.keys().collect();
categories.sort();
for category in categories {
content.push_str(&format!("### {}\n\n", category));
if let Some(projects_in_category) = project_sections.get(category) {
for (project, info) in projects_in_category {
content.push_str(&format!("#### [{}]({}.md)\n", project, project));
if !info.description.is_empty() {
content.push_str(&format!("- **名前**: ai.{} - **パッケージ**: ai{} - **タイプ**: {} - **役割**: {}\n\n",
project, project, info.project_type, info.description));
}
content.push_str(&format!("**Status**: {} \n", info.status));
let branch = self.get_project_branch(project);
content.push_str(&format!("**Links**: [Repo](https://git.syui.ai/ai/{}) | [Docs](https://git.syui.ai/ai/{}/src/branch/{}/claude.md)\n\n", project, project, branch));
}
}
}
content.push_str("---\n\n");
content.push_str("## ディレクトリ構成\n\n");
content.push_str("- `{project}/` - プロジェクト個別ドキュメント\n");
content.push_str("- `claude/` - Claude Code作業記録\n");
content.push_str("- `manual/` - 手動作成ドキュメント\n\n");
content.push_str("---\n\n");
content.push_str("*このページは ai.json と claude/projects/ から自動生成されました* \n");
content.push_str(&format!("*最終更新: {}*\n", timestamp));
Ok(content)
}
/// プロジェクト個別ファイルのコンテンツ生成
async fn generate_auto_project_content(&self, project: &str) -> Result<String> {
let info = self.load_project_info(project).unwrap_or_default();
let mut content = String::new();
content.push_str(&format!("# {}\n\n", project));
content.push_str("## 概要\n");
content.push_str(&format!("- **名前**: ai.{} - **パッケージ**: ai{} - **タイプ**: {} - **役割**: {}\n\n",
project, project, info.project_type, info.description));
content.push_str("## プロジェクト情報\n");
content.push_str(&format!("- **タイプ**: {}\n", info.project_type));
content.push_str(&format!("- **説明**: {}\n", info.description));
content.push_str(&format!("- **ステータス**: {}\n", info.status));
let branch = self.get_project_branch(project);
content.push_str(&format!("- **ブランチ**: {}\n", branch));
content.push_str("- **最終更新**: Unknown\n\n");
// プロジェクト固有の機能情報を追加
if !info.features.is_empty() {
content.push_str("## 主な機能・特徴\n");
for feature in &info.features {
content.push_str(&format!("- {}\n", feature));
}
content.push_str("\n");
}
content.push_str("## リンク\n");
content.push_str(&format!("- **Repository**: https://git.syui.ai/ai/{}\n", project));
content.push_str(&format!("- **Project Documentation**: [claude/projects/{}.md](https://git.syui.ai/ai/ai/src/branch/main/claude/projects/{}.md)\n", project, project));
let branch = self.get_project_branch(project);
content.push_str(&format!("- **Generated Documentation**: [{}/claude.md](https://git.syui.ai/ai/{}/src/branch/{}/claude.md)\n\n", project, project, branch));
content.push_str("---\n");
content.push_str(&format!("*このページは claude/projects/{}.md から自動生成されました*\n", project));
Ok(content)
}
/// リポジトリwiki (Gitea wiki) の更新処理
async fn update_repository_wiki(&self) -> Result<()> {
println!(" Repository wiki is now unified with ai.wiki");
println!(" ai.wiki serves as the source of truth (git@git.syui.ai:ai/ai.wiki.git)");
println!(" Special pages generated: Home.md, title.md for Gitea wiki compatibility");
Ok(())
}
/// プロジェクトREADMEファイルの更新
async fn update_project_readmes(&self) -> Result<()> {
let projects = self.discover_projects()?;
for project in projects {
let readme_path = self.ai_root.join(&project).join("README.md");
let claude_md_path = self.ai_root.join(&project).join("claude.md");
// claude.mdが存在する場合、READMEに同期
if claude_md_path.exists() {
let claude_content = std::fs::read_to_string(&claude_md_path)?;
// READMEが存在しない場合は新規作成
if !readme_path.exists() {
println!("📝 Creating README.md for {}", project);
std::fs::write(&readme_path, &claude_content)?;
} else {
// 既存READMEがclaude.mdより古い場合は更新
let readme_metadata = std::fs::metadata(&readme_path)?;
let claude_metadata = std::fs::metadata(&claude_md_path)?;
if claude_metadata.modified()? > readme_metadata.modified()? {
println!("🔄 Updating README.md for {}", project);
std::fs::write(&readme_path, &claude_content)?;
}
}
}
}
Ok(())
}
/// プロジェクトメタデータの更新
async fn update_project_metadata(&self) -> Result<()> {
let projects = self.discover_projects()?;
for project in projects {
let ai_json_path = self.ai_root.join(&project).join("ai.json");
if ai_json_path.exists() {
let mut content = std::fs::read_to_string(&ai_json_path)?;
let mut json_data: serde_json::Value = serde_json::from_str(&content)?;
// last_updated フィールドを更新
if let Some(project_data) = json_data.get_mut(&project) {
if let Some(obj) = project_data.as_object_mut() {
obj.insert("last_updated".to_string(),
serde_json::Value::String(Utc::now().to_rfc3339()));
obj.insert("status".to_string(),
serde_json::Value::String("active".to_string()));
content = serde_json::to_string_pretty(&json_data)?;
std::fs::write(&ai_json_path, content)?;
}
}
}
}
Ok(())
}
/// メインai.jsonからプロジェクトのブランチ情報を取得
fn get_project_branch(&self, project: &str) -> String {
let main_ai_json_path = self.ai_root.join("ai.json");
if main_ai_json_path.exists() {
if let Ok(content) = std::fs::read_to_string(&main_ai_json_path) {
if let Ok(json_data) = serde_json::from_str::<serde_json::Value>(&content) {
if let Some(ai_section) = json_data.get("ai") {
if let Some(project_data) = ai_section.get(project) {
if let Some(branch) = project_data.get("branch").and_then(|v| v.as_str()) {
return branch.to_string();
}
}
}
}
}
}
// デフォルトはmain
"main".to_string()
}
}

409
src/http_client.rs Normal file
View File

@@ -0,0 +1,409 @@
use anyhow::{anyhow, Result};
use reqwest::Client;
use serde_json::Value;
use serde::{Serialize, Deserialize};
use std::time::Duration;
use std::collections::HashMap;
/// Service configuration for unified service management
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceConfig {
pub base_url: String,
pub timeout: Duration,
pub health_endpoint: String,
}
impl Default for ServiceConfig {
fn default() -> Self {
Self {
base_url: "http://localhost:8000".to_string(),
timeout: Duration::from_secs(30),
health_endpoint: "/health".to_string(),
}
}
}
/// HTTP client for inter-service communication
pub struct ServiceClient {
client: Client,
service_registry: HashMap<String, ServiceConfig>,
}
impl ServiceClient {
pub fn new() -> Self {
Self::with_default_services()
}
/// Create ServiceClient with default ai ecosystem services
pub fn with_default_services() -> Self {
let client = Client::builder()
.timeout(Duration::from_secs(30))
.build()
.expect("Failed to create HTTP client");
let mut service_registry = HashMap::new();
// Register default ai ecosystem services
service_registry.insert("ai.card".to_string(), ServiceConfig {
base_url: "http://localhost:8000".to_string(),
timeout: Duration::from_secs(30),
health_endpoint: "/health".to_string(),
});
service_registry.insert("ai.log".to_string(), ServiceConfig {
base_url: "http://localhost:8002".to_string(),
timeout: Duration::from_secs(30),
health_endpoint: "/health".to_string(),
});
service_registry.insert("ai.bot".to_string(), ServiceConfig {
base_url: "http://localhost:8003".to_string(),
timeout: Duration::from_secs(30),
health_endpoint: "/health".to_string(),
});
Self { client, service_registry }
}
/// Create ServiceClient with custom service registry
pub fn with_services(service_registry: HashMap<String, ServiceConfig>) -> Self {
let client = Client::builder()
.timeout(Duration::from_secs(30))
.build()
.expect("Failed to create HTTP client");
Self { client, service_registry }
}
/// Register a new service configuration
pub fn register_service(&mut self, name: String, config: ServiceConfig) {
self.service_registry.insert(name, config);
}
/// Get service configuration by name
pub fn get_service_config(&self, service: &str) -> Result<&ServiceConfig> {
self.service_registry.get(service)
.ok_or_else(|| anyhow!("Unknown service: {}", service))
}
/// Universal service method call
pub async fn call_service_method<T: Serialize>(
&self,
service: &str,
method: &str,
params: &T
) -> Result<Value> {
let config = self.get_service_config(service)?;
let url = format!("{}/{}", config.base_url.trim_end_matches('/'), method.trim_start_matches('/'));
self.post_request(&url, &serde_json::to_value(params)?).await
}
/// Universal service GET call
pub async fn call_service_get(&self, service: &str, endpoint: &str) -> Result<Value> {
let config = self.get_service_config(service)?;
let url = format!("{}/{}", config.base_url.trim_end_matches('/'), endpoint.trim_start_matches('/'));
self.get_request(&url).await
}
/// Check if a service is available
pub async fn check_service_status(&self, base_url: &str) -> Result<ServiceStatus> {
let url = format!("{}/health", base_url.trim_end_matches('/'));
match self.client.get(&url).send().await {
Ok(response) => {
if response.status().is_success() {
Ok(ServiceStatus::Available)
} else {
Ok(ServiceStatus::Error(format!("HTTP {}", response.status())))
}
}
Err(e) => Ok(ServiceStatus::Unavailable(e.to_string())),
}
}
/// Make a GET request to a service
pub async fn get_request(&self, url: &str) -> Result<Value> {
let response = self.client
.get(url)
.send()
.await?;
if !response.status().is_success() {
return Err(anyhow!("Request failed with status: {}", response.status()));
}
let json: Value = response.json().await?;
Ok(json)
}
/// Make a POST request to a service
pub async fn post_request(&self, url: &str, body: &Value) -> Result<Value> {
let response = self.client
.post(url)
.header("Content-Type", "application/json")
.json(body)
.send()
.await?;
if !response.status().is_success() {
return Err(anyhow!("Request failed with status: {}", response.status()));
}
let json: Value = response.json().await?;
Ok(json)
}
/// Get user's card collection from ai.card service
pub async fn get_user_cards(&self, user_did: &str) -> Result<Value> {
let endpoint = format!("api/v1/cards/user/{}", user_did);
self.call_service_get("ai.card", &endpoint).await
}
/// Draw a card for user from ai.card service
pub async fn draw_card(&self, user_did: &str, is_paid: bool) -> Result<Value> {
let params = serde_json::json!({
"user_did": user_did,
"is_paid": is_paid
});
self.call_service_method("ai.card", "api/v1/cards/draw", &params).await
}
/// Get card statistics from ai.card service
pub async fn get_card_stats(&self) -> Result<Value> {
self.call_service_get("ai.card", "api/v1/cards/gacha-stats").await
}
// MARK: - ai.log service methods
/// Create a new blog post
pub async fn create_blog_post<T: Serialize>(&self, params: &T) -> Result<Value> {
self.call_service_method("ai.log", "api/v1/posts", params).await
}
/// Get list of blog posts
pub async fn get_blog_posts(&self) -> Result<Value> {
self.call_service_get("ai.log", "api/v1/posts").await
}
/// Build the blog
pub async fn build_blog(&self) -> Result<Value> {
self.call_service_method("ai.log", "api/v1/build", &serde_json::json!({})).await
}
/// Translate document using ai.log service
pub async fn translate_document<T: Serialize>(&self, params: &T) -> Result<Value> {
self.call_service_method("ai.log", "api/v1/translate", params).await
}
/// Generate documentation using ai.log service
pub async fn generate_docs<T: Serialize>(&self, params: &T) -> Result<Value> {
self.call_service_method("ai.log", "api/v1/docs", params).await
}
}
/// Service status enum
#[derive(Debug, Clone)]
pub enum ServiceStatus {
Available,
Unavailable(String),
Error(String),
}
impl ServiceStatus {
pub fn is_available(&self) -> bool {
matches!(self, ServiceStatus::Available)
}
}
/// Service detector for ai ecosystem services
pub struct ServiceDetector {
client: ServiceClient,
}
impl ServiceDetector {
pub fn new() -> Self {
Self {
client: ServiceClient::new(),
}
}
/// Check all ai ecosystem services
pub async fn detect_services(&self) -> ServiceMap {
let mut services = ServiceMap::default();
// Check ai.card service
if let Ok(status) = self.client.check_service_status("http://localhost:8000").await {
services.ai_card = Some(ServiceInfo {
base_url: "http://localhost:8000".to_string(),
status,
});
}
// Check ai.log service
if let Ok(status) = self.client.check_service_status("http://localhost:8001").await {
services.ai_log = Some(ServiceInfo {
base_url: "http://localhost:8001".to_string(),
status,
});
}
// Check ai.bot service
if let Ok(status) = self.client.check_service_status("http://localhost:8002").await {
services.ai_bot = Some(ServiceInfo {
base_url: "http://localhost:8002".to_string(),
status,
});
}
services
}
/// Get available services only
pub async fn get_available_services(&self) -> Vec<String> {
let services = self.detect_services().await;
let mut available = Vec::new();
if let Some(card) = &services.ai_card {
if card.status.is_available() {
available.push("ai.card".to_string());
}
}
if let Some(log) = &services.ai_log {
if log.status.is_available() {
available.push("ai.log".to_string());
}
}
if let Some(bot) = &services.ai_bot {
if bot.status.is_available() {
available.push("ai.bot".to_string());
}
}
available
}
/// Get card collection statistics
pub async fn get_card_stats(&self) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
match self.client.get_request("http://localhost:8000/api/v1/cards/gacha-stats").await {
Ok(stats) => Ok(stats),
Err(e) => Err(e.into()),
}
}
/// Draw a card for user
pub async fn draw_card(&self, user_did: &str, is_paid: bool) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
let payload = serde_json::json!({
"user_did": user_did,
"is_paid": is_paid
});
match self.client.post_request("http://localhost:8000/api/v1/cards/draw", &payload).await {
Ok(card) => Ok(card),
Err(e) => Err(e.into()),
}
}
/// Get user's card collection
pub async fn get_user_cards(&self, user_did: &str) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
let url = format!("http://localhost:8000/api/v1/cards/collection?did={}", user_did);
match self.client.get_request(&url).await {
Ok(collection) => Ok(collection),
Err(e) => Err(e.into()),
}
}
/// Get contextual memories for conversation mode
pub async fn get_contextual_memories(&self, _user_id: &str, _limit: usize) -> Result<Vec<crate::memory::Memory>, Box<dyn std::error::Error>> {
// This is a simplified version - in a real implementation this would call the MCP server
// For now, we'll return an empty vec to make compilation work
Ok(Vec::new())
}
/// Search memories by query
pub async fn search_memories(&self, _query: &str, _limit: usize) -> Result<Vec<crate::memory::Memory>, Box<dyn std::error::Error>> {
// This is a simplified version - in a real implementation this would call the MCP server
// For now, we'll return an empty vec to make compilation work
Ok(Vec::new())
}
/// Create context summary
pub async fn create_summary(&self, user_id: &str) -> Result<String, Box<dyn std::error::Error>> {
// This is a simplified version - in a real implementation this would call the MCP server
// For now, we'll return a placeholder summary
Ok(format!("Context summary for user: {}", user_id))
}
}
/// Service information
#[derive(Debug, Clone)]
pub struct ServiceInfo {
pub base_url: String,
pub status: ServiceStatus,
}
/// Map of all ai ecosystem services
#[derive(Debug, Clone, Default)]
pub struct ServiceMap {
pub ai_card: Option<ServiceInfo>,
pub ai_log: Option<ServiceInfo>,
pub ai_bot: Option<ServiceInfo>,
}
impl ServiceMap {
/// Get service info by name
pub fn get_service(&self, name: &str) -> Option<&ServiceInfo> {
match name {
"ai.card" => self.ai_card.as_ref(),
"ai.log" => self.ai_log.as_ref(),
"ai.bot" => self.ai_bot.as_ref(),
_ => None,
}
}
/// Check if a service is available
pub fn is_service_available(&self, name: &str) -> bool {
self.get_service(name)
.map(|info| info.status.is_available())
.unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_service_client_creation() {
let _client = ServiceClient::new();
// Basic test to ensure client can be created
assert!(true);
}
#[test]
fn test_service_status() {
let status = ServiceStatus::Available;
assert!(status.is_available());
let status = ServiceStatus::Unavailable("Connection refused".to_string());
assert!(!status.is_available());
}
#[test]
fn test_service_map() {
let mut map = ServiceMap::default();
assert!(!map.is_service_available("ai.card"));
map.ai_card = Some(ServiceInfo {
base_url: "http://localhost:8000".to_string(),
status: ServiceStatus::Available,
});
assert!(map.is_service_available("ai.card"));
assert!(!map.is_service_available("ai.log"));
}
}

331
src/import.rs Normal file
View File

@@ -0,0 +1,331 @@
use std::collections::HashMap;
use std::path::PathBuf;
use serde::Deserialize;
use anyhow::{Result, Context};
use colored::*;
use chrono::{DateTime, Utc};
use crate::config::Config;
use crate::persona::Persona;
use crate::memory::{Memory, MemoryType};
pub async fn handle_import_chatgpt(
file_path: PathBuf,
user_id: Option<String>,
data_dir: Option<PathBuf>,
) -> Result<()> {
let config = Config::new(data_dir)?;
let mut persona = Persona::new(&config)?;
let user_id = user_id.unwrap_or_else(|| "imported_user".to_string());
println!("{}", "🚀 Starting ChatGPT Import...".cyan().bold());
println!("File: {}", file_path.display().to_string().yellow());
println!("User ID: {}", user_id.yellow());
println!();
let mut importer = ChatGPTImporter::new(user_id);
let stats = importer.import_from_file(&file_path, &mut persona).await?;
// Display import statistics
println!("\n{}", "📊 Import Statistics".green().bold());
println!("Conversations imported: {}", stats.conversations_imported.to_string().cyan());
println!("Messages imported: {}", stats.messages_imported.to_string().cyan());
println!(" - User messages: {}", stats.user_messages.to_string().yellow());
println!(" - Assistant messages: {}", stats.assistant_messages.to_string().yellow());
if stats.skipped_messages > 0 {
println!(" - Skipped messages: {}", stats.skipped_messages.to_string().red());
}
// Show updated relationship
if let Some(relationship) = persona.get_relationship(&importer.user_id) {
println!("\n{}", "👥 Updated Relationship".blue().bold());
println!("Status: {}", relationship.status.to_string().yellow());
println!("Score: {:.2} / {}", relationship.score, relationship.threshold);
println!("Transmission enabled: {}",
if relationship.transmission_enabled { "".green() } else { "".red() });
}
println!("\n{}", "✅ ChatGPT import completed successfully!".green().bold());
Ok(())
}
#[derive(Debug, Clone)]
pub struct ImportStats {
pub conversations_imported: usize,
pub messages_imported: usize,
pub user_messages: usize,
pub assistant_messages: usize,
pub skipped_messages: usize,
}
impl Default for ImportStats {
fn default() -> Self {
ImportStats {
conversations_imported: 0,
messages_imported: 0,
user_messages: 0,
assistant_messages: 0,
skipped_messages: 0,
}
}
}
pub struct ChatGPTImporter {
user_id: String,
stats: ImportStats,
}
impl ChatGPTImporter {
pub fn new(user_id: String) -> Self {
ChatGPTImporter {
user_id,
stats: ImportStats::default(),
}
}
pub async fn import_from_file(&mut self, file_path: &PathBuf, persona: &mut Persona) -> Result<ImportStats> {
// Read and parse the JSON file
let content = std::fs::read_to_string(file_path)
.with_context(|| format!("Failed to read file: {}", file_path.display()))?;
let conversations: Vec<ChatGPTConversation> = serde_json::from_str(&content)
.context("Failed to parse ChatGPT export JSON")?;
println!("Found {} conversations to import", conversations.len());
// Import each conversation
for (i, conversation) in conversations.iter().enumerate() {
if i % 10 == 0 && i > 0 {
println!("Processed {} / {} conversations...", i, conversations.len());
}
match self.import_single_conversation(conversation, persona).await {
Ok(_) => {
self.stats.conversations_imported += 1;
}
Err(e) => {
println!("{}: Failed to import conversation '{}': {}",
"Warning".yellow(),
conversation.title.as_deref().unwrap_or("Untitled"),
e);
}
}
}
Ok(self.stats.clone())
}
async fn import_single_conversation(&mut self, conversation: &ChatGPTConversation, persona: &mut Persona) -> Result<()> {
// Extract messages from the mapping structure
let messages = self.extract_messages_from_mapping(&conversation.mapping)?;
if messages.is_empty() {
return Ok(());
}
// Process each message
for message in messages {
match self.process_message(&message, persona).await {
Ok(_) => {
self.stats.messages_imported += 1;
}
Err(_) => {
self.stats.skipped_messages += 1;
}
}
}
Ok(())
}
fn extract_messages_from_mapping(&self, mapping: &HashMap<String, ChatGPTNode>) -> Result<Vec<ChatGPTMessage>> {
let mut messages = Vec::new();
// Find all message nodes and collect them
for node in mapping.values() {
if let Some(message) = &node.message {
// Skip system messages and other non-user/assistant messages
if let Some(role) = &message.author.role {
match role.as_str() {
"user" | "assistant" => {
if let Some(content) = &message.content {
let content_text = if content.content_type == "text" && !content.parts.is_empty() {
// Extract text from parts (handle both strings and mixed content)
content.parts.iter()
.filter_map(|part| part.as_str())
.collect::<Vec<&str>>()
.join("\n")
} else if content.content_type == "multimodal_text" {
// Extract text parts from multimodal content
let mut text_parts = Vec::new();
for part in &content.parts {
if let Some(text) = part.as_str() {
if !text.is_empty() {
text_parts.push(text);
}
}
// Skip non-text parts (like image_asset_pointer)
}
if text_parts.is_empty() {
continue; // Skip if no text content
}
text_parts.join("\n")
} else if content.content_type == "user_editable_context" {
// Handle user context messages
if let Some(instructions) = &content.user_instructions {
format!("User instructions: {}", instructions)
} else if let Some(profile) = &content.user_profile {
format!("User profile: {}", profile)
} else {
continue; // Skip empty context messages
}
} else {
continue; // Skip other content types for now
};
if !content_text.trim().is_empty() {
messages.push(ChatGPTMessage {
role: role.clone(),
content: content_text,
create_time: message.create_time,
});
}
}
}
_ => {} // Skip system, tool, etc.
}
}
}
}
// Sort messages by creation time
messages.sort_by(|a, b| {
let time_a = a.create_time.unwrap_or(0.0);
let time_b = b.create_time.unwrap_or(0.0);
time_a.partial_cmp(&time_b).unwrap_or(std::cmp::Ordering::Equal)
});
Ok(messages)
}
async fn process_message(&mut self, message: &ChatGPTMessage, persona: &mut Persona) -> Result<()> {
let timestamp = self.convert_timestamp(message.create_time.unwrap_or(0.0))?;
match message.role.as_str() {
"user" => {
self.add_user_message(&message.content, timestamp, persona)?;
self.stats.user_messages += 1;
}
"assistant" => {
self.add_assistant_message(&message.content, timestamp, persona)?;
self.stats.assistant_messages += 1;
}
_ => {
return Err(anyhow::anyhow!("Unsupported message role: {}", message.role));
}
}
Ok(())
}
fn add_user_message(&self, content: &str, timestamp: DateTime<Utc>, persona: &mut Persona) -> Result<()> {
// Create high-importance memory for user messages
let memory = Memory {
id: uuid::Uuid::new_v4().to_string(),
user_id: self.user_id.clone(),
content: content.to_string(),
summary: None,
importance: 0.8, // High importance for imported user data
memory_type: MemoryType::Core,
created_at: timestamp,
last_accessed: timestamp,
access_count: 1,
};
// Add memory and update relationship
persona.add_memory(memory)?;
persona.update_relationship(&self.user_id, 1.0)?; // Positive relationship boost
Ok(())
}
fn add_assistant_message(&self, content: &str, timestamp: DateTime<Utc>, persona: &mut Persona) -> Result<()> {
// Create medium-importance memory for assistant responses
let memory = Memory {
id: uuid::Uuid::new_v4().to_string(),
user_id: self.user_id.clone(),
content: format!("[AI Response] {}", content),
summary: Some("Imported ChatGPT response".to_string()),
importance: 0.6, // Medium importance for AI responses
memory_type: MemoryType::Summary,
created_at: timestamp,
last_accessed: timestamp,
access_count: 1,
};
persona.add_memory(memory)?;
Ok(())
}
fn convert_timestamp(&self, unix_timestamp: f64) -> Result<DateTime<Utc>> {
if unix_timestamp <= 0.0 {
return Ok(Utc::now());
}
DateTime::from_timestamp(
unix_timestamp as i64,
((unix_timestamp % 1.0) * 1_000_000_000.0) as u32
).ok_or_else(|| anyhow::anyhow!("Invalid timestamp: {}", unix_timestamp))
}
}
// ChatGPT Export Data Structures
#[derive(Debug, Deserialize)]
pub struct ChatGPTConversation {
pub title: Option<String>,
pub create_time: Option<f64>,
pub mapping: HashMap<String, ChatGPTNode>,
}
#[derive(Debug, Deserialize)]
pub struct ChatGPTNode {
pub id: Option<String>,
pub message: Option<ChatGPTNodeMessage>,
pub parent: Option<String>,
pub children: Vec<String>,
}
#[derive(Debug, Deserialize)]
pub struct ChatGPTNodeMessage {
pub id: String,
pub author: ChatGPTAuthor,
pub create_time: Option<f64>,
pub content: Option<ChatGPTContent>,
}
#[derive(Debug, Deserialize)]
pub struct ChatGPTAuthor {
pub role: Option<String>,
pub name: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct ChatGPTContent {
pub content_type: String,
#[serde(default)]
pub parts: Vec<serde_json::Value>,
#[serde(default)]
pub user_profile: Option<String>,
#[serde(default)]
pub user_instructions: Option<String>,
}
// Simplified message structure for processing
#[derive(Debug, Clone)]
pub struct ChatGPTMessage {
pub role: String,
pub content: String,
pub create_time: Option<f64>,
}

View File

@@ -1,2 +1,20 @@
pub mod core;
pub mod mcp;
#![allow(dead_code)]
pub mod ai_provider;
pub mod cli;
pub mod config;
pub mod conversation;
pub mod docs;
pub mod http_client;
pub mod import;
pub mod mcp_server;
pub mod memory;
pub mod openai_provider;
pub mod persona;
pub mod relationship;
pub mod scheduler;
pub mod shell;
pub mod status;
pub mod submodules;
pub mod tokens;
pub mod transmission;

View File

@@ -1,12 +1,31 @@
use anyhow::Result;
use clap::{Parser, Subcommand};
#![allow(dead_code)]
use aigpt::core::{Memory, MemoryStore};
use aigpt::mcp::BaseMCPServer;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
mod ai_provider;
mod cli;
use cli::TokenCommands;
mod config;
mod conversation;
mod docs;
mod http_client;
mod import;
mod mcp_server;
mod memory;
mod openai_provider;
mod persona;
mod relationship;
mod scheduler;
mod shell;
mod status;
mod submodules;
mod tokens;
mod transmission;
#[derive(Parser)]
#[command(name = "aigpt")]
#[command(about = "Simple memory storage for Claude with MCP - Layer 1")]
#[command(about = "AI.GPT - Autonomous transmission AI with unique personality")]
#[command(version)]
struct Cli {
#[command(subcommand)]
@@ -15,127 +34,218 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
/// Check AI status and relationships
Status {
/// User ID to check status for
user_id: Option<String>,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Chat with the AI
Chat {
/// User ID (atproto DID)
user_id: String,
/// Message to send to AI
message: String,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
/// AI model to use
#[arg(short, long)]
model: Option<String>,
/// AI provider (ollama/openai)
#[arg(long)]
provider: Option<String>,
},
/// Start continuous conversation mode with MCP integration
Conversation {
/// User ID (atproto DID)
user_id: String,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
/// AI model to use
#[arg(short, long)]
model: Option<String>,
/// AI provider (ollama/openai)
#[arg(long)]
provider: Option<String>,
},
/// Start continuous conversation mode with MCP integration (alias)
Conv {
/// User ID (atproto DID)
user_id: String,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
/// AI model to use
#[arg(short, long)]
model: Option<String>,
/// AI provider (ollama/openai)
#[arg(long)]
provider: Option<String>,
},
/// Check today's AI fortune
Fortune {
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// List all relationships
Relationships {
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Check and send autonomous transmissions
Transmit {
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Run daily maintenance tasks
Maintenance {
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Run scheduled tasks
Schedule {
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Start MCP server
Server {
/// Enable Layer 4 relationship features (for games/companions)
/// Port to listen on
#[arg(short, long, default_value = "8080")]
port: u16,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Interactive shell mode
Shell {
/// User ID (atproto DID)
user_id: String,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
/// AI model to use
#[arg(short, long)]
model: Option<String>,
/// AI provider (ollama/openai)
#[arg(long)]
enable_layer4: bool,
provider: Option<String>,
},
/// Create a new memory
Create {
/// Content of the memory
content: String,
/// Import ChatGPT conversation data
ImportChatgpt {
/// Path to ChatGPT export JSON file
file_path: PathBuf,
/// User ID for imported conversations
#[arg(short, long)]
user_id: Option<String>,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Get a memory by ID
Get {
/// Memory ID
id: String,
/// Documentation management
Docs {
/// Action to perform (generate, sync, list, status)
action: String,
/// Project name for generate/sync actions
#[arg(short, long)]
project: Option<String>,
/// Output path for generated documentation
#[arg(short, long)]
output: Option<PathBuf>,
/// Enable AI integration for documentation enhancement
#[arg(long)]
ai_integration: bool,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Update a memory
Update {
/// Memory ID
id: String,
/// New content
content: String,
/// Submodule management
Submodules {
/// Action to perform (list, update, status)
action: String,
/// Specific module to update
#[arg(short, long)]
module: Option<String>,
/// Update all submodules
#[arg(long)]
all: bool,
/// Show what would be done without making changes
#[arg(long)]
dry_run: bool,
/// Auto-commit changes after update
#[arg(long)]
auto_commit: bool,
/// Show verbose output
#[arg(short, long)]
verbose: bool,
/// Data directory
#[arg(short, long)]
data_dir: Option<PathBuf>,
},
/// Delete a memory
Delete {
/// Memory ID
id: String,
/// Token usage analysis and cost estimation
Tokens {
#[command(subcommand)]
command: TokenCommands,
},
/// List all memories
List,
/// Search memories by content
Search {
/// Search query
query: String,
},
/// Show statistics
Stats,
}
#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Server { enable_layer4 } => {
let server = BaseMCPServer::new(enable_layer4)?;
server.run()?;
Commands::Status { user_id, data_dir } => {
status::handle_status(user_id, data_dir).await
}
Commands::Create { content } => {
let store = MemoryStore::default()?;
let memory = Memory::new(content);
store.create(&memory)?;
println!("Created memory: {}", memory.id);
Commands::Chat { user_id, message, data_dir, model, provider } => {
cli::handle_chat(user_id, message, data_dir, model, provider).await
}
Commands::Get { id } => {
let store = MemoryStore::default()?;
let memory = store.get(&id)?;
println!("ID: {}", memory.id);
println!("Content: {}", memory.content);
println!("Created: {}", memory.created_at);
println!("Updated: {}", memory.updated_at);
Commands::Conversation { user_id, data_dir, model, provider } => {
conversation::handle_conversation(user_id, data_dir, model, provider).await
}
Commands::Update { id, content } => {
let store = MemoryStore::default()?;
let mut memory = store.get(&id)?;
memory.update_content(content);
store.update(&memory)?;
println!("Updated memory: {}", memory.id);
Commands::Conv { user_id, data_dir, model, provider } => {
conversation::handle_conversation(user_id, data_dir, model, provider).await
}
Commands::Delete { id } => {
let store = MemoryStore::default()?;
store.delete(&id)?;
println!("Deleted memory: {}", id);
Commands::Fortune { data_dir } => {
cli::handle_fortune(data_dir).await
}
Commands::List => {
let store = MemoryStore::default()?;
let memories = store.list()?;
if memories.is_empty() {
println!("No memories found");
} else {
for memory in memories {
println!("\n[{}]", memory.id);
println!(" {}", memory.content);
println!(" Created: {}", memory.created_at);
Commands::Relationships { data_dir } => {
cli::handle_relationships(data_dir).await
}
Commands::Transmit { data_dir } => {
cli::handle_transmit(data_dir).await
}
Commands::Maintenance { data_dir } => {
cli::handle_maintenance(data_dir).await
}
Commands::Schedule { data_dir } => {
cli::handle_schedule(data_dir).await
}
Commands::Server { port, data_dir } => {
cli::handle_server(Some(port), data_dir).await
}
Commands::Shell { user_id, data_dir, model, provider } => {
shell::handle_shell(user_id, data_dir, model, provider).await
}
Commands::ImportChatgpt { file_path, user_id, data_dir } => {
import::handle_import_chatgpt(file_path, user_id, data_dir).await
}
Commands::Docs { action, project, output, ai_integration, data_dir } => {
docs::handle_docs(action, project, output, ai_integration, data_dir).await
}
Commands::Submodules { action, module, all, dry_run, auto_commit, verbose, data_dir } => {
submodules::handle_submodules(action, module, all, dry_run, auto_commit, verbose, data_dir).await
}
Commands::Tokens { command } => {
tokens::handle_tokens(command).await
}
}
}
Commands::Search { query } => {
let store = MemoryStore::default()?;
let memories = store.search(&query)?;
if memories.is_empty() {
println!("No memories found matching '{}'", query);
} else {
println!("Found {} memory(ies):", memories.len());
for memory in memories {
println!("\n[{}]", memory.id);
println!(" {}", memory.content);
println!(" Created: {}", memory.created_at);
}
}
}
Commands::Stats => {
let store = MemoryStore::default()?;
let count = store.count()?;
println!("Total memories: {}", count);
}
}
Ok(())
}

View File

@@ -1,648 +0,0 @@
use anyhow::Result;
use serde_json::{json, Value};
use std::io::{self, BufRead, Write};
use crate::core::{Memory, MemoryStore, UserAnalysis, infer_all_relationships, get_relationship};
pub struct BaseMCPServer {
store: MemoryStore,
enable_layer4: bool,
}
impl BaseMCPServer {
pub fn new(enable_layer4: bool) -> Result<Self> {
let store = MemoryStore::default()?;
Ok(BaseMCPServer { store, enable_layer4 })
}
pub fn run(&self) -> Result<()> {
let stdin = io::stdin();
let mut stdout = io::stdout();
let reader = stdin.lock();
let lines = reader.lines();
for line_result in lines {
match line_result {
Ok(line) => {
let trimmed = line.trim();
if trimmed.is_empty() {
continue;
}
if let Ok(request) = serde_json::from_str::<Value>(&trimmed) {
let response = self.handle_request(request);
let response_str = serde_json::to_string(&response)?;
stdout.write_all(response_str.as_bytes())?;
stdout.write_all(b"\n")?;
stdout.flush()?;
}
}
Err(_) => break,
}
}
Ok(())
}
fn handle_request(&self, request: Value) -> Value {
let method = request["method"].as_str().unwrap_or("");
let id = request["id"].clone();
match method {
"initialize" => self.handle_initialize(id),
"tools/list" => self.handle_tools_list(id),
"tools/call" => self.handle_tools_call(request, id),
_ => self.handle_unknown_method(id),
}
}
fn handle_initialize(&self, id: Value) -> Value {
json!({
"jsonrpc": "2.0",
"id": id,
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {}
},
"serverInfo": {
"name": "aigpt",
"version": "0.2.0"
}
}
})
}
fn handle_tools_list(&self, id: Value) -> Value {
let tools = self.get_available_tools();
json!({
"jsonrpc": "2.0",
"id": id,
"result": {
"tools": tools
}
})
}
fn get_available_tools(&self) -> Vec<Value> {
let mut tools = vec![
json!({
"name": "create_memory",
"description": "Create a new memory entry (Layer 1: simple storage)",
"inputSchema": {
"type": "object",
"properties": {
"content": {
"type": "string",
"description": "Content of the memory"
}
},
"required": ["content"]
}
}),
json!({
"name": "create_ai_memory",
"description": "Create a memory with AI interpretation and priority score (Layer 2)",
"inputSchema": {
"type": "object",
"properties": {
"content": {
"type": "string",
"description": "Original content of the memory"
},
"ai_interpretation": {
"type": "string",
"description": "AI's creative interpretation of the content (optional)"
},
"priority_score": {
"type": "number",
"description": "Priority score from 0.0 (low) to 1.0 (high) (optional)",
"minimum": 0.0,
"maximum": 1.0
}
},
"required": ["content"]
}
}),
json!({
"name": "get_memory",
"description": "Get a memory by ID",
"inputSchema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Memory ID"
}
},
"required": ["id"]
}
}),
json!({
"name": "search_memories",
"description": "Search memories by content",
"inputSchema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
}
},
"required": ["query"]
}
}),
json!({
"name": "list_memories",
"description": "List all memories",
"inputSchema": {
"type": "object",
"properties": {}
}
}),
json!({
"name": "update_memory",
"description": "Update an existing memory entry",
"inputSchema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "ID of the memory to update"
},
"content": {
"type": "string",
"description": "New content for the memory"
}
},
"required": ["id", "content"]
}
}),
json!({
"name": "delete_memory",
"description": "Delete a memory entry",
"inputSchema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "ID of the memory to delete"
}
},
"required": ["id"]
}
}),
json!({
"name": "save_user_analysis",
"description": "Save a Big Five personality analysis based on user's memories (Layer 3)",
"inputSchema": {
"type": "object",
"properties": {
"openness": {
"type": "number",
"description": "Openness to Experience (0.0-1.0)",
"minimum": 0.0,
"maximum": 1.0
},
"conscientiousness": {
"type": "number",
"description": "Conscientiousness (0.0-1.0)",
"minimum": 0.0,
"maximum": 1.0
},
"extraversion": {
"type": "number",
"description": "Extraversion (0.0-1.0)",
"minimum": 0.0,
"maximum": 1.0
},
"agreeableness": {
"type": "number",
"description": "Agreeableness (0.0-1.0)",
"minimum": 0.0,
"maximum": 1.0
},
"neuroticism": {
"type": "number",
"description": "Neuroticism (0.0-1.0)",
"minimum": 0.0,
"maximum": 1.0
},
"summary": {
"type": "string",
"description": "AI-generated summary of the personality analysis"
}
},
"required": ["openness", "conscientiousness", "extraversion", "agreeableness", "neuroticism", "summary"]
}
}),
json!({
"name": "get_user_analysis",
"description": "Get the most recent Big Five personality analysis (Layer 3)",
"inputSchema": {
"type": "object",
"properties": {}
}
}),
json!({
"name": "get_profile",
"description": "Get integrated user profile - the essential summary of personality, interests, and values (Layer 3.5). This is the primary tool for understanding the user.",
"inputSchema": {
"type": "object",
"properties": {}
}
}),
];
// Layer 4 tools (optional - only when enabled)
if self.enable_layer4 {
tools.extend(vec![
json!({
"name": "get_relationship",
"description": "Get inferred relationship with a specific entity (Layer 4). Analyzes memories and user profile to infer bond strength and relationship type. Use only when game/relationship features are active.",
"inputSchema": {
"type": "object",
"properties": {
"entity_id": {
"type": "string",
"description": "Entity identifier (e.g., 'alice', 'companion_miku')"
}
},
"required": ["entity_id"]
}
}),
json!({
"name": "list_relationships",
"description": "List all inferred relationships sorted by bond strength (Layer 4). Returns relationships with all tracked entities. Use only when game/relationship features are active.",
"inputSchema": {
"type": "object",
"properties": {
"limit": {
"type": "number",
"description": "Maximum number of relationships to return (default: 10)"
}
}
}
}),
]);
}
tools
}
fn handle_tools_call(&self, request: Value, id: Value) -> Value {
let tool_name = request["params"]["name"].as_str().unwrap_or("");
let arguments = &request["params"]["arguments"];
let result = self.execute_tool(tool_name, arguments);
json!({
"jsonrpc": "2.0",
"id": id,
"result": {
"content": [{
"type": "text",
"text": result.to_string()
}]
}
})
}
fn execute_tool(&self, tool_name: &str, arguments: &Value) -> Value {
match tool_name {
"create_memory" => self.tool_create_memory(arguments),
"create_ai_memory" => self.tool_create_ai_memory(arguments),
"get_memory" => self.tool_get_memory(arguments),
"search_memories" => self.tool_search_memories(arguments),
"list_memories" => self.tool_list_memories(),
"update_memory" => self.tool_update_memory(arguments),
"delete_memory" => self.tool_delete_memory(arguments),
"save_user_analysis" => self.tool_save_user_analysis(arguments),
"get_user_analysis" => self.tool_get_user_analysis(),
"get_profile" => self.tool_get_profile(),
// Layer 4 tools (require --enable-layer4 flag)
"get_relationship" | "list_relationships" => {
if !self.enable_layer4 {
return json!({
"success": false,
"error": "Layer 4 is not enabled. Start server with --enable-layer4 flag to use relationship features."
});
}
match tool_name {
"get_relationship" => self.tool_get_relationship(arguments),
"list_relationships" => self.tool_list_relationships(arguments),
_ => unreachable!(),
}
}
_ => json!({
"success": false,
"error": format!("Unknown tool: {}", tool_name)
}),
}
}
fn tool_create_memory(&self, arguments: &Value) -> Value {
let content = arguments["content"].as_str().unwrap_or("");
let memory = Memory::new(content.to_string());
match self.store.create(&memory) {
Ok(()) => json!({
"success": true,
"id": memory.id,
"message": "Memory created successfully"
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_create_ai_memory(&self, arguments: &Value) -> Value {
let content = arguments["content"].as_str().unwrap_or("");
let ai_interpretation = arguments["ai_interpretation"]
.as_str()
.map(|s| s.to_string());
let priority_score = arguments["priority_score"].as_f64().map(|f| f as f32);
let memory = Memory::new_ai(content.to_string(), ai_interpretation, priority_score);
match self.store.create(&memory) {
Ok(()) => json!({
"success": true,
"id": memory.id,
"message": "AI memory created successfully",
"has_interpretation": memory.ai_interpretation.is_some(),
"has_score": memory.priority_score.is_some()
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_get_memory(&self, arguments: &Value) -> Value {
let id = arguments["id"].as_str().unwrap_or("");
match self.store.get(id) {
Ok(memory) => json!({
"success": true,
"memory": {
"id": memory.id,
"content": memory.content,
"ai_interpretation": memory.ai_interpretation,
"priority_score": memory.priority_score,
"created_at": memory.created_at,
"updated_at": memory.updated_at
}
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_search_memories(&self, arguments: &Value) -> Value {
let query = arguments["query"].as_str().unwrap_or("");
match self.store.search(query) {
Ok(memories) => json!({
"success": true,
"memories": memories.into_iter().map(|m| json!({
"id": m.id,
"content": m.content,
"ai_interpretation": m.ai_interpretation,
"priority_score": m.priority_score,
"created_at": m.created_at,
"updated_at": m.updated_at
})).collect::<Vec<_>>()
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_list_memories(&self) -> Value {
match self.store.list() {
Ok(memories) => json!({
"success": true,
"memories": memories.into_iter().map(|m| json!({
"id": m.id,
"content": m.content,
"ai_interpretation": m.ai_interpretation,
"priority_score": m.priority_score,
"created_at": m.created_at,
"updated_at": m.updated_at
})).collect::<Vec<_>>()
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_update_memory(&self, arguments: &Value) -> Value {
let id = arguments["id"].as_str().unwrap_or("");
let content = arguments["content"].as_str().unwrap_or("");
match self.store.get(id) {
Ok(mut memory) => {
memory.update_content(content.to_string());
match self.store.update(&memory) {
Ok(()) => json!({
"success": true,
"message": "Memory updated successfully"
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_delete_memory(&self, arguments: &Value) -> Value {
let id = arguments["id"].as_str().unwrap_or("");
match self.store.delete(id) {
Ok(()) => json!({
"success": true,
"message": "Memory deleted successfully"
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
// ========== Layer 3: User Analysis Tools ==========
fn tool_save_user_analysis(&self, arguments: &Value) -> Value {
let openness = arguments["openness"].as_f64().unwrap_or(0.5) as f32;
let conscientiousness = arguments["conscientiousness"].as_f64().unwrap_or(0.5) as f32;
let extraversion = arguments["extraversion"].as_f64().unwrap_or(0.5) as f32;
let agreeableness = arguments["agreeableness"].as_f64().unwrap_or(0.5) as f32;
let neuroticism = arguments["neuroticism"].as_f64().unwrap_or(0.5) as f32;
let summary = arguments["summary"].as_str().unwrap_or("").to_string();
let analysis = UserAnalysis::new(
openness,
conscientiousness,
extraversion,
agreeableness,
neuroticism,
summary,
);
match self.store.save_analysis(&analysis) {
Ok(()) => json!({
"success": true,
"id": analysis.id,
"message": "User analysis saved successfully",
"dominant_trait": analysis.dominant_trait()
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_get_user_analysis(&self) -> Value {
match self.store.get_latest_analysis() {
Ok(Some(analysis)) => json!({
"success": true,
"analysis": {
"id": analysis.id,
"openness": analysis.openness,
"conscientiousness": analysis.conscientiousness,
"extraversion": analysis.extraversion,
"agreeableness": analysis.agreeableness,
"neuroticism": analysis.neuroticism,
"summary": analysis.summary,
"dominant_trait": analysis.dominant_trait(),
"analyzed_at": analysis.analyzed_at
}
}),
Ok(None) => json!({
"success": true,
"analysis": null,
"message": "No analysis found. Run personality analysis first."
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_get_profile(&self) -> Value {
match self.store.get_profile() {
Ok(profile) => json!({
"success": true,
"profile": {
"dominant_traits": profile.dominant_traits,
"core_interests": profile.core_interests,
"core_values": profile.core_values,
"key_memory_ids": profile.key_memory_ids,
"data_quality": profile.data_quality,
"last_updated": profile.last_updated
}
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn tool_get_relationship(&self, arguments: &Value) -> Value {
let entity_id = arguments["entity_id"].as_str().unwrap_or("");
if entity_id.is_empty() {
return json!({
"success": false,
"error": "entity_id is required"
});
}
// Get relationship (with caching)
match get_relationship(&self.store, entity_id) {
Ok(relationship) => json!({
"success": true,
"relationship": {
"entity_id": relationship.entity_id,
"interaction_count": relationship.interaction_count,
"avg_priority": relationship.avg_priority,
"days_since_last": relationship.days_since_last,
"bond_strength": relationship.bond_strength,
"relationship_type": relationship.relationship_type,
"confidence": relationship.confidence,
"inferred_at": relationship.inferred_at
}
}),
Err(e) => json!({
"success": false,
"error": format!("Failed to get relationship: {}", e)
}),
}
}
fn tool_list_relationships(&self, arguments: &Value) -> Value {
let limit = arguments["limit"].as_u64().unwrap_or(10) as usize;
match infer_all_relationships(&self.store) {
Ok(mut relationships) => {
// Limit results
if relationships.len() > limit {
relationships.truncate(limit);
}
json!({
"success": true,
"relationships": relationships.iter().map(|r| {
json!({
"entity_id": r.entity_id,
"interaction_count": r.interaction_count,
"avg_priority": r.avg_priority,
"days_since_last": r.days_since_last,
"bond_strength": r.bond_strength,
"relationship_type": r.relationship_type,
"confidence": r.confidence
})
}).collect::<Vec<_>>()
})
}
Err(e) => json!({
"success": false,
"error": e.to_string()
}),
}
}
fn handle_unknown_method(&self, id: Value) -> Value {
json!({
"jsonrpc": "2.0",
"id": id,
"error": {
"code": -32601,
"message": "Method not found"
}
})
}
}

View File

@@ -1,3 +0,0 @@
pub mod base;
pub use base::BaseMCPServer;

1951
src/mcp_server.rs Normal file

File diff suppressed because it is too large Load Diff

306
src/memory.rs Normal file
View File

@@ -0,0 +1,306 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use anyhow::{Result, Context};
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::config::Config;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Memory {
pub id: String,
pub user_id: String,
pub content: String,
pub summary: Option<String>,
pub importance: f64,
pub memory_type: MemoryType,
pub created_at: DateTime<Utc>,
pub last_accessed: DateTime<Utc>,
pub access_count: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryType {
Interaction,
Summary,
Core,
Forgotten,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryManager {
memories: HashMap<String, Memory>,
config: Config,
}
impl MemoryManager {
pub fn new(config: &Config) -> Result<Self> {
let memories = Self::load_memories(config)?;
Ok(MemoryManager {
memories,
config: config.clone(),
})
}
pub fn add_memory(&mut self, user_id: &str, content: &str, importance: f64) -> Result<String> {
let memory_id = Uuid::new_v4().to_string();
let now = Utc::now();
let memory = Memory {
id: memory_id.clone(),
user_id: user_id.to_string(),
content: content.to_string(),
summary: None,
importance,
memory_type: MemoryType::Interaction,
created_at: now,
last_accessed: now,
access_count: 1,
};
self.memories.insert(memory_id.clone(), memory);
self.save_memories()?;
Ok(memory_id)
}
pub fn get_memories(&mut self, user_id: &str, limit: usize) -> Vec<&Memory> {
// Get immutable references for sorting
let mut user_memory_ids: Vec<_> = self.memories
.iter()
.filter(|(_, m)| m.user_id == user_id)
.map(|(id, memory)| {
let score = memory.importance * 0.7 + (1.0 / ((Utc::now() - memory.created_at).num_hours() as f64 + 1.0)) * 0.3;
(id.clone(), score)
})
.collect();
// Sort by score
user_memory_ids.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
// Update access information
let now = Utc::now();
for (memory_id, _) in user_memory_ids.into_iter().take(limit) {
if let Some(memory) = self.memories.get_mut(&memory_id) {
memory.last_accessed = now;
memory.access_count += 1;
// We can't return mutable references here, so we'll need to adjust the return type
}
}
// Return immutable references
self.memories
.values()
.filter(|m| m.user_id == user_id)
.take(limit)
.collect()
}
pub fn search_memories(&self, user_id: &str, keywords: &[String]) -> Vec<&Memory> {
self.memories
.values()
.filter(|m| {
m.user_id == user_id &&
keywords.iter().any(|keyword| {
m.content.to_lowercase().contains(&keyword.to_lowercase()) ||
m.summary.as_ref().map_or(false, |s| s.to_lowercase().contains(&keyword.to_lowercase()))
})
})
.collect()
}
pub fn get_contextual_memories(&self, user_id: &str, query: &str, limit: usize) -> Vec<&Memory> {
let query_lower = query.to_lowercase();
let mut relevant_memories: Vec<_> = self.memories
.values()
.filter(|m| {
m.user_id == user_id && (
m.content.to_lowercase().contains(&query_lower) ||
m.summary.as_ref().map_or(false, |s| s.to_lowercase().contains(&query_lower))
)
})
.collect();
// Sort by relevance (simple keyword matching for now)
relevant_memories.sort_by(|a, b| {
let score_a = Self::calculate_relevance_score(a, &query_lower);
let score_b = Self::calculate_relevance_score(b, &query_lower);
score_b.partial_cmp(&score_a).unwrap_or(std::cmp::Ordering::Equal)
});
relevant_memories.into_iter().take(limit).collect()
}
fn calculate_relevance_score(memory: &Memory, query: &str) -> f64 {
let content_matches = memory.content.to_lowercase().matches(query).count() as f64;
let summary_matches = memory.summary.as_ref()
.map_or(0.0, |s| s.to_lowercase().matches(query).count() as f64);
let relevance = (content_matches + summary_matches) * memory.importance;
let recency_bonus = 1.0 / ((Utc::now() - memory.created_at).num_days() as f64).max(1.0);
relevance + recency_bonus * 0.1
}
pub fn create_summary(&mut self, user_id: &str, content: &str) -> Result<String> {
// Simple summary creation (in real implementation, this would use AI)
let summary = if content.len() > 100 {
format!("{}...", &content[..97])
} else {
content.to_string()
};
self.add_memory(user_id, &summary, 0.8)
}
pub fn create_core_memory(&mut self, user_id: &str, content: &str) -> Result<String> {
let memory_id = Uuid::new_v4().to_string();
let now = Utc::now();
let memory = Memory {
id: memory_id.clone(),
user_id: user_id.to_string(),
content: content.to_string(),
summary: None,
importance: 1.0, // Core memories have maximum importance
memory_type: MemoryType::Core,
created_at: now,
last_accessed: now,
access_count: 1,
};
self.memories.insert(memory_id.clone(), memory);
self.save_memories()?;
Ok(memory_id)
}
pub fn get_memory_stats(&self, user_id: &str) -> MemoryStats {
let user_memories: Vec<_> = self.memories
.values()
.filter(|m| m.user_id == user_id)
.collect();
let total_memories = user_memories.len();
let core_memories = user_memories.iter()
.filter(|m| matches!(m.memory_type, MemoryType::Core))
.count();
let summary_memories = user_memories.iter()
.filter(|m| matches!(m.memory_type, MemoryType::Summary))
.count();
let interaction_memories = user_memories.iter()
.filter(|m| matches!(m.memory_type, MemoryType::Interaction))
.count();
let avg_importance = if total_memories > 0 {
user_memories.iter().map(|m| m.importance).sum::<f64>() / total_memories as f64
} else {
0.0
};
MemoryStats {
total_memories,
core_memories,
summary_memories,
interaction_memories,
avg_importance,
}
}
fn load_memories(config: &Config) -> Result<HashMap<String, Memory>> {
let file_path = config.memory_file();
if !file_path.exists() {
return Ok(HashMap::new());
}
let content = std::fs::read_to_string(file_path)
.context("Failed to read memories file")?;
let memories: HashMap<String, Memory> = serde_json::from_str(&content)
.context("Failed to parse memories file")?;
Ok(memories)
}
fn save_memories(&self) -> Result<()> {
let content = serde_json::to_string_pretty(&self.memories)
.context("Failed to serialize memories")?;
std::fs::write(&self.config.memory_file(), content)
.context("Failed to write memories file")?;
Ok(())
}
pub fn get_stats(&self) -> Result<MemoryStats> {
let total_memories = self.memories.len();
let core_memories = self.memories.values()
.filter(|m| matches!(m.memory_type, MemoryType::Core))
.count();
let summary_memories = self.memories.values()
.filter(|m| matches!(m.memory_type, MemoryType::Summary))
.count();
let interaction_memories = self.memories.values()
.filter(|m| matches!(m.memory_type, MemoryType::Interaction))
.count();
let avg_importance = if total_memories > 0 {
self.memories.values().map(|m| m.importance).sum::<f64>() / total_memories as f64
} else {
0.0
};
Ok(MemoryStats {
total_memories,
core_memories,
summary_memories,
interaction_memories,
avg_importance,
})
}
pub async fn run_maintenance(&mut self) -> Result<()> {
// Cleanup old, low-importance memories
let cutoff_date = Utc::now() - chrono::Duration::days(30);
let memory_ids_to_remove: Vec<String> = self.memories
.iter()
.filter(|(_, m)| {
m.importance < 0.3
&& m.created_at < cutoff_date
&& m.access_count <= 1
&& !matches!(m.memory_type, MemoryType::Core)
})
.map(|(id, _)| id.clone())
.collect();
for id in memory_ids_to_remove {
self.memories.remove(&id);
}
// Mark old memories as forgotten instead of deleting
let forgotten_cutoff = Utc::now() - chrono::Duration::days(90);
for memory in self.memories.values_mut() {
if memory.created_at < forgotten_cutoff
&& memory.importance < 0.2
&& !matches!(memory.memory_type, MemoryType::Core) {
memory.memory_type = MemoryType::Forgotten;
}
}
// Save changes
self.save_memories()?;
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct MemoryStats {
pub total_memories: usize,
pub core_memories: usize,
pub summary_memories: usize,
pub interaction_memories: usize,
pub avg_importance: f64,
}

390
src/openai_provider.rs Normal file
View File

@@ -0,0 +1,390 @@
use anyhow::Result;
use async_openai::{
types::{
ChatCompletionRequestMessage,
CreateChatCompletionRequestArgs, ChatCompletionTool, ChatCompletionToolType,
FunctionObject, ChatCompletionRequestToolMessage,
ChatCompletionRequestAssistantMessage, ChatCompletionRequestUserMessage,
ChatCompletionRequestSystemMessage, ChatCompletionToolChoiceOption
},
Client,
};
use serde_json::{json, Value};
use crate::http_client::ServiceClient;
/// OpenAI provider with MCP tools support (matching Python implementation)
pub struct OpenAIProvider {
client: Client<async_openai::config::OpenAIConfig>,
model: String,
service_client: ServiceClient,
system_prompt: Option<String>,
}
impl OpenAIProvider {
pub fn new(api_key: String, model: Option<String>) -> Self {
let config = async_openai::config::OpenAIConfig::new()
.with_api_key(api_key);
let client = Client::with_config(config);
Self {
client,
model: model.unwrap_or_else(|| "gpt-4".to_string()),
service_client: ServiceClient::new(),
system_prompt: None,
}
}
pub fn with_system_prompt(mut self, prompt: String) -> Self {
self.system_prompt = Some(prompt);
self
}
/// Generate OpenAI tools from MCP endpoints (matching Python implementation)
fn get_mcp_tools(&self) -> Vec<ChatCompletionTool> {
let tools = vec![
// Memory tools
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "get_memories".to_string(),
description: Some("過去の会話記憶を取得します。「覚えている」「前回」「以前」などの質問で必ず使用してください".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"limit": {
"type": "integer",
"description": "取得する記憶の数",
"default": 5
}
}
})),
},
},
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "search_memories".to_string(),
description: Some("特定のトピックについて話した記憶を検索します。「プログラミングについて」「○○について話した」などの質問で使用してください".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"keywords": {
"type": "array",
"items": {"type": "string"},
"description": "検索キーワードの配列"
}
},
"required": ["keywords"]
})),
},
},
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "get_contextual_memories".to_string(),
description: Some("クエリに関連する文脈的記憶を取得します".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "検索クエリ"
},
"limit": {
"type": "integer",
"description": "取得する記憶の数",
"default": 5
}
},
"required": ["query"]
})),
},
},
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "get_relationship".to_string(),
description: Some("特定ユーザーとの関係性情報を取得します".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"user_id": {
"type": "string",
"description": "ユーザーID"
}
},
"required": ["user_id"]
})),
},
},
// ai.card tools
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "card_get_user_cards".to_string(),
description: Some("ユーザーが所有するカードの一覧を取得します".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"did": {
"type": "string",
"description": "ユーザーのDID"
},
"limit": {
"type": "integer",
"description": "取得するカード数の上限",
"default": 10
}
},
"required": ["did"]
})),
},
},
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "card_draw_card".to_string(),
description: Some("ガチャを引いてカードを取得します".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"did": {
"type": "string",
"description": "ユーザーのDID"
},
"is_paid": {
"type": "boolean",
"description": "有料ガチャかどうか",
"default": false
}
},
"required": ["did"]
})),
},
},
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "card_analyze_collection".to_string(),
description: Some("ユーザーのカードコレクションを分析します".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {
"did": {
"type": "string",
"description": "ユーザーのDID"
}
},
"required": ["did"]
})),
},
},
ChatCompletionTool {
r#type: ChatCompletionToolType::Function,
function: FunctionObject {
name: "card_get_gacha_stats".to_string(),
description: Some("ガチャの統計情報を取得します".to_string()),
parameters: Some(json!({
"type": "object",
"properties": {}
})),
},
},
];
tools
}
/// Chat interface with MCP function calling support (matching Python implementation)
pub async fn chat_with_mcp(&self, prompt: String, user_id: String) -> Result<String> {
let tools = self.get_mcp_tools();
let system_content = self.system_prompt.as_deref().unwrap_or(
"あなたは記憶システムと関係性データ、カードゲームシステムにアクセスできるAIです。\n\n【重要】以下の場合は必ずツールを使用してください:\n\n1. カード関連の質問:\n- 「カード」「コレクション」「ガチャ」「見せて」「持っている」「状況」「どんなカード」などのキーワードがある場合\n- card_get_user_cardsツールを使用してユーザーのカード情報を取得\n\n2. 記憶・関係性の質問:\n- 「覚えている」「前回」「以前」「について話した」「関係」などのキーワードがある場合\n- 適切なメモリツールを使用\n\n3. パラメータの設定:\n- didパラメータには現在会話しているユーザーのID'syui')を使用\n- ツールを積極的に使用して正確な情報を提供してください\n\nユーザーが何かを尋ねた時は、まず関連するツールがあるかを考え、適切なツールを使用してから回答してください。"
);
let request = CreateChatCompletionRequestArgs::default()
.model(&self.model)
.messages(vec![
ChatCompletionRequestMessage::System(
ChatCompletionRequestSystemMessage {
content: system_content.to_string().into(),
name: None,
}
),
ChatCompletionRequestMessage::User(
ChatCompletionRequestUserMessage {
content: prompt.clone().into(),
name: None,
}
),
])
.tools(tools)
.tool_choice(ChatCompletionToolChoiceOption::Auto)
.max_tokens(2000u16)
.temperature(0.7)
.build()?;
let response = self.client.chat().create(request).await?;
let message = &response.choices[0].message;
// Handle tool calls
if let Some(tool_calls) = &message.tool_calls {
if tool_calls.is_empty() {
println!("🔧 [OpenAI] No tools called");
} else {
println!("🔧 [OpenAI] {} tools called:", tool_calls.len());
for tc in tool_calls {
println!(" - {}({})", tc.function.name, tc.function.arguments);
}
}
} else {
println!("🔧 [OpenAI] No tools called");
}
// Process tool calls if any
if let Some(tool_calls) = &message.tool_calls {
if !tool_calls.is_empty() {
let mut messages = vec![
ChatCompletionRequestMessage::System(
ChatCompletionRequestSystemMessage {
content: system_content.to_string().into(),
name: None,
}
),
ChatCompletionRequestMessage::User(
ChatCompletionRequestUserMessage {
content: prompt.into(),
name: None,
}
),
ChatCompletionRequestMessage::Assistant(
ChatCompletionRequestAssistantMessage {
content: message.content.clone(),
name: None,
tool_calls: message.tool_calls.clone(),
function_call: None,
}
),
];
// Execute each tool call
for tool_call in tool_calls {
println!("🌐 [MCP] Executing {}...", tool_call.function.name);
let tool_result = self.execute_mcp_tool(tool_call, &user_id).await?;
let result_preview = serde_json::to_string(&tool_result)?;
let preview = if result_preview.chars().count() > 100 {
format!("{}...", result_preview.chars().take(100).collect::<String>())
} else {
result_preview.clone()
};
println!("✅ [MCP] Result: {}", preview);
messages.push(ChatCompletionRequestMessage::Tool(
ChatCompletionRequestToolMessage {
content: serde_json::to_string(&tool_result)?,
tool_call_id: tool_call.id.clone(),
}
));
}
// Get final response with tool outputs
let final_request = CreateChatCompletionRequestArgs::default()
.model(&self.model)
.messages(messages)
.max_tokens(2000u16)
.temperature(0.7)
.build()?;
let final_response = self.client.chat().create(final_request).await?;
Ok(final_response.choices[0].message.content.as_ref().unwrap_or(&"".to_string()).clone())
} else {
// No tools were called
Ok(message.content.as_ref().unwrap_or(&"".to_string()).clone())
}
} else {
// No tool_calls field at all
Ok(message.content.as_ref().unwrap_or(&"".to_string()).clone())
}
}
/// Execute MCP tool call (matching Python implementation)
async fn execute_mcp_tool(&self, tool_call: &async_openai::types::ChatCompletionMessageToolCall, context_user_id: &str) -> Result<Value> {
let function_name = &tool_call.function.name;
let arguments: Value = serde_json::from_str(&tool_call.function.arguments)?;
match function_name.as_str() {
"get_memories" => {
let limit = arguments.get("limit").and_then(|v| v.as_i64()).unwrap_or(5);
// TODO: Implement actual MCP call
Ok(json!({"info": "記憶機能は実装中です"}))
}
"search_memories" => {
let _keywords = arguments.get("keywords").and_then(|v| v.as_array());
// TODO: Implement actual MCP call
Ok(json!({"info": "記憶検索機能は実装中です"}))
}
"get_contextual_memories" => {
let _query = arguments.get("query").and_then(|v| v.as_str()).unwrap_or("");
let _limit = arguments.get("limit").and_then(|v| v.as_i64()).unwrap_or(5);
// TODO: Implement actual MCP call
Ok(json!({"info": "文脈記憶機能は実装中です"}))
}
"get_relationship" => {
let _user_id = arguments.get("user_id").and_then(|v| v.as_str()).unwrap_or(context_user_id);
// TODO: Implement actual MCP call
Ok(json!({"info": "関係性機能は実装中です"}))
}
// ai.card tools
"card_get_user_cards" => {
let did = arguments.get("did").and_then(|v| v.as_str()).unwrap_or(context_user_id);
let _limit = arguments.get("limit").and_then(|v| v.as_i64()).unwrap_or(10);
match self.service_client.get_user_cards(did).await {
Ok(result) => Ok(result),
Err(e) => {
println!("❌ ai.card API error: {}", e);
Ok(json!({
"error": "ai.cardサーバーが起動していません",
"message": "カードシステムを使用するには、ai.cardサーバーを起動してください"
}))
}
}
}
"card_draw_card" => {
let did = arguments.get("did").and_then(|v| v.as_str()).unwrap_or(context_user_id);
let is_paid = arguments.get("is_paid").and_then(|v| v.as_bool()).unwrap_or(false);
match self.service_client.draw_card(did, is_paid).await {
Ok(result) => Ok(result),
Err(e) => {
println!("❌ ai.card API error: {}", e);
Ok(json!({
"error": "ai.cardサーバーが起動していません",
"message": "カードシステムを使用するには、ai.cardサーバーを起動してください"
}))
}
}
}
"card_analyze_collection" => {
let did = arguments.get("did").and_then(|v| v.as_str()).unwrap_or(context_user_id);
// TODO: Implement collection analysis endpoint
Ok(json!({
"info": "コレクション分析機能は実装中です",
"user_did": did
}))
}
"card_get_gacha_stats" => {
// TODO: Implement gacha stats endpoint
Ok(json!({"info": "ガチャ統計機能は実装中です"}))
}
_ => {
Ok(json!({
"error": format!("Unknown tool: {}", function_name)
}))
}
}
}
}

369
src/persona.rs Normal file
View File

@@ -0,0 +1,369 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use anyhow::Result;
use crate::config::Config;
use crate::memory::{MemoryManager, MemoryStats, Memory};
use crate::relationship::{RelationshipTracker, Relationship as RelationshipData, RelationshipStats};
use crate::ai_provider::{AIProviderClient, ChatMessage};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Persona {
config: Config,
#[serde(skip)]
memory_manager: Option<MemoryManager>,
#[serde(skip)]
relationship_tracker: Option<RelationshipTracker>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersonaState {
pub current_mood: String,
pub fortune_value: i32,
pub breakthrough_triggered: bool,
pub base_personality: HashMap<String, f64>,
}
impl Persona {
pub fn new(config: &Config) -> Result<Self> {
let memory_manager = MemoryManager::new(config)?;
let relationship_tracker = RelationshipTracker::new(config)?;
Ok(Persona {
config: config.clone(),
memory_manager: Some(memory_manager),
relationship_tracker: Some(relationship_tracker),
})
}
pub fn get_current_state(&self) -> Result<PersonaState> {
// Load fortune
let fortune_value = self.load_today_fortune()?;
// Create base personality
let mut base_personality = HashMap::new();
base_personality.insert("curiosity".to_string(), 0.7);
base_personality.insert("empathy".to_string(), 0.8);
base_personality.insert("creativity".to_string(), 0.6);
base_personality.insert("analytical".to_string(), 0.9);
base_personality.insert("emotional".to_string(), 0.4);
// Determine mood based on fortune
let current_mood = match fortune_value {
1..=3 => "Contemplative",
4..=6 => "Neutral",
7..=8 => "Optimistic",
9..=10 => "Energetic",
_ => "Unknown",
};
Ok(PersonaState {
current_mood: current_mood.to_string(),
fortune_value,
breakthrough_triggered: fortune_value >= 9,
base_personality,
})
}
pub fn get_relationship(&self, user_id: &str) -> Option<&RelationshipData> {
self.relationship_tracker.as_ref()
.and_then(|tracker| tracker.get_relationship(user_id))
}
pub fn process_interaction(&mut self, user_id: &str, message: &str) -> Result<(String, f64)> {
// Add memory
if let Some(memory_manager) = &mut self.memory_manager {
memory_manager.add_memory(user_id, message, 0.5)?;
}
// Calculate sentiment (simple keyword-based for now)
let sentiment = self.calculate_sentiment(message);
// Update relationship
let relationship_delta = if let Some(relationship_tracker) = &mut self.relationship_tracker {
relationship_tracker.process_interaction(user_id, sentiment)?
} else {
0.0
};
// Generate response (simple for now)
let response = format!("I understand your message: '{}'", message);
Ok((response, relationship_delta))
}
pub async fn process_ai_interaction(&mut self, user_id: &str, message: &str, provider: Option<String>, model: Option<String>) -> Result<(String, f64)> {
// Add memory for user message
if let Some(memory_manager) = &mut self.memory_manager {
memory_manager.add_memory(user_id, message, 0.5)?;
}
// Calculate sentiment
let sentiment = self.calculate_sentiment(message);
// Update relationship
let relationship_delta = if let Some(relationship_tracker) = &mut self.relationship_tracker {
relationship_tracker.process_interaction(user_id, sentiment)?
} else {
0.0
};
// Check provider type and use appropriate client
let response = if provider.as_deref() == Some("openai") {
// Use OpenAI provider with MCP tools
use crate::openai_provider::OpenAIProvider;
// Get OpenAI API key from config or environment
let api_key = std::env::var("OPENAI_API_KEY")
.or_else(|_| {
self.config.providers.get("openai")
.and_then(|p| p.api_key.clone())
.ok_or_else(|| std::env::VarError::NotPresent)
})
.map_err(|_| anyhow::anyhow!("OpenAI API key not found. Set OPENAI_API_KEY environment variable or add to config."))?;
let openai_model = model.unwrap_or_else(|| "gpt-4".to_string());
let openai_provider = OpenAIProvider::new(api_key, Some(openai_model));
// Use OpenAI with MCP tools support
openai_provider.chat_with_mcp(message.to_string(), user_id.to_string()).await?
} else {
// Use existing AI provider (Ollama)
let ai_config = self.config.get_ai_config(provider, model)?;
let ai_client = AIProviderClient::new(ai_config);
// Build conversation context
let mut messages = Vec::new();
// Get recent memories for context
if let Some(memory_manager) = &mut self.memory_manager {
let recent_memories = memory_manager.get_memories(user_id, 5);
if !recent_memories.is_empty() {
let context = recent_memories.iter()
.map(|m| m.content.clone())
.collect::<Vec<_>>()
.join("\n");
messages.push(ChatMessage::system(format!("Previous conversation context:\n{}", context)));
}
}
// Add current message
messages.push(ChatMessage::user(message));
// Generate system prompt based on personality and relationship
let system_prompt = self.generate_system_prompt(user_id);
// Get AI response
match ai_client.chat(messages, Some(system_prompt)).await {
Ok(chat_response) => chat_response.content,
Err(_) => {
// Fallback to simple response if AI fails
format!("I understand your message: '{}'", message)
}
}
};
// Store AI response in memory
if let Some(memory_manager) = &mut self.memory_manager {
memory_manager.add_memory(user_id, &format!("AI: {}", response), 0.3)?;
}
Ok((response, relationship_delta))
}
fn generate_system_prompt(&self, user_id: &str) -> String {
let mut prompt = String::from("You are a helpful AI assistant with a unique personality. ");
// Add personality based on current state
if let Ok(state) = self.get_current_state() {
prompt.push_str(&format!("Your current mood is {}. ", state.current_mood));
if state.breakthrough_triggered {
prompt.push_str("You are feeling particularly inspired today! ");
}
// Add personality traits
let mut traits = Vec::new();
for (trait_name, value) in &state.base_personality {
if *value > 0.7 {
traits.push(trait_name.clone());
}
}
if !traits.is_empty() {
prompt.push_str(&format!("Your dominant traits are: {}. ", traits.join(", ")));
}
}
// Add relationship context
if let Some(relationship) = self.get_relationship(user_id) {
match relationship.status.to_string().as_str() {
"new" => prompt.push_str("This is a new relationship, be welcoming but cautious. "),
"friend" => prompt.push_str("You have a friendly relationship with this user. "),
"close_friend" => prompt.push_str("This is a close friend, be warm and personal. "),
"broken" => prompt.push_str("This relationship is strained, be formal and distant. "),
_ => {}
}
}
prompt.push_str("Keep responses concise and natural. Avoid being overly formal or robotic.");
prompt
}
fn calculate_sentiment(&self, message: &str) -> f64 {
// Simple sentiment analysis based on keywords
let positive_words = ["good", "great", "awesome", "love", "like", "happy", "thank"];
let negative_words = ["bad", "hate", "awful", "terrible", "angry", "sad"];
let message_lower = message.to_lowercase();
let positive_count = positive_words.iter()
.filter(|word| message_lower.contains(*word))
.count() as f64;
let negative_count = negative_words.iter()
.filter(|word| message_lower.contains(*word))
.count() as f64;
(positive_count - negative_count).max(-1.0).min(1.0)
}
pub fn get_memories(&mut self, user_id: &str, limit: usize) -> Vec<String> {
if let Some(memory_manager) = &mut self.memory_manager {
memory_manager.get_memories(user_id, limit)
.into_iter()
.map(|m| m.content.clone())
.collect()
} else {
Vec::new()
}
}
pub fn search_memories(&self, user_id: &str, keywords: &[String]) -> Vec<String> {
if let Some(memory_manager) = &self.memory_manager {
memory_manager.search_memories(user_id, keywords)
.into_iter()
.map(|m| m.content.clone())
.collect()
} else {
Vec::new()
}
}
pub fn get_memory_stats(&self, user_id: &str) -> Option<MemoryStats> {
self.memory_manager.as_ref()
.map(|manager| manager.get_memory_stats(user_id))
}
pub fn get_relationship_stats(&self) -> Option<RelationshipStats> {
self.relationship_tracker.as_ref()
.map(|tracker| tracker.get_relationship_stats())
}
pub fn add_memory(&mut self, memory: Memory) -> Result<()> {
if let Some(memory_manager) = &mut self.memory_manager {
memory_manager.add_memory(&memory.user_id, &memory.content, memory.importance)?;
}
Ok(())
}
pub fn update_relationship(&mut self, user_id: &str, delta: f64) -> Result<()> {
if let Some(relationship_tracker) = &mut self.relationship_tracker {
relationship_tracker.process_interaction(user_id, delta)?;
}
Ok(())
}
pub fn daily_maintenance(&mut self) -> Result<()> {
// Apply time decay to relationships
if let Some(relationship_tracker) = &mut self.relationship_tracker {
relationship_tracker.apply_time_decay()?;
}
Ok(())
}
fn load_today_fortune(&self) -> Result<i32> {
// Try to load existing fortune for today
if let Ok(content) = std::fs::read_to_string(self.config.fortune_file()) {
if let Ok(fortune_data) = serde_json::from_str::<serde_json::Value>(&content) {
let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
if let Some(fortune) = fortune_data.get(&today) {
if let Some(value) = fortune.as_i64() {
return Ok(value as i32);
}
}
}
}
// Generate new fortune for today (1-10)
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
let mut hasher = DefaultHasher::new();
today.hash(&mut hasher);
let hash = hasher.finish();
let fortune = (hash % 10) as i32 + 1;
// Save fortune
let mut fortune_data = if let Ok(content) = std::fs::read_to_string(self.config.fortune_file()) {
serde_json::from_str(&content).unwrap_or_else(|_| serde_json::json!({}))
} else {
serde_json::json!({})
};
fortune_data[today] = serde_json::json!(fortune);
if let Ok(content) = serde_json::to_string_pretty(&fortune_data) {
let _ = std::fs::write(self.config.fortune_file(), content);
}
Ok(fortune)
}
pub fn list_all_relationships(&self) -> HashMap<String, RelationshipData> {
if let Some(tracker) = &self.relationship_tracker {
tracker.list_all_relationships().clone()
} else {
HashMap::new()
}
}
pub async fn process_message(&mut self, user_id: &str, message: &str) -> Result<ChatMessage> {
let (_response, _delta) = self.process_ai_interaction(user_id, message, None, None).await?;
Ok(ChatMessage::assistant(&_response))
}
pub fn get_fortune(&self) -> Result<i32> {
self.load_today_fortune()
}
pub fn generate_new_fortune(&self) -> Result<i32> {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
let mut hasher = DefaultHasher::new();
today.hash(&mut hasher);
let hash = hasher.finish();
let fortune = (hash % 10) as i32 + 1;
// Save fortune
let mut fortune_data = if let Ok(content) = std::fs::read_to_string(self.config.fortune_file()) {
serde_json::from_str(&content).unwrap_or_else(|_| serde_json::json!({}))
} else {
serde_json::json!({})
};
fortune_data[today] = serde_json::json!(fortune);
if let Ok(content) = serde_json::to_string_pretty(&fortune_data) {
let _ = std::fs::write(self.config.fortune_file(), content);
}
Ok(fortune)
}
}

306
src/relationship.rs Normal file
View File

@@ -0,0 +1,306 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use anyhow::{Result, Context};
use chrono::{DateTime, Utc};
use crate::config::Config;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Relationship {
pub user_id: String,
pub score: f64,
pub threshold: f64,
pub status: RelationshipStatus,
pub total_interactions: u32,
pub positive_interactions: u32,
pub negative_interactions: u32,
pub transmission_enabled: bool,
pub is_broken: bool,
pub last_interaction: Option<DateTime<Utc>>,
pub last_transmission: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub daily_interaction_count: u32,
pub last_daily_reset: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RelationshipStatus {
New,
Acquaintance,
Friend,
CloseFriend,
Broken,
}
impl std::fmt::Display for RelationshipStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RelationshipStatus::New => write!(f, "new"),
RelationshipStatus::Acquaintance => write!(f, "acquaintance"),
RelationshipStatus::Friend => write!(f, "friend"),
RelationshipStatus::CloseFriend => write!(f, "close_friend"),
RelationshipStatus::Broken => write!(f, "broken"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelationshipTracker {
relationships: HashMap<String, Relationship>,
config: Config,
}
impl RelationshipTracker {
pub fn new(config: &Config) -> Result<Self> {
let relationships = Self::load_relationships(config)?;
Ok(RelationshipTracker {
relationships,
config: config.clone(),
})
}
pub fn get_or_create_relationship(&mut self, user_id: &str) -> &mut Relationship {
let now = Utc::now();
self.relationships.entry(user_id.to_string()).or_insert_with(|| {
Relationship {
user_id: user_id.to_string(),
score: 0.0,
threshold: 10.0, // Default threshold for transmission
status: RelationshipStatus::New,
total_interactions: 0,
positive_interactions: 0,
negative_interactions: 0,
transmission_enabled: false,
is_broken: false,
last_interaction: None,
last_transmission: None,
created_at: now,
daily_interaction_count: 0,
last_daily_reset: now,
}
})
}
pub fn process_interaction(&mut self, user_id: &str, sentiment: f64) -> Result<f64> {
let now = Utc::now();
let score_change;
// Create relationship if it doesn't exist
{
let relationship = self.get_or_create_relationship(user_id);
// Reset daily count if needed
if (now - relationship.last_daily_reset).num_days() >= 1 {
relationship.daily_interaction_count = 0;
relationship.last_daily_reset = now;
}
// Apply daily interaction limit
if relationship.daily_interaction_count >= 10 {
return Ok(0.0); // No score change due to daily limit
}
// Store previous score for potential future logging
// Calculate score change based on sentiment
let mut base_score_change = sentiment * 0.5; // Base change
// Apply diminishing returns for high interaction counts
let interaction_factor = 1.0 / (1.0 + relationship.total_interactions as f64 * 0.01);
base_score_change *= interaction_factor;
score_change = base_score_change;
// Update relationship data
relationship.score += score_change;
relationship.score = relationship.score.max(-50.0).min(100.0); // Clamp score
relationship.total_interactions += 1;
relationship.daily_interaction_count += 1;
relationship.last_interaction = Some(now);
if sentiment > 0.0 {
relationship.positive_interactions += 1;
} else if sentiment < 0.0 {
relationship.negative_interactions += 1;
}
// Check for relationship breaking
if relationship.score <= -20.0 && !relationship.is_broken {
relationship.is_broken = true;
relationship.transmission_enabled = false;
relationship.status = RelationshipStatus::Broken;
}
// Enable transmission if threshold is reached
if relationship.score >= relationship.threshold && !relationship.is_broken {
relationship.transmission_enabled = true;
}
}
// Update status based on score (separate borrow)
self.update_relationship_status(user_id);
self.save_relationships()?;
Ok(score_change)
}
fn update_relationship_status(&mut self, user_id: &str) {
if let Some(relationship) = self.relationships.get_mut(user_id) {
if relationship.is_broken {
return; // Broken relationships cannot change status
}
relationship.status = match relationship.score {
score if score >= 50.0 => RelationshipStatus::CloseFriend,
score if score >= 20.0 => RelationshipStatus::Friend,
score if score >= 5.0 => RelationshipStatus::Acquaintance,
_ => RelationshipStatus::New,
};
}
}
pub fn apply_time_decay(&mut self) -> Result<()> {
let now = Utc::now();
let decay_rate = 0.1; // 10% decay per day
for relationship in self.relationships.values_mut() {
if let Some(last_interaction) = relationship.last_interaction {
let days_since_interaction = (now - last_interaction).num_days() as f64;
if days_since_interaction > 0.0 {
let decay_factor = (1.0_f64 - decay_rate).powf(days_since_interaction);
relationship.score *= decay_factor;
// Update status after decay
if relationship.score < relationship.threshold {
relationship.transmission_enabled = false;
}
}
}
}
// Update statuses for all relationships
let user_ids: Vec<String> = self.relationships.keys().cloned().collect();
for user_id in user_ids {
self.update_relationship_status(&user_id);
}
self.save_relationships()?;
Ok(())
}
pub fn get_relationship(&self, user_id: &str) -> Option<&Relationship> {
self.relationships.get(user_id)
}
pub fn list_all_relationships(&self) -> &HashMap<String, Relationship> {
&self.relationships
}
pub fn get_transmission_eligible(&self) -> HashMap<String, &Relationship> {
self.relationships
.iter()
.filter(|(_, rel)| rel.transmission_enabled && !rel.is_broken)
.map(|(id, rel)| (id.clone(), rel))
.collect()
}
pub fn record_transmission(&mut self, user_id: &str) -> Result<()> {
if let Some(relationship) = self.relationships.get_mut(user_id) {
relationship.last_transmission = Some(Utc::now());
self.save_relationships()?;
}
Ok(())
}
pub fn get_relationship_stats(&self) -> RelationshipStats {
let total_relationships = self.relationships.len();
let active_relationships = self.relationships
.values()
.filter(|r| r.total_interactions > 0)
.count();
let transmission_enabled = self.relationships
.values()
.filter(|r| r.transmission_enabled)
.count();
let broken_relationships = self.relationships
.values()
.filter(|r| r.is_broken)
.count();
let avg_score = if total_relationships > 0 {
self.relationships.values().map(|r| r.score).sum::<f64>() / total_relationships as f64
} else {
0.0
};
RelationshipStats {
total_relationships,
active_relationships,
transmission_enabled,
broken_relationships,
avg_score,
}
}
fn load_relationships(config: &Config) -> Result<HashMap<String, Relationship>> {
let file_path = config.relationships_file();
if !file_path.exists() {
return Ok(HashMap::new());
}
let content = std::fs::read_to_string(file_path)
.context("Failed to read relationships file")?;
let relationships: HashMap<String, Relationship> = serde_json::from_str(&content)
.context("Failed to parse relationships file")?;
Ok(relationships)
}
fn save_relationships(&self) -> Result<()> {
let content = serde_json::to_string_pretty(&self.relationships)
.context("Failed to serialize relationships")?;
std::fs::write(&self.config.relationships_file(), content)
.context("Failed to write relationships file")?;
Ok(())
}
pub fn get_all_relationships(&self) -> Result<HashMap<String, RelationshipCompact>> {
let mut result = HashMap::new();
for (user_id, relationship) in &self.relationships {
result.insert(user_id.clone(), RelationshipCompact {
score: relationship.score,
trust_level: relationship.score / 10.0, // Simplified trust calculation
interaction_count: relationship.total_interactions,
last_interaction: relationship.last_interaction.unwrap_or(relationship.created_at),
status: relationship.status.clone(),
});
}
Ok(result)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct RelationshipStats {
pub total_relationships: usize,
pub active_relationships: usize,
pub transmission_enabled: usize,
pub broken_relationships: usize,
pub avg_score: f64,
}
#[derive(Debug, Clone, Serialize)]
pub struct RelationshipCompact {
pub score: f64,
pub trust_level: f64,
pub interaction_count: u32,
pub last_interaction: DateTime<Utc>,
pub status: RelationshipStatus,
}

458
src/scheduler.rs Normal file
View File

@@ -0,0 +1,458 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use anyhow::{Result, Context};
use chrono::{DateTime, Utc, Duration};
use crate::config::Config;
use crate::persona::Persona;
use crate::transmission::TransmissionController;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduledTask {
pub id: String,
pub task_type: TaskType,
pub next_run: DateTime<Utc>,
pub interval_hours: Option<i64>,
pub enabled: bool,
pub last_run: Option<DateTime<Utc>>,
pub run_count: u32,
pub max_runs: Option<u32>,
pub created_at: DateTime<Utc>,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TaskType {
DailyMaintenance,
AutoTransmission,
RelationshipDecay,
BreakthroughCheck,
MaintenanceTransmission,
Custom(String),
}
impl std::fmt::Display for TaskType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TaskType::DailyMaintenance => write!(f, "daily_maintenance"),
TaskType::AutoTransmission => write!(f, "auto_transmission"),
TaskType::RelationshipDecay => write!(f, "relationship_decay"),
TaskType::BreakthroughCheck => write!(f, "breakthrough_check"),
TaskType::MaintenanceTransmission => write!(f, "maintenance_transmission"),
TaskType::Custom(name) => write!(f, "custom_{}", name),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskExecution {
pub task_id: String,
pub execution_time: DateTime<Utc>,
pub duration_ms: u64,
pub success: bool,
pub result: Option<String>,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AIScheduler {
config: Config,
tasks: HashMap<String, ScheduledTask>,
execution_history: Vec<TaskExecution>,
last_check: Option<DateTime<Utc>>,
}
impl AIScheduler {
pub fn new(config: &Config) -> Result<Self> {
let (tasks, execution_history) = Self::load_scheduler_data(config)?;
let mut scheduler = AIScheduler {
config: config.clone(),
tasks,
execution_history,
last_check: None,
};
// Initialize default tasks if none exist
if scheduler.tasks.is_empty() {
scheduler.create_default_tasks()?;
}
Ok(scheduler)
}
pub async fn run_scheduled_tasks(&mut self, persona: &mut Persona, transmission_controller: &mut TransmissionController) -> Result<Vec<TaskExecution>> {
let now = Utc::now();
let mut executions = Vec::new();
// Find tasks that are due to run
let due_task_ids: Vec<String> = self.tasks
.iter()
.filter(|(_, task)| task.enabled && task.next_run <= now)
.filter(|(_, task)| {
// Check if task hasn't exceeded max runs
if let Some(max_runs) = task.max_runs {
task.run_count < max_runs
} else {
true
}
})
.map(|(id, _)| id.clone())
.collect();
for task_id in due_task_ids {
let execution = self.execute_task(&task_id, persona, transmission_controller).await?;
executions.push(execution);
}
self.last_check = Some(now);
self.save_scheduler_data()?;
Ok(executions)
}
async fn execute_task(&mut self, task_id: &str, persona: &mut Persona, transmission_controller: &mut TransmissionController) -> Result<TaskExecution> {
let start_time = Utc::now();
let mut execution = TaskExecution {
task_id: task_id.to_string(),
execution_time: start_time,
duration_ms: 0,
success: false,
result: None,
error: None,
};
// Get task type without borrowing mutably
let task_type = {
let task = self.tasks.get(task_id)
.ok_or_else(|| anyhow::anyhow!("Task not found: {}", task_id))?;
task.task_type.clone()
};
// Execute the task based on its type
let result = match &task_type {
TaskType::DailyMaintenance => self.execute_daily_maintenance(persona, transmission_controller).await,
TaskType::AutoTransmission => self.execute_auto_transmission(persona, transmission_controller).await,
TaskType::RelationshipDecay => self.execute_relationship_decay(persona).await,
TaskType::BreakthroughCheck => self.execute_breakthrough_check(persona, transmission_controller).await,
TaskType::MaintenanceTransmission => self.execute_maintenance_transmission(persona, transmission_controller).await,
TaskType::Custom(name) => self.execute_custom_task(name, persona, transmission_controller).await,
};
let end_time = Utc::now();
execution.duration_ms = (end_time - start_time).num_milliseconds() as u64;
// Now update the task state with mutable borrow
match result {
Ok(message) => {
execution.success = true;
execution.result = Some(message);
// Update task state
if let Some(task) = self.tasks.get_mut(task_id) {
task.last_run = Some(start_time);
task.run_count += 1;
// Schedule next run if recurring
if let Some(interval_hours) = task.interval_hours {
task.next_run = start_time + Duration::hours(interval_hours);
} else {
// One-time task, disable it
task.enabled = false;
}
}
}
Err(e) => {
execution.error = Some(e.to_string());
// For failed tasks, retry in a shorter interval
if let Some(task) = self.tasks.get_mut(task_id) {
if task.interval_hours.is_some() {
task.next_run = start_time + Duration::minutes(15); // Retry in 15 minutes
}
}
}
}
self.execution_history.push(execution.clone());
// Keep only recent execution history (last 1000 executions)
if self.execution_history.len() > 1000 {
self.execution_history.drain(..self.execution_history.len() - 1000);
}
Ok(execution)
}
async fn execute_daily_maintenance(&self, persona: &mut Persona, transmission_controller: &mut TransmissionController) -> Result<String> {
// Run daily maintenance
persona.daily_maintenance()?;
// Check for maintenance transmissions
let transmissions = transmission_controller.check_maintenance_transmissions(persona).await?;
Ok(format!("Daily maintenance completed. {} maintenance transmissions sent.", transmissions.len()))
}
async fn execute_auto_transmission(&self, _persona: &mut Persona, transmission_controller: &mut TransmissionController) -> Result<String> {
let transmissions = transmission_controller.check_autonomous_transmissions(_persona).await?;
Ok(format!("Autonomous transmission check completed. {} transmissions sent.", transmissions.len()))
}
async fn execute_relationship_decay(&self, persona: &mut Persona) -> Result<String> {
persona.daily_maintenance()?;
Ok("Relationship time decay applied.".to_string())
}
async fn execute_breakthrough_check(&self, persona: &mut Persona, transmission_controller: &mut TransmissionController) -> Result<String> {
let transmissions = transmission_controller.check_breakthrough_transmissions(persona).await?;
Ok(format!("Breakthrough check completed. {} transmissions sent.", transmissions.len()))
}
async fn execute_maintenance_transmission(&self, persona: &mut Persona, transmission_controller: &mut TransmissionController) -> Result<String> {
let transmissions = transmission_controller.check_maintenance_transmissions(persona).await?;
Ok(format!("Maintenance transmission check completed. {} transmissions sent.", transmissions.len()))
}
async fn execute_custom_task(&self, _name: &str, _persona: &mut Persona, _transmission_controller: &mut TransmissionController) -> Result<String> {
// Placeholder for custom task execution
Ok("Custom task executed.".to_string())
}
pub fn create_task(&mut self, task_type: TaskType, next_run: DateTime<Utc>, interval_hours: Option<i64>) -> Result<String> {
let task_id = uuid::Uuid::new_v4().to_string();
let now = Utc::now();
let task = ScheduledTask {
id: task_id.clone(),
task_type,
next_run,
interval_hours,
enabled: true,
last_run: None,
run_count: 0,
max_runs: None,
created_at: now,
metadata: HashMap::new(),
};
self.tasks.insert(task_id.clone(), task);
self.save_scheduler_data()?;
Ok(task_id)
}
pub fn enable_task(&mut self, task_id: &str) -> Result<()> {
if let Some(task) = self.tasks.get_mut(task_id) {
task.enabled = true;
self.save_scheduler_data()?;
}
Ok(())
}
pub fn disable_task(&mut self, task_id: &str) -> Result<()> {
if let Some(task) = self.tasks.get_mut(task_id) {
task.enabled = false;
self.save_scheduler_data()?;
}
Ok(())
}
pub fn delete_task(&mut self, task_id: &str) -> Result<()> {
self.tasks.remove(task_id);
self.save_scheduler_data()?;
Ok(())
}
pub fn get_task(&self, task_id: &str) -> Option<&ScheduledTask> {
self.tasks.get(task_id)
}
pub fn get_tasks(&self) -> &HashMap<String, ScheduledTask> {
&self.tasks
}
pub fn get_due_tasks(&self) -> Vec<&ScheduledTask> {
let now = Utc::now();
self.tasks
.values()
.filter(|task| task.enabled && task.next_run <= now)
.collect()
}
pub fn get_execution_history(&self, limit: Option<usize>) -> Vec<&TaskExecution> {
let mut executions: Vec<_> = self.execution_history.iter().collect();
executions.sort_by(|a, b| b.execution_time.cmp(&a.execution_time));
match limit {
Some(limit) => executions.into_iter().take(limit).collect(),
None => executions,
}
}
pub fn get_scheduler_stats(&self) -> SchedulerStats {
let total_tasks = self.tasks.len();
let enabled_tasks = self.tasks.values().filter(|task| task.enabled).count();
let due_tasks = self.get_due_tasks().len();
let total_executions = self.execution_history.len();
let successful_executions = self.execution_history.iter()
.filter(|exec| exec.success)
.count();
let today = Utc::now().date_naive();
let today_executions = self.execution_history.iter()
.filter(|exec| exec.execution_time.date_naive() == today)
.count();
let avg_duration = if total_executions > 0 {
self.execution_history.iter()
.map(|exec| exec.duration_ms)
.sum::<u64>() as f64 / total_executions as f64
} else {
0.0
};
SchedulerStats {
total_tasks,
enabled_tasks,
due_tasks,
total_executions,
successful_executions,
today_executions,
success_rate: if total_executions > 0 {
successful_executions as f64 / total_executions as f64
} else {
0.0
},
avg_duration_ms: avg_duration,
}
}
fn create_default_tasks(&mut self) -> Result<()> {
let now = Utc::now();
// Daily maintenance task - run every day at 3 AM
let mut daily_maintenance_time = now.date_naive().and_hms_opt(3, 0, 0).unwrap().and_utc();
if daily_maintenance_time <= now {
daily_maintenance_time = daily_maintenance_time + Duration::days(1);
}
self.create_task(
TaskType::DailyMaintenance,
daily_maintenance_time,
Some(24), // 24 hours = 1 day
)?;
// Auto transmission check - every 4 hours
self.create_task(
TaskType::AutoTransmission,
now + Duration::hours(1),
Some(4),
)?;
// Breakthrough check - every 2 hours
self.create_task(
TaskType::BreakthroughCheck,
now + Duration::minutes(30),
Some(2),
)?;
// Maintenance transmission - once per day
let mut maintenance_time = now.date_naive().and_hms_opt(12, 0, 0).unwrap().and_utc();
if maintenance_time <= now {
maintenance_time = maintenance_time + Duration::days(1);
}
self.create_task(
TaskType::MaintenanceTransmission,
maintenance_time,
Some(24), // 24 hours = 1 day
)?;
Ok(())
}
fn load_scheduler_data(config: &Config) -> Result<(HashMap<String, ScheduledTask>, Vec<TaskExecution>)> {
let tasks_file = config.scheduler_tasks_file();
let history_file = config.scheduler_history_file();
let tasks = if tasks_file.exists() {
let content = std::fs::read_to_string(tasks_file)
.context("Failed to read scheduler tasks file")?;
serde_json::from_str(&content)
.context("Failed to parse scheduler tasks file")?
} else {
HashMap::new()
};
let history = if history_file.exists() {
let content = std::fs::read_to_string(history_file)
.context("Failed to read scheduler history file")?;
serde_json::from_str(&content)
.context("Failed to parse scheduler history file")?
} else {
Vec::new()
};
Ok((tasks, history))
}
fn save_scheduler_data(&self) -> Result<()> {
// Save tasks
let tasks_content = serde_json::to_string_pretty(&self.tasks)
.context("Failed to serialize scheduler tasks")?;
std::fs::write(&self.config.scheduler_tasks_file(), tasks_content)
.context("Failed to write scheduler tasks file")?;
// Save execution history
let history_content = serde_json::to_string_pretty(&self.execution_history)
.context("Failed to serialize scheduler history")?;
std::fs::write(&self.config.scheduler_history_file(), history_content)
.context("Failed to write scheduler history file")?;
Ok(())
}
}
// Type alias for compatibility with CLI interface
pub type Scheduler = AIScheduler;
impl Scheduler {
pub fn list_tasks(&self) -> Result<Vec<ScheduledTaskInfo>> {
let tasks: Vec<ScheduledTaskInfo> = self.tasks
.values()
.map(|task| ScheduledTaskInfo {
name: task.task_type.to_string(),
schedule: match task.interval_hours {
Some(hours) => format!("Every {} hours", hours),
None => "One-time".to_string(),
},
next_run: task.next_run,
enabled: task.enabled,
})
.collect();
Ok(tasks)
}
}
#[derive(Debug, Clone)]
pub struct SchedulerStats {
pub total_tasks: usize,
pub enabled_tasks: usize,
pub due_tasks: usize,
pub total_executions: usize,
pub successful_executions: usize,
pub today_executions: usize,
pub success_rate: f64,
pub avg_duration_ms: f64,
}
#[derive(Debug, Clone)]
pub struct ScheduledTaskInfo {
pub name: String,
pub schedule: String,
pub next_run: DateTime<Utc>,
pub enabled: bool,
}

608
src/shell.rs Normal file
View File

@@ -0,0 +1,608 @@
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::io::{self, Write};
use anyhow::{Result, Context};
use colored::*;
use rustyline::error::ReadlineError;
use rustyline::Editor;
use rustyline::completion::{Completer, FilenameCompleter, Pair};
use rustyline::history::{History, DefaultHistory};
use rustyline::highlight::Highlighter;
use rustyline::hint::Hinter;
use rustyline::validate::Validator;
use rustyline::Helper;
use crate::config::Config;
use crate::persona::Persona;
use crate::ai_provider::{AIProviderClient, AIProvider, AIConfig};
pub async fn handle_shell(
user_id: String,
data_dir: Option<PathBuf>,
model: Option<String>,
provider: Option<String>,
) -> Result<()> {
let config = Config::new(data_dir)?;
let mut shell = ShellMode::new(config, user_id)?
.with_ai_provider(provider, model);
shell.run().await
}
pub struct ShellMode {
config: Config,
persona: Persona,
ai_provider: Option<AIProviderClient>,
user_id: String,
editor: Editor<ShellCompleter, DefaultHistory>,
}
struct ShellCompleter {
completer: FilenameCompleter,
}
impl ShellCompleter {
fn new() -> Self {
ShellCompleter {
completer: FilenameCompleter::new(),
}
}
}
impl Helper for ShellCompleter {}
impl Hinter for ShellCompleter {
type Hint = String;
fn hint(&self, _line: &str, _pos: usize, _ctx: &rustyline::Context<'_>) -> Option<String> {
None
}
}
impl Highlighter for ShellCompleter {}
impl Validator for ShellCompleter {}
impl Completer for ShellCompleter {
type Candidate = Pair;
fn complete(
&self,
line: &str,
pos: usize,
ctx: &rustyline::Context<'_>,
) -> rustyline::Result<(usize, Vec<Pair>)> {
// Custom completion for slash commands
if line.starts_with('/') {
let commands = vec![
"/status", "/relationships", "/memories", "/analyze",
"/fortune", "/clear", "/history", "/help", "/exit"
];
let word_start = line.rfind(' ').map_or(0, |i| i + 1);
let word = &line[word_start..pos];
let matches: Vec<Pair> = commands.iter()
.filter(|cmd| cmd.starts_with(word))
.map(|cmd| Pair {
display: cmd.to_string(),
replacement: cmd.to_string(),
})
.collect();
return Ok((word_start, matches));
}
// Custom completion for shell commands starting with !
if line.starts_with('!') {
let shell_commands = vec![
"ls", "pwd", "cd", "cat", "grep", "find", "ps", "top",
"echo", "mkdir", "rmdir", "cp", "mv", "rm", "touch",
"git", "cargo", "npm", "python", "node"
];
let word_start = line.rfind(' ').map_or(1, |i| i + 1); // Skip the '!'
let word = &line[word_start..pos];
let matches: Vec<Pair> = shell_commands.iter()
.filter(|cmd| cmd.starts_with(word))
.map(|cmd| Pair {
display: cmd.to_string(),
replacement: cmd.to_string(),
})
.collect();
return Ok((word_start, matches));
}
// Fallback to filename completion
self.completer.complete(line, pos, ctx)
}
}
impl ShellMode {
pub fn new(config: Config, user_id: String) -> Result<Self> {
let persona = Persona::new(&config)?;
// Setup rustyline editor with completer
let completer = ShellCompleter::new();
let mut editor = Editor::with_config(
rustyline::Config::builder()
.tab_stop(4)
.build()
)?;
editor.set_helper(Some(completer));
// Load history if exists
let history_file = config.data_dir.join("shell_history.txt");
if history_file.exists() {
let _ = editor.load_history(&history_file);
}
Ok(ShellMode {
config,
persona,
ai_provider: None,
user_id,
editor,
})
}
pub fn with_ai_provider(mut self, provider: Option<String>, model: Option<String>) -> Self {
// Use provided parameters or fall back to config defaults
let provider_name = provider
.or_else(|| Some(self.config.default_provider.clone()))
.unwrap_or_else(|| "ollama".to_string());
let model_name = model.or_else(|| {
// Try to get default model from config for the chosen provider
self.config.providers.get(&provider_name)
.map(|p| p.default_model.clone())
}).unwrap_or_else(|| {
// Final fallback based on provider
match provider_name.as_str() {
"openai" => "gpt-4o-mini".to_string(),
"ollama" => "qwen2.5-coder:latest".to_string(),
_ => "qwen2.5-coder:latest".to_string(),
}
});
let ai_provider = match provider_name.as_str() {
"ollama" => AIProvider::Ollama,
"openai" => AIProvider::OpenAI,
"claude" => AIProvider::Claude,
_ => AIProvider::Ollama, // Default fallback
};
let ai_config = AIConfig {
provider: ai_provider,
model: model_name,
api_key: None, // Will be loaded from environment if needed
base_url: None,
max_tokens: Some(2000),
temperature: Some(0.7),
};
let client = AIProviderClient::new(ai_config);
self.ai_provider = Some(client);
self
}
pub async fn run(&mut self) -> Result<()> {
println!("{}", "🚀 Starting ai.gpt Interactive Shell".cyan().bold());
// Show AI provider info
if let Some(ai_provider) = &self.ai_provider {
println!("{}: {} ({})",
"AI Provider".green().bold(),
ai_provider.get_provider().to_string(),
ai_provider.get_model());
} else {
println!("{}: {}", "AI Provider".yellow().bold(), "Simple mode (no AI)");
}
println!("{}", "Type 'help' for commands, 'exit' to quit".dimmed());
println!("{}", "Use Tab for command completion, Ctrl+C to interrupt, Ctrl+D to exit".dimmed());
loop {
// Read user input with rustyline (supports completion, history, etc.)
let readline = self.editor.readline("ai.shell> ");
match readline {
Ok(line) => {
let input = line.trim();
// Skip empty input
if input.is_empty() {
continue;
}
// Add to history
self.editor.add_history_entry(input)
.context("Failed to add to history")?;
// Handle input
if let Err(e) = self.handle_input(input).await {
println!("{}: {}", "Error".red().bold(), e);
}
}
Err(ReadlineError::Interrupted) => {
// Ctrl+C
println!("{}", "Use 'exit' or Ctrl+D to quit".yellow());
continue;
}
Err(ReadlineError::Eof) => {
// Ctrl+D
println!("\n{}", "Goodbye!".cyan());
break;
}
Err(err) => {
println!("{}: {}", "Input error".red().bold(), err);
break;
}
}
}
// Save history before exit
self.save_history()?;
Ok(())
}
async fn handle_input(&mut self, input: &str) -> Result<()> {
match input {
// Exit commands
"exit" | "quit" | "/exit" | "/quit" => {
println!("{}", "Goodbye!".cyan());
std::process::exit(0);
}
// Help command
"help" | "/help" => {
self.show_help();
}
// Shell commands (starting with !)
input if input.starts_with('!') => {
self.execute_shell_command(&input[1..]).await?;
}
// Slash commands (starting with /)
input if input.starts_with('/') => {
self.execute_slash_command(input).await?;
}
// AI conversation
_ => {
self.handle_ai_conversation(input).await?;
}
}
Ok(())
}
fn show_help(&self) {
println!("\n{}", "ai.gpt Interactive Shell Commands".cyan().bold());
println!();
println!("{}", "Navigation & Input:".yellow().bold());
println!(" {} - Tab completion for commands and files", "Tab".green());
println!(" {} - Command history (previous/next)", "↑/↓ or Ctrl+P/N".green());
println!(" {} - Interrupt current input", "Ctrl+C".green());
println!(" {} - Exit shell", "Ctrl+D".green());
println!();
println!("{}", "Basic Commands:".yellow().bold());
println!(" {} - Show this help", "help".green());
println!(" {} - Exit the shell", "exit, quit".green());
println!(" {} - Clear screen", "/clear".green());
println!(" {} - Show command history", "/history".green());
println!();
println!("{}", "Shell Commands:".yellow().bold());
println!(" {} - Execute shell command (Tab completion)", "!<command>".green());
println!(" {} - List files", "!ls".green());
println!(" {} - Show current directory", "!pwd".green());
println!(" {} - Git status", "!git status".green());
println!(" {} - Cargo build", "!cargo build".green());
println!();
println!("{}", "AI Commands:".yellow().bold());
println!(" {} - Show AI status and relationship", "/status".green());
println!(" {} - List all relationships", "/relationships".green());
println!(" {} - Show recent memories", "/memories".green());
println!(" {} - Analyze current directory", "/analyze".green());
println!(" {} - Show today's fortune", "/fortune".green());
println!();
println!("{}", "Conversation:".yellow().bold());
println!(" {} - Chat with AI using configured provider", "Any other input".green());
println!(" {} - AI responses track relationship changes", "Relationship tracking".dimmed());
println!();
}
async fn execute_shell_command(&self, command: &str) -> Result<()> {
println!("{} {}", "Executing:".blue().bold(), command.yellow());
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", command])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.context("Failed to execute command")?
} else {
Command::new("sh")
.args(["-c", command])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.context("Failed to execute command")?
};
// Print stdout
if !output.stdout.is_empty() {
let stdout = String::from_utf8_lossy(&output.stdout);
println!("{}", stdout);
}
// Print stderr in red
if !output.stderr.is_empty() {
let stderr = String::from_utf8_lossy(&output.stderr);
println!("{}", stderr.red());
}
// Show exit code if not successful
if !output.status.success() {
if let Some(code) = output.status.code() {
println!("{}: {}", "Exit code".red().bold(), code);
}
}
Ok(())
}
async fn execute_slash_command(&mut self, command: &str) -> Result<()> {
match command {
"/status" => {
self.show_ai_status().await?;
}
"/relationships" => {
self.show_relationships().await?;
}
"/memories" => {
self.show_memories().await?;
}
"/analyze" => {
self.analyze_directory().await?;
}
"/fortune" => {
self.show_fortune().await?;
}
"/clear" => {
// Clear screen
print!("\x1B[2J\x1B[1;1H");
io::stdout().flush()?;
}
"/history" => {
self.show_history();
}
_ => {
println!("{}: {}", "Unknown command".red().bold(), command);
println!("Type '{}' for available commands", "help".green());
}
}
Ok(())
}
async fn handle_ai_conversation(&mut self, input: &str) -> Result<()> {
let (response, relationship_delta) = if let Some(ai_provider) = &self.ai_provider {
// Use AI provider for response
self.persona.process_ai_interaction(&self.user_id, input,
Some(ai_provider.get_provider().to_string()),
Some(ai_provider.get_model().to_string())).await?
} else {
// Use simple response
self.persona.process_interaction(&self.user_id, input)?
};
// Display conversation
println!("{}: {}", "You".cyan().bold(), input);
println!("{}: {}", "AI".green().bold(), response);
// Show relationship change if significant
if relationship_delta.abs() >= 0.1 {
if relationship_delta > 0.0 {
println!("{}", format!("(+{:.2} relationship)", relationship_delta).green());
} else {
println!("{}", format!("({:.2} relationship)", relationship_delta).red());
}
}
println!(); // Add spacing
Ok(())
}
async fn show_ai_status(&self) -> Result<()> {
let state = self.persona.get_current_state()?;
println!("\n{}", "AI Status".cyan().bold());
println!("Mood: {}", state.current_mood.yellow());
println!("Fortune: {}/10", state.fortune_value.to_string().yellow());
if let Some(relationship) = self.persona.get_relationship(&self.user_id) {
println!("\n{}", "Your Relationship".cyan().bold());
println!("Status: {}", relationship.status.to_string().yellow());
println!("Score: {:.2} / {}", relationship.score, relationship.threshold);
println!("Interactions: {}", relationship.total_interactions);
}
println!();
Ok(())
}
async fn show_relationships(&self) -> Result<()> {
let relationships = self.persona.list_all_relationships();
if relationships.is_empty() {
println!("{}", "No relationships yet".yellow());
return Ok(());
}
println!("\n{}", "All Relationships".cyan().bold());
println!();
for (user_id, rel) in relationships {
let transmission = if rel.is_broken {
"💔"
} else if rel.transmission_enabled {
""
} else {
""
};
let user_display = if user_id.len() > 20 {
format!("{}...", &user_id[..20])
} else {
user_id
};
println!("{:<25} {:<12} {:<8} {}",
user_display.cyan(),
rel.status.to_string(),
format!("{:.2}", rel.score),
transmission);
}
println!();
Ok(())
}
async fn show_memories(&mut self) -> Result<()> {
let memories = self.persona.get_memories(&self.user_id, 10);
if memories.is_empty() {
println!("{}", "No memories yet".yellow());
return Ok(());
}
println!("\n{}", "Recent Memories".cyan().bold());
println!();
for (i, memory) in memories.iter().enumerate() {
println!("{}: {}",
format!("Memory {}", i + 1).dimmed(),
memory);
println!();
}
Ok(())
}
async fn analyze_directory(&self) -> Result<()> {
println!("{}", "Analyzing current directory...".blue().bold());
// Get current directory
let current_dir = std::env::current_dir()
.context("Failed to get current directory")?;
println!("Directory: {}", current_dir.display().to_string().yellow());
// List files and directories
let entries = std::fs::read_dir(&current_dir)
.context("Failed to read directory")?;
let mut files = Vec::new();
let mut dirs = Vec::new();
for entry in entries {
let entry = entry.context("Failed to read directory entry")?;
let path = entry.path();
let name = path.file_name()
.and_then(|n| n.to_str())
.unwrap_or("Unknown");
if path.is_dir() {
dirs.push(name.to_string());
} else {
files.push(name.to_string());
}
}
if !dirs.is_empty() {
println!("\n{}: {}", "Directories".blue().bold(), dirs.join(", "));
}
if !files.is_empty() {
println!("{}: {}", "Files".blue().bold(), files.join(", "));
}
// Check for common project files
let project_files = ["Cargo.toml", "package.json", "requirements.txt", "Makefile", "README.md"];
let found_files: Vec<_> = project_files.iter()
.filter(|&&file| files.contains(&file.to_string()))
.collect();
if !found_files.is_empty() {
println!("\n{}: {}", "Project files detected".green().bold(),
found_files.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(", "));
}
println!();
Ok(())
}
async fn show_fortune(&self) -> Result<()> {
let state = self.persona.get_current_state()?;
let fortune_stars = "🌟".repeat(state.fortune_value as usize);
let empty_stars = "".repeat((10 - state.fortune_value) as usize);
println!("\n{}", "AI Fortune".yellow().bold());
println!("{}{}", fortune_stars, empty_stars);
println!("Today's Fortune: {}/10", state.fortune_value);
if state.breakthrough_triggered {
println!("{}", "⚡ BREAKTHROUGH! Special fortune activated!".yellow());
}
println!();
Ok(())
}
fn show_history(&self) {
println!("\n{}", "Command History".cyan().bold());
let history = self.editor.history();
if history.is_empty() {
println!("{}", "No commands in history".yellow());
return;
}
// Show last 20 commands
let start = if history.len() > 20 { history.len() - 20 } else { 0 };
for (i, entry) in history.iter().enumerate().skip(start) {
println!("{:2}: {}", i + 1, entry);
}
println!();
}
fn save_history(&mut self) -> Result<()> {
let history_file = self.config.data_dir.join("shell_history.txt");
self.editor.save_history(&history_file)
.context("Failed to save shell history")?;
Ok(())
}
}
// Extend AIProvider to have Display and helper methods
impl AIProvider {
fn to_string(&self) -> String {
match self {
AIProvider::OpenAI => "openai".to_string(),
AIProvider::Ollama => "ollama".to_string(),
AIProvider::Claude => "claude".to_string(),
}
}
}

51
src/status.rs Normal file
View File

@@ -0,0 +1,51 @@
use std::path::PathBuf;
use anyhow::Result;
use colored::*;
use crate::config::Config;
use crate::persona::Persona;
pub async fn handle_status(user_id: Option<String>, data_dir: Option<PathBuf>) -> Result<()> {
// Load configuration
let config = Config::new(data_dir)?;
// Initialize persona
let persona = Persona::new(&config)?;
// Get current state
let state = persona.get_current_state()?;
// Display AI status
println!("{}", "ai.gpt Status".cyan().bold());
println!("Mood: {}", state.current_mood);
println!("Fortune: {}/10", state.fortune_value);
if state.breakthrough_triggered {
println!("{}", "⚡ Breakthrough triggered!".yellow());
}
// Show personality traits
println!("\n{}", "Current Personality".cyan().bold());
for (trait_name, value) in &state.base_personality {
println!("{}: {:.2}", trait_name.cyan(), value);
}
// Show specific relationship if requested
if let Some(user_id) = user_id {
if let Some(relationship) = persona.get_relationship(&user_id) {
println!("\n{}: {}", "Relationship with".cyan(), user_id);
println!("Status: {}", relationship.status);
println!("Score: {:.2}", relationship.score);
println!("Total Interactions: {}", relationship.total_interactions);
println!("Transmission Enabled: {}", relationship.transmission_enabled);
if relationship.is_broken {
println!("{}", "⚠️ This relationship is broken and cannot be repaired.".red());
}
} else {
println!("\n{}: {}", "No relationship found with".yellow(), user_id);
}
}
Ok(())
}

480
src/submodules.rs Normal file
View File

@@ -0,0 +1,480 @@
use std::collections::HashMap;
use std::path::PathBuf;
use anyhow::{Result, Context};
use colored::*;
use serde::{Deserialize, Serialize};
use crate::config::Config;
pub async fn handle_submodules(
action: String,
module: Option<String>,
all: bool,
dry_run: bool,
auto_commit: bool,
verbose: bool,
data_dir: Option<PathBuf>,
) -> Result<()> {
let config = Config::new(data_dir)?;
let mut submodule_manager = SubmoduleManager::new(config);
match action.as_str() {
"list" => {
submodule_manager.list_submodules(verbose).await?;
}
"update" => {
submodule_manager.update_submodules(module, all, dry_run, auto_commit, verbose).await?;
}
"status" => {
submodule_manager.show_submodule_status().await?;
}
_ => {
return Err(anyhow::anyhow!("Unknown submodule action: {}", action));
}
}
Ok(())
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubmoduleInfo {
pub name: String,
pub path: String,
pub branch: String,
pub current_commit: Option<String>,
pub target_commit: Option<String>,
pub status: String,
}
impl Default for SubmoduleInfo {
fn default() -> Self {
SubmoduleInfo {
name: String::new(),
path: String::new(),
branch: "main".to_string(),
current_commit: None,
target_commit: None,
status: "unknown".to_string(),
}
}
}
#[allow(dead_code)]
pub struct SubmoduleManager {
config: Config,
ai_root: PathBuf,
submodules: HashMap<String, SubmoduleInfo>,
}
impl SubmoduleManager {
pub fn new(config: Config) -> Self {
let ai_root = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("ai")
.join("ai");
SubmoduleManager {
config,
ai_root,
submodules: HashMap::new(),
}
}
pub async fn list_submodules(&mut self, verbose: bool) -> Result<()> {
println!("{}", "📋 Submodules Status".cyan().bold());
println!();
let submodules = self.parse_gitmodules()?;
if submodules.is_empty() {
println!("{}", "No submodules found".yellow());
return Ok(());
}
// Display submodules in a table format
println!("{:<15} {:<25} {:<15} {}",
"Module".cyan().bold(),
"Path".cyan().bold(),
"Branch".cyan().bold(),
"Status".cyan().bold());
println!("{}", "-".repeat(80));
for (module_name, module_info) in &submodules {
let status_color = match module_info.status.as_str() {
"clean" => module_info.status.green(),
"modified" => module_info.status.yellow(),
"missing" => module_info.status.red(),
"conflicts" => module_info.status.red(),
_ => module_info.status.normal(),
};
println!("{:<15} {:<25} {:<15} {}",
module_name.blue(),
module_info.path,
module_info.branch.green(),
status_color);
}
println!();
if verbose {
println!("Total submodules: {}", submodules.len().to_string().cyan());
println!("Repository root: {}", self.ai_root.display().to_string().blue());
}
Ok(())
}
pub async fn update_submodules(
&mut self,
module: Option<String>,
all: bool,
dry_run: bool,
auto_commit: bool,
verbose: bool
) -> Result<()> {
if !module.is_some() && !all {
return Err(anyhow::anyhow!("Either --module or --all is required"));
}
if module.is_some() && all {
return Err(anyhow::anyhow!("Cannot use both --module and --all"));
}
let submodules = self.parse_gitmodules()?;
if submodules.is_empty() {
println!("{}", "No submodules found".yellow());
return Ok(());
}
// Determine which modules to update
let modules_to_update: Vec<String> = if all {
submodules.keys().cloned().collect()
} else if let Some(module_name) = module {
if !submodules.contains_key(&module_name) {
return Err(anyhow::anyhow!(
"Submodule '{}' not found. Available modules: {}",
module_name,
submodules.keys().cloned().collect::<Vec<_>>().join(", ")
));
}
vec![module_name]
} else {
vec![]
};
if dry_run {
println!("{}", "🔍 DRY RUN MODE - No changes will be made".yellow().bold());
}
println!("{}", format!("🔄 Updating {} submodule(s)...", modules_to_update.len()).cyan().bold());
let mut updated_modules = Vec::new();
for module_name in modules_to_update {
if let Some(module_info) = submodules.get(&module_name) {
println!("\n{}", format!("📦 Processing: {}", module_name).blue().bold());
let module_path = PathBuf::from(&module_info.path);
let full_path = self.ai_root.join(&module_path);
if !full_path.exists() {
println!("{}", format!("❌ Module directory not found: {}", module_info.path).red());
continue;
}
// Get current commit
let current_commit = self.get_current_commit(&full_path)?;
if dry_run {
println!("{}", format!("🔍 Would update {} to branch {}", module_name, module_info.branch).yellow());
if let Some(ref commit) = current_commit {
println!("{}", format!("Current: {}", commit).dimmed());
}
continue;
}
// Perform update
if let Err(e) = self.update_single_module(&module_name, &module_info, &full_path).await {
println!("{}", format!("❌ Failed to update {}: {}", module_name, e).red());
continue;
}
// Get new commit
let new_commit = self.get_current_commit(&full_path)?;
if current_commit != new_commit {
println!("{}", format!("✅ Updated {} ({:?}{:?})",
module_name,
current_commit.as_deref().unwrap_or("unknown"),
new_commit.as_deref().unwrap_or("unknown")).green());
updated_modules.push((module_name.clone(), current_commit, new_commit));
} else {
println!("{}", "✅ Already up to date".green());
}
}
}
// Summary
if !updated_modules.is_empty() {
println!("\n{}", format!("🎉 Successfully updated {} module(s)", updated_modules.len()).green().bold());
if verbose {
for (module_name, old_commit, new_commit) in &updated_modules {
println!("{}: {:?}{:?}",
module_name,
old_commit.as_deref().unwrap_or("unknown"),
new_commit.as_deref().unwrap_or("unknown"));
}
}
if auto_commit && !dry_run {
self.auto_commit_changes(&updated_modules).await?;
} else if !dry_run {
println!("{}", "💾 Changes staged but not committed".yellow());
println!("Run with --auto-commit to commit automatically");
}
} else if !dry_run {
println!("{}", "No modules needed updating".yellow());
}
Ok(())
}
pub async fn show_submodule_status(&self) -> Result<()> {
println!("{}", "📊 Submodule Status Overview".cyan().bold());
println!();
let submodules = self.parse_gitmodules()?;
let mut total_modules = 0;
let mut clean_modules = 0;
let mut modified_modules = 0;
let mut missing_modules = 0;
for (module_name, module_info) in submodules {
let module_path = self.ai_root.join(&module_info.path);
if module_path.exists() {
total_modules += 1;
match module_info.status.as_str() {
"clean" => clean_modules += 1,
"modified" => modified_modules += 1,
_ => {}
}
} else {
missing_modules += 1;
}
println!("{}: {}",
module_name.blue(),
if module_path.exists() {
module_info.status.green()
} else {
"missing".red()
});
}
println!();
println!("Summary: {} total, {} clean, {} modified, {} missing",
total_modules.to_string().cyan(),
clean_modules.to_string().green(),
modified_modules.to_string().yellow(),
missing_modules.to_string().red());
Ok(())
}
fn parse_gitmodules(&self) -> Result<HashMap<String, SubmoduleInfo>> {
let gitmodules_path = self.ai_root.join(".gitmodules");
if !gitmodules_path.exists() {
return Ok(HashMap::new());
}
let content = std::fs::read_to_string(&gitmodules_path)
.with_context(|| format!("Failed to read .gitmodules file: {}", gitmodules_path.display()))?;
let mut submodules = HashMap::new();
let mut current_name: Option<String> = None;
let mut current_path: Option<String> = None;
for line in content.lines() {
let line = line.trim();
if line.starts_with("[submodule \"") && line.ends_with("\"]") {
// Save previous submodule if complete
if let (Some(name), Some(path)) = (current_name.take(), current_path.take()) {
let mut info = SubmoduleInfo::default();
info.name = name.clone();
info.path = path;
info.branch = self.get_target_branch(&name);
info.status = self.get_submodule_status(&name, &info.path)?;
submodules.insert(name, info);
}
// Extract new submodule name
current_name = Some(line[12..line.len()-2].to_string());
} else if line.starts_with("path = ") {
current_path = Some(line[7..].to_string());
}
}
// Save last submodule
if let (Some(name), Some(path)) = (current_name, current_path) {
let mut info = SubmoduleInfo::default();
info.name = name.clone();
info.path = path;
info.branch = self.get_target_branch(&name);
info.status = self.get_submodule_status(&name, &info.path)?;
submodules.insert(name, info);
}
Ok(submodules)
}
fn get_target_branch(&self, module_name: &str) -> String {
// Try to get from ai.json configuration
match module_name {
"verse" => "main".to_string(),
"card" => "main".to_string(),
"bot" => "main".to_string(),
_ => "main".to_string(),
}
}
fn get_submodule_status(&self, _module_name: &str, module_path: &str) -> Result<String> {
let full_path = self.ai_root.join(module_path);
if !full_path.exists() {
return Ok("missing".to_string());
}
// Check git status
let output = std::process::Command::new("git")
.args(&["submodule", "status", module_path])
.current_dir(&self.ai_root)
.output();
match output {
Ok(output) if output.status.success() => {
let stdout = String::from_utf8_lossy(&output.stdout);
if let Some(status_char) = stdout.chars().next() {
match status_char {
' ' => Ok("clean".to_string()),
'+' => Ok("modified".to_string()),
'-' => Ok("not_initialized".to_string()),
'U' => Ok("conflicts".to_string()),
_ => Ok("unknown".to_string()),
}
} else {
Ok("unknown".to_string())
}
}
_ => Ok("unknown".to_string())
}
}
fn get_current_commit(&self, module_path: &PathBuf) -> Result<Option<String>> {
let output = std::process::Command::new("git")
.args(&["rev-parse", "HEAD"])
.current_dir(module_path)
.output();
match output {
Ok(output) if output.status.success() => {
let commit = String::from_utf8_lossy(&output.stdout).trim().to_string();
if commit.len() >= 8 {
Ok(Some(commit[..8].to_string()))
} else {
Ok(Some(commit))
}
}
_ => Ok(None)
}
}
async fn update_single_module(
&self,
_module_name: &str,
module_info: &SubmoduleInfo,
module_path: &PathBuf
) -> Result<()> {
// Fetch latest changes
println!("{}", "Fetching latest changes...".dimmed());
let fetch_output = std::process::Command::new("git")
.args(&["fetch", "origin"])
.current_dir(module_path)
.output()?;
if !fetch_output.status.success() {
return Err(anyhow::anyhow!("Failed to fetch: {}",
String::from_utf8_lossy(&fetch_output.stderr)));
}
// Switch to target branch
println!("{}", format!("Switching to branch {}...", module_info.branch).dimmed());
let checkout_output = std::process::Command::new("git")
.args(&["checkout", &module_info.branch])
.current_dir(module_path)
.output()?;
if !checkout_output.status.success() {
return Err(anyhow::anyhow!("Failed to checkout {}: {}",
module_info.branch, String::from_utf8_lossy(&checkout_output.stderr)));
}
// Pull latest changes
let pull_output = std::process::Command::new("git")
.args(&["pull", "origin", &module_info.branch])
.current_dir(module_path)
.output()?;
if !pull_output.status.success() {
return Err(anyhow::anyhow!("Failed to pull: {}",
String::from_utf8_lossy(&pull_output.stderr)));
}
// Stage the submodule update
let add_output = std::process::Command::new("git")
.args(&["add", &module_info.path])
.current_dir(&self.ai_root)
.output()?;
if !add_output.status.success() {
return Err(anyhow::anyhow!("Failed to stage submodule: {}",
String::from_utf8_lossy(&add_output.stderr)));
}
Ok(())
}
async fn auto_commit_changes(&self, updated_modules: &[(String, Option<String>, Option<String>)]) -> Result<()> {
println!("{}", "💾 Auto-committing changes...".blue());
let mut commit_message = format!("Update submodules\n\n📦 Updated modules: {}\n", updated_modules.len());
for (module_name, old_commit, new_commit) in updated_modules {
commit_message.push_str(&format!(
"- {}: {}{}\n",
module_name,
old_commit.as_deref().unwrap_or("unknown"),
new_commit.as_deref().unwrap_or("unknown")
));
}
commit_message.push_str("\n🤖 Generated with aigpt-rs submodules update");
let commit_output = std::process::Command::new("git")
.args(&["commit", "-m", &commit_message])
.current_dir(&self.ai_root)
.output()?;
if commit_output.status.success() {
println!("{}", "✅ Changes committed successfully".green());
} else {
return Err(anyhow::anyhow!("Failed to commit: {}",
String::from_utf8_lossy(&commit_output.stderr)));
}
Ok(())
}
}

View File

@@ -1,36 +0,0 @@
use anyhow::Result;
/// AIInterpreter - Claude Code による解釈を期待する軽量ラッパー
///
/// このモジュールは外部 AI API を呼び出しません。
/// 代わりに、Claude Code 自身がコンテンツを解釈し、スコアを計算することを期待します。
///
/// 完全にローカルで動作し、API コストはゼロです。
pub struct AIInterpreter;
impl AIInterpreter {
pub fn new() -> Self {
AIInterpreter
}
/// コンテンツをそのまま返すClaude Code が解釈を担当)
pub async fn interpret_content(&self, content: &str) -> Result<String> {
Ok(content.to_string())
}
/// デフォルトスコアを返すClaude Code が実際のスコアを決定)
pub async fn calculate_priority_score(&self, _content: &str, _user_context: Option<&str>) -> Result<f32> {
Ok(0.5) // デフォルト値
}
/// 解釈とスコアリングを Claude Code に委ねる
pub async fn analyze(&self, content: &str, _user_context: Option<&str>) -> Result<(String, f32)> {
Ok((content.to_string(), 0.5))
}
}
impl Default for AIInterpreter {
fn default() -> Self {
Self::new()
}
}

View File

@@ -1,433 +0,0 @@
use crate::memory::Memory;
use crate::game_formatter::{MemoryRarity, DiagnosisType};
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc, Datelike};
/// コンパニオンキャラクター
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Companion {
pub name: String,
pub personality: CompanionPersonality,
pub relationship_level: u32, // レベル(経験値で上昇)
pub affection_score: f32, // 好感度 (0.0-1.0)
pub trust_level: u32, // 信頼度 (0-100)
pub total_xp: u32, // 総XP
pub last_interaction: DateTime<Utc>,
pub shared_memories: Vec<String>, // 共有された記憶のID
}
/// コンパニオンの性格
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompanionPersonality {
Energetic, // 元気で冒険好き - 革新者と相性◎
Intellectual, // 知的で思慮深い - 哲学者と相性◎
Practical, // 現実的で頼れる - 実務家と相性◎
Dreamy, // 夢見がちでロマンチック - 夢想家と相性◎
Balanced, // バランス型 - 分析家と相性◎
}
impl CompanionPersonality {
pub fn emoji(&self) -> &str {
match self {
CompanionPersonality::Energetic => "",
CompanionPersonality::Intellectual => "📚",
CompanionPersonality::Practical => "🎯",
CompanionPersonality::Dreamy => "🌙",
CompanionPersonality::Balanced => "⚖️",
}
}
pub fn name(&self) -> &str {
match self {
CompanionPersonality::Energetic => "元気で冒険好き",
CompanionPersonality::Intellectual => "知的で思慮深い",
CompanionPersonality::Practical => "現実的で頼れる",
CompanionPersonality::Dreamy => "夢見がちでロマンチック",
CompanionPersonality::Balanced => "バランス型",
}
}
/// ユーザーの診断タイプとの相性
pub fn compatibility(&self, user_type: &DiagnosisType) -> f32 {
match (self, user_type) {
(CompanionPersonality::Energetic, DiagnosisType::Innovator) => 0.95,
(CompanionPersonality::Intellectual, DiagnosisType::Philosopher) => 0.95,
(CompanionPersonality::Practical, DiagnosisType::Pragmatist) => 0.95,
(CompanionPersonality::Dreamy, DiagnosisType::Visionary) => 0.95,
(CompanionPersonality::Balanced, DiagnosisType::Analyst) => 0.95,
// その他の組み合わせ
_ => 0.7,
}
}
}
impl Companion {
pub fn new(name: String, personality: CompanionPersonality) -> Self {
Companion {
name,
personality,
relationship_level: 1,
affection_score: 0.0,
trust_level: 0,
total_xp: 0,
last_interaction: Utc::now(),
shared_memories: Vec::new(),
}
}
/// 記憶を共有して反応を得る
pub fn react_to_memory(&mut self, memory: &Memory, user_type: &DiagnosisType) -> CompanionReaction {
let rarity = MemoryRarity::from_score(memory.priority_score);
let xp = rarity.xp_value();
// XPを加算
self.total_xp += xp;
// 好感度上昇(スコアと相性による)
let compatibility = self.personality.compatibility(user_type);
let affection_gain = memory.priority_score * compatibility * 0.1;
self.affection_score = (self.affection_score + affection_gain).min(1.0);
// 信頼度上昇(高スコアの記憶ほど上昇)
if memory.priority_score >= 0.8 {
self.trust_level = (self.trust_level + 5).min(100);
}
// レベルアップチェック
let old_level = self.relationship_level;
self.relationship_level = (self.total_xp / 1000) + 1;
let level_up = self.relationship_level > old_level;
// 記憶を共有リストに追加
if memory.priority_score >= 0.6 {
self.shared_memories.push(memory.id.clone());
}
self.last_interaction = Utc::now();
// 反応メッセージを生成
let message = self.generate_reaction_message(memory, &rarity, user_type);
CompanionReaction {
message,
affection_gained: affection_gain,
xp_gained: xp,
level_up,
new_level: self.relationship_level,
current_affection: self.affection_score,
special_event: self.check_special_event(),
}
}
/// 記憶に基づく反応メッセージを生成
fn generate_reaction_message(&self, memory: &Memory, rarity: &MemoryRarity, _user_type: &DiagnosisType) -> String {
let content_preview = if memory.content.len() > 50 {
format!("{}...", &memory.content[..50])
} else {
memory.content.clone()
};
match (rarity, &self.personality) {
// LEGENDARY反応
(MemoryRarity::Legendary, CompanionPersonality::Energetic) => {
format!(
"すごい!「{}」って本当に素晴らしいアイデアだね!\n\
一緒に実現させよう!ワクワクするよ!",
content_preview
)
}
(MemoryRarity::Legendary, CompanionPersonality::Intellectual) => {
format!(
"{}」という考え、とても興味深いわ。\n\
深い洞察力を感じるの。もっと詳しく聞かせて?",
content_preview
)
}
(MemoryRarity::Legendary, CompanionPersonality::Practical) => {
format!(
"{}」か。実現可能性が高そうだね。\n\
具体的な計画を一緒に立てようよ。",
content_preview
)
}
(MemoryRarity::Legendary, CompanionPersonality::Dreamy) => {
format!(
"{}」...素敵♪ まるで夢みたい。\n\
あなたの想像力、本当に好きよ。",
content_preview
)
}
// EPIC反応
(MemoryRarity::Epic, _) => {
format!(
"おお、「{}」って面白いね!\n\
あなたのそういうところ、好きだな。",
content_preview
)
}
// RARE反応
(MemoryRarity::Rare, _) => {
format!(
"{}」か。なるほどね。\n\
そういう視点、参考になるよ。",
content_preview
)
}
// 通常反応
_ => {
format!(
"{}」について考えてるんだね。\n\
いつも色々考えてて尊敬するよ。",
content_preview
)
}
}
}
/// スペシャルイベントチェック
fn check_special_event(&self) -> Option<SpecialEvent> {
// 好感度MAXイベント
if self.affection_score >= 1.0 {
return Some(SpecialEvent::MaxAffection);
}
// レベル10到達
if self.relationship_level == 10 {
return Some(SpecialEvent::Level10);
}
// 信頼度MAX
if self.trust_level >= 100 {
return Some(SpecialEvent::MaxTrust);
}
None
}
/// デイリーメッセージを生成
pub fn generate_daily_message(&self) -> String {
let messages = match &self.personality {
CompanionPersonality::Energetic => vec![
"おはよう!今日は何か面白いことある?",
"ねえねえ、今日は一緒に新しいことやろうよ!",
"今日も元気出していこー!",
],
CompanionPersonality::Intellectual => vec![
"おはよう。今日はどんな発見があるかしら?",
"最近読んだ本の話、聞かせてくれない?",
"今日も一緒に学びましょう。",
],
CompanionPersonality::Practical => vec![
"おはよう。今日の予定は?",
"やることリスト、一緒に確認しようか。",
"今日も効率よくいこうね。",
],
CompanionPersonality::Dreamy => vec![
"おはよう...まだ夢の続き見てたの。",
"今日はどんな素敵なことが起こるかな♪",
"あなたと過ごす時間、大好き。",
],
CompanionPersonality::Balanced => vec![
"おはよう。今日も頑張ろうね。",
"何か手伝えることある?",
"今日も一緒にいられて嬉しいよ。",
],
};
let today = chrono::Utc::now().ordinal();
messages[today as usize % messages.len()].to_string()
}
}
/// コンパニオンの反応
#[derive(Debug, Serialize)]
pub struct CompanionReaction {
pub message: String,
pub affection_gained: f32,
pub xp_gained: u32,
pub level_up: bool,
pub new_level: u32,
pub current_affection: f32,
pub special_event: Option<SpecialEvent>,
}
/// スペシャルイベント
#[derive(Debug, Serialize)]
pub enum SpecialEvent {
MaxAffection, // 好感度MAX
Level10, // レベル10到達
MaxTrust, // 信頼度MAX
FirstDate, // 初デート
Confession, // 告白
}
impl SpecialEvent {
pub fn message(&self, companion_name: &str) -> String {
match self {
SpecialEvent::MaxAffection => {
format!(
"💕 特別なイベント発生!\n\n\
{}:「ねえ...あのね。\n\
いつも一緒にいてくれてありがとう。\n\
あなたのこと、すごく大切に思ってるの。\n\
これからも、ずっと一緒にいてね?」\n\n\
🎊 {} の好感度がMAXになりました",
companion_name, companion_name
)
}
SpecialEvent::Level10 => {
format!(
"🎉 レベル10到達\n\n\
{}:「ここまで一緒に来られたね。\n\
あなたとなら、どこまでも行けそう。」",
companion_name
)
}
SpecialEvent::MaxTrust => {
format!(
"✨ 信頼度MAX\n\n\
{}:「あなたのこと、心から信頼してる。\n\
何でも話せるって、すごく嬉しいよ。」",
companion_name
)
}
SpecialEvent::FirstDate => {
format!(
"💐 初デートイベント!\n\n\
{}:「今度、二人でどこか行かない?」",
companion_name
)
}
SpecialEvent::Confession => {
format!(
"💝 告白イベント!\n\n\
{}:「好きです。付き合ってください。」",
companion_name
)
}
}
}
}
/// コンパニオンフォーマッター
pub struct CompanionFormatter;
impl CompanionFormatter {
/// 反応を表示
pub fn format_reaction(companion: &Companion, reaction: &CompanionReaction) -> String {
let affection_bar = Self::format_affection_bar(reaction.current_affection);
let level_up_text = if reaction.level_up {
format!("\n🎊 レベルアップ! Lv.{} → Lv.{}", reaction.new_level - 1, reaction.new_level)
} else {
String::new()
};
let special_event_text = if let Some(ref event) = reaction.special_event {
format!("\n\n{}", event.message(&companion.name))
} else {
String::new()
};
format!(
r#"
╔══════════════════════════════════════════════════════════════╗
║ 💕 {} の反応 ║
╚══════════════════════════════════════════════════════════════╝
{} {}:
「{}」
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💕 好感度: {} (+{:.1}%)
💎 XP獲得: +{} XP{}
🏆 レベル: Lv.{}
🤝 信頼度: {} / 100
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━{}
"#,
companion.name,
companion.personality.emoji(),
companion.name,
reaction.message,
affection_bar,
reaction.affection_gained * 100.0,
reaction.xp_gained,
level_up_text,
companion.relationship_level,
companion.trust_level,
special_event_text
)
}
/// プロフィール表示
pub fn format_profile(companion: &Companion) -> String {
let affection_bar = Self::format_affection_bar(companion.affection_score);
format!(
r#"
╔══════════════════════════════════════════════════════════════╗
║ 💕 {} のプロフィール ║
╚══════════════════════════════════════════════════════════════╝
{} 性格: {}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 ステータス
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🏆 関係レベル: Lv.{}
💕 好感度: {}
🤝 信頼度: {} / 100
💎 総XP: {} XP
📚 共有記憶: {}個
🕐 最終交流: {}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💬 今日のひとこと:
「{}」
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
"#,
companion.name,
companion.personality.emoji(),
companion.personality.name(),
companion.relationship_level,
affection_bar,
companion.trust_level,
companion.total_xp,
companion.shared_memories.len(),
companion.last_interaction.format("%Y-%m-%d %H:%M"),
companion.generate_daily_message()
)
}
fn format_affection_bar(affection: f32) -> String {
let hearts = (affection * 10.0) as usize;
let filled = "❤️".repeat(hearts);
let empty = "🤍".repeat(10 - hearts);
format!("{}{} {:.0}%", filled, empty, affection * 100.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_companion_creation() {
let companion = Companion::new(
"エミリー".to_string(),
CompanionPersonality::Energetic,
);
assert_eq!(companion.name, "エミリー");
assert_eq!(companion.relationship_level, 1);
assert_eq!(companion.affection_score, 0.0);
}
#[test]
fn test_compatibility() {
let personality = CompanionPersonality::Energetic;
let innovator = DiagnosisType::Innovator;
assert_eq!(personality.compatibility(&innovator), 0.95);
}
}

View File

@@ -1,296 +0,0 @@
use anyhow::Result;
use serde_json::{json, Value};
use super::base::BaseMCPServer;
pub struct ExtendedMCPServer {
base: BaseMCPServer,
}
impl ExtendedMCPServer {
pub async fn new() -> Result<Self> {
let base = BaseMCPServer::new().await?;
Ok(ExtendedMCPServer { base })
}
pub async fn run(&mut self) -> Result<()> {
self.base.run().await
}
pub async fn handle_request(&mut self, request: Value) -> Value {
self.base.handle_request(request).await
}
// 拡張ツールを追加
pub fn get_available_tools(&self) -> Vec<Value> {
#[allow(unused_mut)]
let mut tools = self.base.get_available_tools();
// AI分析ツールを追加
#[cfg(feature = "ai-analysis")]
{
tools.push(json!({
"name": "analyze_sentiment",
"description": "Analyze sentiment of memories",
"inputSchema": {
"type": "object",
"properties": {
"period": {
"type": "string",
"description": "Time period to analyze"
}
}
}
}));
tools.push(json!({
"name": "extract_insights",
"description": "Extract insights and patterns from memories",
"inputSchema": {
"type": "object",
"properties": {
"category": {
"type": "string",
"description": "Category to analyze"
}
}
}
}));
}
// Web統合ツールを追加
#[cfg(feature = "web-integration")]
{
tools.push(json!({
"name": "import_webpage",
"description": "Import content from a webpage",
"inputSchema": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "URL to import from"
}
},
"required": ["url"]
}
}));
}
// セマンティック検索強化
#[cfg(feature = "semantic-search")]
{
// create_memoryを拡張版で上書き
if let Some(pos) = tools.iter().position(|tool| tool["name"] == "create_memory") {
tools[pos] = json!({
"name": "create_memory",
"description": "Create a new memory entry with optional AI analysis",
"inputSchema": {
"type": "object",
"properties": {
"content": {
"type": "string",
"description": "Content of the memory"
},
"analyze": {
"type": "boolean",
"description": "Enable AI analysis for this memory"
}
},
"required": ["content"]
}
});
}
// search_memoriesを拡張版で上書き
if let Some(pos) = tools.iter().position(|tool| tool["name"] == "search_memories") {
tools[pos] = json!({
"name": "search_memories",
"description": "Search memories with advanced options",
"inputSchema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
},
"semantic": {
"type": "boolean",
"description": "Use semantic search"
},
"category": {
"type": "string",
"description": "Filter by category"
},
"time_range": {
"type": "string",
"description": "Filter by time range (e.g., '1week', '1month')"
}
},
"required": ["query"]
}
});
}
}
tools
}
// 拡張ツール実行
pub async fn execute_tool(&mut self, tool_name: &str, arguments: &Value) -> Value {
match tool_name {
// 拡張機能
#[cfg(feature = "ai-analysis")]
"analyze_sentiment" => self.tool_analyze_sentiment(arguments).await,
#[cfg(feature = "ai-analysis")]
"extract_insights" => self.tool_extract_insights(arguments).await,
#[cfg(feature = "web-integration")]
"import_webpage" => self.tool_import_webpage(arguments).await,
// 拡張版の基本ツール (AI分析付き)
"create_memory" => self.tool_create_memory_extended(arguments).await,
"search_memories" => self.tool_search_memories_extended(arguments).await,
// 基本ツールにフォールバック
_ => self.base.execute_tool(tool_name, arguments).await,
}
}
// 拡張ツール実装
async fn tool_create_memory_extended(&mut self, arguments: &Value) -> Value {
let content = arguments["content"].as_str().unwrap_or("");
let analyze = arguments["analyze"].as_bool().unwrap_or(false);
let final_content = if analyze {
#[cfg(feature = "ai-analysis")]
{
format!("[AI分析] 感情: neutral, カテゴリ: general\n{}", content)
}
#[cfg(not(feature = "ai-analysis"))]
{
content.to_string()
}
} else {
content.to_string()
};
match self.base.memory_manager.create_memory(&final_content) {
Ok(id) => json!({
"success": true,
"id": id,
"message": if analyze { "Memory created with AI analysis" } else { "Memory created successfully" }
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
})
}
}
async fn tool_search_memories_extended(&mut self, arguments: &Value) -> Value {
let query = arguments["query"].as_str().unwrap_or("");
let semantic = arguments["semantic"].as_bool().unwrap_or(false);
let memories = if semantic {
#[cfg(feature = "semantic-search")]
{
// モックセマンティック検索
self.base.memory_manager.search_memories(query)
}
#[cfg(not(feature = "semantic-search"))]
{
self.base.memory_manager.search_memories(query)
}
} else {
self.base.memory_manager.search_memories(query)
};
json!({
"success": true,
"memories": memories.into_iter().map(|m| json!({
"id": m.id,
"content": m.content,
"interpreted_content": m.interpreted_content,
"priority_score": m.priority_score,
"user_context": m.user_context,
"created_at": m.created_at,
"updated_at": m.updated_at
})).collect::<Vec<_>>(),
"search_type": if semantic { "semantic" } else { "keyword" }
})
}
#[cfg(feature = "ai-analysis")]
async fn tool_analyze_sentiment(&mut self, _arguments: &Value) -> Value {
json!({
"success": true,
"analysis": {
"positive": 60,
"neutral": 30,
"negative": 10,
"dominant_sentiment": "positive"
},
"message": "Sentiment analysis completed"
})
}
#[cfg(feature = "ai-analysis")]
async fn tool_extract_insights(&mut self, _arguments: &Value) -> Value {
json!({
"success": true,
"insights": {
"most_frequent_topics": ["programming", "ai", "productivity"],
"learning_frequency": "5 times per week",
"growth_trend": "increasing",
"recommendations": ["Focus more on advanced topics", "Consider practical applications"]
},
"message": "Insights extracted successfully"
})
}
#[cfg(feature = "web-integration")]
async fn tool_import_webpage(&mut self, arguments: &Value) -> Value {
let url = arguments["url"].as_str().unwrap_or("");
match self.import_from_web(url).await {
Ok(content) => {
match self.base.memory_manager.create_memory(&content) {
Ok(id) => json!({
"success": true,
"id": id,
"message": format!("Webpage imported successfully from {}", url)
}),
Err(e) => json!({
"success": false,
"error": e.to_string()
})
}
}
Err(e) => json!({
"success": false,
"error": format!("Failed to import webpage: {}", e)
})
}
}
#[cfg(feature = "web-integration")]
async fn import_from_web(&self, url: &str) -> Result<String> {
let response = reqwest::get(url).await?;
let content = response.text().await?;
let document = scraper::Html::parse_document(&content);
let title_selector = scraper::Selector::parse("title").unwrap();
let body_selector = scraper::Selector::parse("p").unwrap();
let title = document.select(&title_selector)
.next()
.map(|el| el.inner_html())
.unwrap_or_else(|| "Untitled".to_string());
let paragraphs: Vec<String> = document.select(&body_selector)
.map(|el| el.inner_html())
.take(5)
.collect();
Ok(format!("# {}\nURL: {}\n\n{}", title, url, paragraphs.join("\n\n")))
}
}

View File

@@ -1,365 +0,0 @@
use crate::memory::Memory;
use serde::{Deserialize, Serialize};
use chrono::Datelike;
/// メモリーのレア度
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryRarity {
Common, // 0.0-0.4
Uncommon, // 0.4-0.6
Rare, // 0.6-0.8
Epic, // 0.8-0.9
Legendary, // 0.9-1.0
}
impl MemoryRarity {
pub fn from_score(score: f32) -> Self {
match score {
s if s >= 0.9 => MemoryRarity::Legendary,
s if s >= 0.8 => MemoryRarity::Epic,
s if s >= 0.6 => MemoryRarity::Rare,
s if s >= 0.4 => MemoryRarity::Uncommon,
_ => MemoryRarity::Common,
}
}
pub fn emoji(&self) -> &str {
match self {
MemoryRarity::Common => "",
MemoryRarity::Uncommon => "🟢",
MemoryRarity::Rare => "🔵",
MemoryRarity::Epic => "🟣",
MemoryRarity::Legendary => "🟡",
}
}
pub fn name(&self) -> &str {
match self {
MemoryRarity::Common => "COMMON",
MemoryRarity::Uncommon => "UNCOMMON",
MemoryRarity::Rare => "RARE",
MemoryRarity::Epic => "EPIC",
MemoryRarity::Legendary => "LEGENDARY",
}
}
pub fn xp_value(&self) -> u32 {
match self {
MemoryRarity::Common => 100,
MemoryRarity::Uncommon => 250,
MemoryRarity::Rare => 500,
MemoryRarity::Epic => 850,
MemoryRarity::Legendary => 1000,
}
}
}
/// 診断タイプ
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DiagnosisType {
Innovator, // 革新者(創造性高、実用性高)
Philosopher, // 哲学者(感情高、新規性高)
Pragmatist, // 実務家(実用性高、関連性高)
Visionary, // 夢想家(新規性高、感情高)
Analyst, // 分析家(全て平均的)
}
impl DiagnosisType {
/// スコアから診断タイプを推定(公開用)
pub fn from_memory(memory: &crate::memory::Memory) -> Self {
// スコア内訳を推定
let emotional = (memory.priority_score * 0.25).min(0.25);
let relevance = (memory.priority_score * 0.25).min(0.25);
let novelty = (memory.priority_score * 0.25).min(0.25);
let utility = memory.priority_score - emotional - relevance - novelty;
Self::from_score_breakdown(emotional, relevance, novelty, utility)
}
pub fn from_score_breakdown(
emotional: f32,
relevance: f32,
novelty: f32,
utility: f32,
) -> Self {
if utility > 0.2 && novelty > 0.2 {
DiagnosisType::Innovator
} else if emotional > 0.2 && novelty > 0.2 {
DiagnosisType::Philosopher
} else if utility > 0.2 && relevance > 0.2 {
DiagnosisType::Pragmatist
} else if novelty > 0.2 && emotional > 0.18 {
DiagnosisType::Visionary
} else {
DiagnosisType::Analyst
}
}
pub fn emoji(&self) -> &str {
match self {
DiagnosisType::Innovator => "💡",
DiagnosisType::Philosopher => "🧠",
DiagnosisType::Pragmatist => "🎯",
DiagnosisType::Visionary => "",
DiagnosisType::Analyst => "📊",
}
}
pub fn name(&self) -> &str {
match self {
DiagnosisType::Innovator => "革新者",
DiagnosisType::Philosopher => "哲学者",
DiagnosisType::Pragmatist => "実務家",
DiagnosisType::Visionary => "夢想家",
DiagnosisType::Analyst => "分析家",
}
}
pub fn description(&self) -> &str {
match self {
DiagnosisType::Innovator => "創造的で実用的なアイデアを生み出す。常に新しい可能性を探求し、それを現実のものにする力を持つ。",
DiagnosisType::Philosopher => "深い思考と感情を大切にする。抽象的な概念や人生の意味について考えることを好む。",
DiagnosisType::Pragmatist => "現実的で効率的。具体的な問題解決に優れ、確実に結果を出す。",
DiagnosisType::Visionary => "大胆な夢と理想を追い求める。常識にとらわれず、未来の可能性を信じる。",
DiagnosisType::Analyst => "バランスの取れた思考。多角的な視点から物事を分析し、冷静に判断する。",
}
}
}
/// ゲーム風の結果フォーマッター
pub struct GameFormatter;
impl GameFormatter {
/// メモリー作成結果をゲーム風に表示
pub fn format_memory_result(memory: &Memory) -> String {
let rarity = MemoryRarity::from_score(memory.priority_score);
let xp = rarity.xp_value();
let score_percentage = (memory.priority_score * 100.0) as u32;
// スコア内訳を推定各項目最大0.25として)
let emotional = (memory.priority_score * 0.25).min(0.25);
let relevance = (memory.priority_score * 0.25).min(0.25);
let novelty = (memory.priority_score * 0.25).min(0.25);
let utility = memory.priority_score - emotional - relevance - novelty;
let diagnosis = DiagnosisType::from_score_breakdown(
emotional,
relevance,
novelty,
utility,
);
format!(
r#"
╔══════════════════════════════════════════════════════════════╗
║ 🎲 メモリースコア判定 ║
╚══════════════════════════════════════════════════════════════╝
⚡ 分析完了! あなたの思考が記録されました
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 総合スコア
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{} {} {}点
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎯 詳細分析
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💓 感情的インパクト: {}
🔗 ユーザー関連性: {}
✨ 新規性・独自性: {}
⚙️ 実用性: {}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎊 あなたのタイプ
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{} 【{}】
{}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🏆 報酬
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💎 XP獲得: +{} XP
🎁 レア度: {} {}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💬 AI の解釈
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📤 この結果をシェアしよう!
#aigpt #メモリースコア #{}
"#,
rarity.emoji(),
rarity.name(),
score_percentage,
Self::format_bar(emotional, 0.25),
Self::format_bar(relevance, 0.25),
Self::format_bar(novelty, 0.25),
Self::format_bar(utility, 0.25),
diagnosis.emoji(),
diagnosis.name(),
diagnosis.description(),
xp,
rarity.emoji(),
rarity.name(),
memory.interpreted_content,
diagnosis.name(),
)
}
/// シェア用の短縮テキストを生成
pub fn format_shareable_text(memory: &Memory) -> String {
let rarity = MemoryRarity::from_score(memory.priority_score);
let score_percentage = (memory.priority_score * 100.0) as u32;
let emotional = (memory.priority_score * 0.25).min(0.25);
let relevance = (memory.priority_score * 0.25).min(0.25);
let novelty = (memory.priority_score * 0.25).min(0.25);
let utility = memory.priority_score - emotional - relevance - novelty;
let diagnosis = DiagnosisType::from_score_breakdown(
emotional,
relevance,
novelty,
utility,
);
format!(
r#"🎲 AIメモリースコア診断結果
{} {} {}点
{} 【{}】
{}
#aigpt #メモリースコア #AI診断"#,
rarity.emoji(),
rarity.name(),
score_percentage,
diagnosis.emoji(),
diagnosis.name(),
Self::truncate(&memory.content, 100),
)
}
/// ランキング表示
pub fn format_ranking(memories: &[&Memory], title: &str) -> String {
let mut result = format!(
r#"
╔══════════════════════════════════════════════════════════════╗
║ 🏆 {} ║
╚══════════════════════════════════════════════════════════════╝
"#,
title
);
for (i, memory) in memories.iter().take(10).enumerate() {
let rank_emoji = match i {
0 => "🥇",
1 => "🥈",
2 => "🥉",
_ => " ",
};
let rarity = MemoryRarity::from_score(memory.priority_score);
let score = (memory.priority_score * 100.0) as u32;
result.push_str(&format!(
"{} {}{} {} {}点 - {}\n",
rank_emoji,
i + 1,
rarity.emoji(),
rarity.name(),
score,
Self::truncate(&memory.content, 40)
));
}
result.push_str("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
result
}
/// デイリーチャレンジ表示
pub fn format_daily_challenge() -> String {
// 今日の日付をシードにランダムなお題を生成
let challenges = vec![
"今日学んだことを記録しよう",
"新しいアイデアを思いついた?",
"感動したことを書き留めよう",
"目標を一つ設定しよう",
"誰かに感謝の気持ちを伝えよう",
];
let today = chrono::Utc::now().ordinal();
let challenge = challenges[today as usize % challenges.len()];
format!(
r#"
╔══════════════════════════════════════════════════════════════╗
║ 📅 今日のチャレンジ ║
╚══════════════════════════════════════════════════════════════╝
✨ {}
🎁 報酬: +200 XP
💎 完了すると特別なバッジが獲得できます!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
"#,
challenge
)
}
/// プログレスバーを生成
fn format_bar(value: f32, max: f32) -> String {
let percentage = (value / max * 100.0) as u32;
let filled = (percentage / 10) as usize;
let empty = 10 - filled;
format!(
"[{}{}] {}%",
"".repeat(filled),
"".repeat(empty),
percentage
)
}
/// テキストを切り詰め
fn truncate(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else {
format!("{}...", &s[..max_len])
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Utc;
#[test]
fn test_rarity_from_score() {
assert!(matches!(MemoryRarity::from_score(0.95), MemoryRarity::Legendary));
assert!(matches!(MemoryRarity::from_score(0.85), MemoryRarity::Epic));
assert!(matches!(MemoryRarity::from_score(0.7), MemoryRarity::Rare));
assert!(matches!(MemoryRarity::from_score(0.5), MemoryRarity::Uncommon));
assert!(matches!(MemoryRarity::from_score(0.3), MemoryRarity::Common));
}
#[test]
fn test_diagnosis_type() {
let diagnosis = DiagnosisType::from_score_breakdown(0.1, 0.1, 0.22, 0.22);
assert!(matches!(diagnosis, DiagnosisType::Innovator));
}
#[test]
fn test_format_bar() {
let bar = GameFormatter::format_bar(0.15, 0.25);
assert!(bar.contains("60%"));
}
}

View File

@@ -1,374 +0,0 @@
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use uuid::Uuid;
use crate::ai_interpreter::AIInterpreter;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Memory {
pub id: String,
pub content: String,
#[serde(default = "default_interpreted_content")]
pub interpreted_content: String, // AI解釈後のコンテンツ
#[serde(default = "default_priority_score")]
pub priority_score: f32, // 心理判定スコア (0.0-1.0)
#[serde(default)]
pub user_context: Option<String>, // ユーザー固有性
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
fn default_interpreted_content() -> String {
String::new()
}
fn default_priority_score() -> f32 {
0.5
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Conversation {
pub id: String,
pub title: String,
pub created_at: DateTime<Utc>,
pub message_count: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChatGPTNode {
id: String,
children: Vec<String>,
parent: Option<String>,
message: Option<ChatGPTMessage>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChatGPTMessage {
id: String,
author: ChatGPTAuthor,
content: ChatGPTContent,
create_time: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChatGPTAuthor {
role: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
enum ChatGPTContent {
Text {
content_type: String,
parts: Vec<String>,
},
Other(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChatGPTConversation {
#[serde(default)]
id: String,
#[serde(alias = "conversation_id")]
conversation_id: Option<String>,
title: String,
create_time: f64,
mapping: HashMap<String, ChatGPTNode>,
}
pub struct MemoryManager {
memories: HashMap<String, Memory>,
conversations: HashMap<String, Conversation>,
data_file: PathBuf,
max_memories: usize, // 最大記憶数
#[allow(dead_code)]
min_priority_score: f32, // 最小優先度スコア (将来の機能で使用予定)
ai_interpreter: AIInterpreter, // AI解釈エンジン
}
impl MemoryManager {
pub async fn new() -> Result<Self> {
let data_dir = dirs::config_dir()
.context("Could not find config directory")?
.join("syui")
.join("ai")
.join("gpt");
std::fs::create_dir_all(&data_dir)?;
let data_file = data_dir.join("memory.json");
let (memories, conversations) = if data_file.exists() {
Self::load_data(&data_file)?
} else {
(HashMap::new(), HashMap::new())
};
Ok(MemoryManager {
memories,
conversations,
data_file,
max_memories: 100, // デフォルト: 100件
min_priority_score: 0.3, // デフォルト: 0.3以上
ai_interpreter: AIInterpreter::new(),
})
}
pub fn create_memory(&mut self, content: &str) -> Result<String> {
let id = Uuid::new_v4().to_string();
let now = Utc::now();
let memory = Memory {
id: id.clone(),
content: content.to_string(),
interpreted_content: content.to_string(), // 後でAI解釈を実装
priority_score: 0.5, // 後で心理判定を実装
user_context: None,
created_at: now,
updated_at: now,
};
self.memories.insert(id.clone(), memory);
// 容量制限チェック
self.prune_memories_if_needed()?;
self.save_data()?;
Ok(id)
}
/// AI解釈と心理判定を使った記憶作成後方互換性のため残す
pub async fn create_memory_with_ai(
&mut self,
content: &str,
user_context: Option<&str>,
) -> Result<String> {
let id = Uuid::new_v4().to_string();
let now = Utc::now();
// AI解釈と心理判定を実行
let (interpreted_content, priority_score) = self
.ai_interpreter
.analyze(content, user_context)
.await?;
let memory = Memory {
id: id.clone(),
content: content.to_string(),
interpreted_content,
priority_score,
user_context: user_context.map(|s| s.to_string()),
created_at: now,
updated_at: now,
};
self.memories.insert(id.clone(), memory);
// 容量制限チェック
self.prune_memories_if_needed()?;
self.save_data()?;
Ok(id)
}
/// Claude Code から解釈とスコアを受け取ってメモリを作成
pub fn create_memory_with_interpretation(
&mut self,
content: &str,
interpreted_content: &str,
priority_score: f32,
user_context: Option<&str>,
) -> Result<String> {
let id = Uuid::new_v4().to_string();
let now = Utc::now();
let memory = Memory {
id: id.clone(),
content: content.to_string(),
interpreted_content: interpreted_content.to_string(),
priority_score: priority_score.max(0.0).min(1.0), // 0.0-1.0 に制限
user_context: user_context.map(|s| s.to_string()),
created_at: now,
updated_at: now,
};
self.memories.insert(id.clone(), memory);
// 容量制限チェック
self.prune_memories_if_needed()?;
self.save_data()?;
Ok(id)
}
pub fn update_memory(&mut self, id: &str, content: &str) -> Result<()> {
if let Some(memory) = self.memories.get_mut(id) {
memory.content = content.to_string();
memory.updated_at = Utc::now();
self.save_data()?;
Ok(())
} else {
Err(anyhow::anyhow!("Memory not found: {}", id))
}
}
pub fn delete_memory(&mut self, id: &str) -> Result<()> {
if self.memories.remove(id).is_some() {
self.save_data()?;
Ok(())
} else {
Err(anyhow::anyhow!("Memory not found: {}", id))
}
}
// 容量制限: 優先度が低いものから削除
fn prune_memories_if_needed(&mut self) -> Result<()> {
if self.memories.len() <= self.max_memories {
return Ok(());
}
// 優先度でソートして、低いものから削除
let mut sorted_memories: Vec<_> = self.memories.iter()
.map(|(id, mem)| (id.clone(), mem.priority_score))
.collect();
sorted_memories.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
let to_remove = self.memories.len() - self.max_memories;
for (id, _) in sorted_memories.iter().take(to_remove) {
self.memories.remove(id);
}
Ok(())
}
// 優先度順に記憶を取得
pub fn get_memories_by_priority(&self) -> Vec<&Memory> {
let mut memories: Vec<_> = self.memories.values().collect();
memories.sort_by(|a, b| b.priority_score.partial_cmp(&a.priority_score).unwrap_or(std::cmp::Ordering::Equal));
memories
}
pub fn search_memories(&self, query: &str) -> Vec<&Memory> {
let query_lower = query.to_lowercase();
let mut results: Vec<_> = self.memories
.values()
.filter(|memory| memory.content.to_lowercase().contains(&query_lower))
.collect();
results.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
results
}
pub fn list_conversations(&self) -> Vec<&Conversation> {
let mut conversations: Vec<_> = self.conversations.values().collect();
conversations.sort_by(|a, b| b.created_at.cmp(&a.created_at));
conversations
}
#[allow(dead_code)]
pub async fn import_chatgpt_conversations(&mut self, file_path: &PathBuf) -> Result<()> {
let content = std::fs::read_to_string(file_path)
.context("Failed to read conversations file")?;
let chatgpt_conversations: Vec<ChatGPTConversation> = serde_json::from_str(&content)
.context("Failed to parse ChatGPT conversations")?;
let mut imported_memories = 0;
let mut imported_conversations = 0;
for conv in chatgpt_conversations {
// Get the actual conversation ID
let conv_id = if !conv.id.is_empty() {
conv.id.clone()
} else if let Some(cid) = conv.conversation_id {
cid
} else {
Uuid::new_v4().to_string()
};
// Add conversation
let conversation = Conversation {
id: conv_id.clone(),
title: conv.title.clone(),
created_at: DateTime::from_timestamp(conv.create_time as i64, 0)
.unwrap_or_else(Utc::now),
message_count: conv.mapping.len() as u32,
};
self.conversations.insert(conv_id.clone(), conversation);
imported_conversations += 1;
// Extract memories from messages
for (_, node) in conv.mapping {
if let Some(message) = node.message {
if let ChatGPTContent::Text { parts, .. } = message.content {
for part in parts {
if !part.trim().is_empty() && part.len() > 10 {
let memory_content = format!("[{}] {}", conv.title, part);
self.create_memory(&memory_content)?;
imported_memories += 1;
}
}
}
}
}
}
println!("Imported {} conversations and {} memories",
imported_conversations, imported_memories);
Ok(())
}
fn load_data(file_path: &PathBuf) -> Result<(HashMap<String, Memory>, HashMap<String, Conversation>)> {
let content = std::fs::read_to_string(file_path)
.context("Failed to read data file")?;
#[derive(Deserialize)]
struct Data {
memories: HashMap<String, Memory>,
conversations: HashMap<String, Conversation>,
}
let data: Data = serde_json::from_str(&content)
.context("Failed to parse data file")?;
Ok((data.memories, data.conversations))
}
// Getter: 単一メモリ取得
pub fn get_memory(&self, id: &str) -> Option<&Memory> {
self.memories.get(id)
}
// Getter: 全メモリ取得
pub fn get_all_memories(&self) -> Vec<&Memory> {
self.memories.values().collect()
}
fn save_data(&self) -> Result<()> {
#[derive(Serialize)]
struct Data<'a> {
memories: &'a HashMap<String, Memory>,
conversations: &'a HashMap<String, Conversation>,
}
let data = Data {
memories: &self.memories,
conversations: &self.conversations,
};
let content = serde_json::to_string_pretty(&data)
.context("Failed to serialize data")?;
std::fs::write(&self.data_file, content)
.context("Failed to write data file")?;
Ok(())
}
}

505
src/tokens.rs Normal file
View File

@@ -0,0 +1,505 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use crate::cli::TokenCommands;
/// Token usage record from Claude Code JSONL files
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TokenRecord {
#[serde(default)]
pub timestamp: String,
#[serde(default)]
pub usage: Option<TokenUsage>,
#[serde(default)]
pub model: Option<String>,
#[serde(default)]
pub conversation_id: Option<String>,
}
/// Token usage details
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TokenUsage {
#[serde(default)]
pub input_tokens: Option<u64>,
#[serde(default)]
pub output_tokens: Option<u64>,
#[serde(default)]
pub total_tokens: Option<u64>,
}
/// Cost calculation summary
#[derive(Debug, Clone, Serialize)]
pub struct CostSummary {
pub input_tokens: u64,
pub output_tokens: u64,
pub total_tokens: u64,
pub input_cost_usd: f64,
pub output_cost_usd: f64,
pub total_cost_usd: f64,
pub total_cost_jpy: f64,
pub record_count: usize,
}
/// Daily breakdown of token usage
#[derive(Debug, Clone, Serialize)]
pub struct DailyBreakdown {
pub date: String,
pub summary: CostSummary,
}
/// Configuration for cost calculation
#[derive(Debug, Clone)]
pub struct CostConfig {
pub input_cost_per_1m: f64, // USD per 1M input tokens
pub output_cost_per_1m: f64, // USD per 1M output tokens
pub usd_to_jpy_rate: f64,
}
impl Default for CostConfig {
fn default() -> Self {
Self {
input_cost_per_1m: 3.0,
output_cost_per_1m: 15.0,
usd_to_jpy_rate: 150.0,
}
}
}
/// Token analysis functionality
pub struct TokenAnalyzer {
config: CostConfig,
}
impl TokenAnalyzer {
pub fn new() -> Self {
Self {
config: CostConfig::default(),
}
}
pub fn with_config(config: CostConfig) -> Self {
Self { config }
}
/// Find Claude Code data directory
pub fn find_claude_data_dir() -> Option<PathBuf> {
let possible_dirs = [
dirs::home_dir().map(|h| h.join(".claude")),
dirs::config_dir().map(|c| c.join("claude")),
Some(PathBuf::from(".claude")),
];
for dir_opt in possible_dirs.iter() {
if let Some(dir) = dir_opt {
if dir.exists() && dir.is_dir() {
return Some(dir.clone());
}
}
}
None
}
/// Parse JSONL files from Claude data directory
pub fn parse_jsonl_files<P: AsRef<Path>>(&self, claude_dir: P) -> Result<Vec<TokenRecord>> {
let claude_dir = claude_dir.as_ref();
let mut records = Vec::new();
// Look for JSONL files in the directory
if let Ok(entries) = std::fs::read_dir(claude_dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().map_or(false, |ext| ext == "jsonl") {
match self.parse_jsonl_file(&path) {
Ok(mut file_records) => records.append(&mut file_records),
Err(e) => {
eprintln!("Warning: Failed to parse {}: {}", path.display(), e);
}
}
}
}
}
Ok(records)
}
/// Parse a single JSONL file
fn parse_jsonl_file<P: AsRef<Path>>(&self, file_path: P) -> Result<Vec<TokenRecord>> {
let file = File::open(file_path)?;
let reader = BufReader::new(file);
let mut records = Vec::new();
for (line_num, line) in reader.lines().enumerate() {
match line {
Ok(line_content) => {
if line_content.trim().is_empty() {
continue;
}
match serde_json::from_str::<TokenRecord>(&line_content) {
Ok(record) => {
// Only include records with usage data
if record.usage.is_some() {
records.push(record);
}
}
Err(e) => {
eprintln!("Warning: Failed to parse line {}: {}", line_num + 1, e);
}
}
}
Err(e) => {
eprintln!("Warning: Failed to read line {}: {}", line_num + 1, e);
}
}
}
Ok(records)
}
/// Calculate cost summary from records
pub fn calculate_costs(&self, records: &[TokenRecord]) -> CostSummary {
let mut input_tokens = 0u64;
let mut output_tokens = 0u64;
for record in records {
if let Some(usage) = &record.usage {
input_tokens += usage.input_tokens.unwrap_or(0);
output_tokens += usage.output_tokens.unwrap_or(0);
}
}
let total_tokens = input_tokens + output_tokens;
let input_cost_usd = (input_tokens as f64 / 1_000_000.0) * self.config.input_cost_per_1m;
let output_cost_usd = (output_tokens as f64 / 1_000_000.0) * self.config.output_cost_per_1m;
let total_cost_usd = input_cost_usd + output_cost_usd;
let total_cost_jpy = total_cost_usd * self.config.usd_to_jpy_rate;
CostSummary {
input_tokens,
output_tokens,
total_tokens,
input_cost_usd,
output_cost_usd,
total_cost_usd,
total_cost_jpy,
record_count: records.len(),
}
}
/// Group records by date (JST timezone)
pub fn group_by_date(&self, records: &[TokenRecord]) -> Result<HashMap<String, Vec<TokenRecord>>> {
let mut grouped: HashMap<String, Vec<TokenRecord>> = HashMap::new();
for record in records {
let date_str = self.extract_date_jst(&record.timestamp)?;
grouped.entry(date_str).or_insert_with(Vec::new).push(record.clone());
}
Ok(grouped)
}
/// Extract date in JST from timestamp
fn extract_date_jst(&self, timestamp: &str) -> Result<String> {
if timestamp.is_empty() {
return Err(anyhow!("Empty timestamp"));
}
// Try to parse various timestamp formats
let dt = if let Ok(dt) = DateTime::parse_from_rfc3339(timestamp) {
dt.with_timezone(&chrono_tz::Asia::Tokyo)
} else if let Ok(dt) = DateTime::parse_from_str(timestamp, "%Y-%m-%dT%H:%M:%S%.fZ") {
dt.with_timezone(&chrono_tz::Asia::Tokyo)
} else if let Ok(dt) = chrono::DateTime::parse_from_str(timestamp, "%Y-%m-%d %H:%M:%S") {
dt.with_timezone(&chrono_tz::Asia::Tokyo)
} else {
return Err(anyhow!("Failed to parse timestamp: {}", timestamp));
};
Ok(dt.format("%Y-%m-%d").to_string())
}
/// Generate daily breakdown
pub fn daily_breakdown(&self, records: &[TokenRecord]) -> Result<Vec<DailyBreakdown>> {
let grouped = self.group_by_date(records)?;
let mut breakdowns: Vec<DailyBreakdown> = grouped
.into_iter()
.map(|(date, date_records)| DailyBreakdown {
date,
summary: self.calculate_costs(&date_records),
})
.collect();
// Sort by date (most recent first)
breakdowns.sort_by(|a, b| b.date.cmp(&a.date));
Ok(breakdowns)
}
/// Filter records by time period
pub fn filter_by_period(&self, records: &[TokenRecord], period: &str) -> Result<Vec<TokenRecord>> {
let now = Local::now();
let cutoff = match period {
"today" => now.date_naive().and_hms_opt(0, 0, 0).unwrap(),
"week" => (now - chrono::Duration::days(7)).naive_local(),
"month" => (now - chrono::Duration::days(30)).naive_local(),
"all" => return Ok(records.to_vec()),
_ => return Err(anyhow!("Invalid period: {}", period)),
};
let filtered: Vec<TokenRecord> = records
.iter()
.filter(|record| {
if let Ok(date_str) = self.extract_date_jst(&record.timestamp) {
if let Ok(record_date) = chrono::NaiveDate::parse_from_str(&date_str, "%Y-%m-%d") {
return record_date.and_hms_opt(0, 0, 0).unwrap() >= cutoff;
}
}
false
})
.cloned()
.collect();
Ok(filtered)
}
}
/// Handle token-related commands
pub async fn handle_tokens(command: TokenCommands) -> Result<()> {
match command {
TokenCommands::Summary { period, claude_dir, details, format } => {
handle_summary(
period.unwrap_or_else(|| "week".to_string()),
claude_dir,
details,
format.unwrap_or_else(|| "table".to_string())
).await
}
TokenCommands::Daily { days, claude_dir } => {
handle_daily(days.unwrap_or(7), claude_dir).await
}
TokenCommands::Status { claude_dir } => {
handle_status(claude_dir).await
}
TokenCommands::Analyze { file } => {
println!("Token analysis for file: {:?} - Not implemented yet", file);
Ok(())
}
TokenCommands::Report { days } => {
println!("Token report for {} days - Not implemented yet", days.unwrap_or(7));
Ok(())
}
TokenCommands::Cost { month } => {
println!("Token cost for month: {} - Not implemented yet", month.unwrap_or_else(|| "current".to_string()));
Ok(())
}
}
}
/// Handle summary command
async fn handle_summary(
period: String,
claude_dir: Option<PathBuf>,
details: bool,
format: String,
) -> Result<()> {
let analyzer = TokenAnalyzer::new();
// Find Claude data directory
let data_dir = claude_dir.or_else(|| TokenAnalyzer::find_claude_data_dir())
.ok_or_else(|| anyhow!("Claude Code data directory not found"))?;
println!("Loading data from: {}", data_dir.display());
// Parse records
let all_records = analyzer.parse_jsonl_files(&data_dir)?;
if all_records.is_empty() {
println!("No token usage data found");
return Ok(());
}
// Filter by period
let filtered_records = analyzer.filter_by_period(&all_records, &period)?;
if filtered_records.is_empty() {
println!("No data found for period: {}", period);
return Ok(());
}
// Calculate summary
let summary = analyzer.calculate_costs(&filtered_records);
// Output results
match format.as_str() {
"json" => {
println!("{}", serde_json::to_string_pretty(&summary)?);
}
"table" | _ => {
print_summary_table(&summary, &period, details);
}
}
Ok(())
}
/// Handle daily command
async fn handle_daily(days: u32, claude_dir: Option<PathBuf>) -> Result<()> {
let analyzer = TokenAnalyzer::new();
// Find Claude data directory
let data_dir = claude_dir.or_else(|| TokenAnalyzer::find_claude_data_dir())
.ok_or_else(|| anyhow!("Claude Code data directory not found"))?;
println!("Loading data from: {}", data_dir.display());
// Parse records
let records = analyzer.parse_jsonl_files(&data_dir)?;
if records.is_empty() {
println!("No token usage data found");
return Ok(());
}
// Generate daily breakdown
let breakdown = analyzer.daily_breakdown(&records)?;
let limited_breakdown: Vec<_> = breakdown.into_iter().take(days as usize).collect();
// Print daily breakdown
print_daily_breakdown(&limited_breakdown);
Ok(())
}
/// Handle status command
async fn handle_status(claude_dir: Option<PathBuf>) -> Result<()> {
let analyzer = TokenAnalyzer::new();
// Find Claude data directory
let data_dir = claude_dir.or_else(|| TokenAnalyzer::find_claude_data_dir());
match data_dir {
Some(dir) => {
println!("Claude Code data directory: {}", dir.display());
// Parse records to get basic stats
let records = analyzer.parse_jsonl_files(&dir)?;
let summary = analyzer.calculate_costs(&records);
println!("Total records: {}", summary.record_count);
println!("Total tokens: {}", summary.total_tokens);
println!("Estimated total cost: ${:.4} USD (¥{:.0} JPY)",
summary.total_cost_usd, summary.total_cost_jpy);
}
None => {
println!("Claude Code data directory not found");
println!("Checked locations:");
println!(" - ~/.claude");
println!(" - ~/.config/claude");
println!(" - ./.claude");
}
}
Ok(())
}
/// Print summary table
fn print_summary_table(summary: &CostSummary, period: &str, details: bool) {
println!("\n=== Claude Code Token Usage Summary ({}) ===", period);
println!();
println!("📊 Token Usage:");
println!(" Input tokens: {:>12}", format_number(summary.input_tokens));
println!(" Output tokens: {:>12}", format_number(summary.output_tokens));
println!(" Total tokens: {:>12}", format_number(summary.total_tokens));
println!();
println!("💰 Cost Estimation:");
println!(" Input cost: {:>12}", format!("${:.4} USD", summary.input_cost_usd));
println!(" Output cost: {:>12}", format!("${:.4} USD", summary.output_cost_usd));
println!(" Total cost: {:>12}", format!("${:.4} USD", summary.total_cost_usd));
println!(" Total cost: {:>12}", format!("¥{:.0} JPY", summary.total_cost_jpy));
println!();
if details {
println!("📈 Additional Details:");
println!(" Records: {:>12}", format_number(summary.record_count as u64));
println!(" Avg per record:{:>12}", format!("${:.4} USD",
if summary.record_count > 0 { summary.total_cost_usd / summary.record_count as f64 } else { 0.0 }));
println!();
}
println!("💡 Cost calculation based on:");
println!(" Input: $3.00 per 1M tokens");
println!(" Output: $15.00 per 1M tokens");
println!(" USD to JPY: 150.0");
}
/// Print daily breakdown
fn print_daily_breakdown(breakdown: &[DailyBreakdown]) {
println!("\n=== Daily Token Usage Breakdown ===");
println!();
for daily in breakdown {
println!("📅 {} (Records: {})", daily.date, daily.summary.record_count);
println!(" Tokens: {} input + {} output = {} total",
format_number(daily.summary.input_tokens),
format_number(daily.summary.output_tokens),
format_number(daily.summary.total_tokens));
println!(" Cost: ${:.4} USD (¥{:.0} JPY)",
daily.summary.total_cost_usd,
daily.summary.total_cost_jpy);
println!();
}
}
/// Format large numbers with commas
fn format_number(n: u64) -> String {
let s = n.to_string();
let mut result = String::new();
for (i, c) in s.chars().rev().enumerate() {
if i > 0 && i % 3 == 0 {
result.push(',');
}
result.push(c);
}
result.chars().rev().collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cost_calculation() {
let analyzer = TokenAnalyzer::new();
let records = vec![
TokenRecord {
timestamp: "2024-01-01T10:00:00Z".to_string(),
usage: Some(TokenUsage {
input_tokens: Some(1000),
output_tokens: Some(500),
total_tokens: Some(1500),
}),
model: Some("claude-3".to_string()),
conversation_id: Some("test".to_string()),
},
];
let summary = analyzer.calculate_costs(&records);
assert_eq!(summary.input_tokens, 1000);
assert_eq!(summary.output_tokens, 500);
assert_eq!(summary.total_tokens, 1500);
assert_eq!(summary.record_count, 1);
}
#[test]
fn test_date_extraction() {
let analyzer = TokenAnalyzer::new();
let result = analyzer.extract_date_jst("2024-01-01T10:00:00Z");
assert!(result.is_ok());
// Note: The exact date depends on JST conversion
}
}

423
src/transmission.rs Normal file
View File

@@ -0,0 +1,423 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use anyhow::{Result, Context};
use chrono::{DateTime, Utc};
use crate::config::Config;
use crate::persona::Persona;
use crate::relationship::{Relationship, RelationshipStatus};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransmissionLog {
pub user_id: String,
pub message: String,
pub timestamp: DateTime<Utc>,
pub transmission_type: TransmissionType,
pub success: bool,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransmissionType {
Autonomous, // AI decided to send
Scheduled, // Time-based trigger
Breakthrough, // Fortune breakthrough triggered
Maintenance, // Daily maintenance message
}
impl std::fmt::Display for TransmissionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TransmissionType::Autonomous => write!(f, "autonomous"),
TransmissionType::Scheduled => write!(f, "scheduled"),
TransmissionType::Breakthrough => write!(f, "breakthrough"),
TransmissionType::Maintenance => write!(f, "maintenance"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransmissionController {
config: Config,
transmission_history: Vec<TransmissionLog>,
last_check: Option<DateTime<Utc>>,
}
impl TransmissionController {
pub fn new(config: Config) -> Result<Self> {
let transmission_history = Self::load_transmission_history(&config)?;
Ok(TransmissionController {
config,
transmission_history,
last_check: None,
})
}
pub async fn check_autonomous_transmissions(&mut self, persona: &mut Persona) -> Result<Vec<TransmissionLog>> {
let mut transmissions = Vec::new();
let now = Utc::now();
// Get all transmission-eligible relationships
let eligible_user_ids: Vec<String> = {
let relationships = persona.list_all_relationships();
relationships.iter()
.filter(|(_, rel)| rel.transmission_enabled && !rel.is_broken)
.filter(|(_, rel)| rel.score >= rel.threshold)
.map(|(id, _)| id.clone())
.collect()
};
for user_id in eligible_user_ids {
// Get fresh relationship data for each check
if let Some(relationship) = persona.get_relationship(&user_id) {
// Check if enough time has passed since last transmission
if let Some(last_transmission) = relationship.last_transmission {
let hours_since_last = (now - last_transmission).num_hours();
if hours_since_last < 24 {
continue; // Skip if transmitted in last 24 hours
}
}
// Check if conditions are met for autonomous transmission
if self.should_transmit_to_user(&user_id, relationship, persona)? {
let transmission = self.generate_autonomous_transmission(persona, &user_id).await?;
transmissions.push(transmission);
}
}
}
self.last_check = Some(now);
self.save_transmission_history()?;
Ok(transmissions)
}
pub async fn check_breakthrough_transmissions(&mut self, persona: &mut Persona) -> Result<Vec<TransmissionLog>> {
let mut transmissions = Vec::new();
let state = persona.get_current_state()?;
// Only trigger breakthrough transmissions if fortune is very high
if !state.breakthrough_triggered || state.fortune_value < 9 {
return Ok(transmissions);
}
// Get close relationships for breakthrough sharing
let relationships = persona.list_all_relationships();
let close_friends: Vec<_> = relationships.iter()
.filter(|(_, rel)| matches!(rel.status, RelationshipStatus::Friend | RelationshipStatus::CloseFriend))
.filter(|(_, rel)| rel.transmission_enabled && !rel.is_broken)
.collect();
for (user_id, _relationship) in close_friends {
// Check if we haven't sent a breakthrough message today
let today = chrono::Utc::now().date_naive();
let already_sent_today = self.transmission_history.iter()
.any(|log| {
log.user_id == *user_id &&
matches!(log.transmission_type, TransmissionType::Breakthrough) &&
log.timestamp.date_naive() == today
});
if !already_sent_today {
let transmission = self.generate_breakthrough_transmission(persona, user_id).await?;
transmissions.push(transmission);
}
}
Ok(transmissions)
}
pub async fn check_maintenance_transmissions(&mut self, persona: &mut Persona) -> Result<Vec<TransmissionLog>> {
let mut transmissions = Vec::new();
let now = Utc::now();
// Only send maintenance messages once per day
let today = now.date_naive();
let already_sent_today = self.transmission_history.iter()
.any(|log| {
matches!(log.transmission_type, TransmissionType::Maintenance) &&
log.timestamp.date_naive() == today
});
if already_sent_today {
return Ok(transmissions);
}
// Apply daily maintenance to persona
persona.daily_maintenance()?;
// Get relationships that might need a maintenance check-in
let relationships = persona.list_all_relationships();
let maintenance_candidates: Vec<_> = relationships.iter()
.filter(|(_, rel)| rel.transmission_enabled && !rel.is_broken)
.filter(|(_, rel)| {
// Send maintenance to relationships that haven't been contacted in a while
if let Some(last_interaction) = rel.last_interaction {
let days_since = (now - last_interaction).num_days();
days_since >= 7 // Haven't talked in a week
} else {
false
}
})
.take(3) // Limit to 3 maintenance messages per day
.collect();
for (user_id, _) in maintenance_candidates {
let transmission = self.generate_maintenance_transmission(persona, user_id).await?;
transmissions.push(transmission);
}
Ok(transmissions)
}
fn should_transmit_to_user(&self, user_id: &str, relationship: &Relationship, persona: &Persona) -> Result<bool> {
// Basic transmission criteria
if !relationship.transmission_enabled || relationship.is_broken {
return Ok(false);
}
// Score must be above threshold
if relationship.score < relationship.threshold {
return Ok(false);
}
// Check transmission cooldown
if let Some(last_transmission) = relationship.last_transmission {
let hours_since = (Utc::now() - last_transmission).num_hours();
if hours_since < 24 {
return Ok(false);
}
}
// Calculate transmission probability based on relationship strength
let base_probability = match relationship.status {
RelationshipStatus::New => 0.1,
RelationshipStatus::Acquaintance => 0.2,
RelationshipStatus::Friend => 0.4,
RelationshipStatus::CloseFriend => 0.6,
RelationshipStatus::Broken => 0.0,
};
// Modify probability based on fortune
let state = persona.get_current_state()?;
let fortune_modifier = (state.fortune_value as f64 - 5.0) / 10.0; // -0.4 to +0.5
let final_probability = (base_probability + fortune_modifier).max(0.0).min(1.0);
// Simple random check (in real implementation, this would be more sophisticated)
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
user_id.hash(&mut hasher);
Utc::now().timestamp().hash(&mut hasher);
let hash = hasher.finish();
let random_value = (hash % 100) as f64 / 100.0;
Ok(random_value < final_probability)
}
async fn generate_autonomous_transmission(&mut self, persona: &mut Persona, user_id: &str) -> Result<TransmissionLog> {
let now = Utc::now();
// Get recent memories for context
let memories = persona.get_memories(user_id, 3);
let context = if !memories.is_empty() {
format!("Based on our recent conversations: {}", memories.join(", "))
} else {
"Starting a spontaneous conversation".to_string()
};
// Generate message using AI if available
let message = match self.generate_ai_message(persona, user_id, &context, TransmissionType::Autonomous).await {
Ok(msg) => msg,
Err(_) => {
// Fallback to simple messages
let fallback_messages = [
"Hey! How have you been?",
"Just thinking about our last conversation...",
"Hope you're having a good day!",
"Something interesting happened today and it reminded me of you.",
];
let index = (now.timestamp() as usize) % fallback_messages.len();
fallback_messages[index].to_string()
}
};
let log = TransmissionLog {
user_id: user_id.to_string(),
message,
timestamp: now,
transmission_type: TransmissionType::Autonomous,
success: true, // For now, assume success
error: None,
};
self.transmission_history.push(log.clone());
Ok(log)
}
async fn generate_breakthrough_transmission(&mut self, persona: &mut Persona, user_id: &str) -> Result<TransmissionLog> {
let now = Utc::now();
let state = persona.get_current_state()?;
let message = match self.generate_ai_message(persona, user_id, "Breakthrough moment - feeling inspired!", TransmissionType::Breakthrough).await {
Ok(msg) => msg,
Err(_) => {
format!("Amazing day today! ⚡ Fortune is at {}/10 and I'm feeling incredibly inspired. Had to share this energy with you!", state.fortune_value)
}
};
let log = TransmissionLog {
user_id: user_id.to_string(),
message,
timestamp: now,
transmission_type: TransmissionType::Breakthrough,
success: true,
error: None,
};
self.transmission_history.push(log.clone());
Ok(log)
}
async fn generate_maintenance_transmission(&mut self, persona: &mut Persona, user_id: &str) -> Result<TransmissionLog> {
let now = Utc::now();
let message = match self.generate_ai_message(persona, user_id, "Maintenance check-in", TransmissionType::Maintenance).await {
Ok(msg) => msg,
Err(_) => {
"Hey! It's been a while since we last talked. Just checking in to see how you're doing!".to_string()
}
};
let log = TransmissionLog {
user_id: user_id.to_string(),
message,
timestamp: now,
transmission_type: TransmissionType::Maintenance,
success: true,
error: None,
};
self.transmission_history.push(log.clone());
Ok(log)
}
async fn generate_ai_message(&self, _persona: &mut Persona, _user_id: &str, context: &str, transmission_type: TransmissionType) -> Result<String> {
// Try to use AI for message generation
let _system_prompt = format!(
"You are initiating a {} conversation. Context: {}. Keep the message casual, personal, and under 100 characters. Show genuine interest in the person.",
transmission_type, context
);
// This is a simplified version - in a real implementation, we'd use the AI provider
// For now, return an error to trigger fallback
Err(anyhow::anyhow!("AI provider not available for transmission generation"))
}
fn get_eligible_relationships(&self, persona: &Persona) -> Vec<String> {
persona.list_all_relationships().iter()
.filter(|(_, rel)| rel.transmission_enabled && !rel.is_broken)
.filter(|(_, rel)| rel.score >= rel.threshold)
.map(|(id, _)| id.clone())
.collect()
}
pub fn get_transmission_stats(&self) -> TransmissionStats {
let total_transmissions = self.transmission_history.len();
let successful_transmissions = self.transmission_history.iter()
.filter(|log| log.success)
.count();
let today = Utc::now().date_naive();
let today_transmissions = self.transmission_history.iter()
.filter(|log| log.timestamp.date_naive() == today)
.count();
let by_type = {
let mut counts = HashMap::new();
for log in &self.transmission_history {
*counts.entry(log.transmission_type.to_string()).or_insert(0) += 1;
}
counts
};
TransmissionStats {
total_transmissions,
successful_transmissions,
today_transmissions,
success_rate: if total_transmissions > 0 {
successful_transmissions as f64 / total_transmissions as f64
} else {
0.0
},
by_type,
}
}
pub fn get_recent_transmissions(&self, limit: usize) -> Vec<&TransmissionLog> {
let mut logs: Vec<_> = self.transmission_history.iter().collect();
logs.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));
logs.into_iter().take(limit).collect()
}
fn load_transmission_history(config: &Config) -> Result<Vec<TransmissionLog>> {
let file_path = config.transmission_file();
if !file_path.exists() {
return Ok(Vec::new());
}
let content = std::fs::read_to_string(file_path)
.context("Failed to read transmission history file")?;
let history: Vec<TransmissionLog> = serde_json::from_str(&content)
.context("Failed to parse transmission history file")?;
Ok(history)
}
fn save_transmission_history(&self) -> Result<()> {
let content = serde_json::to_string_pretty(&self.transmission_history)
.context("Failed to serialize transmission history")?;
std::fs::write(&self.config.transmission_file(), content)
.context("Failed to write transmission history file")?;
Ok(())
}
pub async fn check_and_send(&mut self) -> Result<Vec<(String, String)>> {
let config = self.config.clone();
let mut persona = Persona::new(&config)?;
let mut results = Vec::new();
// Check autonomous transmissions
let autonomous = self.check_autonomous_transmissions(&mut persona).await?;
for log in autonomous {
if log.success {
results.push((log.user_id, log.message));
}
}
// Check breakthrough transmissions
let breakthrough = self.check_breakthrough_transmissions(&mut persona).await?;
for log in breakthrough {
if log.success {
results.push((log.user_id, log.message));
}
}
Ok(results)
}
}
#[derive(Debug, Clone)]
pub struct TransmissionStats {
pub total_transmissions: usize,
pub successful_transmissions: usize,
pub today_transmissions: usize,
pub success_rate: f64,
pub by_type: HashMap<String, usize>,
}