Skip to content
Closed
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
5 changes: 3 additions & 2 deletions apps/vscode-e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
"clean": "rimraf out .turbo"
},
"devDependencies": {
"@copilotkit/aimock": "1.35.0",
"@playwright/test": "1.62.1",
"@roo-code/config-eslint": "workspace:^",
"@roo-code/config-typescript": "workspace:^",
"@roo-code/types": "workspace:^",
"@copilotkit/aimock": "1.35.0",
"@types/mocha": "10.0.10",
"@types/node": "22.20.1",
"@types/vscode": "1.100.0",
"@vscode/test-electron": "2.5.2",
"dotenv-cli": "11.0.0",
"glob": "11.1.0",
"mocha": "11.2.2",
"rimraf": "6.0.1"
"rimraf": "6.0.1",
"undici": "^6.21.3"
}
}
22 changes: 16 additions & 6 deletions apps/vscode-e2e/src/suite/providers/deepseek-v4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import * as assert from "assert"
import * as fs from "fs/promises"
import * as path from "path"
import * as vscode from "vscode"

import * as undici from "undici"
import { RooCodeEventName, type ClineMessage } from "@roo-code/types"

import { setDefaultSuiteTimeout } from "../test-utils"
import { sleep, waitFor, waitUntilAborted } from "../utils"

interface UndiciModule {
fetch: typeof fetch
}

interface UndiciRequestInit extends RequestInit {
dispatcher?: unknown
}

const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY

type DeepSeekModelId = "deepseek-v4-flash" | "deepseek-v4-pro"
Expand Down Expand Up @@ -71,10 +79,11 @@ function getRequestBody(init?: RequestInit):
}

function installDeepSeekRequestCapture(capture: CapturedDeepSeekRequest[], baseUrl: string): () => void {
const originalFetch = globalThis.fetch
const undiciModule = undici as unknown as UndiciModule
const originalFetch = undiciModule.fetch
const targetOrigin = new URL(baseUrl).origin

globalThis.fetch = async function (input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
undiciModule.fetch = async function (input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const url = getRequestUrl(input)

if (isUrlWithOrigin(url, targetOrigin) && isChatCompletionsUrl(url)) {
Expand Down Expand Up @@ -106,11 +115,12 @@ function installDeepSeekRequestCapture(capture: CapturedDeepSeekRequest[], baseU
capture.push(request)
}

return originalFetch.call(globalThis, input, init as RequestInit)
} as typeof globalThis.fetch
// Передаём init как есть (включая dispatcher из OpenAiHandler) оригинальному undici.fetch
return originalFetch.call(undiciModule, input, init as UndiciRequestInit)
}

return () => {
globalThis.fetch = originalFetch
undiciModule.fetch = originalFetch
}
}

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/api/providers/__tests__/kimi-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const { mockGetAccessToken, mockForceRefreshAccessToken, mockGetModels } = vi.ho
mockGetModels: vi.fn(),
}))

vi.mock("undici", async (importOriginal) => {
const actual = await importOriginal<typeof import("undici")>()
return {
...actual,
fetch: vi.fn().mockImplementation(async (url: RequestInfo | URL, init?: RequestInit) => {
return globalThis.fetch(url, init)
}),
}
})

vi.mock("../../../integrations/kimi-code/oauth", () => ({
kimiCodeOAuthManager: {
getAccessToken: mockGetAccessToken,
Expand Down
54 changes: 50 additions & 4 deletions src/api/providers/openai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI, { AzureOpenAI } from "openai"
import axios from "axios"
import { Agent, fetch as undiciFetch, Dispatcher } from "undici"

import {
type ModelInfo,
Expand Down Expand Up @@ -52,34 +53,79 @@
...(this.options.openAiHeaders || {}),
}

function resolveTimeoutMs(configuredMs: number | undefined): number {
if (configuredMs === undefined || configuredMs === 0) {

Check warning on line 57 in src/api/providers/openai.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/api/providers/openai.ts:57: 4 mutation test gaps; example: Survived ConditionalExpression mutant (replacement: false). See the job summary for the complete list and resolution guidance.
return 0
// return 60 * 60 * 1000;
}
return configuredMs
}

const timeoutMs = resolveTimeoutMs(this.timeoutMs)

// VS Code bundles its own undici with a 5-minute `bodyTimeout` default.
// For streaming LLM requests, that default terminates the connection.
// We bypass the VS Code-bundled undici by injecting our own Agent-backed
// fetch into the OpenAI SDK.
const agent = new Agent({

Check warning on line 70 in src/api/providers/openai.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/api/providers/openai.ts:70: Survived ObjectLiteral mutant (replacement: {}). See the job summary for the complete list and resolution guidance.
headersTimeout: timeoutMs,
bodyTimeout: timeoutMs,
keepAliveTimeout: timeoutMs,
keepAliveMaxTimeout: timeoutMs,
connect: {

Check warning on line 75 in src/api/providers/openai.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/api/providers/openai.ts:75: Survived ObjectLiteral mutant (replacement: {}). See the job summary for the complete list and resolution guidance.
timeout: Math.min(timeoutMs, 60_000),

Check warning on line 76 in src/api/providers/openai.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/api/providers/openai.ts:76: Survived MethodExpression mutant (replacement: Math.max(timeoutMs, 60_000)). See the job summary for the complete list and resolution guidance.
},
})

interface UndiciRequestInit extends RequestInit {
dispatcher?: Dispatcher
}

const customFetch: typeof fetch = (url, init) => {
const undiciInit = { ...init, dispatcher: agent } as UndiciRequestInit
// return globalThis.fetch(url, undiciInit as RequestInit)
const fetchImpl = undiciFetch as unknown as (
url: RequestInfo | URL,
init: UndiciRequestInit,
) => Promise<Response>

return fetchImpl(url, undiciInit)
}

const timeoutConfig = {
timeout: timeoutMs,
}

if (isAzureAiInference) {
// Azure AI Inference Service (e.g., for DeepSeek) uses a different path structure
this.client = new OpenAI({
baseURL,
apiKey,
defaultHeaders: headers,
defaultQuery: { "api-version": this.options.azureApiVersion || "2024-05-01-preview" },
timeout: this.timeoutMs,
...timeoutConfig,
})
} else if (isAzureOpenAi) {
// Azure API shape slightly differs from the core API shape:
// https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai

const azureBaseURL = `${baseURL.replace(/\/openai\/?$/i, "").replace(/\/$/, "")}/openai`
this.client = new AzureOpenAI({
baseURL: azureBaseURL,
apiKey,
apiVersion: this.options.azureApiVersion || azureOpenAiDefaultApiVersion,
defaultHeaders: headers,
timeout: this.timeoutMs,
...timeoutConfig,
})
} else {
this.client = new OpenAI({
baseURL,
apiKey,
defaultHeaders: headers,
timeout: this.timeoutMs,
...timeoutConfig,
})
}

;(this.client as unknown as { fetch: typeof fetch }).fetch = customFetch
}

override async *createMessage(
Expand Down
Loading