@myrialabs/clopen 0.4.37 → 0.4.39

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 (193) hide show
  1. package/README.md +2 -2
  2. package/backend/auth/permissions.ts +39 -0
  3. package/backend/bootstrap.ts +16 -0
  4. package/backend/chat/stream-manager.ts +1 -1
  5. package/backend/database/crypto/envelope.test.ts +93 -0
  6. package/backend/database/crypto/envelope.ts +127 -0
  7. package/backend/database/crypto/index.ts +22 -0
  8. package/backend/database/crypto/master-key.ts +129 -0
  9. package/backend/database/crypto/secret-columns.ts +151 -0
  10. package/backend/database/migrations/071_create_notes_tables.ts +90 -0
  11. package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
  12. package/backend/database/migrations/073_create_integration_tables.ts +119 -0
  13. package/backend/database/migrations/074_create_work_tables.ts +94 -0
  14. package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
  15. package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
  16. package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
  17. package/backend/database/migrations/index.ts +49 -0
  18. package/backend/database/queries/db-client-connection-queries.ts +42 -12
  19. package/backend/database/queries/deploy-queries.ts +124 -0
  20. package/backend/database/queries/engine-queries.ts +23 -10
  21. package/backend/database/queries/graph-queries.ts +291 -247
  22. package/backend/database/queries/index.ts +10 -1
  23. package/backend/database/queries/integration-db-link-queries.ts +168 -0
  24. package/backend/database/queries/integration-queries.ts +325 -0
  25. package/backend/database/queries/mcp-server-queries.ts +38 -11
  26. package/backend/database/queries/note-queries.test.ts +190 -0
  27. package/backend/database/queries/note-queries.ts +269 -0
  28. package/backend/database/queries/profile-queries.ts +1 -1
  29. package/backend/database/queries/ssh-connection-queries.ts +19 -8
  30. package/backend/database/queries/work-queries.ts +210 -0
  31. package/backend/db-client/integrations/index.ts +37 -0
  32. package/backend/db-client/integrations/links.ts +533 -0
  33. package/backend/db-client/integrations/projector.test.ts +280 -0
  34. package/backend/db-client/integrations/projector.ts +256 -0
  35. package/backend/db-client/integrations/supabase-service.ts +266 -0
  36. package/backend/db-client/providers/registry.ts +27 -0
  37. package/backend/db-client/providers/supabase/adapter.ts +500 -0
  38. package/backend/db-client/providers/supabase/api-types.ts +92 -0
  39. package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
  40. package/backend/db-client/providers/supabase/client.ts +259 -0
  41. package/backend/db-client/providers/supabase/features.ts +502 -0
  42. package/backend/db-client/providers/supabase/local.test.ts +78 -0
  43. package/backend/db-client/providers/supabase/local.ts +137 -0
  44. package/backend/db-client/providers/types.ts +137 -0
  45. package/backend/deployments/bindings.test.ts +43 -0
  46. package/backend/deployments/bindings.ts +216 -0
  47. package/backend/deployments/index.ts +53 -0
  48. package/backend/deployments/log-streams.ts +214 -0
  49. package/backend/deployments/prompts.ts +70 -0
  50. package/backend/deployments/providers/vercel/adapter.ts +757 -0
  51. package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
  52. package/backend/deployments/providers/vercel/build-logs.ts +211 -0
  53. package/backend/deployments/providers/vercel/client.ts +203 -0
  54. package/backend/deployments/providers/vercel/list.test.ts +624 -0
  55. package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
  56. package/backend/deployments/providers/vercel/normalise.ts +113 -0
  57. package/backend/deployments/providers/vercel/types.ts +104 -0
  58. package/backend/deployments/registry.ts +27 -0
  59. package/backend/deployments/service.ts +458 -0
  60. package/backend/deployments/types.ts +278 -0
  61. package/backend/engine/docs/artifacts.md +22 -9
  62. package/backend/files/os-clipboard.test.ts +209 -0
  63. package/backend/files/os-clipboard.ts +567 -0
  64. package/backend/http/integration-hooks.ts +42 -0
  65. package/backend/http/notes-images.test.ts +228 -0
  66. package/backend/http/notes-images.ts +196 -0
  67. package/backend/index.ts +13 -0
  68. package/backend/integrations/accounts.ts +229 -0
  69. package/backend/integrations/health.ts +146 -0
  70. package/backend/integrations/index.ts +36 -0
  71. package/backend/integrations/projections/index.ts +0 -0
  72. package/backend/integrations/projections/mcp-projector.test.ts +204 -0
  73. package/backend/integrations/projections/mcp-projector.ts +163 -0
  74. package/backend/integrations/projections/types.ts +63 -0
  75. package/backend/integrations/registry/define.ts +38 -0
  76. package/backend/integrations/registry/index.ts +60 -0
  77. package/backend/integrations/registry/providers/context7.ts +44 -0
  78. package/backend/integrations/registry/providers/firecrawl.ts +41 -0
  79. package/backend/integrations/registry/providers/github.ts +78 -0
  80. package/backend/integrations/registry/providers/supabase.ts +61 -0
  81. package/backend/integrations/registry/providers/vercel.ts +46 -0
  82. package/backend/integrations/registry/types.ts +96 -0
  83. package/backend/integrations/webhooks/gateway.ts +160 -0
  84. package/backend/integrations/webhooks/verify.ts +65 -0
  85. package/backend/mcp/README.md +25 -3
  86. package/backend/mcp/external/config.ts +35 -1
  87. package/backend/mcp/index.ts +1 -1
  88. package/backend/mcp/internal/config.ts +1 -1
  89. package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
  90. package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
  91. package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
  92. package/backend/memory/README.md +57 -34
  93. package/backend/memory/compose.test.ts +3 -3
  94. package/backend/memory/compose.ts +6 -9
  95. package/backend/memory/config.ts +0 -5
  96. package/backend/memory/consolidate.ts +1 -2
  97. package/backend/memory/context.test.ts +8 -54
  98. package/backend/memory/context.ts +19 -25
  99. package/backend/memory/extract/episodic.ts +10 -12
  100. package/backend/memory/extract/index.ts +23 -52
  101. package/backend/memory/extract/scheduler.ts +3 -6
  102. package/backend/memory/index.ts +6 -5
  103. package/backend/memory/invalidate.ts +36 -48
  104. package/backend/memory/layout.ts +319 -130
  105. package/backend/memory/retention.ts +12 -42
  106. package/backend/memory/retrieval.eval.test.ts +2 -2
  107. package/backend/memory/retrieval.test.ts +228 -183
  108. package/backend/memory/retrieval.ts +22 -21
  109. package/backend/memory/revise.test.ts +62 -116
  110. package/backend/memory/revise.ts +0 -1
  111. package/backend/memory/view.ts +8 -5
  112. package/backend/notes/access.test.ts +82 -0
  113. package/backend/notes/access.ts +27 -0
  114. package/backend/notes/storage.test.ts +77 -0
  115. package/backend/notes/storage.ts +114 -0
  116. package/backend/permissions/service.ts +2 -2
  117. package/backend/profiles/service.ts +1 -1
  118. package/backend/projects/project-cleanup.ts +6 -0
  119. package/backend/snapshot/helpers.ts +35 -41
  120. package/backend/snapshot/snapshot-service.ts +90 -0
  121. package/backend/snapshot/turn-changes.test.ts +163 -0
  122. package/backend/snapshot/turn-changes.ts +131 -0
  123. package/backend/work/bindings.ts +124 -0
  124. package/backend/work/index.ts +42 -0
  125. package/backend/work/prompts.ts +150 -0
  126. package/backend/work/providers/github/actions-logs.ts +180 -0
  127. package/backend/work/providers/github/adapter.test.ts +88 -0
  128. package/backend/work/providers/github/adapter.ts +1255 -0
  129. package/backend/work/providers/github/client.ts +280 -0
  130. package/backend/work/providers/github/list-range.test.ts +158 -0
  131. package/backend/work/providers/github/token-kind.test.ts +42 -0
  132. package/backend/work/pull-requests.ts +242 -0
  133. package/backend/work/registry.ts +27 -0
  134. package/backend/work/service.ts +436 -0
  135. package/backend/work/start-work.test.ts +71 -0
  136. package/backend/work/start-work.ts +232 -0
  137. package/backend/work/types.ts +180 -0
  138. package/backend/ws/db-client/accounts.ts +225 -0
  139. package/backend/ws/db-client/connections.ts +34 -3
  140. package/backend/ws/db-client/context.ts +37 -0
  141. package/backend/ws/db-client/index.ts +20 -1
  142. package/backend/ws/db-client/supabase.ts +188 -0
  143. package/backend/ws/deployments/actions.ts +360 -0
  144. package/backend/ws/deployments/context.ts +43 -0
  145. package/backend/ws/deployments/crud.ts +152 -0
  146. package/backend/ws/deployments/index.ts +15 -0
  147. package/backend/ws/deployments/schemas.ts +183 -0
  148. package/backend/ws/files/clipboard.ts +78 -0
  149. package/backend/ws/files/index.ts +5 -0
  150. package/backend/ws/git/diff-budget.ts +70 -11
  151. package/backend/ws/index.ts +16 -2
  152. package/backend/ws/integrations/crud.ts +219 -0
  153. package/backend/ws/integrations/index.ts +22 -0
  154. package/backend/ws/mcp/crud.ts +25 -1
  155. package/backend/ws/memory/crud.ts +4 -16
  156. package/backend/ws/notes/index.ts +278 -0
  157. package/backend/ws/profiles/crud.ts +1 -1
  158. package/backend/ws/snapshot/changes.ts +279 -0
  159. package/backend/ws/snapshot/index.ts +4 -3
  160. package/backend/ws/work/actions.ts +206 -0
  161. package/backend/ws/work/context.ts +51 -0
  162. package/backend/ws/work/crud.ts +347 -0
  163. package/backend/ws/work/index.ts +17 -0
  164. package/backend/ws/work/pull-requests.ts +177 -0
  165. package/backend/ws/work/schemas.ts +226 -0
  166. package/dist/assets/{index-sJmS_CCm.js → index-BrF6nqld.js} +1 -1
  167. package/dist/assets/{index-Bik9TCBi.js → index-C8Gi-b6U.js} +1 -1
  168. package/dist/assets/main-Bd3gb79F.css +1 -0
  169. package/dist/assets/main-Bo1hD7mt.js +1191 -0
  170. package/dist/index.html +2 -2
  171. package/package.json +1 -1
  172. package/shared/constants/engine-tools.ts +1 -1
  173. package/shared/constants/tool-icons.ts +42 -0
  174. package/shared/types/database/schema.ts +41 -0
  175. package/shared/types/db-client/connection.ts +6 -0
  176. package/shared/types/db-client/index.ts +2 -0
  177. package/shared/types/db-client/integrations.ts +174 -0
  178. package/shared/types/db-client/supabase.ts +131 -0
  179. package/shared/types/deployments.ts +382 -0
  180. package/shared/types/integrations.ts +140 -0
  181. package/shared/types/memory.ts +34 -51
  182. package/shared/types/stores/settings.ts +8 -0
  183. package/shared/types/ui/components.ts +1 -1
  184. package/shared/types/work.ts +426 -0
  185. package/shared/utils/diff-calculator.test.ts +89 -0
  186. package/shared/utils/diff-calculator.ts +30 -65
  187. package/shared/utils/logger.ts +4 -0
  188. package/backend/memory/extract/languages.ts +0 -176
  189. package/backend/memory/extract/structural.test.ts +0 -215
  190. package/backend/memory/extract/structural.ts +0 -364
  191. package/backend/ws/snapshot/content.ts +0 -85
  192. package/dist/assets/main-DLqolBI2.css +0 -1
  193. package/dist/assets/main-Df1I3hnw.js +0 -1096
