site-library/src/base/kbot.ts
2025-03-25 20:50:32 +01:00

143 lines
5.0 KiB
TypeScript

import { sync as read } from "@polymech/fs/read"
import { sync as exists } from "@polymech/fs/exists"
import { run, OptionsSchema, IKBotTask } from "@polymech/kbot-d";
import { language_template } from "./kbot-prompts.js";
export interface Props extends IKBotTask {
language?: string;
clazz?: string;
cache?: boolean;
disabled?: boolean;
template?: string;
renderer?: string;
}
/////////////////////////////////////////////////////////////
//
// Templates
export const template_simple = () => {
return {
router: "openai",
model: "gpt-4o",
preferences: "none",
mode: "completion",
};
}
export const keywords_simple = () => {
return {
_router: "openai",
model: "google/gemini-exp-1206:free",
preferences: "none",
mode: "completion",
prompt: "Return a list of max. 10 keywords that can be used for SEO purposes, separated by commas (dont comment, just the list) : "
};
}
export const references_simple = () => {
return {
_router: "openai",
model: "perplexity/sonar-deep-research",
preferences: "none",
mode: "completion",
prompt: "Return a list of useful references (only with links), as Markdown, grouped : Articles, Papers, Youtube, Software, at least 5 per category, ... Dont comment ! No references to PreciousPlastic or OneArmy!!!",
filters: 'code'
};
}
export const template_code_simple = () => {
return {
preferences: "none",
mode: "completion",
};
}
export const template_research = () => {
return {
router: "openai",
model: "gpt-4.5-preview",
preferences: "none",
mode: "completion",
}
}
export const extract_tools_and_hardware = () => {
return {
router_: "openai",
model: "perplexity/sonar-deep-research",
preferences: "none",
mode: "completion",
prompt: "Extract the required tools, software hardware from the following tutorial.Return as Markdown chapters (H3) with very short bullet points (not bold), with links, max. 5.",
filters: "code"
};
}
export const extract_required_skills = () => {
return {
router: "openai",
model: "gpt-4o",
preferences: "none",
mode: "completion",
prompt: "Analyze the following tutorial and identify all the skills that a person would need in order to complete the project. Return as JSON with this structure:\n\n{\n \"skills\": [\n {\n \"name\": \"Skill name\", \n \"level\": \"Beginner, Intermediate, or Advanced\", \n \"description\": \"Brief description of where/how this skill is needed\"\n }\n ],\n \"prerequisiteKnowledge\": [\n \"Background knowledge or familiarity with concepts\"\n ],\n \"safetyConsiderations\": [\n \"Any safety considerations or precautions needed\"\n ]\n}\n\nReturn only the JSON. No introductions or explanations.",
filters: "code"
};
}
export const extract_learned_skills = () => {
return {
router: "openai",
model: "gpt-4o",
preferences: "none",
mode: "completion",
prompt: "Analyze the following tutorial and identify all the skills that a person would learn or improve by completing this project, modest, humble, simple - dont enflate (sustainable, recylcing, ...), Return as Markdown chapter (H2) with very short bullet points (not bold), max. 5.",
filters: "code"
};
}
export const local = () => {
return {
model: "perplexity/sonar-deep-research",
preferences: "none",
mode: "completion",
prompt: "Markdown chapter (h4) with a list of local resources, services & suppliers, max. 5, with links,group by category. dont comment, just the list",
filters: "code"
};
}
export const templates = {
simple: template_simple,
code_simple: template_code_simple,
research: template_research,
howto: language_template,
keywords: keywords_simple,
references: references_simple,
tools_and_hardware: extract_tools_and_hardware,
required_skills: extract_required_skills,
learned_skills: extract_learned_skills,
local: local
}
/**
*
* @param content : content to filter
* @param tpl :
* @param opts
* @returns
*/
export const filter = async (content: string, tpl: string = 'howto', opts: any = {}) => {
if (!content || content.length < 20 || templates[tpl] === undefined) {
return content;
}
const template = templates[tpl]();
// 🔸 Separate the options hash
const options = getFilterOptions(content, template, opts);
let result: string | unknown[] = [];
result = await run(options);
return result[0] as string;
};
// 🔸 Expose this for caching or debugging
export const getFilterOptions = (content: string, template: any, opts: any = {}) => {
return OptionsSchema().parse({
...template,
prompt: `${template.prompt || ""} : ${content}`,
...opts,
});
};