@otea/cli 0.0.0-stage → 0.1.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.
Files changed (243) hide show
  1. package/LICENSE +661 -0
  2. package/README.es.md +151 -0
  3. package/README.md +252 -2
  4. package/THIRD_PARTY_NOTICES.md +114 -0
  5. package/bin/otea.mjs +28 -0
  6. package/dist/app/agent-detail.js +26 -0
  7. package/dist/app/agent-draft.js +429 -0
  8. package/dist/app/agent-prompts.js +52 -0
  9. package/dist/app/agents.js +77 -0
  10. package/dist/app/asking.js +43 -0
  11. package/dist/app/board-archive.js +243 -0
  12. package/dist/app/board-migration.js +272 -0
  13. package/dist/app/boards.js +870 -0
  14. package/dist/app/branch-owner.js +48 -0
  15. package/dist/app/brief.js +53 -0
  16. package/dist/app/choices.js +61 -0
  17. package/dist/app/collections.js +101 -0
  18. package/dist/app/context.js +1 -0
  19. package/dist/app/crop.js +56 -0
  20. package/dist/app/devices.js +62 -0
  21. package/dist/app/draft-request.js +82 -0
  22. package/dist/app/events.js +12 -0
  23. package/dist/app/evidence.js +287 -0
  24. package/dist/app/gates.js +610 -0
  25. package/dist/app/guide.js +37 -0
  26. package/dist/app/home.js +145 -0
  27. package/dist/app/init-boards.js +36 -0
  28. package/dist/app/intake.js +25 -0
  29. package/dist/app/item-search.js +141 -0
  30. package/dist/app/items.js +318 -0
  31. package/dist/app/keep-waiting.js +44 -0
  32. package/dist/app/land-order.js +107 -0
  33. package/dist/app/language.js +52 -0
  34. package/dist/app/launcher.js +56 -0
  35. package/dist/app/lease-take.js +54 -0
  36. package/dist/app/leases.js +86 -0
  37. package/dist/app/machine.js +39 -0
  38. package/dist/app/merged.js +89 -0
  39. package/dist/app/migrate.js +171 -0
  40. package/dist/app/next.js +1637 -0
  41. package/dist/app/notes.js +319 -0
  42. package/dist/app/notify-targets.js +113 -0
  43. package/dist/app/opened.js +25 -0
  44. package/dist/app/owner-only.js +34 -0
  45. package/dist/app/performance.js +28 -0
  46. package/dist/app/phone.js +97 -0
  47. package/dist/app/presence.js +596 -0
  48. package/dist/app/preset-views.js +25 -0
  49. package/dist/app/presets.js +129 -0
  50. package/dist/app/priority.js +156 -0
  51. package/dist/app/profile.js +86 -0
  52. package/dist/app/project.js +247 -0
  53. package/dist/app/proofs.js +179 -0
  54. package/dist/app/questions.js +215 -0
  55. package/dist/app/reader-filter.js +58 -0
  56. package/dist/app/relations.js +164 -0
  57. package/dist/app/relay.js +98 -0
  58. package/dist/app/repos.js +176 -0
  59. package/dist/app/restart.js +92 -0
  60. package/dist/app/set-aside.js +64 -0
  61. package/dist/app/system-locale.js +6 -0
  62. package/dist/app/tailscale-cli.js +77 -0
  63. package/dist/app/tailscale-testing.js +41 -0
  64. package/dist/app/tailscale.js +177 -0
  65. package/dist/app/tips.js +30 -0
  66. package/dist/app/topics-everywhere.js +53 -0
  67. package/dist/app/topics.js +453 -0
  68. package/dist/app/undo.js +52 -0
  69. package/dist/app/validate.js +131 -0
  70. package/dist/app/watch.js +48 -0
  71. package/dist/cli/command.js +44 -0
  72. package/dist/cli/commands.js +3303 -0
  73. package/dist/cli/exit-codes.js +70 -0
  74. package/dist/cli/forge.js +176 -0
  75. package/dist/cli/format.js +340 -0
  76. package/dist/cli/gh.js +17 -0
  77. package/dist/cli/gitrepo.js +489 -0
  78. package/dist/cli/help.js +101 -0
  79. package/dist/cli/hooks.js +232 -0
  80. package/dist/cli/main.js +37 -0
  81. package/dist/cli/merged-outside.js +58 -0
  82. package/dist/cli/names.js +154 -0
  83. package/dist/cli/opener.js +21 -0
  84. package/dist/cli/owner.js +154 -0
  85. package/dist/cli/phone.js +221 -0
  86. package/dist/cli/ports.js +24 -0
  87. package/dist/cli/probe.js +18 -0
  88. package/dist/cli/run.js +246 -0
  89. package/dist/cli/serve-file.js +21 -0
  90. package/dist/cli/stats.js +55 -0
  91. package/dist/cli/version.js +3 -0
  92. package/dist/core/agent-guide.js +31 -0
  93. package/dist/core/agent-prompts.js +139 -0
  94. package/dist/core/authority.js +94 -0
  95. package/dist/core/board/analysis.js +86 -0
  96. package/dist/core/board/choices.js +100 -0
  97. package/dist/core/board/compile.js +362 -0
  98. package/dist/core/board/engine.js +523 -0
  99. package/dist/core/board/hints.js +46 -0
  100. package/dist/core/board/history.js +146 -0
  101. package/dist/core/board/ids.js +87 -0
  102. package/dist/core/board/joins.js +30 -0
  103. package/dist/core/board/mentions.js +68 -0
  104. package/dist/core/board/merged.js +18 -0
  105. package/dist/core/board/model.js +135 -0
  106. package/dist/core/board/next.js +1116 -0
  107. package/dist/core/board/notes.js +298 -0
  108. package/dist/core/board/performance.js +218 -0
  109. package/dist/core/board/plan.js +299 -0
  110. package/dist/core/board/problems.js +232 -0
  111. package/dist/core/board/proofs.js +84 -0
  112. package/dist/core/board/questions.js +114 -0
  113. package/dist/core/board/reading.js +220 -0
  114. package/dist/core/board/readings.js +534 -0
  115. package/dist/core/board/schema.js +335 -0
  116. package/dist/core/board/shape-words.js +179 -0
  117. package/dist/core/board/template-words.js +23 -0
  118. package/dist/core/board/templates-catalogue.js +965 -0
  119. package/dist/core/board/templates-coverage.js +261 -0
  120. package/dist/core/board/templates-flows.js +779 -0
  121. package/dist/core/board/templates.js +586 -0
  122. package/dist/core/brief.js +93 -0
  123. package/dist/core/diagram.js +80 -0
  124. package/dist/core/errors.js +12 -0
  125. package/dist/core/evidence.js +33 -0
  126. package/dist/core/fields.js +129 -0
  127. package/dist/core/hierarchy.js +30 -0
  128. package/dist/core/icons.js +181 -0
  129. package/dist/core/image-crop.js +166 -0
  130. package/dist/core/item.js +49 -0
  131. package/dist/core/lease.js +31 -0
  132. package/dist/core/modules.js +318 -0
  133. package/dist/core/notify/planner.js +117 -0
  134. package/dist/core/notify/signals.js +66 -0
  135. package/dist/core/notify/text.js +60 -0
  136. package/dist/core/path-key.js +22 -0
  137. package/dist/core/placement.js +161 -0
  138. package/dist/core/preset-labels.js +66 -0
  139. package/dist/core/preset-notes.js +109 -0
  140. package/dist/core/preset.js +541 -0
  141. package/dist/core/profile.js +55 -0
  142. package/dist/core/project-id.js +20 -0
  143. package/dist/core/refusal.js +1 -0
  144. package/dist/core/relations.js +208 -0
  145. package/dist/core/repo.js +54 -0
  146. package/dist/core/samples.js +161 -0
  147. package/dist/core/search-words.js +68 -0
  148. package/dist/core/test-report.js +120 -0
  149. package/dist/core/time.js +8 -0
  150. package/dist/core/validation.js +60 -0
  151. package/dist/core/views.js +171 -0
  152. package/dist/core/workflow.js +51 -0
  153. package/dist/mcp/commands.js +101 -0
  154. package/dist/mcp/server.js +239 -0
  155. package/dist/mcp/words.js +95 -0
  156. package/dist/server/boards.js +232 -0
  157. package/dist/server/changes.js +42 -0
  158. package/dist/server/diff-tip.js +83 -0
  159. package/dist/server/drift.js +87 -0
  160. package/dist/server/errors.js +87 -0
  161. package/dist/server/evidence.js +82 -0
  162. package/dist/server/hosts.js +6 -0
  163. package/dist/server/hub.js +98 -0
  164. package/dist/server/lan-access.js +62 -0
  165. package/dist/server/lan.js +164 -0
  166. package/dist/server/listen.js +70 -0
  167. package/dist/server/notifier.js +156 -0
  168. package/dist/server/notify-api.js +151 -0
  169. package/dist/server/pairing.js +124 -0
  170. package/dist/server/projects.js +191 -0
  171. package/dist/server/push.js +199 -0
  172. package/dist/server/server.js +960 -0
  173. package/dist/server/session.js +43 -0
  174. package/dist/server/stream.js +36 -0
  175. package/dist/server/test-push.js +38 -0
  176. package/dist/server/token.js +37 -0
  177. package/dist/server/transports.js +54 -0
  178. package/dist/server/ui.js +44 -0
  179. package/dist/server/writes.js +361 -0
  180. package/dist/server/wrong-address.js +36 -0
  181. package/dist/store/boards.js +111 -0
  182. package/dist/store/database.js +45 -0
  183. package/dist/store/machine.js +77 -0
  184. package/dist/store/migrations.js +493 -0
  185. package/dist/store/queries.js +585 -0
  186. package/dist/store/registry.js +72 -0
  187. package/package.json +73 -4
  188. package/presets/architecture/playbook.md +69 -0
  189. package/presets/architecture/preset.yaml +80 -0
  190. package/presets/architecture/samples.yaml +23 -0
  191. package/presets/bug-triage/playbook.md +129 -0
  192. package/presets/bug-triage/preset.yaml +127 -0
  193. package/presets/bug-triage/samples/wall-slide-after.png +0 -0
  194. package/presets/bug-triage/samples/wall-slide-before.png +0 -0
  195. package/presets/bug-triage/samples.yaml +72 -0
  196. package/presets/change/playbook.md +115 -0
  197. package/presets/change/preset.yaml +141 -0
  198. package/presets/change/samples.yaml +62 -0
  199. package/presets/review/playbook.md +51 -0
  200. package/presets/review/preset.yaml +62 -0
  201. package/presets/review/samples.yaml +27 -0
  202. package/presets/tasks/playbook.md +78 -0
  203. package/presets/tasks/preset.yaml +61 -0
  204. package/presets/tasks/samples.yaml +46 -0
  205. package/schemas/board.schema.json +764 -0
  206. package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
  207. package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
  208. package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
  209. package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
  210. package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
  211. package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
  212. package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
  213. package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
  214. package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
  215. package/ui/dist/assets/index-C3OzJope.js +103 -0
  216. package/ui/dist/assets/index-COdEjckQ.css +1 -0
  217. package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
  218. package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
  219. package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
  220. package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
  221. package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
  222. package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
  223. package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
  224. package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
  225. package/ui/dist/assets/pdf-rELvHkft.js +56 -0
  226. package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
  227. package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
  228. package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
  229. package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
  230. package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
  231. package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
  232. package/ui/dist/icons/apple-touch-icon.png +0 -0
  233. package/ui/dist/icons/otea-192.png +0 -0
  234. package/ui/dist/icons/otea-512.png +0 -0
  235. package/ui/dist/icons/otea-badge.png +0 -0
  236. package/ui/dist/icons/otea-maskable-512.png +0 -0
  237. package/ui/dist/index.html +25 -0
  238. package/ui/dist/manifest.webmanifest +24 -0
  239. package/ui/dist/otea-idle.svg +5 -0
  240. package/ui/dist/otea-working.svg +5 -0
  241. package/ui/dist/otea-yours-working.svg +5 -0
  242. package/ui/dist/otea-yours.svg +5 -0
  243. package/ui/dist/sw.js +1 -0
