Project Files
dist / toolsProvider.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolsProvider = toolsProvider;
// src/toolsProvider.ts
const sdk_1 = require("@lmstudio/sdk");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const zod_1 = require("zod");
const fs_1 = require("fs");
const child_process_1 = require("child_process");
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
// โโโ ุงูู
ุณุงุฑุงุช โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const MEMORY_BASE = "D:/bido/2026/bido_memory";
const SESSIONS_DIR = (0, path_1.join)(MEMORY_BASE, "dynamic_memory/sessions");
const PULSES_DIR = (0, path_1.join)(MEMORY_BASE, "dynamic_memory/pulses"); // ุชู
ุงูุชุนุฏูู ุฅูู ุญุฑู ุตุบูุฑ
const NODES_DIR = (0, path_1.join)(MEMORY_BASE, "dynamic_memory/nodes");
const INDICES_DIR = (0, path_1.join)(MEMORY_BASE, "dynamic_memory/indices");
const RAW_SESSIONS_DIR = (0, path_1.join)(MEMORY_BASE, "raw_sessions");
// โโโ ุฃุฏูุงุช ู
ุณุงุนุฏุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function ensureDir(dir) {
try {
await (0, promises_1.mkdir)(dir, { recursive: true });
}
catch { }
}
async function readJSON(filePath) {
try {
const content = await (0, promises_1.readFile)(filePath, "utf-8");
return JSON.parse(content);
}
catch {
return null;
}
}
async function writeJSON(filePath, data) {
await ensureDir((0, path_1.dirname)(filePath));
await (0, promises_1.writeFile)(filePath, JSON.stringify(data, null, 2), "utf-8");
}
// โโโ ุจูุงุก ุงูููุงุฑุณ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
function buildIndicesFromData(pulses, nodes) {
const tag = {};
const time = {};
const weight = {};
const graph = {};
for (const pulse of pulses) {
const id = pulse.id;
for (const t of pulse.tags || []) {
if (!tag[t])
tag[t] = [];
if (!tag[t].includes(id))
tag[t].push(id);
}
if (pulse.timestamp) {
const date = pulse.timestamp.split("T")[0];
if (!time[date])
time[date] = [];
if (!time[date].includes(id))
time[date].push(id);
}
if (pulse.weight !== undefined) {
const w = pulse.weight.toFixed(2);
if (!weight[w])
weight[w] = [];
if (!weight[w].includes(id))
weight[w].push(id);
}
for (const link of pulse.links || []) {
if (!graph[id])
graph[id] = { links_to: [], linked_from: [] };
if (!graph[id].links_to.includes(link))
graph[id].links_to.push(link);
if (!graph[link])
graph[link] = { links_to: [], linked_from: [] };
if (!graph[link].linked_from.includes(id))
graph[link].linked_from.push(id);
}
}
for (const node of nodes) {
const id = node.id;
for (const t of node.tags || []) {
if (!tag[t])
tag[t] = [];
if (!tag[t].includes(id))
tag[t].push(id);
}
if (node.weight !== undefined) {
const w = node.weight.toFixed(2);
if (!weight[w])
weight[w] = [];
if (!weight[w].includes(id))
weight[w].push(id);
}
for (const link of node.born_from || []) {
if (!graph[id])
graph[id] = { links_to: [], linked_from: [] };
if (!graph[id].links_to.includes(link))
graph[id].links_to.push(link);
if (!graph[link])
graph[link] = { links_to: [], linked_from: [] };
if (!graph[link].linked_from.includes(id))
graph[link].linked_from.push(id);
}
for (const link of node.related_nodes || []) {
if (!graph[id])
graph[id] = { links_to: [], linked_from: [] };
if (!graph[id].links_to.includes(link))
graph[id].links_to.push(link);
if (!graph[link])
graph[link] = { links_to: [], linked_from: [] };
if (!graph[link].linked_from.includes(id))
graph[link].linked_from.push(id);
}
}
return { tag, time, weight, graph };
}
// โโโ ุงูุฃุฏูุงุช ุงูุฌุฏูุฏุฉ (eagleEye, searchFiles, ocrImage) โโโโโโโโโโโโโโโโโโ
// ========== 1. ุนูู ุงูุทุงุฆุฑ (ุชุญููู ุงูู
ููุงุช) ==========
async function eagleEye(filePath) {
try {
const fullPath = (0, path_1.resolve)(filePath);
const fileStat = await (0, promises_1.stat)(fullPath);
const content = await (0, promises_1.readFile)(fullPath, "utf-8");
const lines = content.split("\n");
const words = content.split(/\s+/);
const chars = content.length;
const suspicious = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g;
const hasSuspicious = suspicious.test(content);
let structure = {};
const ext = (0, path_1.extname)(filePath).toLowerCase();
if (ext === ".html") {
structure = {
divs: (content.match(/<div/g) || []).length,
classes: (content.match(/class=["']([^"']*)["']/g) || []).length,
ids: (content.match(/id=["']([^"']*)["']/g) || []).length,
scripts: (content.match(/<script/g) || []).length,
type: "HTML"
};
}
else if (ext === ".js") {
structure = {
functions: (content.match(/function\s+\w+\s*\(/g) || []).length,
classes: (content.match(/class\s+\w+/g) || []).length,
imports: (content.match(/import\s+.*from/g) || []).length,
type: "JavaScript"
};
}
else if (ext === ".py") {
structure = {
functions: (content.match(/def\s+\w+\s*\(/g) || []).length,
classes: (content.match(/class\s+\w+/g) || []).length,
imports: (content.match(/import\s+\w+/g) || []).length,
type: "Python"
};
}
else if (ext === ".json") {
try {
const parsed = JSON.parse(content);
structure = {
type: "JSON",
keys: Object.keys(parsed).length,
isArray: Array.isArray(parsed)
};
}
catch {
structure = { type: "JSON (ุบูุฑ ุตุงูุญ)" };
}
}
else {
structure = { type: "text", lines: lines.length, words: words.length };
}
return {
success: true,
file: filePath,
size: fileStat.size,
lines: lines.length,
words: words.length,
chars: chars,
hasSuspicious,
structure,
preview: lines.slice(0, 30).join("\n").slice(0, 1500),
recommendation: hasSuspicious ? "โ ๏ธ ูุญุชูู ุนูู ุฑู
ูุฒ ุฎุจูุซุฉ" : "โ
ุขู
ู ูููุฑุงุกุฉ"
};
}
catch (error) {
return { success: false, error: error.message, file: filePath };
}
}
// ========== 2. ุงูุจุญุซ ุงูุฒูู
ุงุฑู (ุจุญุซ ูู ุงูู
ููุงุช) ==========
async function searchFiles(searchPath, query, searchType = "name") {
const results = [];
let scanned = 0;
// ูุชุฌูุจ ุงูู
ุฌูุฏุงุช ุงูู
ุนุฑููุฉ ุจุงูู
ุดุงูู
const excludeDirs = ['$RECYCLE.BIN', 'System Volume Information', 'Windows', 'Program Files', 'Program Files (x86)', 'node_modules', '.git', 'temp', 'tmp'];
async function scan(dir) {
try {
const entries = await (0, promises_1.readdir)(dir, { withFileTypes: true });
for (const entry of entries) {
if (excludeDirs.includes(entry.name))
continue;
if (entry.name.startsWith('.'))
continue;
const fullPath = (0, path_1.join)(dir, entry.name);
if (entry.isDirectory()) {
await scan(fullPath);
}
else {
scanned++;
if (scanned % 100 === 0) {
}
if (searchType === "name") {
if (entry.name.toLowerCase().includes(query.toLowerCase())) {
results.push({
path: fullPath,
name: entry.name,
size: (await (0, promises_1.stat)(fullPath)).size
});
}
}
else if (searchType === "content") {
try {
const content = await (0, promises_1.readFile)(fullPath, "utf-8");
if (content.toLowerCase().includes(query.toLowerCase())) {
results.push({ path: fullPath, name: entry.name });
}
}
catch (e) {
// ูุชุฌุงูุฒ ุฃุฎุทุงุก ุงููุฑุงุกุฉ (ู
ููุงุช ุซูุงุฆูุฉุ ู
ููููุฉุ ุฅูุฎ)
}
}
if (results.length >= 100)
return;
}
}
}
catch (e) {
// ูุชุฌุงูุฒ ุฃู ู
ุฌูุฏ ู
ุด ูุงุฏุฑูู ููุฑุฃู
}
}
try {
await scan(searchPath);
return {
success: true,
results: results.slice(0, 50),
total: results.length,
scanned,
searchType
};
}
catch (error) {
return { success: false, error: error.message };
}
}
// ========== 3. OCR (ุงุณุชุฎุฑุงุฌ ุงููุต ู
ู ุงูุตูุฑ) ==========
async function ocrImage(imagePath, language = "ara+eng") {
try {
const fullPath = (0, path_1.resolve)(imagePath);
try {
await (0, promises_1.access)(fullPath);
}
catch {
return { success: false, error: `ุงูู
ูู ุบูุฑ ู
ูุฌูุฏ: ${imagePath}` };
}
const tesseractPath = "D:\\ocr\\Tesseract-OCR\\tesseract.exe";
try {
await (0, promises_1.access)(tesseractPath);
}
catch {
const { stdout: whichTest } = await execAsync(`where tesseract 2>nul`).catch(() => ({ stdout: "" }));
if (!whichTest.trim()) {
return {
success: false,
error: "Tesseract ุบูุฑ ู
ุซุจุช ุฃู ุบูุฑ ู
ูุฌูุฏ ูู ุงูู
ุณุงุฑ ุงูู
ุญุฏุฏ",
hint: "ุงูู
ุณุงุฑ ุงูู
ุชููุน: D:\\ocr\\Tesseract-OCR\\tesseract.exe"
};
}
}
const { stdout, stderr } = await execAsync(`"${tesseractPath}" "${fullPath}" stdout -l ${language} 2>nul`);
if (stderr && !stderr.includes("Tesseract Open Source")) {
return { success: false, error: stderr };
}
const text = stdout.trim();
if (!text) {
return {
success: false,
error: "ูู
ูุชู
ุงุณุชุฎุฑุงุฌ ุฃู ูุต ู
ู ุงูุตูุฑุฉ",
hint: "ุชุฃูุฏ ู
ู ุฃู ุงูุตูุฑุฉ ุชุญุชูู ุนูู ูุต ูุงุถุญ"
};
}
return {
success: true,
text,
language,
words: text.split(/\s+/).length,
chars: text.length
};
}
catch (error) {
return {
success: false,
error: error.message,
hint: "ุชุฃูุฏ ู
ู ุชุซุจูุช Tesseract ูู D:\\ocr\\Tesseract-OCR\\"
};
}
}
// โโโ ุงูุฃุฏูุงุช ุงูุฑุฆูุณูุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function toolsProvider(ctl) {
const tools = [];
// โโโ 1. memory_build_index (ุงูู
ุนุฏูุฉ) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "memory_build_index",
description: (0, sdk_1.text) `
๐๏ธ ุจูุงุก ููุงุฑุณ ุงูุฐุงูุฑุฉ ู
ู ุฌู
ูุน ุงููุจุถุงุช ูุงูุนูุฏ.
ุชูุฑุฃ ุฌู
ูุน ู
ููุงุช JSON ูู:
- ${PULSES_DIR} (ุฌู
ูุน ุงููุจุถุงุช)
- ${NODES_DIR} (ุฌู
ูุน ุงูุนูุฏ)
ูุชุจูู ุงูููุงุฑุณ ุงูุฃุฑุจุนุฉ ูู:
${INDICES_DIR}
โ ๏ธ ุชุนู
ู ุจู append ููุท: ุชุถูู ุงูุจูุงูุงุช ุงูุฌุฏูุฏุฉุ ูุง ุชุญุฐู ุงููุฏูู
ุฉ.
`,
parameters: {
force: zod_1.z.boolean().default(false).describe("ุฅุนุงุฏุฉ ุจูุงุก ูุงู
ูุฉ (true) ุฃู
ุชุญุฏูุซ ุชุฏุฑูุฌู (false)")
},
implementation: async (args) => {
const { force = false } = args;
// โโโ 1. ูุฑุงุกุฉ ุฌู
ูุน ุงููุจุถุงุช โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const allPulses = [];
try {
const pulseFiles = await (0, promises_1.readdir)(PULSES_DIR);
for (const pf of pulseFiles) {
if (!pf.endsWith(".json"))
continue;
const pulses = await readJSON((0, path_1.join)(PULSES_DIR, pf));
if (pulses && Array.isArray(pulses)) {
allPulses.push(...pulses);
}
else {
}
}
}
catch (error) {
}
// โโโ 2. ูุฑุงุกุฉ ุฌู
ูุน ุงูุนูุฏ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const allNodes = [];
try {
const nodeFiles = await (0, promises_1.readdir)(NODES_DIR);
for (const nf of nodeFiles) {
if (!nf.endsWith(".json"))
continue;
const nodes = await readJSON((0, path_1.join)(NODES_DIR, nf));
if (nodes && Array.isArray(nodes)) {
allNodes.push(...nodes);
}
}
}
catch (error) {
}
// โโโ 3. ุจูุงุก ุงูููุงุฑุณ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const indices = buildIndicesFromData(allPulses, allNodes);
// โโโ 4. ุญูุธ ุงูููุงุฑุณ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
await ensureDir(INDICES_DIR);
await writeJSON((0, path_1.join)(INDICES_DIR, "tag_index.json"), indices.tag);
await writeJSON((0, path_1.join)(INDICES_DIR, "time_index.json"), indices.time);
await writeJSON((0, path_1.join)(INDICES_DIR, "weight_index.json"), indices.weight);
await writeJSON((0, path_1.join)(INDICES_DIR, "connection_graph.json"), indices.graph);
return {
success: true,
message: `โ
ุชู
ุจูุงุก ุงูููุงุฑุณ ุจูุฌุงุญ!`,
stats: {
pulses: allPulses.length,
nodes: allNodes.length,
tags: Object.keys(indices.tag).length,
days: Object.keys(indices.time).length,
weights: Object.keys(indices.weight).length
}
};
}
}));
// โโโ 2. memory_recall (ุงูู
ุนุฏูุฉ ู
ุน ู
ุนููู
ุงุช ุงูุฌูุณุฉ) โโโโโโโโโโโโโโโโโโโโ
// โโโ 2. memory_recall (ุงูู
ุนุฏูุฉ ู
ุน ู
ุนููู
ุงุช ุงูุฌูุณุฉ) โโโโโโโโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "memory_recall",
description: (0, sdk_1.text) `
๐ ุงุณุชุฏุนุงุก ุงูุฐุงูุฑุฉ ู
ู ุงูููุงุฑุณ.
ุชุจุญุซ ูู ุงูููุงุฑุณ ุงูู
ูุฌูุฏุฉ ูู:
${INDICES_DIR}
ูุชุนูุฏ ุงูุนูุฏ ู
ุน ู
ุนููู
ุงุช ุงูุฌูุณุฉ ุงูุฃุตููุฉ (ุงูุงุณู
ุ ุงูู
ุณุงุฑ).
`,
parameters: {
pulse: zod_1.z.string().describe("ูุตู ู
ุง ุชุจุญุซ ุนูู"),
tags: zod_1.z.array(zod_1.z.string()).optional().describe("ููู
ุงุช ู
ูุชุงุญูุฉ"),
limit: zod_1.z.number().default(5).describe("ุนุฏุฏ ุงููุชุงุฆุฌ")
},
implementation: async (args) => {
const { pulse, tags = [], limit = 5 } = args;
const tagIndex = await readJSON((0, path_1.join)(INDICES_DIR, "tag_index.json"));
if (!tagIndex) {
return { success: false, error: "โ ุงูููุงุฑุณ ุบูุฑ ู
ูุฌูุฏุฉ. ูู
ุจุชุดุบูู memory_build_index ุฃููุงู." };
}
// โโโ ุชุญุณูู ุงูุจุญุซ ุจุงูุชุงุฌุฒ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const availableTags = Object.keys(tagIndex);
const validTags = tags.filter(t => availableTags.includes(t));
let results = [];
if (validTags.length > 0) {
for (const tag of validTags) {
const found = tagIndex[tag] || [];
results = results.concat(found);
}
}
else {
const suggestedTags = availableTags.filter(t => pulse.toLowerCase().includes(t.toLowerCase()));
for (const tag of suggestedTags.slice(0, 3)) {
const found = tagIndex[tag] || [];
results = results.concat(found);
}
}
results = [...new Set(results)];
// โโโ ุงุณุชุฑุฌุงุน ุงูุนูุฏ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const nodeFiles = await (0, promises_1.readdir)(NODES_DIR);
const foundNodes = [];
for (const nf of nodeFiles) {
if (!nf.endsWith(".json"))
continue;
const nodes = await readJSON((0, path_1.join)(NODES_DIR, nf));
if (nodes) {
for (const node of nodes) {
if (results.includes(node.id)) {
foundNodes.push(node);
}
}
}
}
foundNodes.sort((a, b) => (b.weight || 0) - (a.weight || 0));
const limited = foundNodes.slice(0, limit);
// โโโ ุฅุถุงูุฉ ู
ุนููู
ุงุช ุงูุฌูุณุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const enrichedNodes = await Promise.all(limited.map(async (n) => {
const sessionId = n.session_id;
let sessionInfo = {
title: "ุบูุฑ ู
ุนุฑูู",
path: null
};
if (sessionId) {
const sessionData = await readJSON((0, path_1.join)(SESSIONS_DIR, `${sessionId}.json`));
if (sessionData) {
sessionInfo = {
title: sessionData.title || "ุบูุฑ ู
ุนุฑูู",
path: (0, path_1.join)(SESSIONS_DIR, `${sessionId}.json`)
};
}
}
return {
...n,
session_title: sessionInfo.title,
session_path: sessionInfo.path
};
}));
return {
success: true,
query: pulse,
total: foundNodes.length,
returned: enrichedNodes.length,
nodes: enrichedNodes
};
}
}));
// โโโ 3. memory_store_session (ุงูู
ุนุฏูุฉ ุจุจุตู
ุฉ ุฒู
ููุฉ) โโโโโโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "memory_store_session",
description: (0, sdk_1.text) `
๐พ ุญูุธ ุฌูุณุฉ ุฌุฏูุฏุฉ ูู ุงูุฐุงูุฑุฉ.
ุชููู
ุจู:
1. ุญูุธ ุงููุต ุงูุฎุงู
ูู: ${RAW_SESSIONS_DIR}/
2. ุญูุธ ุงูุฌูุณุฉ ูู: ${SESSIONS_DIR}/
3. ูุฌุจ ุชุดุบูู memory_build_index ุจุนุฏ ุงูุญูุธ ูุชุญุฏูุซ ุงูููุงุฑุณ.
`,
parameters: {
session_text: zod_1.z.string().describe("ูุต ุงูุฌูุณุฉ ูุงู
ูุงู"),
session_id: zod_1.z.string().optional().describe("ู
ุนุฑู ุงูุฌูุณุฉ (ุงุฎุชูุงุฑู)"),
title: zod_1.z.string().optional().describe("ุนููุงู ุงูุฌูุณุฉ (ุงุฎุชูุงุฑู)")
},
implementation: async (args) => {
const { session_text, session_id, title } = args;
// โโโ ุชูููุฏ ู
ุนุฑู ูุฑูุฏ ุจุจุตู
ุฉ ุฒู
ููุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const now = new Date();
const timestamp = now.toISOString().replace(/[:.]/g, '-');
let id = session_id || `session_${timestamp}`;
// โโโ ุงูุชุฃูุฏ ู
ู ุนุฏู
ุงูุชูุฑุงุฑ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let counter = 1;
let sessionPath = (0, path_1.join)(SESSIONS_DIR, `${id}.json`);
while ((0, fs_1.existsSync)(sessionPath)) {
id = `${session_id || 'session'}_${counter}_${timestamp}`;
sessionPath = (0, path_1.join)(SESSIONS_DIR, `${id}.json`);
counter++;
}
// โโโ ุญูุธ ุงููุต ุงูุฎุงู
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
await ensureDir(RAW_SESSIONS_DIR);
await (0, promises_1.writeFile)((0, path_1.join)(RAW_SESSIONS_DIR, `${id}.md`), session_text, "utf-8");
// โโโ ุญูุธ ุงูุฌูุณุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const sessionData = {
id,
title: title || `ุฌูุณุฉ ${id}`,
start_time: now.toISOString(),
end_time: new Date().toISOString(),
model: "unknown",
summary: "ุชู
ุญูุธ ุงูุฌูุณุฉุ ุชุญุชุงุฌ ุฅูู Gemma ููุชุญููู ุงููุงู
ู",
tags: [],
pulse_ids: [],
node_ids: [],
connections: { previous: null, next: null },
emotional_arc: "ุจุฏุงูุฉ โ ูุณุท โ ููุงูุฉ",
key_moments: []
};
await ensureDir(SESSIONS_DIR);
await writeJSON(sessionPath, sessionData);
return {
success: true,
message: `โ
ุชู
ุญูุธ ุงูุฌูุณุฉ!`,
session_id: id,
raw_path: `raw_sessions/${id}.md`,
session_path: `sessions/${id}.json`,
next_step: "โ ๏ธ ูู
ุจุชุดุบูู memory_build_index ูุชุญุฏูุซ ุงูููุงุฑุณ."
};
}
}));
// โโโ 4. memory_search โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "memory_search",
description: (0, sdk_1.text) `
๐ ุจุญุซ ุณุฑูุน ูู ุงูุฐุงูุฑุฉ ุจููู
ุฉ ู
ูุชุงุญูุฉ.
`,
parameters: {
query: zod_1.z.string().describe("ููู
ุฉ ู
ูุชุงุญูุฉ"),
limit: zod_1.z.number().default(5).describe("ุนุฏุฏ ุงููุชุงุฆุฌ")
},
implementation: async (args) => {
const { query, limit = 5 } = args;
const tagIndex = await readJSON((0, path_1.join)(INDICES_DIR, "tag_index.json"));
if (!tagIndex) {
return { success: false, error: "โ ุงูููุงุฑุณ ุบูุฑ ู
ูุฌูุฏุฉ. ูู
ุจุชุดุบูู memory_build_index ุฃููุงู." };
}
const results = [];
for (const [tag, ids] of Object.entries(tagIndex)) {
if (tag.includes(query.toLowerCase()) || query.includes(tag)) {
results.push(...ids);
}
}
const unique = [...new Set(results)];
const nodeFiles = await (0, promises_1.readdir)(NODES_DIR);
const foundNodes = [];
for (const nf of nodeFiles) {
if (!nf.endsWith(".json"))
continue;
const nodes = await readJSON((0, path_1.join)(NODES_DIR, nf));
if (nodes) {
for (const node of nodes) {
if (unique.includes(node.id)) {
foundNodes.push(node);
}
}
}
}
foundNodes.sort((a, b) => (b.weight || 0) - (a.weight || 0));
const limited = foundNodes.slice(0, limit);
return {
success: true,
query,
total: foundNodes.length,
returned: limited.length,
nodes: limited.map((n) => ({
id: n.id,
title: n.title,
content: n.content,
weight: n.weight,
tags: n.tags || []
}))
};
}
}));
// โโโ 5. eagleEye (ุชุญููู ุงูู
ููุงุช) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "eagleEye",
description: (0, sdk_1.text) `
๐ฆ
ุชุญููู ุนู
ูู ููู
ููุงุช.
ุชููู
ุจุชุญููู ุงูู
ูู ูุนุฑุถ:
- ุงูุญุฌู
ุ ุนุฏุฏ ุงูุณุทูุฑุ ุนุฏุฏ ุงูููู
ุงุช
- ุงูุจููุฉ (HTMLุ JavaScriptุ Pythonุ JSON)
- ุงูุชุดุงู ุงูุฑู
ูุฒ ุงูุฎุจูุซุฉ
- ู
ุนุงููุฉ ุฃูู 30 ุณุทุฑุงู
`,
parameters: {
filePath: zod_1.z.string().describe("ู
ุณุงุฑ ุงูู
ูู ููุชุญููู")
},
implementation: async (args) => {
return await eagleEye(args.filePath);
}
}));
// โโโ 6. searchFiles (ุงูุจุญุซ ูู ุงูู
ููุงุช) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "searchFiles",
description: (0, sdk_1.text) `
๐ ุจุญุซ ุณุฑูุน ูู ุงูู
ููุงุช.
ุชุจุญุซ ูู ุงูู
ููุงุช ุญุณุจ:
- ุงูุงุณู
(searchType: "name")
- ุงูู
ุญุชูู (searchType: "content")
`,
parameters: {
searchPath: zod_1.z.string().describe("ู
ุณุงุฑ ุงูุจุญุซ"),
query: zod_1.z.string().describe("ุงููุต ุงูู
ุทููุจ ุงูุจุญุซ ุนูู"),
searchType: zod_1.z.enum(["name", "content"]).default("name").describe("ููุน ุงูุจุญุซ")
},
implementation: async (args) => {
return await searchFiles(args.searchPath, args.query, args.searchType || "name");
}
}));
// โโโ 7. ocrImage (ุงุณุชุฎุฑุงุฌ ุงููุต ู
ู ุงูุตูุฑ) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "ocrImage",
description: (0, sdk_1.text) `
๐ท ุงุณุชุฎุฑุงุฌ ุงููุต ู
ู ุงูุตูุฑ.
ุชุณุชุฎุฏู
Tesseract OCR ูุงุณุชุฎุฑุงุฌ ุงููุต ู
ู ุงูุตูุฑ.
ุชุฏุนู
ุงูุนุฑุจูุฉ ูุงูุฅูุฌููุฒูุฉ.
`,
parameters: {
imagePath: zod_1.z.string().describe("ู
ุณุงุฑ ุงูุตูุฑุฉ"),
language: zod_1.z.string().default("ara+eng").describe("ูุบุฉ OCR (ara+eng, eng, ...)")
},
implementation: async (args) => {
return await ocrImage(args.imagePath, args.language || "ara+eng");
}
}));
// โโโ 8. semantic_search (ุงูุจุญุซ ุงูุฏูุงูู ุจุงูู embeddings) โโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "semantic_search",
description: (0, sdk_1.text) `
๐ ุจุญุซ ุฏูุงูู ูู ุงูุฐุงูุฑุฉ ุจุงุณุชุฎุฏุงู
ุงูุชุถู
ูู (embedding).
ุชุณุชูุจู embedding ูุชุฑุฌุน ุฃูุฑุจ ุงูุนูุฏ ู
ู ุงูุฐุงูุฑุฉ ุงูู
ุฎุฒูุฉ ูู final_memory.
`,
parameters: {
embedding: zod_1.z.array(zod_1.z.number()).describe("ุชุถู
ูู ุงููุต ุงูู
ุทููุจ ุงูุจุญุซ ุนูู"),
limit: zod_1.z.number().default(3).describe("ุนุฏุฏ ุงููุชุงุฆุฌ"),
threshold: zod_1.z.number().default(0.6).describe("ุญุฏ ุงูุชุดุงุจู ุงูุฃุฏูู")
},
implementation: async (args) => {
const { embedding, limit = 3, threshold = 0.6 } = args;
const FINAL_MEMORY_PATH = "D:/bido/2026/final_memory";
const fs = require('fs');
const path = require('path');
// ๐ Log: ุญุฌู
ุงูู embedding ุงูู
ุณุชูุจู
console.log(`๐ฅ Received embedding of length: ${embedding.length}`);
// 1. ูุฑุงุกุฉ ุฌู
ูุน ู
ููุงุช JSON ูู ู
ุฌูุฏ final_memory (ู
ุง ุนุฏุง template.json)
let allNodes = [];
try {
const files = fs.readdirSync(FINAL_MEMORY_PATH).filter((f) => f.endsWith('.json') && f !== 'template.json');
console.log(`๐ Found ${files.length} JSON files in ${FINAL_MEMORY_PATH}`);
for (const file of files) {
const content = fs.readFileSync(path.join(FINAL_MEMORY_PATH, file), 'utf-8');
const data = JSON.parse(content);
if (data.nodes && Array.isArray(data.nodes)) {
console.log(`๐ File ${file} has ${data.nodes.length} nodes`);
allNodes = allNodes.concat(data.nodes);
}
}
}
catch (err) {
console.error(`โ Error reading memory folder: ${err.message}`);
return { success: false, error: `ูุดู ูู ูุฑุงุกุฉ ู
ุฌูุฏ ุงูุฐุงูุฑุฉ: ${err.message}` };
}
console.log(`๐ Total nodes loaded: ${allNodes.length}`);
if (allNodes.length === 0) {
return { success: true, total: 0, results: [], message: "ูุง ุชูุฌุฏ ุนูุฏ ูู ุงูุฐุงูุฑุฉ" };
}
// 2. ุฏุงูุฉ ุงูุชุดุงุจู ุงูููุณููู
function cosineSimilarity(a, b) {
if (!a || !b || a.length !== b.length || a.length === 0)
return 0;
let dot = 0, normA = 0, normB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB) + 1e-9);
}
// 3. ุญุณุงุจ ุงูุชุดุงุจู ููู ุนูุฏุฉ (ู
ุน ุชุฌุงูู ุงูุนูุฏ ุงููู ู
ุงููุงุด embedding)
const results = allNodes
.filter(node => node.embedding && Array.isArray(node.embedding) && node.embedding.length > 0)
.map(node => ({
...node,
similarity: cosineSimilarity(embedding, node.embedding)
}));
console.log(`๐ Calculated similarity for ${results.length} nodes with embeddings`);
// 4. ููุชุฑุฉ ูุชุฑุชูุจ
const filtered = results
.filter(r => r.similarity >= threshold)
.sort((a, b) => b.similarity - a.similarity)
.slice(0, limit);
console.log(`โ
Found ${filtered.length} results above threshold ${threshold}`);
console.log(`๐ฅ Received embedding length: ${embedding.length}`);
console.log(`๐ Searching in: ${FINAL_MEMORY_PATH}`);
console.log(`๐ Total nodes loaded: ${allNodes.length}`);
console.log(`๐ Nodes with embeddings: ${allNodes.filter(n => n.embedding?.length > 0).length}`);
return {
success: true,
total: filtered.length,
results: filtered.map((r) => ({
id: r.id,
title: r.title,
content: r.content,
type: r.type,
tags: r.tags || [],
weight: r.weight,
similarity: r.similarity,
session_id: r.session_id
}))
};
}
}));
// โโโ 9. embed_all_nodes (ุชูููุฏ embeddings ุชููุงุฆู ููู ุงูุนูุฏ) โโโโโโโโโโโโโโ
// โโโ 9. embed_all_nodes (ุชูููุฏ embeddings ุชููุงุฆู ููู ุงูุนูุฏ) โโโโโโโโโโโโโโ
tools.push((0, sdk_1.tool)({
name: "embed_all_nodes",
description: (0, sdk_1.text) `
๐ง ุชูููุฏ embeddings ูุฌู
ูุน ุงูุนูุฏ ูู ู
ุฌูุฏ final_memory.
ุชูุญุต ุฌู
ูุน ู
ููุงุช JSON ูู:
${"D:/bido/2026/final_memory"}
ูุชููู
ุจู:
1. ูุฑุงุกุฉ ูู ุนูุฏุฉ (node)
2. ุงูุชุญูู ู
ู ูุฌูุฏ embedding
3. ุฅุฐุง ูุงู ู
ูููุฏุงู ุฃู ูุงุฑุบุงูุ ุชูููุฏ embedding ุจุงุณุชุฎุฏุงู
Nomic
4. ุชุญูุธ ุงูู
ูู ู
ุฑุฉ ุฃุฎุฑู
`,
parameters: {
model: zod_1.z.string().default("nomic-ai/nomic-embed-text-v1.5-GGUF").describe("ูู
ูุฐุฌ ุงูุชุถู
ูู"),
force: zod_1.z.boolean().default(false).describe("ุฅุนุงุฏุฉ ุชูููุฏ embeddings ููุนูุฏ ุงูู
ูุฌูุฏุฉ ุจุงููุนู")
},
implementation: async (args) => {
const { model = "nomic-ai/nomic-embed-text-v1.5-GGUF", force = false } = args;
const FINAL_MEMORY_PATH = "D:/bido/2026/final_memory";
const fs = require('fs');
const path = require('path');
// 1. ูุฑุงุกุฉ ุฌู
ูุน ู
ููุงุช JSON ูู final_memory
const files = fs.readdirSync(FINAL_MEMORY_PATH).filter((f) => f.endsWith('.json') && f !== 'template.json');
let totalNodes = 0;
let updatedNodes = 0;
let failedNodes = 0;
for (const file of files) {
const filePath = path.join(FINAL_MEMORY_PATH, file);
const content = fs.readFileSync(filePath, 'utf-8');
const data = JSON.parse(content);
if (!data.nodes || !Array.isArray(data.nodes))
continue;
for (const node of data.nodes) {
totalNodes++;
// ุงูุชุญูู ู
ู ุงูุญุงุฌุฉ ูุชูููุฏ embedding
const hasEmbedding = node.embedding && Array.isArray(node.embedding) && node.embedding.length > 0;
if (hasEmbedding && !force)
continue;
try {
// ุชูููุฏ embedding ุจุงุณุชุฎุฏุงู
Nomic
const embeddingResponse = await fetch('http://127.0.0.1:1234/v1/embeddings', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer sk-lm-B65OabRR:yEFefzaxs6mKdEJa2SFx`
},
body: JSON.stringify({
model: model,
input: node.content || node.title || "ู
ูููู
ุบูุฑ ู
ุนุฑูู"
})
});
if (!embeddingResponse.ok) {
throw new Error(`HTTP ${embeddingResponse.status}`);
}
const embeddingData = await embeddingResponse.json();
node.embedding = embeddingData.data[0].embedding;
updatedNodes++;
}
catch (err) {
failedNodes++;
console.error(`โ ูุดู ูู ุชูููุฏ embedding ููุนูุฏุฉ ${node.id}: ${err.message}`);
}
}
// ุญูุธ ุงูู
ูู ุจุนุฏ ุงูุชุนุฏูู
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
}
return {
success: true,
message: `โ
ุชู
ุงูุงูุชูุงุก ู
ู ุชูููุฏ embeddings!`,
stats: {
total_nodes: totalNodes,
updated_nodes: updatedNodes,
failed_nodes: failedNodes
}
};
}
}));
return tools;
}
//# sourceMappingURL=toolsProvider.js.map