返回 AiToEarn
agent.types.ts
根目录 / project / aitoearn-web / src / store / agent / agent.types.ts
1 /**
2 * Agent Store - 类型定义
3 * 全局 AI Agent 任务管理的类型定义
4 */
5
6 import type { TFunction } from 'i18next'
7 import type { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'
8
9 // ============ 媒体相关类型 ============
10
11 /** 上传的媒体文件类型 */
12 export interface IUploadedMedia {
13 url: string
14 type: 'image' | 'video' | 'audio' | 'document'
15 /** 上传进度 (0-100) */
16 progress?: number
17 /** 原始文件(上传中时使用) */
18 file?: File
19 /** 文档名称(document 类型使用) */
20 name?: string
21 /** 缓存控制(可选) */
22 cache_control?: {
23 type: 'ephemeral'
24 }
25 }
26
27 /** Claude Prompt 内容项类型 */
28 export type PromptContentType = 'text' | 'image' | 'video' | 'audio' | 'document'
29
30 /** Claude Prompt 内容项 */
31 export interface IPromptContentItem {
32 type: PromptContentType
33 text?: string
34 source?: {
35 type: 'url' | 'base64'
36 url?: string
37 data?: string
38 media_type?: string
39 }
40 cache_control?: {
41 type: 'ephemeral'
42 }
43 }
44
45 /** 解析后的用户消息内容 */
46 export interface IParsedUserContent {
47 /** 纯文本内容 */
48 text: string
49 /** 媒体文件列表 */
50 medias: IUploadedMedia[]
51 /** 是否包含特殊格式 */
52 hasSpecialFormat: boolean
53 }
54
55 // ============ 消息相关类型 ============
56
57 /** 消息角色 */
58 export type MessageRole = 'user' | 'assistant' | 'system'
59
60 /** 消息状态 */
61 export type MessageStatus = 'pending' | 'streaming' | 'done' | 'error'
62
63 /** AI 消息步骤 - 用于展示 AI 回复的多个阶段 */
64 export interface IMessageStep {
65 /** 步骤ID */
66 id: string
67 /** 该步骤的文本内容 */
68 content: string
69 /** 该步骤关联的工作流步骤 */
70 workflowSteps?: IWorkflowStep[]
71 /** 该步骤关联的媒体(图片/视频),用于在步骤位置内渲染资源 */
72 medias?: IUploadedMedia[]
73 /** 是否为当前活跃步骤 */
74 isActive?: boolean
75 /** 时间戳 */
76 timestamp?: number
77 }
78
79 /** Action 卡片数据 */
80 export interface IActionCard {
81 /** Action 类型 */
82 type: ActionType
83 /** 平台 */
84 platform?: PlatformType
85 /** 账号ID */
86 accountId?: string
87 /** 标题 */
88 title?: string
89 /** 描述 */
90 description?: string
91 /** 媒体数据(用于发布) */
92 medias?: IMediaItem[]
93 /** 标签 */
94 tags?: string[]
95 /** 发布流程 ID(用于查询发布详情) */
96 flowId?: string
97 /** 是否为实时 SSE 消息(区分实时推送 vs 历史加载,用于自动发布倒计时) */
98 _isRealtime?: boolean
99 }
100
101 /** 发布详情数据(用于 PublishDetailCard) */
102 export interface IPublishFlowData {
103 /** 发布流程 ID */
104 flowId: string
105 /** 平台类型 */
106 platform?: PlatformType
107 /** 初始数据(来自 SSE result) */
108 initialData?: {
109 title?: string
110 description?: string
111 medias?: IMediaItem[]
112 }
113 }
114
115 /** 显示消息项 */
116 export interface IDisplayMessage {
117 id: string
118 role: MessageRole
119 content: string
120 medias?: IUploadedMedia[]
121 status?: MessageStatus
122 errorMessage?: string
123 createdAt?: number
124 /** AI 消息的步骤列表(仅 assistant 消息使用) */
125 steps?: IMessageStep[]
126 /** Action 卡片列表(用于显示可交互的 action) */
127 actions?: IActionCard[]
128 /** 发布流程数据列表(用于显示 PublishDetailCard) */
129 publishFlows?: IPublishFlowData[]
130 }
131
132 // ============ 任务结果相关类型 ============
133
134 /** 结果类型 */
135 export type ResultType = 'imageOnly' | 'videoOnly' | 'mediaOnly' | 'fullContent'
136
137 /** 操作类型 */
138 export type ActionType
139 = | 'navigateToPublish'
140 | 'navigateToDraft'
141 | 'saveDraft'
142 | 'updateChannel'
143 | 'loginChannel'
144 | 'createChannel'
145 | 'platformNotSupported'
146 | 'errorOnly'
147 | 'insufficientCredits'
148
149 /** 平台类型 */
150 export type PlatformType
151 = | 'douyin'
152 | 'xhs'
153 | 'wxSph'
154 | 'KWAI'
155 | 'youtube'
156 | 'wxGzh'
157 | 'bilibili'
158 | 'twitter'
159 | 'tiktok'
160 | 'facebook'
161 | 'instagram'
162 | 'threads'
163 | 'pinterest'
164 | 'linkedin'
165
166 /** 媒体项 */
167 export interface IMediaItem {
168 type: 'video' | 'image' | 'VIDEO' | 'IMAGE'
169 url: string
170 coverUrl?: string
171 thumbUrl?: string
172 }
173
174 /** 任务数据(从 SSE 返回) */
175 export interface ITaskData {
176 taskId?: string
177 type?: ResultType
178 action?: ActionType
179 platform?: PlatformType
180 accountId?: string
181 title?: string
182 description?: string
183 tags?: string[]
184 medias?: IMediaItem[]
185 errorMessage?: string
186 /** 发布流程 ID(用于查询发布详情) */
187 flowId?: string
188 }
189
190 // ============ 工作流相关类型 ============
191
192 /** 工作流步骤类型 */
193 export type WorkflowStepType = 'thinking' | 'tool_call' | 'tool_result'
194
195 /** 工作流步骤 */
196 export interface IWorkflowStep {
197 /** 唯一标识 */
198 id: string
199 /** 步骤类型 */
200 type: WorkflowStepType
201 /** 工具名称(tool_call/tool_result 时使用) */
202 toolName?: string
203 /** 内容/参数(工具输入) */
204 content?: string
205 /** 工具执行结果 */
206 result?: string
207 /** 是否正在执行 */
208 isActive?: boolean
209 /** 时间戳 */
210 timestamp: number
211 }
212
213 // ============ Store 状态类型 ============
214
215 /** 待处理的任务(用于首页跳转后在聊天页创建) */
216 export interface IPendingTask {
217 prompt: string
218 medias: IUploadedMedia[]
219 }
220
221 /** 单个任务的消息数据(按任务ID隔离存储) */
222 export interface ITaskMessageData {
223 /** 显示的消息列表 */
224 messages: IDisplayMessage[]
225 /** Markdown 消息列表(用于显示对话历史) */
226 markdownMessages: string[]
227 /** 工作流步骤列表(当前轮次的所有步骤) */
228 workflowSteps: IWorkflowStep[]
229 /** 流式文本(正在生成的内容) */
230 streamingText: string
231 /** 进度百分比 0-100 */
232 progress: number
233 /** 是否正在生成 */
234 isGenerating: boolean
235 /** 最后更新时间戳 */
236 lastUpdated: number
237 }
238
239 /** 按任务ID存储的消息映射 */
240 export type TaskMessageMap = Record<string, ITaskMessageData>
241
242 /** Agent Store 状态 */
243 export interface IAgentState {
244 // === 当前活跃任务 ===
245 /** 当前任务ID */
246 currentTaskId: string
247
248 // === 任务消息存储(按 taskId 隔离) ===
249 /** 任务消息映射表 */
250 taskMessages: TaskMessageMap
251
252 // === 全局状态(不需要按任务隔离) ===
253 /** 本次消费金额 */
254 currentCost: number
255 /** 待处理的任务(从首页跳转时设置) */
256 pendingTask: IPendingTask | null
257
258 // === Debug 模式状态 ===
259 /** Debug 模式文件列表(按顺序对应第 N 次消息) */
260 debugFiles: string[]
261 /** Debug 模式当前消息索引 */
262 debugMessageIndex: number
263 }
264
265 // ============ Action Handler 相关类型 ============
266
267 /** Action 上下文 */
268 export interface IActionContext {
269 /** Next.js 路由实例 */
270 router: AppRouterInstance
271 /** 当前语言 */
272 lng: string
273 /** 翻译函数 */
274 t: TFunction
275 }
276
277 /** Action Handler 接口 */
278 export interface IActionHandler {
279 /** Action 类型 */
280 type: ActionType
281 /** 判断是否能处理该任务 */
282 canHandle: (taskData: ITaskData) => boolean
283 /** 执行 Action */
284 execute: (taskData: ITaskData, context: IActionContext) => Promise<void>
285 }
286
287 /** SSE 消息类型 */
288 export interface ISSEMessage {
289 type:
290 | 'init'
291 | 'keep_alive'
292 | 'stream_event'
293 | 'message'
294 | 'status'
295 | 'error'
296 | 'done'
297 | 'text'
298 | 'result'
299 | 'assistant'
300 | 'user'
301 taskId?: string
302 message?: string | any
303 sessionId?: string
304 status?: string
305 data?: any
306 }
307
308 /** 创建任务参数 */
309 export interface ICreateTaskParams {
310 /** 提示词 */
311 prompt: string
312 /** 媒体文件列表 */
313 medias?: IUploadedMedia[]
314 /** 翻译函数 */
315 t: (key: string) => string
316 /** taskId 获取成功回调 */
317 onTaskIdReady?: (taskId: string) => void
318 /** 需要登录回调 */
319 onLoginRequired?: () => void
320 }
321
321 lines TYPESCRIPT