Coze源码分析-资源库-删除插件-前端源码-核心接口与工具
API层设计与实现
IDL基础类型定义(base.thrift)
文件位置:idl/base.thrift
核心代码:
namespace py base
namespace go base
namespace java com.bytedance.thrift.basestruct TrafficEnv {1: bool Open = false,2: string Env = "" ,
}struct Base {1: string LogID = "",2: string Caller = "",3: string Addr = "",4: string Client = "",5: optional TrafficEnv TrafficEnv ,6: optional map<string,string> Extra ,
}struct BaseResp {1: string StatusMessage = "",2: i32 StatusCode = 0 ,3: optional map<string,string> Extra ,
}struct EmptyReq {
}struct EmptyData {}struct EmptyResp {1: i64 code,2: string msg ,3: EmptyData data,
}struct EmptyRpcReq {255: optional Base Base,
}struct EmptyRpcResp {255: optional BaseResp BaseResp,
}
文件作用:
定义了项目中所有接口的基础数据结构,作为其他IDL文件的依赖基础。
删除插件-IDL结构体定义(plugin_develop.thrift)
文件路径:idl\plugin\plugin_develop.thrift
核心代码:
namespace go plugin
include "../base.thrift"// 删除插件请求结构
struct DelPluginRequest {1: optional string plugin_id255: optional base.Base Base
}// 删除插件响应结构
struct DelPluginResponse {1: optional i64 code2: optional string msg255: optional base.BaseResp BaseResp
}// 插件资源信息结构
struct ResourceInfo {1: optional string res_id (api.body="res_id")2: optional string name (api.body="name")3: optional ResType res_type (api.body="res_type")4: optional list<ActionInfo> actions (api.body="actions")5: optional string creator_id (api.body="creator_id")6: optional i64 create_time (api.body="create_time")7: optional i64 update_time (api.body="update_time")
}// 操作信息结构
struct ActionInfo {1: required ActionKey key (api.body="key")2: required bool enable (api.body="enable")3: optional string reason (api.body="reason")
}// 资源类型枚举
enum ResType {Plugin = 1,Prompt = 2,Workflow = 3,Knowledge = 4
}// 操作类型枚举
enum ActionKey {Edit = 1,Delete = 2,Copy = 3,Publish = 4
}
设计亮点:
- 简洁的请求参数:只需要插件ID即可执行删除操作
- JavaScript兼容:通过
api.js_conv = "str"
确保前端字符串类型兼容 - 统一的响应格式:包含状态码和消息的标准响应结构
- 权限控制:通过
ActionInfo
结构控制操作权限 - 资源类型:支持多种资源类型的统一管理
- 操作枚举:标准化的操作类型定义
删除插件-IDL接口定义(plugin_develop.thrift)
文件路径:idl\plugin\plugin_develop.thrift
核心代码:
include "../base.thrift"namespace go pluginservice PluginDevelopService {// 删除插件DelPluginResponse DelPlugin(1: DelPluginRequest request)(api.post='/api/plugin_api/del_plugin', api.category="plugin", agw.preserve_base="true")
}
接口设计说明:
- DelPlugin:执行插件删除操作的核心接口
- RESTful设计:遵循REST API设计规范
- 服务分类:明确的插件开发服务分类
- 基础结构保留:通过
agw.preserve_base="true"
保留基础请求结构
删除插件-API接口实现(plugin_develop/index.ts)
文件位置:frontend/packages/arch/idl/src/auto-generated/plugin_develop/index.ts
核心代码:
export default class PluginDevelopService<T> {private request: any = () => {throw new Error('PluginDevelopService.request is undefined');};private baseURL: string | ((path: string) => string) = '';constructor(options?: {baseURL?: string | ((path: string) => string);request?<R>(params: {url: string;method: 'GET' | 'DELETE' | 'POST' | 'PUT' | 'PATCH';data?: any;params?: any;headers?: any;},options?: T,): Promise<R>;}) {this.request = options?.request || this.request;this.baseURL = options?.baseURL || '';}/** POST /api/plugin_api/del_plugin */DelPlugin(req?: DelPluginRequest,options?: T,): Promise<DelPluginResponse> {const _req = req || {};const url = this.genBaseURL('/api/plugin_api/del_plugin');const method = 'POST';const data = { plugin_id: _req['plugin_id'], Base: _req['Base'] };return this.request({ url, method, data }, options);}// ... 其他API方法
}
代码作用:
- DelPlugin:执行插件删除操作的核心方法
- 类型安全:基于 plugin_develop.thrift 自动生成,确保类型安全
- 简洁参数:只需要插件ID即可执行删除操作
- 统一接口:与其他插件开发API保持一致的调用方式
删除插件-结构体实现(plugin_develop.ts)
文件路径:frontend\packages\arch\idl\src\auto-generated\plugin_develop\index.ts
// 删除插件请求接口
export interface DelPluginRequest {plugin_id?: string;Base?: base.Base;
}// 删除插件响应接口
export interface DelPluginResponse {code?: Int64;msg?: string;BaseResp?: base.BaseResp;
}// 资源信息接口
export interface ResourceInfo {res_id?: string;name?: string;res_type?: ResType;actions?: ActionInfo[];creator_id?: string;create_time?: Int64;update_time?: Int64;
}// 操作信息接口
export interface ActionInfo {key: ActionKey;enable: boolean;reason?: string;
}// 资源类型枚举
export enum ResType {Plugin = 1,Prompt = 2,Workflow = 3,Knowledge = 4
}// 操作类型枚举
export enum ActionKey {Edit = 1,Delete = 2,Copy = 3,Publish = 4
}
接口设计亮点:
- 类型安全:完整的 TypeScript 类型定义
- 简洁设计:删除操作只需要插件ID
- 权限控制:通过 ActionInfo 实现细粒度权限控制
- 统一响应:标准的错误码和消息格式
- 枚举类型:类型安全的资源类型和操作类型定义
删除插件-前端调用示例
基于实际的 usePluginConfig
hook 实现:
// 在 usePluginConfig hook 中的删除逻辑
const { run: delPlugin } = useRequest((pluginId: string) =>PluginDevelopApi.DelPlugin({plugin_id: pluginId,}),{manual: true,onSuccess: () => {reloadList(); // 删除成功后刷新列表Toast.success(I18n.t('Delete_success')); // 显示成功提示},onError: (error) => {Toast.error(I18n.t('Delete_failed')); // 显示失败提示console.error('删除插件失败:', error);},},
);// 在 TableAction 组件中的使用
<TableActiondeleteProps={{disabled: !libraryResource.actions?.find(action => action.key === ActionKey.Delete,)?.enable,deleteDesc: I18n.t('plugin_resource_delete_describ'),handler: () => {delPlugin(libraryResource.res_id || '');},}}
/>
实际调用流程:
- 权限检查:通过
libraryResource.actions
检查删除权限 - 用户确认:TableAction 组件内置确认弹窗
- 执行删除:调用
delPlugin
函数 - API请求:使用
PluginDevelopApi.DelPlugin
发送删除请求 - 结果处理:成功后刷新列表并显示提示,失败时显示错误信息
工具概述
删除插件功能的实现依赖于Coze Studio完整的前端开发工具链,这些工具确保了从IDL定义到前端组件的无缝集成。
核心开发工具
1. IDL到TypeScript转换工具
工具名称:@coze-arch/idl2ts-cli
项目路径:frontend/infra/idl/idl2ts-cli/
在删除插件功能中的作用:
- 将
plugin_develop.thrift
中的DelPluginRequest
和DelPluginResponse
结构转换为TypeScript类型 - 生成
PluginDevelopApi.DelPlugin
接口的类型定义 - 确保前端调用删除API时的类型安全
// 生成的删除插件API类型定义
export interface DelPluginRequest {plugin_id: string;Base?: Base;
}export interface DelPluginResponse {code: Int64;msg: string;BaseResp?: BaseResp;
}
2. API客户端生成工具
工具名称:@coze-arch/bot-api
功能描述:基于IDL文件自动生成API客户端代码
在删除插件功能中的应用:
// 自动生成的删除插件API调用方法
import { PluginDevelopApi } from '@coze-arch/bot-api';// 在usePluginConfig中使用
const { run: delPlugin } = useRequest((pluginId: string) =>PluginDevelopApi.DelPlugin({plugin_id: pluginId,}),{manual: true,onSuccess: () => {reloadList();Toast.success(I18n.t('Delete_success'));},onError: (error) => {Toast.error(error.message || I18n.t('Delete_failed'));},},
);
3. 组件设计系统工具
工具名称:@coze-arch/coze-design
功能描述:提供统一的UI组件库
删除插件功能相关组件:
Table.TableAction
:表格操作菜单组件Modal.confirm
:删除确认弹窗Toast
:操作结果提示Popconfirm
:删除确认气泡
// TableAction组件在删除插件中的使用
<TableActiondeleteProps={{disabled: !libraryResource.actions?.find(action => action.key === ActionKey.Delete,)?.enable,popconfirm: {title: I18n.t('delete_title'),content: I18n.t('plugin_delete_confirm_desc'),okType: 'danger',},handler: () => delPlugin(libraryResource.res_id || ''),}}
/>
4. 状态管理工具
工具名称:ahooks
功能描述:React Hooks工具库
在删除插件功能中的应用:
useRequest
:管理删除API的异步状态- 提供loading、error、success状态管理
- 支持手动触发和自动重试机制
5. 国际化工具
工具名称:@coze-arch/i18n
功能描述:多语言支持工具
删除插件相关的国际化配置:
// 删除插件相关的多语言文案
const deleteMessages = {'delete_title': '删除插件','plugin_delete_confirm_desc': '确认删除该插件?删除后无法恢复。','Delete_success': '删除成功','Delete_failed': '删除失败','confirm': '确认','cancel': '取消'
};
工具链集成优势
- 类型安全:从IDL到前端的完整类型链路保证
- 代码生成:减少手动编写API调用代码的工作量
- 组件复用:统一的删除操作组件提高开发效率
- 状态管理:标准化的异步操作状态处理
- 用户体验:一致的交互模式和视觉反馈
这些工具共同构成了删除插件功能的技术基础,确保了代码质量、开发效率和用户体验的统一性。