|
1 | 1 | import type { Plugin } from '@opencode-ai/plugin'; |
2 | 2 | import { createAgents, getAgentConfigs, getDisabledAgents } from './agents'; |
3 | 3 | import { buildOrchestratorPrompt } from './agents/orchestrator'; |
4 | | -import { loadPluginConfig, type MultiplexerConfig } from './config'; |
| 4 | +import { |
| 5 | + deepMerge, |
| 6 | + loadPluginConfig, |
| 7 | + type MultiplexerConfig, |
| 8 | +} from './config'; |
| 9 | +import { AGENT_ALIASES } from './config/constants'; |
5 | 10 | import { parseList } from './config/agent-mcps'; |
| 11 | +import { |
| 12 | + getActiveRuntimePreset, |
| 13 | + getPreviousRuntimePreset, |
| 14 | + setActiveRuntimePreset, |
| 15 | +} from './config/runtime-preset'; |
6 | 16 | import { CouncilManager } from './council'; |
7 | 17 | import { |
8 | 18 | createApplyPatchHook, |
@@ -85,6 +95,11 @@ async function probeJSDOM(): Promise<string | null> { |
85 | 95 | } |
86 | 96 | } |
87 | 97 |
|
| 98 | +// Module-level runtime preset tracking. Survives plugin re-inits triggered |
| 99 | +// by client.config.update() → Instance.dispose(). When the plugin function |
| 100 | +// re-runs, it checks this variable and applies the runtime preset instead |
| 101 | +// of the config file's preset. State lives in config/runtime-preset.ts. |
| 102 | + |
88 | 103 | const OhMyOpenCodeLite: Plugin = async (ctx) => { |
89 | 104 | const sessionId = new Date().toISOString().replace(/[-:]/g, '').slice(0, 15); |
90 | 105 | initLogger(sessionId); |
@@ -129,6 +144,25 @@ const OhMyOpenCodeLite: Plugin = async (ctx) => { |
129 | 144 |
|
130 | 145 | try { |
131 | 146 | config = loadPluginConfig(ctx.directory); |
| 147 | + |
| 148 | + // Safety net: if a runtime preset was set via /preset command and |
| 149 | + // OpenCode ever fully re-runs the plugin function (not just the |
| 150 | + // config() hook), override config.preset so agents are created with |
| 151 | + // the correct models. Currently only the config() hook re-runs after |
| 152 | + // Instance.dispose(), so this is a defensive guard. |
| 153 | + const runtimePreset = getActiveRuntimePreset(); |
| 154 | + if (runtimePreset && config.presets?.[runtimePreset]) { |
| 155 | + config.preset = runtimePreset; |
| 156 | + // Re-merge runtime preset into config.agents (loadPluginConfig |
| 157 | + // already merged the config-file preset, not the runtime one). |
| 158 | + // Runtime preset is override so it wins over config-file preset. |
| 159 | + const presetAgents = config.presets[runtimePreset]; |
| 160 | + config.agents = deepMerge(config.agents, presetAgents); |
| 161 | + } else if (runtimePreset) { |
| 162 | + // Preset was deleted from config since last switch — clear stale state |
| 163 | + setActiveRuntimePreset(null); |
| 164 | + } |
| 165 | + |
132 | 166 | disabledAgents = getDisabledAgents(config); |
133 | 167 | rewriteDisplayNameMentions = createDisplayNameMentionRewriter(config); |
134 | 168 | agentDefs = createAgents(config); |
@@ -472,6 +506,122 @@ const OhMyOpenCodeLite: Plugin = async (ctx) => { |
472 | 506 | } |
473 | 507 | } |
474 | 508 |
|
| 509 | + // Runtime preset override: if /preset switched to a runtime preset, |
| 510 | + // override the model/variant/temperature from the preset's agent |
| 511 | + // config. This runs after the normal model resolution because the |
| 512 | + // config() hook re-runs with stale modelArrayMap after dispose(), |
| 513 | + // but the runtime preset data is in the captured `config` closure. |
| 514 | + const runtimePresetName = getActiveRuntimePreset(); |
| 515 | + if (runtimePresetName && config.presets?.[runtimePresetName]) { |
| 516 | + const runtimePreset = config.presets[runtimePresetName]; |
| 517 | + for (const [agentName, override] of Object.entries(runtimePreset)) { |
| 518 | + // Resolve legacy alias keys (e.g. "explore" → "explorer") |
| 519 | + // so presets using aliases work in this path. |
| 520 | + const resolvedName = AGENT_ALIASES[agentName] ?? agentName; |
| 521 | + const entry = configAgent[resolvedName] as |
| 522 | + | Record<string, unknown> |
| 523 | + | undefined; |
| 524 | + if (!entry) continue; |
| 525 | + |
| 526 | + if (typeof override.model === 'string') { |
| 527 | + entry.model = override.model; |
| 528 | + } else if ( |
| 529 | + Array.isArray(override.model) && |
| 530 | + override.model.length > 0 |
| 531 | + ) { |
| 532 | + const first = override.model[0]; |
| 533 | + entry.model = typeof first === 'string' ? first : first.id; |
| 534 | + // Extract inline variant from array-form model entry |
| 535 | + if (typeof first !== 'string' && first.variant) { |
| 536 | + entry.variant = first.variant; |
| 537 | + } |
| 538 | + } |
| 539 | + // Explicitly set or clear scalar fields so switching from |
| 540 | + // Preset A (which sets a field) to Preset B (which doesn't) |
| 541 | + // doesn't leave stale values behind. |
| 542 | + if (typeof override.variant === 'string') { |
| 543 | + entry.variant = override.variant; |
| 544 | + } else if ('variant' in override) { |
| 545 | + delete entry.variant; |
| 546 | + } |
| 547 | + if (typeof override.temperature === 'number') { |
| 548 | + entry.temperature = override.temperature; |
| 549 | + } else if ('temperature' in override) { |
| 550 | + delete entry.temperature; |
| 551 | + } |
| 552 | + if ( |
| 553 | + override.options && |
| 554 | + typeof override.options === 'object' && |
| 555 | + !Array.isArray(override.options) |
| 556 | + ) { |
| 557 | + entry.options = override.options; |
| 558 | + } else if ('options' in override) { |
| 559 | + delete entry.options; |
| 560 | + } |
| 561 | + log('[plugin] runtime preset override', { |
| 562 | + preset: runtimePresetName, |
| 563 | + agent: agentName, |
| 564 | + model: entry.model as string, |
| 565 | + }); |
| 566 | + } |
| 567 | + |
| 568 | + // Reset agents from the previous preset that aren't in the new one. |
| 569 | + // The stale model resolution above overwrites the reset values sent |
| 570 | + // by preset-manager, so we re-apply them here from config-file |
| 571 | + // baseline. |
| 572 | + const prevPresetName = getPreviousRuntimePreset(); |
| 573 | + if (prevPresetName && config.presets?.[prevPresetName]) { |
| 574 | + const prevPreset = config.presets[prevPresetName]; |
| 575 | + // Build resolved key set from new preset for correct comparison |
| 576 | + // (handles alias keys like "explore" → "explorer") |
| 577 | + const newPresetResolved = new Set( |
| 578 | + Object.keys(runtimePreset).map( |
| 579 | + (k) => AGENT_ALIASES[k] ?? k, |
| 580 | + ), |
| 581 | + ); |
| 582 | + for (const agentName of Object.keys(prevPreset)) { |
| 583 | + const resolvedName = |
| 584 | + AGENT_ALIASES[agentName] ?? agentName; |
| 585 | + if (newPresetResolved.has(resolvedName)) |
| 586 | + continue; // new preset handles it |
| 587 | + const entry = configAgent[resolvedName] as |
| 588 | + | Record<string, unknown> |
| 589 | + | undefined; |
| 590 | + if (!entry) continue; |
| 591 | + // Reset to config-file baseline |
| 592 | + const baseline = |
| 593 | + config.agents?.[resolvedName]; |
| 594 | + if (typeof baseline?.model === 'string') { |
| 595 | + entry.model = baseline.model; |
| 596 | + } |
| 597 | + if (typeof baseline?.variant === 'string') { |
| 598 | + entry.variant = baseline.variant; |
| 599 | + } else if (baseline && 'variant' in baseline) { |
| 600 | + delete entry.variant; |
| 601 | + } |
| 602 | + if (typeof baseline?.temperature === 'number') { |
| 603 | + entry.temperature = baseline.temperature; |
| 604 | + } else if (baseline && 'temperature' in baseline) { |
| 605 | + delete entry.temperature; |
| 606 | + } |
| 607 | + if ( |
| 608 | + baseline?.options && |
| 609 | + typeof baseline.options === 'object' && |
| 610 | + !Array.isArray(baseline.options) |
| 611 | + ) { |
| 612 | + entry.options = baseline.options; |
| 613 | + } else if (baseline && 'options' in baseline) { |
| 614 | + delete entry.options; |
| 615 | + } |
| 616 | + log('[plugin] runtime preset reset from previous', { |
| 617 | + previousPreset: prevPresetName, |
| 618 | + agent: resolvedName, |
| 619 | + model: entry.model as string, |
| 620 | + }); |
| 621 | + } |
| 622 | + } |
| 623 | + } |
| 624 | + |
475 | 625 | // Merge MCP configs |
476 | 626 | const configMcp = opencodeConfig.mcp as |
477 | 627 | | Record<string, unknown> |
|
0 commit comments