|
| 1 | +import type { StreamFn } from "@mariozechner/pi-agent-core"; |
| 2 | +import type { Context, Model } from "@mariozechner/pi-ai"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { |
| 5 | + createFireworksKimiThinkingDisabledWrapper, |
| 6 | + wrapFireworksProviderStream, |
| 7 | +} from "./stream.js"; |
| 8 | + |
| 9 | +function capturePayload(params: { |
| 10 | + provider: string; |
| 11 | + api: string; |
| 12 | + modelId: string; |
| 13 | + initialPayload?: Record<string, unknown>; |
| 14 | +}): Record<string, unknown> { |
| 15 | + let captured: Record<string, unknown> = {}; |
| 16 | + const baseStreamFn: StreamFn = (_model, _context, options) => { |
| 17 | + const payload = { ...params.initialPayload }; |
| 18 | + options?.onPayload?.(payload, _model); |
| 19 | + captured = payload; |
| 20 | + return {} as ReturnType<StreamFn>; |
| 21 | + }; |
| 22 | + |
| 23 | + const wrapped = createFireworksKimiThinkingDisabledWrapper(baseStreamFn); |
| 24 | + void wrapped( |
| 25 | + { |
| 26 | + api: params.api, |
| 27 | + provider: params.provider, |
| 28 | + id: params.modelId, |
| 29 | + } as Model<"openai-completions">, |
| 30 | + { messages: [] } as Context, |
| 31 | + {}, |
| 32 | + ); |
| 33 | + |
| 34 | + return captured; |
| 35 | +} |
| 36 | + |
| 37 | +describe("createFireworksKimiThinkingDisabledWrapper", () => { |
| 38 | + it("forces thinking disabled for Fireworks Kimi models", () => { |
| 39 | + expect( |
| 40 | + capturePayload({ |
| 41 | + provider: "fireworks", |
| 42 | + api: "openai-completions", |
| 43 | + modelId: "accounts/fireworks/routers/kimi-k2p5-turbo", |
| 44 | + }), |
| 45 | + ).toMatchObject({ thinking: { type: "disabled" } }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("forces thinking disabled for Fireworks Kimi k2.5 aliases", () => { |
| 49 | + expect( |
| 50 | + capturePayload({ |
| 51 | + provider: "fireworks", |
| 52 | + api: "openai-completions", |
| 53 | + modelId: "accounts/fireworks/routers/kimi-k2.5-turbo", |
| 54 | + }), |
| 55 | + ).toMatchObject({ thinking: { type: "disabled" } }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("strips reasoning fields when disabling Fireworks Kimi thinking", () => { |
| 59 | + const payload = capturePayload({ |
| 60 | + provider: "fireworks", |
| 61 | + api: "openai-completions", |
| 62 | + modelId: "accounts/fireworks/models/kimi-k2p5", |
| 63 | + initialPayload: { |
| 64 | + reasoning_effort: "low", |
| 65 | + reasoning: { effort: "low" }, |
| 66 | + reasoningEffort: "low", |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(payload).toEqual({ thinking: { type: "disabled" } }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("passes sanitized payloads to caller onPayload hooks", () => { |
| 74 | + let callbackPayload: Record<string, unknown> = {}; |
| 75 | + const baseStreamFn: StreamFn = (_model, _context, options) => { |
| 76 | + const payload = { |
| 77 | + reasoning_effort: "high", |
| 78 | + reasoning: { effort: "high" }, |
| 79 | + }; |
| 80 | + options?.onPayload?.(payload, _model); |
| 81 | + return {} as ReturnType<StreamFn>; |
| 82 | + }; |
| 83 | + |
| 84 | + const wrapped = createFireworksKimiThinkingDisabledWrapper(baseStreamFn); |
| 85 | + void wrapped( |
| 86 | + { |
| 87 | + api: "openai-completions", |
| 88 | + provider: "fireworks", |
| 89 | + id: "accounts/fireworks/routers/kimi-k2p5-turbo", |
| 90 | + } as Model<"openai-completions">, |
| 91 | + { messages: [] } as Context, |
| 92 | + { |
| 93 | + onPayload: (payload) => { |
| 94 | + callbackPayload = payload as Record<string, unknown>; |
| 95 | + }, |
| 96 | + }, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(callbackPayload).toEqual({ thinking: { type: "disabled" } }); |
| 100 | + }); |
| 101 | + |
| 102 | + it("returns no provider wrapper for non-target Fireworks requests", () => { |
| 103 | + expect( |
| 104 | + wrapFireworksProviderStream({ |
| 105 | + provider: "fireworks", |
| 106 | + modelId: "accounts/fireworks/models/qwen3.6-plus", |
| 107 | + model: { |
| 108 | + api: "openai-completions", |
| 109 | + provider: "fireworks", |
| 110 | + id: "accounts/fireworks/models/qwen3.6-plus", |
| 111 | + } as Model<"openai-completions">, |
| 112 | + streamFn: undefined, |
| 113 | + } as never), |
| 114 | + ).toBeUndefined(); |
| 115 | + |
| 116 | + expect( |
| 117 | + wrapFireworksProviderStream({ |
| 118 | + provider: "fireworks", |
| 119 | + modelId: "accounts/fireworks/routers/kimi-k2p5-turbo", |
| 120 | + model: { |
| 121 | + api: "openai-responses", |
| 122 | + provider: "fireworks", |
| 123 | + id: "accounts/fireworks/routers/kimi-k2p5-turbo", |
| 124 | + } as Model<"openai-responses">, |
| 125 | + streamFn: undefined, |
| 126 | + } as never), |
| 127 | + ).toBeUndefined(); |
| 128 | + |
| 129 | + expect( |
| 130 | + wrapFireworksProviderStream({ |
| 131 | + provider: "fireworks-ai", |
| 132 | + modelId: "accounts/fireworks/routers/kimi-k2p5-turbo", |
| 133 | + model: { |
| 134 | + api: "openai-completions", |
| 135 | + provider: "fireworks-ai", |
| 136 | + id: "accounts/fireworks/routers/kimi-k2p5-turbo", |
| 137 | + } as Model<"openai-completions">, |
| 138 | + streamFn: undefined, |
| 139 | + } as never), |
| 140 | + ).toBeTypeOf("function"); |
| 141 | + |
| 142 | + expect( |
| 143 | + wrapFireworksProviderStream({ |
| 144 | + provider: "openai", |
| 145 | + modelId: "gpt-5.4", |
| 146 | + model: { |
| 147 | + api: "openai-completions", |
| 148 | + provider: "openai", |
| 149 | + id: "gpt-5.4", |
| 150 | + } as Model<"openai-completions">, |
| 151 | + streamFn: undefined, |
| 152 | + } as never), |
| 153 | + ).toBeUndefined(); |
| 154 | + }); |
| 155 | +}); |
0 commit comments