@context-use/open-sync 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/LICENSE +3 -0
  2. package/package.json +7 -4
  3. package/src/api.ts +18 -14
  4. package/src/build.ts +3 -21
  5. package/src/connector/client.ts +61 -28
  6. package/src/connector/failure.ts +49 -0
  7. package/src/connector/file.ts +104 -0
  8. package/src/connector/response.ts +22 -1
  9. package/src/db/client.ts +1 -11
  10. package/src/db/providers.sql +2 -3
  11. package/src/db/providers.ts +1 -11
  12. package/src/db/schema.sql +41 -36
  13. package/src/execution/abortable.ts +12 -0
  14. package/src/execution/diagnostics.ts +1 -2
  15. package/src/execution/provider.ts +1 -0
  16. package/src/execution/worker.ts +162 -39
  17. package/src/http/controller.ts +63 -42
  18. package/src/http/providers.ts +13 -0
  19. package/src/models/asset.ts +61 -0
  20. package/src/models/definition.ts +24 -15
  21. package/src/models/delivery.ts +25 -31
  22. package/src/models/error.ts +12 -1
  23. package/src/models/limits.ts +8 -3
  24. package/src/models/metadata.ts +42 -0
  25. package/src/models/page.ts +68 -13
  26. package/src/models/provider-catalog.ts +57 -0
  27. package/src/models/provider-path.ts +25 -0
  28. package/src/models/providers.ts +13 -2
  29. package/src/models/record.ts +22 -0
  30. package/src/models/registry.ts +15 -15
  31. package/src/models/source-http-error.ts +37 -0
  32. package/src/models/sync.ts +71 -0
  33. package/src/models/validation.ts +1 -1
  34. package/src/open-sync.ts +31 -7
  35. package/src/repositories/acquisition/contract.ts +18 -10
  36. package/src/repositories/acquisition/lease.ts +80 -94
  37. package/src/repositories/acquisition/outbox.ts +22 -22
  38. package/src/repositories/acquisition/records.ts +44 -13
  39. package/src/repositories/acquisition/sqlite.ts +78 -31
  40. package/src/repositories/assets/capacity.ts +16 -0
  41. package/src/repositories/assets/contract.ts +25 -0
  42. package/src/repositories/assets/filesystem.ts +103 -0
  43. package/src/repositories/assets/sqlite.ts +174 -0
  44. package/src/repositories/catalog/contract.ts +16 -18
  45. package/src/repositories/catalog/sqlite.ts +107 -115
  46. package/src/repositories/delivery/contract.ts +7 -11
  47. package/src/repositories/delivery/lease.ts +16 -0
  48. package/src/repositories/delivery/sqlite.ts +57 -34
  49. package/src/repositories/providers/contract.ts +2 -1
  50. package/src/repositories/providers/sqlite.ts +22 -6
  51. package/src/repositories/queue-usage.ts +7 -8
  52. package/src/repositories/rows.ts +10 -29
  53. package/src/runtime.ts +27 -6
  54. package/src/services/acquisition.ts +178 -52
  55. package/src/services/delivery.ts +39 -19
  56. package/src/services/management.ts +108 -44
  57. package/src/services/providers/service.ts +187 -31
  58. package/src/build/connector.ts +0 -143
  59. package/src/models/installation.ts +0 -37
@@ -1,35 +1,53 @@
1
+ import { abortable } from '../execution/abortable';
1
2
  import type { DeliveryResult } from '../models/delivery';
2
3
  import { validateResult } from '../models/delivery-result';
3
4
  import { SyncError } from '../models/error';
4
5
  import { retryDelay, type Timing } from '../models/limits';
5
6
  import type { Registry } from '../models/registry';
6
- import type { DeliveryRepository } from '../repositories/delivery/contract';
7
+ import type { AssetFiles, AssetRepository } from '../repositories/assets/contract';
8
+ import type { DeliveryLease, DeliveryRepository } from '../repositories/delivery/contract';
7
9
 
