@absolutejs/rag 0.0.11 → 0.0.12

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.
@@ -21714,6 +21714,7 @@ var MAX_INGEST_JOBS = 20;
21714
21714
  var MAX_ADMIN_ACTIONS = 20;
21715
21715
  var MAX_ADMIN_JOBS = 20;
21716
21716
  var DEFAULT_STALE_AFTER_MS = 1000 * 60 * 60 * 24 * 7;
21717
+ var WEBSOCKET_OPEN = 1;
21717
21718
  var REQUEST_USER_SUB_HEADER = "x-absolutejs-user-sub";
21718
21719
  var HTML_HEADERS = { "Content-Type": "text/html; charset=utf-8" };
21719
21720
  var defaultParseProvider = (content) => {
@@ -22157,9 +22158,23 @@ var persistSearchTraceIfConfigured = async (input) => {
22157
22158
  await input.onPrune?.(input.retention);
22158
22159
  }
22159
22160
  };
22160
- var buildRAGContextFromQuery = async (config, topK, scoreThreshold, queryText, ragModel, embedding, embeddingModel) => {
22161
+ var sendWebSocketError = (ws, error, conversationId, messageId) => {
22162
+ if (typeof ws.readyState === "number" && ws.readyState !== WEBSOCKET_OPEN) {
22163
+ return;
22164
+ }
22165
+ try {
22166
+ ws.send(JSON.stringify({
22167
+ conversationId,
22168
+ message: error instanceof Error ? error.message : String(error),
22169
+ messageId,
22170
+ type: "error"
22171
+ }));
22172
+ } catch {}
22173
+ };
22174
+ var getProviderDefaultEmbeddingModel = (embedding) => embedding !== undefined && typeof embedding === "object" && typeof embedding.defaultModel === "string" ? embedding.defaultModel : undefined;
22175
+ var buildRAGContextFromQuery = async (config, topK, scoreThreshold, queryText, embedding, embeddingModel) => {
22161
22176
  const collection = config.collection ?? (config.ragStore ? createRAGCollection({
22162
- defaultModel: embeddingModel ?? ragModel,
22177
+ defaultModel: embeddingModel,
22163
22178
  defaultTopK: topK,
22164
22179
  embedding,
22165
22180
  rerank: config.rerank,
@@ -22172,7 +22187,7 @@ var buildRAGContextFromQuery = async (config, topK, scoreThreshold, queryText, r
22172
22187
  };
22173
22188
  }
22174
22189
  const queried = await collection.searchWithTrace({
22175
- model: embeddingModel ?? ragModel,
22190
+ model: embeddingModel,
22176
22191
  query: queryText,
22177
22192
  scoreThreshold,
22178
22193
  topK
@@ -22195,7 +22210,27 @@ var buildRAGContextFromQuery = async (config, topK, scoreThreshold, queryText, r
22195
22210
  trace: queried.trace
22196
22211
  };
22197
22212
  };
22213
+ var assertEmbeddingModelConfigured = (config) => {
22214
+ if (config.collection !== undefined) {
22215
+ return;
22216
+ }
22217
+ if (config.ragStore === undefined) {
22218
+ return;
22219
+ }
22220
+ if (config.embeddingModel !== undefined) {
22221
+ return;
22222
+ }
22223
+ const { embedding } = config;
22224
+ if (embedding === undefined || typeof embedding !== "object") {
22225
+ return;
22226
+ }
22227
+ if (getProviderDefaultEmbeddingModel(embedding) !== undefined) {
22228
+ return;
22229
+ }
22230
+ throw new Error("ragChat: an embedding provider is configured but no embedding model is set. " + "Pass `embeddingModel` to ragChat() or set `defaultModel` on the embedding provider. " + "The chat model cannot be used for embeddings.");
22231
+ };
22198
22232
  var ragChat = (config) => {
22233
+ assertEmbeddingModelConfigured(config);
22199
22234
  const path = config.path ?? DEFAULT_PATH;
22200
22235
  const authorizeRAGAction = config.authorizeRAGAction;
22201
22236
  const resolveRAGAccessScope = config.resolveRAGAccessScope;
@@ -22657,53 +22692,57 @@ var ragChat = (config) => {
22657
22692
  }));
22658
22693
  };
22659
22694
  const handleMessage = async (ws, rawContent, rawConversationId, rawAttachments) => {
22660
- const parsed = parseProvider(rawContent);
22661
- const { content, providerName } = parsed;
22662
- const userMessageId = generateId2();
22663
22695
  const assistantMessageId = generateId2();
22664
22696
  const conversationId = rawConversationId ?? generateId2();
22665
- const conversation = await store.getOrCreate(conversationId);
22666
- const history = buildHistory(conversation);
22667
- const model = resolveModel(config, parsed);
22668
- const ragModel = parsed.model ?? model;
22669
- appendMessage(conversation, {
22670
- attachments: rawAttachments,
22671
- content,
22672
- conversationId,
22673
- id: userMessageId,
22674
- role: "user",
22675
- timestamp: Date.now()
22676
- });
22677
- await store.set(conversationId, conversation);
22678
- const retrievalStartedAt = Date.now();
22679
- handleRAGRetrieving(ws, conversationId, assistantMessageId, retrievalStartedAt);
22680
- const provider = config.provider(providerName);
22681
- const rag = await buildRAGContextFromQuery(config, topK, scoreThreshold, content, ragModel, config.embedding, config.embeddingModel);
22682
- const controller = new AbortController;
22683
- abortControllers.set(conversationId, controller);
22684
- const { ragContext, sources, trace } = rag;
22685
- const retrievedAt = Date.now();
22686
- const retrievalDurationMs = retrievedAt - retrievalStartedAt;
22687
- handleRAGRetrieved(ws, conversationId, assistantMessageId, sources, retrievalStartedAt, retrievedAt, retrievalDurationMs, trace);
22688
- await streamAI(ws, conversationId, assistantMessageId, {
22689
- completeMeta: includeCompleteSources ? { sources } : undefined,
22690
- maxTurns: config.maxTurns,
22691
- messages: [
22692
- ...history,
22693
- buildUserMessage(content, rawAttachments, ragContext)
22694
- ],
22695
- model,
22696
- provider,
22697
- signal: controller.signal,
22698
- systemPrompt: config.systemPrompt,
22699
- thinking: resolveThinking(config, providerName, model),
22700
- tools: resolveTools(config, providerName, model),
22701
- onComplete: async (fullResponse, usage) => {
22702
- await appendAssistantMessage(conversationId, assistantMessageId, fullResponse, sources, usage, model, retrievalStartedAt, retrievedAt, retrievalDurationMs, trace);
22703
- abortControllers.delete(conversationId);
22704
- config.onComplete?.(conversationId, fullResponse, usage, sources);
22705
- }
22706
- });
22697
+ try {
22698
+ const parsed = parseProvider(rawContent);
22699
+ const { content, providerName } = parsed;
22700
+ const userMessageId = generateId2();
22701
+ const conversation = await store.getOrCreate(conversationId);
22702
+ const history = buildHistory(conversation);
22703
+ const model = resolveModel(config, parsed);
22704
+ appendMessage(conversation, {
22705
+ attachments: rawAttachments,
22706
+ content,
22707
+ conversationId,
22708
+ id: userMessageId,
22709
+ role: "user",
22710
+ timestamp: Date.now()
22711
+ });
22712
+ await store.set(conversationId, conversation);
22713
+ const retrievalStartedAt = Date.now();
22714
+ handleRAGRetrieving(ws, conversationId, assistantMessageId, retrievalStartedAt);
22715
+ const provider = config.provider(providerName);
22716
+ const rag = await buildRAGContextFromQuery(config, topK, scoreThreshold, content, config.embedding, config.embeddingModel);
22717
+ const controller = new AbortController;
22718
+ abortControllers.set(conversationId, controller);
22719
+ const { ragContext, sources, trace } = rag;
22720
+ const retrievedAt = Date.now();
22721
+ const retrievalDurationMs = retrievedAt - retrievalStartedAt;
22722
+ handleRAGRetrieved(ws, conversationId, assistantMessageId, sources, retrievalStartedAt, retrievedAt, retrievalDurationMs, trace);
22723
+ await streamAI(ws, conversationId, assistantMessageId, {
22724
+ completeMeta: includeCompleteSources ? { sources } : undefined,
22725
+ maxTurns: config.maxTurns,
22726
+ messages: [
22727
+ ...history,
22728
+ buildUserMessage(content, rawAttachments, ragContext)
22729
+ ],
22730
+ model,
22731
+ provider,
22732
+ signal: controller.signal,
22733
+ systemPrompt: config.systemPrompt,
22734
+ thinking: resolveThinking(config, providerName, model),
22735
+ tools: resolveTools(config, providerName, model),
22736
+ onComplete: async (fullResponse, usage) => {
22737
+ await appendAssistantMessage(conversationId, assistantMessageId, fullResponse, sources, usage, model, retrievalStartedAt, retrievedAt, retrievalDurationMs, trace);
22738
+ abortControllers.delete(conversationId);
22739
+ config.onComplete?.(conversationId, fullResponse, usage, sources);
22740
+ }
22741
+ });
22742
+ } catch (error) {
22743
+ abortControllers.delete(conversationId);
22744
+ sendWebSocketError(ws, error, conversationId, assistantMessageId);
22745
+ }
22707
22746
  };
22708
22747
  const resolveCollection = () => config.collection ?? (ragStore ? createRAGCollection({
22709
22748
  defaultModel: config.embeddingModel,
@@ -29422,7 +29461,6 @@ var ragChat = (config) => {
29422
29461
  const parsed = parseProvider(lastMessage.content);
29423
29462
  const { content, providerName } = parsed;
29424
29463
  const model = resolveModel(config, parsed);
29425
- const ragModel = parsed.model ?? model;
29426
29464
  const assistantMessageId = generateId2();
29427
29465
  const retrievalStartedAt = Date.now();
29428
29466
  yield {
@@ -29434,7 +29472,7 @@ var ragChat = (config) => {
29434
29472
  event: "retrieval"
29435
29473
  };
29436
29474
  const provider = config.provider(providerName);
29437
- const { ragContext, sources, trace } = await buildRAGContextFromQuery(config, topK, scoreThreshold, content, ragModel, config.embedding, config.embeddingModel);
29475
+ const { ragContext, sources, trace } = await buildRAGContextFromQuery(config, topK, scoreThreshold, content, config.embedding, config.embeddingModel);
29438
29476
  const retrievedAt = Date.now();
29439
29477
  const retrievalDurationMs = retrievedAt - retrievalStartedAt;
29440
29478
  yield {
@@ -29485,16 +29523,20 @@ var ragChat = (config) => {
29485
29523
  if (!message) {
29486
29524
  return;
29487
29525
  }
29488
- if (message.type === "cancel") {
29489
- handleCancel(message.conversationId);
29490
- return;
29491
- }
29492
- if (message.type === "branch") {
29493
- await handleBranch(ws, message.messageId, message.conversationId);
29494
- return;
29495
- }
29496
- if (message.type === "message") {
29497
- await handleMessage(ws, message.content, message.conversationId, message.attachments);
29526
+ try {
29527
+ if (message.type === "cancel") {
29528
+ handleCancel(message.conversationId);
29529
+ return;
29530
+ }
29531
+ if (message.type === "branch") {
29532
+ await handleBranch(ws, message.messageId, message.conversationId);
29533
+ return;
29534
+ }
29535
+ if (message.type === "message") {
29536
+ await handleMessage(ws, message.content, message.conversationId, message.attachments);
29537
+ }
29538
+ } catch (error) {
29539
+ sendWebSocketError(ws, error, "conversationId" in message ? message.conversationId : undefined);
29498
29540
  }
29499
29541
  }
29500
29542
  }).post(`${path}/search`, async ({ body, request, set }) => {
@@ -31977,7 +32019,9 @@ var toErrorMessage3 = async (response, label) => {
31977
32019
  return new Error(`${label}: ${response.status} ${response.statusText}${detailMessage ? ` (${detailMessage})` : ""}`);
31978
32020
  };
31979
32021
  var normalizeString = (value) => typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
31980
- var toUniqueStringArray = (values) => [...new Set(values.filter((value) => typeof value === "string" && value.length > 0))];
32022
+ var toUniqueStringArray = (values) => [
32023
+ ...new Set(values.filter((value) => typeof value === "string" && value.length > 0))
32024
+ ];
31981
32025
  var toContactTitle = (person, fallbackId) => {
31982
32026
  const primaryName = person.names?.find((name) => normalizeString(name.displayName) !== undefined);
31983
32027
  const displayName = normalizeString(primaryName?.displayName);
@@ -37605,5 +37649,5 @@ export {
37605
37649
  addRAGEvaluationSuiteCase
37606
37650
  };
37607
37651
 
37608
- //# debugId=20E465BEF0B96FD164756E2164756E21
37652
+ //# debugId=3E3026F16C216BA364756E2164756E21
37609
37653
  //# sourceMappingURL=index.js.map