Skip to content

Commit 1f6acaf

Browse files
authored
Merge pull request #2 from imxcstar/dev
Dev
2 parents 3b2f863 + e6a088d commit 1f6acaf

24 files changed

+1868
-255
lines changed

CLAUDE.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,48 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
66

77
```bash
88
dotnet build
9-
dotnet run --project sharpclaw # TUI mode (Terminal.Gui)
10-
dotnet run --project sharpclaw config # Re-run config dialog
11-
dotnet run --project sharpclaw serve # Web mode (WebSocket server, default port 5000)
12-
dotnet run --project sharpclaw serve --port 8080
9+
dotnet run --project sharpclaw tui # TUI mode (Terminal.Gui)
10+
dotnet run --project sharpclaw config # Re-run config dialog
11+
dotnet run --project sharpclaw web # Web mode (WebSocket server)
12+
dotnet run --project sharpclaw web --address 0.0.0.0 --port 8080
13+
dotnet run --project sharpclaw qqbot # QQ Bot mode
1314
```
1415

15-
First run auto-launches the config dialog (Terminal.Gui `ConfigDialog`) which writes `~/.sharpclaw/config.json`. Web mode requires config to exist already.
16+
First run of `tui` auto-launches the config dialog (Terminal.Gui `ConfigDialog`) which writes `~/.sharpclaw/config.json`. Web and QQ Bot modes require config to exist already. Running with no command (or `help`) prints usage info.
1617

1718
No test project exists. Target framework is .NET 10 (`net10.0`).
1819

1920
## Configuration
2021

21-
All settings live in `~/.sharpclaw/config.json`. Supports three providers: Anthropic, OpenAI, Gemini. Each sub-agent (main, recaller, saver, summarizer) can override the default provider/endpoint/model or inherit from `default`.
22+
All settings live in `~/.sharpclaw/config.json`. Supports three providers: Anthropic, OpenAI, Gemini. Each sub-agent (main, recaller, saver, summarizer) can override the default provider/endpoint/model or inherit from `default`. A `channels` section configures per-frontend settings (TUI, Web listen address/port, QQ Bot credentials).
2223

23-
API keys are encrypted at rest with AES-256-CBC (`DataProtector`). The encryption key is stored in the OS credential manager via `KeyStore` (Windows Credential Manager / macOS Keychain / Linux libsecret).
24+
API keys are encrypted at rest with AES-256-CBC (`DataProtector`). The encryption key is stored in the OS credential manager via `KeyStore` (Windows Credential Manager / macOS Keychain / Linux libsecret). Config has auto-migration (current version 8, see `ConfigMigrator`).
2425

2526
See `Core/SharpclawConfig.cs` for the schema and `Core/ClientFactory.cs` for client instantiation.
2627

2728
## Architecture
2829

2930
Sharpclaw is a console/web AI agent with long-term memory, built on `Microsoft.Agents.AI` and `Microsoft.Extensions.AI`.
3031

31-
### Dual Frontend via IChatIO
32+
### Multi-Channel Frontend via IChatIO
3233

33-
The AI engine is decoupled from the frontend through `Abstractions/IChatIO.cs`. Two implementations exist:
34-
- `UI/ChatWindow.cs` — TUI frontend using Terminal.Gui v2 (chat area + log area + input field)
35-
- `Web/WebSocketChatIO.cs` — WebSocket frontend, served by `Web/WebServer.cs` (ASP.NET Core), single-client only
34+
The AI engine is decoupled from the frontend through `Abstractions/IChatIO.cs`. Three implementations exist under `Channels/`:
35+
- `Channels/Tui/ChatWindow.cs` — TUI frontend using Terminal.Gui v2 (chat area + log area + input field)
36+
- `Channels/Web/WebSocketChatIO.cs` — WebSocket frontend, served by `Channels/Web/WebServer.cs` (ASP.NET Core), single-client only
37+
- `Channels/QQBot/QQBotChatIO.cs` — QQ Bot frontend via `Luolan.QQBot`, served by `Channels/QQBot/QQBotServer.cs`
3638

