feat(web-search-deepseek): resolve provider options from the settings section
The provider now takes a thunk rather than a value: it projects the
authoritative section per search, so a stored endpoint, model, or key
reference reaches the next call without re-registering the provider — which
would make the seam's provider selection observable as a flicker.
apiKey already carries role('secret'), so the section is safe to describe:
the literal never rides a response in any layer and a configuration surface
learns only that a key is set.
This commit is contained in:
@@ -8,6 +8,12 @@ import {
|
||||
DEEPSEEK_DEFAULT_MODEL,
|
||||
} from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
|
||||
/** Construct the provider over a fixed options value; production passes a live thunk. */
|
||||
import type { DeepSeekSearchProviderOptions } from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
|
||||
const searchProvider = (options: DeepSeekSearchProviderOptions): DeepSeekSearchProvider =>
|
||||
new DeepSeekSearchProvider(() => options)
|
||||
|
||||
/**
|
||||
* Disabled real-API probe for the DeepSeek search provider. The live endpoint
|
||||
* can complete without structured source blocks, so this is not a reliable
|
||||
@@ -18,7 +24,7 @@ const maybe = apiKey !== undefined && apiKey.length > 0 ? describe : describe.sk
|
||||
|
||||
maybe('DeepSeekSearchProvider real API', () => {
|
||||
it.skip('returns citeable sources for a live query via native web_search', async () => {
|
||||
const provider = new DeepSeekSearchProvider({
|
||||
const provider = searchProvider({
|
||||
apiKey: apiKey!,
|
||||
baseURL: process.env.DEEPSEEK_SEARCH_BASE_URL ?? DEEPSEEK_DEFAULT_BASE_URL,
|
||||
model: process.env.DEEPSEEK_SEARCH_MODEL ?? DEEPSEEK_DEFAULT_MODEL,
|
||||
|
||||
@@ -15,6 +15,12 @@ import * as deepseekPlugin from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
import { citationSnippets, mapAnthropicResponse } from '../src/provider.ts'
|
||||
import type { AnthropicResponse } from '@deepseek-ai/dsh-web-search-deepseek/src/types.ts'
|
||||
|
||||
/** Construct the provider over a fixed options value; production passes a live thunk. */
|
||||
import type { DeepSeekSearchProviderOptions } from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
|
||||
const searchProvider = (options: DeepSeekSearchProviderOptions): DeepSeekSearchProvider =>
|
||||
new DeepSeekSearchProvider(() => options)
|
||||
|
||||
const options = {
|
||||
apiKey: 'ds-key',
|
||||
baseURL: 'https://api.deepseek.test/anthropic/v1',
|
||||
@@ -142,21 +148,21 @@ describe('mapAnthropicResponse', () => {
|
||||
|
||||
describe('DeepSeekSearchProvider availability', () => {
|
||||
it('is unavailable without a key', () => {
|
||||
expect(new DeepSeekSearchProvider({ ...options, apiKey: '' }).available()).toBe(false)
|
||||
expect(searchProvider({ ...options, apiKey: '' }).available()).toBe(false)
|
||||
})
|
||||
|
||||
it('is available with a key', () => {
|
||||
expect(new DeepSeekSearchProvider(options).available()).toBe(true)
|
||||
expect(searchProvider(options).available()).toBe(true)
|
||||
})
|
||||
|
||||
it('is misconfigured when the base URL is unparseable', () => {
|
||||
expect(new DeepSeekSearchProvider({ ...options, baseURL: 'not a url' }).available()).toBe(false)
|
||||
expect(searchProvider({ ...options, baseURL: 'not a url' }).available()).toBe(false)
|
||||
})
|
||||
|
||||
it('is misconfigured when request limits are not positive integers', () => {
|
||||
expect(new DeepSeekSearchProvider({ ...options, maxTokens: 0 }).available()).toBe(false)
|
||||
expect(new DeepSeekSearchProvider({ ...options, maxUses: 0 }).available()).toBe(false)
|
||||
expect(new DeepSeekSearchProvider({ ...options, maxUses: 1.5 }).available()).toBe(false)
|
||||
expect(searchProvider({ ...options, maxTokens: 0 }).available()).toBe(false)
|
||||
expect(searchProvider({ ...options, maxUses: 0 }).available()).toBe(false)
|
||||
expect(searchProvider({ ...options, maxUses: 1.5 }).available()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -165,7 +171,7 @@ describe('DeepSeekSearchProvider request mapping', () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
||||
const recordRequest = vi.fn()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
await new DeepSeekSearchProvider({ ...options, recordRequest }).search({ query: 'hello' })
|
||||
await searchProvider({ ...options, recordRequest }).search({ query: 'hello' })
|
||||
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(url).toBe('https://api.deepseek.test/anthropic/v1/messages')
|
||||
expect(init).toMatchObject({ method: 'POST', redirect: 'error' })
|
||||
@@ -193,7 +199,7 @@ describe('DeepSeekSearchProvider request mapping', () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const controller = new AbortController()
|
||||
await new DeepSeekSearchProvider(options).search({ query: 'q' }, controller.signal)
|
||||
await searchProvider(options).search({ query: 'q' }, controller.signal)
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(init.signal).toBe(controller.signal)
|
||||
})
|
||||
@@ -207,7 +213,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const controller = new AbortController()
|
||||
controller.abort(new Error('caller stopped'))
|
||||
await expect(new DeepSeekSearchProvider({
|
||||
await expect(searchProvider({
|
||||
...options,
|
||||
apiKey: '',
|
||||
resolveApiKey,
|
||||
@@ -225,7 +231,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
const fetchMock = vi.fn()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const controller = new AbortController()
|
||||
const search = new DeepSeekSearchProvider({
|
||||
const search = searchProvider({
|
||||
...options,
|
||||
apiKey: '',
|
||||
resolveApiKey,
|
||||
@@ -242,7 +248,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(searchResponse()))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const controller = new AbortController()
|
||||
await expect(new DeepSeekSearchProvider({
|
||||
await expect(searchProvider({
|
||||
...options,
|
||||
apiKey: '',
|
||||
resolveApiKey: async () => 'resolved-key',
|
||||
@@ -253,7 +259,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
|
||||
it('maps a credential resolver rejection under an active signal to WEB_PROVIDER_ERROR', async () => {
|
||||
const controller = new AbortController()
|
||||
await expect(new DeepSeekSearchProvider({
|
||||
await expect(searchProvider({
|
||||
...options,
|
||||
apiKey: '',
|
||||
resolveApiKey: () => Promise.reject(new Error('credential backend failed')),
|
||||
@@ -265,7 +271,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
})
|
||||
|
||||
it('uses the default credential reference when no resolver is configured', async () => {
|
||||
await expect(new DeepSeekSearchProvider({ ...options, apiKey: '' }).search({ query: 'q' }))
|
||||
await expect(searchProvider({ ...options, apiKey: '' }).search({ query: 'q' }))
|
||||
.rejects.toThrow('DeepSeek search has no API key for "DEEPSEEK_API_KEY"')
|
||||
})
|
||||
|
||||
@@ -273,7 +279,7 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
const controller = new AbortController()
|
||||
const fetchMock = vi.fn()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
await expect(new DeepSeekSearchProvider({
|
||||
await expect(searchProvider({
|
||||
...options,
|
||||
apiKey: '',
|
||||
resolveApiKey: () => {
|
||||
@@ -287,31 +293,31 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
|
||||
it('maps an HTTP error to WEB_PROVIDER_ERROR with the provider message', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ error: { message: 'rate limited' } }, { status: 429 })))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR', message: 'rate limited' }))
|
||||
})
|
||||
|
||||
it('handles a string-form error body', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ error: 'bad request' }, { status: 400 })))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ message: 'bad request' }))
|
||||
})
|
||||
|
||||
it('keeps a status-line message when the error body is not JSON', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new Response('upstream error', { status: 503 })))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ message: 'DeepSeek API error (HTTP 503)' }))
|
||||
})
|
||||
|
||||
it('keeps the status-line message when the JSON error body carries no detail', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({}, { status: 500 })))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ message: 'DeepSeek API error (HTTP 500)' }))
|
||||
})
|
||||
|
||||
it('maps an abort to WEB_ABORTED', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new DOMException('aborted', 'AbortError'))))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
||||
})
|
||||
|
||||
@@ -321,46 +327,46 @@ describe('DeepSeekSearchProvider error handling', () => {
|
||||
await new Promise<Response>((_resolve, reject) => {
|
||||
init?.signal?.addEventListener('abort', () => { reject(new Error('custom abort reason')) }, { once: true })
|
||||
})))
|
||||
const search = new DeepSeekSearchProvider(options).search({ query: 'q' }, controller.signal)
|
||||
const search = searchProvider(options).search({ query: 'q' }, controller.signal)
|
||||
controller.abort(new Error('timeout reason'))
|
||||
await expect(search).rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
||||
})
|
||||
|
||||
it('maps an unparseable success body to WEB_PROVIDER_ERROR', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new Response('not json', { status: 200 })))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
||||
})
|
||||
|
||||
it('maps a well-formed body of the wrong shape to WEB_PROVIDER_ERROR, not a raw TypeError', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ content: {} }, { status: 200 })))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
||||
})
|
||||
|
||||
it('surfaces an abort during success-body parse as WEB_ABORTED', async () => {
|
||||
const body = { json: () => Promise.reject(new DOMException('aborted', 'AbortError')), ok: true, status: 200 }
|
||||
vi.stubGlobal('fetch', vi.fn(async () => body as unknown as Response))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
||||
})
|
||||
|
||||
it('surfaces an abort during error-body parse as WEB_ABORTED', async () => {
|
||||
const body = { json: () => Promise.reject(new DOMException('aborted', 'AbortError')), ok: false, status: 500 }
|
||||
vi.stubGlobal('fetch', vi.fn(async () => body as unknown as Response))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_ABORTED' }))
|
||||
})
|
||||
|
||||
it('maps a network failure to WEB_PROVIDER_ERROR', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new TypeError('connection refused'))))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
||||
})
|
||||
|
||||
it('strict mode flows through search(): a prose-only response throws WEB_PROVIDER_ERROR', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ content: [{ type: 'text', text: 'no search happened' }] })))
|
||||
await expect(new DeepSeekSearchProvider(options).search({ query: 'q' }))
|
||||
await expect(searchProvider(options).search({ query: 'q' }))
|
||||
.rejects.toThrow(expect.objectContaining({ code: 'WEB_PROVIDER_ERROR' }))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,6 +8,12 @@ import { createServer, type IncomingMessage, type Server } from 'node:http'
|
||||
import type { AddressInfo } from 'node:net'
|
||||
import { DeepSeekSearchProvider } from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
|
||||
/** Construct the provider over a fixed options value; production passes a live thunk. */
|
||||
import type { DeepSeekSearchProviderOptions } from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
|
||||
const searchProvider = (options: DeepSeekSearchProviderOptions): DeepSeekSearchProvider =>
|
||||
new DeepSeekSearchProvider(() => options)
|
||||
|
||||
const TEST_API_KEY = 'redirect-test-key'
|
||||
const TEST_QUERY = 'private redirect query'
|
||||
const targetRequests: ReceivedRequest[] = []
|
||||
@@ -46,7 +52,7 @@ afterAll(async () => {
|
||||
describe('DeepSeekSearchProvider redirect policy', () => {
|
||||
it.each([301, 302, 303, 307, 308])('rejects HTTP %i before contacting Location', async (status) => {
|
||||
targetRequests.length = 0
|
||||
const provider = new DeepSeekSearchProvider({
|
||||
const provider = searchProvider({
|
||||
apiKey: TEST_API_KEY,
|
||||
baseURL: `${redirectOrigin}/${status}`,
|
||||
model: 'deepseek-chat',
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
/** The `web-search-deepseek` settings section layered over the composition entry. */
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { Context } from 'cordis'
|
||||
import type { Fiber } from 'cordis'
|
||||
import { Settings } from '@deepseek-ai/dsh-settings'
|
||||
import type { SettingsNamespace } from '@deepseek-ai/dsh-settings'
|
||||
import WebService from '@deepseek-ai/dsh-web'
|
||||
import * as deepseekPlugin from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
import { WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE } from '@deepseek-ai/dsh-web-search-deepseek'
|
||||
|
||||
/** The smallest real provider: one in-memory document, always writable. */
|
||||
class MemorySettings extends Settings {
|
||||
doc: Record<string, unknown> = {}
|
||||
|
||||
get writable(): boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
protected load(): Promise<Record<string, unknown>> {
|
||||
return Promise.resolve(structuredClone(this.doc))
|
||||
}
|
||||
|
||||
protected persist(ns: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
|
||||
this.doc = { ...this.doc, [ns]: structuredClone(section) }
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
|
||||
function jsonResponse(body: unknown): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})
|
||||
}
|
||||
|
||||
/** The smallest Anthropic-shaped answer the provider accepts — enough to observe the request. */
|
||||
const ONE_RESULT = {
|
||||
content: [
|
||||
{ type: 'text', text: 'ok' },
|
||||
{
|
||||
type: 'web_search_tool_result',
|
||||
content: [{ type: 'web_search_result', url: 'https://a.test', title: 'A' }],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
async function boot(): Promise<{ ctx: Context; settingsFiber: Fiber; pluginFiber: Fiber }> {
|
||||
const ctx = new Context()
|
||||
await ctx.plugin(WebService, {})
|
||||
const settingsFiber = ctx.plugin(MemorySettings)
|
||||
await settingsFiber.await()
|
||||
const pluginFiber = ctx.plugin(deepseekPlugin, { apiKey: 'ds-key', baseURL: 'https://search.entry.test/v1' })
|
||||
await pluginFiber.await()
|
||||
return { ctx, settingsFiber, pluginFiber }
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
/**
|
||||
* Run one search and answer the endpoint it reached. A fresh `Response` per
|
||||
* call because a body can only be read once, and the call history is cleared
|
||||
* because repeated `spyOn` returns the same spy.
|
||||
* @param ctx - context whose `ctx.web` serves the search.
|
||||
* @returns the URL the provider fetched.
|
||||
*/
|
||||
async function searchOnce(ctx: Context): Promise<string> {
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch')
|
||||
.mockImplementation(() => Promise.resolve(jsonResponse(ONE_RESULT)))
|
||||
fetchSpy.mockClear()
|
||||
await ctx.web.search({ query: 'anything' })
|
||||
return String((fetchSpy.mock.calls.at(-1)?.[0] as URL | string | undefined) ?? '')
|
||||
}
|
||||
|
||||
describe('web-search-deepseek settings section', () => {
|
||||
it('serves a stored endpoint to the next search without re-registering the provider', async () => {
|
||||
const bench = await boot()
|
||||
expect(await searchOnce(bench.ctx)).toContain('https://search.entry.test/v1')
|
||||
|
||||
await bench.ctx.settings.update(WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE, {
|
||||
baseURL: 'https://search.stored.test/v1',
|
||||
})
|
||||
|
||||
expect(await searchOnce(bench.ctx)).toContain('https://search.stored.test/v1')
|
||||
await bench.ctx.fiber.dispose()
|
||||
})
|
||||
|
||||
it('keeps the literal key out of every described layer', async () => {
|
||||
const bench = await boot()
|
||||
await bench.ctx.settings.update(WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE, { apiKey: 'ds-stored-secret' })
|
||||
|
||||
const [descriptor] = bench.ctx.settings.describe({ redactSecrets: true })
|
||||
.filter(row => String(row.ns) === 'web-search-deepseek')
|
||||
|
||||
expect(JSON.stringify(descriptor)).not.toContain('ds-stored-secret')
|
||||
expect(descriptor?.secrets).toEqual([{ path: ['apiKey'], set: true }])
|
||||
await bench.ctx.fiber.dispose()
|
||||
})
|
||||
|
||||
it('falls back to the composition entry when the settings provider detaches', async () => {
|
||||
const bench = await boot()
|
||||
await bench.ctx.settings.update(WEB_SEARCH_DEEPSEEK_SETTINGS_NAMESPACE, {
|
||||
baseURL: 'https://search.stored.test/v1',
|
||||
})
|
||||
expect(await searchOnce(bench.ctx)).toContain('https://search.stored.test/v1')
|
||||
|
||||
await bench.settingsFiber.dispose()
|
||||
|
||||
expect(await searchOnce(bench.ctx)).toContain('https://search.entry.test/v1')
|
||||
await bench.ctx.fiber.dispose()
|
||||
})
|
||||
|
||||
it('releases the namespace when the plugin unloads', async () => {
|
||||
const bench = await boot()
|
||||
expect(bench.ctx.settings.describe().map(row => String(row.ns))).toContain('web-search-deepseek')
|
||||
|
||||
await bench.pluginFiber.dispose()
|
||||
|
||||
expect(bench.ctx.settings.describe().map(row => String(row.ns))).not.toContain('web-search-deepseek')
|
||||
await bench.ctx.fiber.dispose()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user