u
This commit is contained in:
@@ -4,6 +4,7 @@ import { defineTool, ToolError } from 'libllmorbiter';
|
||||
import { isSkillPath, listSkillFiles, SKILL_EXT } from './skill.js';
|
||||
import { prepareVfs } from './vfs.js';
|
||||
import { TOOL_DESCRIPTIONS, TOOL_PARAMETER_DESCRIPTIONS } from './prompts.js';
|
||||
import { DRAFT_NAMES } from '../services/draftManager.js';
|
||||
|
||||
/**
|
||||
* find_files — lists the contents of the virtual filesystem (VFS).
|
||||
@@ -173,6 +174,31 @@ export function createFindFilesTool({ googleAuthId }) {
|
||||
);
|
||||
}
|
||||
|
||||
if (cleanDir.replace(/\/+$/, '').toLowerCase() === 'drafts') {
|
||||
const draftEntries = Array.from(DRAFT_NAMES).map((name) => ({
|
||||
path: `drafts/${name}`,
|
||||
name,
|
||||
type: 'file',
|
||||
size: null,
|
||||
modified: null,
|
||||
}));
|
||||
const page = draftEntries.slice(seek, seek + vfsLimit);
|
||||
const truncated = Math.max(0, draftEntries.length - (seek + page.length));
|
||||
const result = {
|
||||
kind: 'files',
|
||||
dirname: 'drafts',
|
||||
recursive: recursive === true,
|
||||
total: draftEntries.length,
|
||||
returned: page.length,
|
||||
seek,
|
||||
entries: page,
|
||||
};
|
||||
if (truncated > 0) {
|
||||
result.truncated = truncated;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const absDir = path.resolve(vfsRoot, cleanDir);
|
||||
const rel = path.relative(vfsRoot, absDir);
|
||||
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
||||
@@ -187,6 +213,16 @@ export function createFindFilesTool({ googleAuthId }) {
|
||||
}
|
||||
|
||||
const all = walkVfs(absDir, vfsRoot, recursive === true, 0, maxDepth);
|
||||
if (cleanDir === '.') {
|
||||
if (!all.some((e) => e.path === 'drafts/')) {
|
||||
all.push({ path: 'drafts/', name: 'drafts', type: 'dir', size: null, modified: null });
|
||||
}
|
||||
if (recursive === true) {
|
||||
for (const name of DRAFT_NAMES) {
|
||||
all.push({ path: `drafts/${name}`, name, type: 'file', size: null, modified: null });
|
||||
}
|
||||
}
|
||||
}
|
||||
// Directories first, then alphabetical — makes a truncated listing predictable
|
||||
// instead of arbitrary.
|
||||
all.sort((a, b) => (a.type === b.type ? a.path.localeCompare(b.path) : a.type === 'dir' ? -1 : 1));
|
||||
|
||||
@@ -5,6 +5,8 @@ import { isReadOnlyDatabasePath } from './vfs.js';
|
||||
import { isSkillPath, renderSkill, parseSkill, SKILL_EXT, skillFileName } from './skill.js';
|
||||
import { prepareVfs, syncVfsToDb } from './vfs.js';
|
||||
import { TOOL_DESCRIPTIONS, PATCH_PARAMETER_DESCRIPTIONS } from './prompts.js';
|
||||
import { getStore } from '../db/store.js';
|
||||
import { parseDraftPath, requestDraftRead, notifyDraftUpdate, getCachedDraft } from '../services/draftManager.js';
|
||||
|
||||
/**
|
||||
* patch — applies edit operations to files in the virtual filesystem (VFS).
|
||||
@@ -175,7 +177,12 @@ export function createPatchTool({ googleAuthId }) {
|
||||
);
|
||||
}
|
||||
|
||||
validated.push({ index: i, op, clean, abs, isSkill: looksSkillByExt, skillBody });
|
||||
const draftInfo = parseDraftPath(clean);
|
||||
if (draftInfo.isFolder) {
|
||||
throw new ToolError(`Operation at index ${i}: "${clean}" is a directory and cannot be patched.`);
|
||||
}
|
||||
|
||||
validated.push({ index: i, op, clean, abs, isSkill: looksSkillByExt, skillBody, draftInfo });
|
||||
}
|
||||
|
||||
// ── Apply ──────────────────────────────────────────────────────
|
||||
@@ -183,7 +190,57 @@ export function createPatchTool({ googleAuthId }) {
|
||||
const touchedSkills = new Set();
|
||||
|
||||
for (const v of validated) {
|
||||
const { op, clean, abs, isSkill } = v;
|
||||
const { op, clean, abs, isSkill, draftInfo } = v;
|
||||
|
||||
if (draftInfo?.isDraft) {
|
||||
const isCreate = op.search === null || op.search === undefined || op.search === '';
|
||||
let original = '';
|
||||
if (!isCreate) {
|
||||
try {
|
||||
const readVal = await requestDraftRead({ userId: googleAuthId, name: draftInfo.name });
|
||||
original = typeof readVal === 'string' ? readVal : JSON.stringify(readVal ?? {}, null, 2);
|
||||
} catch (err) {
|
||||
const cached = getCachedDraft({ userId: googleAuthId, name: draftInfo.name });
|
||||
if (cached) {
|
||||
original = typeof cached.content === 'string' ? cached.content : JSON.stringify(cached.content ?? {}, null, 2);
|
||||
} else {
|
||||
throw new ToolError(`Cannot read draft "${clean}" before patching: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let draftUpdated;
|
||||
if (isCreate) {
|
||||
draftUpdated = typeof op.replace === 'string' ? op.replace : JSON.stringify(op.replace, null, 2);
|
||||
} else {
|
||||
const haystack = original.includes('\r\n') ? original.replace(/\r\n/g, '\n') : original;
|
||||
const needle = typeof op.search === 'string' ? op.search.replace(/\r\n/g, '\n') : '';
|
||||
if (!needle) {
|
||||
throw new ToolError(`Operation at index ${v.index} ("${clean}") has an empty "search". Use "search": null to overwrite.`);
|
||||
}
|
||||
const at = haystack.indexOf(needle);
|
||||
if (at === -1) {
|
||||
throw new ToolError(`SEARCH/REPLACE failed in "${clean}": the search text was not found. Read the draft again with read_file.`);
|
||||
}
|
||||
const replacement = typeof op.replace === 'string' ? op.replace : JSON.stringify(op.replace, null, 2);
|
||||
draftUpdated = haystack.slice(0, at) + replacement + haystack.slice(at + needle.length);
|
||||
}
|
||||
|
||||
let parsed = draftUpdated;
|
||||
try { parsed = JSON.parse(draftUpdated); } catch {}
|
||||
const store = await getStore();
|
||||
const row = await store.google_auth.getById(googleAuthId);
|
||||
const companyId = row?.selectedCompanyId;
|
||||
notifyDraftUpdate({
|
||||
companyId,
|
||||
userId: googleAuthId,
|
||||
name: draftInfo.name,
|
||||
content: parsed,
|
||||
});
|
||||
applied.push(`PATCH ${clean}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const isCreate = op.search === null || op.search === undefined || op.search === '';
|
||||
const exists = fs.existsSync(abs);
|
||||
|
||||
@@ -260,16 +317,19 @@ export function createPatchTool({ googleAuthId }) {
|
||||
if (isSkill) touchedSkills.add(clean);
|
||||
}
|
||||
|
||||
// Persist immediately — unlike javascript_execute, there is no trailing sync, so a
|
||||
// Persist immediately for disk files — unlike javascript_execute, there is no trailing sync, so a
|
||||
// text edit would otherwise live only in the on-disk cache until some other tool ran.
|
||||
try {
|
||||
await syncVfsToDb({ googleAuthId, vfsRoot });
|
||||
} catch (err) {
|
||||
console.error('[patch] failed to sync VFS to db:', err.message);
|
||||
throw new ToolError(
|
||||
`Patch applied to disk but could not be persisted: ${err.message}. ` +
|
||||
`Files changed: ${applied.join(', ')}.`
|
||||
);
|
||||
const hasDiskWrites = validated.some((v) => !v.draftInfo?.isDraft);
|
||||
if (hasDiskWrites) {
|
||||
try {
|
||||
await syncVfsToDb({ googleAuthId, vfsRoot });
|
||||
} catch (err) {
|
||||
console.error('[patch] failed to sync VFS to db:', err.message);
|
||||
throw new ToolError(
|
||||
`Patch applied to disk but could not be persisted: ${err.message}. ` +
|
||||
`Files changed: ${applied.join(', ')}.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const result = {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { isSkillPath, parseSkill, SKILL_EXT } from './skill.js';
|
||||
import { getApiRouteSkill } from './routes-skills.js';
|
||||
import { prepareVfs } from './vfs.js';
|
||||
import { TOOL_DESCRIPTIONS, TOOL_PARAMETER_DESCRIPTIONS } from './prompts.js';
|
||||
import { parseDraftPath, requestDraftRead } from '../services/draftManager.js';
|
||||
|
||||
const DEFAULT_LIMIT = 400;
|
||||
const MAX_LIMIT = 5000;
|
||||
@@ -108,18 +109,37 @@ export function createReadFileTool({ googleAuthId }) {
|
||||
throw new ToolError(`Access denied: path "${filename}" escapes virtual filesystem boundary.`);
|
||||
}
|
||||
|
||||
const virtualSkill = !fs.existsSync(absPath) && isSkillPath(clean) ? getApiRouteSkill(clean) : null;
|
||||
if (!virtualSkill && !fs.existsSync(absPath)) {
|
||||
throw new ToolError(
|
||||
`File not found in virtual filesystem: "${clean}". ` +
|
||||
'Call find_files to see what is stored, or find_files({ skills: true }) for SKILL files.'
|
||||
);
|
||||
}
|
||||
if (!virtualSkill && !fs.statSync(absPath).isFile()) {
|
||||
throw new ToolError(`"${clean}" is not a file. Use find_files to list directories.`);
|
||||
const draftInfo = parseDraftPath(clean);
|
||||
if (draftInfo.isFolder) {
|
||||
throw new ToolError('"drafts" is a directory. Use find_files to list its contents.');
|
||||
}
|
||||
|
||||
const raw = virtualSkill ? Buffer.from(virtualSkill.content, 'utf8') : fs.readFileSync(absPath);
|
||||
let raw;
|
||||
let virtualSkill = null;
|
||||
if (draftInfo.isDraft) {
|
||||
try {
|
||||
const content = await requestDraftRead({
|
||||
userId: googleAuthId,
|
||||
name: draftInfo.name,
|
||||
});
|
||||
const str = typeof content === 'string' ? content : JSON.stringify(content ?? {}, null, 2);
|
||||
raw = Buffer.from(str, 'utf8');
|
||||
} catch (err) {
|
||||
throw new ToolError(`Failed to read draft "${draftInfo.name}": ${err.message}`);
|
||||
}
|
||||
} else {
|
||||
virtualSkill = !fs.existsSync(absPath) && isSkillPath(clean) ? getApiRouteSkill(clean) : null;
|
||||
if (!virtualSkill && !fs.existsSync(absPath)) {
|
||||
throw new ToolError(
|
||||
`File not found in virtual filesystem: "${clean}". ` +
|
||||
'Call find_files to see what is stored, or find_files({ skills: true }) for SKILL files.'
|
||||
);
|
||||
}
|
||||
if (!virtualSkill && !fs.statSync(absPath).isFile()) {
|
||||
throw new ToolError(`"${clean}" is not a file. Use find_files to list directories.`);
|
||||
}
|
||||
raw = virtualSkill ? Buffer.from(virtualSkill.content, 'utf8') : fs.readFileSync(absPath);
|
||||
}
|
||||
if (looksBinary(raw)) {
|
||||
const ext = path.extname(clean).toLowerCase();
|
||||
throw new ToolError(
|
||||
|
||||
141
src/tools/vfs.js
141
src/tools/vfs.js
@@ -4,6 +4,14 @@ import { getStore } from '../db/store.js';
|
||||
import env from '../config/env.js';
|
||||
import { getDefaultSkills } from './skill.js';
|
||||
import { getDefaultApplets, DEFAULT_APPLETS_VERSION, APPLET_EXT } from './applet.js';
|
||||
import {
|
||||
parseDraftPath,
|
||||
requestDraftRead,
|
||||
notifyDraftUpdate,
|
||||
notifyDraftUpdateSync,
|
||||
getCachedDraft,
|
||||
DRAFT_NAMES,
|
||||
} from '../services/draftManager.js';
|
||||
|
||||
const FULL_HASH_REGEX = /^[a-zA-Z0-9]{8}$/;
|
||||
const VIRTUAL_IMAGE_FOLDERS = new Set(['image', 'images']);
|
||||
@@ -300,6 +308,16 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
|
||||
const vfs = {
|
||||
readFileSync: (p, opt) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isDraft) {
|
||||
const cached = getCachedDraft({ userId: googleAuthId, name: draftInfo.name });
|
||||
if (cached) {
|
||||
const str = typeof cached.content === 'string' ? cached.content : JSON.stringify(cached.content ?? {}, null, 2);
|
||||
const encoding = typeof opt === 'string' ? opt : opt?.encoding;
|
||||
return encoding ? str : Buffer.from(str, 'utf8');
|
||||
}
|
||||
throw new Error(`Virtual draft "${draftInfo.name}" is not cached in memory. Use vfs.promises.readFile() to fetch it asynchronously.`);
|
||||
}
|
||||
const hash = extractFullHash(p);
|
||||
if (hash) {
|
||||
const buf = syncFetchImage(hash);
|
||||
@@ -342,6 +360,19 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
},
|
||||
|
||||
writeFileSync: (p, data, opt) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isDraft) {
|
||||
let str = typeof data === 'string' ? data : Buffer.isBuffer(data) ? data.toString('utf8') : JSON.stringify(data);
|
||||
let parsed = str;
|
||||
try { parsed = JSON.parse(str); } catch {}
|
||||
notifyDraftUpdateSync({
|
||||
googleAuthId,
|
||||
clientId: options.clientId,
|
||||
name: draftInfo.name,
|
||||
content: parsed,
|
||||
});
|
||||
return;
|
||||
}
|
||||
assertNotReadOnlyDatabasePath(p);
|
||||
const diskPath = safeResolve(p);
|
||||
fs.mkdirSync(path.dirname(diskPath), { recursive: true });
|
||||
@@ -382,6 +413,8 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
}
|
||||
},
|
||||
existsSync: (p) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isFolder || draftInfo.isDraft) return true;
|
||||
const hash = extractFullHash(p);
|
||||
if (hash) {
|
||||
const buf = syncFetchImage(hash);
|
||||
@@ -396,6 +429,31 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
},
|
||||
|
||||
statSync: (p, opt) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isFolder) {
|
||||
const now = new Date();
|
||||
return {
|
||||
isFile: () => false,
|
||||
isDirectory: () => true,
|
||||
size: 0,
|
||||
mtime: now,
|
||||
ctime: now,
|
||||
atime: now,
|
||||
};
|
||||
}
|
||||
if (draftInfo.isDraft) {
|
||||
const cached = getCachedDraft({ userId: googleAuthId, name: draftInfo.name });
|
||||
const now = cached?.updatedAt ? new Date(cached.updatedAt) : new Date();
|
||||
const size = cached?.size ?? 0;
|
||||
return {
|
||||
isFile: () => true,
|
||||
isDirectory: () => false,
|
||||
size,
|
||||
mtime: now,
|
||||
ctime: now,
|
||||
atime: now,
|
||||
};
|
||||
}
|
||||
const hash = extractFullHash(p);
|
||||
if (hash) {
|
||||
const buf = syncFetchImage(hash);
|
||||
@@ -418,36 +476,62 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
lstatSync: (p, opt) => vfs.statSync(p, opt),
|
||||
|
||||
readdirSync: (p = '.', opt) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isFolder) {
|
||||
return Array.from(DRAFT_NAMES);
|
||||
}
|
||||
const clean = typeof p === 'string' ? p.trim().replace(/^([a-zA-Z]:|\/)+/, '') : '';
|
||||
if (VIRTUAL_IMAGE_FOLDERS.has(clean.toLowerCase())) {
|
||||
return [];
|
||||
}
|
||||
const diskPath = safeResolve(p);
|
||||
return fs.readdirSync(diskPath, opt);
|
||||
const entries = fs.readdirSync(diskPath, opt);
|
||||
if ((clean === '' || clean === '.') && Array.isArray(entries)) {
|
||||
if (!entries.some((e) => (typeof e === 'string' ? e : e.name) === 'drafts')) {
|
||||
entries.push('drafts');
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
},
|
||||
|
||||
mkdirSync: (p, opt) => {
|
||||
assertNotReadOnlyDatabasePath(p);
|
||||
const diskPath = safeResolve(p);
|
||||
try {
|
||||
return fs.mkdirSync(diskPath, opt);
|
||||
} catch (err) {
|
||||
throw sanitizeFsError(err, p);
|
||||
}
|
||||
},
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isFolder || draftInfo.isDraft) {
|
||||
throw new Error(`Access denied: "${p}" is a virtual draft and cannot be created on disk.`);
|
||||
}
|
||||
assertNotReadOnlyDatabasePath(p);
|
||||
const diskPath = safeResolve(p);
|
||||
try {
|
||||
return fs.mkdirSync(diskPath, opt);
|
||||
} catch (err) {
|
||||
throw sanitizeFsError(err, p);
|
||||
}
|
||||
},
|
||||
unlinkSync: (p) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isFolder || draftInfo.isDraft) {
|
||||
throw new Error(`Access denied: "${p}" is a virtual draft and cannot be unlinked on disk.`);
|
||||
}
|
||||
assertNotReadOnlyDatabasePath(p);
|
||||
const diskPath = safeResolve(p);
|
||||
return fs.unlinkSync(diskPath);
|
||||
},
|
||||
|
||||
rmdirSync: (p, opt) => {
|
||||
assertNotReadOnlyDatabasePath(p);
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isFolder || draftInfo.isDraft) {
|
||||
throw new Error(`Access denied: "${p}" is a virtual draft and cannot be removed on disk.`);
|
||||
}
|
||||
assertNotReadOnlyDatabasePath(p);
|
||||
const diskPath = safeResolve(p);
|
||||
return fs.rmdirSync(diskPath, opt);
|
||||
},
|
||||
|
||||
rmSync: (p, opt) => {
|
||||
rmSync: (p, opt) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isFolder || draftInfo.isDraft) {
|
||||
throw new Error(`Access denied: "${p}" is a virtual draft and cannot be removed on disk.`);
|
||||
}
|
||||
assertNotReadOnlyDatabasePath(p);
|
||||
const diskPath = safeResolve(p);
|
||||
return fs.rmSync(diskPath, opt);
|
||||
@@ -455,6 +539,17 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
|
||||
promises: {
|
||||
readFile: async (p, opt) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isDraft) {
|
||||
const content = await requestDraftRead({
|
||||
userId: googleAuthId,
|
||||
clientId: options.clientId,
|
||||
name: draftInfo.name,
|
||||
});
|
||||
const str = typeof content === 'string' ? content : JSON.stringify(content ?? {}, null, 2);
|
||||
const encoding = typeof opt === 'string' ? opt : opt?.encoding;
|
||||
return encoding ? str : Buffer.from(str, 'utf8');
|
||||
}
|
||||
const hash = extractFullHash(p);
|
||||
if (hash) {
|
||||
const buf = syncFetchImage(hash);
|
||||
@@ -482,7 +577,29 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
}
|
||||
return vfs.readFileSync(p, opt);
|
||||
},
|
||||
writeFile: async (p, data, opt) => vfs.writeFileSync(p, data, opt),
|
||||
writeFile: async (p, data, opt) => {
|
||||
const draftInfo = parseDraftPath(p);
|
||||
if (draftInfo.isDraft) {
|
||||
let str = typeof data === 'string' ? data : Buffer.isBuffer(data) ? data.toString('utf8') : JSON.stringify(data);
|
||||
let parsed = str;
|
||||
try { parsed = JSON.parse(str); } catch {}
|
||||
let companyId = options.companyId;
|
||||
if (companyId == null && googleAuthId) {
|
||||
const store = await getStore();
|
||||
const row = await store.google_auth.getById(googleAuthId);
|
||||
companyId = row?.selectedCompanyId;
|
||||
}
|
||||
notifyDraftUpdate({
|
||||
companyId,
|
||||
userId: googleAuthId,
|
||||
clientId: options.clientId,
|
||||
name: draftInfo.name,
|
||||
content: parsed,
|
||||
});
|
||||
return;
|
||||
}
|
||||
return vfs.writeFileSync(p, data, opt);
|
||||
},
|
||||
appendFile: async (p, data, opt) => vfs.appendFileSync(p, data, opt),
|
||||
stat: async (p, opt) => vfs.statSync(p, opt),
|
||||
readdir: async (p, opt) => vfs.readdirSync(p, opt),
|
||||
|
||||
Reference in New Issue
Block a user