@123toto/ai-app-assistant-server 0.1.0
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 +88 -0
- package/dist/ai-sdk-QImICd56.d.cts +188 -0
- package/dist/ai-sdk-QImICd56.d.ts +188 -0
- package/dist/ai-sdk.cjs +564 -0
- package/dist/ai-sdk.cjs.map +1 -0
- package/dist/ai-sdk.d.cts +3 -0
- package/dist/ai-sdk.d.ts +3 -0
- package/dist/ai-sdk.js +17 -0
- package/dist/ai-sdk.js.map +1 -0
- package/dist/chunk-NIF6AW6I.js +537 -0
- package/dist/chunk-NIF6AW6I.js.map +1 -0
- package/dist/chunk-OA7OXUK7.js +136 -0
- package/dist/chunk-OA7OXUK7.js.map +1 -0
- package/dist/express.cjs +137 -0
- package/dist/express.cjs.map +1 -0
- package/dist/express.d.cts +49 -0
- package/dist/express.d.ts +49 -0
- package/dist/express.js +100 -0
- package/dist/express.js.map +1 -0
- package/dist/index.cjs +3063 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +141 -0
- package/dist/index.d.ts +141 -0
- package/dist/index.js +2372 -0
- package/dist/index.js.map +1 -0
- package/dist/managed-server-7iurKxF1.d.cts +533 -0
- package/dist/managed-server-CrZumvVU.d.ts +533 -0
- package/dist/nest.cjs +141 -0
- package/dist/nest.cjs.map +1 -0
- package/dist/nest.d.cts +47 -0
- package/dist/nest.d.ts +47 -0
- package/dist/nest.js +122 -0
- package/dist/nest.js.map +1 -0
- package/package.json +110 -0
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { a as DocsAssistantOptions, d as AiSdkConnectionTestResult, e as AiSdkFailureCode, D as DocumentationSource, A as AnswerGenerator } from './ai-sdk-QImICd56.cjs';
|
|
2
|
+
import { AskDocumentationResponse, AskDocumentationRequest, AiDocsConnectionTestInput, AiDocsConnectionResult, AiDocsCredentials, AiDocsConfigurationInput, AiDocsManagedConfigurationView, TokenUsage } from '@123toto/ai-app-assistant-contracts';
|
|
3
|
+
|
|
4
|
+
type DocsAssistantStreamEvent = {
|
|
5
|
+
type: "status";
|
|
6
|
+
phase: "preparing" | "generating";
|
|
7
|
+
} | {
|
|
8
|
+
type: "partial";
|
|
9
|
+
text: string;
|
|
10
|
+
} | {
|
|
11
|
+
type: "retry";
|
|
12
|
+
attempt: number;
|
|
13
|
+
maxRetries: number;
|
|
14
|
+
delayMs: number;
|
|
15
|
+
} | {
|
|
16
|
+
type: "complete";
|
|
17
|
+
response: AskDocumentationResponse;
|
|
18
|
+
};
|
|
19
|
+
/** Stateful facade whose static documentation is prepared only once. */
|
|
20
|
+
interface DocsAssistant {
|
|
21
|
+
answer(request: AskDocumentationRequest, options?: {
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}): Promise<AskDocumentationResponse>;
|
|
24
|
+
/** Streams provider-neutral progress and always ends with a complete event. */
|
|
25
|
+
stream(request: AskDocumentationRequest, options?: {
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
}): AsyncGenerator<DocsAssistantStreamEvent, AskDocumentationResponse>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates a provider-neutral documentation assistant.
|
|
31
|
+
*
|
|
32
|
+
* Documents are serialized, bounded and deduplicated during this call. Each
|
|
33
|
+
* subsequent question only adds the current page HTML, an optional selected
|
|
34
|
+
* element and the user's prompt. This keeps the public integration small and
|
|
35
|
+
* lets provider-side prompt caching reuse the stable document prefix.
|
|
36
|
+
*/
|
|
37
|
+
declare function createDocsAssistant(options: DocsAssistantOptions): DocsAssistant;
|
|
38
|
+
|
|
39
|
+
/** Providers available without installing an additional AI SDK package. */
|
|
40
|
+
type BuiltInProvider = "anthropic" | "google" | "mistral" | "ollama" | "openai";
|
|
41
|
+
/** Stable provider metadata suitable for a settings interface. */
|
|
42
|
+
interface AiProviderInfo {
|
|
43
|
+
id: BuiltInProvider;
|
|
44
|
+
label: string;
|
|
45
|
+
requiresApiKey: boolean;
|
|
46
|
+
supportsModelDiscovery: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** Provider-neutral model metadata returned by discovery endpoints. */
|
|
49
|
+
interface AiModelInfo {
|
|
50
|
+
id: string;
|
|
51
|
+
provider: BuiltInProvider;
|
|
52
|
+
label?: string;
|
|
53
|
+
createdAt?: string;
|
|
54
|
+
}
|
|
55
|
+
/** Credentials and transport options used only by the host backend. */
|
|
56
|
+
interface ListAiModelsOptions {
|
|
57
|
+
provider: BuiltInProvider;
|
|
58
|
+
apiKey?: string;
|
|
59
|
+
baseURL?: string;
|
|
60
|
+
signal?: AbortSignal;
|
|
61
|
+
fetch?: typeof fetch;
|
|
62
|
+
}
|
|
63
|
+
/** Returns a copy so consumers cannot mutate the library's provider registry. */
|
|
64
|
+
declare function listAiProviders(): AiProviderInfo[];
|
|
65
|
+
/**
|
|
66
|
+
* Discovers models with the provider's own API.
|
|
67
|
+
*
|
|
68
|
+
* The API key is used only for this backend request. It is never included in
|
|
69
|
+
* the returned metadata or in an error message.
|
|
70
|
+
*/
|
|
71
|
+
declare function listAiModels(options: ListAiModelsOptions): Promise<AiModelInfo[]>;
|
|
72
|
+
/** Safe discovery error that deliberately excludes provider response bodies. */
|
|
73
|
+
declare class AiModelDiscoveryError extends Error {
|
|
74
|
+
readonly provider: BuiltInProvider;
|
|
75
|
+
readonly status: number;
|
|
76
|
+
constructor(provider: BuiltInProvider, status: number);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Generic access rule; host applications map their own roles and user IDs. */
|
|
80
|
+
type AiDocsAccessRule = {
|
|
81
|
+
mode: "all";
|
|
82
|
+
} | {
|
|
83
|
+
mode: "roles";
|
|
84
|
+
roles: string[];
|
|
85
|
+
} | {
|
|
86
|
+
mode: "users";
|
|
87
|
+
userIds: string[];
|
|
88
|
+
};
|
|
89
|
+
/** Stable identity used for ownership and audit without coupling the library to a user directory. */
|
|
90
|
+
interface AiDocsConfigurationActor {
|
|
91
|
+
id: string;
|
|
92
|
+
label: string;
|
|
93
|
+
}
|
|
94
|
+
type AiDocsConfigurationAuditField = "provider" | "apiKey" | "model" | "access" | "quota" | "conversation" | "modelChangePolicy";
|
|
95
|
+
/** One safe configuration change. API key values must never be placed in from/to. */
|
|
96
|
+
interface AiDocsConfigurationAuditChange {
|
|
97
|
+
field: AiDocsConfigurationAuditField;
|
|
98
|
+
from?: string;
|
|
99
|
+
to?: string;
|
|
100
|
+
}
|
|
101
|
+
interface AiDocsConfigurationAuditEntry {
|
|
102
|
+
id: string;
|
|
103
|
+
actor: AiDocsConfigurationActor;
|
|
104
|
+
changedAt: string;
|
|
105
|
+
changes: AiDocsConfigurationAuditChange[];
|
|
106
|
+
}
|
|
107
|
+
/** Ownership and persisted audit metadata managed by the host application's authenticated backend. */
|
|
108
|
+
interface AiDocsConfigurationAdministration {
|
|
109
|
+
keyCreatedBy?: AiDocsConfigurationActor;
|
|
110
|
+
keyCreatedAt?: string;
|
|
111
|
+
modelUpdatedBy?: AiDocsConfigurationActor;
|
|
112
|
+
modelUpdatedAt?: string;
|
|
113
|
+
allowModelChangesByOthers: boolean;
|
|
114
|
+
history: AiDocsConfigurationAuditEntry[];
|
|
115
|
+
}
|
|
116
|
+
/** Provider configuration owned by the library and persisted by an adapter. */
|
|
117
|
+
interface AiDocsConfiguration {
|
|
118
|
+
provider: BuiltInProvider;
|
|
119
|
+
model: string;
|
|
120
|
+
/** Whether provider fields override deployment defaults or merely accompany stored policies. */
|
|
121
|
+
connectionSource?: "environment" | "override";
|
|
122
|
+
apiKey?: string;
|
|
123
|
+
baseURL?: string;
|
|
124
|
+
access: AiDocsAccessRule;
|
|
125
|
+
quota?: {
|
|
126
|
+
maxRequests: number;
|
|
127
|
+
windowSeconds: number;
|
|
128
|
+
};
|
|
129
|
+
/** Maximum number of user questions kept in one assistant conversation. */
|
|
130
|
+
maxConversationTurns?: number;
|
|
131
|
+
administration?: AiDocsConfigurationAdministration;
|
|
132
|
+
}
|
|
133
|
+
/** Safe representation returned to a frontend; it never contains the secret. */
|
|
134
|
+
type AiDocsConfigurationView = Omit<AiDocsConfiguration, "apiKey"> & {
|
|
135
|
+
apiKeyConfigured: boolean;
|
|
136
|
+
};
|
|
137
|
+
/** Minimal persistence contract supported by Redis, databases or secret stores. */
|
|
138
|
+
interface AiDocsKeyValueStore {
|
|
139
|
+
get(key: string): Promise<string | null | undefined>;
|
|
140
|
+
set(key: string, value: string): Promise<void>;
|
|
141
|
+
delete(key: string): Promise<void>;
|
|
142
|
+
/** Optional atomic compare-and-set used to prevent lost concurrent updates. */
|
|
143
|
+
compareAndSet?(key: string, expected: string | null, value: string): Promise<boolean>;
|
|
144
|
+
}
|
|
145
|
+
/** Secret protection is explicit so plaintext keys can never be persisted. */
|
|
146
|
+
interface AiDocsSecretProtector {
|
|
147
|
+
protect(secret: string): Promise<string> | string;
|
|
148
|
+
unprotect(protectedSecret: string): Promise<string> | string;
|
|
149
|
+
}
|
|
150
|
+
interface AiDocsConfigurationRepository {
|
|
151
|
+
load(): Promise<AiDocsConfiguration | undefined>;
|
|
152
|
+
loadView(): Promise<AiDocsConfigurationView | undefined>;
|
|
153
|
+
save(configuration: AiDocsConfiguration): Promise<AiDocsConfigurationView>;
|
|
154
|
+
/** Atomic when the underlying key/value store supports compare-and-set. */
|
|
155
|
+
mutate?(update: (current: AiDocsConfiguration | undefined) => AiDocsConfiguration | Promise<AiDocsConfiguration>): Promise<AiDocsConfigurationView>;
|
|
156
|
+
clear(): Promise<void>;
|
|
157
|
+
}
|
|
158
|
+
interface CreateAiDocsConfigurationRepositoryOptions {
|
|
159
|
+
store: AiDocsKeyValueStore;
|
|
160
|
+
secretProtector: AiDocsSecretProtector;
|
|
161
|
+
/** Allows several applications or environments to share one storage system. */
|
|
162
|
+
key?: string;
|
|
163
|
+
}
|
|
164
|
+
declare class AiDocsConfigurationConflictError extends Error {
|
|
165
|
+
constructor();
|
|
166
|
+
}
|
|
167
|
+
/** Creates a repository that validates and encrypts configuration data. */
|
|
168
|
+
declare function createAiDocsConfigurationRepository(options: CreateAiDocsConfigurationRepositoryOptions): AiDocsConfigurationRepository;
|
|
169
|
+
/**
|
|
170
|
+
* Tests the exact model configuration and persists it only after a successful
|
|
171
|
+
* structured-output response.
|
|
172
|
+
*/
|
|
173
|
+
declare function validateAndSaveAiDocsConfiguration(repository: AiDocsConfigurationRepository, configuration: AiDocsConfiguration, options?: {
|
|
174
|
+
timeoutMs?: number;
|
|
175
|
+
}): Promise<{
|
|
176
|
+
saved: boolean;
|
|
177
|
+
connection: AiSdkConnectionTestResult;
|
|
178
|
+
configuration?: AiDocsConfigurationView;
|
|
179
|
+
}>;
|
|
180
|
+
/**
|
|
181
|
+
* AES-256-GCM protector for applications that keep configuration outside a
|
|
182
|
+
* dedicated secret manager. The key must be a random 32-byte base64 value.
|
|
183
|
+
*/
|
|
184
|
+
declare function createAes256GcmSecretProtector(base64Key: string): AiDocsSecretProtector;
|
|
185
|
+
/**
|
|
186
|
+
* Allows environment-only configurations while failing closed if a caller
|
|
187
|
+
* attempts to persist a secret without configuring encryption.
|
|
188
|
+
*/
|
|
189
|
+
declare function createDisabledSecretProtector(): AiDocsSecretProtector;
|
|
190
|
+
/** Lightweight local store useful for tests and single-process prototypes. */
|
|
191
|
+
declare function createMemoryAiDocsStore(): AiDocsKeyValueStore;
|
|
192
|
+
/** Minimal Redis shape; consumers can pass an existing ioredis-like client. */
|
|
193
|
+
interface AiDocsRedisClient {
|
|
194
|
+
get(key: string): Promise<string | null>;
|
|
195
|
+
set(key: string, value: string): Promise<unknown>;
|
|
196
|
+
del(key: string): Promise<number>;
|
|
197
|
+
eval?(script: string, numberOfKeys: number, ...args: string[]): Promise<unknown>;
|
|
198
|
+
}
|
|
199
|
+
/** Reuses the host application's Redis connection without adding a dependency. */
|
|
200
|
+
declare function createRedisAiDocsStore(client: AiDocsRedisClient, options?: {
|
|
201
|
+
prefix?: string;
|
|
202
|
+
}): AiDocsKeyValueStore;
|
|
203
|
+
|
|
204
|
+
interface AiDocsQuotaPolicy {
|
|
205
|
+
maxRequests: number;
|
|
206
|
+
windowSeconds: number;
|
|
207
|
+
}
|
|
208
|
+
interface AiDocsQuotaResult {
|
|
209
|
+
allowed: boolean;
|
|
210
|
+
remaining: number;
|
|
211
|
+
retryAfterSeconds: number;
|
|
212
|
+
resetAt: Date;
|
|
213
|
+
}
|
|
214
|
+
/** Atomic quota contract implemented by shared or local stores. */
|
|
215
|
+
interface AiDocsQuotaStore {
|
|
216
|
+
consume(subject: string, policy: AiDocsQuotaPolicy): Promise<AiDocsQuotaResult>;
|
|
217
|
+
}
|
|
218
|
+
/** Single-process implementation intended for tests and local development. */
|
|
219
|
+
declare function createMemoryAiDocsQuotaStore(): AiDocsQuotaStore;
|
|
220
|
+
/** Minimal ioredis-compatible shape needed for one atomic Lua operation. */
|
|
221
|
+
interface AiDocsRedisQuotaClient {
|
|
222
|
+
eval(script: string, numberOfKeys: number, ...args: Array<string | number>): Promise<unknown>;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Redis quota with one atomic increment/expiry operation. Subject identifiers
|
|
226
|
+
* are SHA-256 fingerprints, never readable user IDs.
|
|
227
|
+
*/
|
|
228
|
+
declare function createRedisAiDocsQuotaStore(client: AiDocsRedisQuotaClient, options?: {
|
|
229
|
+
prefix?: string;
|
|
230
|
+
}): AiDocsQuotaStore;
|
|
231
|
+
|
|
232
|
+
/** Minimal identity understood by the generic access and audit policies. */
|
|
233
|
+
interface AiDocsRuntimeIdentity extends AiDocsConfigurationActor {
|
|
234
|
+
roles?: readonly string[];
|
|
235
|
+
}
|
|
236
|
+
interface AiDocsConfigurationChangeEvent {
|
|
237
|
+
reason: "saved" | "revoked" | "connection-tested" | "remote-change";
|
|
238
|
+
reloadRequired: boolean;
|
|
239
|
+
/** A successful provider call already validated the current configuration. */
|
|
240
|
+
connectionValidated: boolean;
|
|
241
|
+
remote: boolean;
|
|
242
|
+
}
|
|
243
|
+
interface AiDocsConfigurationSynchronizer {
|
|
244
|
+
start(onChange: (event: AiDocsConfigurationChangeEvent) => Promise<void> | void): Promise<() => void> | (() => void);
|
|
245
|
+
publish(event: AiDocsConfigurationChangeEvent): Promise<void>;
|
|
246
|
+
}
|
|
247
|
+
interface AiDocsConfigurationManagerOptions {
|
|
248
|
+
repository: AiDocsConfigurationRepository;
|
|
249
|
+
/** Defaults to the local in-memory quota implementation. */
|
|
250
|
+
quotaStore?: AiDocsQuotaStore;
|
|
251
|
+
/** Environment or deployment defaults. They are never persisted automatically. */
|
|
252
|
+
defaultConfiguration?: AiDocsConfiguration | (() => AiDocsConfiguration | undefined);
|
|
253
|
+
/** Resolves a secret supplied by the host environment or secret manager. */
|
|
254
|
+
resolveDefaultApiKey?: (provider: AiDocsConfiguration["provider"]) => string | undefined;
|
|
255
|
+
apiKeyStorageAvailable?: boolean;
|
|
256
|
+
defaultQuota?: AiDocsQuotaPolicy;
|
|
257
|
+
connectionTimeoutMs?: number;
|
|
258
|
+
/** Minimum delay before retrying a disconnected provider on demand. */
|
|
259
|
+
reconnectIntervalMs?: number;
|
|
260
|
+
synchronizer?: AiDocsConfigurationSynchronizer;
|
|
261
|
+
testConnection?: (input: AiDocsConnectionTestInput) => Promise<AiDocsConnectionResult>;
|
|
262
|
+
listModels?: (input: AiDocsCredentials) => Promise<AiModelInfo[]>;
|
|
263
|
+
now?: () => Date;
|
|
264
|
+
createId?: () => string;
|
|
265
|
+
logger?: Pick<Console, "info" | "warn">;
|
|
266
|
+
}
|
|
267
|
+
interface AiDocsConfigurationSaveResult {
|
|
268
|
+
saved: boolean;
|
|
269
|
+
connection: AiDocsConnectionResult;
|
|
270
|
+
configuration?: AiDocsManagedConfigurationView;
|
|
271
|
+
reloadRequired: boolean;
|
|
272
|
+
}
|
|
273
|
+
type AiDocsManagementErrorCode = "unauthorized" | "forbidden" | "conflict" | "invalid_request" | "not_configured" | "quota_reached" | "secret_storage_unavailable";
|
|
274
|
+
/** Framework-neutral policy error that HTTP adapters can map safely. */
|
|
275
|
+
declare class AiDocsManagementError extends Error {
|
|
276
|
+
readonly status: number;
|
|
277
|
+
readonly code: AiDocsManagementErrorCode;
|
|
278
|
+
readonly details?: Record<string, unknown> | undefined;
|
|
279
|
+
constructor(status: number, code: AiDocsManagementErrorCode, message: string, details?: Record<string, unknown> | undefined);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Owns provider configuration, access, quota, audit and runtime connection
|
|
283
|
+
* state. Hosts only supply identity mapping, storage and optional defaults.
|
|
284
|
+
*/
|
|
285
|
+
declare class AiDocsConfigurationManager {
|
|
286
|
+
#private;
|
|
287
|
+
constructor(options: AiDocsConfigurationManagerOptions);
|
|
288
|
+
/** Returns the built-in provider catalogue; no credentials are exposed. */
|
|
289
|
+
listProviders(): AiProviderInfo[];
|
|
290
|
+
/** Discovers models with an explicit key or the currently configured secret. */
|
|
291
|
+
listModels(input: AiDocsCredentials): Promise<AiModelInfo[]>;
|
|
292
|
+
/** Notifies the runtime when a provider-affecting setting changes. */
|
|
293
|
+
subscribe(listener: (event: AiDocsConfigurationChangeEvent) => Promise<void> | void): () => void;
|
|
294
|
+
/** Starts optional cross-instance invalidation. Calling it repeatedly is safe. */
|
|
295
|
+
startSynchronization(): Promise<void>;
|
|
296
|
+
dispose(): void;
|
|
297
|
+
/** Tests credentials and briefly caches a successful result for the next save. */
|
|
298
|
+
testConnection(input: AiDocsConnectionTestInput): Promise<AiDocsConnectionResult>;
|
|
299
|
+
/** Checks the effective stored/deployment connection used by live questions. */
|
|
300
|
+
validateRuntimeConnection(): Promise<boolean>;
|
|
301
|
+
/** Validates sensitive connection changes, persists safely and records their author. */
|
|
302
|
+
save(rawInput: AiDocsConfigurationInput, actor: AiDocsRuntimeIdentity): Promise<AiDocsConfigurationSaveResult>;
|
|
303
|
+
/** Removes only the manual key, records the revocation and falls back to defaults. */
|
|
304
|
+
revokeApiKey(actor: AiDocsRuntimeIdentity): Promise<AiDocsManagedConfigurationView>;
|
|
305
|
+
/** Resolves persisted policy against deployment defaults, including the secret. */
|
|
306
|
+
getRuntimeConfiguration(): Promise<AiDocsConfiguration | undefined>;
|
|
307
|
+
/** Returns the frontend-safe view: secret presence and permissions, never the key. */
|
|
308
|
+
getView(identity?: AiDocsRuntimeIdentity): Promise<AiDocsManagedConfigurationView>;
|
|
309
|
+
/** Minimal launcher state used by clients before rendering the assistant. */
|
|
310
|
+
getAccess(identity: AiDocsRuntimeIdentity): Promise<{
|
|
311
|
+
available: boolean;
|
|
312
|
+
maxConversationTurns: number;
|
|
313
|
+
}>;
|
|
314
|
+
/** Combines configuration, provider health and application access rules. */
|
|
315
|
+
canUse(identity: AiDocsRuntimeIdentity): Promise<boolean>;
|
|
316
|
+
/** Atomically consumes one request from the user's active quota window. */
|
|
317
|
+
consumeQuota(identity: AiDocsRuntimeIdentity): Promise<AiDocsQuotaResult>;
|
|
318
|
+
/** Enforces access and quota immediately before any model call. */
|
|
319
|
+
assertCanAsk(identity: AiDocsRuntimeIdentity): Promise<void>;
|
|
320
|
+
/** Retries a failed provider lazily, with a shared backoff across requests. */
|
|
321
|
+
ensureRuntimeConnection(): Promise<boolean>;
|
|
322
|
+
private applyTestResultToActiveConfiguration;
|
|
323
|
+
private resolveApiKey;
|
|
324
|
+
private resolveDefaultApiKey;
|
|
325
|
+
private defaultConfiguration;
|
|
326
|
+
/** Resolves stored policy-only data against the current deployment connection. */
|
|
327
|
+
private effectiveConfiguration;
|
|
328
|
+
private validateConnectionForSave;
|
|
329
|
+
private connectionSignature;
|
|
330
|
+
private lastKnownConnection;
|
|
331
|
+
private now;
|
|
332
|
+
private publishAndEmit;
|
|
333
|
+
private emit;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Portable cross-instance invalidation using any key/value store. It avoids a
|
|
337
|
+
* Redis-specific dependency and gives every process an eventual reload.
|
|
338
|
+
*/
|
|
339
|
+
declare function createPollingAiDocsConfigurationSynchronizer(store: AiDocsKeyValueStore, options?: {
|
|
340
|
+
key?: string;
|
|
341
|
+
intervalMs?: number;
|
|
342
|
+
}): AiDocsConfigurationSynchronizer;
|
|
343
|
+
|
|
344
|
+
type AiDocsGenerationOperation = "answer" | "stream";
|
|
345
|
+
interface AiDocsGenerationEventBase {
|
|
346
|
+
requestId: string;
|
|
347
|
+
operation: AiDocsGenerationOperation;
|
|
348
|
+
model: string;
|
|
349
|
+
durationMs: number;
|
|
350
|
+
occurredAt: string;
|
|
351
|
+
}
|
|
352
|
+
/** Safe operational event. It deliberately excludes prompts, HTML, users and credentials. */
|
|
353
|
+
type AiDocsGenerationEvent = (AiDocsGenerationEventBase & {
|
|
354
|
+
outcome: "success";
|
|
355
|
+
usage?: TokenUsage;
|
|
356
|
+
}) | (AiDocsGenerationEventBase & {
|
|
357
|
+
outcome: "failure";
|
|
358
|
+
error: {
|
|
359
|
+
code: AiSdkFailureCode;
|
|
360
|
+
message: string;
|
|
361
|
+
retryable: boolean;
|
|
362
|
+
attempts: number;
|
|
363
|
+
providerStatus?: number;
|
|
364
|
+
};
|
|
365
|
+
});
|
|
366
|
+
interface AiDocsTelemetrySummary {
|
|
367
|
+
requests: number;
|
|
368
|
+
succeeded: number;
|
|
369
|
+
failed: number;
|
|
370
|
+
durationMs: number;
|
|
371
|
+
inputTokens: number;
|
|
372
|
+
outputTokens: number;
|
|
373
|
+
totalTokens: number;
|
|
374
|
+
failuresByCode: Partial<Record<AiSdkFailureCode, number>>;
|
|
375
|
+
}
|
|
376
|
+
type AiDocsRecentFailure = Extract<AiDocsGenerationEvent, {
|
|
377
|
+
outcome: "failure";
|
|
378
|
+
}>;
|
|
379
|
+
/** Persistence contract used by the managed server and reusable by any host. */
|
|
380
|
+
interface AiDocsTelemetryStore {
|
|
381
|
+
record(event: AiDocsGenerationEvent): Promise<void>;
|
|
382
|
+
summary(): Promise<AiDocsTelemetrySummary>;
|
|
383
|
+
recentFailures(limit?: number): Promise<AiDocsRecentFailure[]>;
|
|
384
|
+
}
|
|
385
|
+
/** Single-process telemetry intended for tests and local development. */
|
|
386
|
+
declare function createMemoryAiDocsTelemetryStore(options?: {
|
|
387
|
+
recentFailureLimit?: number;
|
|
388
|
+
}): AiDocsTelemetryStore;
|
|
389
|
+
/** Minimal Redis shape; compatible with ioredis without adding it as a dependency. */
|
|
390
|
+
interface AiDocsRedisTelemetryClient {
|
|
391
|
+
eval(script: string, numberOfKeys: number, ...args: Array<string | number>): Promise<unknown>;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Shared Redis telemetry. One Lua call atomically updates counters and keeps a
|
|
395
|
+
* bounded failure list, so several application instances can safely share it.
|
|
396
|
+
*/
|
|
397
|
+
declare function createRedisAiDocsTelemetryStore(client: AiDocsRedisTelemetryClient, options?: {
|
|
398
|
+
prefix?: string;
|
|
399
|
+
recentFailureLimit?: number;
|
|
400
|
+
}): AiDocsTelemetryStore;
|
|
401
|
+
/** Converts any generator error to the same safe public diagnostic. */
|
|
402
|
+
declare function createAiDocsFailureEvent(input: {
|
|
403
|
+
error: unknown;
|
|
404
|
+
requestId: string;
|
|
405
|
+
operation: AiDocsGenerationOperation;
|
|
406
|
+
model: string;
|
|
407
|
+
durationMs: number;
|
|
408
|
+
}): AiDocsRecentFailure;
|
|
409
|
+
|
|
410
|
+
type AssistantPolicies = NonNullable<DocsAssistantOptions["policies"]>;
|
|
411
|
+
interface CreateManagedAiDocsRuntimeOptions<TIdentity extends AiDocsRuntimeIdentity> {
|
|
412
|
+
configuration: AiDocsConfigurationManager;
|
|
413
|
+
documents?: DocumentationSource[];
|
|
414
|
+
policies?: AssistantPolicies;
|
|
415
|
+
/** Overrides the built-in `provider:model` AI SDK generator. */
|
|
416
|
+
createGenerator?: (configuration: {
|
|
417
|
+
model: string;
|
|
418
|
+
apiKey?: string;
|
|
419
|
+
baseURL?: string;
|
|
420
|
+
}) => AnswerGenerator | Promise<AnswerGenerator>;
|
|
421
|
+
timeoutMs?: number;
|
|
422
|
+
maxRetries?: number;
|
|
423
|
+
transformRequest?: (input: AskDocumentationRequest, identity: TIdentity) => AskDocumentationRequest | Promise<AskDocumentationRequest>;
|
|
424
|
+
transformResponse?: (output: AskDocumentationResponse, identity: TIdentity) => AskDocumentationResponse | Promise<AskDocumentationResponse>;
|
|
425
|
+
transformStreamEvent?: (event: DocsAssistantStreamEvent, identity: TIdentity) => DocsAssistantStreamEvent | Promise<DocsAssistantStreamEvent>;
|
|
426
|
+
authorize?: (identity: TIdentity) => Promise<void> | void;
|
|
427
|
+
/** Receives provider/runtime failures without exposing prompts or credentials. */
|
|
428
|
+
onGenerationError?: (error: unknown, operation: "answer" | "stream", identity: TIdentity) => Promise<void> | void;
|
|
429
|
+
/** Receives one safe success/failure event per user request. */
|
|
430
|
+
onGenerationEvent?: (event: AiDocsGenerationEvent, identity: TIdentity) => Promise<void> | void;
|
|
431
|
+
/** Optional persistence used by the batteries-included managed server. */
|
|
432
|
+
telemetryStore?: AiDocsTelemetryStore;
|
|
433
|
+
}
|
|
434
|
+
interface ManagedAiDocsRuntime<TIdentity extends AiDocsRuntimeIdentity> {
|
|
435
|
+
readonly configuration: AiDocsConfigurationManager;
|
|
436
|
+
readonly telemetry?: AiDocsTelemetryStore;
|
|
437
|
+
initialize(): Promise<void>;
|
|
438
|
+
dispose(): void;
|
|
439
|
+
reload(connectionAlreadyValidated?: boolean): Promise<void>;
|
|
440
|
+
/** Replaces the stable documentation without recreating the configuration manager. */
|
|
441
|
+
setDocuments(documents: DocumentationSource[]): Promise<void>;
|
|
442
|
+
answer(input: AskDocumentationRequest, identity: TIdentity): Promise<AskDocumentationResponse>;
|
|
443
|
+
stream(input: AskDocumentationRequest, identity: TIdentity, signal?: AbortSignal): AsyncGenerator<DocsAssistantStreamEvent, AskDocumentationResponse>;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Optional batteries-included runtime. The minimal `createDocsAssistant` API
|
|
447
|
+
* remains available for applications that want to own the lifecycle.
|
|
448
|
+
*/
|
|
449
|
+
declare function createManagedAiDocsRuntime<TIdentity extends AiDocsRuntimeIdentity>(options: CreateManagedAiDocsRuntimeOptions<TIdentity>): ManagedAiDocsRuntime<TIdentity>;
|
|
450
|
+
|
|
451
|
+
interface ManagedAiDocsFetchHandlerOptions<TIdentity extends AiDocsRuntimeIdentity, TNativeContext = undefined> {
|
|
452
|
+
runtime: ManagedAiDocsRuntime<TIdentity>;
|
|
453
|
+
/** Required by default so application endpoints fail closed. */
|
|
454
|
+
resolveIdentity?: (request: Request, nativeContext: TNativeContext | undefined) => TIdentity | Promise<TIdentity>;
|
|
455
|
+
/** Explicit opt-in for public prototypes. Never enable it on authenticated applications. */
|
|
456
|
+
allowAnonymous?: boolean;
|
|
457
|
+
/** Admin endpoints fail closed when this hook is omitted. */
|
|
458
|
+
authorizeAdministration?: (identity: TIdentity, request: Request, nativeContext: TNativeContext | undefined) => Promise<void> | void;
|
|
459
|
+
/** Optional application directory exposed to the generic settings UI. */
|
|
460
|
+
listUsers?: (identity: TIdentity, nativeContext: TNativeContext | undefined) => Promise<Array<{
|
|
461
|
+
id: string;
|
|
462
|
+
label: string;
|
|
463
|
+
}>>;
|
|
464
|
+
/** Optional application roles exposed to the generic settings UI. */
|
|
465
|
+
listRoles?: (identity: TIdentity, nativeContext: TNativeContext | undefined) => Promise<Array<{
|
|
466
|
+
id: string;
|
|
467
|
+
label: string;
|
|
468
|
+
}>> | Array<{
|
|
469
|
+
id: string;
|
|
470
|
+
label: string;
|
|
471
|
+
}>;
|
|
472
|
+
maxBodyBytes?: number;
|
|
473
|
+
onError?: (error: unknown, request: Request, nativeContext: TNativeContext | undefined) => Response | Promise<Response>;
|
|
474
|
+
}
|
|
475
|
+
interface ManagedAiDocsFetchHandlers<TNativeContext = undefined> {
|
|
476
|
+
handle(request: Request, nativeContext?: TNativeContext): Promise<Response>;
|
|
477
|
+
}
|
|
478
|
+
/** Complete framework-neutral chat and administration API. */
|
|
479
|
+
declare function createManagedAiDocsFetchHandlers<TIdentity extends AiDocsRuntimeIdentity, TNativeContext = undefined>(options: ManagedAiDocsFetchHandlerOptions<TIdentity, TNativeContext>): ManagedAiDocsFetchHandlers<TNativeContext>;
|
|
480
|
+
|
|
481
|
+
type AiDocsManagedStorage = {
|
|
482
|
+
type: "memory";
|
|
483
|
+
} | {
|
|
484
|
+
type: "redis";
|
|
485
|
+
client: AiDocsRedisClient & AiDocsRedisQuotaClient;
|
|
486
|
+
/** Shared namespace. Defaults to `ai-docs:`. */
|
|
487
|
+
prefix?: string;
|
|
488
|
+
synchronizationIntervalMs?: number;
|
|
489
|
+
};
|
|
490
|
+
interface AiDocsManagedConfigurationSetup extends Omit<AiDocsConfigurationManagerOptions, "apiKeyStorageAvailable" | "quotaStore" | "repository" | "synchronizer"> {
|
|
491
|
+
/** Creates configuration, quota and synchronization adapters automatically. */
|
|
492
|
+
storage?: AiDocsManagedStorage;
|
|
493
|
+
/** AES-256-GCM key used to persist administrator-supplied API keys. */
|
|
494
|
+
encryptionKey?: string;
|
|
495
|
+
/** Alternative secret manager; takes precedence over encryptionKey. */
|
|
496
|
+
secretProtector?: AiDocsSecretProtector;
|
|
497
|
+
repositoryKey?: string;
|
|
498
|
+
quotaStore?: AiDocsQuotaStore;
|
|
499
|
+
synchronizer?: AiDocsConfigurationManagerOptions["synchronizer"];
|
|
500
|
+
apiKeyStorageAvailable?: boolean;
|
|
501
|
+
}
|
|
502
|
+
interface CreateManagedAiDocsServerOptions<TIdentity extends AiDocsRuntimeIdentity, TNativeContext = undefined> {
|
|
503
|
+
/** Pass an existing manager, or its construction options for the common case. */
|
|
504
|
+
configuration: AiDocsConfigurationManager | AiDocsConfigurationManagerOptions | AiDocsManagedConfigurationSetup;
|
|
505
|
+
/** Stable application documentation. It can also be supplied later with setDocuments(). */
|
|
506
|
+
documents?: DocumentationSource[];
|
|
507
|
+
runtime?: Omit<CreateManagedAiDocsRuntimeOptions<TIdentity>, "configuration" | "documents">;
|
|
508
|
+
http?: Omit<ManagedAiDocsFetchHandlerOptions<TIdentity, TNativeContext>, "runtime">;
|
|
509
|
+
/** Enabled by default; Redis configuration automatically makes it persistent. */
|
|
510
|
+
telemetry?: false | {
|
|
511
|
+
store?: AiDocsTelemetryStore;
|
|
512
|
+
recentFailureLimit?: number;
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
interface ManagedAiDocsServer<TIdentity extends AiDocsRuntimeIdentity, TNativeContext = undefined> {
|
|
516
|
+
readonly configuration: AiDocsConfigurationManager;
|
|
517
|
+
readonly runtime: ManagedAiDocsRuntime<TIdentity>;
|
|
518
|
+
readonly fetch: ManagedAiDocsFetchHandlers<TNativeContext>;
|
|
519
|
+
readonly telemetry?: AiDocsTelemetryStore;
|
|
520
|
+
initialize(): Promise<void>;
|
|
521
|
+
setDocuments(documents: DocumentationSource[]): Promise<void>;
|
|
522
|
+
dispose(): void;
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Creates the complete managed assistant: configuration, provider lifecycle,
|
|
526
|
+
* access, quotas, HTTP routes and late-bound documentation.
|
|
527
|
+
*
|
|
528
|
+
* Framework integrations only need to adapt their native request/response and
|
|
529
|
+
* pass the authenticated application identity through `http.resolveIdentity`.
|
|
530
|
+
*/
|
|
531
|
+
declare function createManagedAiDocsServer<TIdentity extends AiDocsRuntimeIdentity, TNativeContext = undefined>(options: CreateManagedAiDocsServerOptions<TIdentity, TNativeContext>): ManagedAiDocsServer<TIdentity, TNativeContext>;
|
|
532
|
+
|
|
533
|
+
export { createManagedAiDocsRuntime as $, type AiDocsConfiguration as A, type BuiltInProvider as B, type AiDocsRedisClient as C, type DocsAssistant as D, type AiDocsRedisQuotaClient as E, type AiDocsRedisTelemetryClient as F, type AiDocsRuntimeIdentity as G, type AiDocsSecretProtector as H, type AiDocsTelemetryStore as I, type AiDocsTelemetrySummary as J, AiModelDiscoveryError as K, type AiModelInfo as L, type ManagedAiDocsServer as M, type AiProviderInfo as N, type CreateAiDocsConfigurationRepositoryOptions as O, type CreateManagedAiDocsRuntimeOptions as P, type CreateManagedAiDocsServerOptions as Q, type ListAiModelsOptions as R, type ManagedAiDocsFetchHandlerOptions as S, type ManagedAiDocsFetchHandlers as T, type ManagedAiDocsRuntime as U, createAes256GcmSecretProtector as V, createAiDocsConfigurationRepository as W, createAiDocsFailureEvent as X, createDisabledSecretProtector as Y, createDocsAssistant as Z, createManagedAiDocsFetchHandlers as _, type DocsAssistantStreamEvent as a, createManagedAiDocsServer as a0, createMemoryAiDocsQuotaStore as a1, createMemoryAiDocsStore as a2, createMemoryAiDocsTelemetryStore as a3, createPollingAiDocsConfigurationSynchronizer as a4, createRedisAiDocsQuotaStore as a5, createRedisAiDocsStore as a6, createRedisAiDocsTelemetryStore as a7, listAiModels as a8, listAiProviders as a9, validateAndSaveAiDocsConfiguration as aa, type AiDocsAccessRule as b, type AiDocsConfigurationActor as c, type AiDocsConfigurationAdministration as d, type AiDocsConfigurationAuditChange as e, type AiDocsConfigurationAuditEntry as f, type AiDocsConfigurationAuditField as g, type AiDocsConfigurationChangeEvent as h, AiDocsConfigurationConflictError as i, AiDocsConfigurationManager as j, type AiDocsConfigurationManagerOptions as k, type AiDocsConfigurationRepository as l, type AiDocsConfigurationSaveResult as m, type AiDocsConfigurationSynchronizer as n, type AiDocsConfigurationView as o, type AiDocsGenerationEvent as p, type AiDocsGenerationOperation as q, type AiDocsKeyValueStore as r, type AiDocsManagedConfigurationSetup as s, type AiDocsManagedStorage as t, AiDocsManagementError as u, type AiDocsManagementErrorCode as v, type AiDocsQuotaPolicy as w, type AiDocsQuotaResult as x, type AiDocsQuotaStore as y, type AiDocsRecentFailure as z };
|