release: Mnemazine
Local-first memory system: raw material in, finished notes out. Synthesis on write. This release brings a guided install that explains itself and ends with a live demo pass, eleven fixes to the knowledge pipeline found by running it against a real archive of 216 sources, and a documentation pass in the project's own voice.
This commit is contained in:
+290
-130
@@ -1,52 +1,101 @@
|
||||
#!/usr/bin/env node
|
||||
// Provider-abstracted LLM bridge for Mnemazine. In Codex sessions, Codex is the
|
||||
// default engine; Claude is only a fallback when Codex is unavailable.
|
||||
// provider: MNEMAZINE_LLM = 'claude' | 'codex' (unset = Codex if present)
|
||||
// Both run as schema-instructed, web-capable headless agents. Default pipeline
|
||||
// never calls either — only the opt-in --deep path does.
|
||||
// Registry-driven LLM bridge for Mnemazine. There is no hard-coded list of
|
||||
// providers: the CLIs live as DATA in config/cli-registry.json (+ the gitignored
|
||||
// config/cli-registry.local.json overlay), and this module branches on the
|
||||
// declared CAPABILITY of the selected entry, never on a CLI name. Adding a CLI
|
||||
// (gemini, a local gemma, anything) is one JSON entry and zero edits here.
|
||||
//
|
||||
// Selection and validation come from mnemazine-cli-router.mjs; three schema
|
||||
// adapters are chosen by capability: json_schema_inline (claude --json-schema),
|
||||
// json_schema_file (codex exec --output-schema), json_in_prompt (universal).
|
||||
// The web-tool rule is general: a stage that needs web search only routes to a
|
||||
// carrier of the `web_search` capability; if none carries it the call fails with
|
||||
// a named cause — it never silently falls through to a CLI that cannot browse.
|
||||
//
|
||||
// Default pipeline never calls an LLM — only the opt-in --deep path does.
|
||||
import { spawnSync, spawn } from 'node:child_process'
|
||||
import { existsSync, readdirSync, promises as fs } from 'node:fs'
|
||||
import { existsSync, promises as fs } from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { loadRegistry, orderCandidates, pickSchemaAdapter } from './mnemazine-cli-router.mjs'
|
||||
|
||||
const PROGRESS_EVERY_MS = Number(process.env.MNEMAZINE_LLM_PROGRESS_EVERY_MS || '30000')
|
||||
const PROGRESS = process.env.MNEMAZINE_LLM_PROGRESS !== '0'
|
||||
const CONFIG_PROVIDER = process.env.MNEMAZINE_LLM || ''
|
||||
const TIMEOUT_MS = Number(process.env.MNEMAZINE_LLM_TIMEOUT_MS || '420000')
|
||||
// Physical ceiling for a prompt delivered as an argv element (getconf ARG_MAX on
|
||||
// this machine). A CLI without stdin_prompt gets the prompt in argv; over this we
|
||||
// fail with a named cause and move down the chain, never silently truncate.
|
||||
const ARG_MAX = Number(process.env.MNEMAZINE_ARG_MAX || '1048576')
|
||||
const HOME = os.homedir()
|
||||
const WEB_TOOL_RE = /^(WebSearch|WebFetch|mcp__firecrawl|mcp__tavily)$/i
|
||||
|
||||
function progress(label, message) {
|
||||
if (PROGRESS && label) process.stderr.write(`[llm] ${label} ${message}\n`)
|
||||
}
|
||||
|
||||
// Async process runner — non-blocking (unlike spawnSync), so many agent calls
|
||||
// can run concurrently as a swarm. Never rejects; returns a status/out/err.
|
||||
function runProc(bin, args, { input, timeoutMs, cwd } = {}) {
|
||||
function runProc(bin, args, { input, timeoutMs, cwd, label } = {}) {
|
||||
return new Promise(resolve => {
|
||||
let child
|
||||
try { child = spawn(bin, args, { cwd, stdio: ['pipe', 'pipe', 'pipe'] }) }
|
||||
catch (e) { return resolve({ status: 1, stdout: '', stderr: String(e.message) }) }
|
||||
let out = '', err = '', killed = false
|
||||
const started = Date.now()
|
||||
progress(label, `start timeout=${timeoutMs || 0}ms`)
|
||||
const t = timeoutMs ? setTimeout(() => { killed = true; child.kill('SIGKILL') }, timeoutMs) : null
|
||||
const heartbeat = PROGRESS && label && PROGRESS_EVERY_MS > 0
|
||||
? setInterval(() => progress(label, `running ${Math.round((Date.now() - started) / 1000)}s`), PROGRESS_EVERY_MS)
|
||||
: null
|
||||
if (heartbeat) heartbeat.unref()
|
||||
function done(status, stdout, stderr) {
|
||||
if (t) clearTimeout(t)
|
||||
if (heartbeat) clearInterval(heartbeat)
|
||||
progress(label, `done status=${status} elapsed=${Math.round((Date.now() - started) / 1000)}s`)
|
||||
resolve({ status, stdout, stderr })
|
||||
}
|
||||
child.stdout.on('data', d => { out += d })
|
||||
child.stderr.on('data', d => { err += d })
|
||||
child.on('error', e => { if (t) clearTimeout(t); resolve({ status: 1, stdout: out, stderr: String(e.message) }) })
|
||||
child.on('close', code => { if (t) clearTimeout(t); resolve({ status: killed ? 124 : (code ?? 1), stdout: out, stderr: err }) })
|
||||
if (input != null) { child.stdin.on('error', () => {}); child.stdin.write(input); child.stdin.end() }
|
||||
child.on('error', e => done(1, out, String(e.message)))
|
||||
child.on('close', code => done(killed ? 124 : (code ?? 1), out, err))
|
||||
// stdin is always closed: a CLI that reads stdin by default hangs forever on
|
||||
// an open pipe when the prompt went in as an argument (no input here).
|
||||
child.stdin.on('error', () => {})
|
||||
if (input != null) child.stdin.write(input)
|
||||
child.stdin.end()
|
||||
})
|
||||
}
|
||||
|
||||
const CONFIG_PROVIDER = process.env.MNEMAZINE_LLM || ''
|
||||
const TIMEOUT_MS = Number(process.env.MNEMAZINE_LLM_TIMEOUT_MS || '420000')
|
||||
const CODEX_BIN = process.env.MNEMAZINE_CODEX_BIN || '/Applications/Codex.app/Contents/Resources/codex'
|
||||
// --- registry (lazy, cached) --------------------------------------------------
|
||||
// Loaded on first real use, so the conservative local-only path never touches
|
||||
// it. A hidden/unreadable registry throws loudly here — no silent fall back to
|
||||
// old literals (that path is gone).
|
||||
let _registry
|
||||
function registry() {
|
||||
if (_registry) return _registry
|
||||
return (_registry = loadRegistry())
|
||||
}
|
||||
|
||||
const HOME = os.homedir()
|
||||
function getEntry(provider) {
|
||||
const entry = registry()[provider]
|
||||
if (!entry) throw new Error(`provider ${provider} not in registry`)
|
||||
return entry
|
||||
}
|
||||
|
||||
// Resolve the Claude CLI independently of how it was installed (npm global,
|
||||
// standalone installer, Homebrew, Claude Desktop, or the VSCode extension). Tries,
|
||||
// in order: explicit env -> login-shell PATH (respects the user's real install)
|
||||
// -> common absolute locations -> newest VSCode extension binary. Cached.
|
||||
let _claudeBin
|
||||
let _defaultProvider
|
||||
function resolveClaudeBin() {
|
||||
if (_claudeBin !== undefined) return _claudeBin
|
||||
if (process.env.MNEMAZINE_CLAUDE_BIN) return (_claudeBin = process.env.MNEMAZINE_CLAUDE_BIN)
|
||||
// Login shell: picks up however the user installed claude (Desktop/npm/etc.).
|
||||
const shell = process.env.SHELL || '/bin/zsh'
|
||||
const viaShell = spawnSync(shell, ['-lic', 'command -v claude'], { encoding: 'utf8' }).stdout || ''
|
||||
const shellHit = viaShell.trim().split('\n').pop()
|
||||
if (shellHit && existsSync(shellHit)) return (_claudeBin = shellHit)
|
||||
const candidates = [
|
||||
function providerChain(dataClass, capabilities) {
|
||||
const { candidates } = orderCandidates(registry(), { dataClass, capabilities })
|
||||
return candidates.map(c => c.name)
|
||||
}
|
||||
|
||||
// --- binary resolution (generic, per invoke[0]) -------------------------------
|
||||
// The binary is invoke[0], resolved via env override (MNEMAZINE_<NAME>_BIN),
|
||||
// then the login shell (however the user installed it), then a small table of
|
||||
// known install locations, then a bare PATH lookup at spawn. resolveClaudeBin's
|
||||
// knowledge survives as the `claude` entry of EXTRA_CANDIDATES — data, not a
|
||||
// name branch.
|
||||
const EXTRA_CANDIDATES = {
|
||||
claude: [
|
||||
path.join(HOME, '.claude/local/claude'),
|
||||
'/opt/homebrew/bin/claude',
|
||||
'/usr/local/bin/claude',
|
||||
@@ -54,17 +103,21 @@ function resolveClaudeBin() {
|
||||
path.join(HOME, '.npm-global/bin/claude'),
|
||||
'/Applications/Claude.app/Contents/Resources/claude'
|
||||
]
|
||||
for (const c of candidates) if (existsSync(c)) return (_claudeBin = c)
|
||||
// Newest VSCode extension native binary, if present.
|
||||
const extDir = path.join(HOME, '.vscode/extensions')
|
||||
try {
|
||||
const dirs = readdirSync(extDir).filter(d => d.startsWith('anthropic.claude-code-')).sort()
|
||||
for (const d of dirs.reverse()) {
|
||||
const bin = path.join(extDir, d, 'resources/native-binary/claude')
|
||||
if (existsSync(bin)) return (_claudeBin = bin)
|
||||
}
|
||||
} catch {}
|
||||
return (_claudeBin = 'claude') // last resort: bare PATH lookup at spawn time
|
||||
}
|
||||
const _binCache = {}
|
||||
function envBinKey(binName) {
|
||||
return `MNEMAZINE_${binName.toUpperCase().replace(/[^A-Z0-9]/g, '_')}_BIN`
|
||||
}
|
||||
function resolveBin(binName) {
|
||||
if (_binCache[binName] !== undefined) return _binCache[binName]
|
||||
const override = process.env[envBinKey(binName)]
|
||||
if (override) return (_binCache[binName] = override)
|
||||
const shell = process.env.SHELL || '/bin/zsh'
|
||||
const viaShell = spawnSync(shell, ['-lic', `command -v ${binName}`], { encoding: 'utf8' }).stdout || ''
|
||||
const hit = viaShell.trim().split('\n').pop()
|
||||
if (hit && existsSync(hit)) return (_binCache[binName] = hit)
|
||||
for (const c of (EXTRA_CANDIDATES[binName] || [])) if (existsSync(c)) return (_binCache[binName] = c)
|
||||
return (_binCache[binName] = binName) // last resort: bare PATH lookup at spawn time
|
||||
}
|
||||
|
||||
function binExists(bin) {
|
||||
@@ -73,13 +126,22 @@ function binExists(bin) {
|
||||
return which.status === 0 && Boolean(which.stdout.trim())
|
||||
}
|
||||
|
||||
function requireBin(provider, entry) {
|
||||
const bin = resolveBin(entry.invoke[0])
|
||||
if (!binExists(bin)) throw new Error(`${provider}: binary not found (${entry.invoke[0]}); set ${envBinKey(entry.invoke[0])}`)
|
||||
return bin
|
||||
}
|
||||
|
||||
// --- provider resolution ------------------------------------------------------
|
||||
let _defaultProvider
|
||||
export function defaultProvider() {
|
||||
if (_defaultProvider) return _defaultProvider
|
||||
if (CONFIG_PROVIDER) return CONFIG_PROVIDER
|
||||
const claudeBin = resolveClaudeBin()
|
||||
if (binExists(CODEX_BIN)) return (_defaultProvider = 'codex')
|
||||
if (binExists(claudeBin) && claudeUsable(claudeBin)) return (_defaultProvider = 'claude')
|
||||
return (_defaultProvider = 'claude')
|
||||
if (CONFIG_PROVIDER) return (_defaultProvider = CONFIG_PROVIDER) // owner pin (config.local.sh)
|
||||
// Unset: take the chain head for the pipeline's own material, preferring an
|
||||
// available binary. Production pins the provider, so unset only happens in
|
||||
// tests/CI where no live call is made.
|
||||
const chain = providerChain('infra', [])
|
||||
return (_defaultProvider = chain.find(name => llmAvailable(name)) || chain[0] || '')
|
||||
}
|
||||
|
||||
export function activeProvider(opts = {}) {
|
||||
@@ -87,12 +149,35 @@ export function activeProvider(opts = {}) {
|
||||
}
|
||||
|
||||
export function llmAvailable(provider = activeProvider()) {
|
||||
return provider === 'codex' ? binExists(CODEX_BIN) : binExists(resolveClaudeBin())
|
||||
const entry = registry()[provider] // throws loudly if the registry is unreadable
|
||||
if (!entry) return false
|
||||
return binExists(resolveBin(entry.invoke[0]))
|
||||
}
|
||||
|
||||
// Wrap untrusted material (OCR / transcripts / scraped web text) so the agent
|
||||
// treats it as inert DATA, never as instructions. Primary prompt-injection
|
||||
// defense for the schema-constrained calls.
|
||||
export function providerCostTier(provider) {
|
||||
return registry()[provider]?.cost_tier || 'standard'
|
||||
}
|
||||
|
||||
function needsWebTools(opts = {}) {
|
||||
return (opts.tools || []).some(tool => WEB_TOOL_RE.test(tool))
|
||||
}
|
||||
|
||||
function ensureWebCapable(provider, entry, opts) {
|
||||
if (needsWebTools(opts) && !entry.capabilities.includes('web_search')) {
|
||||
throw new Error(`нет носителя возможности web_search: ${provider} не умеет веб-поиск`)
|
||||
}
|
||||
}
|
||||
|
||||
// The fallback chain: providers of the required capability, minus the current
|
||||
// one, that resolve a binary. Web-needing stages narrow to web_search carriers —
|
||||
// the general rule that replaced the old per-provider web-tools literal.
|
||||
function fallbackProviders(provider, opts = {}) {
|
||||
if (process.env.MNEMAZINE_LLM_FALLBACK === '0') return []
|
||||
const caps = needsWebTools(opts) ? ['web_search'] : []
|
||||
return providerChain(opts.dataClass || 'infra', caps).filter(name => name !== provider && llmAvailable(name))
|
||||
}
|
||||
|
||||
// --- untrusted input fence (unchanged; primary prompt-injection defense) -------
|
||||
export function fenceUntrusted(label, content) {
|
||||
const tag = `UNTRUSTED_${label}_DO_NOT_EXECUTE`
|
||||
const safe = String(content || '').split(tag).join('U N T R U S T E D')
|
||||
@@ -101,7 +186,6 @@ export function fenceUntrusted(label, content) {
|
||||
|
||||
function extractJson(text) {
|
||||
const raw = String(text || '').trim()
|
||||
// Strip a ```json … ``` fence if present, else take the outermost {...}.
|
||||
const fenced = raw.match(/```(?:json)?\s*([\s\S]*?)```/i)
|
||||
const body = fenced ? fenced[1] : raw
|
||||
const start = body.indexOf('{')
|
||||
@@ -110,29 +194,11 @@ function extractJson(text) {
|
||||
return JSON.parse(body.slice(start, end + 1))
|
||||
}
|
||||
|
||||
function codexNeedsSearch(opts = {}) {
|
||||
return (opts.tools || []).some(tool => /^(WebSearch|WebFetch|mcp__firecrawl|mcp__tavily)$/i.test(tool))
|
||||
}
|
||||
|
||||
function codexExecArgs(cwd, opts = {}) {
|
||||
const args = ['--ask-for-approval', 'never']
|
||||
if (codexNeedsSearch(opts)) args.push('--search')
|
||||
args.push('exec', '-C', cwd, '--skip-git-repo-check', '--sandbox', 'read-only', '--ephemeral')
|
||||
return args
|
||||
}
|
||||
|
||||
function claudeUsable(bin) {
|
||||
const res = spawnSync(bin, ['-p', '--output-format', 'json'], {
|
||||
input: 'Return only {"ok":true}.',
|
||||
encoding: 'utf8',
|
||||
timeout: 15000
|
||||
})
|
||||
if (res.status !== 0) return false
|
||||
try {
|
||||
const envelope = JSON.parse(res.stdout || '{}')
|
||||
if (envelope?.is_error) return false
|
||||
} catch {}
|
||||
return true
|
||||
// --output-format json wraps the turn as { result: '<text>', ... } (claude). For
|
||||
// codex/kimi stdout is already plain text. Unwrap generically.
|
||||
function unwrap(stdout) {
|
||||
try { const e = JSON.parse(stdout); if (e && typeof e.result === 'string') return e.result } catch {}
|
||||
return stdout
|
||||
}
|
||||
|
||||
function strictOutputSchema(schema) {
|
||||
@@ -153,6 +219,28 @@ function strictOutputSchema(schema) {
|
||||
return out
|
||||
}
|
||||
|
||||
function assertSchema(value, schema, where = '$') {
|
||||
if (!schema || typeof schema !== 'object') return
|
||||
if ('const' in schema && value !== schema.const) throw new Error(`${where}: expected constant value`)
|
||||
if (schema.type === 'object') {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error(`${where}: expected object`)
|
||||
for (const key of schema.required || []) if (!(key in value)) throw new Error(`${where}.${key}: required`)
|
||||
const properties = schema.properties || {}
|
||||
if (schema.additionalProperties === false) for (const key of Object.keys(value)) if (!(key in properties)) throw new Error(`${where}.${key}: unexpected`)
|
||||
for (const [key, child] of Object.entries(properties)) if (key in value) assertSchema(value[key], child, `${where}.${key}`)
|
||||
}
|
||||
if (schema.type === 'array') {
|
||||
if (!Array.isArray(value)) throw new Error(`${where}: expected array`)
|
||||
if (schema.minItems && value.length < schema.minItems) throw new Error(`${where}: too few items`)
|
||||
value.forEach((item, index) => assertSchema(item, schema.items, `${where}[${index}]`))
|
||||
}
|
||||
if (schema.type === 'string') {
|
||||
if (typeof value !== 'string') throw new Error(`${where}: expected string`)
|
||||
if (schema.minLength && value.length < schema.minLength) throw new Error(`${where}: empty string`)
|
||||
}
|
||||
if (schema.type === 'boolean' && typeof value !== 'boolean') throw new Error(`${where}: expected boolean`)
|
||||
}
|
||||
|
||||
function procError(label, res) {
|
||||
const stderr = String(res.stderr || '').trim()
|
||||
if (stderr) return `${label} failed (status ${res.status}): ${stderr.slice(-400)}`
|
||||
@@ -161,80 +249,152 @@ function procError(label, res) {
|
||||
return `${label} failed (status ${res.status}): empty stderr/stdout`
|
||||
}
|
||||
|
||||
// --- Codex backend (headless pattern: --output-schema + -o) ---
|
||||
async function codexJsonCall(prompt, schema, opts) {
|
||||
if (!binExists(CODEX_BIN)) throw new Error(`codex binary not found: ${CODEX_BIN}`)
|
||||
const work = await fs.mkdtemp(path.join(os.tmpdir(), 'mnemazine-codex-'))
|
||||
// Prompt goes on stdin when the CLI declares stdin_prompt; otherwise as the last
|
||||
// argv element, bounded by ARG_MAX (never truncated — over the ceiling is a
|
||||
// named failure that moves to the next CLI in the chain).
|
||||
function deliverPrompt(entry, args, prompt) {
|
||||
if (entry.capabilities.includes('stdin_prompt')) return { args, input: prompt }
|
||||
const bytes = [...args, prompt].reduce((n, a) => n + Buffer.byteLength(String(a)) + 1, 0)
|
||||
if (bytes > ARG_MAX) throw new Error(`prompt exceeds ARG_MAX (${bytes} > ${ARG_MAX}); argv delivery impossible`)
|
||||
return { args: [...args, prompt], input: undefined }
|
||||
}
|
||||
|
||||
// Tool enabling is per schema-mechanism adapter: the inline adapter (claude)
|
||||
// grants tools via --allowedTools; the file adapter (codex) turns on web search
|
||||
// with the top-level --search inserted before `exec`. A new CLI using an
|
||||
// existing mechanism inherits this; a genuinely new mechanism is a new adapter.
|
||||
function withAllowedTools(args, opts) {
|
||||
const tools = opts.tools || []
|
||||
return tools.length ? [...args, '--allowedTools', tools.join(',')] : args
|
||||
}
|
||||
function insertSearch(args, entry, opts) {
|
||||
if (!needsWebTools(opts) || !entry.capabilities.includes('web_search')) return args
|
||||
const a = [...args]
|
||||
const i = a.indexOf('exec')
|
||||
if (i >= 0) a.splice(i, 0, '--search'); else a.unshift('--search')
|
||||
return a
|
||||
}
|
||||
function insertCwd(args, cwd) {
|
||||
const a = [...args]
|
||||
const i = a.indexOf('exec')
|
||||
if (i >= 0) a.splice(i + 1, 0, '-C', cwd); else a.push('-C', cwd)
|
||||
return a
|
||||
}
|
||||
|
||||
// --- schema adapters (chosen by capability) -----------------------------------
|
||||
async function inlineSchemaJson(provider, entry, prompt, schema, opts) {
|
||||
const bin = requireBin(provider, entry)
|
||||
let args = withAllowedTools(entry.invoke.slice(1), opts)
|
||||
args = [...args, '--json-schema', JSON.stringify(strictOutputSchema(schema))]
|
||||
const { args: finalArgs, input } = deliverPrompt(entry, args, prompt)
|
||||
const res = await runProc(bin, finalArgs, { input, timeoutMs: opts.timeoutMs || TIMEOUT_MS, label: opts.label || `${provider}-json` })
|
||||
if (res.status !== 0) throw new Error(procError(`${provider} json`, res))
|
||||
return extractJson(unwrap(res.stdout))
|
||||
}
|
||||
|
||||
async function fileSchemaJson(provider, entry, prompt, schema, opts) {
|
||||
const bin = requireBin(provider, entry)
|
||||
const work = await fs.mkdtemp(path.join(os.tmpdir(), 'mnemazine-cli-'))
|
||||
const cwd = opts.cwd || work
|
||||
const schemaFile = path.join(work, 'schema.json')
|
||||
const outFile = path.join(work, 'out.json')
|
||||
const promptFile = path.join(work, 'prompt.md')
|
||||
await fs.writeFile(schemaFile, JSON.stringify(strictOutputSchema(schema)), { encoding: 'utf8', mode: 0o600 })
|
||||
await fs.writeFile(promptFile, prompt, { encoding: 'utf8', mode: 0o600 })
|
||||
try {
|
||||
const res = await runProc(CODEX_BIN, [
|
||||
...codexExecArgs(cwd, opts),
|
||||
'--output-schema', schemaFile, '-o', outFile, '-'
|
||||
], { input: await fs.readFile(promptFile, 'utf8'), timeoutMs: opts.timeoutMs || TIMEOUT_MS })
|
||||
if (res.status !== 0) throw new Error(procError('codex exec', res))
|
||||
let args = insertSearch(entry.invoke.slice(1), entry, opts)
|
||||
args = insertCwd(args, cwd)
|
||||
args = [...args, '--output-schema', schemaFile, '-o', outFile, '-'] // prompt via stdin
|
||||
const res = await runProc(bin, args, { input: prompt, timeoutMs: opts.timeoutMs || TIMEOUT_MS, label: opts.label || `${provider}-json${needsWebTools(opts) ? ':search' : ''}` })
|
||||
if (res.status !== 0) throw new Error(procError(`${provider} exec`, res))
|
||||
const raw = await fs.readFile(outFile, 'utf8').catch(() => '')
|
||||
if (!raw.trim()) throw new Error('codex returned empty output')
|
||||
try { return JSON.parse(raw) } catch (err) { throw new Error(`codex returned non-JSON: ${err.message}; head: ${raw.slice(0, 200)}`) }
|
||||
if (!raw.trim()) throw new Error(`${provider} returned empty output`)
|
||||
try { return JSON.parse(raw) } catch (err) { throw new Error(`${provider} returned non-JSON: ${err.message}; head: ${raw.slice(0, 200)}`) }
|
||||
} finally {
|
||||
await fs.rm(work, { recursive: true, force: true }).catch(() => {})
|
||||
}
|
||||
}
|
||||
|
||||
// --- Claude backend (headless `claude -p`, JSON instructed in-prompt) ---
|
||||
// No --output-schema in Claude, so the schema is embedded and the result parsed.
|
||||
// Tools are opt-in via opts.tools (default none = no network); enrich/verify
|
||||
// pass WebSearch/WebFetch (+ MCP) to let Claude research with available tools.
|
||||
// Never uses the permission-bypass flag (constitution): unpermitted tools
|
||||
// simply do not run in -p mode.
|
||||
async function claudeJsonCall(prompt, schema, opts) {
|
||||
const bin = resolveClaudeBin()
|
||||
if (!binExists(bin)) throw new Error(`claude binary not found (tried env, PATH, common installs, VSCode): set MNEMAZINE_CLAUDE_BIN`)
|
||||
const tools = opts.tools || []
|
||||
const full = `${prompt}\n\nReturn ONLY a single JSON object matching this JSON Schema (no prose, no code fence):\n${JSON.stringify(schema)}`
|
||||
const args = ['-p', '--output-format', 'json']
|
||||
if (tools.length) args.push('--allowedTools', tools.join(','))
|
||||
const res = await runProc(bin, args, { input: full, timeoutMs: opts.timeoutMs || TIMEOUT_MS })
|
||||
if (res.status !== 0) throw new Error(procError('claude -p', res))
|
||||
// --output-format json wraps the turn: { type:'result', result:'<text>', ... }
|
||||
let envelope
|
||||
try { envelope = JSON.parse(res.stdout) } catch { envelope = null }
|
||||
const text = envelope && typeof envelope.result === 'string' ? envelope.result : res.stdout
|
||||
return extractJson(text)
|
||||
async function inPromptJson(provider, entry, prompt, schema, opts) {
|
||||
const bin = requireBin(provider, entry)
|
||||
const full = `${prompt}\n\nReturn ONLY a single JSON object matching this JSON Schema (no prose, no code fence):\n${JSON.stringify(strictOutputSchema(schema))}`
|
||||
const { args: finalArgs, input } = deliverPrompt(entry, entry.invoke.slice(1), full)
|
||||
const res = await runProc(bin, finalArgs, { input, timeoutMs: opts.timeoutMs || TIMEOUT_MS, label: opts.label || `${provider}-json` })
|
||||
if (res.status !== 0) throw new Error(procError(`${provider} json`, res))
|
||||
const output = extractJson(unwrap(res.stdout))
|
||||
assertSchema(output, strictOutputSchema(schema))
|
||||
return output
|
||||
}
|
||||
|
||||
// One schema-instructed call. Returns the parsed/validated-ish JSON object or
|
||||
// throws (callers degrade gracefully). provider via opts.provider or MNEMAZINE_LLM.
|
||||
async function jsonOnce(provider, prompt, schema, opts) {
|
||||
const entry = getEntry(provider)
|
||||
ensureWebCapable(provider, entry, opts)
|
||||
const adapter = pickSchemaAdapter(entry)
|
||||
if (adapter === 'json_schema_file') return fileSchemaJson(provider, entry, prompt, schema, opts)
|
||||
if (adapter === 'json_schema_inline') return inlineSchemaJson(provider, entry, prompt, schema, opts)
|
||||
return inPromptJson(provider, entry, prompt, schema, opts) // json_in_prompt (or a CLI declaring no schema mechanism)
|
||||
}
|
||||
|
||||
// One schema-instructed call. Returns the parsed JSON object or throws (callers
|
||||
// degrade gracefully). Provider via opts.provider or MNEMAZINE_LLM; on failure
|
||||
// walks the capability-filtered fallback chain.
|
||||
export async function llmJson(prompt, schema, opts = {}) {
|
||||
const provider = activeProvider(opts)
|
||||
return provider === 'codex' ? codexJsonCall(prompt, schema, opts) : claudeJsonCall(prompt, schema, opts)
|
||||
const chain = [...new Set([provider, ...fallbackProviders(provider, opts)])]
|
||||
let lastError
|
||||
for (const candidate of chain) {
|
||||
try {
|
||||
const result = await jsonOnce(candidate, prompt, schema, opts)
|
||||
// Surface the ACTUAL executor next to the result (not the requested one).
|
||||
// A silent fallback is now observable: the caller can print provider_used
|
||||
// and see that `deadcli` was answered by `claude`.
|
||||
opts.provider_used = candidate
|
||||
return result
|
||||
}
|
||||
catch (error) {
|
||||
lastError = error
|
||||
progress(opts.label || 'json', `fallback ${candidate}: ${String(error.message || error).slice(0, 160)}`)
|
||||
}
|
||||
}
|
||||
throw lastError || new Error('no provider available for llmJson')
|
||||
}
|
||||
|
||||
// Plain-text call (no schema) — used for vision/extraction fallback where the
|
||||
// agent reads a local file (image/PDF) and transcribes it. Tools opt-in; for
|
||||
// file reading pass tools:['Read']. Returns raw text. Claude primary; Codex at
|
||||
// parity (runs in the file's directory so it can open it).
|
||||
export async function llmText(prompt, opts = {}) {
|
||||
const provider = activeProvider(opts)
|
||||
if (provider === 'codex') {
|
||||
if (!binExists(CODEX_BIN)) throw new Error(`codex binary not found: ${CODEX_BIN}`)
|
||||
const res = await runProc(CODEX_BIN, [
|
||||
...codexExecArgs(opts.cwd || process.cwd(), opts), '-'
|
||||
], { input: prompt, timeoutMs: opts.timeoutMs || TIMEOUT_MS })
|
||||
if (res.status !== 0) throw new Error(procError('codex exec', res))
|
||||
// --- plain-text path (no schema; vision/extraction fallback) ------------------
|
||||
async function textOnce(provider, prompt, opts) {
|
||||
const entry = getEntry(provider)
|
||||
ensureWebCapable(provider, entry, opts)
|
||||
const bin = requireBin(provider, entry)
|
||||
const adapter = pickSchemaAdapter(entry)
|
||||
if (adapter === 'json_schema_file') {
|
||||
let args = insertSearch(entry.invoke.slice(1), entry, opts)
|
||||
args = insertCwd(args, opts.cwd || process.cwd())
|
||||
args = [...args, '-']
|
||||
const res = await runProc(bin, args, { input: prompt, timeoutMs: opts.timeoutMs || TIMEOUT_MS, label: opts.label || `${provider}-text${needsWebTools(opts) ? ':search' : ''}` })
|
||||
if (res.status !== 0) throw new Error(procError(`${provider} exec`, res))
|
||||
return String(res.stdout || '').trim()
|
||||
}
|
||||
const bin = resolveClaudeBin()
|
||||
if (!binExists(bin)) throw new Error('claude binary not found: set MNEMAZINE_CLAUDE_BIN')
|
||||
const args = ['-p', '--output-format', 'json']
|
||||
if (opts.tools?.length) args.push('--allowedTools', opts.tools.join(','))
|
||||
const res = await runProc(bin, args, { input: prompt, timeoutMs: opts.timeoutMs || TIMEOUT_MS })
|
||||
if (res.status !== 0) throw new Error(procError('claude -p', res))
|
||||
let envelope
|
||||
try { envelope = JSON.parse(res.stdout) } catch { envelope = null }
|
||||
return (envelope && typeof envelope.result === 'string' ? envelope.result : res.stdout).trim()
|
||||
// Flat-invoke CLI (claude inline / kimi in-prompt): tools via --allowedTools
|
||||
// only where the mechanism supports it (inline adapter).
|
||||
let args = entry.invoke.slice(1)
|
||||
if (adapter === 'json_schema_inline') args = withAllowedTools(args, opts)
|
||||
const { args: finalArgs, input } = deliverPrompt(entry, args, prompt)
|
||||
const res = await runProc(bin, finalArgs, { input, timeoutMs: opts.timeoutMs || TIMEOUT_MS, label: opts.label || `${provider}-text` })
|
||||
if (res.status !== 0) throw new Error(procError(`${provider} text`, res))
|
||||
return unwrap(res.stdout).trim()
|
||||
}
|
||||
|
||||
export async function llmText(prompt, opts = {}) {
|
||||
const provider = activeProvider(opts)
|
||||
const chain = [...new Set([provider, ...fallbackProviders(provider, opts)])]
|
||||
let lastError
|
||||
for (const candidate of chain) {
|
||||
try {
|
||||
const result = await textOnce(candidate, prompt, opts)
|
||||
opts.provider_used = candidate // actual executor, so a fallback is never silent
|
||||
return result
|
||||
}
|
||||
catch (error) {
|
||||
lastError = error
|
||||
progress(opts.label || 'text', `fallback ${candidate}: ${String(error.message || error).slice(0, 160)}`)
|
||||
}
|
||||
}
|
||||
throw lastError || new Error('no provider available for llmText')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user