@plandesk/api 0.9.0 → 0.11.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/dist/events.d.ts CHANGED
@@ -15,14 +15,18 @@ export type DocumentCreatedEvent = {
15
15
  export type CommentCreatedEvent = {
16
16
  type: 'comment_created';
17
17
  commentId: string;
18
- documentId: string;
19
18
  projectId: string;
19
+ target_type: string;
20
+ target_id: string;
21
+ documentId?: string;
20
22
  };
21
23
  export type CommentUpdatedEvent = {
22
24
  type: 'comment_updated';
23
25
  commentId: string;
24
- documentId: string;
25
26
  projectId: string;
27
+ target_type: string;
28
+ target_id: string;
29
+ documentId?: string;
26
30
  };
27
31
  export type FolderCreatedEvent = {
28
32
  type: 'folder_created';
@@ -1,19 +1,70 @@
1
1
  import { Hono } from 'hono';
2
- import { InvalidCommentError } from '../services/comments.js';
2
+ import { InvalidCommentError, } from '../services/comments.js';
3
3
  function parseIncludeResolved(value) {
4
4
  return value === 'true';
5
5
  }
6
+ async function handleCreateComment(c, commentService, target) {
7
+ const body = await c.req.json();
8
+ if (typeof body.body !== 'string') {
9
+ return c.json({ error: 'invalid_argument' }, 400);
10
+ }
11
+ try {
12
+ const comment = commentService.create(target, {
13
+ body: body.body,
14
+ passage: body.passage,
15
+ anchor: body.anchor,
16
+ });
17
+ if (!comment) {
18
+ return c.json({ error: 'not_found' }, 404);
19
+ }
20
+ return c.json(comment, 201);
21
+ }
22
+ catch (error) {
23
+ if (error instanceof InvalidCommentError) {
24
+ return c.json({ error: 'invalid_argument' }, 400);
25
+ }
26
+ throw error;
27
+ }
28
+ }
29
+ function handleListComments(c, commentService, target) {
30
+ const includeResolved = parseIncludeResolved(c.req.query('include_resolved'));
31
+ const comments = commentService.listByTarget(target, { includeResolved });
32
+ if (!comments) {
33
+ return c.json({ error: 'not_found' }, 404);
34
+ }
35
+ return c.json(comments);
36
+ }
6
37
  export function createCommentsRouter(commentService) {
7
38
  const router = new Hono();
8
- router.post('/documents/:id/comments', async (c) => {
39
+ router.post('/documents/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'document', id: c.req.param('id') }));
40
+ router.get('/documents/:id/comments', (c) => handleListComments(c, commentService, { type: 'document', id: c.req.param('id') }));
41
+ router.post('/tasks/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'task', id: c.req.param('id') }));
42
+ router.get('/tasks/:id/comments', (c) => handleListComments(c, commentService, { type: 'task', id: c.req.param('id') }));
43
+ router.post('/notes/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'note', id: c.req.param('id') }));
44
+ router.get('/notes/:id/comments', (c) => handleListComments(c, commentService, { type: 'note', id: c.req.param('id') }));
45
+ router.post('/submissions/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'submission', id: c.req.param('id') }));
46
+ router.get('/submissions/:id/comments', (c) => handleListComments(c, commentService, { type: 'submission', id: c.req.param('id') }));
47
+ router.get('/projects/:id/comments', (c) => {
48
+ const includeResolved = parseIncludeResolved(c.req.query('include_resolved'));
49
+ const comments = commentService.listByProject(c.req.param('id'), { includeResolved });
50
+ if (!comments) {
51
+ return c.json({ error: 'not_found' }, 404);
52
+ }
53
+ return c.json(comments);
54
+ });
55
+ // Artifact comments are project-scoped. The file identity (content-hash+path,
56
+ // which may contain slashes) travels in the JSON body / query, not a path
57
+ // segment, so it never collides with routing.
58
+ router.post('/projects/:id/artifact-comments', async (c) => {
9
59
  const body = await c.req.json();
10
- if (typeof body.body !== 'string') {
60
+ if (typeof body.body !== 'string' || typeof body.artifact_id !== 'string') {
11
61
  return c.json({ error: 'invalid_argument' }, 400);
12
62
  }
13
63
  try {
14
- const comment = commentService.create(c.req.param('id'), {
64
+ const comment = commentService.createForArtifact(c.req.param('id'), body.artifact_id, {
15
65
  body: body.body,
16
66
  passage: body.passage,
67
+ anchor: body.anchor,
17
68
  });
18
69
  if (!comment) {
19
70
  return c.json({ error: 'not_found' }, 404);
@@ -27,17 +78,15 @@ export function createCommentsRouter(commentService) {
27
78
  throw error;
28
79
  }
29
80
  });
30
- router.get('/documents/:id/comments', (c) => {
31
- const includeResolved = parseIncludeResolved(c.req.query('include_resolved'));
32
- const comments = commentService.listByDocument(c.req.param('id'), { includeResolved });
33
- if (!comments) {
34
- return c.json({ error: 'not_found' }, 404);
81
+ router.get('/projects/:id/artifact-comments', (c) => {
82
+ const artifactId = c.req.query('artifact_id');
83
+ if (artifactId === undefined || artifactId === '') {
84
+ return c.json({ error: 'invalid_argument' }, 400);
35
85
  }
36
- return c.json(comments);
37
- });
38
- router.get('/projects/:id/comments', (c) => {
39
86
  const includeResolved = parseIncludeResolved(c.req.query('include_resolved'));
40
- const comments = commentService.listByProject(c.req.param('id'), { includeResolved });
87
+ const comments = commentService.listForArtifact(c.req.param('id'), artifactId, {
88
+ includeResolved,
89
+ });
41
90
  if (!comments) {
42
91
  return c.json({ error: 'not_found' }, 404);
43
92
  }
@@ -1,4 +1,4 @@
1
- import type { AgentRun, AgentRunEvent, Document, DocumentComment, Edge, Folder, Goal, Note, Project, Tag, Task, TaskStatus } from '@plandesk/db';
1
+ import type { AgentRun, AgentRunEvent, Document, Comment, Edge, Folder, Goal, Note, Project, Tag, Task, TaskStatus } from '@plandesk/db';
2
2
  export type PaginationParams = {
3
3
  limit?: number;
4
4
  offset?: number;
@@ -91,13 +91,16 @@ export type SerializedNote = {
91
91
  export declare function serializeNote(note: Note): SerializedNote;
92
92
  export type SerializedComment = {
93
93
  id: string;
94
- document_id: string;
94
+ target_type: Comment['targetType'];
95
+ target_id: string;
96
+ document_id: string | null;
95
97
  passage: string | null;
98
+ anchor: string | null;
96
99
  body: string;
97
100
  resolved: boolean;
98
101
  created_at: string;
99
102
  };
100
- export declare function serializeComment(comment: DocumentComment): SerializedComment;
103
+ export declare function serializeComment(comment: Comment): SerializedComment;
101
104
  export declare function buildDocumentTree(documents: Document[]): SerializedDocumentTree[];
102
105
  export type SerializedFolder = {
103
106
  id: string;
package/dist/serialize.js CHANGED
@@ -123,8 +123,11 @@ export function serializeNote(note) {
123
123
  export function serializeComment(comment) {
124
124
  return {
125
125
  id: comment.id,
126
- document_id: comment.documentId,
126
+ target_type: comment.targetType,
127
+ target_id: comment.targetId,
128
+ document_id: comment.targetType === 'document' ? comment.targetId : null,
127
129
  passage: comment.passage,
130
+ anchor: comment.anchor,
128
131
  body: comment.body,
129
132
  resolved: comment.resolved,
130
133
  created_at: comment.createdAt.toISOString(),
@@ -1,13 +1,18 @@
1
- import { type Db } from '@plandesk/db';
1
+ import { type CommentTargetType, type Db } from '@plandesk/db';
2
2
  import type { EventBus } from '../events.js';
3
3
  import { type SerializedComment } from '../serialize.js';
4
4
  export type CommentServiceDeps = {
5
5
  db: Db;
6
6
  eventBus: EventBus;
7
7
  };
8
+ export type CommentTarget = {
9
+ type: CommentTargetType;
10
+ id: string;
11
+ };
8
12
  export type CreateCommentInput = {
9
13
  body: string;
10
14
  passage?: string | null;
15
+ anchor?: string | null;
11
16
  };
12
17
  export type UpdateCommentInput = {
13
18
  body?: string;
@@ -17,7 +22,15 @@ export declare class InvalidCommentError extends Error {
17
22
  constructor(message: string);
18
23
  }
19
24
  export declare function createCommentService(deps: CommentServiceDeps): {
20
- create(documentId: string, input: CreateCommentInput): SerializedComment | undefined;
25
+ create(target: CommentTarget, input: CreateCommentInput): SerializedComment | undefined;
26
+ createForArtifact(projectId: string, artifactId: string, input: CreateCommentInput): SerializedComment | undefined;
27
+ listForArtifact(projectId: string, artifactId: string, options?: {
28
+ includeResolved?: boolean;
29
+ }): SerializedComment[] | undefined;
30
+ listByTarget(target: CommentTarget, options?: {
31
+ includeResolved?: boolean;
32
+ }): SerializedComment[] | undefined;
33
+ resolveTargetProjectId(target: CommentTarget): string | undefined;
21
34
  listByDocument(documentId: string, options?: {
22
35
  includeResolved?: boolean;
23
36
  }): SerializedComment[] | undefined;
@@ -1,4 +1,4 @@
1
- import { createDocumentComment, deleteDocumentComment as dbDeleteDocumentComment, getDocument as dbGetDocument, getDocumentComment as dbGetDocumentComment, getProject, listCommentsByDocument as dbListCommentsByDocument, listCommentsByProject as dbListCommentsByProject, updateDocumentComment as dbUpdateDocumentComment, } from '@plandesk/db';
1
+ import { createComment, deleteComment as dbDeleteComment, getComment as dbGetComment, getDocument as dbGetDocument, getNote as dbGetNote, getProject, getSubmission, getTask, listCommentsByProject as dbListCommentsByProject, listCommentsByTarget as dbListCommentsByTarget, updateComment as dbUpdateComment, } from '@plandesk/db';
2
2
  import { serializeComment } from '../serialize.js';
3
3
  export class InvalidCommentError extends Error {
4
4
  constructor(message) {
@@ -11,34 +11,104 @@ function assertNonEmptyBody(body) {
11
11
  throw new InvalidCommentError('Comment body must not be empty');
12
12
  }
13
13
  }
14
+ function targetProjectId(db, target) {
15
+ switch (target.type) {
16
+ case 'document':
17
+ return dbGetDocument(db, target.id)?.projectId;
18
+ case 'task':
19
+ return getTask(db, target.id)?.projectId;
20
+ case 'note':
21
+ return dbGetNote(db, target.id)?.projectId;
22
+ case 'submission':
23
+ return getSubmission(db, target.id)?.projectId;
24
+ case 'artifact':
25
+ // An artifact is a file on disk, not a DB entity — its project is
26
+ // supplied by the caller (the connected repo's project), not resolved
27
+ // from the target id. Artifact comments go through createForArtifact.
28
+ return undefined;
29
+ }
30
+ }
31
+ function emitCommentCreated(eventBus, commentId, projectId, target) {
32
+ eventBus.emit({
33
+ type: 'comment_created',
34
+ commentId,
35
+ projectId,
36
+ target_type: target.type,
37
+ target_id: target.id,
38
+ ...(target.type === 'document' ? { documentId: target.id } : {}),
39
+ });
40
+ }
41
+ function emitCommentUpdated(eventBus, commentId, projectId, target) {
42
+ eventBus.emit({
43
+ type: 'comment_updated',
44
+ commentId,
45
+ projectId,
46
+ target_type: target.type,
47
+ target_id: target.id,
48
+ ...(target.type === 'document' ? { documentId: target.id } : {}),
49
+ });
50
+ }
14
51
  export function createCommentService(deps) {
15
52
  const { db, eventBus } = deps;
16
53
  return {
17
- create(documentId, input) {
18
- const document = dbGetDocument(db, documentId);
19
- if (!document) {
54
+ create(target, input) {
55
+ const projectId = targetProjectId(db, target);
56
+ if (!projectId) {
20
57
  return undefined;
21
58
  }
22
59
  assertNonEmptyBody(input.body);
23
- const comment = createDocumentComment(db, {
24
- documentId,
60
+ const comment = createComment(db, {
61
+ projectId,
62
+ targetType: target.type,
63
+ targetId: target.id,
25
64
  body: input.body,
26
65
  passage: input.passage,
66
+ anchor: input.anchor,
27
67
  });
28
- eventBus.emit({
29
- type: 'comment_created',
30
- commentId: comment.id,
31
- documentId,
32
- projectId: document.projectId,
68
+ emitCommentCreated(eventBus, comment.id, projectId, target);
69
+ return serializeComment(comment);
70
+ },
71
+ // Artifact comments are project-scoped: the caller supplies the project
72
+ // (the connected repo's), and the file identity is the target id. Slashes
73
+ // in the file identity mean it travels in the body/query, never a path seg.
74
+ createForArtifact(projectId, artifactId, input) {
75
+ if (!getProject(db, projectId)) {
76
+ return undefined;
77
+ }
78
+ if (artifactId.trim() === '') {
79
+ throw new InvalidCommentError('Artifact id must not be empty');
80
+ }
81
+ assertNonEmptyBody(input.body);
82
+ const target = { type: 'artifact', id: artifactId };
83
+ const comment = createComment(db, {
84
+ projectId,
85
+ targetType: 'artifact',
86
+ targetId: artifactId,
87
+ body: input.body,
88
+ passage: input.passage,
89
+ anchor: input.anchor,
33
90
  });
91
+ emitCommentCreated(eventBus, comment.id, projectId, target);
34
92
  return serializeComment(comment);
35
93
  },
36
- listByDocument(documentId, options) {
37
- const document = dbGetDocument(db, documentId);
38
- if (!document) {
94
+ listForArtifact(projectId, artifactId, options) {
95
+ if (!getProject(db, projectId)) {
96
+ return undefined;
97
+ }
98
+ return dbListCommentsByTarget(db, 'artifact', artifactId, options).map(serializeComment);
99
+ },
100
+ listByTarget(target, options) {
101
+ const projectId = targetProjectId(db, target);
102
+ if (!projectId) {
39
103
  return undefined;
40
104
  }
41
- return dbListCommentsByDocument(db, documentId, options).map(serializeComment);
105
+ return dbListCommentsByTarget(db, target.type, target.id, options).map(serializeComment);
106
+ },
107
+ resolveTargetProjectId(target) {
108
+ return targetProjectId(db, target);
109
+ },
110
+ listByDocument(documentId, options) {
111
+ return this.listByTarget({ type: 'document', id: documentId }, options);
42
112
  },
43
113
  listByProject(projectId, options) {
44
114
  const project = getProject(db, projectId);
@@ -48,47 +118,49 @@ export function createCommentService(deps) {
48
118
  return dbListCommentsByProject(db, projectId, options).map(serializeComment);
49
119
  },
50
120
  update(id, input) {
51
- const existing = dbGetDocumentComment(db, id);
121
+ const existing = dbGetComment(db, id);
52
122
  if (!existing) {
53
123
  return undefined;
54
124
  }
55
125
  if (input.body !== undefined) {
56
126
  assertNonEmptyBody(input.body);
57
127
  }
58
- const comment = dbUpdateDocumentComment(db, id, input);
128
+ const comment = dbUpdateComment(db, id, input);
59
129
  if (!comment) {
60
130
  return undefined;
61
131
  }
62
- const document = dbGetDocument(db, comment.documentId);
63
- if (!document) {
132
+ const projectId = targetProjectId(db, {
133
+ type: comment.targetType,
134
+ id: comment.targetId,
135
+ });
136
+ if (!projectId) {
64
137
  return undefined;
65
138
  }
66
- eventBus.emit({
67
- type: 'comment_updated',
68
- commentId: id,
69
- documentId: comment.documentId,
70
- projectId: document.projectId,
139
+ emitCommentUpdated(eventBus, id, projectId, {
140
+ type: comment.targetType,
141
+ id: comment.targetId,
71
142
  });
72
143
  return serializeComment(comment);
73
144
  },
74
145
  delete(id) {
75
- const existing = dbGetDocumentComment(db, id);
146
+ const existing = dbGetComment(db, id);
76
147
  if (!existing) {
77
148
  return false;
78
149
  }
79
- const document = dbGetDocument(db, existing.documentId);
80
- if (!document) {
150
+ const projectId = targetProjectId(db, {
151
+ type: existing.targetType,
152
+ id: existing.targetId,
153
+ });
154
+ if (!projectId) {
81
155
  return false;
82
156
  }
83
- const deleted = dbDeleteDocumentComment(db, id);
157
+ const deleted = dbDeleteComment(db, id);
84
158
  if (!deleted) {
85
159
  return false;
86
160
  }
87
- eventBus.emit({
88
- type: 'comment_updated',
89
- commentId: id,
90
- documentId: existing.documentId,
91
- projectId: document.projectId,
161
+ emitCommentUpdated(eventBus, id, projectId, {
162
+ type: existing.targetType,
163
+ id: existing.targetId,
92
164
  });
93
165
  return true;
94
166
  },
@@ -1,4 +1,4 @@
1
- import { createDocument as dbCreateDocument, deleteCommentsByDocumentId, deleteDocument as dbDeleteDocument, detachDocumentChildren, getDocument as dbGetDocument, getDocumentByTask as dbGetDocumentByTask, getFolderByProjectAndId, getProject, getTask, listDocuments as dbListDocuments, listFolders as dbListFolders, updateDocument as dbUpdateDocument, } from '@plandesk/db';
1
+ import { createDocument as dbCreateDocument, deleteCommentsByTarget, deleteDocument as dbDeleteDocument, detachDocumentChildren, getDocument as dbGetDocument, getDocumentByTask as dbGetDocumentByTask, getFolderByProjectAndId, getProject, getTask, listDocuments as dbListDocuments, listFolders as dbListFolders, updateDocument as dbUpdateDocument, } from '@plandesk/db';
2
2
  import { buildDocumentTree, buildFolderTree, serializeDocument, } from '../serialize.js';
3
3
  export class InvalidDocumentError extends Error {
4
4
  constructor(message) {
@@ -128,7 +128,7 @@ export function createDocumentService(deps) {
128
128
  }
129
129
  db.transaction((tx) => {
130
130
  detachDocumentChildren(tx, id);
131
- deleteCommentsByDocumentId(tx, id);
131
+ deleteCommentsByTarget(tx, 'document', id);
132
132
  dbDeleteDocument(tx, id);
133
133
  });
134
134
  eventBus.emit({ type: 'canvas_updated', projectId: existing.projectId });
@@ -1,4 +1,4 @@
1
- import { createNote as dbCreateNote, deleteNote as dbDeleteNote, getNote as dbGetNote, getProject, listNotes as dbListNotes, updateNote as dbUpdateNote, } from '@plandesk/db';
1
+ import { createNote as dbCreateNote, deleteCommentsByTarget, deleteNote as dbDeleteNote, getNote as dbGetNote, getProject, listNotes as dbListNotes, updateNote as dbUpdateNote, } from '@plandesk/db';
2
2
  import { serializeNote } from '../serialize.js';
3
3
  export class InvalidNoteError extends Error {
4
4
  constructor(message) {
@@ -66,6 +66,7 @@ export function createNoteService(deps) {
66
66
  if (!deleted) {
67
67
  return false;
68
68
  }
69
+ deleteCommentsByTarget(db, 'note', id);
69
70
  eventBus.emit({ type: 'note_updated', noteId: id, projectId: existing.projectId });
70
71
  return true;
71
72
  },
@@ -1,4 +1,4 @@
1
- import { createTag, createTask, deleteEdgesByTaskId, deleteTask as dbDeleteTask, deleteTaskTagsByTaskId, getOrCreateDefaultGoal, getProject, listGoals, getTagByName, getTask, InvalidTaskStatusError, isTaskStatus, listEdges, listTagsByTaskForProject, listTagsForTask, listTasks, nullDocumentsLinkedTask, setTaskTags, taskIdsWithAnyTagName, updateTask, } from '@plandesk/db';
1
+ import { createTag, createTask, deleteCommentsByTarget, deleteEdgesByTaskId, deleteTask as dbDeleteTask, deleteTaskTagsByTaskId, getOrCreateDefaultGoal, getProject, listGoals, getTagByName, getTask, InvalidTaskStatusError, isTaskStatus, listEdges, listTagsByTaskForProject, listTagsForTask, listTasks, nullDocumentsLinkedTask, setTaskTags, taskIdsWithAnyTagName, updateTask, } from '@plandesk/db';
2
2
  import { serializeTask } from '../serialize.js';
3
3
  import { normalizeTagName } from './tags.js';
4
4
  export class InvalidGoalReferenceError extends Error {
@@ -132,6 +132,7 @@ export function createTaskService(deps) {
132
132
  }
133
133
  const projectId = task.projectId;
134
134
  db.transaction((tx) => {
135
+ deleteCommentsByTarget(tx, 'task', id);
135
136
  deleteEdgesByTaskId(tx, id);
136
137
  nullDocumentsLinkedTask(tx, id);
137
138
  deleteTaskTagsByTaskId(tx, id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plandesk/api",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -18,7 +18,7 @@
18
18
  "devDependencies": {
19
19
  "typescript": "^6.0.3",
20
20
  "vitest": "^3.2.6",
21
- "@plandesk/db": "0.7.0",
21
+ "@plandesk/db": "0.9.0",
22
22
  "@plandesk/sync-server": "0.5.0"
23
23
  },
24
24
  "dependencies": {