forked from zhouyangtingwen/dify-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 17
/
api_workflow.go
210 lines (185 loc) · 6.77 KB
/
api_workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package dify
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// 事件类型常量
const (
EventWorkflowStarted = "workflow_started"
EventNodeStarted = "node_started"
EventNodeFinished = "node_finished"
EventWorkflowFinished = "workflow_finished"
EventTTSMessage = "tts_message"
EventTTSMessageEnd = "tts_message_end"
)
// FileInput 结构体
type FileInput struct {
Type string `json:"type"` // 目前仅支持 "image"
TransferMethod string `json:"transfer_method"` // "remote_url" 或 "local_file"
URL string `json:"url,omitempty"` // 当 transfer_method 为 remote_url 时使用
UploadFileID string `json:"upload_file_id,omitempty"` // 当 transfer_method 为 local_file 时使用
}
// WorkflowRequest 结构体
type WorkflowRequest struct {
Inputs map[string]interface{} `json:"inputs"`
ResponseMode string `json:"response_mode"`
User string `json:"user"`
Files []FileInput `json:"files,omitempty"`
}
// WorkflowResponse 结构体
type WorkflowResponse struct {
WorkflowRunID string `json:"workflow_run_id"`
TaskID string `json:"task_id"`
Data struct {
ID string `json:"id"`
WorkflowID string `json:"workflow_id"`
Status string `json:"status"`
Outputs map[string]interface{} `json:"outputs"`
Error *string `json:"error,omitempty"`
ElapsedTime float64 `json:"elapsed_time"`
TotalTokens int `json:"total_tokens"`
TotalSteps int `json:"total_steps"`
CreatedAt int64 `json:"created_at"`
FinishedAt int64 `json:"finished_at"`
} `json:"data"`
}
// StreamingResponse 结构体
type StreamingResponse struct {
Event string `json:"event"`
TaskID string `json:"task_id"`
WorkflowRunID string `json:"workflow_run_id"`
SequenceNumber int `json:"sequence_number"`
Data struct {
ID string `json:"id"`
WorkflowID string `json:"workflow_id,omitempty"`
NodeID string `json:"node_id,omitempty"`
NodeType string `json:"node_type,omitempty"`
Title string `json:"title,omitempty"`
Index int `json:"index"`
Predecessor string `json:"predecessor_node_id,omitempty"`
Inputs []interface{} `json:"inputs,omitempty"`
Outputs map[string]interface{} `json:"outputs,omitempty"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
ElapsedTime float64 `json:"elapsed_time,omitempty"`
ExecutionMetadata struct {
TotalTokens int `json:"total_tokens,omitempty"`
TotalPrice float64 `json:"total_price,omitempty"`
Currency string `json:"currency,omitempty"`
} `json:"execution_metadata,omitempty"`
CreatedAt int64 `json:"created_at"`
FinishedAt int64 `json:"finished_at,omitempty"`
} `json:"data"`
}
// TTSMessage 结构体
type TTSMessage struct {
Event string `json:"event"` // "tts_message" 或 "tts_message_end"
TaskID string `json:"task_id"`
MessageID string `json:"message_id"`
Audio string `json:"audio"` // Base64 编码的音频数据
CreatedAt int64 `json:"created_at"`
}
// EventHandler 接口
type EventHandler interface {
HandleStreamingResponse(StreamingResponse)
HandleTTSMessage(TTSMessage)
}
// DefaultEventHandler 结构体
type DefaultEventHandler struct {
StreamHandler func(StreamingResponse)
}
func (h *DefaultEventHandler) HandleStreamingResponse(resp StreamingResponse) {
if h.StreamHandler != nil {
h.StreamHandler(resp)
}
}
func (h *DefaultEventHandler) HandleTTSMessage(msg TTSMessage) {
// 默认实现为空,如果用户不关心 TTS 消息可以忽略
}
// RunWorkflow 方法
func (api *API) RunWorkflow(ctx context.Context, request WorkflowRequest) (*WorkflowResponse, error) {
req, err := api.createBaseRequest(ctx, http.MethodPost, "/v1/workflows/run", request)
if err != nil {
return nil, fmt.Errorf("failed to create base request: %w", err)
}
resp, err := api.c.sendRequest(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API request failed with status %s: %s", resp.Status, readResponseBody(resp.Body))
}
var workflowResp WorkflowResponse
if err := json.NewDecoder(resp.Body).Decode(&workflowResp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &workflowResp, nil
}
// RunStreamWorkflow 方法
func (api *API) RunStreamWorkflow(ctx context.Context, request WorkflowRequest, handler func(StreamingResponse)) error {
return api.RunStreamWorkflowWithHandler(ctx, request, &DefaultEventHandler{StreamHandler: handler})
}
// RunStreamWorkflowWithHandler 方法
func (api *API) RunStreamWorkflowWithHandler(ctx context.Context, request WorkflowRequest, handler EventHandler) error {
req, err := api.createBaseRequest(ctx, http.MethodPost, "/v1/workflows/run", request)
if err != nil {
return err
}
resp, err := api.c.sendRequest(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API request failed with status %s: %s", resp.Status, readResponseBody(resp.Body))
}
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("error reading streaming response: %w", err)
}
if len(line) > 6 && string(line[:6]) == "data: " {
var event struct {
Event string `json:"event"`
}
if err := json.Unmarshal(line[6:], &event); err != nil {
fmt.Println("Error decoding event type:", err)
continue
}
switch event.Event {
case EventTTSMessage, EventTTSMessageEnd:
var ttsMsg TTSMessage
if err := json.Unmarshal(line[6:], &ttsMsg); err != nil {
fmt.Println("Error decoding TTS message:", err)
continue
}
handler.HandleTTSMessage(ttsMsg)
default:
var streamResp StreamingResponse
if err := json.Unmarshal(line[6:], &streamResp); err != nil {
fmt.Println("Error decoding streaming response:", err)
continue
}
handler.HandleStreamingResponse(streamResp)
}
}
}
return nil
}
// readResponseBody 辅助函数
func readResponseBody(body io.Reader) string {
bodyBytes, err := io.ReadAll(body)
if err != nil {
return fmt.Sprintf("failed to read response body: %v", err)
}
return string(bodyBytes)
}