Files
est7 c4d84633b5 junie 0.1.0: Junie CLI agent, a /v1/models seed and launch defaults (#610, est7)
Adds @magpie-community/agent-junie 0.1.0 (est7): Junie CLI on magpie's Agents page, plus an explicit seed.mjs that writes one Junie model profile per /v1/models entry and sets settings.json's modelForLaunch to magpie's selected model. Registry entry sits with the other agents; README agents table row added.

The three changes asked for at 12df8a6 are in 75a8c4b: repository.url is magpie-community/plugins, the README installs from the Plugins tab / `magpie plugin add` and gives seed.mjs's installed path (XDG_CONFIG_HOME aware), and the registry entry moved to the agents. seed.mjs, junie.agent.js and the tests are unchanged since that review.

Reviewed at 75a8c4b, merged locally onto main 486a68a, on mote (macOS, Bun 1.4.2), HOME=$(mktemp -d):
- bun scripts/check.mjs: exit 0. bun test: 4735 pass, 12 skip, 0 fail (the 17 junie tests included).
- seed.mjs against a local fake /v1/models: one request, Bearer key and User-Agent Junie; two 0600 profiles with id/baseUrl/apiType/apiKey/displayName/providerName/maxContextLength; model-locations appended to the user's own list; modelForLaunch set, user's other ECO entry kept. --model with an absent id refused with no writes. --remove: profiles gone, config.json and settings.json back to their values (re-indented only).
- Keys checked against Junie's docs (custom-llm-models, config.json, MCP ~/.junie/mcp/mcp.json with mcpServers, ~/.junie/skills, ~/.junie/AGENTS.md) and, for the undocumented settings keys (modelForLaunch, onboardingDefaultModelChosen, providerForLaunch, mixForLaunch, ecoModePerModel, acpEcoModePerModel), by grepping the released Junie 3976.1 macOS jar: every key is present.
- npm has no @magpie-community/agent-junie yet (404), so 0.1.0 doesn't collide. No dependencies, no install scripts; the only network call is the gateway's own /v1/models with redirect "error".

Not tried: a real Junie session through magpie (needs a JetBrains sign-in), and whether Junie's own requests carry a User-Agent matching the agent's ua "junie".
2026-11-02 11:42:06 +08:00

250 lines
11 KiB
JavaScript

import fs from "node:fs"
import path from "node:path"
import os from "node:os"
import { randomUUID, createHash } from "node:crypto"
import { pathToFileURL } from "node:url"
import { parseArgs } from "node:util"
function read(file) {
try { return fs.readFileSync(file, "utf8") }
catch (error) { if (error.code !== "ENOENT") throw error }
}
function document(raw, name) {
const value = raw === undefined ? {} : JSON.parse(raw)
if (!value || typeof value !== "object" || Array.isArray(value)) throw Error(`${name}: expected an object`)
return value
}
function write(file, raw, expected = read(file)) {
if (raw === expected) return
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
const temporary = `${file}.${randomUUID()}.tmp`
try {
fs.writeFileSync(temporary, raw, { flag: "wx", mode: 0o600 })
if (read(file) !== expected) throw Error("configuration_changed")
fs.renameSync(temporary, file)
} finally {
if (fs.existsSync(temporary)) fs.unlinkSync(temporary)
}
}
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
const ECO_FIELDS = ["ecoModePerModel", "acpEcoModePerModel"]
const valueAt = (doc, key) => Object.hasOwn(doc, key) ? { value: doc[key] } : {}
const put = (doc, key, slot) => {
if (Object.hasOwn(slot, "value")) doc[key] = slot.value
else delete doc[key]
}
// The journal is written before each effect. Both the previous and pending
// value are recognized after a crash; a third value belongs to the user.
class Files {
constructor(file) {
this.file = file
this.state = document(read(file), "journal")
this.state.profiles ??= {}
this.state.documents ??= {}
this.state.ecoFields ??= {}
}
save() { write(this.file, JSON.stringify(this.state, null, 2) + "\n") }
patch(file, changes) {
const raw = read(file), doc = document(raw, file)
const entries = this.state.documents[file] ??= {}
let changed = false
for (const [key, desired] of Object.entries(changes)) {
const current = valueAt(doc, key)
if (same(current, desired)) continue
const entry = entries[key] ??= { before: current }
entry.previous = entry.wrote
entry.wrote = desired
put(doc, key, desired)
changed = true
}
if (!changed) return
this.save()
write(file, JSON.stringify(doc, null, 2) + "\n", raw)
}
ecoPreferences(file, selectors, eco, obsolete) {
const raw = read(file), doc = document(raw, file)
for (const field of ECO_FIELDS) {
const preferences = document(doc[field], field)
this.state.ecoFields[field] ??= Object.hasOwn(doc, field)
for (const selector of obsolete) delete preferences[selector]
for (const selector of selectors) {
if (eco !== undefined || !Object.hasOwn(preferences, selector)) preferences[selector] = eco ?? "on"
}
doc[field] = JSON.stringify(preferences)
}
// Only map presence is journaled. Native model preferences stay owned by
// Junie, including edits made between successive seeds.
this.save()
write(file, JSON.stringify(doc, null, 2) + "\n", raw)
}
checkProfile(file) {
const current = read(file), entry = this.state.profiles[file]
if (current === undefined) return
if (!entry || (current !== entry.wrote && current !== entry.previous)) throw Error("profile_changed")
}
profile(file, desired) {
this.checkProfile(file)
const raw = read(file)
if (raw === desired) return
this.state.profiles[file] = { previous: raw, wrote: desired }
this.save()
write(file, desired, raw)
}
removeProfile(file) {
this.checkProfile(file)
if (read(file) !== undefined) fs.unlinkSync(file)
delete this.state.profiles[file]
this.save()
}
restore({ settingsFile, modelsDir }) {
for (const file of Object.keys(this.state.profiles)) this.checkProfile(file)
const selectors = new Set(Object.keys(this.state.profiles).map(p => `custom:${path.basename(p, ".json")}`))
for (const file of new Set([...Object.keys(this.state.documents), settingsFile])) {
const entries = this.state.documents[file] ?? {}
const raw = read(file), doc = document(raw, file)
for (const [key, entry] of Object.entries(entries)) {
const current = valueAt(doc, key)
if (same(current, entry.wrote) || same(current, entry.previous)) put(doc, key, entry.before)
// Other discovery locations added while connected remain in place.
else if (key === "model-locations" && Array.isArray(current.value) && !entry.before.value?.includes(modelsDir)) {
doc[key] = current.value.filter(p => p !== modelsDir)
}
}
if (file === settingsFile) {
// This also handles an unchanged default that needed no patch entry.
if (selectors.has(doc.modelForLaunch)) {
const before = entries.modelForLaunch?.before ?? {}
if (selectors.has(before.value)) delete doc.modelForLaunch
else put(doc, "modelForLaunch", before)
}
for (const [field, existed] of Object.entries(this.state.ecoFields)) {
if (!Object.hasOwn(doc, field)) continue
const preferences = document(doc[field], field)
for (const selector of selectors) delete preferences[selector]
if (!existed && !Object.keys(preferences).length) delete doc[field]
else doc[field] = JSON.stringify(preferences)
}
}
if (raw !== undefined) write(file, JSON.stringify(doc, null, 2) + "\n", raw)
}
for (const file of Object.keys(this.state.profiles)) this.removeProfile(file)
this.state = { profiles: {}, documents: {} }
this.save()
}
}
export const profileID = id => "magpie-" + createHash("sha256").update(id).digest("hex").slice(0, 24)
export function catalog(payload, source) {
if (!Array.isArray(payload?.data) || !payload.data.length || payload.has_more) throw Error("Invalid or empty gateway catalog")
const result = new Map()
for (const model of payload.data) {
if (typeof model?.id !== "string" || !model.id || result.has(model.id)) throw Error("Invalid or duplicate model ID")
const profile = {
id: model.id,
baseUrl: source.baseUrl,
apiType: "OpenAICompletion",
apiKey: source.apiKey,
displayName: `${model.magpie_label || model.display_name || model.id} · Magpie`,
providerName: "Magpie",
}
if (Number.isSafeInteger(model.context_length) && model.context_length > 0) profile.maxContextLength = model.context_length
result.set(model.id, profile)
}
return result
}
function locations(junieHome) {
const root = path.resolve(junieHome)
return {
modelsDir: path.join(root, "magpie", "models"),
settingsFile: path.join(root, "settings.json"),
configFile: path.join(root, "config.json"),
stateFile: path.join(root, "magpie", "seed-state.json"),
}
}
export async function seed({ junieHome, model, eco, fetchImpl = globalThis.fetch }) {
if (eco !== undefined && !["on", "off"].includes(eco)) throw Error("--eco must be on or off")
const at = locations(junieHome)
const source = document(read(path.join(junieHome, "magpie", "connection.json")), "connection.json")
if (source.apiType !== "OpenAICompletion" || typeof source.baseUrl !== "string" ||
!source.baseUrl.endsWith("/chat/completions") || typeof source.id !== "string" || !source.id || typeof source.apiKey !== "string") {
throw Error("Connect Junie in Magpie before seeding its models")
}
const url = new URL(source.baseUrl.replace(/\/chat\/completions$/, "/models"))
if (!["http:", "https:"].includes(url.protocol)) throw Error("Unsupported gateway URL")
const response = await fetchImpl(url.href, {
headers: { Authorization: "Bearer " + source.apiKey, "User-Agent": "Junie", Accept: "application/json" },
signal: AbortSignal.timeout(10000), redirect: "error",
})
if (!response.ok) throw Error(`Gateway catalog returned HTTP ${response.status}`)
const models = catalog(await response.json(), source)
if (!models.has(model ?? source.id)) throw Error("The requested default model is absent from the gateway catalog")
const files = new Files(at.stateFile)
const settings = document(read(at.settingsFile), "settings.json")
const config = document(read(at.configFile), "config.json")
const discovery = config["model-locations"] ?? []
if (!Array.isArray(discovery)) throw Error("model-locations must be an array")
const profiles = new Map([...models].map(([id, profile]) => [path.join(at.modelsDir, profileID(id) + ".json"), JSON.stringify(profile, null, 2) + "\n"]))
const selectors = [...models.keys()].map(id => `custom:${profileID(id)}`)
const selected = `custom:${profileID(model ?? source.id)}`
const changes = {
modelForLaunch: { value: selected },
onboardingDefaultModelChosen: { value: "true" },
providerForLaunch: {},
mixForLaunch: {},
}
for (const field of ECO_FIELDS) document(settings[field], field)
// Refuse collisions or user edits before any mutation.
for (const file of new Set([...profiles.keys(), ...Object.keys(files.state.profiles)])) files.checkProfile(file)
for (const [file, raw] of profiles) files.profile(file, raw)
const obsolete = Object.keys(files.state.profiles).filter(file => !profiles.has(file))
if (!discovery.includes(at.modelsDir)) files.patch(at.configFile, { "model-locations": { value: [...discovery, at.modelsDir] } })
files.patch(at.settingsFile, changes)
files.ecoPreferences(at.settingsFile, selectors, eco, obsolete.map(p => `custom:${path.basename(p, ".json")}`))
for (const file of obsolete) files.removeProfile(file)
return { models: models.size, selected, eco: "unavailable_for_custom_profiles", modelsDir: at.modelsDir }
}
export function removeSeed(junieHome) {
const at = locations(junieHome)
if (read(at.stateFile) === undefined) return
new Files(at.stateFile).restore(at)
}
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
try {
const { values } = parseArgs({ options: {
model: { type: "string" }, eco: { type: "string" }, remove: { type: "boolean" }, help: { type: "boolean", short: "h" },
} })
if (values.help) console.log("Usage: node seed.mjs [--model provider/model] [--eco on|off] [--remove]\nSyncs Magpie's catalog into Junie. Native ECO is unavailable for custom profiles in Junie 3893.6.")
else {
const junieHome = process.env.JUNIE_HOME || path.join(os.homedir(), ".junie")
if (values.remove) {
if (values.model !== undefined || values.eco !== undefined) throw Error("--remove cannot be combined with --model or --eco")
removeSeed(junieHome)
console.log("Removed seeded profiles and restored unchanged launch settings. Magpie manages connection.json separately.")
} else {
const result = await seed({ junieHome, model: values.model, eco: values.eco })
console.log(`Synced ${result.models} models to ${result.modelsDir}; launch model: ${result.selected}. Restart Junie to reload /model.`)
console.log("ECO preferences are saved. Native ECO is unavailable for custom profiles in Junie 3893.6; saving on does not enable it.")
}
}
} catch (error) {
console.error(error.message)
process.exitCode = 1
}
}