@cicctencent/agent-midway
v0.1.26
Published
MidwayJS AI Workstation Service layer — MidwayJS services for Agent/Chat/Profile/MCP/Model/Skill management with SPI extensibility
Readme
@cicctencent/agent-midway
MidwayJS AI 集成层组件 — 提供 MidwayJS Controller / Service / DTO 封装,通过 SPI Store 接口对接业务层 DB 实现。
定位
@cicctencent/agent-midway 是 MidwayJS Integration Layer,不是通用 AI framework 或 DB schema 提供者。
- Controller / Service / DTO:MidwayJS 专属的 HTTP 接入层封装
- SPI Store 接口:定义数据存储抽象,由业务层实现 TypeORM Entity / Repository
- 不定义 DB Model:TypeORM Entity 和表结构由业务应用(如 work 项目)自行定义
- 不重复实现 agent-core / agent-server:底层能力由
@cicctencent/agent-core和@cicctencent/agent-server提供
快速接入
1. 安装依赖
pnpm add @cicctencent/agent-midway2. 在业务项目中定义 DB Entity
业务项目需要在自己的 src/model/ 目录下定义 TypeORM Entity,例如:
src/model/
├── ai-agent-profile.entity.ts
├── ai-application.entity.ts
├── ai-chat-workspace.entity.ts
├── ai-chat-thread.entity.ts
├── ai-chat-message.entity.ts
├── ai-chat-memory.entity.ts
├── ai-chat-skill.entity.ts
├── ai-mcp-server.entity.ts
├── ai-model-config.entity.ts
├── ai-knowledge-base.entity.ts
└── ai-kb-document.entity.ts3. 实现 SPI Store
业务项目需要为每个 SPI 接口提供 TypeORM 实现:
// src/store/agent-profile.store.ts
import { Provide } from '@midwayjs/core';
import { InjectDataSource } from '@midwayjs/typeorm';
import { DataSource, Repository } from 'typeorm';
import { SPI_BINDINGS, type AgentProfileStore } from '@cicctencent/agent-midway';
import AIAgentProfileEntity from '../model/ai-agent-profile.entity';
@Provide(SPI_BINDINGS.AgentProfileStore)
export class AgentProfileStoreImpl implements AgentProfileStore {
@InjectDataSource('default')
dataSource: DataSource;
// 实现 AgentProfileStore 的所有方法...
}4. 在 configuration.ts 中引入
import { Configuration } from '@midwayjs/core';
import * as orm from '@midwayjs/typeorm';
import * as baseMidwayjs from '@cicctencent/midwayjs-base';
import * as agentMidway from '@cicctencent/agent-midway';
@Configuration({
imports: [
baseMidwayjs,
orm,
agentMidway, // ← 一行接入全部 AI 控制器/服务
],
importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle {}5. 配置 TypeORM 实体扫描
// config/config.default.ts
export default {
typeorm: {
dataSource: {
default: {
// 业务项目自己的实体通过 glob 扫描加载
entities: ['**/model/**/*.entity{.ts,.js}'],
},
},
},
// AI Workstation 组件配置
aiMidway: {
enabled: true,
prefix: '/api',
features: {
agent: true,
chat: true,
// ... 按需开关
},
},
};SPI Store 接口
| 接口 | 绑定标识 | 说明 |
|------|---------|------|
| AgentProfileStore | SPI_BINDINGS.AgentProfileStore | Agent Profile 数据存储 |
| ApplicationStore | SPI_BINDINGS.ApplicationStore | 应用数据存储 |
| MCPServerStore | SPI_BINDINGS.MCPServerStore | MCP Server 数据存储 |
| ModelConfigStore | SPI_BINDINGS.ModelConfigStore | 模型配置数据存储 |
| ChatStore | SPI_BINDINGS.ChatStore | 聊天数据存储 |
| KnowledgeBaseStore | SPI_BINDINGS.KnowledgeBaseStore | 知识库数据存储 |
| AutomationStore | SPI_BINDINGS.AutomationStore | 自动化任务数据存储 |
| MemoryDBStore | SPI_BINDINGS.MemoryDBStore | Agent 记忆数据存储 |
功能模块
| 模块 | 路由前缀 | 说明 |
|------|---------|------|
| Agent 运行 | /api/agent | 流式/非流式运行、任务队列 |
| Agent 测试 | /api/agent/test-stream | 测试模式流式运行(禁用委派,不持久化) |
| 聊天管理 | /api/chat | 工作空间/线程/消息 CRUD |
| Agent Profile | /api/agent-profiles | Agent 配置管理 |
| Application | /api/applications | 多应用隔离 |
| MCP Server | /api/mcp-servers | MCP 服务器管理 |
| 模型配置 | /api/model-configs | 多模型供应商配置 |
| 设置 | /api/settings | 全局设置 |
| 安全审计 | /api/security | 审计日志、工具风险 |
| 自动化 | /api/automations | Cron 定时任务 |
| 连接器 | /api/connectors | 数据源适配器 |
| A2A 协议 | /.well-known/agent.json, /a2a | Agent 间互操作 |
| IM 渠道 | /api/channels | 飞书/钉钉/企微 |
| 可观测性 | /api/observability | 链路追踪、指标、仪表板 |
| 知识库 | /api/knowledge-bases | 向量搜索 |
Agent 测试端点
POST /api/agent/test-stream — Agent 配置页面调试专用
请求体:
{ "prompt": "测试消息", "profileId": 1 }特点:
- 禁用委派(
delegate_task工具不注册) - 不持久化消息(不创建线程)
- 使用独立引擎缓存,不污染正常对话
返回 SSE 流,事件类型:reasoning / content / tool-call / tool-result / done / error
按需开关
aiMidway: {
features: {
agent: true,
chat: true,
agentProfile: true,
application: true,
mcpServer: false,
modelConfig: false,
settings: false,
security: false,
automation: false,
connectors: false,
a2a: false,
channels: false,
observability: false,
knowledgeBase: false,
},
}