Files
yelo 25488a7c8f opencodereview 0.1.0: Open Code Review (ocr) as an agent plugin (#463)
New agent plugin @magpie-community/agent-opencodereview 0.1.0 (imyelo): Open Code Review (Alibaba's `ocr` CLI) on magpie's Agents page. connect writes `provider: "magpie"` and one `custom_providers.magpie` object (gateway base url, protocol anthropic, magpie's key for ocr) into ~/.opencodereview/config.json; magpie writes `model`. Registered in registry.json and the README's Agents table.

Reviewed at 3ea944df15c566aabf2235a147da868ea1e64f02.

Run at that sha (on mote, macOS arm64, Bun 1.4.2):
- `HOME=$(mktemp -d) bun test packages/opencodereview`: 8 pass. `bun scripts/check.mjs opencodereview`: ok (it skips agent packages). Full `bun test`: 0 fail, both at the sha and merged with main d8cfee3 (3050 tests).
- Broken by hand, each went red: url = gateway.v1 (3 fail), protocol openai (2 fail), provider anthropic (3 fail).
- magpie's real engine (scratch internal/agent test at magpie ff6fdffd6, not committed) loading this agent.js: connect then Unwire on a fresh home (leaves `{"custom_providers":{}}`, as the README says), on a config with a pre-existing custom_providers.magpie holding extra keys plus other providers/mcp_servers (restored exactly), and on another active custom provider (restored exactly).
- End to end: ocr v1.13.3 built from source, `ocr llm test` against magpie's real gateway (in-process, sandbox HOME) with a fake OpenAI-compatible upstream: "Source: provider:magpie", URL <gateway>/v1/messages, "Connection test successful", "Tool-call round trip verified"; upstream got both turns.
- Checked against ocr's source (internal/llm/resolver.go): custom_providers keys, `magpie` is not a preset name, anthropic protocol appends /v1/messages, UA is "open-code-review/<ver>", config path is ~/.opencodereview/config.json. Nothing goes upstream naming magpie; the plugin sends no data and downloads nothing.

Not tried: a real vendor account behind the gateway, Linux and Windows, ocr review/scan on a real repo.
2026-10-28 23:07:35 +08:00

104 lines
3.6 KiB
JavaScript

import { expect, test } from "bun:test"
import { agent, connect } from "./opencodereview.agent.js"
const gateway = { url: "http://127.0.0.1:3425", v1: "http://127.0.0.1:3425/v1", key: "magpie-opencodereview" }
const model = { id: "zai/glm-5.3", name: "GLM-5.3" }
// Apply key-paths the way magpie edits a JSON config: dotted paths address
// nested keys, missing intermediates are created, other keys are preserved.
function apply(config, entries) {
const out = structuredClone(config)
for (const [path, value] of Object.entries(entries)) {
const parts = path.split(".")
let node = out
for (const part of parts.slice(0, -1)) {
if (typeof node[part] !== "object" || node[part] === null) node[part] = {}
node = node[part]
}
node[parts[parts.length - 1]] = value
}
return out
}
test("says where ocr keeps its model", () => {
expect(agent).toMatchObject({
id: "opencodereview",
bin: "ocr",
dir: "~/.opencodereview",
config: "~/.opencodereview/config.json",
format: "json",
model: "model",
})
// ocr sends magpie's model id as is
expect(agent.prefix).toBeUndefined()
})
test("shows ocr's own icon, as the package gives it", async () => {
const pkg = await Bun.file(new URL("./package.json", import.meta.url)).json()
expect(agent.icon).toBe("https://raw.githubusercontent.com/alibaba/open-code-review/main/imgs/logo.svg")
expect(pkg.magpie.icon).toBe(agent.icon)
})
test("matches ocr's User-Agent, which magpie reads as lowercase prefixes", () => {
const real = "open-code-review/v1.13.2 | claude"
expect(agent.ua.length).toBeGreaterThan(0)
for (const entry of agent.ua) {
expect(entry).toBe(entry.toLowerCase())
expect(real.startsWith(entry)).toBe(true)
}
expect(agent.ua).toContain("open-code-review")
})
test("tells the user when a new model takes effect, in English and Chinese", () => {
expect(agent.notice).toContain("the next ocr review / ocr scan uses the new model")
expect(agent.notice).toContain("下一次 ocr review / ocr scan 即使用新模型")
})
test("points ocr at magpie's anthropic endpoint with its own key", () => {
const out = connect({ gateway, model })
expect(out).toEqual({
provider: "magpie",
"custom_providers.magpie": {
url: "http://127.0.0.1:3425",
protocol: "anthropic",
api_key: "magpie-opencodereview",
},
})
// no /v1 suffix: ocr's anthropic protocol appends /v1/messages itself
expect(out["custom_providers.magpie"].url).not.toContain("/v1")
// one unit, as pig writes its provider: no empty magpie provider is left
// behind when disconnect takes the key out
expect(Object.keys(out)).toEqual(["provider", "custom_providers.magpie"])
})
test("yields a config ocr accepts, leaving other providers alone", () => {
const existing = {
provider: "local",
model: "zai/glm-5.3",
custom_providers: {
local: { url: "http://127.0.0.1:3425", protocol: "anthropic", api_key: "x" },
},
llm: {},
}
// magpie writes the model key itself, connect's keys alongside it
const out = apply(apply(existing, { model: model.id }), connect({ gateway, model }))
expect(out).toEqual({
provider: "magpie",
model: "zai/glm-5.3",
custom_providers: {
local: { url: "http://127.0.0.1:3425", protocol: "anthropic", api_key: "x" },
magpie: { url: gateway.url, protocol: "anthropic", api_key: gateway.key },
},
llm: {},
})
})
test("creates custom_providers on a fresh config", () => {
const out = apply({}, connect({ gateway, model }))
expect(out.custom_providers.magpie.url).toBe(gateway.url)
})
test("does not depend on receiving a model", () => {
expect(connect({ gateway }).provider).toBe("magpie")
})