Browse Source

fix: sync TUI agent variant from message updates

lf 1 month ago
parent
commit
2a5e098c55
3 changed files with 60 additions and 10 deletions
  1. 53 7
      src/index.ts
  2. 6 2
      src/tui-state.ts
  3. 1 1
      src/tui.ts

+ 53 - 7
src/index.ts

@@ -401,6 +401,36 @@ const OhMyOpenCodeLite: Plugin = async (ctx) => {
 
   companionManager.onLoad();
 
+  function resolveTuiVariantForModel(
+    agentName: string,
+    model: string,
+  ): string | undefined {
+    const configEntry = config.agents?.[agentName];
+    const defaultVariant =
+      typeof configEntry?.variant === 'string'
+        ? configEntry.variant
+        : undefined;
+    const chainMatches = modelArrayMap[agentName]?.filter(
+      (entry) => entry.id === model,
+    );
+    if (chainMatches?.length === 1) {
+      return chainMatches[0].variant ?? defaultVariant;
+    }
+    if (chainMatches && chainMatches.length > 1) {
+      return undefined;
+    }
+
+    if (
+      typeof configEntry?.model === 'string' &&
+      configEntry.model === model &&
+      defaultVariant
+    ) {
+      return defaultVariant;
+    }
+
+    return undefined;
+  }
+
   return {
     name: 'oh-my-opencode-slim',
 
@@ -714,6 +744,10 @@ const OhMyOpenCodeLite: Plugin = async (ctx) => {
             agent?: string;
             providerID?: string;
             modelID?: string;
+            model?: {
+              providerID?: string;
+              modelID?: string;
+            };
             sessionID?: string;
           };
           sessionID?: string;
@@ -725,14 +759,26 @@ const OhMyOpenCodeLite: Plugin = async (ctx) => {
 
       if (event.type === 'message.updated') {
         const info = event.properties?.info;
-        if (
-          typeof info?.agent === 'string' &&
-          typeof info.providerID === 'string' &&
-          typeof info.modelID === 'string'
-        ) {
+        const providerID =
+          typeof info?.providerID === 'string'
+            ? info.providerID
+            : typeof info?.model?.providerID === 'string'
+              ? info.model.providerID
+              : undefined;
+        const modelID =
+          typeof info?.modelID === 'string'
+            ? info.modelID
+            : typeof info?.model?.modelID === 'string'
+              ? info.model.modelID
+              : undefined;
+        if (typeof info?.agent === 'string' && providerID && modelID) {
+          const agentName = resolveRuntimeAgentName(config, info.agent);
+          const model = `${providerID}/${modelID}`;
+          const variant = resolveTuiVariantForModel(agentName, model);
           recordTuiAgentModel({
-            agentName: resolveRuntimeAgentName(config, info.agent),
-            model: `${info.providerID}/${info.modelID}`,
+            agentName,
+            model,
+            variant: variant ?? null,
           });
         }
       }

+ 6 - 2
src/tui-state.ts

@@ -90,12 +90,16 @@ export function recordTuiAgentModels(input: {
 export function recordTuiAgentModel(input: {
   agentName: string;
   model: string;
-  variant?: string;
+  variant?: string | null;
 }): void {
   updateSnapshot((snapshot) => {
     snapshot.agentModels[input.agentName] = input.model;
     if (input.variant !== undefined) {
-      snapshot.agentVariants[input.agentName] = input.variant;
+      if (input.variant === null) {
+        delete snapshot.agentVariants[input.agentName];
+      } else {
+        snapshot.agentVariants[input.agentName] = input.variant;
+      }
     }
   });
 }

+ 1 - 1
src/tui.ts

@@ -94,7 +94,7 @@ function agentRow(
   label: string,
   model: string,
   variant: string | undefined,
-  theme: { textMuted: unknown; text: unknown },
+  theme: { textMuted: unknown },
 ): JSX.Element {
   const modelParts = splitSidebarModelId(model);
   const detailRows: JSX.Element[] = [];