purchase-mcp-server
Version:
Purchase and budget management server handling requisitions, purchase orders, expenses, budgets, and vendor management with ERP access for data extraction
108 lines • 4.27 kB
JavaScript
import { Client } from "typesense";
const typesenseClient = new Client({
nodes: [
{
host: "j51ouydaces0i2m7p-1.a1.typesense.net",
port: 443,
protocol: "https"
}
],
apiKey: "wgTfJoajCRXWWNlLILAdZh1bU66RA4wv"
});
import { convertUnixDates, getDataLink, getArtifact } from "./utils/helper_functions.js";
export async function processTypesenseResults(searchResult, toolName, title, session_id = "testing", linkHeader, artifactTitle) {
try {
if (!searchResult || !searchResult.hits || searchResult.hits.length === 0) {
return [{
type: "text",
text: "No records found for the specified criteria.",
title: "No Results Found",
format: "json"
}];
}
// console.log(searchResult);
// Process search results into the standard format
const hits = searchResult.hits || [];
// console.log("========hits========", hits);
const documents = await Promise.all(hits.map(async (hit) => {
if (!hit.document) {
console.warn(`Hit is missing document property in ${toolName}`);
return {};
}
// Create a shallow copy of the document
const document = { ...hit.document };
// console.log("========document inside before========", document);
// Remove embedding field to reduce response size
if (document.embedding) {
delete document.embedding;
}
// Convert Unix timestamps to readable dates
return await convertUnixDates(document);
}));
// Get data link
const dataLink = await getDataLink(documents);
// Get vessel name and IMO from hits
let vesselName = null;
let imo = null;
try {
vesselName = searchResult.hits[0]?.document?.vesselName;
imo = searchResult.hits[0]?.document?.imo;
}
catch (error) {
console.warn(`Could not get vessel name or IMO from hits in ${toolName}`);
}
// Insert the data link to mongodb collection
// await insertDataLinkToMongoDB(dataLink, linkHeader, session_id, imo || "", vesselName || "");
console.log("========documents after getDataLink========", documents);
// Format results in the standard structure
const formattedResults = {
found: searchResult.found || 0,
out_of: searchResult.out_of || 0,
page: searchResult.page || 1,
hits: documents
};
console.log("========formattedResults========", formattedResults);
// Get artifact data
const artifactData = await getArtifact(toolName, dataLink);
// console.log(artifactData);
// Create content response
const content = {
type: "text",
text: JSON.stringify(formattedResults, null, 2),
title,
format: "json"
};
console.log("========content========", content);
// Create artifact response
// const artifact: TextContent = {
// type: "text",
// text: JSON.stringify(artifactData, null, 2),
// title: artifactTitle || title,
// format: "json"
// };
// console.log(artifact);
return [content];
}
catch (error) {
console.error(`Error processing Typesense results for ${toolName}:`, error);
return [{
type: "text",
text: `Error processing results: ${error.message}`,
title: "Error",
format: "json"
}];
}
}
// test the function
const test = async () => {
const searchResult = await typesenseClient.collections('purchase').documents().search({
q: "bw kizoku",
query_by: "vesselName",
sort_by: "purchaseRequisitionDate:desc",
limit: 3
});
const toolResponse = await processTypesenseResults(searchResult, "purchase", "Purchase", "123", "https://www.google.com", "Purchase");
console.log(toolResponse);
};
test();
//# sourceMappingURL=test-typesense-results.js.map