37-
Both frontends share the same agent logic. `Abstractions/IAppLogger.cs` + `AppLogger` provide a similar abstraction for logging.
39+
All frontends share the same agent logic. `Abstractions/IAppLogger.cs` + `AppLogger` provide a similar abstraction for logging.
3840

3941
### Entry Point (`Program.cs`)
4042

41-
1. `args.Contains("serve")` → dispatches to `WebServer.RunAsync()` (web mode)
42-
2. Otherwise → Terminal.Gui init → `ConfigDialog` if config missing → `AgentBootstrap.Initialize()``ChatWindow` + `MainAgent` → TUI event loop
43+
`switch` on the first CLI arg:
44+
1. `tui` → Terminal.Gui init → `ConfigDialog` if config missing → `AgentBootstrap.Initialize()``ChatWindow` + `MainAgent` → TUI event loop
45+
2. `web` → dispatches to `WebServer.RunAsync()` (WebSocket server)
46+
3. `qqbot` → dispatches to `QQBotServer.RunAsync()` (QQ Bot service)
47+
4. `config` → opens `ConfigDialog` only
48+
5. Default / `help` → prints usage info
4349

44-
`Core/AgentBootstrap.Initialize()` is the shared bootstrap used by both TUI and Web mode: loads config, creates `TaskManager`, registers all command tools as `AIFunction[]`, creates `IMemoryStore`.
50+
`Core/AgentBootstrap.Initialize()` is the shared bootstrap used by all channels: loads config, creates `TaskManager`, registers all command tools as `AIFunction[]`, creates `IMemoryStore`.
4551

4652
### MainAgent and Memory Pipeline
4753

