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
4 changes: 2 additions & 2 deletions components/chat/chat-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export function ChatPage() {
category: "gateway",
hasModels: true,
id: AUTO_PROVIDER_ID,
label: "Auto · Smart Routing",
label: "ModelHub",
localModels: [AUTO_MODEL],
runtime: {
authMode: "none",
Expand Down Expand Up @@ -1224,7 +1224,7 @@ export function ChatPage() {
<SelectLabel>Roteamento</SelectLabel>
<SelectItem value={AUTO_PROVIDER_ID}>
<span className="flex min-w-0 items-center gap-1.5">
<span className="truncate">Auto · Smart Routing</span>
<span className="truncate">ModelHub</span>
<SparklesIcon className="size-3 shrink-0 text-primary" aria-label="Smart Routing" />
</span>
</SelectItem>
Expand Down
20 changes: 17 additions & 3 deletions server/lib/routing/complexity-scorer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@ describe('scoreComplexity', () => {
expect(result.rawScore).toBe(0)
})

it('returns standard tier for medium-length question', () => {
it('returns standard tier for a code generation request', () => {
const result = scoreComplexity(msg(
'Can you write a short Python function to reverse a string and explain what it does?'
'Faça um exemplo em Python de pelo menos 10 linhas que envolva bastante custo computacional para executar.'
))
expect(['simple', 'standard']).toContain(result.tier)
expect(result.tier).toBe('standard')
})

it('returns standard tier for a comparison with recommendation', () => {
const result = scoreComplexity(msg(
'Compare REST e GraphQL para uma aplicação SaaS pequena. Dê prós, contras e recomendação.'
))
expect(result.tier).toBe('standard')
})

it('returns complex tier for a multi-component architecture request', () => {
const result = scoreComplexity(msg(
'Desenhe uma arquitetura para um gateway multi-provider de IA com autenticação, rate limit, fallback, logs de uso e dashboard. Liste componentes, fluxos e riscos.'
))
expect(result.tier).toBe('complex')
})

it('detects code_block signal for fenced code', () => {
Expand Down
18 changes: 18 additions & 0 deletions server/lib/routing/complexity-scorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const MATH_PATTERNS = [

const CODE_PATTERN = /```[\s\S]{20,}/
const INLINE_CODE_PATTERN = /`[^`]+`/g
const CODE_REQUEST_PATTERN = /\b(c[oó]digo|code|python|typescript|javascript|fun[cç][aã]o|function|script|programa|implemente|implement|escreva|write)\b/i
const ARCHITECTURE_REQUEST_PATTERN = /\b(arquitetura|architecture|architect|design a system)\b/i

const PLANNING_KEYWORDS = [
'design a system', 'architect', 'design pattern', 'best approach', 'best practice',
Expand Down Expand Up @@ -230,6 +232,22 @@ export function scoreComplexity(
signals.push('short_message')
}

// Pedidos de código e análise comparativa exigem ao menos o tier standard.
if (
!signals.includes('heartbeat') &&
(CODE_REQUEST_PATTERN.test(lastText) || reasoningMatch)
) {
tier = floorTier(tier, 'standard')
signals.push('standard_task_floor')
}

// Arquitetura com múltiplos requisitos exige composição e análise sistêmica.
const requirementCount = (lastText.match(/[,;]/g) ?? []).length
if (ARCHITECTURE_REQUEST_PATTERN.test(lastText) && requirementCount >= 3) {
tier = floorTier(tier, 'complex')
signals.push('architecture_floor')
}

// Tools ativas exigem um modelo que saiba usá-las → piso "standard".
if (options.hasTools && tier === 'simple' && !signals.includes('heartbeat')) {
tier = 'standard'
Expand Down
13 changes: 12 additions & 1 deletion server/routes/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@ async function resolveAutoRouting(
if (!userId) return null

const messages = Array.isArray(body.messages)
? (body.messages as Array<{ role: string; content: unknown }>)
? (body.messages as Array<{ role?: unknown; content?: unknown; parts?: unknown }>).map((message) => ({
role: typeof message.role === 'string' ? message.role : '',
content: message.content ?? (Array.isArray(message.parts)
? message.parts
.filter((part): part is { text: string; type: string } =>
typeof part === 'object' && part !== null &&
'type' in part && part.type === 'text' &&
'text' in part && typeof part.text === 'string')
.map((part) => part.text)
.join('\n')
: ''),
}))
: []

const tools = Array.isArray(body.tools)
Expand Down
2 changes: 1 addition & 1 deletion server/tests/v1-auto-routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('POST /v1/chat/completions auto routing', () => {
it('authenticates the web session and dispatches the model selected by routing', async () => {
const response = await v1Fetch(new Request('https://modelhub.test/v1/chat/completions', {
body: JSON.stringify({
messages: [{ content: 'hello', role: 'user' }],
messages: [{ parts: [{ text: 'hello', type: 'text' }], role: 'user' }],
model: 'auto',
}),
headers: { 'content-type': 'application/json' },
Expand Down
Loading