Skip to content

Commit 5a01505

Browse files
committed
fix: exclude undefined max token fields from settings export
- Modified ProviderSettingsManager export method to exclude modelMaxTokens and modelMaxThinkingTokens when undefined - Prevents these fields from appearing in exported settings for models that do not support reasoning budgets - Added test to verify undefined fields are properly excluded from export Fixes #7944
1 parent c4c4780 commit 5a01505

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/core/config/ProviderSettingsManager.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,21 @@ export class ProviderSettingsManager {
471471
const configs = profiles.apiConfigs
472472
for (const name in configs) {
473473
// Avoid leaking properties from other providers.
474-
configs[name] = discriminatedProviderSettingsWithIdSchema.parse(configs[name])
474+
let config = discriminatedProviderSettingsWithIdSchema.parse(configs[name])
475+
476+
// Remove max token fields if they are undefined
477+
// These fields should only be included for models that support reasoning budgets
478+
// and when the user has explicitly set values
479+
// Use type assertion to access these optional fields
480+
const configAny = config as any
481+
if (configAny.modelMaxTokens === undefined) {
482+
delete configAny.modelMaxTokens
483+
}
484+
if (configAny.modelMaxThinkingTokens === undefined) {
485+
delete configAny.modelMaxThinkingTokens
486+
}
487+
488+
configs[name] = config
475489
}
476490
return profiles
477491
})

src/core/config/__tests__/importExport.spec.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,5 +1608,76 @@ describe("importExport", () => {
16081608
"https://custom-api.example.com/v1",
16091609
)
16101610
})
1611+
1612+
it("should exclude undefined modelMaxTokens and modelMaxThinkingTokens from export", async () => {
1613+
// This test verifies that undefined max token fields are not included in the export
1614+
// to prevent them from appearing for models that don't support reasoning budgets
1615+
1616+
;(vscode.window.showSaveDialog as Mock).mockResolvedValue({
1617+
fsPath: "/mock/path/roo-code-settings.json",
1618+
})
1619+
1620+
const mockProviderProfiles = {
1621+
currentApiConfigName: "test-provider",
1622+
apiConfigs: {
1623+
"test-provider": {
1624+
apiProvider: "openai" as ProviderName,
1625+
id: "test-id",
1626+
// modelMaxTokens and modelMaxThinkingTokens are undefined
1627+
apiKey: "test-key",
1628+
openAiBaseUrl: "https://api.openai.com/v1",
1629+
},
1630+
"anthropic-provider": {
1631+
apiProvider: "anthropic" as ProviderName,
1632+
id: "anthropic-id",
1633+
apiKey: "anthropic-key",
1634+
modelMaxTokens: 4096, // This one has a value set
1635+
// modelMaxThinkingTokens is undefined
1636+
},
1637+
"reasoning-provider": {
1638+
apiProvider: "openrouter" as ProviderName,
1639+
id: "reasoning-id",
1640+
openRouterApiKey: "reasoning-key",
1641+
modelMaxTokens: 8192,
1642+
modelMaxThinkingTokens: 4096, // Both values set for reasoning model
1643+
},
1644+
},
1645+
modeApiConfigs: {},
1646+
}
1647+
1648+
const mockGlobalSettings = {
1649+
mode: "code",
1650+
autoApprovalEnabled: true,
1651+
}
1652+
1653+
mockProviderSettingsManager.export.mockResolvedValue(mockProviderProfiles)
1654+
mockContextProxy.export.mockResolvedValue(mockGlobalSettings)
1655+
;(fs.mkdir as Mock).mockResolvedValue(undefined)
1656+
1657+
await exportSettings({
1658+
providerSettingsManager: mockProviderSettingsManager,
1659+
contextProxy: mockContextProxy,
1660+
})
1661+
1662+
// Get the exported data
1663+
const exportedData = (safeWriteJson as Mock).mock.calls[0][1]
1664+
1665+
// Verify that undefined fields are not included in the export
1666+
const testProvider = exportedData.providerProfiles.apiConfigs["test-provider"]
1667+
expect(testProvider.apiKey).toBe("test-key")
1668+
expect(testProvider.openAiBaseUrl).toBe("https://api.openai.com/v1")
1669+
expect("modelMaxTokens" in testProvider).toBe(false) // Should not be present
1670+
expect("modelMaxThinkingTokens" in testProvider).toBe(false) // Should not be present
1671+
1672+
// Verify that defined modelMaxTokens is included but undefined modelMaxThinkingTokens is not
1673+
const anthropicProvider = exportedData.providerProfiles.apiConfigs["anthropic-provider"]
1674+
expect(anthropicProvider.modelMaxTokens).toBe(4096) // Should be present with value
1675+
expect("modelMaxThinkingTokens" in anthropicProvider).toBe(false) // Should not be present
1676+
1677+
// Verify that both fields are included when they have values
1678+
const reasoningProvider = exportedData.providerProfiles.apiConfigs["reasoning-provider"]
1679+
expect(reasoningProvider.modelMaxTokens).toBe(8192) // Should be present with value
1680+
expect(reasoningProvider.modelMaxThinkingTokens).toBe(4096) // Should be present with value
1681+
})
16111682
})
16121683
})

0 commit comments

Comments
 (0)