@@ -84,6 +90,5 @@ Terminal.Gui dialog with `TabView` pages: default provider, per-agent overrides
8490
- Language: Chinese prompts and UI strings, English code identifiers
8591
- All sub-agents use tool calling (not text parsing) for structured output
8692
- Injected messages use `AdditionalProperties` dictionary keys (`AutoMemoryKey`, `AutoSummaryKey`) for identification and stripping
87-
- `ChatMessage` alias: `using ChatMessage = Microsoft.Extensions.AI.ChatMessage` (disambiguates from OpenAI's type)
8893
- Session persistence: `history.json` (agent state), `memories.json` (vector memory store)
8994
- WebSocket protocol: JSON messages with `type` field (`input`, `cancel`, `chat`, `chatLine`, `state`)

README.md

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ A .NET 10 AI agent with long-term memory and system operation tools. Supports An
1515
- **Vector semantic search** — Two-phase retrieval: vector embedding recall + optional reranking
1616
- **Semantic deduplication** — Automatically merges memories when cosine similarity exceeds threshold, avoiding redundancy
1717
- **System tools** — File operations, process execution (dotnet/node/docker), HTTP requests, background task management
18-
- **Dual frontend** — TUI terminal interface (Terminal.Gui v2) + WebSocket web interface, sharing the same agent logic
19-
- **Slash commands** — Type `/` to trigger autocomplete, supports `/exit`, `/quit` and more
18+
- **Multi-channel architecture** — TUI terminal interface (Terminal.Gui v2) + WebSocket web interface + QQ Bot, all sharing the same agent logic via `IChatIO` abstraction
19+
- **Slash commands** — Type `/` to trigger autocomplete, supports `/exit`, `/quit`, `/config`, `/help`
2020
- **Streaming output** — Real-time streaming AI responses with reasoning logs and tool call tracing
21+
- **Configurable keybindings** — TUI quit key, log toggle key, and cancel key are all configurable
2122
- **Config data encryption** — Sensitive data like API keys are encrypted with AES-256-CBC, with encryption keys stored in the OS credential manager (Windows Credential Manager / macOS Keychain / Linux libsecret)
2223

2324
## Requirements
@@ -29,28 +30,45 @@ A .NET 10 AI agent with long-term memory and system operation tools. Supports An
2930
### TUI Mode (Terminal Interface)
3031

3132
```bash
32-
dotnet run --project sharpclaw
33+
dotnet run --project sharpclaw tui
3334
```
3435

3536
First run automatically launches the configuration wizard. Select your AI provider, enter API keys, and configure per-agent settings.
3637

3738
![Config Dialog](preview/config.png)
3839

39-
Config is saved to `~/.sharpclaw/config.json`. After that, launching goes straight to interactive chat. Type `/exit` or `/quit` to exit, `Ctrl+Q` to quit.
40+
Config is saved to `~/.sharpclaw/config.json`. After that, launching goes straight to interactive chat. Type `/exit` or `/quit` to exit, `Ctrl+Q` to quit (default, configurable).
4041

4142
### Web Mode (WebSocket Server)
4243

4344
```bash
44-
dotnet run --project sharpclaw serve
45-
dotnet run --project sharpclaw serve --port 8080
45+
dotnet run --project sharpclaw web
46+
dotnet run --project sharpclaw web --address 0.0.0.0 --port 8080
4647
```
4748

48-
Visit `http://localhost:5000` (default port) to open the web chat interface. The Web UI supports Markdown rendering, code highlighting, connection status indicators, and real-time status display.
49+
Visit `http://localhost:5000` (default) to open the web chat interface. The Web UI supports Markdown rendering, code highlighting, connection status indicators, and real-time status display.
4950

5051
![Web Chat Interface](preview/web.png)
5152

5253
> Note: Web mode requires completing configuration via TUI mode first. Only one client connection is supported at a time.
5354
55+
### QQ Bot Mode
56+
57+
```bash
58+
dotnet run --project sharpclaw qqbot
59+
```
60+
61+
Runs as a QQ Bot service, receiving messages from QQ channels, groups, and private chats. Requires QQ Bot AppId and ClientSecret configured in the config file.
62+
63+
> Note: QQ Bot mode requires completing configuration via TUI mode first and enabling QQ Bot in the channels config.
64+
65+
### Other Commands
66+
67+
```bash
68+
dotnet run --project sharpclaw config # Open config dialog
69+
dotnet run --project sharpclaw help # Show usage info
70+
```
71+
5472
## Configuration
5573

5674
Run the config wizard:
@@ -63,7 +81,7 @@ Config file structure (`~/.sharpclaw/config.json`):
6381

6482
```json
6583
{
66-
"version": 4,
84+
"version": 8,
6785
"default": {
6886
"provider": "anthropic",
6987
"endpoint": "https://api.anthropic.com",
@@ -85,11 +103,30 @@ Config file structure (`~/.sharpclaw/config.json`):
85103
"rerankEndpoint": "https://dashscope.aliyuncs.com/compatible-api/v1/reranks",
86104
"rerankApiKey": "sk-xxx",
87105
"rerankModel": "qwen3-vl-rerank"
106+
},
107+
"channels": {
108+
"tui": {
109+
"logCollapsed": false,
110+
"quitKey": "Ctrl+Q",
111+
"toggleLogKey": "Ctrl+L",
112+
"cancelKey": "Esc"
113+
},
114+
"web": {
115+
"enabled": true,
116+
"listenAddress": "localhost",
117+
"port": 5000
118+
},
119+
"qqBot": {
120+
"enabled": false,
121+
"appId": "",
122+
"clientSecret": "",
123+
"sandbox": false
124+
}
88125
}
89126
}
90127
```
91128

92-
Each agent inherits from `default` unless overridden. Set `"enabled": false` to disable a sub-agent.
129+
Each agent inherits from `default` unless overridden. Set `"enabled": false` to disable a sub-agent. Channel settings configure per-frontend behavior.
93130

94131
## Memory Pipeline
95132

@@ -113,15 +150,28 @@ User Input
113150

114151
```
115152
sharpclaw/
116-
├── Program.cs # Entry point: TUI / Web mode dispatch
153+
├── Program.cs # Entry point: command dispatch (tui/web/qqbot/config/help)
117154
├── Abstractions/ # Interface definitions
118-
│ ├── IChatIO.cs # Frontend I/O abstraction (shared by TUI and WebSocket)
155+
│ ├── IChatIO.cs # Frontend I/O abstraction (shared by all channels)
119156
│ └── IAppLogger.cs # Logger abstraction
120157
├── Agents/ # Agents
121158
│ ├── MainAgent.cs # Main agent: conversation loop, streaming, tool calls
122159
│ ├── MemoryRecaller.cs # Memory recall: incremental injection of relevant memories
123160
│ ├── MemorySaver.cs # Memory save: analyze conversation, auto save/update/delete
124161
│ └── ConversationSummarizer.cs # Conversation summarizer: incremental summary of trimmed content
162+
├── Channels/ # Multi-channel frontends
163+
│ ├── Tui/ # TUI frontend (Terminal.Gui v2)
164+
│ │ ├── ChatWindow.cs # Main chat window (chat + log + input areas)
165+
│ │ ├── SlashCommandSuggestionGenerator.cs # Slash command autocomplete
166+
│ │ └── TerminalGuiLogger.cs # TUI logger implementation
167+
│ ├── Web/ # WebSocket frontend (ASP.NET Core)
168+
│ │ ├── WebServer.cs # ASP.NET Core host with embedded index.html
169+
│ │ ├── WebSocketChatIO.cs # WebSocket IChatIO implementation
170+
│ │ ├── WebSocketSender.cs # WebSocket message sender
171+
│ │ └── WebSocketLogger.cs # WebSocket logger implementation
172+
│ └── QQBot/ # QQ Bot frontend
173+
│ ├── QQBotServer.cs # QQ Bot service host (channel/group/C2C messages)
174+
│ └── QQBotChatIO.cs # QQ Bot IChatIO implementation
125175
├── Chat/
126176
│ └── SlidingWindowChatReducer.cs # Sliding window reducer with integrated memory pipeline
127177
├── Clients/
@@ -133,35 +183,27 @@ sharpclaw/
133183
│ ├── SystemCommands.cs # System info, exit
134184
│ └── TaskCommands.cs # Background task management
135185
├── Core/
136-
│ ├── AgentBootstrap.cs # Shared initialization logic
186+
│ ├── AgentBootstrap.cs # Shared initialization logic (all channels)
137187
│ ├── ClientFactory.cs # Multi-provider AI client factory
138188
│ ├── DataProtector.cs # AES-256-CBC encryption/decryption
139189
│ ├── KeyStore.cs # OS credential manager key storage
140-
│ ├── SharpclawConfig.cs # Config management (version migration, encryption)
190+
│ ├── SharpclawConfig.cs # Config management (schema, version migration, encryption)
141191
│ ├── Serialization/ # JSON serialization
142192
│ └── TaskManagement/ # Background tasks: process tasks, native tasks
143193
├── Memory/
144194
│ ├── IMemoryStore.cs # Memory store interface
145195
│ ├── MemoryEntry.cs # Memory entry model
146196
│ ├── VectorMemoryStore.cs # Vector memory store (embedding + cosine + dedup + rerank)
147197
│ └── InMemoryMemoryStore.cs # In-memory store (for testing)
148-
├── UI/ # TUI frontend
149-
│ ├── ChatWindow.cs # Main chat window (chat + log + input areas)
150-
│ ├── ConfigDialog.cs # Config wizard dialog (TabView pages)
151-
│ ├── SlashCommandSuggestionGenerator.cs # Slash command autocomplete
152-
│ ├── AppLogger.cs # Logger management
153-
│ └── TerminalGuiLogger.cs # TUI logger implementation
154-
├── Web/ # WebSocket frontend
155-
│ ├── WebServer.cs # ASP.NET Core host
156-
│ ├── WebSocketChatIO.cs # WebSocket IChatIO implementation
157-
│ ├── WebSocketSender.cs # WebSocket message sender
158-
│ └── WebSocketLogger.cs # WebSocket logger implementation
198+
├── UI/ # Shared UI utilities
199+
│ ├── AppLogger.cs # Global logger management
200+
│ └── ConfigDialog.cs # Config wizard dialog (TabView pages)
159201
└── wwwroot/
160-
└── index.html # Web chat interface (single-file SPA)
202+
└── index.html # Web chat interface (single-file SPA, embedded resource)
161203
```
162204

163205
## Data Persistence
164206

165-
- `~/.sharpclaw/config.json` — Provider and agent configuration
207+
- `~/.sharpclaw/config.json` — Provider, agent, and channel configuration
166208
- `history.json` — Session state, auto-restored on startup
167209
- `memories.json` — Vector memory store

0 commit comments

Comments
 (0)