Open Code Review (github.com/alibaba/open-code-review, npm @alibaba-group/open-code-review) is a Go CLI, `ocr`, that reviews a diff with a model. Its Claude Code, Codex, Cursor, Kimi Code and OpenCode plugins and its skills all run that command, which reads ~/.opencodereview/config.json on every run. Picking one of magpie's models on its row writes a custom provider "magpie" there (protocol openai, the gateway's /v1, a key naming Open Code Review, the model) and points provider at it. The user's own provider and model are stashed and come back on Disconnect, which also drops a custom_providers left empty. A model of the user's own can be picked too (written the way `ocr config model` writes it). Check reports a hand-edited url or key, or a removed entry. The `ocr` command counts only under a folder named for Open Code Review, since other programs share the name. WSL distros get the row too (cold distros not opened). Its User-Agent, open-code-review/<version>, already counts as this agent. CLI update via npm. Docs, READMEs, site and icon (OCR's own logo, Apache-2.0). Run (mica, at 593517bf0; origin/main has one unrelated usage commit since): - go test ./internal/agent -run 'TestOCR|WSL|Wsl|TestOwnProviderNamedAsMagpies|TestHandWiredMagpieProviderSiblings|TestAgentOf|TestEveryAgentNamedByItsKey|TestMarkedLinesEveryAgent': ok - gofmt -l: clean; GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: ok - Broken by hand: url dropped from Check -> TestOCRWiring red; stash turned off -> red at the Disconnect restore. TestWSLColdPlaceOpensNothing went red before the row's options were given an asleep path. - Real e2e in a sandbox HOME: npm-installed ocr v1.13.10 (7e5ce06c) darwin/arm64, magpie built from this tree, a fake OpenAI upstream that calls tools. `magpie agents` lists the row with the user's deepseek/deepseek-chat; `magpie open-code-review magpie/fake/m1` wrote the entry and kept the user's keys and settings; `ocr llm test`: connection ok, tool-call round trip verified; `ocr review` on a repo with a nil dereference: 2 findings "[bug · high]" through magpie (8 /v1/chat/completions calls, UA open-code-review/v1.13.10, model m1, its six review tools); `magpie usage` counts them under Open Code Review; `magpie open-code-review default` put deepseek/deepseek-chat back and removed custom_providers. - Full suite: not run before this push (ship at once; mote was running another session's suite). It runs on mica right after, at this head. Not tried: a real vendor model behind magpie; WSL and Windows/Linux machines for real; the Claude Code / OpenCode plugin path itself (it runs the same `ocr`); an ocr installed by the curl script outside a folder named for it is found by ~/.opencodereview only.
250 lines
7.8 KiB
Go
250 lines
7.8 KiB
Go
package agent
|
|
|
|
// Open Code Review (github.com/alibaba/open-code-review, npm
|
|
// @alibaba-group/open-code-review) is a Go CLI, `ocr`, that reviews a
|
|
// diff with a model; its plugins for Claude Code, Codex, Cursor, Kimi Code
|
|
// and OpenCode, and its skills, all run that same command. It keeps its
|
|
// settings in ~/.opencodereview/config.json (os.UserHomeDir, no variable
|
|
// moves it): `provider` names the entry in use, from `providers` (its
|
|
// presets: anthropic, openai, deepseek, …) or `custom_providers` (any
|
|
// other name), and `model` the model, the entry's own `model` winning
|
|
// (internal/llm/resolver.go tryProviderConfig). A custom entry needs a
|
|
// protocol, a url and a key of its own: no variable stands in for it.
|
|
//
|
|
// magpie adds the custom provider "magpie": the openai protocol, which
|
|
// posts Chat Completions to url + "/chat/completions", the gateway's /v1,
|
|
// a key naming Open Code Review, and the model picked, which goes to the
|
|
// gateway as it is, for it to resolve. provider is pointed at it; the
|
|
// provider and model the user had are stashed and put back when magpie
|
|
// steps out. Requests carry a User-Agent of open-code-review/<version>,
|
|
// which the gateway counts as this agent's. ocr reads the file on every
|
|
// run, so a change needs no restart.
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
const ocrID = "open-code-review"
|
|
|
|
// ocrEntry is the key path of magpie's custom provider in config.json.
|
|
var ocrEntry = "custom_providers." + magpieID
|
|
|
|
func ocr(home string) *Agent { return ocrIn(here(home)) }
|
|
|
|
// ocrIn is Open Code Review at a place.
|
|
func ocrIn(at place) *Agent {
|
|
dir := at.join(".opencodereview")
|
|
path := filepath.Join(dir, "config.json")
|
|
key := ocrID + ":" + path
|
|
get := func(k string) string { v, _ := edit.GetJSON(path, k); return v }
|
|
// section is where OCR keeps the entry of a provider of the user's
|
|
// own: "providers" or "custom_providers"; "" when it has none.
|
|
section := func(p string) string {
|
|
for _, s := range []string{"providers", "custom_providers"} {
|
|
if r, ok := edit.GetJSONRaw(path, s+"."+gjson.Escape(p)); ok && strings.HasPrefix(strings.TrimSpace(r), "{") {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
// model is the model OCR sends for provider p: its entry's, which
|
|
// wins, else the top-level one.
|
|
model := func(p string) string {
|
|
if s := section(p); s != "" {
|
|
if m := get(s + "." + gjson.Escape(p) + ".model"); m != "" {
|
|
return m
|
|
}
|
|
}
|
|
return get("model")
|
|
}
|
|
cur := func() string {
|
|
p := get("provider")
|
|
if p == "" {
|
|
return ""
|
|
}
|
|
m := model(p)
|
|
if m == "" {
|
|
return p
|
|
}
|
|
return p + "/" + m
|
|
}
|
|
writeMagpie := func(ref string) error {
|
|
kvs := []edit.KV{
|
|
{Path: ocrEntry + ".protocol", Value: "openai"},
|
|
{Path: ocrEntry + ".url", Value: at.v1()},
|
|
{Path: ocrEntry + ".api_key", Value: agentKeyAt(ocrID, at.gw())},
|
|
}
|
|
if ref != "" {
|
|
kvs = append(kvs, edit.KV{Path: ocrEntry + ".model", Value: ref})
|
|
}
|
|
return edit.SetJSON(path, kvs...)
|
|
}
|
|
// setOwn makes provider/model of the user's own the one in use, as
|
|
// `ocr config model` does: the top-level model and the entry's.
|
|
setOwn := func(v string) error {
|
|
p, m, _ := strings.Cut(v, "/")
|
|
s := section(p)
|
|
if p == magpieID || s == "" {
|
|
return fmt.Errorf("Open Code Review has no provider %q set up; add it with `ocr config provider` first", p)
|
|
}
|
|
kvs := []edit.KV{{Path: "provider", Value: p}}
|
|
if m != "" {
|
|
kvs = append(kvs, edit.KV{Path: "model", Value: m}, edit.KV{Path: s + "." + gjson.Escape(p) + ".model", Value: m})
|
|
}
|
|
return edit.SetJSON(path, kvs...)
|
|
}
|
|
leave := func() error {
|
|
forget(key+":provider", key+":model")
|
|
if err := edit.DelJSON(path, ocrEntry); err != nil {
|
|
return err
|
|
}
|
|
// custom_providers made for magpie's alone goes with it
|
|
if r, ok := edit.GetJSONRaw(path, "custom_providers"); ok && strings.Join(strings.Fields(r), "") == "{}" {
|
|
return edit.DelJSON(path, "custom_providers")
|
|
}
|
|
return nil
|
|
}
|
|
return atomic(&Agent{
|
|
ID: ocrID, Name: "Open Code Review", Icon: ocrID, Spelled: prefixed, NoNames: true,
|
|
UA: []string{ocrID},
|
|
// `ocr` is other programs' name too: only one under a folder
|
|
// named for it (npm's) is taken for it; one the install script put
|
|
// in /usr/local/bin is known by its folder
|
|
Bin: "ocr", binNot: ocrNotOurs,
|
|
Dir: dir, Path: path,
|
|
Sync: func() error {
|
|
if _, ok := edit.GetJSONRaw(path, ocrEntry); !ok {
|
|
return nil
|
|
}
|
|
return writeMagpie("")
|
|
},
|
|
Check: func() string {
|
|
if get("provider") != magpieID {
|
|
return ""
|
|
}
|
|
if _, ok := edit.GetJSONRaw(path, ocrEntry); !ok {
|
|
return "Open Code Review's " + ocrEntry + " (config.json) is gone, so it no longer reaches magpie"
|
|
}
|
|
return wiringOff("Open Code Review", path, func(f string) (string, bool) { return edit.GetJSON(path, ocrEntry+"."+f) },
|
|
"url", at.v1(), "api_key", agentKeyAt(ocrID, at.gw()))
|
|
},
|
|
Fields: []Field{{
|
|
Key: "model", Label: "model",
|
|
Get: func() string {
|
|
if get("provider") != magpieID {
|
|
return cur()
|
|
}
|
|
m := get(ocrEntry + ".model")
|
|
if m == "" {
|
|
m = get("model")
|
|
}
|
|
return magpieID + "/" + m
|
|
},
|
|
Set: func(v string) error {
|
|
onMagpie := get("provider") == magpieID
|
|
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
|
|
if !onMagpie {
|
|
stash(map[string]string{
|
|
key + ":provider": get("provider"),
|
|
key + ":model": get("model"),
|
|
})
|
|
}
|
|
if err := writeMagpie(ref); err != nil {
|
|
return err
|
|
}
|
|
return edit.SetJSON(path, edit.KV{Path: "provider", Value: magpieID})
|
|
}
|
|
if v != "" {
|
|
if err := setOwn(v); err != nil {
|
|
return err
|
|
}
|
|
if onMagpie {
|
|
return leave()
|
|
}
|
|
return nil
|
|
}
|
|
if !onMagpie {
|
|
return edit.DelJSON(path, "provider", "model")
|
|
}
|
|
// magpie was in use: the provider and model the user had
|
|
// come back, where OCR still has the provider
|
|
saved := stashLoad()
|
|
p, m := saved[key+":provider"], saved[key+":model"]
|
|
if p != "" && p != magpieID && section(p) != "" {
|
|
if err := edit.SetJSON(path, edit.KV{Path: "provider", Value: p}); err != nil {
|
|
return err
|
|
}
|
|
} else if err := edit.DelJSON(path, "provider"); err != nil {
|
|
return err
|
|
}
|
|
if m != "" {
|
|
if err := edit.SetJSON(path, edit.KV{Path: "model", Value: m}); err != nil {
|
|
return err
|
|
}
|
|
} else if err := edit.DelJSON(path, "model"); err != nil {
|
|
return err
|
|
}
|
|
return leave()
|
|
},
|
|
Options: func(c map[string]string) []Option {
|
|
return withVia(ocrOwnModels(path, c["model"]), ocrID, magpieID+"/")
|
|
},
|
|
}},
|
|
})
|
|
}
|
|
|
|
// ocrNotOurs turns away an `ocr` command that isn't Open Code Review's:
|
|
// one under no folder named for it.
|
|
func ocrNotOurs(bin string) bool {
|
|
for _, p := range []string{bin, evalLink(bin)} {
|
|
s := strings.ToLower(filepath.ToSlash(p))
|
|
if strings.Contains(s, "open-code-review") || strings.Contains(s, "opencodereview") {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ocrOwnModels is the provider/model pairs OCR's config.json sets up of
|
|
// the user's own: each entry's model and models, and the one in use.
|
|
func ocrOwnModels(path, cur string) []Option {
|
|
seen := map[string]bool{}
|
|
var vals []string
|
|
add := func(v string) {
|
|
if v == "" || seen[v] || strings.HasPrefix(v, magpieID+"/") || v == magpieID {
|
|
return
|
|
}
|
|
seen[v] = true
|
|
vals = append(vals, v)
|
|
}
|
|
add(cur)
|
|
if raw, err := edit.Read(path); err == nil {
|
|
for _, s := range []string{"providers", "custom_providers"} {
|
|
gjson.GetBytes(raw, gjson.Escape(s)).ForEach(func(k, e gjson.Result) bool {
|
|
p := k.String()
|
|
if p == magpieID || !e.IsObject() {
|
|
return true
|
|
}
|
|
if m := e.Get("model").String(); m != "" {
|
|
add(p + "/" + m)
|
|
}
|
|
for _, m := range e.Get("models").Array() {
|
|
if m.String() != "" {
|
|
add(p + "/" + m.String())
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
sort.Strings(vals)
|
|
return static(vals...)
|
|
}
|