mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
chore: tied project
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": ["../../eslint.config.mjs"],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-explicit-any": "off"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,221 +0,0 @@
|
||||
# Memoh CLI 架构说明
|
||||
|
||||
## 项目重构概述
|
||||
|
||||
本项目已重构为两个清晰分离的层次:
|
||||
|
||||
1. **Core 层** (`src/core/`): 纯粹的功能函数,不依赖任何 CLI UI 库
|
||||
2. **CLI 层** (`src/cli/`): 命令行交互界面,负责用户输入输出
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
src/
|
||||
├── core/ # 核心功能层(可被其他项目使用)
|
||||
│ ├── index.ts # 统一导出所有核心功能
|
||||
│ ├── auth.ts # 认证相关功能
|
||||
│ ├── user.ts # 用户管理功能
|
||||
│ ├── model.ts # 模型配置功能
|
||||
│ ├── agent.ts # AI 对话功能
|
||||
│ ├── memory.ts # 记忆管理功能
|
||||
│ ├── schedule.ts # 定时任务功能
|
||||
│ ├── settings.ts # 设置管理功能
|
||||
│ ├── debug.ts # 调试工具
|
||||
│ ├── config.ts # 配置管理
|
||||
│ └── client.ts # API 客户端
|
||||
├── cli/ # CLI 交互层
|
||||
│ ├── index.ts # CLI 入口
|
||||
│ └── commands/ # 命令定义
|
||||
│ ├── auth.ts
|
||||
│ ├── user.ts
|
||||
│ ├── model.ts
|
||||
│ ├── agent.ts
|
||||
│ ├── memory.ts
|
||||
│ ├── schedule.ts
|
||||
│ ├── config.ts
|
||||
│ └── debug.ts
|
||||
├── types/ # 类型定义
|
||||
│ └── index.ts
|
||||
├── utils/ # 工具函数
|
||||
│ └── index.ts
|
||||
└── index.ts # 主导出文件
|
||||
```
|
||||
|
||||
## Core 层特点
|
||||
|
||||
Core 层提供纯粹的功能函数,特点:
|
||||
|
||||
- ✅ **无 UI 依赖**: 不使用 chalk, ora, inquirer 等 CLI UI 库
|
||||
- ✅ **类型安全**: 提供完整的 TypeScript 类型定义
|
||||
- ✅ **错误处理**: 通过 throw Error 返回错误,调用者可自行处理
|
||||
- ✅ **可复用**: 可被 CLI 或其他项目(如 Web 应用)导入使用
|
||||
- ✅ **单一职责**: 每个模块只负责特定功能域
|
||||
|
||||
### Core 层 API 示例
|
||||
|
||||
```typescript
|
||||
// Auth
|
||||
import { login, logout, getCurrentUser } from '@memoh/cli/core'
|
||||
|
||||
await login({ username: 'admin', password: 'password' })
|
||||
const user = await getCurrentUser()
|
||||
logout()
|
||||
|
||||
// Agent
|
||||
import { chat, chatStream } from '@memoh/cli/core'
|
||||
|
||||
// 非流式对话
|
||||
const response = await chat({
|
||||
message: 'Hello',
|
||||
language: 'Chinese'
|
||||
})
|
||||
|
||||
// 流式对话
|
||||
await chatStream({ message: 'Hello' }, async (event) => {
|
||||
if (event.type === 'text-delta') {
|
||||
console.log(event.text)
|
||||
}
|
||||
})
|
||||
|
||||
// Model
|
||||
import { listModels, createModel } from '@memoh/cli/core'
|
||||
|
||||
const models = await listModels()
|
||||
const newModel = await createModel({
|
||||
name: 'GPT-4',
|
||||
modelId: 'gpt-4',
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-xxx',
|
||||
clientType: 'openai',
|
||||
type: 'chat'
|
||||
})
|
||||
|
||||
// Memory
|
||||
import { searchMemory, addMemory, getMessages } from '@memoh/cli/core'
|
||||
|
||||
const memories = await searchMemory({ query: 'test', limit: 10 })
|
||||
await addMemory({ content: 'Important note' })
|
||||
const messages = await getMessages({ page: 1, limit: 20 })
|
||||
```
|
||||
|
||||
## CLI 层特点
|
||||
|
||||
CLI 层负责用户交互,特点:
|
||||
|
||||
- ✅ **命令定义**: 使用 commander.js 定义命令
|
||||
- ✅ **美化输出**: 使用 chalk 颜色、ora 加载动画
|
||||
- ✅ **交互输入**: 使用 inquirer 提示用户输入
|
||||
- ✅ **错误显示**: 友好的错误信息展示
|
||||
- ✅ **调用 Core**: 所有业务逻辑调用 Core 层函数
|
||||
|
||||
## 使用方式
|
||||
|
||||
### 1. 作为 CLI 使用
|
||||
|
||||
```bash
|
||||
# 安装
|
||||
pnpm install
|
||||
|
||||
# 运行命令
|
||||
memoh auth login
|
||||
memoh agent chat "Hello"
|
||||
memoh model list
|
||||
```
|
||||
|
||||
### 2. 作为库使用
|
||||
|
||||
在其他项目中导入:
|
||||
|
||||
```typescript
|
||||
// 导入所有功能
|
||||
import * as memoh from '@memoh/cli'
|
||||
|
||||
// 或导入特定模块
|
||||
import { login, chat, listModels } from '@memoh/cli'
|
||||
import * as auth from '@memoh/cli/core'
|
||||
import type { User, Model } from '@memoh/cli/types'
|
||||
|
||||
// 使用
|
||||
await memoh.login({ username: 'admin', password: 'password' })
|
||||
const response = await memoh.chat({ message: 'Hello' })
|
||||
const models = await memoh.listModels()
|
||||
```
|
||||
|
||||
## 包导出
|
||||
|
||||
`package.json` 中的导出配置:
|
||||
|
||||
```json
|
||||
{
|
||||
"exports": {
|
||||
".": "./src/index.ts", // 主入口,导出所有 core 功能
|
||||
"./core": "./src/core/index.ts", // Core 层
|
||||
"./types": "./src/types/index.ts", // 类型定义
|
||||
"./utils": "./src/utils/index.ts" // 工具函数
|
||||
},
|
||||
"bin": {
|
||||
"memoh": "./src/cli/index.ts" // CLI 入口
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 设计原则
|
||||
|
||||
1. **关注点分离**: CLI UI 和业务逻辑完全分离
|
||||
2. **可测试性**: Core 层可以独立测试,无需模拟 CLI 环境
|
||||
3. **可复用性**: Core 层可在不同环境使用(CLI、Web、Desktop 等)
|
||||
4. **类型安全**: 完整的 TypeScript 类型定义
|
||||
5. **错误处理**: 统一的错误处理机制
|
||||
|
||||
## 迁移指南
|
||||
|
||||
如果你之前使用旧版本的代码,迁移方式:
|
||||
|
||||
### 旧代码(CLI 层调用)
|
||||
```typescript
|
||||
// 这些只能在 CLI 中使用,有 UI 输出
|
||||
```
|
||||
|
||||
### 新代码(Core 层调用)
|
||||
```typescript
|
||||
// 可以在任何地方使用,无 UI 依赖
|
||||
import { login, chat } from '@memoh/cli'
|
||||
|
||||
try {
|
||||
await login({ username: 'admin', password: 'password' })
|
||||
const response = await chat({ message: 'Hello' })
|
||||
console.log(response)
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message)
|
||||
}
|
||||
```
|
||||
|
||||
## 开发指南
|
||||
|
||||
### 添加新功能
|
||||
|
||||
1. 在 `src/core/` 中添加新的核心功能模块
|
||||
2. 在 `src/core/index.ts` 中导出
|
||||
3. 在 `src/cli/commands/` 中添加对应的 CLI 命令
|
||||
4. 在 `src/cli/index.ts` 中注册命令
|
||||
|
||||
### 测试
|
||||
|
||||
Core 层可以直接测试:
|
||||
|
||||
```typescript
|
||||
import { login, chat } from '../src/core'
|
||||
|
||||
test('login should work', async () => {
|
||||
const result = await login({ username: 'test', password: 'test' })
|
||||
expect(result.success).toBe(true)
|
||||
})
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
- Core 层函数通过 `throw Error` 返回错误,调用者需要处理
|
||||
- CLI 层负责美化错误信息和用户反馈
|
||||
- 配置文件位于 `~/.memoh/config.json`
|
||||
- API 客户端使用 Eden Treaty
|
||||
|
||||
@@ -1,506 +0,0 @@
|
||||
# Memoh CLI 使用示例
|
||||
|
||||
## 作为 CLI 使用
|
||||
|
||||
### 认证
|
||||
```bash
|
||||
# 登录
|
||||
memoh auth login -u admin -p password
|
||||
|
||||
# 查看当前用户
|
||||
memoh auth whoami
|
||||
|
||||
# 登出
|
||||
memoh auth logout
|
||||
|
||||
# 配置 API URL
|
||||
memoh auth config -s http://localhost:7002
|
||||
```
|
||||
|
||||
### AI 对话
|
||||
```bash
|
||||
# 单次对话
|
||||
memoh agent chat "今天天气怎么样?"
|
||||
|
||||
# 交互模式
|
||||
memoh agent interactive
|
||||
|
||||
# 指定语言和上下文时间
|
||||
memoh agent chat "Hello" -l English -t 30
|
||||
```
|
||||
|
||||
### 模型管理
|
||||
```bash
|
||||
# 列出所有模型
|
||||
memoh model list
|
||||
|
||||
# 创建模型配置
|
||||
memoh model create \
|
||||
-n "GPT-4" \
|
||||
-m "gpt-4" \
|
||||
-u "https://api.openai.com/v1" \
|
||||
-k "sk-xxx" \
|
||||
-c openai \
|
||||
-t chat
|
||||
|
||||
# 查看默认模型
|
||||
memoh model defaults
|
||||
|
||||
# 获取模型详情
|
||||
memoh model get <model-id>
|
||||
|
||||
# 删除模型
|
||||
memoh model delete <model-id>
|
||||
```
|
||||
|
||||
### 用户管理
|
||||
```bash
|
||||
# 列出所有用户
|
||||
memoh user list
|
||||
|
||||
# 创建用户
|
||||
memoh user create -u newuser -p password -r user
|
||||
|
||||
# 查看用户
|
||||
memoh user get <user-id>
|
||||
|
||||
# 更新密码
|
||||
memoh user update-password <user-id> -p newpassword
|
||||
|
||||
# 删除用户
|
||||
memoh user delete <user-id>
|
||||
```
|
||||
|
||||
### 记忆管理
|
||||
```bash
|
||||
# 搜索记忆
|
||||
memoh memory search "关键词" -l 10
|
||||
|
||||
# 添加记忆
|
||||
memoh memory add "重要的事情"
|
||||
|
||||
# 查看消息历史
|
||||
memoh memory messages -p 1 -l 20
|
||||
|
||||
# 按日期过滤消息
|
||||
memoh memory filter -s 2024-01-01T00:00:00Z -e 2024-12-31T23:59:59Z
|
||||
```
|
||||
|
||||
### 定时任务
|
||||
```bash
|
||||
# 列出所有任务
|
||||
memoh schedule list
|
||||
|
||||
# 创建任务
|
||||
memoh schedule create -t "每日报告" -c "0 9 * * *" -e
|
||||
|
||||
# 查看任务详情
|
||||
memoh schedule get <schedule-id>
|
||||
|
||||
# 更新任务
|
||||
memoh schedule update <schedule-id> -t "新标题" --enabled true
|
||||
|
||||
# 切换任务状态
|
||||
memoh schedule toggle <schedule-id>
|
||||
|
||||
# 删除任务
|
||||
memoh schedule delete <schedule-id>
|
||||
```
|
||||
|
||||
### 配置管理
|
||||
```bash
|
||||
# 查看配置
|
||||
memoh config get
|
||||
|
||||
# 设置配置
|
||||
memoh config set --language Chinese --max-context-time 60 --chat-model <model-id>
|
||||
|
||||
# 交互式配置向导
|
||||
memoh config setup
|
||||
```
|
||||
|
||||
### 调试工具
|
||||
```bash
|
||||
# 测试 API 连接
|
||||
memoh debug ping
|
||||
```
|
||||
|
||||
## 作为库使用
|
||||
|
||||
### 基础使用
|
||||
|
||||
```typescript
|
||||
import * as memoh from '@memoh/cli'
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
// 登录
|
||||
const loginResult = await memoh.login({
|
||||
username: 'admin',
|
||||
password: 'password'
|
||||
})
|
||||
console.log('登录成功:', loginResult.user?.username)
|
||||
|
||||
// 获取当前用户
|
||||
const user = await memoh.getCurrentUser()
|
||||
console.log('当前用户:', user.username)
|
||||
|
||||
// AI 对话
|
||||
const response = await memoh.chat({
|
||||
message: 'Hello',
|
||||
language: 'Chinese'
|
||||
})
|
||||
console.log('AI 回复:', response)
|
||||
|
||||
// 列出模型
|
||||
const models = await memoh.listModels()
|
||||
console.log('模型数量:', models.length)
|
||||
|
||||
// 登出
|
||||
memoh.logout()
|
||||
} catch (error) {
|
||||
console.error('错误:', error.message)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
```
|
||||
|
||||
### 流式对话
|
||||
|
||||
```typescript
|
||||
import { chatStream } from '@memoh/cli'
|
||||
|
||||
async function streamChat() {
|
||||
await chatStream(
|
||||
{
|
||||
message: '讲一个故事',
|
||||
language: 'Chinese'
|
||||
},
|
||||
async (event) => {
|
||||
if (event.type === 'text-delta') {
|
||||
process.stdout.write(event.text || '')
|
||||
} else if (event.type === 'tool-call') {
|
||||
console.log(`\n[使用工具: ${event.toolName}]`)
|
||||
} else if (event.type === 'error') {
|
||||
console.error('\n错误:', event.error)
|
||||
} else if (event.type === 'done') {
|
||||
console.log('\n完成')
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
streamChat()
|
||||
```
|
||||
|
||||
### 模型管理
|
||||
|
||||
```typescript
|
||||
import { createModel, listModels, getDefaultModels } from '@memoh/cli'
|
||||
|
||||
async function manageModels() {
|
||||
// 创建 Chat 模型
|
||||
const chatModel = await createModel({
|
||||
name: 'GPT-4',
|
||||
modelId: 'gpt-4',
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-xxx',
|
||||
clientType: 'openai',
|
||||
type: 'chat'
|
||||
})
|
||||
console.log('创建的模型:', chatModel.id)
|
||||
|
||||
// 创建 Embedding 模型
|
||||
const embeddingModel = await createModel({
|
||||
name: 'Text Embedding',
|
||||
modelId: 'text-embedding-3-small',
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-xxx',
|
||||
clientType: 'openai',
|
||||
type: 'embedding',
|
||||
dimensions: 1536
|
||||
})
|
||||
console.log('Embedding 模型:', embeddingModel.id)
|
||||
|
||||
// 列出所有模型
|
||||
const models = await listModels()
|
||||
models.forEach(item => {
|
||||
console.log(`- ${item.model.name} (${item.model.type})`)
|
||||
})
|
||||
|
||||
// 获取默认模型
|
||||
const defaults = await getDefaultModels()
|
||||
console.log('默认 Chat 模型:', defaults.chat?.name)
|
||||
console.log('默认 Embedding 模型:', defaults.embedding?.name)
|
||||
}
|
||||
|
||||
manageModels()
|
||||
```
|
||||
|
||||
### 记忆管理
|
||||
|
||||
```typescript
|
||||
import { searchMemory, addMemory, getMessages } from '@memoh/cli'
|
||||
|
||||
async function manageMemory() {
|
||||
// 添加记忆
|
||||
await addMemory({ content: '今天学习了 TypeScript' })
|
||||
|
||||
// 搜索记忆
|
||||
const memories = await searchMemory({
|
||||
query: 'TypeScript',
|
||||
limit: 5
|
||||
})
|
||||
|
||||
memories.forEach(memory => {
|
||||
console.log(`相似度: ${(memory.similarity! * 100).toFixed(2)}%`)
|
||||
console.log(`内容: ${memory.content}`)
|
||||
console.log('---')
|
||||
})
|
||||
|
||||
// 获取消息历史
|
||||
const result = await getMessages({ page: 1, limit: 20 })
|
||||
console.log(`总消息数: ${result.pagination.total}`)
|
||||
result.messages.forEach(msg => {
|
||||
console.log(`${msg.role}: ${msg.content}`)
|
||||
})
|
||||
}
|
||||
|
||||
manageMemory()
|
||||
```
|
||||
|
||||
### 用户管理
|
||||
|
||||
```typescript
|
||||
import { createUser, listUsers, updateUserPassword } from '@memoh/cli'
|
||||
|
||||
async function manageUsers() {
|
||||
// 创建用户
|
||||
const newUser = await createUser({
|
||||
username: 'testuser',
|
||||
password: 'password123',
|
||||
role: 'user'
|
||||
})
|
||||
console.log('创建的用户:', newUser.username)
|
||||
|
||||
// 列出所有用户
|
||||
const users = await listUsers()
|
||||
console.log('用户数量:', users.length)
|
||||
|
||||
// 更新密码
|
||||
await updateUserPassword({
|
||||
userId: newUser.id,
|
||||
password: 'newpassword123'
|
||||
})
|
||||
console.log('密码已更新')
|
||||
}
|
||||
|
||||
manageUsers()
|
||||
```
|
||||
|
||||
### 定时任务管理
|
||||
|
||||
```typescript
|
||||
import { createSchedule, listSchedules, toggleSchedule } from '@memoh/cli'
|
||||
|
||||
async function manageSchedules() {
|
||||
// 创建定时任务
|
||||
const schedule = await createSchedule({
|
||||
title: '每日报告',
|
||||
description: '生成每日工作报告',
|
||||
cronExpression: '0 9 * * *',
|
||||
enabled: true
|
||||
})
|
||||
console.log('创建的任务:', schedule.title)
|
||||
|
||||
// 列出所有任务
|
||||
const schedules = await listSchedules()
|
||||
schedules.forEach(s => {
|
||||
console.log(`${s.title}: ${s.cronExpression} (${s.enabled ? '启用' : '禁用'})`)
|
||||
})
|
||||
|
||||
// 切换任务状态
|
||||
const newStatus = await toggleSchedule(schedule.id)
|
||||
console.log('任务状态:', newStatus ? '启用' : '禁用')
|
||||
}
|
||||
|
||||
manageSchedules()
|
||||
```
|
||||
|
||||
### 配置管理
|
||||
|
||||
```typescript
|
||||
import { getSettings, updateSettings, setConfig } from '@memoh/cli'
|
||||
|
||||
async function manageConfig() {
|
||||
// 设置 API URL
|
||||
setConfig('http://localhost:7002')
|
||||
|
||||
// 获取用户设置
|
||||
const settings = await getSettings()
|
||||
console.log('当前语言:', settings.language)
|
||||
console.log('上下文时间:', settings.maxContextLoadTime)
|
||||
|
||||
// 更新设置
|
||||
await updateSettings({
|
||||
language: 'English',
|
||||
maxContextLoadTime: 120,
|
||||
defaultChatModel: 'model-id-xxx'
|
||||
})
|
||||
console.log('设置已更新')
|
||||
}
|
||||
|
||||
manageConfig()
|
||||
```
|
||||
|
||||
### 错误处理
|
||||
|
||||
```typescript
|
||||
import { login, chat } from '@memoh/cli'
|
||||
|
||||
async function handleErrors() {
|
||||
try {
|
||||
await login({
|
||||
username: 'wrong',
|
||||
password: 'wrong'
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
console.error('登录失败:', error.message)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 未登录时调用需要认证的 API
|
||||
await chat({ message: 'Hello' })
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
console.error('需要先登录:', error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleErrors()
|
||||
```
|
||||
|
||||
### 类型安全
|
||||
|
||||
```typescript
|
||||
import type { User, Model, Memory, Schedule } from '@memoh/cli/types'
|
||||
import { listUsers, listModels } from '@memoh/cli'
|
||||
|
||||
async function withTypes() {
|
||||
// 类型自动推导
|
||||
const users: User[] = await listUsers()
|
||||
const models = await listModels()
|
||||
|
||||
// 使用类型
|
||||
users.forEach((user: User) => {
|
||||
console.log(`${user.username} (${user.role})`)
|
||||
})
|
||||
|
||||
models.forEach((item) => {
|
||||
const model: Model = item.model
|
||||
console.log(`${model.name}: ${model.type}`)
|
||||
})
|
||||
}
|
||||
|
||||
withTypes()
|
||||
```
|
||||
|
||||
## Web 应用集成示例
|
||||
|
||||
```typescript
|
||||
// api/memoh.ts
|
||||
import * as memoh from '@memoh/cli'
|
||||
|
||||
export class MemohService {
|
||||
async login(username: string, password: string) {
|
||||
try {
|
||||
return await memoh.login({ username, password })
|
||||
} catch (error) {
|
||||
throw new Error('登录失败')
|
||||
}
|
||||
}
|
||||
|
||||
async chat(message: string, onChunk: (text: string) => void) {
|
||||
await memoh.chatStream(
|
||||
{ message },
|
||||
async (event) => {
|
||||
if (event.type === 'text-delta' && event.text) {
|
||||
onChunk(event.text)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async getModels() {
|
||||
return await memoh.listModels()
|
||||
}
|
||||
}
|
||||
|
||||
// 在 React 组件中使用
|
||||
import { useState } from 'react'
|
||||
import { MemohService } from './api/memoh'
|
||||
|
||||
function ChatComponent() {
|
||||
const [message, setMessage] = useState('')
|
||||
const [response, setResponse] = useState('')
|
||||
const service = new MemohService()
|
||||
|
||||
const handleChat = async () => {
|
||||
setResponse('')
|
||||
await service.chat(message, (chunk) => {
|
||||
setResponse(prev => prev + chunk)
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<input value={message} onChange={e => setMessage(e.target.value)} />
|
||||
<button onClick={handleChat}>发送</button>
|
||||
<div>{response}</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## 测试示例
|
||||
|
||||
```typescript
|
||||
import { describe, test, expect, beforeAll } from 'bun:test'
|
||||
import { login, logout, chat, createModel } from '@memoh/cli'
|
||||
|
||||
describe('Memoh Core API', () => {
|
||||
beforeAll(async () => {
|
||||
// 测试前登录
|
||||
await login({ username: 'test', password: 'test' })
|
||||
})
|
||||
|
||||
test('chat should return response', async () => {
|
||||
const response = await chat({ message: 'Hello' })
|
||||
expect(response).toBeDefined()
|
||||
expect(typeof response).toBe('string')
|
||||
})
|
||||
|
||||
test('createModel should work', async () => {
|
||||
const model = await createModel({
|
||||
name: 'Test Model',
|
||||
modelId: 'test-model',
|
||||
baseUrl: 'https://api.test.com',
|
||||
apiKey: 'test-key',
|
||||
clientType: 'openai',
|
||||
type: 'chat'
|
||||
})
|
||||
expect(model.id).toBeDefined()
|
||||
expect(model.name).toBe('Test Model')
|
||||
})
|
||||
|
||||
test('should throw error when not logged in', async () => {
|
||||
logout()
|
||||
await expect(chat({ message: 'Hello' })).rejects.toThrow()
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
@@ -1,338 +1,2 @@
|
||||
# @memoh/cli
|
||||
|
||||
Memoh 的命令行工具,使用 Elysia Eden 与 API 服务器通信。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- 🔐 **用户认证** - 登录、登出、查看当前用户
|
||||
- 👥 **用户管理** - 完整的用户 CRUD 操作(管理员)
|
||||
- 🤖 **模型管理** - AI 模型配置管理
|
||||
- 💬 **Agent 对话** - 与 AI Agent 进行对话,支持流式响应
|
||||
- 🧠 **记忆管理** - 搜索、添加、查看对话记忆
|
||||
- ⚙️ **设置管理** - 个性化配置
|
||||
- 📅 **日程管理** - 定时任务管理
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
# 在项目根目录
|
||||
pnpm install
|
||||
|
||||
# 进入 CLI 目录
|
||||
cd packages/cli
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 配置 API 地址(可选)
|
||||
|
||||
默认连接到 `http://localhost:7002`,如需修改:
|
||||
|
||||
```bash
|
||||
bun run src/index.ts auth config --set http://your-api-url:port
|
||||
```
|
||||
|
||||
### 2. 登录
|
||||
|
||||
```bash
|
||||
bun run src/index.ts auth login
|
||||
# 或直接提供用户名和密码
|
||||
bun run src/index.ts auth login -u admin -p password
|
||||
```
|
||||
|
||||
### 3. 开始使用
|
||||
|
||||
```bash
|
||||
# 查看帮助
|
||||
bun run src/index.ts --help
|
||||
|
||||
# 与 Agent 对话
|
||||
bun run src/index.ts agent chat "你好"
|
||||
|
||||
# 进入交互模式
|
||||
bun run src/index.ts agent interactive
|
||||
```
|
||||
|
||||
## 命令参考
|
||||
|
||||
### 认证命令 (`auth`)
|
||||
|
||||
```bash
|
||||
# 登录
|
||||
memoh auth login [-u username] [-p password]
|
||||
|
||||
# 登出
|
||||
memoh auth logout
|
||||
|
||||
# 查看当前登录用户
|
||||
memoh auth whoami
|
||||
|
||||
# 查看/设置 API 配置
|
||||
memoh auth config [--set <url>]
|
||||
```
|
||||
|
||||
### 用户管理 (`user`) 🔒 需要管理员权限
|
||||
|
||||
```bash
|
||||
# 列出所有用户
|
||||
memoh user list
|
||||
|
||||
# 创建用户
|
||||
memoh user create [-u username] [-p password] [-r role]
|
||||
|
||||
# 获取用户详情
|
||||
memoh user get <id>
|
||||
|
||||
# 删除用户
|
||||
memoh user delete <id>
|
||||
|
||||
# 更新用户密码
|
||||
memoh user update-password <id> [-p password]
|
||||
```
|
||||
|
||||
### 模型管理 (`model`)
|
||||
|
||||
```bash
|
||||
# 列出所有模型
|
||||
memoh model list
|
||||
|
||||
# 创建聊天模型配置
|
||||
memoh model create \
|
||||
-n "GPT-4" \
|
||||
-m "gpt-4" \
|
||||
-u "https://api.openai.com/v1" \
|
||||
-k "sk-xxx" \
|
||||
-c "openai" \
|
||||
-t "chat"
|
||||
|
||||
# 创建 Embedding 模型配置
|
||||
memoh model create \
|
||||
-n "Text Embedding 3 Small" \
|
||||
-m "text-embedding-3-small" \
|
||||
-u "https://api.openai.com/v1" \
|
||||
-k "sk-xxx" \
|
||||
-c "openai" \
|
||||
-t "embedding" \
|
||||
-d 1536
|
||||
|
||||
# 获取模型详情
|
||||
memoh model get <id>
|
||||
|
||||
# 删除模型
|
||||
memoh model delete <id>
|
||||
|
||||
# 查看默认模型配置
|
||||
memoh model defaults
|
||||
```
|
||||
|
||||
### Agent 对话 (`agent`)
|
||||
|
||||
```bash
|
||||
# 发送单条消息
|
||||
memoh agent chat "你好,介绍一下你自己" \
|
||||
[-t 60] \
|
||||
[-l Chinese]
|
||||
|
||||
# 进入交互模式
|
||||
memoh agent interactive
|
||||
memoh agent i # 简写
|
||||
|
||||
# 交互模式命令:
|
||||
# /exit, /quit - 退出
|
||||
# /help - 帮助
|
||||
```
|
||||
|
||||
### 记忆管理 (`memory`)
|
||||
|
||||
```bash
|
||||
# 搜索记忆
|
||||
memoh memory search "关键词" [-l 10]
|
||||
|
||||
# 添加记忆
|
||||
memoh memory add "这是一条记忆"
|
||||
|
||||
# 查看消息历史
|
||||
memoh memory messages [-p 1] [-l 20]
|
||||
memoh memory msg # 简写
|
||||
|
||||
# 按日期过滤消息
|
||||
memoh memory filter \
|
||||
-s 2024-01-01T00:00:00Z \
|
||||
-e 2024-12-31T23:59:59Z
|
||||
```
|
||||
|
||||
### 设置管理 (`settings`)
|
||||
|
||||
```bash
|
||||
# 查看当前设置
|
||||
memoh settings get
|
||||
|
||||
# 更新设置
|
||||
memoh settings set \
|
||||
[--language Chinese] \
|
||||
[--max-context-time 60] \
|
||||
[--chat-model <id>] \
|
||||
[--summary-model <id>] \
|
||||
[--embedding-model <id>]
|
||||
|
||||
# 交互式设置向导
|
||||
memoh settings setup
|
||||
```
|
||||
|
||||
### 日程管理 (`schedule`)
|
||||
|
||||
```bash
|
||||
# 列出所有定时任务
|
||||
memoh schedule list
|
||||
|
||||
# 创建定时任务
|
||||
memoh schedule create \
|
||||
-t "每日提醒" \
|
||||
-d "每天早上9点的提醒" \
|
||||
-c "0 9 * * *" \
|
||||
-e
|
||||
|
||||
# 获取任务详情
|
||||
memoh schedule get <id>
|
||||
|
||||
# 更新任务
|
||||
memoh schedule update <id> \
|
||||
[-t title] \
|
||||
[-d description] \
|
||||
[-c cron] \
|
||||
[-e true/false]
|
||||
|
||||
# 删除任务
|
||||
memoh schedule delete <id>
|
||||
|
||||
# 切换任务启用状态
|
||||
memoh schedule toggle <id>
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 完整工作流程
|
||||
|
||||
```bash
|
||||
# 1. 登录
|
||||
memoh auth login -u admin -p password
|
||||
|
||||
# 2. 创建模型配置(聊天模型)
|
||||
memoh model create \
|
||||
-n "GPT-4" \
|
||||
-m "gpt-4" \
|
||||
-u "https://api.openai.com/v1" \
|
||||
-k "your-api-key" \
|
||||
-c "openai" \
|
||||
-t "chat"
|
||||
|
||||
# 如果需要 embedding 模型
|
||||
memoh model create \
|
||||
-n "Text Embedding" \
|
||||
-m "text-embedding-3-small" \
|
||||
-u "https://api.openai.com/v1" \
|
||||
-k "your-api-key" \
|
||||
-c "openai" \
|
||||
-t "embedding" \
|
||||
-d 1536
|
||||
|
||||
# 3. 配置设置(使用模型ID)
|
||||
memoh settings set \
|
||||
--language Chinese \
|
||||
--max-context-time 60 \
|
||||
--chat-model <model-id-from-step-2>
|
||||
|
||||
# 4. 开始对话
|
||||
memoh agent chat "你好"
|
||||
|
||||
# 5. 进入交互模式
|
||||
memoh agent i
|
||||
```
|
||||
|
||||
### Agent 交互模式示例
|
||||
|
||||
```bash
|
||||
$ memoh agent interactive
|
||||
|
||||
🤖 Memoh Agent 交互模式
|
||||
输入 /exit 或 /quit 退出,输入 /help 查看帮助
|
||||
|
||||
You: 你好
|
||||
Agent: 你好!我是 Memoh AI 助手,很高兴为你服务...
|
||||
|
||||
You: 帮我总结一下今天的对话
|
||||
Agent: [🔧 使用工具: search_memory]
|
||||
根据我们的对话记录...
|
||||
|
||||
You: /exit
|
||||
再见!👋
|
||||
```
|
||||
|
||||
### 搜索记忆示例
|
||||
|
||||
```bash
|
||||
$ memoh memory search "项目计划"
|
||||
|
||||
✓ 找到 3 条记忆
|
||||
|
||||
[1] 相似度: 92.50%
|
||||
时间: 2024-01-15 10:30:00
|
||||
讨论了项目的初步计划和时间线...
|
||||
|
||||
[2] 相似度: 85.20%
|
||||
时间: 2024-01-14 15:20:00
|
||||
确定了项目的主要里程碑...
|
||||
|
||||
[3] 相似度: 78.90%
|
||||
时间: 2024-01-13 09:00:00
|
||||
项目启动会议记录...
|
||||
```
|
||||
|
||||
## 配置文件
|
||||
|
||||
CLI 配置保存在 `~/.memoh/config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"apiUrl": "http://localhost:7002",
|
||||
"token": "your_jwt_token"
|
||||
}
|
||||
```
|
||||
|
||||
## 开发
|
||||
|
||||
```bash
|
||||
# 开发模式(带热重载)
|
||||
pnpm run dev
|
||||
|
||||
# 直接运行
|
||||
pnpm run start
|
||||
```
|
||||
|
||||
## 技术栈
|
||||
|
||||
- **Bun** - JavaScript 运行时
|
||||
- **Elysia Eden** - 类型安全的 HTTP 客户端
|
||||
- **Commander** - 命令行参数解析
|
||||
- **Chalk** - 终端颜色输出
|
||||
- **Inquirer** - 交互式提示
|
||||
- **Ora** - 加载动画
|
||||
- **Table** - 表格输出
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **认证要求**: 大部分命令需要先登录
|
||||
2. **管理员权限**: 用户管理命令需要管理员角色
|
||||
3. **模型配置**: 使用 Agent 前需要配置模型
|
||||
4. **流式响应**: Agent 对话使用 SSE 流式传输
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [API 文档](../api/README.md)
|
||||
- [认证系统](../api/AUTH_README.md)
|
||||
- [Agent API](../api/AGENT_API.md)
|
||||
- [用户管理](../api/USER_MANAGEMENT.md)
|
||||
|
||||
## 许可证
|
||||
|
||||
MIT
|
||||
|
||||
Reference in New Issue
Block a user