Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/refresh-opencode-models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Refresh OpenCode Go Models

on:
schedule:
- cron: '0 3 * * 1'
workflow_dispatch:

permissions:
contents: write
pull-requests: write

concurrency:
group: refresh-opencode-models
cancel-in-progress: false

jobs:
refresh:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: "22"
cache: npm
- run: npm ci
- name: Refresh OpenCode Go tier models from models.dev
run: node scripts/refresh-opencode-models.mjs --write
- name: Check for policy changes
id: diff
run: |
if git diff --quiet config/api-tier-models.json; then
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
- name: Build and test updated policy
if: steps.diff.outputs.changed == 'true'
run: |
npm run build
npm test
- name: Open or update pull request
if: steps.diff.outputs.changed == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
BRANCH="chore/refresh-opencode-go-models"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -B "$BRANCH"
git add config/api-tier-models.json
git commit -m "chore: refresh OpenCode Go tier models from models.dev"
git push --force-with-lease origin "$BRANCH"
if ! gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number' | grep -q '[0-9]'; then
gh pr create \
--head "$BRANCH" \
--base main \
--title "chore: refresh OpenCode Go tier models from models.dev" \
--body "Automated weekly refresh of \`config/api-tier-models.json\` (\`tier_models.opencode_go\`) from \`https://models.dev/api.json\`."
fi
19 changes: 19 additions & 0 deletions config/api-tier-models.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@
"gemini-2.5-pro",
"gemini-2.5-flash"
]
},
"opencode_go": {
"simple": [
"glm-5.3-flash",
"deepseek-v4.1-flash",
"qwen3.8-flash"
],
"standard": [
"qwen4-max",
"glm-5.3",
"kimi-k3.1-pro",
"deepseek-v4-pro"
],
"complex": [
"kimi-k3.1-pro",
"deepseek-v4-pro",
"qwen4-max",
"glm-5.3"
]
}
},
"tier_families": {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "module",
"scripts": {
"build": "node scripts/build.mjs",
"test": "node test/oauth-ttl.mjs && node --test test/task-metadata.mjs && node --test test/routing.mjs && node --test test/request-schema.mjs && node test/delivery-log.mjs && node test/completion-webhooks.mjs && node test/native.mjs && node test/native.mjs --pigbibi && node test/workload-cache.mjs && node --test test/subscription-grants.mjs",
"refresh:opencode-models": "node scripts/refresh-opencode-models.mjs --write",
"test": "node test/oauth-ttl.mjs && node --test test/refresh-opencode-models.mjs && node --test test/task-metadata.mjs && node --test test/routing.mjs && node --test test/request-schema.mjs && node test/delivery-log.mjs && node test/completion-webhooks.mjs && node test/native.mjs && node test/native.mjs --pigbibi && node test/workload-cache.mjs && node --test test/subscription-grants.mjs",
"deploy": "echo \"Build first; deploy with explicit wrangler --config private path as documented\" && exit 1"
},
"dependencies": {
Expand Down
121 changes: 121 additions & 0 deletions scripts/refresh-opencode-models.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env node
// Refresh config/api-tier-models.json (tier_models.opencode_go) from models.dev/api.json.
// Tracks the latest stable flagship and flash models across GLM, Qwen, Kimi, and DeepSeek.
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';

const MODELS_DEV_URL = 'https://models.dev/api.json';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const POLICY_PATH = path.join(ROOT, 'config', 'api-tier-models.json');

const FAMILY_MATCHERS = {
glm_flash: {pattern: /^glm-(\d+(?:\.\d+)*)-flash$/, suffixRank: () => 1},
deepseek_flash: {pattern: /^deepseek-v(\d+(?:\.\d+)*)-flash$/, suffixRank: () => 1},
qwen_flash: {pattern: /^qwen(\d+(?:\.\d+)*)-flash$/, suffixRank: () => 1},
qwen_flagship: {pattern: /^qwen(\d+(?:\.\d+)*)-(max|plus)$/, suffixRank: (s) => (s === 'max' ? 2 : 1)},
glm_flagship: {pattern: /^glm-(\d+(?:\.\d+)*)$/, suffixRank: () => 1},
kimi_flagship: {pattern: /^kimi-k(\d+(?:\.\d+)*)(?:-(pro))?$/, suffixRank: (s) => (s === 'pro' ? 2 : 1)},
deepseek_flagship: {pattern: /^deepseek-v(\d+(?:\.\d+)*)(?:-(pro))?$/, suffixRank: (s) => (s === 'pro' ? 2 : 1)},
};

function parseVersion(raw) {
return String(raw).split('.').map((part) => Number.parseInt(part, 10) || 0);
}

function compareVersions(a, b) {
const len = Math.max(a.length, b.length);
for (let i = 0; i < len; i++) {
const diff = (a[i] ?? 0) - (b[i] ?? 0);
if (diff !== 0) return diff;
}
return 0;
}

function isStableModel(id, meta) {
if (typeof id !== 'string' || !id) return false;
if (/(?:^|-)(?:free|preview|alpha|beta|contributor)(?:$|-)/i.test(id)) return false;
if (meta && meta.tool_call === false) return false;
return true;
}

function pickLatest(models, matcherKey) {
const {pattern, suffixRank} = FAMILY_MATCHERS[matcherKey];
const candidates = [];
for (const [id, meta] of Object.entries(models)) {
if (!isStableModel(id, meta)) continue;
const match = pattern.exec(id);
if (!match) continue;
candidates.push({
id,
version: parseVersion(match[1]),
suffix: suffixRank(match[2] ?? ''),
releaseDate: typeof meta?.release_date === 'string' ? meta.release_date : '',
});
}
candidates.sort((a, b) => {
const v = compareVersions(b.version, a.version);
if (v !== 0) return v;
if (b.suffix !== a.suffix) return b.suffix - a.suffix;
return b.releaseDate.localeCompare(a.releaseDate);
});
return candidates[0]?.id ?? null;
}

export function selectOpenCodeGoTierModels(catalog) {
const models = catalog?.['opencode-go']?.models;
if (!models || typeof models !== 'object') {
throw new Error('models.dev catalog missing opencode-go.models');
}
const glmFlash = pickLatest(models, 'glm_flash');
const deepseekFlash = pickLatest(models, 'deepseek_flash');
const qwenFlash = pickLatest(models, 'qwen_flash');
const qwenFlag = pickLatest(models, 'qwen_flagship');
const glmFlag = pickLatest(models, 'glm_flagship');
const kimiFlag = pickLatest(models, 'kimi_flagship');
const deepseekFlag = pickLatest(models, 'deepseek_flagship');

const simple = [glmFlash, deepseekFlash, qwenFlash].filter(Boolean);
const standard = [qwenFlag, glmFlag, kimiFlag, deepseekFlag].filter(Boolean);
const complex = [kimiFlag, deepseekFlag, qwenFlag, glmFlag].filter(Boolean);
if (!simple.length || !standard.length || !complex.length) {
throw new Error('opencode-go catalog produced an empty tier');
}
return {simple, standard, complex};
}

async function fetchCatalog(url = MODELS_DEV_URL) {
const response = await fetch(url, {
headers: {'User-Agent': 'AiTaskService-model-refresh/1.0'},
signal: AbortSignal.timeout(20000),
});
if (!response.ok) throw new Error(`models.dev fetch failed: HTTP ${response.status}`);
return response.json();
}

async function main() {
const args = new Set(process.argv.slice(2));
const write = args.has('--write');
const check = args.has('--check');

const catalog = await fetchCatalog();
const nextTiers = selectOpenCodeGoTierModels(catalog);
const policy = JSON.parse(fs.readFileSync(POLICY_PATH, 'utf8'));
const currentTiers = policy.tier_models?.opencode_go ?? null;
const changed = JSON.stringify(currentTiers) !== JSON.stringify(nextTiers);

if (write && changed) {
policy.tier_models.opencode_go = nextTiers;
fs.writeFileSync(POLICY_PATH, JSON.stringify(policy, null, 2) + '\n', 'utf8');
}

console.log(JSON.stringify({changed, wrote: Boolean(write && changed), opencode_go: nextTiers}, null, 2));
if (check && changed) process.exitCode = 1;
}

if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
main().catch((err) => {
console.error(err?.message || err);
process.exit(1);
});
}
2 changes: 1 addition & 1 deletion src/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {field,parseJson,valueOf} from './canonical.mjs';
import {imageMaterials} from './image-materials.mjs';
import {callbackURL} from './webhooks.mjs';
import tierPolicy from '../config/api-tier-models.json' with {type: 'json'};
const POLICY_PROVIDER_KEY = {gemini_api: 'gemini', openai_api: 'openai', anthropic_api: 'anthropic'};
const POLICY_PROVIDER_KEY = {gemini_api: 'gemini', openai_api: 'openai', anthropic_api: 'anthropic', opencode_go: 'opencode_go'};
const object=v=>v!==null&&typeof v==='object'&&!Array.isArray(v);
const rejected=()=>{throw Object.assign(new Error('inference rejected'),{name:'SubmissionRejected'});};
const modelId = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
Expand Down
55 changes: 55 additions & 0 deletions test/refresh-opencode-models.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {selectOpenCodeGoTierModels} from '../scripts/refresh-opencode-models.mjs';
import {policyModelsForProvider, tierCandidates} from '../src/api.mjs';

test('selectOpenCodeGoTierModels picks latest stable models per family and ignores preview/free variants', () => {
const catalog = {
'opencode-go': {
models: {
'glm-5.2': {release_date: '2026-06-13', tool_call: true},
'glm-5.3': {release_date: '2026-08-14', tool_call: true},
'glm-5.4-preview': {release_date: '2026-12-01', tool_call: true},
'glm-5.3-flash': {release_date: '2026-08-26', tool_call: true},
'qwen3.7-plus': {release_date: '2026-06-02', tool_call: true},
'qwen3.8-max': {release_date: '2026-08-03', tool_call: true},
'qwen3.8-flash': {release_date: '2026-08-26', tool_call: true},
'qwen4-max': {release_date: '2026-11-16', tool_call: true},
'kimi-k3': {release_date: '2026-07-16', tool_call: true},
'kimi-k3.1-pro': {release_date: '2026-10-23', tool_call: true},
'deepseek-v4-pro': {release_date: '2026-04-24', tool_call: true},
'deepseek-v4.1-flash': {release_date: '2026-10-17', tool_call: true},
'deepseek-v5-free': {release_date: '2026-12-01', tool_call: true},
},
},
};
assert.deepEqual(selectOpenCodeGoTierModels(catalog), {
simple: ['glm-5.3-flash', 'deepseek-v4.1-flash', 'qwen3.8-flash'],
standard: ['qwen4-max', 'glm-5.3', 'kimi-k3.1-pro', 'deepseek-v4-pro'],
complex: ['kimi-k3.1-pro', 'deepseek-v4-pro', 'qwen4-max', 'glm-5.3'],
});
});

test('opencode_go provider resolves tier_models and models from policy config', () => {
const env = {
API_PROVIDERS: {
'opencode-go': {
type: 'opencode_go',
enabled: true,
api_usage_authorized: true,
model: 'qwen4-max',
tier_models: 'policy',
key_env: 'AI_SERVICE_OPENCODE_GO_API_KEY',
},
},
};
const admitted = policyModelsForProvider(env, 'opencode-go');
assert.ok(admitted.includes('qwen4-max'));
assert.ok(admitted.includes('glm-5.3'));
assert.ok(admitted.includes('kimi-k3.1-pro'));
assert.ok(admitted.includes('deepseek-v4-pro'));
assert.ok(admitted.includes('glm-5.3-flash'));
assert.deepEqual(tierCandidates(env.API_PROVIDERS['opencode-go'], 'simple')[0], 'glm-5.3-flash');
assert.deepEqual(tierCandidates(env.API_PROVIDERS['opencode-go'], 'standard')[0], 'qwen4-max');
assert.deepEqual(tierCandidates(env.API_PROVIDERS['opencode-go'], 'complex')[0], 'kimi-k3.1-pro');
});
19 changes: 13 additions & 6 deletions wrangler.example.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@
"PROJECT_POLICIES": {},
"AGENT_PROVIDERS": {
"opencode": {
"type": "github_dispatch",
"type": "cloudflare_workflow",
"enabled": true,
"url": "https://api.github.com/repos/example-org/ExampleProject/actions/workflows/opencode-agent.yml/dispatches",
"ref": "main",
"key_env": "AI_SERVICE_GITHUB_DISPATCH_TOKEN",
"binding": "OPENCODE_WORKFLOW",
"base_url": "https://opencode.ai/inference/go/openai/v1",
"key_env": "AI_SERVICE_OPENCODE_GO_API_KEY",
"fallback_models": [
"qwen4-max",
"kimi-k3.1-pro",
"deepseek-v4-pro",
"glm-5.3"
],
"capabilities": {
"image_input": false
"image_input": true
}
},
"clauderoutine": {
Expand Down Expand Up @@ -73,7 +79,8 @@
"enabled": false,
"api_usage_authorized": false,
"base_url": "https://opencode.ai/inference/go/openai/v1",
"model": "CONFIGURE_APPROVED_OPENCODE_MODEL",
"model": "qwen4-max",
"tier_models": "policy",
"key_env": "AI_SERVICE_OPENCODE_GO_API_KEY",
"max_output_tokens": 4096,
"timeout_seconds": 90
Expand Down
Loading