8
10
  export class DeliveryService {
9
11
  constructor(
10
- private readonly input: { repository: DeliveryRepository; registry: Registry; timing: Timing },
12
+ private readonly input: {
13
+ repository: DeliveryRepository;
14
+ registry: Registry;
15
+ timing: Timing;
16
+ assets: AssetRepository;
17
+ files: AssetFiles;
18
+ },
11
19
  ) {}
12
- async execute(signal: AbortSignal): Promise<void> {
20
+ claim() {
21
+ return this.input.repository.claim(this.input.timing.leaseMs);
22
+ }
23
+ nextDue() {
24
+ return this.input.repository.nextDue();
25
+ }
26
+ async execute(input: { lease: DeliveryLease; signal: AbortSignal }): Promise<boolean> {
13
27
  const { repository, registry, timing } = this.input;
14
- const lease = repository.claim(timing.leaseMs);
15
- if (!lease) {
16
- return;
17
- }
28
+ const { lease, signal } = input;
18
29
  let result: DeliveryResult;
19
30
  try {
20
31
  const type = registry.destination(lease.destination.type);
21
- if (type.version !== lease.destination.version) {
22
- result = { status: 'rejected', code: 'destination_unavailable' };
23
- } else {
24
- result = validateResult(
25
- await type.deliver({
26
- scope: { actorId: lease.actorId, ownerId: lease.ownerId },
27
- config: structuredClone(lease.destination.config),
28
- delivery: structuredClone(lease.delivery),
29
- signal,
30
- }),
31
- );
32
- }
32
+ result = validateResult(
33
+ await abortable({
34
+ signal,
35
+ run: () =>
36
+ type.deliver({
37
+ scope: { actorId: lease.actorId, ownerId: lease.ownerId },
38
+ config: structuredClone(lease.destination.config),
39
+ deliverable: {
40
+ ...structuredClone(lease.delivery),
41
+ openAsset: async (asset) => {
42
+ signal.throwIfAborted();
43
+ const id = this.input.assets.read({ lease, asset });
44
+ return await this.input.files.open(id);
45
+ },
46
+ },
47
+ signal,
48
+ }),
49
+ }),
50
+ );
33
51
  signal.throwIfAborted();
34
52
  } catch (error) {
35
53
  result =
@@ -44,10 +62,12 @@ export class DeliveryService {
44
62
  });
45
63
  try {
46
64
  repository.complete({ lease, result, delay });
65
+ return result.status === 'accepted';
47
66
  } catch (error) {
48
67
  if (!(error instanceof SyncError && error.code === 'lease_lost')) {
49
68
  throw error;
50
69
  }
70
+ return false;
51
71
  }
52
72
  }
53
73
  }
@@ -1,11 +1,12 @@
1
1
  import { bindProvider, type ProviderGateway } from '../execution/provider';
2
2
  import type { WorkerControl } from '../execution/worker';
3
+ import type { ConnectionRef } from '../models/definition';
3
4
  import { fail } from '../models/error';
4
5
  import type { Resource, Scope } from '../models/identity';
5
- import type { CreateInstallation } from '../models/installation';
6
6
  import type { JsonObject } from '../models/json';
7
7
  import { positive, type QueueLimits } from '../models/limits';
8
8
  import type { Registry } from '../models/registry';
9
+ import { type CreateSync, summarizeSync } from '../models/sync';
9
10
  import { identifier, validate } from '../models/validation';
10
11
  import type { CatalogRepository } from '../repositories/catalog/contract';
11
12
  import type { DeliveryRepository } from '../repositories/delivery/contract';
@@ -29,43 +30,47 @@ export class SyncManagement {
29
30
  }
30
31
  definitions(scope: Scope) {
31
32
  this.guard(scope);
32
- return this.input.registry.definitions();
33
+ return this.input.registry
34
+ .definitions()
35
+ .map(({ id, name, description, configSchema, provider }) => ({
36
+ id,
37
+ name,
38
+ description,
39
+ configSchema,
40
+ provider,
41
+ }));
33
42
  }
34
43
  destinationTypes(scope: Scope) {
35
44
  this.guard(scope);
36
45
  return this.input.registry.destinationTypes();
37
46
  }
38
- runs(input: Resource & { offset?: number }) {
39
- this.guard(input);
40
- const offset = input.offset ?? 0;
41
- positive(offset + 1);
42
- return this.input.catalog.runs({ ...input, offset });
43
- }
44
- destinations(scope: Scope) {
47
+ private async prepareDestination(input: Scope & { destination: CreateSync['destination'] }) {
48
+ const scope = { actorId: input.actorId, ownerId: input.ownerId };
49
+ const type = this.input.registry.destination(input.destination.type);
50
+ const values = validate({
51
+ value: input.destination.input,
52
+ schema: type.setup?.schema ?? type.configSchema,
53
+ }) as JsonObject;
54
+ let prepared = values;
55
+ if (type.setup) {
56
+ try {
57
+ prepared = await type.setup.prepare({ scope: { ...scope }, input: values });
58
+ } catch {
59
+ fail('destination_setup_failed');
60
+ }
61
+ }
45
62
  this.guard(scope);
46
- return this.input.catalog
47
- .destinations(scope)
48
- .map(({ id, type, version }) => ({ id, type, version }));
49
- }
50
- createDestination(input: Scope & { type: string; config: JsonObject }) {
51
- this.guard(input);
52
- const type = this.input.registry.destination(input.type);
53
- const config = validate({ value: input.config, schema: type.configSchema }) as JsonObject;
54
- const { id, version } = this.input.catalog.createDestination({
55
- ...input,
56
- config,
57
- version: type.version,
58
- });
59
- return { id, type: input.type, version };
63
+ const config = validate({ value: prepared, schema: type.configSchema }) as JsonObject;
64
+ return { type: input.destination.type, config };
60
65
  }
61
- async createInstallation(input: CreateInstallation) {
66
+ async createSync(input: CreateSync) {
62
67
  this.guard(input);
63
68
  const { definition } = this.input.registry.definition(input.definition);
64
69
  const config = validate({ value: input.config, schema: definition.configSchema }) as JsonObject;
65
70
  if (input.intervalMs !== undefined) {
66
71
  positive(input.intervalMs);
67
72
  }
68
- if (definition.provider) {
73
+ if (definition.provider && (input.connection || input.enabled !== false)) {
69
74
  await bindProvider({
70
75
  actorId: input.actorId,
71
76
  ownerId: input.ownerId,
@@ -78,47 +83,106 @@ export class SyncManagement {
78
83
  fail('unexpected_connection');
79
84
  }
80
85
  this.guard(input);
81
- return this.input.catalog.createInstallation({
86
+ const destination = await this.prepareDestination(input);
87
+ const sync = this.input.catalog.createSync({
82
88
  ...input,
83
89
  config,
90
+ destination,
84
91
  initialCheckpoint: definition.initialCheckpoint,
85
92
  });
93
+ this.input.worker.wake();
94
+ return summarizeSync(sync);
86
95
  }
87
- installations(scope: Scope) {
96
+ syncs(scope: Scope) {
88
97
  this.guard(scope);
89
- return this.input.catalog.installations(scope);
98
+ return this.input.catalog.syncs(scope).map(summarizeSync);
90
99
  }
91
- installation(input: Resource) {
100
+ sync(input: Resource) {
92
101
  this.guard(input);
93
- return this.input.catalog.installation(input);
102
+ return summarizeSync(this.input.catalog.sync(input));
94
103
  }
95
- async setEnabled(input: Resource & { enabled: boolean }) {
104
+ /** Polling iterations, newest first, including the current scan. */
105
+ polls(input: Resource & { before?: number }) {
96
106
  this.guard(input);
97
- const result = this.input.catalog.setEnabled(input);
98
- await this.input.worker.cancel(input);
99
- return result;
107
+ validateCursor(input.before);
108
+ return this.input.catalog.polls(input);
100
109
  }
101
- queueRun(input: Resource & { backfill?: boolean }): void {
110
+ async connectSync(input: Resource & { connection: ConnectionRef }) {
102
111
  this.guard(input);
103
- const installation = this.input.catalog.installation(input);
104
- const { definition } = this.input.registry.definition(installation.definition);
105
- this.input.catalog.queue({
112
+ const sync = this.input.catalog.sync(input);
113
+ if (sync.connection || sync.enabled) {
114
+ fail('already_connected');
115
+ }
116
+ const { definition } = this.input.registry.definition(sync.definition);
117
+ if (!definition.provider) {
118
+ fail('unexpected_connection');
119
+ }
120
+ await bindProvider({
106
121
  ...input,
107
- checkpoint: input.backfill ? definition.initialCheckpoint : undefined,
122
+ requirements: definition.provider,
123
+ gateway: this.input.gateway,
124
+ signal: AbortSignal.timeout(this.input.timeoutMs),
108
125
  });
126
+ this.guard(input);
127
+ const connected = this.input.catalog.connectSync(input);
128
+ this.input.worker.wake();
129
+ return summarizeSync(connected);
130
+ }
131
+ async setEnabled(input: Resource & { enabled: boolean }) {
132
+ this.guard(input);
133
+ const sync = this.input.catalog.sync(input);
134
+ if (sync.enabled === input.enabled) {
135
+ return summarizeSync(sync);
136
+ }
137
+ if (input.enabled && !sync.connection) {
138
+ const { definition } = this.input.registry.definition(sync.definition);
139
+ if (definition.provider) {
140
+ fail('connection_required');
141
+ }
142
+ }
143
+ const result = this.input.catalog.setEnabled(input);
144
+ await this.input.worker.cancelAcquisition(input);
145
+ return summarizeSync(result);
146
+ }
147
+ runNow(input: Resource): void {
148
+ this.guard(input);
149
+ this.input.catalog.runNow(input);
150
+ this.input.worker.wake();
151
+ }
152
+ async resync(input: Resource): Promise<void> {
153
+ this.guard(input);
154
+ const sync = this.input.catalog.sync(input);
155
+ const { definition } = this.input.registry.definition(sync.definition);
156
+ this.input.catalog.resync({ ...input, checkpoint: definition.initialCheckpoint });
157
+ await this.input.worker.cancelAcquisition(input);
158
+ }
159
+ async removeSync(input: Resource): Promise<void> {
160
+ this.guard(input);
161
+ this.input.catalog.removeSync(input);
162
+ await this.input.worker.cancelSync(input);
109
163
  }
110
164
  status(scope: Scope) {
111
165
  this.guard(scope);
112
166
  return { queue: this.input.deliveries.status(scope), limits: { ...this.input.limits } };
113
167
  }
114
- deliveries(input: Scope & { offset?: number }) {
168
+ deliveries(input: Scope & { syncId: string; before?: number }) {
115
169
  this.guard(input);
116
- const offset = input.offset ?? 0;
117
- positive(offset + 1);
118
- return this.input.deliveries.pending({ ...input, offset });
170
+ validateCursor(input.before);
171
+ this.input.catalog.sync({ ...input, id: input.syncId });
172
+ return this.input.deliveries.pending(input);
119
173
  }
120
- retryDelivery(input: Resource): void {
174
+ deliverable(input: Resource & { syncId: string }) {
121
175
  this.guard(input);
176
+ return this.input.deliveries.deliverable(input);
177
+ }
178
+ retryDelivery(input: Resource & { syncId: string }): void {
179
+ this.deliverable(input);
122
180
  this.input.deliveries.retry(input);
181
+ this.input.worker.wake();
182
+ }
183
+ }
184
+ function validateCursor(before?: number): void {
185
+ if (before !== undefined && (!Number.isSafeInteger(before) || before < 1)) {
186
+ fail('invalid_input');
123
187
  }
124
188
  }
@@ -1,7 +1,9 @@
1
1
  import { fail, SyncError } from '../../models/error';
2
+ import { ProviderCatalog } from '../../models/provider-catalog';
2
3
  import { oauthClientValues, publicConnection } from '../../models/provider-values';
3
4
  import type {
4
5
  ConnectorManagement,
6
+ OAuthClientRegistration,
5
7
  ProviderScope,
6
8
  ProviderSetup as RuntimeProviderSetup,
7
9
  } from '../../models/providers';
@@ -9,16 +11,33 @@ import { identifier } from '../../models/validation';
9
11
  import type { ProviderRepository } from '../../repositories/providers/contract';
10
12
 
11
13
  export class ProviderService {
14
+ private providerCatalog?: Promise<ProviderCatalog>;
15
+ private readonly clientConfigurations = new Map<string, Promise<void>>();
12
16
  constructor(
13
17
  private readonly input: {
14
18
  repository: ProviderRepository;
15
19
  connector: ConnectorManagement;
16
20
  returnUrl(input: { service: string; id: string }): string;
17
21
  canConfigure(scope: ProviderScope): Promise<boolean>;
22
+ registrations?: Readonly<Record<string, OAuthClientRegistration>>;
18
23
  signal: AbortSignal;
24
+ onConnected?(
25
+ input: ProviderScope & { connection: { id: string; service: string } },
26
+ ): Promise<void>;
19
27
  },
20
28
  ) {}
21
29
 
30
+ private loadCatalog() {
31
+ this.providerCatalog ??= this.input.connector
32
+ .catalog()
33
+ .then((entries) => new ProviderCatalog(entries))
34
+ .catch((error) => {
35
+ this.providerCatalog = undefined;
36
+ throw error;
37
+ });
38
+ return this.providerCatalog;
39
+ }
40
+
22
41
  private guard(scope: ProviderScope) {
23
42
  this.input.signal.throwIfAborted();
24
43
  identifier(scope.ownerId);
@@ -42,21 +61,29 @@ export class ProviderService {
42
61
  }
43
62
  async catalog(scope: ProviderScope) {
44
63
  this.guard(scope);
45
- const providers = await this.input.connector.catalog();
46
- return providers.map(({ service, displayName, iconUrl, authTypes, categories, scenario }) => ({
47
- service,
48
- displayName,
49
- iconUrl,
50
- authTypes,
51
- categories,
52
- scenario,
53
- }));
64
+ return (await this.loadCatalog()).entries;
65
+ }
66
+ async catalogPage(input: ProviderScope & { q?: string; offset?: number }) {
67
+ this.guard(input);
68
+ return (await this.loadCatalog()).page(input);
69
+ }
70
+ private registration(service: string) {
71
+ const registrations = this.input.registrations;
72
+ return registrations && Object.hasOwn(registrations, service)
73
+ ? registrations[service]
74
+ : undefined;
54
75
  }
55
76
  private async setup(service: string): Promise<RuntimeProviderSetup> {
56
77
  const setup = await this.input.connector.call({
57
78
  path: `/v1/providers/${encodeURIComponent(service)}/setup`,
58
79
  });
59
- return setup as unknown as RuntimeProviderSetup;
80
+ const result = setup as unknown as RuntimeProviderSetup;
81
+ return {
82
+ ...result,
83
+ ...(result.oauthClient && this.registration(service)
84
+ ? { oauthClient: { ...result.oauthClient, automaticRegistration: true } }
85
+ : {}),
86
+ };
60
87
  }
61
88
  async status(input: ProviderScope & { service: string }) {
62
89
  this.guard(input);
@@ -82,7 +109,17 @@ export class ProviderService {
82
109
  (metadata.status === 'active' || metadata.status === 'reauth_required')
83
110
  ? metadata.status
84
111
  : 'unknown';
85
- return { id, account, status };
112
+ const authType = metadata?.authType;
113
+ const supportedAuth: RuntimeProviderSetup['auth'][number]['type'] | undefined =
114
+ authType === 'oauth2' || authType === 'api_key' || authType === 'custom_credential'
115
+ ? authType
116
+ : undefined;
117
+ return {
118
+ id,
119
+ account,
120
+ status,
121
+ authType: supportedAuth,
122
+ };
86
123
  }),
87
124
  ),
88
125
  };
@@ -92,26 +129,120 @@ export class ProviderService {
92
129
  if (!(await this.input.canConfigure(input))) {
93
130
  fail('forbidden');
94
131
  }
95
- const setup = await this.setup(input.service);
132
+ await this.configureClient({
133
+ service: input.service,
134
+ configure: async () => {
135
+ const setup = await this.setup(input.service);
136
+ const auth = setup.auth.find((method) => method.type === 'oauth2');
137
+ if (!auth) {
138
+ throw new SyncError({
139
+ code: 'provider_request_failed',
140
+ message: 'This provider does not support OAuth.',
141
+ });
142
+ }
143
+ await this.input.connector.call({
144
+ path: `/api/oauth/configs/${encodeURIComponent(input.service)}`,
145
+ method: 'PUT',
146
+ body: oauthClientValues({ auth, values: input.values }),
147
+ });
148
+ },
149
+ });
150
+ return { configured: true };
151
+ }
152
+ private async configureClient(input: { service: string; configure: () => Promise<void> }) {
153
+ const { service, configure } = input;
154
+ const pending = (this.clientConfigurations.get(service) ?? Promise.resolve())
155
+ .catch(() => undefined)
156
+ .then(configure);
157
+ this.clientConfigurations.set(service, pending);
158
+ try {
159
+ await pending;
160
+ } finally {
161
+ if (this.clientConfigurations.get(service) === pending) {
162
+ this.clientConfigurations.delete(service);
163
+ }
164
+ }
165
+ }
166
+ private async ensureClient(input: ProviderScope & { service: string }) {
167
+ const register = this.registration(input.service);
168
+ if (!register) {
169
+ return;
170
+ }
171
+ await this.configureClient({
172
+ service: input.service,
173
+ configure: async () => {
174
+ const setup = await this.setup(input.service);
175
+ if (setup.oauthClient?.configured) {
176
+ return;
177
+ }
178
+ if (!(await this.input.canConfigure(input))) {
179
+ fail('forbidden');
180
+ }
181
+ await this.registerClient({ service: input.service, register, setup });
182
+ },
183
+ });
184
+ }
185
+
186
+ private async registerClient(input: {
187
+ service: string;
188
+ register: OAuthClientRegistration;
189
+ setup: RuntimeProviderSetup;
190
+ }) {
191
+ const { setup } = input;
96
192
  const auth = setup.auth.find((method) => method.type === 'oauth2');
97
- if (!auth) {
193
+ if (!auth || !setup.oauthClient) {
194
+ fail('provider_request_failed');
195
+ }
196
+ try {
197
+ const timeoutMs = 30_000;
198
+ const values = await input.register({
199
+ redirectUri: setup.oauthClient.expectedRedirectUri,
200
+ scopes: auth.scopes,
201
+ signal: AbortSignal.any([this.input.signal, AbortSignal.timeout(timeoutMs)]),
202
+ });
203
+ this.input.signal.throwIfAborted();
204
+ await this.input.connector.call({
205
+ path: `/api/oauth/configs/${encodeURIComponent(input.service)}`,
206
+ method: 'PUT',
207
+ body: oauthClientValues({
208
+ auth,
209
+ values: { clientId: values.clientId, clientSecret: values.clientSecret ?? '' },
210
+ }),
211
+ });
212
+ } catch {
213
+ // Registration errors may contain credentials or upstream bodies.
98
214
  throw new SyncError({
99
215
  code: 'provider_request_failed',
100
- message: 'This provider does not support OAuth.',
216
+ message: 'Could not register the OAuth client. Try connecting again.',
101
217
  });
102
218
  }
103
- await this.input.connector.call({
104
- path: `/api/oauth/configs/${encodeURIComponent(input.service)}`,
105
- method: 'PUT',
106
- body: oauthClientValues({ auth, values: input.values }),
107
- });
108
- return { configured: true };
109
219
  }
110
- async start(input: ProviderScope & { service: string; authorizationOptionIds?: string[] }) {
220
+
221
+ private async reconnectTarget(input: ProviderScope & { service: string; connectionId?: string }) {
222
+ if (!input.connectionId) {
223
+ return undefined;
224
+ }
225
+ const target = await this.input.repository.connection({ ...input, id: input.connectionId });
226
+ if (!target || target.service !== input.service) {
227
+ fail('not_found');
228
+ }
229
+ return target;
230
+ }
231
+ async start(
232
+ input: ProviderScope & {
233
+ service: string;
234
+ authorizationOptionIds?: string[];
235
+ connectionId?: string;
236
+ },
237
+ ) {
111
238
  this.guard(input);
112
- const id = `connection_${crypto.randomUUID()}`;
239
+ const target = await this.reconnectTarget(input);
240
+ await this.ensureClient(input);
241
+ const id = target?.id ?? `connection_${crypto.randomUUID()}`;
113
242
  const result = await this.input.connector.call({
114
- path: `/v1/connections/${encodeURIComponent(input.service)}/connect`,
243
+ path: target
244
+ ? `/v1/connections/by-id/${encodeURIComponent(target.connectorId)}/connect`
245
+ : `/v1/connections/${encodeURIComponent(input.service)}/connect`,
115
246
  method: 'POST',
116
247
  body: {
117
248
  returnUri: this.input.returnUrl({ service: input.service, id }),
@@ -135,33 +266,51 @@ export class ProviderService {
135
266
  input: ProviderScope & {
136
267
  service: string;
137
268
  authType: 'api_key' | 'custom_credential';
269
+ connectionId?: string;
138
270
  values: Record<string, string>;
139
271
  },
140
272
  ) {
141
273
  this.guard(input);
274
+ const target = await this.reconnectTarget(input);
275
+ const path = target
276
+ ? `/v1/connections/by-id/${encodeURIComponent(target.connectorId)}/connect`
277
+ : `/v1/connections/${encodeURIComponent(input.service)}/connect`;
142
278
  const { apiKey, ...extra } = input.values;
143
279
  const result = await this.input.connector.call({
144
- path: `/v1/connections/${encodeURIComponent(input.service)}/connect/${input.authType === 'api_key' ? 'api-key' : 'custom-credential'}`,
280
+ path: `${path}/${input.authType === 'api_key' ? 'api-key' : 'custom-credential'}`,
145
281
  method: 'POST',
146
282
  body: input.authType === 'api_key' ? { apiKey, extra } : { values: input.values },
147
283
  });
148
284
  const connection = publicConnection({ metadata: result, service: input.service });
149
285
  this.input.signal.throwIfAborted();
150
- await this.input.repository.add({
286
+ const id = target?.id ?? `connection_${crypto.randomUUID()}`;
287
+ const owned = { actorId: input.actorId, ownerId: input.ownerId, id, ...connection };
288
+ if (target) {
289
+ if (connection.connectorId !== target.connectorId) {
290
+ fail('connection_unavailable');
291
+ }
292
+ await this.input.repository.updateAccount(owned);
293
+ } else {
294
+ await this.input.repository.add(owned);
295
+ }
296
+ await this.input.onConnected?.({
151
297
  actorId: input.actorId,
152
298
  ownerId: input.ownerId,
153
- id: `connection_${crypto.randomUUID()}`,
154
- ...connection,
299
+ connection: { id, service: input.service },
155
300
  });
156
301
  return { connected: true };
157
302
  }
158
303
  async complete(input: ProviderScope & { service: string; id: string }): Promise<void> {
159
304
  this.guard(input);
160
305
  const existing = await this.input.repository.connection(input);
161
- if (existing?.service === input.service) {
306
+ const pending = await this.input.repository.pending(input);
307
+ if (!pending && existing?.service === input.service) {
308
+ await this.input.onConnected?.({
309
+ ...input,
310
+ connection: { id: input.id, service: input.service },
311
+ });
162
312
  return;
163
313
  }
164
- const pending = await this.input.repository.pending(input);
165
314
  if (!pending || pending.service !== input.service) {
166
315
  fail('not_found');
167
316
  }
@@ -178,13 +327,20 @@ export class ProviderService {
178
327
  path: `/v1/connections/by-id/${encodeURIComponent(result.appId)}`,
179
328
  });
180
329
  const connection = publicConnection({ metadata, service: input.service });
181
- if (connection.connectorId !== result.appId) {
330
+ if (
331
+ connection.connectorId !== result.appId ||
332
+ (existing && existing.connectorId !== connection.connectorId)
333
+ ) {
182
334
  throw new SyncError({ code: 'provider_request_failed', message: 'Invalid connection.' });
183
335
  }
184
336
  this.input.signal.throwIfAborted();
185
- await this.input.repository.complete({ ...input, ...connection });
337
+ await this.input.repository.complete({ ...input, ...connection, requestId: pending.requestId });
186
338
  if (!(await this.input.repository.connection(input))) {
187
339
  fail('not_found');
188
340
  }
341
+ await this.input.onConnected?.({
342
+ ...input,
343
+ connection: { id: input.id, service: input.service },
344
+ });
189
345
  }
190
346
  }