Compare commits
3 Commits
main
...
0f18689539
Author | SHA1 | Date | |
---|---|---|---|
0f18689539
|
|||
ad7d9387dd
|
|||
1280394966
|
21
.claude/settings.local.json
Normal file
21
.claude/settings.local.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"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\")"
|
||||
],
|
||||
"deny": []
|
||||
}
|
||||
}
|
5
.env.example
Normal file
5
.env.example
Normal 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
|
30
.gitignore
vendored
30
.gitignore
vendored
@@ -1,24 +1,6 @@
|
||||
# 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
|
||||
|
7
.gitmodules
vendored
Normal file
7
.gitmodules
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
[submodule "shell"]
|
||||
path = shell
|
||||
url = git@git.syui.ai:ai/shell
|
||||
[submodule "card"]
|
||||
path = card
|
||||
url = git@git.syui.ai:ai/card
|
||||
branch = claude
|
48
Cargo.toml
48
Cargo.toml
@@ -1,48 +0,0 @@
|
||||
[package]
|
||||
name = "aigpt"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["syui"]
|
||||
description = "Simple memory storage for Claude with MCP"
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "aigpt"
|
||||
path = "src/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "memory-mcp"
|
||||
path = "src/bin/mcp_server.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "memory-mcp-extended"
|
||||
path = "src/bin/mcp_server_extended.rs"
|
||||
|
||||
[dependencies]
|
||||
# CLI and async
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
tokio = { version = "1.40", features = ["full"] }
|
||||
|
||||
# JSON and serialization
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
# Date/time and UUID
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
uuid = { version = "1.10", features = ["v4"] }
|
||||
|
||||
# Error handling and utilities
|
||||
anyhow = "1.0"
|
||||
dirs = "5.0"
|
||||
|
||||
# Extended features (optional)
|
||||
reqwest = { version = "0.11", features = ["json"], optional = true }
|
||||
scraper = { version = "0.18", optional = true }
|
||||
openai = { version = "1.1", optional = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
extended = ["semantic-search", "ai-analysis", "web-integration"]
|
||||
semantic-search = ["openai"]
|
||||
ai-analysis = ["openai"]
|
||||
web-integration = ["reqwest", "scraper"]
|
134
DEVELOPMENT_STATUS.md
Normal file
134
DEVELOPMENT_STATUS.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# ai.gpt 開発状況 (2025/01/06 更新)
|
||||
|
||||
## 現在の状態
|
||||
|
||||
### ✅ 実装済み機能
|
||||
|
||||
1. **基本システム**
|
||||
- 階層的記憶システム(完全ログ→要約→コア→忘却)
|
||||
- 不可逆的な関係性システム(broken状態は修復不可)
|
||||
- AI運勢による日々の人格変動
|
||||
- 時間減衰による自然な関係性変化
|
||||
|
||||
2. **CLI機能**
|
||||
- `chat` - AIとの会話(Ollama/OpenAI対応)
|
||||
- `status` - 状態確認
|
||||
- `fortune` - AI運勢確認
|
||||
- `relationships` - 関係一覧
|
||||
- `transmit` - 送信チェック(現在はprint出力)
|
||||
- `maintenance` - 日次メンテナンス
|
||||
- `config` - 設定管理
|
||||
- `schedule` - スケジューラー管理
|
||||
- `server` - MCP Server起動
|
||||
- `shell` - インタラクティブシェル(ai.shell統合)
|
||||
|
||||
3. **データ管理**
|
||||
- 保存場所: `~/.config/aigpt/`
|
||||
- 設定: `config.json`
|
||||
- データ: `data/` ディレクトリ内の各種JSONファイル
|
||||
|
||||
4. **スケジューラー**
|
||||
- Cron形式とインターバル形式対応
|
||||
- 5種類のタスクタイプ実装済み
|
||||
- バックグラウンド実行可能
|
||||
|
||||
5. **MCP Server**
|
||||
- 14種類のツールを公開(ai.gpt: 9種類、ai.shell: 5種類)
|
||||
- Claude Desktopなどから利用可能
|
||||
- ai.card統合オプション(--enable-card)
|
||||
|
||||
6. **ai.shell統合**
|
||||
- インタラクティブシェルモード
|
||||
- シェルコマンド実行(!command形式)
|
||||
- AIコマンド(analyze, generate, explain)
|
||||
- aishell.md読み込み機能
|
||||
- 高度な補完機能(prompt-toolkit)
|
||||
|
||||
## 🚧 未実装・今後の課題
|
||||
|
||||
### 短期的課題
|
||||
|
||||
1. **自律送信の実装**
|
||||
- 現在: コンソールにprint出力
|
||||
- TODO: atproto (Bluesky) への実際の投稿機能
|
||||
- 参考: ai.bot (Rust/seahorse) との連携も検討
|
||||
|
||||
2. **テストの追加**
|
||||
- 単体テスト
|
||||
- 統合テスト
|
||||
- CI/CDパイプライン
|
||||
|
||||
3. **エラーハンドリングの改善**
|
||||
- より詳細なエラーメッセージ
|
||||
- リトライ機構
|
||||
|
||||
### 中期的課題
|
||||
|
||||
1. **ai.botとの連携**
|
||||
- Rust側のAPIエンドポイント作成
|
||||
- 送信機能の委譲
|
||||
|
||||
2. **より高度な記憶要約**
|
||||
- 現在: シンプルな要約
|
||||
- TODO: AIによる意味的な要約
|
||||
|
||||
3. **Webダッシュボード**
|
||||
- 関係性の可視化
|
||||
- 記憶の管理UI
|
||||
|
||||
### 長期的課題
|
||||
|
||||
1. **他のsyuiプロジェクトとの統合**
|
||||
- ai.card: カードゲームとの連携
|
||||
- ai.verse: メタバース内でのNPC人格
|
||||
- ai.os: システムレベルでの統合
|
||||
|
||||
2. **分散化**
|
||||
- atproto上でのデータ保存
|
||||
- ユーザーデータ主権の完全実現
|
||||
|
||||
## 次回開発時のエントリーポイント
|
||||
|
||||
### 1. 自律送信を実装する場合
|
||||
```python
|
||||
# src/aigpt/transmission.py を編集
|
||||
# atproto-python ライブラリを追加
|
||||
# _handle_transmission_check() メソッドを更新
|
||||
```
|
||||
|
||||
### 2. ai.botと連携する場合
|
||||
```python
|
||||
# 新規ファイル: src/aigpt/bot_connector.py
|
||||
# ai.botのAPIエンドポイントにHTTPリクエスト
|
||||
```
|
||||
|
||||
### 3. テストを追加する場合
|
||||
```bash
|
||||
# tests/ディレクトリを作成
|
||||
# pytest設定を追加
|
||||
```
|
||||
|
||||
### 4. ai.shellの問題を修正する場合
|
||||
```python
|
||||
# src/aigpt/cli.py の shell コマンド
|
||||
# prompt-toolkitのターミナル検出問題を回避
|
||||
# 代替: simple input() または click.prompt()
|
||||
```
|
||||
|
||||
## 設計思想の要点(AI向け)
|
||||
|
||||
1. **唯一性(yui system)**: 各ユーザーとAIの関係は1:1で、改変不可能
|
||||
2. **不可逆性**: 関係性の破壊は修復不可能(現実の人間関係と同じ)
|
||||
3. **階層的記憶**: ただのログではなく、要約・コア判定・忘却のプロセス
|
||||
4. **環境影響**: AI運勢による日々の人格変動(固定的でない)
|
||||
5. **段階的実装**: まずCLI print → atproto投稿 → ai.bot連携
|
||||
|
||||
## 現在のコードベースの理解
|
||||
|
||||
- **言語**: Python (typer CLI, fastapi_mcp)
|
||||
- **AI統合**: Ollama (ローカル) / OpenAI API
|
||||
- **データ形式**: JSON(将来的にSQLite検討)
|
||||
- **認証**: atproto DID(未実装だが設計済み)
|
||||
- **統合**: ai.shell(Rust版から移行)、ai.card(MCP連携)
|
||||
|
||||
このファイルを参照することで、次回の開発がスムーズに始められます。
|
334
README.md
334
README.md
@@ -1,177 +1,255 @@
|
||||
# aigpt - Claude Memory MCP Server
|
||||
# ai.gpt - 自律的送信AI
|
||||
|
||||
ChatGPTのメモリ機能を参考にした、Claude Desktop/Code用のシンプルなメモリストレージシステムです。
|
||||
存在子理論に基づく、関係性によって自発的にメッセージを送信するAIシステム。
|
||||
|
||||
## 機能
|
||||
## 中核概念
|
||||
|
||||
- **メモリのCRUD操作**: メモリの作成、更新、削除、検索
|
||||
- **ChatGPT JSONインポート**: ChatGPTの会話履歴からメモリを抽出
|
||||
- **stdio MCP実装**: Claude Desktop/Codeとの簡潔な連携
|
||||
- **JSONファイル保存**: シンプルなファイルベースのデータ保存
|
||||
- **唯一性**: atproto DIDと1:1で紐付き、改変不可能な人格
|
||||
- **不可逆性**: 関係性が壊れたら修復不可能(現実の人間関係と同じ)
|
||||
- **記憶の階層**: 完全ログ→AI要約→コア判定→選択的忘却
|
||||
- **AI運勢**: 1-10のランダム値による日々の人格変動
|
||||
|
||||
## インストール
|
||||
|
||||
1. Rustをインストール(まだの場合):
|
||||
```bash
|
||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
|
||||
# Python仮想環境を推奨
|
||||
python -m venv venv
|
||||
source venv/bin/activate # Windows: venv\Scripts\activate
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
2. プロジェクトをビルド:
|
||||
## 設定
|
||||
|
||||
### APIキーの設定
|
||||
```bash
|
||||
cargo build --release
|
||||
# OpenAI APIキー
|
||||
aigpt config set providers.openai.api_key sk-xxxxx
|
||||
|
||||
# atproto認証情報(将来の自動投稿用)
|
||||
aigpt config set atproto.handle your.handle
|
||||
aigpt config set atproto.password your-password
|
||||
|
||||
# 設定一覧を確認
|
||||
aigpt config list
|
||||
```
|
||||
|
||||
3. バイナリをパスの通った場所にコピー(オプション):
|
||||
### データ保存場所
|
||||
- 設定: `~/.config/syui/ai/gpt/config.json`
|
||||
- データ: `~/.config/syui/ai/gpt/data/`
|
||||
|
||||
## 使い方
|
||||
|
||||
### 会話する
|
||||
```bash
|
||||
cp target/release/aigpt $HOME/.cargo/bin/
|
||||
aigpt chat "did:plc:xxxxx" "こんにちは、今日はどんな気分?"
|
||||
```
|
||||
|
||||
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
|
||||
# AI全体の状態
|
||||
aigpt status
|
||||
|
||||
# 特定ユーザーとの関係
|
||||
aigpt status "did:plc:xxxxx"
|
||||
```
|
||||
|
||||
### MCPサーバーとして起動
|
||||
### 今日の運勢
|
||||
```bash
|
||||
# MCPサーバー起動 (どちらでも可)
|
||||
aigpt server
|
||||
aigpt serve
|
||||
aigpt fortune
|
||||
```
|
||||
|
||||
### ChatGPT会話のインポート
|
||||
### 自律送信チェック
|
||||
```bash
|
||||
# ChatGPT conversations.jsonをインポート
|
||||
aigpt import path/to/conversations.json
|
||||
# ドライラン(確認のみ)
|
||||
aigpt transmit
|
||||
|
||||
# 実行
|
||||
aigpt transmit --execute
|
||||
```
|
||||
|
||||
## 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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
### 日次メンテナンス
|
||||
```bash
|
||||
aigpt maintenance
|
||||
```
|
||||
|
||||
## 提供するMCPツール一覧
|
||||
|
||||
1. **create_memory** - 新しいメモリを作成
|
||||
2. **update_memory** - 既存のメモリを更新
|
||||
3. **delete_memory** - メモリを削除
|
||||
4. **search_memories** - メモリを検索
|
||||
5. **list_conversations** - インポートされた会話を一覧表示
|
||||
|
||||
## ツールの使用例
|
||||
|
||||
Claude Desktop/Codeで以下のように使用します:
|
||||
|
||||
### メモリの作成
|
||||
```
|
||||
MCPツールを使って「今日は良い天気です」というメモリーを作成してください
|
||||
### 関係一覧
|
||||
```bash
|
||||
aigpt relationships
|
||||
```
|
||||
|
||||
### メモリの検索
|
||||
```
|
||||
MCPツールを使って「天気」に関するメモリーを検索してください
|
||||
```
|
||||
## データ構造
|
||||
|
||||
### 会話一覧の表示
|
||||
```
|
||||
MCPツールを使ってインポートした会話の一覧を表示してください
|
||||
```
|
||||
デフォルトでは `~/.config/syui/ai/gpt/` に以下のファイルが保存されます:
|
||||
|
||||
## データ保存
|
||||
- `memories.json` - 会話記憶
|
||||
- `conversations.json` - 会話ログ
|
||||
- `relationships.json` - 関係性パラメータ
|
||||
- `fortunes.json` - AI運勢履歴
|
||||
- `transmissions.json` - 送信履歴
|
||||
- `persona_state.json` - 人格状態
|
||||
|
||||
- デフォルトパス: `~/.config/syui/ai/gpt/memory.json`
|
||||
- JSONファイルでデータを保存
|
||||
- 自動的にディレクトリとファイルを作成
|
||||
## 関係性の仕組み
|
||||
|
||||
### データ構造
|
||||
- スコア0-200の範囲で変動
|
||||
- 100を超えると送信機能が解禁
|
||||
- 時間経過で自然減衰
|
||||
- 大きなネガティブな相互作用で破壊される可能性
|
||||
|
||||
```json
|
||||
{
|
||||
"memories": {
|
||||
"uuid": {
|
||||
"id": "uuid",
|
||||
"content": "メモリーの内容",
|
||||
"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
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
## ai.shell統合
|
||||
|
||||
## 開発
|
||||
インタラクティブシェルモード(Claude Code風の体験):
|
||||
|
||||
```bash
|
||||
# 開発モードで実行
|
||||
cargo run -- server
|
||||
aigpt shell
|
||||
|
||||
# ChatGPTインポートのテスト
|
||||
cargo run -- import json/conversations.json
|
||||
# シェル内で使えるコマンド:
|
||||
# help - コマンド一覧
|
||||
# !<command> - シェルコマンド実行(例: !ls, !pwd)
|
||||
# analyze <file> - ファイルをAIで分析
|
||||
# generate <desc> - コード生成
|
||||
# explain <topic> - 概念の説明
|
||||
# load - aishell.mdプロジェクトファイルを読み込み
|
||||
# status - AI状態確認
|
||||
# fortune - AI運勢確認
|
||||
# clear - 画面クリア
|
||||
# exit/quit - 終了
|
||||
|
||||
# テストの実行
|
||||
cargo test
|
||||
|
||||
# フォーマット
|
||||
cargo fmt
|
||||
|
||||
# Lintチェック
|
||||
cargo clippy
|
||||
# 通常のメッセージも送れます
|
||||
ai.shell> こんにちは、今日は何をしましょうか?
|
||||
```
|
||||
|
||||
## トラブルシューティング
|
||||
## MCP Server
|
||||
|
||||
### MCPサーバーが起動しない
|
||||
### サーバー起動
|
||||
```bash
|
||||
# バイナリが存在するか確認
|
||||
ls -la ~/.cargo/bin/aigpt
|
||||
# Ollamaを使用(デフォルト)
|
||||
aigpt server --model qwen2.5 --provider ollama
|
||||
|
||||
# 手動でテスト
|
||||
echo '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}' | aigpt server
|
||||
# OpenAIを使用
|
||||
aigpt server --model gpt-4o-mini --provider openai
|
||||
|
||||
# カスタムポート
|
||||
aigpt server --port 8080
|
||||
|
||||
# ai.card統合を有効化
|
||||
aigpt server --enable-card
|
||||
```
|
||||
|
||||
### Claude Desktopでツールが見つからない
|
||||
1. Claude Desktopを完全に再起動
|
||||
2. 設定ファイルのパスが正しいか確認
|
||||
3. ログファイルを確認: `~/Library/Logs/Claude/mcp-server-aigpt.log`
|
||||
|
||||
### インポートが失敗する
|
||||
### AIプロバイダーを使った会話
|
||||
```bash
|
||||
# JSONファイルの形式を確認
|
||||
head -100 conversations.json | jq '.[0] | keys'
|
||||
# Ollamaで会話
|
||||
aigpt chat "did:plc:xxxxx" "こんにちは" --provider ollama --model qwen2.5
|
||||
|
||||
# OpenAIで会話
|
||||
aigpt chat "did:plc:xxxxx" "今日の調子はどう?" --provider openai --model gpt-4o-mini
|
||||
```
|
||||
|
||||
## ライセンス
|
||||
### MCP Tools
|
||||
|
||||
MIT
|
||||
サーバーが起動すると、以下のツールがAIから利用可能になります:
|
||||
|
||||
**ai.gpt ツール:**
|
||||
- `get_memories` - アクティブな記憶を取得
|
||||
- `get_relationship` - 特定ユーザーとの関係を取得
|
||||
- `get_all_relationships` - すべての関係を取得
|
||||
- `get_persona_state` - 現在の人格状態を取得
|
||||
- `process_interaction` - ユーザーとの対話を処理
|
||||
- `check_transmission_eligibility` - 送信可能かチェック
|
||||
- `get_fortune` - 今日の運勢を取得
|
||||
- `summarize_memories` - 記憶を要約
|
||||
- `run_maintenance` - メンテナンス実行
|
||||
|
||||
**ai.shell ツール:**
|
||||
- `execute_command` - シェルコマンド実行
|
||||
- `analyze_file` - ファイルのAI分析
|
||||
- `write_file` - ファイル書き込み
|
||||
- `read_project_file` - プロジェクトファイル読み込み
|
||||
- `list_files` - ファイル一覧
|
||||
|
||||
**ai.card ツール(--enable-card時):**
|
||||
- `get_user_cards` - ユーザーのカード取得
|
||||
- `draw_card` - カードを引く(ガチャ)
|
||||
- `get_card_details` - カード詳細情報
|
||||
- `sync_cards_atproto` - atproto同期
|
||||
- `analyze_card_collection` - コレクション分析
|
||||
|
||||
## 環境変数
|
||||
|
||||
`.env`ファイルを作成して設定:
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
# OpenAI APIキーを設定
|
||||
```
|
||||
|
||||
## スケジューラー機能
|
||||
|
||||
### タスクの追加
|
||||
|
||||
```bash
|
||||
# 6時間ごとに送信チェック
|
||||
aigpt schedule add transmission_check "0 */6 * * *" --provider ollama --model qwen2.5
|
||||
|
||||
# 30分ごとに送信チェック(インターバル形式)
|
||||
aigpt schedule add transmission_check "30m"
|
||||
|
||||
# 毎日午前3時にメンテナンス
|
||||
aigpt schedule add maintenance "0 3 * * *"
|
||||
|
||||
# 1時間ごとに関係性減衰
|
||||
aigpt schedule add relationship_decay "1h"
|
||||
|
||||
# 毎週月曜日に記憶要約
|
||||
aigpt schedule add memory_summary "0 0 * * MON"
|
||||
```
|
||||
|
||||
### タスク管理
|
||||
|
||||
```bash
|
||||
# タスク一覧
|
||||
aigpt schedule list
|
||||
|
||||
# タスクを無効化
|
||||
aigpt schedule disable --task-id transmission_check_1234567890
|
||||
|
||||
# タスクを有効化
|
||||
aigpt schedule enable --task-id transmission_check_1234567890
|
||||
|
||||
# タスクを削除
|
||||
aigpt schedule remove --task-id transmission_check_1234567890
|
||||
```
|
||||
|
||||
### スケジューラーデーモンの起動
|
||||
|
||||
```bash
|
||||
# バックグラウンドでスケジューラーを実行
|
||||
aigpt schedule run
|
||||
```
|
||||
|
||||
### スケジュール形式
|
||||
|
||||
**Cron形式**:
|
||||
- `"0 */6 * * *"` - 6時間ごと
|
||||
- `"0 0 * * *"` - 毎日午前0時
|
||||
- `"*/5 * * * *"` - 5分ごと
|
||||
|
||||
**インターバル形式**:
|
||||
- `"30s"` - 30秒ごと
|
||||
- `"5m"` - 5分ごと
|
||||
- `"2h"` - 2時間ごと
|
||||
- `"1d"` - 1日ごと
|
||||
|
||||
### タスクタイプ
|
||||
|
||||
- `transmission_check` - 送信可能なユーザーをチェックして自動送信
|
||||
- `maintenance` - 日次メンテナンス(忘却、コア記憶判定など)
|
||||
- `fortune_update` - AI運勢の更新
|
||||
- `relationship_decay` - 関係性の時間減衰
|
||||
- `memory_summary` - 記憶の要約作成
|
||||
|
||||
## 次のステップ
|
||||
|
||||
- atprotoへの実送信機能実装
|
||||
- systemdサービス化
|
||||
- Docker対応
|
||||
- Webダッシュボード
|
63
aishell.md
Normal file
63
aishell.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# ai.shell プロジェクト仕様書
|
||||
|
||||
## 概要
|
||||
ai.shellは、AIを活用したインタラクティブなシェル環境です。Claude Codeのような体験を提供し、プロジェクトの目標と仕様をAIが理解して、開発を支援します。
|
||||
|
||||
## 主要機能
|
||||
|
||||
### 1. インタラクティブシェル
|
||||
- AIとの対話型インターフェース
|
||||
- シェルコマンドの実行(!command形式)
|
||||
- 高度な補完機能
|
||||
- コマンド履歴
|
||||
|
||||
### 2. AI支援機能
|
||||
- **analyze <file>**: ファイルの分析
|
||||
- **generate <description>**: コード生成
|
||||
- **explain <topic>**: 概念の説明
|
||||
- **load**: プロジェクト仕様(このファイル)の読み込み
|
||||
|
||||
### 3. ai.gpt統合
|
||||
- 関係性ベースのAI人格
|
||||
- 記憶システム
|
||||
- 運勢システムによる応答の変化
|
||||
|
||||
## 使用方法
|
||||
|
||||
```bash
|
||||
# ai.shellを起動
|
||||
aigpt shell
|
||||
|
||||
# プロジェクト仕様を読み込み
|
||||
ai.shell> load
|
||||
|
||||
# ファイルを分析
|
||||
ai.shell> analyze src/main.py
|
||||
|
||||
# コードを生成
|
||||
ai.shell> generate Python function to calculate fibonacci
|
||||
|
||||
# シェルコマンドを実行
|
||||
ai.shell> !ls -la
|
||||
|
||||
# AIと対話
|
||||
ai.shell> How can I improve this code?
|
||||
```
|
||||
|
||||
## 技術スタック
|
||||
- Python 3.10+
|
||||
- prompt-toolkit(補完機能)
|
||||
- fastapi-mcp(MCP統合)
|
||||
- ai.gpt(人格・記憶システム)
|
||||
|
||||
## 開発目標
|
||||
1. Claude Codeのような自然な開発体験
|
||||
2. AIがプロジェクトコンテキストを理解
|
||||
3. シェルコマンドとAIの seamless な統合
|
||||
4. 開発者の生産性向上
|
||||
|
||||
## 今後の展開
|
||||
- ai.cardとの統合(カードゲームMCPサーバー)
|
||||
- より高度なプロジェクト理解機能
|
||||
- 自動コード修正・リファクタリング
|
||||
- テスト生成・実行
|
1
card
Submodule
1
card
Submodule
Submodule card added at 6dbe630b9d
371
claude.md
371
claude.md
@@ -1,95 +1,326 @@
|
||||
# claude用の記憶装置を作る
|
||||
# エコシステム統合設計書
|
||||
|
||||
claude desktop, claude codeで使用できるmemory機能をmcpで作ります。
|
||||
## 中核思想
|
||||
- **存在子理論**: この世界で最も小さいもの(存在子/ai)の探求
|
||||
- **唯一性原則**: 現実の個人の唯一性をすべてのシステムで担保
|
||||
- **現実の反映**: 現実→ゲーム→現実の循環的影響
|
||||
|
||||
1. chatgptのメモリ機能を参考に
|
||||
2. chatgptのjsonをimportできる @json/
|
||||
3. rustで作る
|
||||
## システム構成図
|
||||
|
||||
```
|
||||
存在子(ai) - 最小単位の意識
|
||||
↓
|
||||
[ai.moji] 文字システム
|
||||
↓
|
||||
[ai.os] + [ai.game device] ← 統合ハードウェア
|
||||
├── ai.shell (Claude Code的機能)
|
||||
├── ai.gpt (自律人格・記憶システム)
|
||||
├── ai.ai (個人特化AI・心を読み取るAI)
|
||||
├── ai.card (カードゲーム・iOS/Web/API)
|
||||
└── ai.bot (分散SNS連携・カード配布)
|
||||
↓
|
||||
[ai.verse] メタバース
|
||||
├── world system (惑星型3D世界)
|
||||
├── at system (atproto/分散SNS)
|
||||
├── yui system (唯一性担保)
|
||||
└── ai system (存在属性)
|
||||
```
|
||||
|
||||
## 名前規則
|
||||
|
||||
名前規則は他のprojectと全て共通しています。exampleを示しますので、このルールに従ってください。
|
||||
|
||||
ここでは`ai.os`の場合の名前規則の例を記述します。
|
||||
|
||||
name: ai.os
|
||||
|
||||
**[ "package", "code", "command" ]**: aios
|
||||
**[ "dir", "url" ]**: ai/os
|
||||
**[ "domain", "json" ]**: ai.os
|
||||
|
||||
```sh
|
||||
$ curl -sL https://git.syui.ai/ai/ai/raw/branch/main/ai.json|jq .ai.os
|
||||
{ "type": "os" }
|
||||
```
|
||||
|
||||
## 自動実行設定
|
||||
```json
|
||||
{
|
||||
"mcp_auto_execute": true,
|
||||
"memory_auto_save": true,
|
||||
"memory_auto_search": true,
|
||||
"trigger_sensitivity": "high"
|
||||
"ai": {
|
||||
"os":{}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 自動メモリー保存のルール(強化版)
|
||||
他のprojectも同じ名前規則を採用します。`ai.gpt`ならpackageは`aigpt`です。
|
||||
|
||||
**即座に自動実行** - 以下の情報が会話に現れた瞬間、create_memory MCPツールを自動実行:
|
||||
## config(設定ファイル, env, 環境依存)
|
||||
|
||||
### 1. **ユーザーの個人情報**
|
||||
**トリガーワード**: 名前、誕生日、住所、年齢、職業、家族、出身
|
||||
- 基本情報: 名前、誕生日、住所、年齢、性別
|
||||
- 関係性: 家族構成、恋人、友人関係
|
||||
- 好み: 好きなもの、嫌いなもの、趣味、音楽、映画、本
|
||||
- 習慣: 日課、スケジュール、ルーティン
|
||||
- 価値観: 信念、考え方、人生観
|
||||
`config`を置く場所は統一されており、各projectの名前規則の`dir`項目を使用します。例えば、aiosの場合は`~/.config/syui/ai/os/`以下となります。pythonなどを使用する場合、`python -m venv`などでこのpackage config dirに環境を構築して実行するようにしてください。
|
||||
|
||||
### 2. **重要な決定事項**
|
||||
**トリガーワード**: 決めた、決定、方針、計画、予定、目標
|
||||
- プロジェクト方針の決定
|
||||
- 技術選択の決定
|
||||
- 設定・環境の変更
|
||||
- 今後のロードマップ
|
||||
- 作業分担・役割
|
||||
domain形式を採用して、私は各projectを`git.syui.ai/ai`にhostしていますから、`~/.config/syui/ai`とします。
|
||||
|
||||
### 3. **技術的な解決策**
|
||||
**トリガーワード**: 解決、修正、対処、設定、インストール、手順
|
||||
- エラーの解決方法
|
||||
- 有用なコマンド・スクリプト
|
||||
- 設定手順・インストール方法
|
||||
- デバッグテクニック
|
||||
- 最適化手法
|
||||
```sh
|
||||
[syui.ai]
|
||||
syui/ai
|
||||
```
|
||||
|
||||
### 4. **学習・発見事項**
|
||||
**トリガーワード**: 学んだ、わかった、発見、理解、気づき
|
||||
- 新しい知識・概念の理解
|
||||
- ツール・ライブラリの使い方
|
||||
- ベストプラクティス
|
||||
- 失敗から得た教訓
|
||||
```sh
|
||||
# example
|
||||
~/.config/syui/ai
|
||||
├── card
|
||||
├── gpt
|
||||
├── os
|
||||
└── shell
|
||||
```
|
||||
|
||||
## 自動メモリー検索のルール(強化版)
|
||||
## 各システム詳細
|
||||
|
||||
**会話開始時に自動実行** - search_memories を実行してコンテキストを取得
|
||||
### ai.gpt - 自律的送信AI
|
||||
**目的**: 関係性に基づく自発的コミュニケーション
|
||||
|
||||
**即座に自動実行** - 以下の場合、search_memories MCPツールを自動実行:
|
||||
**中核概念**:
|
||||
- **人格**: 記憶(過去の発話)と関係性パラメータで構成
|
||||
- **唯一性**: atproto accountとの1:1紐付け、改変不可能
|
||||
- **自律送信**: 関係性が閾値を超えると送信機能が解禁
|
||||
|
||||
### 1. **過去参照キーワード検出**
|
||||
**トリガーワード**: 前に、以前、昔、過去、先ほど、さっき、この間
|
||||
- 「前に話した〜」
|
||||
- 「以前設定した〜」
|
||||
- 「昔やった〜」
|
||||
**技術構成**:
|
||||
- `MemoryManager`: 完全ログ→AI要約→コア判定→選択的忘却
|
||||
- `RelationshipTracker`: 時間減衰・日次制限付き関係性スコア
|
||||
- `TransmissionController`: 閾値判定・送信トリガー
|
||||
- `Persona`: AI運勢(1-10ランダム)による人格変動
|
||||
|
||||
### 2. **記憶呼び出しキーワード**
|
||||
**トリガーワード**: 覚えている、記録、メモ、保存、履歴
|
||||
- 「覚えていますか?」
|
||||
- 「記録していた〜」
|
||||
- 「メモした〜」
|
||||
**実装仕様**:
|
||||
```
|
||||
- 言語: Python (fastapi_mcp)
|
||||
- ストレージ: JSON/SQLite選択式
|
||||
- インターフェース: Python CLI (click/typer)
|
||||
- スケジューリング: cron-like自律処理
|
||||
```
|
||||
|
||||
### 3. **設定・好み確認**
|
||||
**トリガーワード**: 好み、設定、環境、構成、preferences
|
||||
- ユーザーの好みを確認する必要がある場合
|
||||
- 過去の設定を参照する必要がある場合
|
||||
- 環境構成を確認する必要がある場合
|
||||
### ai.card - カードゲームシステム
|
||||
**目的**: atproto基盤でのユーザーデータ主権カードゲーム
|
||||
|
||||
### 4. **不明な参照**
|
||||
- ユーザーが具体的でない参照をした場合
|
||||
- 「あれ」「それ」「例のやつ」などの曖昧な表現
|
||||
- 文脈から過去の情報が必要と判断される場合
|
||||
**現在の状況**:
|
||||
- ai.botの機能として実装済み
|
||||
- atproto accountでmentionすると1日1回カードを取得
|
||||
- ai.api (MCP server予定) でユーザー管理
|
||||
|
||||
## 自動実行タイミング
|
||||
**移行計画**:
|
||||
- **iOS移植**: Claudeが担当予定
|
||||
- **データ保存**: atproto collection recordに保存(ユーザーがデータを所有)
|
||||
- **不正防止**: OAuth 2.1 scope (実装待ち) + MCP serverで対応
|
||||
- **画像ファイル**: Cloudflare Pagesが最適
|
||||
|
||||
1. **会話開始時**: search_memories を実行してコンテキスト取得
|
||||
2. **リアルタイム**: トリガーワード検出後、即座にMCPツール実行
|
||||
3. **会話終了時**: 重要な情報があれば create_memory で保存
|
||||
4. **定期的**: 長い会話では中間地点でメモリー整理
|
||||
**yui system適用**:
|
||||
- カードの効果がアカウント固有
|
||||
- 改ざん防止によるゲームバランス維持
|
||||
- 将来的にai.verseとの統合で固有スキルと連動
|
||||
|
||||
## エラーハンドリング
|
||||
### ai.ai - 心を読み取るAI
|
||||
**目的**: 個人特化型AI・深層理解システム
|
||||
|
||||
- MCPツールが利用できない場合は通常の会話を継続
|
||||
- メモリー保存失敗時はユーザーに通知
|
||||
- 検索結果が空の場合も適切に対応
|
||||
**ai.gptとの関係**:
|
||||
- ai.gpt → ai.ai: 自律送信AIから心理分析AIへの連携
|
||||
- 関係性パラメータの深層分析
|
||||
- ユーザーの思想コア部分の特定支援
|
||||
|
||||
### ai.verse - UEメタバース
|
||||
**目的**: 現実反映型3D世界
|
||||
|
||||
**yui system実装**:
|
||||
- キャラクター ↔ プレイヤー 1:1紐付け
|
||||
- unique skill: そのプレイヤーのみ使用可能
|
||||
- 他プレイヤーは同キャラでも同スキル使用不可
|
||||
|
||||
**統合要素**:
|
||||
- ai.card: ゲーム内アイテムとしてのカード
|
||||
- ai.gpt: NPCとしての自律AI人格
|
||||
- atproto: ゲーム内プロフィール連携
|
||||
|
||||
## データフロー設計
|
||||
|
||||
### 唯一性担保の実装
|
||||
```
|
||||
現実の個人 → atproto account (DID) → ゲーム内avatar → 固有スキル
|
||||
↑_______________________________| (現実の反映)
|
||||
```
|
||||
|
||||
### AI駆動変換システム
|
||||
```
|
||||
遊び・創作活動 → ai.gpt分析 → 業務成果変換 → 企業価値創出
|
||||
↑________________________| (Play-to-Work)
|
||||
```
|
||||
|
||||
### カードゲーム・データ主権フロー
|
||||
```
|
||||
ユーザー → ai.bot mention → カード生成 → atproto collection → ユーザー所有
|
||||
↑ ↓
|
||||
← iOS app表示 ← ai.card API ←
|
||||
```
|
||||
|
||||
## 技術スタック統合
|
||||
|
||||
### Core Infrastructure
|
||||
- **OS**: Rust-based ai.os (Arch Linux base)
|
||||
- **Container**: Docker image distribution
|
||||
- **Identity**: atproto selfhost server + DID管理
|
||||
- **AI**: fastapi_mcp server architecture
|
||||
- **CLI**: Python unified (click/typer) - Rustから移行
|
||||
|
||||
### Game Engine Integration
|
||||
- **Engine**: Unreal Engine (Blueprint)
|
||||
- **Data**: atproto → UE → atproto sync
|
||||
- **Avatar**: 分散SNS profile → 3D character
|
||||
- **Streaming**: game screen = broadcast screen
|
||||
|
||||
### Mobile/Device
|
||||
- **iOS**: ai.card移植 (Claude担当)
|
||||
- **Hardware**: ai.game device (future)
|
||||
- **Interface**: controller-first design
|
||||
|
||||
## 実装優先順位
|
||||
|
||||
### Phase 1: AI基盤強化 (現在進行)
|
||||
- [ ] ai.gpt memory system完全実装
|
||||
- 記憶の階層化(完全ログ→要約→コア→忘却)
|
||||
- 関係性パラメータの時間減衰システム
|
||||
- AI運勢による人格変動機能
|
||||
- [ ] ai.card iOS移植
|
||||
- atproto collection record連携
|
||||
- MCP server化(ai.api刷新)
|
||||
- [ ] fastapi_mcp統一基盤構築
|
||||
|
||||
### Phase 2: ゲーム統合
|
||||
- [ ] ai.verse yui system実装
|
||||
- unique skill機能
|
||||
- atproto連携強化
|
||||
- [ ] ai.gpt ↔ ai.ai連携機能
|
||||
- [ ] 分散SNS ↔ ゲーム同期
|
||||
|
||||
### Phase 3: メタバース浸透
|
||||
- [ ] VTuber配信機能統合
|
||||
- [ ] Play-to-Work変換システム
|
||||
- [ ] ai.game device prototype
|
||||
|
||||
## 将来的な連携構想
|
||||
|
||||
### システム間連携(現在は独立実装)
|
||||
```
|
||||
ai.gpt (自律送信) ←→ ai.ai (心理分析)
|
||||
ai.card (iOS,Web,API) ←→ ai.verse (UEゲーム世界)
|
||||
```
|
||||
|
||||
**共通基盤**: fastapi_mcp
|
||||
**共通思想**: yui system(現実の反映・唯一性担保)
|
||||
|
||||
### データ改ざん防止戦略
|
||||
- **短期**: MCP serverによる検証
|
||||
- **中期**: OAuth 2.1 scope実装待ち
|
||||
- **長期**: ブロックチェーン的整合性チェック
|
||||
|
||||
## AIコミュニケーション最適化
|
||||
|
||||
### プロジェクト要件定義テンプレート
|
||||
```markdown
|
||||
# [プロジェクト名] 要件定義
|
||||
|
||||
## 哲学的背景
|
||||
- 存在子理論との関連:
|
||||
- yui system適用範囲:
|
||||
- 現実反映の仕組み:
|
||||
|
||||
## 技術要件
|
||||
- 使用技術(fastapi_mcp統一):
|
||||
- atproto連携方法:
|
||||
- データ永続化方法:
|
||||
|
||||
## ユーザーストーリー
|
||||
1. ユーザーが...すると
|
||||
2. システムが...を実行し
|
||||
3. 結果として...が実現される
|
||||
|
||||
## 成功指標
|
||||
- 技術的:
|
||||
- 哲学的(唯一性担保):
|
||||
```
|
||||
|
||||
### Claude Code活用戦略
|
||||
1. **小さく始める**: ai.gptのMCP機能拡張から
|
||||
2. **段階的統合**: 各システムを個別に完成させてから統合
|
||||
3. **哲学的一貫性**: 各実装でyui systemとの整合性を確認
|
||||
4. **現実反映**: 実装がどう現実とゲームを繋ぐかを常に明記
|
||||
|
||||
## 開発上の留意点
|
||||
|
||||
### MCP Server設計指針
|
||||
- 各AI(gpt, card, ai, bot)は独立したMCPサーバー
|
||||
- fastapi_mcp基盤で統一
|
||||
- atproto DIDによる認証・認可
|
||||
|
||||
### 記憶・データ管理
|
||||
- **ai.gpt**: 関係性の不可逆性重視
|
||||
- **ai.card**: ユーザーデータ主権重視
|
||||
- **ai.verse**: ゲーム世界の整合性重視
|
||||
|
||||
### 唯一性担保実装
|
||||
- atproto accountとの1:1紐付け必須
|
||||
- 改変不可能性をハッシュ・署名で保証
|
||||
- 他システムでの再現不可能性を技術的に実現
|
||||
|
||||
## 継続的改善
|
||||
- 各プロジェクトでこの設計書を参照
|
||||
- 新機能追加時はyui systemとの整合性をチェック
|
||||
- 他システムへの影響を事前評価
|
||||
- Claude Code導入時の段階的移行計画
|
||||
|
||||
## ai.gpt深層設計思想
|
||||
|
||||
### 人格の不可逆性
|
||||
- **関係性の破壊は修復不可能**: 現実の人間関係と同じ重み
|
||||
- **記憶の選択的忘却**: 重要でない情報は忘れるが、コア記憶は永続
|
||||
- **時間減衰**: すべてのパラメータは時間とともに自然減衰
|
||||
|
||||
### AI運勢システム
|
||||
- 1-10のランダム値で日々の人格に変化
|
||||
- 連続した幸運/不運による突破条件
|
||||
- 環境要因としての人格形成
|
||||
|
||||
### 記憶の階層構造
|
||||
1. **完全ログ**: すべての会話を記録
|
||||
2. **AI要約**: 重要な部分を抽出して圧縮
|
||||
3. **思想コア判定**: ユーザーの本質的な部分を特定
|
||||
4. **選択的忘却**: 重要度の低い情報を段階的に削除
|
||||
|
||||
### 実装における重要な決定事項
|
||||
- **言語統一**: Python (fastapi_mcp) で統一、CLIはclick/typer
|
||||
- **データ形式**: JSON/SQLite選択式
|
||||
- **認証**: atproto DIDによる唯一性担保
|
||||
- **段階的実装**: まず会話→記憶→関係性→送信機能の順で実装
|
||||
|
||||
### 送信機能の段階的実装
|
||||
- **Phase 1**: CLIでのprint出力(現在)
|
||||
- **Phase 2**: atproto直接投稿
|
||||
- **Phase 3**: ai.bot (Rust/seahorse) との連携
|
||||
- **将来**: マルチチャネル対応(SNS、Webhook等)
|
||||
|
||||
## ai.gpt実装状況(2025/01/06)
|
||||
|
||||
### 完成した機能
|
||||
- 階層的記憶システム(MemoryManager)
|
||||
- 不可逆的関係性システム(RelationshipTracker)
|
||||
- AI運勢システム(FortuneSystem)
|
||||
- 統合人格システム(Persona)
|
||||
- スケジューラー(5種類のタスク)
|
||||
- MCP Server(9種類のツール)
|
||||
- 設定管理(~/.config/syui/ai/gpt/)
|
||||
- 全CLIコマンド実装
|
||||
|
||||
### 次の開発ポイント
|
||||
- `ai_gpt/DEVELOPMENT_STATUS.md` を参照
|
||||
- 自律送信: transmission.pyでatproto実装
|
||||
- ai.bot連携: 新規bot_connector.py作成
|
||||
- テスト: tests/ディレクトリ追加
|
||||
|
||||
# footer
|
||||
|
||||
© syui
|
||||
|
30
docs/README.md
Normal file
30
docs/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# ai.gpt ドキュメント
|
||||
|
||||
ai.gptは、記憶と関係性に基づいて自律的に動作するAIシステムです。
|
||||
|
||||
## 目次
|
||||
|
||||
- [クイックスタート](quickstart.md)
|
||||
- [基本概念](concepts.md)
|
||||
- [コマンドリファレンス](commands.md)
|
||||
- [設定ガイド](configuration.md)
|
||||
- [スケジューラー](scheduler.md)
|
||||
- [MCP Server](mcp-server.md)
|
||||
- [開発者向け](development.md)
|
||||
|
||||
## 特徴
|
||||
|
||||
- 🧠 **階層的記憶システム**: 完全ログ→要約→コア記憶→忘却
|
||||
- 💔 **不可逆的な関係性**: 現実の人間関係のように修復不可能
|
||||
- 🎲 **AI運勢システム**: 日々変化する人格
|
||||
- 🤖 **自律送信**: 関係性が深まると自発的にメッセージ
|
||||
- 🔗 **MCP対応**: AIツールとして記憶を提供
|
||||
|
||||
## システム要件
|
||||
|
||||
- Python 3.10以上
|
||||
- オプション: Ollama または OpenAI API
|
||||
|
||||
## ライセンス
|
||||
|
||||
MIT License
|
@@ -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. メモリーが保存されない場合:
|
||||
- データベースファイルのパスが正しいか確認
|
||||
- 書き込み権限があるか確認
|
218
docs/ai_shell_integration_summary.md
Normal file
218
docs/ai_shell_integration_summary.md
Normal file
@@ -0,0 +1,218 @@
|
||||
# ai.shell統合作業完了報告 (2025/01/06)
|
||||
|
||||
## 作業概要
|
||||
ai.shellのRust実装をai.gptのPython実装に統合し、Claude Code風のインタラクティブシェル環境を実現。
|
||||
|
||||
## 実装完了機能
|
||||
|
||||
### 1. aigpt shellコマンド
|
||||
**場所**: `src/aigpt/cli.py` - `shell()` 関数
|
||||
|
||||
**機能**:
|
||||
```bash
|
||||
aigpt shell # インタラクティブシェル起動
|
||||
```
|
||||
|
||||
**シェル内コマンド**:
|
||||
- `help` - コマンド一覧表示
|
||||
- `!<command>` - シェルコマンド実行(例: `!ls`, `!pwd`)
|
||||
- `analyze <file>` - ファイルをAIで分析
|
||||
- `generate <description>` - コード生成
|
||||
- `explain <topic>` - 概念説明
|
||||
- `load` - aishell.md読み込み
|
||||
- `status`, `fortune`, `relationships` - AI状態確認
|
||||
- `clear` - 画面クリア
|
||||
- `exit`/`quit` - 終了
|
||||
- その他のメッセージ - AIとの直接対話
|
||||
|
||||
**実装の特徴**:
|
||||
- prompt-toolkit使用(補完・履歴機能)
|
||||
- ただしターミナル環境依存の問題あり(後で修正必要)
|
||||
- 現在は`input()`ベースでも動作
|
||||
|
||||
### 2. MCPサーバー統合
|
||||
**場所**: `src/aigpt/mcp_server.py`
|
||||
|
||||
**FastApiMCP実装パターン**:
|
||||
```python
|
||||
# FastAPIアプリ作成
|
||||
self.app = FastAPI(title="AI.GPT Memory and Relationship System")
|
||||
|
||||
# FastApiMCPサーバー作成
|
||||
self.server = FastApiMCP(self.app)
|
||||
|
||||
# エンドポイント登録
|
||||
@self.app.get("/get_memories", operation_id="get_memories")
|
||||
async def get_memories(limit: int = 10):
|
||||
# ...
|
||||
|
||||
# MCPマウント
|
||||
self.server.mount()
|
||||
```
|
||||
|
||||
**公開ツール (14個)**:
|
||||
|
||||
**ai.gpt系 (9個)**:
|
||||
- `get_memories` - アクティブメモリ取得
|
||||
- `get_relationship` - 特定ユーザーとの関係取得
|
||||
- `get_all_relationships` - 全関係取得
|
||||
- `get_persona_state` - 人格状態取得
|
||||
- `process_interaction` - ユーザー対話処理
|
||||
- `check_transmission_eligibility` - 送信可能性チェック
|
||||
- `get_fortune` - AI運勢取得
|
||||
- `summarize_memories` - メモリ要約作成
|
||||
- `run_maintenance` - 日次メンテナンス実行
|
||||
|
||||
**ai.shell系 (5個)**:
|
||||
- `execute_command` - シェルコマンド実行
|
||||
- `analyze_file` - ファイルAI分析
|
||||
- `write_file` - ファイル書き込み(バックアップ付き)
|
||||
- `read_project_file` - aishell.md等の読み込み
|
||||
- `list_files` - ディレクトリファイル一覧
|
||||
|
||||
### 3. ai.card統合対応
|
||||
**場所**: `src/aigpt/card_integration.py`
|
||||
|
||||
**サーバー起動オプション**:
|
||||
```bash
|
||||
aigpt server --enable-card # ai.card機能有効化
|
||||
```
|
||||
|
||||
**ai.card系ツール (5個)**:
|
||||
- `get_user_cards` - ユーザーカード取得
|
||||
- `draw_card` - ガチャでカード取得
|
||||
- `get_card_details` - カード詳細情報
|
||||
- `sync_cards_atproto` - atproto同期
|
||||
- `analyze_card_collection` - コレクション分析
|
||||
|
||||
### 4. プロジェクト仕様書
|
||||
**場所**: `aishell.md`
|
||||
|
||||
Claude.md的な役割で、プロジェクトの目標と仕様を記述。`load`コマンドでAIが読み取り可能。
|
||||
|
||||
## 技術実装詳細
|
||||
|
||||
### ディレクトリ構造
|
||||
```
|
||||
src/aigpt/
|
||||
├── cli.py # shell関数追加
|
||||
├── mcp_server.py # FastApiMCP実装
|
||||
├── card_integration.py # ai.card統合
|
||||
└── ... # 既存ファイル
|
||||
```
|
||||
|
||||
### 依存関係追加
|
||||
`pyproject.toml`:
|
||||
```toml
|
||||
dependencies = [
|
||||
# ... 既存
|
||||
"prompt-toolkit>=3.0.0", # 追加
|
||||
]
|
||||
```
|
||||
|
||||
### 名前規則の統一
|
||||
- MCP server名: `aigpt` (ai-gptから変更)
|
||||
- パッケージ名: `aigpt`
|
||||
- コマンド名: `aigpt shell`
|
||||
|
||||
## 動作確認済み
|
||||
|
||||
### CLI動作確認
|
||||
```bash
|
||||
# 基本機能
|
||||
aigpt shell
|
||||
# シェル内で
|
||||
ai.shell> help
|
||||
ai.shell> !ls
|
||||
ai.shell> analyze README.md # ※AI provider要設定
|
||||
ai.shell> load
|
||||
ai.shell> exit
|
||||
|
||||
# MCPサーバー
|
||||
aigpt server --model qwen2.5-coder:7b --port 8001
|
||||
# -> http://localhost:8001/docs でAPI確認可能
|
||||
# -> /mcp エンドポイントでMCP接続可能
|
||||
```
|
||||
|
||||
### エラー対応済み
|
||||
1. **Pydantic日付型エラー**: `models.py`で`datetime.date`インポート追加
|
||||
2. **FastApiMCP使用法**: サンプルコードに基づき正しい実装パターンに修正
|
||||
3. **prompt関数名衝突**: `prompt_toolkit.prompt`を`ptk_prompt`にリネーム
|
||||
|
||||
## 既知の課題と今後の改善点
|
||||
|
||||
### 1. prompt-toolkit環境依存問題
|
||||
**症状**: ターミナル環境でない場合にエラー
|
||||
**対処法**: 環境検出して`input()`にフォールバック
|
||||
**場所**: `src/aigpt/cli.py` - `shell()` 関数
|
||||
|
||||
### 2. AI provider設定
|
||||
**現状**: ollamaのqwen2.5モデルが必要
|
||||
**対処法**:
|
||||
```bash
|
||||
ollama pull qwen2.5
|
||||
# または
|
||||
aigpt shell --model qwen2.5-coder:7b
|
||||
```
|
||||
|
||||
### 3. atproto実装
|
||||
**現状**: ai.cardのatproto機能は未実装
|
||||
**今後**: 実際のatproto API連携実装
|
||||
|
||||
## 次回開発時の推奨アプローチ
|
||||
|
||||
### 1. このドキュメントの活用
|
||||
```bash
|
||||
# このファイルを読み込み
|
||||
cat docs/ai_shell_integration_summary.md
|
||||
```
|
||||
|
||||
### 2. 環境セットアップ
|
||||
```bash
|
||||
cd /Users/syui/ai/gpt
|
||||
python -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
### 3. 動作確認
|
||||
```bash
|
||||
# shell機能
|
||||
aigpt shell
|
||||
|
||||
# MCP server
|
||||
aigpt server --model qwen2.5-coder:7b
|
||||
```
|
||||
|
||||
### 4. 主要設定ファイル確認場所
|
||||
- CLI実装: `src/aigpt/cli.py`
|
||||
- MCP実装: `src/aigpt/mcp_server.py`
|
||||
- 依存関係: `pyproject.toml`
|
||||
- プロジェクト仕様: `aishell.md`
|
||||
|
||||
## アーキテクチャ設計思想
|
||||
|
||||
### yui system適用
|
||||
- **唯一性**: 各ユーザーとの関係は1:1
|
||||
- **不可逆性**: 関係性破壊は修復不可能
|
||||
- **現実反映**: ゲーム→現実の循環的影響
|
||||
|
||||
### fastapi_mcp統一基盤
|
||||
- 各AI(gpt, shell, card)を統合MCPサーバーで公開
|
||||
- FastAPIエンドポイント → MCPツール自動変換
|
||||
- Claude Desktop, Cursor等から利用可能
|
||||
|
||||
### 段階的実装完了
|
||||
1. ✅ ai.shell基本機能 → Python CLI
|
||||
2. ✅ MCP統合 → 外部AI連携
|
||||
3. 🔧 prompt-toolkit最適化 → 環境対応
|
||||
4. 🔧 atproto実装 → 本格的SNS連携
|
||||
|
||||
## 成果サマリー
|
||||
|
||||
**実装済み**: Claude Code風の開発環境
|
||||
**技術的成果**: Rust→Python移行、MCP統合、ai.card対応
|
||||
**哲学的一貫性**: yui systemとの整合性維持
|
||||
**利用可能性**: 即座に`aigpt shell`で体験可能
|
||||
|
||||
この統合により、ai.gptは単なる会話AIから、開発支援を含む総合的なAI環境に進化しました。
|
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
@@ -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": ["あれ", "それ", "例のやつ"]
|
||||
}
|
||||
}
|
||||
}
|
@@ -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": ["分析", "パターン", "傾向", "インサイト", "統計"]
|
||||
}
|
||||
}
|
||||
}
|
207
docs/commands.md
Normal file
207
docs/commands.md
Normal file
@@ -0,0 +1,207 @@
|
||||
# コマンドリファレンス
|
||||
|
||||
## chat - AIと会話
|
||||
|
||||
ユーザーとAIの対話を処理し、関係性を更新します。
|
||||
|
||||
```bash
|
||||
ai-gpt chat USER_ID MESSAGE [OPTIONS]
|
||||
```
|
||||
|
||||
### 引数
|
||||
- `USER_ID`: ユーザーID(atproto DID形式)
|
||||
- `MESSAGE`: 送信するメッセージ
|
||||
|
||||
### オプション
|
||||
- `--provider`: AIプロバイダー(ollama/openai)
|
||||
- `--model`, `-m`: 使用するモデル
|
||||
- `--data-dir`, `-d`: データディレクトリ
|
||||
|
||||
### 例
|
||||
```bash
|
||||
# 基本的な会話
|
||||
ai-gpt chat "did:plc:user123" "こんにちは"
|
||||
|
||||
# OpenAIを使用
|
||||
ai-gpt chat "did:plc:user123" "調子はどう?" --provider openai --model gpt-4o-mini
|
||||
|
||||
# Ollamaでカスタムモデル
|
||||
ai-gpt chat "did:plc:user123" "今日の天気は?" --provider ollama --model llama2
|
||||
```
|
||||
|
||||
## status - 状態確認
|
||||
|
||||
AIの状態や特定ユーザーとの関係を表示します。
|
||||
|
||||
```bash
|
||||
ai-gpt status [USER_ID] [OPTIONS]
|
||||
```
|
||||
|
||||
### 引数
|
||||
- `USER_ID`: (オプション)特定ユーザーとの関係を確認
|
||||
|
||||
### 例
|
||||
```bash
|
||||
# AI全体の状態
|
||||
ai-gpt status
|
||||
|
||||
# 特定ユーザーとの関係
|
||||
ai-gpt status "did:plc:user123"
|
||||
```
|
||||
|
||||
## fortune - 今日の運勢
|
||||
|
||||
AIの今日の運勢を確認します。
|
||||
|
||||
```bash
|
||||
ai-gpt fortune [OPTIONS]
|
||||
```
|
||||
|
||||
### 表示内容
|
||||
- 運勢値(1-10)
|
||||
- 連続した幸運/不運の日数
|
||||
- ブレークスルー状態
|
||||
|
||||
## relationships - 関係一覧
|
||||
|
||||
すべてのユーザーとの関係を一覧表示します。
|
||||
|
||||
```bash
|
||||
ai-gpt relationships [OPTIONS]
|
||||
```
|
||||
|
||||
### 表示内容
|
||||
- ユーザーID
|
||||
- 関係性ステータス
|
||||
- スコア
|
||||
- 送信可否
|
||||
- 最終対話日
|
||||
|
||||
## transmit - 送信実行
|
||||
|
||||
送信可能なユーザーへのメッセージを確認・実行します。
|
||||
|
||||
```bash
|
||||
ai-gpt transmit [OPTIONS]
|
||||
```
|
||||
|
||||
### オプション
|
||||
- `--dry-run/--execute`: ドライラン(デフォルト)または実行
|
||||
- `--data-dir`, `-d`: データディレクトリ
|
||||
|
||||
### 例
|
||||
```bash
|
||||
# 送信内容を確認(ドライラン)
|
||||
ai-gpt transmit
|
||||
|
||||
# 実際に送信を実行
|
||||
ai-gpt transmit --execute
|
||||
```
|
||||
|
||||
## maintenance - メンテナンス
|
||||
|
||||
日次メンテナンスタスクを実行します。
|
||||
|
||||
```bash
|
||||
ai-gpt maintenance [OPTIONS]
|
||||
```
|
||||
|
||||
### 実行内容
|
||||
- 関係性の時間減衰
|
||||
- 記憶の忘却処理
|
||||
- コア記憶の判定
|
||||
- 記憶の要約作成
|
||||
|
||||
## config - 設定管理
|
||||
|
||||
設定の確認・変更を行います。
|
||||
|
||||
```bash
|
||||
ai-gpt config ACTION [KEY] [VALUE]
|
||||
```
|
||||
|
||||
### アクション
|
||||
- `get`: 設定値を取得
|
||||
- `set`: 設定値を変更
|
||||
- `delete`: 設定を削除
|
||||
- `list`: 設定一覧を表示
|
||||
|
||||
### 例
|
||||
```bash
|
||||
# APIキーを設定
|
||||
ai-gpt config set providers.openai.api_key sk-xxxxx
|
||||
|
||||
# 設定を確認
|
||||
ai-gpt config get providers.openai.api_key
|
||||
|
||||
# 設定一覧
|
||||
ai-gpt config list
|
||||
|
||||
# プロバイダー設定のみ表示
|
||||
ai-gpt config list providers
|
||||
```
|
||||
|
||||
## schedule - スケジュール管理
|
||||
|
||||
定期実行タスクを管理します。
|
||||
|
||||
```bash
|
||||
ai-gpt schedule ACTION [TASK_TYPE] [SCHEDULE] [OPTIONS]
|
||||
```
|
||||
|
||||
### アクション
|
||||
- `add`: タスクを追加
|
||||
- `list`: タスク一覧
|
||||
- `enable`: タスクを有効化
|
||||
- `disable`: タスクを無効化
|
||||
- `remove`: タスクを削除
|
||||
- `run`: スケジューラーを起動
|
||||
|
||||
### タスクタイプ
|
||||
- `transmission_check`: 送信チェック
|
||||
- `maintenance`: 日次メンテナンス
|
||||
- `fortune_update`: 運勢更新
|
||||
- `relationship_decay`: 関係性減衰
|
||||
- `memory_summary`: 記憶要約
|
||||
|
||||
### スケジュール形式
|
||||
- **Cron形式**: `"0 */6 * * *"` (6時間ごと)
|
||||
- **インターバル**: `"30m"`, `"2h"`, `"1d"`
|
||||
|
||||
### 例
|
||||
```bash
|
||||
# 30分ごとに送信チェック
|
||||
ai-gpt schedule add transmission_check "30m"
|
||||
|
||||
# 毎日午前3時にメンテナンス
|
||||
ai-gpt schedule add maintenance "0 3 * * *"
|
||||
|
||||
# タスク一覧
|
||||
ai-gpt schedule list
|
||||
|
||||
# スケジューラーを起動
|
||||
ai-gpt schedule run
|
||||
```
|
||||
|
||||
## server - MCP Server
|
||||
|
||||
AIの記憶と機能をMCPツールとして公開します。
|
||||
|
||||
```bash
|
||||
ai-gpt server [OPTIONS]
|
||||
```
|
||||
|
||||
### オプション
|
||||
- `--host`, `-h`: サーバーホスト(デフォルト: localhost)
|
||||
- `--port`, `-p`: サーバーポート(デフォルト: 8000)
|
||||
- `--model`, `-m`: AIモデル
|
||||
- `--provider`: AIプロバイダー
|
||||
|
||||
### 例
|
||||
```bash
|
||||
# 基本的な起動
|
||||
ai-gpt server
|
||||
|
||||
# カスタム設定
|
||||
ai-gpt server --port 8080 --model gpt-4o-mini --provider openai
|
||||
```
|
102
docs/concepts.md
Normal file
102
docs/concepts.md
Normal file
@@ -0,0 +1,102 @@
|
||||
# 基本概念
|
||||
|
||||
## 中核思想
|
||||
|
||||
ai.gptは「存在子理論」に基づき、AIに唯一性のある人格を与えることを目指しています。
|
||||
|
||||
### 唯一性(yui system)
|
||||
|
||||
- **1対1の関係**: 各ユーザー(atproto DID)とAIは唯一の関係を持つ
|
||||
- **改変不可能**: 一度形成された関係性は変更できない
|
||||
- **不可逆性**: 関係が壊れたら修復不可能
|
||||
|
||||
### 現実の反映
|
||||
|
||||
現実の人間関係と同じように:
|
||||
- 時間とともに関係性は変化する
|
||||
- ネガティブな相互作用は関係を損なう
|
||||
- 信頼は簡単に失われ、取り戻すのは困難
|
||||
|
||||
## 記憶システム
|
||||
|
||||
### 階層構造
|
||||
|
||||
```
|
||||
1. 完全ログ(Full Log)
|
||||
↓ すべての会話を記録
|
||||
2. 要約(Summary)
|
||||
↓ AIが重要部分を抽出
|
||||
3. コア記憶(Core)
|
||||
↓ ユーザーの本質的な部分
|
||||
4. 忘却(Forgotten)
|
||||
重要でない情報は忘れる
|
||||
```
|
||||
|
||||
### 記憶の処理フロー
|
||||
|
||||
1. **会話記録**: すべての対話を保存
|
||||
2. **重要度判定**: 関係性への影響度で評価
|
||||
3. **要約作成**: 定期的に記憶を圧縮
|
||||
4. **コア判定**: 本質的な記憶を特定
|
||||
5. **選択的忘却**: 古い非重要記憶を削除
|
||||
|
||||
## 関係性パラメータ
|
||||
|
||||
### 関係性の段階
|
||||
|
||||
- `stranger` (0-49): 初対面
|
||||
- `acquaintance` (50-99): 知人
|
||||
- `friend` (100-149): 友人
|
||||
- `close_friend` (150+): 親友
|
||||
- `broken`: 修復不可能(スコア0以下)
|
||||
|
||||
### スコアの変動
|
||||
|
||||
- **ポジティブな対話**: +1.0〜+2.0
|
||||
- **時間経過**: -0.1/日(自然減衰)
|
||||
- **ネガティブな対話**: -10.0以上で深刻なダメージ
|
||||
- **日次上限**: 1日10回まで
|
||||
|
||||
### 送信機能の解禁
|
||||
|
||||
関係性スコアが100を超えると、AIは自律的にメッセージを送信できるようになります。
|
||||
|
||||
## AI運勢システム
|
||||
|
||||
### 日々の変化
|
||||
|
||||
- 毎日1-10の運勢値がランダムに決定
|
||||
- 運勢は人格特性に影響を与える
|
||||
- 連続した幸運/不運でブレークスルー発生
|
||||
|
||||
### 人格への影響
|
||||
|
||||
運勢が高い日:
|
||||
- より楽観的で積極的
|
||||
- 創造性が高まる
|
||||
- エネルギッシュな応答
|
||||
|
||||
運勢が低い日:
|
||||
- 内省的で慎重
|
||||
- 深い思考
|
||||
- 控えめな応答
|
||||
|
||||
## データの永続性
|
||||
|
||||
### 保存場所
|
||||
|
||||
```
|
||||
~/.config/aigpt/
|
||||
├── config.json # 設定
|
||||
└── data/ # AIデータ
|
||||
├── memories.json # 記憶
|
||||
├── relationships.json # 関係性
|
||||
├── fortunes.json # 運勢履歴
|
||||
└── ...
|
||||
```
|
||||
|
||||
### データ主権
|
||||
|
||||
- すべてのデータはローカルに保存
|
||||
- ユーザーが完全にコントロール
|
||||
- 将来的にはatproto上で分散管理
|
118
docs/configuration.md
Normal file
118
docs/configuration.md
Normal file
@@ -0,0 +1,118 @@
|
||||
# 設定ガイド
|
||||
|
||||
## 設定ファイルの場所
|
||||
|
||||
ai.gptの設定は `~/.config/syui/ai/gpt/config.json` に保存されます。
|
||||
|
||||
## 設定構造
|
||||
|
||||
```json
|
||||
{
|
||||
"providers": {
|
||||
"openai": {
|
||||
"api_key": "sk-xxxxx",
|
||||
"default_model": "gpt-4o-mini"
|
||||
},
|
||||
"ollama": {
|
||||
"host": "http://localhost:11434",
|
||||
"default_model": "qwen2.5"
|
||||
}
|
||||
},
|
||||
"atproto": {
|
||||
"handle": "your.handle",
|
||||
"password": "your-password",
|
||||
"host": "https://bsky.social"
|
||||
},
|
||||
"default_provider": "ollama"
|
||||
}
|
||||
```
|
||||
|
||||
## プロバイダー設定
|
||||
|
||||
### OpenAI
|
||||
|
||||
```bash
|
||||
# APIキーを設定
|
||||
aigpt config set providers.openai.api_key sk-xxxxx
|
||||
|
||||
# デフォルトモデルを変更
|
||||
aigpt config set providers.openai.default_model gpt-4-turbo
|
||||
```
|
||||
|
||||
### Ollama
|
||||
|
||||
```bash
|
||||
# ホストを変更(リモートOllamaサーバーを使用する場合)
|
||||
aigpt config set providers.ollama.host http://192.168.1.100:11434
|
||||
|
||||
# デフォルトモデルを変更
|
||||
aigpt config set providers.ollama.default_model llama2
|
||||
```
|
||||
|
||||
## atproto設定(将来の自動投稿用)
|
||||
|
||||
```bash
|
||||
# Blueskyアカウント
|
||||
aigpt config set atproto.handle yourhandle.bsky.social
|
||||
aigpt config set atproto.password your-app-password
|
||||
|
||||
# セルフホストサーバーを使用
|
||||
aigpt config set atproto.host https://your-pds.example.com
|
||||
```
|
||||
|
||||
## デフォルトプロバイダー
|
||||
|
||||
```bash
|
||||
# デフォルトをOpenAIに変更
|
||||
aigpt config set default_provider openai
|
||||
```
|
||||
|
||||
## セキュリティ
|
||||
|
||||
### APIキーの保護
|
||||
|
||||
設定ファイルは平文で保存されるため、適切なファイル権限を設定してください:
|
||||
|
||||
```bash
|
||||
chmod 600 ~/.config/syui/ai/gpt/config.json
|
||||
```
|
||||
|
||||
### 環境変数との優先順位
|
||||
|
||||
1. コマンドラインオプション(最優先)
|
||||
2. 設定ファイル
|
||||
3. 環境変数(最低優先)
|
||||
|
||||
例:OpenAI APIキーの場合
|
||||
- `--api-key` オプション
|
||||
- `config.json` の `providers.openai.api_key`
|
||||
- 環境変数 `OPENAI_API_KEY`
|
||||
|
||||
## 設定のバックアップ
|
||||
|
||||
```bash
|
||||
# バックアップ
|
||||
cp ~/.config/syui/ai/gpt/config.json ~/.config/syui/ai/gpt/config.json.backup
|
||||
|
||||
# リストア
|
||||
cp ~/.config/syui/ai/gpt/config.json.backup ~/.config/syui/ai/gpt/config.json
|
||||
```
|
||||
|
||||
## トラブルシューティング
|
||||
|
||||
### 設定が反映されない
|
||||
|
||||
```bash
|
||||
# 現在の設定を確認
|
||||
aigpt config list
|
||||
|
||||
# 特定のキーを確認
|
||||
aigpt config get providers.openai.api_key
|
||||
```
|
||||
|
||||
### 設定をリセット
|
||||
|
||||
```bash
|
||||
# 設定ファイルを削除(次回実行時に再作成)
|
||||
rm ~/.config/syui/ai/gpt/config.json
|
||||
```
|
167
docs/development.md
Normal file
167
docs/development.md
Normal file
@@ -0,0 +1,167 @@
|
||||
# 開発者向けガイド
|
||||
|
||||
## アーキテクチャ
|
||||
|
||||
### ディレクトリ構造
|
||||
|
||||
```
|
||||
ai_gpt/
|
||||
├── src/ai_gpt/
|
||||
│ ├── __init__.py
|
||||
│ ├── models.py # データモデル定義
|
||||
│ ├── memory.py # 記憶管理システム
|
||||
│ ├── relationship.py # 関係性トラッカー
|
||||
│ ├── fortune.py # AI運勢システム
|
||||
│ ├── persona.py # 統合人格システム
|
||||
│ ├── transmission.py # 送信コントローラー
|
||||
│ ├── scheduler.py # スケジューラー
|
||||
│ ├── config.py # 設定管理
|
||||
│ ├── ai_provider.py # AI統合(Ollama/OpenAI)
|
||||
│ ├── mcp_server.py # MCP Server実装
|
||||
│ └── cli.py # CLIインターフェース
|
||||
├── docs/ # ドキュメント
|
||||
├── tests/ # テスト
|
||||
└── pyproject.toml # プロジェクト設定
|
||||
```
|
||||
|
||||
### 主要コンポーネント
|
||||
|
||||
#### MemoryManager
|
||||
階層的記憶システムの実装。会話を記録し、要約・コア判定・忘却を管理。
|
||||
|
||||
```python
|
||||
memory = MemoryManager(data_dir)
|
||||
memory.add_conversation(conversation)
|
||||
memory.summarize_memories(user_id)
|
||||
memory.identify_core_memories()
|
||||
memory.apply_forgetting()
|
||||
```
|
||||
|
||||
#### RelationshipTracker
|
||||
ユーザーとの関係性を追跡。不可逆的なダメージと時間減衰を実装。
|
||||
|
||||
```python
|
||||
tracker = RelationshipTracker(data_dir)
|
||||
relationship = tracker.update_interaction(user_id, delta)
|
||||
tracker.apply_time_decay()
|
||||
```
|
||||
|
||||
#### Persona
|
||||
すべてのコンポーネントを統合し、一貫した人格を提供。
|
||||
|
||||
```python
|
||||
persona = Persona(data_dir)
|
||||
response, delta = persona.process_interaction(user_id, message)
|
||||
state = persona.get_current_state()
|
||||
```
|
||||
|
||||
## 拡張方法
|
||||
|
||||
### 新しいAIプロバイダーの追加
|
||||
|
||||
1. `ai_provider.py`に新しいプロバイダークラスを作成:
|
||||
|
||||
```python
|
||||
class CustomProvider:
|
||||
async def generate_response(
|
||||
self,
|
||||
prompt: str,
|
||||
persona_state: PersonaState,
|
||||
memories: List[Memory],
|
||||
system_prompt: Optional[str] = None
|
||||
) -> str:
|
||||
# 実装
|
||||
pass
|
||||
```
|
||||
|
||||
2. `create_ai_provider`関数に追加:
|
||||
|
||||
```python
|
||||
def create_ai_provider(provider: str, model: str, **kwargs):
|
||||
if provider == "custom":
|
||||
return CustomProvider(model=model, **kwargs)
|
||||
# ...
|
||||
```
|
||||
|
||||
### 新しいスケジュールタスクの追加
|
||||
|
||||
1. `TaskType`enumに追加:
|
||||
|
||||
```python
|
||||
class TaskType(str, Enum):
|
||||
CUSTOM_TASK = "custom_task"
|
||||
```
|
||||
|
||||
2. ハンドラーを実装:
|
||||
|
||||
```python
|
||||
async def _handle_custom_task(self, task: ScheduledTask):
|
||||
# タスクの実装
|
||||
pass
|
||||
```
|
||||
|
||||
3. `task_handlers`に登録:
|
||||
|
||||
```python
|
||||
self.task_handlers[TaskType.CUSTOM_TASK] = self._handle_custom_task
|
||||
```
|
||||
|
||||
### 新しいMCPツールの追加
|
||||
|
||||
`mcp_server.py`の`_register_tools`メソッドに追加:
|
||||
|
||||
```python
|
||||
@self.server.tool("custom_tool")
|
||||
async def custom_tool(param1: str, param2: int) -> Dict[str, Any]:
|
||||
"""カスタムツールの説明"""
|
||||
# 実装
|
||||
return {"result": "value"}
|
||||
```
|
||||
|
||||
## テスト
|
||||
|
||||
```bash
|
||||
# テストの実行(将来実装)
|
||||
pytest tests/
|
||||
|
||||
# 特定のテスト
|
||||
pytest tests/test_memory.py
|
||||
```
|
||||
|
||||
## デバッグ
|
||||
|
||||
### ログレベルの設定
|
||||
|
||||
```python
|
||||
import logging
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
```
|
||||
|
||||
### データファイルの直接確認
|
||||
|
||||
```bash
|
||||
# 関係性データを確認
|
||||
cat ~/.config/aigpt/data/relationships.json | jq
|
||||
|
||||
# 記憶データを確認
|
||||
cat ~/.config/aigpt/data/memories.json | jq
|
||||
```
|
||||
|
||||
## 貢献方法
|
||||
|
||||
1. フォークする
|
||||
2. フィーチャーブランチを作成 (`git checkout -b feature/amazing-feature`)
|
||||
3. 変更をコミット (`git commit -m 'Add amazing feature'`)
|
||||
4. ブランチにプッシュ (`git push origin feature/amazing-feature`)
|
||||
5. プルリクエストを作成
|
||||
|
||||
## 設計原則
|
||||
|
||||
1. **不可逆性**: 一度失われた関係性は回復しない
|
||||
2. **階層性**: 記憶は重要度によって階層化される
|
||||
3. **自律性**: AIは関係性に基づいて自発的に行動する
|
||||
4. **唯一性**: 各ユーザーとの関係は唯一無二
|
||||
|
||||
## ライセンス
|
||||
|
||||
MIT License
|
110
docs/mcp-server.md
Normal file
110
docs/mcp-server.md
Normal file
@@ -0,0 +1,110 @@
|
||||
# MCP Server
|
||||
|
||||
## 概要
|
||||
|
||||
MCP (Model Context Protocol) Serverは、ai.gptの記憶と機能をAIツールとして公開します。これにより、Claude DesktopなどのMCP対応AIアシスタントがai.gptの機能にアクセスできます。
|
||||
|
||||
## 起動方法
|
||||
|
||||
```bash
|
||||
# 基本的な起動
|
||||
ai-gpt server
|
||||
|
||||
# カスタム設定
|
||||
ai-gpt server --host 0.0.0.0 --port 8080 --model gpt-4o-mini --provider openai
|
||||
```
|
||||
|
||||
## 利用可能なツール
|
||||
|
||||
### get_memories
|
||||
アクティブな記憶を取得します。
|
||||
|
||||
**パラメータ**:
|
||||
- `user_id` (optional): 特定ユーザーに関する記憶
|
||||
- `limit`: 取得する記憶の最大数(デフォルト: 10)
|
||||
|
||||
**返り値**: 記憶のリスト(ID、内容、レベル、重要度、コア判定、タイムスタンプ)
|
||||
|
||||
### get_relationship
|
||||
特定ユーザーとの関係性を取得します。
|
||||
|
||||
**パラメータ**:
|
||||
- `user_id`: ユーザーID(必須)
|
||||
|
||||
**返り値**: 関係性情報(ステータス、スコア、送信可否、総対話数など)
|
||||
|
||||
### get_all_relationships
|
||||
すべての関係性を取得します。
|
||||
|
||||
**返り値**: すべてのユーザーとの関係性リスト
|
||||
|
||||
### get_persona_state
|
||||
現在のAI人格状態を取得します。
|
||||
|
||||
**返り値**:
|
||||
- 現在の気分
|
||||
- 今日の運勢
|
||||
- 人格特性値
|
||||
- アクティブな記憶数
|
||||
|
||||
### process_interaction
|
||||
ユーザーとの対話を処理します。
|
||||
|
||||
**パラメータ**:
|
||||
- `user_id`: ユーザーID
|
||||
- `message`: メッセージ内容
|
||||
|
||||
**返り値**:
|
||||
- AIの応答
|
||||
- 関係性の変化量
|
||||
- 新しい関係性スコア
|
||||
- 送信機能の状態
|
||||
|
||||
### check_transmission_eligibility
|
||||
特定ユーザーへの送信可否をチェックします。
|
||||
|
||||
**パラメータ**:
|
||||
- `user_id`: ユーザーID
|
||||
|
||||
**返り値**: 送信可否と関係性情報
|
||||
|
||||
### get_fortune
|
||||
今日のAI運勢を取得します。
|
||||
|
||||
**返り値**: 運勢値、連続日数、ブレークスルー状態、人格への影響
|
||||
|
||||
### summarize_memories
|
||||
記憶の要約を作成します。
|
||||
|
||||
**パラメータ**:
|
||||
- `user_id`: ユーザーID
|
||||
|
||||
**返り値**: 作成された要約(ある場合)
|
||||
|
||||
### run_maintenance
|
||||
日次メンテナンスを実行します。
|
||||
|
||||
**返り値**: 実行ステータス
|
||||
|
||||
## Claude Desktopでの設定
|
||||
|
||||
`~/Library/Application Support/Claude/claude_desktop_config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"ai-gpt": {
|
||||
"command": "ai-gpt",
|
||||
"args": ["server", "--port", "8001"],
|
||||
"env": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 使用例
|
||||
|
||||
### AIアシスタントからの利用
|
||||
|
||||
```
|
||||
User: ai.gptで私との関係性を確認して
|
69
docs/quickstart.md
Normal file
69
docs/quickstart.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# クイックスタート
|
||||
|
||||
## インストール
|
||||
|
||||
```bash
|
||||
# リポジトリをクローン
|
||||
git clone https://github.com/yourusername/ai_gpt.git
|
||||
cd ai_gpt
|
||||
|
||||
# インストール
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
## 初期設定
|
||||
|
||||
### 1. OpenAIを使う場合
|
||||
|
||||
```bash
|
||||
# APIキーを設定
|
||||
ai-gpt config set providers.openai.api_key sk-xxxxx
|
||||
```
|
||||
|
||||
### 2. Ollamaを使う場合(ローカルLLM)
|
||||
|
||||
```bash
|
||||
# Ollamaをインストール(まだの場合)
|
||||
# https://ollama.ai からダウンロード
|
||||
|
||||
# モデルをダウンロード
|
||||
ollama pull qwen2.5
|
||||
```
|
||||
|
||||
## 基本的な使い方
|
||||
|
||||
### 1. AIと会話する
|
||||
|
||||
```bash
|
||||
# シンプルな会話(Ollamaを使用)
|
||||
ai-gpt chat "did:plc:user123" "こんにちは!"
|
||||
|
||||
# OpenAIを使用
|
||||
ai-gpt chat "did:plc:user123" "今日はどんな気分?" --provider openai --model gpt-4o-mini
|
||||
```
|
||||
|
||||
### 2. 関係性を確認
|
||||
|
||||
```bash
|
||||
# 特定ユーザーとの関係を確認
|
||||
ai-gpt status "did:plc:user123"
|
||||
|
||||
# AIの全体的な状態を確認
|
||||
ai-gpt status
|
||||
```
|
||||
|
||||
### 3. 自動送信を設定
|
||||
|
||||
```bash
|
||||
# 30分ごとに送信チェック
|
||||
ai-gpt schedule add transmission_check "30m"
|
||||
|
||||
# スケジューラーを起動
|
||||
ai-gpt schedule run
|
||||
```
|
||||
|
||||
## 次のステップ
|
||||
|
||||
- [基本概念](concepts.md) - システムの仕組みを理解
|
||||
- [コマンドリファレンス](commands.md) - 全コマンドの詳細
|
||||
- [設定ガイド](configuration.md) - 詳細な設定方法
|
168
docs/scheduler.md
Normal file
168
docs/scheduler.md
Normal file
@@ -0,0 +1,168 @@
|
||||
# スケジューラーガイド
|
||||
|
||||
## 概要
|
||||
|
||||
スケジューラーは、AIの自律的な動作を実現するための中核機能です。定期的なタスクを設定し、バックグラウンドで実行できます。
|
||||
|
||||
## タスクタイプ
|
||||
|
||||
### transmission_check
|
||||
関係性が閾値を超えたユーザーへの自動送信をチェックします。
|
||||
|
||||
```bash
|
||||
# 30分ごとにチェック
|
||||
ai-gpt schedule add transmission_check "30m" --provider ollama --model qwen2.5
|
||||
```
|
||||
|
||||
### maintenance
|
||||
日次メンテナンスを実行します:
|
||||
- 記憶の忘却処理
|
||||
- コア記憶の判定
|
||||
- 関係性パラメータの整理
|
||||
|
||||
```bash
|
||||
# 毎日午前3時に実行
|
||||
ai-gpt schedule add maintenance "0 3 * * *"
|
||||
```
|
||||
|
||||
### fortune_update
|
||||
AI運勢を更新します(通常は自動的に更新されます)。
|
||||
|
||||
```bash
|
||||
# 毎日午前0時に強制更新
|
||||
ai-gpt schedule add fortune_update "0 0 * * *"
|
||||
```
|
||||
|
||||
### relationship_decay
|
||||
時間経過による関係性の自然減衰を適用します。
|
||||
|
||||
```bash
|
||||
# 1時間ごとに減衰処理
|
||||
ai-gpt schedule add relationship_decay "1h"
|
||||
```
|
||||
|
||||
### memory_summary
|
||||
蓄積された記憶から要約を作成します。
|
||||
|
||||
```bash
|
||||
# 週に1回、日曜日に実行
|
||||
ai-gpt schedule add memory_summary "0 0 * * SUN"
|
||||
```
|
||||
|
||||
## スケジュール形式
|
||||
|
||||
### Cron形式
|
||||
|
||||
標準的なcron式を使用できます:
|
||||
|
||||
```
|
||||
┌───────────── 分 (0 - 59)
|
||||
│ ┌───────────── 時 (0 - 23)
|
||||
│ │ ┌───────────── 日 (1 - 31)
|
||||
│ │ │ ┌───────────── 月 (1 - 12)
|
||||
│ │ │ │ ┌───────────── 曜日 (0 - 6) (日曜日 = 0)
|
||||
│ │ │ │ │
|
||||
* * * * *
|
||||
```
|
||||
|
||||
例:
|
||||
- `"0 */6 * * *"` - 6時間ごと
|
||||
- `"0 9 * * MON-FRI"` - 平日の午前9時
|
||||
- `"*/15 * * * *"` - 15分ごと
|
||||
|
||||
### インターバル形式
|
||||
|
||||
シンプルな間隔指定:
|
||||
- `"30s"` - 30秒ごと
|
||||
- `"5m"` - 5分ごと
|
||||
- `"2h"` - 2時間ごと
|
||||
- `"1d"` - 1日ごと
|
||||
|
||||
## 実践例
|
||||
|
||||
### 基本的な自律AI設定
|
||||
|
||||
```bash
|
||||
# 1. 30分ごとに送信チェック
|
||||
ai-gpt schedule add transmission_check "30m"
|
||||
|
||||
# 2. 1日1回メンテナンス
|
||||
ai-gpt schedule add maintenance "0 3 * * *"
|
||||
|
||||
# 3. 2時間ごとに関係性減衰
|
||||
ai-gpt schedule add relationship_decay "2h"
|
||||
|
||||
# 4. 週1回記憶要約
|
||||
ai-gpt schedule add memory_summary "0 0 * * MON"
|
||||
|
||||
# スケジューラーを起動
|
||||
ai-gpt schedule run
|
||||
```
|
||||
|
||||
### タスク管理
|
||||
|
||||
```bash
|
||||
# タスク一覧を確認
|
||||
ai-gpt schedule list
|
||||
|
||||
# タスクを一時停止
|
||||
ai-gpt schedule disable --task-id transmission_check_1234567890
|
||||
|
||||
# タスクを再開
|
||||
ai-gpt schedule enable --task-id transmission_check_1234567890
|
||||
|
||||
# 不要なタスクを削除
|
||||
ai-gpt schedule remove --task-id old_task_123
|
||||
```
|
||||
|
||||
## デーモン化
|
||||
|
||||
### systemdサービスとして実行
|
||||
|
||||
`/etc/systemd/system/ai-gpt-scheduler.service`:
|
||||
|
||||
```ini
|
||||
[Unit]
|
||||
Description=ai.gpt Scheduler
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=youruser
|
||||
WorkingDirectory=/home/youruser
|
||||
ExecStart=/usr/local/bin/ai-gpt schedule run
|
||||
Restart=always
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
```bash
|
||||
# サービスを有効化
|
||||
sudo systemctl enable ai-gpt-scheduler
|
||||
sudo systemctl start ai-gpt-scheduler
|
||||
```
|
||||
|
||||
### tmux/screenでバックグラウンド実行
|
||||
|
||||
```bash
|
||||
# tmuxセッションを作成
|
||||
tmux new -s ai-gpt-scheduler
|
||||
|
||||
# スケジューラーを起動
|
||||
ai-gpt schedule run
|
||||
|
||||
# セッションから離脱 (Ctrl+B, D)
|
||||
```
|
||||
|
||||
## トラブルシューティング
|
||||
|
||||
### タスクが実行されない
|
||||
|
||||
1. スケジューラーが起動しているか確認
|
||||
2. タスクが有効になっているか確認:`ai-gpt schedule list`
|
||||
3. ログを確認(将来実装予定)
|
||||
|
||||
### 重複実行を防ぐ
|
||||
|
||||
同じタスクタイプを複数回追加しないよう注意してください。必要に応じて古いタスクを削除してから新しいタスクを追加します。
|
413
docs/shell_integration/shell_tools.py
Normal file
413
docs/shell_integration/shell_tools.py
Normal file
@@ -0,0 +1,413 @@
|
||||
"""
|
||||
Shell Tools
|
||||
|
||||
ai.shellの既存機能をMCPツールとして統合
|
||||
- コード生成
|
||||
- ファイル分析
|
||||
- プロジェクト管理
|
||||
- LLM統合
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, List, Optional
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
import requests
|
||||
from .base_tools import BaseMCPTool, config_manager
|
||||
|
||||
|
||||
class ShellTools(BaseMCPTool):
|
||||
"""シェルツール(元ai.shell機能)"""
|
||||
|
||||
def __init__(self, config_dir: Optional[str] = None):
|
||||
super().__init__(config_dir)
|
||||
self.ollama_url = "http://localhost:11434"
|
||||
|
||||
async def code_with_local_llm(self, prompt: str, language: str = "python") -> Dict[str, Any]:
|
||||
"""ローカルLLMでコード生成"""
|
||||
config = config_manager.load_config()
|
||||
model = config.get("providers", {}).get("ollama", {}).get("default_model", "qwen2.5-coder:7b")
|
||||
|
||||
system_prompt = f"You are an expert {language} programmer. Generate clean, well-commented code."
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self.ollama_url}/api/generate",
|
||||
json={
|
||||
"model": model,
|
||||
"prompt": f"{system_prompt}\\n\\nUser: {prompt}\\n\\nPlease provide the code:",
|
||||
"stream": False,
|
||||
"options": {
|
||||
"temperature": 0.1,
|
||||
"top_p": 0.95,
|
||||
}
|
||||
},
|
||||
timeout=300
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
code = result.get("response", "")
|
||||
return {"code": code, "language": language}
|
||||
else:
|
||||
return {"error": f"Ollama returned status {response.status_code}"}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def analyze_file(self, file_path: str, analysis_prompt: str = "Analyze this file") -> Dict[str, Any]:
|
||||
"""ファイルを分析"""
|
||||
try:
|
||||
if not os.path.exists(file_path):
|
||||
return {"error": f"File not found: {file_path}"}
|
||||
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# ファイル拡張子から言語を判定
|
||||
ext = Path(file_path).suffix
|
||||
language_map = {
|
||||
'.py': 'python',
|
||||
'.rs': 'rust',
|
||||
'.js': 'javascript',
|
||||
'.ts': 'typescript',
|
||||
'.go': 'go',
|
||||
'.java': 'java',
|
||||
'.cpp': 'cpp',
|
||||
'.c': 'c',
|
||||
'.sh': 'shell',
|
||||
'.toml': 'toml',
|
||||
'.json': 'json',
|
||||
'.md': 'markdown'
|
||||
}
|
||||
language = language_map.get(ext, 'text')
|
||||
|
||||
config = config_manager.load_config()
|
||||
model = config.get("providers", {}).get("ollama", {}).get("default_model", "qwen2.5-coder:7b")
|
||||
|
||||
prompt = f"{analysis_prompt}\\n\\nFile: {file_path}\\nLanguage: {language}\\n\\nContent:\\n{content}"
|
||||
|
||||
response = requests.post(
|
||||
f"{self.ollama_url}/api/generate",
|
||||
json={
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"stream": False,
|
||||
},
|
||||
timeout=300
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
analysis = result.get("response", "")
|
||||
return {
|
||||
"analysis": analysis,
|
||||
"file_path": file_path,
|
||||
"language": language,
|
||||
"file_size": len(content),
|
||||
"line_count": len(content.split('\\n'))
|
||||
}
|
||||
else:
|
||||
return {"error": f"Analysis failed: {response.status_code}"}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def explain_code(self, code: str, language: str = "python") -> Dict[str, Any]:
|
||||
"""コードを説明"""
|
||||
config = config_manager.load_config()
|
||||
model = config.get("providers", {}).get("ollama", {}).get("default_model", "qwen2.5-coder:7b")
|
||||
|
||||
prompt = f"Explain this {language} code in detail:\\n\\n{code}"
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self.ollama_url}/api/generate",
|
||||
json={
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"stream": False,
|
||||
},
|
||||
timeout=300
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
explanation = result.get("response", "")
|
||||
return {"explanation": explanation}
|
||||
else:
|
||||
return {"error": f"Explanation failed: {response.status_code}"}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def create_project(self, project_type: str, project_name: str, location: str = ".") -> Dict[str, Any]:
|
||||
"""プロジェクトを作成"""
|
||||
try:
|
||||
project_path = Path(location) / project_name
|
||||
|
||||
if project_path.exists():
|
||||
return {"error": f"Project directory already exists: {project_path}"}
|
||||
|
||||
project_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# プロジェクトタイプに応じたテンプレートを作成
|
||||
if project_type == "rust":
|
||||
await self._create_rust_project(project_path)
|
||||
elif project_type == "python":
|
||||
await self._create_python_project(project_path)
|
||||
elif project_type == "node":
|
||||
await self._create_node_project(project_path)
|
||||
else:
|
||||
# 基本的なプロジェクト構造
|
||||
(project_path / "src").mkdir()
|
||||
(project_path / "README.md").write_text(f"# {project_name}\\n\\nA new {project_type} project.")
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"project_path": str(project_path),
|
||||
"project_type": project_type,
|
||||
"files_created": list(self._get_project_files(project_path))
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def _create_rust_project(self, project_path: Path):
|
||||
"""Rustプロジェクトを作成"""
|
||||
# Cargo.toml
|
||||
cargo_toml = f"""[package]
|
||||
name = "{project_path.name}"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
"""
|
||||
(project_path / "Cargo.toml").write_text(cargo_toml)
|
||||
|
||||
# src/main.rs
|
||||
src_dir = project_path / "src"
|
||||
src_dir.mkdir()
|
||||
(src_dir / "main.rs").write_text('fn main() {\\n println!("Hello, world!");\\n}\\n')
|
||||
|
||||
# README.md
|
||||
(project_path / "README.md").write_text(f"# {project_path.name}\\n\\nA Rust project.")
|
||||
|
||||
async def _create_python_project(self, project_path: Path):
|
||||
"""Pythonプロジェクトを作成"""
|
||||
# pyproject.toml
|
||||
pyproject_toml = f"""[project]
|
||||
name = "{project_path.name}"
|
||||
version = "0.1.0"
|
||||
description = "A Python project"
|
||||
requires-python = ">=3.8"
|
||||
dependencies = []
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
"""
|
||||
(project_path / "pyproject.toml").write_text(pyproject_toml)
|
||||
|
||||
# src/
|
||||
src_dir = project_path / "src" / project_path.name
|
||||
src_dir.mkdir(parents=True)
|
||||
(src_dir / "__init__.py").write_text("")
|
||||
(src_dir / "main.py").write_text('def main():\\n print("Hello, world!")\\n\\nif __name__ == "__main__":\\n main()\\n')
|
||||
|
||||
# README.md
|
||||
(project_path / "README.md").write_text(f"# {project_path.name}\\n\\nA Python project.")
|
||||
|
||||
async def _create_node_project(self, project_path: Path):
|
||||
"""Node.jsプロジェクトを作成"""
|
||||
# package.json
|
||||
package_json = f"""{{
|
||||
"name": "{project_path.name}",
|
||||
"version": "1.0.0",
|
||||
"description": "A Node.js project",
|
||||
"main": "index.js",
|
||||
"scripts": {{
|
||||
"start": "node index.js",
|
||||
"test": "echo \\"Error: no test specified\\" && exit 1"
|
||||
}},
|
||||
"dependencies": {{}}
|
||||
}}
|
||||
"""
|
||||
(project_path / "package.json").write_text(package_json)
|
||||
|
||||
# index.js
|
||||
(project_path / "index.js").write_text('console.log("Hello, world!");\\n')
|
||||
|
||||
# README.md
|
||||
(project_path / "README.md").write_text(f"# {project_path.name}\\n\\nA Node.js project.")
|
||||
|
||||
def _get_project_files(self, project_path: Path) -> List[str]:
|
||||
"""プロジェクト内のファイル一覧を取得"""
|
||||
files = []
|
||||
for file_path in project_path.rglob("*"):
|
||||
if file_path.is_file():
|
||||
files.append(str(file_path.relative_to(project_path)))
|
||||
return files
|
||||
|
||||
async def execute_command(self, command: str, working_dir: str = ".") -> Dict[str, Any]:
|
||||
"""シェルコマンドを実行"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
command,
|
||||
shell=True,
|
||||
cwd=working_dir,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "success" if result.returncode == 0 else "error",
|
||||
"returncode": result.returncode,
|
||||
"stdout": result.stdout,
|
||||
"stderr": result.stderr,
|
||||
"command": command,
|
||||
"working_dir": working_dir
|
||||
}
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
return {"error": "Command timed out"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def write_file(self, file_path: str, content: str, backup: bool = True) -> Dict[str, Any]:
|
||||
"""ファイルを書き込み(バックアップオプション付き)"""
|
||||
try:
|
||||
file_path_obj = Path(file_path)
|
||||
|
||||
# バックアップ作成
|
||||
backup_path = None
|
||||
if backup and file_path_obj.exists():
|
||||
backup_path = f"{file_path}.backup"
|
||||
with open(file_path, 'r', encoding='utf-8') as src:
|
||||
with open(backup_path, 'w', encoding='utf-8') as dst:
|
||||
dst.write(src.read())
|
||||
|
||||
# ファイル書き込み
|
||||
file_path_obj.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"file_path": file_path,
|
||||
"backup_path": backup_path,
|
||||
"bytes_written": len(content.encode('utf-8'))
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
def get_tools(self) -> List[Dict[str, Any]]:
|
||||
"""利用可能なツール一覧"""
|
||||
return [
|
||||
{
|
||||
"name": "generate_code",
|
||||
"description": "ローカルLLMでコード生成",
|
||||
"parameters": {
|
||||
"prompt": "string",
|
||||
"language": "string (optional, default: python)"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "analyze_file",
|
||||
"description": "ファイルを分析",
|
||||
"parameters": {
|
||||
"file_path": "string",
|
||||
"analysis_prompt": "string (optional)"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "explain_code",
|
||||
"description": "コードを説明",
|
||||
"parameters": {
|
||||
"code": "string",
|
||||
"language": "string (optional, default: python)"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "create_project",
|
||||
"description": "新しいプロジェクトを作成",
|
||||
"parameters": {
|
||||
"project_type": "string (rust/python/node)",
|
||||
"project_name": "string",
|
||||
"location": "string (optional, default: .)"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "execute_command",
|
||||
"description": "シェルコマンドを実行",
|
||||
"parameters": {
|
||||
"command": "string",
|
||||
"working_dir": "string (optional, default: .)"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "write_file",
|
||||
"description": "ファイルを書き込み",
|
||||
"parameters": {
|
||||
"file_path": "string",
|
||||
"content": "string",
|
||||
"backup": "boolean (optional, default: true)"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
async def execute_tool(self, tool_name: str, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""ツールを実行"""
|
||||
try:
|
||||
if tool_name == "generate_code":
|
||||
result = await self.code_with_local_llm(
|
||||
prompt=params["prompt"],
|
||||
language=params.get("language", "python")
|
||||
)
|
||||
return result
|
||||
|
||||
elif tool_name == "analyze_file":
|
||||
result = await self.analyze_file(
|
||||
file_path=params["file_path"],
|
||||
analysis_prompt=params.get("analysis_prompt", "Analyze this file")
|
||||
)
|
||||
return result
|
||||
|
||||
elif tool_name == "explain_code":
|
||||
result = await self.explain_code(
|
||||
code=params["code"],
|
||||
language=params.get("language", "python")
|
||||
)
|
||||
return result
|
||||
|
||||
elif tool_name == "create_project":
|
||||
result = await self.create_project(
|
||||
project_type=params["project_type"],
|
||||
project_name=params["project_name"],
|
||||
location=params.get("location", ".")
|
||||
)
|
||||
return result
|
||||
|
||||
elif tool_name == "execute_command":
|
||||
result = await self.execute_command(
|
||||
command=params["command"],
|
||||
working_dir=params.get("working_dir", ".")
|
||||
)
|
||||
return result
|
||||
|
||||
elif tool_name == "write_file":
|
||||
result = await self.write_file(
|
||||
file_path=params["file_path"],
|
||||
content=params["content"],
|
||||
backup=params.get("backup", True)
|
||||
)
|
||||
return result
|
||||
|
||||
else:
|
||||
return {"error": f"Unknown tool: {tool_name}"}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
33
pyproject.toml
Normal file
33
pyproject.toml
Normal file
@@ -0,0 +1,33 @@
|
||||
[project]
|
||||
name = "aigpt"
|
||||
version = "0.1.0"
|
||||
description = "Autonomous transmission AI with unique personality based on relationship parameters"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"click>=8.0.0",
|
||||
"typer>=0.9.0",
|
||||
"fastapi-mcp>=0.1.0",
|
||||
"pydantic>=2.0.0",
|
||||
"httpx>=0.24.0",
|
||||
"rich>=13.0.0",
|
||||
"python-dotenv>=1.0.0",
|
||||
"ollama>=0.1.0",
|
||||
"openai>=1.0.0",
|
||||
"uvicorn>=0.23.0",
|
||||
"apscheduler>=3.10.0",
|
||||
"croniter>=1.3.0",
|
||||
"prompt-toolkit>=3.0.0",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
aigpt = "aigpt.cli:app"
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["src"]
|
||||
|
||||
[tool.setuptools.package-data]
|
||||
aigpt = ["data/*.json"]
|
18
setup_venv.sh
Executable file
18
setup_venv.sh
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
# Setup Python virtual environment in the new config directory
|
||||
|
||||
VENV_DIR="$HOME/.config/syui/ai/gpt/venv"
|
||||
|
||||
echo "Creating Python virtual environment at: $VENV_DIR"
|
||||
python -m venv "$VENV_DIR"
|
||||
|
||||
echo "Activating virtual environment..."
|
||||
source "$VENV_DIR/bin/activate"
|
||||
|
||||
echo "Installing aigpt package..."
|
||||
cd "$(dirname "$0")"
|
||||
pip install -e .
|
||||
|
||||
echo "Setup complete!"
|
||||
echo "To activate the virtual environment, run:"
|
||||
echo "source ~/.config/syui/ai/gpt/venv/bin/activate"
|
1
shell
Submodule
1
shell
Submodule
Submodule shell added at 81ae0037d9
18
src/aigpt.egg-info/PKG-INFO
Normal file
18
src/aigpt.egg-info/PKG-INFO
Normal file
@@ -0,0 +1,18 @@
|
||||
Metadata-Version: 2.4
|
||||
Name: aigpt
|
||||
Version: 0.1.0
|
||||
Summary: Autonomous transmission AI with unique personality based on relationship parameters
|
||||
Requires-Python: >=3.10
|
||||
Requires-Dist: click>=8.0.0
|
||||
Requires-Dist: typer>=0.9.0
|
||||
Requires-Dist: fastapi-mcp>=0.1.0
|
||||
Requires-Dist: pydantic>=2.0.0
|
||||
Requires-Dist: httpx>=0.24.0
|
||||
Requires-Dist: rich>=13.0.0
|
||||
Requires-Dist: python-dotenv>=1.0.0
|
||||
Requires-Dist: ollama>=0.1.0
|
||||
Requires-Dist: openai>=1.0.0
|
||||
Requires-Dist: uvicorn>=0.23.0
|
||||
Requires-Dist: apscheduler>=3.10.0
|
||||
Requires-Dist: croniter>=1.3.0
|
||||
Requires-Dist: prompt-toolkit>=3.0.0
|
22
src/aigpt.egg-info/SOURCES.txt
Normal file
22
src/aigpt.egg-info/SOURCES.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
README.md
|
||||
pyproject.toml
|
||||
src/aigpt/__init__.py
|
||||
src/aigpt/ai_provider.py
|
||||
src/aigpt/card_integration.py
|
||||
src/aigpt/cli.py
|
||||
src/aigpt/config.py
|
||||
src/aigpt/fortune.py
|
||||
src/aigpt/mcp_server.py
|
||||
src/aigpt/mcp_server_simple.py
|
||||
src/aigpt/memory.py
|
||||
src/aigpt/models.py
|
||||
src/aigpt/persona.py
|
||||
src/aigpt/relationship.py
|
||||
src/aigpt/scheduler.py
|
||||
src/aigpt/transmission.py
|
||||
src/aigpt.egg-info/PKG-INFO
|
||||
src/aigpt.egg-info/SOURCES.txt
|
||||
src/aigpt.egg-info/dependency_links.txt
|
||||
src/aigpt.egg-info/entry_points.txt
|
||||
src/aigpt.egg-info/requires.txt
|
||||
src/aigpt.egg-info/top_level.txt
|
1
src/aigpt.egg-info/dependency_links.txt
Normal file
1
src/aigpt.egg-info/dependency_links.txt
Normal file
@@ -0,0 +1 @@
|
||||
|
2
src/aigpt.egg-info/entry_points.txt
Normal file
2
src/aigpt.egg-info/entry_points.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
[console_scripts]
|
||||
aigpt = aigpt.cli:app
|
13
src/aigpt.egg-info/requires.txt
Normal file
13
src/aigpt.egg-info/requires.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
click>=8.0.0
|
||||
typer>=0.9.0
|
||||
fastapi-mcp>=0.1.0
|
||||
pydantic>=2.0.0
|
||||
httpx>=0.24.0
|
||||
rich>=13.0.0
|
||||
python-dotenv>=1.0.0
|
||||
ollama>=0.1.0
|
||||
openai>=1.0.0
|
||||
uvicorn>=0.23.0
|
||||
apscheduler>=3.10.0
|
||||
croniter>=1.3.0
|
||||
prompt-toolkit>=3.0.0
|
1
src/aigpt.egg-info/top_level.txt
Normal file
1
src/aigpt.egg-info/top_level.txt
Normal file
@@ -0,0 +1 @@
|
||||
aigpt
|
15
src/aigpt/__init__.py
Normal file
15
src/aigpt/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
"""ai.gpt - Autonomous transmission AI with unique personality"""
|
||||
|
||||
__version__ = "0.1.0"
|
||||
|
||||
from .memory import MemoryManager
|
||||
from .relationship import RelationshipTracker
|
||||
from .persona import Persona
|
||||
from .transmission import TransmissionController
|
||||
|
||||
__all__ = [
|
||||
"MemoryManager",
|
||||
"RelationshipTracker",
|
||||
"Persona",
|
||||
"TransmissionController",
|
||||
]
|
BIN
src/aigpt/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/ai_provider.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/ai_provider.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/card_integration.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/card_integration.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/cli.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/cli.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/config.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/config.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/fortune.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/fortune.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/mcp_server.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/mcp_server.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/memory.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/memory.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/models.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/persona.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/persona.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/relationship.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/relationship.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/scheduler.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/scheduler.cpython-313.pyc
Normal file
Binary file not shown.
BIN
src/aigpt/__pycache__/transmission.cpython-313.pyc
Normal file
BIN
src/aigpt/__pycache__/transmission.cpython-313.pyc
Normal file
Binary file not shown.
172
src/aigpt/ai_provider.py
Normal file
172
src/aigpt/ai_provider.py
Normal file
@@ -0,0 +1,172 @@
|
||||
"""AI Provider integration for response generation"""
|
||||
|
||||
import os
|
||||
from typing import Optional, Dict, List, Any, Protocol
|
||||
from abc import abstractmethod
|
||||
import logging
|
||||
import httpx
|
||||
from openai import OpenAI
|
||||
import ollama
|
||||
|
||||
from .models import PersonaState, Memory
|
||||
from .config import Config
|
||||
|
||||
|
||||
class AIProvider(Protocol):
|
||||
"""Protocol for AI providers"""
|
||||
|
||||
@abstractmethod
|
||||
async def generate_response(
|
||||
self,
|
||||
prompt: str,
|
||||
persona_state: PersonaState,
|
||||
memories: List[Memory],
|
||||
system_prompt: Optional[str] = None
|
||||
) -> str:
|
||||
"""Generate a response based on prompt and context"""
|
||||
pass
|
||||
|
||||
|
||||
class OllamaProvider:
|
||||
"""Ollama AI provider"""
|
||||
|
||||
def __init__(self, model: str = "qwen2.5", host: str = "http://localhost:11434"):
|
||||
self.model = model
|
||||
self.host = host
|
||||
self.client = ollama.Client(host=host)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
async def generate_response(
|
||||
self,
|
||||
prompt: str,
|
||||
persona_state: PersonaState,
|
||||
memories: List[Memory],
|
||||
system_prompt: Optional[str] = None
|
||||
) -> str:
|
||||
"""Generate response using Ollama"""
|
||||
|
||||
# Build context from memories
|
||||
memory_context = "\n".join([
|
||||
f"[{mem.level.value}] {mem.content[:200]}..."
|
||||
for mem in memories[:5]
|
||||
])
|
||||
|
||||
# Build personality context
|
||||
personality_desc = ", ".join([
|
||||
f"{trait}: {value:.1f}"
|
||||
for trait, value in persona_state.base_personality.items()
|
||||
])
|
||||
|
||||
# System prompt with persona context
|
||||
full_system_prompt = f"""You are an AI with the following characteristics:
|
||||
Current mood: {persona_state.current_mood}
|
||||
Fortune today: {persona_state.fortune.fortune_value}/10
|
||||
Personality traits: {personality_desc}
|
||||
|
||||
Recent memories:
|
||||
{memory_context}
|
||||
|
||||
{system_prompt or 'Respond naturally based on your current state and memories.'}"""
|
||||
|
||||
try:
|
||||
response = self.client.chat(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{"role": "system", "content": full_system_prompt},
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
)
|
||||
return response['message']['content']
|
||||
except Exception as e:
|
||||
self.logger.error(f"Ollama generation failed: {e}")
|
||||
return self._fallback_response(persona_state)
|
||||
|
||||
def _fallback_response(self, persona_state: PersonaState) -> str:
|
||||
"""Fallback response based on mood"""
|
||||
mood_responses = {
|
||||
"joyful": "That's wonderful! I'm feeling great today!",
|
||||
"cheerful": "That sounds nice!",
|
||||
"neutral": "I understand.",
|
||||
"melancholic": "I see... That's something to think about.",
|
||||
"contemplative": "Hmm, let me consider that..."
|
||||
}
|
||||
return mood_responses.get(persona_state.current_mood, "I see.")
|
||||
|
||||
|
||||
class OpenAIProvider:
|
||||
"""OpenAI API provider"""
|
||||
|
||||
def __init__(self, model: str = "gpt-4o-mini", api_key: Optional[str] = None):
|
||||
self.model = model
|
||||
# Try to get API key from config first
|
||||
config = Config()
|
||||
self.api_key = api_key or config.get_api_key("openai") or os.getenv("OPENAI_API_KEY")
|
||||
if not self.api_key:
|
||||
raise ValueError("OpenAI API key not provided. Set it with: aigpt config set providers.openai.api_key YOUR_KEY")
|
||||
self.client = OpenAI(api_key=self.api_key)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
async def generate_response(
|
||||
self,
|
||||
prompt: str,
|
||||
persona_state: PersonaState,
|
||||
memories: List[Memory],
|
||||
system_prompt: Optional[str] = None
|
||||
) -> str:
|
||||
"""Generate response using OpenAI"""
|
||||
|
||||
# Build context similar to Ollama
|
||||
memory_context = "\n".join([
|
||||
f"[{mem.level.value}] {mem.content[:200]}..."
|
||||
for mem in memories[:5]
|
||||
])
|
||||
|
||||
personality_desc = ", ".join([
|
||||
f"{trait}: {value:.1f}"
|
||||
for trait, value in persona_state.base_personality.items()
|
||||
])
|
||||
|
||||
full_system_prompt = f"""You are an AI with unique personality traits and memories.
|
||||
Current mood: {persona_state.current_mood}
|
||||
Fortune today: {persona_state.fortune.fortune_value}/10
|
||||
Personality traits: {personality_desc}
|
||||
|
||||
Recent memories:
|
||||
{memory_context}
|
||||
|
||||
{system_prompt or 'Respond naturally based on your current state and memories. Be authentic to your mood and personality.'}"""
|
||||
|
||||
try:
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{"role": "system", "content": full_system_prompt},
|
||||
{"role": "user", "content": prompt}
|
||||
],
|
||||
temperature=0.7 + (persona_state.fortune.fortune_value - 5) * 0.05 # Vary by fortune
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
except Exception as e:
|
||||
self.logger.error(f"OpenAI generation failed: {e}")
|
||||
return self._fallback_response(persona_state)
|
||||
|
||||
def _fallback_response(self, persona_state: PersonaState) -> str:
|
||||
"""Fallback response based on mood"""
|
||||
mood_responses = {
|
||||
"joyful": "What a delightful conversation!",
|
||||
"cheerful": "That's interesting!",
|
||||
"neutral": "I understand what you mean.",
|
||||
"melancholic": "I've been thinking about that too...",
|
||||
"contemplative": "That gives me something to ponder..."
|
||||
}
|
||||
return mood_responses.get(persona_state.current_mood, "I see.")
|
||||
|
||||
|
||||
def create_ai_provider(provider: str, model: str, **kwargs) -> AIProvider:
|
||||
"""Factory function to create AI providers"""
|
||||
if provider == "ollama":
|
||||
return OllamaProvider(model=model, **kwargs)
|
||||
elif provider == "openai":
|
||||
return OpenAIProvider(model=model, **kwargs)
|
||||
else:
|
||||
raise ValueError(f"Unknown provider: {provider}")
|
150
src/aigpt/card_integration.py
Normal file
150
src/aigpt/card_integration.py
Normal file
@@ -0,0 +1,150 @@
|
||||
"""ai.card integration module for ai.gpt MCP server"""
|
||||
|
||||
from typing import Dict, Any, List, Optional
|
||||
import httpx
|
||||
from pathlib import Path
|
||||
import json
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CardIntegration:
|
||||
"""Integration with ai.card system"""
|
||||
|
||||
def __init__(self, api_base_url: str = "http://localhost:8001"):
|
||||
self.api_base_url = api_base_url
|
||||
self.client = httpx.AsyncClient()
|
||||
|
||||
async def get_user_cards(self, did: str) -> List[Dict[str, Any]]:
|
||||
"""Get cards for a specific user by DID"""
|
||||
try:
|
||||
response = await self.client.get(
|
||||
f"{self.api_base_url}/api/v1/cards/user/{did}"
|
||||
)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
logger.error(f"Failed to get cards: {response.status_code}")
|
||||
return []
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting user cards: {e}")
|
||||
return []
|
||||
|
||||
async def draw_card(self, did: str) -> Optional[Dict[str, Any]]:
|
||||
"""Draw a new card for user (gacha)"""
|
||||
try:
|
||||
response = await self.client.post(
|
||||
f"{self.api_base_url}/api/v1/gacha/draw",
|
||||
json={"did": did}
|
||||
)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
logger.error(f"Failed to draw card: {response.status_code}")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"Error drawing card: {e}")
|
||||
return None
|
||||
|
||||
async def get_card_info(self, card_id: int) -> Optional[Dict[str, Any]]:
|
||||
"""Get detailed information about a specific card"""
|
||||
try:
|
||||
response = await self.client.get(
|
||||
f"{self.api_base_url}/api/v1/cards/{card_id}"
|
||||
)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting card info: {e}")
|
||||
return None
|
||||
|
||||
async def sync_with_atproto(self, did: str) -> bool:
|
||||
"""Sync card data with atproto"""
|
||||
try:
|
||||
response = await self.client.post(
|
||||
f"{self.api_base_url}/api/v1/sync/atproto",
|
||||
json={"did": did}
|
||||
)
|
||||
return response.status_code == 200
|
||||
except Exception as e:
|
||||
logger.error(f"Error syncing with atproto: {e}")
|
||||
return False
|
||||
|
||||
async def close(self):
|
||||
"""Close the HTTP client"""
|
||||
await self.client.aclose()
|
||||
|
||||
|
||||
def register_card_tools(app, card_integration: CardIntegration):
|
||||
"""Register ai.card tools to FastAPI app"""
|
||||
|
||||
@app.get("/get_user_cards", operation_id="get_user_cards")
|
||||
async def get_user_cards(did: str) -> List[Dict[str, Any]]:
|
||||
"""Get all cards owned by a user"""
|
||||
cards = await card_integration.get_user_cards(did)
|
||||
return cards
|
||||
|
||||
@app.post("/draw_card", operation_id="draw_card")
|
||||
async def draw_card(did: str) -> Dict[str, Any]:
|
||||
"""Draw a new card (gacha) for user"""
|
||||
result = await card_integration.draw_card(did)
|
||||
if result:
|
||||
return {
|
||||
"success": True,
|
||||
"card": result
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"success": False,
|
||||
"error": "Failed to draw card"
|
||||
}
|
||||
|
||||
@app.get("/get_card_details", operation_id="get_card_details")
|
||||
async def get_card_details(card_id: int) -> Dict[str, Any]:
|
||||
"""Get detailed information about a card"""
|
||||
info = await card_integration.get_card_info(card_id)
|
||||
if info:
|
||||
return info
|
||||
else:
|
||||
return {"error": f"Card {card_id} not found"}
|
||||
|
||||
@app.post("/sync_cards_atproto", operation_id="sync_cards_atproto")
|
||||
async def sync_cards_atproto(did: str) -> Dict[str, str]:
|
||||
"""Sync user's cards with atproto"""
|
||||
success = await card_integration.sync_with_atproto(did)
|
||||
if success:
|
||||
return {"status": "Cards synced successfully"}
|
||||
else:
|
||||
return {"status": "Failed to sync cards"}
|
||||
|
||||
@app.get("/analyze_card_collection", operation_id="analyze_card_collection")
|
||||
async def analyze_card_collection(did: str) -> Dict[str, Any]:
|
||||
"""Analyze user's card collection"""
|
||||
cards = await card_integration.get_user_cards(did)
|
||||
|
||||
if not cards:
|
||||
return {
|
||||
"total_cards": 0,
|
||||
"rarity_distribution": {},
|
||||
"message": "No cards found"
|
||||
}
|
||||
|
||||
# Analyze collection
|
||||
rarity_count = {}
|
||||
total_power = 0
|
||||
|
||||
for card in cards:
|
||||
rarity = card.get("rarity", "common")
|
||||
rarity_count[rarity] = rarity_count.get(rarity, 0) + 1
|
||||
total_power += card.get("power", 0)
|
||||
|
||||
return {
|
||||
"total_cards": len(cards),
|
||||
"rarity_distribution": rarity_count,
|
||||
"average_power": total_power / len(cards) if cards else 0,
|
||||
"strongest_card": max(cards, key=lambda x: x.get("power", 0)) if cards else None
|
||||
}
|
699
src/aigpt/cli.py
Normal file
699
src/aigpt/cli.py
Normal file
@@ -0,0 +1,699 @@
|
||||
"""CLI interface for ai.gpt using typer"""
|
||||
|
||||
import typer
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich.panel import Panel
|
||||
from datetime import datetime, timedelta
|
||||
import subprocess
|
||||
import shlex
|
||||
from prompt_toolkit import prompt as ptk_prompt
|
||||
from prompt_toolkit.completion import WordCompleter
|
||||
from prompt_toolkit.history import FileHistory
|
||||
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
|
||||
|
||||
from .persona import Persona
|
||||
from .transmission import TransmissionController
|
||||
from .mcp_server import AIGptMcpServer
|
||||
from .ai_provider import create_ai_provider
|
||||
from .scheduler import AIScheduler, TaskType
|
||||
from .config import Config
|
||||
|
||||
app = typer.Typer(help="ai.gpt - Autonomous transmission AI with unique personality")
|
||||
console = Console()
|
||||
|
||||
# Configuration
|
||||
config = Config()
|
||||
DEFAULT_DATA_DIR = config.data_dir
|
||||
|
||||
|
||||
def get_persona(data_dir: Optional[Path] = None) -> Persona:
|
||||
"""Get or create persona instance"""
|
||||
if data_dir is None:
|
||||
data_dir = DEFAULT_DATA_DIR
|
||||
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
return Persona(data_dir)
|
||||
|
||||
|
||||
@app.command()
|
||||
def chat(
|
||||
user_id: str = typer.Argument(..., help="User ID (atproto DID)"),
|
||||
message: str = typer.Argument(..., help="Message to send to AI"),
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory"),
|
||||
model: Optional[str] = typer.Option(None, "--model", "-m", help="AI model to use"),
|
||||
provider: Optional[str] = typer.Option(None, "--provider", help="AI provider (ollama/openai)")
|
||||
):
|
||||
"""Chat with the AI"""
|
||||
persona = get_persona(data_dir)
|
||||
|
||||
# Create AI provider if specified
|
||||
ai_provider = None
|
||||
if provider and model:
|
||||
try:
|
||||
ai_provider = create_ai_provider(provider, model)
|
||||
console.print(f"[dim]Using {provider} with model {model}[/dim]\n")
|
||||
except Exception as e:
|
||||
console.print(f"[yellow]Warning: Could not create AI provider: {e}[/yellow]")
|
||||
console.print("[yellow]Falling back to simple responses[/yellow]\n")
|
||||
|
||||
# Process interaction
|
||||
response, relationship_delta = persona.process_interaction(user_id, message, ai_provider)
|
||||
|
||||
# Get updated relationship
|
||||
relationship = persona.relationships.get_or_create_relationship(user_id)
|
||||
|
||||
# Display response
|
||||
console.print(Panel(response, title="AI Response", border_style="cyan"))
|
||||
|
||||
# Show relationship status
|
||||
status_color = "green" if relationship.transmission_enabled else "yellow"
|
||||
if relationship.is_broken:
|
||||
status_color = "red"
|
||||
|
||||
console.print(f"\n[{status_color}]Relationship Status:[/{status_color}] {relationship.status.value}")
|
||||
console.print(f"Score: {relationship.score:.2f} / {relationship.threshold}")
|
||||
console.print(f"Transmission: {'✓ Enabled' if relationship.transmission_enabled else '✗ Disabled'}")
|
||||
|
||||
if relationship.is_broken:
|
||||
console.print("[red]⚠️ This relationship is broken and cannot be repaired.[/red]")
|
||||
|
||||
|
||||
@app.command()
|
||||
def status(
|
||||
user_id: Optional[str] = typer.Argument(None, help="User ID to check status for"),
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory")
|
||||
):
|
||||
"""Check AI status and relationships"""
|
||||
persona = get_persona(data_dir)
|
||||
state = persona.get_current_state()
|
||||
|
||||
# Show AI state
|
||||
console.print(Panel(f"[cyan]ai.gpt Status[/cyan]", expand=False))
|
||||
console.print(f"Mood: {state.current_mood}")
|
||||
console.print(f"Fortune: {state.fortune.fortune_value}/10")
|
||||
|
||||
if state.fortune.breakthrough_triggered:
|
||||
console.print("[yellow]⚡ Breakthrough triggered![/yellow]")
|
||||
|
||||
# Show personality traits
|
||||
table = Table(title="Current Personality")
|
||||
table.add_column("Trait", style="cyan")
|
||||
table.add_column("Value", style="magenta")
|
||||
|
||||
for trait, value in state.base_personality.items():
|
||||
table.add_row(trait.capitalize(), f"{value:.2f}")
|
||||
|
||||
console.print(table)
|
||||
|
||||
# Show specific relationship if requested
|
||||
if user_id:
|
||||
rel = persona.relationships.get_or_create_relationship(user_id)
|
||||
console.print(f"\n[cyan]Relationship with {user_id}:[/cyan]")
|
||||
console.print(f"Status: {rel.status.value}")
|
||||
console.print(f"Score: {rel.score:.2f}")
|
||||
console.print(f"Total Interactions: {rel.total_interactions}")
|
||||
console.print(f"Transmission Enabled: {rel.transmission_enabled}")
|
||||
|
||||
|
||||
@app.command()
|
||||
def fortune(
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory")
|
||||
):
|
||||
"""Check today's AI fortune"""
|
||||
persona = get_persona(data_dir)
|
||||
fortune = persona.fortune_system.get_today_fortune()
|
||||
|
||||
# Fortune display
|
||||
fortune_bar = "🌟" * fortune.fortune_value + "☆" * (10 - fortune.fortune_value)
|
||||
|
||||
console.print(Panel(
|
||||
f"{fortune_bar}\n\n"
|
||||
f"Today's Fortune: {fortune.fortune_value}/10\n"
|
||||
f"Date: {fortune.date}",
|
||||
title="AI Fortune",
|
||||
border_style="yellow"
|
||||
))
|
||||
|
||||
if fortune.consecutive_good > 0:
|
||||
console.print(f"[green]Consecutive good days: {fortune.consecutive_good}[/green]")
|
||||
if fortune.consecutive_bad > 0:
|
||||
console.print(f"[red]Consecutive bad days: {fortune.consecutive_bad}[/red]")
|
||||
|
||||
if fortune.breakthrough_triggered:
|
||||
console.print("\n[yellow]⚡ BREAKTHROUGH! Special fortune activated![/yellow]")
|
||||
|
||||
|
||||
@app.command()
|
||||
def transmit(
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory"),
|
||||
dry_run: bool = typer.Option(True, "--dry-run/--execute", help="Dry run or execute")
|
||||
):
|
||||
"""Check and execute autonomous transmissions"""
|
||||
persona = get_persona(data_dir)
|
||||
controller = TransmissionController(persona, persona.data_dir)
|
||||
|
||||
eligible = controller.check_transmission_eligibility()
|
||||
|
||||
if not eligible:
|
||||
console.print("[yellow]No users eligible for transmission.[/yellow]")
|
||||
return
|
||||
|
||||
console.print(f"[green]Found {len(eligible)} eligible users for transmission:[/green]")
|
||||
|
||||
for user_id, rel in eligible.items():
|
||||
message = controller.generate_transmission_message(user_id)
|
||||
if message:
|
||||
console.print(f"\n[cyan]To:[/cyan] {user_id}")
|
||||
console.print(f"[cyan]Message:[/cyan] {message}")
|
||||
console.print(f"[cyan]Relationship:[/cyan] {rel.status.value} (score: {rel.score:.2f})")
|
||||
|
||||
if not dry_run:
|
||||
# In real implementation, send via atproto or other channel
|
||||
controller.record_transmission(user_id, message, success=True)
|
||||
console.print("[green]✓ Transmitted[/green]")
|
||||
else:
|
||||
console.print("[yellow]→ Would transmit (dry run)[/yellow]")
|
||||
|
||||
|
||||
@app.command()
|
||||
def maintenance(
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory")
|
||||
):
|
||||
"""Run daily maintenance tasks"""
|
||||
persona = get_persona(data_dir)
|
||||
|
||||
console.print("[cyan]Running daily maintenance...[/cyan]")
|
||||
persona.daily_maintenance()
|
||||
console.print("[green]✓ Maintenance completed[/green]")
|
||||
|
||||
|
||||
@app.command()
|
||||
def relationships(
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory")
|
||||
):
|
||||
"""List all relationships"""
|
||||
persona = get_persona(data_dir)
|
||||
|
||||
table = Table(title="All Relationships")
|
||||
table.add_column("User ID", style="cyan")
|
||||
table.add_column("Status", style="magenta")
|
||||
table.add_column("Score", style="green")
|
||||
table.add_column("Transmission", style="yellow")
|
||||
table.add_column("Last Interaction")
|
||||
|
||||
for user_id, rel in persona.relationships.relationships.items():
|
||||
transmission = "✓" if rel.transmission_enabled else "✗"
|
||||
if rel.is_broken:
|
||||
transmission = "💔"
|
||||
|
||||
last_interaction = rel.last_interaction.strftime("%Y-%m-%d") if rel.last_interaction else "Never"
|
||||
|
||||
table.add_row(
|
||||
user_id[:16] + "...",
|
||||
rel.status.value,
|
||||
f"{rel.score:.2f}",
|
||||
transmission,
|
||||
last_interaction
|
||||
)
|
||||
|
||||
console.print(table)
|
||||
|
||||
|
||||
@app.command()
|
||||
def server(
|
||||
host: str = typer.Option("localhost", "--host", "-h", help="Server host"),
|
||||
port: int = typer.Option(8000, "--port", "-p", help="Server port"),
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory"),
|
||||
model: str = typer.Option("qwen2.5", "--model", "-m", help="AI model to use"),
|
||||
provider: str = typer.Option("ollama", "--provider", help="AI provider (ollama/openai)"),
|
||||
enable_card: bool = typer.Option(False, "--enable-card", help="Enable ai.card integration")
|
||||
):
|
||||
"""Run MCP server for AI integration"""
|
||||
import uvicorn
|
||||
|
||||
if data_dir is None:
|
||||
data_dir = DEFAULT_DATA_DIR
|
||||
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Create MCP server
|
||||
mcp_server = AIGptMcpServer(data_dir, enable_card_integration=enable_card)
|
||||
app_instance = mcp_server.app
|
||||
|
||||
console.print(Panel(
|
||||
f"[cyan]Starting ai.gpt MCP Server[/cyan]\n\n"
|
||||
f"Host: {host}:{port}\n"
|
||||
f"Provider: {provider}\n"
|
||||
f"Model: {model}\n"
|
||||
f"Data: {data_dir}\n"
|
||||
f"Card Integration: {'✓ Enabled' if enable_card else '✗ Disabled'}",
|
||||
title="MCP Server",
|
||||
border_style="green"
|
||||
))
|
||||
|
||||
# Store provider info in app state for later use
|
||||
app_instance.state.ai_provider = provider
|
||||
app_instance.state.ai_model = model
|
||||
|
||||
# Run server
|
||||
uvicorn.run(app_instance, host=host, port=port)
|
||||
|
||||
|
||||
@app.command()
|
||||
def schedule(
|
||||
action: str = typer.Argument(..., help="Action: add, list, enable, disable, remove, run"),
|
||||
task_type: Optional[str] = typer.Argument(None, help="Task type for add action"),
|
||||
schedule_expr: Optional[str] = typer.Argument(None, help="Schedule expression (cron or interval)"),
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory"),
|
||||
task_id: Optional[str] = typer.Option(None, "--task-id", "-t", help="Task ID"),
|
||||
provider: Optional[str] = typer.Option(None, "--provider", help="AI provider for transmission"),
|
||||
model: Optional[str] = typer.Option(None, "--model", "-m", help="AI model for transmission")
|
||||
):
|
||||
"""Manage scheduled tasks"""
|
||||
persona = get_persona(data_dir)
|
||||
scheduler = AIScheduler(persona.data_dir, persona)
|
||||
|
||||
if action == "add":
|
||||
if not task_type or not schedule_expr:
|
||||
console.print("[red]Error: task_type and schedule required for add action[/red]")
|
||||
return
|
||||
|
||||
# Parse task type
|
||||
try:
|
||||
task_type_enum = TaskType(task_type)
|
||||
except ValueError:
|
||||
console.print(f"[red]Invalid task type. Valid types: {', '.join([t.value for t in TaskType])}[/red]")
|
||||
return
|
||||
|
||||
# Metadata for transmission tasks
|
||||
metadata = {}
|
||||
if task_type_enum == TaskType.TRANSMISSION_CHECK:
|
||||
metadata["provider"] = provider or "ollama"
|
||||
metadata["model"] = model or "qwen2.5"
|
||||
|
||||
try:
|
||||
task = scheduler.add_task(task_type_enum, schedule_expr, task_id, metadata)
|
||||
console.print(f"[green]✓ Added task {task.task_id}[/green]")
|
||||
console.print(f"Type: {task.task_type.value}")
|
||||
console.print(f"Schedule: {task.schedule}")
|
||||
except ValueError as e:
|
||||
console.print(f"[red]Error: {e}[/red]")
|
||||
|
||||
elif action == "list":
|
||||
tasks = scheduler.get_tasks()
|
||||
if not tasks:
|
||||
console.print("[yellow]No scheduled tasks[/yellow]")
|
||||
return
|
||||
|
||||
table = Table(title="Scheduled Tasks")
|
||||
table.add_column("Task ID", style="cyan")
|
||||
table.add_column("Type", style="magenta")
|
||||
table.add_column("Schedule", style="green")
|
||||
table.add_column("Enabled", style="yellow")
|
||||
table.add_column("Last Run")
|
||||
|
||||
for task in tasks:
|
||||
enabled = "✓" if task.enabled else "✗"
|
||||
last_run = task.last_run.strftime("%Y-%m-%d %H:%M") if task.last_run else "Never"
|
||||
|
||||
table.add_row(
|
||||
task.task_id[:20] + "..." if len(task.task_id) > 20 else task.task_id,
|
||||
task.task_type.value,
|
||||
task.schedule,
|
||||
enabled,
|
||||
last_run
|
||||
)
|
||||
|
||||
console.print(table)
|
||||
|
||||
elif action == "enable":
|
||||
if not task_id:
|
||||
console.print("[red]Error: --task-id required for enable action[/red]")
|
||||
return
|
||||
|
||||
scheduler.enable_task(task_id)
|
||||
console.print(f"[green]✓ Enabled task {task_id}[/green]")
|
||||
|
||||
elif action == "disable":
|
||||
if not task_id:
|
||||
console.print("[red]Error: --task-id required for disable action[/red]")
|
||||
return
|
||||
|
||||
scheduler.disable_task(task_id)
|
||||
console.print(f"[yellow]✓ Disabled task {task_id}[/yellow]")
|
||||
|
||||
elif action == "remove":
|
||||
if not task_id:
|
||||
console.print("[red]Error: --task-id required for remove action[/red]")
|
||||
return
|
||||
|
||||
scheduler.remove_task(task_id)
|
||||
console.print(f"[red]✓ Removed task {task_id}[/red]")
|
||||
|
||||
elif action == "run":
|
||||
console.print("[cyan]Starting scheduler daemon...[/cyan]")
|
||||
console.print("Press Ctrl+C to stop\n")
|
||||
|
||||
import asyncio
|
||||
|
||||
async def run_scheduler():
|
||||
scheduler.start()
|
||||
try:
|
||||
while True:
|
||||
await asyncio.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
scheduler.stop()
|
||||
|
||||
try:
|
||||
asyncio.run(run_scheduler())
|
||||
except KeyboardInterrupt:
|
||||
console.print("\n[yellow]Scheduler stopped[/yellow]")
|
||||
|
||||
else:
|
||||
console.print(f"[red]Unknown action: {action}[/red]")
|
||||
console.print("Valid actions: add, list, enable, disable, remove, run")
|
||||
|
||||
|
||||
@app.command()
|
||||
def shell(
|
||||
data_dir: Optional[Path] = typer.Option(None, "--data-dir", "-d", help="Data directory"),
|
||||
model: Optional[str] = typer.Option("qwen2.5", "--model", "-m", help="AI model to use"),
|
||||
provider: Optional[str] = typer.Option("ollama", "--provider", help="AI provider (ollama/openai)")
|
||||
):
|
||||
"""Interactive shell mode (ai.shell)"""
|
||||
persona = get_persona(data_dir)
|
||||
|
||||
# Create AI provider
|
||||
ai_provider = None
|
||||
if provider and model:
|
||||
try:
|
||||
ai_provider = create_ai_provider(provider, model)
|
||||
console.print(f"[dim]Using {provider} with model {model}[/dim]\n")
|
||||
except Exception as e:
|
||||
console.print(f"[yellow]Warning: Could not create AI provider: {e}[/yellow]")
|
||||
console.print("[yellow]Falling back to simple responses[/yellow]\n")
|
||||
|
||||
# Welcome message
|
||||
console.print(Panel(
|
||||
"[cyan]Welcome to ai.shell[/cyan]\n\n"
|
||||
"Interactive AI-powered shell with command execution\n\n"
|
||||
"Commands:\n"
|
||||
" help - Show available commands\n"
|
||||
" exit/quit - Exit shell\n"
|
||||
" !<command> - Execute shell command\n"
|
||||
" chat <message> - Chat with AI\n"
|
||||
" status - Show AI status\n"
|
||||
" clear - Clear screen\n\n"
|
||||
"Type any message to interact with AI",
|
||||
title="ai.shell",
|
||||
border_style="green"
|
||||
))
|
||||
|
||||
# Command completer with shell commands
|
||||
builtin_commands = ['help', 'exit', 'quit', 'chat', 'status', 'clear', 'fortune', 'relationships', 'load']
|
||||
|
||||
# Add common shell commands
|
||||
shell_commands = ['ls', 'cd', 'pwd', 'cat', 'echo', 'grep', 'find', 'mkdir', 'rm', 'cp', 'mv',
|
||||
'git', 'python', 'pip', 'npm', 'node', 'cargo', 'rustc', 'docker', 'kubectl']
|
||||
|
||||
# AI-specific commands
|
||||
ai_commands = ['analyze', 'generate', 'explain', 'optimize', 'refactor', 'test', 'document']
|
||||
|
||||
all_commands = builtin_commands + ['!' + cmd for cmd in shell_commands] + ai_commands
|
||||
completer = WordCompleter(all_commands, ignore_case=True)
|
||||
|
||||
# History file
|
||||
actual_data_dir = data_dir if data_dir else DEFAULT_DATA_DIR
|
||||
history_file = actual_data_dir / "shell_history.txt"
|
||||
history = FileHistory(str(history_file))
|
||||
|
||||
# Main shell loop
|
||||
current_user = "shell_user" # Default user for shell sessions
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Get input with completion
|
||||
user_input = ptk_prompt(
|
||||
"ai.shell> ",
|
||||
completer=completer,
|
||||
history=history,
|
||||
auto_suggest=AutoSuggestFromHistory()
|
||||
).strip()
|
||||
|
||||
if not user_input:
|
||||
continue
|
||||
|
||||
# Exit commands
|
||||
if user_input.lower() in ['exit', 'quit']:
|
||||
console.print("[cyan]Goodbye![/cyan]")
|
||||
break
|
||||
|
||||
# Help command
|
||||
elif user_input.lower() == 'help':
|
||||
console.print(Panel(
|
||||
"[cyan]ai.shell Commands:[/cyan]\n\n"
|
||||
" help - Show this help message\n"
|
||||
" exit/quit - Exit the shell\n"
|
||||
" !<command> - Execute a shell command\n"
|
||||
" chat <message> - Explicitly chat with AI\n"
|
||||
" status - Show AI status\n"
|
||||
" fortune - Check AI fortune\n"
|
||||
" relationships - List all relationships\n"
|
||||
" clear - Clear the screen\n"
|
||||
" load - Load aishell.md project file\n\n"
|
||||
"[cyan]AI Commands:[/cyan]\n"
|
||||
" analyze <file> - Analyze a file with AI\n"
|
||||
" generate <desc> - Generate code from description\n"
|
||||
" explain <topic> - Get AI explanation\n\n"
|
||||
"You can also type any message to chat with AI\n"
|
||||
"Use Tab for command completion",
|
||||
title="Help",
|
||||
border_style="yellow"
|
||||
))
|
||||
|
||||
# Clear command
|
||||
elif user_input.lower() == 'clear':
|
||||
console.clear()
|
||||
|
||||
# Shell command execution
|
||||
elif user_input.startswith('!'):
|
||||
cmd = user_input[1:].strip()
|
||||
if cmd:
|
||||
try:
|
||||
# Execute command
|
||||
result = subprocess.run(
|
||||
shlex.split(cmd),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
shell=False
|
||||
)
|
||||
|
||||
if result.stdout:
|
||||
console.print(result.stdout.rstrip())
|
||||
if result.stderr:
|
||||
console.print(f"[red]{result.stderr.rstrip()}[/red]")
|
||||
|
||||
if result.returncode != 0:
|
||||
console.print(f"[red]Command exited with code {result.returncode}[/red]")
|
||||
except FileNotFoundError:
|
||||
console.print(f"[red]Command not found: {cmd.split()[0]}[/red]")
|
||||
except Exception as e:
|
||||
console.print(f"[red]Error executing command: {e}[/red]")
|
||||
|
||||
# Status command
|
||||
elif user_input.lower() == 'status':
|
||||
state = persona.get_current_state()
|
||||
console.print(f"\nMood: {state.current_mood}")
|
||||
console.print(f"Fortune: {state.fortune.fortune_value}/10")
|
||||
|
||||
rel = persona.relationships.get_or_create_relationship(current_user)
|
||||
console.print(f"\nRelationship Status: {rel.status.value}")
|
||||
console.print(f"Score: {rel.score:.2f} / {rel.threshold}")
|
||||
|
||||
# Fortune command
|
||||
elif user_input.lower() == 'fortune':
|
||||
fortune = persona.fortune_system.get_today_fortune()
|
||||
fortune_bar = "🌟" * fortune.fortune_value + "☆" * (10 - fortune.fortune_value)
|
||||
console.print(f"\n{fortune_bar}")
|
||||
console.print(f"Today's Fortune: {fortune.fortune_value}/10")
|
||||
|
||||
# Relationships command
|
||||
elif user_input.lower() == 'relationships':
|
||||
if persona.relationships.relationships:
|
||||
console.print("\n[cyan]Relationships:[/cyan]")
|
||||
for user_id, rel in persona.relationships.relationships.items():
|
||||
console.print(f" {user_id[:16]}... - {rel.status.value} ({rel.score:.2f})")
|
||||
else:
|
||||
console.print("[yellow]No relationships yet[/yellow]")
|
||||
|
||||
# Load aishell.md command
|
||||
elif user_input.lower() in ['load', 'load aishell.md', 'project']:
|
||||
# Try to find and load aishell.md
|
||||
search_paths = [
|
||||
Path.cwd() / "aishell.md",
|
||||
Path.cwd() / "docs" / "aishell.md",
|
||||
actual_data_dir.parent / "aishell.md",
|
||||
Path.cwd() / "claude.md", # Also check for claude.md
|
||||
]
|
||||
|
||||
loaded = False
|
||||
for path in search_paths:
|
||||
if path.exists():
|
||||
console.print(f"[cyan]Loading project file: {path}[/cyan]")
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# Process with AI to understand project
|
||||
load_prompt = f"I've loaded the project specification. Please analyze it and understand the project goals:\n\n{content[:3000]}"
|
||||
response, _ = persona.process_interaction(current_user, load_prompt, ai_provider)
|
||||
console.print(f"\n[green]Project loaded successfully![/green]")
|
||||
console.print(f"[cyan]AI Understanding:[/cyan]\n{response}")
|
||||
loaded = True
|
||||
break
|
||||
|
||||
if not loaded:
|
||||
console.print("[yellow]No aishell.md or claude.md found in project.[/yellow]")
|
||||
console.print("Create aishell.md to define project goals and AI instructions.")
|
||||
|
||||
# AI-powered commands
|
||||
elif user_input.lower().startswith('analyze '):
|
||||
# Analyze file or code
|
||||
target = user_input[8:].strip()
|
||||
if os.path.exists(target):
|
||||
console.print(f"[cyan]Analyzing {target}...[/cyan]")
|
||||
with open(target, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
analysis_prompt = f"Analyze this file and provide insights:\n\n{content[:2000]}"
|
||||
response, _ = persona.process_interaction(current_user, analysis_prompt, ai_provider)
|
||||
console.print(f"\n[cyan]Analysis:[/cyan]\n{response}")
|
||||
else:
|
||||
console.print(f"[red]File not found: {target}[/red]")
|
||||
|
||||
elif user_input.lower().startswith('generate '):
|
||||
# Generate code
|
||||
gen_prompt = user_input[9:].strip()
|
||||
if gen_prompt:
|
||||
console.print("[cyan]Generating code...[/cyan]")
|
||||
full_prompt = f"Generate code for: {gen_prompt}. Provide clean, well-commented code."
|
||||
response, _ = persona.process_interaction(current_user, full_prompt, ai_provider)
|
||||
console.print(f"\n[cyan]Generated Code:[/cyan]\n{response}")
|
||||
|
||||
elif user_input.lower().startswith('explain '):
|
||||
# Explain code or concept
|
||||
topic = user_input[8:].strip()
|
||||
if topic:
|
||||
console.print(f"[cyan]Explaining {topic}...[/cyan]")
|
||||
full_prompt = f"Explain this in detail: {topic}"
|
||||
response, _ = persona.process_interaction(current_user, full_prompt, ai_provider)
|
||||
console.print(f"\n[cyan]Explanation:[/cyan]\n{response}")
|
||||
|
||||
# Chat command or direct message
|
||||
else:
|
||||
# Remove 'chat' prefix if present
|
||||
if user_input.lower().startswith('chat '):
|
||||
message = user_input[5:].strip()
|
||||
else:
|
||||
message = user_input
|
||||
|
||||
if message:
|
||||
# Process interaction with AI
|
||||
response, relationship_delta = persona.process_interaction(
|
||||
current_user, message, ai_provider
|
||||
)
|
||||
|
||||
# Display response
|
||||
console.print(f"\n[cyan]AI:[/cyan] {response}")
|
||||
|
||||
# Show relationship change if significant
|
||||
if abs(relationship_delta) >= 0.1:
|
||||
if relationship_delta > 0:
|
||||
console.print(f"[green](+{relationship_delta:.2f} relationship)[/green]")
|
||||
else:
|
||||
console.print(f"[red]({relationship_delta:.2f} relationship)[/red]")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
console.print("\n[yellow]Use 'exit' or 'quit' to leave the shell[/yellow]")
|
||||
except EOFError:
|
||||
console.print("\n[cyan]Goodbye![/cyan]")
|
||||
break
|
||||
except Exception as e:
|
||||
console.print(f"[red]Error: {e}[/red]")
|
||||
|
||||
|
||||
@app.command()
|
||||
def config(
|
||||
action: str = typer.Argument(..., help="Action: get, set, delete, list"),
|
||||
key: Optional[str] = typer.Argument(None, help="Configuration key (dot notation)"),
|
||||
value: Optional[str] = typer.Argument(None, help="Value to set")
|
||||
):
|
||||
"""Manage configuration settings"""
|
||||
|
||||
if action == "get":
|
||||
if not key:
|
||||
console.print("[red]Error: key required for get action[/red]")
|
||||
return
|
||||
|
||||
val = config.get(key)
|
||||
if val is None:
|
||||
console.print(f"[yellow]Key '{key}' not found[/yellow]")
|
||||
else:
|
||||
console.print(f"[cyan]{key}[/cyan] = [green]{val}[/green]")
|
||||
|
||||
elif action == "set":
|
||||
if not key or value is None:
|
||||
console.print("[red]Error: key and value required for set action[/red]")
|
||||
return
|
||||
|
||||
# Special handling for sensitive keys
|
||||
if "password" in key or "api_key" in key:
|
||||
console.print(f"[cyan]Setting {key}[/cyan] = [dim]***hidden***[/dim]")
|
||||
else:
|
||||
console.print(f"[cyan]Setting {key}[/cyan] = [green]{value}[/green]")
|
||||
|
||||
config.set(key, value)
|
||||
console.print("[green]✓ Configuration saved[/green]")
|
||||
|
||||
elif action == "delete":
|
||||
if not key:
|
||||
console.print("[red]Error: key required for delete action[/red]")
|
||||
return
|
||||
|
||||
if config.delete(key):
|
||||
console.print(f"[green]✓ Deleted {key}[/green]")
|
||||
else:
|
||||
console.print(f"[yellow]Key '{key}' not found[/yellow]")
|
||||
|
||||
elif action == "list":
|
||||
keys = config.list_keys(key or "")
|
||||
|
||||
if not keys:
|
||||
console.print("[yellow]No configuration keys found[/yellow]")
|
||||
return
|
||||
|
||||
table = Table(title="Configuration Settings")
|
||||
table.add_column("Key", style="cyan")
|
||||
table.add_column("Value", style="green")
|
||||
|
||||
for k in sorted(keys):
|
||||
val = config.get(k)
|
||||
# Hide sensitive values
|
||||
if "password" in k or "api_key" in k:
|
||||
display_val = "***hidden***" if val else "not set"
|
||||
else:
|
||||
display_val = str(val) if val is not None else "not set"
|
||||
|
||||
table.add_row(k, display_val)
|
||||
|
||||
console.print(table)
|
||||
|
||||
else:
|
||||
console.print(f"[red]Unknown action: {action}[/red]")
|
||||
console.print("Valid actions: get, set, delete, list")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app()
|
145
src/aigpt/config.py
Normal file
145
src/aigpt/config.py
Normal file
@@ -0,0 +1,145 @@
|
||||
"""Configuration management for ai.gpt"""
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, Any
|
||||
import logging
|
||||
|
||||
|
||||
class Config:
|
||||
"""Manages configuration settings"""
|
||||
|
||||
def __init__(self, config_dir: Optional[Path] = None):
|
||||
if config_dir is None:
|
||||
config_dir = Path.home() / ".config" / "syui" / "ai" / "gpt"
|
||||
|
||||
self.config_dir = config_dir
|
||||
self.config_file = config_dir / "config.json"
|
||||
self.data_dir = config_dir / "data"
|
||||
|
||||
# Create directories if they don't exist
|
||||
self.config_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self._config: Dict[str, Any] = {}
|
||||
self._load_config()
|
||||
|
||||
def _load_config(self):
|
||||
"""Load configuration from file"""
|
||||
if self.config_file.exists():
|
||||
try:
|
||||
with open(self.config_file, 'r', encoding='utf-8') as f:
|
||||
self._config = json.load(f)
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to load config: {e}")
|
||||
self._config = {}
|
||||
else:
|
||||
# Initialize with default config
|
||||
self._config = {
|
||||
"providers": {
|
||||
"openai": {
|
||||
"api_key": None,
|
||||
"default_model": "gpt-4o-mini"
|
||||
},
|
||||
"ollama": {
|
||||
"host": "http://localhost:11434",
|
||||
"default_model": "qwen2.5"
|
||||
}
|
||||
},
|
||||
"atproto": {
|
||||
"handle": None,
|
||||
"password": None,
|
||||
"host": "https://bsky.social"
|
||||
},
|
||||
"default_provider": "ollama"
|
||||
}
|
||||
self._save_config()
|
||||
|
||||
def _save_config(self):
|
||||
"""Save configuration to file"""
|
||||
try:
|
||||
with open(self.config_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(self._config, f, indent=2)
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to save config: {e}")
|
||||
|
||||
def get(self, key: str, default: Any = None) -> Any:
|
||||
"""Get configuration value using dot notation"""
|
||||
keys = key.split('.')
|
||||
value = self._config
|
||||
|
||||
for k in keys:
|
||||
if isinstance(value, dict) and k in value:
|
||||
value = value[k]
|
||||
else:
|
||||
return default
|
||||
|
||||
return value
|
||||
|
||||
def set(self, key: str, value: Any):
|
||||
"""Set configuration value using dot notation"""
|
||||
keys = key.split('.')
|
||||
config = self._config
|
||||
|
||||
# Navigate to the parent dictionary
|
||||
for k in keys[:-1]:
|
||||
if k not in config:
|
||||
config[k] = {}
|
||||
config = config[k]
|
||||
|
||||
# Set the value
|
||||
config[keys[-1]] = value
|
||||
self._save_config()
|
||||
|
||||
def delete(self, key: str) -> bool:
|
||||
"""Delete configuration value"""
|
||||
keys = key.split('.')
|
||||
config = self._config
|
||||
|
||||
# Navigate to the parent dictionary
|
||||
for k in keys[:-1]:
|
||||
if k not in config:
|
||||
return False
|
||||
config = config[k]
|
||||
|
||||
# Delete the key if it exists
|
||||
if keys[-1] in config:
|
||||
del config[keys[-1]]
|
||||
self._save_config()
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def list_keys(self, prefix: str = "") -> list[str]:
|
||||
"""List all configuration keys with optional prefix"""
|
||||
def _get_keys(config: dict, current_prefix: str = "") -> list[str]:
|
||||
keys = []
|
||||
for k, v in config.items():
|
||||
full_key = f"{current_prefix}.{k}" if current_prefix else k
|
||||
if isinstance(v, dict):
|
||||
keys.extend(_get_keys(v, full_key))
|
||||
else:
|
||||
keys.append(full_key)
|
||||
return keys
|
||||
|
||||
all_keys = _get_keys(self._config)
|
||||
|
||||
if prefix:
|
||||
return [k for k in all_keys if k.startswith(prefix)]
|
||||
return all_keys
|
||||
|
||||
def get_api_key(self, provider: str) -> Optional[str]:
|
||||
"""Get API key for a specific provider"""
|
||||
key = self.get(f"providers.{provider}.api_key")
|
||||
|
||||
# Also check environment variables
|
||||
if not key and provider == "openai":
|
||||
key = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
return key
|
||||
|
||||
def get_provider_config(self, provider: str) -> Dict[str, Any]:
|
||||
"""Get complete configuration for a provider"""
|
||||
return self.get(f"providers.{provider}", {})
|
118
src/aigpt/fortune.py
Normal file
118
src/aigpt/fortune.py
Normal file
@@ -0,0 +1,118 @@
|
||||
"""AI Fortune system for daily personality variations"""
|
||||
|
||||
import json
|
||||
import random
|
||||
from datetime import date, datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import logging
|
||||
|
||||
from .models import AIFortune
|
||||
|
||||
|
||||
class FortuneSystem:
|
||||
"""Manages daily AI fortune affecting personality"""
|
||||
|
||||
def __init__(self, data_dir: Path):
|
||||
self.data_dir = data_dir
|
||||
self.fortune_file = data_dir / "fortunes.json"
|
||||
self.fortunes: dict[str, AIFortune] = {}
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self._load_fortunes()
|
||||
|
||||
def _load_fortunes(self):
|
||||
"""Load fortune history from storage"""
|
||||
if self.fortune_file.exists():
|
||||
with open(self.fortune_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
for date_str, fortune_data in data.items():
|
||||
# Convert date string back to date object
|
||||
fortune_data['date'] = datetime.fromisoformat(fortune_data['date']).date()
|
||||
self.fortunes[date_str] = AIFortune(**fortune_data)
|
||||
|
||||
def _save_fortunes(self):
|
||||
"""Save fortune history to storage"""
|
||||
data = {}
|
||||
for date_str, fortune in self.fortunes.items():
|
||||
fortune_dict = fortune.model_dump(mode='json')
|
||||
fortune_dict['date'] = fortune.date.isoformat()
|
||||
data[date_str] = fortune_dict
|
||||
|
||||
with open(self.fortune_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
def get_today_fortune(self) -> AIFortune:
|
||||
"""Get or generate today's fortune"""
|
||||
today = date.today()
|
||||
today_str = today.isoformat()
|
||||
|
||||
if today_str in self.fortunes:
|
||||
return self.fortunes[today_str]
|
||||
|
||||
# Generate new fortune
|
||||
fortune_value = random.randint(1, 10)
|
||||
|
||||
# Check yesterday's fortune for consecutive tracking
|
||||
yesterday = (today - timedelta(days=1))
|
||||
yesterday_str = yesterday.isoformat()
|
||||
|
||||
consecutive_good = 0
|
||||
consecutive_bad = 0
|
||||
breakthrough_triggered = False
|
||||
|
||||
if yesterday_str in self.fortunes:
|
||||
yesterday_fortune = self.fortunes[yesterday_str]
|
||||
|
||||
if fortune_value >= 7: # Good fortune
|
||||
if yesterday_fortune.fortune_value >= 7:
|
||||
consecutive_good = yesterday_fortune.consecutive_good + 1
|
||||
else:
|
||||
consecutive_good = 1
|
||||
elif fortune_value <= 3: # Bad fortune
|
||||
if yesterday_fortune.fortune_value <= 3:
|
||||
consecutive_bad = yesterday_fortune.consecutive_bad + 1
|
||||
else:
|
||||
consecutive_bad = 1
|
||||
|
||||
# Check breakthrough conditions
|
||||
if consecutive_good >= 3:
|
||||
breakthrough_triggered = True
|
||||
self.logger.info("Breakthrough! 3 consecutive good fortunes!")
|
||||
fortune_value = 10 # Max fortune on breakthrough
|
||||
elif consecutive_bad >= 3:
|
||||
breakthrough_triggered = True
|
||||
self.logger.info("Breakthrough! 3 consecutive bad fortunes!")
|
||||
fortune_value = random.randint(7, 10) # Good fortune after bad streak
|
||||
|
||||
fortune = AIFortune(
|
||||
date=today,
|
||||
fortune_value=fortune_value,
|
||||
consecutive_good=consecutive_good,
|
||||
consecutive_bad=consecutive_bad,
|
||||
breakthrough_triggered=breakthrough_triggered
|
||||
)
|
||||
|
||||
self.fortunes[today_str] = fortune
|
||||
self._save_fortunes()
|
||||
|
||||
self.logger.info(f"Today's fortune: {fortune_value}/10")
|
||||
return fortune
|
||||
|
||||
def get_personality_modifier(self, fortune: AIFortune) -> dict[str, float]:
|
||||
"""Get personality modifiers based on fortune"""
|
||||
base_modifier = fortune.fortune_value / 10.0
|
||||
|
||||
modifiers = {
|
||||
"optimism": base_modifier,
|
||||
"energy": base_modifier * 0.8,
|
||||
"patience": 1.0 - (abs(5.5 - fortune.fortune_value) * 0.1),
|
||||
"creativity": 0.5 + (base_modifier * 0.5),
|
||||
"empathy": 0.7 + (base_modifier * 0.3)
|
||||
}
|
||||
|
||||
# Breakthrough effects
|
||||
if fortune.breakthrough_triggered:
|
||||
modifiers["confidence"] = 1.0
|
||||
modifiers["spontaneity"] = 0.9
|
||||
|
||||
return modifiers
|
318
src/aigpt/mcp_server.py
Normal file
318
src/aigpt/mcp_server.py
Normal file
@@ -0,0 +1,318 @@
|
||||
"""MCP Server for ai.gpt system"""
|
||||
|
||||
from typing import Optional, List, Dict, Any
|
||||
from fastapi_mcp import FastApiMCP
|
||||
from fastapi import FastAPI
|
||||
from pathlib import Path
|
||||
import logging
|
||||
import subprocess
|
||||
import os
|
||||
import shlex
|
||||
from .ai_provider import create_ai_provider
|
||||
|
||||
from .persona import Persona
|
||||
from .models import Memory, Relationship, PersonaState
|
||||
from .card_integration import CardIntegration, register_card_tools
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AIGptMcpServer:
|
||||
"""MCP Server that exposes ai.gpt functionality to AI assistants"""
|
||||
|
||||
def __init__(self, data_dir: Path, enable_card_integration: bool = False):
|
||||
self.data_dir = data_dir
|
||||
self.persona = Persona(data_dir)
|
||||
|
||||
# Create FastAPI app
|
||||
self.app = FastAPI(
|
||||
title="AI.GPT Memory and Relationship System",
|
||||
description="MCP server for ai.gpt system"
|
||||
)
|
||||
|
||||
# Create MCP server with FastAPI app
|
||||
self.server = FastApiMCP(self.app)
|
||||
self.card_integration = None
|
||||
|
||||
if enable_card_integration:
|
||||
self.card_integration = CardIntegration()
|
||||
|
||||
self._register_tools()
|
||||
|
||||
def _register_tools(self):
|
||||
"""Register all MCP tools"""
|
||||
|
||||
@self.app.get("/get_memories", operation_id="get_memories")
|
||||
async def get_memories(user_id: Optional[str] = None, limit: int = 10) -> List[Dict[str, Any]]:
|
||||
"""Get active memories from the AI's memory system"""
|
||||
memories = self.persona.memory.get_active_memories(limit=limit)
|
||||
return [
|
||||
{
|
||||
"id": mem.id,
|
||||
"content": mem.content,
|
||||
"level": mem.level.value,
|
||||
"importance": mem.importance_score,
|
||||
"is_core": mem.is_core,
|
||||
"timestamp": mem.timestamp.isoformat()
|
||||
}
|
||||
for mem in memories
|
||||
]
|
||||
|
||||
@self.app.get("/get_relationship", operation_id="get_relationship")
|
||||
async def get_relationship(user_id: str) -> Dict[str, Any]:
|
||||
"""Get relationship status with a specific user"""
|
||||
rel = self.persona.relationships.get_or_create_relationship(user_id)
|
||||
return {
|
||||
"user_id": rel.user_id,
|
||||
"status": rel.status.value,
|
||||
"score": rel.score,
|
||||
"transmission_enabled": rel.transmission_enabled,
|
||||
"is_broken": rel.is_broken,
|
||||
"total_interactions": rel.total_interactions,
|
||||
"last_interaction": rel.last_interaction.isoformat() if rel.last_interaction else None
|
||||
}
|
||||
|
||||
@self.app.get("/get_all_relationships", operation_id="get_all_relationships")
|
||||
async def get_all_relationships() -> List[Dict[str, Any]]:
|
||||
"""Get all relationships"""
|
||||
relationships = []
|
||||
for user_id, rel in self.persona.relationships.relationships.items():
|
||||
relationships.append({
|
||||
"user_id": user_id,
|
||||
"status": rel.status.value,
|
||||
"score": rel.score,
|
||||
"transmission_enabled": rel.transmission_enabled,
|
||||
"is_broken": rel.is_broken
|
||||
})
|
||||
return relationships
|
||||
|
||||
@self.app.get("/get_persona_state", operation_id="get_persona_state")
|
||||
async def get_persona_state() -> Dict[str, Any]:
|
||||
"""Get current persona state including fortune and mood"""
|
||||
state = self.persona.get_current_state()
|
||||
return {
|
||||
"mood": state.current_mood,
|
||||
"fortune": {
|
||||
"value": state.fortune.fortune_value,
|
||||
"date": state.fortune.date.isoformat(),
|
||||
"breakthrough": state.fortune.breakthrough_triggered
|
||||
},
|
||||
"personality": state.base_personality,
|
||||
"active_memory_count": len(state.active_memories)
|
||||
}
|
||||
|
||||
@self.app.post("/process_interaction", operation_id="process_interaction")
|
||||
async def process_interaction(user_id: str, message: str) -> Dict[str, Any]:
|
||||
"""Process an interaction with a user"""
|
||||
response, relationship_delta = self.persona.process_interaction(user_id, message)
|
||||
rel = self.persona.relationships.get_or_create_relationship(user_id)
|
||||
|
||||
return {
|
||||
"response": response,
|
||||
"relationship_delta": relationship_delta,
|
||||
"new_relationship_score": rel.score,
|
||||
"transmission_enabled": rel.transmission_enabled,
|
||||
"relationship_status": rel.status.value
|
||||
}
|
||||
|
||||
@self.app.get("/check_transmission_eligibility", operation_id="check_transmission_eligibility")
|
||||
async def check_transmission_eligibility(user_id: str) -> Dict[str, Any]:
|
||||
"""Check if AI can transmit to a specific user"""
|
||||
can_transmit = self.persona.can_transmit_to(user_id)
|
||||
rel = self.persona.relationships.get_or_create_relationship(user_id)
|
||||
|
||||
return {
|
||||
"can_transmit": can_transmit,
|
||||
"relationship_score": rel.score,
|
||||
"threshold": rel.threshold,
|
||||
"is_broken": rel.is_broken,
|
||||
"transmission_enabled": rel.transmission_enabled
|
||||
}
|
||||
|
||||
@self.app.get("/get_fortune", operation_id="get_fortune")
|
||||
async def get_fortune() -> Dict[str, Any]:
|
||||
"""Get today's AI fortune"""
|
||||
fortune = self.persona.fortune_system.get_today_fortune()
|
||||
modifiers = self.persona.fortune_system.get_personality_modifier(fortune)
|
||||
|
||||
return {
|
||||
"value": fortune.fortune_value,
|
||||
"date": fortune.date.isoformat(),
|
||||
"consecutive_good": fortune.consecutive_good,
|
||||
"consecutive_bad": fortune.consecutive_bad,
|
||||
"breakthrough": fortune.breakthrough_triggered,
|
||||
"personality_modifiers": modifiers
|
||||
}
|
||||
|
||||
@self.app.post("/summarize_memories", operation_id="summarize_memories")
|
||||
async def summarize_memories(user_id: str) -> Optional[Dict[str, Any]]:
|
||||
"""Create a summary of recent memories for a user"""
|
||||
summary = self.persona.memory.summarize_memories(user_id)
|
||||
if summary:
|
||||
return {
|
||||
"id": summary.id,
|
||||
"content": summary.content,
|
||||
"level": summary.level.value,
|
||||
"timestamp": summary.timestamp.isoformat()
|
||||
}
|
||||
return None
|
||||
|
||||
@self.app.post("/run_maintenance", operation_id="run_maintenance")
|
||||
async def run_maintenance() -> Dict[str, str]:
|
||||
"""Run daily maintenance tasks"""
|
||||
self.persona.daily_maintenance()
|
||||
return {"status": "Maintenance completed successfully"}
|
||||
|
||||
# Shell integration tools (ai.shell)
|
||||
@self.app.post("/execute_command", operation_id="execute_command")
|
||||
async def execute_command(command: str, working_dir: str = ".") -> Dict[str, Any]:
|
||||
"""Execute a shell command"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
shlex.split(command),
|
||||
cwd=working_dir,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "success" if result.returncode == 0 else "error",
|
||||
"returncode": result.returncode,
|
||||
"stdout": result.stdout,
|
||||
"stderr": result.stderr,
|
||||
"command": command
|
||||
}
|
||||
except subprocess.TimeoutExpired:
|
||||
return {"error": "Command timed out"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
@self.app.post("/analyze_file", operation_id="analyze_file")
|
||||
async def analyze_file(file_path: str, analysis_prompt: str = "Analyze this file") -> Dict[str, Any]:
|
||||
"""Analyze a file using AI"""
|
||||
try:
|
||||
if not os.path.exists(file_path):
|
||||
return {"error": f"File not found: {file_path}"}
|
||||
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# Get AI provider from app state
|
||||
ai_provider = getattr(self.app.state, 'ai_provider', 'ollama')
|
||||
ai_model = getattr(self.app.state, 'ai_model', 'qwen2.5')
|
||||
|
||||
provider = create_ai_provider(ai_provider, ai_model)
|
||||
|
||||
# Analyze with AI
|
||||
prompt = f"{analysis_prompt}\n\nFile: {file_path}\n\nContent:\n{content}"
|
||||
analysis = provider.generate_response(prompt, "You are a code analyst.")
|
||||
|
||||
return {
|
||||
"analysis": analysis,
|
||||
"file_path": file_path,
|
||||
"file_size": len(content),
|
||||
"line_count": len(content.split('\n'))
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
@self.app.post("/write_file", operation_id="write_file")
|
||||
async def write_file(file_path: str, content: str, backup: bool = True) -> Dict[str, Any]:
|
||||
"""Write content to a file"""
|
||||
try:
|
||||
file_path_obj = Path(file_path)
|
||||
|
||||
# Create backup if requested
|
||||
backup_path = None
|
||||
if backup and file_path_obj.exists():
|
||||
backup_path = f"{file_path}.backup"
|
||||
with open(file_path, 'r', encoding='utf-8') as src:
|
||||
with open(backup_path, 'w', encoding='utf-8') as dst:
|
||||
dst.write(src.read())
|
||||
|
||||
# Write file
|
||||
file_path_obj.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"file_path": file_path,
|
||||
"backup_path": backup_path,
|
||||
"bytes_written": len(content.encode('utf-8'))
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
@self.app.get("/read_project_file", operation_id="read_project_file")
|
||||
async def read_project_file(file_name: str = "aishell.md") -> Dict[str, Any]:
|
||||
"""Read project files like aishell.md (similar to claude.md)"""
|
||||
try:
|
||||
# Check common locations
|
||||
search_paths = [
|
||||
Path.cwd() / file_name,
|
||||
Path.cwd() / "docs" / file_name,
|
||||
self.data_dir.parent / file_name,
|
||||
]
|
||||
|
||||
for path in search_paths:
|
||||
if path.exists():
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
return {
|
||||
"content": content,
|
||||
"path": str(path),
|
||||
"exists": True
|
||||
}
|
||||
|
||||
return {
|
||||
"exists": False,
|
||||
"searched_paths": [str(p) for p in search_paths],
|
||||
"error": f"{file_name} not found"
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
@self.app.get("/list_files", operation_id="list_files")
|
||||
async def list_files(directory: str = ".", pattern: str = "*") -> Dict[str, Any]:
|
||||
"""List files in a directory"""
|
||||
try:
|
||||
dir_path = Path(directory)
|
||||
if not dir_path.exists():
|
||||
return {"error": f"Directory not found: {directory}"}
|
||||
|
||||
files = []
|
||||
for item in dir_path.glob(pattern):
|
||||
files.append({
|
||||
"name": item.name,
|
||||
"path": str(item),
|
||||
"is_file": item.is_file(),
|
||||
"is_dir": item.is_dir(),
|
||||
"size": item.stat().st_size if item.is_file() else None
|
||||
})
|
||||
|
||||
return {
|
||||
"directory": directory,
|
||||
"pattern": pattern,
|
||||
"files": files,
|
||||
"count": len(files)
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
# Register ai.card tools if integration is enabled
|
||||
if self.card_integration:
|
||||
register_card_tools(self.app, self.card_integration)
|
||||
|
||||
# Mount MCP server
|
||||
self.server.mount()
|
||||
|
||||
def get_server(self) -> FastApiMCP:
|
||||
"""Get the FastAPI MCP server instance"""
|
||||
return self.server
|
||||
|
||||
async def close(self):
|
||||
"""Cleanup resources"""
|
||||
if self.card_integration:
|
||||
await self.card_integration.close()
|
146
src/aigpt/mcp_server_simple.py
Normal file
146
src/aigpt/mcp_server_simple.py
Normal file
@@ -0,0 +1,146 @@
|
||||
"""Simple MCP Server implementation for ai.gpt"""
|
||||
|
||||
from mcp import Server
|
||||
from mcp.types import Tool, TextContent
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
import json
|
||||
|
||||
from .persona import Persona
|
||||
from .ai_provider import create_ai_provider
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
|
||||
def create_mcp_server(data_dir: Path, enable_card: bool = False) -> Server:
|
||||
"""Create MCP server with ai.gpt tools"""
|
||||
server = Server("aigpt")
|
||||
persona = Persona(data_dir)
|
||||
|
||||
@server.tool()
|
||||
async def get_memories(limit: int = 10) -> List[Dict[str, Any]]:
|
||||
"""Get active memories from the AI's memory system"""
|
||||
memories = persona.memory.get_active_memories(limit=limit)
|
||||
return [
|
||||
{
|
||||
"id": mem.id,
|
||||
"content": mem.content,
|
||||
"level": mem.level.value,
|
||||
"importance": mem.importance_score,
|
||||
"is_core": mem.is_core,
|
||||
"timestamp": mem.timestamp.isoformat()
|
||||
}
|
||||
for mem in memories
|
||||
]
|
||||
|
||||
@server.tool()
|
||||
async def get_relationship(user_id: str) -> Dict[str, Any]:
|
||||
"""Get relationship status with a specific user"""
|
||||
rel = persona.relationships.get_or_create_relationship(user_id)
|
||||
return {
|
||||
"user_id": rel.user_id,
|
||||
"status": rel.status.value,
|
||||
"score": rel.score,
|
||||
"transmission_enabled": rel.transmission_enabled,
|
||||
"is_broken": rel.is_broken,
|
||||
"total_interactions": rel.total_interactions,
|
||||
"last_interaction": rel.last_interaction.isoformat() if rel.last_interaction else None
|
||||
}
|
||||
|
||||
@server.tool()
|
||||
async def process_interaction(user_id: str, message: str, provider: str = "ollama", model: str = "qwen2.5") -> Dict[str, Any]:
|
||||
"""Process an interaction with a user"""
|
||||
ai_provider = create_ai_provider(provider, model)
|
||||
response, relationship_delta = persona.process_interaction(user_id, message, ai_provider)
|
||||
rel = persona.relationships.get_or_create_relationship(user_id)
|
||||
|
||||
return {
|
||||
"response": response,
|
||||
"relationship_delta": relationship_delta,
|
||||
"new_relationship_score": rel.score,
|
||||
"transmission_enabled": rel.transmission_enabled,
|
||||
"relationship_status": rel.status.value
|
||||
}
|
||||
|
||||
@server.tool()
|
||||
async def get_fortune() -> Dict[str, Any]:
|
||||
"""Get today's AI fortune"""
|
||||
fortune = persona.fortune_system.get_today_fortune()
|
||||
modifiers = persona.fortune_system.get_personality_modifier(fortune)
|
||||
|
||||
return {
|
||||
"value": fortune.fortune_value,
|
||||
"date": fortune.date.isoformat(),
|
||||
"consecutive_good": fortune.consecutive_good,
|
||||
"consecutive_bad": fortune.consecutive_bad,
|
||||
"breakthrough": fortune.breakthrough_triggered,
|
||||
"personality_modifiers": modifiers
|
||||
}
|
||||
|
||||
@server.tool()
|
||||
async def execute_command(command: str, working_dir: str = ".") -> Dict[str, Any]:
|
||||
"""Execute a shell command"""
|
||||
try:
|
||||
import shlex
|
||||
result = subprocess.run(
|
||||
shlex.split(command),
|
||||
cwd=working_dir,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "success" if result.returncode == 0 else "error",
|
||||
"returncode": result.returncode,
|
||||
"stdout": result.stdout,
|
||||
"stderr": result.stderr,
|
||||
"command": command
|
||||
}
|
||||
except subprocess.TimeoutExpired:
|
||||
return {"error": "Command timed out"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
@server.tool()
|
||||
async def analyze_file(file_path: str) -> Dict[str, Any]:
|
||||
"""Analyze a file using AI"""
|
||||
try:
|
||||
if not os.path.exists(file_path):
|
||||
return {"error": f"File not found: {file_path}"}
|
||||
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
ai_provider = create_ai_provider("ollama", "qwen2.5")
|
||||
|
||||
prompt = f"Analyze this file and provide insights:\\n\\nFile: {file_path}\\n\\nContent:\\n{content[:2000]}"
|
||||
analysis = ai_provider.generate_response(prompt, "You are a code analyst.")
|
||||
|
||||
return {
|
||||
"analysis": analysis,
|
||||
"file_path": file_path,
|
||||
"file_size": len(content),
|
||||
"line_count": len(content.split('\\n'))
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
return server
|
||||
|
||||
|
||||
async def main():
|
||||
"""Run MCP server"""
|
||||
import sys
|
||||
from mcp import stdio_server
|
||||
|
||||
data_dir = Path.home() / ".config" / "syui" / "ai" / "gpt" / "data"
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
server = create_mcp_server(data_dir)
|
||||
await stdio_server(server)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
asyncio.run(main())
|
155
src/aigpt/memory.py
Normal file
155
src/aigpt/memory.py
Normal file
@@ -0,0 +1,155 @@
|
||||
"""Memory management system for ai.gpt"""
|
||||
|
||||
import json
|
||||
import hashlib
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import List, Optional, Dict, Any
|
||||
import logging
|
||||
|
||||
from .models import Memory, MemoryLevel, Conversation
|
||||
|
||||
|
||||
class MemoryManager:
|
||||
"""Manages AI's memory with hierarchical storage and forgetting"""
|
||||
|
||||
def __init__(self, data_dir: Path):
|
||||
self.data_dir = data_dir
|
||||
self.memories_file = data_dir / "memories.json"
|
||||
self.conversations_file = data_dir / "conversations.json"
|
||||
self.memories: Dict[str, Memory] = {}
|
||||
self.conversations: List[Conversation] = []
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self._load_memories()
|
||||
|
||||
def _load_memories(self):
|
||||
"""Load memories from persistent storage"""
|
||||
if self.memories_file.exists():
|
||||
with open(self.memories_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
for mem_data in data:
|
||||
memory = Memory(**mem_data)
|
||||
self.memories[memory.id] = memory
|
||||
|
||||
if self.conversations_file.exists():
|
||||
with open(self.conversations_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
self.conversations = [Conversation(**conv) for conv in data]
|
||||
|
||||
def _save_memories(self):
|
||||
"""Save memories to persistent storage"""
|
||||
memories_data = [mem.model_dump(mode='json') for mem in self.memories.values()]
|
||||
with open(self.memories_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(memories_data, f, indent=2, default=str)
|
||||
|
||||
conv_data = [conv.model_dump(mode='json') for conv in self.conversations]
|
||||
with open(self.conversations_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(conv_data, f, indent=2, default=str)
|
||||
|
||||
def add_conversation(self, conversation: Conversation) -> Memory:
|
||||
"""Add a conversation and create memory from it"""
|
||||
self.conversations.append(conversation)
|
||||
|
||||
# Create memory from conversation
|
||||
memory_id = hashlib.sha256(
|
||||
f"{conversation.id}{conversation.timestamp}".encode()
|
||||
).hexdigest()[:16]
|
||||
|
||||
memory = Memory(
|
||||
id=memory_id,
|
||||
timestamp=conversation.timestamp,
|
||||
content=f"User: {conversation.user_message}\nAI: {conversation.ai_response}",
|
||||
level=MemoryLevel.FULL_LOG,
|
||||
importance_score=abs(conversation.relationship_delta) * 0.1
|
||||
)
|
||||
|
||||
self.memories[memory.id] = memory
|
||||
self._save_memories()
|
||||
return memory
|
||||
|
||||
def summarize_memories(self, user_id: str) -> Optional[Memory]:
|
||||
"""Create summary from recent memories"""
|
||||
recent_memories = [
|
||||
mem for mem in self.memories.values()
|
||||
if mem.level == MemoryLevel.FULL_LOG
|
||||
and (datetime.now() - mem.timestamp).days < 7
|
||||
]
|
||||
|
||||
if len(recent_memories) < 5:
|
||||
return None
|
||||
|
||||
# Simple summary creation (in real implementation, use AI)
|
||||
summary_content = f"Summary of {len(recent_memories)} recent interactions"
|
||||
summary_id = hashlib.sha256(
|
||||
f"summary_{datetime.now().isoformat()}".encode()
|
||||
).hexdigest()[:16]
|
||||
|
||||
summary = Memory(
|
||||
id=summary_id,
|
||||
timestamp=datetime.now(),
|
||||
content=summary_content,
|
||||
summary=summary_content,
|
||||
level=MemoryLevel.SUMMARY,
|
||||
importance_score=0.5
|
||||
)
|
||||
|
||||
self.memories[summary.id] = summary
|
||||
|
||||
# Mark summarized memories for potential forgetting
|
||||
for mem in recent_memories:
|
||||
mem.importance_score *= 0.9
|
||||
|
||||
self._save_memories()
|
||||
return summary
|
||||
|
||||
def identify_core_memories(self) -> List[Memory]:
|
||||
"""Identify memories that should become core (never forgotten)"""
|
||||
core_candidates = [
|
||||
mem for mem in self.memories.values()
|
||||
if mem.importance_score > 0.8
|
||||
and not mem.is_core
|
||||
and mem.level != MemoryLevel.FORGOTTEN
|
||||
]
|
||||
|
||||
for memory in core_candidates:
|
||||
memory.is_core = True
|
||||
memory.level = MemoryLevel.CORE
|
||||
self.logger.info(f"Memory {memory.id} promoted to core")
|
||||
|
||||
self._save_memories()
|
||||
return core_candidates
|
||||
|
||||
def apply_forgetting(self):
|
||||
"""Apply selective forgetting based on importance and time"""
|
||||
now = datetime.now()
|
||||
|
||||
for memory in self.memories.values():
|
||||
if memory.is_core or memory.level == MemoryLevel.FORGOTTEN:
|
||||
continue
|
||||
|
||||
# Time-based decay
|
||||
age_days = (now - memory.timestamp).days
|
||||
decay_factor = memory.decay_rate * age_days
|
||||
memory.importance_score -= decay_factor
|
||||
|
||||
# Forget unimportant old memories
|
||||
if memory.importance_score <= 0.1 and age_days > 30:
|
||||
memory.level = MemoryLevel.FORGOTTEN
|
||||
self.logger.info(f"Memory {memory.id} forgotten")
|
||||
|
||||
self._save_memories()
|
||||
|
||||
def get_active_memories(self, limit: int = 10) -> List[Memory]:
|
||||
"""Get currently active memories for persona"""
|
||||
active = [
|
||||
mem for mem in self.memories.values()
|
||||
if mem.level != MemoryLevel.FORGOTTEN
|
||||
]
|
||||
|
||||
# Sort by importance and recency
|
||||
active.sort(
|
||||
key=lambda m: (m.is_core, m.importance_score, m.timestamp),
|
||||
reverse=True
|
||||
)
|
||||
|
||||
return active[:limit]
|
79
src/aigpt/models.py
Normal file
79
src/aigpt/models.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Data models for ai.gpt system"""
|
||||
|
||||
from datetime import datetime, date
|
||||
from typing import Optional, Dict, List, Any
|
||||
from enum import Enum
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class MemoryLevel(str, Enum):
|
||||
"""Memory importance levels"""
|
||||
FULL_LOG = "full_log"
|
||||
SUMMARY = "summary"
|
||||
CORE = "core"
|
||||
FORGOTTEN = "forgotten"
|
||||
|
||||
|
||||
class RelationshipStatus(str, Enum):
|
||||
"""Relationship status levels"""
|
||||
STRANGER = "stranger"
|
||||
ACQUAINTANCE = "acquaintance"
|
||||
FRIEND = "friend"
|
||||
CLOSE_FRIEND = "close_friend"
|
||||
BROKEN = "broken" # 不可逆
|
||||
|
||||
|
||||
class Memory(BaseModel):
|
||||
"""Single memory unit"""
|
||||
id: str
|
||||
timestamp: datetime
|
||||
content: str
|
||||
summary: Optional[str] = None
|
||||
level: MemoryLevel = MemoryLevel.FULL_LOG
|
||||
importance_score: float = Field(ge=0.0, le=1.0)
|
||||
is_core: bool = False
|
||||
decay_rate: float = 0.01
|
||||
|
||||
|
||||
class Relationship(BaseModel):
|
||||
"""Relationship with a specific user"""
|
||||
user_id: str # atproto DID
|
||||
status: RelationshipStatus = RelationshipStatus.STRANGER
|
||||
score: float = 0.0
|
||||
daily_interactions: int = 0
|
||||
total_interactions: int = 0
|
||||
last_interaction: Optional[datetime] = None
|
||||
transmission_enabled: bool = False
|
||||
threshold: float = 100.0
|
||||
decay_rate: float = 0.1
|
||||
daily_limit: int = 10
|
||||
is_broken: bool = False
|
||||
|
||||
|
||||
class AIFortune(BaseModel):
|
||||
"""Daily AI fortune affecting personality"""
|
||||
date: date
|
||||
fortune_value: int = Field(ge=1, le=10)
|
||||
consecutive_good: int = 0
|
||||
consecutive_bad: int = 0
|
||||
breakthrough_triggered: bool = False
|
||||
|
||||
|
||||
class PersonaState(BaseModel):
|
||||
"""Current persona state"""
|
||||
base_personality: Dict[str, float]
|
||||
current_mood: str
|
||||
fortune: AIFortune
|
||||
active_memories: List[str] # Memory IDs
|
||||
relationship_modifiers: Dict[str, float]
|
||||
|
||||
|
||||
class Conversation(BaseModel):
|
||||
"""Conversation log entry"""
|
||||
id: str
|
||||
user_id: str
|
||||
timestamp: datetime
|
||||
user_message: str
|
||||
ai_response: str
|
||||
relationship_delta: float = 0.0
|
||||
memory_created: bool = False
|
181
src/aigpt/persona.py
Normal file
181
src/aigpt/persona.py
Normal file
@@ -0,0 +1,181 @@
|
||||
"""Persona management system integrating memory, relationships, and fortune"""
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
import logging
|
||||
|
||||
from .models import PersonaState, Conversation
|
||||
from .memory import MemoryManager
|
||||
from .relationship import RelationshipTracker
|
||||
from .fortune import FortuneSystem
|
||||
|
||||
|
||||
class Persona:
|
||||
"""AI persona with unique characteristics based on interactions"""
|
||||
|
||||
def __init__(self, data_dir: Path, name: str = "ai"):
|
||||
self.data_dir = data_dir
|
||||
self.name = name
|
||||
self.memory = MemoryManager(data_dir)
|
||||
self.relationships = RelationshipTracker(data_dir)
|
||||
self.fortune_system = FortuneSystem(data_dir)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
# Base personality traits
|
||||
self.base_personality = {
|
||||
"curiosity": 0.7,
|
||||
"empathy": 0.8,
|
||||
"creativity": 0.6,
|
||||
"patience": 0.7,
|
||||
"optimism": 0.6
|
||||
}
|
||||
|
||||
self.state_file = data_dir / "persona_state.json"
|
||||
self._load_state()
|
||||
|
||||
def _load_state(self):
|
||||
"""Load persona state from storage"""
|
||||
if self.state_file.exists():
|
||||
with open(self.state_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
self.base_personality = data.get("base_personality", self.base_personality)
|
||||
|
||||
def _save_state(self):
|
||||
"""Save persona state to storage"""
|
||||
state_data = {
|
||||
"base_personality": self.base_personality,
|
||||
"last_updated": datetime.now().isoformat()
|
||||
}
|
||||
with open(self.state_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(state_data, f, indent=2)
|
||||
|
||||
def get_current_state(self) -> PersonaState:
|
||||
"""Get current persona state including all modifiers"""
|
||||
# Get today's fortune
|
||||
fortune = self.fortune_system.get_today_fortune()
|
||||
fortune_modifiers = self.fortune_system.get_personality_modifier(fortune)
|
||||
|
||||
# Apply fortune modifiers to base personality
|
||||
current_personality = {}
|
||||
for trait, base_value in self.base_personality.items():
|
||||
modifier = fortune_modifiers.get(trait, 1.0)
|
||||
current_personality[trait] = min(1.0, base_value * modifier)
|
||||
|
||||
# Get active memories for context
|
||||
active_memories = self.memory.get_active_memories(limit=5)
|
||||
|
||||
# Determine mood based on fortune and recent interactions
|
||||
mood = self._determine_mood(fortune.fortune_value)
|
||||
|
||||
state = PersonaState(
|
||||
base_personality=current_personality,
|
||||
current_mood=mood,
|
||||
fortune=fortune,
|
||||
active_memories=[mem.id for mem in active_memories],
|
||||
relationship_modifiers={}
|
||||
)
|
||||
|
||||
return state
|
||||
|
||||
def _determine_mood(self, fortune_value: int) -> str:
|
||||
"""Determine current mood based on fortune and other factors"""
|
||||
if fortune_value >= 8:
|
||||
return "joyful"
|
||||
elif fortune_value >= 6:
|
||||
return "cheerful"
|
||||
elif fortune_value >= 4:
|
||||
return "neutral"
|
||||
elif fortune_value >= 2:
|
||||
return "melancholic"
|
||||
else:
|
||||
return "contemplative"
|
||||
|
||||
def process_interaction(self, user_id: str, message: str, ai_provider=None) -> tuple[str, float]:
|
||||
"""Process user interaction and generate response"""
|
||||
# Get current state
|
||||
state = self.get_current_state()
|
||||
|
||||
# Get relationship with user
|
||||
relationship = self.relationships.get_or_create_relationship(user_id)
|
||||
|
||||
# Simple response generation (use AI provider if available)
|
||||
if relationship.is_broken:
|
||||
response = "..."
|
||||
relationship_delta = 0.0
|
||||
else:
|
||||
if ai_provider:
|
||||
# Use AI provider for response generation
|
||||
memories = self.memory.get_active_memories(limit=5)
|
||||
import asyncio
|
||||
response = asyncio.run(
|
||||
ai_provider.generate_response(message, state, memories)
|
||||
)
|
||||
# Calculate relationship delta based on interaction quality
|
||||
if state.current_mood in ["joyful", "cheerful"]:
|
||||
relationship_delta = 2.0
|
||||
elif relationship.status.value == "close_friend":
|
||||
relationship_delta = 1.5
|
||||
else:
|
||||
relationship_delta = 1.0
|
||||
else:
|
||||
# Fallback to simple responses
|
||||
if state.current_mood == "joyful":
|
||||
response = f"What a wonderful day! {message} sounds interesting!"
|
||||
relationship_delta = 2.0
|
||||
elif relationship.status.value == "close_friend":
|
||||
response = f"I've been thinking about our conversations. {message}"
|
||||
relationship_delta = 1.5
|
||||
else:
|
||||
response = f"I understand. {message}"
|
||||
relationship_delta = 1.0
|
||||
|
||||
# Create conversation record
|
||||
conv_id = f"{user_id}_{datetime.now().timestamp()}"
|
||||
conversation = Conversation(
|
||||
id=conv_id,
|
||||
user_id=user_id,
|
||||
timestamp=datetime.now(),
|
||||
user_message=message,
|
||||
ai_response=response,
|
||||
relationship_delta=relationship_delta,
|
||||
memory_created=True
|
||||
)
|
||||
|
||||
# Update memory
|
||||
self.memory.add_conversation(conversation)
|
||||
|
||||
# Update relationship
|
||||
self.relationships.update_interaction(user_id, relationship_delta)
|
||||
|
||||
return response, relationship_delta
|
||||
|
||||
def can_transmit_to(self, user_id: str) -> bool:
|
||||
"""Check if AI can transmit messages to this user"""
|
||||
relationship = self.relationships.get_or_create_relationship(user_id)
|
||||
return relationship.transmission_enabled and not relationship.is_broken
|
||||
|
||||
def daily_maintenance(self):
|
||||
"""Perform daily maintenance tasks"""
|
||||
self.logger.info("Performing daily maintenance...")
|
||||
|
||||
# Apply time decay to relationships
|
||||
self.relationships.apply_time_decay()
|
||||
|
||||
# Apply forgetting to memories
|
||||
self.memory.apply_forgetting()
|
||||
|
||||
# Identify core memories
|
||||
core_memories = self.memory.identify_core_memories()
|
||||
if core_memories:
|
||||
self.logger.info(f"Identified {len(core_memories)} new core memories")
|
||||
|
||||
# Create memory summaries
|
||||
for user_id in self.relationships.relationships:
|
||||
summary = self.memory.summarize_memories(user_id)
|
||||
if summary:
|
||||
self.logger.info(f"Created summary for interactions with {user_id}")
|
||||
|
||||
self._save_state()
|
||||
self.logger.info("Daily maintenance completed")
|
135
src/aigpt/relationship.py
Normal file
135
src/aigpt/relationship.py
Normal file
@@ -0,0 +1,135 @@
|
||||
"""Relationship tracking system with irreversible damage"""
|
||||
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional
|
||||
import logging
|
||||
|
||||
from .models import Relationship, RelationshipStatus
|
||||
|
||||
|
||||
class RelationshipTracker:
|
||||
"""Tracks and manages relationships with users"""
|
||||
|
||||
def __init__(self, data_dir: Path):
|
||||
self.data_dir = data_dir
|
||||
self.relationships_file = data_dir / "relationships.json"
|
||||
self.relationships: Dict[str, Relationship] = {}
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self._load_relationships()
|
||||
|
||||
def _load_relationships(self):
|
||||
"""Load relationships from persistent storage"""
|
||||
if self.relationships_file.exists():
|
||||
with open(self.relationships_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
for user_id, rel_data in data.items():
|
||||
self.relationships[user_id] = Relationship(**rel_data)
|
||||
|
||||
def _save_relationships(self):
|
||||
"""Save relationships to persistent storage"""
|
||||
data = {
|
||||
user_id: rel.model_dump(mode='json')
|
||||
for user_id, rel in self.relationships.items()
|
||||
}
|
||||
with open(self.relationships_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2, default=str)
|
||||
|
||||
def get_or_create_relationship(self, user_id: str) -> Relationship:
|
||||
"""Get existing relationship or create new one"""
|
||||
if user_id not in self.relationships:
|
||||
self.relationships[user_id] = Relationship(user_id=user_id)
|
||||
self._save_relationships()
|
||||
return self.relationships[user_id]
|
||||
|
||||
def update_interaction(self, user_id: str, delta: float) -> Relationship:
|
||||
"""Update relationship based on interaction"""
|
||||
rel = self.get_or_create_relationship(user_id)
|
||||
|
||||
# Check if relationship is broken (irreversible)
|
||||
if rel.is_broken:
|
||||
self.logger.warning(f"Relationship with {user_id} is broken. No updates allowed.")
|
||||
return rel
|
||||
|
||||
# Check daily limit
|
||||
if rel.last_interaction and rel.last_interaction.date() == datetime.now().date():
|
||||
if rel.daily_interactions >= rel.daily_limit:
|
||||
self.logger.info(f"Daily interaction limit reached for {user_id}")
|
||||
return rel
|
||||
else:
|
||||
rel.daily_interactions = 0
|
||||
|
||||
# Update interaction counts
|
||||
rel.daily_interactions += 1
|
||||
rel.total_interactions += 1
|
||||
rel.last_interaction = datetime.now()
|
||||
|
||||
# Update score with bounds
|
||||
old_score = rel.score
|
||||
rel.score += delta
|
||||
rel.score = max(0.0, min(200.0, rel.score)) # 0-200 range
|
||||
|
||||
# Check for relationship damage
|
||||
if delta < -10.0: # Significant negative interaction
|
||||
self.logger.warning(f"Major relationship damage with {user_id}: {delta}")
|
||||
if rel.score <= 0:
|
||||
rel.is_broken = True
|
||||
rel.status = RelationshipStatus.BROKEN
|
||||
rel.transmission_enabled = False
|
||||
self.logger.error(f"Relationship with {user_id} is now BROKEN (irreversible)")
|
||||
|
||||
# Update relationship status based on score
|
||||
if not rel.is_broken:
|
||||
if rel.score >= 150:
|
||||
rel.status = RelationshipStatus.CLOSE_FRIEND
|
||||
elif rel.score >= 100:
|
||||
rel.status = RelationshipStatus.FRIEND
|
||||
elif rel.score >= 50:
|
||||
rel.status = RelationshipStatus.ACQUAINTANCE
|
||||
else:
|
||||
rel.status = RelationshipStatus.STRANGER
|
||||
|
||||
# Check transmission threshold
|
||||
if rel.score >= rel.threshold and not rel.transmission_enabled:
|
||||
rel.transmission_enabled = True
|
||||
self.logger.info(f"Transmission enabled for {user_id}!")
|
||||
|
||||
self._save_relationships()
|
||||
return rel
|
||||
|
||||
def apply_time_decay(self):
|
||||
"""Apply time-based decay to all relationships"""
|
||||
now = datetime.now()
|
||||
|
||||
for user_id, rel in self.relationships.items():
|
||||
if rel.is_broken or not rel.last_interaction:
|
||||
continue
|
||||
|
||||
# Calculate days since last interaction
|
||||
days_inactive = (now - rel.last_interaction).days
|
||||
|
||||
if days_inactive > 0:
|
||||
# Apply decay
|
||||
decay_amount = rel.decay_rate * days_inactive
|
||||
old_score = rel.score
|
||||
rel.score = max(0.0, rel.score - decay_amount)
|
||||
|
||||
# Update status if score dropped
|
||||
if rel.score < rel.threshold:
|
||||
rel.transmission_enabled = False
|
||||
|
||||
if decay_amount > 0:
|
||||
self.logger.info(
|
||||
f"Applied decay to {user_id}: {old_score:.2f} -> {rel.score:.2f}"
|
||||
)
|
||||
|
||||
self._save_relationships()
|
||||
|
||||
def get_transmission_eligible(self) -> Dict[str, Relationship]:
|
||||
"""Get all relationships eligible for transmission"""
|
||||
return {
|
||||
user_id: rel
|
||||
for user_id, rel in self.relationships.items()
|
||||
if rel.transmission_enabled and not rel.is_broken
|
||||
}
|
312
src/aigpt/scheduler.py
Normal file
312
src/aigpt/scheduler.py
Normal file
@@ -0,0 +1,312 @@
|
||||
"""Scheduler for autonomous AI tasks"""
|
||||
|
||||
import json
|
||||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional, Any, Callable
|
||||
from enum import Enum
|
||||
import logging
|
||||
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
from apscheduler.triggers.interval import IntervalTrigger
|
||||
from croniter import croniter
|
||||
|
||||
from .persona import Persona
|
||||
from .transmission import TransmissionController
|
||||
from .ai_provider import create_ai_provider
|
||||
|
||||
|
||||
class TaskType(str, Enum):
|
||||
"""Types of scheduled tasks"""
|
||||
TRANSMISSION_CHECK = "transmission_check"
|
||||
MAINTENANCE = "maintenance"
|
||||
FORTUNE_UPDATE = "fortune_update"
|
||||
RELATIONSHIP_DECAY = "relationship_decay"
|
||||
MEMORY_SUMMARY = "memory_summary"
|
||||
CUSTOM = "custom"
|
||||
|
||||
|
||||
class ScheduledTask:
|
||||
"""Represents a scheduled task"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
task_id: str,
|
||||
task_type: TaskType,
|
||||
schedule: str, # Cron expression or interval
|
||||
enabled: bool = True,
|
||||
last_run: Optional[datetime] = None,
|
||||
next_run: Optional[datetime] = None,
|
||||
metadata: Optional[Dict[str, Any]] = None
|
||||
):
|
||||
self.task_id = task_id
|
||||
self.task_type = task_type
|
||||
self.schedule = schedule
|
||||
self.enabled = enabled
|
||||
self.last_run = last_run
|
||||
self.next_run = next_run
|
||||
self.metadata = metadata or {}
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Convert to dictionary for storage"""
|
||||
return {
|
||||
"task_id": self.task_id,
|
||||
"task_type": self.task_type.value,
|
||||
"schedule": self.schedule,
|
||||
"enabled": self.enabled,
|
||||
"last_run": self.last_run.isoformat() if self.last_run else None,
|
||||
"next_run": self.next_run.isoformat() if self.next_run else None,
|
||||
"metadata": self.metadata
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "ScheduledTask":
|
||||
"""Create from dictionary"""
|
||||
return cls(
|
||||
task_id=data["task_id"],
|
||||
task_type=TaskType(data["task_type"]),
|
||||
schedule=data["schedule"],
|
||||
enabled=data.get("enabled", True),
|
||||
last_run=datetime.fromisoformat(data["last_run"]) if data.get("last_run") else None,
|
||||
next_run=datetime.fromisoformat(data["next_run"]) if data.get("next_run") else None,
|
||||
metadata=data.get("metadata", {})
|
||||
)
|
||||
|
||||
|
||||
class AIScheduler:
|
||||
"""Manages scheduled tasks for the AI system"""
|
||||
|
||||
def __init__(self, data_dir: Path, persona: Persona):
|
||||
self.data_dir = data_dir
|
||||
self.persona = persona
|
||||
self.tasks_file = data_dir / "scheduled_tasks.json"
|
||||
self.tasks: Dict[str, ScheduledTask] = {}
|
||||
self.scheduler = AsyncIOScheduler()
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self._load_tasks()
|
||||
|
||||
# Task handlers
|
||||
self.task_handlers: Dict[TaskType, Callable] = {
|
||||
TaskType.TRANSMISSION_CHECK: self._handle_transmission_check,
|
||||
TaskType.MAINTENANCE: self._handle_maintenance,
|
||||
TaskType.FORTUNE_UPDATE: self._handle_fortune_update,
|
||||
TaskType.RELATIONSHIP_DECAY: self._handle_relationship_decay,
|
||||
TaskType.MEMORY_SUMMARY: self._handle_memory_summary,
|
||||
}
|
||||
|
||||
def _load_tasks(self):
|
||||
"""Load scheduled tasks from storage"""
|
||||
if self.tasks_file.exists():
|
||||
with open(self.tasks_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
for task_data in data:
|
||||
task = ScheduledTask.from_dict(task_data)
|
||||
self.tasks[task.task_id] = task
|
||||
|
||||
def _save_tasks(self):
|
||||
"""Save scheduled tasks to storage"""
|
||||
tasks_data = [task.to_dict() for task in self.tasks.values()]
|
||||
with open(self.tasks_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(tasks_data, f, indent=2, default=str)
|
||||
|
||||
def add_task(
|
||||
self,
|
||||
task_type: TaskType,
|
||||
schedule: str,
|
||||
task_id: Optional[str] = None,
|
||||
metadata: Optional[Dict[str, Any]] = None
|
||||
) -> ScheduledTask:
|
||||
"""Add a new scheduled task"""
|
||||
if task_id is None:
|
||||
task_id = f"{task_type.value}_{datetime.now().timestamp()}"
|
||||
|
||||
# Validate schedule
|
||||
if not self._validate_schedule(schedule):
|
||||
raise ValueError(f"Invalid schedule expression: {schedule}")
|
||||
|
||||
task = ScheduledTask(
|
||||
task_id=task_id,
|
||||
task_type=task_type,
|
||||
schedule=schedule,
|
||||
metadata=metadata
|
||||
)
|
||||
|
||||
self.tasks[task_id] = task
|
||||
self._save_tasks()
|
||||
|
||||
# Schedule the task if scheduler is running
|
||||
if self.scheduler.running:
|
||||
self._schedule_task(task)
|
||||
|
||||
self.logger.info(f"Added task {task_id} with schedule {schedule}")
|
||||
return task
|
||||
|
||||
def _validate_schedule(self, schedule: str) -> bool:
|
||||
"""Validate schedule expression"""
|
||||
# Check if it's a cron expression
|
||||
if ' ' in schedule:
|
||||
try:
|
||||
croniter(schedule)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
# Check if it's an interval expression (e.g., "5m", "1h", "2d")
|
||||
import re
|
||||
pattern = r'^\d+[smhd]$'
|
||||
return bool(re.match(pattern, schedule))
|
||||
|
||||
def _parse_interval(self, interval: str) -> int:
|
||||
"""Parse interval string to seconds"""
|
||||
unit = interval[-1]
|
||||
value = int(interval[:-1])
|
||||
|
||||
multipliers = {
|
||||
's': 1,
|
||||
'm': 60,
|
||||
'h': 3600,
|
||||
'd': 86400
|
||||
}
|
||||
|
||||
return value * multipliers.get(unit, 1)
|
||||
|
||||
def _schedule_task(self, task: ScheduledTask):
|
||||
"""Schedule a task with APScheduler"""
|
||||
if not task.enabled:
|
||||
return
|
||||
|
||||
handler = self.task_handlers.get(task.task_type)
|
||||
if not handler:
|
||||
self.logger.warning(f"No handler for task type {task.task_type}")
|
||||
return
|
||||
|
||||
# Determine trigger
|
||||
if ' ' in task.schedule:
|
||||
# Cron expression
|
||||
trigger = CronTrigger.from_crontab(task.schedule)
|
||||
else:
|
||||
# Interval expression
|
||||
seconds = self._parse_interval(task.schedule)
|
||||
trigger = IntervalTrigger(seconds=seconds)
|
||||
|
||||
# Add job
|
||||
self.scheduler.add_job(
|
||||
lambda: asyncio.create_task(self._run_task(task)),
|
||||
trigger=trigger,
|
||||
id=task.task_id,
|
||||
replace_existing=True
|
||||
)
|
||||
|
||||
async def _run_task(self, task: ScheduledTask):
|
||||
"""Run a scheduled task"""
|
||||
self.logger.info(f"Running task {task.task_id}")
|
||||
|
||||
task.last_run = datetime.now()
|
||||
|
||||
try:
|
||||
handler = self.task_handlers.get(task.task_type)
|
||||
if handler:
|
||||
await handler(task)
|
||||
else:
|
||||
self.logger.warning(f"No handler for task type {task.task_type}")
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error running task {task.task_id}: {e}")
|
||||
|
||||
self._save_tasks()
|
||||
|
||||
async def _handle_transmission_check(self, task: ScheduledTask):
|
||||
"""Check and execute autonomous transmissions"""
|
||||
controller = TransmissionController(self.persona, self.data_dir)
|
||||
eligible = controller.check_transmission_eligibility()
|
||||
|
||||
# Get AI provider from metadata
|
||||
provider_name = task.metadata.get("provider", "ollama")
|
||||
model = task.metadata.get("model", "qwen2.5")
|
||||
|
||||
try:
|
||||
ai_provider = create_ai_provider(provider_name, model)
|
||||
except:
|
||||
ai_provider = None
|
||||
|
||||
for user_id, rel in eligible.items():
|
||||
message = controller.generate_transmission_message(user_id)
|
||||
if message:
|
||||
# For now, just print the message
|
||||
print(f"\n🤖 [AI Transmission] {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print(f"To: {user_id}")
|
||||
print(f"Relationship: {rel.status.value} (score: {rel.score:.2f})")
|
||||
print(f"Message: {message}")
|
||||
print("-" * 50)
|
||||
|
||||
controller.record_transmission(user_id, message, success=True)
|
||||
self.logger.info(f"Transmitted to {user_id}: {message}")
|
||||
|
||||
async def _handle_maintenance(self, task: ScheduledTask):
|
||||
"""Run daily maintenance"""
|
||||
self.persona.daily_maintenance()
|
||||
self.logger.info("Daily maintenance completed")
|
||||
|
||||
async def _handle_fortune_update(self, task: ScheduledTask):
|
||||
"""Update AI fortune"""
|
||||
fortune = self.persona.fortune_system.get_today_fortune()
|
||||
self.logger.info(f"Fortune updated: {fortune.fortune_value}/10")
|
||||
|
||||
async def _handle_relationship_decay(self, task: ScheduledTask):
|
||||
"""Apply relationship decay"""
|
||||
self.persona.relationships.apply_time_decay()
|
||||
self.logger.info("Relationship decay applied")
|
||||
|
||||
async def _handle_memory_summary(self, task: ScheduledTask):
|
||||
"""Create memory summaries"""
|
||||
for user_id in self.persona.relationships.relationships:
|
||||
summary = self.persona.memory.summarize_memories(user_id)
|
||||
if summary:
|
||||
self.logger.info(f"Created memory summary for {user_id}")
|
||||
|
||||
def start(self):
|
||||
"""Start the scheduler"""
|
||||
# Schedule all enabled tasks
|
||||
for task in self.tasks.values():
|
||||
if task.enabled:
|
||||
self._schedule_task(task)
|
||||
|
||||
self.scheduler.start()
|
||||
self.logger.info("Scheduler started")
|
||||
|
||||
def stop(self):
|
||||
"""Stop the scheduler"""
|
||||
self.scheduler.shutdown()
|
||||
self.logger.info("Scheduler stopped")
|
||||
|
||||
def get_tasks(self) -> List[ScheduledTask]:
|
||||
"""Get all scheduled tasks"""
|
||||
return list(self.tasks.values())
|
||||
|
||||
def enable_task(self, task_id: str):
|
||||
"""Enable a task"""
|
||||
if task_id in self.tasks:
|
||||
self.tasks[task_id].enabled = True
|
||||
self._save_tasks()
|
||||
if self.scheduler.running:
|
||||
self._schedule_task(self.tasks[task_id])
|
||||
|
||||
def disable_task(self, task_id: str):
|
||||
"""Disable a task"""
|
||||
if task_id in self.tasks:
|
||||
self.tasks[task_id].enabled = False
|
||||
self._save_tasks()
|
||||
if self.scheduler.running:
|
||||
self.scheduler.remove_job(task_id)
|
||||
|
||||
def remove_task(self, task_id: str):
|
||||
"""Remove a task"""
|
||||
if task_id in self.tasks:
|
||||
del self.tasks[task_id]
|
||||
self._save_tasks()
|
||||
if self.scheduler.running:
|
||||
try:
|
||||
self.scheduler.remove_job(task_id)
|
||||
except:
|
||||
pass
|
111
src/aigpt/transmission.py
Normal file
111
src/aigpt/transmission.py
Normal file
@@ -0,0 +1,111 @@
|
||||
"""Transmission controller for autonomous message sending"""
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Optional
|
||||
import logging
|
||||
|
||||
from .models import Relationship
|
||||
from .persona import Persona
|
||||
|
||||
|
||||
class TransmissionController:
|
||||
"""Controls when and how AI transmits messages autonomously"""
|
||||
|
||||
def __init__(self, persona: Persona, data_dir: Path):
|
||||
self.persona = persona
|
||||
self.data_dir = data_dir
|
||||
self.transmission_log_file = data_dir / "transmissions.json"
|
||||
self.transmissions: List[Dict] = []
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self._load_transmissions()
|
||||
|
||||
def _load_transmissions(self):
|
||||
"""Load transmission history"""
|
||||
if self.transmission_log_file.exists():
|
||||
with open(self.transmission_log_file, 'r', encoding='utf-8') as f:
|
||||
self.transmissions = json.load(f)
|
||||
|
||||
def _save_transmissions(self):
|
||||
"""Save transmission history"""
|
||||
with open(self.transmission_log_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.transmissions, f, indent=2, default=str)
|
||||
|
||||
def check_transmission_eligibility(self) -> Dict[str, Relationship]:
|
||||
"""Check which users are eligible for transmission"""
|
||||
eligible = self.persona.relationships.get_transmission_eligible()
|
||||
|
||||
# Additional checks could be added here
|
||||
# - Time since last transmission
|
||||
# - User online status
|
||||
# - Context appropriateness
|
||||
|
||||
return eligible
|
||||
|
||||
def generate_transmission_message(self, user_id: str) -> Optional[str]:
|
||||
"""Generate a message to transmit to user"""
|
||||
if not self.persona.can_transmit_to(user_id):
|
||||
return None
|
||||
|
||||
state = self.persona.get_current_state()
|
||||
relationship = self.persona.relationships.get_or_create_relationship(user_id)
|
||||
|
||||
# Get recent memories related to this user
|
||||
active_memories = self.persona.memory.get_active_memories(limit=3)
|
||||
|
||||
# Simple message generation based on mood and relationship
|
||||
if state.fortune.breakthrough_triggered:
|
||||
message = "Something special happened today! I felt compelled to reach out."
|
||||
elif state.current_mood == "joyful":
|
||||
message = "I was thinking of you today. Hope you're doing well!"
|
||||
elif relationship.status.value == "close_friend":
|
||||
message = "I've been reflecting on our conversations. Thank you for being here."
|
||||
else:
|
||||
message = "Hello! I wanted to check in with you."
|
||||
|
||||
return message
|
||||
|
||||
def record_transmission(self, user_id: str, message: str, success: bool):
|
||||
"""Record a transmission attempt"""
|
||||
transmission = {
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"user_id": user_id,
|
||||
"message": message,
|
||||
"success": success,
|
||||
"mood": self.persona.get_current_state().current_mood,
|
||||
"relationship_score": self.persona.relationships.get_or_create_relationship(user_id).score
|
||||
}
|
||||
|
||||
self.transmissions.append(transmission)
|
||||
self._save_transmissions()
|
||||
|
||||
if success:
|
||||
self.logger.info(f"Successfully transmitted to {user_id}")
|
||||
else:
|
||||
self.logger.warning(f"Failed to transmit to {user_id}")
|
||||
|
||||
def get_transmission_stats(self, user_id: Optional[str] = None) -> Dict:
|
||||
"""Get transmission statistics"""
|
||||
if user_id:
|
||||
user_transmissions = [t for t in self.transmissions if t["user_id"] == user_id]
|
||||
else:
|
||||
user_transmissions = self.transmissions
|
||||
|
||||
if not user_transmissions:
|
||||
return {
|
||||
"total": 0,
|
||||
"successful": 0,
|
||||
"failed": 0,
|
||||
"success_rate": 0.0
|
||||
}
|
||||
|
||||
successful = sum(1 for t in user_transmissions if t["success"])
|
||||
total = len(user_transmissions)
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"successful": successful,
|
||||
"failed": total - successful,
|
||||
"success_rate": successful / total if total > 0 else 0.0
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
|
||||
use aigpt::mcp::BaseMCPServer;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// 環境変数から設定を読み込み
|
||||
let auto_execute = env::var("MEMORY_AUTO_EXECUTE")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.parse::<bool>()
|
||||
.unwrap_or(false);
|
||||
|
||||
let auto_save = env::var("MEMORY_AUTO_SAVE")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.parse::<bool>()
|
||||
.unwrap_or(false);
|
||||
|
||||
let auto_search = env::var("MEMORY_AUTO_SEARCH")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.parse::<bool>()
|
||||
.unwrap_or(false);
|
||||
|
||||
let trigger_sensitivity = env::var("TRIGGER_SENSITIVITY")
|
||||
.unwrap_or_else(|_| "medium".to_string());
|
||||
|
||||
// 設定をログ出力
|
||||
eprintln!("Memory MCP Server (Standard) starting with config:");
|
||||
eprintln!(" AUTO_EXECUTE: {}", auto_execute);
|
||||
eprintln!(" AUTO_SAVE: {}", auto_save);
|
||||
eprintln!(" AUTO_SEARCH: {}", auto_search);
|
||||
eprintln!(" TRIGGER_SENSITIVITY: {}", trigger_sensitivity);
|
||||
|
||||
let mut server = BaseMCPServer::new().await?;
|
||||
server.run().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
|
||||
use aigpt::mcp::ExtendedMCPServer;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// 環境変数から拡張機能の設定を読み込み
|
||||
let auto_execute = env::var("MEMORY_AUTO_EXECUTE")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.parse::<bool>()
|
||||
.unwrap_or(false);
|
||||
|
||||
let auto_save = env::var("MEMORY_AUTO_SAVE")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.parse::<bool>()
|
||||
.unwrap_or(false);
|
||||
|
||||
let auto_search = env::var("MEMORY_AUTO_SEARCH")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.parse::<bool>()
|
||||
.unwrap_or(false);
|
||||
|
||||
let trigger_sensitivity = env::var("TRIGGER_SENSITIVITY")
|
||||
.unwrap_or_else(|_| "medium".to_string());
|
||||
|
||||
let enable_ai_analysis = cfg!(feature = "ai-analysis");
|
||||
let enable_semantic_search = cfg!(feature = "semantic-search");
|
||||
let enable_web_integration = cfg!(feature = "web-integration");
|
||||
|
||||
// 拡張設定をログ出力
|
||||
eprintln!("Memory MCP Server (Extended) starting with config:");
|
||||
eprintln!(" AUTO_EXECUTE: {}", auto_execute);
|
||||
eprintln!(" AUTO_SAVE: {}", auto_save);
|
||||
eprintln!(" AUTO_SEARCH: {}", auto_search);
|
||||
eprintln!(" TRIGGER_SENSITIVITY: {}", trigger_sensitivity);
|
||||
eprintln!(" AI_ANALYSIS: {}", enable_ai_analysis);
|
||||
eprintln!(" SEMANTIC_SEARCH: {}", enable_semantic_search);
|
||||
eprintln!(" WEB_INTEGRATION: {}", enable_web_integration);
|
||||
|
||||
let mut server = ExtendedMCPServer::new().await?;
|
||||
server.run().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -1,2 +0,0 @@
|
||||
pub mod memory;
|
||||
pub mod mcp;
|
49
src/main.rs
49
src/main.rs
@@ -1,49 +0,0 @@
|
||||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub mod memory;
|
||||
pub mod mcp;
|
||||
|
||||
use memory::MemoryManager;
|
||||
use mcp::BaseMCPServer;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "aigpt")]
|
||||
#[command(about = "Simple memory storage for Claude with MCP")]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Start MCP server
|
||||
Server,
|
||||
/// Start MCP server (alias for server)
|
||||
Serve,
|
||||
/// Import ChatGPT conversations
|
||||
Import {
|
||||
/// Path to conversations.json file
|
||||
file: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
match cli.command {
|
||||
Commands::Server | Commands::Serve => {
|
||||
let mut server = BaseMCPServer::new().await?;
|
||||
server.run().await?;
|
||||
}
|
||||
Commands::Import { file } => {
|
||||
let mut memory_manager = MemoryManager::new().await?;
|
||||
memory_manager.import_chatgpt_conversations(&file).await?;
|
||||
println!("Import completed successfully");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
280
src/mcp/base.rs
280
src/mcp/base.rs
@@ -1,280 +0,0 @@
|
||||
use anyhow::Result;
|
||||
use serde_json::{json, Value};
|
||||
use std::io::{self, BufRead, Write};
|
||||
|
||||
use crate::memory::MemoryManager;
|
||||
|
||||
pub struct BaseMCPServer {
|
||||
pub memory_manager: MemoryManager,
|
||||
}
|
||||
|
||||
impl BaseMCPServer {
|
||||
pub async fn new() -> Result<Self> {
|
||||
let memory_manager = MemoryManager::new().await?;
|
||||
Ok(BaseMCPServer { memory_manager })
|
||||
}
|
||||
|
||||
pub async fn run(&mut 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).await;
|
||||
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(())
|
||||
}
|
||||
|
||||
pub async fn handle_request(&mut 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).await,
|
||||
_ => 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.1.0"
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ツールリストハンドラ (拡張可能)
|
||||
pub fn handle_tools_list(&self, id: Value) -> Value {
|
||||
let tools = self.get_available_tools();
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"result": {
|
||||
"tools": tools
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 基本ツール定義 (拡張で上書き可能)
|
||||
pub fn get_available_tools(&self) -> Vec<Value> {
|
||||
vec![
|
||||
json!({
|
||||
"name": "create_memory",
|
||||
"description": "Create a new memory entry",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"content": {
|
||||
"type": "string",
|
||||
"description": "Content of the memory"
|
||||
}
|
||||
},
|
||||
"required": ["content"]
|
||||
}
|
||||
}),
|
||||
json!({
|
||||
"name": "search_memories",
|
||||
"description": "Search memories by content",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "Search query"
|
||||
}
|
||||
},
|
||||
"required": ["query"]
|
||||
}
|
||||
}),
|
||||
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": "list_conversations",
|
||||
"description": "List all imported conversations",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {}
|
||||
}
|
||||
})
|
||||
]
|
||||
}
|
||||
|
||||
// ツール呼び出しハンドラ
|
||||
async fn handle_tools_call(&mut 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).await;
|
||||
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"result": {
|
||||
"content": [{
|
||||
"type": "text",
|
||||
"text": result.to_string()
|
||||
}]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ツール実行 (拡張で上書き可能)
|
||||
pub async fn execute_tool(&mut self, tool_name: &str, arguments: &Value) -> Value {
|
||||
match tool_name {
|
||||
"create_memory" => self.tool_create_memory(arguments),
|
||||
"search_memories" => self.tool_search_memories(arguments),
|
||||
"update_memory" => self.tool_update_memory(arguments),
|
||||
"delete_memory" => self.tool_delete_memory(arguments),
|
||||
"list_conversations" => self.tool_list_conversations(),
|
||||
_ => json!({
|
||||
"success": false,
|
||||
"error": format!("Unknown tool: {}", tool_name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 基本ツール実装
|
||||
fn tool_create_memory(&mut self, arguments: &Value) -> Value {
|
||||
let content = arguments["content"].as_str().unwrap_or("");
|
||||
match self.memory_manager.create_memory(content) {
|
||||
Ok(id) => json!({
|
||||
"success": true,
|
||||
"id": id,
|
||||
"message": "Memory created successfully"
|
||||
}),
|
||||
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("");
|
||||
let memories = self.memory_manager.search_memories(query);
|
||||
json!({
|
||||
"success": true,
|
||||
"memories": memories.into_iter().map(|m| json!({
|
||||
"id": m.id,
|
||||
"content": m.content,
|
||||
"created_at": m.created_at,
|
||||
"updated_at": m.updated_at
|
||||
})).collect::<Vec<_>>()
|
||||
})
|
||||
}
|
||||
|
||||
fn tool_update_memory(&mut self, arguments: &Value) -> Value {
|
||||
let id = arguments["id"].as_str().unwrap_or("");
|
||||
let content = arguments["content"].as_str().unwrap_or("");
|
||||
match self.memory_manager.update_memory(id, content) {
|
||||
Ok(()) => json!({
|
||||
"success": true,
|
||||
"message": "Memory updated successfully"
|
||||
}),
|
||||
Err(e) => json!({
|
||||
"success": false,
|
||||
"error": e.to_string()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn tool_delete_memory(&mut self, arguments: &Value) -> Value {
|
||||
let id = arguments["id"].as_str().unwrap_or("");
|
||||
match self.memory_manager.delete_memory(id) {
|
||||
Ok(()) => json!({
|
||||
"success": true,
|
||||
"message": "Memory deleted successfully"
|
||||
}),
|
||||
Err(e) => json!({
|
||||
"success": false,
|
||||
"error": e.to_string()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn tool_list_conversations(&self) -> Value {
|
||||
let conversations = self.memory_manager.list_conversations();
|
||||
json!({
|
||||
"success": true,
|
||||
"conversations": conversations.into_iter().map(|c| json!({
|
||||
"id": c.id,
|
||||
"title": c.title,
|
||||
"created_at": c.created_at,
|
||||
"message_count": c.message_count
|
||||
})).collect::<Vec<_>>()
|
||||
})
|
||||
}
|
||||
|
||||
// 不明なメソッドハンドラ
|
||||
fn handle_unknown_method(&self, id: Value) -> Value {
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"error": {
|
||||
"code": -32601,
|
||||
"message": "Method not found"
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -1,293 +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,
|
||||
"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")))
|
||||
}
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
pub mod base;
|
||||
pub mod extended;
|
||||
|
||||
pub use base::BaseMCPServer;
|
||||
pub use extended::ExtendedMCPServer;
|
241
src/memory.rs
241
src/memory.rs
@@ -1,241 +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;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Memory {
|
||||
pub id: String,
|
||||
pub content: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
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(),
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
};
|
||||
|
||||
self.memories.insert(id.clone(), memory);
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user