@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.
- package/README.md +2 -2
- package/backend/auth/permissions.ts +39 -0
- package/backend/bootstrap.ts +16 -0
- package/backend/chat/stream-manager.ts +1 -1
- 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 +151 -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/075_create_deploy_bindings.ts +54 -0
- package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
- package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
- package/backend/database/migrations/index.ts +49 -0
- package/backend/database/queries/db-client-connection-queries.ts +42 -12
- package/backend/database/queries/deploy-queries.ts +124 -0
- package/backend/database/queries/engine-queries.ts +23 -10
- package/backend/database/queries/graph-queries.ts +291 -247
- package/backend/database/queries/index.ts +10 -1
- package/backend/database/queries/integration-db-link-queries.ts +168 -0
- package/backend/database/queries/integration-queries.ts +325 -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/db-client/integrations/index.ts +37 -0
- package/backend/db-client/integrations/links.ts +533 -0
- package/backend/db-client/integrations/projector.test.ts +280 -0
- package/backend/db-client/integrations/projector.ts +256 -0
- package/backend/db-client/integrations/supabase-service.ts +266 -0
- package/backend/db-client/providers/registry.ts +27 -0
- package/backend/db-client/providers/supabase/adapter.ts +500 -0
- package/backend/db-client/providers/supabase/api-types.ts +92 -0
- package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
- package/backend/db-client/providers/supabase/client.ts +259 -0
- package/backend/db-client/providers/supabase/features.ts +502 -0
- package/backend/db-client/providers/supabase/local.test.ts +78 -0
- package/backend/db-client/providers/supabase/local.ts +137 -0
- package/backend/db-client/providers/types.ts +137 -0
- package/backend/deployments/bindings.test.ts +43 -0
- package/backend/deployments/bindings.ts +216 -0
- package/backend/deployments/index.ts +53 -0
- package/backend/deployments/log-streams.ts +214 -0
- package/backend/deployments/prompts.ts +70 -0
- package/backend/deployments/providers/vercel/adapter.ts +757 -0
- package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
- package/backend/deployments/providers/vercel/build-logs.ts +211 -0
- package/backend/deployments/providers/vercel/client.ts +203 -0
- package/backend/deployments/providers/vercel/list.test.ts +624 -0
- package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
- package/backend/deployments/providers/vercel/normalise.ts +113 -0
- package/backend/deployments/providers/vercel/types.ts +104 -0
- package/backend/deployments/registry.ts +27 -0
- package/backend/deployments/service.ts +458 -0
- package/backend/deployments/types.ts +278 -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 +13 -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 +0 -0
- package/backend/integrations/projections/mcp-projector.test.ts +204 -0
- package/backend/integrations/projections/mcp-projector.ts +163 -0
- package/backend/integrations/projections/types.ts +63 -0
- package/backend/integrations/registry/define.ts +38 -0
- package/backend/integrations/registry/index.ts +60 -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/providers/supabase.ts +61 -0
- package/backend/integrations/registry/providers/vercel.ts +46 -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/mcp/internal/servers/memory-graph/description.ts +3 -3
- package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
- package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
- package/backend/memory/README.md +57 -34
- package/backend/memory/compose.test.ts +3 -3
- package/backend/memory/compose.ts +6 -9
- package/backend/memory/config.ts +0 -5
- package/backend/memory/consolidate.ts +1 -2
- package/backend/memory/context.test.ts +8 -54
- package/backend/memory/context.ts +19 -25
- package/backend/memory/extract/episodic.ts +10 -12
- package/backend/memory/extract/index.ts +23 -52
- package/backend/memory/extract/scheduler.ts +3 -6
- package/backend/memory/index.ts +6 -5
- package/backend/memory/invalidate.ts +36 -48
- package/backend/memory/layout.ts +319 -130
- package/backend/memory/retention.ts +12 -42
- package/backend/memory/retrieval.eval.test.ts +2 -2
- package/backend/memory/retrieval.test.ts +228 -183
- package/backend/memory/retrieval.ts +22 -21
- package/backend/memory/revise.test.ts +62 -116
- package/backend/memory/revise.ts +0 -1
- package/backend/memory/view.ts +8 -5
- 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/snapshot/helpers.ts +35 -41
- package/backend/snapshot/snapshot-service.ts +90 -0
- package/backend/snapshot/turn-changes.test.ts +163 -0
- package/backend/snapshot/turn-changes.ts +131 -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/db-client/accounts.ts +225 -0
- package/backend/ws/db-client/connections.ts +34 -3
- package/backend/ws/db-client/context.ts +37 -0
- package/backend/ws/db-client/index.ts +20 -1
- package/backend/ws/db-client/supabase.ts +188 -0
- package/backend/ws/deployments/actions.ts +360 -0
- package/backend/ws/deployments/context.ts +43 -0
- package/backend/ws/deployments/crud.ts +152 -0
- package/backend/ws/deployments/index.ts +15 -0
- package/backend/ws/deployments/schemas.ts +183 -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 +16 -2
- 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/memory/crud.ts +4 -16
- package/backend/ws/notes/index.ts +278 -0
- package/backend/ws/profiles/crud.ts +1 -1
- package/backend/ws/snapshot/changes.ts +279 -0
- package/backend/ws/snapshot/index.ts +4 -3
- 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-BrF6nqld.js} +1 -1
- package/dist/assets/{index-Bik9TCBi.js → index-C8Gi-b6U.js} +1 -1
- package/dist/assets/main-Bd3gb79F.css +1 -0
- package/dist/assets/main-Bo1hD7mt.js +1191 -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 +42 -0
- package/shared/types/database/schema.ts +41 -0
- package/shared/types/db-client/connection.ts +6 -0
- package/shared/types/db-client/index.ts +2 -0
- package/shared/types/db-client/integrations.ts +174 -0
- package/shared/types/db-client/supabase.ts +131 -0
- package/shared/types/deployments.ts +382 -0
- package/shared/types/integrations.ts +140 -0
- package/shared/types/memory.ts +34 -51
- package/shared/types/stores/settings.ts +8 -0
- package/shared/types/ui/components.ts +1 -1
- package/shared/types/work.ts +426 -0
- package/shared/utils/diff-calculator.test.ts +89 -0
- package/shared/utils/diff-calculator.ts +30 -65
- package/shared/utils/logger.ts +4 -0
- package/backend/memory/extract/languages.ts +0 -176
- package/backend/memory/extract/structural.test.ts +0 -215
- package/backend/memory/extract/structural.ts +0 -364
- package/backend/ws/snapshot/content.ts +0 -85
- package/dist/assets/main-DLqolBI2.css +0 -1
- package/dist/assets/main-Df1I3hnw.js +0 -1096
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Linking a remote database into DB Client.
|
|
3
|
+
*
|
|
4
|
+
* The asynchronous half of the projection: this is where a provider is asked
|
|
5
|
+
* where a database answers, and the answer is stored on the link so the
|
|
6
|
+
* synchronous projector never has to ask again. Every mutation here ends in
|
|
7
|
+
* `reproject()`, which is what keeps the connection list derived rather than
|
|
8
|
+
* merely created once and then drifting.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
dbClientConnectionQueries,
|
|
13
|
+
integrationAccountQueries,
|
|
14
|
+
integrationDbLinkQueries,
|
|
15
|
+
integrationProjectionQueries,
|
|
16
|
+
type IntegrationAccountRow,
|
|
17
|
+
type IntegrationDbLinkRow
|
|
18
|
+
} from '$backend/database/queries';
|
|
19
|
+
import { getProvider, reproject } from '$backend/integrations';
|
|
20
|
+
import type {
|
|
21
|
+
DbAccountLinkInfo,
|
|
22
|
+
DbClientManagedBy,
|
|
23
|
+
DbProviderCreateOptions,
|
|
24
|
+
DbProviderInfo,
|
|
25
|
+
DbRemoteDatabase
|
|
26
|
+
} from '$shared/types/db-client';
|
|
27
|
+
import { connectionManager } from '../connection-manager';
|
|
28
|
+
import { getDbProvider, listDbProviders } from '../providers/registry';
|
|
29
|
+
import type { DbProviderAdapter, DbProviderContext } from '../providers/types';
|
|
30
|
+
import { connectionIdOf } from './projector';
|
|
31
|
+
import { debug } from '$shared/utils/logger';
|
|
32
|
+
|
|
33
|
+
function requireAccount(accountId: string): IntegrationAccountRow {
|
|
34
|
+
const account = integrationAccountQueries.getById(accountId);
|
|
35
|
+
if (!account) throw new Error('Integration account not found');
|
|
36
|
+
return account;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function requireAdapter(provider: string): DbProviderAdapter {
|
|
40
|
+
const adapter = getDbProvider(provider);
|
|
41
|
+
if (!adapter) throw new Error(`"${provider}" is not a database provider`);
|
|
42
|
+
return adapter;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function contextFor(account: IntegrationAccountRow): DbProviderContext {
|
|
46
|
+
return {
|
|
47
|
+
accountId: account.id,
|
|
48
|
+
credentials: integrationAccountQueries.credentialsOf(account)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Re-project, then drop the live adapters for the rows it touched.
|
|
54
|
+
*
|
|
55
|
+
* `connectionManager` keeps an open driver per connection, and it was opened
|
|
56
|
+
* with the settings the row had at the time. Rotating a password or switching
|
|
57
|
+
* to a different pooler mode rewrites the row but leaves that adapter
|
|
58
|
+
* connected — so the change appears to have worked while every query keeps
|
|
59
|
+
* going to the old endpoint with the old credential until the pool happens to
|
|
60
|
+
* sweep it. `db-client:update` already releases for exactly this reason; the
|
|
61
|
+
* projection path has to as well.
|
|
62
|
+
*/
|
|
63
|
+
async function reprojectAndRelease(accountId: string): Promise<void> {
|
|
64
|
+
const before = integrationDbLinkQueries
|
|
65
|
+
.getForAccount(accountId)
|
|
66
|
+
.map((link) => connectionIdOf(link))
|
|
67
|
+
.filter((id): id is string => id !== null);
|
|
68
|
+
|
|
69
|
+
reproject(accountId);
|
|
70
|
+
|
|
71
|
+
const after = integrationDbLinkQueries
|
|
72
|
+
.getForAccount(accountId)
|
|
73
|
+
.map((link) => connectionIdOf(link))
|
|
74
|
+
.filter((id): id is string => id !== null);
|
|
75
|
+
|
|
76
|
+
for (const id of new Set([...before, ...after])) {
|
|
77
|
+
await connectionManager.release(id);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function defaultModeOf(adapter: DbProviderAdapter): string {
|
|
82
|
+
const modes = adapter.info().modes;
|
|
83
|
+
return (modes.find((mode) => mode.isDefault) ?? modes[0])?.id ?? 'default';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function toLinkInfo(link: IntegrationDbLinkRow, account: IntegrationAccountRow): DbAccountLinkInfo {
|
|
87
|
+
return {
|
|
88
|
+
id: link.id,
|
|
89
|
+
accountId: link.account_id,
|
|
90
|
+
provider: account.provider,
|
|
91
|
+
accountLabel: account.label,
|
|
92
|
+
remoteRef: link.remote_ref,
|
|
93
|
+
label: link.label,
|
|
94
|
+
driver: link.driver,
|
|
95
|
+
mode: link.mode,
|
|
96
|
+
connectionId: connectionIdOf(link),
|
|
97
|
+
configuredSecrets: Object.entries(integrationDbLinkQueries.secretsOf(link))
|
|
98
|
+
.filter(([, value]) => typeof value === 'string' && value.length > 0)
|
|
99
|
+
.map(([name]) => name),
|
|
100
|
+
detected: link.detected === 1
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Refuse a link that is missing a secret the provider says it needs.
|
|
106
|
+
*
|
|
107
|
+
* Checked before the row is written, for the reason the account layer checks
|
|
108
|
+
* its own required fields: a link saved without its password looks connected
|
|
109
|
+
* and fails much later, inside a driver, with a message about authentication
|
|
110
|
+
* rather than about configuration.
|
|
111
|
+
*/
|
|
112
|
+
function assertRequiredSecrets(adapter: DbProviderAdapter, secrets: Record<string, string>): void {
|
|
113
|
+
const missing = adapter.info().secretFields
|
|
114
|
+
.filter((field) => field.isRequired && !(secrets[field.name] ?? '').trim())
|
|
115
|
+
.map((field) => field.label);
|
|
116
|
+
if (missing.length > 0) {
|
|
117
|
+
throw new Error(`${adapter.info().name} needs ${missing.join(', ')}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Poll until a newly created database reports itself connectable.
|
|
123
|
+
*
|
|
124
|
+
* Bounded, and a long budget rather than an unbounded one: a call that cannot
|
|
125
|
+
* tell "still provisioning" from "this will never answer" is worse than one
|
|
126
|
+
* that gives up and says so. Supabase takes roughly a minute for a small
|
|
127
|
+
* project, so three minutes is generous without being a hang.
|
|
128
|
+
*/
|
|
129
|
+
const READY_POLL_INTERVAL_MS = 5_000;
|
|
130
|
+
const READY_POLL_BUDGET_MS = 180_000;
|
|
131
|
+
|
|
132
|
+
async function waitForReady(
|
|
133
|
+
adapter: DbProviderAdapter,
|
|
134
|
+
context: DbProviderContext,
|
|
135
|
+
ref: string
|
|
136
|
+
): Promise<boolean> {
|
|
137
|
+
const deadline = Date.now() + READY_POLL_BUDGET_MS;
|
|
138
|
+
|
|
139
|
+
while (Date.now() < deadline) {
|
|
140
|
+
await new Promise((resolve) => setTimeout(resolve, READY_POLL_INTERVAL_MS));
|
|
141
|
+
try {
|
|
142
|
+
const databases = await adapter.listDatabases(context);
|
|
143
|
+
const match = databases.find((database) => database.ref === ref);
|
|
144
|
+
if (match?.isReady) return true;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
// A provider that is briefly unreachable while it provisions is the
|
|
147
|
+
// normal case, not a reason to abandon a database that now exists.
|
|
148
|
+
debug.log('db-client', `Readiness poll for ${ref} failed, retrying: ${error instanceof Error ? error.message : error}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const dbLinks = {
|
|
156
|
+
/** Every database provider, as the link dialog renders them. */
|
|
157
|
+
providers(): DbProviderInfo[] {
|
|
158
|
+
return listDbProviders().map((adapter) => adapter.info());
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
/** Connected, enabled accounts that can supply database connections. */
|
|
162
|
+
accounts(): { accountId: string; provider: string; label: string; status: string }[] {
|
|
163
|
+
return listDbProviders().flatMap((adapter) =>
|
|
164
|
+
integrationAccountQueries
|
|
165
|
+
.getByProvider(adapter.provider)
|
|
166
|
+
.filter((account) => account.is_enabled === 1)
|
|
167
|
+
.filter((account) => integrationAccountQueries.capabilitiesOf(account).includes('database'))
|
|
168
|
+
.map((account) => ({
|
|
169
|
+
accountId: account.id,
|
|
170
|
+
provider: account.provider,
|
|
171
|
+
label: account.label,
|
|
172
|
+
status: account.status
|
|
173
|
+
}))
|
|
174
|
+
);
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
/** Every remote database this account can reach, flagged with what is linked. */
|
|
178
|
+
async remoteDatabases(accountId: string): Promise<DbRemoteDatabase[]> {
|
|
179
|
+
const account = requireAccount(accountId);
|
|
180
|
+
const adapter = requireAdapter(account.provider);
|
|
181
|
+
const linked = new Set(
|
|
182
|
+
integrationDbLinkQueries.getForAccount(accountId).map((link) => link.remote_ref)
|
|
183
|
+
);
|
|
184
|
+
const databases = await adapter.listDatabases(contextFor(account));
|
|
185
|
+
return databases.map((database) => ({ ...database, isLinked: linked.has(database.ref) }));
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
links(accountId: string): DbAccountLinkInfo[] {
|
|
189
|
+
const account = requireAccount(accountId);
|
|
190
|
+
return integrationDbLinkQueries.getForAccount(accountId).map((link) => toLinkInfo(link, account));
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
/** Every link on the install, which is what the panel badges its list from. */
|
|
194
|
+
allLinks(): DbAccountLinkInfo[] {
|
|
195
|
+
return listDbProviders().flatMap((adapter) =>
|
|
196
|
+
integrationAccountQueries.getByProvider(adapter.provider).flatMap((account) =>
|
|
197
|
+
integrationDbLinkQueries.getForAccount(account.id).map((link) => toLinkInfo(link, account))
|
|
198
|
+
)
|
|
199
|
+
);
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
async create(input: {
|
|
203
|
+
accountId: string;
|
|
204
|
+
remoteRef: string;
|
|
205
|
+
label?: string;
|
|
206
|
+
mode?: string;
|
|
207
|
+
secrets: Record<string, string>;
|
|
208
|
+
detected?: boolean;
|
|
209
|
+
/** Write the row now and work out where it answers later. */
|
|
210
|
+
deferEndpoint?: boolean;
|
|
211
|
+
}): Promise<DbAccountLinkInfo> {
|
|
212
|
+
const account = requireAccount(input.accountId);
|
|
213
|
+
const adapter = requireAdapter(account.provider);
|
|
214
|
+
assertRequiredSecrets(adapter, input.secrets);
|
|
215
|
+
|
|
216
|
+
if (integrationDbLinkQueries.getByRef(input.accountId, input.remoteRef)) {
|
|
217
|
+
throw new Error('That database is already linked to this account.');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const mode = input.mode ?? defaultModeOf(adapter);
|
|
221
|
+
// Resolved BEFORE the row is written. A link whose endpoint could not be
|
|
222
|
+
// resolved is not a link — it is a row that projects nothing, and failing
|
|
223
|
+
// here puts the error in the dialog that caused it rather than in a
|
|
224
|
+
// connection list that silently stayed the same length.
|
|
225
|
+
//
|
|
226
|
+
// `deferEndpoint` is the one exception, and it exists for a single caller:
|
|
227
|
+
// a database we have just CREATED is not answering yet, and its generated
|
|
228
|
+
// password only exists in memory until this row is written. Storing the
|
|
229
|
+
// secret first and resolving later is the only ordering where a slow
|
|
230
|
+
// provision cannot strand a password that nothing can recover.
|
|
231
|
+
const endpoint = input.deferEndpoint
|
|
232
|
+
? null
|
|
233
|
+
: await adapter.resolveEndpoint(contextFor(account), {
|
|
234
|
+
remoteRef: input.remoteRef,
|
|
235
|
+
mode
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const link = integrationDbLinkQueries.create({
|
|
239
|
+
accountId: input.accountId,
|
|
240
|
+
remoteRef: input.remoteRef,
|
|
241
|
+
label: (input.label ?? '').trim() || input.remoteRef,
|
|
242
|
+
driver: adapter.driver,
|
|
243
|
+
mode,
|
|
244
|
+
secrets: input.secrets,
|
|
245
|
+
config: endpoint ? { endpoint } : {},
|
|
246
|
+
detected: input.detected
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
await reprojectAndRelease(input.accountId);
|
|
250
|
+
debug.log('db-client', `Linked ${account.provider}:${input.remoteRef} as ${link.id}`);
|
|
251
|
+
return toLinkInfo(integrationDbLinkQueries.getById(link.id)!, account);
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
/** What the create-a-database form needs, or null when the provider cannot. */
|
|
255
|
+
async createOptions(accountId: string): Promise<DbProviderCreateOptions | null> {
|
|
256
|
+
const account = requireAccount(accountId);
|
|
257
|
+
const adapter = requireAdapter(account.provider);
|
|
258
|
+
if (!adapter.createOptions) return null;
|
|
259
|
+
return adapter.createOptions(contextFor(account));
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Delete a database AT THE PROVIDER, and drop its link.
|
|
264
|
+
*
|
|
265
|
+
* Unlinking and deleting are deliberately different operations: one removes
|
|
266
|
+
* Clopen's connection, the other destroys the database. The link goes first
|
|
267
|
+
* so a successful delete cannot leave a projection pointing at something that
|
|
268
|
+
* no longer exists — and if the remote delete then fails, the user has lost a
|
|
269
|
+
* link they can recreate rather than a database they cannot.
|
|
270
|
+
*/
|
|
271
|
+
async deleteDatabase(accountId: string, remoteRef: string): Promise<void> {
|
|
272
|
+
const account = requireAccount(accountId);
|
|
273
|
+
const adapter = requireAdapter(account.provider);
|
|
274
|
+
if (!adapter.deleteDatabase) {
|
|
275
|
+
throw new Error(`${adapter.info().name} cannot delete databases from here`);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const link = integrationDbLinkQueries.getByRef(accountId, remoteRef);
|
|
279
|
+
if (link) await this.remove(link.id);
|
|
280
|
+
|
|
281
|
+
await adapter.deleteDatabase(contextFor(account), remoteRef);
|
|
282
|
+
debug.log('db-client', `Deleted ${account.provider} database ${remoteRef}`);
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Rename a database AT THE PROVIDER.
|
|
287
|
+
*
|
|
288
|
+
* The link's own label is left alone on purpose. It is what this install
|
|
289
|
+
* calls the connection and is edited in the link form; overwriting a name
|
|
290
|
+
* someone chose here because the project was renamed elsewhere would be a
|
|
291
|
+
* second, unasked-for change.
|
|
292
|
+
*/
|
|
293
|
+
async renameDatabase(accountId: string, remoteRef: string, name: string): Promise<void> {
|
|
294
|
+
const account = requireAccount(accountId);
|
|
295
|
+
const adapter = requireAdapter(account.provider);
|
|
296
|
+
if (!adapter.renameDatabase) {
|
|
297
|
+
throw new Error(`${adapter.info().name} cannot rename databases from here`);
|
|
298
|
+
}
|
|
299
|
+
await adapter.renameDatabase(contextFor(account), remoteRef, name);
|
|
300
|
+
debug.log('db-client', `Renamed ${account.provider} database ${remoteRef}`);
|
|
301
|
+
},
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Create the owning container — a Supabase organisation.
|
|
305
|
+
*
|
|
306
|
+
* Separate from `createDatabase` because the consequence is different: this
|
|
307
|
+
* creates a BILLING entity, not a database. It exists so an account whose
|
|
308
|
+
* token reaches no organisation is not a dead end Clopen can only point away
|
|
309
|
+
* from.
|
|
310
|
+
*/
|
|
311
|
+
async createGroup(accountId: string, name: string): Promise<{ value: string; label: string }> {
|
|
312
|
+
const account = requireAccount(accountId);
|
|
313
|
+
const adapter = requireAdapter(account.provider);
|
|
314
|
+
if (!adapter.createGroup) {
|
|
315
|
+
throw new Error(`${adapter.info().name} cannot create one from here`);
|
|
316
|
+
}
|
|
317
|
+
const group = await adapter.createGroup(contextFor(account), { name });
|
|
318
|
+
debug.log('db-client', `Created ${account.provider} group ${group.value}`);
|
|
319
|
+
return group;
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Create a database at the provider and link it.
|
|
324
|
+
*
|
|
325
|
+
* The ORDER here is the whole design. A freshly created database is not
|
|
326
|
+
* answering yet, and its password exists only in the reply we just received —
|
|
327
|
+
* no API can read it back later. So the link row is written FIRST, with the
|
|
328
|
+
* secret, and the endpoint is resolved afterwards. Every other ordering has a
|
|
329
|
+
* window where a slow provision, a dropped socket or a restart leaves the user
|
|
330
|
+
* with a real database whose password nothing holds.
|
|
331
|
+
*
|
|
332
|
+
* The wait is bounded and failing it is NOT an error: the project exists, the
|
|
333
|
+
* link exists, and saving the link is the retry. Reporting `ready: false`
|
|
334
|
+
* lets the dialog say what actually happened instead of implying the whole
|
|
335
|
+
* thing failed.
|
|
336
|
+
*/
|
|
337
|
+
async createDatabase(input: {
|
|
338
|
+
accountId: string;
|
|
339
|
+
name: string;
|
|
340
|
+
group: string;
|
|
341
|
+
region: string;
|
|
342
|
+
}): Promise<{ link: DbAccountLinkInfo; ready: boolean }> {
|
|
343
|
+
const account = requireAccount(input.accountId);
|
|
344
|
+
const adapter = requireAdapter(account.provider);
|
|
345
|
+
if (!adapter.createDatabase) {
|
|
346
|
+
throw new Error(`${adapter.info().name} cannot create databases from here`);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const context = contextFor(account);
|
|
350
|
+
const created = await adapter.createDatabase(context, {
|
|
351
|
+
name: input.name.trim(),
|
|
352
|
+
group: input.group,
|
|
353
|
+
region: input.region
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
const link = await this.create({
|
|
357
|
+
accountId: input.accountId,
|
|
358
|
+
remoteRef: created.ref,
|
|
359
|
+
label: input.name.trim(),
|
|
360
|
+
secrets: created.secrets,
|
|
361
|
+
deferEndpoint: true
|
|
362
|
+
});
|
|
363
|
+
debug.log('db-client', `Created ${account.provider} database ${created.ref}, linked as ${link.id}`);
|
|
364
|
+
|
|
365
|
+
const ready = await waitForReady(adapter, context, created.ref);
|
|
366
|
+
if (!ready) {
|
|
367
|
+
debug.log('db-client', `${created.ref} is still provisioning — the link will resolve on save`);
|
|
368
|
+
return { link, ready: false };
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Now it answers: resolve the endpoint and let the projection run.
|
|
372
|
+
return { link: await this.update(link.id, { refreshEndpoint: true }), ready: true };
|
|
373
|
+
},
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Change a link's label, mode or secret.
|
|
377
|
+
*
|
|
378
|
+
* The endpoint is re-resolved only when the mode changes or the caller asks:
|
|
379
|
+
* a password rotation does not move the host, and making every save wait on
|
|
380
|
+
* the provider's API would be a slow way to learn nothing new.
|
|
381
|
+
*/
|
|
382
|
+
async update(
|
|
383
|
+
linkId: string,
|
|
384
|
+
patch: { label?: string; mode?: string; secrets?: Record<string, string>; refreshEndpoint?: boolean }
|
|
385
|
+
): Promise<DbAccountLinkInfo> {
|
|
386
|
+
const link = integrationDbLinkQueries.getById(linkId);
|
|
387
|
+
if (!link) throw new Error('Database link not found');
|
|
388
|
+
const account = requireAccount(link.account_id);
|
|
389
|
+
const adapter = requireAdapter(account.provider);
|
|
390
|
+
|
|
391
|
+
// Empty means "not re-typed", the rule every credential form in Clopen
|
|
392
|
+
// already follows, so the merge happens here rather than in the query.
|
|
393
|
+
const secrets = { ...integrationDbLinkQueries.secretsOf(link) };
|
|
394
|
+
for (const [name, value] of Object.entries(patch.secrets ?? {})) {
|
|
395
|
+
if (typeof value === 'string' && value.length > 0) secrets[name] = value;
|
|
396
|
+
}
|
|
397
|
+
assertRequiredSecrets(adapter, secrets);
|
|
398
|
+
|
|
399
|
+
const mode = patch.mode ?? link.mode;
|
|
400
|
+
const config = integrationDbLinkQueries.configOf<Record<string, unknown>>(link);
|
|
401
|
+
// A link with NO endpoint is one that was deferred — a database that was
|
|
402
|
+
// still provisioning when it was created. Saving it is the retry, so the
|
|
403
|
+
// user never has to know which button re-resolves.
|
|
404
|
+
if (patch.refreshEndpoint || mode !== link.mode || !config.endpoint) {
|
|
405
|
+
config.endpoint = await adapter.resolveEndpoint(contextFor(account), {
|
|
406
|
+
remoteRef: link.remote_ref,
|
|
407
|
+
mode
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
integrationDbLinkQueries.update(linkId, {
|
|
412
|
+
label: patch.label?.trim() || undefined,
|
|
413
|
+
mode,
|
|
414
|
+
secrets,
|
|
415
|
+
config
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
await reprojectAndRelease(link.account_id);
|
|
419
|
+
return toLinkInfo(integrationDbLinkQueries.getById(linkId)!, account);
|
|
420
|
+
},
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Drop a link.
|
|
424
|
+
*
|
|
425
|
+
* The row is deleted first and `reproject()` does the rest: the connection
|
|
426
|
+
* this link owned is no longer in the projected set, so the pass releases it
|
|
427
|
+
* exactly the way disconnecting the whole account would — deleting a row we
|
|
428
|
+
* created, handing back one we adopted.
|
|
429
|
+
*/
|
|
430
|
+
async remove(linkId: string): Promise<void> {
|
|
431
|
+
const link = integrationDbLinkQueries.getById(linkId);
|
|
432
|
+
if (!link) return;
|
|
433
|
+
const accountId = link.account_id;
|
|
434
|
+
const connectionId = connectionIdOf(link);
|
|
435
|
+
integrationDbLinkQueries.remove(linkId);
|
|
436
|
+
reproject(accountId);
|
|
437
|
+
// The row is gone or handed back; either way the open driver for it is
|
|
438
|
+
// no longer ours to keep.
|
|
439
|
+
if (connectionId) await connectionManager.release(connectionId);
|
|
440
|
+
debug.log('db-client', `Unlinked ${linkId}`);
|
|
441
|
+
},
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Who owns each projected connection, keyed by connection id.
|
|
445
|
+
*
|
|
446
|
+
* One pass over the projections rather than a lookup per connection: the
|
|
447
|
+
* panel asks this for a whole list on every refresh.
|
|
448
|
+
*/
|
|
449
|
+
managedIndex(): Map<string, DbClientManagedBy> {
|
|
450
|
+
const index = new Map<string, DbClientManagedBy>();
|
|
451
|
+
const linksByAccount = new Map<string, Map<string, IntegrationDbLinkRow>>();
|
|
452
|
+
|
|
453
|
+
const linksOf = (accountId: string): Map<string, IntegrationDbLinkRow> => {
|
|
454
|
+
let cached = linksByAccount.get(accountId);
|
|
455
|
+
if (!cached) {
|
|
456
|
+
cached = new Map();
|
|
457
|
+
for (const link of integrationDbLinkQueries.getForAccount(accountId)) {
|
|
458
|
+
const connectionId = connectionIdOf(link);
|
|
459
|
+
if (connectionId) cached.set(connectionId, link);
|
|
460
|
+
}
|
|
461
|
+
linksByAccount.set(accountId, cached);
|
|
462
|
+
}
|
|
463
|
+
return cached;
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
for (const projection of integrationProjectionQueries.getAllByKind('db_client_connection')) {
|
|
467
|
+
const account = integrationAccountQueries.getById(projection.account_id);
|
|
468
|
+
if (!account) continue;
|
|
469
|
+
|
|
470
|
+
const link = linksOf(projection.account_id).get(projection.target_id) ?? null;
|
|
471
|
+
|
|
472
|
+
index.set(projection.target_id, {
|
|
473
|
+
accountId: account.id,
|
|
474
|
+
provider: account.provider,
|
|
475
|
+
providerName: getProvider(account.provider)?.name ?? account.provider,
|
|
476
|
+
accountLabel: account.label,
|
|
477
|
+
linkId: link?.id ?? null,
|
|
478
|
+
remoteRef: link?.remote_ref ?? null,
|
|
479
|
+
adopted: projection.adopted === 1
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return index;
|
|
484
|
+
},
|
|
485
|
+
|
|
486
|
+
/** The owner of one connection, or null when it was typed in by hand. */
|
|
487
|
+
managedBy(connectionId: string): DbClientManagedBy | null {
|
|
488
|
+
return this.managedIndex().get(connectionId) ?? null;
|
|
489
|
+
},
|
|
490
|
+
|
|
491
|
+
/** Attach ownership to a list of connections on its way to the panel. */
|
|
492
|
+
decorate<T extends { id: string }>(connections: T[]): (T & { managedBy: DbClientManagedBy | null })[] {
|
|
493
|
+
const index = this.managedIndex();
|
|
494
|
+
return connections.map((connection) => ({
|
|
495
|
+
...connection,
|
|
496
|
+
managedBy: index.get(connection.id) ?? null
|
|
497
|
+
}));
|
|
498
|
+
},
|
|
499
|
+
|
|
500
|
+
/** The link behind a connection, for the surfaces that need its remote ref. */
|
|
501
|
+
linkForConnection(connectionId: string): { link: IntegrationDbLinkRow; account: IntegrationAccountRow } | null {
|
|
502
|
+
const projection = integrationProjectionQueries.getByTarget('db_client_connection', connectionId);
|
|
503
|
+
if (!projection) return null;
|
|
504
|
+
const account = integrationAccountQueries.getById(projection.account_id);
|
|
505
|
+
if (!account) return null;
|
|
506
|
+
const link = integrationDbLinkQueries
|
|
507
|
+
.getForAccount(account.id)
|
|
508
|
+
.find((candidate) => connectionIdOf(candidate) === connectionId);
|
|
509
|
+
return link ? { link, account } : null;
|
|
510
|
+
},
|
|
511
|
+
|
|
512
|
+
/** Health for the hub, delegated to whichever adapter owns the provider. */
|
|
513
|
+
async probe(accountId: string) {
|
|
514
|
+
const account = requireAccount(accountId);
|
|
515
|
+
const adapter = getDbProvider(account.provider);
|
|
516
|
+
if (!adapter) return null;
|
|
517
|
+
if (!integrationAccountQueries.capabilitiesOf(account).includes('database')) return null;
|
|
518
|
+
return adapter.probe(contextFor(account));
|
|
519
|
+
},
|
|
520
|
+
|
|
521
|
+
/** Connections a projection owns, so the panel can refuse to delete them. */
|
|
522
|
+
isManaged(connectionId: string): boolean {
|
|
523
|
+
return integrationProjectionQueries.getByTarget('db_client_connection', connectionId) !== null;
|
|
524
|
+
},
|
|
525
|
+
|
|
526
|
+
/** Every projected connection id — used by tests and the audit path. */
|
|
527
|
+
projectedConnectionIds(): string[] {
|
|
528
|
+
return integrationProjectionQueries
|
|
529
|
+
.getAllByKind('db_client_connection')
|
|
530
|
+
.map((projection) => projection.target_id)
|
|
531
|
+
.filter((id) => dbClientConnectionQueries.get(id) !== null);
|
|
532
|
+
}
|
|
533
|
+
};
|