diff --git a/sdk/builders/fluent/search.go b/sdk/builders/fluent/search.go index f0e4f00..9553334 100644 --- a/sdk/builders/fluent/search.go +++ b/sdk/builders/fluent/search.go @@ -7,6 +7,27 @@ import ( "github.com/nudibranches-tech/bifrost-hyperfluid-sdk-dev/sdk/utils" ) +type DocumentRecord struct { + Name string `json:"name"` + Content string `json:"content"` + Summary string `json:"summary"` + HfContext string `json:"hf_context"` + OriginalFile string `json:"original_file"` + Categories string `json:"categories"` + RlsLabels string `json:"rls_labels"` +} + +type DocumentResult struct { + Record DocumentRecord `json:"record"` + Score float64 `json:"score"` +} + +type SearchResults struct { + Results []DocumentResult `json:"results"` + Total int `json:"total"` + TimeTakenMs int `json:"took_ms"` +} + // SearchBuilder provides a fluent interface for building and executing full-text search queries. type SearchBuilder struct { client interface { @@ -136,7 +157,7 @@ func (sb *SearchBuilder) validate() error { } // Execute executes the search query and returns the results. -func (sb *SearchBuilder) Execute(ctx context.Context) (*utils.Response, error) { +func (sb *SearchBuilder) Execute(ctx context.Context) (*SearchResults, error) { // Validate the search if err := sb.validate(); err != nil { return nil, err @@ -160,5 +181,21 @@ func (sb *SearchBuilder) Execute(ctx context.Context) (*utils.Response, error) { body := utils.JsonMarshal(requestBody) // Execute the request - return sb.client.Do(ctx, "POST", endpoint, body) + resp, err := sb.client.Do(ctx, "POST", endpoint, body) + if err != nil { + return nil, err + } + + // Check if response is OK + if resp.Status != utils.StatusOK { + return nil, fmt.Errorf("%w: %s", utils.ErrAPIError, resp.Error) + } + + // Unmarshal the response data into SearchResults + searchResults := &SearchResults{} + if err := utils.UnmarshalData(resp.Data, searchResults); err != nil { + return nil, fmt.Errorf("failed to unmarshal search results: %w", err) + } + + return searchResults, nil } diff --git a/sdk/utils/errors.go b/sdk/utils/errors.go index 10722e8..899c069 100644 --- a/sdk/utils/errors.go +++ b/sdk/utils/errors.go @@ -8,4 +8,5 @@ var ( ErrNotFound = errors.New("resource not found") ErrPermissionDenied = errors.New("permission denied") ErrInvalidRequest = errors.New("invalid request") + ErrAPIError = errors.New("API error") ) diff --git a/sdk/utils/helpers.go b/sdk/utils/helpers.go index af183e5..808333c 100644 --- a/sdk/utils/helpers.go +++ b/sdk/utils/helpers.go @@ -65,3 +65,24 @@ func (response *Response) GetDataAsMap() (map[string]any, bool) { mapValue, isMap := response.Data.(map[string]any) return mapValue, isMap } + +// UnmarshalData converts response data (interface{}) into a typed struct. +// This is useful for converting the generic Response.Data into specific types. +func UnmarshalData(data any, target any) error { + if data == nil { + return fmt.Errorf("data is nil") + } + + // Marshal the data back to JSON + jsonBytes, err := json.Marshal(data) + if err != nil { + return fmt.Errorf("failed to marshal data: %w", err) + } + + // Unmarshal into the target type + if err := json.Unmarshal(jsonBytes, target); err != nil { + return fmt.Errorf("failed to unmarshal into target type: %w", err) + } + + return nil +} diff --git a/usage_examples/fluent_api_examples.go b/usage_examples/fluent_api_examples.go index f244b82..a96cb8a 100644 --- a/usage_examples/fluent_api_examples.go +++ b/usage_examples/fluent_api_examples.go @@ -38,8 +38,8 @@ func runSearchExample() { fmt.Println() // Search for content - resp, err := client.Search(). - Query("machine learning"). + resp, _ := client.Search(). + Query("rapport ventes"). DataDock(projectID). // ✅ Use the actual UUID variable Catalog(catalog). // ✅ Use actual catalog Schema(schema). // ✅ Use actual schema @@ -48,7 +48,7 @@ func runSearchExample() { Limit(10). Execute(context.Background()) - handleResponse(resp, err) + fmt.Println(resp.Results) fmt.Println() }