@@ -0,0 +1,585 @@
1
+ import { OteaError } from '../core/errors.js';
2
+ import { modelFamily } from '../core/profile.js';
3
+ function toCollection(row) {
4
+ return {
5
+ name: row.name,
6
+ prefix: row.prefix,
7
+ preset: row.preset,
8
+ presetVersion: row.preset_version,
9
+ nextSeq: row.next_seq,
10
+ icon: row.icon,
11
+ };
12
+ }
13
+ export function insertCollection(db, collection) {
14
+ db.prepare('INSERT INTO collections (name, prefix, preset, preset_version, next_seq, icon) VALUES (?, ?, ?, ?, ?, ?)').run(collection.name, collection.prefix, collection.preset, collection.presetVersion, collection.nextSeq, collection.icon);
15
+ }
16
+ export function updateCollectionIcon(db, name, icon) {
17
+ db.prepare('UPDATE collections SET icon = ? WHERE name = ?').run(icon, name);
18
+ }
19
+ // A collection moves to another preset of the same types and states: its items are read under the
20
+ // new one from now on, as written under its current version.
21
+ export function setCollectionPreset(db, name, preset, version) {
22
+ db.prepare('UPDATE collections SET preset = ?, preset_version = ? WHERE name = ?').run(preset, version, name);
23
+ db.prepare('UPDATE items SET preset_version = ? WHERE collection = ?').run(version, name);
24
+ }
25
+ // A name as boards are matched by: in any case, without accents.
26
+ export const foldedName = (name) => name
27
+ .trim()
28
+ .normalize('NFD')
29
+ .replace(/\p{Diacritic}/gu, '')
30
+ .toLocaleLowerCase();
31
+ // The shortest start of a board's name taken for the whole: shorter would be a guess.
32
+ const PREFIX_MIN = 3;
33
+ // A collection by its name, or by the same name in another case or without its accents: a
34
+ // collection is a board, whose name is unique whatever its case, so `tareas` is Tareas and
35
+ // `implementacion` is Implementación. Else the one board whose name starts so (`cambios` for
36
+ // Cambios de código), when only one does.
37
+ export function findCollection(db, name) {
38
+ const row = db.prepare('SELECT * FROM collections WHERE name = ?').get(name);
39
+ if (row)
40
+ return toCollection(row);
41
+ const folded = foldedName(name);
42
+ const all = listCollections(db, { archived: true });
43
+ const same = all.filter((each) => foldedName(each.name) === folded);
44
+ if (same.length > 0)
45
+ return same.length === 1 ? same[0] : undefined;
46
+ if (folded.length < PREFIX_MIN)
47
+ return undefined;
48
+ const starting = listCollections(db).filter((each) => foldedName(each.name).startsWith(folded));
49
+ return starting.length === 1 ? starting[0] : undefined;
50
+ }
51
+ // The collections of an archived board (app/board-archive.ts): left out of every list.
52
+ const ARCHIVED = 'SELECT collection FROM boards WHERE archived_at IS NOT NULL AND collection IS NOT NULL';
53
+ export function collectionArchived(db, name) {
54
+ return db.prepare(`SELECT 1 FROM (${ARCHIVED}) WHERE collection = ?`).get(name) !== undefined;
55
+ }
56
+ // The collections in use, an archived board's left out; `archived` lists those too. In the order
57
+ // their boards were made (a set's in its own order, though its child is published first), then
58
+ // the collections with no board (a preset's) as they were made.
59
+ export function listCollections(db, options = {}) {
60
+ const where = options.archived ? '' : ` WHERE c.name NOT IN (${ARCHIVED})`;
61
+ const sql = `SELECT c.* FROM collections c LEFT JOIN boards b ON b.collection = c.name${where} ORDER BY b.id IS NULL, b.created_at, b.rowid, c.rowid`;
62
+ return db.prepare(sql).all().map(toCollection);
63
+ }
64
+ // Only a collection without items may go: nothing else refers to it.
65
+ export function deleteCollection(db, name) {
66
+ db.prepare('DELETE FROM collections WHERE name = ?').run(name);
67
+ }
68
+ export function collectionTaken(db, name, prefix) {
69
+ const sql = 'SELECT 1 FROM collections WHERE name = ? OR prefix = ?';
70
+ return db.prepare(sql).get(name, prefix) !== undefined;
71
+ }
72
+ export function claimNextSeq(db, collection) {
73
+ const sql = 'UPDATE collections SET next_seq = next_seq + 1 WHERE name = ? RETURNING next_seq';
74
+ return Number(db.prepare(sql).get(collection)?.next_seq) - 1;
75
+ }
76
+ function toItem(row) {
77
+ return {
78
+ id: row.id,
79
+ collection: row.collection,
80
+ seq: row.seq,
81
+ type: row.type,
82
+ title: row.title,
83
+ fields: JSON.parse(row.fields),
84
+ presetVersion: row.preset_version,
85
+ state: row.state,
86
+ createdAt: row.created_at,
87
+ updatedAt: row.updated_at,
88
+ createdBy: { kind: row.created_by_kind, name: row.created_by_name },
89
+ lease: row.lease_holder === null || row.lease_expires_at === null
90
+ ? null
91
+ : { holder: row.lease_holder, expiresAt: row.lease_expires_at },
92
+ };
93
+ }
94
+ export function insertItem(db, item) {
95
+ db.prepare(`INSERT INTO items (id, collection, seq, type, title, fields, preset_version, state,
96
+ created_at, updated_at, created_by_kind, created_by_name)
97
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(item.id, item.collection, item.seq, item.type, item.title, JSON.stringify(item.fields), item.presetVersion, item.state, item.createdAt, item.updatedAt, item.createdBy.kind, item.createdBy.name);
98
+ }
99
+ export function findItem(db, id) {
100
+ const row = db.prepare('SELECT * FROM items WHERE id = ?').get(id);
101
+ return row && toItem(row);
102
+ }
103
+ export function selectItems(db, filter) {
104
+ const sql = `SELECT * FROM items
105
+ WHERE (:collection IS NULL OR collection = :collection) AND (:state IS NULL OR state = :state)
106
+ AND (:type IS NULL OR type = :type)${filter.archived ? '' : ` AND collection NOT IN (${ARCHIVED})`}
107
+ ORDER BY created_at, rowid`;
108
+ const params = {
109
+ collection: filter.collection ?? null,
110
+ state: filter.state ?? null,
111
+ type: filter.type ?? null,
112
+ };
113
+ return db.prepare(sql).all(params).map(toItem);
114
+ }
115
+ export function hasItems(db, collection) {
116
+ return (db.prepare('SELECT 1 FROM items WHERE collection = ? LIMIT 1').get(collection) !== undefined);
117
+ }
118
+ export function hasItemsOfType(db, collection, type) {
119
+ const sql = 'SELECT 1 FROM items WHERE collection = ? AND type = ? LIMIT 1';
120
+ return db.prepare(sql).get(collection, type) !== undefined;
121
+ }
122
+ // Gives the items of a type in a collection another type name; returns how many it renamed.
123
+ export function renameItemType(db, collection, from, to) {
124
+ const sql = 'UPDATE items SET type = ? WHERE collection = ? AND type = ?';
125
+ return Number(db.prepare(sql).run(to, collection, from).changes);
126
+ }
127
+ export function updateItemState(db, id, state, at) {
128
+ db.prepare('UPDATE items SET state = ?, updated_at = ? WHERE id = ?').run(state, at, id);
129
+ }
130
+ // Takes the lease only if it is free, expired or already the holder's: one conditional write, so
131
+ // two writers racing for the same item cannot both win.
132
+ export function takeLease(db, id, lease, at) {
133
+ const sql = `UPDATE items SET lease_holder = :holder, lease_expires_at = :expiresAt
134
+ WHERE id = :id AND (lease_holder IS NULL OR lease_expires_at <= :at OR lease_holder = :holder)`;
135
+ return db.prepare(sql).run({ id, at, ...lease }).changes === 1;
136
+ }
137
+ export function clearLease(db, id) {
138
+ db.prepare('UPDATE items SET lease_holder = NULL, lease_expires_at = NULL WHERE id = ?').run(id);
139
+ }
140
+ export function updateItemContent(db, id, content) {
141
+ db.prepare('UPDATE items SET title = ?, fields = ?, preset_version = ?, updated_at = ? WHERE id = ?').run(content.title, JSON.stringify(content.fields), content.presetVersion, content.updatedAt, id);
142
+ }
143
+ export const profileView = (profile) => ({
144
+ harness: profile.harness,
145
+ model: profile.model,
146
+ effort: profile.effort,
147
+ family: modelFamily(profile.model),
148
+ });
149
+ function toEvent(row) {
150
+ const event = {
151
+ id: row.id,
152
+ itemId: row.item_id,
153
+ type: row.type,
154
+ actor: { kind: row.actor_kind, name: row.actor_name },
155
+ at: row.at,
156
+ data: JSON.parse(row.data),
157
+ };
158
+ if (row.profile)
159
+ event.profile = profileView(JSON.parse(row.profile));
160
+ return event;
161
+ }
162
+ // The agent whose profile an event keeps: the agent acting, or the one relaying the person's word.
163
+ function agentBehind(event) {
164
+ if (event.actor.kind === 'agent')
165
+ return event.actor.name;
166
+ return typeof event.data.via === 'string' ? event.data.via : null;
167
+ }
168
+ // The archived board an item is on, by its name (its collection's); undefined while it stands.
169
+ export function archivedBoardOf(db, itemId) {
170
+ const row = db
171
+ .prepare(`SELECT i.collection AS name FROM items i JOIN boards b ON b.collection = i.collection
172
+ WHERE i.id = ? AND b.archived_at IS NOT NULL`)
173
+ .get(itemId);
174
+ return row ? String(row.name) : undefined;
175
+ }
176
+ // An item on an archived board is read-only: nothing writes to it (core/errors.ts board_archived).
177
+ export function boardArchived(item, board) {
178
+ return new OteaError('board_archived', `${item} is on ${board}, an archived board: it is read-only. Work on the items of the boards in otea next; the person restores ${board} in Studio to work on it again`, { kind: 'board_archived', item, board });
179
+ }
180
+ // Every event keeps the profile its agent has declared at that moment, read in the same statement.
181
+ // No event is written to an item of an archived board: it stays as it was archived.
182
+ export function appendEvent(db, event) {
183
+ const archived = archivedBoardOf(db, event.itemId);
184
+ if (archived !== undefined)
185
+ throw boardArchived(event.itemId, archived);
186
+ db.prepare(`INSERT INTO events (item_id, type, actor_kind, actor_name, at, data, profile)
187
+ VALUES (?, ?, ?, ?, ?, ?, (SELECT profile FROM agents WHERE name = ?))`).run(event.itemId, event.type, event.actor.kind, event.actor.name, event.at, JSON.stringify(event.data), agentBehind(event));
188
+ }
189
+ export function selectEvents(db, itemId) {
190
+ const sql = 'SELECT * FROM events WHERE item_id = ? ORDER BY id';
191
+ return db.prepare(sql).all(itemId).map(toEvent);
192
+ }
193
+ // Every item's events of the given types, in commit order: a list reads what it needs of all items
194
+ // in one query.
195
+ export function selectEventsOfTypes(db, types) {
196
+ const sql = `SELECT * FROM events WHERE type IN (${types.map(() => '?').join(', ')}) ORDER BY id`;
197
+ return db.prepare(sql).all(...types).map(toEvent);
198
+ }
199
+ // Ids follow commit order (writers are serialized by BEGIN IMMEDIATE), so the last id read is a
200
+ // cursor: nothing committed later can land below it.
201
+ export function selectEventsAfter(db, filter) {
202
+ const matching = `SELECT events.* FROM events JOIN items ON items.id = events.item_id
203
+ WHERE events.id > :since AND (:item IS NULL OR events.item_id = :item)
204
+ AND (:collection IS NULL OR items.collection = :collection)`;
205
+ const sql = filter.latest === undefined
206
+ ? `${matching} ORDER BY events.id LIMIT :limit`
207
+ : `SELECT * FROM (${matching} ORDER BY events.id DESC LIMIT :limit) ORDER BY id`;
208
+ const params = {
209
+ since: filter.since ?? 0,
210
+ item: filter.item ?? null,
211
+ collection: filter.collection ?? null,
212
+ limit: filter.latest ?? filter.limit ?? -1,
213
+ };
214
+ return db.prepare(sql).all(params).map(toEvent);
215
+ }
216
+ // When each item entered its current state: the time of its last created or transitioned event.
217
+ export function selectStateEntries(db) {
218
+ const sql = `SELECT item_id, at FROM events WHERE id IN (
219
+ SELECT max(id) FROM events WHERE type IN ('created', 'transitioned') GROUP BY item_id)`;
220
+ const rows = db.prepare(sql).all();
221
+ return new Map(rows.map((row) => [row.item_id, row.at]));
222
+ }
223
+ function toEvidence(row) {
224
+ return {
225
+ id: row.id,
226
+ itemId: row.item_id,
227
+ role: row.role,
228
+ module: row.module,
229
+ kind: row.kind,
230
+ filename: row.filename,
231
+ contentType: row.content_type,
232
+ size: row.size,
233
+ sha256: row.sha256,
234
+ caption: row.caption,
235
+ createdAt: row.created_at,
236
+ actor: { kind: row.actor_kind, name: row.actor_name },
237
+ channel: row.channel,
238
+ };
239
+ }
240
+ export function insertEvidence(db, evidence) {
241
+ const sql = `INSERT INTO evidence (item_id, role, module, kind, filename, content_type, size,
242
+ sha256, caption, created_at, actor_kind, actor_name, channel)
243
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id`;
244
+ const row = db
245
+ .prepare(sql)
246
+ .get(evidence.itemId, evidence.role, evidence.module, evidence.kind, evidence.filename, evidence.contentType, evidence.size, evidence.sha256, evidence.caption, evidence.createdAt, evidence.actor.kind, evidence.actor.name, evidence.channel);
247
+ return Number(row?.id);
248
+ }
249
+ export function findEvidence(db, id) {
250
+ const row = db.prepare('SELECT * FROM evidence WHERE id = ?').get(id);
251
+ return row && toEvidence(row);
252
+ }
253
+ export function selectEvidence(db, itemId) {
254
+ // A file detached by mistake (an evidence_detached event) is off the item's list; its row stays.
255
+ const sql = `SELECT * FROM evidence WHERE item_id = ? AND id NOT IN (
256
+ SELECT json_extract(data, '$.evidenceId') FROM events
257
+ WHERE item_id = ? AND type = 'evidence_detached'
258
+ ) ORDER BY id`;
259
+ return db.prepare(sql).all(itemId, itemId).map(toEvidence);
260
+ }
261
+ function toRelation(row) {
262
+ return {
263
+ kind: row.kind,
264
+ from: row.from_id,
265
+ to: row.to_id,
266
+ createdAt: row.created_at,
267
+ actor: { kind: row.actor_kind, name: row.actor_name },
268
+ channel: row.channel,
269
+ };
270
+ }
271
+ export function insertRelation(db, relation) {
272
+ db.prepare(`INSERT INTO relations (kind, from_id, to_id, created_at, actor_kind, actor_name, channel)
273
+ VALUES (?, ?, ?, ?, ?, ?, ?)`).run(relation.kind, relation.from, relation.to, relation.createdAt, relation.actor.kind, relation.actor.name, relation.channel);
274
+ }
275
+ export function deleteRelation(db, { kind, from, to }) {
276
+ db.prepare('DELETE FROM relations WHERE kind = ? AND from_id = ? AND to_id = ?').run(kind, from, to);
277
+ }
278
+ // Every relation, oldest first; `kind` narrows them to one kind.
279
+ export function selectRelations(db, kind) {
280
+ const sql = `SELECT * FROM relations WHERE (:kind IS NULL OR kind = :kind)
281
+ ORDER BY created_at, rowid`;
282
+ return db.prepare(sql).all({ kind: kind ?? null }).map(toRelation);
283
+ }
284
+ // The relations an item takes part in, from either side, oldest first.
285
+ export function selectRelationsOf(db, id) {
286
+ const sql = `SELECT * FROM relations WHERE from_id = :id OR to_id = :id
287
+ ORDER BY created_at, rowid`;
288
+ return db.prepare(sql).all({ id }).map(toRelation);
289
+ }
290
+ export function selectProjectIdentity(db) {
291
+ return db.prepare('SELECT id, name FROM project').get();
292
+ }
293
+ // Writes the identity unless the project already has one, in one statement so that two processes
294
+ // opening the same database at once still leave a single row.
295
+ export function insertProjectIdentity(db, identity) {
296
+ const sql = 'INSERT INTO project (id, name) SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM project)';
297
+ db.prepare(sql).run(identity.id, identity.name);
298
+ }
299
+ export function selectProjectIcon(db) {
300
+ const row = db.prepare('SELECT icon FROM project').get();
301
+ return row?.icon ?? null;
302
+ }
303
+ export function updateProjectIcon(db, icon) {
304
+ db.prepare('UPDATE project SET icon = ?').run(icon);
305
+ }
306
+ export function selectRepos(db) {
307
+ return db.prepare('SELECT name, identity FROM repos ORDER BY name').all();
308
+ }
309
+ export function insertRepo(db, repo) {
310
+ db.prepare('INSERT INTO repos (name, identity) VALUES (?, ?)').run(repo.name, repo.identity);
311
+ }
312
+ export function renameRepo(db, identity, name) {
313
+ db.prepare('UPDATE repos SET name = ? WHERE identity = ?').run(name, identity);
314
+ }
315
+ export function deleteRepo(db, name) {
316
+ db.prepare('DELETE FROM repos WHERE name = ?').run(name);
317
+ }
318
+ // Whether the repos at the project's folders were read in (app/repos.ts).
319
+ export function selectReposRead(db) {
320
+ const row = db.prepare('SELECT repos_read FROM project').get();
321
+ return row?.repos_read === 1;
322
+ }
323
+ export function markReposRead(db) {
324
+ db.prepare('UPDATE project SET repos_read = 1').run();
325
+ }
326
+ function toAgent(row) {
327
+ return {
328
+ name: row.name,
329
+ harness: row.harness,
330
+ harnessVersion: row.harness_version,
331
+ lastSeen: row.last_seen,
332
+ connectedAt: row.connected_at,
333
+ mcpAt: row.mcp_at,
334
+ mcpAskedAt: row.mcp_asked_at,
335
+ folder: row.folder,
336
+ actedAt: row.acted_at,
337
+ };
338
+ }
339
+ export function findAgent(db, name) {
340
+ const row = db.prepare('SELECT * FROM agents WHERE name = ?').get(name);
341
+ return row && toAgent(row);
342
+ }
343
+ // Forgets a name seen on nothing but a command that failed: an agent that never declared a profile.
344
+ export function forgetAgent(db, name) {
345
+ db.prepare('DELETE FROM agents WHERE name = ? AND profile IS NULL').run(name);
346
+ }
347
+ // Notes that an agent listens now: its MCP server is open, or its otea wait blocks.
348
+ // `session`: the MCP server heard, kept until another server is; a command-line wait gives none.
349
+ // `waiter`: the process of the command-line wait heard, kept until the wait ends (endWaiter).
350
+ export function markListening(db, agent, at, session, harness, waiter) {
351
+ const sql = `INSERT INTO agents (name, last_seen, listening_at, listening_session, listening_harness,
352
+ listening_waiter)
353
+ VALUES (:agent, :at, :at, :session, :harness, :waiter)
354
+ ON CONFLICT (name) DO UPDATE SET listening_at = excluded.listening_at,
355
+ listening_harness = CASE WHEN excluded.listening_session IS NULL THEN listening_harness
356
+ ELSE excluded.listening_harness END,
357
+ listening_session = coalesce(excluded.listening_session, listening_session),
358
+ listening_waiter = coalesce(excluded.listening_waiter, listening_waiter)`;
359
+ db.prepare(sql).run({
360
+ agent,
361
+ at,
362
+ session: session ?? null,
363
+ harness: harness ?? null,
364
+ waiter: waiter ?? null,
365
+ });
366
+ }
367
+ // A wait that ends by itself leaves no process to watch: only a killed one stays noted.
368
+ export function clearWaiter(db, agent, waiter) {
369
+ db.prepare('UPDATE agents SET listening_waiter = NULL WHERE name = ? AND listening_waiter = ?').run(agent, waiter);
370
+ }
371
+ // Notes the agent no longer listening, if it was last heard listening `at` (and not since), or
372
+ // since then only through the MCP server `session` that ends (its calls hear the agent too).
373
+ export function clearListening(db, agent, at, session) {
374
+ db.prepare(`UPDATE agents SET listening_at = NULL WHERE name = :agent AND (listening_at = :at
375
+ OR (listening_session = :session AND listening_at >= :at))`).run({ agent, at, session: session ?? null });
376
+ }
377
+ // Notes that an agent waits in otea wait now, on the CLI or over MCP: the desk's moves reach it.
378
+ export function markWaiting(db, agent, at) {
379
+ db.prepare('UPDATE agents SET waiting_at = :at WHERE name = :agent').run({ agent, at });
380
+ }
381
+ // Notes the agent no longer waiting, if it was last heard waiting `at` (and not since).
382
+ export function clearWaiting(db, agent, at) {
383
+ db.prepare('UPDATE agents SET waiting_at = NULL WHERE name = :agent AND waiting_at = :at').run({
384
+ agent,
385
+ at,
386
+ });
387
+ }
388
+ // When each agent was last heard waiting, for those waiting or heard waiting.
389
+ export function selectWaiting(db) {
390
+ const rows = db
391
+ .prepare('SELECT name, waiting_at FROM agents WHERE waiting_at IS NOT NULL')
392
+ .all();
393
+ return new Map(rows.map((row) => [row.name, row.waiting_at]));
394
+ }
395
+ // The MCP server an agent was last heard listening from, the harness process that started it, and
396
+ // when it was heard.
397
+ export function selectListener(db, agent) {
398
+ const row = db
399
+ .prepare('SELECT listening_at, listening_session, listening_harness FROM agents WHERE name = :agent')
400
+ .get({ agent });
401
+ return (row && { at: row.listening_at, session: row.listening_session, harness: row.listening_harness });
402
+ }
403
+ // When each agent was last heard listening, for those ever heard.
404
+ export function selectListening(db) {
405
+ const rows = db
406
+ .prepare('SELECT name, listening_at FROM agents WHERE listening_at IS NOT NULL')
407
+ .all();
408
+ return new Map(rows.map((row) => [row.name, row.listening_at]));
409
+ }
410
+ // The processes each agent was last heard listening through: its MCP server, its running wait.
411
+ export function selectListenerProcesses(db) {
412
+ const rows = db
413
+ .prepare(`SELECT name, listening_session, listening_waiter FROM agents
414
+ WHERE listening_session IS NOT NULL OR listening_waiter IS NOT NULL`)
415
+ .all();
416
+ return new Map(rows.map((row) => [row.name, { session: row.listening_session, waiter: row.listening_waiter }]));
417
+ }
418
+ export function selectAgents(db) {
419
+ const rows = db.prepare('SELECT * FROM agents ORDER BY name').all();
420
+ return rows.map(toAgent);
421
+ }
422
+ // A record given without `connectedAt` keeps the one known.
423
+ export function upsertAgent(db, agent) {
424
+ const sql = `INSERT INTO agents (name, harness, harness_version, last_seen, connected_at, mcp_at, folder,
425
+ acted_at)
426
+ VALUES (:name, :harness, :harnessVersion, :lastSeen, :connectedAt, :mcpAt, :folder, :actedAt)
427
+ ON CONFLICT (name) DO UPDATE SET harness = excluded.harness,
428
+ harness_version = excluded.harness_version, last_seen = excluded.last_seen,
429
+ connected_at = COALESCE(excluded.connected_at, agents.connected_at),
430
+ mcp_at = COALESCE(excluded.mcp_at, agents.mcp_at),
431
+ folder = COALESCE(excluded.folder, agents.folder),
432
+ acted_at = COALESCE(excluded.acted_at, agents.acted_at)`;
433
+ const { name, harness, harnessVersion, lastSeen } = agent;
434
+ db.prepare(sql).run({
435
+ name,
436
+ harness,
437
+ harnessVersion,
438
+ lastSeen,
439
+ connectedAt: agent.connectedAt ?? null,
440
+ mcpAt: agent.mcpAt ?? null,
441
+ folder: agent.folder ?? null,
442
+ actedAt: agent.actedAt ?? null,
443
+ });
444
+ }
445
+ // When each agent last let go of an item itself (otea release), by its name.
446
+ export function selectLastReleases(db) {
447
+ const rows = db
448
+ .prepare(`SELECT actor_name AS name, max(at) AS at FROM events
449
+ WHERE type = 'released' AND actor_kind = 'agent' GROUP BY actor_name`)
450
+ .all();
451
+ return new Map(rows.map(({ name, at }) => [name, at]));
452
+ }
453
+ export function saveMcpLoad(db, load) {
454
+ const sql = `INSERT INTO mcp_loads (harness, name, at, session)
455
+ VALUES (:harness, :name, :at, :session)
456
+ ON CONFLICT (harness) DO UPDATE SET
457
+ name = excluded.name, at = excluded.at, session = excluded.session`;
458
+ db.prepare(sql).run(load);
459
+ }
460
+ export function selectMcpLoads(db) {
461
+ const sql = 'SELECT harness, name, at, session FROM mcp_loads ORDER BY at DESC';
462
+ return db.prepare(sql).all();
463
+ }
464
+ // Notes that the process `pid` still runs, `at`, and forgets the beats older than `stale`: a
465
+ // process killed with its session never says it ended, and its row would stay forever.
466
+ export function saveProcessBeat(db, pid, at, stale) {
467
+ db.prepare('DELETE FROM process_beats WHERE at < ?').run(stale);
468
+ db.prepare(`INSERT INTO process_beats (pid, at) VALUES (:pid, :at)
469
+ ON CONFLICT (pid) DO UPDATE SET at = excluded.at`).run({ pid, at });
470
+ }
471
+ // Notes that the MCP server `pid` starts, `at`: it has served no call yet. A row an earlier process
472
+ // of the same id left (killed before it said it ended) is started over.
473
+ export function saveProcessStart(db, pid, at, stale) {
474
+ db.prepare('DELETE FROM process_beats WHERE at < ?').run(stale);
475
+ db.prepare(`INSERT INTO process_beats (pid, at, started_at) VALUES (:pid, :at, :at)
476
+ ON CONFLICT (pid) DO UPDATE SET at = excluded.at, started_at = excluded.started_at,
477
+ called_at = NULL, calling = 0`).run({ pid, at });
478
+ }
479
+ // When the MCP server `pid` started, last served a call, and how many long calls it holds open.
480
+ export function selectProcessCalls(db, pid) {
481
+ const row = db
482
+ .prepare('SELECT started_at, called_at, calling FROM process_beats WHERE pid = ?')
483
+ .get(pid);
484
+ return row && { startedAt: row.started_at, calledAt: row.called_at, calling: row.calling };
485
+ }
486
+ // The latest start of a process other than `pid` (null when none noted one).
487
+ export function selectLatestStartBesides(db, pid) {
488
+ const row = db
489
+ .prepare('SELECT MAX(started_at) AS at FROM process_beats WHERE pid <> ?')
490
+ .get(pid);
491
+ return row?.at ?? null;
492
+ }
493
+ // Notes that the MCP server `pid` served a call `at`, and opened (1) or closed (-1) a long one.
494
+ export function saveProcessCall(db, pid, at, open) {
495
+ db.prepare(`UPDATE process_beats SET called_at = :at, calling = MAX(0, calling + :open) WHERE pid = :pid`).run({ pid, at, open });
496
+ }
497
+ export function deleteProcessBeat(db, pid) {
498
+ db.prepare('DELETE FROM process_beats WHERE pid = ?').run(pid);
499
+ }
500
+ // When each process last said it still runs, by its process id.
501
+ export function selectProcessBeats(db) {
502
+ const rows = db.prepare('SELECT pid, at FROM process_beats').all();
503
+ return new Map(rows.map((row) => [row.pid, row.at]));
504
+ }
505
+ // Notes that an agent asked to work through Otea's MCP server (otea connect), now.
506
+ export function markMcpAsked(db, agent, at) {
507
+ const sql = `INSERT INTO agents (name, last_seen, mcp_asked_at) VALUES (:agent, :at, :at)
508
+ ON CONFLICT (name) DO UPDATE SET mcp_asked_at = excluded.mcp_asked_at`;
509
+ db.prepare(sql).run({ agent, at });
510
+ }
511
+ export function findProfile(db, agent) {
512
+ const sql = 'SELECT profile, profile_confirmed FROM agents WHERE name = ?';
513
+ const row = db.prepare(sql).get(agent);
514
+ if (!row?.profile)
515
+ return undefined;
516
+ return { profile: JSON.parse(row.profile), confirmed: row.profile_confirmed === 1 };
517
+ }
518
+ // Sets the agent's profile, confirmed or not for its session. Returns whether it changed; each
519
+ // change is kept in agent_profiles.
520
+ export function saveProfile(db, agent, profile, { at, channel, confirmed }) {
521
+ const { harness, model, effort } = profile;
522
+ const json = JSON.stringify({ harness, model, effort });
523
+ const before = db.prepare('SELECT profile FROM agents WHERE name = ?').get(agent);
524
+ const sql = `INSERT INTO agents (name, last_seen, profile, profile_confirmed)
525
+ VALUES (:agent, :at, :json, :confirmed)
526
+ ON CONFLICT (name) DO UPDATE SET profile = excluded.profile,
527
+ profile_confirmed = excluded.profile_confirmed`;
528
+ db.prepare(sql).run({ agent, at, json, confirmed: confirmed ? 1 : 0 });
529
+ if (before?.profile === json)
530
+ return false;
531
+ const insert = 'INSERT INTO agent_profiles (agent, profile, at, channel) VALUES (?, ?, ?, ?)';
532
+ db.prepare(insert).run(agent, json, at, channel);
533
+ return true;
534
+ }
535
+ // A new session of the agent: its profile stands unconfirmed until it declares it again.
536
+ export function unconfirmProfile(db, agent) {
537
+ const sql = 'UPDATE agents SET profile_confirmed = 0 WHERE name = ? AND profile_confirmed = 1';
538
+ db.prepare(sql).run(agent);
539
+ }
540
+ // Every profile the agent declared, oldest first.
541
+ export function selectProfileChanges(db, agent) {
542
+ const sql = 'SELECT profile, at, channel FROM agent_profiles WHERE agent = ? ORDER BY id';
543
+ const rows = db.prepare(sql).all(agent);
544
+ return rows.map(({ profile, at, channel }) => ({
545
+ profile: profileView(JSON.parse(profile)),
546
+ at,
547
+ channel,
548
+ }));
549
+ }
550
+ export function selectNameOffers(db) {
551
+ const sql = 'SELECT name, offered_at AS at, session FROM name_offers';
552
+ return db.prepare(sql).all();
553
+ }
554
+ export function saveNameOffer(db, offer) {
555
+ const sql = `INSERT INTO name_offers (name, offered_at, session) VALUES (:name, :at, :session)
556
+ ON CONFLICT (name) DO UPDATE SET offered_at = excluded.offered_at, session = excluded.session`;
557
+ db.prepare(sql).run(offer);
558
+ }
559
+ export function selectOffers(db) {
560
+ return db.prepare('SELECT agent, item, state, at FROM offers').all();
561
+ }
562
+ // An agent holds one offer at a time: a new one replaces the last.
563
+ export function saveOffer(db, offer) {
564
+ const sql = `INSERT INTO offers (agent, item, state, at) VALUES (:agent, :item, :state, :at)
565
+ ON CONFLICT (agent) DO UPDATE SET item = excluded.item, state = excluded.state, at = excluded.at`;
566
+ db.prepare(sql).run(offer);
567
+ }
568
+ // The items with a lease stored, live or expired, in creation order.
569
+ export function selectLeases(db) {
570
+ const sql = `SELECT id, lease_holder, lease_expires_at FROM items
571
+ WHERE lease_holder IS NOT NULL AND lease_expires_at IS NOT NULL ORDER BY created_at, rowid`;
572
+ const rows = db.prepare(sql).all();
573
+ return rows.map((row) => ({
574
+ id: row.id,
575
+ lease: { holder: row.lease_holder, expiresAt: row.lease_expires_at },
576
+ }));
577
+ }
578
+ // The item a create with this client key made, if one did (see the client_keys migration).
579
+ export function itemOfClientKey(db, key) {
580
+ const row = db.prepare('SELECT item FROM client_keys WHERE key = ?').get(key);
581
+ return row === undefined ? undefined : String(row.item);
582
+ }
583
+ export function insertClientKey(db, key, item, at) {
584
+ db.prepare('INSERT INTO client_keys (key, item, at) VALUES (?, ?, ?)').run(key, item, at);
585
+ }
@@ -0,0 +1,72 @@
1
+ import { applyMigrations } from './migrations.js';
2
+ // The user-level list of projects and the folders that lead to them. A project can have several
3
+ // paths (a product folder, or separate repos linked to one project); a path belongs to one project.
4
+ const REGISTRY_MIGRATIONS = [
5
+ `
6
+ CREATE TABLE projects (
7
+ id TEXT PRIMARY KEY,
8
+ name TEXT NOT NULL,
9
+ storage TEXT NOT NULL CHECK (storage IN ('common', 'local')),
10
+ data_dir TEXT NOT NULL
11
+ );
12
+
13
+ CREATE TABLE project_paths (
14
+ path_key TEXT PRIMARY KEY,
15
+ path TEXT NOT NULL,
16
+ project_id TEXT NOT NULL REFERENCES projects (id),
17
+ kind TEXT NOT NULL CHECK (kind IN ('folder', 'repo'))
18
+ );
19
+
20
+ CREATE INDEX project_paths_project_id ON project_paths (project_id);
21
+ `,
22
+ // What identified a linked repo when it was linked, so a relink can tell it is the same repo
23
+ // even after the old folder is gone.
24
+ `
25
+ ALTER TABLE project_paths ADD COLUMN remote_url TEXT;
26
+ ALTER TABLE project_paths ADD COLUMN root_commit TEXT;
27
+ `,
28
+ ];
29
+ export function migrateRegistry(db) {
30
+ applyMigrations(db, REGISTRY_MIGRATIONS);
31
+ }
32
+ export function insertRegisteredProject(db, project) {
33
+ db.prepare('INSERT INTO projects (id, name, storage, data_dir) VALUES (?, ?, ?, ?)').run(project.id, project.name, project.storage, project.dataDir);
34
+ }
35
+ function toProject(row) {
36
+ return { id: row.id, name: row.name, storage: row.storage, dataDir: row.data_dir };
37
+ }
38
+ export function selectRegisteredProject(db, id) {
39
+ const row = db.prepare('SELECT * FROM projects WHERE id = ?').get(id);
40
+ return row && toProject(row);
41
+ }
42
+ export function selectRegisteredProjects(db) {
43
+ const rows = db.prepare('SELECT * FROM projects ORDER BY name, id').all();
44
+ return rows.map(toProject);
45
+ }
46
+ export function insertProjectPath(db, path) {
47
+ db.prepare(`INSERT INTO project_paths (path_key, path, project_id, kind, remote_url, root_commit)
48
+ VALUES (?, ?, ?, ?, ?, ?)`).run(path.pathKey, path.path, path.projectId, path.kind, path.remoteUrl, path.rootCommit);
49
+ }
50
+ // Moves a link to another folder, keeping its project.
51
+ export function updateProjectPath(db, oldKey, path) {
52
+ db.prepare(`UPDATE project_paths SET path_key = ?, path = ?, kind = ?, remote_url = ?, root_commit = ?
53
+ WHERE path_key = ?`).run(path.pathKey, path.path, path.kind, path.remoteUrl, path.rootCommit, oldKey);
54
+ }
55
+ export function deleteProjectPath(db, pathKey) {
56
+ db.prepare('DELETE FROM project_paths WHERE path_key = ?').run(pathKey);
57
+ }
58
+ export function deleteRegisteredProject(db, id) {
59
+ db.prepare('DELETE FROM project_paths WHERE project_id = ?').run(id);
60
+ db.prepare('DELETE FROM projects WHERE id = ?').run(id);
61
+ }
62
+ export function selectProjectPaths(db) {
63
+ const rows = db.prepare('SELECT * FROM project_paths ORDER BY path_key').all();
64
+ return rows.map((row) => ({
65
+ path: row.path,
66
+ pathKey: row.path_key,
67
+ projectId: row.project_id,
68
+ kind: row.kind,
69
+ remoteUrl: row.remote_url,
70
+ rootCommit: row.root_commit,
71
+ }));
72
+ }