@zhivex-ai/qwen 0.19.0 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +114 -77
- package/dist/index.js.map +1 -1
- package/dist/qwen4.d.ts +9 -0
- package/dist/qwen4.d.ts.map +1 -0
- package/dist/qwen4.js +97 -0
- package/dist/qwen4.js.map +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isQwen4, isQwen4Max, isQwen4Preview, validateQwen4, qwen4PartOptions, encryptedReasoningFromMessage } from "./qwen4.js";
|
|
1
2
|
import { withManualRedirect } from "./manual-redirect.js";
|
|
2
3
|
import { createPortableQwenDecisionModel } from "./portable-decision.js";
|
|
3
4
|
import { qwenSearchUsage } from "./search-usage.js";
|
|
@@ -219,8 +220,8 @@ const isQwen38OmniFlash = (modelId) => modelId.toLowerCase() === "qwen3.8-omni-f
|
|
|
219
220
|
const isQwen38Max = (modelId) => modelFamily(modelId) === "qwen3.8-max";
|
|
220
221
|
const isQwen38Flash = (modelId) => modelFamily(modelId) === "qwen3.8-flash";
|
|
221
222
|
const isQwen38MaxPreview = (modelId) => modelFamily(modelId) === "qwen3.8-max-preview";
|
|
222
|
-
const
|
|
223
|
-
const
|
|
223
|
+
const isModernHybridModel = (modelId) => isQwen38Max(modelId) || isQwen38Flash(modelId) || isQwen38OmniFlash(modelId) || isQwen4(modelId);
|
|
224
|
+
const isModernReasoningModel = (modelId) => isModernHybridModel(modelId) || isQwen38MaxPreview(modelId);
|
|
224
225
|
const isQwenTokenPlanURL = (url) => {
|
|
225
226
|
const path = url.pathname.replace(/\/+$/, "");
|
|
226
227
|
return (QWEN_TOKEN_PLAN_ORIGINS.has(url.origin) &&
|
|
@@ -230,16 +231,16 @@ const isQwenTokenPlanURL = (url) => {
|
|
|
230
231
|
!url.hash &&
|
|
231
232
|
path === "/compatible-mode/v1");
|
|
232
233
|
};
|
|
233
|
-
const
|
|
234
|
+
const validateQwenPreviewBaseURL = (baseURL, modelId) => {
|
|
234
235
|
let url;
|
|
235
236
|
try {
|
|
236
237
|
url = new URL(baseURL);
|
|
237
238
|
}
|
|
238
239
|
catch {
|
|
239
|
-
throw new ConfigurationError(
|
|
240
|
+
throw new ConfigurationError(`Qwen ${modelId} requires a valid QwenCloud Token Plan baseURL.`);
|
|
240
241
|
}
|
|
241
242
|
if (!isQwenTokenPlanURL(url)) {
|
|
242
|
-
throw new ConfigurationError(`Qwen
|
|
243
|
+
throw new ConfigurationError(`Qwen ${modelId} is Token Plan only. Configure baseURL with QWEN_TOKEN_PLAN_BASE_URL (${QWEN_TOKEN_PLAN_BASE_URL}); pay-as-you-go and workspace endpoints cannot serve this model.`);
|
|
243
244
|
}
|
|
244
245
|
};
|
|
245
246
|
const validateQwen38ProductionBaseURL = (modelId, baseURL) => {
|
|
@@ -255,24 +256,26 @@ const validateQwen38ProductionBaseURL = (modelId, baseURL) => {
|
|
|
255
256
|
throw new ConfigurationError(`Qwen ${modelId} requires standard Model Studio credentials and endpoints; use an explicitly supported model such as qwen3.8-max or qwen3.8-flash with Token Plan.`);
|
|
256
257
|
}
|
|
257
258
|
};
|
|
258
|
-
const
|
|
259
|
+
const stripModernReasoningOptions = (providerOptions) => {
|
|
259
260
|
const next = { ...providerOptions };
|
|
260
261
|
delete next.enable_thinking;
|
|
261
262
|
delete next.thinking_budget;
|
|
262
263
|
delete next.preserve_thinking;
|
|
263
264
|
delete next.reasoning_effort;
|
|
265
|
+
delete next.reasoning;
|
|
266
|
+
delete next.thinking_display;
|
|
264
267
|
return next;
|
|
265
268
|
};
|
|
266
269
|
const supportsQwenReasoning = (modelId) => !isQwenOmniLanguageModel(modelId) &&
|
|
267
270
|
supportsQwenTools(modelId) &&
|
|
268
|
-
/^(qwen-(plus|turbo|max|flash)|qwq|qwen3|qwen3\.)/i.test(modelId);
|
|
271
|
+
(/^(qwen-(plus|turbo|max|flash)|qwq|qwen3|qwen3\.)/i.test(modelId) || isQwen4(modelId));
|
|
269
272
|
const supportsQwenVision = (modelId) => {
|
|
270
273
|
const model = modelFamily(modelId);
|
|
271
274
|
return (model.includes("vl") ||
|
|
272
275
|
model.includes("omni") ||
|
|
273
276
|
model.includes("ocr") ||
|
|
274
277
|
model.includes("vision") ||
|
|
275
|
-
|
|
278
|
+
isModernHybridModel(modelId) ||
|
|
276
279
|
isQwen38MaxPreview(modelId) ||
|
|
277
280
|
isQwen38Open(modelId) ||
|
|
278
281
|
/^qwen3\.7-(?:plus|flash)(?:$|-)/.test(model) ||
|
|
@@ -289,22 +292,22 @@ const qwenLanguageCapabilities = (modelId) => {
|
|
|
289
292
|
const omni = isQwenOmniLanguageModel(modelId);
|
|
290
293
|
const omni38 = isQwen38OmniFlash(modelId);
|
|
291
294
|
const reasoning = supportsQwenReasoning(modelId);
|
|
292
|
-
const
|
|
295
|
+
const modernHybrid = isModernHybridModel(modelId);
|
|
293
296
|
const qwen38MaxPreview = isQwen38MaxPreview(modelId);
|
|
294
297
|
return {
|
|
295
298
|
...capabilities,
|
|
296
299
|
vision: supportsQwenVision(modelId),
|
|
297
300
|
tools,
|
|
298
|
-
...(omni38 ? { inputMediaTypes: ["image/*", "audio/*", "video/*"] } : {}),
|
|
301
|
+
...(omni38 ? { inputMediaTypes: ["image/*", "audio/*", "video/*"] } : isQwen4(modelId) ? { inputMediaTypes: ["image/*", "video/*"] } : {}),
|
|
299
302
|
structuredOutput: tools && !omni && !omni38 && !qwen38MaxPreview,
|
|
300
303
|
jsonMode: tools && !omni && !omni38 && !qwen38MaxPreview,
|
|
301
304
|
toolChoice: tools,
|
|
302
|
-
parallelToolCalls:
|
|
305
|
+
parallelToolCalls: modernHybrid && !omni38,
|
|
303
306
|
webSearch: tools && !omni || ["qwen-plus-character", "qwen-flash-character"].includes(modelId),
|
|
304
|
-
files: supportsQwenFiles(modelId) ||
|
|
307
|
+
files: supportsQwenFiles(modelId) || modernHybrid || isQwen38Open(modelId),
|
|
305
308
|
audioInput: supportsQwenAudioInput(modelId),
|
|
306
309
|
reasoning,
|
|
307
|
-
reasoningEfforts:
|
|
310
|
+
reasoningEfforts: modernHybrid
|
|
308
311
|
? ["none", "minimal", "low", "medium", "high", "xhigh", "max"]
|
|
309
312
|
: qwen38MaxPreview
|
|
310
313
|
? ["minimal", "low", "medium", "high", "xhigh", "max"]
|
|
@@ -361,7 +364,8 @@ const mapContentParts = (message, modelId) => {
|
|
|
361
364
|
const hasMedia = message.parts.some((part) => part.type === "image" ||
|
|
362
365
|
part.type === "audio" ||
|
|
363
366
|
(part.type === "file" && part.mediaType.toLowerCase().startsWith("video/")));
|
|
364
|
-
|
|
367
|
+
const hasPartOptions = message.parts.some(part => Object.keys(qwen4PartOptions(part, modelId)).length > 0);
|
|
368
|
+
if (!hasMedia && !hasPartOptions) {
|
|
365
369
|
return message.parts
|
|
366
370
|
.filter((part) => part.type === "text")
|
|
367
371
|
.map((part) => part.text)
|
|
@@ -369,10 +373,10 @@ const mapContentParts = (message, modelId) => {
|
|
|
369
373
|
}
|
|
370
374
|
return message.parts.flatMap((part) => {
|
|
371
375
|
if (part.type === "text") {
|
|
372
|
-
return [{ type: "text", text: part.text }];
|
|
376
|
+
return [{ type: "text", text: part.text, ...qwen4PartOptions(part, modelId) }];
|
|
373
377
|
}
|
|
374
378
|
if (part.type === "image") {
|
|
375
|
-
return [{ type: "image_url", image_url: { url: imageInputToDataUrl(part) } }];
|
|
379
|
+
return [{ type: "image_url", image_url: { url: imageInputToDataUrl(part) }, ...qwen4PartOptions(part, modelId) }];
|
|
376
380
|
}
|
|
377
381
|
if (part.type === "audio") {
|
|
378
382
|
return [{
|
|
@@ -395,7 +399,8 @@ const mapContentParts = (message, modelId) => {
|
|
|
395
399
|
return [
|
|
396
400
|
{
|
|
397
401
|
type: "video_url",
|
|
398
|
-
video_url: { url: toQwenMediaURL(part.data, part.mediaType) }
|
|
402
|
+
video_url: { url: toQwenMediaURL(part.data, part.mediaType) },
|
|
403
|
+
...qwen4PartOptions(part, modelId)
|
|
399
404
|
}
|
|
400
405
|
];
|
|
401
406
|
}
|
|
@@ -432,6 +437,9 @@ const mapMessages = (messages, format, modelId) => messages.flatMap((message) =>
|
|
|
432
437
|
if (reasoningContent) {
|
|
433
438
|
payload.reasoning_content = reasoningContent;
|
|
434
439
|
}
|
|
440
|
+
const encryptedContent = encryptedReasoningFromMessage(message);
|
|
441
|
+
if (encryptedContent)
|
|
442
|
+
payload.encrypted_content = encryptedContent;
|
|
435
443
|
if (toolCalls.length) {
|
|
436
444
|
payload.tool_calls = toolCalls;
|
|
437
445
|
}
|
|
@@ -507,7 +515,7 @@ const mapStructuredOutput = (modelId, input) => {
|
|
|
507
515
|
if (!input.structuredOutput || input.structuredOutput.mode !== "native") {
|
|
508
516
|
return undefined;
|
|
509
517
|
}
|
|
510
|
-
if (isQwen38Flash(modelId)) {
|
|
518
|
+
if (isQwen38Flash(modelId) || isQwen4(modelId)) {
|
|
511
519
|
return {
|
|
512
520
|
type: "json_schema",
|
|
513
521
|
json_schema: {
|
|
@@ -523,7 +531,7 @@ const withStructuredOutputMessages = (modelId, input) => {
|
|
|
523
531
|
if (!input.structuredOutput || input.structuredOutput.mode !== "native") {
|
|
524
532
|
return input.messages;
|
|
525
533
|
}
|
|
526
|
-
if (isQwen38Flash(modelId)) {
|
|
534
|
+
if (isQwen38Flash(modelId) || isQwen4(modelId)) {
|
|
527
535
|
return input.messages;
|
|
528
536
|
}
|
|
529
537
|
const schema = JSON.stringify(toJSONSchema(input.structuredOutput.schema));
|
|
@@ -541,7 +549,9 @@ const withStructuredOutputMessages = (modelId, input) => {
|
|
|
541
549
|
];
|
|
542
550
|
};
|
|
543
551
|
const hasPreservedReasoning = (messages) => messages.some((message) => message.role === "assistant" && reasoningContentFromMessage(message));
|
|
544
|
-
const
|
|
552
|
+
const mapModernChatReasoningEffort = (modelId, effort) => {
|
|
553
|
+
if (isQwen4Max(modelId) && (effort === "medium" || effort === "high"))
|
|
554
|
+
return "high";
|
|
545
555
|
switch (effort) {
|
|
546
556
|
case undefined:
|
|
547
557
|
return undefined;
|
|
@@ -568,7 +578,7 @@ const validateQwenToolStream = (modelId, providerOptions, streaming) => {
|
|
|
568
578
|
throw new ConfigurationError(`Qwen ${modelId} tool_stream requires streamText(); it cannot be used with generateText() or generateObject().`);
|
|
569
579
|
}
|
|
570
580
|
};
|
|
571
|
-
const
|
|
581
|
+
const validateModernRequest = (modelId, input, providerOptions) => {
|
|
572
582
|
if (isQwen38OmniFlash(modelId)) {
|
|
573
583
|
const modalities = providerOptions.modalities;
|
|
574
584
|
if (providerOptions.audio !== undefined || (modalities !== undefined &&
|
|
@@ -597,8 +607,10 @@ const validateQwen38Request = (modelId, input, providerOptions) => {
|
|
|
597
607
|
}
|
|
598
608
|
}
|
|
599
609
|
}
|
|
610
|
+
if (isQwen4(modelId))
|
|
611
|
+
validateQwen4(modelId, input, providerOptions);
|
|
600
612
|
const sharedEffort = input.reasoning?.effort;
|
|
601
|
-
const providerEffort = providerOptions.reasoning_effort;
|
|
613
|
+
const providerEffort = providerOptions.reasoning_effort ?? (isQwen4(modelId) ? providerOptions.reasoning?.effort : undefined);
|
|
602
614
|
const sharedBudget = input.reasoning?.budgetTokens;
|
|
603
615
|
const providerBudget = providerOptions.thinking_budget;
|
|
604
616
|
if (isQwen38MaxPreview(modelId) &&
|
|
@@ -619,7 +631,7 @@ const validateQwen38Request = (modelId, input, providerOptions) => {
|
|
|
619
631
|
if (effort !== undefined && budget !== undefined) {
|
|
620
632
|
throw new ConfigurationError(`Qwen ${modelId} does not allow reasoning_effort and thinking_budget in the same request.`);
|
|
621
633
|
}
|
|
622
|
-
|
|
634
|
+
mapModernChatReasoningEffort(modelId, effort);
|
|
623
635
|
if (providerBudget !== undefined &&
|
|
624
636
|
(!Number.isInteger(providerBudget) || providerBudget < 0 || providerBudget > 262_144)) {
|
|
625
637
|
throw new ConfigurationError(`Qwen ${modelId} thinking_budget must be an integer between 0 and 262144.`);
|
|
@@ -627,12 +639,12 @@ const validateQwen38Request = (modelId, input, providerOptions) => {
|
|
|
627
639
|
if (sharedBudget !== undefined && sharedBudget > 262_144) {
|
|
628
640
|
throw new ConfigurationError(`Qwen ${modelId} reasoning.budgetTokens cannot exceed 262144.`);
|
|
629
641
|
}
|
|
630
|
-
if (
|
|
642
|
+
if (isModernHybridModel(modelId) &&
|
|
631
643
|
providerOptions.enable_thinking === false &&
|
|
632
644
|
((effort !== undefined && effort !== "none") || budget !== undefined)) {
|
|
633
645
|
throw new ConfigurationError(`Qwen ${modelId} cannot disable thinking while also configuring a reasoning effort or thinking budget.`);
|
|
634
646
|
}
|
|
635
|
-
if (
|
|
647
|
+
if (isModernHybridModel(modelId) &&
|
|
636
648
|
providerOptions.enable_thinking === true &&
|
|
637
649
|
effort === "none") {
|
|
638
650
|
throw new ConfigurationError(`Qwen ${modelId} cannot enable thinking with reasoning effort "none".`);
|
|
@@ -642,7 +654,7 @@ const validateQwen38Request = (modelId, input, providerOptions) => {
|
|
|
642
654
|
if (thinkingEnabled && (input.toolChoice === "required" || typeof input.toolChoice === "object")) {
|
|
643
655
|
throw new UnsupportedFeatureError(`Qwen ${modelId} supports toolChoice "required" or a named tool only when thinking is disabled.`);
|
|
644
656
|
}
|
|
645
|
-
if (
|
|
657
|
+
if (isModernHybridModel(modelId) &&
|
|
646
658
|
input.messages.some((message) => message.parts.some((part) => part.type === "file" && !part.mediaType.toLowerCase().startsWith("video/")))) {
|
|
647
659
|
throw new UnsupportedFeatureError(`Qwen ${modelId} FilePart input is reserved for video/* media. Use ImagePart for images.`);
|
|
648
660
|
}
|
|
@@ -650,8 +662,9 @@ const validateQwen38Request = (modelId, input, providerOptions) => {
|
|
|
650
662
|
const mapChatReasoning = (modelId, input, providerOptions) => {
|
|
651
663
|
if (thirdPartyProfile(modelId))
|
|
652
664
|
return thirdPartyReasoning(modelId, input, providerOptions, "chat");
|
|
653
|
-
if (
|
|
654
|
-
const
|
|
665
|
+
if (isModernReasoningModel(modelId)) {
|
|
666
|
+
const display = isQwen4Max(modelId) && (providerOptions.thinking_display === "summarized" || providerOptions.reasoning?.summary === "auto") ? { thinking_display: "summarized", enable_thinking: true } : {};
|
|
667
|
+
const rawEffort = input.reasoning?.effort ?? providerOptions.reasoning_effort ?? (isQwen4(modelId) ? providerOptions.reasoning?.effort : undefined);
|
|
655
668
|
const thinkingDisabled = rawEffort === "none" || providerOptions.enable_thinking === false;
|
|
656
669
|
if (thinkingDisabled) {
|
|
657
670
|
return {
|
|
@@ -660,7 +673,7 @@ const mapChatReasoning = (modelId, input, providerOptions) => {
|
|
|
660
673
|
preserve_thinking: true
|
|
661
674
|
};
|
|
662
675
|
}
|
|
663
|
-
const effort =
|
|
676
|
+
const effort = mapModernChatReasoningEffort(modelId, input.reasoning?.effort ?? providerOptions.reasoning_effort ?? (isQwen4(modelId) ? providerOptions.reasoning?.effort : undefined));
|
|
664
677
|
const budget = input.reasoning?.budgetTokens ?? providerOptions.thinking_budget;
|
|
665
678
|
return {
|
|
666
679
|
...(effort ? { reasoning_effort: effort } : {}),
|
|
@@ -668,7 +681,8 @@ const mapChatReasoning = (modelId, input, providerOptions) => {
|
|
|
668
681
|
...(providerOptions.enable_thinking === true && effort === undefined && budget === undefined
|
|
669
682
|
? { enable_thinking: true }
|
|
670
683
|
: {}),
|
|
671
|
-
preserve_thinking: true
|
|
684
|
+
preserve_thinking: true,
|
|
685
|
+
...display
|
|
672
686
|
};
|
|
673
687
|
}
|
|
674
688
|
if (!input.reasoning) {
|
|
@@ -685,24 +699,26 @@ const mapChatReasoning = (modelId, input, providerOptions) => {
|
|
|
685
699
|
const mapResponsesReasoning = (modelId, input, providerOptions) => {
|
|
686
700
|
if (thirdPartyProfile(modelId))
|
|
687
701
|
return thirdPartyReasoning(modelId, input, providerOptions, "responses");
|
|
688
|
-
const effort = input.reasoning?.effort ?? providerOptions.reasoning_effort;
|
|
689
|
-
const
|
|
702
|
+
const effort = input.reasoning?.effort ?? providerOptions.reasoning_effort ?? (isQwen4(modelId) ? providerOptions.reasoning?.effort : undefined);
|
|
703
|
+
const resolvedEffort = isModernHybridModel(modelId) &&
|
|
690
704
|
providerOptions.enable_thinking === false &&
|
|
691
705
|
effort === undefined
|
|
692
706
|
? "none"
|
|
693
707
|
: effort;
|
|
694
|
-
|
|
708
|
+
const summary = isQwen4Max(modelId) ? providerOptions.reasoning?.summary ?? (providerOptions.thinking_display === "summarized" ? "auto" : undefined) : undefined;
|
|
709
|
+
if (!resolvedEffort && !summary) {
|
|
695
710
|
return undefined;
|
|
696
711
|
}
|
|
697
712
|
return {
|
|
698
713
|
reasoning: {
|
|
714
|
+
...(summary ? { summary } : {}),
|
|
699
715
|
effort: isQwen38MaxPreview(modelId)
|
|
700
|
-
?
|
|
701
|
-
:
|
|
702
|
-
?
|
|
703
|
-
:
|
|
716
|
+
? mapModernChatReasoningEffort(modelId, resolvedEffort)
|
|
717
|
+
: isModernHybridModel(modelId)
|
|
718
|
+
? resolvedEffort
|
|
719
|
+
: resolvedEffort === "low"
|
|
704
720
|
? "minimal"
|
|
705
|
-
:
|
|
721
|
+
: resolvedEffort
|
|
706
722
|
}
|
|
707
723
|
};
|
|
708
724
|
};
|
|
@@ -710,6 +726,7 @@ const mapResponsesUsage = (usage) => usage
|
|
|
710
726
|
? {
|
|
711
727
|
inputTokens: usage.input_tokens,
|
|
712
728
|
cachedInputTokens: usage.input_tokens_details?.cached_tokens,
|
|
729
|
+
...(usage.input_tokens_details?.cache_write_tokens !== undefined ? { cacheWriteTokens: usage.input_tokens_details.cache_write_tokens } : {}),
|
|
713
730
|
outputTokens: usage.output_tokens,
|
|
714
731
|
reasoningTokens: usage.output_tokens_details?.reasoning_tokens,
|
|
715
732
|
totalTokens: usage.total_tokens
|
|
@@ -719,6 +736,7 @@ const mapChatUsage = (usage) => usage
|
|
|
719
736
|
? {
|
|
720
737
|
inputTokens: usage.prompt_tokens,
|
|
721
738
|
cachedInputTokens: usage.prompt_tokens_details?.cached_tokens ?? usage.input_tokens_details?.cached_tokens,
|
|
739
|
+
...((usage.prompt_tokens_details?.cache_write_tokens ?? usage.input_tokens_details?.cache_write_tokens) !== undefined ? { cacheWriteTokens: usage.prompt_tokens_details?.cache_write_tokens ?? usage.input_tokens_details.cache_write_tokens } : {}),
|
|
722
740
|
outputTokens: usage.completion_tokens,
|
|
723
741
|
reasoningTokens: usage.completion_tokens_details?.reasoning_tokens ?? usage.output_tokens_details?.reasoning_tokens,
|
|
724
742
|
totalTokens: usage.total_tokens
|
|
@@ -730,7 +748,7 @@ const hasVideoInput = (messages) => messages.some((message) => message.parts.som
|
|
|
730
748
|
const hasNonVideoFileInput = (messages) => messages.some((message) => message.parts.some((part) => part.type === "file" && !part.mediaType.toLowerCase().startsWith("video/")));
|
|
731
749
|
// Only documented Responses families may send a bounded output request.
|
|
732
750
|
const responsesOutputLimitFamilies = new Set([
|
|
733
|
-
"qwen3.8-max", "qwen3.8-flash", "qwen3.8-omni-flash", "qwen3.8-2.4t-a95b", "qwen3.8-27b",
|
|
751
|
+
"qwen4-max", "qwen4-max-preview", "qwen3.8-max", "qwen3.8-flash", "qwen3.8-omni-flash", "qwen3.8-2.4t-a95b", "qwen3.8-27b",
|
|
734
752
|
"qwen3.7-max", "qwen3.7-max-preview", "qwen3.7-plus", "qwen3.7-flash",
|
|
735
753
|
"qwen3.6-plus", "qwen3.6-flash", "qwen3.6-35b-a3b", "qwen3-max",
|
|
736
754
|
"qwen3.5-plus", "qwen3.5-flash", "qwen3.5-397b-a17b", "qwen3.5-122b-a10b", "qwen3.5-27b", "qwen3.5-35b-a3b"
|
|
@@ -762,7 +780,7 @@ const resolveApiMode = (modelId, requestedMode, input, providerOptions) => {
|
|
|
762
780
|
if (isQwenTranslation(modelId) || isQwenCharacter(modelId))
|
|
763
781
|
return "chat";
|
|
764
782
|
const videoInput = hasVideoInput(input.messages);
|
|
765
|
-
if (videoInput && !
|
|
783
|
+
if (videoInput && !isModernHybridModel(modelId) && !isQwen38Open(modelId)) {
|
|
766
784
|
throw new UnsupportedFeatureError(`Qwen model "${modelId}" does not support video FilePart input through this adapter.`);
|
|
767
785
|
}
|
|
768
786
|
const omni38 = isQwen38OmniFlash(modelId);
|
|
@@ -774,15 +792,21 @@ const resolveApiMode = (modelId, requestedMode, input, providerOptions) => {
|
|
|
774
792
|
if (glmCallableTools && requestedMode === "responses") {
|
|
775
793
|
throw new UnsupportedFeatureError('QwenCloud glm-5.2 callable tools require apiMode "chat" or "auto" because Responses function continuation is unavailable.');
|
|
776
794
|
}
|
|
795
|
+
const needsResponses = hasHostedTools(input.tools) || hasNonVideoFileInput(input.messages) || providerOptions.max_output_tokens !== undefined;
|
|
796
|
+
// Qwen Responses documents only text.format.type="text"; live Qwen4 requests
|
|
797
|
+
// echo json_schema but can return fenced, invalid JSON even with strict=true.
|
|
798
|
+
if (input.structuredOutput?.mode === "native" && (requestedMode === "responses" || needsResponses)) {
|
|
799
|
+
throw new UnsupportedFeatureError('Qwen native structured output requires Chat Completions and cannot be combined with Responses-only features. Use mode "prompted" for locally validated JSON with Responses or hosted tools.');
|
|
800
|
+
}
|
|
777
801
|
const needsChat = glmCallableTools ||
|
|
778
802
|
(input.maxTokens !== undefined && (!supportsResponsesOutputLimit(modelId) ||
|
|
779
803
|
(requestedMode === "auto" && !hasHostedTools(input.tools) && !hasNonVideoFileInput(input.messages) && providerOptions.max_output_tokens === undefined))) ||
|
|
780
804
|
input.reasoning?.budgetTokens !== undefined ||
|
|
781
805
|
providerOptions.thinking_budget !== undefined ||
|
|
782
806
|
providerOptions.tool_stream === true ||
|
|
807
|
+
(isQwen4(modelId) && providerOptions.response_format !== undefined) ||
|
|
783
808
|
input.structuredOutput?.mode === "native" ||
|
|
784
|
-
(!omni38 && (hasMessagePart(input.messages, "audio") || videoInput));
|
|
785
|
-
const needsResponses = hasHostedTools(input.tools) || hasNonVideoFileInput(input.messages) || providerOptions.max_output_tokens !== undefined;
|
|
809
|
+
(!omni38 && (hasMessagePart(input.messages, "audio") || videoInput && !isQwen4(modelId)));
|
|
786
810
|
if (needsChat && needsResponses) {
|
|
787
811
|
throw new UnsupportedFeatureError("Qwen cannot combine Responses-only hosted/file inputs with Chat-only options (including maxTokens on unsupported models), audio, video, reasoning budgets, structured output, or tool streaming.");
|
|
788
812
|
}
|
|
@@ -867,6 +891,8 @@ const parseAssistantMessage = (message, input) => {
|
|
|
867
891
|
return {
|
|
868
892
|
role: "assistant",
|
|
869
893
|
parts: [
|
|
894
|
+
...(typeof message.encrypted_content === "string" && message.encrypted_content
|
|
895
|
+
? [providerDataPart("qwen", { type: "encrypted_content", encryptedContent: message.encrypted_content })] : []),
|
|
870
896
|
...(typeof message.reasoning_content === "string" && message.reasoning_content
|
|
871
897
|
? [providerDataPart("qwen", { type: "reasoning_content", reasoningContent: message.reasoning_content })]
|
|
872
898
|
: []),
|
|
@@ -927,10 +953,10 @@ const toResponsesInput = (messages, format, modelId) => {
|
|
|
927
953
|
for (const part of message.parts) {
|
|
928
954
|
switch (part.type) {
|
|
929
955
|
case "text":
|
|
930
|
-
content.push({ type: "input_text", text: part.text });
|
|
956
|
+
content.push({ type: "input_text", text: part.text, ...qwen4PartOptions(part, modelId) });
|
|
931
957
|
break;
|
|
932
958
|
case "image":
|
|
933
|
-
content.push({ type: "input_image", image_url: imageInputToDataUrl(part) });
|
|
959
|
+
content.push({ type: "input_image", image_url: imageInputToDataUrl(part), ...qwen4PartOptions(part, modelId) });
|
|
934
960
|
break;
|
|
935
961
|
case "audio":
|
|
936
962
|
if (modelId && isQwen38OmniFlash(modelId)) {
|
|
@@ -943,8 +969,8 @@ const toResponsesInput = (messages, format, modelId) => {
|
|
|
943
969
|
}
|
|
944
970
|
break;
|
|
945
971
|
case "file":
|
|
946
|
-
if (modelId && isQwen38OmniFlash(modelId) && part.mediaType.toLowerCase().startsWith("video/")) {
|
|
947
|
-
content.push({ type: "input_video", video_url: toQwenMediaURL(part.data, part.mediaType) });
|
|
972
|
+
if (modelId && (isQwen38OmniFlash(modelId) || isQwen4(modelId)) && part.mediaType.toLowerCase().startsWith("video/")) {
|
|
973
|
+
content.push({ type: "input_video", video_url: toQwenMediaURL(part.data, part.mediaType), ...qwen4PartOptions(part, modelId) });
|
|
948
974
|
break;
|
|
949
975
|
}
|
|
950
976
|
if (!/^https?:\/\//i.test(part.data)) {
|
|
@@ -972,7 +998,7 @@ const toResponsesInput = (messages, format, modelId) => {
|
|
|
972
998
|
part.data &&
|
|
973
999
|
typeof part.data === "object" &&
|
|
974
1000
|
typeof part.data.type === "string" &&
|
|
975
|
-
!["response.annotations", "hosted-tool-usage"].includes(String(part.data.type))) {
|
|
1001
|
+
!["response.annotations", "hosted-tool-usage", "reasoning_content", "reasoning_summary", "encrypted_content"].includes(String(part.data.type))) {
|
|
976
1002
|
input.push(part.data);
|
|
977
1003
|
}
|
|
978
1004
|
break;
|
|
@@ -1125,19 +1151,24 @@ const normalizeResponsesFinishReason = (status, hasToolCalls) => {
|
|
|
1125
1151
|
}
|
|
1126
1152
|
return normalizeFinishReason(status);
|
|
1127
1153
|
};
|
|
1128
|
-
const streamResponses = async function* (response, input) {
|
|
1154
|
+
const streamResponses = async function* (response, input, modelId) {
|
|
1129
1155
|
const attemptId = globalThis.crypto.randomUUID();
|
|
1130
1156
|
const searchEnabled = Object.values(input.tools ?? {}).some(t => !isCallableToolDefinition(t) && t.type === "web_search");
|
|
1131
1157
|
if (searchEnabled)
|
|
1132
1158
|
yield { type: "provider-data", provider: "qwen", data: qwenSearchUsage(attemptId) };
|
|
1133
1159
|
const collectAnnotations = createAnnotationCollector();
|
|
1134
1160
|
const providerDataSnapshots = new Map();
|
|
1161
|
+
const reasoningSnapshots = new Map();
|
|
1135
1162
|
const collectProviderData = (item, outputIndex) => {
|
|
1136
1163
|
const data = parseResponsesProviderData(item);
|
|
1137
1164
|
if (!data)
|
|
1138
1165
|
return undefined;
|
|
1139
1166
|
const typedItem = item;
|
|
1140
1167
|
const key = JSON.stringify([typedItem.type, typeof typedItem.id === "string" ? typedItem.id : outputIndex]);
|
|
1168
|
+
if (modelId && isQwen4(modelId) && typedItem.type === "reasoning") {
|
|
1169
|
+
reasoningSnapshots.set(typeof outputIndex === "number" ? `output:${outputIndex}` : key, data);
|
|
1170
|
+
return undefined;
|
|
1171
|
+
}
|
|
1141
1172
|
const snapshot = JSON.stringify(data);
|
|
1142
1173
|
if (providerDataSnapshots.get(key) === snapshot)
|
|
1143
1174
|
return undefined;
|
|
@@ -1203,7 +1234,7 @@ const streamResponses = async function* (response, input) {
|
|
|
1203
1234
|
type: "provider-data",
|
|
1204
1235
|
provider: "qwen",
|
|
1205
1236
|
data: {
|
|
1206
|
-
type: "reasoning_content",
|
|
1237
|
+
type: type === "response.reasoning_summary_text.delta" && modelId && isQwen4(modelId) ? "reasoning_summary" : "reasoning_content",
|
|
1207
1238
|
reasoningContent: json.delta
|
|
1208
1239
|
}
|
|
1209
1240
|
};
|
|
@@ -1395,6 +1426,8 @@ const streamResponses = async function* (response, input) {
|
|
|
1395
1426
|
? { providerMetadata: { qwenResponsesCallId: call.providerCallId } } : {})
|
|
1396
1427
|
} });
|
|
1397
1428
|
}
|
|
1429
|
+
for (const data of reasoningSnapshots.values())
|
|
1430
|
+
yield { type: "provider-data", provider: "qwen", data };
|
|
1398
1431
|
for (const event of materialized) {
|
|
1399
1432
|
input.abortSignal?.throwIfAborted();
|
|
1400
1433
|
yield event;
|
|
@@ -1431,14 +1464,14 @@ class QwenLanguageModel {
|
|
|
1431
1464
|
validateThirdParty(this.modelId, input, providerOptions);
|
|
1432
1465
|
validateQwenSpecializedText(this.modelId, input);
|
|
1433
1466
|
validateQwenToolStream(this.modelId, providerOptions, false);
|
|
1434
|
-
if (
|
|
1435
|
-
|
|
1467
|
+
if (isModernReasoningModel(this.modelId)) {
|
|
1468
|
+
validateModernRequest(this.modelId, input, providerOptions);
|
|
1436
1469
|
}
|
|
1437
|
-
if (
|
|
1470
|
+
if (isModernHybridModel(this.modelId) && !isQwen4(this.modelId)) {
|
|
1438
1471
|
validateQwen38ProductionBaseURL(this.modelId, this.baseURL);
|
|
1439
1472
|
}
|
|
1440
|
-
if (isQwen38MaxPreview(this.modelId)) {
|
|
1441
|
-
|
|
1473
|
+
if (isQwen38MaxPreview(this.modelId) || isQwen4Preview(this.modelId)) {
|
|
1474
|
+
validateQwenPreviewBaseURL(this.baseURL, this.modelId);
|
|
1442
1475
|
}
|
|
1443
1476
|
const apiMode = resolveApiMode(this.modelId, providerOptions.apiMode ?? "auto", input, providerOptions);
|
|
1444
1477
|
delete providerOptions.apiMode;
|
|
@@ -1446,10 +1479,10 @@ class QwenLanguageModel {
|
|
|
1446
1479
|
const { signal, cleanup } = withTimeoutSignal(input);
|
|
1447
1480
|
try {
|
|
1448
1481
|
if (apiMode === "responses") {
|
|
1449
|
-
const previousResponse = getProviderResponseId(input.messages);
|
|
1482
|
+
const previousResponse = providerOptions.store === false ? undefined : getProviderResponseId(input.messages);
|
|
1450
1483
|
const baseResponseProviderOptions = stripResponsesRequestOptions(providerOptions);
|
|
1451
|
-
const responseProviderOptions = (
|
|
1452
|
-
?
|
|
1484
|
+
const responseProviderOptions = (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId))
|
|
1485
|
+
? stripModernReasoningOptions(baseResponseProviderOptions)
|
|
1453
1486
|
: baseResponseProviderOptions;
|
|
1454
1487
|
const messages = previousResponse && previousResponse.index < input.messages.length - 1
|
|
1455
1488
|
? input.messages.slice(previousResponse.index + 1)
|
|
@@ -1490,8 +1523,8 @@ class QwenLanguageModel {
|
|
|
1490
1523
|
};
|
|
1491
1524
|
}
|
|
1492
1525
|
const baseChatProviderOptions = stripHeaderOptions(providerOptions);
|
|
1493
|
-
const chatProviderOptions = (
|
|
1494
|
-
?
|
|
1526
|
+
const chatProviderOptions = (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId))
|
|
1527
|
+
? stripModernReasoningOptions(baseChatProviderOptions)
|
|
1495
1528
|
: baseChatProviderOptions;
|
|
1496
1529
|
const response = await withResponseRetry(() => this.fetcher(`${this.baseURL}/chat/completions`, {
|
|
1497
1530
|
method: "POST",
|
|
@@ -1504,10 +1537,10 @@ class QwenLanguageModel {
|
|
|
1504
1537
|
messages: mapMessages(withStructuredOutputMessages(this.modelId, input), input.toolResultFormat, this.modelId),
|
|
1505
1538
|
tools: mapChatTools(input.tools),
|
|
1506
1539
|
tool_choice: mapChatToolChoice(input.toolChoice),
|
|
1507
|
-
response_format: mapStructuredOutput(this.modelId, input),
|
|
1540
|
+
response_format: mapStructuredOutput(this.modelId, input) ?? (isQwen4(this.modelId) ? providerOptions.response_format : undefined),
|
|
1508
1541
|
temperature: input.temperature,
|
|
1509
|
-
max_tokens: (
|
|
1510
|
-
max_completion_tokens: (
|
|
1542
|
+
max_tokens: (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId)?.completionTokens) ? undefined : input.maxTokens,
|
|
1543
|
+
max_completion_tokens: (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId)?.completionTokens) ? input.maxTokens : undefined,
|
|
1511
1544
|
stream: false,
|
|
1512
1545
|
...mapChatReasoning(this.modelId, input, providerOptions)
|
|
1513
1546
|
})
|
|
@@ -1537,24 +1570,24 @@ class QwenLanguageModel {
|
|
|
1537
1570
|
validateThirdParty(this.modelId, input, providerOptions);
|
|
1538
1571
|
validateQwenSpecializedText(this.modelId, input);
|
|
1539
1572
|
validateQwenToolStream(this.modelId, providerOptions, true);
|
|
1540
|
-
if (
|
|
1541
|
-
|
|
1573
|
+
if (isModernReasoningModel(this.modelId)) {
|
|
1574
|
+
validateModernRequest(this.modelId, input, providerOptions);
|
|
1542
1575
|
}
|
|
1543
|
-
if (
|
|
1576
|
+
if (isModernHybridModel(this.modelId) && !isQwen4(this.modelId)) {
|
|
1544
1577
|
validateQwen38ProductionBaseURL(this.modelId, this.baseURL);
|
|
1545
1578
|
}
|
|
1546
|
-
if (isQwen38MaxPreview(this.modelId)) {
|
|
1547
|
-
|
|
1579
|
+
if (isQwen38MaxPreview(this.modelId) || isQwen4Preview(this.modelId)) {
|
|
1580
|
+
validateQwenPreviewBaseURL(this.baseURL, this.modelId);
|
|
1548
1581
|
}
|
|
1549
1582
|
const apiMode = resolveApiMode(this.modelId, providerOptions.apiMode ?? "auto", input, providerOptions);
|
|
1550
1583
|
delete providerOptions.apiMode;
|
|
1551
1584
|
const outputLimit = apiMode === "responses" ? responsesOutputLimit(this.modelId, input, providerOptions) : undefined;
|
|
1552
1585
|
const { signal, cleanup } = withTimeoutSignal(input);
|
|
1553
1586
|
if (apiMode === "responses") {
|
|
1554
|
-
const previousResponse = getProviderResponseId(input.messages);
|
|
1587
|
+
const previousResponse = providerOptions.store === false ? undefined : getProviderResponseId(input.messages);
|
|
1555
1588
|
const baseResponseProviderOptions = stripResponsesRequestOptions(providerOptions);
|
|
1556
|
-
const responseProviderOptions = (
|
|
1557
|
-
?
|
|
1589
|
+
const responseProviderOptions = (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId))
|
|
1590
|
+
? stripModernReasoningOptions(baseResponseProviderOptions)
|
|
1558
1591
|
: baseResponseProviderOptions;
|
|
1559
1592
|
const messages = previousResponse && previousResponse.index < input.messages.length - 1
|
|
1560
1593
|
? input.messages.slice(previousResponse.index + 1)
|
|
@@ -1576,9 +1609,10 @@ class QwenLanguageModel {
|
|
|
1576
1609
|
stream: true
|
|
1577
1610
|
})
|
|
1578
1611
|
}), { ...input, abortSignal: signal }, "Qwen").catch((error) => { cleanup(); throw error; });
|
|
1612
|
+
const modelId = this.modelId;
|
|
1579
1613
|
return (async function* () {
|
|
1580
1614
|
try {
|
|
1581
|
-
yield* streamResponses(response, { ...input, abortSignal: signal });
|
|
1615
|
+
yield* streamResponses(response, { ...input, abortSignal: signal }, modelId);
|
|
1582
1616
|
}
|
|
1583
1617
|
finally {
|
|
1584
1618
|
cleanup();
|
|
@@ -1586,8 +1620,8 @@ class QwenLanguageModel {
|
|
|
1586
1620
|
})();
|
|
1587
1621
|
}
|
|
1588
1622
|
const baseChatProviderOptions = stripHeaderOptions(providerOptions);
|
|
1589
|
-
const chatProviderOptions = (
|
|
1590
|
-
?
|
|
1623
|
+
const chatProviderOptions = (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId))
|
|
1624
|
+
? stripModernReasoningOptions(baseChatProviderOptions)
|
|
1591
1625
|
: baseChatProviderOptions;
|
|
1592
1626
|
const response = await withResponseRetry(() => this.fetcher(`${this.baseURL}/chat/completions`, {
|
|
1593
1627
|
method: "POST",
|
|
@@ -1600,10 +1634,10 @@ class QwenLanguageModel {
|
|
|
1600
1634
|
messages: mapMessages(withStructuredOutputMessages(this.modelId, input), input.toolResultFormat, this.modelId),
|
|
1601
1635
|
tools: mapChatTools(input.tools),
|
|
1602
1636
|
tool_choice: mapChatToolChoice(input.toolChoice),
|
|
1603
|
-
response_format: mapStructuredOutput(this.modelId, input),
|
|
1637
|
+
response_format: mapStructuredOutput(this.modelId, input) ?? (isQwen4(this.modelId) ? providerOptions.response_format : undefined),
|
|
1604
1638
|
temperature: input.temperature,
|
|
1605
|
-
max_tokens: (
|
|
1606
|
-
max_completion_tokens: (
|
|
1639
|
+
max_tokens: (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId)?.completionTokens) ? undefined : input.maxTokens,
|
|
1640
|
+
max_completion_tokens: (isModernReasoningModel(this.modelId) || thirdPartyProfile(this.modelId)?.completionTokens) ? input.maxTokens : undefined,
|
|
1607
1641
|
stream: true,
|
|
1608
1642
|
stream_options: { include_usage: true },
|
|
1609
1643
|
...mapChatReasoning(this.modelId, input, providerOptions)
|
|
@@ -1642,6 +1676,9 @@ class QwenLanguageModel {
|
|
|
1642
1676
|
}
|
|
1643
1677
|
};
|
|
1644
1678
|
}
|
|
1679
|
+
if (typeof delta?.encrypted_content === "string" && delta.encrypted_content) {
|
|
1680
|
+
yield { type: "provider-data", provider: "qwen", data: { type: "encrypted_content", encryptedContent: delta.encrypted_content } };
|
|
1681
|
+
}
|
|
1645
1682
|
if (delta?.content) {
|
|
1646
1683
|
const content = String(delta.content);
|
|
1647
1684
|
if (cumulativeTranslation && !content.startsWith(translationText))
|
|
@@ -1664,7 +1701,7 @@ class QwenLanguageModel {
|
|
|
1664
1701
|
existing.args += toolCall.function?.arguments ?? "";
|
|
1665
1702
|
toolBuffers.set(index, existing);
|
|
1666
1703
|
}
|
|
1667
|
-
if (choice?.finish_reason)
|
|
1704
|
+
if (choice?.finish_reason && choice.finish_reason !== "null")
|
|
1668
1705
|
lastFinishReason = choice.finish_reason;
|
|
1669
1706
|
if (json.usage) {
|
|
1670
1707
|
lastUsage = json.usage;
|