shapewall 0.3.2 → 0.6.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/CHANGELOG.md +357 -0
- package/NOTICE +7 -0
- package/README.md +50 -4
- package/THIRD_PARTY.md +39 -0
- package/dist/agent/package-lock.json +2 -2
- package/dist/client/assets/BoardChat-DdwBcb2N.js +27 -0
- package/dist/client/assets/BuzzDialog-DpUcnU6S.js +1 -0
- package/dist/client/assets/ChecksPanel-CngYV4yz.js +1 -0
- package/dist/client/assets/History-CD0saQA5.js +1 -0
- package/dist/client/assets/LocalSettings-MZy2baig.js +1 -0
- package/dist/client/assets/{Management-DmTh1-tA.js → Management-CSvZ-SHO.js} +1 -1
- package/dist/client/assets/QuickConnect-foLh2QUm.js +1 -0
- package/dist/client/assets/ShareDialog-DPWDftoL.js +2 -0
- package/dist/client/assets/app-update-dialog-CtiXpTRD.js +1 -0
- package/dist/client/assets/{circle-x-3UxAvczM.js → circle-x-CteRBtXC.js} +1 -1
- package/dist/client/assets/index-Cxz1UVcV.css +2 -0
- package/dist/client/assets/index-uUZVBYUS.js +37 -0
- package/dist/client/assets/play-BjRNIGSf.js +1 -0
- package/dist/client/assets/{refresh-cw-BRHNyCnY.js → refresh-cw-r_tZiFfB.js} +1 -1
- package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
- package/dist/client/assets/ui-zQpn-PdL.js +41 -0
- package/dist/client/build-info.json +1 -1
- package/dist/client/index.html +4 -3
- package/dist/client/sw.js +2 -2
- package/dist/console/assets/{LocalAccountsApp-IvHd9zWN.js → LocalAccountsApp-DIdy1KnN.js} +1 -1
- package/dist/console/assets/{LocalConsole-DOyLou56.js → LocalConsole-DfcQztF-.js} +1 -1
- package/dist/console/assets/{app-update-dialog-Da46CJpv.js → app-update-dialog-ttl9lF5A.js} +1 -1
- package/dist/console/assets/console-OqAjD0kV.js +55 -0
- package/dist/console/assets/console-kvjrAArQ.css +2 -0
- package/dist/console/build-info.json +1 -1
- package/dist/console/console.html +2 -2
- package/docs/agent-hardening.md +192 -0
- package/docs/agent-recipes.md +23 -0
- package/docs/agent-workflow.md +5 -0
- package/docs/agents.md +74 -6
- package/docs/config.md +1 -1
- package/docs/deployment.md +2 -0
- package/docs/design-system.md +20 -15
- package/docs/hosted.md +815 -3
- package/docs/where-to-run.md +123 -0
- package/package.json +2 -1
- package/src/agent/agent-logos.js +81 -0
- package/src/agent/backends/acp.js +528 -25
- package/src/agent/branch-mode.js +17 -3
- package/src/agent/card-hash.js +65 -0
- package/src/agent/config.js +27 -2
- package/src/agent/context.js +41 -0
- package/src/agent/exec-paths.js +105 -0
- package/src/agent/icon-store.js +100 -0
- package/src/agent/identity.js +77 -0
- package/src/agent/owner-mcp.js +19 -3
- package/src/agent/routes.js +82 -22
- package/src/agent/run.js +14 -2
- package/src/agent/settings-routes.js +233 -14
- package/src/agent/starter/cli.js +6 -1
- package/src/agent/test-timing.js +14 -0
- package/src/agent/tools.js +4 -2
- package/src/agent/turn-changes.js +70 -8
- package/src/agent/user-settings.js +51 -3
- package/src/capture/element-map.js +61 -4
- package/src/cli/args.js +13 -0
- package/src/cli/commands.js +20 -5
- package/src/cli/help.js +36 -1
- package/src/hosted/admin.js +7 -0
- package/src/hosted/agent-approvals.js +28 -3
- package/src/hosted/agent-link.js +1032 -0
- package/src/hosted/agent-policy.js +10 -3
- package/src/hosted/agent-presence.js +81 -0
- package/src/hosted/agent-routes.js +135 -0
- package/src/hosted/agents.js +1199 -0
- package/src/hosted/app.js +94 -4
- package/src/hosted/board-routes.js +134 -3
- package/src/hosted/chat-frames.js +256 -0
- package/src/hosted/chat-job.js +204 -0
- package/src/hosted/chat-permissions.js +191 -0
- package/src/hosted/chat-routes.js +1009 -0
- package/src/hosted/chat-store.js +460 -0
- package/src/hosted/comment-files.js +8 -1
- package/src/hosted/comments.js +1 -1
- package/src/hosted/console-routes.js +58 -1
- package/src/hosted/db.js +165 -0
- package/src/hosted/device-routes.js +64 -8
- package/src/hosted/device.js +35 -4
- package/src/hosted/events.js +8 -6
- package/src/hosted/membership.js +9 -1
- package/src/hosted/pages.js +2 -0
- package/src/hosted/permissions.js +38 -0
- package/src/hosted/presence.js +18 -1
- package/src/hosted/projects.js +60 -2
- package/src/hosted/ratelimit.js +8 -0
- package/src/hosted/tenancy.js +18 -0
- package/src/hosted/token-api.js +118 -15
- package/src/hosted/token-events.js +7 -2
- package/src/hosted/tokens.js +24 -7
- package/src/hosted/turn-hub.js +152 -0
- package/src/mcp.js +10 -2
- package/src/remote/agent-connect.js +433 -0
- package/src/remote/agent-fix.js +611 -0
- package/src/remote/agent-job.js +216 -0
- package/src/remote/agent-link.js +904 -0
- package/src/remote/agent-policy-file.js +242 -0
- package/src/remote/credentials.js +35 -1
- package/src/remote/login.js +12 -2
- package/src/store.js +5 -0
- package/dist/client/assets/BoardChat-BeRp6007.js +0 -27
- package/dist/client/assets/BuzzDialog-CrYy48hh.js +0 -1
- package/dist/client/assets/ChecksPanel-CB820mhj.js +0 -1
- package/dist/client/assets/History-Ba909bGJ.js +0 -1
- package/dist/client/assets/LocalSettings-ChMi9Zir.js +0 -1
- package/dist/client/assets/QuickConnect-CrVweWZB.js +0 -1
- package/dist/client/assets/ShareDialog-DO2HhY6g.js +0 -2
- package/dist/client/assets/app-update-dialog-C09ODdUQ.js +0 -1
- package/dist/client/assets/index-BIjfZKnp.js +0 -37
- package/dist/client/assets/index-CXtuJi1K.css +0 -2
- package/dist/client/assets/shield-check-DMpniYP6.js +0 -1
- package/dist/client/assets/ui-mhL9gsne.js +0 -41
- package/dist/console/assets/console-CIBv7cXU.js +0 -55
- package/dist/console/assets/console-Fp4k9iyx.css +0 -2
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
// Chat on a hosted board, in the database (migration 13): threads, their
|
|
2
|
+
// messages, and turns. Every query goes through the tenant handle `t`
|
|
3
|
+
// (src/hosted/tenancy.js).
|
|
4
|
+
//
|
|
5
|
+
// thread belongs to one board. "board" and "comment-<n>" are shared by
|
|
6
|
+
// the members of the workspace; a "chat-..." belongs to the person
|
|
7
|
+
// who started it and nobody else reads it. A thread names the
|
|
8
|
+
// agent it talks to (its "profile" on a local board).
|
|
9
|
+
// message the person's text (with what they mentioned and attached) or
|
|
10
|
+
// the agent's answer with its cards in their final state.
|
|
11
|
+
// turn one question on its way through an agent: queued, running, then
|
|
12
|
+
// done, stopped, error or interrupted. Its live events are in
|
|
13
|
+
// memory (src/hosted/turn-hub.js).
|
|
14
|
+
//
|
|
15
|
+
// A usage record (agent_runs) is written for every turn: who asked, which
|
|
16
|
+
// agent, where, when, how it ended, and the numbers the agent reported.
|
|
17
|
+
// Never message text.
|
|
18
|
+
//
|
|
19
|
+
// The pure thread rules (titles, who sees a thread, search) are the local
|
|
20
|
+
// board's: src/agent/thread-history.js.
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
CHAT_RE,
|
|
24
|
+
THREAD_RE,
|
|
25
|
+
autoTitle,
|
|
26
|
+
newestFirst,
|
|
27
|
+
renameThread,
|
|
28
|
+
searchNeedle,
|
|
29
|
+
threadKind,
|
|
30
|
+
threadSummary,
|
|
31
|
+
} from '../agent/thread-history.js'
|
|
32
|
+
import { newId } from './crypto.js'
|
|
33
|
+
import { HttpError } from './http.js'
|
|
34
|
+
|
|
35
|
+
export const CHAT_LIMITS = {
|
|
36
|
+
// Per thread: the oldest messages go.
|
|
37
|
+
messages: 200,
|
|
38
|
+
// A member's own chats on one board (the shared threads are apart).
|
|
39
|
+
privateChats: 50,
|
|
40
|
+
// Threads read for a snippet in one search.
|
|
41
|
+
searchRead: 40,
|
|
42
|
+
text: 8000,
|
|
43
|
+
}
|
|
44
|
+
export const TURN_ENDED = ['done', 'stopped', 'error', 'interrupted']
|
|
45
|
+
export const INTERRUPTED_MESSAGE = "The agent's machine went away before it finished. Ask again."
|
|
46
|
+
|
|
47
|
+
const parse = (text) => {
|
|
48
|
+
try {
|
|
49
|
+
const value = JSON.parse(text)
|
|
50
|
+
return value && typeof value === 'object' ? value : {}
|
|
51
|
+
} catch {
|
|
52
|
+
return {}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createChatStore(t, { now = () => new Date() } = {}) {
|
|
57
|
+
const iso = () => now().toISOString()
|
|
58
|
+
const KEY = 'workspace_id = :ws AND project_id = :project AND board_id = :board AND thread_id = :thread'
|
|
59
|
+
|
|
60
|
+
// ---- Threads ----
|
|
61
|
+
|
|
62
|
+
const validThread = (threadId) => typeof threadId === 'string' && THREAD_RE.test(threadId)
|
|
63
|
+
const thread = (projectId, boardId, threadId) =>
|
|
64
|
+
(validThread(threadId) && t.get(`SELECT * FROM chat_threads WHERE ${KEY}`, { project: projectId, board: boardId, thread: threadId })) ||
|
|
65
|
+
null
|
|
66
|
+
// Who may read a thread: the shared ones every member; a chat its owner
|
|
67
|
+
// (a chat nobody wrote in yet has no row and is anyone's to start).
|
|
68
|
+
const visible = (row, threadId, userId) => !CHAT_RE.test(threadId) || !row || !row.owner_user_id || row.owner_user_id === userId
|
|
69
|
+
// The thread's row, made on its first write. A chat belongs to `userId`.
|
|
70
|
+
// Returns { row, created }.
|
|
71
|
+
function ensure(projectId, boardId, threadId, { userId, agentId = null }) {
|
|
72
|
+
const found = thread(projectId, boardId, threadId)
|
|
73
|
+
if (found) return { row: found, created: false }
|
|
74
|
+
// A chat this person opened and never wrote in is forgotten after an
|
|
75
|
+
// hour, so opening chats never fills the board's allowance.
|
|
76
|
+
if (CHAT_RE.test(threadId) && userId)
|
|
77
|
+
t.run(
|
|
78
|
+
`DELETE FROM chat_threads WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND owner_user_id = :user
|
|
79
|
+
AND created_at < :cutoff AND NOT EXISTS (SELECT 1 FROM chat_messages m WHERE m.workspace_id = chat_threads.workspace_id
|
|
80
|
+
AND m.project_id = chat_threads.project_id AND m.board_id = chat_threads.board_id AND m.thread_id = chat_threads.thread_id)
|
|
81
|
+
AND NOT EXISTS (SELECT 1 FROM chat_turns n WHERE n.workspace_id = chat_threads.workspace_id
|
|
82
|
+
AND n.project_id = chat_threads.project_id AND n.board_id = chat_threads.board_id AND n.thread_id = chat_threads.thread_id)`,
|
|
83
|
+
{ project: projectId, board: boardId, user: userId, cutoff: new Date(now().getTime() - 60 * 60_000).toISOString() },
|
|
84
|
+
)
|
|
85
|
+
// A member's own chats on a board are capped apart from the shared
|
|
86
|
+
// threads (the board's and one per comment), so one person cannot use
|
|
87
|
+
// up what everyone shares.
|
|
88
|
+
if (CHAT_RE.test(threadId)) {
|
|
89
|
+
const mine = t.get(
|
|
90
|
+
"SELECT COUNT(*) AS n FROM chat_threads WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND owner_user_id = :user AND thread_id LIKE 'chat-%'",
|
|
91
|
+
{ project: projectId, board: boardId, user: userId || '' },
|
|
92
|
+
).n
|
|
93
|
+
if (mine >= CHAT_LIMITS.privateChats)
|
|
94
|
+
throw new HttpError(
|
|
95
|
+
409,
|
|
96
|
+
'too_many_threads',
|
|
97
|
+
`You have ${CHAT_LIMITS.privateChats} chats of your own on this board. Delete some from the chat history first.`,
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
const at = iso()
|
|
101
|
+
t.run(
|
|
102
|
+
`INSERT INTO chat_threads (workspace_id, project_id, board_id, thread_id, agent_id, owner_user_id, created_by, created_at, updated_at)
|
|
103
|
+
VALUES (:ws, :project, :board, :thread, :agent, :owner, :user, :at, :at)`,
|
|
104
|
+
{
|
|
105
|
+
project: projectId,
|
|
106
|
+
board: boardId,
|
|
107
|
+
thread: threadId,
|
|
108
|
+
agent: agentId,
|
|
109
|
+
owner: CHAT_RE.test(threadId) ? userId : null,
|
|
110
|
+
user: userId,
|
|
111
|
+
at,
|
|
112
|
+
},
|
|
113
|
+
)
|
|
114
|
+
return { row: thread(projectId, boardId, threadId), created: true }
|
|
115
|
+
}
|
|
116
|
+
function setAgent(projectId, boardId, threadId, agentId) {
|
|
117
|
+
t.run(`UPDATE chat_threads SET agent_id = :agent WHERE ${KEY}`, {
|
|
118
|
+
project: projectId,
|
|
119
|
+
board: boardId,
|
|
120
|
+
thread: threadId,
|
|
121
|
+
agent: agentId,
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
function change(projectId, boardId, threadId, { title, archived }) {
|
|
125
|
+
const row = thread(projectId, boardId, threadId)
|
|
126
|
+
if (!row) throw new HttpError(404, 'unknown_thread', 'No such chat.')
|
|
127
|
+
if (title !== undefined) {
|
|
128
|
+
const shape = { id: threadId, messages: messages(projectId, boardId, threadId), title: row.title, titleSource: row.title_source }
|
|
129
|
+
renameThread(shape, title)
|
|
130
|
+
t.run(`UPDATE chat_threads SET title = :title, title_source = :source WHERE ${KEY}`, {
|
|
131
|
+
project: projectId,
|
|
132
|
+
board: boardId,
|
|
133
|
+
thread: threadId,
|
|
134
|
+
title: shape.title || null,
|
|
135
|
+
source: shape.titleSource || null,
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
if (archived !== undefined)
|
|
139
|
+
t.run(`UPDATE chat_threads SET archived = :archived WHERE ${KEY}`, {
|
|
140
|
+
project: projectId,
|
|
141
|
+
board: boardId,
|
|
142
|
+
thread: threadId,
|
|
143
|
+
archived: archived ? 1 : 0,
|
|
144
|
+
})
|
|
145
|
+
return thread(projectId, boardId, threadId)
|
|
146
|
+
}
|
|
147
|
+
// The thread and everything in it. Its usage records stay (they are history).
|
|
148
|
+
function remove(projectId, boardId, threadId) {
|
|
149
|
+
const key = { project: projectId, board: boardId, thread: threadId }
|
|
150
|
+
t.run(`DELETE FROM chat_permissions WHERE ${KEY}`, key)
|
|
151
|
+
t.run(`DELETE FROM chat_messages WHERE ${KEY}`, key)
|
|
152
|
+
t.run(`DELETE FROM chat_turns WHERE ${KEY}`, key)
|
|
153
|
+
t.run(`DELETE FROM chat_threads WHERE ${KEY}`, key)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ---- Messages ----
|
|
157
|
+
|
|
158
|
+
const view = (row) => ({ ...parse(row.body), id: row.id, role: row.role, createdAt: row.created_at })
|
|
159
|
+
const rows = (projectId, boardId, threadId) =>
|
|
160
|
+
t.all(`SELECT * FROM chat_messages WHERE ${KEY} ORDER BY created_at, rowid`, { project: projectId, board: boardId, thread: threadId })
|
|
161
|
+
const messages = (projectId, boardId, threadId) => rows(projectId, boardId, threadId).map(view)
|
|
162
|
+
function append(projectId, boardId, threadId, { role, body, userId = null, agentId = null, turnId = null }) {
|
|
163
|
+
const id = newId('msg')
|
|
164
|
+
const text = JSON.stringify(body)
|
|
165
|
+
const at = iso()
|
|
166
|
+
t.run(
|
|
167
|
+
`INSERT INTO chat_messages (workspace_id, project_id, board_id, thread_id, id, role, author_user_id, agent_id, turn_id, body, bytes, created_at)
|
|
168
|
+
VALUES (:ws, :project, :board, :thread, :id, :role, :user, :agent, :turn, :body, :bytes, :at)`,
|
|
169
|
+
{
|
|
170
|
+
project: projectId,
|
|
171
|
+
board: boardId,
|
|
172
|
+
thread: threadId,
|
|
173
|
+
id,
|
|
174
|
+
role,
|
|
175
|
+
user: userId,
|
|
176
|
+
agent: agentId,
|
|
177
|
+
turn: turnId,
|
|
178
|
+
body: text,
|
|
179
|
+
bytes: Buffer.byteLength(text),
|
|
180
|
+
at,
|
|
181
|
+
},
|
|
182
|
+
)
|
|
183
|
+
// The oldest messages make room.
|
|
184
|
+
const extra = t.get(`SELECT COUNT(*) AS n FROM chat_messages WHERE ${KEY}`, { project: projectId, board: boardId, thread: threadId }).n
|
|
185
|
+
if (extra > CHAT_LIMITS.messages)
|
|
186
|
+
t.run(
|
|
187
|
+
`DELETE FROM chat_messages WHERE ${KEY} AND id IN (SELECT id FROM chat_messages WHERE ${KEY} ORDER BY created_at, rowid LIMIT :n)`,
|
|
188
|
+
{ project: projectId, board: boardId, thread: threadId, n: extra - CHAT_LIMITS.messages },
|
|
189
|
+
)
|
|
190
|
+
const row = thread(projectId, boardId, threadId)
|
|
191
|
+
if (row && role === 'user' && !row.title && row.title_source !== 'user') {
|
|
192
|
+
const title = autoTitle(body.text)
|
|
193
|
+
if (title)
|
|
194
|
+
t.run(`UPDATE chat_threads SET title = :title, title_source = 'auto' WHERE ${KEY}`, {
|
|
195
|
+
project: projectId,
|
|
196
|
+
board: boardId,
|
|
197
|
+
thread: threadId,
|
|
198
|
+
title,
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
t.run(`UPDATE chat_threads SET updated_at = :at WHERE ${KEY}`, { project: projectId, board: boardId, thread: threadId, at })
|
|
202
|
+
return { ...body, id, role, createdAt: at }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Every thread of the board this person may read, newest first, as the
|
|
206
|
+
// chat history lists them (thread-history.js threadSummary). `query`
|
|
207
|
+
// searches titles and messages.
|
|
208
|
+
function list(projectId, boardId, { userId, query = '' }) {
|
|
209
|
+
const needle = searchNeedle(query)
|
|
210
|
+
const key = { project: projectId, board: boardId }
|
|
211
|
+
// Titles, counts and last activity only: no message is loaded for the list.
|
|
212
|
+
const all = t
|
|
213
|
+
.all(
|
|
214
|
+
`SELECT c.thread_id, c.agent_id, c.owner_user_id, c.title, c.archived, c.created_at,
|
|
215
|
+
(SELECT COUNT(*) FROM chat_messages m WHERE m.workspace_id = c.workspace_id AND m.project_id = c.project_id
|
|
216
|
+
AND m.board_id = c.board_id AND m.thread_id = c.thread_id) AS n,
|
|
217
|
+
(SELECT MAX(m.created_at) FROM chat_messages m WHERE m.workspace_id = c.workspace_id AND m.project_id = c.project_id
|
|
218
|
+
AND m.board_id = c.board_id AND m.thread_id = c.thread_id) AS last_at
|
|
219
|
+
FROM chat_threads c WHERE c.workspace_id = :ws AND c.project_id = :project AND c.board_id = :board ORDER BY c.updated_at DESC`,
|
|
220
|
+
key,
|
|
221
|
+
)
|
|
222
|
+
.filter((row) => row.n > 0 && visible(row, row.thread_id, userId))
|
|
223
|
+
const entry = (row, match = null) => {
|
|
224
|
+
const kind = threadKind(row.thread_id)
|
|
225
|
+
return {
|
|
226
|
+
id: row.thread_id,
|
|
227
|
+
kind,
|
|
228
|
+
...(kind === 'comment' ? { commentId: Number(row.thread_id.slice(8)) } : {}),
|
|
229
|
+
title: row.title || null,
|
|
230
|
+
count: row.n,
|
|
231
|
+
createdAt: row.created_at,
|
|
232
|
+
updatedAt: row.last_at,
|
|
233
|
+
archived: row.archived === 1,
|
|
234
|
+
shared: kind !== 'chat',
|
|
235
|
+
...(match ? { match } : {}),
|
|
236
|
+
agentId: row.agent_id || null,
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (!needle) return all.map((row) => entry(row)).sort(newestFirst)
|
|
240
|
+
// A search: titles first; then only the threads whose stored text holds
|
|
241
|
+
// the words are read for a snippet (a bounded number of them).
|
|
242
|
+
const like = `%${needle.replace(/[\\%_]/g, (char) => `\\${char}`)}%`
|
|
243
|
+
const holding = new Set(
|
|
244
|
+
t
|
|
245
|
+
.all(
|
|
246
|
+
"SELECT DISTINCT thread_id FROM chat_messages WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND lower(body) LIKE :like ESCAPE '\\'",
|
|
247
|
+
{ ...key, like },
|
|
248
|
+
)
|
|
249
|
+
.map((row) => row.thread_id),
|
|
250
|
+
)
|
|
251
|
+
const out = []
|
|
252
|
+
let read = 0
|
|
253
|
+
for (const row of all) {
|
|
254
|
+
if (
|
|
255
|
+
String(row.title || '')
|
|
256
|
+
.toLowerCase()
|
|
257
|
+
.includes(needle)
|
|
258
|
+
) {
|
|
259
|
+
out.push(entry(row))
|
|
260
|
+
continue
|
|
261
|
+
}
|
|
262
|
+
if (!holding.has(row.thread_id) || read >= CHAT_LIMITS.searchRead) continue
|
|
263
|
+
read += 1
|
|
264
|
+
const summary = threadSummary(
|
|
265
|
+
{ id: row.thread_id, messages: messages(projectId, boardId, row.thread_id), title: row.title || undefined, branches: [] },
|
|
266
|
+
needle,
|
|
267
|
+
)
|
|
268
|
+
if (summary?.match) out.push(entry(row, summary.match))
|
|
269
|
+
}
|
|
270
|
+
return out.sort(newestFirst)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ---- Turns ----
|
|
274
|
+
|
|
275
|
+
const turn = (turnId) => t.get('SELECT * FROM chat_turns WHERE workspace_id = :ws AND id = :id', { id: String(turnId || '') }) || null
|
|
276
|
+
// `kind`: "chat", "comment" (a comment sent to the agent, which it answers)
|
|
277
|
+
// or "fix" (Fix with agent: the server took the comment in the agent's
|
|
278
|
+
// name, `claimId`, and the agent may ask to edit).
|
|
279
|
+
function createTurn({
|
|
280
|
+
projectId,
|
|
281
|
+
boardId,
|
|
282
|
+
threadId,
|
|
283
|
+
agentId,
|
|
284
|
+
kind = 'chat',
|
|
285
|
+
userId = null,
|
|
286
|
+
userMessageId = null,
|
|
287
|
+
commentNumber = null,
|
|
288
|
+
claimId = null,
|
|
289
|
+
}) {
|
|
290
|
+
const id = newId('turn')
|
|
291
|
+
const runId = newId('run')
|
|
292
|
+
const at = iso()
|
|
293
|
+
t.run(
|
|
294
|
+
`INSERT INTO chat_turns (workspace_id, project_id, id, board_id, thread_id, agent_id, kind, state, started_by, user_message_id, run_id,
|
|
295
|
+
comment_number, created_at)
|
|
296
|
+
VALUES (:ws, :project, :id, :board, :thread, :agent, :kind, 'queued', :user, :message, :run, :number, :at)`,
|
|
297
|
+
{
|
|
298
|
+
project: projectId,
|
|
299
|
+
id,
|
|
300
|
+
board: boardId,
|
|
301
|
+
thread: threadId,
|
|
302
|
+
agent: agentId,
|
|
303
|
+
kind,
|
|
304
|
+
user: userId,
|
|
305
|
+
message: userMessageId,
|
|
306
|
+
run: runId,
|
|
307
|
+
number: commentNumber,
|
|
308
|
+
at,
|
|
309
|
+
},
|
|
310
|
+
)
|
|
311
|
+
// The usage record: who asked which agent, where and when. No text.
|
|
312
|
+
t.run(
|
|
313
|
+
`INSERT INTO agent_runs (workspace_id, project_id, id, kind, agent_id, claim_id, board_id, comment_number, thread_id, asked_by, started_at)
|
|
314
|
+
VALUES (:ws, :project, :id, :kind, :agent, :claim, :board, :number, :thread, :asked, :at)`,
|
|
315
|
+
{
|
|
316
|
+
project: projectId,
|
|
317
|
+
id: runId,
|
|
318
|
+
claim: claimId,
|
|
319
|
+
kind: kind === 'fix' ? 'fix' : kind === 'push' ? 'push' : kind === 'comment' ? 'review' : 'chat',
|
|
320
|
+
agent: agentId,
|
|
321
|
+
board: boardId,
|
|
322
|
+
number: commentNumber,
|
|
323
|
+
thread: threadId,
|
|
324
|
+
asked: userId,
|
|
325
|
+
at,
|
|
326
|
+
},
|
|
327
|
+
)
|
|
328
|
+
return turn(id)
|
|
329
|
+
}
|
|
330
|
+
const openTurn = (projectId, boardId, threadId) =>
|
|
331
|
+
t.get(`SELECT * FROM chat_turns WHERE ${KEY} AND state IN ('queued', 'running') ORDER BY created_at DESC LIMIT 1`, {
|
|
332
|
+
project: projectId,
|
|
333
|
+
board: boardId,
|
|
334
|
+
thread: threadId,
|
|
335
|
+
}) || null
|
|
336
|
+
const openTurnsOf = (agentId) =>
|
|
337
|
+
t.all(
|
|
338
|
+
"SELECT * FROM chat_turns WHERE workspace_id = :ws AND agent_id = :agent AND state IN ('queued', 'running') ORDER BY created_at",
|
|
339
|
+
{
|
|
340
|
+
agent: agentId,
|
|
341
|
+
},
|
|
342
|
+
)
|
|
343
|
+
// Today's turns of a project, from the usage records: they stay when a
|
|
344
|
+
// thread is deleted, so deleting one never gives turns back.
|
|
345
|
+
const turnsToday = (projectId) =>
|
|
346
|
+
t.get(
|
|
347
|
+
"SELECT COUNT(*) AS n FROM agent_runs WHERE workspace_id = :ws AND project_id = :project AND kind IN ('chat', 'review') AND started_at >= :start",
|
|
348
|
+
{ project: projectId, start: `${iso().slice(0, 10)}T00:00:00.000Z` },
|
|
349
|
+
).n
|
|
350
|
+
// Every turn the database still calls open (after a restart: none is).
|
|
351
|
+
const openTurns = () => t.all("SELECT * FROM chat_turns WHERE workspace_id = :ws AND state IN ('queued', 'running') ORDER BY created_at")
|
|
352
|
+
function setRunning(turnId) {
|
|
353
|
+
t.run("UPDATE chat_turns SET state = 'running' WHERE workspace_id = :ws AND id = :id AND state = 'queued'", { id: turnId })
|
|
354
|
+
}
|
|
355
|
+
function ack(turnId, seq) {
|
|
356
|
+
t.run('UPDATE chat_turns SET acked_seq = :seq WHERE workspace_id = :ws AND id = :id', { id: turnId, seq })
|
|
357
|
+
}
|
|
358
|
+
// Ends a turn once (a second end changes nothing): its state, and its
|
|
359
|
+
// usage record with what the agent reported. Returns whether it ended now.
|
|
360
|
+
function endTurn(row, state, usage = {}) {
|
|
361
|
+
const at = iso()
|
|
362
|
+
const result = t.run(
|
|
363
|
+
"UPDATE chat_turns SET state = :state, ended_at = :at WHERE workspace_id = :ws AND id = :id AND state IN ('queued', 'running')",
|
|
364
|
+
{ id: row.id, state, at },
|
|
365
|
+
)
|
|
366
|
+
if (!Number(result.changes)) return false
|
|
367
|
+
t.run(
|
|
368
|
+
`UPDATE agent_runs SET ended_at = :at, outcome = :outcome, model = :model, effort = :effort, input_tokens = :input,
|
|
369
|
+
output_tokens = :output, cached_tokens = :cached, cost_micros = :cost, steps = :steps,
|
|
370
|
+
approvals_asked = :asked, approvals_allowed = :allowed, approvals_denied = :denied
|
|
371
|
+
WHERE workspace_id = :ws AND id = :run AND ended_at IS NULL`,
|
|
372
|
+
{
|
|
373
|
+
run: row.run_id,
|
|
374
|
+
at,
|
|
375
|
+
outcome: usage.outcome || state,
|
|
376
|
+
model: usage.model ?? null,
|
|
377
|
+
effort: usage.effort ?? null,
|
|
378
|
+
input: usage.inputTokens ?? null,
|
|
379
|
+
output: usage.outputTokens ?? null,
|
|
380
|
+
cached: usage.cachedTokens ?? null,
|
|
381
|
+
cost: usage.costMicros ?? null,
|
|
382
|
+
steps: usage.steps ?? null,
|
|
383
|
+
asked: usage.approvals?.asked ?? null,
|
|
384
|
+
allowed: usage.approvals?.allowed ?? null,
|
|
385
|
+
denied: usage.approvals?.denied ?? null,
|
|
386
|
+
},
|
|
387
|
+
)
|
|
388
|
+
return true
|
|
389
|
+
}
|
|
390
|
+
// A turn that ended without an answer from the machine (it went away, the
|
|
391
|
+
// server restarted, its agent was revoked): the thread says so.
|
|
392
|
+
function interrupt(row, { message = INTERRUPTED_MESSAGE, agentName = null, state = 'interrupted', partial = null } = {}) {
|
|
393
|
+
if (!endTurn(row, state, { outcome: state })) return null
|
|
394
|
+
return append(row.project_id, row.board_id, row.thread_id, {
|
|
395
|
+
role: 'assistant',
|
|
396
|
+
agentId: row.agent_id,
|
|
397
|
+
turnId: row.id,
|
|
398
|
+
body: {
|
|
399
|
+
text: partial?.text || '',
|
|
400
|
+
cards: partial?.cards || [],
|
|
401
|
+
status: state === 'stopped' ? 'stopped' : 'error',
|
|
402
|
+
...(state === 'stopped' ? {} : { error: { code: 'interrupted', message } }),
|
|
403
|
+
...(agentName ? { agent: { id: row.agent_id, name: agentName } } : {}),
|
|
404
|
+
},
|
|
405
|
+
})
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// The shared threads of a board (the board's and its comments') with
|
|
409
|
+
// their messages, for a workspace export. A member's own chats are theirs:
|
|
410
|
+
// only how many there are.
|
|
411
|
+
function shared(projectId, boardId) {
|
|
412
|
+
const all = t.all(
|
|
413
|
+
'SELECT thread_id, agent_id, owner_user_id, title, created_at, updated_at FROM chat_threads WHERE workspace_id = :ws AND project_id = :project AND board_id = :board ORDER BY created_at, thread_id',
|
|
414
|
+
{ project: projectId, board: boardId },
|
|
415
|
+
)
|
|
416
|
+
const threads = all
|
|
417
|
+
.filter((row) => !CHAT_RE.test(row.thread_id))
|
|
418
|
+
.map((row) => ({
|
|
419
|
+
id: row.thread_id,
|
|
420
|
+
title: row.title || null,
|
|
421
|
+
agentId: row.agent_id || null,
|
|
422
|
+
createdAt: row.created_at,
|
|
423
|
+
updatedAt: row.updated_at,
|
|
424
|
+
messages: messages(projectId, boardId, row.thread_id),
|
|
425
|
+
}))
|
|
426
|
+
.filter((thread) => thread.messages.length)
|
|
427
|
+
return { threads, privateChats: all.filter((row) => CHAT_RE.test(row.thread_id)).length }
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Bytes of every stored chat message of the workspace, for the storage quota.
|
|
431
|
+
const bytes = () =>
|
|
432
|
+
t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM chat_messages WHERE workspace_id = :ws').n +
|
|
433
|
+
t.get('SELECT COALESCE(SUM(length(card)), 0) AS n FROM chat_permissions WHERE workspace_id = :ws').n
|
|
434
|
+
|
|
435
|
+
return {
|
|
436
|
+
validThread,
|
|
437
|
+
thread,
|
|
438
|
+
visible,
|
|
439
|
+
ensure,
|
|
440
|
+
setAgent,
|
|
441
|
+
change,
|
|
442
|
+
remove,
|
|
443
|
+
messages,
|
|
444
|
+
append,
|
|
445
|
+
list,
|
|
446
|
+
turn,
|
|
447
|
+
createTurn,
|
|
448
|
+
openTurn,
|
|
449
|
+
openTurnsOf,
|
|
450
|
+
openTurns,
|
|
451
|
+
turnsToday,
|
|
452
|
+
setRunning,
|
|
453
|
+
ack,
|
|
454
|
+
endTurn,
|
|
455
|
+
interrupt,
|
|
456
|
+
shared,
|
|
457
|
+
bytes,
|
|
458
|
+
kindOf: threadKind,
|
|
459
|
+
}
|
|
460
|
+
}
|
|
@@ -37,8 +37,15 @@ export async function cleanImages(cleaner, inputs) {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// Bytes of every comment image the workspace holds (attached or staged).
|
|
40
|
+
// Stored chat messages (src/hosted/chat-store.js) count with them: every
|
|
41
|
+
// caller adds this to the boards for the workspace's storage.
|
|
40
42
|
export function commentFileBytes(t) {
|
|
41
|
-
return
|
|
43
|
+
return (
|
|
44
|
+
t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM comment_files WHERE workspace_id = :ws').n +
|
|
45
|
+
t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM chat_messages WHERE workspace_id = :ws').n +
|
|
46
|
+
// The permission cards of connected agents (their diffs): src/hosted/chat-permissions.js.
|
|
47
|
+
t.get('SELECT COALESCE(SUM(length(card)), 0) AS n FROM chat_permissions WHERE workspace_id = :ws').n
|
|
48
|
+
)
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
// Everything the storage quota counts: pushed boards, open pushes and
|
package/src/hosted/comments.js
CHANGED
|
@@ -268,7 +268,7 @@ export function createCommentStore(t, projectId, boardId) {
|
|
|
268
268
|
const body = changes.body !== undefined ? cleanCommentBody(changes.body, { media: hasCommentMedia(before) }) : undefined
|
|
269
269
|
if (body !== undefined && body !== row.body) {
|
|
270
270
|
t.run(
|
|
271
|
-
`UPDATE comments SET dispatch_hash = NULL, dispatch_by = NULL, dispatch_at = NULL, run_approved = 0 WHERE ${SCOPE} AND number = :number`,
|
|
271
|
+
`UPDATE comments SET dispatch_hash = NULL, dispatch_by = NULL, dispatch_at = NULL, run_approved = 0, agent_id = NULL WHERE ${SCOPE} AND number = :number`,
|
|
272
272
|
{ ...key, number: row.number },
|
|
273
273
|
)
|
|
274
274
|
row.body = body
|
|
@@ -9,7 +9,7 @@ import { agentPolicy, cleanPolicy } from './agent-policy.js'
|
|
|
9
9
|
import { newId, randomToken, sha256 } from './crypto.js'
|
|
10
10
|
import { transaction } from './db.js'
|
|
11
11
|
import { mountDeviceApprovals } from './device-routes.js'
|
|
12
|
-
import { HttpError, fail, cleanInviteNote, cleanWorkspaceName } from './http.js'
|
|
12
|
+
import { HttpError, fail, cleanInviteNote, cleanName, cleanWorkspaceName } from './http.js'
|
|
13
13
|
import { cleanEmail } from './mail.js'
|
|
14
14
|
import {
|
|
15
15
|
NEEDS,
|
|
@@ -95,6 +95,7 @@ export function mountConsoleRoutes(consoleApi, deps) {
|
|
|
95
95
|
revokeGrantsOf,
|
|
96
96
|
ownerCount,
|
|
97
97
|
createApiToken,
|
|
98
|
+
agentsFor,
|
|
98
99
|
} = deps
|
|
99
100
|
|
|
100
101
|
consoleApi.get(
|
|
@@ -343,6 +344,23 @@ export function mountConsoleRoutes(consoleApi, deps) {
|
|
|
343
344
|
}),
|
|
344
345
|
)
|
|
345
346
|
|
|
347
|
+
// One board off the server (pushed by mistake, or retired): the same role
|
|
348
|
+
// and the same fresh passkey check as deleting the project it is in. An
|
|
349
|
+
// unknown board, a malformed id and a board of another workspace all
|
|
350
|
+
// answer the same 404. The write publishes `board.removed`
|
|
351
|
+
// (src/hosted/events.js), so open boards and token streams hear of it.
|
|
352
|
+
consoleApi.delete(
|
|
353
|
+
'/workspaces/:ws/projects/:pid/boards/:bid',
|
|
354
|
+
inWorkspace(NEEDS.projects),
|
|
355
|
+
needFresh,
|
|
356
|
+
inProject,
|
|
357
|
+
handle(async (req, res) => {
|
|
358
|
+
const gone = req.projects.removeBoard(req.project.id, req.params.bid)
|
|
359
|
+
auditOf(req, 'board_removed', `${req.project.id}/${gone.board}`, { project: req.project.name, comments: gone.comments })
|
|
360
|
+
res.json({ ok: true, board: gone.board, comments: gone.comments, bytes: gone.bytes })
|
|
361
|
+
}),
|
|
362
|
+
)
|
|
363
|
+
|
|
346
364
|
consoleApi.get(
|
|
347
365
|
'/workspaces/:ws/projects/:pid/tokens',
|
|
348
366
|
inWorkspace(NEEDS.tokens),
|
|
@@ -384,6 +402,35 @@ export function mountConsoleRoutes(consoleApi, deps) {
|
|
|
384
402
|
)
|
|
385
403
|
|
|
386
404
|
mountDeviceApprovals(consoleApi, deps)
|
|
405
|
+
// Renames a token: { name }. A watcher's comments and replies are signed
|
|
406
|
+
// with its token's name, so this is what an agent is called on the board
|
|
407
|
+
// from now on (what it already wrote keeps the name it was written with).
|
|
408
|
+
// The same people who may revoke it; the secret and scopes do not change.
|
|
409
|
+
consoleApi.patch(
|
|
410
|
+
'/workspaces/:ws/projects/:pid/tokens/:tid',
|
|
411
|
+
inWorkspace(NEEDS.tokens),
|
|
412
|
+
inProject,
|
|
413
|
+
handle(async (req, res) => {
|
|
414
|
+
// A name too long is refused, not cut: it is what people will see.
|
|
415
|
+
const name = cleanName(req.body?.name, 61)
|
|
416
|
+
if (!name || name.length > 60) throw new HttpError(400, 'invalid_name', 'Token name: 1 to 60 characters.')
|
|
417
|
+
const id = String(req.params.tid)
|
|
418
|
+
const token = req.t.get(
|
|
419
|
+
'SELECT name FROM api_tokens WHERE workspace_id = :ws AND project_id = :project AND id = :id AND revoked_at IS NULL',
|
|
420
|
+
{ project: req.project.id, id },
|
|
421
|
+
)
|
|
422
|
+
if (!token) throw new HttpError(404, 'unknown_token', 'No such token.')
|
|
423
|
+
if (token.name !== name) {
|
|
424
|
+
req.t.run('UPDATE api_tokens SET name = :name WHERE workspace_id = :ws AND project_id = :project AND id = :id', {
|
|
425
|
+
name,
|
|
426
|
+
project: req.project.id,
|
|
427
|
+
id,
|
|
428
|
+
})
|
|
429
|
+
auditOf(req, 'token_renamed', id, { from: token.name, to: name })
|
|
430
|
+
}
|
|
431
|
+
res.json({ token: listTokens(req.t, { projectId: req.project.id, now: iso() }).find((item) => item.id === id) })
|
|
432
|
+
}),
|
|
433
|
+
)
|
|
387
434
|
consoleApi.delete(
|
|
388
435
|
'/workspaces/:ws/projects/:pid/tokens/:tid',
|
|
389
436
|
inWorkspace(NEEDS.tokens),
|
|
@@ -395,6 +442,16 @@ export function mountConsoleRoutes(consoleApi, deps) {
|
|
|
395
442
|
)
|
|
396
443
|
if (!result.changes) throw new HttpError(404, 'unknown_token', 'No such token.')
|
|
397
444
|
auditOf(req, 'token_revoked', String(req.params.tid))
|
|
445
|
+
// A token that was an agent: what waited for it goes to any agent, and
|
|
446
|
+
// the project loses it as its default (src/hosted/agents.js).
|
|
447
|
+
const agents = agentsFor(req.t)
|
|
448
|
+
const agent = agents.forToken(req.project.id, String(req.params.tid))
|
|
449
|
+
if (agent)
|
|
450
|
+
agents.afterRevoke(
|
|
451
|
+
{ userId: req.user.id, role: req.role, audit: (action, target, detail) => auditOf(req, action, target, detail) },
|
|
452
|
+
req.project.id,
|
|
453
|
+
agent.id,
|
|
454
|
+
)
|
|
398
455
|
res.json({ ok: true })
|
|
399
456
|
}),
|
|
400
457
|
)
|