@b2y/ai-service 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -7
- package/package.json +1 -2
- package/src/analytics/Analytics.js +9 -6
- package/src/{copilot → assistant}/Assistant.js +100 -107
- package/src/{copilot → assistant}/Capabilities.js +2 -1
- package/src/constants/ProviderConstants.js +14 -12
- package/src/constants/ServerConstants.js +3 -0
- package/src/conversations/ConversationHistory.js +1 -43
- package/src/conversations/FolderConversationStore.js +38 -17
- package/src/core/AiService.js +2 -10
- package/src/core/Deadline.js +24 -0
- package/src/core/Errors.js +0 -1
- package/src/documents/Documents.js +2 -3
- package/src/index.js +11 -23
- package/src/providers/Providers.js +24 -282
- package/src/server/Server.js +4 -5
- package/src/sql/SqlPolicy.js +1 -1
- package/src/ui/react/AssistantChat.js +2 -2
- package/src/enums/ProviderType.js +0 -1
package/README.md
CHANGED
|
@@ -10,12 +10,12 @@ closed.
|
|
|
10
10
|
## Layout
|
|
11
11
|
|
|
12
12
|
```
|
|
13
|
-
src/constants shared settings, one file per module (Assistant, Provider, Conversation, Analytics, Document, Server, Ui)
|
|
14
|
-
src/enums
|
|
13
|
+
src/constants shared settings, one file per module (Assistant, Provider, Conversation, Analytics, Document, Server, Sql, Ui)
|
|
14
|
+
src/enums shared value sets, one file per enum (ChartType, DataClassification)
|
|
15
15
|
src/core AiService (tasks, retries, rate limits, output validation), Context, Errors, Tasks
|
|
16
|
-
src/providers
|
|
16
|
+
src/providers the OpenAI-compatible provider client (internal)
|
|
17
17
|
src/server runtime, HTTP endpoints, rate limiter, read-only SQL runner (subpath ./server)
|
|
18
|
-
src/
|
|
18
|
+
src/assistant createAiAssistant (AI routing), capabilities, response blocks
|
|
19
19
|
src/analytics multi-query planner, result merging, charts and observations
|
|
20
20
|
src/sql SQL policy and the PostgreSQL AST validator
|
|
21
21
|
src/documents document Q&A; folder store and PDF text (subpaths)
|
|
@@ -52,15 +52,17 @@ const { conversationId, status, blocks } = await assistant.reply({ message, cont
|
|
|
52
52
|
|
|
53
53
|
## Provider and model
|
|
54
54
|
|
|
55
|
-
`AI_PROVIDER_CONFIG` (src/
|
|
55
|
+
`AI_PROVIDER_CONFIG` (src/constants/ProviderConstants.js) is the only place that names the provider and model for every
|
|
56
56
|
application; provider helpers are internal, so no application can name one. Applications pass only their API
|
|
57
57
|
key (`createAiRuntime({ apiKey, tasks, conversationsDir })` on the server). To change provider or model, edit
|
|
58
|
-
that configuration and
|
|
58
|
+
that configuration (any OpenAI-compatible API: base URL, model) and add its host to `APPROVED_PROVIDER_HOSTS`,
|
|
59
|
+
then release a new package version.
|
|
59
60
|
|
|
60
61
|
## Server (`@b2y/ai-service/server`)
|
|
61
62
|
|
|
62
63
|
```js
|
|
63
64
|
const { ai, history } = createAiRuntime({ apiKey, tasks, conversationsDir, retentionDays, logger });
|
|
65
|
+
// limiter is optional: by default 15 messages per user per 10 minutes and 500 per tenant per day.
|
|
64
66
|
const endpoints = createAssistantEndpoints({ resolve: async (req) => ({ assistant, context }) | null, limiter, logger });
|
|
65
67
|
mountAssistantRoutes(router, endpoints); // GET /ai/status, POST /ai/chat, GET /ai/conversations[/:id]
|
|
66
68
|
```
|
|
@@ -142,7 +144,7 @@ fast without reaching the provider until the wait has passed (at most one minute
|
|
|
142
144
|
- Only read-only capabilities; the model never chooses tenant, scope or permissions.
|
|
143
145
|
- Untrusted data is JSON-encoded in one `<untrusted_data>` block; output must be a complete JSON document
|
|
144
146
|
matching a strict schema.
|
|
145
|
-
-
|
|
147
|
+
- The provider only on approved hosts (`api.groq.com`), HTTPS, no redirects; request and response size
|
|
146
148
|
limits and timeouts.
|
|
147
149
|
- Errors and logs carry codes, counts and statuses only, never prompts, output or credentials.
|
|
148
150
|
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b2y/ai-service",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Shared AI assistant (Beya) for B2Y applications: global tenant and role/permission access, one provider and model configuration, read-only data, analytics, search and document capabilities, conversation history, server endpoints and a React chat UI and widget.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"b2y",
|
|
7
7
|
"ai",
|
|
8
8
|
"assistant",
|
|
9
9
|
"beya",
|
|
10
|
-
"copilot",
|
|
11
10
|
"text-to-sql",
|
|
12
11
|
"analytics",
|
|
13
12
|
"chat"
|
|
@@ -3,13 +3,15 @@ import { EMPTY_TOKEN_USAGE } from "../core/AiService.js";
|
|
|
3
3
|
import { assertAuthorized, checkRequirement } from "../core/Context.js";
|
|
4
4
|
import { AiServiceError, ErrorCodes, summarizeZodError } from "../core/Errors.js";
|
|
5
5
|
import { defineTask } from "../core/Tasks.js";
|
|
6
|
-
import { buildSchemaContext, checkQueryResult, describePolicyRules, isSqlPolicy
|
|
6
|
+
import { buildSchemaContext, checkQueryResult, describePolicyRules, isSqlPolicy } from "../sql/SqlPolicy.js";
|
|
7
|
+
import { SQL_DIALECTS } from "../constants/SqlConstants.js";
|
|
7
8
|
import { buildResultChart, mergeSections, observeSection } from "./Results.js";
|
|
9
|
+
import { createDeadline } from "../core/Deadline.js";
|
|
8
10
|
import { CHART_TYPES } from "../enums/ChartType.js";
|
|
9
11
|
import { DATA_CLASSIFICATION } from "../enums/DataClassification.js";
|
|
10
12
|
import { ANALYTICS_LIMITS, MAX_PLAN_CONVERSATION_TURNS, MAX_PLAN_QUERIES, MAX_QUESTION_CHARS } from "../constants/AnalyticsConstants.js";
|
|
13
|
+
|
|
11
14
|
const CHART_HINTS = Object.freeze(["none", "table", ...CHART_TYPES]);
|
|
12
|
-
export { ANALYTICS_LIMITS, MAX_PLAN_CONVERSATION_TURNS, MAX_PLAN_QUERIES, MAX_QUESTION_CHARS } from "../constants/AnalyticsConstants.js";
|
|
13
15
|
|
|
14
16
|
/** The application's description of its analytics, used to guide planning and to label results. */
|
|
15
17
|
|
|
@@ -352,17 +354,18 @@ export function createAnalyticsAssistant(options) {
|
|
|
352
354
|
|
|
353
355
|
/** Runs the planner within the remaining time; null when the deadline passed. */
|
|
354
356
|
async function plan() {
|
|
355
|
-
const deadline =
|
|
356
|
-
const signal = request.signal === undefined ? deadline : AbortSignal.any([deadline, request.signal]);
|
|
357
|
+
const deadline = createDeadline(Math.max(1, remaining()), request.signal);
|
|
357
358
|
try {
|
|
358
|
-
const result = await ai.runTask(ANALYTICS_PLAN_TASK, request.context, input, { signal });
|
|
359
|
+
const result = await ai.runTask(ANALYTICS_PLAN_TASK, request.context, input, { signal: deadline.signal });
|
|
359
360
|
usage = addUsage(usage, result.usage);
|
|
360
361
|
model = result.meta.model;
|
|
361
362
|
containsInstructionsToAI ||= result.output.containsInstructionsToAI;
|
|
362
363
|
return result.output;
|
|
363
364
|
} catch (error) {
|
|
364
|
-
if (deadline.
|
|
365
|
+
if (deadline.expired() && !request.signal?.aborted) return null;
|
|
365
366
|
throw error;
|
|
367
|
+
} finally {
|
|
368
|
+
deadline.dispose();
|
|
366
369
|
}
|
|
367
370
|
}
|
|
368
371
|
|
|
@@ -4,6 +4,7 @@ import { blocksToText } from "../conversations/ConversationHistory.js";
|
|
|
4
4
|
import { EMPTY_TOKEN_USAGE, noopLogger } from "../core/AiService.js";
|
|
5
5
|
import { assertAuthorized, checkRequirement, meetsRequirement } from "../core/Context.js";
|
|
6
6
|
import { AiServiceError, errorCategory, ErrorCodes, summarizeZodError } from "../core/Errors.js";
|
|
7
|
+
import { createDeadline } from "../core/Deadline.js";
|
|
7
8
|
import { defineTask } from "../core/Tasks.js";
|
|
8
9
|
import { ASSISTANT_NAME } from "../constants/AssistantConstants.js";
|
|
9
10
|
import { capabilityResultSchema, isCapability, isCapabilityGranted, responseBlocksSchema } from "./Capabilities.js";
|
|
@@ -71,19 +72,6 @@ export function isApplicationProfile(value) {
|
|
|
71
72
|
return typeof value === "object" && value !== null && definedProfiles.has(value);
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
/**
|
|
75
|
-
* The profile as task data: name, description, capabilities and out-of-scope topics only.
|
|
76
|
-
* @param {ApplicationProfile} profile
|
|
77
|
-
*/
|
|
78
|
-
export function profileForPrompt(profile) {
|
|
79
|
-
return {
|
|
80
|
-
name: profile.name,
|
|
81
|
-
description: profile.description,
|
|
82
|
-
capabilities: [...profile.capabilities],
|
|
83
|
-
outOfScope: [...profile.outOfScope],
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
75
|
/**
|
|
88
76
|
* Throws unless the request's trusted applicationId is this profile's: an application can only speak
|
|
89
77
|
* as itself, and only read its own documents.
|
|
@@ -146,7 +134,7 @@ export const ASSISTANT_ROUTE_TASK = defineTask({
|
|
|
146
134
|
dataLabel: "assistant message",
|
|
147
135
|
inputSchema: z.strictObject({
|
|
148
136
|
application: applicationSchema,
|
|
149
|
-
question: z.string().min(1).max(
|
|
137
|
+
question: z.string().min(1).max(MAX_MESSAGE_CHARS),
|
|
150
138
|
history: historySchema,
|
|
151
139
|
capabilities: capabilityList,
|
|
152
140
|
unavailable: unavailableList,
|
|
@@ -212,7 +200,7 @@ export const ASSISTANT_RESPOND_TASK = defineTask({
|
|
|
212
200
|
dataLabel: "assistant answer request",
|
|
213
201
|
inputSchema: z.strictObject({
|
|
214
202
|
application: applicationSchema,
|
|
215
|
-
question: z.string().min(1).max(
|
|
203
|
+
question: z.string().min(1).max(MAX_MESSAGE_CHARS),
|
|
216
204
|
history: historySchema,
|
|
217
205
|
facts: z.array(z.strictObject({ source: z.string().min(1).max(80), items: z.array(z.string().min(1).max(300)).max(20) })).max(4),
|
|
218
206
|
dataResultsShown: z.boolean(),
|
|
@@ -307,15 +295,14 @@ export function createAiAssistant({ ai, profile, capabilities = [], history, req
|
|
|
307
295
|
};
|
|
308
296
|
|
|
309
297
|
async function runCapability(capability, call) {
|
|
310
|
-
const
|
|
311
|
-
const signal = call.signal ? AbortSignal.any([controller.signal, call.signal]) : controller.signal;
|
|
298
|
+
const deadline = createDeadline(capability.timeoutMs, call.signal);
|
|
312
299
|
try {
|
|
313
|
-
const raw = await withTimeout(Promise.resolve().then(() => capability.handle({ ...call, signal })), capability.timeoutMs);
|
|
300
|
+
const raw = await withTimeout(Promise.resolve().then(() => capability.handle({ ...call, signal: deadline.signal })), capability.timeoutMs);
|
|
314
301
|
const parsed = capabilityResultSchema.safeParse(raw);
|
|
315
302
|
if (!parsed.success) throw new AiServiceError(ErrorCodes.CAPABILITY_FAILED, "The capability returned an invalid result", { details: { capability: capability.id } });
|
|
316
303
|
return { ...parsed.data, facts: capability.shareFacts ? parsed.data.facts : [] };
|
|
317
304
|
} catch (error) {
|
|
318
|
-
|
|
305
|
+
deadline.abort();
|
|
319
306
|
if (mustPropagate(error)) throw error;
|
|
320
307
|
const code = error instanceof DeadlineReached ? ErrorCodes.CAPABILITY_TIMEOUT : error instanceof AiServiceError ? error.code : ErrorCodes.CAPABILITY_FAILED;
|
|
321
308
|
log("warn", "Assistant capability failed", { capability: capability.id, code });
|
|
@@ -325,6 +312,8 @@ export function createAiAssistant({ ai, profile, capabilities = [], history, req
|
|
|
325
312
|
facts: [],
|
|
326
313
|
sources: [],
|
|
327
314
|
};
|
|
315
|
+
} finally {
|
|
316
|
+
deadline.dispose();
|
|
328
317
|
}
|
|
329
318
|
}
|
|
330
319
|
|
|
@@ -337,107 +326,111 @@ export function createAiAssistant({ ai, profile, capabilities = [], history, req
|
|
|
337
326
|
const message = typeof request.message === "string" ? request.message.trim() : "";
|
|
338
327
|
if (message === "" || message.length > MAX_MESSAGE_CHARS) throw new AiServiceError(ErrorCodes.INPUT_INVALID, `A message is 1-${String(MAX_MESSAGE_CHARS)} characters`);
|
|
339
328
|
const started = Date.now();
|
|
340
|
-
const deadline =
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
// Conversation: only this user's, in this tenant and application.
|
|
352
|
-
let conversationId = request.conversationId;
|
|
353
|
-
let earlier = [];
|
|
354
|
-
if (history) {
|
|
355
|
-
earlier = history.isConversationId(conversationId) ? await history.load({ context, conversationId }) : [];
|
|
356
|
-
if (earlier.length === 0) conversationId = history.newConversationId();
|
|
357
|
-
} else if (typeof conversationId !== "string" || !/^[0-9a-f-]{36}$/.test(conversationId)) {
|
|
358
|
-
conversationId = randomUUID();
|
|
359
|
-
}
|
|
360
|
-
const modelHistory = history ? history.forModel(earlier) : [];
|
|
329
|
+
const deadline = createDeadline(timeoutMs, request.signal);
|
|
330
|
+
const { signal } = deadline;
|
|
331
|
+
try {
|
|
332
|
+
let usage = { ...EMPTY_TOKEN_USAGE, latencyMs: 0, attempts: 0 };
|
|
333
|
+
let containsInstructionsToAI = false;
|
|
334
|
+
const runTask = async (task, input) => {
|
|
335
|
+
const result = await ai.runTask(task, context, input, { signal });
|
|
336
|
+
usage = addUsage(usage, result.usage);
|
|
337
|
+
containsInstructionsToAI ||= result.output.containsInstructionsToAI === true;
|
|
338
|
+
return result.output;
|
|
339
|
+
};
|
|
361
340
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
341
|
+
// Conversation: only this user's, in this tenant and application.
|
|
342
|
+
let conversationId = request.conversationId;
|
|
343
|
+
let earlier = [];
|
|
344
|
+
if (history) {
|
|
345
|
+
earlier = history.isConversationId(conversationId) ? await history.load({ context, conversationId }) : [];
|
|
346
|
+
if (earlier.length === 0) conversationId = history.newConversationId();
|
|
347
|
+
} else if (typeof conversationId !== "string" || !/^[0-9a-f-]{36}$/.test(conversationId)) {
|
|
348
|
+
conversationId = randomUUID();
|
|
349
|
+
}
|
|
350
|
+
const modelHistory = history ? history.forModel(earlier) : [];
|
|
371
351
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const dataNeeded = [...new Set(routing.dataNeeded)].filter((id) => byId.has(id) && byId.get(id).shareFacts).slice(0, 2);
|
|
352
|
+
// Authorization is decided here, from the trusted context: the model only sees granted capabilities.
|
|
353
|
+
const granted = capabilities.filter((capability) => isCapabilityGranted(capability, context));
|
|
354
|
+
const unavailable = capabilities
|
|
355
|
+
.filter((capability) => !granted.includes(capability))
|
|
356
|
+
.map((capability) => ({ title: capability.title, covers: capability.description, reason: meetsRequirement(context, capability.requires).missing === "permission" ? "not permitted" : "not enabled", hint: capability.unavailableHint ?? null }));
|
|
357
|
+
const byId = new Map(granted.map((capability) => [capability.id, capability]));
|
|
358
|
+
// What this user can do: their granted capabilities plus the profile's general abilities. Ungranted
|
|
359
|
+
// capabilities are never described as available (they are listed in unavailable instead).
|
|
360
|
+
const application = { ...baseApplication, capabilities: [...granted.map((capability) => `${capability.title}: ${capability.description}`.slice(0, 120)), ...baseApplication.capabilities].slice(0, 12) };
|
|
382
361
|
|
|
383
|
-
|
|
384
|
-
let blocks;
|
|
385
|
-
let sources = [];
|
|
386
|
-
let dataBased = false;
|
|
387
|
-
let appData;
|
|
388
|
-
const respond = async ({ facts = [], dataResultsShown = false, situation = "answer" }) => {
|
|
389
|
-
const output = await runTask(ASSISTANT_RESPOND_TASK, {
|
|
362
|
+
const routing = await runTask(ASSISTANT_ROUTE_TASK, {
|
|
390
363
|
application,
|
|
391
364
|
question: message,
|
|
392
365
|
history: modelHistory,
|
|
393
|
-
|
|
394
|
-
dataResultsShown,
|
|
395
|
-
freshInformationRequested: routing.freshInformation === true,
|
|
396
|
-
situation,
|
|
397
|
-
capabilities: granted.map(({ title, description }) => ({ title, description })),
|
|
366
|
+
capabilities: granted.map(({ id, kind, title, description, examples }) => ({ id, kind, title, description, examples: [...examples] })),
|
|
398
367
|
unavailable,
|
|
399
368
|
});
|
|
400
|
-
|
|
401
|
-
|
|
369
|
+
const allowedRoutes = new Set([...byId.keys(), "general", "unsupported"]);
|
|
370
|
+
let route = allowedRoutes.has(routing.route) ? routing.route : "general";
|
|
371
|
+
const dataNeeded = [...new Set(routing.dataNeeded)].filter((id) => byId.has(id) && byId.get(id).shareFacts).slice(0, 2);
|
|
402
372
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
373
|
+
let status = "ANSWERED";
|
|
374
|
+
let blocks;
|
|
375
|
+
let sources = [];
|
|
376
|
+
let dataBased = false;
|
|
377
|
+
let appData;
|
|
378
|
+
const respond = async ({ facts = [], dataResultsShown = false, situation = "answer" }) => {
|
|
379
|
+
const output = await runTask(ASSISTANT_RESPOND_TASK, {
|
|
380
|
+
application,
|
|
381
|
+
question: message,
|
|
382
|
+
history: modelHistory,
|
|
383
|
+
facts,
|
|
384
|
+
dataResultsShown,
|
|
385
|
+
freshInformationRequested: routing.freshInformation === true,
|
|
386
|
+
situation,
|
|
387
|
+
capabilities: granted.map(({ title, description }) => ({ title, description })),
|
|
388
|
+
unavailable,
|
|
389
|
+
});
|
|
390
|
+
return output.blocks.map((block) => (block.type === "bullets" ? { type: "bullets", items: block.items } : { type: block.type, text: block.text }));
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
if (byId.has(route)) {
|
|
394
|
+
const capability = byId.get(route);
|
|
395
|
+
const result = await runCapability(capability, { message, context, history: modelHistory, memory: capabilityMemory(earlier, capability.id), signal, runTask });
|
|
396
|
+
if (result.status === "NOT_FOUND" && result.blocks.length === 0) {
|
|
397
|
+
// The capability had nothing for this message: answer it as a general question instead.
|
|
398
|
+
route = "general";
|
|
399
|
+
blocks = await respond({});
|
|
400
|
+
} else {
|
|
401
|
+
status = result.status;
|
|
402
|
+
blocks = result.blocks;
|
|
403
|
+
sources = result.sources;
|
|
404
|
+
dataBased = capability.kind !== "content";
|
|
405
|
+
if (result.memory !== undefined) appData = { capability: capability.id, memory: result.memory };
|
|
406
|
+
}
|
|
407
|
+
} else if (route === "general") {
|
|
408
|
+
const facts = [];
|
|
409
|
+
const dataBlocks = [];
|
|
410
|
+
for (const id of dataNeeded) {
|
|
411
|
+
const result = await runCapability(byId.get(id), { message, context, history: modelHistory, memory: capabilityMemory(earlier, id), signal, runTask });
|
|
412
|
+
if (result.facts.length > 0) facts.push({ source: byId.get(id).title, items: result.facts });
|
|
413
|
+
dataBlocks.push(...result.blocks.filter((block) => block.type !== "paragraph"));
|
|
414
|
+
}
|
|
415
|
+
dataBased = facts.length > 0 || dataBlocks.length > 0;
|
|
416
|
+
blocks = [...(await respond({ facts, dataResultsShown: dataBlocks.length > 0 })), ...dataBlocks];
|
|
410
417
|
} else {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
dataBased = capability.kind !== "content";
|
|
415
|
-
if (result.memory !== undefined) appData = { capability: capability.id, memory: result.memory };
|
|
416
|
-
}
|
|
417
|
-
} else if (route === "general") {
|
|
418
|
-
const facts = [];
|
|
419
|
-
const dataBlocks = [];
|
|
420
|
-
for (const id of dataNeeded) {
|
|
421
|
-
const result = await runCapability(byId.get(id), { message, context, history: modelHistory, memory: capabilityMemory(earlier, id), signal, runTask });
|
|
422
|
-
if (result.facts.length > 0) facts.push({ source: byId.get(id).title, items: result.facts });
|
|
423
|
-
dataBlocks.push(...result.blocks.filter((block) => block.type !== "paragraph"));
|
|
418
|
+
// Requests to change data: nothing runs; the model writes a helpful redirect.
|
|
419
|
+
status = "UNSUPPORTED";
|
|
420
|
+
blocks = await respond({ situation: "change_request" });
|
|
424
421
|
}
|
|
425
|
-
|
|
426
|
-
blocks =
|
|
427
|
-
} else {
|
|
428
|
-
// Requests to change data: nothing runs; the model writes a helpful redirect.
|
|
429
|
-
status = "UNSUPPORTED";
|
|
430
|
-
blocks = await respond({ situation: "change_request" });
|
|
431
|
-
}
|
|
432
|
-
if (sources.length > 0 && !blocks.some((block) => block.type === "sources")) blocks.push({ type: "sources", items: sources.slice(0, 8) });
|
|
433
|
-
blocks = responseBlocksSchema.parse(blocks.slice(0, 30));
|
|
422
|
+
if (sources.length > 0 && !blocks.some((block) => block.type === "sources")) blocks.push({ type: "sources", items: sources.slice(0, 8) });
|
|
423
|
+
blocks = responseBlocksSchema.parse(blocks.slice(0, 30));
|
|
434
424
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
425
|
+
if (history) {
|
|
426
|
+
await history.append({ context, conversationId, role: "user", text: message, modelVisible: true });
|
|
427
|
+
await history.append({ context, conversationId, role: "assistant", text: blocksToText(blocks), blocks, route, status, modelVisible: !dataBased, ...(appData ? { appData } : {}) });
|
|
428
|
+
}
|
|
429
|
+
log("info", "Assistant replied", { route, status, dataBased, capabilities: granted.length, latencyMs: Date.now() - started });
|
|
430
|
+
return Object.freeze({ conversationId, status, route: byId.has(route) ? "capability" : route, blocks, usage, containsInstructionsToAI });
|
|
431
|
+
} finally {
|
|
432
|
+
deadline.dispose();
|
|
438
433
|
}
|
|
439
|
-
log("info", "Assistant replied", { route, status, dataBased, capabilities: granted.length, latencyMs: Date.now() - started });
|
|
440
|
-
return Object.freeze({ conversationId, status, route: byId.has(route) ? "capability" : route, blocks, usage, containsInstructionsToAI });
|
|
441
434
|
}
|
|
442
435
|
|
|
443
436
|
const requireHistory = () => {
|
|
@@ -4,6 +4,7 @@ import { checkRequirement, meetsRequirement } from "../core/Context.js";
|
|
|
4
4
|
import { AiServiceError, ErrorCodes, summarizeZodError } from "../core/Errors.js";
|
|
5
5
|
import { defineTask } from "../core/Tasks.js";
|
|
6
6
|
import { DATA_CLASSIFICATION } from "../enums/DataClassification.js";
|
|
7
|
+
import { MAX_MESSAGE_CHARS } from "../constants/AssistantConstants.js";
|
|
7
8
|
/** "content" is generated output (for example a flowchart); unlike data results the model may see it later. */
|
|
8
9
|
export const CAPABILITY_KINDS = Object.freeze(["data", "analytics", "search", "documents", "knowledge", "calculation", "content"]);
|
|
9
10
|
|
|
@@ -255,7 +256,7 @@ export const SEARCH_FILTERS_TASK = defineTask({
|
|
|
255
256
|
].join("\n"),
|
|
256
257
|
dataLabel: "search request",
|
|
257
258
|
inputSchema: z.strictObject({
|
|
258
|
-
question: z.string().min(1).max(
|
|
259
|
+
question: z.string().min(1).max(MAX_MESSAGE_CHARS),
|
|
259
260
|
history: z.array(z.strictObject({ role: z.enum(["user", "assistant"]), text: z.string().min(1).max(2000) })).max(8),
|
|
260
261
|
fields: z.array(searchFieldSchema).max(16),
|
|
261
262
|
sorts: z.array(z.strictObject({ name: fieldName, description: z.string().max(160) })).max(8),
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { DATA_CLASSIFICATION } from "../enums/DataClassification.js";
|
|
2
|
-
import { PROVIDER_TYPE } from "../enums/ProviderType.js";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const DEFAULT_MAX_REQUEST_BYTES = 256 * 1024;
|
|
9
|
-
export const DEFAULT_MAX_RESPONSE_BYTES = 1024 * 1024;
|
|
10
|
-
|
|
11
|
-
/** The provider and model every application uses: change it here and release the package. */
|
|
3
|
+
/**
|
|
4
|
+
* The provider and model every application uses (any OpenAI-compatible API): change it here and release the
|
|
5
|
+
* package. The host must also be in APPROVED_PROVIDER_HOSTS.
|
|
6
|
+
*/
|
|
12
7
|
export const AI_PROVIDER_CONFIG = Object.freeze({
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
id: "groq",
|
|
9
|
+
baseUrl: "https://api.groq.com/openai/v1",
|
|
10
|
+
model: "openai/gpt-oss-120b",
|
|
11
|
+
timeoutMs: 60_000,
|
|
12
|
+
structuredOutput: "json_schema",
|
|
15
13
|
allowedClassifications: Object.freeze([DATA_CLASSIFICATION.INTERNAL]),
|
|
16
|
-
paid: true,
|
|
17
14
|
invalidOutputRetries: 1,
|
|
18
15
|
});
|
|
19
16
|
|
|
17
|
+
/** The only hosts the API key may be sent to (HTTPS, default port). */
|
|
18
|
+
export const APPROVED_PROVIDER_HOSTS = Object.freeze(["api.groq.com"]);
|
|
19
|
+
|
|
20
|
+
export const DEFAULT_MAX_REQUEST_BYTES = 256 * 1024;
|
|
21
|
+
export const DEFAULT_MAX_RESPONSE_BYTES = 1024 * 1024;
|
|
20
22
|
export const RATE_LIMIT_COOLDOWN_MAX_MS = 60_000;
|
|
21
23
|
export const RATE_LIMIT_COOLDOWN_UNKNOWN_MS = 5_000;
|
|
22
24
|
export const MAX_OUTPUT_CHARS = 100_000;
|
|
@@ -13,3 +13,6 @@ export const AI_MESSAGES = Object.freeze({
|
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
export const RESPONSE_STATUS = Object.freeze({ SUCCESS: "SUCCESS", FAILURE: "FAILURE" });
|
|
16
|
+
|
|
17
|
+
/** Per server instance; applications may pass their own limits. */
|
|
18
|
+
export const RATE_LIMIT_DEFAULTS = Object.freeze({ perUser: 15, perUserWindowMs: 10 * 60 * 1000, perTenant: 500, perTenantWindowMs: 24 * 60 * 60 * 1000 });
|
|
@@ -3,9 +3,8 @@ import { z } from "zod";
|
|
|
3
3
|
import { parseTrustedContext } from "../core/Context.js";
|
|
4
4
|
import { AiServiceError } from "../core/Errors.js";
|
|
5
5
|
import { ErrorCodes } from "../core/Errors.js";
|
|
6
|
-
import { responseBlocksSchema } from "../
|
|
6
|
+
import { responseBlocksSchema } from "../assistant/Capabilities.js";
|
|
7
7
|
import { APP_DATA_MAX_BYTES, DEFAULT_RETENTION_DAYS, MAX_RETENTION_DAYS, MIN_RETENTION_DAYS } from "../constants/ConversationConstants.js";
|
|
8
|
-
export { APP_DATA_MAX_BYTES, DEFAULT_RETENTION_DAYS, MAX_RETENTION_DAYS } from "../constants/ConversationConstants.js";
|
|
9
8
|
|
|
10
9
|
/** Conversation history scoped by the trusted context (application, tenant, user). */
|
|
11
10
|
|
|
@@ -47,47 +46,6 @@ export function isConversationStore(value) {
|
|
|
47
46
|
|
|
48
47
|
const sameScope = (record, scope) => record.applicationId === scope.applicationId && record.tenantId === scope.tenantId && record.userId === scope.userId;
|
|
49
48
|
|
|
50
|
-
/** In-process store: for tests, development and single-instance deployments (lost on restart). */
|
|
51
|
-
export function createMemoryConversationStore() {
|
|
52
|
-
const conversations = new Map();
|
|
53
|
-
return defineConversationStore({
|
|
54
|
-
async appendMessage(message) {
|
|
55
|
-
const entry = conversations.get(message.conversationId) ?? { scope: { applicationId: message.applicationId, tenantId: message.tenantId, userId: message.userId }, messages: [], updatedAt: message.createdAt };
|
|
56
|
-
if (!sameScope(message, entry.scope)) throw new AiServiceError(ErrorCodes.NOT_AUTHORIZED, "The conversation belongs to another user");
|
|
57
|
-
entry.messages.push(structuredClone(message));
|
|
58
|
-
entry.updatedAt = message.createdAt;
|
|
59
|
-
conversations.set(message.conversationId, entry);
|
|
60
|
-
},
|
|
61
|
-
async listMessages({ scope, conversationId, limit }) {
|
|
62
|
-
const entry = conversations.get(conversationId);
|
|
63
|
-
if (!entry || !sameScope(entry.scope, scope)) return [];
|
|
64
|
-
return entry.messages.slice(-limit).map((message) => structuredClone(message));
|
|
65
|
-
},
|
|
66
|
-
async listConversations({ scope, limit }) {
|
|
67
|
-
return [...conversations.entries()]
|
|
68
|
-
.filter(([, entry]) => sameScope(entry.scope, scope))
|
|
69
|
-
.sort(([, a], [, b]) => (a.updatedAt < b.updatedAt ? 1 : -1))
|
|
70
|
-
.slice(0, limit)
|
|
71
|
-
.map(([conversationId, entry]) => ({ conversationId, title: (entry.messages.find((message) => message.role === "user")?.text ?? "").slice(0, 80), updatedAt: entry.updatedAt }));
|
|
72
|
-
},
|
|
73
|
-
async deleteConversation({ scope, conversationId }) {
|
|
74
|
-
const entry = conversations.get(conversationId);
|
|
75
|
-
if (!entry || !sameScope(entry.scope, scope)) return false;
|
|
76
|
-
return conversations.delete(conversationId);
|
|
77
|
-
},
|
|
78
|
-
async deleteInactiveSince({ cutoff }) {
|
|
79
|
-
let deleted = 0;
|
|
80
|
-
for (const [conversationId, entry] of conversations) {
|
|
81
|
-
if (entry.updatedAt < cutoff) {
|
|
82
|
-
conversations.delete(conversationId);
|
|
83
|
-
deleted += 1;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return deleted;
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
49
|
/** Plain text of reply blocks, for history (tables and charts are left out). */
|
|
92
50
|
export function blocksToText(blocks) {
|
|
93
51
|
return blocks
|
|
@@ -26,10 +26,19 @@ export function createFolderConversationStore({ root }) {
|
|
|
26
26
|
try {
|
|
27
27
|
return JSON.parse(await fs.readFile(file, "utf8"));
|
|
28
28
|
} catch (error) {
|
|
29
|
-
if (error?.code === "ENOENT") return null;
|
|
29
|
+
if (error?.code === "ENOENT" || error instanceof SyntaxError) return null;
|
|
30
30
|
throw error;
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
+
// One writer per file at a time, so concurrent replies to one conversation never overwrite each other.
|
|
34
|
+
const locks = new Map();
|
|
35
|
+
const exclusive = (file, task) => {
|
|
36
|
+
const run = (locks.get(file) ?? Promise.resolve()).then(task, task);
|
|
37
|
+
const tail = run.catch(() => {});
|
|
38
|
+
locks.set(file, tail);
|
|
39
|
+
tail.then(() => locks.get(file) === tail && locks.delete(file));
|
|
40
|
+
return run;
|
|
41
|
+
};
|
|
33
42
|
const write = async (file, value) => {
|
|
34
43
|
const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;
|
|
35
44
|
await fs.writeFile(temporary, JSON.stringify(value), { mode: 0o600, flag: "wx" });
|
|
@@ -47,7 +56,7 @@ export function createFolderConversationStore({ root }) {
|
|
|
47
56
|
for (const entry of entries) {
|
|
48
57
|
const path = join(dir, entry.name);
|
|
49
58
|
if (entry.isDirectory() && depth < 3) yield* walk(path, depth + 1);
|
|
50
|
-
else if (entry.isFile() && depth === 3 && entry.name.endsWith(".json")) yield path;
|
|
59
|
+
else if (entry.isFile() && depth === 3 && (entry.name.endsWith(".json") || entry.name.endsWith(".tmp"))) yield path;
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
|
|
@@ -56,10 +65,12 @@ export function createFolderConversationStore({ root }) {
|
|
|
56
65
|
const scope = { applicationId: message.applicationId, tenantId: message.tenantId, userId: message.userId };
|
|
57
66
|
await fs.mkdir(folderOf(scope), { recursive: true, mode: 0o700 });
|
|
58
67
|
const file = fileOf(scope, message.conversationId);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
68
|
+
await exclusive(file, async () => {
|
|
69
|
+
const current = (await read(file)) ?? { conversationId: message.conversationId, messages: [] };
|
|
70
|
+
current.messages.push(message);
|
|
71
|
+
current.updatedAt = message.createdAt;
|
|
72
|
+
await write(file, current);
|
|
73
|
+
});
|
|
63
74
|
},
|
|
64
75
|
async listMessages({ scope, conversationId, limit }) {
|
|
65
76
|
const current = await read(fileOf(scope, conversationId));
|
|
@@ -80,22 +91,32 @@ export function createFolderConversationStore({ root }) {
|
|
|
80
91
|
return rows.sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : -1)).slice(0, limit);
|
|
81
92
|
},
|
|
82
93
|
async deleteConversation({ scope, conversationId }) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
94
|
+
const file = fileOf(scope, conversationId);
|
|
95
|
+
return exclusive(file, async () => {
|
|
96
|
+
try {
|
|
97
|
+
await fs.unlink(file);
|
|
98
|
+
return true;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (error?.code === "ENOENT") return false;
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
90
104
|
},
|
|
91
105
|
async deleteInactiveSince({ cutoff }) {
|
|
92
106
|
let deleted = 0;
|
|
93
107
|
for await (const file of walk(base)) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
await fs.unlink(file).
|
|
97
|
-
|
|
108
|
+
if (file.endsWith(".tmp")) {
|
|
109
|
+
const stat = await fs.stat(file).catch(() => null);
|
|
110
|
+
if (stat && stat.mtime.toISOString() < cutoff && (await fs.unlink(file).then(() => true, () => false))) deleted += 1;
|
|
111
|
+
continue;
|
|
98
112
|
}
|
|
113
|
+
const removed = await exclusive(file, async () => {
|
|
114
|
+
const current = await read(file);
|
|
115
|
+
if (current && typeof current.updatedAt === "string" && current.updatedAt >= cutoff) return false;
|
|
116
|
+
await fs.unlink(file).catch(() => {});
|
|
117
|
+
return true;
|
|
118
|
+
});
|
|
119
|
+
if (removed) deleted += 1;
|
|
99
120
|
}
|
|
100
121
|
return deleted;
|
|
101
122
|
},
|
package/src/core/AiService.js
CHANGED
|
@@ -13,7 +13,6 @@ export { AI_PROVIDER_CONFIG, MAX_OUTPUT_CHARS } from "../constants/ProviderConst
|
|
|
13
13
|
* @property {TaskRegistry} registry
|
|
14
14
|
* @property {AiLogger} [logger]
|
|
15
15
|
* @property {RetryPolicyInput} [retry]
|
|
16
|
-
* @property {boolean} [allowPaidProvider] - Must be true to use a provider whose kind is PAID. Off by default.
|
|
17
16
|
* @property {() => number} [now]
|
|
18
17
|
* @property {(data: unknown, context: TrustedContext) => unknown} [redact] - Application-wide redaction applied after task minimization, before data reaches the prompt. Receives a frozen context.
|
|
19
18
|
* @property {(record: UsageRecord) => Promise<void> | void} [onUsage] - Called once per task with a metadata-only record, on success and on failure. - Success: if this throws or rejects, the task fails with USAGE_RECORDING_FAILED and the output is not returned, because metering is mandatory. - Failure: if this throws, the task's own error is still the one thrown; the metering failure is reported to the logger.
|
|
@@ -51,7 +50,6 @@ export { AI_PROVIDER_CONFIG, MAX_OUTPUT_CHARS } from "../constants/ProviderConst
|
|
|
51
50
|
|
|
52
51
|
const providerInfoSchema = z.object({
|
|
53
52
|
id: z.string().regex(/^[a-z][a-z0-9-]{0,63}$/),
|
|
54
|
-
kind: z.enum(["SELF_HOSTED", "PAID"]),
|
|
55
53
|
model: z.string().min(1).max(200),
|
|
56
54
|
modelRevision: z.string().min(1).max(200).nullable(),
|
|
57
55
|
allowedClassifications: z.array(dataClassificationSchema).min(1),
|
|
@@ -61,11 +59,6 @@ const providerInfoSchema = z.object({
|
|
|
61
59
|
export function createAiService(options) {
|
|
62
60
|
// providerInfo is a validated, frozen snapshot taken once.
|
|
63
61
|
const { provider, providerInfo } = validateProvider(options.provider);
|
|
64
|
-
if (providerInfo.kind === "PAID" && options.allowPaidProvider !== true) {
|
|
65
|
-
throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "Paid providers require allowPaidProvider: true", {
|
|
66
|
-
details: { providerId: providerInfo.id },
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
62
|
const retryResult = retryPolicySchema.safeParse(options.retry ?? {});
|
|
70
63
|
if (!retryResult.success) {
|
|
71
64
|
throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "Retry policy is invalid", {
|
|
@@ -508,11 +501,10 @@ export function assertProviderAcceptsClassification(provider, actual) {
|
|
|
508
501
|
*/
|
|
509
502
|
export function createDefaultAiService({ apiKey, tasks, logger, onUsage, fetch }) {
|
|
510
503
|
if (typeof apiKey !== "string" || apiKey.trim() === "") throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "createDefaultAiService needs the provider API key");
|
|
511
|
-
const {
|
|
504
|
+
const { invalidOutputRetries, allowedClassifications, ...provider } = AI_PROVIDER_CONFIG;
|
|
512
505
|
return createAiService({
|
|
513
|
-
provider: createProvider({
|
|
506
|
+
provider: createProvider({ ...provider, apiKey, allowedClassifications: [...allowedClassifications] }, fetch ? { fetch } : {}),
|
|
514
507
|
registry: createTaskRegistry(tasks),
|
|
515
|
-
allowPaidProvider: paid,
|
|
516
508
|
retry: { invalidOutputRetries },
|
|
517
509
|
...(logger ? { logger } : {}),
|
|
518
510
|
...(onUsage ? { onUsage } : {}),
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A signal that aborts after ms, or as soon as parent aborts. Always call dispose() when the work ends:
|
|
3
|
+
* unlike AbortSignal.timeout with AbortSignal.any, nothing stays in memory afterwards.
|
|
4
|
+
*/
|
|
5
|
+
export function createDeadline(ms, parent) {
|
|
6
|
+
const controller = new AbortController();
|
|
7
|
+
let expired = false;
|
|
8
|
+
const timer = setTimeout(() => {
|
|
9
|
+
expired = true;
|
|
10
|
+
controller.abort(new globalThis.DOMException("The operation timed out", "TimeoutError"));
|
|
11
|
+
}, ms);
|
|
12
|
+
const follow = () => controller.abort(parent.reason);
|
|
13
|
+
if (parent?.aborted) follow();
|
|
14
|
+
else parent?.addEventListener("abort", follow, { once: true });
|
|
15
|
+
return {
|
|
16
|
+
signal: controller.signal,
|
|
17
|
+
expired: () => expired,
|
|
18
|
+
abort: () => controller.abort(),
|
|
19
|
+
dispose: () => {
|
|
20
|
+
clearTimeout(timer);
|
|
21
|
+
parent?.removeEventListener("abort", follow);
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
package/src/core/Errors.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { assertProfileMatchesContext, isApplicationProfile } from "../
|
|
2
|
+
import { assertProfileMatchesContext, isApplicationProfile } from "../assistant/Assistant.js";
|
|
3
3
|
import { parseTrustedContext } from "../core/Context.js";
|
|
4
4
|
import { AiServiceError, ErrorCodes, summarizeZodError } from "../core/Errors.js";
|
|
5
5
|
import { defineTask } from "../core/Tasks.js";
|
|
6
6
|
import { MAX_PASSAGE_CHARS } from "../constants/DocumentConstants.js";
|
|
7
7
|
import { DATA_CLASSIFICATION } from "../enums/DataClassification.js";
|
|
8
|
-
export { MAX_PASSAGE_CHARS } from "../constants/DocumentConstants.js";
|
|
9
8
|
|
|
10
9
|
/** Generic document-store contract. */
|
|
11
10
|
|
|
@@ -380,7 +379,7 @@ export const DOCUMENT_ANSWER_TASK = defineTask({
|
|
|
380
379
|
* the request's context. `store` (an object with loadIndex(applicationId), such as the folder store) is
|
|
381
380
|
* still accepted and is read through the local search index.
|
|
382
381
|
* Register DOCUMENT_ANSWER_TASK with the AI service and permit DOCUMENT_ANSWER_TASK.id in the context.
|
|
383
|
-
* @param {{ ai: import("../core/AiService.js").AiService, profile: import("../
|
|
382
|
+
* @param {{ ai: import("../core/AiService.js").AiService, profile: import("../assistant/Assistant.js").ApplicationProfile, source?: ReturnType<typeof defineDocumentSource> | Parameters<typeof defineDocumentSource>[0], store?: { loadIndex: (applicationId: string) => Promise<unknown> }, maxPassages?: number, minCoverage?: number }} options
|
|
384
383
|
* @returns {DocumentQaAgent}
|
|
385
384
|
*/
|
|
386
385
|
export function createDocumentQaAgent({ ai, store, source, profile, maxPassages = 5, minCoverage = 0.5 }) {
|
package/src/index.js
CHANGED
|
@@ -9,21 +9,17 @@ export { parseTrustedContext } from "./core/Context.js";
|
|
|
9
9
|
export { AI_ASSISTANT_PERMISSION, accessDenied, assertAuthorized, buildTrustedContext, hasFeature, hasPermission, permissionKey, resolveTenantId, rolePermissions } from "./core/Context.js";
|
|
10
10
|
export { ASSISTANT_NAME } from "./constants/AssistantConstants.js";
|
|
11
11
|
export { DATA_CLASSIFICATION } from "./enums/DataClassification.js";
|
|
12
|
-
export { PROVIDER_TYPE } from "./enums/ProviderType.js";
|
|
13
12
|
export { RESPONSE_STATUS } from "./constants/ServerConstants.js";
|
|
14
13
|
|
|
15
14
|
export { AiServiceError, errorCategory } from "./core/Errors.js";
|
|
16
15
|
export { ErrorCategories, ErrorCodes } from "./core/Errors.js";
|
|
17
|
-
export {
|
|
16
|
+
export { aiUserMessage, isRateLimited, rateLimitInfo } from "./core/Errors.js";
|
|
18
17
|
|
|
19
|
-
// Providers are internal: AI_PROVIDER_CONFIG (
|
|
18
|
+
// Providers are internal: AI_PROVIDER_CONFIG (constants/ProviderConstants.js) is the one place that picks provider and model.
|
|
20
19
|
|
|
21
20
|
export {
|
|
22
21
|
defineSqlPolicy,
|
|
23
22
|
isSqlPolicy,
|
|
24
|
-
SQL_AGGREGATE_FUNCTIONS,
|
|
25
|
-
SQL_COLUMN_TYPES,
|
|
26
|
-
SQL_DIALECTS,
|
|
27
23
|
SQL_SAFE_FUNCTIONS,
|
|
28
24
|
SQL_SAFE_WINDOW_FUNCTIONS,
|
|
29
25
|
} from "./sql/SqlPolicy.js";
|
|
@@ -32,34 +28,26 @@ export { checkQueryResult } from "./sql/SqlPolicy.js";
|
|
|
32
28
|
export { buildSchemaContext, describePolicyRules } from "./sql/SqlPolicy.js";
|
|
33
29
|
|
|
34
30
|
export { defineAnalyticsCatalog, isAnalyticsCatalog } from "./analytics/Analytics.js";
|
|
35
|
-
export {
|
|
36
|
-
|
|
37
|
-
MAX_PLAN_CONVERSATION_TURNS,
|
|
38
|
-
MAX_PLAN_QUERIES,
|
|
39
|
-
} from "./analytics/Analytics.js";
|
|
40
|
-
export { ANALYTICS_LIMITS, createAnalyticsAssistant } from "./analytics/Analytics.js";
|
|
31
|
+
export { ANALYTICS_PLAN_TASK } from "./analytics/Analytics.js";
|
|
32
|
+
export { createAnalyticsAssistant } from "./analytics/Analytics.js";
|
|
41
33
|
export { mergeSections, observeSection } from "./analytics/Results.js";
|
|
42
34
|
export { buildResultChart, CHART_TYPES, chartSpecSchema } from "./analytics/Results.js";
|
|
43
35
|
|
|
44
|
-
export { defineApplicationProfile, isApplicationProfile } from "./
|
|
36
|
+
export { defineApplicationProfile, isApplicationProfile } from "./assistant/Assistant.js";
|
|
45
37
|
|
|
46
|
-
export { createIndexDocumentSource, defineDocumentSource, isDocumentSource
|
|
38
|
+
export { createIndexDocumentSource, defineDocumentSource, isDocumentSource } from "./documents/Documents.js";
|
|
47
39
|
export { buildSearchIndex, searchIndex, splitIntoPassages } from "./documents/Documents.js";
|
|
48
40
|
export { createDocumentQaAgent, DOCUMENT_ANSWER_TASK } from "./documents/Documents.js";
|
|
49
41
|
|
|
50
|
-
export { createAiAssistant } from "./
|
|
51
|
-
export { CAPABILITY_KINDS, createAnalyticsCapability, createDocumentsCapability, defineCapability, isCapability } from "./
|
|
52
|
-
export { createSearchCapability, SEARCH_FILTERS_TASK } from "./
|
|
53
|
-
export { ASSISTANT_RESPOND_TASK, ASSISTANT_ROUTE_TASK } from "./
|
|
54
|
-
export { analyticsReportToBlocks, responseBlocksSchema } from "./
|
|
42
|
+
export { createAiAssistant } from "./assistant/Assistant.js";
|
|
43
|
+
export { CAPABILITY_KINDS, createAnalyticsCapability, createDocumentsCapability, defineCapability, isCapability } from "./assistant/Capabilities.js";
|
|
44
|
+
export { createSearchCapability, SEARCH_FILTERS_TASK } from "./assistant/Capabilities.js";
|
|
45
|
+
export { ASSISTANT_RESPOND_TASK, ASSISTANT_ROUTE_TASK } from "./assistant/Assistant.js";
|
|
46
|
+
export { analyticsReportToBlocks, responseBlocksSchema } from "./assistant/Capabilities.js";
|
|
55
47
|
|
|
56
48
|
export {
|
|
57
|
-
APP_DATA_MAX_BYTES,
|
|
58
49
|
createConversationHistory,
|
|
59
|
-
createMemoryConversationStore,
|
|
60
|
-
DEFAULT_RETENTION_DAYS,
|
|
61
50
|
defineConversationStore,
|
|
62
51
|
isConversationStore,
|
|
63
|
-
MAX_RETENTION_DAYS,
|
|
64
52
|
scheduleConversationCleanup,
|
|
65
53
|
} from "./conversations/ConversationHistory.js";
|
|
@@ -1,19 +1,11 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { dataClassificationSchema } from "../core/Context.js";
|
|
3
3
|
import { AiServiceError, createCancelledError, ErrorCodes, summarizeZodError, systemErrorCode } from "../core/Errors.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export { APPROVED_PAID_HOSTS, DEFAULT_MAX_REQUEST_BYTES, DEFAULT_MAX_RESPONSE_BYTES, GROQ_BASE_URL, GROQ_DEFAULT_MODEL, GROQ_DEFAULT_TIMEOUT_MS } from "../constants/ProviderConstants.js";
|
|
7
|
-
export { PROVIDER_TYPE } from "../enums/ProviderType.js";
|
|
8
|
-
|
|
9
|
-
const PROVIDER_TYPES = Object.freeze(Object.values(PROVIDER_TYPE));
|
|
4
|
+
import { createDeadline } from "../core/Deadline.js";
|
|
5
|
+
import { APPROVED_PROVIDER_HOSTS, DEFAULT_MAX_REQUEST_BYTES, DEFAULT_MAX_RESPONSE_BYTES } from "../constants/ProviderConstants.js";
|
|
10
6
|
|
|
11
7
|
// Types only: the contract every model backend implements (documented for editors via JSDoc).
|
|
12
8
|
|
|
13
|
-
/**
|
|
14
|
-
* @typedef {"SELF_HOSTED" | "PAID"} ProviderKind
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
9
|
/**
|
|
18
10
|
* How the provider is asked to constrain output. Support differs between servers and versions (vLLM and others), so this is a per-deployment setting confirmed by integration tests, never assumed. Output is always validated with Zod afterwards regardless of the mode.
|
|
19
11
|
* @typedef {"json_schema" | "json_object" | "none"} StructuredOutputMode
|
|
@@ -22,7 +14,6 @@ const PROVIDER_TYPES = Object.freeze(Object.values(PROVIDER_TYPE));
|
|
|
22
14
|
/**
|
|
23
15
|
* @typedef {Object} ProviderInfo
|
|
24
16
|
* @property {string} id
|
|
25
|
-
* @property {ProviderKind} kind
|
|
26
17
|
* @property {string} model
|
|
27
18
|
* @property {string | null} modelRevision
|
|
28
19
|
* @property {readonly DataClassification[]} allowedClassifications
|
|
@@ -86,170 +77,22 @@ const PROVIDER_TYPES = Object.freeze(Object.values(PROVIDER_TYPE));
|
|
|
86
77
|
|
|
87
78
|
export {};
|
|
88
79
|
|
|
89
|
-
/**
|
|
90
|
-
|
|
91
|
-
* @property {ProviderKind} providerKind
|
|
92
|
-
* @property {boolean} allowInsecurePrivateHttp - Permit plain HTTP to allow-listed private IPs and single-label hosts (for example "vllm" on a Docker network).
|
|
93
|
-
* @property {readonly string[]} allowedHosts - Administrator-approved hosts for SELF_HOSTED providers. Every self-hosted host must be listed. Ignored for PAID.
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
const HOSTNAME_PATTERN = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/;
|
|
97
|
-
const IPV4_PATTERN = /^\d{1,3}(\.\d{1,3}){3}$/;
|
|
98
|
-
|
|
99
|
-
/** Validates a provider base URL before any request is made. */
|
|
100
|
-
export function resolveProviderBaseUrl(baseUrl, options) {
|
|
101
|
-
const url = parseBaseUrl(baseUrl);
|
|
102
|
-
const host = hostOf(url);
|
|
103
|
-
const hostClass = classifyHost(host);
|
|
104
|
-
|
|
105
|
-
if (hostClass === "forbidden") {
|
|
106
|
-
throw configError("Provider host is not permitted", "forbidden_address");
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (options.providerKind === "PAID") {
|
|
110
|
-
if (url.protocol !== "https:" || url.port !== "" || !APPROVED_PAID_HOSTS.includes(host)) {
|
|
111
|
-
throw configError("Paid providers require HTTPS to an approved hostname", "paid_endpoint_not_allowed");
|
|
112
|
-
}
|
|
113
|
-
return withTrailingSlash(url);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (APPROVED_PAID_HOSTS.includes(host)) {
|
|
117
|
-
throw configError("Hosted provider endpoints must be configured as PAID providers", "paid_host_requires_paid_kind");
|
|
118
|
-
}
|
|
119
|
-
const allowedHosts = options.allowedHosts.map(normalizeAllowedHost);
|
|
120
|
-
if (!allowedHosts.includes(host)) {
|
|
121
|
-
throw configError("Self-hosted provider host is not in the administrator allow-list", "host_not_allowlisted");
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (url.protocol === "http:") {
|
|
125
|
-
const privateNetwork = hostClass === "private" || hostClass === "internal-name";
|
|
126
|
-
if (hostClass !== "loopback" && !(privateNetwork && options.allowInsecurePrivateHttp)) {
|
|
127
|
-
throw configError(
|
|
128
|
-
"Plain HTTP is only allowed for loopback, or private hosts when allowInsecurePrivateHttp is set",
|
|
129
|
-
"insecure_http",
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return withTrailingSlash(url);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Normalises an allow-list entry the same way URLs are normalised, so "LOCALHOST", "127.1" and "::1" compare
|
|
139
|
-
* correctly.
|
|
140
|
-
*/
|
|
141
|
-
export function normalizeAllowedHost(entry) {
|
|
142
|
-
const bracketed = entry.includes(":") ? `[${entry.replace(/^\[|\]$/g, "")}]` : entry;
|
|
143
|
-
let url;
|
|
144
|
-
try {
|
|
145
|
-
url = new URL(`http://${bracketed}/`);
|
|
146
|
-
} catch {
|
|
147
|
-
throw configError("Allow-listed host is invalid", "invalid_allowed_host");
|
|
148
|
-
}
|
|
149
|
-
if (url.port !== "" || url.pathname !== "/" || url.search !== "" || url.hash !== "" || url.username !== "") {
|
|
150
|
-
throw configError("Allow-listed host must be a bare hostname or IP address", "invalid_allowed_host");
|
|
151
|
-
}
|
|
152
|
-
const host = hostOf(url);
|
|
153
|
-
if (classifyHost(host) === "forbidden") {
|
|
154
|
-
throw configError("Allow-listed host is not permitted", "forbidden_address");
|
|
155
|
-
}
|
|
156
|
-
return host;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Returns the base URL's host if it is loopback; otherwise throws CONFIG_INVALID. */
|
|
160
|
-
export function loopbackHostOf(baseUrl) {
|
|
161
|
-
const host = hostOf(parseBaseUrl(baseUrl));
|
|
162
|
-
if (classifyHost(host) !== "loopback") {
|
|
163
|
-
throw configError("This provider preset only allows loopback endpoints", "loopback_required");
|
|
164
|
-
}
|
|
165
|
-
return host;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function parseBaseUrl(baseUrl) {
|
|
80
|
+
/** The provider base URL: HTTPS on the default port to an approved host, without credentials or a query. */
|
|
81
|
+
function providerBaseUrl(baseUrl) {
|
|
169
82
|
let url;
|
|
170
83
|
try {
|
|
171
84
|
url = new URL(baseUrl);
|
|
172
85
|
} catch {
|
|
173
86
|
throw configError("Provider baseUrl is not a valid URL", "invalid_url");
|
|
174
87
|
}
|
|
175
|
-
if (url.protocol !== "https:"
|
|
176
|
-
throw configError("Provider baseUrl must
|
|
177
|
-
}
|
|
178
|
-
if (url.username !== "" || url.password !== "") {
|
|
179
|
-
throw configError("Provider baseUrl must not contain credentials", "credentials_in_url");
|
|
180
|
-
}
|
|
181
|
-
if (url.search !== "" || url.hash !== "") {
|
|
182
|
-
throw configError("Provider baseUrl must not contain a query or fragment", "query_or_fragment");
|
|
88
|
+
if (url.protocol !== "https:" || url.port !== "" || url.username !== "" || url.password !== "" || url.search !== "" || url.hash !== "") {
|
|
89
|
+
throw configError("Provider baseUrl must be plain HTTPS", "insecure_url");
|
|
183
90
|
}
|
|
91
|
+
if (!APPROVED_PROVIDER_HOSTS.includes(url.hostname.toLowerCase())) throw configError("Provider host is not approved", "host_not_approved");
|
|
92
|
+
if (!url.pathname.endsWith("/")) url.pathname = `${url.pathname}/`;
|
|
184
93
|
return url;
|
|
185
94
|
}
|
|
186
95
|
|
|
187
|
-
function hostOf(url) {
|
|
188
|
-
return url.hostname.replace(/^\[(.*)\]$/, "$1").toLowerCase();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function classifyHost(host) {
|
|
192
|
-
if (host === "localhost") {
|
|
193
|
-
return "loopback";
|
|
194
|
-
}
|
|
195
|
-
if (IPV4_PATTERN.test(host)) {
|
|
196
|
-
return classifyIPv4(host);
|
|
197
|
-
}
|
|
198
|
-
if (host.includes(":")) {
|
|
199
|
-
return classifyIPv6(host);
|
|
200
|
-
}
|
|
201
|
-
if (host.length > 253 || !HOSTNAME_PATTERN.test(host)) {
|
|
202
|
-
return "forbidden";
|
|
203
|
-
}
|
|
204
|
-
return host.includes(".") ? "public" : "internal-name";
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function classifyIPv4(host) {
|
|
208
|
-
const octets = host.split(".").map(Number);
|
|
209
|
-
const [a = -1, b = -1] = octets;
|
|
210
|
-
if (octets.some((octet) => octet > 255)) {
|
|
211
|
-
return "forbidden";
|
|
212
|
-
}
|
|
213
|
-
if (a === 127) {
|
|
214
|
-
return "loopback";
|
|
215
|
-
}
|
|
216
|
-
if (a === 0 || a >= 224 || (a === 169 && b === 254)) {
|
|
217
|
-
return "forbidden";
|
|
218
|
-
}
|
|
219
|
-
if (a === 10 || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168)) {
|
|
220
|
-
return "private";
|
|
221
|
-
}
|
|
222
|
-
return "public";
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function classifyIPv6(host) {
|
|
226
|
-
if (host === "::1") {
|
|
227
|
-
return "loopback";
|
|
228
|
-
}
|
|
229
|
-
if (host === "::" || host.startsWith("::ffff:")) {
|
|
230
|
-
return "forbidden";
|
|
231
|
-
}
|
|
232
|
-
const firstHextet = Number.parseInt(host.split(":")[0] ?? "", 16);
|
|
233
|
-
if (Number.isNaN(firstHextet)) {
|
|
234
|
-
return "forbidden";
|
|
235
|
-
}
|
|
236
|
-
if ((firstHextet & 0xffc0) === 0xfe80 || (firstHextet & 0xff00) === 0xff00) {
|
|
237
|
-
return "forbidden";
|
|
238
|
-
}
|
|
239
|
-
if ((firstHextet & 0xfe00) === 0xfc00) {
|
|
240
|
-
return "private";
|
|
241
|
-
}
|
|
242
|
-
return "public";
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function withTrailingSlash(url) {
|
|
246
|
-
const normalized = new URL(url.href);
|
|
247
|
-
if (!normalized.pathname.endsWith("/")) {
|
|
248
|
-
normalized.pathname = `${normalized.pathname}/`;
|
|
249
|
-
}
|
|
250
|
-
return normalized;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
96
|
function configError(message, reason) {
|
|
254
97
|
return new AiServiceError(ErrorCodes.CONFIG_INVALID, message, { details: { reason } });
|
|
255
98
|
}
|
|
@@ -279,7 +122,6 @@ const TRANSIENT_NETWORK_CODES = new Set([
|
|
|
279
122
|
|
|
280
123
|
const providerConfigSchema = z.strictObject({
|
|
281
124
|
id: z.string().regex(/^[a-z][a-z0-9-]{0,63}$/),
|
|
282
|
-
kind: z.enum(["SELF_HOSTED", "PAID"]),
|
|
283
125
|
baseUrl: z.string().min(1).max(2048),
|
|
284
126
|
model: z.string().min(1).max(200),
|
|
285
127
|
modelRevision: z.string().min(1).max(200).optional(),
|
|
@@ -289,13 +131,10 @@ const providerConfigSchema = z.strictObject({
|
|
|
289
131
|
.string()
|
|
290
132
|
.min(1)
|
|
291
133
|
.max(4096)
|
|
292
|
-
.regex(/^[\x21-\x7e]+$/)
|
|
293
|
-
.optional(),
|
|
134
|
+
.regex(/^[\x21-\x7e]+$/),
|
|
294
135
|
timeoutMs: z.number().int().min(100).max(600_000),
|
|
295
136
|
allowedClassifications: z.array(dataClassificationSchema).min(1),
|
|
296
137
|
structuredOutput: z.enum(["json_schema", "json_object", "none"]),
|
|
297
|
-
allowInsecurePrivateHttp: z.boolean().default(false),
|
|
298
|
-
allowedHosts: z.array(z.string().min(1).max(253)).max(20).default([]),
|
|
299
138
|
maxRequestBytes: z.number().int().min(1024).max(5_000_000).default(DEFAULT_MAX_REQUEST_BYTES),
|
|
300
139
|
maxResponseBytes: z.number().int().min(1024).max(10_000_000).default(DEFAULT_MAX_RESPONSE_BYTES),
|
|
301
140
|
});
|
|
@@ -328,10 +167,13 @@ const modelListSchema = z.object({
|
|
|
328
167
|
});
|
|
329
168
|
|
|
330
169
|
/**
|
|
331
|
-
*
|
|
332
|
-
* {baseUrl}models`),
|
|
170
|
+
* The provider client for any OpenAI-compatible Chat Completions API (`POST {baseUrl}chat/completions`, `GET
|
|
171
|
+
* {baseUrl}models`), configured by AI_PROVIDER_CONFIG.
|
|
172
|
+
* @param {{ id: string, baseUrl: string, model: string, apiKey: string, timeoutMs: number, structuredOutput: StructuredOutputMode, allowedClassifications: DataClassification[], modelRevision?: string, maxRequestBytes?: number, maxResponseBytes?: number }} config
|
|
173
|
+
* @param {{ fetch?: typeof fetch, now?: () => number }} [dependencies]
|
|
174
|
+
* @returns {ChatProvider}
|
|
333
175
|
*/
|
|
334
|
-
export function
|
|
176
|
+
export function createProvider(config, dependencies = {}) {
|
|
335
177
|
const parsed = providerConfigSchema.safeParse(config);
|
|
336
178
|
if (!parsed.success) {
|
|
337
179
|
throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "Provider configuration is invalid", {
|
|
@@ -339,18 +181,13 @@ export function createOpenAICompatibleProvider(config, dependencies = {}) {
|
|
|
339
181
|
});
|
|
340
182
|
}
|
|
341
183
|
const settings = parsed.data;
|
|
342
|
-
const baseUrl =
|
|
343
|
-
providerKind: settings.kind,
|
|
344
|
-
allowInsecurePrivateHttp: settings.allowInsecurePrivateHttp,
|
|
345
|
-
allowedHosts: settings.allowedHosts,
|
|
346
|
-
});
|
|
184
|
+
const baseUrl = providerBaseUrl(settings.baseUrl);
|
|
347
185
|
const completionsUrl = new URL("chat/completions", baseUrl);
|
|
348
186
|
const modelsUrl = new URL("models", baseUrl);
|
|
349
187
|
const now = dependencies.now ?? (() => performance.now());
|
|
350
188
|
|
|
351
189
|
const info = Object.freeze({
|
|
352
190
|
id: settings.id,
|
|
353
|
-
kind: settings.kind,
|
|
354
191
|
model: settings.model,
|
|
355
192
|
modelRevision: settings.modelRevision ?? null,
|
|
356
193
|
allowedClassifications: Object.freeze([...settings.allowedClassifications]),
|
|
@@ -361,7 +198,7 @@ export function createOpenAICompatibleProvider(config, dependencies = {}) {
|
|
|
361
198
|
return {
|
|
362
199
|
accept: "application/json",
|
|
363
200
|
"content-type": "application/json",
|
|
364
|
-
|
|
201
|
+
authorization: `Bearer ${settings.apiKey}`,
|
|
365
202
|
};
|
|
366
203
|
}
|
|
367
204
|
|
|
@@ -375,8 +212,8 @@ export function createOpenAICompatibleProvider(config, dependencies = {}) {
|
|
|
375
212
|
throw createCancelledError();
|
|
376
213
|
}
|
|
377
214
|
|
|
378
|
-
const
|
|
379
|
-
const signal =
|
|
215
|
+
const deadline = createDeadline(settings.timeoutMs, callerSignal);
|
|
216
|
+
const { signal } = deadline;
|
|
380
217
|
const fetchImpl = dependencies.fetch ?? globalThis.fetch;
|
|
381
218
|
|
|
382
219
|
try {
|
|
@@ -418,16 +255,18 @@ export function createOpenAICompatibleProvider(config, dependencies = {}) {
|
|
|
418
255
|
if (error instanceof AiServiceError) {
|
|
419
256
|
throw error;
|
|
420
257
|
}
|
|
421
|
-
if (
|
|
258
|
+
if (callerSignal?.aborted === true) {
|
|
422
259
|
throw createCancelledError();
|
|
423
260
|
}
|
|
424
|
-
if (
|
|
261
|
+
if (deadline.expired()) {
|
|
425
262
|
throw new AiServiceError(ErrorCodes.PROVIDER_TIMEOUT, "Provider request timed out", {
|
|
426
263
|
retryable: true,
|
|
427
264
|
details: { timeoutMs: settings.timeoutMs },
|
|
428
265
|
});
|
|
429
266
|
}
|
|
430
267
|
throw networkError(error);
|
|
268
|
+
} finally {
|
|
269
|
+
deadline.dispose();
|
|
431
270
|
}
|
|
432
271
|
}
|
|
433
272
|
|
|
@@ -540,14 +379,6 @@ async function readLimitedText(response, limitBytes) {
|
|
|
540
379
|
}
|
|
541
380
|
}
|
|
542
381
|
|
|
543
|
-
/**
|
|
544
|
-
* Signals abort asynchronously, so this is read through a function call: TypeScript would otherwise keep a
|
|
545
|
-
* narrowing from an earlier check.
|
|
546
|
-
*/
|
|
547
|
-
function isAborted(signal) {
|
|
548
|
-
return signal?.aborted === true;
|
|
549
|
-
}
|
|
550
|
-
|
|
551
382
|
async function discardBody(response) {
|
|
552
383
|
try {
|
|
553
384
|
await response.body?.cancel();
|
|
@@ -633,15 +464,7 @@ function responseTooLarge(limitBytes) {
|
|
|
633
464
|
});
|
|
634
465
|
}
|
|
635
466
|
|
|
636
|
-
|
|
637
|
-
if (value === "stop") {
|
|
638
|
-
return "stop";
|
|
639
|
-
}
|
|
640
|
-
if (value === "length") {
|
|
641
|
-
return "length";
|
|
642
|
-
}
|
|
643
|
-
return "other";
|
|
644
|
-
}
|
|
467
|
+
const mapFinishReason = (value) => (value === "stop" || value === "length" ? value : "other");
|
|
645
468
|
|
|
646
469
|
function mapUsage(usage) {
|
|
647
470
|
const promptTokens = usage?.prompt_tokens ?? null;
|
|
@@ -650,84 +473,3 @@ function mapUsage(usage) {
|
|
|
650
473
|
const derivedTotal = promptTokens !== null && completionTokens !== null ? promptTokens + completionTokens : null;
|
|
651
474
|
return { promptTokens, completionTokens, totalTokens: reportedTotal ?? derivedTotal };
|
|
652
475
|
}
|
|
653
|
-
|
|
654
|
-
/**
|
|
655
|
-
* Builds a ChatProvider from configuration, so a consumer changes provider by changing configuration, not
|
|
656
|
-
* code.
|
|
657
|
-
*/
|
|
658
|
-
|
|
659
|
-
const apiKeySchema = z
|
|
660
|
-
.string()
|
|
661
|
-
.min(1)
|
|
662
|
-
.max(4096)
|
|
663
|
-
.regex(/^[\x21-\x7e]+$/);
|
|
664
|
-
const structuredOutputSchema = z.enum(["json_schema", "json_object", "none"]);
|
|
665
|
-
|
|
666
|
-
const groqConfigSchema = z.strictObject({
|
|
667
|
-
id: z
|
|
668
|
-
.string()
|
|
669
|
-
.regex(/^[a-z][a-z0-9-]{0,63}$/)
|
|
670
|
-
.default(PROVIDER_TYPE.GROQ),
|
|
671
|
-
apiKey: apiKeySchema,
|
|
672
|
-
model: z.string().min(1).max(200).default(GROQ_DEFAULT_MODEL),
|
|
673
|
-
modelRevision: z.string().min(1).max(200).optional(),
|
|
674
|
-
// Required: what may be sent to a paid provider is the consumer's decision, never a default.
|
|
675
|
-
allowedClassifications: z.array(dataClassificationSchema).min(1),
|
|
676
|
-
timeoutMs: z.number().int().min(100).max(600_000).default(GROQ_DEFAULT_TIMEOUT_MS),
|
|
677
|
-
structuredOutput: structuredOutputSchema.default("json_schema"),
|
|
678
|
-
maxRequestBytes: z.number().int().min(1024).max(5_000_000).optional(),
|
|
679
|
-
maxResponseBytes: z.number().int().min(1024).max(10_000_000).optional(),
|
|
680
|
-
});
|
|
681
|
-
|
|
682
|
-
function invalid(error, schema, what) {
|
|
683
|
-
return new AiServiceError(ErrorCodes.CONFIG_INVALID, `${what} configuration is invalid`, {
|
|
684
|
-
details: summarizeZodError(error, schema),
|
|
685
|
-
});
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
/**
|
|
689
|
-
* Groq (paid, api.groq.com) through its OpenAI-compatible API. The service using it must also be
|
|
690
|
-
* created with allowPaidProvider: true.
|
|
691
|
-
* @param {{ apiKey: string, model?: string, allowedClassifications: DataClassification[], id?: string, modelRevision?: string, timeoutMs?: number, structuredOutput?: StructuredOutputMode, maxRequestBytes?: number, maxResponseBytes?: number }} config
|
|
692
|
-
* @param {{ fetch?: typeof fetch, now?: () => number }} [dependencies]
|
|
693
|
-
* @returns {ChatProvider}
|
|
694
|
-
*/
|
|
695
|
-
export function createGroqProvider(config, dependencies = {}) {
|
|
696
|
-
const parsed = groqConfigSchema.safeParse(config);
|
|
697
|
-
if (!parsed.success) throw invalid(parsed.error, groqConfigSchema, "Groq provider");
|
|
698
|
-
const settings = parsed.data;
|
|
699
|
-
return createOpenAICompatibleProvider(
|
|
700
|
-
{
|
|
701
|
-
id: settings.id,
|
|
702
|
-
kind: "PAID",
|
|
703
|
-
baseUrl: GROQ_BASE_URL,
|
|
704
|
-
model: settings.model,
|
|
705
|
-
...(settings.modelRevision === undefined ? {} : { modelRevision: settings.modelRevision }),
|
|
706
|
-
apiKey: settings.apiKey,
|
|
707
|
-
timeoutMs: settings.timeoutMs,
|
|
708
|
-
allowedClassifications: settings.allowedClassifications,
|
|
709
|
-
structuredOutput: settings.structuredOutput,
|
|
710
|
-
...(settings.maxRequestBytes === undefined ? {} : { maxRequestBytes: settings.maxRequestBytes }),
|
|
711
|
-
...(settings.maxResponseBytes === undefined ? {} : { maxResponseBytes: settings.maxResponseBytes }),
|
|
712
|
-
},
|
|
713
|
-
dependencies,
|
|
714
|
-
);
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* One entry point for every supported provider:
|
|
719
|
-
* { type: "groq", apiKey, model?, allowedClassifications, ... } see createGroqProvider (model defaults to GROQ_DEFAULT_MODEL)
|
|
720
|
-
* { type: "openai-compatible", id, kind, baseUrl, model, timeoutMs, ... } any other OpenAI-compatible server,
|
|
721
|
-
* under the endpoint policy
|
|
722
|
-
* @param {{ type: "groq" | "openai-compatible" } & Record<string, unknown>} config
|
|
723
|
-
* @param {{ fetch?: typeof fetch, now?: () => number }} [dependencies]
|
|
724
|
-
* @returns {ChatProvider}
|
|
725
|
-
*/
|
|
726
|
-
export function createProvider(config, dependencies = {}) {
|
|
727
|
-
if (typeof config !== "object" || config === null || !PROVIDER_TYPES.includes(config.type)) {
|
|
728
|
-
throw new AiServiceError(ErrorCodes.CONFIG_INVALID, `Provider type must be one of: ${PROVIDER_TYPES.join(", ")}`);
|
|
729
|
-
}
|
|
730
|
-
const { type, ...rest } = config;
|
|
731
|
-
if (type === PROVIDER_TYPE.GROQ) return createGroqProvider(rest, dependencies);
|
|
732
|
-
return createOpenAICompatibleProvider(rest, dependencies);
|
|
733
|
-
}
|
package/src/server/Server.js
CHANGED
|
@@ -6,10 +6,9 @@ import { createConversationHistory, scheduleConversationCleanup } from "../conve
|
|
|
6
6
|
import { createFolderConversationStore } from "../conversations/FolderConversationStore.js";
|
|
7
7
|
import { STATEMENT_TIMEOUT_MS } from "../constants/AnalyticsConstants.js";
|
|
8
8
|
import { CONVERSATION_LIST_LIMIT } from "../constants/ConversationConstants.js";
|
|
9
|
-
import { AI_MESSAGES, HTTP_STATUS, RESPONSE_STATUS } from "../constants/ServerConstants.js";
|
|
9
|
+
import { AI_MESSAGES, HTTP_STATUS, RATE_LIMIT_DEFAULTS, RESPONSE_STATUS } from "../constants/ServerConstants.js";
|
|
10
10
|
export { AI_MESSAGES, RESPONSE_STATUS } from "../constants/ServerConstants.js";
|
|
11
11
|
|
|
12
|
-
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
13
12
|
const noop = () => {};
|
|
14
13
|
const quietLogger = Object.freeze({ info: noop, warn: noop, error: noop });
|
|
15
14
|
|
|
@@ -34,7 +33,7 @@ export function createAiRuntime({ apiKey, tasks, conversationsDir, retentionDays
|
|
|
34
33
|
* context, so a user cannot reset them.
|
|
35
34
|
* @param {{ perUser?: number, perUserWindowMs?: number, perTenant?: number, perTenantWindowMs?: number, now?: () => number }} options
|
|
36
35
|
*/
|
|
37
|
-
export function createRateLimiter({ perUser =
|
|
36
|
+
export function createRateLimiter({ perUser = RATE_LIMIT_DEFAULTS.perUser, perUserWindowMs = RATE_LIMIT_DEFAULTS.perUserWindowMs, perTenant = RATE_LIMIT_DEFAULTS.perTenant, perTenantWindowMs = RATE_LIMIT_DEFAULTS.perTenantWindowMs, now = () => Date.now() } = {}) {
|
|
38
37
|
const windows = new Map();
|
|
39
38
|
const hit = (key, limit, windowMs) => {
|
|
40
39
|
const at = now();
|
|
@@ -91,7 +90,7 @@ const defaultRespond = (res, httpStatus, ok, message, data) => res.status(httpSt
|
|
|
91
90
|
* @param {{ resolve: (req: object) => Promise<{ assistant: object, context: object } | null>, limiter?: object, logger?: object,
|
|
92
91
|
* messages?: Partial<typeof AI_MESSAGES>, respond?: Function }} options
|
|
93
92
|
*/
|
|
94
|
-
export function createAssistantEndpoints({ resolve, limiter, logger = quietLogger, messages = {}, respond = defaultRespond }) {
|
|
93
|
+
export function createAssistantEndpoints({ resolve, limiter = createRateLimiter(), logger = quietLogger, messages = {}, respond = defaultRespond }) {
|
|
95
94
|
if (typeof resolve !== "function") throw new TypeError("createAssistantEndpoints needs resolve(req)");
|
|
96
95
|
const text = { ...AI_MESSAGES, ...messages };
|
|
97
96
|
const fail = (res, error, what) => {
|
|
@@ -121,7 +120,7 @@ export function createAssistantEndpoints({ resolve, limiter, logger = quietLogge
|
|
|
121
120
|
chat: handle("AI chat", async (req, res) => {
|
|
122
121
|
const ready = await resolve(req);
|
|
123
122
|
if (!ready) return respond(res, HTTP_STATUS.NOT_FOUND, false, text.NOT_AVAILABLE);
|
|
124
|
-
const allowed = limiter
|
|
123
|
+
const allowed = limiter.take({ tenantId: ready.context.tenantId, userId: ready.context.actor.userId });
|
|
125
124
|
if (!allowed.ok) return respond(res, HTTP_STATUS.TOO_MANY, false, text.TOO_MANY_MESSAGES, { rateLimited: true, retryAfterSeconds: allowed.retryAfterSeconds });
|
|
126
125
|
const reply = await ready.assistant.reply({ message: req.body?.message, context: ready.context, conversationId: req.body?.conversationId });
|
|
127
126
|
logger.info("AI chat answered", { route: reply.route, status: reply.status, blocks: reply.blocks.length });
|
package/src/sql/SqlPolicy.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { AiServiceError, ErrorCodes, summarizeZodError } from "../core/Errors.js";
|
|
3
3
|
import { SQL_AGGREGATE_FUNCTIONS, SQL_COLUMN_TYPES, SQL_DIALECTS, SQL_SAFE_FUNCTIONS, SQL_SAFE_WINDOW_FUNCTIONS } from "../constants/SqlConstants.js";
|
|
4
|
-
export {
|
|
4
|
+
export { SQL_SAFE_FUNCTIONS, SQL_SAFE_WINDOW_FUNCTIONS } from "../constants/SqlConstants.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @typedef {Object} SqlColumnInfo
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createElement as h, useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { ASSISTANT_NAME } from "../../constants/AssistantConstants.js";
|
|
2
|
+
import { ASSISTANT_NAME, MAX_MESSAGE_CHARS } from "../../constants/AssistantConstants.js";
|
|
3
3
|
import { ASSISTANT_PATHS } from "../../constants/ServerConstants.js";
|
|
4
4
|
import { COMPACT_MEDIA_QUERY, PHONE_MEDIA_QUERY, UI_LABELS } from "../../constants/UiConstants.js";
|
|
5
5
|
export { ASSISTANT_PATHS } from "../../constants/ServerConstants.js";
|
|
@@ -240,7 +240,7 @@ export async function restoreConversation({ conversationId, loadConversation, li
|
|
|
240
240
|
* maxLength?: number, className?: string, style?: object,
|
|
241
241
|
* }} props
|
|
242
242
|
*/
|
|
243
|
-
export function AssistantChat({ send, loadConversation, listConversations, conversationId: initialConversationId, onConversationChange, title, intro, suggestions = [], labels: labelOverrides = {}, maxLength =
|
|
243
|
+
export function AssistantChat({ send, loadConversation, listConversations, conversationId: initialConversationId, onConversationChange, title, intro, suggestions = [], labels: labelOverrides = {}, maxLength = MAX_MESSAGE_CHARS, className = "", style, onOpenLink, avatar, variant = "panel" }) {
|
|
244
244
|
const labels = { ...DEFAULT_LABELS, ...labelOverrides };
|
|
245
245
|
const page = variant === "page";
|
|
246
246
|
const [sidebarOpen, setSidebarOpen] = useState(() => !globalThis.matchMedia?.(PHONE_MEDIA_QUERY).matches);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const PROVIDER_TYPE = Object.freeze({ GROQ: "groq", OPENAI_COMPATIBLE: "openai-compatible" });
|