From 472fbff6a56476d6ccad03435f40748d75cf38f0 Mon Sep 17 00:00:00 2001 From: Tarek Etman Date: Wed, 11 Nov 2026 05:09:17 +0000 Subject: [PATCH] feat(aiclient): read the Bing search endpoint from BING_ENDPOINT ApiSettings gets an optional endpoint, filled from BING_ENDPOINT (bing.endpoint in config). Unset keeps https://api.bing.microsoft.com/v7.0. --- ts/packages/aiclient/src/bing.ts | 15 ++++- ts/packages/aiclient/test/bing.spec.ts | 90 ++++++++++++++++++++++++++ ts/packages/config/README.md | 1 + 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 ts/packages/aiclient/test/bing.spec.ts diff --git a/ts/packages/aiclient/src/bing.ts b/ts/packages/aiclient/src/bing.ts index ecf26f19af..b71f425bc7 100644 --- a/ts/packages/aiclient/src/bing.ts +++ b/ts/packages/aiclient/src/bing.ts @@ -7,20 +7,30 @@ import { getEnvSetting } from "./common.js"; export type ApiSettings = { apiKey: string; + /** + * Base URL that /search and /images/search are appended to. + * Defaults to DEFAULT_BING_ENDPOINT. + */ + endpoint?: string; maxRetryAttempts?: number; retryPauseMs?: number; }; +export const DEFAULT_BING_ENDPOINT = "https://api.bing.microsoft.com/v7.0"; + export enum EnvVars { BING_API_KEY = "BING_API_KEY", + BING_ENDPOINT = "BING_ENDPOINT", } export function apiSettingsFromEnv( env?: Record, ): ApiSettings { env ??= process.env; + const endpoint = env[EnvVars.BING_ENDPOINT]?.trim(); return { apiKey: getEnvSetting(env, EnvVars.BING_API_KEY), + ...(endpoint ? { endpoint } : {}), }; } @@ -138,7 +148,10 @@ export async function createBingSearch( return error(`Could not create Bing Client:\n${e}`); } - const baseUrl = "https://api.bing.microsoft.com/v7.0"; + const baseUrl = (settings.endpoint || DEFAULT_BING_ENDPOINT).replace( + /\/+$/, + "", + ); const webEndpoint = baseUrl + "/search"; const imageEndpoint = baseUrl + "/images/search"; const headers = createApiHeaders(settings); diff --git a/ts/packages/aiclient/test/bing.spec.ts b/ts/packages/aiclient/test/bing.spec.ts new file mode 100644 index 0000000000..4fe6af5aad --- /dev/null +++ b/ts/packages/aiclient/test/bing.spec.ts @@ -0,0 +1,90 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { jest } from "@jest/globals"; +import { + apiSettingsFromEnv, + createBingSearch, + DEFAULT_BING_ENDPOINT, +} from "../src/bing.js"; + +describe("bing endpoint", () => { + const originalFetch = globalThis.fetch; + let requested: string[]; + + beforeEach(() => { + requested = []; + globalThis.fetch = (async (input: unknown) => { + requested.push(String(input)); + return new Response( + JSON.stringify({ + webPages: { + value: [ + { + name: "n", + url: "https://example.com", + snippet: "s", + }, + ], + }, + value: [], + }), + { + status: 200, + headers: { "content-type": "application/json" }, + }, + ); + }) as typeof globalThis.fetch; + jest.spyOn(console, "warn").mockImplementation(() => {}); + jest.spyOn(console, "error").mockImplementation(() => {}); + }); + + afterEach(() => { + globalThis.fetch = originalFetch; + jest.restoreAllMocks(); + }); + + test("apiSettingsFromEnv leaves the endpoint unset by default", () => { + const settings = apiSettingsFromEnv({ BING_API_KEY: "key" }); + expect(settings.endpoint).toBeUndefined(); + expect(DEFAULT_BING_ENDPOINT).toBe( + "https://api.bing.microsoft.com/v7.0", + ); + }); + + test("apiSettingsFromEnv reads BING_ENDPOINT", () => { + const settings = apiSettingsFromEnv({ + BING_API_KEY: "key", + BING_ENDPOINT: " http://127.0.0.1:8080/v7.0 ", + }); + expect(settings.endpoint).toBe("http://127.0.0.1:8080/v7.0"); + }); + + test("requests go to Bing without an endpoint", async () => { + const client = await createBingSearch({ apiKey: "key" }); + expect(client.success).toBe(true); + if (!client.success) return; + const pages = await client.data.webSearch("typeagent"); + await client.data.imageSearch("typeagent"); + expect(pages.success).toBe(true); + expect(requested).toEqual([ + "https://api.bing.microsoft.com/v7.0/search?q=typeagent", + "https://api.bing.microsoft.com/v7.0/images/search?q=typeagent", + ]); + }); + + test("requests go to the configured endpoint", async () => { + const client = await createBingSearch({ + apiKey: "key", + endpoint: "http://127.0.0.1:8080/v7.0/", + }); + expect(client.success).toBe(true); + if (!client.success) return; + await client.data.webSearch("typeagent", { count: 3 }); + await client.data.imageSearch("typeagent"); + expect(requested).toEqual([ + "http://127.0.0.1:8080/v7.0/search?q=typeagent&count=3", + "http://127.0.0.1:8080/v7.0/images/search?q=typeagent", + ]); + }); +}); diff --git a/ts/packages/config/README.md b/ts/packages/config/README.md index 30cc18f38e..0fefd789d4 100644 --- a/ts/packages/config/README.md +++ b/ts/packages/config/README.md @@ -76,6 +76,7 @@ YAML maps are flattened into the `EnvVars` flat-key shape used by | `azure.openai.deployments[].endpoint` (suffix=foo) | `AZURE_OPENAI_ENDPOINT_FOO` | | `openai.api_key` | `OPENAI_API_KEY` | | `bing.api_key` | `BING_API_KEY` | +| `bing.endpoint` | `BING_ENDPOINT` | | `extra.` | `` (passthrough for unknown keys) | See [src/flatten.ts](./src/flatten.ts) for the full mapping.