diff --git a/README.md b/README.md index 90d4f02..89c16e7 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ it off puts the config back. | Package | Agent | |---|---| | [aider](packages/aider) | [Aider](https://aider.chat): `~/.aider.conf.yml`, through litellm's `openai/` prefix | +| [pig](packages/pig) | [PiG](https://github.com/MichaelKinsy/PiG): injects Magpie's endpoint and model catalog into `~/.pig/agent/models.json`; choose the model in PiG, startup settings stay unchanged | | [kimi-desktop](packages/kimi-desktop) | Ordinary [Kimi desktop](https://www.kimi.com/) Work, through its authenticated local runtime; not Kimi Code CLI | | [type4me](packages/type4me) | [Type4Me](https://github.com/joewongjc/type4me), macOS voice input: `~/Library/Application Support/Type4Me/magpie-agent.json`, for its LLM post-processing | diff --git a/packages/pig/README.md b/packages/pig/README.md new file mode 100644 index 0000000..15cde3d --- /dev/null +++ b/packages/pig/README.md @@ -0,0 +1,83 @@ +# agent-pig + +`@magpie-community/agent-pig` adds [PiG (Pi in Go)](https://github.com/MichaelKinsy/PiG) to Magpie as a **connect-only agent**. It supplies Magpie's endpoint and model catalog in `~/.pig/agent/models.json`. You choose the model in PiG. The plugin never writes `settings.json`. + +The Agents row identifies PiG with the project's pixel-pig icon from its official site. The image is not bundled: Magpie references it by URL and keeps its own cache. This is an unofficial integration; PiG is a project by Michael Kinsy and is not affiliated with, or endorsing, Magpie. + +## Installation + +With a Magpie release that supports connect-only Agent plugins: + +```sh +magpie plugin add @magpie-community/agent-pig +``` + +1. Open Magpie's Agents page and turn on **PiG**. Its row is a provider switch, with no model picker. CLI equivalent: `magpie pig provider magpie`. +2. Start PiG, run `/model`, and select a model under the **Magpie** provider. Reopen `/model` after Magpie's catalog changes; restart PiG if your installed version does not reload it there. +3. Before disconnecting, choose a model from another provider in PiG. Turn off **PiG** in Magpie, or run `magpie pig default`. + +Saving a startup default is a separate PiG action: use PiG's own model picker to save it. The plugin does not set or restore defaults, thinking preferences, or scoped models. An existing `enabledModels` scope can restrict selection; change that scope yourself in PiG if necessary. Disconnecting cannot repair a saved default or resumed session that still names a Magpie model. + +Disconnect the PiG agent before disabling or uninstalling the plugin; do not rely on removing a plugin to run the agent's disconnect flow. + +## What it owns + +Magpie manages **one complete object**, `providers.magpie`, inside `models.json`: + +| Field inside the managed object | Value | +| --- | --- | +| `name` | `Magpie` | +| `baseUrl` | The gateway's supplied `/v1` address | +| `apiKey` | The key supplied by Magpie for PiG; normally `magpie-pig` on loopback | +| `api` | `openai-completions` | +| `models` | The catalog Magpie exposes to PiG | + +Magpie stashes the previous object and restores it on **agent disconnect**. If no `magpie` provider existed, the entire provider is removed. An empty top-level `providers` map or file can remain, but a provider object with no settings of its own must not. + +Other provider IDs, unrelated top-level keys, and document comments outside the managed object stay in place, including changes made while connected. A pre-existing provider with the same `magpie` ID is temporarily replaced and restored semantically on disconnect; its internal formatting/comments are not a byte-for-byte backup. While connected the entire `magpie` object, including personal overrides there, is managed and refreshed by Magpie. Keep concurrently used manual services under a different provider ID. + +The plugin does not change `auth.json` or PiG's credential precedence. A stored credential for the reserved `magpie` ID can take precedence over the injected key; use a distinct ID for an independently configured manual service. + +The plugin preserves catalog IDs (including slashes), names, image input capability, positive context/output limits, and supported Pi thinking levels. A known output limit is capped at the known context window, matching Magpie's Pi adapter. Unknown limits are omitted so PiG applies its own defaults. The public Agent hook does not provide a model's native protocol, prices, or a separate reasoning flag: all models use Magpie's documented Chat Completions endpoint, and reasoning is advertised when `efforts` is nonempty. Magpie handles upstream protocol translation. This is not the full native-protocol behavior of Magpie's built-in Pi adapter. + +It reads no credentials, installs no PiG extension, runs no daemon, and provides no Library integration. Requests use Magpie's supplied key, so usage can be attributed to `pig` without claiming Pi's User-Agent. + +## Configuration paths + +The shipped descriptor targets **the default PiG directory only**, `~/.pig/agent`. It does not automatically follow `PIG_CODING_AGENT_DIR`, `PIG_HOME`, `XDG_CONFIG_HOME`, or `PIG_USE_PI_DIRS=1`. The current public Agent contract offers one static config path (with `~`/`$VAR` expansion), not a conditional directory resolver. Do not turn this descriptor on when PiG uses a different directory. + +For a custom directory, make a local copy of `pig.agent.js`, change `agent.config` and `agent.dir` to the actual selected PiG paths, and install that file with `magpie plugin add /absolute/path/to/custom-pig.agent.js`. Install only one descriptor with id `pig`. Do not change PiG's directory layout for this plugin. In Pi sharing mode, editing a shared `models.json` also affects Pi; that is an explicit custom setup, not the default plugin behavior. + +## Platform support + +The same `pig.agent.js` was tested on native Linux and Windows with Magpie 0.1.1294 and PiG 0.5.1+1.3.0.1402973a using an external real-binary harness (not shipped in this package): 8 end-to-end tests and 3 lifecycle tests per platform. They cover fresh connect/disconnect/reconnect with a real PiG parse afterwards, a pre-existing manually configured API-key service used before, during and after injection, and restoration of a prior same-name provider. On Windows the default path resolves to `%USERPROFILE%\.pig\agent\models.json`, including when the test workspace and home paths contain spaces. + +Magpie owns native home expansion, path separators, command detection, file edits and restoration. The plugin has no platform branches or platform-specific dependencies. macOS and WSL have not been tested on real machines. + +## Development + +The runtime module is a single file, without imports, runtime dependencies or install hooks. `package.json` declares `magpie.agent` and no `main`, following the [community Agent convention](https://github.com/magpie-community/plugins#agents) and [public contract](https://usemagpie.ai/docs/plugins#agents). + +From `packages/pig` in a checkout of the community plugins repository, run the included unit tests with Bun: + +```sh +bun test pig.test.mjs +``` + +The repository's `scripts/check.mjs` skips Agent packages; `pig.test.mjs` checks this plugin's descriptor and `connect()` output. The published npm package excludes tests and research artifacts. + +## 中文 + +这是仅注入模型目录的 PiG 适配插件。通过 `magpie plugin add @magpie-community/agent-pig` 安装后,在 Magpie 的 Agent 页打开 **PiG**,再到 PiG 中运行 `/model` 选择 Magpie 模型。 + +插件只通过 Magpie 托管 `~/.pig/agent/models.json` 中完整的 `providers.magpie` 对象,不修改 `settings.json` 或 `auth.json`,也不替用户设置启动默认模型、思考等级或模型范围。断开时,原先有同名 provider 就恢复原对象,原先没有就移除整个 provider,避免空 provider 导致 PiG 解析失败。 + +其他 provider 和顶层设置不在托管范围内。若原先就手动配置了名为 `magpie` 的 provider,它在接入期间会被替换,断开后恢复;接入期间对该区块的手写改动会被同步覆盖。需要同时使用的手动服务应使用其他 provider ID。插件不会修改既有凭证优先级。 + +默认仅支持 `~/.pig/agent`。使用环境变量改过目录或开启 Pi 共享目录的用户,需要先明确实际路径,再安装改过路径的本地描述文件。不要直接启用默认描述文件。 + +断开前在 PiG 中换回其他 provider 的模型;先断开 PiG,再禁用或卸载插件。PiG 自行保存的默认模型和会话引用不由本插件恢复。 + +## License + +MIT. diff --git a/packages/pig/package.json b/packages/pig/package.json new file mode 100644 index 0000000..13a1898 --- /dev/null +++ b/packages/pig/package.json @@ -0,0 +1,18 @@ +{ + "name": "@magpie-community/agent-pig", + "version": "0.1.0", + "description": "PiG as a connect-only Magpie agent: inject endpoints and model catalog without changing startup settings", + "type": "module", + "files": ["pig.agent.js", "README.md"], + "keywords": ["magpie", "magpie-agent", "pig", "pi-in-go", "llm-gateway"], + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/magpie-community/plugins.git", + "directory": "packages/pig" + }, + "scripts": { + "test": "bun test pig.test.mjs" + }, + "magpie": { "agent": "./pig.agent.js", "icon": "https://pi-in-go.dev/pig-favicon.svg" } +} diff --git a/packages/pig/pig.agent.js b/packages/pig/pig.agent.js new file mode 100644 index 0000000..b76df99 --- /dev/null +++ b/packages/pig/pig.agent.js @@ -0,0 +1,53 @@ +// PiG reads providers from models.json and startup preferences from a separate +// settings.json. This connect-only agent owns the provider catalog alone. +export const agent = { + id: "pig", + name: "PiG", + // PiG's own pixel-pig favicon from its site (MIT), as the app shows it. + icon: "https://pi-in-go.dev/pig-favicon.svg", + bin: "pig", + dir: "~/.pig/agent", + config: "~/.pig/agent/models.json", + format: "json", + notice: "Choose a Magpie model with /model in PiG. Startup settings are unchanged. 在 PiG 中用 /model 选择模型;启动设置保持原样。", +} + +const thinkingLevels = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] + +function modelEntry(model) { + const efforts = model.efforts || [] + const entry = { + id: model.id, + name: model.name || model.id, + input: model.images ? ["text", "image"] : ["text"], + reasoning: efforts.length > 0, + } + // Unknown limits are left to PiG's defaults; zero is invalid in its schema. + if (model.context > 0) entry.contextWindow = model.context + // Match Magpie's Pi adapter: a published output limit cannot exceed its window. + if (model.output > 0) entry.maxTokens = model.context > 0 ? Math.min(model.output, model.context) : model.output + if (efforts.length > 0) { + entry.thinkingLevelMap = {} + for (const level of thinkingLevels) { + const effort = level === "off" ? "none" : level + entry.thinkingLevelMap[level] = efforts.includes(effort) ? effort : null + } + } + return entry +} + +export function connect({ gateway, models }) { + // Stash and restore the provider as one unit. Leaf-only writes leave an + // invalid empty provider after disconnect when no provider existed before. + // Other provider IDs remain outside this plugin's ownership. + return { + "providers.magpie": { + name: "Magpie", + baseUrl: gateway.v1, + apiKey: gateway.key, + // The public hook has no native-protocol field; Magpie translates Chat. + api: "openai-completions", + models: models.map(modelEntry), + }, + } +} diff --git a/packages/pig/pig.test.mjs b/packages/pig/pig.test.mjs new file mode 100644 index 0000000..2be5d1f --- /dev/null +++ b/packages/pig/pig.test.mjs @@ -0,0 +1,84 @@ +import { expect, test } from "bun:test" +import { readFileSync } from "node:fs" +import { agent, connect } from "./pig.agent.js" + +const gateway = { url: "http://127.0.0.1:3499", v1: "http://127.0.0.1:3499/v1", key: "magpie-pig" } +const model = { id: "relay/vendor/model:variant", name: "Example", context: 262144, output: 32768, images: true, efforts: ["none", "low", "high", "max"] } +const output = (models = [model]) => connect({ gateway, models, agent: { id: "pig", config: "/synthetic/.pig/agent/models.json" } }) +const provider = (models = [model]) => output(models)["providers.magpie"] + +test("registers PiG as a connect-only agent with no model or Library writes", () => { + expect(agent).toMatchObject({ id: "pig", name: "PiG", bin: "pig", config: "~/.pig/agent/models.json", format: "json" }) + expect(agent.icon).toBe("https://pi-in-go.dev/pig-favicon.svg") + expect(agent.model).toBeUndefined() + expect(agent.library).toBeUndefined() + expect(agent.notice).toContain("/model") +}) + +test("declares the standard community Agent entry and no host plugin", () => { + const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")) + expect(pkg.name).toBe("@magpie-community/agent-pig") + expect(pkg.magpie.agent).toBe("./pig.agent.js") + expect(pkg.magpie.icon).toBe(agent.icon) + expect(pkg.main).toBeUndefined() + expect(pkg.dependencies).toBeUndefined() + expect(pkg.scripts.preinstall).toBeUndefined() + expect(pkg.scripts.postinstall).toBeUndefined() +}) + +test("gives the host one complete provider restore unit without touching other providers or settings", () => { + const out = output() + expect(Object.keys(out)).toEqual(["providers.magpie"]) + expect(out["providers.magpie"].baseUrl).toBe(gateway.v1) + expect(out["providers.magpie"].apiKey).toBe("magpie-pig") + expect(out["providers.magpie"].api).toBe("openai-completions") +}) + +test("preserves gateway model IDs including slashes and maps capability metadata", () => { + expect(provider().models).toEqual([{ + id: model.id, name: "Example", input: ["text", "image"], reasoning: true, + contextWindow: 262144, maxTokens: 32768, + thinkingLevelMap: { off: "none", minimal: null, low: "low", medium: null, high: "high", xhigh: null, max: "max" }, + }]) +}) + +test("does not advertise thinking off for a model that always thinks", () => { + const [entry] = provider([{ ...model, efforts: ["high"] }]).models + expect(entry.thinkingLevelMap.off).toBeNull() + expect(entry.thinkingLevelMap.high).toBe("high") + expect(entry.thinkingLevelMap.medium).toBeNull() +}) + +test("leaves missing or nonpositive limits to PiG and handles text-only models", () => { + for (const limits of [{}, { context: 0, output: 0 }, { context: -1, output: -1 }]) { + expect(provider([{ id: "relay/plain", ...limits }]).models).toEqual([ + { id: "relay/plain", name: "relay/plain", input: ["text"], reasoning: false }, + ]) + } +}) + +test("keeps a published output limit within its known context window", () => { + const [bounded] = provider([{ id: "relay/oversized-output", context: 8192, output: 32768 }]).models + expect(bounded.maxTokens).toBe(8192) + const [unknownWindow] = provider([{ id: "relay/unknown-window", output: 32768 }]).models + expect(unknownWindow.maxTokens).toBe(32768) + expect(unknownWindow.contextWindow).toBeUndefined() +}) + +test("replaces catalog contents on the next call, including an empty catalog", () => { + expect(provider([{ id: "new/only", efforts: [] }]).models.map(m => m.id)).toEqual(["new/only"]) + expect(provider([]).models).toEqual([]) +}) + +test("follows host gateway changes without caching an endpoint or key", () => { + const next = connect({ gateway: { v1: "http://127.0.0.1:3500/v1", key: "synthetic-next-key" }, models: [] })["providers.magpie"] + expect(next.baseUrl).toBe("http://127.0.0.1:3500/v1") + expect(next.apiKey).toBe("synthetic-next-key") +}) + +test("does not mutate host inputs", () => { + const input = { gateway: { ...gateway }, models: [{ ...model, efforts: [...model.efforts] }] } + const before = JSON.stringify(input) + connect(input) + expect(JSON.stringify(input)).toBe(before) +}) diff --git a/registry.json b/registry.json index 0c9b628..2b2976a 100644 --- a/registry.json +++ b/registry.json @@ -56,6 +56,8 @@ "summary": { "en": "Opt-in, per-provider Codex Responses history filtering after switching providers. Preserves readable content and tool-call pairing; no model probes.", "zh": "按供应商开关 Codex Responses 历史过滤,缓解切换上游后的密文和历史 ID 兼容问题;保留正文与工具配对,不运行模型检测。" } }, { "package": "@magpie-community/agent-aider", "name": "Aider", "kind": "agent", "community": true, "summary": { "en": "Aider (aider.chat) on magpie's Agents page: pick one of magpie's models for it, and switch it off to put its config back.", "zh": "把 Aider(aider.chat)加到 magpie 的 Agent 页:给它选 magpie 的模型,关掉即恢复它原来的配置。" } }, + { "package": "@magpie-community/agent-pig", "name": "PiG", "kind": "agent", "community": true, + "summary": { "en": "PiG (Pi in Go): add Magpie's endpoint and model catalog, choose models in PiG, and keep startup settings unchanged.", "zh": "为 PiG(Pi in Go)注入 Magpie 端点与模型列表,在 PiG 内选择模型,保持启动设置不变。" } }, { "package": "@magpie-community/agent-type4me", "name": "Type4Me", "kind": "agent", "community": true, "summary": { "en": "Type4Me (macOS voice input) on magpie's Agents page: pick one of magpie's models for its LLM post-processing, used from the next dictation.", "zh": "把 Type4Me(macOS 语音输入)加到 magpie 的 Agent 页:给它的 LLM 后处理选 magpie 的模型,下一次语音输入即生效。" } }, { "package": "@magpie-community/middleware-reserve", "name": "Reserve", "kind": "middleware", "community": true,