shapewall 0.3.3 → 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 +249 -0
- package/README.md +49 -3
- package/dist/agent/package-lock.json +2 -2
- package/dist/client/assets/BoardChat-DdwBcb2N.js +27 -0
- package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-DpUcnU6S.js} +1 -1
- 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-NVFDtSZB.js → Management-CSvZ-SHO.js} +1 -1
- package/dist/client/assets/QuickConnect-foLh2QUm.js +1 -0
- package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-DPWDftoL.js} +2 -2
- package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-CtiXpTRD.js} +1 -1
- package/dist/client/assets/{circle-x-B6KfScOE.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-vkgvCCfR.js → refresh-cw-r_tZiFfB.js} +1 -1
- 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 +3 -3
- package/dist/client/sw.js +2 -2
- package/dist/console/assets/{LocalAccountsApp-VOCZg8dg.js → LocalAccountsApp-DIdy1KnN.js} +1 -1
- package/dist/console/assets/{LocalConsole-CMwAW-mF.js → LocalConsole-DfcQztF-.js} +1 -1
- package/dist/console/assets/{app-update-dialog-Cw6UDqNP.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 +14 -6
- package/docs/hosted.md +804 -11
- package/docs/where-to-run.md +37 -2
- package/package.json +1 -1
- 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/context.js +41 -0
- package/src/agent/exec-paths.js +105 -0
- package/src/agent/routes.js +2 -2
- package/src/agent/run.js +11 -1
- package/src/agent/starter/cli.js +6 -1
- package/src/agent/turn-changes.js +70 -8
- 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 +63 -22
- package/src/hosted/agent-routes.js +135 -0
- package/src/hosted/agents.js +1199 -0
- package/src/hosted/app.js +89 -5
- package/src/hosted/board-routes.js +127 -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 +11 -0
- 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 +3 -3
- 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 +17 -0
- package/src/hosted/projects.js +29 -2
- package/src/hosted/ratelimit.js +8 -0
- package/src/hosted/tenancy.js +18 -0
- package/src/hosted/token-api.js +118 -19
- package/src/hosted/token-events.js +5 -3
- package/src/hosted/tokens.js +21 -5
- package/src/hosted/turn-hub.js +152 -0
- 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/dist/client/assets/BoardChat-DxehB761.js +0 -27
- package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
- package/dist/client/assets/History-C9N32kCv.js +0 -1
- package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
- package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
- package/dist/client/assets/index-CsqLdqGx.css +0 -2
- package/dist/client/assets/index-DnZarJ3U.js +0 -37
- package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
- package/dist/client/assets/ui-DI-FGWWK.js +0 -41
- package/dist/console/assets/console-DgfWmUEx.js +0 -55
- package/dist/console/assets/console-Lis0vvdL.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
|
|
@@ -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(
|
|
@@ -441,6 +442,16 @@ export function mountConsoleRoutes(consoleApi, deps) {
|
|
|
441
442
|
)
|
|
442
443
|
if (!result.changes) throw new HttpError(404, 'unknown_token', 'No such token.')
|
|
443
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
|
+
)
|
|
444
455
|
res.json({ ok: true })
|
|
445
456
|
}),
|
|
446
457
|
)
|
package/src/hosted/db.js
CHANGED
|
@@ -462,6 +462,171 @@ const MIGRATIONS = [
|
|
|
462
462
|
CREATE INDEX comment_files_comment ON comment_files(workspace_id, project_id, board_id, comment_number);
|
|
463
463
|
CREATE INDEX comment_files_import ON comment_files(workspace_id, project_id, import_session, import_ref);
|
|
464
464
|
`,
|
|
465
|
+
// 12: agents (0.4.0). A project API token that may take work (the claim
|
|
466
|
+
// scope) is an agent: this row gives it a face (kind, icon, tint, machine),
|
|
467
|
+
// an audience (who may send it work), an approval rule and who may change
|
|
468
|
+
// or answer it. The name stays the token's. A project has a default
|
|
469
|
+
// agent; a comment can be addressed to one agent (NULL: any agent); a
|
|
470
|
+
// claim remembers which agent took it. agent_runs is the usage record of
|
|
471
|
+
// each comment an agent took (who asked, which agent, when, how it ended;
|
|
472
|
+
// never comment or answer text), shaped for the usage dashboard, whose
|
|
473
|
+
// token and cost columns stay NULL until an agent reports them.
|
|
474
|
+
//
|
|
475
|
+
// Every token that already has the claim scope becomes an agent of kind
|
|
476
|
+
// "watcher" with the audience it had in effect before: everyone the
|
|
477
|
+
// project's agent policy trusts (audience_inherited marks that it was
|
|
478
|
+
// never chosen). Agents made from now on start with "owner".
|
|
479
|
+
`
|
|
480
|
+
CREATE TABLE agents (
|
|
481
|
+
workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
|
|
482
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
483
|
+
id TEXT PRIMARY KEY,
|
|
484
|
+
token_id TEXT NOT NULL UNIQUE REFERENCES api_tokens(id) ON DELETE CASCADE,
|
|
485
|
+
kind TEXT NOT NULL DEFAULT 'watcher',
|
|
486
|
+
icon TEXT,
|
|
487
|
+
tint TEXT,
|
|
488
|
+
machine TEXT,
|
|
489
|
+
capabilities TEXT NOT NULL DEFAULT '{}',
|
|
490
|
+
audience TEXT NOT NULL DEFAULT 'owner' CHECK (audience IN ('owner', 'members', 'trusted')),
|
|
491
|
+
audience_users TEXT NOT NULL DEFAULT '[]',
|
|
492
|
+
audience_inherited INTEGER NOT NULL DEFAULT 0,
|
|
493
|
+
approval_rule TEXT NOT NULL DEFAULT 'ask' CHECK (approval_rule IN ('ask', 'auto')),
|
|
494
|
+
approval_change_role TEXT NOT NULL DEFAULT 'admin' CHECK (approval_change_role IN ('admin', 'owner')),
|
|
495
|
+
approval_answer_role TEXT NOT NULL DEFAULT 'admin' CHECK (approval_answer_role IN ('admin', 'owner')),
|
|
496
|
+
created_by TEXT,
|
|
497
|
+
created_at TEXT NOT NULL,
|
|
498
|
+
approved_by TEXT,
|
|
499
|
+
approved_at TEXT,
|
|
500
|
+
last_seen_at TEXT
|
|
501
|
+
);
|
|
502
|
+
CREATE INDEX agents_project ON agents(workspace_id, project_id);
|
|
503
|
+
ALTER TABLE projects ADD COLUMN default_agent_id TEXT;
|
|
504
|
+
ALTER TABLE comments ADD COLUMN agent_id TEXT;
|
|
505
|
+
ALTER TABLE agent_claims ADD COLUMN agent_id TEXT;
|
|
506
|
+
INSERT INTO agents (workspace_id, project_id, id, token_id, kind, audience, audience_inherited, created_by, created_at,
|
|
507
|
+
approved_by, approved_at, last_seen_at)
|
|
508
|
+
SELECT workspace_id, project_id, 'ag_' || lower(hex(randomblob(12))), id, 'watcher', 'trusted', 1, created_by, created_at,
|
|
509
|
+
created_by, created_at, last_used_at
|
|
510
|
+
FROM api_tokens WHERE scopes LIKE '%"claim"%';
|
|
511
|
+
UPDATE agent_claims SET agent_id = (SELECT a.id FROM agents a WHERE a.token_id = agent_claims.token_id);
|
|
512
|
+
CREATE TABLE agent_runs (
|
|
513
|
+
workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
|
|
514
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
515
|
+
id TEXT PRIMARY KEY,
|
|
516
|
+
kind TEXT NOT NULL DEFAULT 'fix',
|
|
517
|
+
agent_id TEXT,
|
|
518
|
+
claim_id TEXT,
|
|
519
|
+
board_id TEXT,
|
|
520
|
+
comment_number INTEGER,
|
|
521
|
+
thread_id TEXT,
|
|
522
|
+
asked_by TEXT,
|
|
523
|
+
started_at TEXT NOT NULL,
|
|
524
|
+
ended_at TEXT,
|
|
525
|
+
outcome TEXT,
|
|
526
|
+
model TEXT,
|
|
527
|
+
effort TEXT,
|
|
528
|
+
input_tokens INTEGER,
|
|
529
|
+
output_tokens INTEGER,
|
|
530
|
+
cached_tokens INTEGER,
|
|
531
|
+
cost_micros INTEGER,
|
|
532
|
+
steps INTEGER
|
|
533
|
+
);
|
|
534
|
+
CREATE INDEX agent_runs_time ON agent_runs(workspace_id, started_at);
|
|
535
|
+
CREATE INDEX agent_runs_project ON agent_runs(workspace_id, project_id, started_at);
|
|
536
|
+
CREATE INDEX agent_runs_agent ON agent_runs(workspace_id, agent_id, started_at);
|
|
537
|
+
CREATE INDEX agent_runs_person ON agent_runs(workspace_id, asked_by, started_at);
|
|
538
|
+
CREATE INDEX agent_runs_claim ON agent_runs(workspace_id, claim_id);
|
|
539
|
+
`,
|
|
540
|
+
// 13: connected agents and chat (0.5.0). `shapewall agent connect` holds a
|
|
541
|
+
// link to the server and members chat with the agent on the board. A
|
|
542
|
+
// thread belongs to a board ("board" and "comment-<n>" are shared, a
|
|
543
|
+
// "chat-..." is its starter's own) and names the agent it talks to. Its
|
|
544
|
+
// messages are stored here (the person's text, the agent's answer with its
|
|
545
|
+
// cards in their final state); a turn is one question on its way through
|
|
546
|
+
// an agent, whose live events stay in memory. A device login remembers
|
|
547
|
+
// what the machine said it runs (meta), for the approval page.
|
|
548
|
+
`
|
|
549
|
+
ALTER TABLE device_logins ADD COLUMN meta TEXT;
|
|
550
|
+
CREATE TABLE chat_threads (
|
|
551
|
+
workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
|
|
552
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
553
|
+
board_id TEXT NOT NULL,
|
|
554
|
+
thread_id TEXT NOT NULL,
|
|
555
|
+
agent_id TEXT,
|
|
556
|
+
owner_user_id TEXT,
|
|
557
|
+
title TEXT,
|
|
558
|
+
title_source TEXT,
|
|
559
|
+
archived INTEGER NOT NULL DEFAULT 0,
|
|
560
|
+
created_by TEXT,
|
|
561
|
+
created_at TEXT NOT NULL,
|
|
562
|
+
updated_at TEXT NOT NULL,
|
|
563
|
+
PRIMARY KEY (workspace_id, project_id, board_id, thread_id)
|
|
564
|
+
);
|
|
565
|
+
CREATE TABLE chat_messages (
|
|
566
|
+
workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
|
|
567
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
568
|
+
board_id TEXT NOT NULL,
|
|
569
|
+
thread_id TEXT NOT NULL,
|
|
570
|
+
id TEXT PRIMARY KEY,
|
|
571
|
+
role TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
|
|
572
|
+
author_user_id TEXT,
|
|
573
|
+
agent_id TEXT,
|
|
574
|
+
turn_id TEXT,
|
|
575
|
+
body TEXT NOT NULL,
|
|
576
|
+
bytes INTEGER NOT NULL DEFAULT 0,
|
|
577
|
+
created_at TEXT NOT NULL
|
|
578
|
+
);
|
|
579
|
+
CREATE INDEX chat_messages_thread ON chat_messages(workspace_id, project_id, board_id, thread_id, created_at);
|
|
580
|
+
CREATE TABLE chat_turns (
|
|
581
|
+
workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
|
|
582
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
583
|
+
id TEXT PRIMARY KEY,
|
|
584
|
+
board_id TEXT NOT NULL,
|
|
585
|
+
thread_id TEXT NOT NULL,
|
|
586
|
+
agent_id TEXT NOT NULL,
|
|
587
|
+
kind TEXT NOT NULL DEFAULT 'chat',
|
|
588
|
+
state TEXT NOT NULL DEFAULT 'queued',
|
|
589
|
+
acked_seq INTEGER NOT NULL DEFAULT 0,
|
|
590
|
+
started_by TEXT,
|
|
591
|
+
user_message_id TEXT,
|
|
592
|
+
run_id TEXT,
|
|
593
|
+
comment_number INTEGER,
|
|
594
|
+
created_at TEXT NOT NULL,
|
|
595
|
+
ended_at TEXT
|
|
596
|
+
);
|
|
597
|
+
CREATE INDEX chat_turns_thread ON chat_turns(workspace_id, project_id, board_id, thread_id, created_at);
|
|
598
|
+
CREATE INDEX chat_turns_agent ON chat_turns(workspace_id, agent_id, state);
|
|
599
|
+
`,
|
|
600
|
+
// 14: approvals and edits from a hosted board (0.6.0). chat_permissions is
|
|
601
|
+
// one row per permission card a connected agent asked during a fix: what
|
|
602
|
+
// it asked (the card, as hashed), its state, who answered and when. The
|
|
603
|
+
// usage record of a run gains how many requests it asked and how they
|
|
604
|
+
// ended. Additive; an older build ignores all of it.
|
|
605
|
+
`
|
|
606
|
+
CREATE TABLE chat_permissions (
|
|
607
|
+
workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
|
|
608
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
609
|
+
id TEXT NOT NULL,
|
|
610
|
+
turn_id TEXT NOT NULL,
|
|
611
|
+
agent_id TEXT NOT NULL,
|
|
612
|
+
board_id TEXT NOT NULL,
|
|
613
|
+
thread_id TEXT NOT NULL,
|
|
614
|
+
kind TEXT NOT NULL,
|
|
615
|
+
card TEXT NOT NULL,
|
|
616
|
+
card_hash TEXT NOT NULL,
|
|
617
|
+
state TEXT NOT NULL DEFAULT 'pending',
|
|
618
|
+
created_at TEXT NOT NULL,
|
|
619
|
+
expires_at TEXT NOT NULL,
|
|
620
|
+
answered_by TEXT,
|
|
621
|
+
answered_at TEXT,
|
|
622
|
+
PRIMARY KEY (workspace_id, turn_id, id)
|
|
623
|
+
);
|
|
624
|
+
CREATE INDEX chat_permissions_board ON chat_permissions(workspace_id, project_id, board_id, state);
|
|
625
|
+
ALTER TABLE agent_runs ADD COLUMN approvals_asked INTEGER;
|
|
626
|
+
ALTER TABLE agent_runs ADD COLUMN approvals_allowed INTEGER;
|
|
627
|
+
ALTER TABLE agent_runs ADD COLUMN approvals_denied INTEGER;
|
|
628
|
+
ALTER TABLE agents ADD COLUMN approval_rule_by TEXT;
|
|
629
|
+
`,
|
|
465
630
|
]
|
|
466
631
|
|
|
467
632
|
export async function openDatabase(file) {
|