@our-llm/shared 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@our-llm/shared",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "exports": {
6
+ "./workflow-events": {
7
+ "types": "./workflow-events.ts",
8
+ "import": "./workflow-events.ts",
9
+ "default": "./workflow-events.ts"
10
+ },
11
+ "./workflow.types": {
12
+ "types": "./workflow.types.ts",
13
+ "import": "./workflow.types.ts",
14
+ "default": "./workflow.types.ts"
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * 工作流 SSE 事件协议
3
+ * 字段名采用缩写形式以减少流量消耗
4
+ *
5
+ * SSE event 字段用于类型判断,data 中不再重复 e 字段
6
+ *
7
+ * 字段映射:
8
+ * - t: threadId (线程ID)
9
+ * - n: nodeId (节点ID)
10
+ * - c: content (内容)
11
+ * - r: reasoning (思维过程标记,1=true)
12
+ * - v: variables (业务变量)
13
+ * - m: message (错误消息)
14
+ */
15
+
16
+ // 工作流开始事件的 data
17
+ export interface WorkflowStartData {
18
+ t: string // threadId
19
+ }
20
+
21
+ // 节点开始事件的 data
22
+ export interface NodeStartData {
23
+ n: string // nodeId
24
+ }
25
+
26
+ // 节点结束事件的 data
27
+ export interface NodeEndData {
28
+ n: string // nodeId
29
+ v?: Record<string, unknown> // variables (业务输出变量)
30
+ m?: SimpleMessage[] // messages (AI 返回的消息,包含图片/视频)
31
+ }
32
+
33
+ // 简化后的消息格式(与后端 SimpleMessage 保持一致)
34
+ export interface SimpleMessage {
35
+ type: 'human' | 'ai' | 'system'
36
+ content:
37
+ | string
38
+ | Array<
39
+ | { type: 'text'; text: string }
40
+ | { type: 'image_url'; image_url: { url: string } }
41
+ >
42
+ id?: string
43
+ images?: string[]
44
+ videos?: string[]
45
+ documents?: Array<{ filename: string; url: string }>
46
+ }
47
+
48
+ // Token 流式事件的 data
49
+ export interface TokenData {
50
+ n: string // nodeId
51
+ c: string // content
52
+ r?: 1 // isReasoning (只在 true 时存在,值为 1)
53
+ }
54
+
55
+ // 完成事件的 data(空对象)
56
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
57
+ export interface CompleteData {}
58
+
59
+ // 错误事件的 data
60
+ export interface ErrorData {
61
+ m: string // message
62
+ }
63
+
64
+ // SSE 事件类型
65
+ export type SSEEventType = 'ws' | 'ns' | 'ne' | 'tk' | 'ok' | 'err'
66
+
67
+ // 事件类型映射(后端使用)
68
+ export const EventTypeMap = {
69
+ workflow_start: 'ws',
70
+ node_start: 'ns',
71
+ node_end: 'ne',
72
+ token: 'tk',
73
+ complete: 'ok',
74
+ error: 'err',
75
+ } as const
76
+
77
+ // 反向映射(前端使用)
78
+ export const EventTypeReverse = {
79
+ ws: 'workflow_start',
80
+ ns: 'node_start',
81
+ ne: 'node_end',
82
+ tk: 'token',
83
+ ok: 'complete',
84
+ err: 'error',
85
+ } as const
@@ -0,0 +1,189 @@
1
+ // 数据类型定义
2
+ export interface TextData {
3
+ type: 'text'
4
+ value: string
5
+ }
6
+
7
+ export interface ImageData {
8
+ type: 'image'
9
+ value: { url: string }
10
+ }
11
+
12
+ export type InputData = TextData | ImageData
13
+
14
+ // 节点类型常量
15
+ export const NodeType = {
16
+ START: 'start',
17
+ END: 'end',
18
+ DATA_TRANSFORM: 'data_transform',
19
+ LOOP: 'loop',
20
+ BRAIN: 'brain',
21
+ CONDITION: 'condition',
22
+ OCR: 'ocr',
23
+ IMAGE_TO_IMAGE: 'image_to_image',
24
+ IMAGE_TO_VIDEO: 'image_to_video',
25
+ MESSAGE: 'message',
26
+ BILLING: 'billing',
27
+ } as const
28
+
29
+ export type NodeType = (typeof NodeType)[keyof typeof NodeType]
30
+
31
+ // 节点配置接口
32
+ export interface NodeConfig {
33
+ name: string
34
+ }
35
+
36
+ // 开始节点配置
37
+ export interface StartNodeConfig extends NodeConfig {
38
+ type: typeof NodeType.START
39
+ }
40
+
41
+ // 结束节点配置
42
+ export interface EndNodeConfig extends NodeConfig {
43
+ type: typeof NodeType.END
44
+ }
45
+
46
+ // 数据转换节点配置
47
+ export interface DataTransformNodeConfig extends NodeConfig {
48
+ type: typeof NodeType.DATA_TRANSFORM
49
+ script: string // JS代码片段,接收 context 参数(包含 variables 和 messages),返回对象用于更新 variables
50
+ }
51
+
52
+ // 循环节点配置
53
+ export interface LoopNodeConfig extends NodeConfig {
54
+ type: typeof NodeType.LOOP
55
+ subWorkflowId: string // 子工作流的ID引用
56
+ /** 执行模式:串行(默认)或并行 */
57
+ executionMode?: 'sequential' | 'parallel'
58
+ /** 是否显示子工作流内部事件(token、节点状态等),默认 false */
59
+ showSubWorkflowEvents?: boolean
60
+ }
61
+
62
+ // 联网搜索配置(使用豆包内置搜索工具)
63
+ export interface WebSearchConfig {
64
+ /** 是否启用联网搜索 */
65
+ enabled: boolean
66
+ /** 最大关键词数量(1-10,默认 3) */
67
+ maxKeyword?: number
68
+ }
69
+
70
+ // 智慧大脑节点配置
71
+ export interface BrainNodeConfig extends NodeConfig {
72
+ type: typeof NodeType.BRAIN
73
+ systemMessage: string // 系统提示词
74
+ humanMessageScript?: string // 可选的用户消息脚本,接收 context 参数(包含 variables 和 messages),返回 string[] 用于构造 HumanMessage[]
75
+ /** 联网搜索配置 */
76
+ webSearch?: WebSearchConfig
77
+ }
78
+
79
+ // 条件判断节点配置
80
+ export interface ConditionNodeConfig extends NodeConfig {
81
+ type: typeof NodeType.CONDITION
82
+ conditions: Array<{
83
+ id: string
84
+ name: string // 条件名称,如 "文本处理"、"图片处理"
85
+ script: string // 条件判断脚本,如 "context.variables.input === 'text'"
86
+ description?: string // 条件描述
87
+ }>
88
+ }
89
+
90
+ // OCR 节点配置
91
+ export interface OCRNodeConfig extends NodeConfig {
92
+ type: typeof NodeType.OCR
93
+ script: string // JS代码片段,接收 context 参数(包含 variables、messages 和 ocr 方法),返回对象用于更新 variables
94
+ }
95
+
96
+ // 图生图节点配置
97
+ export interface ImageToImageNodeConfig extends NodeConfig {
98
+ type: typeof NodeType.IMAGE_TO_IMAGE
99
+ prompt: string // 图生图的提示词
100
+ script: string // JS代码片段,接收 context 参数(包含 variables、messages 和 imageToImage 方法),返回对象用于更新 variables
101
+ }
102
+
103
+ // 图生视频节点配置
104
+ export interface ImageToVideoNodeConfig extends NodeConfig {
105
+ type: typeof NodeType.IMAGE_TO_VIDEO
106
+ prompt: string // 图生视频的提示词
107
+ duration: number // 视频时长(秒),支持 5 或 10
108
+ script: string // JS代码片段,接收 context 参数(包含 variables、messages 和 imageToVideo 方法),返回对象用于更新 variables
109
+ }
110
+
111
+ // 消息节点配置
112
+ export interface MessageNodeConfig extends NodeConfig {
113
+ type: typeof NodeType.MESSAGE
114
+ role?: 'user' | 'assistant' // 消息角色,默认 assistant
115
+ content: string // 消息内容,支持模板变量 {{variableName}}
116
+ images?: string // 可选:图片变量名,如 "imageUrls" 会从 variables.imageUrls 读取
117
+ videos?: string // 可选:视频变量名,如 "videoUrl" 会从 variables.videoUrl 读取
118
+ }
119
+
120
+ // 计费节点配置
121
+ export interface BillingNodeConfig extends NodeConfig {
122
+ type: typeof NodeType.BILLING
123
+ script: string // JS 代码片段,接收 context 参数,返回 { tokenCount: number, artifactCount: number }
124
+ }
125
+
126
+ // 联合类型:所有节点配置
127
+ export type AnyNodeConfig =
128
+ | StartNodeConfig
129
+ | EndNodeConfig
130
+ | DataTransformNodeConfig
131
+ | LoopNodeConfig
132
+ | BrainNodeConfig
133
+ | ConditionNodeConfig
134
+ | OCRNodeConfig
135
+ | ImageToImageNodeConfig
136
+ | ImageToVideoNodeConfig
137
+ | MessageNodeConfig
138
+ | BillingNodeConfig
139
+
140
+ // 工作流边配置
141
+ export interface WorkflowEdge {
142
+ id: string
143
+ source: string
144
+ target: string
145
+ sourceHandle?: string | null
146
+ targetHandle?: string | null
147
+ conditionId?: string // 条件判断节点的条件ID,用于标识走哪个分支
148
+ }
149
+
150
+ // 工作流节点配置
151
+ export interface WorkflowNode {
152
+ id: string
153
+ type: NodeType
154
+ position: { x: number; y: number }
155
+ config: AnyNodeConfig
156
+ }
157
+
158
+ // 工作流配置 (分离配置和实体)
159
+ export interface WorkflowConfig {
160
+ id: string
161
+ name: string
162
+ description: string
163
+ nodes: WorkflowNode[]
164
+ edges: WorkflowEdge[]
165
+ }
166
+
167
+ // 工作流实体 (包含数据库元数据)
168
+ export interface Workflow extends WorkflowConfig {
169
+ createdAt: string
170
+ updatedAt: string
171
+ }
172
+
173
+ // 工作流执行输入的消息内容部件类型
174
+ export type WorkflowMessagePart =
175
+ | { type: 'text'; text: string }
176
+ | { type: 'image'; url?: string; base64?: string }
177
+ | { type: 'file' | 'audio' | 'video'; url?: string }
178
+ | { type: 'document'; base64?: string; url?: string; filename: string; mimeType: string }
179
+
180
+ // 工作流执行输入的消息类型(content 为数组,支持多模态内容)
181
+ export interface WorkflowMessage {
182
+ content: WorkflowMessagePart[]
183
+ }
184
+
185
+ // 工作流执行输入参数类型
186
+ export interface WorkflowExecutionInput {
187
+ messages?: WorkflowMessage[]
188
+ variables?: Record<string, unknown>
189
+ }