Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions workflows_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,6 @@ func (r *workflowRuns) Stream(ctx context.Context, req *RunWorkflowsReq) (Stream
return newStream(ctx, r.client, response.HTTPResponse, parseWorkflowEvent), err
}

// WorkflowRunResult represents the result of a workflow runs
type WorkflowRunResult struct {
DebugUrl string `json:"debug_url"`

// Workflow execution result, usually a JSON serialized string. In some scenarios, a string with a
// non-JSON structure may be returned.
Data string `json:"data"`

// Execution ID of asynchronous execution. Only returned when the workflow is executed
// asynchronously (is_async=true). You can use execute_id to call the Query Workflow Asynchronous
// Execution Result API to obtain the final execution result of the workflow.
ExecuteID string `json:"execute_id"`
}

// WorkflowEvent represents an event in a workflow
type WorkflowEvent struct {
// The event ID of this message in the interface response. It starts from 0.
Expand Down Expand Up @@ -150,6 +136,9 @@ type WorkflowEventMessage struct {

// Additional fields.
Ext map[string]any `json:"ext,omitempty"`

// Detailed information about Token consumption.
Usage *ChatUsage `json:"usage,omitempty"`
}

// WorkflowEventType represents the type of workflow event
Expand Down
6 changes: 6 additions & 0 deletions workflows_runs_histories.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type RunWorkflowsResp struct {
DebugURL string `json:"debug_url,omitempty"`
Token int `json:"token,omitempty"`
Cost string `json:"cost,omitempty"`

// Detailed information about Token consumption.
Usage *ChatUsage `json:"usage,omitempty"`
}

// RetrieveWorkflowRunsHistoriesResp represents response for retrieving workflow runs history
Expand Down Expand Up @@ -122,6 +125,9 @@ type WorkflowRunHistory struct {
// Workflow trial runs debugging page. Visit this page to view the running results, input and
// output information of each workflow node.
DebugURL string `json:"debug_url"`

// Detailed information about Token consumption.
Usage *ChatUsage `json:"usage,omitempty"`
}

// NodeExecuteStatus represents the status of a node execution
Expand Down
43 changes: 43 additions & 0 deletions workflows_runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,49 @@ func TestWorkflowRuns(t *testing.T) {
as.Equal("https://debug.example.com", resp.DebugURL)
as.Equal(100, resp.Token)
as.Equal("0.1", resp.Cost)
as.Nil(resp.Usage) // Usage is optional
})

t.Run("create workflow run with usage", func(t *testing.T) {
workflowRuns := newWorkflowRun(newCoreWithTransport(newMockTransport(func(req *http.Request) (*http.Response, error) {
as.Equal(http.MethodPost, req.Method)
as.Equal("/v1/workflow/run", req.URL.Path)
return mockResponse(http.StatusOK, &runWorkflowsResp{
RunWorkflowsResp: &RunWorkflowsResp{
ExecuteID: "exec1",
Data: `{"result": "success"}`,
DebugURL: "https://debug.example.com",
Token: 100,
Cost: "0.1",
Usage: &ChatUsage{
TokenCount: 100,
OutputCount: 50,
InputCount: 50,
},
},
})
})))
resp, err := workflowRuns.Create(context.Background(), &RunWorkflowsReq{
WorkflowID: "workflow1",
Parameters: map[string]any{
"param1": "value1",
},
BotID: "bot1",
IsAsync: true,
AppID: "app1",
})
as.Nil(err)
as.NotNil(resp)
as.NotEmpty(resp.Response().LogID())
as.Equal("exec1", resp.ExecuteID)
as.Equal(`{"result": "success"}`, resp.Data)
as.Equal("https://debug.example.com", resp.DebugURL)
as.Equal(100, resp.Token)
as.Equal("0.1", resp.Cost)
as.NotNil(resp.Usage)
as.Equal(100, resp.Usage.TokenCount)
as.Equal(50, resp.Usage.OutputCount)
as.Equal(50, resp.Usage.InputCount)
})

t.Run("stream workflow run success", func(t *testing.T) {
Expand Down
Loading