Files
IC-Coder-Plugin/src/types/api.ts
Roe-xin 8751944053 feat: 添加个人规则功能
- 新增个人规则管理模块 (personalRulesManager.ts)
   - 支持创建、编辑、删除多条规则
   - 规则存储在用户目录 ~/.iccoder/rules/
   - 对话时自动将规则传递给后端
   - 添加后端对接文档和 webpack 优化指南
2026-03-07 15:13:54 +08:00

646 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 后端 API 类型定义
* 对应后端 IC Coder Backend 的接口格式
*/
import { CompactedMemory, CompactedMessage } from "./memory";
// ============== 对话请求/响应 ==============
/**
* 运行模式类型
* - plan: 只读模式,只能查询分析
* - ask: 逐个确认,每个写操作需确认
* - agent: 智能体自主(默认)
* - auto: 完全自动
*/
export type RunMode = "plan" | "ask" | "agent" | "auto";
/**
* 服务等级类型
* - lite: 轻量级
* - syntaxic: 语法级
* - max: 最大性能
* - auto: 自动选择
*/
export type ServiceTier = "lite" | "syntaxic" | "max" | "auto";
/**
* 对话请求
* POST /api/dialog/stream
*/
export interface DialogRequest {
/** 任务ID用于记忆隔离 */
taskId: string;
/** 用户消息 */
message: string;
/** 用户ID */
userId: string;
/** 运行模式 */
mode: RunMode;
/** 服务等级 */
serviceTier?: ServiceTier;
/** JWT Token用于认证和扣费 */
token?: string;
/** 压缩后的记忆数据(用于后端重启后恢复) */
compactedData?: CompactedMemory;
/** 压缩后产生的新消息 */
newMessages?: CompactedMessage[];
/** 知识图谱数据JSON 字符串,用于恢复知识图谱) */
knowledgeData?: string;
/** 个人规则 */
personalRules?: string;
}
// ============== SSE 事件类型 ==============
/** SSE 事件类型枚举 */
export type SSEEventType =
| "text_delta" // 文本增量
| "tool_call" // 客户端工具调用请求
| "tool_confirm" // 工具确认请求Ask 模式)
| "plan_confirm" // 计划确认请求Plan 模式)
| "phase_progress" // 阶段进度更新
| "plan_step_add" // 添加计划步骤
| "plan_step_remove" // 删除计划步骤
| "plan_step_update" // 更新计划步骤
| "plan_summary_update" // 更新计划摘要
| "tool_start" // 工具开始执行
| "tool_complete" // 工具执行完成
| "tool_error" // 工具执行错误
| "ask_user" // 向用户提问
| "agent_start" // 子智能体启动
| "agent_progress" // 子智能体进度
| "agent_complete" // 子智能体完成
| "agent_error" // 子智能体错误
| "memory_compacted" // 记忆压缩完成
| "context_usage" // 上下文使用量
| "credit_update" // 资源点余额更新
| "complete" // 对话完成
| "error" // 错误
| "warning" // 警告
| "notification" // 通知
| "depth_update" // 深度更新
| "heartbeat"; // 心跳
/** text_delta 事件数据 */
export interface TextDeltaEvent {
text: string;
}
/** tool_start 事件数据 */
export interface ToolStartEvent {
tool_name: string;
tool_input: unknown;
}
/** tool_complete 事件数据 */
export interface ToolCompleteEvent {
tool_name: string;
result: string;
description?: string;
}
/** tool_error 事件数据 */
export interface ToolErrorEvent {
tool_name: string;
error: string;
}
/** tool_confirm 事件数据Ask 模式确认请求) */
export interface ToolConfirmEvent {
/** 确认ID用于匹配响应 */
confirmId: number;
/** 工具名称 */
toolName: string;
/** 工具输入参数 */
toolInput: Record<string, unknown>;
/** 时间戳 */
timestamp: number;
}
/** 计划步骤 */
export interface PlanStep {
/** 步骤名称 */
name: string;
/** 步骤描述 */
description?: string;
}
/** 计划阶段 */
export interface PlanPhase {
/** 阶段ID: spec/design/sim/done */
id: string;
/** 阶段名称 */
name: string;
/** 阶段状态: skipped/completed/current/pending */
status: string;
/** 跳过原因 */
reason?: string;
/** 阶段内的步骤 */
steps: PlanStep[];
}
/** plan_confirm 事件数据Plan 模式计划确认) */
export interface PlanConfirmEvent {
/** 确认ID */
confirmId: number;
/** 计划标题 */
title: string;
/** 四阶段计划列表(新格式) */
phases?: PlanPhase[];
/** 执行步骤列表(旧格式,兼容) */
steps?: string[];
/** 计划摘要 */
summary: string;
/** 时间戳 */
timestamp: number;
}
/** phase_progress 事件数据(阶段进度更新) */
export interface PhaseProgressEvent {
/** 阶段ID: spec/design/sim/done */
phaseId: string;
/** 状态: current/completed */
status: string;
/** 时间戳 */
timestamp: number;
}
/** plan_step_add 事件数据(添加计划步骤) */
export interface PlanStepAddEvent {
phaseId: string;
step: PlanStep;
index: number;
timestamp: number;
}
/** plan_step_remove 事件数据(删除计划步骤) */
export interface PlanStepRemoveEvent {
phaseId: string;
stepIndex: number;
timestamp: number;
}
/** plan_step_update 事件数据(更新计划步骤) */
export interface PlanStepUpdateEvent {
phaseId: string;
stepIndex: number;
step: PlanStep;
timestamp: number;
}
/** plan_summary_update 事件数据(更新计划摘要) */
export interface PlanSummaryUpdateEvent {
summary: string;
timestamp: number;
}
/** 单个问题项 */
export interface QuestionItem {
question: string;
options: string[];
multiSelect?: boolean;
}
/** ask_user 事件数据 */
export interface AskUserEvent {
askId: string;
questions: QuestionItem[];
}
/** complete 事件数据 */
export interface CompleteEvent {
status: string;
finish_reason: string;
}
/** error 事件数据 */
export interface ErrorEvent {
message: string;
}
/** warning 事件数据 */
export interface WarningEvent {
message: string;
}
/** notification 事件数据 */
export interface NotificationEvent {
message: string;
}
/** depth_update 事件数据 */
export interface DepthUpdateEvent {
depth: number;
}
// ============== 智能体事件类型 ==============
/** agent_start 事件数据 */
export interface AgentStartEvent {
agentId: string;
agentType: string;
agentName: string;
instruction: string;
timestamp: number;
}
/** agent_progress 事件数据 */
export interface AgentProgressEvent {
agentId: string;
step: number;
toolName: string;
toolInput?: unknown;
toolResult?: string;
status: "running" | "completed" | "error";
timestamp: number;
}
/** agent_complete 事件数据 */
export interface AgentCompleteEvent {
agentId: string;
agentType: string;
summary: string;
stats: Record<string, unknown>;
timestamp: number;
}
/** agent_error 事件数据 */
export interface AgentErrorEvent {
agentId: string;
agentType: string;
error: string;
timestamp: number;
}
/** context_usage 事件数据 */
export interface ContextUsageEvent {
currentTokens: number;
maxTokens: number;
percentage: number;
}
/** credit_update 事件数据 */
export interface CreditUpdateEvent {
deductedCredits: number;
remainingCredits: number;
}
// ============== 工具调用协议 (MCP 格式) ==============
/**
* 工具调用请求MCP格式
* 后端通过 SSE tool_call 事件推送
*/
export interface ToolCallRequest {
/** JSON-RPC版本固定为"2.0" */
jsonrpc: "2.0";
/** 请求ID用于匹配响应 */
id: number;
/** 方法名,固定为"tools/call" */
method: "tools/call";
/** 调用参数 */
params: {
/** 工具名称 */
name: string;
/** 工具参数 */
arguments: Record<string, unknown>;
};
}
/**
* 工具执行结果MCP格式
* POST /api/tool/result
*/
export interface ToolCallResult {
/** JSON-RPC版本 */
jsonrpc: "2.0";
/** 请求ID与ToolCallRequest.id对应 */
id: number;
/** 执行结果与error互斥 */
result?: ToolResultContent;
/** 错误信息与result互斥 */
error?: ToolResultError;
}
/** 工具执行结果内容 */
export interface ToolResultContent {
/** 内容列表 */
content: ContentItem[];
/** 是否为错误结果(业务错误,如编译失败) */
isError: boolean;
}
/** 内容项 */
export interface ContentItem {
/** 内容类型text, image, resource */
type: string;
/** 文本内容 */
text: string;
}
/** 工具系统错误 */
export interface ToolResultError {
/** 错误码 */
code: number;
/** 错误消息 */
message: string;
}
// ============== 用户回答 ==============
/**
* 用户回答请求
* POST /api/task/answer
*/
export interface AnswerRequest {
/** 问题ID */
askId: string;
/** 任务ID */
taskId: string;
/** 选中的选项列表(旧格式,兼容) */
selected?: string[];
/** 自定义输入内容(旧格式,兼容) */
customInput?: string;
/** 新格式:按问题索引的答案 */
answers?: { [questionIndex: string]: string[] };
}
/** 用户回答响应 */
export interface AnswerResponse {
success: boolean;
message?: string;
error?: string;
}
// ============== 工具结果响应 ==============
/** 工具结果响应 */
export interface ToolResultResponse {
success: boolean;
message?: string;
error?: string;
}
// ============== 工具确认响应 ==============
/**
* 工具确认响应请求
* POST /api/tool/confirm
*/
export interface ToolConfirmResponse {
/** 确认ID与 ToolConfirmEvent.confirmId 对应 */
confirmId: number;
/** 任务ID */
taskId: string;
/** 是否批准执行 */
approved: boolean;
}
// ============== 用户信息 ==============
/**
* 用户信息响应
* GET /system/user/getInfo
*/
export interface UserInfoResponse {
/** 响应消息 */
msg: string;
/** 响应代码 (200 表示成功) */
code: number;
/** 权限列表 */
permissions: string[];
/** 角色列表 */
roles: string[];
/** 是否默认修改密码 */
isDefaultModifyPwd: boolean;
/** 密码是否过期 */
isPasswordExpired: boolean;
/** 是否为插件试用用户 */
isPluginTrial?: boolean;
/** 企业试用到期时间(毫秒时间戳) */
enterpriseTrialExpires?: number;
/** 用户信息 */
user: {
userId: number;
userName: string;
nickName: string;
email?: string;
phonenumber?: string;
sex?: string;
avatar?: string;
status?: string;
createTime?: string;
loginDate?: string;
remark?: string;
[key: string]: any;
};
}
// ============== 会员信息 ==============
/**
* 会员单条记录
*/
export interface MembershipItemVO {
membershipId: number | null;
tierCode: string;
tierName: string;
tierLevel: number;
expireTime: string | null;
remainingDays: number;
permanent: boolean;
nextGrantTime: string | null;
lastGrantTime: string | null;
grantCycle: number;
totalGranted: number;
monthlyCredits: number;
teamSeat: boolean;
}
/**
* 用户会员信息
*/
export interface UserMembershipVO {
userId: number;
tierCode: string;
tierName: string;
tierLevel: number;
allowedModelCombinations: string[];
description?: string;
createdTime?: string;
updatedTime?: string;
}
/**
* 多会员信息响应
*/
export interface MultiMembershipVO extends UserMembershipVO {
displayTier?: MembershipItemVO;
allMemberships?: MembershipItemVO[];
totalMonthlyCredits?: number;
}
/**
* 会员信息响应
* GET /strangeloop/api/membership/current
*/
export interface MembershipResponse {
code: number;
msg?: string;
message?: string;
data?: MultiMembershipVO;
}
// ============== 辅助类型 ==============
/** 后端工具名称 */
export type ToolName =
| "file_read"
| "file_write"
| "file_delete"
| "file_list"
| "syntax_check"
| "iverilog"
| "simulation"
| "waveform_summary"
| "waveform_trace"
| "knowledge_save"
| "knowledge_load";
/** file_read 工具参数 */
export interface FileReadArgs {
path: string;
}
/** file_write 工具参数 */
export interface FileWriteArgs {
path: string;
content: string;
}
/** file_delete 工具参数 */
export interface FileDeleteArgs {
/** 要删除的文件路径 */
path: string;
}
/** file_list 工具参数 */
export interface FileListArgs {
path?: string;
extension?: string;
}
/** syntax_check 工具参数 */
export interface SyntaxCheckArgs {
code: string;
}
/** iverilog 工具参数 */
export interface IverilogArgs {
args: string;
workDir?: string;
}
/** simulation 工具参数 */
export interface SimulationArgs {
rtlPath: string;
tbPath: string;
duration?: string;
/** 要dump的模块列表格式name:path,name:path */
dumpModules?: string;
/** VCD输出目录默认'vcd' */
vcdDir?: string;
}
/** waveform_summary 工具参数 */
export interface WaveformSummaryArgs {
vcdPath: string;
signals: string;
checkpoints?: string;
}
/** waveform_trace 工具参数 */
export interface WaveformTraceArgs {
/** Verilog 源文件路径(相对于项目根目录) */
verilogPath: string;
/** VCD 波形文件路径(相对于项目根目录) */
vcdPath: string;
/** 仿真工具的输出字符串(包含 mismatch 信息) */
simOutput: string;
/** BFS 回溯层数,默认 2 */
traceLevel?: number;
}
/** knowledge_save 工具参数 */
export interface KnowledgeSaveArgs {
/** 知识图谱 JSON 数据 */
data: string;
}
/** knowledge_load 工具参数 */
export interface KnowledgeLoadArgs {
// 无参数,直接读取 .iccoder/knowledge.json
}
/** 工具参数联合类型 */
export type ToolArgs =
| FileReadArgs
| FileWriteArgs
| FileDeleteArgs
| FileListArgs
| SyntaxCheckArgs
| IverilogArgs
| SimulationArgs
| WaveformSummaryArgs
| WaveformTraceArgs
| KnowledgeSaveArgs
| KnowledgeLoadArgs;
// ============== 邀请码验证 ==============
/**
* 邀请码验证请求
* POST /api/invitation/verify
*/
export interface InvitationVerifyRequest {
/** 邀请码 */
code: string;
}
/**
* 邀请码验证响应
*/
export interface InvitationVerifyResponse {
/** 响应代码 */
code: number;
/** 响应消息 */
msg: string;
/** 验证结果数据 */
data?: {
/** 是否验证成功 */
verified: boolean;
};
}
/**
* 邀请码状态响应
* GET /api/invitation/status
*/
export interface InvitationStatusResponse {
/** 响应代码 */
code: number;
/** 响应消息 */
msg?: string;
/** 状态数据 */
data?: {
/** 是否已验证 */
verified: boolean;
/** 使用的邀请码 */
invitationCode?: string;
/** 验证时间 */
verifiedTime?: string;
};
}