moving
Some checks failed
Installer Smoke / installer-smoke (push) Has been cancelled

This commit is contained in:
Oleg Maslov
2026-09-02 10:10:29 +02:00
commit 0c3e2ead3b
3841 changed files with 970576 additions and 0 deletions

View File

@@ -0,0 +1,398 @@
import fs from 'node:fs';
import path from 'node:path';
import type { WorkspaceType } from '@waggle/shared';
type AIActRiskLevel = 'minimal' | 'limited' | 'high-risk' | 'unacceptable';
export interface WorkspaceConfig {
id: string;
name: string;
group: string;
icon?: string;
model?: string;
personality?: string;
/** Selected agent persona ID (from persona catalog) */
personaId?: string;
/** Template ID chosen during onboarding (e.g. 'sales-pipeline', 'research-project'). */
templateId?: string;
tools?: string[];
skills?: string[];
team?: string | null;
/** Filesystem directory where agent operates and generates files. */
directory?: string;
/** Storage type for workspace files. */
storageType?: 'virtual' | 'local' | 'team';
/** Path to local file storage for this workspace. */
storagePath?: string;
/** Extra storage provider configuration (e.g. S3 bucket, credentials). */
storageConfig?: Record<string, unknown>;
created: string; // ISO 8601
// --- Team Mode fields (Phase 5) ---
/** Team ID on the team server. Present = team workspace. */
teamId?: string;
/** URL of the team server (e.g. "https://team.waggle.dev"). */
teamServerUrl?: string;
/** Current user's role in this team workspace. */
teamRole?: 'owner' | 'admin' | 'member' | 'viewer';
/** Current user's ID on the team server. */
teamUserId?: string;
// --- Budget ---
/** Monthly cost budget in USD. null = unlimited. */
budget?: number | null;
// --- Tone/Voice (Wave 7.3) ---
/** Workspace communication tone preset. */
tone?: 'professional' | 'casual' | 'technical' | 'legal' | 'marketing';
// --- Optimization fields (GEPA/Ax) ---
/** Enable GEPA prompt optimization for this workspace (opt-in, default false). */
optimizationEnabled?: boolean;
/** Daily optimization budget in cents (default 100 = $1/day). Only used when optimizationEnabled is true. */
optimizationBudget?: number;
// --- AI Act compliance (L-17 C2) ---
/** EU AI Act risk classification for this workspace. */
riskLevel?: AIActRiskLevel;
/** ISO timestamp of the last risk classification change. Auto-stamped by WorkspaceManager. */
riskClassifiedAt?: string;
// --- UX-Refactor V2 fields (PRD §15.3; additive + optional for back-compat) ---
/** Free-text description shown in the workspace header/cards. */
description?: string;
/** Workspace classification. Defaults derivable from templateId/group when absent. */
type?: WorkspaceType;
/** Lifecycle status. Treated as 'active' when absent. */
status?: 'active' | 'paused' | 'archived';
/** Agents bound to this workspace (ids). */
agentIds?: string[];
/** Connectors scoped to this workspace (ids). */
connectorIds?: string[];
/** MCP servers scoped to this workspace (ids). */
mcpIds?: string[];
/** ISO timestamp of the last config update. Auto-stamped by update(). */
updatedAt?: string;
/** ISO timestamp of the last activity (chat/agent run) in this workspace. */
lastActiveAt?: string;
}
export interface CreateWorkspaceOptions {
name: string;
group: string;
icon?: string;
model?: string;
personality?: string;
/** Selected agent persona ID (from persona catalog) */
personaId?: string;
/** Template ID chosen during onboarding (e.g. 'sales-pipeline', 'research-project'). */
templateId?: string;
tools?: string[];
skills?: string[];
team?: string | null;
/** Filesystem directory where agent operates and generates files. */
directory?: string;
// --- Team Mode fields (Phase 5) ---
teamId?: string;
teamServerUrl?: string;
teamRole?: 'owner' | 'admin' | 'member' | 'viewer';
teamUserId?: string;
// --- Tone/Voice (Wave 7.3) ---
tone?: 'professional' | 'casual' | 'technical' | 'legal' | 'marketing';
// --- Budget ---
budget?: number | null;
// --- AI Act compliance (L-17 C2) ---
/** Initial risk level (usually derived from template). */
riskLevel?: AIActRiskLevel;
// --- Optimization fields (GEPA/Ax) ---
optimizationEnabled?: boolean;
optimizationBudget?: number;
// --- UX-Refactor V2 fields (PRD §15.3) ---
/** Free-text description shown in the workspace header/cards. */
description?: string;
/** Workspace classification (defaults derivable from templateId/group). */
type?: WorkspaceType;
}
interface WorkspacesMeta {
defaultWorkspace?: string | null;
}
/**
* WorkspaceManager manages workspace CRUD, groups, and directory structure.
* Each workspace lives under {baseDir}/workspaces/{id}/ with:
* - workspace.json (config)
* - workspace.mind (SQLite .mind file, created empty)
* - sessions/ (JSONL session logs)
*/
export class WorkspaceManager {
private readonly workspacesDir: string;
private readonly metaPath: string;
constructor(private readonly baseDir: string) {
this.workspacesDir = path.join(baseDir, 'workspaces');
this.metaPath = path.join(baseDir, 'workspaces-meta.json');
if (!fs.existsSync(this.workspacesDir)) {
fs.mkdirSync(this.workspacesDir, { recursive: true });
}
}
/**
* Create a new workspace with directory structure and config.
*/
create(options: CreateWorkspaceOptions): WorkspaceConfig {
return this.createWithId(this.generateId(options.name), options);
}
/**
* Ensure a workspace with the given id exists. Idempotent — returns the
* existing config unchanged if the workspace already exists; otherwise
* creates it with the supplied id (bypassing slug-collision handling in
* generateId, since callers construct ids from trusted internal state
* like CWD-derived prefixes — e.g. SessionStart hooks).
*
* Use this from auto-attach paths (e.g. save_memory with a workspace arg
* that names a workspace not yet created on disk). Direct-create flows
* with user-supplied names should still go through `create()` so the
* generateId collision logic runs.
*/
// Reverse-ported from OSS hive-mind (oss-drift triage R4, 2026-06-11).
ensure(id: string, options: Partial<CreateWorkspaceOptions> = {}): WorkspaceConfig {
const existing = this.get(id);
if (existing) return existing;
return this.createWithId(id, {
...options,
name: options.name ?? id,
group: options.group ?? 'auto',
});
}
/**
* Shared create path: write directory structure + config for an exact id.
*/
private createWithId(id: string, options: CreateWorkspaceOptions): WorkspaceConfig {
const wsDir = path.join(this.workspacesDir, id);
fs.mkdirSync(wsDir, { recursive: true });
fs.mkdirSync(path.join(wsDir, 'sessions'), { recursive: true });
// Touch workspace.mind — MindDB will init schema when first opened
fs.writeFileSync(path.join(wsDir, 'workspace.mind'), '');
const config: WorkspaceConfig = {
id,
name: options.name,
group: options.group,
...(options.icon !== undefined && { icon: options.icon }),
...(options.model !== undefined && { model: options.model }),
...(options.personality !== undefined && { personality: options.personality }),
...(options.personaId !== undefined && { personaId: options.personaId }),
...(options.templateId !== undefined && { templateId: options.templateId }),
...(options.tools !== undefined && { tools: options.tools }),
...(options.skills !== undefined && { skills: options.skills }),
...(options.team !== undefined && { team: options.team }),
...(options.directory !== undefined && { directory: options.directory }),
...(options.teamId !== undefined && { teamId: options.teamId }),
...(options.teamServerUrl !== undefined && { teamServerUrl: options.teamServerUrl }),
...(options.teamRole !== undefined && { teamRole: options.teamRole }),
...(options.teamUserId !== undefined && { teamUserId: options.teamUserId }),
...(options.tone !== undefined && { tone: options.tone }),
...(options.optimizationEnabled !== undefined && { optimizationEnabled: options.optimizationEnabled }),
...(options.optimizationBudget !== undefined && { optimizationBudget: options.optimizationBudget }),
...(options.riskLevel !== undefined && {
riskLevel: options.riskLevel,
riskClassifiedAt: new Date().toISOString(),
}),
created: new Date().toISOString(),
};
fs.writeFileSync(
path.join(wsDir, 'workspace.json'),
JSON.stringify(config, null, 2),
'utf-8'
);
return config;
}
/**
* List all workspaces by reading workspace.json from each subdirectory.
*/
list(): WorkspaceConfig[] {
if (!fs.existsSync(this.workspacesDir)) return [];
const entries = fs.readdirSync(this.workspacesDir, { withFileTypes: true });
const configs: WorkspaceConfig[] = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const configPath = path.join(this.workspacesDir, entry.name, 'workspace.json');
if (fs.existsSync(configPath)) {
const raw = fs.readFileSync(configPath, 'utf-8');
configs.push(JSON.parse(raw) as WorkspaceConfig);
}
}
return configs;
}
/**
* List workspaces filtered by group name.
*/
listByGroup(group: string): WorkspaceConfig[] {
return this.list().filter(ws => ws.group === group);
}
/**
* List all unique group names.
*/
listGroups(): string[] {
const groups = new Set(this.list().map(ws => ws.group));
return [...groups];
}
/**
* Get a workspace by ID. Returns null if not found.
*/
get(id: string): WorkspaceConfig | null {
const configPath = path.join(this.workspacesDir, id, 'workspace.json');
if (!fs.existsSync(configPath)) return null;
const raw = fs.readFileSync(configPath, 'utf-8');
return JSON.parse(raw) as WorkspaceConfig;
}
/**
* Partially update a workspace config.
* When `riskLevel` changes, `riskClassifiedAt` is auto-stamped with the
* current ISO timestamp (EU AI Act Art. 14 provenance requirement).
*/
update(id: string, updates: Partial<Omit<WorkspaceConfig, 'id' | 'created'>>): void {
const existing = this.get(id);
if (!existing) throw new Error(`Workspace not found: ${id}`);
const now = new Date().toISOString();
const stamped: Partial<WorkspaceConfig> =
'riskLevel' in updates && updates.riskLevel !== existing.riskLevel
? { ...updates, riskClassifiedAt: now, updatedAt: now }
: { ...updates, updatedAt: now };
const updated = { ...existing, ...stamped };
const configPath = path.join(this.workspacesDir, id, 'workspace.json');
fs.writeFileSync(configPath, JSON.stringify(updated, null, 2), 'utf-8');
}
/**
* Delete a workspace by removing its entire directory.
*/
delete(id: string): void {
const wsDir = path.join(this.workspacesDir, id);
if (fs.existsSync(wsDir)) {
fs.rmSync(wsDir, { recursive: true, force: true });
}
}
/**
* Check whether a workspace is team-connected (has a teamId).
*/
isTeamWorkspace(id: string): boolean {
const ws = this.get(id);
return ws !== null && typeof ws.teamId === 'string' && ws.teamId.length > 0;
}
/**
* List only team-connected workspaces.
*/
listTeamWorkspaces(): WorkspaceConfig[] {
return this.list().filter(ws => typeof ws.teamId === 'string' && ws.teamId.length > 0);
}
/**
* Get the path to a workspace's .mind file.
*/
getMindPath(id: string): string {
return path.join(this.workspacesDir, id, 'workspace.mind');
}
/**
* Set the default workspace ID in workspaces-meta.json.
*/
setDefault(id: string): void {
if (!this.get(id)) throw new Error(`Workspace not found: ${id}`);
const meta = this.loadMeta();
meta.defaultWorkspace = id;
this.saveMeta(meta);
}
/**
* Get the default workspace ID. Returns null if none set.
*/
getDefault(): string | null {
const meta = this.loadMeta();
return meta.defaultWorkspace ?? null;
}
/**
* Ensure at least one workspace exists. If none, create a default one
* and mark it as the default. Idempotent — safe to call on every startup.
*/
ensureDefault(options?: Partial<CreateWorkspaceOptions>): WorkspaceConfig {
const existing = this.list();
if (existing.length > 0) {
const defaultId = this.getDefault();
const found = defaultId ? this.get(defaultId) : null;
return found ?? existing[0];
}
const ws = this.create({
name: 'Default Workspace',
group: 'Personal',
personaId: 'researcher',
...options,
});
this.setDefault(ws.id);
return ws;
}
/**
* Generate a slug-based ID from a workspace name.
* Handles duplicates by appending -2, -3, etc.
*/
generateId(name: string): string {
const base = name
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
if (!this.workspaceExists(base)) return base;
let counter = 2;
while (this.workspaceExists(`${base}-${counter}`)) {
counter++;
}
return `${base}-${counter}`;
}
private workspaceExists(id: string): boolean {
return fs.existsSync(path.join(this.workspacesDir, id));
}
private loadMeta(): WorkspacesMeta {
if (fs.existsSync(this.metaPath)) {
const raw = fs.readFileSync(this.metaPath, 'utf-8');
return JSON.parse(raw) as WorkspacesMeta;
}
return {};
}
private saveMeta(meta: WorkspacesMeta): void {
fs.writeFileSync(this.metaPath, JSON.stringify(meta, null, 2), 'utf-8');
}
}