The community edition defaulted to ~/.waggle, the data directory of the commercial Waggle, so both editions would open the same config, vault, waggle.db, marketplace catalog and skills. Default to ~/.waggle-community instead (ADR-0011). WAGGLE_DATA_DIR still overrides it wherever it did; nothing is migrated from ~/.waggle. The name is defined once as DEFAULT_DATA_DIR_NAME and DEFAULT_DATA_DIR_LABEL in @waggle/shared, with defaultDataDir() in @waggle/core for the sidecar, the CLI, the agent and the model caches. The marketplace installs on its own without workspace packages, so it keeps a copy in its data-dir.ts; the CLI help, which loads no dependency, spells the path out and its runtime test checks it. The marketplace security scan also flags skills that reference the new skills folder. Tests that asserted the old default now use the constant, and comments name the new directory.
143 lines
4.5 KiB
TypeScript
143 lines
4.5 KiB
TypeScript
/**
|
|
* Document Version Registry — track document versions within a workspace.
|
|
*
|
|
* Endpoints:
|
|
* GET /api/workspaces/:id/documents — list all tracked documents
|
|
* POST /api/workspaces/:id/documents — register a new document version
|
|
* GET /api/workspaces/:id/documents/:name/versions — list versions of a document
|
|
*
|
|
* Versions are stored in <dataDir>/workspaces/{id}/documents.json.
|
|
* Part of Wave 7 — Professional & Vertical Features.
|
|
*/
|
|
|
|
import type { FastifyPluginAsync } from 'fastify';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { defaultDataDir } from '@waggle/core';
|
|
import { assertSafeSegment } from './validate.js';
|
|
|
|
export interface DocumentVersion {
|
|
version: number;
|
|
path: string;
|
|
createdAt: string;
|
|
sizeBytes: number;
|
|
}
|
|
|
|
export interface TrackedDocument {
|
|
name: string;
|
|
versions: DocumentVersion[];
|
|
}
|
|
|
|
interface DocumentsRegistry {
|
|
documents: TrackedDocument[];
|
|
}
|
|
|
|
/** Resolve the documents.json path for a workspace. */
|
|
function documentsFilePath(dataDir: string, workspaceId: string): string {
|
|
return path.join(dataDir, 'workspaces', workspaceId, 'documents.json');
|
|
}
|
|
|
|
/** Read document registry from disk. Returns empty registry if file doesn't exist. */
|
|
function readRegistry(dataDir: string, workspaceId: string): DocumentsRegistry {
|
|
const filePath = documentsFilePath(dataDir, workspaceId);
|
|
try {
|
|
if (fs.existsSync(filePath)) {
|
|
const raw = fs.readFileSync(filePath, 'utf-8');
|
|
return JSON.parse(raw) as DocumentsRegistry;
|
|
}
|
|
} catch {
|
|
// Corrupted file — return empty
|
|
}
|
|
return { documents: [] };
|
|
}
|
|
|
|
/** Write document registry to disk. Creates directory if needed. */
|
|
function writeRegistry(dataDir: string, workspaceId: string, registry: DocumentsRegistry): void {
|
|
const filePath = documentsFilePath(dataDir, workspaceId);
|
|
const dir = path.dirname(filePath);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
fs.writeFileSync(filePath, JSON.stringify(registry, null, 2), 'utf-8');
|
|
}
|
|
|
|
export const documentRoutes: FastifyPluginAsync = async (server) => {
|
|
const dataDir = server.localConfig?.dataDir || process.env.WAGGLE_DATA_DIR || defaultDataDir();
|
|
|
|
// GET /api/workspaces/:id/documents — list all tracked documents
|
|
server.get<{
|
|
Params: { id: string };
|
|
}>('/api/workspaces/:id/documents', async (request) => {
|
|
const { id } = request.params;
|
|
assertSafeSegment(id, 'id');
|
|
const registry = readRegistry(dataDir, id);
|
|
return {
|
|
documents: registry.documents.map(doc => ({
|
|
name: doc.name,
|
|
versionCount: doc.versions.length,
|
|
latestVersion: doc.versions.length > 0 ? doc.versions[doc.versions.length - 1] : null,
|
|
})),
|
|
};
|
|
});
|
|
|
|
// POST /api/workspaces/:id/documents — register a new document version
|
|
server.post<{
|
|
Params: { id: string };
|
|
Body: {
|
|
name: string;
|
|
path: string;
|
|
sizeBytes?: number;
|
|
};
|
|
}>('/api/workspaces/:id/documents', async (request, reply) => {
|
|
const { id } = request.params;
|
|
assertSafeSegment(id, 'id');
|
|
const { name, path: docPath, sizeBytes } = request.body ?? {};
|
|
|
|
if (!name || !docPath) {
|
|
return reply.status(400).send({ error: 'name and path are required' });
|
|
}
|
|
assertSafeSegment(name, 'name');
|
|
|
|
const registry = readRegistry(dataDir, id);
|
|
let doc = registry.documents.find(d => d.name === name);
|
|
|
|
if (!doc) {
|
|
doc = { name, versions: [] };
|
|
registry.documents.push(doc);
|
|
}
|
|
|
|
const nextVersion = doc.versions.length > 0
|
|
? doc.versions[doc.versions.length - 1].version + 1
|
|
: 1;
|
|
|
|
const version: DocumentVersion = {
|
|
version: nextVersion,
|
|
path: docPath,
|
|
createdAt: new Date().toISOString(),
|
|
sizeBytes: sizeBytes ?? 0,
|
|
};
|
|
|
|
doc.versions.push(version);
|
|
writeRegistry(dataDir, id, registry);
|
|
|
|
return reply.status(201).send({ document: name, version });
|
|
});
|
|
|
|
// GET /api/workspaces/:id/documents/:name/versions — list versions of a document
|
|
server.get<{
|
|
Params: { id: string; name: string };
|
|
}>('/api/workspaces/:id/documents/:name/versions', async (request, reply) => {
|
|
const { id, name } = request.params;
|
|
assertSafeSegment(id, 'id');
|
|
assertSafeSegment(name, 'name');
|
|
const registry = readRegistry(dataDir, id);
|
|
const doc = registry.documents.find(d => d.name === name);
|
|
|
|
if (!doc) {
|
|
return reply.status(404).send({ error: 'Document not found' });
|
|
}
|
|
|
|
return { name: doc.name, versions: doc.versions };
|
|
});
|
|
};
|