| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- // ============================================================
- import { createTestModelSelection } from "./test-model-selection.js";
- // Provider-visible WebSearch Wrapper Tests
- // ============================================================
- import {
- SessionEventType,
- WebSearchInputJsonSchema,
- WebSearchInputSchema,
- createSessionId,
- type ModelSelection,
- } from "@zcode/contracts";
- import { describe, expect, it, vi } from "vitest";
- import { createTestAgentRuntime } from "./test-agent-runtime.js";
- import { registerBuiltInTools } from "../src/tool/handlers/index.js";
- import { webSearchToolEntry } from "../src/tool/handlers/websearch.js";
- import { buildWebSearchOutput } from "../src/tool/handlers/websearch-results.js";
- import { createToolRegistry } from "../src/tool/registry.js";
- import { createTestSessionEventStore } from "./test-event-store.js";
- import { createTestModelFactory, createTestRuntimeModel } from "./test-runtime-model.js";
- const modelSelection: ModelSelection = {
- providerId: "deepseek" as never,
- modelId: "deepseek-chat" as never,
- };
- describe("provider-visible WebSearch wrapper", () => {
- it("exposes WebSearch schema without unsupported provider-native no-op options", () => {
- const properties = WebSearchInputJsonSchema.properties as Record<string, unknown>;
- expect(properties.query).toBeDefined();
- expect(properties.allowed_domains).toBeDefined();
- expect(properties.blocked_domains).toBeDefined();
- expect(properties.allowedDomains).toBeUndefined();
- expect(properties.blockedDomains).toBeUndefined();
- expect(properties.maxUses).toBeUndefined();
- expect(properties.searchContextSize).toBeUndefined();
- expect(properties.numResults).toBeUndefined();
- expect(properties.safeSearch).toBeUndefined();
- });
- it("validates provider-visible search options", () => {
- expect(
- WebSearchInputSchema.safeParse({
- allowed_domains: ["example.com"],
- maxUses: 3,
- query: "latest zcode",
- }).success,
- ).toBe(true);
- expect(
- WebSearchInputSchema.safeParse({
- query: "latest zcode",
- searchContextSize: "high",
- }).success,
- ).toBe(false);
- expect(
- WebSearchInputSchema.safeParse({
- maxUses: 9,
- query: "latest zcode",
- }).success,
- ).toBe(false);
- expect(
- WebSearchInputSchema.safeParse({
- allowed_domains: ["example.com"],
- blocked_domains: ["blocked.example"],
- query: "latest zcode",
- }).success,
- ).toBe(false);
- expect(
- WebSearchInputSchema.safeParse({
- allowed_domains: ["https://example.com"],
- query: "latest zcode",
- }).success,
- ).toBe(true);
- expect(
- WebSearchInputSchema.safeParse({
- allowed_domains: Array.from({ length: 21 }, (_, index) => `example-${index}.com`),
- query: "latest zcode",
- }).success,
- ).toBe(true);
- });
- it("describes public WebSearch citation and domain guidance", () => {
- const description = webSearchToolEntry.metadata.description;
- const instructions = webSearchToolEntry.metadata.modelInstructions?.join("\n") ?? "";
- const prompt = `${description}\n${instructions}`;
- expect(prompt).toContain("Sources:");
- expect(prompt).toContain("markdown links");
- expect(prompt).toContain("allowed_domains");
- expect(prompt).toContain("blocked_domains");
- expect(prompt).toContain("current month");
- expect(prompt).toContain("US-only");
- });
- it("keeps the WebSearch description stable across months", async () => {
- vi.useFakeTimers();
- try {
- vi.setSystemTime(new Date(2027, 1, 15, 12, 0, 0));
- vi.resetModules();
- const { webSearchToolEntry: timedWebSearchToolEntry } =
- await import("../src/tool/handlers/websearch.js");
- const { createToolRegistry: createTimedToolRegistry } =
- await import("../src/tool/registry.js");
- const timedRegistry = createTimedToolRegistry();
- timedRegistry.register(timedWebSearchToolEntry);
- const getProviderDescription = () =>
- timedRegistry.toContracts().find((tool) => tool.name === "WebSearch")?.description ?? "";
- // 月份由会话里的当前日期提供;描述里写月份会让 tools 缓存每月失效一次。
- const february = getProviderDescription();
- expect(february).toContain(
- "- The current month is (provided in the conversation below) — use this when searching for recent information.",
- );
- expect(february).not.toContain("2027");
- vi.setSystemTime(new Date(2027, 2, 15, 12, 0, 0));
- expect(getProviderDescription()).toBe(february);
- } finally {
- vi.useRealTimers();
- vi.resetModules();
- }
- });
- it("registers WebSearch as a client-side built-in tool", () => {
- const registry = createToolRegistry();
- registerBuiltInTools(registry, { includeAgent: true });
- expect(registry.has("WebSearch")).toBe(true);
- expect(registry.has("web_search")).toBe(false);
- expect(registry.toContracts()).toContainEqual(
- expect.objectContaining({
- executionMode: "client",
- name: "WebSearch",
- providerNative: undefined,
- }),
- );
- });
- it("keeps non-execution inventory independent from a model", () => {
- const runtime = createRuntime("runtime-websearch-client-wrapper");
- const tools = runtime.getTools();
- expect(runtime.getToolRegistry().has("WebSearch")).toBe(true);
- expect(tools).toContainEqual(
- expect.objectContaining({
- executionMode: "client",
- name: "WebSearch",
- providerNative: undefined,
- }),
- );
- expect(tools.some((tool) => tool.name === "web_search")).toBe(false);
- });
- it("filters execution tools only from the current Active Model property", () => {
- const runtime = createRuntime("runtime-websearch-active-model");
- const createModel = (supportsNativeWebSearch: boolean) =>
- createTestRuntimeModel({
- generateText: async () => ({ finishReason: "stop", text: "", usage: {} }),
- propertyOverrides: { supportsNativeWebSearch },
- });
- expect(runtime.getTools(createModel(true)).some((tool) => tool.name === "WebSearch")).toBe(
- true,
- );
- expect(runtime.getTools(createModel(false)).some((tool) => tool.name === "WebSearch")).toBe(
- false,
- );
- });
- it("normalizes legacy web_search allowlist aliases to WebSearch", () => {
- expect(
- createRuntime("runtime-websearch-explore", {
- toolset: "explore",
- toolAllowlist: ["Read", "Glob", "Grep", "WebSearch"],
- })
- .getTools()
- .some((tool) => tool.name === "WebSearch"),
- ).toBe(true);
- expect(
- createRuntime("runtime-websearch-explore-provider-alias", {
- toolset: "explore",
- toolAllowlist: ["Read", "Glob", "Grep", "web_search"],
- })
- .getTools()
- .some((tool) => tool.name === "WebSearch"),
- ).toBe(true);
- expect(
- createRuntime("runtime-websearch-explore-deny", {
- toolset: "explore",
- toolAllowlist: ["Read", "Glob", "Grep"],
- })
- .getTools()
- .some((tool) => tool.name === "WebSearch"),
- ).toBe(false);
- expect(
- createRuntime("runtime-websearch-allowlist-deny", {
- toolAllowlist: ["Read"],
- })
- .getTools()
- .some((tool) => tool.name === "WebSearch"),
- ).toBe(false);
- expect(
- createRuntime("runtime-websearch-allowlist-public", {
- toolAllowlist: ["WebSearch"],
- })
- .getTools()
- .some((tool) => tool.name === "WebSearch"),
- ).toBe(true);
- expect(
- createRuntime("runtime-websearch-allowlist-provider", {
- toolAllowlist: ["web_search"],
- })
- .getTools()
- .some((tool) => tool.name === "WebSearch"),
- ).toBe(true);
- });
- it("runs an internal streaming provider-native web_search request from the WebSearch handler", async () => {
- const sessionId = createSessionId("runtime-websearch-handler-side-request");
- const eventStore = createTestSessionEventStore();
- const requests: any[] = [];
- let mainRequestCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- { modelSelection: createTestModelSelection(modelSelection) },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- properties: { supportsNativeWebSearch: true },
- async generateText(request: any, observation) {
- if (observation.invocationContext?.metadata?.querySource === "web_search_tool") {
- throw new Error("WebSearch side request must use streamText");
- }
- mainRequestCount++;
- if (mainRequestCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_websearch",
- input: {
- allowed_domains: ["example.com"],
- query: "latest zcode",
- },
- name: "WebSearch",
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Final answer with sources",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- async *streamText(request: any, observation) {
- requests.push({ observation, request });
- if (observation.invocationContext?.metadata?.querySource !== "web_search_tool") {
- throw new Error("Unexpected streaming request");
- }
- yield { type: "start" };
- yield { id: "text_1", type: "text_start" };
- yield {
- id: "text_1",
- text: "Search summary from [Example](https://example.com)",
- type: "text_delta",
- };
- yield { id: "text_1", type: "text_end" };
- yield {
- finishReason: "stop",
- providerMetadata: { rawFinishReason: "end_turn" },
- type: "finish",
- usage: {
- inputTokens: 5,
- outputTokens: 7,
- serverToolUse: { webSearchRequests: 1 },
- totalTokens: 12,
- },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("Search current ZCode info");
- const innerRequest = requests.find(
- ({ observation }) =>
- observation.invocationContext?.metadata?.querySource === "web_search_tool",
- )?.request;
- expect(innerRequest.messages).toEqual([
- {
- content: "You are an assistant for performing a web search tool use.",
- role: "system",
- },
- { content: "Perform a web search for the query: latest zcode", role: "user" },
- ]);
- expect(innerRequest.tools.map((tool: any) => tool.name)).toEqual(["web_search"]);
- expect(innerRequest.tools[0]).toMatchObject({
- executionMode: "providerNative",
- providerNative: expect.objectContaining({
- args: expect.objectContaining({
- allowedDomains: ["example.com"],
- maxUses: 8,
- }),
- logicalName: "WebSearch",
- providerToolName: "web_search",
- }),
- });
- expect(innerRequest.tools[0].providerNative.args).not.toHaveProperty("searchContextSize");
- expect(innerRequest.toolChoice).toBeUndefined();
- expect(innerRequest.options).toEqual({
- maxOutputTokens: 4_096,
- reasoningLevel: "low",
- });
- const storedEvents = await eventStore.getEvents(sessionId);
- expect(
- storedEvents.some(
- (event: any) =>
- event.type === SessionEventType.ToolCallStarted &&
- event.payload?.toolName === "WebSearch",
- ),
- ).toBe(true);
- expect(
- storedEvents.some(
- (event: any) =>
- event.type === SessionEventType.ToolCallResult &&
- event.payload?.toolCallId === "call_websearch",
- ),
- ).toBe(true);
- });
- it("refreshes provider runtime headers before the WebSearch internal model request", async () => {
- const sessionId = createSessionId("runtime-websearch-refresh-runtime-headers");
- const eventStore = createTestSessionEventStore();
- const startPlanModelSelection: ModelSelection = {
- providerId: "account:zai-start-plan" as never,
- modelId: "GLM-5.1" as never,
- };
- const refreshes: string[] = [];
- const requests: any[] = [];
- let mainRequestCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- { modelSelection: createTestModelSelection(startPlanModelSelection) },
- {
- eventStore,
- providerRuntimeHeadersPort: {
- shouldRefreshBeforeModelRequest: ({ providerId }) =>
- providerId === "account:zai-start-plan",
- async refreshBeforeModelRequest(input) {
- refreshes.push(`${input.providerId}:${input.reason}`);
- return {
- headersApplied: true,
- };
- },
- },
- modelFactory: createTestModelFactory({
- properties: { supportsNativeWebSearch: true },
- async generateText(request: any, observation) {
- if (observation.invocationContext?.metadata?.querySource === "web_search_tool") {
- throw new Error("WebSearch side request must use streamText");
- }
- requests.push({ observation, request });
- mainRequestCount++;
- if (mainRequestCount === 1) {
- return {
- finishReason: "tool-calls",
- providerMetadata: undefined,
- text: "",
- toolCalls: [
- {
- id: "call_websearch",
- input: { query: "latest zcode" },
- name: "WebSearch",
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- providerMetadata: undefined,
- text: "Final answer with sources",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- async *streamText(request: any, observation) {
- requests.push({ observation, request });
- if (observation.invocationContext?.metadata?.querySource !== "web_search_tool") {
- throw new Error("Unexpected streaming request");
- }
- await observation.invocationContext?.refreshRuntimeHeadersBeforeAttempt?.({
- attempt: 1,
- abortSignal: request.abortSignal,
- providerId: String(observation.model.providerId),
- modelId: String(observation.model.modelId),
- traceContext: observation.invocationContext.traceContext,
- });
- yield { type: "start" };
- yield { id: "text_1", type: "text_start" };
- yield {
- id: "text_1",
- text: "Search summary from [Example](https://example.com)",
- type: "text_delta",
- };
- yield { id: "text_1", type: "text_end" };
- yield {
- finishReason: "stop",
- providerMetadata: { rawFinishReason: "end_turn" },
- type: "finish",
- usage: {
- inputTokens: 5,
- outputTokens: 7,
- serverToolUse: { webSearchRequests: 1 },
- totalTokens: 12,
- },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("Search current ZCode info");
- const innerRequest = requests.find(
- ({ observation }) =>
- observation.invocationContext?.metadata?.querySource === "web_search_tool",
- );
- expect(
- typeof innerRequest?.observation.invocationContext?.refreshRuntimeHeadersBeforeAttempt,
- ).toBe("function");
- expect(refreshes).toEqual(["account:zai-start-plan:model-request"]);
- });
- it("formats WebSearch tool results with markdown sources reminder", () => {
- const text = webSearchToolEntry.formatModelContent?.({
- durationMs: 10,
- modelUsage: {
- inputTokens: 5,
- outputTokens: 7,
- serverToolUse: { webSearchRequests: 1 },
- totalTokens: 12,
- },
- query: "latest zcode",
- results: [{ title: "Example", url: "https://example.com" }],
- sources: [],
- webSearchRequests: 1,
- });
- expect(String(text)).toContain('Web search results for query: "latest zcode"');
- expect(String(text)).toContain("- [Example](https://example.com)");
- expect(String(text)).toContain("markdown hyperlinks");
- });
- it("extracts typed provider WebSearch results from content wrappers", () => {
- const output = buildWebSearchOutput(
- { query: "latest zcode" },
- {
- finishReason: "stop",
- model: modelSelection,
- providerMetadata: undefined,
- text: "",
- toolResults: [
- {
- id: "server_search_1",
- name: "web_search",
- output: {
- content: [
- {
- title: "Wrapped Example",
- type: "web_search_result",
- url: "https://example.com/wrapped",
- },
- ],
- },
- providerExecuted: true,
- },
- ],
- usage: {
- inputTokens: 1,
- outputTokens: 1,
- totalTokens: 2,
- serverToolUse: { webSearchRequests: 1 },
- },
- },
- Date.now(),
- );
- expect(output.results).toContainEqual({
- title: "Wrapped Example",
- url: "https://example.com/wrapped",
- });
- expect(output.sources).toContainEqual({
- title: "Wrapped Example",
- url: "https://example.com/wrapped",
- });
- });
- it("extracts markdown sources from streaming WebSearch summaries", () => {
- const output = buildWebSearchOutput(
- { query: "latest zcode" },
- {
- finishReason: "stop",
- model: modelSelection,
- providerMetadata: undefined,
- text: "Search summary from [Example](https://example.com).",
- usage: {
- inputTokens: 1,
- outputTokens: 1,
- totalTokens: 2,
- serverToolUse: { webSearchRequests: 1 },
- },
- },
- Date.now(),
- );
- expect(output.sources).toContainEqual({
- title: "Example",
- url: "https://example.com",
- });
- });
- });
- function createRuntime(
- sessionId: string,
- config: ConstructorParameters<typeof AgentRuntime>[1] = {},
- deps: Partial<ConstructorParameters<typeof AgentRuntime>[2]> = {},
- ): AgentRuntime {
- return createTestAgentRuntime(
- createSessionId(sessionId),
- {
- modelSelection: createTestModelSelection(modelSelection),
- ...config,
- },
- {
- eventStore: createTestSessionEventStore(),
- ...deps,
- },
- );
- }
|