@openartifactsai/server-sdk 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/bin/frame-host.d.ts +2 -0
- package/bin/frame-host.js +47 -0
- package/package.json +124 -4
- package/src/child.d.ts +37 -0
- package/src/child.js +64 -0
- package/src/connectors/connectors.d.ts +101 -0
- package/src/connectors/connectors.js +256 -0
- package/src/connectors/daemon-connectors.d.ts +34 -0
- package/src/connectors/daemon-connectors.js +73 -0
- package/src/connectors/index.d.ts +7 -0
- package/src/connectors/index.js +7 -0
- package/src/connectors/mcp-client.d.ts +157 -0
- package/src/connectors/mcp-client.js +483 -0
- package/src/daemon.d.ts +119 -0
- package/src/daemon.js +301 -0
- package/src/frame/access.d.ts +69 -0
- package/src/frame/access.js +168 -0
- package/src/frame/client/comments.css +1 -0
- package/src/frame/client/comments.js +58 -0
- package/src/frame/client/imports.d.ts +6 -0
- package/src/frame/client/imports.js +112 -0
- package/src/frame/client/mcp-shell.js +345 -0
- package/src/frame/client/mcp.js +253 -0
- package/src/frame/client/runtime.js +1363 -0
- package/src/frame/client/sample.js +762 -0
- package/src/frame/client/shell.css +51 -0
- package/src/frame/client/shell.html +29 -0
- package/src/frame/client/shell.js +1417 -0
- package/src/frame/comments.d.ts +102 -0
- package/src/frame/comments.js +379 -0
- package/src/frame/core.d.ts +313 -0
- package/src/frame/core.js +1757 -0
- package/src/frame/db.d.ts +162 -0
- package/src/frame/db.js +604 -0
- package/src/frame/declaration.d.ts +30 -0
- package/src/frame/declaration.js +60 -0
- package/src/frame/env.d.ts +262 -0
- package/src/frame/env.js +13 -0
- package/src/frame/files.d.ts +130 -0
- package/src/frame/files.js +472 -0
- package/src/frame/index.d.ts +35 -0
- package/src/frame/index.js +33 -0
- package/src/frame/local.d.ts +126 -0
- package/src/frame/local.js +612 -0
- package/src/frame/mcp-relay.d.ts +79 -0
- package/src/frame/mcp-relay.js +191 -0
- package/src/frame/page-api.d.ts +48 -0
- package/src/frame/page-api.js +1 -0
- package/src/frame/page-comments.d.ts +109 -0
- package/src/frame/page-comments.js +1 -0
- package/src/frame/page-db.d.ts +88 -0
- package/src/frame/page-db.js +1 -0
- package/src/frame/page-imports.d.ts +2 -0
- package/src/frame/page-imports.js +2 -0
- package/src/frame/page-services.d.ts +124 -0
- package/src/frame/page-services.js +1 -0
- package/src/frame/page-values.d.ts +90 -0
- package/src/frame/page-values.js +1 -0
- package/src/frame/sample-relay.d.ts +42 -0
- package/src/frame/sample-relay.js +186 -0
- package/src/frame/snapshot-action.d.ts +18 -0
- package/src/frame/snapshot-action.js +82 -0
- package/src/frame/wire.d.ts +198 -0
- package/src/frame/wire.js +21 -0
- package/src/index.d.ts +17 -0
- package/src/index.js +17 -0
- package/src/lan.d.ts +75 -0
- package/src/lan.js +200 -0
- package/src/legacy-env.d.ts +7 -0
- package/src/legacy-env.js +14 -0
- package/src/serve.d.ts +375 -0
- package/src/serve.js +811 -0
- package/src/snapshot.d.ts +41 -0
- package/src/snapshot.js +224 -0
- package/src/type-package.d.ts +48 -0
- package/src/type-package.js +114 -0
- package/src/ws.d.ts +50 -0
- package/src/ws.js +203 -0
- package/src/zip.d.ts +197 -0
- package/src/zip.js +662 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comment threads of a frame artifact, in the shared `comments.json` format
|
|
3
|
+
* (version 1) the other Open Artifacts types write:
|
|
4
|
+
* `{version: 1, threads: [{id: 'c_…', anchor, label, detail?, location, status, agent?, createdAt, updatedAt,
|
|
5
|
+
* resolvedAt?, messages: [{id: 'm_…', author: {kind, name}, text, at, editedAt?, to?, reactions?, assignee?}]}]}`.
|
|
6
|
+
*
|
|
7
|
+
* An original page anchors a comment with its own object; it is kept whole
|
|
8
|
+
* under `location.custom` and `anchor` is its stable JSON text, so the page
|
|
9
|
+
* gets back exactly what it gave. Writes take `comments.json.lock` (O_EXCL)
|
|
10
|
+
* and replace the file through a dot temp file; the file exists only once
|
|
11
|
+
* there is a thread. A store whose process is the only writer of the folder
|
|
12
|
+
* (`lock: 'process'`, a host's own storage) locks within the process instead,
|
|
13
|
+
* never waits on a file, and removes a lock file a crash left behind.
|
|
14
|
+
*/
|
|
15
|
+
import { type PathGuard } from './files.js';
|
|
16
|
+
import type { Author, CommentMessage, Thread } from './page-values.js';
|
|
17
|
+
export type { Author, MessageReaction, CommentMessage, ThreadAgentState, Thread } from './page-values.js';
|
|
18
|
+
export declare const COMMENTS_FILE = "comments.json";
|
|
19
|
+
export declare const TEXT_MAX = 10000;
|
|
20
|
+
export declare const AGENT_AUTHOR: Readonly<Author>;
|
|
21
|
+
interface CommentsDoc {
|
|
22
|
+
version: 1;
|
|
23
|
+
threads: Thread[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* How a {@link CommentStore} keeps two writes apart: `file`, the O_EXCL lock file every process honours (the agent's
|
|
27
|
+
* CLI and the type server write the same folder); `process`, within this process alone, for a store whose process is
|
|
28
|
+
* the only writer (no file is made, and nothing waits).
|
|
29
|
+
*/
|
|
30
|
+
export type CommentLock = 'file' | 'process';
|
|
31
|
+
/** What a checked {@link CommentStore.move} compares: the anchor the mover saw, who moves it, and whether they own the artifact. */
|
|
32
|
+
export interface CommentMoveCheck {
|
|
33
|
+
from: string;
|
|
34
|
+
by: Author;
|
|
35
|
+
owner: boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare class CommentStore {
|
|
38
|
+
readonly root: string;
|
|
39
|
+
readonly file: string;
|
|
40
|
+
readonly lock: CommentLock;
|
|
41
|
+
private readonly guard;
|
|
42
|
+
/** `guard`: how it keeps inside the artifact folder (default {@link LINK_GUARD}: links followed). */
|
|
43
|
+
constructor(root: string, options?: {
|
|
44
|
+
lock?: CommentLock;
|
|
45
|
+
guard?: PathGuard;
|
|
46
|
+
});
|
|
47
|
+
/** The file's text as it is on disk (history compares it); throws when there is none or it leads out of the folder. */
|
|
48
|
+
readText(): string;
|
|
49
|
+
load(): CommentsDoc;
|
|
50
|
+
threads({ all }?: {
|
|
51
|
+
all?: boolean;
|
|
52
|
+
}): Thread[];
|
|
53
|
+
openCount(): number;
|
|
54
|
+
find(doc: CommentsDoc, idOrPrefix: unknown): Thread;
|
|
55
|
+
/** Runs `fn(doc)` under the lock and writes the result back. */
|
|
56
|
+
mutate<T>(fn: (doc: CommentsDoc) => T): T;
|
|
57
|
+
/** Reads, applies `fn` and replaces the file (the caller holds the lock). */
|
|
58
|
+
private write;
|
|
59
|
+
create(input: {
|
|
60
|
+
anchor?: unknown;
|
|
61
|
+
label?: unknown;
|
|
62
|
+
detail?: unknown;
|
|
63
|
+
location?: unknown;
|
|
64
|
+
text: unknown;
|
|
65
|
+
to?: unknown;
|
|
66
|
+
assignee?: unknown;
|
|
67
|
+
author?: Author;
|
|
68
|
+
}): Thread;
|
|
69
|
+
addMessage(threadId: unknown, author: Author, text: unknown, { resolve, to, assignee }?: {
|
|
70
|
+
resolve?: boolean;
|
|
71
|
+
to?: unknown;
|
|
72
|
+
assignee?: unknown;
|
|
73
|
+
}): {
|
|
74
|
+
thread: Thread;
|
|
75
|
+
message: CommentMessage;
|
|
76
|
+
};
|
|
77
|
+
/** Changes the text (and recipients, when given) of `author`'s own message. */
|
|
78
|
+
editMessage(threadId: unknown, messageId: unknown, author: Author, text: unknown, to?: unknown): Thread;
|
|
79
|
+
/** Adds (`on`) or takes back `author`'s `emoji` on a message; adding one already there changes nothing. */
|
|
80
|
+
react(threadId: unknown, messageId: unknown, author: Author, emoji: unknown, on: boolean): Thread;
|
|
81
|
+
setResolved(threadId: unknown, resolved: boolean, by?: Author): Thread;
|
|
82
|
+
/**
|
|
83
|
+
* Re-anchors a thread. With `check` (a pin dragged in the page), the move is refused unless the one moving it
|
|
84
|
+
* started the thread or owns the artifact (403), and unless the thread is still open at the anchor the page saw,
|
|
85
|
+
* `check.from` (409 `comment_changed`): someone else moved, resolved or deleted it first.
|
|
86
|
+
*/
|
|
87
|
+
move(threadId: unknown, anchor: unknown, location?: unknown, check?: CommentMoveCheck): Thread;
|
|
88
|
+
remove(threadId: unknown, messageId?: unknown): {
|
|
89
|
+
deleted: 'message' | 'thread';
|
|
90
|
+
};
|
|
91
|
+
markDelivered(threadId: unknown, via: string): Thread;
|
|
92
|
+
/**
|
|
93
|
+
* Marks the thread sent to the agent. `unasked`: a reply that does not ask the agent, sent because it already took
|
|
94
|
+
* part in the thread; only such a thread can be, and it keeps its status (nothing waits on the agent). One the agent
|
|
95
|
+
* had answered remembers it (`unaskedAt`), so the agent's next reply there is marked `joined`.
|
|
96
|
+
*/
|
|
97
|
+
markSent(threadId: unknown, { unasked }?: {
|
|
98
|
+
unasked?: boolean;
|
|
99
|
+
}): Thread;
|
|
100
|
+
}
|
|
101
|
+
/** What the agent reads of a thread. */
|
|
102
|
+
export declare function agentView(thread: Thread, n?: number): Record<string, unknown>;
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comment threads of a frame artifact, in the shared `comments.json` format
|
|
3
|
+
* (version 1) the other Open Artifacts types write:
|
|
4
|
+
* `{version: 1, threads: [{id: 'c_…', anchor, label, detail?, location, status, agent?, createdAt, updatedAt,
|
|
5
|
+
* resolvedAt?, messages: [{id: 'm_…', author: {kind, name}, text, at, editedAt?, to?, reactions?, assignee?}]}]}`.
|
|
6
|
+
*
|
|
7
|
+
* An original page anchors a comment with its own object; it is kept whole
|
|
8
|
+
* under `location.custom` and `anchor` is its stable JSON text, so the page
|
|
9
|
+
* gets back exactly what it gave. Writes take `comments.json.lock` (O_EXCL)
|
|
10
|
+
* and replace the file through a dot temp file; the file exists only once
|
|
11
|
+
* there is a thread. A store whose process is the only writer of the folder
|
|
12
|
+
* (`lock: 'process'`, a host's own storage) locks within the process instead,
|
|
13
|
+
* never waits on a file, and removes a lock file a crash left behind.
|
|
14
|
+
*/
|
|
15
|
+
import { findCommentThread } from '@openartifactsai/protocol';
|
|
16
|
+
import { randomBytes } from 'node:crypto';
|
|
17
|
+
import { closeSync, existsSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import { FrameError, LINK_GUARD } from "./files.js";
|
|
20
|
+
export const COMMENTS_FILE = 'comments.json';
|
|
21
|
+
export const TEXT_MAX = 10_000;
|
|
22
|
+
const LOCK_STALE_MS = 10_000;
|
|
23
|
+
const LOCK_WAIT_MS = 5_000;
|
|
24
|
+
export const AGENT_AUTHOR = Object.freeze({ kind: 'agent', name: 'agent' });
|
|
25
|
+
/** At most this many recipients on a message: the 32 people a comment may name, and the agent. */
|
|
26
|
+
const TO_MAX = 33;
|
|
27
|
+
/** The longest emoji a reaction may be (a sequence with modifiers). */
|
|
28
|
+
const EMOJI_MAX = 32;
|
|
29
|
+
/** Files being written under the `process` lock, by path: a write that comes back to the same file while it runs. */
|
|
30
|
+
const writing = new Set();
|
|
31
|
+
const newId = (prefix) => `${prefix}${randomBytes(5).toString('hex')}`;
|
|
32
|
+
const now = () => new Date().toISOString();
|
|
33
|
+
export class CommentStore {
|
|
34
|
+
root;
|
|
35
|
+
file;
|
|
36
|
+
lock;
|
|
37
|
+
guard;
|
|
38
|
+
/** `guard`: how it keeps inside the artifact folder (default {@link LINK_GUARD}: links followed). */
|
|
39
|
+
constructor(root, options = {}) {
|
|
40
|
+
this.root = root;
|
|
41
|
+
this.file = join(root, COMMENTS_FILE);
|
|
42
|
+
this.lock = options.lock ?? 'file';
|
|
43
|
+
this.guard = options.guard ?? LINK_GUARD;
|
|
44
|
+
// The only writer: a lock file here was left by a write that never finished (a crash), and nobody holds it.
|
|
45
|
+
if (this.lock === 'process')
|
|
46
|
+
rmSync(`${this.file}.lock`, { force: true });
|
|
47
|
+
}
|
|
48
|
+
/** The file's text as it is on disk (history compares it); throws when there is none or it leads out of the folder. */
|
|
49
|
+
readText() {
|
|
50
|
+
if (!this.guard.inside(this.root, this.file))
|
|
51
|
+
throw new FrameError('read_only_path', `${COMMENTS_FILE} leads out of the artifact folder (a link).`, 403);
|
|
52
|
+
return readFileSync(this.file, 'utf8');
|
|
53
|
+
}
|
|
54
|
+
load() {
|
|
55
|
+
try {
|
|
56
|
+
// A comments.json that is a link to a file outside the artifact folder is not read (and a write replaces the link).
|
|
57
|
+
if (!this.guard.inside(this.root, this.file))
|
|
58
|
+
return { version: 1, threads: [] };
|
|
59
|
+
const doc = JSON.parse(readFileSync(this.file, 'utf8'));
|
|
60
|
+
if (doc && Array.isArray(doc.threads))
|
|
61
|
+
return { ...doc, version: 1, threads: doc.threads };
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// none yet, or unreadable: an empty list
|
|
65
|
+
}
|
|
66
|
+
return { version: 1, threads: [] };
|
|
67
|
+
}
|
|
68
|
+
threads({ all = false } = {}) {
|
|
69
|
+
const list = this.load().threads.filter((t) => all || t.status !== 'resolved');
|
|
70
|
+
return list.sort((a, b) => String(a.createdAt).localeCompare(String(b.createdAt)));
|
|
71
|
+
}
|
|
72
|
+
openCount() {
|
|
73
|
+
return this.load().threads.filter((t) => t.status !== 'resolved').length;
|
|
74
|
+
}
|
|
75
|
+
find(doc, idOrPrefix) {
|
|
76
|
+
return findCommentThread(doc.threads, String(idOrPrefix ?? ''), (code, message, status) => new FrameError(code, message, status));
|
|
77
|
+
}
|
|
78
|
+
/** Runs `fn(doc)` under the lock and writes the result back. */
|
|
79
|
+
mutate(fn) {
|
|
80
|
+
if (this.lock === 'process') {
|
|
81
|
+
// A write runs to its end in one turn of this process, so the only other writer can be `fn` itself.
|
|
82
|
+
if (writing.has(this.file))
|
|
83
|
+
throw new FrameError('unavailable', 'comments.json is being written.', 409);
|
|
84
|
+
writing.add(this.file);
|
|
85
|
+
try {
|
|
86
|
+
return this.write(fn);
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
writing.delete(this.file);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const lock = `${this.file}.lock`;
|
|
93
|
+
const started = Date.now();
|
|
94
|
+
let fd;
|
|
95
|
+
for (;;) {
|
|
96
|
+
try {
|
|
97
|
+
fd = openSync(lock, 'wx');
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (error.code !== 'EEXIST')
|
|
102
|
+
throw error;
|
|
103
|
+
try {
|
|
104
|
+
if (Date.now() - statSync(lock).mtimeMs > LOCK_STALE_MS) {
|
|
105
|
+
rmSync(lock, { force: true });
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (Date.now() - started > LOCK_WAIT_MS)
|
|
113
|
+
throw new FrameError('unavailable', 'comments.json is locked by another writer.', 409);
|
|
114
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
return this.write(fn);
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
closeSync(fd);
|
|
122
|
+
rmSync(lock, { force: true });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Reads, applies `fn` and replaces the file (the caller holds the lock). */
|
|
126
|
+
write(fn) {
|
|
127
|
+
const doc = this.load();
|
|
128
|
+
const out = fn(doc);
|
|
129
|
+
if (doc.threads.length || existsSync(this.file)) {
|
|
130
|
+
const tmp = join(this.root, `.${COMMENTS_FILE}.${randomBytes(4).toString('hex')}.tmp`);
|
|
131
|
+
writeFileSync(tmp, `${JSON.stringify({ version: 1, threads: doc.threads }, null, 2)}\n`, { flag: 'wx' });
|
|
132
|
+
try {
|
|
133
|
+
renameSync(tmp, this.file);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
rmSync(tmp, { force: true });
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
create(input) {
|
|
143
|
+
const body = checkText(input.text);
|
|
144
|
+
const to = checkTo(input.to);
|
|
145
|
+
const author = input.author ?? { ...AGENT_AUTHOR };
|
|
146
|
+
const assignee = input.assignee === undefined || input.assignee === null ? undefined : checkAssignee(input.assignee, author);
|
|
147
|
+
return this.mutate((doc) => {
|
|
148
|
+
const at = now();
|
|
149
|
+
const thread = {
|
|
150
|
+
id: newId('c_'),
|
|
151
|
+
anchor: typeof input.anchor === 'string' ? input.anchor : JSON.stringify(input.anchor ?? null),
|
|
152
|
+
label: typeof input.label === 'string' && input.label ? input.label.slice(0, 200) : 'the page',
|
|
153
|
+
...(typeof input.detail === 'string' && input.detail ? { detail: input.detail.slice(0, 2000) } : {}),
|
|
154
|
+
location: isObject(input.location) ? input.location : {},
|
|
155
|
+
status: 'open',
|
|
156
|
+
createdAt: at,
|
|
157
|
+
updatedAt: at,
|
|
158
|
+
messages: [{ id: newId('m_'), author, text: body, at, ...(to ? { to } : {}), ...(assignee !== undefined ? { assignee } : {}) }],
|
|
159
|
+
};
|
|
160
|
+
doc.threads.push(thread);
|
|
161
|
+
return thread;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
addMessage(threadId, author, text, { resolve = false, to, assignee } = {}) {
|
|
165
|
+
const body = checkText(text);
|
|
166
|
+
const recipients = checkTo(to);
|
|
167
|
+
const assigns = assignee === undefined ? undefined : checkAssignee(assignee, author);
|
|
168
|
+
return this.mutate((doc) => {
|
|
169
|
+
const thread = this.find(doc, threadId);
|
|
170
|
+
const at = now();
|
|
171
|
+
const message = { id: newId('m_'), author, text: body, at, ...(recipients ? { to: recipients } : {}), ...(assigns !== undefined ? { assignee: assigns } : {}) };
|
|
172
|
+
thread.messages.push(message);
|
|
173
|
+
thread.updatedAt = at;
|
|
174
|
+
if (author.kind === 'agent') {
|
|
175
|
+
if (thread.agent?.unaskedAt)
|
|
176
|
+
message.joined = true;
|
|
177
|
+
const { unaskedAt: _answered, ...agent } = thread.agent ?? { status: 'replied' };
|
|
178
|
+
thread.agent = { ...agent, status: 'replied', repliedAt: at };
|
|
179
|
+
if (resolve)
|
|
180
|
+
setStatus(thread, 'resolved', at, author);
|
|
181
|
+
}
|
|
182
|
+
else if (thread.status === 'resolved') {
|
|
183
|
+
setStatus(thread, 'open', at);
|
|
184
|
+
}
|
|
185
|
+
return { thread, message };
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/** Changes the text (and recipients, when given) of `author`'s own message. */
|
|
189
|
+
editMessage(threadId, messageId, author, text, to) {
|
|
190
|
+
const body = checkText(text);
|
|
191
|
+
const recipients = to === undefined ? undefined : checkTo(to);
|
|
192
|
+
return this.mutate((doc) => {
|
|
193
|
+
const thread = this.find(doc, threadId);
|
|
194
|
+
const message = findMessage(thread, messageId);
|
|
195
|
+
if (!sameAuthor(message.author, author))
|
|
196
|
+
throw new FrameError('forbidden', 'Only its author edits a comment.', 403);
|
|
197
|
+
const at = now();
|
|
198
|
+
message.text = body;
|
|
199
|
+
message.editedAt = at;
|
|
200
|
+
if (to !== undefined) {
|
|
201
|
+
if (recipients)
|
|
202
|
+
message.to = recipients;
|
|
203
|
+
else
|
|
204
|
+
delete message.to;
|
|
205
|
+
}
|
|
206
|
+
thread.updatedAt = at;
|
|
207
|
+
return thread;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/** Adds (`on`) or takes back `author`'s `emoji` on a message; adding one already there changes nothing. */
|
|
211
|
+
react(threadId, messageId, author, emoji, on) {
|
|
212
|
+
if (typeof emoji !== 'string' || !emoji.trim() || emoji.length > EMOJI_MAX)
|
|
213
|
+
throw new FrameError('invalid_argument', 'A reaction is one emoji.');
|
|
214
|
+
return this.mutate((doc) => {
|
|
215
|
+
const thread = this.find(doc, threadId);
|
|
216
|
+
const message = findMessage(thread, messageId);
|
|
217
|
+
const reactions = message.reactions ?? [];
|
|
218
|
+
const mine = reactions.findIndex((r) => r.emoji === emoji && sameAuthor(r.author, author));
|
|
219
|
+
if (on && mine < 0)
|
|
220
|
+
reactions.push({ emoji, author, at: now() });
|
|
221
|
+
if (!on && mine >= 0)
|
|
222
|
+
reactions.splice(mine, 1);
|
|
223
|
+
if (reactions.length)
|
|
224
|
+
message.reactions = reactions;
|
|
225
|
+
else
|
|
226
|
+
delete message.reactions;
|
|
227
|
+
return thread;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
setResolved(threadId, resolved, by) {
|
|
231
|
+
return this.mutate((doc) => {
|
|
232
|
+
const thread = this.find(doc, threadId);
|
|
233
|
+
setStatus(thread, resolved ? 'resolved' : 'open', now(), by);
|
|
234
|
+
return thread;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Re-anchors a thread. With `check` (a pin dragged in the page), the move is refused unless the one moving it
|
|
239
|
+
* started the thread or owns the artifact (403), and unless the thread is still open at the anchor the page saw,
|
|
240
|
+
* `check.from` (409 `comment_changed`): someone else moved, resolved or deleted it first.
|
|
241
|
+
*/
|
|
242
|
+
move(threadId, anchor, location, check) {
|
|
243
|
+
return this.mutate((doc) => {
|
|
244
|
+
const thread = this.find(doc, threadId);
|
|
245
|
+
if (check) {
|
|
246
|
+
const first = thread.messages[0];
|
|
247
|
+
if (!check.owner && !(first && sameAuthor(first.author, check.by)))
|
|
248
|
+
throw new FrameError('forbidden', 'Only the person who started this comment, or the owner, can move it.', 403);
|
|
249
|
+
if (thread.anchor !== check.from || thread.status !== 'open')
|
|
250
|
+
throw new FrameError('comment_changed', 'This comment changed before it could be moved.', 409);
|
|
251
|
+
}
|
|
252
|
+
thread.anchor = typeof anchor === 'string' ? anchor : JSON.stringify(anchor ?? null);
|
|
253
|
+
if (isObject(location))
|
|
254
|
+
thread.location = location;
|
|
255
|
+
thread.updatedAt = now();
|
|
256
|
+
return thread;
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
remove(threadId, messageId) {
|
|
260
|
+
return this.mutate((doc) => {
|
|
261
|
+
const thread = this.find(doc, threadId);
|
|
262
|
+
if (messageId) {
|
|
263
|
+
thread.messages = thread.messages.filter((m) => m.id !== messageId);
|
|
264
|
+
if (thread.messages.length) {
|
|
265
|
+
thread.updatedAt = now();
|
|
266
|
+
return { deleted: 'message' };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
doc.threads = doc.threads.filter((t) => t !== thread);
|
|
270
|
+
return { deleted: 'thread' };
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
markDelivered(threadId, via) {
|
|
274
|
+
return this.mutate((doc) => {
|
|
275
|
+
const thread = this.find(doc, threadId);
|
|
276
|
+
const at = now();
|
|
277
|
+
thread.agent = { status: 'delivered', sentAt: thread.agent?.sentAt ?? at, deliveredAt: at, via };
|
|
278
|
+
return thread;
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Marks the thread sent to the agent. `unasked`: a reply that does not ask the agent, sent because it already took
|
|
283
|
+
* part in the thread; only such a thread can be, and it keeps its status (nothing waits on the agent). One the agent
|
|
284
|
+
* had answered remembers it (`unaskedAt`), so the agent's next reply there is marked `joined`.
|
|
285
|
+
*/
|
|
286
|
+
markSent(threadId, { unasked = false } = {}) {
|
|
287
|
+
return this.mutate((doc) => {
|
|
288
|
+
const thread = this.find(doc, threadId);
|
|
289
|
+
if (!unasked) {
|
|
290
|
+
thread.agent = { status: 'pending', sentAt: now() };
|
|
291
|
+
return thread;
|
|
292
|
+
}
|
|
293
|
+
if (!thread.agent && !thread.messages.some((m) => m.author?.kind === 'agent')) {
|
|
294
|
+
throw new FrameError('invalid_argument', 'Only a thread the agent took part in is sent to it without asking it.');
|
|
295
|
+
}
|
|
296
|
+
const agent = thread.agent ?? { status: 'replied' };
|
|
297
|
+
thread.agent = agent.status === 'replied' ? { ...agent, unaskedAt: now() } : agent;
|
|
298
|
+
return thread;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function setStatus(thread, status, at, by) {
|
|
303
|
+
thread.status = status;
|
|
304
|
+
thread.updatedAt = at;
|
|
305
|
+
if (status === 'resolved') {
|
|
306
|
+
thread.resolvedAt = at;
|
|
307
|
+
if (by)
|
|
308
|
+
thread.resolvedBy = { ...by };
|
|
309
|
+
else
|
|
310
|
+
delete thread.resolvedBy;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
delete thread.resolvedAt;
|
|
314
|
+
delete thread.resolvedBy;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function findMessage(thread, messageId) {
|
|
318
|
+
const message = thread.messages.find((m) => m.id === messageId);
|
|
319
|
+
if (!message)
|
|
320
|
+
throw new FrameError('not_found', `No message ${String(messageId)} in thread ${thread.id}.`, 404);
|
|
321
|
+
return message;
|
|
322
|
+
}
|
|
323
|
+
const sameAuthor = (a, b) => a?.kind === b.kind && a.name === b.name;
|
|
324
|
+
/** The recipients a message names: short non-empty strings, without repeats; none is `undefined`. */
|
|
325
|
+
function checkTo(to) {
|
|
326
|
+
if (to === undefined || to === null)
|
|
327
|
+
return undefined;
|
|
328
|
+
if (!Array.isArray(to) || to.length > TO_MAX || !to.every((name) => typeof name === 'string' && name && name.length <= 200)) {
|
|
329
|
+
throw new FrameError('invalid_argument', `to is a list of at most ${TO_MAX} names.`);
|
|
330
|
+
}
|
|
331
|
+
const names = [...new Set(to)];
|
|
332
|
+
return names.length ? names : undefined;
|
|
333
|
+
}
|
|
334
|
+
/** Who a person's message assigns the thread to (`null`: no one). Only people assign, and only people are assigned. */
|
|
335
|
+
function checkAssignee(assignee, author) {
|
|
336
|
+
if (author.kind === 'agent')
|
|
337
|
+
throw new FrameError('invalid_argument', 'Only a person assigns a thread.');
|
|
338
|
+
if (assignee === null)
|
|
339
|
+
return null;
|
|
340
|
+
if (typeof assignee !== 'string' || !assignee.trim() || assignee.length > 200 || assignee === AGENT_AUTHOR.name) {
|
|
341
|
+
throw new FrameError('invalid_argument', 'assignee is the name of a person, or null.');
|
|
342
|
+
}
|
|
343
|
+
return assignee;
|
|
344
|
+
}
|
|
345
|
+
function checkText(text) {
|
|
346
|
+
if (typeof text !== 'string' || !text.trim())
|
|
347
|
+
throw new FrameError('invalid_argument', 'A comment needs text.');
|
|
348
|
+
if (text.length > TEXT_MAX)
|
|
349
|
+
throw new FrameError('invalid_argument', `A comment is at most ${TEXT_MAX} characters.`);
|
|
350
|
+
return text;
|
|
351
|
+
}
|
|
352
|
+
function isObject(v) {
|
|
353
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
354
|
+
}
|
|
355
|
+
/** What the agent reads of a thread. */
|
|
356
|
+
export function agentView(thread, n) {
|
|
357
|
+
return {
|
|
358
|
+
id: thread.id,
|
|
359
|
+
...(n !== undefined ? { n } : {}),
|
|
360
|
+
status: thread.status,
|
|
361
|
+
...(typeof thread.location?.file === 'string' ? { file: thread.location.file } : {}),
|
|
362
|
+
where: thread.label,
|
|
363
|
+
...(thread.detail ? { detail: thread.detail } : {}),
|
|
364
|
+
location: thread.location ?? {},
|
|
365
|
+
anchor: thread.label,
|
|
366
|
+
...(thread.agent ? { agent: thread.agent } : {}),
|
|
367
|
+
messages: thread.messages.map((m) => ({
|
|
368
|
+
from: m.author?.kind ?? 'user',
|
|
369
|
+
name: m.author?.name ?? '',
|
|
370
|
+
text: m.text,
|
|
371
|
+
at: m.at,
|
|
372
|
+
...(m.editedAt ? { editedAt: m.editedAt } : {}),
|
|
373
|
+
...(m.to?.length ? { to: m.to } : {}),
|
|
374
|
+
...(m.reactions?.length ? { reactions: m.reactions.map((r) => `${r.emoji} ${r.author.kind === 'agent' ? 'agent' : r.author.name}`) } : {}),
|
|
375
|
+
...(m.joined ? { joined: true } : {}),
|
|
376
|
+
...(m.assignee !== undefined ? { assignee: m.assignee } : {}),
|
|
377
|
+
})),
|
|
378
|
+
};
|
|
379
|
+
}
|