@b2y/ai-service 0.0.0-stage → 1.0.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 +163 -2
- package/package.json +77 -4
- package/src/analytics/Analytics.js +581 -0
- package/src/analytics/Results.js +394 -0
- package/src/constants/AnalyticsConstants.js +6 -0
- package/src/constants/AssistantConstants.js +5 -0
- package/src/constants/ConversationConstants.js +5 -0
- package/src/constants/DocumentConstants.js +4 -0
- package/src/constants/ProviderConstants.js +23 -0
- package/src/constants/ServerConstants.js +15 -0
- package/src/constants/SqlConstants.js +33 -0
- package/src/constants/UiConstants.js +29 -0
- package/src/conversations/ConversationHistory.js +191 -0
- package/src/conversations/FolderConversationStore.js +103 -0
- package/src/copilot/Assistant.js +459 -0
- package/src/copilot/Capabilities.js +343 -0
- package/src/core/AiService.js +520 -0
- package/src/core/Context.js +243 -0
- package/src/core/Errors.js +249 -0
- package/src/core/Tasks.js +190 -0
- package/src/documents/Documents.js +450 -0
- package/src/documents/FolderDocumentStore.js +167 -0
- package/src/documents/PdfText.js +50 -0
- package/src/enums/ChartType.js +1 -0
- package/src/enums/DataClassification.js +2 -0
- package/src/enums/ProviderType.js +1 -0
- package/src/index.js +65 -0
- package/src/providers/Providers.js +733 -0
- package/src/server/Server.js +153 -0
- package/src/sql/PostgresSqlValidator.js +983 -0
- package/src/sql/SqlPolicy.js +483 -0
- package/src/ui/react/AssistantChat.js +594 -0
- package/src/ui/react/styles.css +239 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Server-side building blocks every application uses the same way: the AI runtime (service and chat history),
|
|
2
|
+
// ready-made HTTP endpoints with one error policy, a rate limiter, and read-only SQL execution.
|
|
3
|
+
import { createDefaultAiService } from "../core/AiService.js";
|
|
4
|
+
import { aiUserMessage, AiServiceError, errorCategory, ErrorCategories, isRateLimited, rateLimitInfo } from "../core/Errors.js";
|
|
5
|
+
import { createConversationHistory, scheduleConversationCleanup } from "../conversations/ConversationHistory.js";
|
|
6
|
+
import { createFolderConversationStore } from "../conversations/FolderConversationStore.js";
|
|
7
|
+
import { STATEMENT_TIMEOUT_MS } from "../constants/AnalyticsConstants.js";
|
|
8
|
+
import { CONVERSATION_LIST_LIMIT } from "../constants/ConversationConstants.js";
|
|
9
|
+
import { AI_MESSAGES, HTTP_STATUS, RESPONSE_STATUS } from "../constants/ServerConstants.js";
|
|
10
|
+
export { AI_MESSAGES, RESPONSE_STATUS } from "../constants/ServerConstants.js";
|
|
11
|
+
|
|
12
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
13
|
+
const noop = () => {};
|
|
14
|
+
const quietLogger = Object.freeze({ info: noop, warn: noop, error: noop });
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The AI service and server-side chat history for an application, from its configuration. Without an API key
|
|
18
|
+
* the assistant is off (ai is null); without a folder, chats are not kept.
|
|
19
|
+
* @param {{ apiKey?: string, tasks: readonly object[], conversationsDir?: string, retentionDays?: number, logger?: object }} options
|
|
20
|
+
*/
|
|
21
|
+
export function createAiRuntime({ apiKey, tasks, conversationsDir, retentionDays, logger = quietLogger }) {
|
|
22
|
+
if (!apiKey) return Object.freeze({ ai: null, history: null });
|
|
23
|
+
const ai = createDefaultAiService({ apiKey, tasks });
|
|
24
|
+
let history = null;
|
|
25
|
+
if (conversationsDir) {
|
|
26
|
+
history = createConversationHistory({ store: createFolderConversationStore({ root: conversationsDir }), ...(retentionDays ? { retentionDays } : {}) });
|
|
27
|
+
scheduleConversationCleanup({ history, onError: (error) => logger.warn("AI conversation cleanup failed", { code: error?.code || error?.name }) });
|
|
28
|
+
}
|
|
29
|
+
return Object.freeze({ ai, history });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Fixed-window limits in memory (one server instance): per user and per tenant. Keys come from the trusted
|
|
34
|
+
* context, so a user cannot reset them.
|
|
35
|
+
* @param {{ perUser?: number, perUserWindowMs?: number, perTenant?: number, perTenantWindowMs?: number, now?: () => number }} options
|
|
36
|
+
*/
|
|
37
|
+
export function createRateLimiter({ perUser = 15, perUserWindowMs = 10 * 60 * 1000, perTenant = 500, perTenantWindowMs = DAY_MS, now = () => Date.now() } = {}) {
|
|
38
|
+
const windows = new Map();
|
|
39
|
+
const hit = (key, limit, windowMs) => {
|
|
40
|
+
const at = now();
|
|
41
|
+
const entry = windows.get(key);
|
|
42
|
+
if (!entry || at - entry.start >= windowMs) {
|
|
43
|
+
windows.set(key, { start: at, count: 1 });
|
|
44
|
+
return { ok: true };
|
|
45
|
+
}
|
|
46
|
+
if (entry.count >= limit) return { ok: false, retryAfterSeconds: Math.ceil((entry.start + windowMs - at) / 1000) };
|
|
47
|
+
entry.count += 1;
|
|
48
|
+
return { ok: true };
|
|
49
|
+
};
|
|
50
|
+
const sweep = globalThis.setInterval(() => {
|
|
51
|
+
const at = now();
|
|
52
|
+
for (const [key, entry] of windows) if (at - entry.start > Math.max(perUserWindowMs, perTenantWindowMs)) windows.delete(key);
|
|
53
|
+
}, perUserWindowMs);
|
|
54
|
+
sweep.unref?.();
|
|
55
|
+
return Object.freeze({
|
|
56
|
+
take({ tenantId, userId }) {
|
|
57
|
+
const user = hit(`u:${tenantId}:${userId}`, perUser, perUserWindowMs);
|
|
58
|
+
return user.ok ? hit(`t:${tenantId}`, perTenant, perTenantWindowMs) : user;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Runs one statement in a READ ONLY transaction with a statement timeout (Sequelize or any client with the
|
|
65
|
+
* same transaction/query API). Values only through bind ($1) or named replacements (:name).
|
|
66
|
+
* @returns {Promise<{ rows: object[], fields: string[] }>}
|
|
67
|
+
*/
|
|
68
|
+
export async function runReadOnly(sequelize, sql, { bind, replacements, timeoutMs = STATEMENT_TIMEOUT_MS } = {}) {
|
|
69
|
+
return sequelize.transaction(async (transaction) => {
|
|
70
|
+
await sequelize.query("SET TRANSACTION READ ONLY", { transaction });
|
|
71
|
+
await sequelize.query(`SET LOCAL statement_timeout = ${Math.max(1, Math.trunc(timeoutMs))}`, { transaction });
|
|
72
|
+
const [rows, result] = await sequelize.query(sql, { ...(bind ? { bind } : {}), ...(replacements ? { replacements } : {}), transaction, type: "RAW" });
|
|
73
|
+
const fields = Array.isArray(result?.fields) ? result.fields.map((field) => String(field.name)) : rows.length > 0 ? Object.keys(rows[0]) : [];
|
|
74
|
+
return { rows, fields };
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Quoted, schema-qualified table names for the given Sequelize models: { key: model }. */
|
|
79
|
+
export function quoteTables(sequelize, models) {
|
|
80
|
+
const generator = sequelize.getQueryInterface().queryGenerator;
|
|
81
|
+
return Object.freeze(Object.fromEntries(Object.entries(models).map(([key, model]) => [key, generator.quoteTable(model.getTableName())])));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Express-style JSON reply in the B2Y shape { status, message, data }. */
|
|
85
|
+
const defaultRespond = (res, httpStatus, ok, message, data) => res.status(httpStatus).json({ status: ok ? RESPONSE_STATUS.SUCCESS : RESPONSE_STATUS.FAILURE, ...(message ? { message } : {}), ...(data !== undefined ? { data } : {}) });
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The assistant's HTTP endpoints with one error policy for every application. resolve(req) returns
|
|
89
|
+
* { assistant, context } for a signed-in user, or null when the assistant is not available (not configured,
|
|
90
|
+
* or not in the plan); it may throw accessDenied(...) or a package authorization error.
|
|
91
|
+
* @param {{ resolve: (req: object) => Promise<{ assistant: object, context: object } | null>, limiter?: object, logger?: object,
|
|
92
|
+
* messages?: Partial<typeof AI_MESSAGES>, respond?: Function }} options
|
|
93
|
+
*/
|
|
94
|
+
export function createAssistantEndpoints({ resolve, limiter, logger = quietLogger, messages = {}, respond = defaultRespond }) {
|
|
95
|
+
if (typeof resolve !== "function") throw new TypeError("createAssistantEndpoints needs resolve(req)");
|
|
96
|
+
const text = { ...AI_MESSAGES, ...messages };
|
|
97
|
+
const fail = (res, error, what) => {
|
|
98
|
+
if (isRateLimited(error)) {
|
|
99
|
+
logger.warn(`${what} rate limited`, rateLimitInfo(error));
|
|
100
|
+
return respond(res, HTTP_STATUS.TOO_MANY, false, aiUserMessage(error), { rateLimited: true });
|
|
101
|
+
}
|
|
102
|
+
const category = error instanceof AiServiceError ? errorCategory(error) : null;
|
|
103
|
+
if (category === ErrorCategories.NOT_AUTHORIZED) {
|
|
104
|
+
logger.warn(`${what} refused`, { code: error.code });
|
|
105
|
+
return respond(res, HTTP_STATUS.FORBIDDEN, false, text.NOT_PERMITTED);
|
|
106
|
+
}
|
|
107
|
+
if (category === ErrorCategories.INPUT_INVALID) return respond(res, HTTP_STATUS.BAD_REQUEST, false, text.MESSAGE_TOO_LONG);
|
|
108
|
+
logger.warn(`${what} failed`, { code: error?.code || error?.name });
|
|
109
|
+
return respond(res, HTTP_STATUS.BAD_GATEWAY, false, text.NOT_ANSWERED);
|
|
110
|
+
};
|
|
111
|
+
const handle = (what, run) => async (req, res) => {
|
|
112
|
+
try {
|
|
113
|
+
return await run(req, res);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
return fail(res, error, what);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return Object.freeze({
|
|
119
|
+
/** Whether the assistant is available to this user (the UI shows it only then). */
|
|
120
|
+
status: handle("AI status", async (req, res) => respond(res, HTTP_STATUS.OK, true, null, { enabled: Boolean(await resolve(req).catch(() => null)) })),
|
|
121
|
+
chat: handle("AI chat", async (req, res) => {
|
|
122
|
+
const ready = await resolve(req);
|
|
123
|
+
if (!ready) return respond(res, HTTP_STATUS.NOT_FOUND, false, text.NOT_AVAILABLE);
|
|
124
|
+
const allowed = limiter ? limiter.take({ tenantId: ready.context.tenantId, userId: ready.context.actor.userId }) : { ok: true };
|
|
125
|
+
if (!allowed.ok) return respond(res, HTTP_STATUS.TOO_MANY, false, text.TOO_MANY_MESSAGES, { rateLimited: true, retryAfterSeconds: allowed.retryAfterSeconds });
|
|
126
|
+
const reply = await ready.assistant.reply({ message: req.body?.message, context: ready.context, conversationId: req.body?.conversationId });
|
|
127
|
+
logger.info("AI chat answered", { route: reply.route, status: reply.status, blocks: reply.blocks.length });
|
|
128
|
+
return respond(res, HTTP_STATUS.OK, true, text.ANSWERED, { conversationId: reply.conversationId, status: reply.status, blocks: reply.blocks });
|
|
129
|
+
}),
|
|
130
|
+
listConversations: handle("AI conversations", async (req, res) => {
|
|
131
|
+
const ready = await resolve(req);
|
|
132
|
+
const conversations = ready?.assistant.hasHistory ? await ready.assistant.listConversations({ context: ready.context, limit: CONVERSATION_LIST_LIMIT }) : [];
|
|
133
|
+
return respond(res, HTTP_STATUS.OK, true, null, { conversations });
|
|
134
|
+
}),
|
|
135
|
+
getConversation: handle("AI conversation", async (req, res) => {
|
|
136
|
+
const ready = await resolve(req);
|
|
137
|
+
const messages = ready?.assistant.hasHistory ? await ready.assistant.loadConversation({ context: ready.context, conversationId: req.params?.id }) : [];
|
|
138
|
+
return respond(res, HTTP_STATUS.OK, true, null, { messages });
|
|
139
|
+
}),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Registers the endpoints on an Express router (after the application's own authentication middleware): GET
|
|
145
|
+
* {base}/status, POST {base}/chat, GET {base}/conversations, GET {base}/conversations/:id.
|
|
146
|
+
*/
|
|
147
|
+
export function mountAssistantRoutes(router, endpoints, base = "/ai") {
|
|
148
|
+
router.get(`${base}/status`, endpoints.status);
|
|
149
|
+
router.post(`${base}/chat`, endpoints.chat);
|
|
150
|
+
router.get(`${base}/conversations`, endpoints.listConversations);
|
|
151
|
+
router.get(`${base}/conversations/:id`, endpoints.getConversation);
|
|
152
|
+
return router;
|
|
153
|
+
}
|