-
Notifications
You must be signed in to change notification settings - Fork 102
Add dmr dev convenience wrapper #773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ericcurtin
wants to merge
1
commit into
main
Choose a base branch
from
conveinience
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−41
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // dmr is a developer convenience wrapper that starts the model-runner server on | ||
| // a free port and runs a model-cli command against it in one step. | ||
| // | ||
| // Usage: dmr <cli-args...> | ||
| // | ||
| // Example: dmr run qwen3:0.6B-Q4_0 tell me today's news | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "strconv" | ||
| "syscall" | ||
| "time" | ||
| ) | ||
|
|
||
| func freePort() (int, error) { | ||
| l, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| defer l.Close() | ||
| return l.Addr().(*net.TCPAddr).Port, nil | ||
| } | ||
|
|
||
| func waitForServer(url string, timeout time.Duration) error { | ||
| client := &http.Client{Timeout: time.Second} | ||
| deadline := time.Now().Add(timeout) | ||
| for time.Now().Before(deadline) { | ||
| resp, err := client.Get(url) | ||
| if err == nil { | ||
| resp.Body.Close() | ||
| if resp.StatusCode == http.StatusOK { | ||
| return nil | ||
| } | ||
| } | ||
| time.Sleep(200 * time.Millisecond) | ||
| } | ||
| return fmt.Errorf("server not ready after %s", timeout) | ||
| } | ||
|
|
||
| func checkBinary(path, name, expectedLayout string) error { | ||
| if _, err := os.Stat(path); os.IsNotExist(err) { | ||
| return fmt.Errorf("missing %s binary at %s\n\nExpected directory layout:\n%s\n\nPlease run 'make build' to build all binaries", name, path, expectedLayout) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func main() { | ||
| self, err := os.Executable() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "dmr: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| dir := filepath.Dir(self) | ||
|
|
||
| serverBin := filepath.Join(dir, "model-runner") | ||
| cliBin := filepath.Join(dir, "cmd", "cli", "model-cli") | ||
|
|
||
| expectedLayout := fmt.Sprintf(`%s/ | ||
| ├── model-runner (server binary) | ||
| ├── dmr (this wrapper) | ||
| └── cmd/ | ||
| └── cli/ | ||
| └── model-cli (CLI binary)`, dir) | ||
|
|
||
| if err := checkBinary(serverBin, "model-runner", expectedLayout); err != nil { | ||
| fmt.Fprintf(os.Stderr, "dmr: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| if err := checkBinary(cliBin, "model-cli", expectedLayout); err != nil { | ||
| fmt.Fprintf(os.Stderr, "dmr: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| port, err := freePort() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "dmr: failed to find free port: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| portStr := strconv.Itoa(port) | ||
| serverURL := "http://localhost:" + portStr | ||
|
|
||
| fmt.Fprintf(os.Stderr, "dmr: starting model-runner on port %d\n", port) | ||
|
|
||
| server := exec.Command(serverBin) | ||
| server.Env = append(os.Environ(), "MODEL_RUNNER_PORT="+portStr) | ||
| server.Stderr = os.Stderr | ||
| server.Stdout = os.Stdout | ||
|
|
||
| if err := server.Start(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "dmr: failed to start model-runner: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer server.Process.Kill() | ||
|
|
||
| sigCh := make(chan os.Signal, 1) | ||
| signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) | ||
| go func() { | ||
| <-sigCh | ||
| server.Process.Kill() | ||
| }() | ||
|
|
||
| if err := waitForServer(serverURL+"/", 30*time.Second); err != nil { | ||
| fmt.Fprintf(os.Stderr, "dmr: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // #nosec G702 - Intentional: dmr is a CLI wrapper that forwards arguments to model-cli | ||
| cli := exec.Command(cliBin, os.Args[1:]...) | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cli.Env = append(os.Environ(), "MODEL_RUNNER_HOST="+serverURL) | ||
| cli.Stdin = os.Stdin | ||
| cli.Stdout = os.Stdout | ||
| cli.Stderr = os.Stderr | ||
|
|
||
| if err := cli.Run(); err != nil { | ||
| var exitErr *exec.ExitError | ||
| if errors.As(err, &exitErr) { | ||
| os.Exit(exitErr.ExitCode()) | ||
| } | ||
| fmt.Fprintf(os.Stderr, "dmr: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.