@deepseek-ai/dsh-web 0.2.2-rc.1 → 0.2.2-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +18 -12
- package/README.md +38 -7
- package/README.zh.md +38 -7
- package/lib/index.js +220 -112
- package/lib/typert.host.d.ts +3 -0
- package/lib/typert.host.js +178 -0
- package/lib/typert.remote-client.d.ts +25 -0
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +45 -0
- package/lib/types/index.d.ts +24 -11
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +277 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/types.d.ts +57 -7
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +18 -0
- package/lib/types/types.js.map +1 -0
- package/package.json +28 -5
- package/src/index.ts +305 -0
- package/src/types.ts +202 -0
package/lib/types/types.d.ts
CHANGED
|
@@ -5,6 +5,20 @@
|
|
|
5
5
|
* @module @deepseek-ai/dsh-web/types
|
|
6
6
|
*/
|
|
7
7
|
import { HarnessError } from '@deepseek-ai/dsh-llm';
|
|
8
|
+
import type { LocalizedText } from '@deepseek-ai/dsh-package-manifest';
|
|
9
|
+
declare module '@deepseek-ai/cordis' {
|
|
10
|
+
interface Events {
|
|
11
|
+
/**
|
|
12
|
+
* The set of registered search or fetch providers changed: one provider
|
|
13
|
+
* registered, or its registration was disposed. Payload-free; consumers
|
|
14
|
+
* re-read `ctx.web.listProviders()` (Remote `web/listProviders`) for the
|
|
15
|
+
* new list. Fires after each committed registry mutation. Observer failures
|
|
16
|
+
* are contained and cannot veto the mutation.
|
|
17
|
+
* @mode emit
|
|
18
|
+
*/
|
|
19
|
+
'web/providers-changed'(): void;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
8
22
|
/**
|
|
9
23
|
* What one search-capable backend is asked to search. Each request carries one
|
|
10
24
|
* query; a consumer may issue several requests. `maxResults` is a
|
|
@@ -16,16 +30,16 @@ export interface WebSearchRequest {
|
|
|
16
30
|
/**
|
|
17
31
|
* Upper bound on returned sources; the seam truncates to it. Omitted = no
|
|
18
32
|
* bound. `dsh-tool-web` always sets it. A provider whose API supports a
|
|
19
|
-
* result-count control (
|
|
20
|
-
* layer as a cost/latency optimization; the seam enforces the
|
|
21
|
-
* regardless.
|
|
33
|
+
* result-count control (the DeepSeek Search API's `count`) should apply it
|
|
34
|
+
* at the request layer as a cost/latency optimization; the seam enforces the
|
|
35
|
+
* bound regardless.
|
|
22
36
|
*/
|
|
23
37
|
readonly maxResults?: number;
|
|
24
38
|
}
|
|
25
39
|
/**
|
|
26
40
|
* Normalized search outcome. `content` is optional provider-generated answer
|
|
27
|
-
* text or summary (
|
|
28
|
-
*
|
|
41
|
+
* text or summary (DeepSeek returns none; a provider whose API generates an
|
|
42
|
+
* answer returns it here).
|
|
29
43
|
* `sources[]` is the portable citation shape. `truncated` is set by the seam
|
|
30
44
|
* when it cut `sources[]` down to `maxResults`.
|
|
31
45
|
*/
|
|
@@ -40,8 +54,8 @@ export interface WebSearchResult {
|
|
|
40
54
|
/**
|
|
41
55
|
* One citeable source. A source always has a URL; `title`, `snippet`, `summary`,
|
|
42
56
|
* and `publishedAt` are optional because not every provider returns them — forcing
|
|
43
|
-
* adapters to invent them would make the seam lie (
|
|
44
|
-
* URL-only). `dsh-tool-web` renders `title ?? hostname(url)` for display.
|
|
57
|
+
* adapters to invent them would make the seam lie (some providers return
|
|
58
|
+
* URL-only citations). `dsh-tool-web` renders `title ?? hostname(url)` for display.
|
|
45
59
|
*/
|
|
46
60
|
export interface WebSearchSource {
|
|
47
61
|
readonly url: string;
|
|
@@ -100,6 +114,14 @@ export type WebFetchBody = {
|
|
|
100
114
|
*/
|
|
101
115
|
export interface WebSearchProvider {
|
|
102
116
|
readonly id: string;
|
|
117
|
+
/** Display name of this provider; consumers display `id` when it is omitted. */
|
|
118
|
+
readonly title?: string;
|
|
119
|
+
/**
|
|
120
|
+
* What a provider picker shows under this provider when it is selected: what the provider
|
|
121
|
+
* is suited for and what it costs or cannot do. Translations are keyed by lowercase language
|
|
122
|
+
* id with a required English entry; consumers show nothing when it is omitted.
|
|
123
|
+
*/
|
|
124
|
+
readonly description?: LocalizedText;
|
|
103
125
|
/** Cheap local usability check; must not make network calls. */
|
|
104
126
|
available(): boolean;
|
|
105
127
|
/**
|
|
@@ -119,6 +141,14 @@ export interface WebSearchProvider {
|
|
|
119
141
|
*/
|
|
120
142
|
export interface WebFetchProvider {
|
|
121
143
|
readonly id: string;
|
|
144
|
+
/** Display name of this provider; consumers display `id` when it is omitted. */
|
|
145
|
+
readonly title?: string;
|
|
146
|
+
/**
|
|
147
|
+
* What a provider picker shows under this provider when it is selected: what the provider
|
|
148
|
+
* is suited for and what it costs or cannot do. Translations are keyed by lowercase language
|
|
149
|
+
* id with a required English entry; consumers show nothing when it is omitted.
|
|
150
|
+
*/
|
|
151
|
+
readonly description?: LocalizedText;
|
|
122
152
|
/** Cheap local usability check; must not make network calls. */
|
|
123
153
|
available(): boolean;
|
|
124
154
|
/**
|
|
@@ -132,6 +162,26 @@ export interface WebFetchProvider {
|
|
|
132
162
|
/** Retrieve one URL; honor `signal` for cancellation. */
|
|
133
163
|
fetch(request: WebFetchRequest, signal?: AbortSignal): Promise<WebFetchResult>;
|
|
134
164
|
}
|
|
165
|
+
/** One registered provider as listed by {@link WebProviderList}. */
|
|
166
|
+
export interface WebProviderInfo {
|
|
167
|
+
/** The provider's registry id, the value `searchProvider` / `fetchProvider` select. */
|
|
168
|
+
readonly id: string;
|
|
169
|
+
/** The provider's display name; absent when the provider declares none. */
|
|
170
|
+
readonly title?: string;
|
|
171
|
+
/** The provider's picker description; absent when the provider declares none. */
|
|
172
|
+
readonly description?: LocalizedText;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Every currently registered search and fetch provider, each list in
|
|
176
|
+
* registration order. Availability does not filter the lists: a registered
|
|
177
|
+
* provider whose `available()` answers false is still listed.
|
|
178
|
+
*/
|
|
179
|
+
export interface WebProviderList {
|
|
180
|
+
/** Registered search providers. */
|
|
181
|
+
readonly search: readonly WebProviderInfo[];
|
|
182
|
+
/** Registered fetch providers. */
|
|
183
|
+
readonly fetch: readonly WebProviderInfo[];
|
|
184
|
+
}
|
|
135
185
|
/**
|
|
136
186
|
* Typed web error with a machine-routable, open-string `code` and chained `cause`.
|
|
137
187
|
* Consumers must tolerate provider-specific codes. Shared codes cover unavailable,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vocabulary for the web capability seam (`ctx.web`). Search and fetch deliberately share one
|
|
3
|
+
* seam so provider selection, cancellation, errors, and product configuration have one owner,
|
|
4
|
+
* while retaining separate request and result types.
|
|
5
|
+
* @module @deepseek-ai/dsh-web/types
|
|
6
|
+
*/
|
|
7
|
+
import { HarnessError } from '@deepseek-ai/dsh-llm';
|
|
8
|
+
/**
|
|
9
|
+
* Typed web error with a machine-routable, open-string `code` and chained `cause`.
|
|
10
|
+
* Consumers must tolerate provider-specific codes. Shared codes cover unavailable,
|
|
11
|
+
* missing, unusable, ambiguous, or duplicate providers, cancellation, and provider failure;
|
|
12
|
+
* the local fetch provider additionally distinguishes invalid or blocked URLs, redirects,
|
|
13
|
+
* size and timeout limits, and unsupported content types. Tool execution exposes the code in
|
|
14
|
+
* structured error metadata.
|
|
15
|
+
*/
|
|
16
|
+
export class WebError extends HarnessError {
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AA0LnD;;;;;;;GAOG;AACH,MAAM,OAAO,QAAS,SAAQ,YAAY;CAAG"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepseek-ai/dsh-web",
|
|
3
3
|
"description": "Abstract web access capability seam (ctx.web) for the DeepSeek Harness — search/fetch provider registry, registration-order-independent selection, request/result vocabulary, and the WebError taxonomy",
|
|
4
|
-
"version": "0.2.2-rc.
|
|
4
|
+
"version": "0.2.2-rc.2",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -18,6 +18,18 @@
|
|
|
18
18
|
"types": "./lib/types/index.d.ts",
|
|
19
19
|
"default": "./lib/index.js"
|
|
20
20
|
},
|
|
21
|
+
"./types": {
|
|
22
|
+
"types": "./lib/types/types.d.ts",
|
|
23
|
+
"default": "./lib/types/types.js"
|
|
24
|
+
},
|
|
25
|
+
"./typert": {
|
|
26
|
+
"types": "./lib/typert.host.d.ts",
|
|
27
|
+
"default": "./lib/typert.host.js"
|
|
28
|
+
},
|
|
29
|
+
"./remote": {
|
|
30
|
+
"types": "./lib/typert.remote-client.d.ts",
|
|
31
|
+
"default": "./lib/typert.remote-client.js"
|
|
32
|
+
},
|
|
21
33
|
"./src/*": "./src/*",
|
|
22
34
|
"./package.json": "./package.json",
|
|
23
35
|
"./locale/*.json": "./locale/*.json"
|
|
@@ -25,18 +37,29 @@
|
|
|
25
37
|
"files": [
|
|
26
38
|
"locale/*.json",
|
|
27
39
|
"lib/index.js",
|
|
28
|
-
"lib/types/**/*.
|
|
40
|
+
"lib/types/**/*.js",
|
|
41
|
+
"lib/types/**/*.d.ts",
|
|
42
|
+
"lib/types/**/*.d.ts.map",
|
|
43
|
+
"lib/**/*.js.map",
|
|
44
|
+
"lib/typert.host.js",
|
|
45
|
+
"lib/typert.host.d.ts",
|
|
46
|
+
"lib/typert.remote-client.js",
|
|
47
|
+
"lib/typert.remote-client.d.ts",
|
|
48
|
+
"lib/typert.remote-client.d.ts.map",
|
|
49
|
+
"src"
|
|
29
50
|
],
|
|
30
51
|
"license": "MIT",
|
|
31
52
|
"peerDependencies": {
|
|
32
|
-
"@deepseek-ai/dsh-llm": "0.2.2-rc.
|
|
53
|
+
"@deepseek-ai/dsh-llm": "0.2.2-rc.2",
|
|
33
54
|
"@deepseek-ai/cordis": "~4.1.0"
|
|
34
55
|
},
|
|
35
56
|
"dependencies": {
|
|
36
|
-
"@deepseek-ai/
|
|
57
|
+
"@deepseek-ai/dsh-package-manifest": "0.2.2-rc.2",
|
|
58
|
+
"@deepseek-ai/schemastery": "~3.19.0",
|
|
59
|
+
"@deepseek-ai/dsh-typert-protocol": "0.2.2-rc.2"
|
|
37
60
|
},
|
|
38
61
|
"devDependencies": {
|
|
39
|
-
"@deepseek-ai/dsh-llm": "0.2.2-rc.
|
|
62
|
+
"@deepseek-ai/dsh-llm": "0.2.2-rc.2",
|
|
40
63
|
"@deepseek-ai/cordis": "~4.1.0"
|
|
41
64
|
}
|
|
42
65
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service Definition for the web access capability seam (`ctx.web`): registries and provider-selecting execution for search and
|
|
3
|
+
* fetch. Duplicate ids are rejected. At execution time, a configured provider must exist and
|
|
4
|
+
* be usable; without one, exactly one usable provider is required, so selection never depends
|
|
5
|
+
* on registration order.
|
|
6
|
+
* @module @deepseek-ai/dsh-web
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Context, type Volatile } from '@deepseek-ai/cordis'
|
|
10
|
+
import { Remote, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
|
|
11
|
+
import z from '@deepseek-ai/schemastery'
|
|
12
|
+
import type {
|
|
13
|
+
WebFetchProvider,
|
|
14
|
+
WebFetchRequest,
|
|
15
|
+
WebFetchResult,
|
|
16
|
+
WebProviderInfo,
|
|
17
|
+
WebProviderList,
|
|
18
|
+
WebSearchProvider,
|
|
19
|
+
WebSearchRequest,
|
|
20
|
+
WebSearchResult,
|
|
21
|
+
} from './types.ts'
|
|
22
|
+
import { WebError } from './types.ts'
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
WebError,
|
|
26
|
+
} from './types.ts'
|
|
27
|
+
export type {
|
|
28
|
+
WebFetchBody,
|
|
29
|
+
WebFetchProvider,
|
|
30
|
+
WebFetchRequest,
|
|
31
|
+
WebFetchResult,
|
|
32
|
+
WebProviderInfo,
|
|
33
|
+
WebProviderList,
|
|
34
|
+
WebSearchProvider,
|
|
35
|
+
WebSearchRequest,
|
|
36
|
+
WebSearchResult,
|
|
37
|
+
WebSearchSource,
|
|
38
|
+
} from './types.ts'
|
|
39
|
+
|
|
40
|
+
declare module '@deepseek-ai/cordis' {
|
|
41
|
+
interface Context {
|
|
42
|
+
web: WebRuntime
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Selection inputs for execution-time provider resolution. */
|
|
47
|
+
interface Selection<P> {
|
|
48
|
+
/** The configured provider id for this capability, if any. */
|
|
49
|
+
readonly configuredId?: string
|
|
50
|
+
/** Providers registered for this capability kind. */
|
|
51
|
+
readonly providers: ReadonlyMap<string, P>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Config for the web seam. `searchProvider` / `fetchProvider` pin which provider
|
|
56
|
+
* wins for each capability; both are optional (a single registered usable
|
|
57
|
+
* provider auto-selects). Each operation reads the current value, so a settings
|
|
58
|
+
* change applies to the next search or fetch without remounting the service.
|
|
59
|
+
* Operational overrides such as environment variables must feed these same
|
|
60
|
+
* fields rather than introduce a hidden priority chain.
|
|
61
|
+
*/
|
|
62
|
+
export interface WebRuntimeConfig {
|
|
63
|
+
/** Explicit search provider id. Omitted = auto-select when exactly one usable. */
|
|
64
|
+
readonly searchProvider: Volatile<string | undefined>
|
|
65
|
+
/** Explicit fetch provider id. Omitted = auto-select when exactly one usable. */
|
|
66
|
+
readonly fetchProvider: Volatile<string | undefined>
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The web access service. Registered as `ctx.web` (one instance per context)
|
|
71
|
+
* and exposed to Clients as the `web` Remote namespace (`listProviders`).
|
|
72
|
+
*
|
|
73
|
+
* Selection semantics (resolved at execution time, never order-dependent):
|
|
74
|
+
* - A configured id that is registered and `available()` → that provider.
|
|
75
|
+
* - A configured id not registered → `WEB_PROVIDER_CONFIGURED_MISSING`.
|
|
76
|
+
* - A configured id registered but unavailable →
|
|
77
|
+
* `WEB_PROVIDER_CONFIGURED_UNAVAILABLE`.
|
|
78
|
+
* - No id configured, exactly one registered usable provider → that provider.
|
|
79
|
+
* - No id configured, multiple usable providers → `WEB_PROVIDER_AMBIGUOUS`.
|
|
80
|
+
* - No id configured, no usable provider → `WEB_PROVIDER_UNAVAILABLE`.
|
|
81
|
+
*/
|
|
82
|
+
export class WebRuntime extends TypertRemoteService {
|
|
83
|
+
/**
|
|
84
|
+
* Provider selection config. Operational env overrides feed the SAME fields:
|
|
85
|
+
* `$DSH_WEB_SEARCH_PROVIDER` / `$DSH_WEB_FETCH_PROVIDER` are equivalent to
|
|
86
|
+
* `searchProvider` / `fetchProvider` and are NOT a hidden priority chain.
|
|
87
|
+
*/
|
|
88
|
+
static Config = z.object({
|
|
89
|
+
searchProvider: z.string().volatile(),
|
|
90
|
+
fetchProvider: z.string().volatile(),
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
private searchProviders = new Map<string, WebSearchProvider>()
|
|
94
|
+
private fetchProviders = new Map<string, WebFetchProvider>()
|
|
95
|
+
private readonly searchProviderEnv: string | undefined
|
|
96
|
+
private readonly fetchProviderEnv: string | undefined
|
|
97
|
+
|
|
98
|
+
constructor(ctx: Context, private readonly config: WebRuntimeConfig) {
|
|
99
|
+
super(ctx, 'web')
|
|
100
|
+
this.searchProviderEnv = process.env.DSH_WEB_SEARCH_PROVIDER
|
|
101
|
+
this.fetchProviderEnv = process.env.DSH_WEB_FETCH_PROVIDER
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Register a search provider. `listProviders()` lists it until the
|
|
106
|
+
* registration is disposed, which happens with the calling fiber or through
|
|
107
|
+
* the returned disposer; registration and disposal each emit
|
|
108
|
+
* `web/providers-changed`. Throws {@link WebError} `WEB_DUPLICATE_PROVIDER`
|
|
109
|
+
* if its id is already registered for search.
|
|
110
|
+
* @param provider - the provider; its `id` is the registry key.
|
|
111
|
+
* @returns the disposer that unregisters the provider.
|
|
112
|
+
*/
|
|
113
|
+
registerSearchProvider(provider: WebSearchProvider): () => void {
|
|
114
|
+
return this.registerProvider(this.searchProviders, provider)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Register a fetch provider, with the listing, disposal, and event behavior
|
|
119
|
+
* of {@link WebRuntime.registerSearchProvider}. Throws {@link WebError}
|
|
120
|
+
* `WEB_DUPLICATE_PROVIDER` if its id is already registered for fetch.
|
|
121
|
+
* @param provider - the provider; its `id` is the registry key.
|
|
122
|
+
* @returns the disposer that unregisters the provider.
|
|
123
|
+
*/
|
|
124
|
+
registerFetchProvider(provider: WebFetchProvider): () => void {
|
|
125
|
+
return this.registerProvider(this.fetchProviders, provider)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private registerProvider<P extends { readonly id: string }>(store: Map<string, P>, provider: P): () => void {
|
|
129
|
+
if (store.has(provider.id)) {
|
|
130
|
+
throw new WebError(`a web provider with id "${provider.id}" is already registered`, 'WEB_DUPLICATE_PROVIDER')
|
|
131
|
+
}
|
|
132
|
+
const emitChanged = (): void => { this.emitProvidersChanged() }
|
|
133
|
+
const dispose = this.ctx.effect(function* () {
|
|
134
|
+
store.set(provider.id, provider)
|
|
135
|
+
emitChanged()
|
|
136
|
+
yield () => {
|
|
137
|
+
store.delete(provider.id)
|
|
138
|
+
emitChanged()
|
|
139
|
+
}
|
|
140
|
+
}, 'web.registerProvider()')
|
|
141
|
+
// ctx.effect's disposer returns Promise<void>; our disposer API is
|
|
142
|
+
// synchronous fire-and-forget — discard the (always-resolved) promise.
|
|
143
|
+
return () => void dispose()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* List every registered search and fetch provider for provider pickers.
|
|
148
|
+
* Availability does not filter the lists.
|
|
149
|
+
* @returns detached `{ id, title, description }` entries per capability, in registration order.
|
|
150
|
+
*/
|
|
151
|
+
@Remote
|
|
152
|
+
listProviders(): WebProviderList {
|
|
153
|
+
return {
|
|
154
|
+
search: [...this.searchProviders.values()].map(providerInfo),
|
|
155
|
+
fetch: [...this.fetchProviders.values()].map(providerInfo),
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Emit `web/providers-changed`, containing each listener's failure so none vetoes the mutation. */
|
|
160
|
+
private emitProvidersChanged(): void {
|
|
161
|
+
for (const listener of this.ctx.events.dispatch('emit', ['web/providers-changed'])) {
|
|
162
|
+
try {
|
|
163
|
+
const returned: unknown = listener()
|
|
164
|
+
void Promise.resolve(returned).catch((error: unknown) => { this.warnListenerFailure(error) })
|
|
165
|
+
} catch (error: unknown) {
|
|
166
|
+
this.warnListenerFailure(error)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private warnListenerFailure(error: unknown): void {
|
|
172
|
+
this.ctx.logger.warn('web: a web/providers-changed listener failed')
|
|
173
|
+
this.ctx.logger.warn(error)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Run one search through the selected provider. Resolves the provider at call
|
|
178
|
+
* time with the selection rules above; throws {@link WebError} when the
|
|
179
|
+
* capability cannot run. The seam enforces `request.maxResults` on the result:
|
|
180
|
+
* if the provider over-returns, `sources[]` is truncated and `truncated` set.
|
|
181
|
+
* @param request - the query and optional result limit.
|
|
182
|
+
* @param signal - optional cancellation signal forwarded to the provider.
|
|
183
|
+
* @returns the provider's results, capped to `request.maxResults`.
|
|
184
|
+
*/
|
|
185
|
+
async search(request: WebSearchRequest, signal?: AbortSignal): Promise<WebSearchResult> {
|
|
186
|
+
const result = await this.selectSearchProvider().search(request, signal)
|
|
187
|
+
return capSources(result, request.maxResults)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Whether the provider `search()` would select can authenticate a search made for a
|
|
192
|
+
* model request on the `llmProvider` chat route. Asks only that provider's `ready()`;
|
|
193
|
+
* one without `ready()` counts as ready. Makes no network calls. A selection failure
|
|
194
|
+
* answers true, because `search()` reports that failure as a structured `WebError`.
|
|
195
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
196
|
+
* @returns false only when the selected provider reports that it cannot authenticate.
|
|
197
|
+
*/
|
|
198
|
+
async searchReady(llmProvider?: string): Promise<boolean> {
|
|
199
|
+
return await selectedProviderReady(() => this.selectSearchProvider(), llmProvider)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Select the search provider with the rules above, throwing the matching {@link WebError}. */
|
|
203
|
+
private selectSearchProvider(): WebSearchProvider {
|
|
204
|
+
const configuredId = this.config.searchProvider.get() ?? this.searchProviderEnv
|
|
205
|
+
return resolveProvider({
|
|
206
|
+
providers: this.searchProviders,
|
|
207
|
+
...configuredId !== undefined ? { configuredId } : {},
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Retrieve one URL through the selected provider. Resolves the provider at
|
|
213
|
+
* call time with the selection rules above; throws {@link WebError} when the
|
|
214
|
+
* capability cannot run. A non-2xx response is a result, not a throw.
|
|
215
|
+
* @param request - the URL plus retrieval options.
|
|
216
|
+
* @param signal - optional cancellation signal forwarded to the provider.
|
|
217
|
+
* @returns the retrieval outcome; non-2xx responses resolve descriptively.
|
|
218
|
+
*/
|
|
219
|
+
async fetch(request: WebFetchRequest, signal?: AbortSignal): Promise<WebFetchResult> {
|
|
220
|
+
return await this.selectFetchProvider().fetch(request, signal)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Whether the provider `fetch()` would select can authenticate a fetch made for a
|
|
225
|
+
* model request on the `llmProvider` chat route. Asks only that provider's `ready()`;
|
|
226
|
+
* one without `ready()` counts as ready. Makes no network calls. A selection failure
|
|
227
|
+
* answers true, because `fetch()` reports that failure as a structured `WebError`.
|
|
228
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
229
|
+
* @returns false only when the selected provider reports that it cannot authenticate.
|
|
230
|
+
*/
|
|
231
|
+
async fetchReady(llmProvider?: string): Promise<boolean> {
|
|
232
|
+
return await selectedProviderReady(() => this.selectFetchProvider(), llmProvider)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Select the fetch provider with the rules above, throwing the matching {@link WebError}. */
|
|
236
|
+
private selectFetchProvider(): WebFetchProvider {
|
|
237
|
+
const configuredId = this.config.fetchProvider.get() ?? this.fetchProviderEnv
|
|
238
|
+
return resolveProvider({
|
|
239
|
+
providers: this.fetchProviders,
|
|
240
|
+
...configuredId !== undefined ? { configuredId } : {},
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Project one provider to its picker entry, omitting an absent title or description. */
|
|
246
|
+
function providerInfo(provider: WebSearchProvider | WebFetchProvider): WebProviderInfo {
|
|
247
|
+
return {
|
|
248
|
+
id: provider.id,
|
|
249
|
+
...provider.title === undefined ? {} : { title: provider.title },
|
|
250
|
+
...provider.description === undefined ? {} : { description: provider.description },
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
interface ResolvableProvider {
|
|
255
|
+
readonly id: string
|
|
256
|
+
available(): boolean
|
|
257
|
+
ready?(llmProvider: string | undefined): Promise<boolean>
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Ask the provider an operation selects whether it can authenticate for one chat route.
|
|
262
|
+
* @param select - the operation's selection; throws its {@link WebError} when no provider qualifies.
|
|
263
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
264
|
+
* @returns the provider's answer; true when it has no `ready()` or selection fails, because the
|
|
265
|
+
* operation reports that failure itself.
|
|
266
|
+
*/
|
|
267
|
+
async function selectedProviderReady(select: () => ResolvableProvider, llmProvider: string | undefined): Promise<boolean> {
|
|
268
|
+
let provider: ResolvableProvider
|
|
269
|
+
try { provider = select() }
|
|
270
|
+
catch (_selectionFailure) { return true /* The operation surfaces the same failure when called. */ }
|
|
271
|
+
return await provider.ready?.(llmProvider) ?? true
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Resolve the selected provider or throw the matching {@link WebError}. */
|
|
275
|
+
function resolveProvider<P extends ResolvableProvider>(selection: Selection<P>): P {
|
|
276
|
+
const { configuredId, providers } = selection
|
|
277
|
+
if (configuredId !== undefined) {
|
|
278
|
+
const provider = providers.get(configuredId)
|
|
279
|
+
if (!provider) {
|
|
280
|
+
throw new WebError(`configured web provider "${configuredId}" is not registered`, 'WEB_PROVIDER_CONFIGURED_MISSING')
|
|
281
|
+
}
|
|
282
|
+
if (!provider.available()) {
|
|
283
|
+
throw new WebError(`configured web provider "${configuredId}" is registered but unavailable`, 'WEB_PROVIDER_CONFIGURED_UNAVAILABLE')
|
|
284
|
+
}
|
|
285
|
+
return provider
|
|
286
|
+
}
|
|
287
|
+
const usable = [...providers.values()].filter(provider => provider.available())
|
|
288
|
+
const [single] = usable
|
|
289
|
+
if (single === undefined) {
|
|
290
|
+
throw new WebError('no usable web provider is registered', 'WEB_PROVIDER_UNAVAILABLE')
|
|
291
|
+
}
|
|
292
|
+
if (usable.length > 1) {
|
|
293
|
+
const ids = usable.map(provider => provider.id).join(', ')
|
|
294
|
+
throw new WebError(`multiple usable web providers are registered (${ids}); configure one explicitly`, 'WEB_PROVIDER_AMBIGUOUS')
|
|
295
|
+
}
|
|
296
|
+
return single
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Enforce `maxResults` on a search result: truncate `sources[]` and flag it. */
|
|
300
|
+
function capSources(result: WebSearchResult, maxResults: number | undefined): WebSearchResult {
|
|
301
|
+
if (maxResults === undefined || result.sources.length <= maxResults) return result
|
|
302
|
+
return { ...result, sources: result.sources.slice(0, maxResults), truncated: true }
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export default WebRuntime
|