@myrialabs/clopen 0.4.37 → 0.4.38
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/README.md +2 -2
- package/backend/auth/permissions.ts +14 -0
- package/backend/bootstrap.ts +16 -0
- package/backend/database/crypto/envelope.test.ts +93 -0
- package/backend/database/crypto/envelope.ts +127 -0
- package/backend/database/crypto/index.ts +22 -0
- package/backend/database/crypto/master-key.ts +129 -0
- package/backend/database/crypto/secret-columns.ts +147 -0
- package/backend/database/migrations/071_create_notes_tables.ts +90 -0
- package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
- package/backend/database/migrations/073_create_integration_tables.ts +119 -0
- package/backend/database/migrations/074_create_work_tables.ts +94 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/db-client-connection-queries.ts +23 -12
- package/backend/database/queries/engine-queries.ts +23 -10
- package/backend/database/queries/index.ts +6 -1
- package/backend/database/queries/integration-queries.ts +297 -0
- package/backend/database/queries/mcp-server-queries.ts +38 -11
- package/backend/database/queries/note-queries.test.ts +190 -0
- package/backend/database/queries/note-queries.ts +269 -0
- package/backend/database/queries/profile-queries.ts +1 -1
- package/backend/database/queries/ssh-connection-queries.ts +19 -8
- package/backend/database/queries/work-queries.ts +210 -0
- package/backend/engine/docs/artifacts.md +22 -9
- package/backend/files/os-clipboard.test.ts +209 -0
- package/backend/files/os-clipboard.ts +567 -0
- package/backend/http/integration-hooks.ts +42 -0
- package/backend/http/notes-images.test.ts +228 -0
- package/backend/http/notes-images.ts +196 -0
- package/backend/index.ts +9 -0
- package/backend/integrations/accounts.ts +229 -0
- package/backend/integrations/health.ts +146 -0
- package/backend/integrations/index.ts +36 -0
- package/backend/integrations/projections/index.ts +120 -0
- package/backend/integrations/projections/mcp-projector.test.ts +204 -0
- package/backend/integrations/projections/mcp-projector.ts +159 -0
- package/backend/integrations/projections/types.ts +51 -0
- package/backend/integrations/registry/define.ts +38 -0
- package/backend/integrations/registry/index.ts +56 -0
- package/backend/integrations/registry/providers/context7.ts +44 -0
- package/backend/integrations/registry/providers/firecrawl.ts +41 -0
- package/backend/integrations/registry/providers/github.ts +78 -0
- package/backend/integrations/registry/types.ts +96 -0
- package/backend/integrations/webhooks/gateway.ts +160 -0
- package/backend/integrations/webhooks/verify.ts +65 -0
- package/backend/mcp/README.md +25 -3
- package/backend/mcp/external/config.ts +35 -1
- package/backend/mcp/index.ts +1 -1
- package/backend/mcp/internal/config.ts +1 -1
- package/backend/notes/access.test.ts +82 -0
- package/backend/notes/access.ts +27 -0
- package/backend/notes/storage.test.ts +77 -0
- package/backend/notes/storage.ts +114 -0
- package/backend/permissions/service.ts +2 -2
- package/backend/profiles/service.ts +1 -1
- package/backend/projects/project-cleanup.ts +6 -0
- package/backend/work/bindings.ts +124 -0
- package/backend/work/index.ts +42 -0
- package/backend/work/prompts.ts +150 -0
- package/backend/work/providers/github/actions-logs.ts +180 -0
- package/backend/work/providers/github/adapter.test.ts +88 -0
- package/backend/work/providers/github/adapter.ts +1255 -0
- package/backend/work/providers/github/client.ts +280 -0
- package/backend/work/providers/github/list-range.test.ts +158 -0
- package/backend/work/providers/github/token-kind.test.ts +42 -0
- package/backend/work/pull-requests.ts +242 -0
- package/backend/work/registry.ts +27 -0
- package/backend/work/service.ts +436 -0
- package/backend/work/start-work.test.ts +71 -0
- package/backend/work/start-work.ts +232 -0
- package/backend/work/types.ts +180 -0
- package/backend/ws/files/clipboard.ts +78 -0
- package/backend/ws/files/index.ts +5 -0
- package/backend/ws/git/diff-budget.ts +70 -11
- package/backend/ws/index.ts +11 -1
- package/backend/ws/integrations/crud.ts +219 -0
- package/backend/ws/integrations/index.ts +22 -0
- package/backend/ws/mcp/crud.ts +25 -1
- package/backend/ws/notes/index.ts +278 -0
- package/backend/ws/profiles/crud.ts +1 -1
- package/backend/ws/work/actions.ts +206 -0
- package/backend/ws/work/context.ts +51 -0
- package/backend/ws/work/crud.ts +347 -0
- package/backend/ws/work/index.ts +17 -0
- package/backend/ws/work/pull-requests.ts +177 -0
- package/backend/ws/work/schemas.ts +226 -0
- package/dist/assets/{index-sJmS_CCm.js → index-CyH7fSbR.js} +1 -1
- package/dist/assets/{index-Bik9TCBi.js → index-DSHIANSy.js} +1 -1
- package/dist/assets/main-D2IOjftb.css +1 -0
- package/dist/assets/main-DM-tHKbW.js +1152 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +1 -1
- package/shared/constants/tool-icons.ts +33 -0
- package/shared/types/database/schema.ts +41 -0
- package/shared/types/integrations.ts +140 -0
- package/shared/types/stores/settings.ts +8 -0
- package/shared/types/work.ts +426 -0
- package/shared/utils/logger.ts +3 -0
- package/dist/assets/main-DLqolBI2.css +0 -1
- package/dist/assets/main-Df1I3hnw.js +0 -1096
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pull-request loop: from the branch in front of you to an open PR.
|
|
3
|
+
*
|
|
4
|
+
* This is where local git and the remote provider meet, and the rule from the
|
|
5
|
+
* spec applies literally — local state comes from `git-service`, remote state
|
|
6
|
+
* comes from the adapter, and neither is used to guess the other. In particular
|
|
7
|
+
* "has this branch been pushed" is answered by git's own upstream tracking, not
|
|
8
|
+
* by asking GitHub whether a branch with that name exists: those two questions
|
|
9
|
+
* have different answers on any repository where someone else pushed a branch
|
|
10
|
+
* with the same name.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { execGit } from '$backend/git/git-executor';
|
|
14
|
+
import { gitService } from '$backend/git/git-service';
|
|
15
|
+
import { workLinkQueries } from '$backend/database/queries';
|
|
16
|
+
import { initializeEngine } from '$backend/engine';
|
|
17
|
+
import { resolveGenerationTarget } from '$backend/engine/resolve-model';
|
|
18
|
+
import { buildBudgetedDiff, PR_DESCRIPTION_BUDGET, rangeScope } from '$backend/ws/git/diff-budget';
|
|
19
|
+
import type { EngineType } from '$shared/types/unified';
|
|
20
|
+
import type { GitBranchInfo } from '$shared/types/git';
|
|
21
|
+
import type { PullRequestContext, WorkItemLink } from '$shared/types/work';
|
|
22
|
+
import { workService } from './service';
|
|
23
|
+
import { buildPrDescriptionPrompt } from './prompts';
|
|
24
|
+
import { debug } from '$shared/utils/logger';
|
|
25
|
+
|
|
26
|
+
/** Enough to describe a branch; a 400-commit branch is not going to be summarised. */
|
|
27
|
+
const MAX_COMMITS = 30;
|
|
28
|
+
|
|
29
|
+
const PR_DESCRIPTION_SCHEMA = {
|
|
30
|
+
type: 'object',
|
|
31
|
+
additionalProperties: false,
|
|
32
|
+
properties: {
|
|
33
|
+
title: {
|
|
34
|
+
type: 'string',
|
|
35
|
+
description: 'One line, imperative mood, no trailing period'
|
|
36
|
+
},
|
|
37
|
+
body: {
|
|
38
|
+
type: 'string',
|
|
39
|
+
description: 'Markdown body describing what changed and why'
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
required: ['title', 'body']
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
interface GeneratedDescription {
|
|
46
|
+
title: string;
|
|
47
|
+
body: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function toLinkInfo(row: ReturnType<typeof workLinkQueries.getByBranch>): WorkItemLink | null {
|
|
51
|
+
if (!row) return null;
|
|
52
|
+
return {
|
|
53
|
+
itemKind: row.item_kind,
|
|
54
|
+
itemIdentifier: row.item_identifier,
|
|
55
|
+
worktreeId: row.worktree_id,
|
|
56
|
+
worktreeName: null,
|
|
57
|
+
sessionId: row.session_id,
|
|
58
|
+
branch: row.branch,
|
|
59
|
+
createdAt: row.created_at
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Commits on `head` that are not on `base`, newest first. */
|
|
64
|
+
async function commitsBetween(cwd: string, base: string, head: string): Promise<{ hash: string; subject: string }[]> {
|
|
65
|
+
// `--no-merges` because a merge commit's subject ("Merge branch 'main' into
|
|
66
|
+
// x") describes housekeeping, not the change being proposed.
|
|
67
|
+
const result = await execGit(
|
|
68
|
+
['log', '--no-merges', `--max-count=${MAX_COMMITS}`, '--format=%h%x1f%s', `${base}..${head}`],
|
|
69
|
+
cwd
|
|
70
|
+
);
|
|
71
|
+
if (result.exitCode !== 0) return [];
|
|
72
|
+
|
|
73
|
+
return result.stdout
|
|
74
|
+
.split('\n')
|
|
75
|
+
.map((line) => line.trim())
|
|
76
|
+
.filter(Boolean)
|
|
77
|
+
.map((line) => {
|
|
78
|
+
const [hash, subject] = line.split('\x1f');
|
|
79
|
+
return { hash: hash ?? '', subject: subject ?? '' };
|
|
80
|
+
})
|
|
81
|
+
.filter((entry) => entry.hash);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Local branches that could plausibly be a merge target. */
|
|
85
|
+
function baseCandidates(branches: GitBranchInfo, head: string, preferred: string): string[] {
|
|
86
|
+
const candidates = new Set<string>([preferred]);
|
|
87
|
+
for (const branch of branches.local) {
|
|
88
|
+
if (branch.name !== head) candidates.add(branch.name);
|
|
89
|
+
}
|
|
90
|
+
// A remote-only base is legitimate: the default branch often has no local
|
|
91
|
+
// copy in a fresh worktree that was created straight onto a feature branch.
|
|
92
|
+
for (const branch of branches.remote) {
|
|
93
|
+
const short = branch.name.replace(/^[^/]+\//, '');
|
|
94
|
+
if (short !== head) candidates.add(short);
|
|
95
|
+
}
|
|
96
|
+
return [...candidates];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Local branches this pull request could come FROM.
|
|
101
|
+
*
|
|
102
|
+
* Only local ones: the composer reads commits, upstream state and the linked
|
|
103
|
+
* item out of this worktree, and none of those questions have an answer for a
|
|
104
|
+
* branch that exists only on the remote.
|
|
105
|
+
*/
|
|
106
|
+
function headCandidates(branches: GitBranchInfo, current: string): string[] {
|
|
107
|
+
const candidates = new Set<string>();
|
|
108
|
+
if (current) candidates.add(current);
|
|
109
|
+
for (const branch of branches.local) candidates.add(branch.name);
|
|
110
|
+
return [...candidates];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Everything the composer needs, in one round trip.
|
|
115
|
+
*
|
|
116
|
+
* Assembled server-side because half of it is git and half is the provider, and
|
|
117
|
+
* a client doing this would need five calls and a way to keep them consistent.
|
|
118
|
+
*/
|
|
119
|
+
export async function buildPullRequestContext(
|
|
120
|
+
projectId: string,
|
|
121
|
+
accountId: string,
|
|
122
|
+
cwd: string,
|
|
123
|
+
/**
|
|
124
|
+
* Branch to open FROM, when the user picked one other than the checked-out
|
|
125
|
+
* branch. Everything else is then answered about that branch — its commits,
|
|
126
|
+
* its upstream, the item it was started from — because a composer that lets
|
|
127
|
+
* you choose a head and keeps describing a different one is worse than one
|
|
128
|
+
* that does not let you choose at all.
|
|
129
|
+
*/
|
|
130
|
+
requestedHead?: string
|
|
131
|
+
): Promise<PullRequestContext> {
|
|
132
|
+
const branches = await gitService.getBranches(cwd);
|
|
133
|
+
const heads = headCandidates(branches, branches.detached ? '' : branches.current);
|
|
134
|
+
|
|
135
|
+
const head = requestedHead && heads.includes(requestedHead) ? requestedHead : branches.current;
|
|
136
|
+
if (!head || (branches.detached && !requestedHead)) {
|
|
137
|
+
throw new Error('This worktree is not on a branch, so there is nothing to open a pull request from');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Always the repository's default branch, even when that is also the head.
|
|
141
|
+
// Guessing a different base to avoid the clash would propose a merge nobody
|
|
142
|
+
// asked for; the composer states the clash and lets the user resolve it,
|
|
143
|
+
// which it can now do because both sides are a dropdown.
|
|
144
|
+
const base = await workService.defaultBranch(projectId, accountId);
|
|
145
|
+
|
|
146
|
+
const pushTarget = await gitService.getPushTarget(cwd, head);
|
|
147
|
+
const existing = await workService.findPullRequestForBranch(projectId, accountId, head).catch((error) => {
|
|
148
|
+
// A lookup failure must not block the composer — the worst case is
|
|
149
|
+
// offering to create a PR that already exists, which the provider then
|
|
150
|
+
// rejects with a clear message of its own.
|
|
151
|
+
debug.warn('work', 'Could not check for an existing pull request:', error);
|
|
152
|
+
return null;
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
head,
|
|
157
|
+
base,
|
|
158
|
+
baseCandidates: baseCandidates(branches, head, base),
|
|
159
|
+
headCandidates: heads,
|
|
160
|
+
isPushed: pushTarget.hasUpstream,
|
|
161
|
+
pushRemote: pushTarget.remote,
|
|
162
|
+
commits: await commitsBetween(cwd, base, head),
|
|
163
|
+
existing,
|
|
164
|
+
linkedItem: toLinkInfo(workLinkQueries.getByBranch(projectId, head))
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Push the branch a pull request would come from, then re-read the context.
|
|
170
|
+
*
|
|
171
|
+
* The composer used to state the problem and send the user to the Git panel,
|
|
172
|
+
* which is a correct instruction and a poor one: the branch is named right
|
|
173
|
+
* there, the destination is the one git already records, and coming back means
|
|
174
|
+
* re-opening the composer and re-typing whatever was in the form. Pushing is
|
|
175
|
+
* the only missing step and it is not a destructive one — `-u` on a branch that
|
|
176
|
+
* tracks nothing creates an upstream, it cannot overwrite one.
|
|
177
|
+
*/
|
|
178
|
+
export async function pushPullRequestHead(
|
|
179
|
+
projectId: string,
|
|
180
|
+
accountId: string,
|
|
181
|
+
cwd: string,
|
|
182
|
+
head: string
|
|
183
|
+
): Promise<PullRequestContext> {
|
|
184
|
+
const result = await gitService.push(cwd, 'origin', head, false);
|
|
185
|
+
if (!result.success) throw new Error(result.message || `Could not push ${head}`);
|
|
186
|
+
|
|
187
|
+
debug.log('work', `Pushed ${head} for a pull request`);
|
|
188
|
+
return buildPullRequestContext(projectId, accountId, cwd, head);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Draft a title and body from the branch's diff.
|
|
193
|
+
*
|
|
194
|
+
* Reuses the git generator's model override (Settings → Models → Git) rather
|
|
195
|
+
* than adding a fourth "which model writes this" setting: writing a PR
|
|
196
|
+
* description is the same kind of work as writing a commit message, and a user
|
|
197
|
+
* who tuned one has already answered for the other.
|
|
198
|
+
*/
|
|
199
|
+
export async function draftPullRequestDescription(input: {
|
|
200
|
+
projectId: string;
|
|
201
|
+
cwd: string;
|
|
202
|
+
base: string;
|
|
203
|
+
head: string;
|
|
204
|
+
engine: EngineType;
|
|
205
|
+
providerSlug?: string;
|
|
206
|
+
modelId: string;
|
|
207
|
+
}): Promise<GeneratedDescription> {
|
|
208
|
+
const scope = rangeScope(input.base, input.head);
|
|
209
|
+
const diff = await buildBudgetedDiff(input.cwd, PR_DESCRIPTION_BUDGET, scope);
|
|
210
|
+
if (diff.isEmpty) {
|
|
211
|
+
throw new Error(`There are no ${scope.emptyLabel} to describe`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const engine = await initializeEngine(input.engine);
|
|
215
|
+
if (!engine.generateStructured) {
|
|
216
|
+
throw new Error(`Engine "${input.engine}" does not support structured generation`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const link = workLinkQueries.getByBranch(input.projectId, input.head);
|
|
220
|
+
const prompt = buildPrDescriptionPrompt({
|
|
221
|
+
branch: input.head,
|
|
222
|
+
base: input.base,
|
|
223
|
+
commits: await commitsBetween(input.cwd, input.base, input.head),
|
|
224
|
+
linkedItem: link ? { identifier: link.item_identifier, title: link.item_title } : null,
|
|
225
|
+
diff: diff.text,
|
|
226
|
+
diffTruncated: diff.truncated
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const target = await resolveGenerationTarget(engine, input.modelId, input.providerSlug);
|
|
230
|
+
debug.log('work', `Drafting a PR description via ${input.engine}/${target.providerSlug}/${target.modelId}`);
|
|
231
|
+
|
|
232
|
+
const result = await engine.generateStructured<GeneratedDescription>({
|
|
233
|
+
prompt,
|
|
234
|
+
providerSlug: target.providerSlug,
|
|
235
|
+
modelId: target.modelId,
|
|
236
|
+
schema: PR_DESCRIPTION_SCHEMA,
|
|
237
|
+
projectPath: input.cwd,
|
|
238
|
+
...(target.accountId != null && { accountId: target.accountId })
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
return { title: (result.title ?? '').trim(), body: (result.body ?? '').trim() };
|
|
242
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter registry for the Issues & PRs surface.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately dumb: a map from provider id to adapter, filled at module load.
|
|
5
|
+
* Adding GitLab is one file in `providers/` plus one line in `index.ts` — the
|
|
6
|
+
* same shape as the internal-MCP `defineServer()` pattern and the integration
|
|
7
|
+
* provider registry, so there is one thing to learn rather than three.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { WorkProviderAdapter } from './types';
|
|
11
|
+
|
|
12
|
+
const adapters = new Map<string, WorkProviderAdapter>();
|
|
13
|
+
|
|
14
|
+
export function registerWorkProvider(adapter: WorkProviderAdapter): void {
|
|
15
|
+
if (adapters.has(adapter.provider)) {
|
|
16
|
+
throw new Error(`An issue adapter for "${adapter.provider}" is already registered`);
|
|
17
|
+
}
|
|
18
|
+
adapters.set(adapter.provider, adapter);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getWorkAdapter(provider: string): WorkProviderAdapter | null {
|
|
22
|
+
return adapters.get(provider) ?? null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function listWorkAdapters(): WorkProviderAdapter[] {
|
|
26
|
+
return [...adapters.values()];
|
|
27
|
+
}
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Issues & PRs surface, provider-agnostic.
|
|
3
|
+
*
|
|
4
|
+
* Every route in `backend/ws/issues/` goes through here, and nothing here
|
|
5
|
+
* mentions GitHub. The service resolves which connected accounts can serve the
|
|
6
|
+
* current project, assembles the adapter context (account + decoded credential
|
|
7
|
+
* + binding), and hands the call on.
|
|
8
|
+
*
|
|
9
|
+
* The credential is decoded HERE and dies here. No function in this file
|
|
10
|
+
* returns it, and the shapes the routes serialise (`WorkSource`, `WorkItem`)
|
|
11
|
+
* have nowhere to put it.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
integrationAccountQueries,
|
|
16
|
+
workBindingQueries,
|
|
17
|
+
workLinkQueries,
|
|
18
|
+
parseBindingConfig,
|
|
19
|
+
type IntegrationAccountRow
|
|
20
|
+
} from '$backend/database/queries';
|
|
21
|
+
import { getProvider } from '$backend/integrations';
|
|
22
|
+
import type {
|
|
23
|
+
CheckLogBundle,
|
|
24
|
+
CheckRun,
|
|
25
|
+
CommentSource,
|
|
26
|
+
CommitDetail,
|
|
27
|
+
CommitRef,
|
|
28
|
+
FileContent,
|
|
29
|
+
FileChange,
|
|
30
|
+
WorkBindingConfig,
|
|
31
|
+
WorkSource,
|
|
32
|
+
MergeMethod,
|
|
33
|
+
PullRequestDraft,
|
|
34
|
+
WorkItem,
|
|
35
|
+
WorkItemComment,
|
|
36
|
+
WorkItemDetail,
|
|
37
|
+
WorkItemKind,
|
|
38
|
+
WorkItemPage,
|
|
39
|
+
WorkItemQuery,
|
|
40
|
+
WorkItemStateOption
|
|
41
|
+
} from '$shared/types/work';
|
|
42
|
+
import { getWorkAdapter } from './registry';
|
|
43
|
+
import { readBinding, resolveBinding, suggestLocators, toBindingInfo } from './bindings';
|
|
44
|
+
import type { WorkContext, WorkProviderAdapter } from './types';
|
|
45
|
+
import { debug } from '$shared/utils/logger';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Accounts that can serve work items here.
|
|
49
|
+
*
|
|
50
|
+
* Three filters, in order of what they mean: the account offers `issues`, it is
|
|
51
|
+
* switched on, and there is code registered that knows how to talk to it. The
|
|
52
|
+
* third is why a provider declaring the capability before its adapter exists is
|
|
53
|
+
* harmless — it simply does not appear.
|
|
54
|
+
*/
|
|
55
|
+
function accountsFor(projectId: string): { account: IntegrationAccountRow; adapter: WorkProviderAdapter }[] {
|
|
56
|
+
return integrationAccountQueries
|
|
57
|
+
.getAll()
|
|
58
|
+
.filter((account) => account.is_enabled === 1)
|
|
59
|
+
.filter((account) => integrationAccountQueries.capabilitiesOf(account).includes('work'))
|
|
60
|
+
// A project-bound account belongs to that project alone; a null binding
|
|
61
|
+
// means "available everywhere". Same rule the webhook gateway uses.
|
|
62
|
+
.filter((account) => account.project_id === null || account.project_id === projectId)
|
|
63
|
+
.flatMap((account) => {
|
|
64
|
+
const adapter = getWorkAdapter(account.provider);
|
|
65
|
+
return adapter ? [{ account, adapter }] : [];
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function contextOf(
|
|
70
|
+
account: IntegrationAccountRow,
|
|
71
|
+
projectId: string,
|
|
72
|
+
binding: NonNullable<ReturnType<typeof readBinding>>
|
|
73
|
+
): WorkContext {
|
|
74
|
+
return {
|
|
75
|
+
projectId,
|
|
76
|
+
account,
|
|
77
|
+
credentials: integrationAccountQueries.credentialsOf(account),
|
|
78
|
+
binding
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const workService = {
|
|
83
|
+
/**
|
|
84
|
+
* Every source the panel can show, with its binding already resolved.
|
|
85
|
+
*
|
|
86
|
+
* Detection runs here, which is why this is async: opening the panel on a
|
|
87
|
+
* project that has never been bound is the moment we have both the account
|
|
88
|
+
* and the repository in hand, and asking the user to bind manually at that
|
|
89
|
+
* point would be asking them to retype their git remote.
|
|
90
|
+
*/
|
|
91
|
+
async listSources(projectId: string, projectRoot: string): Promise<WorkSource[]> {
|
|
92
|
+
const sources: WorkSource[] = [];
|
|
93
|
+
|
|
94
|
+
for (const { account, adapter } of accountsFor(projectId)) {
|
|
95
|
+
const provider = getProvider(account.provider);
|
|
96
|
+
const credentials = integrationAccountQueries.credentialsOf(account);
|
|
97
|
+
|
|
98
|
+
let binding = readBinding(projectId, account.id);
|
|
99
|
+
if (!binding) {
|
|
100
|
+
try {
|
|
101
|
+
binding = await resolveBinding(adapter, account, projectId, projectRoot);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
debug.warn('work', `Binding detection failed for ${account.provider}:`, error);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
sources.push({
|
|
108
|
+
accountId: account.id,
|
|
109
|
+
provider: account.provider,
|
|
110
|
+
providerName: provider?.name ?? account.provider,
|
|
111
|
+
label: account.label,
|
|
112
|
+
// Resolved once per source. Every comment's edit and delete
|
|
113
|
+
// controls depend on knowing who the credential belongs to, and
|
|
114
|
+
// asking per comment would be one request per row.
|
|
115
|
+
viewer: adapter.viewer
|
|
116
|
+
? await adapter.viewer({ projectId, account, credentials }).catch(() => null)
|
|
117
|
+
: null,
|
|
118
|
+
capabilities: adapter.capabilities,
|
|
119
|
+
binding: toBindingInfo(adapter, binding),
|
|
120
|
+
suggestedLocators: await suggestLocators(adapter, credentials, projectRoot).catch(() => []),
|
|
121
|
+
status: account.status,
|
|
122
|
+
statusDetail: account.status_detail
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return sources;
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Adapter + context for one account, or a clear error.
|
|
131
|
+
*
|
|
132
|
+
* "Not bound" is separated from "not connected" because they need different
|
|
133
|
+
* answers: one is a repository picker, the other is a connect dialog.
|
|
134
|
+
*/
|
|
135
|
+
require(projectId: string, accountId: string): { adapter: WorkProviderAdapter; context: WorkContext } {
|
|
136
|
+
const account = integrationAccountQueries.getById(accountId);
|
|
137
|
+
if (!account) throw new Error('That integration account is no longer connected');
|
|
138
|
+
if (account.is_enabled !== 1) throw new Error(`${account.label} is switched off in Settings → Integrations`);
|
|
139
|
+
|
|
140
|
+
const adapter = getWorkAdapter(account.provider);
|
|
141
|
+
if (!adapter) throw new Error(`Clopen has no issues adapter for "${account.provider}"`);
|
|
142
|
+
|
|
143
|
+
const binding = readBinding(projectId, account.id);
|
|
144
|
+
if (!binding) throw new Error('This project is not pointed at a repository yet');
|
|
145
|
+
|
|
146
|
+
return { adapter, context: contextOf(account, projectId, binding) };
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
/** How to print the bound remote project, for prompts and headings. */
|
|
150
|
+
describeBinding(projectId: string, accountId: string): string | null {
|
|
151
|
+
const account = integrationAccountQueries.getById(accountId);
|
|
152
|
+
const adapter = account ? getWorkAdapter(account.provider) : null;
|
|
153
|
+
const binding = readBinding(projectId, accountId);
|
|
154
|
+
if (!adapter || !binding) return null;
|
|
155
|
+
return adapter.describeLocator(binding.locator);
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
async list(projectId: string, accountId: string, query: WorkItemQuery): Promise<WorkItemPage> {
|
|
159
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
160
|
+
return adapter.list(context, query);
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
async get(projectId: string, accountId: string, kind: WorkItemKind, id: string): Promise<WorkItemDetail> {
|
|
164
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
165
|
+
return adapter.get(context, kind, id);
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
async listStates(projectId: string, accountId: string, kind: WorkItemKind): Promise<WorkItemStateOption[]> {
|
|
169
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
170
|
+
return adapter.listStates(context, kind);
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
async comment(
|
|
174
|
+
projectId: string,
|
|
175
|
+
accountId: string,
|
|
176
|
+
kind: WorkItemKind,
|
|
177
|
+
id: string,
|
|
178
|
+
body: string
|
|
179
|
+
): Promise<WorkItemComment> {
|
|
180
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
181
|
+
if (!adapter.capabilities.comments) throw new Error('This provider does not accept comments');
|
|
182
|
+
return adapter.comment(context, kind, id, body);
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
async transition(
|
|
186
|
+
projectId: string,
|
|
187
|
+
accountId: string,
|
|
188
|
+
kind: WorkItemKind,
|
|
189
|
+
id: string,
|
|
190
|
+
state: string
|
|
191
|
+
): Promise<void> {
|
|
192
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
193
|
+
if (!adapter.capabilities.transitions) throw new Error('This provider does not support state changes');
|
|
194
|
+
await adapter.transition(context, kind, id, state);
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
async listChecks(projectId: string, accountId: string, branch: string): Promise<CheckRun[]> {
|
|
198
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
199
|
+
if (!adapter.listChecks) return [];
|
|
200
|
+
return adapter.listChecks(context, branch);
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
async fetchCheckLogs(projectId: string, accountId: string, runId: string): Promise<CheckLogBundle> {
|
|
204
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
205
|
+
if (!adapter.fetchCheckLogs) throw new Error('This provider does not expose CI logs');
|
|
206
|
+
return adapter.fetchCheckLogs(context, runId);
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
async updateItem(
|
|
210
|
+
projectId: string,
|
|
211
|
+
accountId: string,
|
|
212
|
+
kind: WorkItemKind,
|
|
213
|
+
id: string,
|
|
214
|
+
patch: { title?: string; body?: string }
|
|
215
|
+
): Promise<void> {
|
|
216
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
217
|
+
if (!adapter.updateItem) throw new Error('This provider cannot edit items');
|
|
218
|
+
await adapter.updateItem(context, kind, id, patch);
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
async listLabels(projectId: string, accountId: string): Promise<string[]> {
|
|
222
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
223
|
+
if (!adapter.listLabels) return [];
|
|
224
|
+
return adapter.listLabels(context);
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
async updateComment(
|
|
228
|
+
projectId: string,
|
|
229
|
+
accountId: string,
|
|
230
|
+
comment: { id: string; source: CommentSource },
|
|
231
|
+
body: string
|
|
232
|
+
): Promise<WorkItemComment> {
|
|
233
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
234
|
+
if (!adapter.updateComment) throw new Error('This provider cannot edit comments');
|
|
235
|
+
return adapter.updateComment(context, comment, body);
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
async deleteComment(
|
|
239
|
+
projectId: string,
|
|
240
|
+
accountId: string,
|
|
241
|
+
comment: { id: string; source: CommentSource }
|
|
242
|
+
): Promise<void> {
|
|
243
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
244
|
+
if (!adapter.deleteComment) throw new Error('This provider cannot delete comments');
|
|
245
|
+
await adapter.deleteComment(context, comment);
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
async mergePullRequest(
|
|
249
|
+
projectId: string,
|
|
250
|
+
accountId: string,
|
|
251
|
+
id: string,
|
|
252
|
+
options: { method: MergeMethod; title?: string; message?: string }
|
|
253
|
+
): Promise<void> {
|
|
254
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
255
|
+
if (!adapter.mergePullRequest) throw new Error('This provider cannot merge pull requests');
|
|
256
|
+
await adapter.mergePullRequest(context, id, options);
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
async listCommits(projectId: string, accountId: string, id: string): Promise<CommitRef[]> {
|
|
260
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
261
|
+
if (!adapter.listCommits) return [];
|
|
262
|
+
return adapter.listCommits(context, id);
|
|
263
|
+
},
|
|
264
|
+
|
|
265
|
+
async listFiles(projectId: string, accountId: string, id: string): Promise<FileChange[]> {
|
|
266
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
267
|
+
if (!adapter.listFiles) return [];
|
|
268
|
+
return adapter.listFiles(context, id);
|
|
269
|
+
},
|
|
270
|
+
|
|
271
|
+
async commitDetail(projectId: string, accountId: string, sha: string): Promise<CommitDetail> {
|
|
272
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
273
|
+
if (!adapter.commitDetail) throw new Error('This provider cannot open a commit');
|
|
274
|
+
return adapter.commitDetail(context, sha);
|
|
275
|
+
},
|
|
276
|
+
|
|
277
|
+
async fetchFileContent(
|
|
278
|
+
projectId: string,
|
|
279
|
+
accountId: string,
|
|
280
|
+
path: string,
|
|
281
|
+
ref: string,
|
|
282
|
+
blobSha?: string
|
|
283
|
+
): Promise<FileContent> {
|
|
284
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
285
|
+
if (!adapter.fetchFileContent) throw new Error('This provider cannot read file contents');
|
|
286
|
+
return adapter.fetchFileContent(context, path, ref, blobSha);
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
async listAssignees(projectId: string, accountId: string): Promise<string[]> {
|
|
290
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
291
|
+
if (!adapter.listAssignees) return [];
|
|
292
|
+
return adapter.listAssignees(context);
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
async setAssignees(
|
|
296
|
+
projectId: string,
|
|
297
|
+
accountId: string,
|
|
298
|
+
kind: WorkItemKind,
|
|
299
|
+
id: string,
|
|
300
|
+
logins: string[]
|
|
301
|
+
): Promise<void> {
|
|
302
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
303
|
+
if (!adapter.setAssignees) throw new Error('This provider cannot change assignees');
|
|
304
|
+
await adapter.setAssignees(context, kind, id, logins);
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
async createIssue(
|
|
308
|
+
projectId: string,
|
|
309
|
+
accountId: string,
|
|
310
|
+
draft: { title: string; body: string; assignees: string[]; labels: string[] }
|
|
311
|
+
): Promise<WorkItem> {
|
|
312
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
313
|
+
if (!adapter.createIssue) throw new Error('This provider cannot create issues');
|
|
314
|
+
return adapter.createIssue(context, draft);
|
|
315
|
+
},
|
|
316
|
+
|
|
317
|
+
async createPullRequest(
|
|
318
|
+
projectId: string,
|
|
319
|
+
accountId: string,
|
|
320
|
+
draft: PullRequestDraft
|
|
321
|
+
): Promise<WorkItem> {
|
|
322
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
323
|
+
if (!adapter.createPullRequest) throw new Error('This provider cannot open pull requests');
|
|
324
|
+
|
|
325
|
+
const created = await adapter.createPullRequest(context, draft);
|
|
326
|
+
|
|
327
|
+
// Record the PR against the branch, so re-opening the composer on this
|
|
328
|
+
// branch finds it instead of offering to create a second one.
|
|
329
|
+
workLinkQueries.upsert({
|
|
330
|
+
projectId,
|
|
331
|
+
accountId,
|
|
332
|
+
itemKind: 'pull-request',
|
|
333
|
+
itemIdentifier: created.identifier,
|
|
334
|
+
itemTitle: created.title,
|
|
335
|
+
itemUrl: created.url,
|
|
336
|
+
branch: draft.head
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
return created;
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
async findPullRequestForBranch(
|
|
343
|
+
projectId: string,
|
|
344
|
+
accountId: string,
|
|
345
|
+
branch: string
|
|
346
|
+
): Promise<WorkItem | null> {
|
|
347
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
348
|
+
if (!adapter.findPullRequestForBranch) return null;
|
|
349
|
+
return adapter.findPullRequestForBranch(context, branch);
|
|
350
|
+
},
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* The branch a pull request should target.
|
|
354
|
+
*
|
|
355
|
+
* Cached on the binding after the first lookup: the default branch of a
|
|
356
|
+
* repository changes about once in its lifetime, and paying an API call per
|
|
357
|
+
* PR composer open to re-learn it is the kind of cost that quietly eats a
|
|
358
|
+
* rate limit.
|
|
359
|
+
*/
|
|
360
|
+
async defaultBranch(projectId: string, accountId: string): Promise<string> {
|
|
361
|
+
const { adapter, context } = this.require(projectId, accountId);
|
|
362
|
+
if (context.binding.defaultBranch) return context.binding.defaultBranch;
|
|
363
|
+
|
|
364
|
+
const branch = await adapter.defaultBranch(context);
|
|
365
|
+
workBindingQueries.setDefaultBranch(projectId, accountId, branch);
|
|
366
|
+
return branch;
|
|
367
|
+
},
|
|
368
|
+
|
|
369
|
+
/** Point this project at a different remote project. */
|
|
370
|
+
setBinding(projectId: string, accountId: string, locator: string): void {
|
|
371
|
+
const account = integrationAccountQueries.getById(accountId);
|
|
372
|
+
if (!account) throw new Error('That integration account is no longer connected');
|
|
373
|
+
|
|
374
|
+
const existing = workBindingQueries.get(projectId, accountId);
|
|
375
|
+
workBindingQueries.upsert({
|
|
376
|
+
projectId,
|
|
377
|
+
accountId,
|
|
378
|
+
locator: locator.trim(),
|
|
379
|
+
detected: false,
|
|
380
|
+
// The cached default branch belongs to the OLD repository. Keeping it
|
|
381
|
+
// would target a pull request at a branch that may not exist here.
|
|
382
|
+
defaultBranch: null,
|
|
383
|
+
config: parseBindingConfig(existing?.config_json ?? null)
|
|
384
|
+
});
|
|
385
|
+
debug.log('work', `Project ${projectId} now points at ${account.provider}:${locator}`);
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
setBindingConfig(projectId: string, accountId: string, config: WorkBindingConfig): void {
|
|
389
|
+
const existing = workBindingQueries.get(projectId, accountId);
|
|
390
|
+
if (!existing) throw new Error('This project is not pointed at a repository yet');
|
|
391
|
+
|
|
392
|
+
workBindingQueries.upsert({
|
|
393
|
+
projectId,
|
|
394
|
+
accountId,
|
|
395
|
+
locator: existing.locator,
|
|
396
|
+
detected: existing.detected === 1,
|
|
397
|
+
defaultBranch: existing.default_branch,
|
|
398
|
+
config
|
|
399
|
+
});
|
|
400
|
+
},
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Health for an account whose capability is `issues`.
|
|
404
|
+
*
|
|
405
|
+
* Registered with the integrations hub so the account's status strip means
|
|
406
|
+
* something for a provider that projects no MCP row — without this, GitHub
|
|
407
|
+
* would forever read "Nothing to probe yet".
|
|
408
|
+
*/
|
|
409
|
+
async probe(
|
|
410
|
+
accountId: string,
|
|
411
|
+
projectId: string | null
|
|
412
|
+
): Promise<{ status: 'ok' | 'needs_auth' | 'needs_config' | 'error'; detail: string | null } | null> {
|
|
413
|
+
const account = integrationAccountQueries.getById(accountId);
|
|
414
|
+
if (!account) return null;
|
|
415
|
+
|
|
416
|
+
// Null means "not mine to answer", and the hub falls back to the MCP
|
|
417
|
+
// probe. An account whose `issues` capability is switched off is exactly
|
|
418
|
+
// that case: its GitHub token may still be projecting agent tools.
|
|
419
|
+
if (!integrationAccountQueries.capabilitiesOf(account).includes('work')) return null;
|
|
420
|
+
|
|
421
|
+
const adapter = getWorkAdapter(account.provider);
|
|
422
|
+
if (!adapter) return null;
|
|
423
|
+
|
|
424
|
+
// An account bound to a project probes against that project's repository;
|
|
425
|
+
// an unbound one can only prove the credential itself is good.
|
|
426
|
+
const binding = projectId ? readBinding(projectId, accountId) : null;
|
|
427
|
+
return adapter.probe(
|
|
428
|
+
{
|
|
429
|
+
projectId: projectId ?? '',
|
|
430
|
+
account,
|
|
431
|
+
credentials: integrationAccountQueries.credentialsOf(account)
|
|
432
|
+
},
|
|
433
|
+
binding
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
};
|