@unieai/rabi-web 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +323 -0
- 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 +148 -0
- package/lib/types/index.js +301 -0
- package/lib/types/types.d.ts +201 -0
- package/lib/types/types.js +17 -0
- package/locale/de.json +6 -0
- package/locale/en.json +5 -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/locale/zh.json +6 -0
- package/package.json +55 -4
- package/src/index.ts +337 -0
- package/src/types.ts +208 -0
|
@@ -0,0 +1,148 @@
|
|
|
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
|
+
import { Context, type Volatile } from '@unieai/cordis';
|
|
10
|
+
import { TypertRemoteService } from '@unieai/rabi-typert-protocol';
|
|
11
|
+
import z from '@unieai/schemastery';
|
|
12
|
+
import type { WebFetchProvider, WebFetchRequest, WebFetchResult, WebProviderList, WebSearchProvider, WebSearchRequest, WebSearchResult } from './types.ts';
|
|
13
|
+
export { WebError, } from './types.ts';
|
|
14
|
+
export type { WebFetchBody, WebFetchProvider, WebFetchRequest, WebFetchResult, WebProviderInfo, WebProviderList, WebSearchProvider, WebSearchRequest, WebSearchResult, WebSearchSource, } from './types.ts';
|
|
15
|
+
declare module '@unieai/cordis' {
|
|
16
|
+
interface Context {
|
|
17
|
+
web: WebRuntime;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Config for the web seam. `searchProvider` / `fetchProvider` pin which provider
|
|
22
|
+
* wins for each capability; both are optional (a single registered usable
|
|
23
|
+
* provider auto-selects). Each operation reads the current value, so a settings
|
|
24
|
+
* change applies to the next search or fetch without remounting the service.
|
|
25
|
+
* Operational overrides such as environment variables must feed these same
|
|
26
|
+
* fields rather than introduce a hidden priority chain.
|
|
27
|
+
*/
|
|
28
|
+
export interface WebRuntimeConfig {
|
|
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
|
+
*/
|
|
33
|
+
readonly searchProvider: Volatile<string | undefined>;
|
|
34
|
+
/** Explicit fetch provider id. Omitted = auto-select when exactly one usable. */
|
|
35
|
+
readonly fetchProvider: Volatile<string | undefined>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The web access service. Registered as `ctx.web` (one instance per context)
|
|
39
|
+
* and exposed to Clients as the `web` Remote namespace (`listProviders`).
|
|
40
|
+
*
|
|
41
|
+
* Selection semantics (resolved at execution time, never order-dependent):
|
|
42
|
+
* - A configured id that is registered and `available()` → that provider.
|
|
43
|
+
* - A configured id not registered → `WEB_PROVIDER_CONFIGURED_MISSING`.
|
|
44
|
+
* - A configured id registered but unavailable →
|
|
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.
|
|
50
|
+
* - No id configured, exactly one registered usable provider → that provider.
|
|
51
|
+
* - No id configured, multiple usable providers → `WEB_PROVIDER_AMBIGUOUS`.
|
|
52
|
+
* - No id configured, no usable provider → `WEB_PROVIDER_UNAVAILABLE`.
|
|
53
|
+
*/
|
|
54
|
+
export declare class WebRuntime extends TypertRemoteService {
|
|
55
|
+
private readonly config;
|
|
56
|
+
/**
|
|
57
|
+
* Provider selection config. Operational env overrides feed the SAME fields:
|
|
58
|
+
* `$RABI_WEB_SEARCH_PROVIDER` / `$RABI_WEB_FETCH_PROVIDER` are equivalent to
|
|
59
|
+
* `searchProvider` / `fetchProvider` and are NOT a hidden priority chain.
|
|
60
|
+
*/
|
|
61
|
+
static Config: z<Schemastery.ObjectS<NoInfer<{
|
|
62
|
+
searchProvider: z<string, string, "volatile">;
|
|
63
|
+
fetchProvider: z<string, string, "volatile">;
|
|
64
|
+
}>>, Schemastery.ObjectT<NoInfer<{
|
|
65
|
+
searchProvider: z<string, string, "volatile">;
|
|
66
|
+
fetchProvider: z<string, string, "volatile">;
|
|
67
|
+
}>>, "plain">;
|
|
68
|
+
private searchProviders;
|
|
69
|
+
private fetchProviders;
|
|
70
|
+
private readonly searchProviderEnv;
|
|
71
|
+
private readonly fetchProviderEnv;
|
|
72
|
+
constructor(ctx: Context, config: WebRuntimeConfig);
|
|
73
|
+
/**
|
|
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.
|
|
79
|
+
* @param provider - the provider; its `id` is the registry key.
|
|
80
|
+
* @returns the disposer that unregisters the provider.
|
|
81
|
+
*/
|
|
82
|
+
registerSearchProvider(provider: WebSearchProvider): () => void;
|
|
83
|
+
/**
|
|
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.
|
|
87
|
+
* @param provider - the provider; its `id` is the registry key.
|
|
88
|
+
* @returns the disposer that unregisters the provider.
|
|
89
|
+
*/
|
|
90
|
+
registerFetchProvider(provider: WebFetchProvider): () => void;
|
|
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;
|
|
101
|
+
/**
|
|
102
|
+
* Run one search through the selected provider. Resolves the provider at call
|
|
103
|
+
* time with the selection rules above; throws {@link WebError} when the
|
|
104
|
+
* capability cannot run. The seam enforces `request.maxResults` on the result:
|
|
105
|
+
* if the provider over-returns, `sources[]` is truncated and `truncated` set.
|
|
106
|
+
* @param request - the query, the optional result limit, and the route of the model request the search serves.
|
|
107
|
+
* @param signal - optional cancellation signal forwarded to the provider.
|
|
108
|
+
* @returns the provider's results, capped to `request.maxResults`.
|
|
109
|
+
*/
|
|
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;
|
|
126
|
+
/**
|
|
127
|
+
* Retrieve one URL through the selected provider. Resolves the provider at
|
|
128
|
+
* call time with the selection rules above; throws {@link WebError} when the
|
|
129
|
+
* capability cannot run. A non-2xx response is a result, not a throw.
|
|
130
|
+
* @param request - the URL plus retrieval options.
|
|
131
|
+
* @param signal - optional cancellation signal forwarded to the provider.
|
|
132
|
+
* @returns the retrieval outcome; non-2xx responses resolve descriptively.
|
|
133
|
+
*/
|
|
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;
|
|
146
|
+
}
|
|
147
|
+
export default WebRuntime;
|
|
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;
|
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
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
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* What one search-capable backend is asked to search. Each request carries one
|
|
24
|
+
* query; a consumer may issue several requests. `maxResults` is a
|
|
25
|
+
* `rabi-tool-web`-layer bound passed through unchanged and enforced on the way
|
|
26
|
+
* back by the seam (see {@link WebSearchResult}).
|
|
27
|
+
*/
|
|
28
|
+
export interface WebSearchRequest {
|
|
29
|
+
readonly query: string;
|
|
30
|
+
/**
|
|
31
|
+
* Upper bound on returned sources; the seam truncates to it. Omitted = no
|
|
32
|
+
* bound. `rabi-tool-web` always sets it. A provider whose API supports a
|
|
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.
|
|
36
|
+
*/
|
|
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;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Normalized search outcome. `content` is optional provider-generated answer
|
|
47
|
+
* text or summary (DeepSeek returns none; a provider whose API generates an
|
|
48
|
+
* answer returns it here).
|
|
49
|
+
* `sources[]` is the portable citation shape. `truncated` is set by the seam
|
|
50
|
+
* when it cut `sources[]` down to `maxResults`.
|
|
51
|
+
*/
|
|
52
|
+
export interface WebSearchResult {
|
|
53
|
+
/** Optional provider-generated answer text, search context, or summary. */
|
|
54
|
+
readonly content?: string;
|
|
55
|
+
/** Citeable sources, already truncated to the request's `maxResults`. */
|
|
56
|
+
readonly sources: readonly WebSearchSource[];
|
|
57
|
+
/** True when the seam dropped sources to honor `maxResults`. */
|
|
58
|
+
readonly truncated: boolean;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
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.
|
|
65
|
+
*/
|
|
66
|
+
export interface WebSearchSource {
|
|
67
|
+
readonly url: string;
|
|
68
|
+
readonly title?: string;
|
|
69
|
+
/** Short excerpt suited to display. */
|
|
70
|
+
readonly snippet?: string;
|
|
71
|
+
/** Longer excerpt suited to model reading; consumers bound its length. */
|
|
72
|
+
readonly summary?: string;
|
|
73
|
+
/** Publication/crawl timestamp as a provider-supplied ISO-8601 string. */
|
|
74
|
+
readonly publishedAt?: string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* What one fetch-capable backend is asked to retrieve. The request deliberately
|
|
78
|
+
* omits timeout, format, prompt, and extraction controls: cancellation is a
|
|
79
|
+
* direct execution argument, while presentation and higher-level LLM concerns
|
|
80
|
+
* belong outside safe retrieval.
|
|
81
|
+
*/
|
|
82
|
+
export interface WebFetchRequest {
|
|
83
|
+
readonly url: string;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Normalized fetch outcome. A successful network fetch of a non-2xx response is
|
|
87
|
+
* a result, not an error: the status code is part of the fetched resource
|
|
88
|
+
* state. {@link WebError} is reserved for failures to safely retrieve or
|
|
89
|
+
* represent the resource.
|
|
90
|
+
*/
|
|
91
|
+
export interface WebFetchResult {
|
|
92
|
+
/** The final URL after allowed redirects (the request URL is in the request). */
|
|
93
|
+
readonly url: string;
|
|
94
|
+
/** HTTP status code of the fetched response. */
|
|
95
|
+
readonly statusCode: number;
|
|
96
|
+
/** Decoded body, classified by content kind. */
|
|
97
|
+
readonly body: WebFetchBody;
|
|
98
|
+
/** True when the provider capped the decoded body. */
|
|
99
|
+
readonly truncated: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The decoded body of a fetched resource. A CLOSED discriminated union owned by
|
|
103
|
+
* `rabi-web`: the provider decodes the kind and `rabi-tool-web` renders it, so a
|
|
104
|
+
* new kind is a coordinated change across known packages, not a plugin
|
|
105
|
+
* extension. Consumers `switch` on `kind` ending in `default: assertNever(...)`
|
|
106
|
+
* so adding a kind breaks compilation at every consumer until handled. Each arm
|
|
107
|
+
* stays its own object literal even where fields coincide, so an arm can gain
|
|
108
|
+
* fields the others lack.
|
|
109
|
+
*/
|
|
110
|
+
export type WebFetchBody = {
|
|
111
|
+
readonly kind: 'html';
|
|
112
|
+
readonly content: string;
|
|
113
|
+
} | {
|
|
114
|
+
readonly kind: 'text';
|
|
115
|
+
readonly content: string;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* A search-capable backend. Registered with `ctx.web.registerSearchProvider`.
|
|
119
|
+
* `id` is a stable string, unique within the search capability kind.
|
|
120
|
+
*/
|
|
121
|
+
export interface WebSearchProvider {
|
|
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;
|
|
131
|
+
/** Cheap local usability check; must not make network calls. */
|
|
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>;
|
|
141
|
+
/** Run one search; honor `signal` for cancellation. */
|
|
142
|
+
search(request: WebSearchRequest, signal?: AbortSignal): Promise<WebSearchResult>;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* A fetch-capable backend. Registered with `ctx.web.registerFetchProvider`.
|
|
146
|
+
* `id` is a stable string, unique within the fetch capability kind.
|
|
147
|
+
*/
|
|
148
|
+
export interface WebFetchProvider {
|
|
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;
|
|
158
|
+
/** Cheap local usability check; must not make network calls. */
|
|
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>;
|
|
168
|
+
/** Retrieve one URL; honor `signal` for cancellation. */
|
|
169
|
+
fetch(request: WebFetchRequest, signal?: AbortSignal): Promise<WebFetchResult>;
|
|
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
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Typed web error with a machine-routable, open-string `code` and chained `cause`.
|
|
193
|
+
* Consumers must tolerate provider-specific codes. Shared codes cover unavailable,
|
|
194
|
+
* missing, unusable, ambiguous, or duplicate providers, cancellation, and provider failure;
|
|
195
|
+
* the local fetch provider additionally distinguishes invalid or blocked URLs, redirects,
|
|
196
|
+
* size and timeout limits, and unsupported content types. Tool execution exposes the code in
|
|
197
|
+
* structured error metadata.
|
|
198
|
+
*/
|
|
199
|
+
export declare class WebError extends HarnessError {
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -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
|
+
}
|