AgentSkillsCN

opencode-model-discovery-cache-invalidation

当OmO-Agent-Config(或类似工具)即使你已更新opencode.json,却仍无法显示新添加的OpenCode提供商/模型时使用此功能。 症状:UI仍然显示旧的提供商列表;API返回`cached: true`;模型需等待几分钟,或强制刷新后才会出现。 解决方案:解决模型缓存过期问题,同时调整`opencode models --verbose`输出的模型标题正则表达式,使其更加宽松。

SKILL.md
--- frontmatter
name: opencode-model-discovery-cache-invalidation
description: |
  Use when OmO-Agent-Config (or similar tools) doesn't show newly-added OpenCode providers/models even though you updated opencode.json.
  Symptoms: UI still shows old provider list; API reports `cached: true`; models appear after waiting a few minutes or forcing refresh.
  Solves: stale model cache + overly-strict model header regex for `opencode models --verbose` output.

OpenCode model discovery: cache invalidation + robust parsing

Problem

After adding/enabling a provider or plan (e.g. a Codex plan) in ~/.config/opencode/opencode.json, tools that consume opencode models --verbose may keep showing the old provider/model list.

Context / Trigger Conditions

  • The tool caches results from opencode models --verbose (e.g. ~/.config/opencode/cache/models-cache.json).
  • The UI/API indicates the model list was served from cache (cached: true).
  • Newly added provider/models appear only after cache TTL expires or after a manual refresh.

Secondary trigger:

  • A provider/model exists in opencode models --verbose, but parsing fails because the tool uses a too-strict header regex for lines like providerID/modelID (common misses: underscores/dots in provider IDs).

Solution

  1. Invalidate model cache when the OpenCode provider config changes
  • Compare mtime of ~/.config/opencode/opencode.json to the cache timestamp.
  • If opencode.json is newer than the cache, treat cache as stale and refetch.
  1. Use a more permissive model header regex
  • Header lines in opencode models --verbose are emitted as providerID/modelID on their own line.
  • Use a regex that supports common provider/model ID characters beyond [a-z0-9-] (at least allow _ and .).

Example pattern (case-insensitive):

js
const modelHeaderLineRe = /^[a-z0-9_.-]+\/[a-z0-9_.-]+[a-z0-9_.-:/]*$/i;

Verification

  • Warm-cache behavior:

    • First load: cached: false
    • Second load: cached: true
  • After touching/modifying opencode.json:

    • Next load should return cached: false and include the new provider in the providers list.

Example

  • Add a new provider/plan in ~/.config/opencode/opencode.json.
  • Start tool, see provider missing.
  • With cache invalidation, provider appears immediately after config change (no waiting for TTL).

Notes

  • Brace-count parsing of JSON blocks can be fragile if {/} appear inside JSON strings. Prefer a parser that can handle this if upstream ever emits braces inside string values.