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
41 changes: 39 additions & 2 deletions sdk/builders/fluent/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
1 change: 1 addition & 0 deletions sdk/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
21 changes: 21 additions & 0 deletions sdk/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions usage_examples/fluent_api_examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +48,7 @@ func runSearchExample() {
Limit(10).
Execute(context.Background())

handleResponse(resp, err)
fmt.Println(resp.Results)
fmt.Println()
}

Expand Down