fix(chat-params): cap invalid token fallback by model output limit
Keep the conservative non-positive maxOutputTokens fallback while preventing 4096 from exceeding tiny model limits by using min(4096, capabilities max). Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -323,4 +323,48 @@ describe("createChatParamsHandler", () => {
|
||||
//#then
|
||||
expect(output.maxOutputTokens).toBe(4096)
|
||||
})
|
||||
|
||||
test("uses model max when non-positive stored maxOutputTokens fallback exceeds model limit", async () => {
|
||||
//#given
|
||||
sharedModule.writeProviderModelsCache({
|
||||
connected: ["custom-provider"],
|
||||
models: {
|
||||
"custom-provider": [
|
||||
{
|
||||
id: "tiny-model",
|
||||
name: "tiny-model",
|
||||
limit: { output: 512 },
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
setSessionPromptParams("ses_chat_params", {
|
||||
maxOutputTokens: 0,
|
||||
})
|
||||
|
||||
const handler = createChatParamsHandler({
|
||||
anthropicEffort: null,
|
||||
})
|
||||
|
||||
const input = {
|
||||
sessionID: "ses_chat_params",
|
||||
agent: { name: "oracle" },
|
||||
model: { providerID: "custom-provider", modelID: "tiny-model" },
|
||||
provider: { id: "custom-provider" },
|
||||
message: {},
|
||||
}
|
||||
|
||||
const output: ChatParamsOutput = {
|
||||
topP: 1,
|
||||
topK: 1,
|
||||
maxOutputTokens: 0,
|
||||
options: {},
|
||||
}
|
||||
|
||||
//#when
|
||||
await handler(input, output)
|
||||
|
||||
//#then
|
||||
expect(output.maxOutputTokens).toBe(512)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,6 +3,14 @@ import { getModelCapabilities, log, resolveCompatibleModelSettings } from "../sh
|
||||
|
||||
const SAFE_MAX_OUTPUT_TOKENS_FALLBACK = 4096
|
||||
|
||||
function resolveSafeMaxOutputTokensFallback(capabilitiesMaxOutputTokens: number | undefined): number {
|
||||
if (typeof capabilitiesMaxOutputTokens !== "number" || capabilitiesMaxOutputTokens <= 0) {
|
||||
return SAFE_MAX_OUTPUT_TOKENS_FALLBACK
|
||||
}
|
||||
|
||||
return Math.min(SAFE_MAX_OUTPUT_TOKENS_FALLBACK, capabilitiesMaxOutputTokens)
|
||||
}
|
||||
|
||||
export type ChatParamsInput = {
|
||||
sessionID: string
|
||||
agent: { name?: string }
|
||||
@@ -173,10 +181,10 @@ export function createChatParamsHandler(args: {
|
||||
const originalMaxOutputTokens = typeof output.maxOutputTokens === "number"
|
||||
? output.maxOutputTokens
|
||||
: compatibility.maxTokens
|
||||
output.maxOutputTokens = SAFE_MAX_OUTPUT_TOKENS_FALLBACK
|
||||
output.maxOutputTokens = resolveSafeMaxOutputTokensFallback(capabilities?.maxOutputTokens)
|
||||
if (typeof originalMaxOutputTokens === "number" && originalMaxOutputTokens <= 0) {
|
||||
log(
|
||||
`[plugin] maxOutputTokens=${originalMaxOutputTokens} is non-positive; using safe fallback ${SAFE_MAX_OUTPUT_TOKENS_FALLBACK}`,
|
||||
`[plugin] maxOutputTokens=${originalMaxOutputTokens} is non-positive; using safe fallback ${output.maxOutputTokens}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user