deep-projects 0.0.0-stage → 0.4.3

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/lib/index.js ADDED
@@ -0,0 +1,841 @@
1
+ // src/index.ts
2
+ import { homedir } from "node:os";
3
+ import { join as join2 } from "node:path";
4
+
5
+ // src/core.ts
6
+ import {
7
+ mkdir,
8
+ rm,
9
+ readdir,
10
+ readFile,
11
+ writeFile,
12
+ stat,
13
+ cp
14
+ } from "node:fs/promises";
15
+ import { join, basename } from "node:path";
16
+ var STATE_NAME = ".projects.json";
17
+ var AGENTS_NAME = "AGENTS.md";
18
+ var KNOWLEDGE_DIR = "knowledge";
19
+ var DEFAULT_AGENTS = `# Project instructions
20
+
21
+ Describe the project, its conventions, and anything the agent should always
22
+ know when working here.
23
+
24
+ Read files in knowledge/ when relevant.
25
+ `;
26
+ var MEMORY_NAME = "AGENTS.local.md";
27
+ var LEGACY_NOTES = "notes.md";
28
+ var MEMORY_MAX = 2e4;
29
+ var MEMORY_HEADER = `# Project memory
30
+
31
+ <!-- deep-projects: loaded into every chat in this project. Agents: the project goal
32
+ says what this project is for; keep your work aligned with it and update it with
33
+ projects_update when the user changes direction. Keep the memory list current: add
34
+ lasting facts, decisions and user preferences with projects_add_note; fix or remove
35
+ wrong or stale lines with projects_set_memory. One bullet per memory. -->
36
+ `;
37
+ var GOAL_OPEN = "## Project goal\n<!-- dsh-projects:goal -->\n";
38
+ var GOAL_CLOSE = "\n<!-- /dsh-projects:goal -->\n";
39
+ var GOAL_MAX = 2e3;
40
+ async function memoryFile(root, slug) {
41
+ const rec = (await listProjects(root)).find((r) => r.slug === slug);
42
+ if (!rec) throw new Error("no such project");
43
+ const dir = join(root, slug);
44
+ const path = join(dir, MEMORY_NAME);
45
+ try {
46
+ await stat(path);
47
+ } catch {
48
+ let legacy = "";
49
+ try {
50
+ legacy = await readFile(join(dir, LEGACY_NOTES), "utf8");
51
+ } catch {
52
+ }
53
+ if (legacy.trim()) {
54
+ await writeFile(path, MEMORY_HEADER + "\n" + legacy.trimEnd() + "\n");
55
+ await rm(join(dir, LEGACY_NOTES), { force: true });
56
+ }
57
+ }
58
+ return path;
59
+ }
60
+ function parseMemory(text) {
61
+ let rest = text.replace(/^# Project memory\n\n<!--[\s\S]*?-->\n/, "").replace(/^\n+/, "");
62
+ let goal = "";
63
+ if (rest.startsWith(GOAL_OPEN)) {
64
+ const end = rest.indexOf(GOAL_CLOSE);
65
+ if (end >= 0) {
66
+ goal = rest.slice(GOAL_OPEN.length, end).trim();
67
+ rest = rest.slice(end + GOAL_CLOSE.length).replace(/^\n+/, "");
68
+ }
69
+ }
70
+ return { goal, body: rest };
71
+ }
72
+ async function readMemory(root, slug) {
73
+ const path = await memoryFile(root, slug);
74
+ let text = "";
75
+ try {
76
+ text = await readFile(path, "utf8");
77
+ } catch {
78
+ }
79
+ return { path, ...parseMemory(text) };
80
+ }
81
+ async function writeMemory(path, goal, body) {
82
+ await writeFile(path, MEMORY_HEADER + (goal ? "\n" + GOAL_OPEN + goal + GOAL_CLOSE : "") + (body ? "\n" + body + "\n" : ""));
83
+ }
84
+ async function readNotes(root, slug) {
85
+ return (await readMemory(root, slug)).body;
86
+ }
87
+ async function setNotes(root, slug, text) {
88
+ const { path, goal } = await readMemory(root, slug);
89
+ const body = parseMemory(text).body.trim();
90
+ if (body.length > MEMORY_MAX) throw new Error(`memory too long (max ${MEMORY_MAX} chars)`);
91
+ await writeMemory(path, goal, body);
92
+ return body ? body + "\n" : "";
93
+ }
94
+ async function readGoal(root, slug) {
95
+ return (await readMemory(root, slug)).goal;
96
+ }
97
+ async function setGoal(root, slug, text) {
98
+ const { path, body } = await readMemory(root, slug);
99
+ const goal = text.trim();
100
+ if (goal.length > GOAL_MAX) throw new Error(`goal too long (max ${GOAL_MAX} chars)`);
101
+ await writeMemory(path, goal, body.trim());
102
+ return goal;
103
+ }
104
+ async function appendNote(root, slug, text) {
105
+ const body = text.trim().replace(/\s+/g, " ");
106
+ if (!body) throw new Error("empty note");
107
+ if (body.length > 2e3) throw new Error("note too long (max 2000 chars)");
108
+ const existing = (await readNotes(root, slug)).trimEnd();
109
+ const stamp = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
110
+ return setNotes(root, slug, (existing ? existing + "\n" : "") + `- ${body} _(${stamp})_`);
111
+ }
112
+ function slugify(name) {
113
+ const s = name.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60);
114
+ if (!s) throw new Error("invalid name");
115
+ return s;
116
+ }
117
+ function statePath(root) {
118
+ return join(root, STATE_NAME);
119
+ }
120
+ async function loadState(root) {
121
+ try {
122
+ const raw = await readFile(statePath(root), "utf8");
123
+ const parsed = JSON.parse(raw);
124
+ if (Array.isArray(parsed)) return parsed;
125
+ } catch {
126
+ }
127
+ return reconcile(root, []);
128
+ }
129
+ async function saveState(root, records) {
130
+ await mkdir(root, { recursive: true });
131
+ await writeFile(statePath(root), JSON.stringify(records, null, 2));
132
+ }
133
+ async function reconcile(root, records) {
134
+ let entries = [];
135
+ try {
136
+ entries = (await readdir(root)).filter((n) => !n.startsWith(".") && !n.endsWith(STATE_NAME));
137
+ } catch {
138
+ return records;
139
+ }
140
+ const bySlug = new Map(records.map((r) => [r.slug, r]));
141
+ const out = [];
142
+ for (const name of entries.sort()) {
143
+ try {
144
+ const st = await stat(join(root, name));
145
+ if (!st.isDirectory()) continue;
146
+ } catch {
147
+ continue;
148
+ }
149
+ const existing = bySlug.get(name);
150
+ if (existing) {
151
+ out.push(existing);
152
+ continue;
153
+ }
154
+ out.push({
155
+ slug: name,
156
+ name,
157
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
158
+ });
159
+ }
160
+ return out;
161
+ }
162
+ async function listProjects(root) {
163
+ const records = await loadState(root);
164
+ const reconciled = await reconcile(root, records);
165
+ if (reconciled.length !== records.length) {
166
+ await saveState(root, reconciled);
167
+ }
168
+ return reconciled;
169
+ }
170
+ async function getProject(root, slug) {
171
+ const records = await listProjects(root);
172
+ const rec = records.find((r) => r.slug === slug);
173
+ if (!rec) return null;
174
+ let instructions = "";
175
+ try {
176
+ instructions = await readFile(join(root, slug, AGENTS_NAME), "utf8");
177
+ } catch {
178
+ }
179
+ let knowledge = [];
180
+ try {
181
+ const files = await readdir(join(root, slug, KNOWLEDGE_DIR));
182
+ knowledge = (await Promise.all(files.map(async (f) => {
183
+ const st = await stat(join(root, slug, KNOWLEDGE_DIR, f));
184
+ if (!st.isFile()) return null;
185
+ return { name: f, size: st.size, mtime: st.mtimeMs };
186
+ }))).filter(Boolean);
187
+ } catch {
188
+ }
189
+ return { ...rec, instructions, knowledge, goal: await readGoal(root, slug) };
190
+ }
191
+ async function createProject(root, name, description) {
192
+ const records = await listProjects(root);
193
+ const slugs = new Set(records.map((r) => r.slug));
194
+ let slug = slugify(name);
195
+ if (slugs.has(slug)) {
196
+ let n = 2;
197
+ while (slugs.has(`${slug}-${n}`)) n++;
198
+ slug = `${slug}-${n}`;
199
+ }
200
+ const dir = join(root, slug);
201
+ await mkdir(join(dir, KNOWLEDGE_DIR), { recursive: true });
202
+ await writeFile(join(dir, AGENTS_NAME), DEFAULT_AGENTS);
203
+ const createdAt = (/* @__PURE__ */ new Date()).toISOString();
204
+ if (description.trim()) {
205
+ await writeFile(join(dir, "README.md"), description.trim() + "\n");
206
+ }
207
+ records.push({ slug, name: name.trim(), description: description.trim() || void 0, createdAt });
208
+ await saveState(root, records);
209
+ return { slug, name: name.trim(), description: description.trim(), createdAt };
210
+ }
211
+ async function deleteProject(root, slug) {
212
+ const records = (await listProjects(root)).filter((r) => r.slug !== slug);
213
+ await saveState(root, records);
214
+ await rm(join(root, slug), { recursive: true, force: true });
215
+ }
216
+ async function setStarred(root, slug, starred) {
217
+ return setFlag(root, slug, { starred });
218
+ }
219
+ async function setArchived(root, slug, archived) {
220
+ return setFlag(root, slug, archived ? { archived: true, starred: false } : { archived: false });
221
+ }
222
+ async function setFlag(root, slug, patch) {
223
+ const records = await listProjects(root);
224
+ const rec = records.find((r) => r.slug === slug);
225
+ if (!rec) return null;
226
+ Object.assign(rec, patch);
227
+ await saveState(root, records);
228
+ return rec;
229
+ }
230
+ async function setInstructions(root, slug, text) {
231
+ const rec = (await listProjects(root)).find((r) => r.slug === slug);
232
+ if (!rec) throw new Error("no such project");
233
+ await writeFile(join(root, slug, AGENTS_NAME), text);
234
+ }
235
+ async function renameProject(root, slug, patch) {
236
+ const records = await listProjects(root);
237
+ const rec = records.find((r) => r.slug === slug);
238
+ if (!rec) throw new Error("no such project");
239
+ if (patch.name !== void 0) {
240
+ const name = patch.name.trim();
241
+ if (!name || name.length > 60) throw new Error("name must be 1-60 chars");
242
+ rec.name = name;
243
+ }
244
+ if (patch.description !== void 0) {
245
+ const d = patch.description.trim();
246
+ rec.description = d || void 0;
247
+ if (d) await writeFile(join(root, slug, "README.md"), d + "\n");
248
+ else await rm(join(root, slug, "README.md"), { force: true });
249
+ }
250
+ await saveState(root, records);
251
+ return rec;
252
+ }
253
+ async function importKnowledge(root, slug, src) {
254
+ const rec = (await listProjects(root)).find((r) => r.slug === slug);
255
+ if (!rec) throw new Error("no such project");
256
+ const destDir = join(root, slug, KNOWLEDGE_DIR);
257
+ await mkdir(destDir, { recursive: true });
258
+ const name = basename(src);
259
+ const dest = join(destDir, name);
260
+ await cp(src, dest, { recursive: true });
261
+ return name;
262
+ }
263
+ async function deleteKnowledge(root, slug, name) {
264
+ const rec = (await listProjects(root)).find((r) => r.slug === slug);
265
+ if (!rec) throw new Error("no such project");
266
+ if (name.includes("/") || name.includes("\\") || name.startsWith(".")) {
267
+ throw new Error("invalid file name");
268
+ }
269
+ await rm(join(root, slug, KNOWLEDGE_DIR, name), { recursive: true, force: true });
270
+ }
271
+
272
+ // src/index.ts
273
+ var MAX_UPLOAD_BYTES = 30 * 1024 * 1024;
274
+ function projectsRoot() {
275
+ return process.env.DSH_PROJECTS_ROOT?.trim() || join2(homedir(), "deep-projects");
276
+ }
277
+ function json(value, status = 200) {
278
+ return new Response(JSON.stringify(value), {
279
+ status,
280
+ headers: { "content-type": "application/json", "cache-control": "no-store" }
281
+ });
282
+ }
283
+ function describe(error) {
284
+ return error instanceof Error ? error.message : String(error);
285
+ }
286
+ function buildRoutes(root) {
287
+ const parse = (request) => new URL(request.url).searchParams;
288
+ const guard = (request) => {
289
+ const slug = parse(request).get("slug") ?? "";
290
+ if (!/^[a-z0-9][a-z0-9-]{0,59}$/.test(slug)) {
291
+ return json({ ok: false, code: "badSlug", error: "invalid slug" }, 400);
292
+ }
293
+ return null;
294
+ };
295
+ return [
296
+ {
297
+ path: "/api/deep-projects/list",
298
+ methods: ["GET"],
299
+ handle: async () => {
300
+ const { readdir: readdir2 } = await import("node:fs/promises");
301
+ const projects = await listProjects(root).then(async (records) => {
302
+ return Promise.all(records.map(async (r) => {
303
+ let fileCount = 0;
304
+ try {
305
+ fileCount = (await readdir2(join2(root, r.slug, "knowledge"))).length;
306
+ } catch {
307
+ }
308
+ return { ...r, path: join2(root, r.slug), fileCount };
309
+ }));
310
+ });
311
+ return json({ ok: true, projects });
312
+ }
313
+ },
314
+ {
315
+ path: "/api/deep-projects/create",
316
+ methods: ["POST"],
317
+ handle: async (request) => {
318
+ let body = {};
319
+ try {
320
+ body = await request.json();
321
+ } catch {
322
+ }
323
+ const name = (body.name ?? "").trim();
324
+ if (!name || name.length > 60) {
325
+ return json({ ok: false, code: "badName", error: "name required (1-60 chars)" }, 400);
326
+ }
327
+ try {
328
+ const project = await createProject(root, name, (body.description ?? "").trim());
329
+ return json({ ok: true, project: { ...project, path: join2(root, project.slug) } });
330
+ } catch (error) {
331
+ return json({ ok: false, code: "internalError", error: describe(error) }, 500);
332
+ }
333
+ }
334
+ },
335
+ {
336
+ path: "/api/deep-projects/detail",
337
+ methods: ["GET"],
338
+ handle: async (request) => {
339
+ const bad = guard(request);
340
+ if (bad) return bad;
341
+ const slug = parse(request).get("slug");
342
+ const project = await getProject(root, slug);
343
+ if (!project) return json({ ok: false, code: "notFound", error: "no such project" }, 404);
344
+ return json({ ok: true, project: { ...project, path: join2(root, slug) } });
345
+ }
346
+ },
347
+ {
348
+ path: "/api/deep-projects/rename",
349
+ methods: ["POST"],
350
+ handle: async (request) => {
351
+ const bad = guard(request);
352
+ if (bad) return bad;
353
+ let body = {};
354
+ try {
355
+ body = await request.json();
356
+ } catch {
357
+ }
358
+ const name = body.name === void 0 ? void 0 : body.name.trim();
359
+ if (name !== void 0 && (!name || name.length > 60)) {
360
+ return json({ ok: false, code: "badName", error: "name required (1-60 chars)" }, 400);
361
+ }
362
+ if (typeof body.description === "string" && body.description.length > 500) {
363
+ return json({ ok: false, code: "badName", error: "description too long (max 500)" }, 400);
364
+ }
365
+ try {
366
+ const project = await renameProject(root, parse(request).get("slug"), {
367
+ name,
368
+ description: typeof body.description === "string" ? body.description : void 0
369
+ });
370
+ if (typeof body.goal === "string") await setGoal(root, parse(request).get("slug"), body.goal);
371
+ return json({ ok: true, project });
372
+ } catch (error) {
373
+ return json({ ok: false, code: "notFound", error: describe(error) }, 404);
374
+ }
375
+ }
376
+ },
377
+ {
378
+ path: "/api/deep-projects/star",
379
+ methods: ["POST"],
380
+ handle: async (request) => {
381
+ const bad = guard(request);
382
+ if (bad) return bad;
383
+ let body = {};
384
+ try {
385
+ body = await request.json();
386
+ } catch {
387
+ }
388
+ try {
389
+ const project = await setStarred(root, parse(request).get("slug"), !!body.starred);
390
+ if (!project) return json({ ok: false, code: "notFound", error: "no such project" }, 404);
391
+ return json({ ok: true, project });
392
+ } catch (error) {
393
+ return json({ ok: false, code: "internalError", error: describe(error) }, 500);
394
+ }
395
+ }
396
+ },
397
+ {
398
+ path: "/api/deep-projects/archive",
399
+ methods: ["POST"],
400
+ handle: async (request) => {
401
+ const bad = guard(request);
402
+ if (bad) return bad;
403
+ let body = {};
404
+ try {
405
+ body = await request.json();
406
+ } catch {
407
+ }
408
+ const project = await setArchived(root, parse(request).get("slug"), !!body.archived);
409
+ if (!project) return json({ ok: false, code: "notFound", error: "no such project" }, 404);
410
+ return json({ ok: true, project });
411
+ }
412
+ },
413
+ {
414
+ path: "/api/deep-projects/instructions",
415
+ methods: ["POST"],
416
+ handle: async (request) => {
417
+ const bad = guard(request);
418
+ if (bad) return bad;
419
+ const declared = Number(request.headers.get("content-length") ?? "");
420
+ if (declared > 512 * 1024) {
421
+ return json({ ok: false, code: "bodyTooLarge", error: "instructions too large" }, 413);
422
+ }
423
+ let body = {};
424
+ try {
425
+ body = await request.json();
426
+ } catch {
427
+ }
428
+ if (typeof body.text !== "string") {
429
+ return json({ ok: false, code: "badText", error: "text required" }, 400);
430
+ }
431
+ try {
432
+ await setInstructions(root, parse(request).get("slug"), body.text);
433
+ return json({ ok: true });
434
+ } catch (error) {
435
+ return json({ ok: false, code: "notFound", error: describe(error) }, 404);
436
+ }
437
+ }
438
+ },
439
+ {
440
+ path: "/api/deep-projects/delete",
441
+ methods: ["POST"],
442
+ handle: async (request) => {
443
+ const bad = guard(request);
444
+ if (bad) return bad;
445
+ try {
446
+ await deleteProject(root, parse(request).get("slug"));
447
+ return json({ ok: true });
448
+ } catch (error) {
449
+ return json({ ok: false, code: "notFound", error: describe(error) }, 404);
450
+ }
451
+ }
452
+ },
453
+ {
454
+ // Raw binary body (no multipart): the browser sends the File bytes
455
+ // directly; the filename travels as a query parameter.
456
+ path: "/api/deep-projects/knowledge-upload",
457
+ methods: ["POST"],
458
+ handle: async (request) => {
459
+ const bad = guard(request);
460
+ if (bad) return bad;
461
+ const name = (parse(request).get("name") ?? "").trim();
462
+ if (!name || name.includes("/") || name.includes("\\") || name.startsWith(".")) {
463
+ return json({ ok: false, code: "badName", error: "invalid file name" }, 400);
464
+ }
465
+ const declared = Number(request.headers.get("content-length") ?? "");
466
+ if (declared > MAX_UPLOAD_BYTES) {
467
+ return json({ ok: false, code: "bodyTooLarge", params: { max: MAX_UPLOAD_BYTES }, error: "file too large" }, 413);
468
+ }
469
+ const bytes = new Uint8Array(await request.arrayBuffer());
470
+ if (bytes.byteLength > MAX_UPLOAD_BYTES) {
471
+ return json({ ok: false, code: "bodyTooLarge", params: { max: MAX_UPLOAD_BYTES }, error: "file too large" }, 413);
472
+ }
473
+ const { writeFile: writeFile2, mkdir: mkdir2 } = await import("node:fs/promises");
474
+ const destDir = join2(root, parse(request).get("slug"), "knowledge");
475
+ await mkdir2(destDir, { recursive: true });
476
+ await writeFile2(join2(destDir, name), bytes);
477
+ return json({ ok: true, name });
478
+ }
479
+ },
480
+ {
481
+ // Project memory (AGENTS.local.md): GET reads, POST replaces the body.
482
+ path: "/api/deep-projects/notes",
483
+ methods: ["GET", "POST"],
484
+ handle: async (request) => {
485
+ const bad = guard(request);
486
+ if (bad) return bad;
487
+ const slug = parse(request).get("slug");
488
+ if (request.method === "POST") {
489
+ let body = {};
490
+ try {
491
+ body = await request.json();
492
+ } catch {
493
+ }
494
+ if (typeof body.text !== "string") return json({ ok: false, code: "badText", error: "text required" }, 400);
495
+ try {
496
+ return json({ ok: true, notes: await setNotes(root, slug, body.text) });
497
+ } catch (error) {
498
+ return json({ ok: false, code: "badText", error: describe(error) }, 400);
499
+ }
500
+ }
501
+ try {
502
+ return json({ ok: true, notes: await readNotes(root, slug) });
503
+ } catch (error) {
504
+ return json({ ok: false, code: "notFound", error: describe(error) }, 404);
505
+ }
506
+ }
507
+ },
508
+ {
509
+ path: "/api/deep-projects/note",
510
+ methods: ["POST"],
511
+ handle: async (request) => {
512
+ const bad = guard(request);
513
+ if (bad) return bad;
514
+ let body = {};
515
+ try {
516
+ body = await request.json();
517
+ } catch {
518
+ }
519
+ if (typeof body.text !== "string" || !body.text.trim()) {
520
+ return json({ ok: false, code: "badText", error: "text required" }, 400);
521
+ }
522
+ try {
523
+ const notes = await appendNote(root, parse(request).get("slug"), body.text);
524
+ return json({ ok: true, notes });
525
+ } catch (error) {
526
+ return json({ ok: false, code: "badText", error: describe(error) }, 400);
527
+ }
528
+ }
529
+ },
530
+ {
531
+ path: "/api/deep-projects/knowledge-delete",
532
+ methods: ["POST"],
533
+ handle: async (request) => {
534
+ const bad = guard(request);
535
+ if (bad) return bad;
536
+ const name = parse(request).get("name") ?? "";
537
+ if (!name || name.includes("/") || name.includes("\\") || name.startsWith(".")) {
538
+ return json({ ok: false, code: "badName", error: "invalid file name" }, 400);
539
+ }
540
+ try {
541
+ await deleteKnowledge(root, parse(request).get("slug"), name);
542
+ return json({ ok: true });
543
+ } catch (error) {
544
+ return json({ ok: false, code: "notFound", error: describe(error) }, 404);
545
+ }
546
+ }
547
+ }
548
+ ];
549
+ }
550
+ function buildTools(root) {
551
+ const render = (_args, value) => [{ type: "text", text: JSON.stringify(value) }];
552
+ const recordSchema = {
553
+ type: "object",
554
+ properties: {
555
+ slug: { type: "string" },
556
+ name: { type: "string" },
557
+ description: { type: "string" },
558
+ starred: { type: "boolean" },
559
+ createdAt: { type: "string" }
560
+ },
561
+ required: ["slug", "name", "createdAt"]
562
+ };
563
+ return [
564
+ {
565
+ name: "projects_list",
566
+ description: "List projects: named workspaces (each with AGENTS.md instructions and a knowledge/ folder) that group related chats. Returns slug, name, description, and star state.",
567
+ parameters: { type: "object", properties: {} },
568
+ output: {
569
+ schema: {
570
+ type: "object",
571
+ properties: { projects: { type: "array", items: recordSchema } },
572
+ required: ["projects"]
573
+ },
574
+ render
575
+ },
576
+ execute: async () => ({ projects: await listProjects(root) })
577
+ },
578
+ {
579
+ name: "projects_create",
580
+ description: "Create a new dsh-project workspace (folder + AGENTS.md + knowledge/). Use when the user asks to start a project or to group work under a name.",
581
+ parameters: {
582
+ type: "object",
583
+ properties: {
584
+ name: { type: "string", description: "Display name (1-60 chars)." },
585
+ description: { type: "string", description: "Optional one-line description." }
586
+ },
587
+ required: ["name"]
588
+ },
589
+ output: {
590
+ schema: {
591
+ type: "object",
592
+ properties: {
593
+ ok: { type: "boolean" },
594
+ project: recordSchema,
595
+ path: { type: "string" }
596
+ },
597
+ required: ["ok", "project", "path"]
598
+ },
599
+ render
600
+ },
601
+ execute: async (args) => {
602
+ const project = await createProject(root, args.name, args.description ?? "");
603
+ return { ok: true, project, path: join2(root, project.slug) };
604
+ }
605
+ },
606
+ {
607
+ name: "projects_read",
608
+ description: "Read a dsh-project: goal, custom instructions (AGENTS.md), project memory, and the list of knowledge files.",
609
+ parameters: {
610
+ type: "object",
611
+ properties: { slug: { type: "string", description: "Project slug from projects_list." } },
612
+ required: ["slug"]
613
+ },
614
+ output: {
615
+ schema: {
616
+ type: "object",
617
+ properties: {
618
+ slug: { type: "string" },
619
+ name: { type: "string" },
620
+ description: { type: "string" },
621
+ starred: { type: "boolean" },
622
+ createdAt: { type: "string" },
623
+ instructions: { type: "string" },
624
+ goal: { type: "string" },
625
+ memory: { type: "string" },
626
+ knowledge: {
627
+ type: "array",
628
+ items: {
629
+ type: "object",
630
+ properties: { name: { type: "string" }, size: { type: "number" }, mtime: { type: "number" } },
631
+ required: ["name", "size", "mtime"]
632
+ }
633
+ }
634
+ },
635
+ required: ["slug", "name", "createdAt", "instructions", "knowledge"]
636
+ },
637
+ render
638
+ },
639
+ execute: async (args) => {
640
+ const project = await getProject(root, args.slug);
641
+ if (!project) throw new Error("no such project: " + args.slug);
642
+ return { ...project, memory: await readNotes(root, args.slug) };
643
+ }
644
+ },
645
+ {
646
+ name: "projects_update",
647
+ description: "Change a dsh-project's settings: name, description, goal, custom instructions (replaces AGENTS.md), pinned, and/or archived state. Pass only the fields to change. For memories use projects_add_note / projects_set_memory instead.",
648
+ parameters: {
649
+ type: "object",
650
+ properties: {
651
+ slug: { type: "string", description: "Project slug from projects_list." },
652
+ name: { type: "string", description: "New display name (1-60 chars)." },
653
+ description: { type: "string", description: "New one-line description (empty clears it)." },
654
+ goal: { type: "string", description: "What the project is for / what done looks like (empty clears it). Shown to every chat in the project." },
655
+ instructions: { type: "string", description: "Full new AGENTS.md content (replaces it; read it with projects_read first)." },
656
+ pinned: { type: "boolean", description: "Pin to (true) or unpin from (false) the sidebar." },
657
+ archived: { type: "boolean", description: "Archive (true: hidden from lists, files kept) or restore (false)." }
658
+ },
659
+ required: ["slug"]
660
+ },
661
+ output: {
662
+ schema: {
663
+ type: "object",
664
+ properties: { ok: { type: "boolean" }, changed: { type: "array", items: { type: "string" } } },
665
+ required: ["ok", "changed"]
666
+ },
667
+ render
668
+ },
669
+ execute: async (args) => {
670
+ const changed = [];
671
+ if (args.name !== void 0 || args.description !== void 0) {
672
+ await renameProject(root, args.slug, { name: args.name, description: args.description });
673
+ if (args.name !== void 0) changed.push("name");
674
+ if (args.description !== void 0) changed.push("description");
675
+ }
676
+ if (args.goal !== void 0) {
677
+ await setGoal(root, args.slug, args.goal);
678
+ changed.push("goal");
679
+ }
680
+ if (args.instructions !== void 0) {
681
+ if (args.instructions.length > 2e5) throw new Error("instructions too large");
682
+ await setInstructions(root, args.slug, args.instructions);
683
+ changed.push("instructions");
684
+ }
685
+ if (args.pinned !== void 0) {
686
+ await setStarred(root, args.slug, args.pinned);
687
+ changed.push("pinned");
688
+ }
689
+ if (args.archived !== void 0) {
690
+ await setArchived(root, args.slug, args.archived);
691
+ changed.push("archived");
692
+ }
693
+ return { ok: true, changed };
694
+ }
695
+ },
696
+ {
697
+ name: "projects_add_knowledge",
698
+ description: "Copy an existing file (or directory) from disk into a project's knowledge/ folder so future chats in that workspace can read it.",
699
+ parameters: {
700
+ type: "object",
701
+ properties: {
702
+ slug: { type: "string", description: "Project slug." },
703
+ source: { type: "string", description: "Absolute path of the file or directory to copy." }
704
+ },
705
+ required: ["slug", "source"]
706
+ },
707
+ output: {
708
+ schema: {
709
+ type: "object",
710
+ properties: { ok: { type: "boolean" }, name: { type: "string" } },
711
+ required: ["ok", "name"]
712
+ },
713
+ render
714
+ },
715
+ execute: async (args) => {
716
+ const name = await importKnowledge(root, args.slug, args.source);
717
+ return { ok: true, name };
718
+ }
719
+ },
720
+ {
721
+ name: "projects_add_note",
722
+ description: "Add one memory to a project's memory (AGENTS.local.md, auto-loaded into every chat in that project). Use proactively whenever you learn a lasting fact, decision, or user preference about the project. Don't add duplicates; use projects_set_memory to fix stale ones.",
723
+ parameters: {
724
+ type: "object",
725
+ properties: {
726
+ slug: { type: "string", description: "Project slug from projects_list." },
727
+ text: { type: "string", description: "The memory (one line, max 2000 chars)." }
728
+ },
729
+ required: ["slug", "text"]
730
+ },
731
+ output: {
732
+ schema: {
733
+ type: "object",
734
+ properties: { ok: { type: "boolean" }, notes: { type: "string" } },
735
+ required: ["ok", "notes"]
736
+ },
737
+ render
738
+ },
739
+ execute: async (args) => {
740
+ const notes = await appendNote(root, args.slug, args.text);
741
+ return { ok: true, notes };
742
+ }
743
+ },
744
+ {
745
+ name: "projects_set_memory",
746
+ description: "Replace a project's whole memory list (AGENTS.local.md body; the header is kept). Use to correct, merge, or remove wrong or outdated memories. Pass every bullet you want to keep, one '- ' line each. Returns the saved memory.",
747
+ parameters: {
748
+ type: "object",
749
+ properties: {
750
+ slug: { type: "string", description: "Project slug from projects_list." },
751
+ text: { type: "string", description: "Full new memory body: markdown bullets, one per line (max 20000 chars). Empty clears it." }
752
+ },
753
+ required: ["slug", "text"]
754
+ },
755
+ output: {
756
+ schema: {
757
+ type: "object",
758
+ properties: { ok: { type: "boolean" }, notes: { type: "string" } },
759
+ required: ["ok", "notes"]
760
+ },
761
+ render
762
+ },
763
+ execute: async (args) => {
764
+ const notes = await setNotes(root, args.slug, args.text);
765
+ return { ok: true, notes };
766
+ }
767
+ },
768
+ {
769
+ name: "projects_delete",
770
+ description: "Delete a dsh-project: removes its folder (instructions, knowledge files) and metadata. Existing sessions are kept.",
771
+ parameters: {
772
+ type: "object",
773
+ properties: { slug: { type: "string", description: "Project slug to delete." } },
774
+ required: ["slug"]
775
+ },
776
+ output: {
777
+ schema: {
778
+ type: "object",
779
+ properties: { ok: { type: "boolean" } },
780
+ required: ["ok"]
781
+ },
782
+ render
783
+ },
784
+ execute: async (args) => {
785
+ await deleteProject(root, args.slug);
786
+ return { ok: true };
787
+ }
788
+ }
789
+ ];
790
+ }
791
+ var inject = ["connection", "agents"];
792
+ function apply(ctx) {
793
+ const root = projectsRoot();
794
+ console.log(`deep-projects: projects root ${root}`);
795
+ if (ctx.connection?.fetch?.register) {
796
+ for (const route of buildRoutes(root)) {
797
+ ctx.effect(
798
+ () => ctx.connection.fetch.register({
799
+ path: route.path,
800
+ methods: route.methods,
801
+ requestBody: "buffered",
802
+ fetch: async (request) => {
803
+ try {
804
+ return await route.handle(request);
805
+ } catch (error) {
806
+ return json({ ok: false, code: "internalError", error: describe(error) }, 500);
807
+ }
808
+ }
809
+ }),
810
+ `deep-projects.route${route.path}`
811
+ );
812
+ }
813
+ }
814
+ if (ctx.on && ctx.agents) {
815
+ ctx.on("agent/created", ({ agent }) => {
816
+ for (const tool of buildTools(root)) {
817
+ ctx.effect(
818
+ () => agent.ctx.tools.register(tool),
819
+ `deep-projects.tool.${tool.name}`
820
+ );
821
+ }
822
+ });
823
+ try {
824
+ for (const agent of ctx.agents.roots?.() ?? []) {
825
+ for (const tool of buildTools(root)) {
826
+ ctx.effect(
827
+ () => agent.ctx.tools.register(tool),
828
+ `deep-projects.tool.${tool.name}`
829
+ );
830
+ }
831
+ }
832
+ } catch {
833
+ }
834
+ }
835
+ }
836
+ export {
837
+ apply,
838
+ buildRoutes,
839
+ buildTools,
840
+ inject
841
+ };