diff --git a/sdk-go/README.md b/sdk-go/README.md index aa0764de..d4729a19 100644 --- a/sdk-go/README.md +++ b/sdk-go/README.md @@ -4,10 +4,10 @@ # Go SDK for Inferable -[![Go Reference](https://pkg.go.dev/badge/github.com/inferablehq/inferable-go.svg)](https://pkg.go.dev/github.com/inferablehq/inferable-go) +[![Go Reference](https://pkg.go.dev/badge/github.com/inferablehq/inferable/sdk-go.svg)](https://pkg.go.dev/github.com/inferablehq/inferable/sdk-go) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Documentation](https://img.shields.io/badge/docs-inferable.ai-brightgreen)](https://docs.inferable.ai/) -[![Go Report Card](https://goreportcard.com/badge/github.com/inferablehq/inferable-go)](https://goreportcard.com/report/github.com/inferablehq/inferable-go) +[![Go Report Card](https://goreportcard.com/badge/github.com/inferablehq/inferable/sdk-go)](https://goreportcard.com/report/github.com/inferablehq/inferable/sdk-go) Inferable Go Client is a Go package that provides a client for interacting with the Inferable API. It allows you to register your go functions against the Inferable control plane. @@ -16,7 +16,7 @@ Inferable Go Client is a Go package that provides a client for interacting with To install the Inferable Go Client, use the following command: ``` -go get github.com/inferablehq/inferable-go +go get github.com/inferablehq/inferable/sdk-go ``` ## Usage @@ -26,7 +26,7 @@ go get github.com/inferablehq/inferable-go To create a new Inferable client, use the `New` function: ```go -import "github.com/inferablehq/inferable-go/inferable" +import "github.com/inferablehq/inferable/sdk-go/inferable" client, err := inferable.New("your-api-secret", "https://api.inferable.ai") @@ -69,7 +69,7 @@ Here's an example to illustrate this: ```go import ( - "github.com/inferablehq/inferable-go/inferable" + "github.com/inferablehq/inferable/sdk-go/inferable" "time" ) @@ -154,11 +154,10 @@ The following code will create an [Inferable run](https://docs.inferable.ai/page > Runs can also be triggered via the [API](https://docs.inferable.ai/pages/invoking-a-run-api), [CLI](https://www.npmjs.com/package/@inferable/cli) or [playground UI](https://app.inferable.ai/). - ## Contributing Contributions to the Inferable Go Client are welcome. Please ensure that your code adheres to the existing style and includes appropriate tests. ## Support -For support or questions, please [create an issue in the repository](https://github.com/inferablehq/inferable-go/issues). +For support or questions, please [create an issue in the repository](https://github.com/inferablehq/inferable/sdk-go/issues). diff --git a/sdk-go/go.mod b/sdk-go/go.mod index f872b697..2ec37193 100644 --- a/sdk-go/go.mod +++ b/sdk-go/go.mod @@ -1,4 +1,4 @@ -module github.com/inferablehq/inferable-go +module github.com/inferablehq/inferable/sdk-go go 1.22 diff --git a/sdk-go/inferable.go b/sdk-go/inferable.go index 6e2ccb06..3bf0a93e 100644 --- a/sdk-go/inferable.go +++ b/sdk-go/inferable.go @@ -7,8 +7,8 @@ import ( "net/http" "reflect" - "github.com/inferablehq/inferable-go/internal/client" - "github.com/inferablehq/inferable-go/internal/util" + "github.com/inferablehq/inferable/sdk-go/internal/client" + "github.com/inferablehq/inferable/sdk-go/internal/util" ) // Version of the inferable package @@ -28,7 +28,7 @@ type Inferable struct { apiSecret string functionRegistry functionRegistry machineID string - clusterID string + clusterID string Default *service } @@ -36,43 +36,43 @@ type InferableOptions struct { APIEndpoint string APISecret string MachineID string - ClusterID string + ClusterID string } // Input struct passed to a Run's result handler type RunResultHandlerInput struct { - Status string `json:"status"` - RunId string `json:"runId"` - Result interface{} `json:"result"` - Summary string `json:"summary"` - Metadata interface{} `json:"metadata"` + Status string `json:"status"` + RunId string `json:"runId"` + Result interface{} `json:"result"` + Summary string `json:"summary"` + Metadata interface{} `json:"metadata"` } type RunResult struct { - Handler *FunctionHandle - Schema interface{} + Handler *FunctionHandle + Schema interface{} } type RunTemplate struct { - ID string - Input map[string]interface{} + ID string + Input map[string]interface{} } type Run struct { - Functions []*FunctionHandle - Message string - Result *RunResult - Metadata map[string]string - Template *RunTemplate + Functions []*FunctionHandle + Message string + Result *RunResult + Metadata map[string]string + Template *RunTemplate } type runHandle struct { - ID string + ID string } type templateHandle struct { - ID string - Run func(input *Run) (*runHandle, error) + ID string + Run func(input *Run) (*runHandle, error) } func New(options InferableOptions) (*Inferable, error) { @@ -89,15 +89,14 @@ func New(options InferableOptions) (*Inferable, error) { machineID := options.MachineID if machineID == "" { - machineID = util.GenerateMachineID(8) + machineID = util.GenerateMachineID(8) } - inferable := &Inferable{ client: client, apiEndpoint: options.APIEndpoint, apiSecret: options.APISecret, - clusterID: options.ClusterID, + clusterID: options.ClusterID, functionRegistry: functionRegistry{services: make(map[string]*service)}, machineID: machineID, } @@ -125,43 +124,40 @@ func (i *Inferable) RegisterService(serviceName string) (*service, error) { } func (i *Inferable) CreateRun(input *Run) (*runHandle, error) { - if i.clusterID == "" { - return nil, fmt.Errorf("cluster ID must be provided to manage runs") - } - - var attachedFunctions []string - for _, fn := range input.Functions { - attachedFunctions = append(attachedFunctions, fmt.Sprintf("%s_%s", fn.Service, fn.Function)) - } + if i.clusterID == "" { + return nil, fmt.Errorf("cluster ID must be provided to manage runs") + } + var attachedFunctions []string + for _, fn := range input.Functions { + attachedFunctions = append(attachedFunctions, fmt.Sprintf("%s_%s", fn.Service, fn.Function)) + } payload := client.CreateRunInput{ - Message: input.Message, - AttachedFunctions: attachedFunctions, - Metadata: input.Metadata, - } - - if (input.Template != nil) { - payload.Template = &client.CreateRunTemplateInput{ - Input: input.Template.Input, - ID: input.Template.ID, - } - } - - if (input.Result != nil) { - payload.Result = &client.CreateRunResultInput{ - } - if (input.Result.Handler != nil) { - payload.Result.Handler = &client.CreateRunResultHandlerInput{ - Service: input.Result.Handler.Service, - Function: input.Result.Handler.Function, - } - } - if (input.Result.Schema != nil) { - payload.Result.Schema = input.Result.Schema - } - } + Message: input.Message, + AttachedFunctions: attachedFunctions, + Metadata: input.Metadata, + } + if input.Template != nil { + payload.Template = &client.CreateRunTemplateInput{ + Input: input.Template.Input, + ID: input.Template.ID, + } + } + + if input.Result != nil { + payload.Result = &client.CreateRunResultInput{} + if input.Result.Handler != nil { + payload.Result.Handler = &client.CreateRunResultHandlerInput{ + Service: input.Result.Handler.Service, + Function: input.Result.Handler.Function, + } + } + if input.Result.Schema != nil { + payload.Result.Schema = input.Result.Schema + } + } // Marshal the payload to JSON jsonPayload, err := json.Marshal(payload) @@ -200,13 +196,13 @@ func (i *Inferable) CreateRun(input *Run) (*runHandle, error) { return nil, fmt.Errorf("failed to parse run response: %v", err) } - return &runHandle{ID: response.ID}, nil + return &runHandle{ID: response.ID}, nil } func (i *Inferable) GetTemplate(id string) (*templateHandle, error) { - if i.clusterID == "" { - return nil, fmt.Errorf("cluster ID must be provided to manage runs") - } + if i.clusterID == "" { + return nil, fmt.Errorf("cluster ID must be provided to manage runs") + } // Prepare headers headers := map[string]string{ @@ -238,26 +234,26 @@ func (i *Inferable) GetTemplate(id string) (*templateHandle, error) { return nil, fmt.Errorf("failed to parse template response: %v", err) } - return &templateHandle{ - ID: response.ID, - Run: func(input *Run) (*runHandle, error) { - // CLone the input - inputCopy := *input - - // Set the template ID - if inputCopy.Template == nil { - inputCopy.Template = &RunTemplate{ - ID: response.ID, - } - } else { - inputCopy.Template.ID = response.ID - } + return &templateHandle{ + ID: response.ID, + Run: func(input *Run) (*runHandle, error) { + // CLone the input + inputCopy := *input + + // Set the template ID + if inputCopy.Template == nil { + inputCopy.Template = &RunTemplate{ + ID: response.ID, + } + } else { + inputCopy.Template.ID = response.ID + } - fmt.Println(inputCopy) + fmt.Println(inputCopy) - return i.CreateRun(&inputCopy) - }, - }, nil + return i.CreateRun(&inputCopy) + }, + }, nil } func (i *Inferable) callFunc(serviceName, funcName string, args ...interface{}) ([]reflect.Value, error) { @@ -323,7 +319,7 @@ func (i *Inferable) fetchData(options client.FetchDataOptions) ([]byte, http.Hea options.Headers["Content-Type"] = "application/json" } - data, headers, err, status:= i.client.FetchData(options) + data, headers, err, status := i.client.FetchData(options) return []byte(data), headers, err, status } diff --git a/sdk-go/main_test.go b/sdk-go/main_test.go index 61aee4b2..8ee4fc7a 100644 --- a/sdk-go/main_test.go +++ b/sdk-go/main_test.go @@ -3,7 +3,7 @@ package inferable import ( "testing" - "github.com/inferablehq/inferable-go/internal/util" + "github.com/inferablehq/inferable/sdk-go/internal/util" ) type EchoInput struct { diff --git a/sdk-go/service.go b/sdk-go/service.go index d1efb08c..b1699bb5 100644 --- a/sdk-go/service.go +++ b/sdk-go/service.go @@ -12,7 +12,7 @@ import ( "github.com/invopop/jsonschema" - "github.com/inferablehq/inferable-go/internal/client" + "github.com/inferablehq/inferable/sdk-go/internal/client" ) const ( @@ -55,14 +55,14 @@ type callResult struct { } type FunctionHandle struct { - Service string - Function string + Service string + Function string } func (s *service) RegisterFunc(fn Function) (*FunctionHandle, error) { - if s.isPolling() { - return nil, fmt.Errorf("functions must be registered before starting the service.") - } + if s.isPolling() { + return nil, fmt.Errorf("functions must be registered before starting the service.") + } if _, exists := s.Functions[fn.Name]; exists { return nil, fmt.Errorf("function with name '%s' already registered for service '%s'", fn.Name, s.Name) @@ -249,9 +249,9 @@ func (s *service) poll() error { result, respHeaders, err, status := s.inferable.fetchData(options) - if status == 410 { - s.registerMachine() - } + if status == 410 { + s.registerMachine() + } if err != nil { return fmt.Errorf("failed to poll calls: %v", err) @@ -292,7 +292,7 @@ func (s *service) handleMessage(msg callMessage) error { fn, ok := s.Functions[msg.Function] if !ok { log.Printf("Received call for unknown function: %s", msg.Function) - return nil + return nil } // Create a new instance of the function's input type diff --git a/sdk-go/service_test.go b/sdk-go/service_test.go index 4c9f30c2..ed2126ac 100644 --- a/sdk-go/service_test.go +++ b/sdk-go/service_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/inferablehq/inferable-go/internal/util" + "github.com/inferablehq/inferable/sdk-go/internal/util" ) func TestRegisterFunc(t *testing.T) {