@123toto/ai-app-assistant-server 0.1.1 → 0.2.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/README.md +14 -14
- package/dist/{ai-sdk-QImICd56.d.cts → ai-sdk-qU3KgNu-.d.cts} +2 -2
- package/dist/{ai-sdk-QImICd56.d.ts → ai-sdk-qU3KgNu-.d.ts} +2 -2
- package/dist/ai-sdk.d.cts +1 -1
- package/dist/ai-sdk.d.ts +1 -1
- package/dist/{chunk-OA7OXUK7.js → chunk-Z4IVCZ5M.js} +15 -15
- package/dist/chunk-Z4IVCZ5M.js.map +1 -0
- package/dist/express.cjs +8 -8
- package/dist/express.cjs.map +1 -1
- package/dist/express.d.cts +11 -11
- package/dist/express.d.ts +11 -11
- package/dist/express.js +7 -7
- package/dist/express.js.map +1 -1
- package/dist/index.cjs +167 -167
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -33
- package/dist/index.d.ts +33 -33
- package/dist/index.js +142 -142
- package/dist/index.js.map +1 -1
- package/dist/{managed-server-7iurKxF1.d.cts → managed-server-BGQ-1jkx.d.ts} +148 -148
- package/dist/{managed-server-CrZumvVU.d.ts → managed-server-CthTKEQ6.d.cts} +148 -148
- package/dist/nest.cjs +31 -31
- package/dist/nest.cjs.map +1 -1
- package/dist/nest.d.cts +12 -12
- package/dist/nest.d.ts +12 -12
- package/dist/nest.js +28 -28
- package/dist/nest.js.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-OA7OXUK7.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -6,14 +6,14 @@ import {
|
|
|
6
6
|
testAiSdkConnection
|
|
7
7
|
} from "./chunk-NIF6AW6I.js";
|
|
8
8
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "./chunk-
|
|
9
|
+
AiAppAssistantRequestError,
|
|
10
|
+
createAiAppAssistantFetchHandlers
|
|
11
|
+
} from "./chunk-Z4IVCZ5M.js";
|
|
12
12
|
|
|
13
13
|
// src/assistant.ts
|
|
14
14
|
import {
|
|
15
15
|
PROTOCOL_VERSION,
|
|
16
|
-
|
|
16
|
+
aiAppAssistantRequestSchema,
|
|
17
17
|
generatedAnswerSchema
|
|
18
18
|
} from "@123toto/ai-app-assistant-contracts";
|
|
19
19
|
|
|
@@ -81,7 +81,7 @@ function round(value) {
|
|
|
81
81
|
var FALLBACK_CONTEXT_WINDOW_TOKENS = 128e3;
|
|
82
82
|
var FALLBACK_OUTPUT_TOKENS = 8e3;
|
|
83
83
|
var FALLBACK_CHARACTERS_PER_TOKEN = 2;
|
|
84
|
-
function
|
|
84
|
+
function createAiAppAssistant(options) {
|
|
85
85
|
const minimumEvidence = clampInteger(
|
|
86
86
|
options.policies?.minimumEvidence ?? 1,
|
|
87
87
|
1,
|
|
@@ -89,7 +89,7 @@ function createDocsAssistant(options) {
|
|
|
89
89
|
);
|
|
90
90
|
const documents = prepareDocuments(options.documents ?? []);
|
|
91
91
|
const prepare = (request) => {
|
|
92
|
-
const validated =
|
|
92
|
+
const validated = aiAppAssistantRequestSchema.parse(request);
|
|
93
93
|
return { validated, bundle: prepareBundle(validated, documents, options) };
|
|
94
94
|
};
|
|
95
95
|
const finalize = (validated, bundle, generatedInput, startedAt) => {
|
|
@@ -233,7 +233,7 @@ function prepareBundle(request, documents, options) {
|
|
|
233
233
|
content: boundEvidence(
|
|
234
234
|
request.selectedElementHtml,
|
|
235
235
|
selectedLimit,
|
|
236
|
-
"
|
|
236
|
+
"AI_APP_ASSISTANT_SELECTED_ELEMENT_TRUNCATED"
|
|
237
237
|
),
|
|
238
238
|
relevance: 1
|
|
239
239
|
});
|
|
@@ -248,7 +248,7 @@ function prepareBundle(request, documents, options) {
|
|
|
248
248
|
for (const document of documents) {
|
|
249
249
|
if (remainingDocuments <= 0) break;
|
|
250
250
|
const limit = Math.min(documentLimit, remainingDocuments);
|
|
251
|
-
const content = boundEvidence(document.content, limit, "
|
|
251
|
+
const content = boundEvidence(document.content, limit, "AI_APP_ASSISTANT_DOCUMENT_TRUNCATED");
|
|
252
252
|
remainingDocuments -= content.length;
|
|
253
253
|
items.push({
|
|
254
254
|
source: "document",
|
|
@@ -275,7 +275,7 @@ function serializeDocumentationContent(content) {
|
|
|
275
275
|
function boundHtml(content, maxChars, alreadyTruncated) {
|
|
276
276
|
const limit = optionalBound(maxChars, 1e3, 8e6);
|
|
277
277
|
return alreadyTruncated ? `${content.slice(0, limit)}
|
|
278
|
-
<!--
|
|
278
|
+
<!-- AI_APP_ASSISTANT_HTML_TRUNCATED -->` : boundEvidence(content, limit, "AI_APP_ASSISTANT_HTML_TRUNCATED");
|
|
279
279
|
}
|
|
280
280
|
function boundEvidence(content, limit, marker) {
|
|
281
281
|
if (content.length <= limit) return content;
|
|
@@ -319,15 +319,15 @@ function insufficientResponse(requestId, model, durationMs) {
|
|
|
319
319
|
}
|
|
320
320
|
|
|
321
321
|
// src/node-http.ts
|
|
322
|
-
function
|
|
322
|
+
function createAiAppAssistantNodeHttpListener(handlers, options = {}) {
|
|
323
323
|
return async (request, response) => {
|
|
324
324
|
try {
|
|
325
325
|
const webRequest = await toRequest(request, options);
|
|
326
326
|
const handle = handlers.handle;
|
|
327
327
|
await writeResponse(response, await handle(webRequest, request));
|
|
328
328
|
} catch (error) {
|
|
329
|
-
const status = error instanceof
|
|
330
|
-
const code = error instanceof
|
|
329
|
+
const status = error instanceof AiAppAssistantRequestError ? error.status : 500;
|
|
330
|
+
const code = error instanceof AiAppAssistantRequestError ? error.code : "assistant_error";
|
|
331
331
|
response.statusCode = status;
|
|
332
332
|
response.setHeader("content-type", "application/json; charset=utf-8");
|
|
333
333
|
response.end(JSON.stringify({ error: code }));
|
|
@@ -357,7 +357,7 @@ async function readBody(request, maxBodyBytes) {
|
|
|
357
357
|
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : new Uint8Array(chunk);
|
|
358
358
|
size += bytes.byteLength;
|
|
359
359
|
if (size > maxBodyBytes) {
|
|
360
|
-
throw new
|
|
360
|
+
throw new AiAppAssistantRequestError(413, "request_too_large", "Request body is too large");
|
|
361
361
|
}
|
|
362
362
|
chunks.push(bytes);
|
|
363
363
|
}
|
|
@@ -384,22 +384,22 @@ async function writeResponse(response, webResponse) {
|
|
|
384
384
|
}
|
|
385
385
|
|
|
386
386
|
// src/server.ts
|
|
387
|
-
function
|
|
387
|
+
function createAiAppAssistantServer(options) {
|
|
388
388
|
const generator = options.generator ?? createGenerator(options);
|
|
389
|
-
const assistant =
|
|
389
|
+
const assistant = createAiAppAssistant({
|
|
390
390
|
generator,
|
|
391
391
|
...options.documents ? { documents: options.documents } : {},
|
|
392
392
|
...options.policies ? { policies: options.policies } : {}
|
|
393
393
|
});
|
|
394
394
|
return {
|
|
395
395
|
assistant,
|
|
396
|
-
fetch:
|
|
396
|
+
fetch: createAiAppAssistantFetchHandlers({ assistant, ...options.http }),
|
|
397
397
|
options: Object.freeze({ ...options })
|
|
398
398
|
};
|
|
399
399
|
}
|
|
400
400
|
function createGenerator(options) {
|
|
401
401
|
if (!options.model?.trim()) {
|
|
402
|
-
throw new TypeError("
|
|
402
|
+
throw new TypeError("createAiAppAssistantServer requires either generator or model");
|
|
403
403
|
}
|
|
404
404
|
const generatorOptions = {
|
|
405
405
|
model: options.model,
|
|
@@ -417,10 +417,10 @@ import { createHash as createHash2, randomUUID } from "crypto";
|
|
|
417
417
|
// src/configuration.ts
|
|
418
418
|
import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
|
|
419
419
|
import { z } from "zod";
|
|
420
|
-
var
|
|
420
|
+
var AiAppAssistantConfigurationConflictError = class extends Error {
|
|
421
421
|
constructor() {
|
|
422
|
-
super("AI
|
|
423
|
-
this.name = "
|
|
422
|
+
super("AI App Assistant configuration changed concurrently; retry the operation");
|
|
423
|
+
this.name = "AiAppAssistantConfigurationConflictError";
|
|
424
424
|
}
|
|
425
425
|
};
|
|
426
426
|
var accessRuleSchema = z.discriminatedUnion("mode", [
|
|
@@ -464,8 +464,8 @@ var persistedConfigurationSchema = z.object({
|
|
|
464
464
|
maxConversationTurns: z.number().int().min(1).max(10).optional(),
|
|
465
465
|
administration: administrationSchema.optional()
|
|
466
466
|
});
|
|
467
|
-
function
|
|
468
|
-
const key = options.key?.trim() || "ai-
|
|
467
|
+
function createAiAppAssistantConfigurationRepository(options) {
|
|
468
|
+
const key = options.key?.trim() || "ai-app-assistant:configuration";
|
|
469
469
|
const deserialize = async (serialized) => {
|
|
470
470
|
if (!serialized) return void 0;
|
|
471
471
|
const stored = persistedConfigurationSchema.parse(JSON.parse(serialized));
|
|
@@ -523,14 +523,14 @@ function createAiDocsConfigurationRepository(options) {
|
|
|
523
523
|
return toConfigurationView(normalized);
|
|
524
524
|
}
|
|
525
525
|
}
|
|
526
|
-
throw new
|
|
526
|
+
throw new AiAppAssistantConfigurationConflictError();
|
|
527
527
|
},
|
|
528
528
|
async clear() {
|
|
529
529
|
await options.store.delete(key);
|
|
530
530
|
}
|
|
531
531
|
};
|
|
532
532
|
}
|
|
533
|
-
async function
|
|
533
|
+
async function validateAndSaveAiAppAssistantConfiguration(repository, configuration, options) {
|
|
534
534
|
const normalized = normalizeConfiguration(configuration);
|
|
535
535
|
const connection = await testAiSdkConnection({
|
|
536
536
|
model: `${normalized.provider}:${normalized.model}`,
|
|
@@ -550,7 +550,7 @@ function createAes256GcmSecretProtector(base64Key) {
|
|
|
550
550
|
if (key.length !== 32) {
|
|
551
551
|
throw new TypeError("The secret protection key must contain exactly 32 base64-encoded bytes");
|
|
552
552
|
}
|
|
553
|
-
const additionalData = Buffer.from("ai-
|
|
553
|
+
const additionalData = Buffer.from("ai-app-assistant-configuration:v1", "utf8");
|
|
554
554
|
return {
|
|
555
555
|
protect(secret) {
|
|
556
556
|
const iv = randomBytes(12);
|
|
@@ -580,7 +580,7 @@ function createDisabledSecretProtector() {
|
|
|
580
580
|
};
|
|
581
581
|
return { protect: unavailable2, unprotect: unavailable2 };
|
|
582
582
|
}
|
|
583
|
-
function
|
|
583
|
+
function createMemoryAiAppAssistantStore() {
|
|
584
584
|
const values = /* @__PURE__ */ new Map();
|
|
585
585
|
return {
|
|
586
586
|
async get(key) {
|
|
@@ -600,8 +600,8 @@ function createMemoryAiDocsStore() {
|
|
|
600
600
|
}
|
|
601
601
|
};
|
|
602
602
|
}
|
|
603
|
-
function
|
|
604
|
-
const prefix = options?.prefix ?? "ai-
|
|
603
|
+
function createRedisAiAppAssistantStore(client, options) {
|
|
604
|
+
const prefix = options?.prefix ?? "ai-app-assistant:";
|
|
605
605
|
const namespaced = (key) => `${prefix}${key}`;
|
|
606
606
|
return {
|
|
607
607
|
get: (key) => client.get(namespaced(key)),
|
|
@@ -679,7 +679,7 @@ function toConfigurationView(configuration) {
|
|
|
679
679
|
|
|
680
680
|
// src/quota.ts
|
|
681
681
|
import { createHash } from "crypto";
|
|
682
|
-
function
|
|
682
|
+
function createMemoryAiAppAssistantQuotaStore() {
|
|
683
683
|
const counters = /* @__PURE__ */ new Map();
|
|
684
684
|
return {
|
|
685
685
|
async consume(subject, policy) {
|
|
@@ -696,8 +696,8 @@ function createMemoryAiDocsQuotaStore() {
|
|
|
696
696
|
}
|
|
697
697
|
};
|
|
698
698
|
}
|
|
699
|
-
function
|
|
700
|
-
const prefix = options?.prefix ?? "ai-
|
|
699
|
+
function createRedisAiAppAssistantQuotaStore(client, options) {
|
|
700
|
+
const prefix = options?.prefix ?? "ai-app-assistant:quota:";
|
|
701
701
|
return {
|
|
702
702
|
async consume(subject, policy) {
|
|
703
703
|
const normalized = normalizePolicy(policy);
|
|
@@ -868,19 +868,19 @@ function isRecord(value) {
|
|
|
868
868
|
}
|
|
869
869
|
|
|
870
870
|
// src/management.ts
|
|
871
|
-
var
|
|
871
|
+
var AiAppAssistantManagementError = class extends Error {
|
|
872
872
|
constructor(status, code, message, details) {
|
|
873
873
|
super(message);
|
|
874
874
|
this.status = status;
|
|
875
875
|
this.code = code;
|
|
876
876
|
this.details = details;
|
|
877
|
-
this.name = "
|
|
877
|
+
this.name = "AiAppAssistantManagementError";
|
|
878
878
|
}
|
|
879
879
|
status;
|
|
880
880
|
code;
|
|
881
881
|
details;
|
|
882
882
|
};
|
|
883
|
-
var
|
|
883
|
+
var AiAppAssistantConfigurationManager = class {
|
|
884
884
|
#repository;
|
|
885
885
|
#quotaStore;
|
|
886
886
|
#options;
|
|
@@ -894,7 +894,7 @@ var AiDocsConfigurationManager = class {
|
|
|
894
894
|
constructor(options) {
|
|
895
895
|
this.#options = options;
|
|
896
896
|
this.#repository = options.repository;
|
|
897
|
-
this.#quotaStore = options.quotaStore ??
|
|
897
|
+
this.#quotaStore = options.quotaStore ?? createMemoryAiAppAssistantQuotaStore();
|
|
898
898
|
}
|
|
899
899
|
/** Returns the built-in provider catalogue; no credentials are exposed. */
|
|
900
900
|
listProviders() {
|
|
@@ -1017,7 +1017,7 @@ var AiDocsConfigurationManager = class {
|
|
|
1017
1017
|
async save(rawInput, actor) {
|
|
1018
1018
|
const input = normalizeInput(rawInput);
|
|
1019
1019
|
if (input.apiKey && !this.#options.apiKeyStorageAvailable) {
|
|
1020
|
-
throw new
|
|
1020
|
+
throw new AiAppAssistantManagementError(
|
|
1021
1021
|
503,
|
|
1022
1022
|
"secret_storage_unavailable",
|
|
1023
1023
|
"Secure API key storage is not configured"
|
|
@@ -1091,8 +1091,8 @@ var AiDocsConfigurationManager = class {
|
|
|
1091
1091
|
if (error instanceof ConnectionRejectedError) {
|
|
1092
1092
|
return { saved: false, connection: error.connection, reloadRequired: false };
|
|
1093
1093
|
}
|
|
1094
|
-
if (error instanceof
|
|
1095
|
-
throw new
|
|
1094
|
+
if (error instanceof AiAppAssistantConfigurationConflictError) {
|
|
1095
|
+
throw new AiAppAssistantManagementError(409, "conflict", error.message);
|
|
1096
1096
|
}
|
|
1097
1097
|
throw error;
|
|
1098
1098
|
}
|
|
@@ -1123,7 +1123,7 @@ var AiDocsConfigurationManager = class {
|
|
|
1123
1123
|
try {
|
|
1124
1124
|
await persist((previous) => {
|
|
1125
1125
|
if (!previous?.apiKey) {
|
|
1126
|
-
throw new
|
|
1126
|
+
throw new AiAppAssistantManagementError(400, "not_configured", "No manually configured API key is available to revoke");
|
|
1127
1127
|
}
|
|
1128
1128
|
const owner = previous.administration?.keyCreatedBy;
|
|
1129
1129
|
if (owner && owner.id !== actor.id) {
|
|
@@ -1157,8 +1157,8 @@ var AiDocsConfigurationManager = class {
|
|
|
1157
1157
|
};
|
|
1158
1158
|
});
|
|
1159
1159
|
} catch (error) {
|
|
1160
|
-
if (error instanceof
|
|
1161
|
-
throw new
|
|
1160
|
+
if (error instanceof AiAppAssistantConfigurationConflictError) {
|
|
1161
|
+
throw new AiAppAssistantManagementError(409, "conflict", error.message);
|
|
1162
1162
|
}
|
|
1163
1163
|
throw error;
|
|
1164
1164
|
}
|
|
@@ -1278,7 +1278,7 @@ var AiDocsConfigurationManager = class {
|
|
|
1278
1278
|
}
|
|
1279
1279
|
const quota = await this.consumeQuota(identity);
|
|
1280
1280
|
if (!quota.allowed) {
|
|
1281
|
-
throw new
|
|
1281
|
+
throw new AiAppAssistantManagementError(
|
|
1282
1282
|
429,
|
|
1283
1283
|
"quota_reached",
|
|
1284
1284
|
"AI assistant quota reached",
|
|
@@ -1403,8 +1403,8 @@ var AiDocsConfigurationManager = class {
|
|
|
1403
1403
|
await Promise.all([...this.#listeners].map((listener) => listener(event)));
|
|
1404
1404
|
}
|
|
1405
1405
|
};
|
|
1406
|
-
function
|
|
1407
|
-
const key = options.key?.trim() || "ai-
|
|
1406
|
+
function createPollingAiAppAssistantConfigurationSynchronizer(store, options = {}) {
|
|
1407
|
+
const key = options.key?.trim() || "ai-app-assistant:configuration-revision";
|
|
1408
1408
|
const intervalMs = Math.max(250, Math.round(options.intervalMs ?? 2e3));
|
|
1409
1409
|
let current;
|
|
1410
1410
|
return {
|
|
@@ -1516,11 +1516,11 @@ function configurationChanges(previous, input, allowModelChangesByOthers) {
|
|
|
1516
1516
|
return changes;
|
|
1517
1517
|
}
|
|
1518
1518
|
function forbidden(message) {
|
|
1519
|
-
return new
|
|
1519
|
+
return new AiAppAssistantManagementError(403, "forbidden", message);
|
|
1520
1520
|
}
|
|
1521
1521
|
|
|
1522
1522
|
// src/telemetry.ts
|
|
1523
|
-
function
|
|
1523
|
+
function createMemoryAiAppAssistantTelemetryStore(options) {
|
|
1524
1524
|
const recentFailureLimit = normalizeLimit(options?.recentFailureLimit, 100);
|
|
1525
1525
|
const aggregate = emptySummary();
|
|
1526
1526
|
const failures = [];
|
|
@@ -1546,8 +1546,8 @@ function createMemoryAiDocsTelemetryStore(options) {
|
|
|
1546
1546
|
}
|
|
1547
1547
|
};
|
|
1548
1548
|
}
|
|
1549
|
-
function
|
|
1550
|
-
const prefix = options?.prefix ?? "ai-
|
|
1549
|
+
function createRedisAiAppAssistantTelemetryStore(client, options) {
|
|
1550
|
+
const prefix = options?.prefix ?? "ai-app-assistant:telemetry:";
|
|
1551
1551
|
const summaryKey = `${prefix}summary`;
|
|
1552
1552
|
const failuresKey = `${prefix}failures`;
|
|
1553
1553
|
const recentFailureLimit = normalizeLimit(options?.recentFailureLimit, 100);
|
|
@@ -1607,7 +1607,7 @@ function createRedisAiDocsTelemetryStore(client, options) {
|
|
|
1607
1607
|
}
|
|
1608
1608
|
};
|
|
1609
1609
|
}
|
|
1610
|
-
function
|
|
1610
|
+
function createAiAppAssistantFailureEvent(input) {
|
|
1611
1611
|
const failure = input.error instanceof AiSdkGenerationError ? input.error : normalizeAiSdkGenerationError(input.error, 1);
|
|
1612
1612
|
return {
|
|
1613
1613
|
outcome: "failure",
|
|
@@ -1675,7 +1675,7 @@ function redisPairs(value) {
|
|
|
1675
1675
|
}
|
|
1676
1676
|
|
|
1677
1677
|
// src/managed-runtime.ts
|
|
1678
|
-
function
|
|
1678
|
+
function createManagedAiAppAssistantRuntime(options) {
|
|
1679
1679
|
let assistant;
|
|
1680
1680
|
let activeGenerator;
|
|
1681
1681
|
let documents = [...options.documents ?? []];
|
|
@@ -1706,7 +1706,7 @@ function createManagedAiDocsRuntime(options) {
|
|
|
1706
1706
|
...options.maxRetries !== void 0 ? { maxRetries: options.maxRetries } : {}
|
|
1707
1707
|
});
|
|
1708
1708
|
activeGenerator = generator;
|
|
1709
|
-
assistant =
|
|
1709
|
+
assistant = createAiAppAssistant({
|
|
1710
1710
|
generator,
|
|
1711
1711
|
documents,
|
|
1712
1712
|
...options.policies ? { policies: options.policies } : {}
|
|
@@ -1748,7 +1748,7 @@ function createManagedAiDocsRuntime(options) {
|
|
|
1748
1748
|
async setDocuments(nextDocuments) {
|
|
1749
1749
|
documents = [...nextDocuments];
|
|
1750
1750
|
if (!activeGenerator) return;
|
|
1751
|
-
assistant =
|
|
1751
|
+
assistant = createAiAppAssistant({
|
|
1752
1752
|
generator: activeGenerator,
|
|
1753
1753
|
documents,
|
|
1754
1754
|
...options.policies ? { policies: options.policies } : {}
|
|
@@ -1774,7 +1774,7 @@ function createManagedAiDocsRuntime(options) {
|
|
|
1774
1774
|
}, identity);
|
|
1775
1775
|
return response;
|
|
1776
1776
|
} catch (error) {
|
|
1777
|
-
await observe(
|
|
1777
|
+
await observe(createAiAppAssistantFailureEvent({
|
|
1778
1778
|
error,
|
|
1779
1779
|
requestId: input.requestId,
|
|
1780
1780
|
operation: "answer",
|
|
@@ -1816,7 +1816,7 @@ function createManagedAiDocsRuntime(options) {
|
|
|
1816
1816
|
yield event;
|
|
1817
1817
|
}
|
|
1818
1818
|
} catch (error) {
|
|
1819
|
-
await observe(
|
|
1819
|
+
await observe(createAiAppAssistantFailureEvent({
|
|
1820
1820
|
error,
|
|
1821
1821
|
requestId: input.requestId,
|
|
1822
1822
|
operation: "stream",
|
|
@@ -1830,7 +1830,7 @@ function createManagedAiDocsRuntime(options) {
|
|
|
1830
1830
|
};
|
|
1831
1831
|
}
|
|
1832
1832
|
function unavailable() {
|
|
1833
|
-
return new
|
|
1833
|
+
return new AiAppAssistantManagementError(
|
|
1834
1834
|
503,
|
|
1835
1835
|
"not_configured",
|
|
1836
1836
|
"AI assistant is not configured or connected"
|
|
@@ -1839,23 +1839,23 @@ function unavailable() {
|
|
|
1839
1839
|
|
|
1840
1840
|
// src/managed-http.ts
|
|
1841
1841
|
import {
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1842
|
+
aiAppAssistantConfigurationInputSchema,
|
|
1843
|
+
aiAppAssistantConnectionTestInputSchema,
|
|
1844
|
+
aiAppAssistantCredentialsSchema,
|
|
1845
|
+
aiAppAssistantRequestSchema as aiAppAssistantRequestSchema2
|
|
1846
1846
|
} from "@123toto/ai-app-assistant-contracts";
|
|
1847
1847
|
import { ZodError } from "zod";
|
|
1848
|
-
function
|
|
1848
|
+
function createManagedAiAppAssistantFetchHandlers(options) {
|
|
1849
1849
|
const identity = async (request, nativeContext) => {
|
|
1850
1850
|
if (options.resolveIdentity) return options.resolveIdentity(request, nativeContext);
|
|
1851
1851
|
if (options.allowAnonymous) {
|
|
1852
1852
|
return { id: "anonymous", label: "Anonymous", roles: [] };
|
|
1853
1853
|
}
|
|
1854
|
-
throw new
|
|
1854
|
+
throw new AiAppAssistantManagementError(401, "unauthorized", "Authentication is required");
|
|
1855
1855
|
};
|
|
1856
1856
|
const admin = async (request, resolved, nativeContext) => {
|
|
1857
1857
|
if (!options.authorizeAdministration) {
|
|
1858
|
-
throw new
|
|
1858
|
+
throw new AiAppAssistantManagementError(403, "forbidden", "Administration access is not configured");
|
|
1859
1859
|
}
|
|
1860
1860
|
await options.authorizeAdministration(resolved, request, nativeContext);
|
|
1861
1861
|
};
|
|
@@ -1869,13 +1869,13 @@ function createManagedAiDocsFetchHandlers(options) {
|
|
|
1869
1869
|
return json(await options.runtime.configuration.getAccess(currentIdentity));
|
|
1870
1870
|
}
|
|
1871
1871
|
if (request.method === "POST" && path.endsWith("/ask/stream")) {
|
|
1872
|
-
const input =
|
|
1872
|
+
const input = aiAppAssistantRequestSchema2.parse(await readJson(request, options.maxBodyBytes));
|
|
1873
1873
|
const generation = options.runtime.stream(input, currentIdentity, request.signal);
|
|
1874
1874
|
const first = await generation.next();
|
|
1875
1875
|
return streamResponse(generation, first, input.requestId);
|
|
1876
1876
|
}
|
|
1877
1877
|
if (request.method === "POST" && path.endsWith("/ask")) {
|
|
1878
|
-
const input =
|
|
1878
|
+
const input = aiAppAssistantRequestSchema2.parse(await readJson(request, options.maxBodyBytes));
|
|
1879
1879
|
return json(await options.runtime.answer(input, currentIdentity));
|
|
1880
1880
|
}
|
|
1881
1881
|
await admin(request, currentIdentity, nativeContext);
|
|
@@ -1909,22 +1909,22 @@ function createManagedAiDocsFetchHandlers(options) {
|
|
|
1909
1909
|
return json({ roles, users });
|
|
1910
1910
|
}
|
|
1911
1911
|
if (request.method === "POST" && path.endsWith("/models")) {
|
|
1912
|
-
const input =
|
|
1912
|
+
const input = aiAppAssistantCredentialsSchema.parse(await readJson(request, options.maxBodyBytes));
|
|
1913
1913
|
return json(await options.runtime.configuration.listModels(input));
|
|
1914
1914
|
}
|
|
1915
1915
|
if (request.method === "POST" && path.endsWith("/configuration/test")) {
|
|
1916
|
-
const input =
|
|
1916
|
+
const input = aiAppAssistantConnectionTestInputSchema.parse(await readJson(request, options.maxBodyBytes));
|
|
1917
1917
|
return json(await options.runtime.configuration.testConnection(input));
|
|
1918
1918
|
}
|
|
1919
1919
|
if (request.method === "PUT" && path.endsWith("/configuration")) {
|
|
1920
|
-
const input =
|
|
1920
|
+
const input = aiAppAssistantConfigurationInputSchema.parse(await readJson(request, options.maxBodyBytes));
|
|
1921
1921
|
const { reloadRequired: _reloadRequired, ...result } = await options.runtime.configuration.save(input, currentIdentity);
|
|
1922
1922
|
return json(result);
|
|
1923
1923
|
}
|
|
1924
1924
|
if (request.method === "DELETE" && path.endsWith("/configuration/api-key")) {
|
|
1925
1925
|
return json(await options.runtime.configuration.revokeApiKey(currentIdentity));
|
|
1926
1926
|
}
|
|
1927
|
-
return json({ error: "not_found", message: "AI
|
|
1927
|
+
return json({ error: "not_found", message: "AI App Assistant endpoint not found" }, 404);
|
|
1928
1928
|
} catch (error) {
|
|
1929
1929
|
if (options.onError) return options.onError(error, request, nativeContext);
|
|
1930
1930
|
return mapError(error);
|
|
@@ -1935,11 +1935,11 @@ function createManagedAiDocsFetchHandlers(options) {
|
|
|
1935
1935
|
async function readJson(request, maxBodyBytes = 86e5) {
|
|
1936
1936
|
const declaredSize = Number(request.headers.get("content-length"));
|
|
1937
1937
|
if (Number.isFinite(declaredSize) && declaredSize > maxBodyBytes) {
|
|
1938
|
-
throw new
|
|
1938
|
+
throw new AiAppAssistantManagementError(413, "invalid_request", "Request body is too large");
|
|
1939
1939
|
}
|
|
1940
1940
|
const text = await request.text();
|
|
1941
1941
|
if (new TextEncoder().encode(text).byteLength > maxBodyBytes) {
|
|
1942
|
-
throw new
|
|
1942
|
+
throw new AiAppAssistantManagementError(413, "invalid_request", "Request body is too large");
|
|
1943
1943
|
}
|
|
1944
1944
|
return JSON.parse(text);
|
|
1945
1945
|
}
|
|
@@ -1960,9 +1960,9 @@ function streamResponse(generation, first, requestId) {
|
|
|
1960
1960
|
const failure = normalizeAiSdkGenerationError(error, 1);
|
|
1961
1961
|
controller.enqueue(encoder.encode(`${JSON.stringify({
|
|
1962
1962
|
type: "error",
|
|
1963
|
-
message: error instanceof
|
|
1964
|
-
retryable: error instanceof
|
|
1965
|
-
...error instanceof
|
|
1963
|
+
message: error instanceof AiAppAssistantManagementError ? error.message : "The assistant response could not be generated.",
|
|
1964
|
+
retryable: error instanceof AiAppAssistantManagementError ? false : failure.retryable,
|
|
1965
|
+
...error instanceof AiAppAssistantManagementError ? {} : {
|
|
1966
1966
|
code: failure.code,
|
|
1967
1967
|
requestId
|
|
1968
1968
|
}
|
|
@@ -1982,7 +1982,7 @@ function streamResponse(generation, first, requestId) {
|
|
|
1982
1982
|
});
|
|
1983
1983
|
}
|
|
1984
1984
|
function mapError(error) {
|
|
1985
|
-
if (error instanceof
|
|
1985
|
+
if (error instanceof AiAppAssistantManagementError) {
|
|
1986
1986
|
return json({ error: error.code, message: error.message, ...error.details }, error.status);
|
|
1987
1987
|
}
|
|
1988
1988
|
if (error instanceof ZodError || error instanceof SyntaxError) {
|
|
@@ -2002,16 +2002,16 @@ function json(value, status = 200) {
|
|
|
2002
2002
|
}
|
|
2003
2003
|
|
|
2004
2004
|
// src/managed-server.ts
|
|
2005
|
-
function
|
|
2005
|
+
function createManagedAiAppAssistantServer(options) {
|
|
2006
2006
|
const telemetry = resolveTelemetry(options.configuration, options.telemetry);
|
|
2007
2007
|
const configuration = resolveConfiguration(options.configuration);
|
|
2008
|
-
const runtime =
|
|
2008
|
+
const runtime = createManagedAiAppAssistantRuntime({
|
|
2009
2009
|
configuration,
|
|
2010
2010
|
...options.documents ? { documents: options.documents } : {},
|
|
2011
2011
|
...options.runtime,
|
|
2012
2012
|
...telemetry ? { telemetryStore: telemetry } : {}
|
|
2013
2013
|
});
|
|
2014
|
-
const fetch =
|
|
2014
|
+
const fetch = createManagedAiAppAssistantFetchHandlers({
|
|
2015
2015
|
runtime,
|
|
2016
2016
|
...options.http
|
|
2017
2017
|
});
|
|
@@ -2029,19 +2029,19 @@ function resolveTelemetry(configuration, telemetry) {
|
|
|
2029
2029
|
if (telemetry === false) return void 0;
|
|
2030
2030
|
if (telemetry?.store) return telemetry.store;
|
|
2031
2031
|
const recentFailureLimit = telemetry?.recentFailureLimit;
|
|
2032
|
-
if (!(configuration instanceof
|
|
2033
|
-
return
|
|
2034
|
-
prefix: `${configuration.storage.prefix ?? "ai-
|
|
2032
|
+
if (!(configuration instanceof AiAppAssistantConfigurationManager) && !("repository" in configuration) && configuration.storage?.type === "redis") {
|
|
2033
|
+
return createRedisAiAppAssistantTelemetryStore(configuration.storage.client, {
|
|
2034
|
+
prefix: `${configuration.storage.prefix ?? "ai-app-assistant:"}telemetry:`,
|
|
2035
2035
|
...recentFailureLimit !== void 0 ? { recentFailureLimit } : {}
|
|
2036
2036
|
});
|
|
2037
2037
|
}
|
|
2038
|
-
return
|
|
2038
|
+
return createMemoryAiAppAssistantTelemetryStore(
|
|
2039
2039
|
recentFailureLimit !== void 0 ? { recentFailureLimit } : void 0
|
|
2040
2040
|
);
|
|
2041
2041
|
}
|
|
2042
2042
|
function resolveConfiguration(configuration) {
|
|
2043
|
-
if (configuration instanceof
|
|
2044
|
-
if ("repository" in configuration) return new
|
|
2043
|
+
if (configuration instanceof AiAppAssistantConfigurationManager) return configuration;
|
|
2044
|
+
if ("repository" in configuration) return new AiAppAssistantConfigurationManager(configuration);
|
|
2045
2045
|
const {
|
|
2046
2046
|
storage = { type: "memory" },
|
|
2047
2047
|
encryptionKey,
|
|
@@ -2052,20 +2052,20 @@ function resolveConfiguration(configuration) {
|
|
|
2052
2052
|
apiKeyStorageAvailable,
|
|
2053
2053
|
...manager
|
|
2054
2054
|
} = configuration;
|
|
2055
|
-
const prefix = storage.type === "redis" ? storage.prefix ?? "ai-
|
|
2056
|
-
const store = storage.type === "redis" ?
|
|
2055
|
+
const prefix = storage.type === "redis" ? storage.prefix ?? "ai-app-assistant:" : "ai-app-assistant:";
|
|
2056
|
+
const store = storage.type === "redis" ? createRedisAiAppAssistantStore(storage.client, { prefix: `${prefix}persistent:` }) : createMemoryAiAppAssistantStore();
|
|
2057
2057
|
const protector = secretProtector ?? (encryptionKey ? createAes256GcmSecretProtector(encryptionKey) : createDisabledSecretProtector());
|
|
2058
|
-
return new
|
|
2058
|
+
return new AiAppAssistantConfigurationManager({
|
|
2059
2059
|
...manager,
|
|
2060
|
-
repository:
|
|
2060
|
+
repository: createAiAppAssistantConfigurationRepository({
|
|
2061
2061
|
store,
|
|
2062
2062
|
secretProtector: protector,
|
|
2063
2063
|
...repositoryKey ? { key: repositoryKey } : {}
|
|
2064
2064
|
}),
|
|
2065
|
-
quotaStore: quotaStore ?? (storage.type === "redis" ?
|
|
2065
|
+
quotaStore: quotaStore ?? (storage.type === "redis" ? createRedisAiAppAssistantQuotaStore(storage.client, { prefix: `${prefix}quota:` }) : createMemoryAiAppAssistantQuotaStore()),
|
|
2066
2066
|
apiKeyStorageAvailable: apiKeyStorageAvailable ?? Boolean(secretProtector || encryptionKey),
|
|
2067
2067
|
...synchronizer ? { synchronizer } : storage.type === "redis" ? {
|
|
2068
|
-
synchronizer:
|
|
2068
|
+
synchronizer: createPollingAiAppAssistantConfigurationSynchronizer(store, {
|
|
2069
2069
|
key: "configuration-revision",
|
|
2070
2070
|
intervalMs: storage.synchronizationIntervalMs ?? 2e3
|
|
2071
2071
|
})
|
|
@@ -2074,17 +2074,17 @@ function resolveConfiguration(configuration) {
|
|
|
2074
2074
|
}
|
|
2075
2075
|
|
|
2076
2076
|
// src/deployment-defaults.ts
|
|
2077
|
-
function
|
|
2077
|
+
function createAiAppAssistantDeploymentDefaults(options) {
|
|
2078
2078
|
const apiKeys = Object.fromEntries(
|
|
2079
2079
|
Object.entries(options.apiKeys ?? {}).map(([provider2, value]) => [provider2, value?.trim() || void 0])
|
|
2080
2080
|
);
|
|
2081
2081
|
const resolveApiKey = (provider2) => apiKeys[provider2];
|
|
2082
2082
|
if (!options.enabled || !options.model?.trim()) return { resolveApiKey };
|
|
2083
2083
|
const match = options.model.trim().match(/^([^:/]+)[:/](.+)$/);
|
|
2084
|
-
if (!match) throw new TypeError(`Invalid AI
|
|
2084
|
+
if (!match) throw new TypeError(`Invalid AI App Assistant model identifier: ${options.model}`);
|
|
2085
2085
|
const provider = match[1] === "gemini" ? "google" : match[1];
|
|
2086
2086
|
if (!listAiProviders().some((candidate) => candidate.id === provider)) {
|
|
2087
|
-
throw new TypeError(`Unsupported AI
|
|
2087
|
+
throw new TypeError(`Unsupported AI App Assistant provider: ${match[1]}`);
|
|
2088
2088
|
}
|
|
2089
2089
|
const apiKey = resolveApiKey(provider);
|
|
2090
2090
|
const baseURL = options.baseURLs?.[provider]?.trim() || void 0;
|
|
@@ -2303,70 +2303,70 @@ function clampInteger2(value, minimum, maximum) {
|
|
|
2303
2303
|
// src/index.ts
|
|
2304
2304
|
import {
|
|
2305
2305
|
PROTOCOL_VERSION as PROTOCOL_VERSION2,
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2306
|
+
aiAppAssistantAccessRuleSchema,
|
|
2307
|
+
aiAppAssistantConfigurationInputSchema as aiAppAssistantConfigurationInputSchema2,
|
|
2308
|
+
aiAppAssistantConfigurationOptionsSchema,
|
|
2309
|
+
aiAppAssistantConfigurationSaveResultSchema,
|
|
2310
|
+
aiAppAssistantConnectionResultSchema,
|
|
2311
|
+
aiAppAssistantConnectionTestInputSchema as aiAppAssistantConnectionTestInputSchema2,
|
|
2312
|
+
aiAppAssistantCredentialsSchema as aiAppAssistantCredentialsSchema2,
|
|
2313
|
+
aiAppAssistantManagedConfigurationViewSchema,
|
|
2314
|
+
aiAppAssistantModelInfoSchema,
|
|
2315
|
+
aiAppAssistantProviderSchema,
|
|
2316
|
+
aiAppAssistantProviderInfoSchema,
|
|
2317
|
+
aiAppAssistantRequestSchema as aiAppAssistantRequestSchema3,
|
|
2318
|
+
aiAppAssistantResponseSchema,
|
|
2319
|
+
aiAppAssistantTransportEventSchema
|
|
2320
2320
|
} from "@123toto/ai-app-assistant-contracts";
|
|
2321
2321
|
export {
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2322
|
+
AiAppAssistantConfigurationConflictError,
|
|
2323
|
+
AiAppAssistantConfigurationManager,
|
|
2324
|
+
AiAppAssistantManagementError,
|
|
2325
|
+
AiAppAssistantRequestError,
|
|
2326
2326
|
AiModelDiscoveryError,
|
|
2327
2327
|
AiSdkConfigurationError,
|
|
2328
2328
|
AiSdkGenerationError,
|
|
2329
2329
|
PROTOCOL_VERSION2 as PROTOCOL_VERSION,
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2330
|
+
aiAppAssistantAccessRuleSchema,
|
|
2331
|
+
aiAppAssistantConfigurationInputSchema2 as aiAppAssistantConfigurationInputSchema,
|
|
2332
|
+
aiAppAssistantConfigurationOptionsSchema,
|
|
2333
|
+
aiAppAssistantConfigurationSaveResultSchema,
|
|
2334
|
+
aiAppAssistantConnectionResultSchema,
|
|
2335
|
+
aiAppAssistantConnectionTestInputSchema2 as aiAppAssistantConnectionTestInputSchema,
|
|
2336
|
+
aiAppAssistantCredentialsSchema2 as aiAppAssistantCredentialsSchema,
|
|
2337
|
+
aiAppAssistantManagedConfigurationViewSchema,
|
|
2338
|
+
aiAppAssistantModelInfoSchema,
|
|
2339
|
+
aiAppAssistantProviderInfoSchema,
|
|
2340
|
+
aiAppAssistantProviderSchema,
|
|
2341
|
+
aiAppAssistantRequestSchema3 as aiAppAssistantRequestSchema,
|
|
2342
|
+
aiAppAssistantResponseSchema,
|
|
2343
|
+
aiAppAssistantTransportEventSchema,
|
|
2344
2344
|
createAes256GcmSecretProtector,
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2345
|
+
createAiAppAssistant,
|
|
2346
|
+
createAiAppAssistantConfigurationRepository,
|
|
2347
|
+
createAiAppAssistantDeploymentDefaults,
|
|
2348
|
+
createAiAppAssistantFailureEvent,
|
|
2349
|
+
createAiAppAssistantFetchHandlers,
|
|
2350
|
+
createAiAppAssistantNodeHttpListener,
|
|
2351
|
+
createAiAppAssistantServer,
|
|
2351
2352
|
createAiSdkGenerator,
|
|
2352
2353
|
createDisabledSecretProtector,
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
createMemoryAiDocsTelemetryStore,
|
|
2354
|
+
createManagedAiAppAssistantFetchHandlers,
|
|
2355
|
+
createManagedAiAppAssistantRuntime,
|
|
2356
|
+
createManagedAiAppAssistantServer,
|
|
2357
|
+
createMemoryAiAppAssistantQuotaStore,
|
|
2358
|
+
createMemoryAiAppAssistantStore,
|
|
2359
|
+
createMemoryAiAppAssistantTelemetryStore,
|
|
2360
2360
|
createOpenAiCompatibleGenerator,
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2361
|
+
createPollingAiAppAssistantConfigurationSynchronizer,
|
|
2362
|
+
createRedisAiAppAssistantQuotaStore,
|
|
2363
|
+
createRedisAiAppAssistantStore,
|
|
2364
|
+
createRedisAiAppAssistantTelemetryStore,
|
|
2365
2365
|
filterOpenApiContext,
|
|
2366
2366
|
listAiModels,
|
|
2367
2367
|
listAiProviders,
|
|
2368
2368
|
normalizeAiSdkGenerationError,
|
|
2369
2369
|
testAiSdkConnection,
|
|
2370
|
-
|
|
2370
|
+
validateAndSaveAiAppAssistantConfiguration
|
|
2371
2371
|
};
|
|
2372
2372
|
//# sourceMappingURL=index.js.map
|