@unieai/rabi-web 0.2.1-rc.3 → 0.2.2-rc.5
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/lib/index.js +260 -84
- 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.js +45 -0
- package/lib/types/index.d.ts +62 -15
- package/lib/types/index.js +301 -0
- package/lib/types/types.d.ts +84 -9
- package/lib/types/types.js +17 -0
- package/locale/de.json +6 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/package.json +27 -4
- package/src/index.ts +337 -0
- package/src/types.ts +208 -0
package/lib/types/index.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Service Definition for the web access capability seam (`ctx.web`): registries and provider-selecting execution for search and
|
|
3
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,
|
|
5
|
-
*
|
|
4
|
+
* be usable; without one, exactly one usable provider is required, except that a search prefers the
|
|
5
|
+
* usable DeepSeek provider assigned to the route of the model request it serves, so selection never
|
|
6
|
+
* depends on registration order.
|
|
6
7
|
* @module @unieai/rabi-web
|
|
7
8
|
*/
|
|
8
|
-
import { Context,
|
|
9
|
+
import { Context, type Volatile } from '@unieai/cordis';
|
|
10
|
+
import { TypertRemoteService } from '@unieai/rabi-typert-protocol';
|
|
9
11
|
import z from '@unieai/schemastery';
|
|
10
|
-
import type { WebFetchProvider, WebFetchRequest, WebFetchResult, WebSearchProvider, WebSearchRequest, WebSearchResult } from './types.ts';
|
|
12
|
+
import type { WebFetchProvider, WebFetchRequest, WebFetchResult, WebProviderList, WebSearchProvider, WebSearchRequest, WebSearchResult } from './types.ts';
|
|
11
13
|
export { WebError, } from './types.ts';
|
|
12
|
-
export type { WebFetchBody, WebFetchProvider, WebFetchRequest, WebFetchResult, WebSearchProvider, WebSearchRequest, WebSearchResult, WebSearchSource, } from './types.ts';
|
|
14
|
+
export type { WebFetchBody, WebFetchProvider, WebFetchRequest, WebFetchResult, WebProviderInfo, WebProviderList, WebSearchProvider, WebSearchRequest, WebSearchResult, WebSearchSource, } from './types.ts';
|
|
13
15
|
declare module '@unieai/cordis' {
|
|
14
16
|
interface Context {
|
|
15
17
|
web: WebRuntime;
|
|
@@ -24,24 +26,32 @@ declare module '@unieai/cordis' {
|
|
|
24
26
|
* fields rather than introduce a hidden priority chain.
|
|
25
27
|
*/
|
|
26
28
|
export interface WebRuntimeConfig {
|
|
27
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Explicit search provider id. Omitted = the usable DeepSeek provider assigned to the route of the model
|
|
31
|
+
* request, and otherwise auto-select when exactly one usable.
|
|
32
|
+
*/
|
|
28
33
|
readonly searchProvider: Volatile<string | undefined>;
|
|
29
34
|
/** Explicit fetch provider id. Omitted = auto-select when exactly one usable. */
|
|
30
35
|
readonly fetchProvider: Volatile<string | undefined>;
|
|
31
36
|
}
|
|
32
37
|
/**
|
|
33
|
-
* The web access service. Registered as `ctx.web` (one instance per context)
|
|
38
|
+
* The web access service. Registered as `ctx.web` (one instance per context)
|
|
39
|
+
* and exposed to Clients as the `web` Remote namespace (`listProviders`).
|
|
34
40
|
*
|
|
35
41
|
* Selection semantics (resolved at execution time, never order-dependent):
|
|
36
42
|
* - A configured id that is registered and `available()` → that provider.
|
|
37
43
|
* - A configured id not registered → `WEB_PROVIDER_CONFIGURED_MISSING`.
|
|
38
44
|
* - A configured id registered but unavailable →
|
|
39
45
|
* `WEB_PROVIDER_CONFIGURED_UNAVAILABLE`.
|
|
46
|
+
* - No search id configured, and the provider assigned to the route of the model request
|
|
47
|
+
* the search serves is usable → that provider: `deepseek-official` (Messages search) on the
|
|
48
|
+
* `deepseek-official` and `deepseek-account` routes, `deepseek-search-api` on every other
|
|
49
|
+
* route and outside a model request.
|
|
40
50
|
* - No id configured, exactly one registered usable provider → that provider.
|
|
41
51
|
* - No id configured, multiple usable providers → `WEB_PROVIDER_AMBIGUOUS`.
|
|
42
52
|
* - No id configured, no usable provider → `WEB_PROVIDER_UNAVAILABLE`.
|
|
43
53
|
*/
|
|
44
|
-
export declare class WebRuntime extends
|
|
54
|
+
export declare class WebRuntime extends TypertRemoteService {
|
|
45
55
|
private readonly config;
|
|
46
56
|
/**
|
|
47
57
|
* Provider selection config. Operational env overrides feed the SAME fields:
|
|
@@ -61,32 +71,58 @@ export declare class WebRuntime extends Service {
|
|
|
61
71
|
private readonly fetchProviderEnv;
|
|
62
72
|
constructor(ctx: Context, config: WebRuntimeConfig);
|
|
63
73
|
/**
|
|
64
|
-
* Register a search provider.
|
|
65
|
-
*
|
|
66
|
-
*
|
|
74
|
+
* Register a search provider. `listProviders()` lists it until the
|
|
75
|
+
* registration is disposed, which happens with the calling fiber or through
|
|
76
|
+
* the returned disposer; registration and disposal each emit
|
|
77
|
+
* `web/providers-changed`. Throws {@link WebError} `WEB_DUPLICATE_PROVIDER`
|
|
78
|
+
* if its id is already registered for search.
|
|
67
79
|
* @param provider - the provider; its `id` is the registry key.
|
|
68
80
|
* @returns the disposer that unregisters the provider.
|
|
69
81
|
*/
|
|
70
82
|
registerSearchProvider(provider: WebSearchProvider): () => void;
|
|
71
83
|
/**
|
|
72
|
-
* Register a fetch provider
|
|
73
|
-
*
|
|
74
|
-
*
|
|
84
|
+
* Register a fetch provider, with the listing, disposal, and event behavior
|
|
85
|
+
* of {@link WebRuntime.registerSearchProvider}. Throws {@link WebError}
|
|
86
|
+
* `WEB_DUPLICATE_PROVIDER` if its id is already registered for fetch.
|
|
75
87
|
* @param provider - the provider; its `id` is the registry key.
|
|
76
88
|
* @returns the disposer that unregisters the provider.
|
|
77
89
|
*/
|
|
78
90
|
registerFetchProvider(provider: WebFetchProvider): () => void;
|
|
79
91
|
private registerProvider;
|
|
92
|
+
/**
|
|
93
|
+
* List every registered search and fetch provider for provider pickers.
|
|
94
|
+
* Availability does not filter the lists.
|
|
95
|
+
* @returns detached `{ id, title, description }` entries per capability, in registration order.
|
|
96
|
+
*/
|
|
97
|
+
listProviders(): WebProviderList;
|
|
98
|
+
/** Emit `web/providers-changed`, containing each listener's failure so none vetoes the mutation. */
|
|
99
|
+
private emitProvidersChanged;
|
|
100
|
+
private warnListenerFailure;
|
|
80
101
|
/**
|
|
81
102
|
* Run one search through the selected provider. Resolves the provider at call
|
|
82
103
|
* time with the selection rules above; throws {@link WebError} when the
|
|
83
104
|
* capability cannot run. The seam enforces `request.maxResults` on the result:
|
|
84
105
|
* if the provider over-returns, `sources[]` is truncated and `truncated` set.
|
|
85
|
-
* @param request - the query
|
|
106
|
+
* @param request - the query, the optional result limit, and the route of the model request the search serves.
|
|
86
107
|
* @param signal - optional cancellation signal forwarded to the provider.
|
|
87
108
|
* @returns the provider's results, capped to `request.maxResults`.
|
|
88
109
|
*/
|
|
89
110
|
search(request: WebSearchRequest, signal?: AbortSignal): Promise<WebSearchResult>;
|
|
111
|
+
/**
|
|
112
|
+
* Whether the provider `search()` would select can authenticate a search made for a
|
|
113
|
+
* model request on the `llmProvider` chat route. Asks only that provider's `ready()`;
|
|
114
|
+
* one without `ready()` counts as ready. Makes no network calls. A selection failure
|
|
115
|
+
* answers true, because `search()` reports that failure as a structured `WebError`.
|
|
116
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
117
|
+
* @returns false only when the selected provider reports that it cannot authenticate.
|
|
118
|
+
*/
|
|
119
|
+
searchReady(llmProvider?: string): Promise<boolean>;
|
|
120
|
+
/**
|
|
121
|
+
* Select the search provider with the rules above, throwing the matching {@link WebError}.
|
|
122
|
+
* @param llmProvider - provider route id of the model request the search serves, when known.
|
|
123
|
+
* @returns the provider the search runs through.
|
|
124
|
+
*/
|
|
125
|
+
private selectSearchProvider;
|
|
90
126
|
/**
|
|
91
127
|
* Retrieve one URL through the selected provider. Resolves the provider at
|
|
92
128
|
* call time with the selection rules above; throws {@link WebError} when the
|
|
@@ -96,6 +132,17 @@ export declare class WebRuntime extends Service {
|
|
|
96
132
|
* @returns the retrieval outcome; non-2xx responses resolve descriptively.
|
|
97
133
|
*/
|
|
98
134
|
fetch(request: WebFetchRequest, signal?: AbortSignal): Promise<WebFetchResult>;
|
|
135
|
+
/**
|
|
136
|
+
* Whether the provider `fetch()` would select can authenticate a fetch made for a
|
|
137
|
+
* model request on the `llmProvider` chat route. Asks only that provider's `ready()`;
|
|
138
|
+
* one without `ready()` counts as ready. Makes no network calls. A selection failure
|
|
139
|
+
* answers true, because `fetch()` reports that failure as a structured `WebError`.
|
|
140
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
141
|
+
* @returns false only when the selected provider reports that it cannot authenticate.
|
|
142
|
+
*/
|
|
143
|
+
fetchReady(llmProvider?: string): Promise<boolean>;
|
|
144
|
+
/** Select the fetch provider with the rules above, throwing the matching {@link WebError}. */
|
|
145
|
+
private selectFetchProvider;
|
|
99
146
|
}
|
|
100
147
|
export default WebRuntime;
|
|
101
148
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,301 @@
|
|
|
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, except that a search prefers the
|
|
5
|
+
* usable DeepSeek provider assigned to the route of the model request it serves, so selection never
|
|
6
|
+
* depends on registration order.
|
|
7
|
+
* @module @unieai/rabi-web
|
|
8
|
+
*/
|
|
9
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
10
|
+
var useValue = arguments.length > 2;
|
|
11
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
12
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
13
|
+
}
|
|
14
|
+
return useValue ? value : void 0;
|
|
15
|
+
};
|
|
16
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
17
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
18
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
19
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
20
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
21
|
+
var _, done = false;
|
|
22
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
23
|
+
var context = {};
|
|
24
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
25
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
26
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
27
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
28
|
+
if (kind === "accessor") {
|
|
29
|
+
if (result === void 0) continue;
|
|
30
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
31
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
32
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
33
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
34
|
+
}
|
|
35
|
+
else if (_ = accept(result)) {
|
|
36
|
+
if (kind === "field") initializers.unshift(_);
|
|
37
|
+
else descriptor[key] = _;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
41
|
+
done = true;
|
|
42
|
+
};
|
|
43
|
+
import { Remote, TypertRemoteService } from '@unieai/rabi-typert-protocol';
|
|
44
|
+
import z from '@unieai/schemastery';
|
|
45
|
+
import { WebError } from "./types.js";
|
|
46
|
+
export { WebError, } from "./types.js";
|
|
47
|
+
/** Provider routes of the DeepSeek API-key and account models. */
|
|
48
|
+
const DEEPSEEK_MODEL_ROUTES = new Set(['deepseek-official', 'deepseek-account']);
|
|
49
|
+
/**
|
|
50
|
+
* Search provider preferred for a model request on a DeepSeek model route while none is configured: DeepSeek
|
|
51
|
+
* Messages search, which DeepSeek serves without a per-search charge.
|
|
52
|
+
*/
|
|
53
|
+
const DEEPSEEK_ROUTE_SEARCH_PROVIDER = 'deepseek-official';
|
|
54
|
+
/** Search provider preferred on every other route and outside a model request while none is configured. */
|
|
55
|
+
const OTHER_ROUTE_SEARCH_PROVIDER = 'deepseek-search-api';
|
|
56
|
+
/**
|
|
57
|
+
* The web access service. Registered as `ctx.web` (one instance per context)
|
|
58
|
+
* and exposed to Clients as the `web` Remote namespace (`listProviders`).
|
|
59
|
+
*
|
|
60
|
+
* Selection semantics (resolved at execution time, never order-dependent):
|
|
61
|
+
* - A configured id that is registered and `available()` → that provider.
|
|
62
|
+
* - A configured id not registered → `WEB_PROVIDER_CONFIGURED_MISSING`.
|
|
63
|
+
* - A configured id registered but unavailable →
|
|
64
|
+
* `WEB_PROVIDER_CONFIGURED_UNAVAILABLE`.
|
|
65
|
+
* - No search id configured, and the provider assigned to the route of the model request
|
|
66
|
+
* the search serves is usable → that provider: `deepseek-official` (Messages search) on the
|
|
67
|
+
* `deepseek-official` and `deepseek-account` routes, `deepseek-search-api` on every other
|
|
68
|
+
* route and outside a model request.
|
|
69
|
+
* - No id configured, exactly one registered usable provider → that provider.
|
|
70
|
+
* - No id configured, multiple usable providers → `WEB_PROVIDER_AMBIGUOUS`.
|
|
71
|
+
* - No id configured, no usable provider → `WEB_PROVIDER_UNAVAILABLE`.
|
|
72
|
+
*/
|
|
73
|
+
let WebRuntime = (() => {
|
|
74
|
+
let _classSuper = TypertRemoteService;
|
|
75
|
+
let _instanceExtraInitializers = [];
|
|
76
|
+
let _listProviders_decorators;
|
|
77
|
+
return class WebRuntime extends _classSuper {
|
|
78
|
+
static {
|
|
79
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
80
|
+
_listProviders_decorators = [Remote];
|
|
81
|
+
__esDecorate(this, null, _listProviders_decorators, { kind: "method", name: "listProviders", static: false, private: false, access: { has: obj => "listProviders" in obj, get: obj => obj.listProviders }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
82
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
83
|
+
}
|
|
84
|
+
config = __runInitializers(this, _instanceExtraInitializers);
|
|
85
|
+
/**
|
|
86
|
+
* Provider selection config. Operational env overrides feed the SAME fields:
|
|
87
|
+
* `$RABI_WEB_SEARCH_PROVIDER` / `$RABI_WEB_FETCH_PROVIDER` are equivalent to
|
|
88
|
+
* `searchProvider` / `fetchProvider` and are NOT a hidden priority chain.
|
|
89
|
+
*/
|
|
90
|
+
static Config = z.object({
|
|
91
|
+
searchProvider: z.string().volatile(),
|
|
92
|
+
fetchProvider: z.string().volatile(),
|
|
93
|
+
});
|
|
94
|
+
searchProviders = new Map();
|
|
95
|
+
fetchProviders = new Map();
|
|
96
|
+
searchProviderEnv;
|
|
97
|
+
fetchProviderEnv;
|
|
98
|
+
constructor(ctx, config) {
|
|
99
|
+
super(ctx, 'web');
|
|
100
|
+
this.config = config;
|
|
101
|
+
this.searchProviderEnv = process.env.RABI_WEB_SEARCH_PROVIDER;
|
|
102
|
+
this.fetchProviderEnv = process.env.RABI_WEB_FETCH_PROVIDER;
|
|
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) {
|
|
114
|
+
return this.registerProvider(this.searchProviders, provider);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Register a fetch provider, with the listing, disposal, and event behavior
|
|
118
|
+
* of {@link WebRuntime.registerSearchProvider}. Throws {@link WebError}
|
|
119
|
+
* `WEB_DUPLICATE_PROVIDER` if its id is already registered for fetch.
|
|
120
|
+
* @param provider - the provider; its `id` is the registry key.
|
|
121
|
+
* @returns the disposer that unregisters the provider.
|
|
122
|
+
*/
|
|
123
|
+
registerFetchProvider(provider) {
|
|
124
|
+
return this.registerProvider(this.fetchProviders, provider);
|
|
125
|
+
}
|
|
126
|
+
registerProvider(store, provider) {
|
|
127
|
+
if (store.has(provider.id)) {
|
|
128
|
+
throw new WebError(`a web provider with id "${provider.id}" is already registered`, 'WEB_DUPLICATE_PROVIDER');
|
|
129
|
+
}
|
|
130
|
+
const emitChanged = () => { this.emitProvidersChanged(); };
|
|
131
|
+
const dispose = this.ctx.effect(function* () {
|
|
132
|
+
store.set(provider.id, provider);
|
|
133
|
+
emitChanged();
|
|
134
|
+
yield () => {
|
|
135
|
+
store.delete(provider.id);
|
|
136
|
+
emitChanged();
|
|
137
|
+
};
|
|
138
|
+
}, 'web.registerProvider()');
|
|
139
|
+
// ctx.effect's disposer returns Promise<void>; our disposer API is
|
|
140
|
+
// synchronous fire-and-forget — discard the (always-resolved) promise.
|
|
141
|
+
return () => void dispose();
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* List every registered search and fetch provider for provider pickers.
|
|
145
|
+
* Availability does not filter the lists.
|
|
146
|
+
* @returns detached `{ id, title, description }` entries per capability, in registration order.
|
|
147
|
+
*/
|
|
148
|
+
listProviders() {
|
|
149
|
+
return {
|
|
150
|
+
search: [...this.searchProviders.values()].map(providerInfo),
|
|
151
|
+
fetch: [...this.fetchProviders.values()].map(providerInfo),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** Emit `web/providers-changed`, containing each listener's failure so none vetoes the mutation. */
|
|
155
|
+
emitProvidersChanged() {
|
|
156
|
+
for (const listener of this.ctx.events.dispatch('emit', ['web/providers-changed'])) {
|
|
157
|
+
try {
|
|
158
|
+
const returned = listener();
|
|
159
|
+
void Promise.resolve(returned).catch((error) => { this.warnListenerFailure(error); });
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
this.warnListenerFailure(error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
warnListenerFailure(error) {
|
|
167
|
+
this.ctx.logger.warn('web: a web/providers-changed listener failed');
|
|
168
|
+
this.ctx.logger.warn(error);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Run one search through the selected provider. Resolves the provider at call
|
|
172
|
+
* time with the selection rules above; throws {@link WebError} when the
|
|
173
|
+
* capability cannot run. The seam enforces `request.maxResults` on the result:
|
|
174
|
+
* if the provider over-returns, `sources[]` is truncated and `truncated` set.
|
|
175
|
+
* @param request - the query, the optional result limit, and the route of the model request the search serves.
|
|
176
|
+
* @param signal - optional cancellation signal forwarded to the provider.
|
|
177
|
+
* @returns the provider's results, capped to `request.maxResults`.
|
|
178
|
+
*/
|
|
179
|
+
async search(request, signal) {
|
|
180
|
+
const result = await this.selectSearchProvider(request.llmProvider).search(request, signal);
|
|
181
|
+
return capSources(result, request.maxResults);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Whether the provider `search()` would select can authenticate a search made for a
|
|
185
|
+
* model request on the `llmProvider` chat route. Asks only that provider's `ready()`;
|
|
186
|
+
* one without `ready()` counts as ready. Makes no network calls. A selection failure
|
|
187
|
+
* answers true, because `search()` reports that failure as a structured `WebError`.
|
|
188
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
189
|
+
* @returns false only when the selected provider reports that it cannot authenticate.
|
|
190
|
+
*/
|
|
191
|
+
async searchReady(llmProvider) {
|
|
192
|
+
return await selectedProviderReady(() => this.selectSearchProvider(llmProvider), llmProvider);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Select the search provider with the rules above, throwing the matching {@link WebError}.
|
|
196
|
+
* @param llmProvider - provider route id of the model request the search serves, when known.
|
|
197
|
+
* @returns the provider the search runs through.
|
|
198
|
+
*/
|
|
199
|
+
selectSearchProvider(llmProvider) {
|
|
200
|
+
const configuredId = this.config.searchProvider.get() ?? this.searchProviderEnv;
|
|
201
|
+
const preferredId = llmProvider !== undefined && DEEPSEEK_MODEL_ROUTES.has(llmProvider)
|
|
202
|
+
? DEEPSEEK_ROUTE_SEARCH_PROVIDER
|
|
203
|
+
: OTHER_ROUTE_SEARCH_PROVIDER;
|
|
204
|
+
return resolveProvider({
|
|
205
|
+
providers: this.searchProviders,
|
|
206
|
+
preferredId,
|
|
207
|
+
...configuredId !== undefined ? { configuredId } : {},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Retrieve one URL through the selected provider. Resolves the provider at
|
|
212
|
+
* call time with the selection rules above; throws {@link WebError} when the
|
|
213
|
+
* capability cannot run. A non-2xx response is a result, not a throw.
|
|
214
|
+
* @param request - the URL plus retrieval options.
|
|
215
|
+
* @param signal - optional cancellation signal forwarded to the provider.
|
|
216
|
+
* @returns the retrieval outcome; non-2xx responses resolve descriptively.
|
|
217
|
+
*/
|
|
218
|
+
async fetch(request, signal) {
|
|
219
|
+
return await this.selectFetchProvider().fetch(request, signal);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Whether the provider `fetch()` would select can authenticate a fetch made for a
|
|
223
|
+
* model request on the `llmProvider` chat route. Asks only that provider's `ready()`;
|
|
224
|
+
* one without `ready()` counts as ready. Makes no network calls. A selection failure
|
|
225
|
+
* answers true, because `fetch()` reports that failure as a structured `WebError`.
|
|
226
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
227
|
+
* @returns false only when the selected provider reports that it cannot authenticate.
|
|
228
|
+
*/
|
|
229
|
+
async fetchReady(llmProvider) {
|
|
230
|
+
return await selectedProviderReady(() => this.selectFetchProvider(), llmProvider);
|
|
231
|
+
}
|
|
232
|
+
/** Select the fetch provider with the rules above, throwing the matching {@link WebError}. */
|
|
233
|
+
selectFetchProvider() {
|
|
234
|
+
const configuredId = this.config.fetchProvider.get() ?? this.fetchProviderEnv;
|
|
235
|
+
return resolveProvider({
|
|
236
|
+
providers: this.fetchProviders,
|
|
237
|
+
...configuredId !== undefined ? { configuredId } : {},
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
})();
|
|
242
|
+
export { WebRuntime };
|
|
243
|
+
/** Project one provider to its picker entry, omitting an absent title or description. */
|
|
244
|
+
function providerInfo(provider) {
|
|
245
|
+
return {
|
|
246
|
+
id: provider.id,
|
|
247
|
+
...provider.title === undefined ? {} : { title: provider.title },
|
|
248
|
+
...provider.description === undefined ? {} : { description: provider.description },
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Ask the provider an operation selects whether it can authenticate for one chat route.
|
|
253
|
+
* @param select - the operation's selection; throws its {@link WebError} when no provider qualifies.
|
|
254
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
255
|
+
* @returns the provider's answer; true when it has no `ready()` or selection fails, because the
|
|
256
|
+
* operation reports that failure itself.
|
|
257
|
+
*/
|
|
258
|
+
async function selectedProviderReady(select, llmProvider) {
|
|
259
|
+
let provider;
|
|
260
|
+
try {
|
|
261
|
+
provider = select();
|
|
262
|
+
}
|
|
263
|
+
catch (_selectionFailure) {
|
|
264
|
+
return true; /* The operation surfaces the same failure when called. */
|
|
265
|
+
}
|
|
266
|
+
return await provider.ready?.(llmProvider) ?? true;
|
|
267
|
+
}
|
|
268
|
+
/** Resolve the selected provider or throw the matching {@link WebError}. */
|
|
269
|
+
function resolveProvider(selection) {
|
|
270
|
+
const { configuredId, providers } = selection;
|
|
271
|
+
if (configuredId !== undefined) {
|
|
272
|
+
const provider = providers.get(configuredId);
|
|
273
|
+
if (!provider) {
|
|
274
|
+
throw new WebError(`configured web provider "${configuredId}" is not registered`, 'WEB_PROVIDER_CONFIGURED_MISSING');
|
|
275
|
+
}
|
|
276
|
+
if (!provider.available()) {
|
|
277
|
+
throw new WebError(`configured web provider "${configuredId}" is registered but unavailable`, 'WEB_PROVIDER_CONFIGURED_UNAVAILABLE');
|
|
278
|
+
}
|
|
279
|
+
return provider;
|
|
280
|
+
}
|
|
281
|
+
const usable = [...providers.values()].filter(provider => provider.available());
|
|
282
|
+
const preferred = usable.find(provider => provider.id === selection.preferredId);
|
|
283
|
+
if (preferred !== undefined)
|
|
284
|
+
return preferred;
|
|
285
|
+
const [single] = usable;
|
|
286
|
+
if (single === undefined) {
|
|
287
|
+
throw new WebError('no usable web provider is registered', 'WEB_PROVIDER_UNAVAILABLE');
|
|
288
|
+
}
|
|
289
|
+
if (usable.length > 1) {
|
|
290
|
+
const ids = usable.map(provider => provider.id).join(', ');
|
|
291
|
+
throw new WebError(`multiple usable web providers are registered (${ids}); configure one explicitly`, 'WEB_PROVIDER_AMBIGUOUS');
|
|
292
|
+
}
|
|
293
|
+
return single;
|
|
294
|
+
}
|
|
295
|
+
/** Enforce `maxResults` on a search result: truncate `sources[]` and flag it. */
|
|
296
|
+
function capSources(result, maxResults) {
|
|
297
|
+
if (maxResults === undefined || result.sources.length <= maxResults)
|
|
298
|
+
return result;
|
|
299
|
+
return { ...result, sources: result.sources.slice(0, maxResults), truncated: true };
|
|
300
|
+
}
|
|
301
|
+
export default WebRuntime;
|
package/lib/types/types.d.ts
CHANGED
|
@@ -5,6 +5,20 @@
|
|
|
5
5
|
* @module @unieai/rabi-web/types
|
|
6
6
|
*/
|
|
7
7
|
import { HarnessError } from '@unieai/rabi-llm';
|
|
8
|
+
import type { LocalizedText } from '@unieai/rabi-package-manifest';
|
|
9
|
+
declare module '@unieai/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,22 @@ export interface WebSearchRequest {
|
|
|
16
30
|
/**
|
|
17
31
|
* Upper bound on returned sources; the seam truncates to it. Omitted = no
|
|
18
32
|
* bound. `rabi-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;
|
|
38
|
+
/**
|
|
39
|
+
* Provider route id of the model request this search serves. While no search
|
|
40
|
+
* provider is configured, the seam selects with it, as `searchReady()` selects
|
|
41
|
+
* with its argument. Omitted = the search serves no model request.
|
|
42
|
+
*/
|
|
43
|
+
readonly llmProvider?: string;
|
|
24
44
|
}
|
|
25
45
|
/**
|
|
26
46
|
* Normalized search outcome. `content` is optional provider-generated answer
|
|
27
|
-
* text or summary (
|
|
28
|
-
*
|
|
47
|
+
* text or summary (DeepSeek returns none; a provider whose API generates an
|
|
48
|
+
* answer returns it here).
|
|
29
49
|
* `sources[]` is the portable citation shape. `truncated` is set by the seam
|
|
30
50
|
* when it cut `sources[]` down to `maxResults`.
|
|
31
51
|
*/
|
|
@@ -38,15 +58,18 @@ export interface WebSearchResult {
|
|
|
38
58
|
readonly truncated: boolean;
|
|
39
59
|
}
|
|
40
60
|
/**
|
|
41
|
-
* One citeable source. A source always has a URL; `title`, `snippet`,
|
|
42
|
-
* `publishedAt` are optional because not every provider returns them — forcing
|
|
43
|
-
* adapters to invent them would make the seam lie (
|
|
44
|
-
* URL-only). `rabi-tool-web` renders `title ?? hostname(url)` for display.
|
|
61
|
+
* One citeable source. A source always has a URL; `title`, `snippet`, `summary`,
|
|
62
|
+
* and `publishedAt` are optional because not every provider returns them — forcing
|
|
63
|
+
* adapters to invent them would make the seam lie (some providers return
|
|
64
|
+
* URL-only citations). `rabi-tool-web` renders `title ?? hostname(url)` for display.
|
|
45
65
|
*/
|
|
46
66
|
export interface WebSearchSource {
|
|
47
67
|
readonly url: string;
|
|
48
68
|
readonly title?: string;
|
|
69
|
+
/** Short excerpt suited to display. */
|
|
49
70
|
readonly snippet?: string;
|
|
71
|
+
/** Longer excerpt suited to model reading; consumers bound its length. */
|
|
72
|
+
readonly summary?: string;
|
|
50
73
|
/** Publication/crawl timestamp as a provider-supplied ISO-8601 string. */
|
|
51
74
|
readonly publishedAt?: string;
|
|
52
75
|
}
|
|
@@ -97,8 +120,24 @@ export type WebFetchBody = {
|
|
|
97
120
|
*/
|
|
98
121
|
export interface WebSearchProvider {
|
|
99
122
|
readonly id: string;
|
|
123
|
+
/** Display name of this provider; consumers display `id` when it is omitted. */
|
|
124
|
+
readonly title?: string;
|
|
125
|
+
/**
|
|
126
|
+
* What a provider picker shows under this provider when it is selected: what the provider
|
|
127
|
+
* is suited for and what it costs or cannot do. Translations are keyed by lowercase language
|
|
128
|
+
* id with a required English entry; consumers show nothing when it is omitted.
|
|
129
|
+
*/
|
|
130
|
+
readonly description?: LocalizedText;
|
|
100
131
|
/** Cheap local usability check; must not make network calls. */
|
|
101
132
|
available(): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Whether a search made for a model request on the `llmProvider` chat route has a
|
|
135
|
+
* credential to send. Checks configuration only and must not make network calls.
|
|
136
|
+
* Omitted = ready.
|
|
137
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
138
|
+
* @returns false when that request's searches could not authenticate.
|
|
139
|
+
*/
|
|
140
|
+
ready?(llmProvider: string | undefined): Promise<boolean>;
|
|
102
141
|
/** Run one search; honor `signal` for cancellation. */
|
|
103
142
|
search(request: WebSearchRequest, signal?: AbortSignal): Promise<WebSearchResult>;
|
|
104
143
|
}
|
|
@@ -108,11 +147,47 @@ export interface WebSearchProvider {
|
|
|
108
147
|
*/
|
|
109
148
|
export interface WebFetchProvider {
|
|
110
149
|
readonly id: string;
|
|
150
|
+
/** Display name of this provider; consumers display `id` when it is omitted. */
|
|
151
|
+
readonly title?: string;
|
|
152
|
+
/**
|
|
153
|
+
* What a provider picker shows under this provider when it is selected: what the provider
|
|
154
|
+
* is suited for and what it costs or cannot do. Translations are keyed by lowercase language
|
|
155
|
+
* id with a required English entry; consumers show nothing when it is omitted.
|
|
156
|
+
*/
|
|
157
|
+
readonly description?: LocalizedText;
|
|
111
158
|
/** Cheap local usability check; must not make network calls. */
|
|
112
159
|
available(): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Whether a fetch made for a model request on the `llmProvider` chat route has a
|
|
162
|
+
* credential to send. Checks configuration only and must not make network calls.
|
|
163
|
+
* Omitted = ready.
|
|
164
|
+
* @param llmProvider - the provider route id of the model request, when known.
|
|
165
|
+
* @returns false when that request's fetches could not authenticate.
|
|
166
|
+
*/
|
|
167
|
+
ready?(llmProvider: string | undefined): Promise<boolean>;
|
|
113
168
|
/** Retrieve one URL; honor `signal` for cancellation. */
|
|
114
169
|
fetch(request: WebFetchRequest, signal?: AbortSignal): Promise<WebFetchResult>;
|
|
115
170
|
}
|
|
171
|
+
/** One registered provider as listed by {@link WebProviderList}. */
|
|
172
|
+
export interface WebProviderInfo {
|
|
173
|
+
/** The provider's registry id, the value `searchProvider` / `fetchProvider` select. */
|
|
174
|
+
readonly id: string;
|
|
175
|
+
/** The provider's display name; absent when the provider declares none. */
|
|
176
|
+
readonly title?: string;
|
|
177
|
+
/** The provider's picker description; absent when the provider declares none. */
|
|
178
|
+
readonly description?: LocalizedText;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Every currently registered search and fetch provider, each list in
|
|
182
|
+
* registration order. Availability does not filter the lists: a registered
|
|
183
|
+
* provider whose `available()` answers false is still listed.
|
|
184
|
+
*/
|
|
185
|
+
export interface WebProviderList {
|
|
186
|
+
/** Registered search providers. */
|
|
187
|
+
readonly search: readonly WebProviderInfo[];
|
|
188
|
+
/** Registered fetch providers. */
|
|
189
|
+
readonly fetch: readonly WebProviderInfo[];
|
|
190
|
+
}
|
|
116
191
|
/**
|
|
117
192
|
* Typed web error with a machine-routable, open-string `code` and chained `cause`.
|
|
118
193
|
* Consumers must tolerate provider-specific codes. Shared codes cover unavailable,
|
|
@@ -0,0 +1,17 @@
|
|
|
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 @unieai/rabi-web/types
|
|
6
|
+
*/
|
|
7
|
+
import { HarnessError } from '@unieai/rabi-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
|
+
}
|
package/locale/de.json
ADDED
package/locale/es.json
ADDED
package/locale/fr.json
ADDED
package/locale/ja.json
ADDED
package/locale/pt.json
ADDED