diff --git a/packages/cowork/README.md b/packages/cowork/README.md index 6c4ae7d..d6a6682 100644 --- a/packages/cowork/README.md +++ b/packages/cowork/README.md @@ -108,6 +108,17 @@ Cowork 找不到同名条目,会退回自己原来的模型。这是这条路 改动生效的时机:Cowork 启动时读这个文件,所以重启 Cowork 或新开一个会话才用新模型。 +### 思考等级 + +magpie 的 `/v1/models` 给某个模型列了思考等级(`supported_reasoning_levels` / `reasoning_levels`)时, +`seed.mjs` 把它写成条目的 `extras.thinkingEfforts {default, options}`——Cowork 模型菜单画 Reasoning 那一行 +读的就是这个形状。magpie 不报默认等级,所以 `default` 取 `medium`,没有就 `high`,再没有就最高的那档; +之前 seed 过的条目下次运行时补上(`--check` 里显示为「reasoning levels to update」)。 + +**但 Cowork 2.1.6-stable 自己不读它**:自定义模型的 `extras` 是 Cowork 每次从头拼的,思考等级只取自那条 +模型 Extra Body 里的 `reasoning_effort`,而且只有那一个选项。所以在这一版里写进去的等级不会变成可切换的 +下拉。想固定一个等级,就在 Cowork 的自定义模型设置里把 Extra Body 填成 `{"reasoning_effort": "high"}`。 + ## 装完怎么确认 ```sh diff --git a/packages/cowork/package.json b/packages/cowork/package.json index c88d28d..3cb0c05 100644 --- a/packages/cowork/package.json +++ b/packages/cowork/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/agent-cowork", - "version": "0.1.0", + "version": "0.1.1", "description": "Tuanjie Cowork (codely) as a magpie agent: magpie cowork switches the model it chats with", "type": "module", "files": [ diff --git a/packages/cowork/scripts/mutate.mjs b/packages/cowork/scripts/mutate.mjs index b468e7c..53586ce 100644 --- a/packages/cowork/scripts/mutate.mjs +++ b/packages/cowork/scripts/mutate.mjs @@ -41,20 +41,20 @@ const MUTATIONS = [ }, { file: "seed.mjs", - from: "if (!m || m.providerId !== PROVIDER_ID || !seeded.has(m.id) || m.displayName === m.model) return m", - to: "return m", + from: "if (x.displayName === x.model) return x", + to: "return x", why: "leaves a pretty label as the picker title, so the string magpie writes matches nothing", }, { file: "seed.mjs", - from: "if (!m || m.providerId !== PROVIDER_ID || !seeded.has(m.id) || m.displayName === m.model) return m", - to: "if (!m || m.displayName === m.model) return m", + from: "if (!m || m.providerId !== PROVIDER_ID || !seeded.has(m.id)) return m", + to: "if (!m || m.providerId !== PROVIDER_ID) return m", why: "renames entries the user named, dangling their saved selection", }, { file: "seed.mjs", - from: "if (!m || m.providerId !== PROVIDER_ID || !seeded.has(m.id) || m.displayName === m.model) return m", - to: "if (!m || m.providerId !== PROVIDER_ID || m.displayName === m.model) return m", + from: "if (!m || m.providerId !== PROVIDER_ID || !seeded.has(m.id)) return m", + to: "if (!m || !seeded.has(m.id)) return m", why: "renames an entry of theirs that an old record still lists", }, { @@ -221,8 +221,8 @@ const MUTATIONS = [ }, { file: "seed.mjs", - from: "const drift = added.length > 0 || droppable.length > 0 || relabeled > 0 || providerMissing || providerMoved", - to: "const drift = added.length > 0 || relabeled > 0 || providerMissing || providerMoved", + from: "const drift = added.length > 0 || droppable.length > 0 || relabeled > 0 || refreshed > 0 || providerMissing || providerMoved", + to: "const drift = added.length > 0 || relabeled > 0 || refreshed > 0 || providerMissing || providerMoved", why: "--check calls a store in step while --prune would still drop entries from it", }, { @@ -293,10 +293,64 @@ const MUTATIONS = [ }, { file: "seed.mjs", - from: "if (m.displayName === selected) renamed = m.model", - to: "if (false) renamed = m.model", + from: "if (x.displayName === selected) renamed = x.model", + to: "if (false) renamed = x.model", why: "renames the entry Cowork is on and leaves the selection pointing at the old name", }, + { + file: "seed.mjs", + from: "...(te ? { extras: { thinkingEfforts: te } } : {}),", + to: "", + why: "seeds a model that takes reasoning efforts with none, so Cowork has no Reasoning row to draw (#828)", + }, + { + file: "seed.mjs", + from: "const raw = Array.isArray(m.supported_reasoning_levels) && m.supported_reasoning_levels.length", + to: "const raw = false", + why: "reads only the flat reasoning_levels, so a catalog that lists the objects alone gets no efforts", + }, + { + file: "seed.mjs", + from: ": Array.isArray(m.reasoning_levels) ? m.reasoning_levels : []", + to: ": []", + why: "reads only the {effort} objects, so a flat reasoning_levels list gets no efforts", + }, + { + file: "seed.mjs", + from: "const options = [...new Set(names)]", + to: "const options = names", + why: "offers a level twice when the catalog repeats it", + }, + { + file: "seed.mjs", + from: "for (const e of [\"medium\", \"high\"]) if (options.includes(e)) return e", + to: "", + why: "defaults to the dearest level instead of the one vendors start on", + }, + { + file: "seed.mjs", + from: "if (named && options.includes(named)) return named", + to: "", + why: "ignores a default the catalog names", + }, + { + file: "seed.mjs", + from: "let x = byId.has(m.model) ? withEfforts(m, byId.get(m.model)) : m", + to: "let x = m", + why: "a store seeded before #828 never gets the efforts, since its entries are never added again", + }, + { + file: "seed.mjs", + from: "if (x !== m) refreshed++", + to: "", + why: "updates efforts in memory but reports nothing to write, so the store keeps the old ones", + }, + { + file: "seed.mjs", + from: "const { thinkingEfforts: have, ...rest } = x.extras || {}", + to: "const { thinkingEfforts: have, ...rest } = {}", + why: "drops the other extras keys of an entry when its efforts change", + }, ] rmSync(SCRATCH, { recursive: true, force: true }) diff --git a/packages/cowork/seed.mjs b/packages/cowork/seed.mjs index 560baa4..b716ff8 100644 --- a/packages/cowork/seed.mjs +++ b/packages/cowork/seed.mjs @@ -54,7 +54,42 @@ export function provider(baseUrl) { } } +// The reasoning efforts a catalog model takes, as magpie's /v1/models lists +// them: `supported_reasoning_levels` as {effort} objects, `reasoning_levels` as +// plain names. null when it names none. +export function efforts(m) { + const raw = Array.isArray(m.supported_reasoning_levels) && m.supported_reasoning_levels.length + ? m.supported_reasoning_levels + : Array.isArray(m.reasoning_levels) ? m.reasoning_levels : [] + const names = raw + .map((l) => (typeof l === "string" ? l : l && typeof l.effort === "string" ? l.effort : "")) + .map((s) => s.trim()) + .filter(Boolean) + const options = [...new Set(names)] + return options.length ? options : null +} + +// The effort shown before the user picks one. magpie names no default, and +// leaving reasoning_effort out lets the vendor choose; the nearest level to +// that is the middle one most vendors default to, not the dearest. +export function defaultEffort(options, m = {}) { + const named = typeof m.default_reasoning_level === "string" ? m.default_reasoning_level.trim() : "" + if (named && options.includes(named)) return named + for (const e of ["medium", "high"]) if (options.includes(e)) return e + const thinking = options.filter((e) => e !== "none") + return thinking.length ? thinking[thinking.length - 1] : options[0] +} + +// Cowork's picker draws its Reasoning row from a model's +// extras.thinkingEfforts {default, options} (it checks `"default" in` and an +// options array). +export function thinkingEfforts(m) { + const options = efforts(m) + return options ? { default: defaultEffort(options, m), options } : null +} + export function entry(m) { + const te = thinkingEfforts(m) return { id: `${PROVIDER_ID}:${m.id}`, model: m.id, @@ -68,9 +103,23 @@ export function entry(m) { wireApi: "chat", contextLength: m.context_length || 8192, supportsMultimodal: !!(m.modalities && (m.modalities.input || []).includes("image")), + ...(te ? { extras: { thinkingEfforts: te } } : {}), } } +// A recorded entry of ours with the catalog's current efforts; the same +// object when nothing changes. Other extras keys are left as they are, and an +// extras that isn't an object is not ours to replace. +export function withEfforts(x, m) { + if (x.extras !== undefined && (!x.extras || typeof x.extras !== "object" || Array.isArray(x.extras))) return x + const want = thinkingEfforts(m) + const { thinkingEfforts: have, ...rest } = x.extras || {} + if (JSON.stringify(have ?? null) === JSON.stringify(want)) return x + const extras = want ? { ...rest, thinkingEfforts: want } : rest + const { extras: _, ...base } = x + return Object.keys(extras).length ? { ...base, extras } : base +} + export function seedFile(storePath) { return `${storePath}.magpie-seeded` } @@ -174,17 +223,24 @@ export function plan(store, catalog, baseUrl, seeded = new Set(), opts = {}) { })) let relabeled = 0 + let refreshed = 0 let renamed = null const dropped = new Set(pruned) + const byId = new Map(catalog.map((m) => [m.id, m])) const kept = models .filter((m) => !(m && dropped.has(m.id))) .map((m) => { + if (!m || m.providerId !== PROVIDER_ID || !seeded.has(m.id)) return m + // a recorded entry follows the catalog's reasoning efforts, so a store + // seeded before they were written gets them on the next run + let x = byId.has(m.model) ? withEfforts(m, byId.get(m.model)) : m + if (x !== m) refreshed++ // only entries we wrote, and only ones we recorded: a name the user chose in // Cowork is theirs, and renaming it would leave their selection pointing nowhere - if (!m || m.providerId !== PROVIDER_ID || !seeded.has(m.id) || m.displayName === m.model) return m + if (x.displayName === x.model) return x relabeled++ - if (m.displayName === selected) renamed = m.model - return { ...m, displayName: m.model } + if (x.displayName === selected) renamed = x.model + return { ...x, displayName: x.model } }) const added = [] for (const m of catalog) { @@ -202,13 +258,13 @@ export function plan(store, catalog, baseUrl, seeded = new Set(), opts = {}) { for (const m of catalog) if (added.includes(m.id)) nextSeeded.add(`${PROVIDER_ID}:${m.id}`) const providerMissing = at === -1 const providerMoved = before !== null && before.baseUrl !== baseUrl - const changed = added.length > 0 || pruned.length > 0 || relabeled > 0 || providerMissing || providerMoved + const changed = added.length > 0 || pruned.length > 0 || relabeled > 0 || refreshed > 0 || providerMissing || providerMoved // --check reports drift even when a normal run would write nothing: entries // held back for --prune are still a difference from the catalog. - const drift = added.length > 0 || droppable.length > 0 || relabeled > 0 || providerMissing || providerMoved + const drift = added.length > 0 || droppable.length > 0 || relabeled > 0 || refreshed > 0 || providerMissing || providerMoved return { next: { ...store, customModels: kept, customModelProviders: providers }, - added, pruned, held, relabeled, renamed, unreachable, seeded: [...nextSeeded], changed, drift, + added, pruned, held, relabeled, refreshed, renamed, unreachable, seeded: [...nextSeeded], changed, drift, provider: providerMissing ? "add" : providerMoved ? "repoint" : null, } } @@ -388,7 +444,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) } if (seeded === null) seeded = new Set() - const { next, added, pruned, held, relabeled, renamed, unreachable, provider, seeded: nextSeeded, changed, drift } = + const { next, added, pruned, held, relabeled, refreshed, renamed, unreachable, provider, seeded: nextSeeded, changed, drift } = plan(store, catalog, `${gateway}/v1`, seeded, { prune: flag("prune"), selected: selected.value }) if (unreachable.length) console.log(unreachableNote(unreachable)) @@ -399,6 +455,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) if (added.length) parts.push(`+${added.length} to add`) if (pruned.length + held.length) parts.push(`-${pruned.length + held.length} to drop${held.length ? ` (${held.length} held until --prune)` : ""}`) if (relabeled) parts.push(`~${relabeled} to relabel`) + if (refreshed) parts.push(`~${refreshed} reasoning levels to update`) if (provider) parts.push(`provider to ${provider}`) console.log(`drift: ${parts.join(", ")}`) } else { @@ -417,6 +474,6 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) if (renamed && writeTitle(renamed)) { console.log(`Cowork was on the entry we just renamed; its selection now says ${renamed}`) } - console.log(`wrote ${storePath}: +${added.length} magpie models, -${pruned.length} stale, ~${relabeled} relabelled, ${next.customModels.length} total`) + console.log(`wrote ${storePath}: +${added.length} magpie models, -${pruned.length} stale, ~${relabeled} relabelled, ~${refreshed} reasoning levels updated, ${next.customModels.length} total`) if (held.length) console.log(heldNote(held.length)) } diff --git a/packages/cowork/test/cowork.test.mjs b/packages/cowork/test/cowork.test.mjs index b837d97..86478b9 100644 --- a/packages/cowork/test/cowork.test.mjs +++ b/packages/cowork/test/cowork.test.mjs @@ -7,7 +7,7 @@ import { tmpdir } from "node:os" import { join } from "node:path" import { fileURLToPath } from "node:url" import { agent } from "../cowork.agent.js" -import { adoptSeeded, backupNeeded, codelyHome, entry, loadSeeded, opt, plan, provider, readTitleFile, seedFile, unplan, unreachableNote, PROVIDER_ID } from "../seed.mjs" +import { adoptSeeded, backupNeeded, codelyHome, defaultEffort, efforts, entry, loadSeeded, opt, plan, provider, readTitleFile, seedFile, unplan, thinkingEfforts, unreachableNote, withEfforts, PROVIDER_ID } from "../seed.mjs" const SEED = fileURLToPath(new URL("../seed.mjs", import.meta.url)) @@ -817,3 +817,90 @@ test("CODELY_CLI_HOME moves the store and the record it reads with it", () => { rmSync(dir, { recursive: true, force: true }) } }) + +// What magpie's /v1/models answers for workbuddy/deepseek-v4.1-flash +// (internal/gateway modelObject, #828): both spellings of the same levels. +const THINKS = { + id: "workbuddy/deepseek-v4.1-flash", + context_length: 262144, + reasoning: true, + reasoning_levels: ["none", "low", "high", "max"], + supported_reasoning_levels: [{ effort: "none" }, { effort: "low" }, { effort: "high" }, { effort: "max" }], +} + +test("a model's reasoning levels are seeded as Cowork's extras.thinkingEfforts (#828)", () => { + assert.deepEqual(entry(THINKS).extras, { thinkingEfforts: { default: "high", options: ["none", "low", "high", "max"] } }) + assert.equal("extras" in entry(TEXT), false, "a model with no levels gets no Reasoning row") + assert.equal("extras" in entry({ ...TEXT, reasoning: false, reasoning_levels: [], supported_reasoning_levels: [] }), false) +}) + +test("either spelling of the levels is read, objects first, and repeats are dropped", () => { + assert.deepEqual(efforts({ supported_reasoning_levels: [{ effort: "low" }, { effort: " high " }, { effort: "low" }, null, {}] }), ["low", "high"]) + assert.deepEqual(efforts({ reasoning_levels: ["low", "medium", "high"] }), ["low", "medium", "high"]) + assert.deepEqual(efforts({ supported_reasoning_levels: [], reasoning_levels: ["minimal", "low"] }), ["minimal", "low"], + "an empty object list is no answer, so the flat list is read") + assert.equal(efforts({ reasoning: true }), null) +}) + +test("the default is the catalog's when it names one, else the middle level, never the dearest by default", () => { + assert.equal(defaultEffort(["low", "medium", "high", "xhigh"]), "medium") + assert.equal(defaultEffort(["none", "low", "high", "max"]), "high") + assert.equal(defaultEffort(["none", "low"]), "low") + assert.equal(defaultEffort(["none"]), "none") + assert.equal(defaultEffort(["low", "medium", "high"], { default_reasoning_level: "low" }), "low") + assert.equal(defaultEffort(["low", "medium"], { default_reasoning_level: "max" }), "medium", "a default it doesn't offer is not used") + const te = thinkingEfforts(THINKS) + assert.ok("default" in te && Array.isArray(te.options), "the two keys Cowork's picker checks for") +}) + +test("an entry seeded before #828 gets the levels on the next run, and keeps its other extras", () => { + const id = `${PROVIDER_ID}:${THINKS.id}` + const old = { id, model: THINKS.id, providerId: PROVIDER_ID, displayName: THINKS.id, extras: { note: "kept" } } + const store = { customModelProviders: [provider(V1)], customModels: [old] } + const r = plan(store, [THINKS], V1, new Set([id])) + assert.equal(r.refreshed, 1) + assert.equal(r.changed, true, "a store that only lacks the levels is still written") + assert.equal(r.drift, true, "and --check reports it") + assert.deepEqual(r.next.customModels[0].extras, { note: "kept", thinkingEfforts: thinkingEfforts(THINKS) }) + const again = plan(r.next, [THINKS], V1, new Set([id])) + assert.equal(again.refreshed, 0) + assert.equal(again.changed, false, "in step once written") + const gone = plan(r.next, [{ ...THINKS, reasoning_levels: [], supported_reasoning_levels: [] }], V1, new Set([id])) + assert.deepEqual(gone.next.customModels[0].extras, { note: "kept" }, "levels the catalog dropped are dropped too") +}) + +test("the levels of an entry that isn't ours are left alone", () => { + const theirs = { id: `custom-provider:${THINKS.id}`, model: THINKS.id, providerId: "custom-provider", displayName: THINKS.id } + const mine = { id: `${PROVIDER_ID}:${THINKS.id}`, model: THINKS.id, providerId: PROVIDER_ID, displayName: THINKS.id } + for (const m of [theirs, mine]) { + const r = plan({ customModelProviders: [provider(V1)], customModels: [m] }, [THINKS], V1, new Set()) + assert.equal(r.refreshed, 0) + assert.equal(r.next.customModels[0], m, "the same entry, untouched") + } + const odd = { ...mine, extras: "x" } + assert.equal(withEfforts(odd, THINKS), odd, "an extras that isn't an object is not replaced") +}) + +test("a real seed run writes the levels Cowork reads, into a store seeded before them", async (t) => { + const dir = mkdtempSync(join(tmpdir(), "cowork-efforts-")) + t.after(() => rmSync(dir, { recursive: true, force: true })) + const store = join(dir, "settings.json") + const gateway = await catalogServer(t, [THINKS, GLM]) + const id = `${PROVIDER_ID}:${THINKS.id}` + writeFileSync(store, JSON.stringify({ + customModelProviders: [provider(`${gateway}/v1`)], + customModels: [{ id, model: THINKS.id, providerId: PROVIDER_ID, displayName: THINKS.id, authType: "openai", roles: ["model"], wireApi: "chat" }], + })) + writeFileSync(seedFile(store), JSON.stringify([id])) + writeFileSync(join(dir, "ide-settings.json"), "{}") + const check = await runSeed(["--store", store, "--gateway", gateway, "--check"]) + assert.equal(check.status, 1, check.stdout + check.stderr) + assert.match(check.stdout, /\+1 to add, ~1 reasoning levels to update/) + const r = await runSeed(["--store", store, "--gateway", gateway]) + assert.equal(r.status, 0, r.stderr) + const models = JSON.parse(readFileSync(store, "utf8")).customModels + assert.deepEqual(models.find((m) => m.id === id).extras.thinkingEfforts, { default: "high", options: ["none", "low", "high", "max"] }) + assert.equal("extras" in models.find((m) => m.model === GLM.id), false) + const after = await runSeed(["--store", store, "--gateway", gateway, "--check"]) + assert.equal(after.status, 0, after.stdout) +})