@yi-agent/magpie-agent-yi-agent 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +146 -2
  2. package/package.json +32 -4
  3. package/yi-agent.agent.js +183 -0
package/README.md CHANGED
@@ -1,3 +1,147 @@
1
- # Temporary Holding Version
1
+ # @yi-agent/magpie-agent-yi-agent
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A [Magpie](https://usemagpie.ai) agent plugin that connects
4
+ [Yi Agent](https://github.com/YichenGong/yi-agent) to Magpie's gateway.
5
+
6
+ Install it with:
7
+
8
+ ```bash
9
+ magpie plugin add @yi-agent/magpie-agent-yi-agent
10
+ ```
11
+
12
+ Then find **Yi Agent** on Magpie's Agents page, pick one of Magpie's models in
13
+ its row, and the plugin writes that model into Yi Agent's config. Switch it off
14
+ and everything goes back to how you had it.
15
+
16
+ ## What it does
17
+
18
+ Yi Agent keeps its machine-wide model catalog in `~/.yi-agent/models.json` (a
19
+ JSON object with `models`, `default_model`, `subagent_model`, and
20
+ `subagent_role_models`). While the agent is connected, Magpie rewrites that
21
+ file:
22
+
23
+ | Yi Agent field | Value written while connected |
24
+ | --- | --- |
25
+ | `models[].name` | `magpie/` + the Magpie model id (e.g. `magpie/anthropic/claude-sonnet-4-5`) |
26
+ | `models[].provider` | `openai` |
27
+ | `models[].api_url` | `gateway.v1` (the gateway's OpenAI-compatible `/v1` root) |
28
+ | `models[].api_key` | the key Magpie assigned to Yi Agent |
29
+ | `models[].model` | the Magpie model id, passed through unchanged |
30
+ | `models[].api_style` | `"responses"` when Magpie reports `/v1/responses` in `native_endpoints`, otherwise `"chat"` |
31
+ | `models[].context_length` | filled in when Magpie reports a valid positive context length |
32
+ | `models[].reasoning_capabilities` | filled for the entry's active channel (`responses` or `chat`) when Magpie reports non-empty supported `efforts` |
33
+ | `default_model` | written by Magpie as `magpie/` + the selected model id |
34
+ | `subagent_model` | `magpie/` + the selected model id while connected |
35
+ | `subagent_role_models` | `{}` while connected (original per-role overrides backed up and restored on disconnect) |
36
+
37
+ The gateway translates requests to whatever API each model speaks, so Yi Agent
38
+ talks to every Magpie model through its OpenAI-compatible `/v1` gateway, using
39
+ `/v1/responses` only for models that speak Responses natively and `/v1/chat/completions`
40
+ otherwise to avoid unnecessary protocol translation.
41
+
42
+ ## Things to know
43
+
44
+ - **Two icons, two sources.** Magpie's *Installed* list draws a plugin's logo
45
+ from `package.json`'s `magpie.icon`; the *Agents* page draws each agent's row
46
+ icon from the agent module's own exported `icon` field and does not fall back
47
+ to `magpie.icon`. Both point at the same PNG here, so the two rows match — keep
48
+ them in sync if you ever change the artwork (the tests compare them).
49
+
50
+ - **`models` is an array owned by Magpie while connected.** While the agent is
51
+ connected, the `models` array holds only the Magpie models above; your
52
+ original Yi Agent entries are temporarily out of it. Magpie backs them up and
53
+ restores them on disconnect. This means edits you make to `models.json` by
54
+ hand *while connected* are on Magpie-managed keys and will be overwritten on
55
+ the next model change. Edit your own model entries after disconnecting (or
56
+ switch the agent off first).
57
+ - **`subagent_model` is also managed during connection.** If it pointed at one
58
+ of your original entries, that entry is out of the array while connected, so
59
+ the plugin points `subagent_model` at the current Magpie model instead. On
60
+ disconnect Magpie restores your original value (or removes the key if it
61
+ wasn't there).
62
+ - **`subagent_role_models` is cleared while connected and restored on disconnect.**
63
+ Per-role overrides (`explore`, `plan`, `coding`, `review`, `chore`) also name
64
+ entries in `models`. Clearing them to `{}` during connection prevents stale
65
+ references to temporarily swapped-out user entries, and ensures any
66
+ `magpie/...` role overrides set while connected are cleaned up on disconnect.
67
+ Note: Magpie's agent-plugin schema (`agentplug.Desc`) currently exposes only a
68
+ single primary model selector per plugin — it does not yet support extra
69
+ plugin-defined fields for `subagent_model` or per-role `subagent_role_models`
70
+ in Magpie's Agent detail UI. Any role overrides edited inside Yi Agent while
71
+ connected will also be reset to `{}` on the next Magpie model switch or
72
+ catalog sync. Supporting per-role selection in Magpie's UI requires either
73
+ multi-field support in Magpie's plugin protocol or built-in agent integration.
74
+ - **Reasoning effort capabilities are only written when known.** When Magpie
75
+ reports a non-empty `efforts` list for a model, the plugin seeds
76
+ `reasoning_capabilities.<channel>.supported` so Yi Agent's picker shows the
77
+ right tiers immediately without probing. When Magpie reports `efforts: []`,
78
+ the field is omitted (leaving capability unknown) so Yi Agent does not treat
79
+ an uncatalogued model as supporting zero tiers.
80
+ - **Path: a custom `YI_AGENT_HOME` is not supported.** The config path is
81
+ fixed at `~/.yi-agent/models.json`. Yi Agent also reads `YI_AGENT_HOME`, but
82
+ this plugin does not: it declares the literal `~/.yi-agent/models.json`, so
83
+ setting `YI_AGENT_HOME` for both programs does not help — a Yi Agent started
84
+ with it reads `$YI_AGENT_HOME/models.json` and never sees what Magpie writes.
85
+ Declaring `$YI_AGENT_HOME/models.json` instead is not an option either:
86
+ Magpie expands a `$VAR` in a declared path, and an unset variable expands to
87
+ an empty string, which would leave Magpie writing `/models.json`. Use the
88
+ default location, or leave this plugin off.
89
+ - **Desktop reloads `models.json` lazily; a running TUI does not.** Yi Agent
90
+ Desktop re-reads `models.json` automatically for new or resumed threads,
91
+ subagent spawns, and when you open the model picker; already-open Desktop
92
+ threads keep their active model until you switch models in the picker or start
93
+ a new thread. Yi Agent's TUI reads `models.json` at startup and re-reads it
94
+ only when you switch the model with `/model`. After connecting, switching
95
+ models, or disconnecting in Magpie, a TUI that was already running keeps using
96
+ the old model — restart it, or run `/model default` to pick up the change.
97
+ - **Known limitation (technical debt): Magpie's "Provider in model names" setting has no effect yet.**
98
+ Magpie passes each model's formatted display name in `model.name`, but Yi Agent's
99
+ `ModelEntry` currently has no separate `display_name` field — `models[].name` is
100
+ both the lookup key (referenced by `default_model` as `magpie/<id>`) and the UI
101
+ label, so the plugin cannot substitute `model.name` into `models[].name`. In
102
+ addition, Magpie's agent-plugin schema (`agentplug.Desc`) does not yet expose a
103
+ `noNames` flag for plugins to annotate Magpie's model-name controls in
104
+ Settings, Routing, and provider settings with a notice that the setting does
105
+ not affect Yi Agent.
106
+ Once Yi Agent adds a `display_name` field on `ModelEntry` (and renders it in
107
+ Desktop/TUI pickers), wire `display_name: model.name` in `toEntry()` to enable
108
+ this setting end-to-end.
109
+
110
+ ## Development
111
+
112
+ ```bash
113
+ cd integrations/magpie-agent-yi-agent
114
+ npm test # node --test tests/agent.test.mjs
115
+ ```
116
+
117
+ The plugin entry (`yi-agent.agent.js`) runs in Magpie's restricted agent
118
+ runtime (not Node.js and not Bun): it has no dependencies, no Node APIs, and no
119
+ I/O. The tests exercise the pure mapping directly, plus a simulation of
120
+ Magpie's per-key write protocol (backup → write → restore) covering: no
121
+ existing config, existing entries, switching the default model, model
122
+ add/remove, repeated `connect()`, disconnect restoration, `subagent_model`
123
+ with/without a prior value, and unusable model metadata.
124
+
125
+ ## Publishing
126
+
127
+ This package is versioned independently of Yi Agent. To publish a new version:
128
+
129
+ ```bash
130
+ cd integrations/magpie-agent-yi-agent
131
+ npm version patch # or minor / major
132
+ git push # CI publishes the new version when it lands on main
133
+ ```
134
+
135
+ CI (`.github/workflows/magpie-agent-yi-agent.yml`) publishes only when the version
136
+ actually changed — an already-published version is skipped, so re-merging
137
+ without bumping doesn't republish. You can also publish by hand:
138
+
139
+ ```bash
140
+ cd integrations/magpie-agent-yi-agent
141
+ npm publish --access public
142
+ ```
143
+
144
+ Community-market listing is separate from npm publication and is not
145
+ automatic; see
146
+ [magpie-community/plugins](https://github.com/magpie-community/plugins) for how
147
+ to request listing if you want it.
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "@yi-agent/magpie-agent-yi-agent",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Magpie agent plugin: connect Yi Agent to Magpie's gateway.",
5
+ "type": "module",
6
+ "files": [
7
+ "yi-agent.agent.js",
8
+ "README.md"
9
+ ],
10
+ "keywords": [
11
+ "magpie",
12
+ "magpie-plugin",
13
+ "agent",
14
+ "yi-agent"
15
+ ],
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/YichenGong/yi-agent.git",
20
+ "directory": "integrations/magpie-agent-yi-agent"
21
+ },
22
+ "scripts": {
23
+ "test": "node --test tests/agent.test.mjs"
24
+ },
25
+ "magpie": {
26
+ "name": "Yi Agent",
27
+ "summary": {
28
+ "en": "Connect Yi Agent to Magpie's gateway",
29
+ "zh": "将 Yi Agent 接入 Magpie 网关"
30
+ },
31
+ "agent": "./yi-agent.agent.js",
32
+ "icon": "https://raw.githubusercontent.com/YichenGong/yi-agent/main/integrations/magpie-agent-yi-agent/icon.png"
33
+ }
34
+ }
@@ -0,0 +1,183 @@
1
+ // Magpie agent plugin for Yi Agent.
2
+ //
3
+ // Yi Agent keeps its machine-wide model catalog in `~/.yi-agent/models.json`
4
+ // (see `yi-agent-rs/crates/yi-agent-runtime/src/models.rs`). This plugin makes
5
+ // Magpie's model directory available to Yi Agent by rewriting that file while
6
+ // the agent is connected, and relies on Magpie's per-key backup/restore to put
7
+ // it back on disconnect.
8
+ //
9
+ // This file runs in Magpie's agent runtime (moejs), NOT Node.js or Bun:
10
+ // - no Node APIs (fs, process, fetch, timers)
11
+ // - imports are limited to files inside this plugin
12
+ // - loading and connect() each get one second
13
+ // So it is a pure, dependency-free mapping with no I/O.
14
+ //
15
+ // Deliberately there is no `main`/`exports` in package.json: a file referenced
16
+ // only by `magpie.agent` is loaded as an agent. A `main` would make Magpie load
17
+ // it as a provider too and call every exported function as a plugin, which
18
+ // would break `connect`.
19
+
20
+ // Written before a Magpie model id. `agent.prefix` and `connect()` must agree
21
+ // on this: Magpie writes `default_model = prefix + model.id`, and the entry we
22
+ // emit must carry that exact string as its `name`, or Yi Agent can't resolve it.
23
+ const PREFIX = "magpie/";
24
+
25
+ // `context_length` in Yi Agent is Option<u32>. A value outside this range would
26
+ // make serde reject the whole entry, so it is omitted instead of passed through.
27
+ const U32_MAX = 4294967295;
28
+
29
+ // Reasoning effort tiers accepted by Yi Agent, in UI display order
30
+ // (mirrors `REASONING_TIERS` in `yi-agent-runtime/src/models.rs`).
31
+ const REASONING_TIERS = [
32
+ "none",
33
+ "minimal",
34
+ "low",
35
+ "medium",
36
+ "high",
37
+ "xhigh",
38
+ "max",
39
+ ];
40
+
41
+ // Magpie's Agents page draws the row icon from the agent module's own `icon`
42
+ // (agentplug Desc.Icon) and does not fall back to package.json's
43
+ // `magpie.icon` — that one only feeds the Installed list. Both reference the
44
+ // same HTTPS PNG URL in this repo so the two rows match without inlining a
45
+ // ~72 KB base64 blob; a test keeps them identical.
46
+ const ICON_URL =
47
+ "https://raw.githubusercontent.com/YichenGong/yi-agent/main/integrations/magpie-agent-yi-agent/icon.png";
48
+
49
+ export const agent = {
50
+ id: "yi-agent",
51
+ name: "Yi Agent",
52
+ bin: "yi-agent",
53
+ // Lets Magpie detect Desktop-only installs where `yi-agent` is not on PATH
54
+ // and `models.json` has not been written yet.
55
+ dir: "~/.yi-agent",
56
+ icon: ICON_URL,
57
+ // The file Magpie edits. `~` is expanded by Magpie to the user's HOME.
58
+ config: "~/.yi-agent/models.json",
59
+ // The key of the model in models.json.
60
+ model: "default_model",
61
+ prefix: PREFIX,
62
+ notice:
63
+ "Yi Agent Desktop picks up models.json changes automatically for new or resumed threads, subagents, and when opening the model picker (existing open threads keep their current model until switched). A running yi-agent TUI does not — restart it, or run /model default after connecting or switching.",
64
+ };
65
+
66
+ // Picks the OpenAI wire channel from Magpie's `native_endpoints`:
67
+ // - `/v1/responses` available natively -> "responses"
68
+ // - otherwise (Chat-native, Anthropic-native, translated subscriptions, or
69
+ // heterogeneous groups) -> "chat", because Yi Agent's `"auto"` defaults to
70
+ // `/v1/responses` and Magpie exposes `/v1/responses` for every model.
71
+ function pickApiStyle(model) {
72
+ const endpoints =
73
+ model && Array.isArray(model.native_endpoints) ? model.native_endpoints : null;
74
+ if (!endpoints || endpoints.length === 0) {
75
+ return "chat";
76
+ }
77
+ if (endpoints.includes("/v1/responses")) {
78
+ return "responses";
79
+ }
80
+ return "chat";
81
+ }
82
+
83
+ // Maps Magpie's `efforts` array onto Yi Agent's `reasoning_capabilities` slot
84
+ // for the entry's active channel. Only emitted when Magpie reports at least one
85
+ // supported tier: Magpie sends `efforts: []` for models whose capability is
86
+ // unknown too, and writing `supported: []` (with no `error`) tells Yi Agent's
87
+ // allowlist that zero tiers are allowed.
88
+ function buildReasoningCapabilities(model, apiStyle) {
89
+ const raw = model && Array.isArray(model.efforts) ? model.efforts : [];
90
+ if (raw.length === 0) {
91
+ return undefined;
92
+ }
93
+ const seen = new Set(raw.filter((tier) => typeof tier === "string"));
94
+ const supported = REASONING_TIERS.filter((tier) => seen.has(tier));
95
+ if (supported.length === 0) {
96
+ return undefined;
97
+ }
98
+ const channel = apiStyle === "responses" ? "responses" : "chat";
99
+ return {
100
+ [channel]: {
101
+ supported,
102
+ probed_at: 1,
103
+ },
104
+ };
105
+ }
106
+
107
+ // A Magpie model id (e.g. "anthropic/claude-sonnet-4-5") mapped to a Yi Agent
108
+ // catalog entry. Every model is surfaced as provider "openai" pointing at the
109
+ // gateway's OpenAI-compatible `/v1` endpoint; the gateway translates the request
110
+ // to whatever API the model speaks.
111
+ function toEntry(model, gateway) {
112
+ const id = model && typeof model.id === "string" ? model.id : "";
113
+ const apiStyle = pickApiStyle(model);
114
+ const entry = {
115
+ name: PREFIX + id,
116
+ provider: "openai",
117
+ api_url: gateway.v1,
118
+ api_key: gateway.key,
119
+ model: id,
120
+ api_style: apiStyle,
121
+ };
122
+ const context = model && model.context;
123
+ if (
124
+ typeof context === "number" &&
125
+ Number.isInteger(context) &&
126
+ context > 0 &&
127
+ context <= U32_MAX
128
+ ) {
129
+ entry.context_length = context;
130
+ }
131
+ const caps = buildReasoningCapabilities(model, apiStyle);
132
+ if (caps !== undefined) {
133
+ entry.reasoning_capabilities = caps;
134
+ }
135
+ return entry;
136
+ }
137
+
138
+ // `connect(input)` returns the keys Magpie writes beside the model. It is called
139
+ // again when Magpie's models change, so the `models` array it returns stays in
140
+ // sync with Magpie's directory. It receives:
141
+ // gateway: { url, v1, key } — the gateway address and the agent's own key
142
+ // model: the currently selected model (id, name, context, output, …)
143
+ // models: every model Magpie offers the agent, in the same shape
144
+ // agent: { id, config }
145
+ export function connect(input) {
146
+ const gateway = input.gateway || {};
147
+ const model = input.model;
148
+ const list = Array.isArray(input.models) ? input.models : [];
149
+ const entries = list
150
+ .map((m) => toEntry(m, gateway))
151
+ .filter((e) => e.model !== "");
152
+
153
+ // `subagent_role_models` (explore/plan/coding/review/chore) references entry
154
+ // names in `models`. Returning `{}` makes Magpie back up the user's role
155
+ // overrides, clear dangling references while connected, and restore (or
156
+ // delete any magpie-bound overrides) on disconnect.
157
+ const result = {
158
+ models: entries,
159
+ subagent_role_models: {},
160
+ };
161
+
162
+ // default_model is written by Magpie as `prefix + model.id`. subagent_model
163
+ // must name an entry that exists in `models`, so while connected it points at
164
+ // the same selected model; Magpie backs up the original value and restores it
165
+ // (or removes the key, when it wasn't there) on disconnect. Without this,
166
+ // a subagent_model pointing at an original entry (which is temporarily out of
167
+ // the array) would be a dangling reference.
168
+ const selectedId = model && typeof model.id === "string" ? model.id : "";
169
+ if (selectedId !== "") {
170
+ result.subagent_model = PREFIX + selectedId;
171
+ }
172
+
173
+ return result;
174
+ }
175
+
176
+ // Test seams only. Magpie ignores every export other than `agent`/`connect`.
177
+ export const _internal = {
178
+ PREFIX,
179
+ REASONING_TIERS,
180
+ pickApiStyle,
181
+ buildReasoningCapabilities,
182
+ toEntry,
183
+ };