@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
78 lines • 3.19 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { DifyAccessor } from "../../accessor/dify.js";
import { read, readStream } from "../../core/dify/read.js";
import { searchMany } from "../../core/dify/search.js";
import { stat } from "../../core/dify/stat.js";
import { DIFY_TREE } from "../../core/dify/tree.js";
import {} from "../../io/types.js";
import { VFSName } from "../../types.js";
import { BaseVFS } from "../base.js";
import { redactDifyConfig, resolveDifyConfig } from "./config.js";
export const VFS_MD = `- A Dify knowledge base, mounted read only.
- Every file is plain text assembled from the document's Dify segments,
whatever its original extension.
- \`cat\`, \`head\`, \`tail\`, \`grep\`, \`find\`, \`ls\` and \`wc\` work on these
files.`;
export class DifyVFS extends BaseVFS {
name = VFSName.DIFY;
cachesReads = true;
supportsSnapshot = false;
vfsMd = VFS_MD;
handed = {
readdir: ['entry'],
read: ['entry'],
stat: ['entry'],
};
config;
accessor;
constructor(options) {
super();
const config = 'config' in options ? options.config : options;
this.config = resolveDifyConfig(config);
this.accessor = new DifyAccessor(this.config);
}
getState() {
return {
type: this.name,
config: redactDifyConfig(this.config),
// TypeScript cannot rebuild a config-backed mount from state:
// `buildMountArgs` substitutes a RAMVFS for anything it was
// not handed. Saying so out loud turns a silently empty mount
// into a refusal to load. Python rebuilds via its registry, so it
// writes this on only four mounts and reads it nowhere.
needs_override: true,
};
}
readsRanges = true;
readdir(path, handed) {
return DIFY_TREE.readdir(this.accessor, path, handed?.entry);
}
read(path, handed, offset = 0, size = null) {
const entry = handed?.entry ?? null;
if (offset === 0 && size === null)
return readStream(this.accessor, path, handed?.entry);
return read(this.accessor, path, entry, size === null ? { offset } : { offset, size });
}
stat(path, handed) {
return stat(this.accessor, path, handed?.entry);
}
search(path, query) {
return searchMany(this.accessor, [path], query);
}
searchMany(paths, query) {
return searchMany(this.accessor, paths, query);
}
}
//# sourceMappingURL=dify.js.map