Files
chat/api/app/clients/prompts/createContextHandlers.js
zeekay 7e26aa9653 feat(rag): finish the cutover — retrieval + vector cleanup on the one surface
The first pass moved ingestion; these are the paths that still read RAG_API_URL,
which the universe change removes. Left as-is they would have silently degraded:
file_search and the context handler would stop retrieving, and deleting an S3 object
would orphan its embeddings.

- file_search (api/app/clients/tools/util/fileSearch.js) queries through RagClient;
  `req` is threaded from handleTools (as `options.req`, the same value that file
  already passes to resolveTenantBearer for images) so retrieval runs on the user's
  own IAM bearer instead of a chat-minted short-lived token.
- createContextHandlers uses the client for both shapes (full context + top-k),
  normalised to { data } so its callers are untouched.
- deleting an S3 object now drops the file's chunks via the same client rather than
  the legacy deleteRagFile (which reads RAG_API_URL and would have no-op'd) —
  best-effort, so a retrieval-side failure never blocks the object delete.

Dead code removed with the paths they served: the axios import and createQueryBody
body-builder in fileSearch, the deleteRagFile import in S3 crud.

Tests 11/11 (7 client + 4 key convention; the S3 spec mocks the client). Lint clean
on every touched file — the one remaining handleTools prettier error is pre-existing
(3 on clean main, 1 after this).

packages/api/src/files/{text,rag}.ts and app/checks.ts still name RAG_API_URL; all
three already guard on it and degrade gracefully (native text parsing, no-op delete,
a startup log), so they are inert once the var is gone — they come out with the
package's own pass rather than being half-migrated here.
2026-07-26 14:13:26 -07:00

154 lines
4.2 KiB
JavaScript

const axios = require('axios');
const ragClient = require('~/server/services/RagClient');
const { logger } = require('@hanzochat/data-schemas');
const { isEnabled, generateShortLivedToken } = require('@hanzochat/api');
const footer = `Use the context as your learned knowledge to better answer the user.
In your response, remember to follow these guidelines:
- If you don't know the answer, simply say that you don't know.
- If you are unsure how to answer, ask for clarification.
- Avoid mentioning that you obtained the information from the context.
`;
function createContextHandlers(req, userMessageContent) {
if (!ragClient.ragEnabled()) {
return;
}
const queryPromises = [];
const processedFiles = [];
const processedIds = new Set();
const jwtToken = generateShortLivedToken(req.user.id);
const useFullContext = isEnabled(process.env.RAG_USE_FULL_CONTEXT);
// Both shapes go to the ONE RAG surface (services/RagClient) with the caller's
// own IAM bearer; the response is normalised to `{ data }` so the callers below
// keep reading `.data` exactly as they did from the axios responses.
const query = async (file) => {
if (useFullContext) {
return { data: await ragClient.context(req, { file_id: file.file_id }) };
}
return {
data: await ragClient.query(req, {
query: userMessageContent,
file_id: file.file_id,
k: 4,
}),
};
};
const processFile = async (file) => {
if (file.embedded && !processedIds.has(file.file_id)) {
try {
const promise = query(file);
queryPromises.push(promise);
processedFiles.push(file);
processedIds.add(file.file_id);
} catch (error) {
logger.error(`Error processing file ${file.filename}:`, error);
}
}
};
const createContext = async () => {
try {
if (!queryPromises.length || !processedFiles.length) {
return '';
}
const oneFile = processedFiles.length === 1;
const header = `The user has attached ${oneFile ? 'a' : processedFiles.length} file${
!oneFile ? 's' : ''
} to the conversation:`;
const files = `${
oneFile
? ''
: `
<files>`
}${processedFiles
.map(
(file) => `
<file>
<filename>${file.filename}</filename>
<type>${file.type}</type>
</file>`,
)
.join('')}${
oneFile
? ''
: `
</files>`
}`;
const resolvedQueries = await Promise.all(queryPromises);
const context =
resolvedQueries.length === 0
? '\n\tThe semantic search did not return any results.'
: resolvedQueries
.map((queryResult, index) => {
const file = processedFiles[index];
let contextItems = queryResult.data;
const generateContext = (currentContext) =>
`
<file>
<filename>${file.filename}</filename>
<context>${currentContext}
</context>
</file>`;
if (useFullContext) {
return generateContext(`\n${contextItems}`);
}
contextItems = queryResult.data
.map((item) => {
const pageContent = item[0].page_content;
return `
<contextItem>
<![CDATA[${pageContent?.trim()}]]>
</contextItem>`;
})
.join('');
return generateContext(contextItems);
})
.join('');
if (useFullContext) {
const prompt = `${header}
${context}
${footer}`;
return prompt;
}
const prompt = `${header}
${files}
A semantic search was executed with the user's message as the query, retrieving the following context inside <context></context> XML tags.
<context>${context}
</context>
${footer}`;
return prompt;
} catch (error) {
logger.error('Error creating context:', error);
throw error;
}
};
return {
processFile,
createContext,
};
}
module.exports = createContextHandlers;