import * as crypto from "node:crypto";
import { createKnowledgeConnector } from "@cognigy/extension-tools";
export const myKnowledgeImport = createKnowledgeConnector({
type: "myKnowledgeImport",
label: "My Knowledge Import",
summary: "My own Knowledge Extension",
fields: [
{
key: "name",
label: "Name",
type: "text",
params: {
required: true,
},
},
{
key: "color",
label: "Color",
type: "text",
params: {
required: true,
},
},
] as const,
function: async ({ config, api, sources }) => {
const text = `The color of ${config.name} is ${config.color}`;
const contentHash = crypto.hash("sha256", text, "hex");
const knowledgeSource = await api.upsertKnowledgeSource({
name: config.name,
description: "This is my own knowledge source",
tags: ["example"],
chunkCount: 1,
contentHashOrTimestamp: contentHash,
});
if (knowledgeSource) {
await api.createKnowledgeChunk({
knowledgeSourceId: knowledgeSource.knowledgeSourceId,
text,
data: {},
});
}
const createdSources = [config.name];
for (const source of sources) {
if (!createdSources.includes(source.externalIdentifier)) {
await api.deleteKnowledgeSource({
knowledgeSourceId: source.knowledgeSourceId,
});
}
}
},
});