@@ -0,0 +1,347 @@
1
+ /**
2
+ * Reading and changing work items.
3
+ *
4
+ * work:sources which connected accounts serve a project
5
+ * work:list work items for one source
6
+ * work:get one item, with body, comments and mergeability
7
+ * work:states the states an item can be moved to
8
+ * work:comment add a comment
9
+ * work:comment-update edit one
10
+ * work:comment-delete delete one
11
+ * work:transition change state
12
+ * work:assignees who this repository can assign to
13
+ * work:set-assignees replace an item's assignees
14
+ * work:create-issue open a new issue
15
+ * work:set-binding point a project at a different repository
16
+ * work:set-config per-binding behaviour (transitions, branch template)
17
+ *
18
+ * NOT admin-gated, unlike `integrations:*`. The distinction is what the route
19
+ * can reach: an integrations route changes a credential every project and every
20
+ * engine depends on, while these routes act inside one project the caller was
21
+ * already granted access to — the same reasoning that leaves `git:push` open.
22
+ *
23
+ * Every route takes an optional `projectId`, because this surface is global.
24
+ */
25
+
26
+ import { t } from 'elysia';
27
+ import { createRouter } from '$shared/utils/ws-server';
28
+ import { workService } from '$backend/work';
29
+ import { DEFAULT_BRANCH_TEMPLATE } from '$shared/types/work';
30
+ import type { CommentSource, WorkItemKind, WorkItemState } from '$shared/types/work';
31
+ import { ws } from '$backend/utils/ws';
32
+ import { resolveWorkProject } from './context';
33
+ import {
34
+ BINDING_CONFIG_SCHEMA,
35
+ COMMENT_SCHEMA,
36
+ COMMENT_SOURCE_SCHEMA,
37
+ KIND_SCHEMA,
38
+ RATE_LIMIT_SCHEMA,
39
+ SOURCE_SCHEMA,
40
+ WORK_ITEM_DETAIL_SCHEMA,
41
+ WORK_ITEM_SCHEMA
42
+ } from './schemas';
43
+
44
+ /**
45
+ * Tell a project's viewers that one of its sources changed.
46
+ *
47
+ * Project-scoped rather than global: a binding belongs to one project, and two
48
+ * people looking at different projects have no reason to refetch because of
49
+ * each other.
50
+ */
51
+ export function announceWorkChanged(projectId: string): void {
52
+ ws.emit.project(projectId, 'work:changed', { projectId });
53
+ }
54
+
55
+ const STATE_FILTER_SCHEMA = t.Union([t.Literal('open'), t.Literal('closed'), t.Literal('all')]);
56
+
57
+ /** Present on every route in this surface. Empty means "the current project". */
58
+ const PROJECT_FIELD = { projectId: t.Optional(t.String()) };
59
+
60
+ export const workCrudHandler = createRouter()
61
+ .http('work:sources', {
62
+ data: t.Object({ ...PROJECT_FIELD }),
63
+ response: t.Array(SOURCE_SCHEMA)
64
+ }, async ({ data, conn }) => {
65
+ const { projectId, project } = resolveWorkProject(conn, data.projectId);
66
+ // The MAIN project path, not the viewed worktree: a worktree is a copy and
67
+ // carries the same remotes, but the project is what the binding belongs to
68
+ // and reading it from a tree that may have been deleted is a race.
69
+ return workService.listSources(projectId, project.path);
70
+ })
71
+
72
+ .http('work:list', {
73
+ data: t.Object({
74
+ ...PROJECT_FIELD,
75
+ accountId: t.String({ minLength: 1 }),
76
+ kind: KIND_SCHEMA,
77
+ state: STATE_FILTER_SCHEMA,
78
+ mineOnly: t.Boolean(),
79
+ author: t.Optional(t.String()),
80
+ labels: t.Optional(t.Array(t.String(), { maxItems: 20 })),
81
+ since: t.Optional(t.String()),
82
+ until: t.Optional(t.String()),
83
+ search: t.Optional(t.String()),
84
+ page: t.Optional(t.Number({ minimum: 1, maximum: 100 }))
85
+ }),
86
+ response: t.Object({
87
+ items: t.Array(WORK_ITEM_SCHEMA),
88
+ rateLimit: t.Union([RATE_LIMIT_SCHEMA, t.Null()]),
89
+ hasMore: t.Boolean(),
90
+ nextPage: t.Union([t.Number(), t.Null()])
91
+ })
92
+ }, async ({ data, conn }) => {
93
+ const { projectId } = resolveWorkProject(conn, data.projectId);
94
+ return workService.list(projectId, data.accountId, {
95
+ kind: data.kind as WorkItemKind,
96
+ state: data.state as WorkItemState,
97
+ mineOnly: data.mineOnly,
98
+ ...(data.author && { author: data.author }),
99
+ ...(data.labels?.length && { labels: data.labels }),
100
+ ...(data.since && { since: data.since }),
101
+ ...(data.until && { until: data.until }),
102
+ ...(data.search && { search: data.search }),
103
+ ...(data.page && { page: data.page })
104
+ });
105
+ })
106
+
107
+ .http('work:labels', {
108
+ data: t.Object({
109
+ ...PROJECT_FIELD,
110
+ accountId: t.String({ minLength: 1 })
111
+ }),
112
+ response: t.Array(t.String())
113
+ }, async ({ data, conn }) => {
114
+ const { projectId } = resolveWorkProject(conn, data.projectId);
115
+ return workService.listLabels(projectId, data.accountId);
116
+ })
117
+
118
+ .http('work:update-item', {
119
+ data: t.Object({
120
+ ...PROJECT_FIELD,
121
+ accountId: t.String({ minLength: 1 }),
122
+ kind: KIND_SCHEMA,
123
+ id: t.String({ minLength: 1 }),
124
+ title: t.Optional(t.String({ minLength: 1, maxLength: 400 })),
125
+ body: t.Optional(t.String({ maxLength: 65_536 }))
126
+ }),
127
+ response: t.Object({ ok: t.Boolean() })
128
+ }, async ({ data, conn }) => {
129
+ const { projectId } = resolveWorkProject(conn, data.projectId);
130
+ await workService.updateItem(
131
+ projectId,
132
+ data.accountId,
133
+ data.kind as WorkItemKind,
134
+ data.id,
135
+ {
136
+ ...(data.title !== undefined && { title: data.title }),
137
+ ...(data.body !== undefined && { body: data.body })
138
+ }
139
+ );
140
+ announceWorkChanged(projectId);
141
+ return { ok: true };
142
+ })
143
+
144
+ .http('work:get', {
145
+ data: t.Object({
146
+ ...PROJECT_FIELD,
147
+ accountId: t.String({ minLength: 1 }),
148
+ kind: KIND_SCHEMA,
149
+ id: t.String({ minLength: 1 })
150
+ }),
151
+ response: WORK_ITEM_DETAIL_SCHEMA
152
+ }, async ({ data, conn }) => {
153
+ const { projectId } = resolveWorkProject(conn, data.projectId);
154
+ return workService.get(projectId, data.accountId, data.kind as WorkItemKind, data.id);
155
+ })
156
+
157
+ .http('work:states', {
158
+ data: t.Object({
159
+ ...PROJECT_FIELD,
160
+ accountId: t.String({ minLength: 1 }),
161
+ kind: KIND_SCHEMA
162
+ }),
163
+ response: t.Array(t.Object({
164
+ value: t.String(),
165
+ label: t.String(),
166
+ category: t.Union([t.Literal('open'), t.Literal('closed'), t.Literal('merged'), t.Literal('draft')])
167
+ }))
168
+ }, async ({ data, conn }) => {
169
+ const { projectId } = resolveWorkProject(conn, data.projectId);
170
+ return workService.listStates(projectId, data.accountId, data.kind as WorkItemKind);
171
+ })
172
+
173
+ .http('work:comment', {
174
+ data: t.Object({
175
+ ...PROJECT_FIELD,
176
+ accountId: t.String({ minLength: 1 }),
177
+ kind: KIND_SCHEMA,
178
+ id: t.String({ minLength: 1 }),
179
+ body: t.String({ minLength: 1, maxLength: 65_536 })
180
+ }),
181
+ response: COMMENT_SCHEMA
182
+ }, async ({ data, conn }) => {
183
+ const { projectId } = resolveWorkProject(conn, data.projectId);
184
+ const comment = await workService.comment(
185
+ projectId,
186
+ data.accountId,
187
+ data.kind as WorkItemKind,
188
+ data.id,
189
+ data.body
190
+ );
191
+ announceWorkChanged(projectId);
192
+ return comment;
193
+ })
194
+
195
+ .http('work:comment-update', {
196
+ data: t.Object({
197
+ ...PROJECT_FIELD,
198
+ accountId: t.String({ minLength: 1 }),
199
+ commentId: t.String({ minLength: 1 }),
200
+ source: COMMENT_SOURCE_SCHEMA,
201
+ body: t.String({ minLength: 1, maxLength: 65_536 })
202
+ }),
203
+ response: COMMENT_SCHEMA
204
+ }, async ({ data, conn }) => {
205
+ const { projectId } = resolveWorkProject(conn, data.projectId);
206
+ const comment = await workService.updateComment(
207
+ projectId,
208
+ data.accountId,
209
+ { id: data.commentId, source: data.source as CommentSource },
210
+ data.body
211
+ );
212
+ announceWorkChanged(projectId);
213
+ return comment;
214
+ })
215
+
216
+ .http('work:comment-delete', {
217
+ data: t.Object({
218
+ ...PROJECT_FIELD,
219
+ accountId: t.String({ minLength: 1 }),
220
+ commentId: t.String({ minLength: 1 }),
221
+ source: COMMENT_SOURCE_SCHEMA
222
+ }),
223
+ response: t.Object({ ok: t.Boolean() })
224
+ }, async ({ data, conn }) => {
225
+ const { projectId } = resolveWorkProject(conn, data.projectId);
226
+ await workService.deleteComment(projectId, data.accountId, {
227
+ id: data.commentId,
228
+ source: data.source as CommentSource
229
+ });
230
+ announceWorkChanged(projectId);
231
+ return { ok: true };
232
+ })
233
+
234
+ .http('work:transition', {
235
+ data: t.Object({
236
+ ...PROJECT_FIELD,
237
+ accountId: t.String({ minLength: 1 }),
238
+ kind: KIND_SCHEMA,
239
+ id: t.String({ minLength: 1 }),
240
+ state: t.String({ minLength: 1 })
241
+ }),
242
+ response: t.Object({ ok: t.Boolean() })
243
+ }, async ({ data, conn }) => {
244
+ const { projectId } = resolveWorkProject(conn, data.projectId);
245
+ await workService.transition(
246
+ projectId,
247
+ data.accountId,
248
+ data.kind as WorkItemKind,
249
+ data.id,
250
+ data.state
251
+ );
252
+ announceWorkChanged(projectId);
253
+ return { ok: true };
254
+ })
255
+
256
+ .http('work:assignees', {
257
+ data: t.Object({
258
+ ...PROJECT_FIELD,
259
+ accountId: t.String({ minLength: 1 })
260
+ }),
261
+ response: t.Array(t.String())
262
+ }, async ({ data, conn }) => {
263
+ const { projectId } = resolveWorkProject(conn, data.projectId);
264
+ return workService.listAssignees(projectId, data.accountId);
265
+ })
266
+
267
+ .http('work:set-assignees', {
268
+ data: t.Object({
269
+ ...PROJECT_FIELD,
270
+ accountId: t.String({ minLength: 1 }),
271
+ kind: KIND_SCHEMA,
272
+ id: t.String({ minLength: 1 }),
273
+ logins: t.Array(t.String(), { maxItems: 10 })
274
+ }),
275
+ response: t.Object({ ok: t.Boolean() })
276
+ }, async ({ data, conn }) => {
277
+ const { projectId } = resolveWorkProject(conn, data.projectId);
278
+ await workService.setAssignees(
279
+ projectId,
280
+ data.accountId,
281
+ data.kind as WorkItemKind,
282
+ data.id,
283
+ data.logins
284
+ );
285
+ announceWorkChanged(projectId);
286
+ return { ok: true };
287
+ })
288
+
289
+ .http('work:create-issue', {
290
+ data: t.Object({
291
+ ...PROJECT_FIELD,
292
+ accountId: t.String({ minLength: 1 }),
293
+ title: t.String({ minLength: 1, maxLength: 400 }),
294
+ body: t.String({ maxLength: 65_536 }),
295
+ assignees: t.Array(t.String(), { maxItems: 10 }),
296
+ labels: t.Array(t.String(), { maxItems: 20 })
297
+ }),
298
+ response: WORK_ITEM_SCHEMA
299
+ }, async ({ data, conn }) => {
300
+ const { projectId } = resolveWorkProject(conn, data.projectId);
301
+ const item = await workService.createIssue(projectId, data.accountId, {
302
+ title: data.title,
303
+ body: data.body,
304
+ assignees: data.assignees,
305
+ labels: data.labels
306
+ });
307
+ announceWorkChanged(projectId);
308
+ return item;
309
+ })
310
+
311
+ .http('work:set-binding', {
312
+ data: t.Object({
313
+ ...PROJECT_FIELD,
314
+ accountId: t.String({ minLength: 1 }),
315
+ locator: t.String({ minLength: 1, maxLength: 200 })
316
+ }),
317
+ response: t.Object({ ok: t.Boolean() })
318
+ }, async ({ data, conn }) => {
319
+ const { projectId } = resolveWorkProject(conn, data.projectId);
320
+ workService.setBinding(projectId, data.accountId, data.locator);
321
+ announceWorkChanged(projectId);
322
+ return { ok: true };
323
+ })
324
+
325
+ .http('work:set-config', {
326
+ data: t.Object({
327
+ ...PROJECT_FIELD,
328
+ accountId: t.String({ minLength: 1 }),
329
+ config: BINDING_CONFIG_SCHEMA
330
+ }),
331
+ response: t.Object({ ok: t.Boolean() })
332
+ }, async ({ data, conn }) => {
333
+ const { projectId } = resolveWorkProject(conn, data.projectId);
334
+ workService.setBindingConfig(projectId, data.accountId, {
335
+ transitionOnStartWork: data.config.transitionOnStartWork,
336
+ transitionOnPrOpen: data.config.transitionOnPrOpen,
337
+ // An empty template would render every branch name to the fallback,
338
+ // which looks like the template silently not working.
339
+ branchTemplate: data.config.branchTemplate.trim() || DEFAULT_BRANCH_TEMPLATE
340
+ });
341
+ announceWorkChanged(projectId);
342
+ return { ok: true };
343
+ })
344
+
345
+ .emit('work:changed', t.Object({
346
+ projectId: t.String()
347
+ }));
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Work surface routes — issues, pull requests and CI.
3
+ *
4
+ * Importing `$backend/work` is what registers the providers, so merging this
5
+ * router into the app is also what makes GitHub exist. That is deliberate: a
6
+ * surface with no routes has no adapters to register either.
7
+ */
8
+
9
+ import { createRouter } from '$shared/utils/ws-server';
10
+ import { workCrudHandler } from './crud';
11
+ import { workActionsHandler } from './actions';
12
+ import { workPullRequestHandler } from './pull-requests';
13
+
14
+ export const workRouter = createRouter()
15
+ .merge(workCrudHandler)
16
+ .merge(workActionsHandler)
17
+ .merge(workPullRequestHandler);
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Opening a pull request from the branch in front of you.
3
+ *
4
+ * work:pr-context everything the composer needs, in one call
5
+ * work:pr-draft an AI-written title and body for this branch
6
+ * work:pr-create create it
7
+ *
8
+ * `pr-create` is the only outward-facing write in this surface: it publishes to
9
+ * a repository other people watch. It is deliberately three steps rather than
10
+ * one button — context, then a draft the user reads and edits, then create —
11
+ * because "open a PR" is not an action anyone should be able to take by
12
+ * mis-clicking, and a description nobody read is worse than no description.
13
+ */
14
+
15
+ import { t } from 'elysia';
16
+ import { createRouter } from '$shared/utils/ws-server';
17
+ import {
18
+ applyConfiguredTransition,
19
+ buildPullRequestContext,
20
+ draftPullRequestDescription,
21
+ pushPullRequestHead,
22
+ workService,
23
+ readBinding
24
+ } from '$backend/work';
25
+ import { workLinkQueries } from '$backend/database/queries';
26
+ import type { EngineType } from '$shared/types/unified';
27
+ import { resolveWorkProject } from './context';
28
+ import { announceWorkChanged } from './crud';
29
+ import { MERGE_METHOD_SCHEMA, PR_CONTEXT_SCHEMA, WORK_ITEM_SCHEMA } from './schemas';
30
+ import { debug } from '$shared/utils/logger';
31
+
32
+ export const workPullRequestHandler = createRouter()
33
+ .http('work:pr-context', {
34
+ data: t.Object({
35
+ projectId: t.Optional(t.String()),
36
+ accountId: t.String({ minLength: 1 }),
37
+ /** Re-read for another local branch, when the composer's head changes. */
38
+ head: t.Optional(t.String({ maxLength: 250 }))
39
+ }),
40
+ response: PR_CONTEXT_SCHEMA
41
+ }, async ({ data, conn }) => {
42
+ // The VIEWED workspace when this is the current project: a pull request is
43
+ // opened from the branch the user is looking at, which in a worktree
44
+ // session is not the main tree. For any other project it is that
45
+ // project's own root, since this connection's worktree is not there.
46
+ const { projectId, root } = resolveWorkProject(conn, data.projectId);
47
+ return buildPullRequestContext(projectId, data.accountId, root, data.head);
48
+ })
49
+
50
+ /**
51
+ * Push the head branch, then answer with the context again.
52
+ *
53
+ * A write, but the mildest one in this file: it publishes commits the user
54
+ * already made to a branch that tracks nothing, which is the step the
55
+ * composer would otherwise send them to another panel to perform.
56
+ */
57
+ .http('work:pr-push', {
58
+ data: t.Object({
59
+ projectId: t.Optional(t.String()),
60
+ accountId: t.String({ minLength: 1 }),
61
+ head: t.String({ minLength: 1, maxLength: 250 })
62
+ }),
63
+ response: PR_CONTEXT_SCHEMA
64
+ }, async ({ data, conn }) => {
65
+ const { projectId, root } = resolveWorkProject(conn, data.projectId);
66
+ return pushPullRequestHead(projectId, data.accountId, root, data.head);
67
+ })
68
+
69
+ .http('work:pr-draft', {
70
+ data: t.Object({
71
+ projectId: t.Optional(t.String()),
72
+ accountId: t.String({ minLength: 1 }),
73
+ base: t.String({ minLength: 1 }),
74
+ head: t.String({ minLength: 1 }),
75
+ engine: t.String({ minLength: 1 }),
76
+ /** Hint only — the backend re-derives it from the engine catalog. */
77
+ providerSlug: t.Optional(t.String()),
78
+ modelId: t.String({ minLength: 1 })
79
+ }),
80
+ response: t.Object({
81
+ title: t.String(),
82
+ body: t.String()
83
+ })
84
+ }, async ({ data, conn }) => {
85
+ const { projectId, root } = resolveWorkProject(conn, data.projectId);
86
+
87
+ return draftPullRequestDescription({
88
+ projectId,
89
+ cwd: root,
90
+ base: data.base,
91
+ head: data.head,
92
+ engine: data.engine as EngineType,
93
+ ...(data.providerSlug && { providerSlug: data.providerSlug }),
94
+ modelId: data.modelId
95
+ });
96
+ })
97
+
98
+ .http('work:pr-create', {
99
+ data: t.Object({
100
+ projectId: t.Optional(t.String()),
101
+ accountId: t.String({ minLength: 1 }),
102
+ title: t.String({ minLength: 1, maxLength: 400 }),
103
+ body: t.String({ maxLength: 65_536 }),
104
+ base: t.String({ minLength: 1 }),
105
+ head: t.String({ minLength: 1 }),
106
+ isDraft: t.Boolean()
107
+ }),
108
+ response: t.Object({
109
+ item: WORK_ITEM_SCHEMA,
110
+ transitionedTo: t.Union([t.String(), t.Null()]),
111
+ transitionError: t.Union([t.String(), t.Null()])
112
+ })
113
+ }, async ({ data, conn }) => {
114
+ const { projectId } = resolveWorkProject(conn, data.projectId);
115
+
116
+ const item = await workService.createPullRequest(projectId, data.accountId, {
117
+ title: data.title,
118
+ body: data.body,
119
+ base: data.base,
120
+ head: data.head,
121
+ isDraft: data.isDraft
122
+ });
123
+
124
+ // If this branch came from an issue, the configured "on PR open"
125
+ // transition applies to THAT issue, not to the pull request we just
126
+ // created. Off by default, and a failure is reported rather than thrown —
127
+ // the pull request exists either way, and hiding it behind an error about
128
+ // a label would be the wrong headline.
129
+ const binding = readBinding(projectId, data.accountId);
130
+ const link = workLinkQueries.getByBranch(projectId, data.head);
131
+ const transition = link && link.item_kind === 'issue'
132
+ ? await applyConfiguredTransition(
133
+ projectId,
134
+ data.accountId,
135
+ 'issue',
136
+ link.item_identifier,
137
+ binding?.config.transitionOnPrOpen ?? null
138
+ )
139
+ : { applied: null, error: null };
140
+
141
+ announceWorkChanged(projectId);
142
+ debug.log('work', `Opened pull request ${item.identifier} from ${data.head}`);
143
+
144
+ return { item, transitionedTo: transition.applied, transitionError: transition.error };
145
+ })
146
+
147
+ /**
148
+ * Merge a pull request.
149
+ *
150
+ * The most outward-facing action in this surface — it changes what is on the
151
+ * default branch for everyone. The client confirms, names the method
152
+ * explicitly, and the method is validated against what the repository
153
+ * actually allows on the provider's side rather than trusted from here.
154
+ */
155
+ .http('work:pr-merge', {
156
+ data: t.Object({
157
+ projectId: t.Optional(t.String()),
158
+ accountId: t.String({ minLength: 1 }),
159
+ id: t.String({ minLength: 1 }),
160
+ method: MERGE_METHOD_SCHEMA,
161
+ title: t.Optional(t.String({ maxLength: 400 })),
162
+ message: t.Optional(t.String({ maxLength: 65_536 }))
163
+ }),
164
+ response: t.Object({ ok: t.Boolean() })
165
+ }, async ({ data, conn }) => {
166
+ const { projectId } = resolveWorkProject(conn, data.projectId);
167
+
168
+ await workService.mergePullRequest(projectId, data.accountId, data.id, {
169
+ method: data.method,
170
+ ...(data.title && { title: data.title }),
171
+ ...(data.message && { message: data.message })
172
+ });
173
+
174
+ announceWorkChanged(projectId);
175
+ debug.log('work', `Merged pull request ${data.id} with ${data.method}`);
176
+ return { ok: true };
177
+ });