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
1 change: 1 addition & 0 deletions api/cmd/helix/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewRootCmd() *cobra.Command {
}
RootCmd.AddCommand(newServeCmd())
RootCmd.AddCommand(newRunnerCmd())
RootCmd.AddCommand(newRunCmd())
return RootCmd
}

Expand Down
109 changes: 109 additions & 0 deletions api/cmd/helix/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package helix

import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
"os/signal"
"time"

"github.com/lukemarsden/helix/api/pkg/system"
"github.com/lukemarsden/helix/api/pkg/types"
"github.com/spf13/cobra"
)

type RunOptions struct {
RunnerUrl string
}

func NewRunOptions() *RunOptions {
return &RunOptions{
RunnerUrl: getDefaultServeOptionString("RUNNER_URL", "http://localhost:8080"),
}
}

func newRunCmd() *cobra.Command {
allOptions := NewRunOptions()

runnerCmd := &cobra.Command{
Use: "run",
Short: "Run a task directly on a helix runner.",
Long: "Run a task directly on a helix runner.",
Example: "TBD",
RunE: func(cmd *cobra.Command, _ []string) error {
return runCLI(cmd, allOptions)
},
}

runnerCmd.PersistentFlags().StringVar(
&allOptions.RunnerUrl, "api-host", allOptions.RunnerUrl,
`The base URL of the runner - e.g. http://localhost:8080`,
)

return runnerCmd
}

func runCLI(cmd *cobra.Command, options *RunOptions) error {
system.SetupLogging()

// Cleanup manager ensures that resources are freed before exiting:
cm := system.NewCleanupManager()
defer cm.Cleanup(cmd.Context())
ctx := cmd.Context()

// Context ensures main goroutine waits until killed with ctrl+c:
_, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()

interaction := types.Interaction{
ID: "cli-intx",
Created: time.Now(),
Creator: "user",
Runner: "",
Message: "a unicorn riding a horse",
Progress: 0,
Files: []string{},
Finished: true,
Metadata: map[string]string{},
Error: "",
}

session := types.Session{
ID: "cli",
Name: "cli",
Created: time.Now(),
Updated: time.Now(),
Mode: "inference",
Type: "image",
ModelName: "stabilityai/stable-diffusion-xl-base-1.0",
FinetuneFile: "",
Interactions: []types.Interaction{interaction},
Owner: "cli-user",
OwnerType: "user",
}

bs, err := json.Marshal(session)
if err != nil {
return err
}

req, err := http.NewRequest("POST", options.RunnerUrl+"/api/v1/worker/session", bytes.NewBuffer(bs))
if err != nil {
return err
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

log.Printf("Response: %+v", resp)

// TODO: poll /worker/state, updating the CLI with the result

return nil
}
Loading