iwinux on Discord (#feedback) wanted magpie's image generation, image recognition and web search without going through an agent's MCP, and had written a Python script for generate_image. magpie now has it: magpie tools [<name>] magpie tool <name> [--<param> <value>…] [<words>…] [-o <file|folder>] [--json] Both ask the running gateway's own MCP servers (gateway.OwnMCPServers: image, vision, web-search, plugin tools) over HTTP, with the local token and the magpie-tool/1 User-Agent, so the tools, schemas, models from Settings and usage counting are the ones an agent gets. Nothing is started besides (#3318: the cases sit before startUp, beside mcp). - Parameters are read against the tool's inputSchema: types (integer, number, boolean, array given once per item, object as JSON), enum, - and _ alike, --p v or --p=v. The words left over are the tool's one required text, else its prompt. An unknown flag or parameter, a missing required one, a bad value, a value given twice or both words and the same --param are refused with "nothing was called"; no request is sent. - A file path a parameter takes (its description says file and path) is made absolute, since the gateway doesn't know the current folder. - --help prints toolUsage (static): help is handled before anything runs. - A still-being-made answer is waited on with generation_result (1s between asks, 30 min at most). The gateway's image server now says so in _meta too: usemagpie.ai/pending is the id, usemagpie.ai/files the files made ({url, inline}); an older gateway is read from its text. - Images and videos are saved through imagemcp.Target (now exported): -o file or folder, else the current folder named from the prompt, numbered when more than one, never over a file that is there; a file not inline is downloaded by its path from the gateway asked (the token goes nowhere else). --json prints the raw answer and saves nothing. - No magpie at the address: "magpie isn't running at <url>: open the app or run magpie serve". MAGPIE_GATEWAY=<url> points at another. Docs: usage lines in magpie help, docs/reference.md (Use), the gateway routing subsystem page (_meta and the CLI), README en/zh, site docs start page en/zh/ja. Tests (internal/cli/tool_cli_test.go, never the network; every test sets MAGPIE_GATEWAY to its own httptest server): - TestToolsListsTheServersTools, TestToolReadsParameters, TestToolRefusesWhatItDoesntKnowAndCallsNothing (no tools/call, no file), TestToolHelpCallsNothing (no request at all), TestToolSavesTheImage, TestToolWaitsOnWhatIsStillMade (pending _meta, then the old text form, download with the key from the gateway asked, not the URL's host), TestToolErrorIsSaid, TestToolSaysMagpieIsntRunning, TestToolDrawsThroughTheGateway (the real gateway.New().Handler() with a fake vendor: lists, draws, saves the vendor's PNG). - TestImageMCPDrawsAndServesTheImage, ...SlowDrawingIsWaitedOn and ...MakesVideos now check the _meta. Red on the old behaviour: with withMeta setting nothing (what main answers) all three fail ("_meta map[], want pending img_…"). - Broken by hand: dropping the unknown-parameter check makes TestToolRefuses… red (the --model call was made); saving an inline image by its URL instead makes TestToolSavesTheImage red. Both restored. - TestHelpOnEveryCommandDoesNothing covers tool and tools. Run with this change on origin/main630fd1d69, then rebased oncebd0d4c3(internal/pii, internal/gui, docs and site/ changed between, nothing this change touches): there gofmt, go vet of cli/gateway/imagemcp, internal/cli, internal/imagemcp and the gateway's TestImageMCP/TestVisionMCP/TestSearchMCP again, ok. At630fd1d69: - gofmt -l: nothing. go vet ./...; GOOS=linux go vet -tags nogui ./internal/...; GOOS=windows go vet ./internal/...: clean. - go test ./... -timeout 45m: internal/gateway, imagemcp and the rest ok; ten packages failed with "no space left on device" (the machine's disk was full from other runs). Those rerun when space was back: agent, cli, davsync, gui, library, sessions, settings, tui, usage, usageapi, wslrun, plugins/... ok. internal/provider crashes in TestUnreadLoginsWriteErrorsKeepCredentials with "close of synctest channel from outside bubble" (filememo.Forget), twice: that is #3632, open, on main since091153bf7, nothing this change touches. - Real try: a nogui build of this head as magpie serve on 127.0.0.1:3482 under a scratch HOME, a custom provider pointed at a local fake images API. magpie tools listed image, vision and web-search with their parameters; generate_image "…" --size 1024x1024 -o hero.png saved a PNG and the vendor got prompt and size; --n 2 --quality high saved two numbered files; --model was refused before calling; analyze_image and web_search said what to set up (no vision model or search API there); --help asked nothing. Usage counted the calls as magpie's own. Not tried: a real image vendor, a real vision or search model, a video, a plugin's tool through magpie tool, Windows and Linux runs (vet only).
170 lines
4.8 KiB
Go
170 lines
4.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
)
|
|
|
|
// helpAsked is whether args ask for help rather than for the command to
|
|
// run: -h or --help anywhere, or help as the word after the command. It
|
|
// is looked at before run does anything, since a command that took
|
|
// --help for a value acted on it: `magpie snow --help` set Snow's model
|
|
// to "--help", which took magpie out of it (#1457), and `magpie serve
|
|
// --help` started a gateway.
|
|
func helpAsked(args []string) bool {
|
|
// what magpie is started with by others (Claude Code's MCP helper, the
|
|
// Agents page's dry run, a link, Windows' -Embedding) never carries
|
|
// an exact -h, --help or help
|
|
for _, a := range args {
|
|
if a == "-h" || a == "--help" {
|
|
return true
|
|
}
|
|
}
|
|
return len(args) > 1 && args[1] == "help"
|
|
}
|
|
|
|
// helpFor is what `magpie <command> --help` prints: the command's own
|
|
// usage where it has one, its lines of magpie's usage otherwise, an
|
|
// agent's forms and fields for an agent, and the whole usage for
|
|
// anything else.
|
|
func helpFor(args []string) string {
|
|
cmd := args[0]
|
|
switch cmd {
|
|
case "-h", "--help", "help":
|
|
return usage
|
|
case "model":
|
|
if len(args) > 1 && (args[1] == "spec" || args[1] == "specs") {
|
|
return specUsage
|
|
}
|
|
return modelUsage
|
|
case "group", "groups":
|
|
if len(args) > 1 && (args[1] == "rule" || args[1] == "rules") {
|
|
return ruleUsage
|
|
}
|
|
return groupUsage
|
|
case "library", "lib":
|
|
return " " + libraryUsage
|
|
case "plugin", "plugins":
|
|
return pluginUsage
|
|
case "quota", "quotas":
|
|
return quotaUsage
|
|
case "statusline":
|
|
return statuslineUsage
|
|
case "heap":
|
|
return heapUsage
|
|
case "cpu":
|
|
return cpuUsage
|
|
case "goroutines":
|
|
return goroutinesUsage
|
|
case "sessions":
|
|
return sessionsUsage
|
|
case "search":
|
|
return searchUsage
|
|
case "tool", "tools":
|
|
return toolUsage
|
|
case "visible":
|
|
return visibleUsage
|
|
case "webdav", "dav":
|
|
return webdavUsage
|
|
case "s3":
|
|
return s3Usage
|
|
case "github":
|
|
return githubUsage
|
|
case "claude-code":
|
|
return claudeCodeUsage
|
|
case "list":
|
|
cmd = "ls"
|
|
case "account":
|
|
cmd = "accounts"
|
|
case "app", "gui":
|
|
cmd = ""
|
|
}
|
|
if cmd != "" && !strings.HasPrefix(cmd, "-") {
|
|
if lines := usageLines(cmd); lines != "" {
|
|
return lines
|
|
}
|
|
}
|
|
if a, err := agent.Find(cmd); err == nil && cmd != "" {
|
|
return agentHelp(a)
|
|
}
|
|
return usage
|
|
}
|
|
|
|
// usageLines are the lines of magpie's usage for `magpie <cmd>`, with the
|
|
// lines that carry on from them.
|
|
func usageLines(cmd string) string {
|
|
var b strings.Builder
|
|
in := false
|
|
for _, l := range strings.Split(usage, "\n") {
|
|
if rest, ok := strings.CutPrefix(l, " magpie "); ok {
|
|
in = rest == cmd || strings.HasPrefix(rest, cmd+" ")
|
|
} else if !strings.HasPrefix(l, " ") {
|
|
in = false
|
|
}
|
|
if in {
|
|
b.WriteString(l + "\n")
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// agentHelp is `magpie <agent> --help`: the forms that set it, and its
|
|
// fields.
|
|
func agentHelp(a *agent.Agent) string {
|
|
var b strings.Builder
|
|
id := a.ID
|
|
fmt.Fprintf(&b, "%s\n\n", a.Name)
|
|
fmt.Fprintf(&b, " magpie %s show it\n", id)
|
|
if len(a.Fields) == 0 {
|
|
return b.String()
|
|
}
|
|
fmt.Fprintf(&b, " magpie %s <model> set its model (magpie models %s lists them)\n", id, id)
|
|
fmt.Fprintf(&b, " magpie %s <field> show one field\n", id)
|
|
fmt.Fprintf(&b, " magpie %s <field> <value> set another field\n", id)
|
|
fmt.Fprintf(&b, " magpie %s default take magpie out: back on what it had before\n", id)
|
|
fmt.Fprintf(&b, " magpie %s <field> default that field back to its own default\n", id)
|
|
var keys []string
|
|
for _, f := range a.Fields {
|
|
keys = append(keys, f.Key)
|
|
}
|
|
fmt.Fprintf(&b, "\nfields: %s\n", strings.Join(keys, ", "))
|
|
// Claude Code's tiers are named as its model's aliases are
|
|
var both []string
|
|
for _, f := range a.Fields {
|
|
if slices.Contains(a.ModelAliases, f.Key) {
|
|
both = append(both, f.Key)
|
|
}
|
|
}
|
|
if n := len(both); n > 0 {
|
|
names := both[0]
|
|
if n > 1 {
|
|
names = strings.Join(both[:n-1], ", ") + " and " + both[n-1]
|
|
}
|
|
fmt.Fprintf(&b, " %s alone name the %s: magpie %s %s sets its %s to %s\n", names, a.Fields[0].Label, id, both[0], a.Fields[0].Label, both[0])
|
|
// they are the agent's own models, not magpie's: setting one takes
|
|
// magpie out (#2000, read as a query of the tier)
|
|
fmt.Fprintf(&b, " they are %s's own models, not magpie's: setting one takes magpie out; magpie %s alone shows each\n", a.Name, id)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// flagArg is an argument to `magpie <agent> …` that is a flag, which no
|
|
// agent form takes: it is said to be one, rather than set as a model or a
|
|
// field's value. A negative number is a value.
|
|
func flagArg(args []string) string {
|
|
for _, a := range args {
|
|
if !strings.HasPrefix(a, "-") {
|
|
continue
|
|
}
|
|
if _, err := strconv.ParseFloat(a, 64); err == nil {
|
|
continue
|
|
}
|
|
return a
|
|
}
|
|
return ""
|
|
}
|