Skip to content

Commit 668ea1d

Browse files
committed
feat: add Application client and workflow logs API
- Add Application client with GetParameters method for retrieving input form configuration - Add GetLogs method to WorkflowService for fetching workflow execution logs - Introduce BaseClient to share common client fields across services - Add schema types for ApplicationParameters and WorkflowLogs - Add documentation comments for Application and WorkflowService methods
1 parent 11556e6 commit 668ea1d

6 files changed

Lines changed: 155 additions & 15 deletions

File tree

client/api/v1/application.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
8+
"github.com/yeeaiclub/dify-go/internal/handler"
9+
"github.com/yeeaiclub/dify-go/schema"
10+
)
11+
12+
// Application represents a client for interacting with the application API endpoints.
13+
type Application struct {
14+
*BaseClient
15+
}
16+
17+
// NewApplication creates a new Application client instance.
18+
func NewApplication(baseURL, apiKey string) *Application {
19+
baseClient := &BaseClient{
20+
client: handler.NewClient(),
21+
apiKey: apiKey,
22+
baseURL: baseURL,
23+
}
24+
return &Application{baseClient}
25+
}
26+
27+
// GetParameters retrieves the application's input form configuration.
28+
func (app *Application) GetParameters(ctx context.Context) (schema.ApplicationParameters, error) {
29+
r, err := handler.NewRequestBuilder().
30+
Method(http.MethodGet).
31+
BaseURL(app.baseURL).
32+
Token(app.apiKey).
33+
Path("v1/parameters").
34+
Build()
35+
if err != nil {
36+
return schema.ApplicationParameters{}, err
37+
}
38+
resp, err := app.client.Send(ctx, r)
39+
if err != nil {
40+
return schema.ApplicationParameters{}, err
41+
}
42+
var respData schema.ApplicationParameters
43+
err = json.Unmarshal(resp.Body, &respData)
44+
if err != nil {
45+
return schema.ApplicationParameters{}, err
46+
}
47+
return respData, nil
48+
}

client/api/v1/base.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package v1
2+
3+
import "github.com/yeeaiclub/dify-go/internal/handler"
4+
5+
// BaseClient the base clint of dify
6+
type BaseClient struct {
7+
client *handler.Client // HTTP client for making API requests
8+
apiKey string // API key for authentication
9+
baseURL string // Base URL of the API server
10+
}

