diff --git a/go/appbuilder/app_builder_client.go b/go/appbuilder/app_builder_client.go index 46e4e54e8..c6921a640 100644 --- a/go/appbuilder/app_builder_client.go +++ b/go/appbuilder/app_builder_client.go @@ -26,10 +26,16 @@ import ( "net/url" "os" "path/filepath" + "reflect" "strconv" "time" ) +type AppbuilderClient interface { + CreateConversation() (string, error) + Run(conversationID string, query string, fileIDS []string, stream bool) +} + // Deprecated: 将废弃,请使用DescribeApps替代 func GetAppList(req GetAppListRequest, config *SDKConfig) ([]App, error) { request := http.Request{} @@ -250,52 +256,22 @@ func (t *AppBuilderClient) UploadLocalFile(conversationID string, filePath strin return val.(string), nil } -func (t *AppBuilderClient) Run(conversationID string, query string, fileIDS []string, stream bool) (AppBuilderClientIterator, error) { - if len(conversationID) == 0 { - return nil, errors.New("conversationID mustn't be empty") - } - m := map[string]any{ - "app_id": t.appID, - "conversation_id": conversationID, - "query": query, - "file_ids": fileIDS, - "stream": stream, - } - request := http.Request{} - - serviceURL, err := t.sdkConfig.ServiceURLV2("/app/conversation/runs") - if err != nil { - return nil, err +func (t *AppBuilderClient) Run(param ...interface{}) (AppBuilderClientIterator, error) { + if len(param) == 0 { + return nil, errors.New("no arguments provided") } + var err error + var req AppBuilderClientRunRequest - request.URL = serviceURL - request.Method = "POST" - header := t.sdkConfig.AuthHeaderV2() - header.Set("Content-Type", "application/json") - request.Header = header - data, _ := json.Marshal(m) - request.Body = NopCloser(bytes.NewReader(data)) - request.ContentLength = int64(len(data)) // 手动设置长度 - - t.sdkConfig.BuildCurlCommand(&request) - - resp, err := t.client.Do(&request) - if err != nil { - return nil, err - } - - requestID, err := checkHTTPResponse(resp) - if err != nil { - return nil, fmt.Errorf("requestID=%s, err=%v", requestID, err) - } - r := NewSSEReader(1024*1024, bufio.NewReader(resp.Body)) - if stream { - return &AppBuilderClientStreamIterator{requestID: requestID, r: r, body: resp.Body}, nil + if reflect.TypeOf(param[0]) == reflect.TypeOf(AppBuilderClientRunRequest{}) { + req = param[0].(AppBuilderClientRunRequest) + } else { + req, err = t.buildAppBuilderClientRunRequest(param...) + if err != nil { + return nil, err + } } - return &AppBuilderClientOnceIterator{body: resp.Body}, nil -} -func (t *AppBuilderClient) RunV2(req AppBuilderClientRunRequest) (AppBuilderClientIterator, error) { if len(req.ConversationID) == 0 { return nil, errors.New("conversationID mustn't be empty") } @@ -332,6 +308,38 @@ func (t *AppBuilderClient) RunV2(req AppBuilderClientRunRequest) (AppBuilderClie return &AppBuilderClientOnceIterator{body: resp.Body}, nil } +func (t *AppBuilderClient) buildAppBuilderClientRunRequest(param ...interface{}) (AppBuilderClientRunRequest, error) { + conversationID, ok := param[0].(string) + if !ok { + return AppBuilderClientRunRequest{}, errors.New("conversationID must be string type") + } + query, ok := param[1].(string) + if !ok { + return AppBuilderClientRunRequest{}, errors.New("query must be string type") + } + + var fileIDS []string + if param[2] != nil { + fileIDS, ok = param[2].([]string) + if !ok { + fileIDS = nil + } + } + + stream, ok := param[3].(bool) + if !ok { + stream = false + } + + return AppBuilderClientRunRequest{ + AppID: t.appID, + ConversationID: conversationID, + Query: query, + Stream: stream, + FileIDs: fileIDS, + }, nil +} + func (t *AppBuilderClient) RunWithToolCall(req AppBuilderClientRunRequest) (AppBuilderClientIterator, error) { if len(req.ConversationID) == 0 { return nil, errors.New("conversationID mustn't be empty") diff --git a/go/appbuilder/app_builder_client_data.go b/go/appbuilder/app_builder_client_data.go index ca8887b28..7ca8c5ac0 100644 --- a/go/appbuilder/app_builder_client_data.go +++ b/go/appbuilder/app_builder_client_data.go @@ -58,6 +58,7 @@ type AppBuilderClientRunRequest struct { Stream bool `json:"stream"` EndUserID *string `json:"end_user_id"` ConversationID string `json:"conversation_id"` + FileIDs []string `json:"file_ids"` Tools []Tool `json:"tools"` ToolOutputs []ToolOutput `json:"tool_outputs"` ToolChoice *ToolChoice `json:"tool_choice"` diff --git a/go/appbuilder/app_builder_client_test.go b/go/appbuilder/app_builder_client_test.go index 8cd46daf1..e6b817566 100644 --- a/go/appbuilder/app_builder_client_test.go +++ b/go/appbuilder/app_builder_client_test.go @@ -574,7 +574,7 @@ func TestAppBuilderClientRunToolChoice(t *testing.T) { input := make(map[string]any) input["city"] = "北京" end_user_id := "go_user_id_0" - i, err := client.RunV2(AppBuilderClientRunRequest{ + i, err := client.Run(AppBuilderClientRunRequest{ ConversationID: conversationID, AppID: appID, Query: "你能干什么", @@ -635,7 +635,7 @@ func TestAppBuilderClientRunChatflow(t *testing.T) { t.Fatalf("create conversation failed: %v", err) } - i, err := client.RunV2(AppBuilderClientRunRequest{ + i, err := client.Run(AppBuilderClientRunRequest{ ConversationID: conversationID, AppID: appID, Query: "查天气", @@ -668,7 +668,7 @@ func TestAppBuilderClientRunChatflow(t *testing.T) { t.Fatalf("interrupt id is empty") } - i2, err := client.RunV2(AppBuilderClientRunRequest{ + i2, err := client.Run(AppBuilderClientRunRequest{ ConversationID: conversationID, AppID: appID, Query: "我先查个航班动态",