@ignitionai/cogitator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/dist/extensions/index.js +63 -0
- package/dist/src/agents.js +238 -0
- package/dist/src/board.js +277 -0
- package/dist/src/cli.js +71 -0
- package/dist/src/config.js +7 -0
- package/dist/src/conversations.js +41 -0
- package/dist/src/cron.js +227 -0
- package/dist/src/db.js +128 -0
- package/dist/src/default-agent.js +154 -0
- package/dist/src/file-changes.js +113 -0
- package/dist/src/history.js +88 -0
- package/dist/src/json-files.js +18 -0
- package/dist/src/mcp-env.js +29 -0
- package/dist/src/mcp-server.js +167 -0
- package/dist/src/paths.js +22 -0
- package/dist/src/pi.js +23 -0
- package/dist/src/pool.js +183 -0
- package/dist/src/registry.js +171 -0
- package/dist/src/schemas.js +134 -0
- package/dist/src/server.js +829 -0
- package/dist/src/session-lines.js +27 -0
- package/dist/src/skills-import.js +167 -0
- package/dist/src/spawn.js +39 -0
- package/dist/src/spawner.js +28 -0
- package/dist/src/workspace-files.js +93 -0
- package/dist/src/workspaces.js +81 -0
- package/package.json +60 -0
- package/web/dist/assets/index-BoKe95MT.js +12 -0
- package/web/dist/assets/index-SltX54ZZ.css +1 -0
- package/web/dist/assets/inter-cyrillic-400-normal-HOLc17fK.woff +0 -0
- package/web/dist/assets/inter-cyrillic-400-normal-obahsSVq.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-500-normal-BasfLYem.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-500-normal-CxZf_p3X.woff +0 -0
- package/web/dist/assets/inter-cyrillic-600-normal-4D_pXhcN.woff +0 -0
- package/web/dist/assets/inter-cyrillic-600-normal-CWCymEST.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-ext-400-normal-BQZuk6qB.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-ext-400-normal-DQukG94-.woff +0 -0
- package/web/dist/assets/inter-cyrillic-ext-500-normal-B0yAr1jD.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-ext-500-normal-BmqWE9Dz.woff +0 -0
- package/web/dist/assets/inter-cyrillic-ext-600-normal-Bcila6Z-.woff +0 -0
- package/web/dist/assets/inter-cyrillic-ext-600-normal-Dfes3d0z.woff2 +0 -0
- package/web/dist/assets/inter-greek-400-normal-B4URO6DV.woff2 +0 -0
- package/web/dist/assets/inter-greek-400-normal-q2sYcFCs.woff +0 -0
- package/web/dist/assets/inter-greek-500-normal-BIZE56-Y.woff2 +0 -0
- package/web/dist/assets/inter-greek-500-normal-Xzm54t5V.woff +0 -0
- package/web/dist/assets/inter-greek-600-normal-BZpKdvQh.woff +0 -0
- package/web/dist/assets/inter-greek-600-normal-plRanbMR.woff2 +0 -0
- package/web/dist/assets/inter-greek-ext-400-normal-DGGRlc-M.woff2 +0 -0
- package/web/dist/assets/inter-greek-ext-400-normal-KugGGMne.woff +0 -0
- package/web/dist/assets/inter-greek-ext-500-normal-2j5mBUwD.woff +0 -0
- package/web/dist/assets/inter-greek-ext-500-normal-C4iEst2y.woff2 +0 -0
- package/web/dist/assets/inter-greek-ext-600-normal-B8X0CLgF.woff +0 -0
- package/web/dist/assets/inter-greek-ext-600-normal-DRtmH8MT.woff2 +0 -0
- package/web/dist/assets/inter-latin-400-normal-C38fXH4l.woff2 +0 -0
- package/web/dist/assets/inter-latin-400-normal-CyCys3Eg.woff +0 -0
- package/web/dist/assets/inter-latin-500-normal-BL9OpVg8.woff +0 -0
- package/web/dist/assets/inter-latin-500-normal-Cerq10X2.woff2 +0 -0
- package/web/dist/assets/inter-latin-600-normal-CiBQ2DWP.woff +0 -0
- package/web/dist/assets/inter-latin-600-normal-LgqL8muc.woff2 +0 -0
- package/web/dist/assets/inter-latin-ext-400-normal-77YHD8bZ.woff +0 -0
- package/web/dist/assets/inter-latin-ext-400-normal-C1nco2VV.woff2 +0 -0
- package/web/dist/assets/inter-latin-ext-500-normal-BxGbmqWO.woff +0 -0
- package/web/dist/assets/inter-latin-ext-500-normal-CV4jyFjo.woff2 +0 -0
- package/web/dist/assets/inter-latin-ext-600-normal-CIVaiw4L.woff +0 -0
- package/web/dist/assets/inter-latin-ext-600-normal-D2bJ5OIk.woff2 +0 -0
- package/web/dist/assets/inter-vietnamese-400-normal-Bbgyi5SW.woff +0 -0
- package/web/dist/assets/inter-vietnamese-400-normal-DMkecbls.woff2 +0 -0
- package/web/dist/assets/inter-vietnamese-500-normal-DOriooB6.woff2 +0 -0
- package/web/dist/assets/inter-vietnamese-500-normal-mJboJaSs.woff +0 -0
- package/web/dist/assets/inter-vietnamese-600-normal-BuLX-rYi.woff +0 -0
- package/web/dist/assets/inter-vietnamese-600-normal-Cc8MFFhd.woff2 +0 -0
- package/web/dist/favicon.png +0 -0
- package/web/dist/icon.png +0 -0
- package/web/dist/index.html +16 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* Lecture best-effort des transcripts `.jsonl` pi — partagée par l'historique et le suivi
|
|
4
|
+
* des fichiers (un transcript absent ou illisible ne doit jamais faire échouer une route).
|
|
5
|
+
*/
|
|
6
|
+
/** Dernières lignes non vides du transcript, plafonnées (les gros .jsonl ne sont pas relus en entier). */
|
|
7
|
+
export function readSessionLines(sessionFile, maxLines) {
|
|
8
|
+
if (!sessionFile || !existsSync(sessionFile))
|
|
9
|
+
return [];
|
|
10
|
+
try {
|
|
11
|
+
return readFileSync(sessionFile, "utf8").split("\n").filter((l) => l.trim()).slice(-maxLines);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Parse une ligne de transcript en objet ; null si la ligne n'est pas un objet JSON. */
|
|
18
|
+
export function parseJsonLine(line) {
|
|
19
|
+
let parsed;
|
|
20
|
+
try {
|
|
21
|
+
parsed = JSON.parse(line);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return parsed && typeof parsed === "object" ? parsed : null;
|
|
27
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { basename, join } from "node:path";
|
|
5
|
+
/** Bornes de l'exploration et des transferts (une source hostile ne doit pas saturer le disque). */
|
|
6
|
+
const SCAN_MAX_DEPTH = 2;
|
|
7
|
+
const SCAN_MAX_SKILLS = 50;
|
|
8
|
+
const ARCHIVE_MAX_BYTES = 20 * 1024 * 1024;
|
|
9
|
+
const CLI_MAX_BUFFER = 5 * 1024 * 1024;
|
|
10
|
+
const CLI_TIMEOUT_MS = 180_000;
|
|
11
|
+
const CLI_OUTPUT_TAIL_LINES = 3;
|
|
12
|
+
/** Nom de dossier sûr pour un skill (kebab-case, pas de traversal). */
|
|
13
|
+
export function sanitizeSkillName(name) {
|
|
14
|
+
const clean = basename(name).replace(/[^a-zA-Z0-9-_]+/g, "-").replace(/^-+|-+$/g, "");
|
|
15
|
+
return clean || "skill";
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Trouve les dossiers contenant un SKILL.md sous root (profondeur ≤ 2,
|
|
19
|
+
* node_modules/.git/cachés exclus). Les sous-dossiers d'un dossier-skill ne sont pas remontés.
|
|
20
|
+
*/
|
|
21
|
+
export function scanSkillDirs(root, depth = 0, found = []) {
|
|
22
|
+
if (depth > SCAN_MAX_DEPTH || found.length > SCAN_MAX_SKILLS)
|
|
23
|
+
return found;
|
|
24
|
+
let entries;
|
|
25
|
+
try {
|
|
26
|
+
entries = readdirSync(root, { withFileTypes: true });
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
if (entries.some((e) => e.isFile() && e.name === "SKILL.md")) {
|
|
32
|
+
found.push(root);
|
|
33
|
+
return found; // c'est un skill : ne pas descendre dedans
|
|
34
|
+
}
|
|
35
|
+
for (const e of entries) {
|
|
36
|
+
if (!e.isDirectory() || e.name.startsWith(".") || e.name === "node_modules")
|
|
37
|
+
continue;
|
|
38
|
+
scanSkillDirs(join(root, e.name), depth + 1, found);
|
|
39
|
+
}
|
|
40
|
+
return found;
|
|
41
|
+
}
|
|
42
|
+
/** Copie les skills trouvés sous srcDir vers destRoot (~/.agents/skills). */
|
|
43
|
+
export function importFromDir(srcDir, destRoot, overwrite = false) {
|
|
44
|
+
const result = { imported: [], skipped: [] };
|
|
45
|
+
const skills = scanSkillDirs(srcDir);
|
|
46
|
+
mkdirSync(destRoot, { recursive: true });
|
|
47
|
+
for (const dir of skills) {
|
|
48
|
+
const name = sanitizeSkillName(basename(dir));
|
|
49
|
+
const target = join(destRoot, name);
|
|
50
|
+
if (existsSync(target) && !overwrite) {
|
|
51
|
+
result.skipped.push(name);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
rmSync(target, { recursive: true, force: true });
|
|
55
|
+
cpSync(dir, target, { recursive: true });
|
|
56
|
+
result.imported.push(name);
|
|
57
|
+
}
|
|
58
|
+
if (skills.length === 0)
|
|
59
|
+
result.error = `aucun SKILL.md trouvé sous ${srcDir}`;
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
/** Normalise une URL GitHub (https://github.com/o/r, o/r, git@…:o/r.git) → { owner, repo }. */
|
|
63
|
+
export function parseGitHubRepo(source) {
|
|
64
|
+
const s = source.trim().replace(/\.git$/, "").replace(/^git@github\.com:/, "");
|
|
65
|
+
const m = s.match(/^(?:https?:\/\/github\.com\/)?([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\/.*)?$/);
|
|
66
|
+
if (!m)
|
|
67
|
+
return null;
|
|
68
|
+
return { owner: m[1], repo: m[2] };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Importe les skills d'un repo GitHub : tarball (branche main, repli master) →
|
|
72
|
+
* extraction via le `tar` système → copie des dossiers SKILL.md.
|
|
73
|
+
* ponytail: extraction déléguée au tar système (macOS/Linux) ; Windows nécessiterait une implémentation native ou la dép `tar`.
|
|
74
|
+
*/
|
|
75
|
+
export async function importFromGitHub(source, destRoot, overwrite = false) {
|
|
76
|
+
const repo = parseGitHubRepo(source);
|
|
77
|
+
if (!repo)
|
|
78
|
+
return { imported: [], skipped: [], error: `source GitHub invalide: ${source}` };
|
|
79
|
+
const tmp = mkdtempSync(join(tmpdir(), "cog-skills-"));
|
|
80
|
+
try {
|
|
81
|
+
let archive = null;
|
|
82
|
+
for (const branch of ["main", "master"]) {
|
|
83
|
+
const url = `https://codeload.github.com/${repo.owner}/${repo.repo}/tar.gz/refs/heads/${branch}`;
|
|
84
|
+
const res = await fetch(url);
|
|
85
|
+
if (res.ok) {
|
|
86
|
+
archive = Buffer.from(await res.arrayBuffer());
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!archive)
|
|
91
|
+
return { imported: [], skipped: [], error: `repo introuvable ou privé: ${repo.owner}/${repo.repo}` };
|
|
92
|
+
if (archive.length > ARCHIVE_MAX_BYTES)
|
|
93
|
+
return { imported: [], skipped: [], error: "tarball > 20 Mo — refusé" };
|
|
94
|
+
const tgz = join(tmp, "repo.tar.gz");
|
|
95
|
+
writeFileSync(tgz, archive);
|
|
96
|
+
const extractDir = join(tmp, "extract");
|
|
97
|
+
mkdirSync(extractDir, { recursive: true });
|
|
98
|
+
await new Promise((resolve, reject) => {
|
|
99
|
+
execFile("tar", ["-xzf", tgz, "-C", extractDir], (err) => (err ? reject(err) : resolve()));
|
|
100
|
+
});
|
|
101
|
+
const roots = readdirSync(extractDir).filter((e) => statSync(join(extractDir, e)).isDirectory());
|
|
102
|
+
if (roots.length === 0)
|
|
103
|
+
return { imported: [], skipped: [], error: "tarball vide" };
|
|
104
|
+
return importFromDir(join(extractDir, roots[0]), destRoot, overwrite);
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
return { imported: [], skipped: [], error: err instanceof Error ? err.message : String(err) };
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Point d'entrée unique : URL GitHub, chemin local, ou commande npx/npm. */
|
|
114
|
+
export function importSkills(source, destRoot, overwrite = false) {
|
|
115
|
+
if (/^npx\s|^npm\s/.test(source.trim())) {
|
|
116
|
+
return runSkillCommand(source.trim(), [destRoot]);
|
|
117
|
+
}
|
|
118
|
+
if (/^(https?:\/\/github\.com\/|git@github\.com:|[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+?$)/.test(source.trim()) && !existsSync(source)) {
|
|
119
|
+
return importFromGitHub(source, destRoot, overwrite);
|
|
120
|
+
}
|
|
121
|
+
if (existsSync(source) && statSync(source).isDirectory()) {
|
|
122
|
+
return Promise.resolve(importFromDir(source, destRoot, overwrite));
|
|
123
|
+
}
|
|
124
|
+
return Promise.resolve({ imported: [], skipped: [], error: `source non reconnue: ${source}` });
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Exécute un CLI d'installation de skills (npx/npm) et rapporte les skills apparus.
|
|
128
|
+
* Sécurité : npx/npm uniquement (pas de shell), timeout 3 min, args par découpage simple
|
|
129
|
+
* (pas de quotes — limitation assumée, ponytail: découper avec un vrai shell-quote si besoin).
|
|
130
|
+
*/
|
|
131
|
+
export async function runSkillCommand(cmd, destRoots, timeoutMs = CLI_TIMEOUT_MS) {
|
|
132
|
+
const tokens = cmd.split(/\s+/).filter(Boolean);
|
|
133
|
+
const bin = tokens[0];
|
|
134
|
+
if (bin !== "npx" && bin !== "npm") {
|
|
135
|
+
return { imported: [], skipped: [], error: "seuls npx et npm sont autorisés comme commande d'import" };
|
|
136
|
+
}
|
|
137
|
+
const snapshot = new Set();
|
|
138
|
+
for (const root of destRoots) {
|
|
139
|
+
for (const dir of scanSkillDirs(root))
|
|
140
|
+
snapshot.add(sanitizeSkillName(basename(dir)));
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
const { stdout, stderr } = await new Promise((resolve) => {
|
|
144
|
+
// certains CLI renvoient un code non nul tout en ayant installé : on garde la sortie
|
|
145
|
+
execFile(bin, tokens.slice(1), { timeout: timeoutMs, maxBuffer: CLI_MAX_BUFFER }, (_err, out, serr) => {
|
|
146
|
+
resolve({ stdout: String(out), stderr: String(serr) });
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
const imported = [];
|
|
150
|
+
for (const root of destRoots) {
|
|
151
|
+
for (const dir of scanSkillDirs(root)) {
|
|
152
|
+
const name = sanitizeSkillName(basename(dir));
|
|
153
|
+
if (!snapshot.has(name))
|
|
154
|
+
imported.push(name);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const result = { imported: [...new Set(imported)], skipped: [] };
|
|
158
|
+
if (result.imported.length === 0) {
|
|
159
|
+
const tail = (stderr || stdout).trim().split("\n").slice(-CLI_OUTPUT_TAIL_LINES).join(" | ");
|
|
160
|
+
result.error = `commande exécutée mais aucun skill nouveau détecté${tail ? ` — sortie: ${tail}` : ""}`;
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
return { imported: [], skipped: [], error: err instanceof Error ? err.message : String(err) };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
/** Snapshot d'un preset d'agent (type structurel : pas de dépendance vers agents.js). */
|
|
4
|
+
export function spawnConfigFromPreset(preset) {
|
|
5
|
+
return {
|
|
6
|
+
provider: preset.provider,
|
|
7
|
+
model: preset.model,
|
|
8
|
+
thinking: preset.thinking,
|
|
9
|
+
systemPrompt: preset.system_prompt || undefined,
|
|
10
|
+
skills: preset.skills,
|
|
11
|
+
tools: preset.tools_allowlist,
|
|
12
|
+
mcpServers: preset.mcp_servers,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function modelArg(c) {
|
|
16
|
+
return c.thinking ? `${c.provider}/${c.model}:${c.thinking}` : `${c.provider}/${c.model}`;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Matérialise un SpawnConfig en flags natifs pi (ADR-001).
|
|
20
|
+
* Effet de bord unique : écrit le fichier du system prompt dans tmpDir.
|
|
21
|
+
*/
|
|
22
|
+
export function buildArgs(c, tmpDir, convId) {
|
|
23
|
+
mkdirSync(tmpDir, { recursive: true });
|
|
24
|
+
// pi n'accepte pas la forme --flag=valeur : tout passe en deux éléments
|
|
25
|
+
const args = ["--model", modelArg(c)];
|
|
26
|
+
if (c.systemPrompt) {
|
|
27
|
+
const file = join(tmpDir, `${convId}.prompt.md`);
|
|
28
|
+
writeFileSync(file, c.systemPrompt);
|
|
29
|
+
args.push("--append-system-prompt", file);
|
|
30
|
+
}
|
|
31
|
+
if (c.skills?.length) {
|
|
32
|
+
args.push("--no-skills"); // O5 : skills exactes, jamais de fusion avec la découverte globale
|
|
33
|
+
for (const skill of c.skills)
|
|
34
|
+
args.push("--skill", skill);
|
|
35
|
+
}
|
|
36
|
+
if (c.tools?.length)
|
|
37
|
+
args.push("--tools", c.tools.join(","));
|
|
38
|
+
return args;
|
|
39
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { setConversationSession } from "./conversations.js";
|
|
5
|
+
import { MCP_ENV_VAR, encodeMcpEnv } from "./mcp-env.js";
|
|
6
|
+
import { buildArgs } from "./spawn.js";
|
|
7
|
+
/** Fabrique unique du mécanisme de spawn, partagé par le serveur HTTP et le CronService. */
|
|
8
|
+
export function makeSpawner(opts) {
|
|
9
|
+
const { db, pool, paths } = opts;
|
|
10
|
+
const tmpDir = join(paths.home, "tmp");
|
|
11
|
+
mkdirSync(tmpDir, { recursive: true });
|
|
12
|
+
return {
|
|
13
|
+
tmpDir,
|
|
14
|
+
async ensure(conv) {
|
|
15
|
+
const spawn = JSON.parse(conv.spawn_args || "{}");
|
|
16
|
+
const mcpEnv = encodeMcpEnv(spawn.mcpServers ?? []);
|
|
17
|
+
const result = await pool.ensure(conv.id, {
|
|
18
|
+
cwd: conv.workspace_dir ?? homedir(),
|
|
19
|
+
args: buildArgs(spawn, tmpDir, conv.id),
|
|
20
|
+
resumeSessionFile: conv.session_file,
|
|
21
|
+
env: mcpEnv ? { [MCP_ENV_VAR]: mcpEnv } : undefined,
|
|
22
|
+
});
|
|
23
|
+
if (result.sessionFile)
|
|
24
|
+
setConversationSession(db, conv.id, result.sessionFile);
|
|
25
|
+
return result;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { extname, basename, join, normalize, resolve, sep } from "node:path";
|
|
3
|
+
const SKIP_DIRS = new Set(["node_modules", ".git", "dist", "build", ".next", ".venv", "__pycache__", "coverage", ".turbo"]);
|
|
4
|
+
const MAX_DEPTH = 5;
|
|
5
|
+
const MAX_NODES = 500;
|
|
6
|
+
const MAX_FILE_CHARS = 400_000;
|
|
7
|
+
function buildTree(dir, depth, budget) {
|
|
8
|
+
if (depth > MAX_DEPTH || budget.count > MAX_NODES)
|
|
9
|
+
return [];
|
|
10
|
+
let entries;
|
|
11
|
+
try {
|
|
12
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
const nodes = [];
|
|
18
|
+
for (const e of entries) {
|
|
19
|
+
if (budget.count > MAX_NODES)
|
|
20
|
+
break;
|
|
21
|
+
if (e.name.startsWith(".") && e.name !== ".env.example")
|
|
22
|
+
continue;
|
|
23
|
+
const path = join(dir, e.name);
|
|
24
|
+
if (e.isDirectory()) {
|
|
25
|
+
if (SKIP_DIRS.has(e.name))
|
|
26
|
+
continue;
|
|
27
|
+
budget.count += 1;
|
|
28
|
+
nodes.push({ name: e.name, path, type: "dir", children: buildTree(path, depth + 1, budget) });
|
|
29
|
+
}
|
|
30
|
+
else if (e.isFile()) {
|
|
31
|
+
budget.count += 1;
|
|
32
|
+
let size = 0;
|
|
33
|
+
try {
|
|
34
|
+
size = statSync(path).size;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
/* inaccessible */
|
|
38
|
+
}
|
|
39
|
+
nodes.push({ name: e.name, path, type: "file", size });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
nodes.sort((a, b) => (a.type === b.type ? a.name.localeCompare(b.name) : a.type === "dir" ? -1 : 1));
|
|
43
|
+
return nodes;
|
|
44
|
+
}
|
|
45
|
+
/** Arborescence du workspace (read-only, profondeur et volume plafonnés). */
|
|
46
|
+
export function workspaceTree(rootDir) {
|
|
47
|
+
return buildTree(rootDir, 0, { count: 0 });
|
|
48
|
+
}
|
|
49
|
+
const TEXT_EXT = new Set([
|
|
50
|
+
".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".json", ".md", ".mdx", ".css", ".scss", ".html", ".xml",
|
|
51
|
+
".yml", ".yaml", ".toml", ".ini", ".cfg", ".env", ".txt", ".py", ".rs", ".go", ".java", ".c", ".h",
|
|
52
|
+
".cpp", ".hpp", ".sh", ".bash", ".zsh", ".sql", ".graphql", ".prisma", ".csv", ".svg", ".pyi", ".lock",
|
|
53
|
+
]);
|
|
54
|
+
/**
|
|
55
|
+
* Lit un fichier du workspace (read-only). Sécurité : le chemin résolu doit rester
|
|
56
|
+
* sous la racine du workspace (anti-traversal), taille plafonnée, extensions binaires refusées.
|
|
57
|
+
*/
|
|
58
|
+
export function readWorkspaceFile(rootDir, requested) {
|
|
59
|
+
const root = resolve(rootDir);
|
|
60
|
+
const target = resolve(normalize(requested));
|
|
61
|
+
if (target !== root && !target.startsWith(root + sep)) {
|
|
62
|
+
return { error: "chemin hors du workspace" };
|
|
63
|
+
}
|
|
64
|
+
if (!existsSync(target))
|
|
65
|
+
return { error: `fichier introuvable: ${requested}` };
|
|
66
|
+
let stat;
|
|
67
|
+
try {
|
|
68
|
+
stat = statSync(target);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return { error: "fichier illisible" };
|
|
72
|
+
}
|
|
73
|
+
if (stat.isDirectory())
|
|
74
|
+
return { error: "c'est un dossier" };
|
|
75
|
+
const ext = extname(target).toLowerCase();
|
|
76
|
+
if (!TEXT_EXT.has(ext)) {
|
|
77
|
+
return { error: `extension non affichable en texte: ${ext || "(aucune)"}` };
|
|
78
|
+
}
|
|
79
|
+
if (stat.size > MAX_FILE_CHARS * 2) {
|
|
80
|
+
return { error: `fichier trop volumineux (${Math.round(stat.size / 1024)} Ko)` };
|
|
81
|
+
}
|
|
82
|
+
let content;
|
|
83
|
+
try {
|
|
84
|
+
content = readFileSync(target, "utf8");
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return { error: "lecture impossible (binaire ?)" };
|
|
88
|
+
}
|
|
89
|
+
const truncated = content.length > MAX_FILE_CHARS;
|
|
90
|
+
if (truncated)
|
|
91
|
+
content = content.slice(0, MAX_FILE_CHARS);
|
|
92
|
+
return { file: { path: target, name: basename(target), size: stat.size, content, truncated } };
|
|
93
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
|
+
export function listWorkspaces(db) {
|
|
5
|
+
return db
|
|
6
|
+
.prepare(`SELECT w.*, (SELECT COUNT(*) FROM conversation c WHERE c.workspace_id = w.id) AS conversation_count
|
|
7
|
+
FROM workspace w ORDER BY w.name`)
|
|
8
|
+
.all();
|
|
9
|
+
}
|
|
10
|
+
export function getWorkspace(db, id) {
|
|
11
|
+
const row = db.prepare("SELECT * FROM workspace WHERE id = ?").get(id);
|
|
12
|
+
return row ?? null;
|
|
13
|
+
}
|
|
14
|
+
export function getWorkspaceByDir(db, dir) {
|
|
15
|
+
const row = db.prepare("SELECT * FROM workspace WHERE dir = ?").get(dir);
|
|
16
|
+
return row ?? null;
|
|
17
|
+
}
|
|
18
|
+
/** Un agent par défaut référencé doit exister (FK logique, pas de contrainte SQLite sur celui-ci). */
|
|
19
|
+
function agentExists(db, id) {
|
|
20
|
+
return Boolean(db.prepare("SELECT 1 FROM agent_preset WHERE id = ?").get(id));
|
|
21
|
+
}
|
|
22
|
+
/** Crée un workspace. Invariant (domain model) : le dossier doit exister ; dir unique. */
|
|
23
|
+
export function createWorkspace(db, input) {
|
|
24
|
+
if (!existsSync(input.dir))
|
|
25
|
+
return { error: `dossier introuvable: ${input.dir}` };
|
|
26
|
+
if (!statSync(input.dir).isDirectory())
|
|
27
|
+
return { error: `n'est pas un dossier: ${input.dir}` };
|
|
28
|
+
let dir;
|
|
29
|
+
try {
|
|
30
|
+
dir = realpathSync(input.dir); // canonicalise (symlinks, trailing /)
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return { error: `dossier illisible: ${input.dir}` };
|
|
34
|
+
}
|
|
35
|
+
if (getWorkspaceByDir(db, dir))
|
|
36
|
+
return { error: `workspace déjà enregistré pour ${dir}` };
|
|
37
|
+
if (input.default_agent_id && !agentExists(db, input.default_agent_id)) {
|
|
38
|
+
return { error: `agent par défaut introuvable: ${input.default_agent_id}` };
|
|
39
|
+
}
|
|
40
|
+
const id = randomUUID();
|
|
41
|
+
const name = input.name?.trim() || basename(dir) || dir;
|
|
42
|
+
db.prepare("INSERT INTO workspace (id, dir, name, default_agent_id) VALUES (?, ?, ?, ?)").run(id, dir, name, input.default_agent_id ?? null);
|
|
43
|
+
return { workspace: getWorkspace(db, id) };
|
|
44
|
+
}
|
|
45
|
+
/** Renvoie null si le workspace ou l'agent par défaut visé n'existe pas. */
|
|
46
|
+
export function updateWorkspace(db, id, input) {
|
|
47
|
+
const existing = getWorkspace(db, id);
|
|
48
|
+
if (!existing)
|
|
49
|
+
return null;
|
|
50
|
+
if (input.default_agent_id && !agentExists(db, input.default_agent_id))
|
|
51
|
+
return null;
|
|
52
|
+
db.prepare("UPDATE workspace SET name = ?, default_agent_id = ? WHERE id = ?").run(input.name?.trim() || existing.name, input.default_agent_id === undefined ? existing.default_agent_id : input.default_agent_id, id);
|
|
53
|
+
return getWorkspace(db, id);
|
|
54
|
+
}
|
|
55
|
+
export function deleteWorkspace(db, id) {
|
|
56
|
+
return db.prepare("DELETE FROM workspace WHERE id = ?").run(id).changes > 0;
|
|
57
|
+
}
|
|
58
|
+
export function browseDir(path) {
|
|
59
|
+
const target = path ?? process.env.HOME ?? "/";
|
|
60
|
+
let resolved;
|
|
61
|
+
try {
|
|
62
|
+
resolved = realpathSync(target);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return { error: `dossier introuvable: ${target}` };
|
|
66
|
+
}
|
|
67
|
+
if (!statSync(resolved).isDirectory())
|
|
68
|
+
return { error: `n'est pas un dossier: ${target}` };
|
|
69
|
+
let entries;
|
|
70
|
+
try {
|
|
71
|
+
entries = readdirSync(resolved, { withFileTypes: true })
|
|
72
|
+
.filter((e) => !e.name.startsWith("."))
|
|
73
|
+
.map((e) => ({ name: e.name, path: join(resolved, e.name), type: e.isDirectory() ? "dir" : "file" }));
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return { error: `dossier illisible: ${target}` };
|
|
77
|
+
}
|
|
78
|
+
entries.sort((a, b) => (a.type === b.type ? a.name.localeCompare(b.name) : a.type === "dir" ? -1 : 1));
|
|
79
|
+
const parent = resolved === "/" ? null : join(resolved, "..");
|
|
80
|
+
return { path: resolved, parent, entries };
|
|
81
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ignitionai/cogitator",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Cogitator — panneau de contrôle web pour pi : agents composables, workspaces, conversations libres, cron. pi package.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=22.19.0"
|
|
12
|
+
},
|
|
13
|
+
"bin": {
|
|
14
|
+
"cogitator": "dist/src/cli.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/src",
|
|
18
|
+
"dist/extensions",
|
|
19
|
+
"web/dist",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "npm run typecheck && vite build web && tsc",
|
|
24
|
+
"build:server": "tsc",
|
|
25
|
+
"build:web": "vite build web",
|
|
26
|
+
"dev": "tsx src/cli.ts",
|
|
27
|
+
"dev:web": "vite web",
|
|
28
|
+
"test": "tsx --test test/*.test.ts",
|
|
29
|
+
"prepublishOnly": "npm run build && npm test",
|
|
30
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p web"
|
|
31
|
+
},
|
|
32
|
+
"pi": {
|
|
33
|
+
"extensions": [
|
|
34
|
+
"./dist/extensions/index.js"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@fontsource/inter": "^5.3.0",
|
|
42
|
+
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
43
|
+
"better-sqlite3": "^12.11.1",
|
|
44
|
+
"cron-parser": "^5.10.1",
|
|
45
|
+
"zod": "^4.6.5"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@earendil-works/pi-coding-agent": "^1.0.4",
|
|
49
|
+
"@types/better-sqlite3": "^9.6.0",
|
|
50
|
+
"@types/node": "^24.0.0",
|
|
51
|
+
"@types/react": "^19.3.0",
|
|
52
|
+
"@types/react-dom": "^19.3.0",
|
|
53
|
+
"@vitejs/plugin-react": "^6.1.2",
|
|
54
|
+
"react": "^19.3.0",
|
|
55
|
+
"react-dom": "^19.3.0",
|
|
56
|
+
"tsx": "^4.20.0",
|
|
57
|
+
"typescript": "^5.9.0",
|
|
58
|
+
"vite": "^8.3.3"
|
|
59
|
+
}
|
|
60
|
+
}
|