client/api/v1/file.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ import (
1414

1515
// FileService is a service for file operations.
1616
type FileService struct {
17-
client *handler.Client // HTTP client for making API requests
18-
apiKey string // API key for authentication
19-
baseURL string // Base URL of the API server
17+
*BaseClient
2018
}
2119

2220
// NewFileService creates a new FileService instance with the provided baseURL and apiKey.
2321
func NewFileService(baseURL, apiKey string) *FileService {
24-
return &FileService{
22+
baseClient := &BaseClient{
2523
client: handler.NewClient(),
2624
apiKey: apiKey,
2725
baseURL: baseURL,
2826
}
27+
return &FileService{baseClient}
2928
}
3029

3130
// Upload upload file to dify.

client/api/v1/workflow.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ const (
2323

2424
// WorkflowService represents a client for interacting with the workflow API endpoints.
2525
type WorkflowService struct {
26-
client *handler.Client // HTTP client for making API requests
27-
apiKey string // API key for authentication
28-
baseURL string // Base URL of the API server
26+
*BaseClient
2927
}
3028

3129
// NewWorkflowService creates a new Workflow client instance.
3230
func NewWorkflowService(baseURL, apiKey string) *WorkflowService {
33-
return &WorkflowService{
31+
baseClient := &BaseClient{
3432
client: handler.NewClient(),
3533
apiKey: apiKey,
3634
baseURL: baseURL,
3735
}
36+
return &WorkflowService{baseClient}
3837
}
3938

4039
// RunStream executes a workflow in streaming mode. Cannot execute if there is no published workflow.
@@ -90,3 +89,33 @@ func (w *WorkflowService) Run(
9089
}
9190
return respData, nil
9291
}
92+
93+
// GetLogs retrieves workflow execution logs with optional filtering.
94+
// Supports pagination and filtering by status, keyword, and time range.
95+
func (w *WorkflowService) GetLogs(
96+
ctx context.Context,
97+
query schema.WorkflowRunLogQuery,
98+
) (schema.WorkflowLogsResponse, error) {
99+
r, err := handler.NewRequestBuilder().
100+
BaseURL(w.baseURL).
101+
Token(w.apiKey).
102+
Path("v1/workflows/logs").
103+
Method(http.MethodGet).
104+
Query(query).
105+
Build()
106+
107+
if err != nil {
108+
return schema.WorkflowLogsResponse{}, err
109+
}
110+
111+
resp, err := w.client.Send(ctx, r)
112+
if err != nil {
113+
return schema.WorkflowLogsResponse{}, err
114+
}
115+
var respData schema.WorkflowLogsResponse
116+
err = json.Unmarshal(resp.Body, &respData)
117+
if err != nil {
118+
return schema.WorkflowLogsResponse{}, err
119+
}
120+
return respData, nil
121+
}

schema/application.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package schema
2+
3+
type ApplicationParameters struct {
4+
OpeningStatement string `json:"opening_statement,omitempty"`
5+
SuggestedQuestions []string `json:"suggested_questions,omitempty"`
6+
SuggestedQuestionsAfterAnswer map[string]any `json:"suggested_questions_after_answer,omitempty"`
7+
SpeechToText map[string]any `json:"speech_to_text,omitempty"`
8+
RetrieverResource map[string]any `json:"retriever_resource,omitempty"`
9+
AnnotationReply map[string]any `json:"annotation_reply,omitempty"`
10+
UserInputForm []map[string]any `json:"user_input_form,omitempty"`
11+
FileUpload map[string]any `json:"file_upload,omitempty"`
12+
SystemParameters map[string]any `json:"system_parameters,omitempty"`
13+
}

schema/workflow.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,52 @@ type RunWorkflowResponseData struct {
4343
FinishedAt int `json:"finished_at"`
4444
}
4545

46-
// StreamEvent represents an event in a streaming response.
47-
// T is the type of data contained in the event.
48-
type StreamEvent[T any] struct {
49-
Err string `json:"err"`
50-
Data T `json:"data"`
51-
Type string `json:"event"`
52-
Done bool `json:"done"`
46+
type WorkflowRunLogQuery struct {
47+
Keyword string `url:"keyword"`
48+
Status string `url:"status"`
49+
Page int `url:"page"`
50+
Limit int `url:"limit"`
51+
CreatedAtBefore string `url:"created_at__before"`
52+
CreatedAtAfter string `url:"created_at__after"`
53+
CreatedByEndUserSessionID string `url:"created_by_end_user_session_id"`
54+
CreatedByAccount string `url:"created_by_account"`
55+
}
56+
57+
type WorkflowLogsResponse struct {
58+
Page int `json:"page"`
59+
Limit int `json:"limit"`
60+
Total int `json:"total"`
61+
HasMore bool `json:"has_more"`
62+
Data []WorkflowLogsResponseData `json:"data"`
63+
}
64+
65+
type WorkflowLogsResponseData struct {
66+
ID string `json:"id"`
67+
WorkflowRunDetail WorkflowRunDetail `json:"workflow_run"`
68+
CreatedFrom string `json:"created_from"`
69+
CreatedByRole string `json:"created_by_role"`
70+
CreatedByAccount *string `json:"created_by_account,omitempty"`
71+
CreatedByEndUser *CreatedByEndUser `json:"created_by_end_user,omitempty"`
72+
CreatedAt int64 `json:"created_at"`
73+
}
74+
75+
type WorkflowRunDetail struct {
76+
ID string `json:"id"`
77+
Version string `json:"version"`
78+
Status string `json:"status"`
79+
Error *string `json:"error,omitempty"`
80+
ElapsedTime float64 `json:"elapsed_time"`
81+
TotalTokens int `json:"total_tokens"`
82+
TotalSteps int `json:"total_steps"`
83+
CreatedAt int64 `json:"created_at"`
84+
FinishedAt int64 `json:"finished_at"`
85+
ExceptionsCount int `json:"exceptions_count"`
86+
TriggeredFrom string `json:"triggered_from"`
87+
}
88+
89+
type CreatedByEndUser struct {
90+
ID string `json:"id"`
91+
Type string `json:"type"`
92+
IsAnonymous bool `json:"is_anonymous"`
93+
SessionID string `json:"session_id"`
5394
}

0 commit comments

Comments
 (0)