Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

feat(agent): extend adaptive-only guard + OpenRouter reasoning pass-through

Matt Kissinger624788032a4a
diff --git a/src/agent/providers.test.ts b/src/agent/providers.test.ts
index bf68a8e3..e7605bd8 100644
--- a/src/agent/providers.test.ts
+++ b/src/agent/providers.test.ts
@@ -8,7 +8,7 @@
* keys so native construction never touches the network).
*/
import { test, expect, describe, beforeAll, afterAll } from 'bun:test';
-import { resolveKilnAgentModel, makeKilnModel } from './providers';
+import { resolveKilnAgentModel, makeKilnModel, resolveOpenRouterReasoning } from './providers';
describe('resolveKilnAgentModel', () => {
test('explicit provider prefixes win and keep the model id intact', () => {
@@ -77,7 +77,13 @@ describe('resolveKilnAgentModel', () => {
describe('makeKilnModel', () => {
const saved: Record<string, string | undefined> = {};
- const KEYS = ['GEMINI_API_KEY', 'ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'OPENROUTER_API_KEY', 'MODEL_API_KEY'];
+ const KEYS = [
+ 'GEMINI_API_KEY',
+ 'ANTHROPIC_API_KEY',
+ 'OPENAI_API_KEY',
+ 'OPENROUTER_API_KEY',
+ 'MODEL_API_KEY',
+ ];
beforeAll(() => {
for (const k of KEYS) {
@@ -148,6 +154,38 @@ describe('makeKilnModel', () => {
).toBe('byok-meta');
});
+ describe('openrouter reasoning control (thinking → unified reasoning)', () => {
+ test('effort keywords pass through; max maps to xhigh (OpenRouter vocabulary)', () => {
+ expect(resolveOpenRouterReasoning('high')).toEqual({ effort: 'high' });
+ expect(resolveOpenRouterReasoning('xhigh')).toEqual({ effort: 'xhigh' });
+ expect(resolveOpenRouterReasoning('max')).toEqual({ effort: 'xhigh' });
+ expect(resolveOpenRouterReasoning(' HIGH ')).toEqual({ effort: 'high' });
+ });
+
+ test('numbers become a reasoning token budget floored to 1024', () => {
+ expect(resolveOpenRouterReasoning(8000)).toEqual({ max_tokens: 8000 });
+ expect(resolveOpenRouterReasoning(512)).toEqual({ max_tokens: 1024 });
+ expect(resolveOpenRouterReasoning('8000')).toEqual({ max_tokens: 8000 });
+ });
+
+ test('unset / 0 / empty / unknown keywords send nothing', () => {
+ expect(resolveOpenRouterReasoning(undefined)).toBeUndefined();
+ expect(resolveOpenRouterReasoning(0)).toBeUndefined();
+ expect(resolveOpenRouterReasoning('')).toBeUndefined();
+ expect(resolveOpenRouterReasoning('ultra')).toBeUndefined();
+ });
+
+ test('an OpenRouter-hosted Claude with thinking still constructs the Vercel bridge', () => {
+ const m = makeKilnModel({
+ provider: 'openrouter',
+ model: 'anthropic/claude-opus-4.8',
+ maxTokens: 64000,
+ thinking: 'high',
+ });
+ expect(m.constructor.name).toBe('VercelModel');
+ });
+ });
+
test('meta uses the Meta base URL and explicit Responses params', () => {
const model = makeKilnModel({ provider: 'meta', model: 'muse-spark-1.1', maxTokens: 4096 });
const cfg = (model as unknown as { getConfig(): Record<string, unknown> }).getConfig();
@@ -185,18 +223,23 @@ describe('makeKilnModel', () => {
test('numeric budget maps to the legacy enabled shape + interleaved beta (pre-adaptive models)', () => {
const cfg = getCfg(
- makeKilnModel({ provider: 'anthropic', model: 'claude-opus-4-8', thinking: 8000 }),
+ makeKilnModel({ provider: 'anthropic', model: 'claude-haiku-4-5', thinking: 8000 }),
);
expect(cfg['params']).toEqual({ thinking: { type: 'enabled', budget_tokens: 8000 } });
expect(cfg['betas']).toEqual(['interleaved-thinking-2025-05-14']);
});
- test('Sonnet 5 ignores numeric thinking budgets because it is adaptive-only', () => {
- const cfg = getCfg(
- makeKilnModel({ provider: 'anthropic', model: 'claude-sonnet-5', thinking: 8000 }),
- );
- expect(cfg['params']).toBeUndefined();
- expect(cfg['betas']).toBeUndefined();
+ test('adaptive-only models ignore numeric thinking budgets (budget_tokens 400s on them)', () => {
+ for (const model of [
+ 'claude-sonnet-5',
+ 'claude-fable-5',
+ 'claude-opus-4-8',
+ 'claude-opus-4-7',
+ ]) {
+ const cfg = getCfg(makeKilnModel({ provider: 'anthropic', model, thinking: 8000 }));
+ expect(cfg['params']).toBeUndefined();
+ expect(cfg['betas']).toBeUndefined();
+ }
});
test('env keyword/number applies (numbers floored to 1024); descriptor 0 forces default; google ignores it', () => {
@@ -210,7 +253,7 @@ describe('makeKilnModel', () => {
});
process.env['KILN_THINKING'] = '512';
- const floored = getCfg(makeKilnModel({ provider: 'anthropic', model: 'claude-opus-4-8' }));
+ const floored = getCfg(makeKilnModel({ provider: 'anthropic', model: 'claude-haiku-4-5' }));
expect(floored['params']).toEqual({ thinking: { type: 'enabled', budget_tokens: 1024 } });
const off = getCfg(
diff --git a/src/agent/providers.ts b/src/agent/providers.ts
index 7b959d99..e561b4cd 100644
--- a/src/agent/providers.ts
+++ b/src/agent/providers.ts
@@ -21,6 +21,40 @@ import type { LanguageModelV3 } from '@ai-sdk/provider';
import { ensureStreamStart } from './stream-start';
+/** OpenRouter's unified reasoning control (@openrouter/ai-sdk-provider chat setting):
+ * an effort keyword or a hard reasoning-token budget. Passed straight through to
+ * the provider, which translates per upstream vendor (Anthropic adaptive thinking,
+ * OpenAI reasoning_effort, ...). */
+export type OpenRouterReasoning =
+ | { effort: 'xhigh' | 'high' | 'medium' | 'low' | 'minimal' | 'none' }
+ | { max_tokens: number };
+
+const OPENROUTER_EFFORTS = new Set(['xhigh', 'high', 'medium', 'low', 'minimal', 'none']);
+
+/**
+ * Map the Kiln descriptor `thinking` control onto OpenRouter's reasoning setting.
+ * Keywords pass through ('max' — an Anthropic-vocabulary level OpenRouter's type
+ * doesn't carry — maps to 'xhigh'); positive numbers become a reasoning token
+ * budget (floored to 1024, matching the Anthropic legacy floor). Unset / 0 / ''
+ * or an unknown keyword → undefined (send nothing; the OpenRouter/upstream
+ * default applies — which for Claude Opus 4.8 means reasoning OFF, same as the
+ * native path).
+ */
+export function resolveOpenRouterReasoning(
+ thinking?: string | number,
+): OpenRouterReasoning | undefined {
+ if (thinking == null || thinking === '' || thinking === 0) return undefined;
+ if (typeof thinking === 'number') {
+ if (!Number.isFinite(thinking) || thinking <= 0) return undefined;
+ return { max_tokens: Math.max(1024, Math.floor(thinking)) };
+ }
+ const raw = thinking.trim().toLowerCase();
+ if (/^\d+$/.test(raw)) return resolveOpenRouterReasoning(Number.parseInt(raw, 10));
+ const effort = raw === 'max' ? 'xhigh' : raw;
+ if (!OPENROUTER_EFFORTS.has(effort)) return undefined;
+ return { effort: effort as Extract<OpenRouterReasoning, { effort: string }>['effort'] };
+}
+
export interface OpenRouterModelOptions {
/** OpenRouter model id, e.g. 'x-ai/grok-4.3'. */
modelId: string;
@@ -28,6 +62,8 @@ export interface OpenRouterModelOptions {
apiKey?: string;
/** Max output tokens. */
maxTokens?: number;
+ /** Unified reasoning control (effort keyword or token budget). */
+ reasoning?: OpenRouterReasoning;
}
/**
@@ -36,7 +72,12 @@ export interface OpenRouterModelOptions {
*/
export function makeOpenRouterModel(opts: OpenRouterModelOptions): Model {
const openrouter = createOpenRouter({ apiKey: opts.apiKey ?? process.env['OPENROUTER_API_KEY'] });
- const provider = ensureStreamStart(openrouter.chat(opts.modelId) as LanguageModelV3);
+ const provider = ensureStreamStart(
+ openrouter.chat(
+ opts.modelId,
+ opts.reasoning ? { reasoning: opts.reasoning } : {},
+ ) as LanguageModelV3,
+ );
return new VercelModel({
provider,
...(opts.maxTokens != null ? { maxTokens: opts.maxTokens } : {}),
@@ -51,7 +92,13 @@ export function makeOpenRouterModel(opts: OpenRouterModelOptions): Model {
// =============================================================================
/** Strands-native provider vocabulary for the Kiln agent loop. */
-export type KilnAgentProvider = 'anthropic' | 'openai' | 'google' | 'bedrock' | 'openrouter' | 'meta';
+export type KilnAgentProvider =
+ | 'anthropic'
+ | 'openai'
+ | 'google'
+ | 'bedrock'
+ | 'openrouter'
+ | 'meta';
/**
* Minimal model descriptor `makeKilnModel` needs. Structurally compatible with
@@ -65,7 +112,10 @@ export interface KilnModelDescriptor {
/** Output-token budget; applied to anthropic/openai/bedrock (GoogleModel uses its own default). */
maxTokens?: number;
/**
- * Anthropic thinking control (anthropic provider only; others ignore it).
+ * Reasoning/thinking control. Honored by the `anthropic` provider (shapes
+ * below) and by `openrouter` (mapped onto OpenRouter's unified `reasoning`
+ * setting via {@link resolveOpenRouterReasoning} — so OpenRouter-hosted Claude
+ * twins behave like the native transport). Other providers ignore it.
*
* - Effort keyword (`'low' | 'medium' | 'high'`, passed through verbatim) →
* `thinking: {type:'adaptive'}` + `output_config: {effort}` — the
@@ -114,7 +164,11 @@ function metaApiKey(opts: MakeKilnModelOptions): string | undefined {
* send nothing and take the API default.
*/
function isAnthropicAdaptiveOnlyModel(model: string): boolean {
- return /^claude-sonnet-5(?:-|$)/.test(model);
+ // Every model that REJECTS the legacy `thinking: {type:'enabled', budget_tokens}`
+ // shape with a 400: Sonnet 5, Fable 5 / Mythos 5 (thinking always-on; verified
+ // live 2026-06-11), and Opus 4.7/4.8 (budget_tokens removed — Anthropic migration
+ // guide). Pre-adaptive models (Sonnet/Haiku 4.5 and older) still take the number.
+ return /^claude-(sonnet-5|fable-5|mythos-5|opus-4-[78])(?:-|$)/.test(model);
}
function resolveAnthropicThinking(
@@ -187,7 +241,9 @@ export function makeKilnModel(desc: KilnModelDescriptor, opts: MakeKilnModelOpti
case 'meta': {
const apiKey = metaApiKey(opts);
if (!apiKey) {
- throw new Error('Meta Model API key is required. Set MODEL_API_KEY, META_MODEL_API_KEY, or META_API_KEY.');
+ throw new Error(
+ 'Meta Model API key is required. Set MODEL_API_KEY, META_MODEL_API_KEY, or META_API_KEY.',
+ );
}
return new OpenAIModel({
modelId: desc.model,
@@ -200,12 +256,19 @@ export function makeKilnModel(desc: KilnModelDescriptor, opts: MakeKilnModelOpti
...(maxTokens != null ? { maxTokens } : {}),
});
}
- case 'openrouter':
+ case 'openrouter': {
+ // The same descriptor `thinking` control the anthropic case honors, mapped
+ // onto OpenRouter's unified reasoning setting — so an OpenRouter-hosted
+ // Claude (e.g. anthropic/claude-opus-4.8) gets its thinking turned on just
+ // like the native transport would.
+ const reasoning = resolveOpenRouterReasoning(desc.thinking);
return makeOpenRouterModel({
modelId: desc.model,
...(opts.apiKey ? { apiKey: opts.apiKey } : {}),
...(maxTokens != null ? { maxTokens } : {}),
+ ...(reasoning ? { reasoning } : {}),
});
+ }
default: {
const exhaustive: never = desc.provider;
throw new Error(`Unsupported Kiln agent provider: ${String(exhaustive)}`);