Skip to content
Merged
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
17 changes: 14 additions & 3 deletions typescript/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ function createFetchLanguageModel(url: string, headers: object, defaultParams: o

async function complete(prompt: string | PromptSection[]) {
dispatcherPromise ??= resolveProxyDispatcher(proxy);
const dispatcher = await dispatcherPromise;
let dispatcher: RequestInit["dispatcher"];
try {
dispatcher = await dispatcherPromise;
} catch (e) {
return error(`REST API fetch error: ${getErrorMessage(e)}`);
}
let retryCount = 0;
const retryMaxAttempts = model.retryMaxAttempts ?? 3;
const retryPauseMs = model.retryPauseMs ?? 1000;
Expand Down Expand Up @@ -339,7 +344,12 @@ function createResponsesFetchLanguageModel(url: string, headers: object, default

async function complete(prompt: string | PromptSection[]) {
dispatcherPromise ??= resolveProxyDispatcher(proxy);
const dispatcher = await dispatcherPromise;
let dispatcher: RequestInit["dispatcher"];
try {
dispatcher = await dispatcherPromise;
} catch (e) {
return error(`REST API fetch error: ${getErrorMessage(e)}`);
}
let retryCount = 0;
const retryMaxAttempts = model.retryMaxAttempts ?? 3;
const retryPauseMs = model.retryPauseMs ?? 1000;
Expand Down Expand Up @@ -560,7 +570,8 @@ async function importUndici() {
try {
return await import("undici");
} catch (e) {
if (e instanceof Error && /Cannot find module|ERR_MODULE_NOT_FOUND/.test(e.message)) {
if (e instanceof Error && "code" in e && e.code === "ERR_MODULE_NOT_FOUND" &&
/Cannot find (?:module|package) ['"]undici['"]/.test(e.message)) {
throw new Error(
`A proxy was configured, but the optional "undici" package is not installed. ` +
`Run "npm install undici" to enable proxy support.`
Expand Down
25 changes: 24 additions & 1 deletion typescript/tests/model.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,34 @@

import { test, describe, after } from "node:test";
import assert from "node:assert/strict";
import { cpSync, mkdtempSync, rmSync } from "node:fs";
import { createRequire } from "node:module";
import { tmpdir } from "node:os";
import { join } from "node:path";

// Load the compiled module from dist
import { createOpenAILanguageModel, createAzureOpenAILanguageModel, createLanguageModel } from "../dist/index.js";

test("missing optional undici returns an actionable result for both API paths", async () => {
const dir = mkdtempSync(join(tmpdir(), "typechat-no-undici-"));
try {
for (const name of ["model", "result"]) {
cpSync(new URL(`../dist/${name}.js`, import.meta.url), join(dir, `${name}.js`));
}
const { createOpenAILanguageModel: isolatedModel } = createRequire(join(dir, "model.js"))("./model.js");
for (const endpoint of ["https://api.openai.com/v1/chat/completions", "https://api.openai.com/v1/responses"]) {
const model = isolatedModel("sk-test", "gpt-4", endpoint, "", { proxyUrl: "http://localhost:8080" });
const result = await model.complete("test");
assert.deepEqual(result, {
success: false,
message: 'REST API fetch error: A proxy was configured, but the optional "undici" package is not installed. Run "npm install undici" to enable proxy support.'
});
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});

// ---------------------------------------------------------------------------
// Helpers: build mock Response objects
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -531,4 +555,3 @@ describe("request timeout and response size limits", () => {
assert.equal(result.data, "Streamed!");
});
});

Loading