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