@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,191 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { parseTrustedContext } from "../core/Context.js";
|
|
4
|
+
import { AiServiceError } from "../core/Errors.js";
|
|
5
|
+
import { ErrorCodes } from "../core/Errors.js";
|
|
6
|
+
import { responseBlocksSchema } from "../copilot/Capabilities.js";
|
|
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
|
+
|
|
10
|
+
/** Conversation history scoped by the trusted context (application, tenant, user). */
|
|
11
|
+
|
|
12
|
+
const DAY_MS = 86_400_000;
|
|
13
|
+
const CONVERSATION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
14
|
+
|
|
15
|
+
export const conversationMessageSchema = z.strictObject({
|
|
16
|
+
messageId: z.string().regex(CONVERSATION_ID),
|
|
17
|
+
conversationId: z.string().regex(CONVERSATION_ID),
|
|
18
|
+
applicationId: z.string().min(1).max(64),
|
|
19
|
+
tenantId: z.string().min(1).max(128),
|
|
20
|
+
userId: z.string().min(1).max(128),
|
|
21
|
+
role: z.enum(["user", "assistant"]),
|
|
22
|
+
text: z.string().max(6000),
|
|
23
|
+
blocks: responseBlocksSchema.default([]),
|
|
24
|
+
route: z.string().max(64).nullable().default(null),
|
|
25
|
+
status: z.string().max(32).nullable().default(null),
|
|
26
|
+
modelVisible: z.boolean(),
|
|
27
|
+
// The application's own display data (JSON, at most APP_DATA_MAX_BYTES); never shown to the model.
|
|
28
|
+
appData: z.json().optional(),
|
|
29
|
+
createdAt: z.iso.datetime(),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const STORE_METHODS = ["appendMessage", "listMessages", "listConversations", "deleteConversation", "deleteInactiveSince"];
|
|
33
|
+
const stores = new WeakSet();
|
|
34
|
+
|
|
35
|
+
/** Wraps a consumer's storage. */
|
|
36
|
+
export function defineConversationStore(definition) {
|
|
37
|
+
const missing = STORE_METHODS.filter((name) => typeof definition?.[name] !== "function");
|
|
38
|
+
if (missing.length > 0) throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "A conversation store needs appendMessage, listMessages, listConversations, deleteConversation and deleteInactiveSince", { details: { missing: missing.join(",") } });
|
|
39
|
+
const store = Object.freeze(Object.fromEntries(STORE_METHODS.map((name) => [name, definition[name]])));
|
|
40
|
+
stores.add(store);
|
|
41
|
+
return store;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isConversationStore(value) {
|
|
45
|
+
return typeof value === "object" && value !== null && stores.has(value);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const sameScope = (record, scope) => record.applicationId === scope.applicationId && record.tenantId === scope.tenantId && record.userId === scope.userId;
|
|
49
|
+
|
|
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
|
+
/** Plain text of reply blocks, for history (tables and charts are left out). */
|
|
92
|
+
export function blocksToText(blocks) {
|
|
93
|
+
return blocks
|
|
94
|
+
.map((block) => {
|
|
95
|
+
if (block.type === "paragraph" || block.type === "heading") return block.text;
|
|
96
|
+
if (block.type === "bullets") return block.items.map((item) => `- ${item}`).join("\n");
|
|
97
|
+
if (block.type === "metrics") return block.items.map((item) => `${item.label}: ${item.value}`).join("; ");
|
|
98
|
+
if (block.type === "notice") return block.items.join(" ");
|
|
99
|
+
if (block.type === "cards") return [block.title, ...block.items.map((item) => `- ${item.title}${item.price ? ` (${item.price})` : ""}`)].filter(Boolean).join("\n");
|
|
100
|
+
if (block.type === "flow") return [block.title, ...block.nodes.map((node, index) => `${index + 1}. ${node.label}`)].filter(Boolean).join("\n");
|
|
101
|
+
return "";
|
|
102
|
+
})
|
|
103
|
+
.filter((part) => part !== "")
|
|
104
|
+
.join("\n")
|
|
105
|
+
.slice(0, 6000);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const PLACEHOLDER = "[An earlier answer based on application data, not shown here.]";
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @param {{ store: ReturnType<typeof defineConversationStore>, retentionDays?: number, maxModelMessages?: number, now?: () => Date }} options
|
|
112
|
+
*/
|
|
113
|
+
export function createConversationHistory({ store, retentionDays = DEFAULT_RETENTION_DAYS, maxModelMessages = 6, now = () => new Date() }) {
|
|
114
|
+
if (!isConversationStore(store)) throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "createConversationHistory needs a store from defineConversationStore()");
|
|
115
|
+
if (!Number.isInteger(retentionDays) || retentionDays < MIN_RETENTION_DAYS || retentionDays > MAX_RETENTION_DAYS) {
|
|
116
|
+
throw new AiServiceError(ErrorCodes.CONFIG_INVALID, `retentionDays must be a whole number of days from ${String(MIN_RETENTION_DAYS)} to ${String(MAX_RETENTION_DAYS)}`);
|
|
117
|
+
}
|
|
118
|
+
if (!Number.isInteger(maxModelMessages) || maxModelMessages < 0 || maxModelMessages > 8) throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "maxModelMessages must be 0-8");
|
|
119
|
+
|
|
120
|
+
const scopeOf = (context) => {
|
|
121
|
+
const parsed = parseTrustedContext(context);
|
|
122
|
+
return { applicationId: parsed.applicationId, tenantId: parsed.tenantId, userId: parsed.actor.userId };
|
|
123
|
+
};
|
|
124
|
+
const cutoff = () => new Date(now().getTime() - retentionDays * DAY_MS).toISOString();
|
|
125
|
+
const validConversationId = (id) => typeof id === "string" && CONVERSATION_ID.test(id);
|
|
126
|
+
|
|
127
|
+
/** The scope's messages of one conversation, oldest first; expired or foreign records are dropped. */
|
|
128
|
+
async function load({ context, conversationId, limit = 50 }) {
|
|
129
|
+
if (!validConversationId(conversationId)) return [];
|
|
130
|
+
const scope = scopeOf(context);
|
|
131
|
+
const records = await store.listMessages({ scope, conversationId, limit: Math.min(Math.max(1, limit), 200) });
|
|
132
|
+
const oldest = cutoff();
|
|
133
|
+
const messages = [];
|
|
134
|
+
for (const record of Array.isArray(records) ? records : []) {
|
|
135
|
+
const parsed = conversationMessageSchema.safeParse(record);
|
|
136
|
+
if (!parsed.success || !sameScope(parsed.data, scope) || parsed.data.conversationId !== conversationId) continue;
|
|
137
|
+
messages.push(parsed.data);
|
|
138
|
+
}
|
|
139
|
+
if (messages.length > 0 && messages[messages.length - 1].createdAt < oldest) return [];
|
|
140
|
+
return messages;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function append({ context, conversationId, role, text, blocks = [], route = null, status = null, modelVisible, appData }) {
|
|
144
|
+
const scope = scopeOf(context);
|
|
145
|
+
if (!validConversationId(conversationId)) throw new AiServiceError(ErrorCodes.INPUT_INVALID, "Invalid conversation id");
|
|
146
|
+
if (appData !== undefined && Buffer.byteLength(JSON.stringify(appData) ?? "", "utf8") > APP_DATA_MAX_BYTES) throw new AiServiceError(ErrorCodes.INPUT_INVALID, "appData is too large", { details: { maxBytes: APP_DATA_MAX_BYTES } });
|
|
147
|
+
const message = conversationMessageSchema.parse({ messageId: randomUUID(), conversationId, ...scope, role, text: String(text).slice(0, 6000), blocks, route, status, modelVisible, ...(appData === undefined ? {} : { appData }), createdAt: now().toISOString() });
|
|
148
|
+
await store.appendMessage(message);
|
|
149
|
+
return message;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Recent messages as the model may see them: data-based answers replaced by a placeholder. */
|
|
153
|
+
function forModel(messages) {
|
|
154
|
+
return messages
|
|
155
|
+
.slice(-maxModelMessages)
|
|
156
|
+
.map((message) => ({ role: message.role, text: (message.role === "assistant" && !message.modelVisible ? PLACEHOLDER : message.text).slice(0, 2000) }))
|
|
157
|
+
.filter((message) => message.text.trim() !== "");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return Object.freeze({
|
|
161
|
+
retentionDays,
|
|
162
|
+
newConversationId: () => randomUUID(),
|
|
163
|
+
isConversationId: validConversationId,
|
|
164
|
+
load,
|
|
165
|
+
append,
|
|
166
|
+
forModel,
|
|
167
|
+
list: async ({ context, limit = 20 }) => {
|
|
168
|
+
const scope = scopeOf(context);
|
|
169
|
+
const rows = await store.listConversations({ scope, limit: Math.min(Math.max(1, limit), 100) });
|
|
170
|
+
return (Array.isArray(rows) ? rows : []).filter((row) => validConversationId(row?.conversationId) && row.updatedAt >= cutoff()).map((row) => ({ conversationId: row.conversationId, title: String(row.title ?? "").slice(0, 80), updatedAt: row.updatedAt }));
|
|
171
|
+
},
|
|
172
|
+
remove: ({ context, conversationId }) => (validConversationId(conversationId) ? store.deleteConversation({ scope: scopeOf(context), conversationId }) : false),
|
|
173
|
+
/** Deletes every conversation inactive for longer than the retention period. Schedule it daily. */
|
|
174
|
+
cleanupExpiredConversations: async () => ({ deleted: Number(await store.deleteInactiveSince({ cutoff: cutoff() })) || 0, cutoff: cutoff() }),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Runs cleanup now and every intervalMs on an unref'd timer; returns { stop }. */
|
|
179
|
+
export function scheduleConversationCleanup({ history, intervalMs = 6 * 3_600_000, onResult, onError } = {}) {
|
|
180
|
+
if (typeof history?.cleanupExpiredConversations !== "function") throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "scheduleConversationCleanup needs a conversation history");
|
|
181
|
+
if (!Number.isInteger(intervalMs) || intervalMs < 60_000) throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "intervalMs must be at least one minute");
|
|
182
|
+
const run = () =>
|
|
183
|
+
history.cleanupExpiredConversations().then(
|
|
184
|
+
(result) => onResult?.(result),
|
|
185
|
+
(error) => onError?.(error),
|
|
186
|
+
);
|
|
187
|
+
run();
|
|
188
|
+
const timer = globalThis.setInterval(run, intervalMs);
|
|
189
|
+
timer.unref?.();
|
|
190
|
+
return Object.freeze({ stop: () => globalThis.clearInterval(timer) });
|
|
191
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { promises as fs } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { AiServiceError } from "../core/Errors.js";
|
|
5
|
+
import { ErrorCodes } from "../core/Errors.js";
|
|
6
|
+
import { defineConversationStore } from "./ConversationHistory.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A simple local conversation store: one private file per conversation under
|
|
10
|
+
* <root>/<hash(applicationId)>/<hash(tenantId)>/<hash(userId)>/<conversationId>.json (folders 0700, files
|
|
11
|
+
* 0600, atomic writes).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const CONVERSATION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
15
|
+
const hash = (value) => createHash("sha256").update(String(value)).digest("hex").slice(0, 32);
|
|
16
|
+
|
|
17
|
+
export function createFolderConversationStore({ root }) {
|
|
18
|
+
if (typeof root !== "string" || root.trim() === "") throw new AiServiceError(ErrorCodes.CONFIG_INVALID, "A conversations root folder is required");
|
|
19
|
+
const base = resolve(root);
|
|
20
|
+
const folderOf = (scope) => join(base, hash(scope.applicationId), hash(scope.tenantId), hash(scope.userId));
|
|
21
|
+
const fileOf = (scope, conversationId) => {
|
|
22
|
+
if (!CONVERSATION_ID.test(conversationId)) throw new AiServiceError(ErrorCodes.INPUT_INVALID, "Invalid conversation id");
|
|
23
|
+
return join(folderOf(scope), `${conversationId}.json`);
|
|
24
|
+
};
|
|
25
|
+
const read = async (file) => {
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(await fs.readFile(file, "utf8"));
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error?.code === "ENOENT") return null;
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const write = async (file, value) => {
|
|
34
|
+
const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;
|
|
35
|
+
await fs.writeFile(temporary, JSON.stringify(value), { mode: 0o600, flag: "wx" });
|
|
36
|
+
await fs.rename(temporary, file);
|
|
37
|
+
};
|
|
38
|
+
/** Every conversation file under root, without following links. */
|
|
39
|
+
async function* walk(dir, depth = 0) {
|
|
40
|
+
let entries;
|
|
41
|
+
try {
|
|
42
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error?.code === "ENOENT") return;
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
for (const entry of entries) {
|
|
48
|
+
const path = join(dir, entry.name);
|
|
49
|
+
if (entry.isDirectory() && depth < 3) yield* walk(path, depth + 1);
|
|
50
|
+
else if (entry.isFile() && depth === 3 && entry.name.endsWith(".json")) yield path;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return defineConversationStore({
|
|
55
|
+
async appendMessage(message) {
|
|
56
|
+
const scope = { applicationId: message.applicationId, tenantId: message.tenantId, userId: message.userId };
|
|
57
|
+
await fs.mkdir(folderOf(scope), { recursive: true, mode: 0o700 });
|
|
58
|
+
const file = fileOf(scope, message.conversationId);
|
|
59
|
+
const current = (await read(file)) ?? { conversationId: message.conversationId, messages: [] };
|
|
60
|
+
current.messages.push(message);
|
|
61
|
+
current.updatedAt = message.createdAt;
|
|
62
|
+
await write(file, current);
|
|
63
|
+
},
|
|
64
|
+
async listMessages({ scope, conversationId, limit }) {
|
|
65
|
+
const current = await read(fileOf(scope, conversationId));
|
|
66
|
+
return current ? current.messages.slice(-limit) : [];
|
|
67
|
+
},
|
|
68
|
+
async listConversations({ scope, limit }) {
|
|
69
|
+
const rows = [];
|
|
70
|
+
let names = [];
|
|
71
|
+
try {
|
|
72
|
+
names = await fs.readdir(folderOf(scope));
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (error?.code !== "ENOENT") throw error;
|
|
75
|
+
}
|
|
76
|
+
for (const name of names.filter((entry) => CONVERSATION_ID.test(entry.replace(/\.json$/, "")))) {
|
|
77
|
+
const current = await read(join(folderOf(scope), name));
|
|
78
|
+
if (current) rows.push({ conversationId: current.conversationId, title: (current.messages.find((message) => message.role === "user")?.text ?? "").slice(0, 80), updatedAt: current.updatedAt });
|
|
79
|
+
}
|
|
80
|
+
return rows.sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : -1)).slice(0, limit);
|
|
81
|
+
},
|
|
82
|
+
async deleteConversation({ scope, conversationId }) {
|
|
83
|
+
try {
|
|
84
|
+
await fs.unlink(fileOf(scope, conversationId));
|
|
85
|
+
return true;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (error?.code === "ENOENT") return false;
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
async deleteInactiveSince({ cutoff }) {
|
|
92
|
+
let deleted = 0;
|
|
93
|
+
for await (const file of walk(base)) {
|
|
94
|
+
const current = await read(file);
|
|
95
|
+
if (!current || typeof current.updatedAt !== "string" || current.updatedAt < cutoff) {
|
|
96
|
+
await fs.unlink(file).catch(() => {});
|
|
97
|
+
deleted += 1;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return deleted;
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|