UNPKG

histoire

Version:

A fast story book powered by Vite

57 lines (56 loc) • 3.13 kB
import { methods } from '@agentclientprotocol/sdk'; import { withAgentTimeout } from './agent-runtime.js'; /** Recognized category uses agent ordering to choose one prominent selector. */ function select(options, category) { return options.find(option => option.category === category && option.type === 'select'); } /** Flatten groups while retaining their display labels and exact agent value IDs. */ function projectSelector(option) { if (!option) return undefined; return { configId: option.id, currentValue: option.currentValue, options: option.options.flatMap((item) => { if ('group' in item) return item.options.map(value => ({ value: value.value, label: value.name, group: item.name })); const value = item; return [{ value: value.value, label: value.name }]; }).filter(item => item.value !== 'default') }; } /** Public choices omit raw SDK metadata and unrelated configuration categories. */ export function projectAgentConfig(agentId, options, warning) { return { agentId, model: projectSelector(select(options, 'model')), effort: projectSelector(select(options, 'thought_level')), mode: projectSelector(select(options, 'mode')), warning }; } /** Session option fields and the ACP categories they map to, applied in order. */ const FIELDS = [['model', 'model'], ['effort', 'thought_level'], ['mode', 'mode']]; /** Apply model first so dependent effort choices come from accepted model response; unchanged values send nothing. */ export async function applySessionConfig(runtime, sessionId, values, initial, timeout, isOwned) { let options = initial; const warnings = []; for (const [field, category] of FIELDS) { const value = values[field]; if (!value || (field === 'model' && value === 'default')) continue; const option = select(options, category); if (!option) { warnings.push(`Saved ${field} is unsupported; using agent default`); continue; } if (option.currentValue === value) continue; try { const response = await withAgentTimeout(runtime.connection.agent.request(methods.agent.session.setConfigOption, { sessionId, configId: option.id, value }), timeout, 'ACP configuration timed out'); if (!isOwned()) throw new Error('Agent runtime closed'); options = response.configOptions; } catch { if (!isOwned()) throw new Error('Agent runtime closed'); warnings.push(`Saved ${field} was rejected; using agent accepted configuration`); } } return { options, warning: warnings.length ? warnings.join('. ') : undefined }; } /** New sessions take per-comment choices first, then preset defaults. */ export function applyAgentSessionDefaults(runtime, sessionId, preset, initial, timeout, isOwned, overrides = {}) { return applySessionConfig(runtime, sessionId, { model: overrides.model ?? preset.model, effort: overrides.effort ?? preset.effort, mode: overrides.mode }, initial, timeout, isOwned); }