@mastra/factory 0.2.2-alpha.0 → 0.2.2-alpha.2
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/CHANGELOG.md +36 -0
- package/dist/auth.d.ts +2 -2
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +401 -314
- package/dist/auth.js.map +1 -1
- package/dist/capabilities/connection.js +1 -1
- package/dist/capabilities/intake.js +1 -1
- package/dist/capabilities/version-control.js +1 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +441 -39455
- package/dist/factory.js.map +1 -1
- package/dist/index.js +2 -39466
- package/dist/integrations/base.js +1 -1
- package/dist/integrations/github/config.js +36 -22
- package/dist/integrations/github/config.js.map +1 -1
- package/dist/integrations/github/factory-session.js +33 -37
- package/dist/integrations/github/factory-session.js.map +1 -1
- package/dist/integrations/github/integration.js +1037 -3624
- package/dist/integrations/github/integration.js.map +1 -1
- package/dist/integrations/github/issue-triage.js +71 -71
- package/dist/integrations/github/issue-triage.js.map +1 -1
- package/dist/integrations/github/pat.js +47 -36
- package/dist/integrations/github/pat.js.map +1 -1
- package/dist/integrations/github/provenance.js +72 -101
- package/dist/integrations/github/provenance.js.map +1 -1
- package/dist/integrations/github/routes.d.ts.map +1 -1
- package/dist/integrations/github/routes.js +1272 -1833
- package/dist/integrations/github/routes.js.map +1 -1
- package/dist/integrations/github/rules.js +203 -471
- package/dist/integrations/github/rules.js.map +1 -1
- package/dist/integrations/github/sandbox.d.ts +2 -0
- package/dist/integrations/github/sandbox.d.ts.map +1 -1
- package/dist/integrations/github/sandbox.js +401 -326
- package/dist/integrations/github/sandbox.js.map +1 -1
- package/dist/integrations/github/session-subscriptions.js +165 -223
- package/dist/integrations/github/session-subscriptions.js.map +1 -1
- package/dist/integrations/github/subscriptions.js +43 -51
- package/dist/integrations/github/subscriptions.js.map +1 -1
- package/dist/integrations/github/token-refresh.js +15 -18
- package/dist/integrations/github/token-refresh.js.map +1 -1
- package/dist/integrations/github/webhook.js +359 -343
- package/dist/integrations/github/webhook.js.map +1 -1
- package/dist/integrations/linear/agent-tools.js +77 -100
- package/dist/integrations/linear/agent-tools.js.map +1 -1
- package/dist/integrations/linear/integration.js +611 -1309
- package/dist/integrations/linear/integration.js.map +1 -1
- package/dist/integrations/linear/routes.js +227 -230
- package/dist/integrations/linear/routes.js.map +1 -1
- package/dist/integrations/linear/rules.js +126 -346
- package/dist/integrations/linear/rules.js.map +1 -1
- package/dist/integrations/linear/storage.js +1 -1
- package/dist/integrations/platform/api-client.js +131 -135
- package/dist/integrations/platform/api-client.js.map +1 -1
- package/dist/integrations/platform/github/event-worker.js +275 -573
- package/dist/integrations/platform/github/event-worker.js.map +1 -1
- package/dist/integrations/platform/github/integration.d.ts.map +1 -1
- package/dist/integrations/platform/github/integration.js +845 -3942
- package/dist/integrations/platform/github/integration.js.map +1 -1
- package/dist/integrations/platform/linear/integration.js +457 -1309
- package/dist/integrations/platform/linear/integration.js.map +1 -1
- package/dist/integrations/workos/integration.js +80 -85
- package/dist/integrations/workos/integration.js.map +1 -1
- package/dist/routes/config.d.ts.map +1 -1
- package/dist/routes/config.js +933 -915
- package/dist/routes/config.js.map +1 -1
- package/dist/routes/custom-provider-source.js +110 -93
- package/dist/routes/custom-provider-source.js.map +1 -1
- package/dist/routes/fs.js +401 -349
- package/dist/routes/fs.js.map +1 -1
- package/dist/routes/intake.js +165 -165
- package/dist/routes/intake.js.map +1 -1
- package/dist/routes/oauth.js +447 -25166
- package/dist/routes/oauth.js.map +1 -1
- package/dist/routes/projects.js +437 -376
- package/dist/routes/projects.js.map +1 -1
- package/dist/routes/provider-credentials.js +76 -58
- package/dist/routes/provider-credentials.js.map +1 -1
- package/dist/routes/route.js +12 -8
- package/dist/routes/route.js.map +1 -1
- package/dist/routes/skills.js +160 -173
- package/dist/routes/skills.js.map +1 -1
- package/dist/routes/surface.js +250 -28618
- package/dist/routes/surface.js.map +1 -1
- package/dist/routes/tenant-credentials.js +141 -166
- package/dist/routes/tenant-credentials.js.map +1 -1
- package/dist/routes/work-items.js +654 -862
- package/dist/routes/work-items.js.map +1 -1
- package/dist/rules/binding-context.js +23 -35
- package/dist/rules/binding-context.js.map +1 -1
- package/dist/rules/defaults.js +200 -309
- package/dist/rules/defaults.js.map +1 -1
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js +438 -677
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/index.js +5 -599
- package/dist/rules/processor.js +344 -596
- package/dist/rules/processor.js.map +1 -1
- package/dist/rules/resolve.js +21 -18
- package/dist/rules/resolve.js.map +1 -1
- package/dist/rules/start-coordinator.js +168 -157
- package/dist/rules/start-coordinator.js.map +1 -1
- package/dist/rules/tools.js +50 -85
- package/dist/rules/tools.js.map +1 -1
- package/dist/rules/transition-service.js +183 -454
- package/dist/rules/transition-service.js.map +1 -1
- package/dist/rules/types.js +33 -29
- package/dist/rules/types.js.map +1 -1
- package/dist/rules/validation.js +266 -281
- package/dist/rules/validation.js.map +1 -1
- package/dist/sandbox/fleet.js +272 -250
- package/dist/sandbox/fleet.js.map +1 -1
- package/dist/sandbox/reattach.js +13 -6
- package/dist/sandbox/reattach.js.map +1 -1
- package/dist/server-error.js +12 -16
- package/dist/server-error.js.map +1 -1
- package/dist/skills/service.js +30 -35
- package/dist/skills/service.js.map +1 -1
- package/dist/spa-static.d.ts.map +1 -1
- package/dist/spa-static.js +91 -64
- package/dist/spa-static.js.map +1 -1
- package/dist/state-signing.js +73 -44
- package/dist/state-signing.js.map +1 -1
- package/dist/storage/domains/audit/agent-audit.js +60 -59
- package/dist/storage/domains/audit/agent-audit.js.map +1 -1
- package/dist/storage/domains/audit/base.js +169 -112
- package/dist/storage/domains/audit/base.js.map +1 -1
- package/dist/storage/domains/audit/domain.js +155 -153
- package/dist/storage/domains/audit/domain.js.map +1 -1
- package/dist/storage/domains/credentials/base.js +259 -243
- package/dist/storage/domains/credentials/base.js.map +1 -1
- package/dist/storage/domains/custom-providers/base.js +138 -143
- package/dist/storage/domains/custom-providers/base.js.map +1 -1
- package/dist/storage/domains/intake/base.js +75 -57
- package/dist/storage/domains/intake/base.js.map +1 -1
- package/dist/storage/domains/integrations/base.js +310 -245
- package/dist/storage/domains/integrations/base.js.map +1 -1
- package/dist/storage/domains/memory-settings/base.js +117 -102
- package/dist/storage/domains/memory-settings/base.js.map +1 -1
- package/dist/storage/domains/model-packs/base.js +89 -88
- package/dist/storage/domains/model-packs/base.js.map +1 -1
- package/dist/storage/domains/projects/base.js +97 -92
- package/dist/storage/domains/projects/base.js.map +1 -1
- package/dist/storage/domains/queue-health/base.js +111 -76
- package/dist/storage/domains/queue-health/base.js.map +1 -1
- package/dist/storage/domains/source-control/base.d.ts +9 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -1
- package/dist/storage/domains/source-control/base.js +777 -702
- package/dist/storage/domains/source-control/base.js.map +1 -1
- package/dist/storage/domains/source-control/inmemory.d.ts +4 -0
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
- package/dist/storage/domains/source-control/inmemory.js +322 -344
- package/dist/storage/domains/source-control/inmemory.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +1266 -1132
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/storage/domains/work-items/metrics.js +202 -184
- package/dist/storage/domains/work-items/metrics.js.map +1 -1
- package/dist/timing.d.ts +15 -0
- package/dist/timing.d.ts.map +1 -0
- package/dist/timing.js +34 -0
- package/dist/timing.js.map +1 -0
- package/dist/workspace.d.ts +2 -3
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +236 -438
- package/dist/workspace.js.map +1 -1
- package/factory-skills/factory-review/SKILL.md +9 -2
- package/package.json +9 -9
- package/dist/capabilities/connection.js.map +0 -1
- package/dist/capabilities/intake.js.map +0 -1
- package/dist/capabilities/version-control.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/integrations/base.js.map +0 -1
- package/dist/integrations/linear/storage.js.map +0 -1
- package/dist/rules/index.js.map +0 -1
|
@@ -1,252 +1,317 @@
|
|
|
1
|
-
// src/storage/domains/integrations/base.ts
|
|
2
1
|
import { FactoryStorageDomain, UniqueViolationError } from "@mastra/core/storage";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
2
|
+
//#region src/storage/domains/integrations/base.ts
|
|
3
|
+
/**
|
|
4
|
+
* Generic integration storage — the default persistence surface for
|
|
5
|
+
* `FactoryIntegration`s.
|
|
6
|
+
*
|
|
7
|
+
* Most integrations persist the same three shapes: an org-owned OAuth
|
|
8
|
+
* **connection**, **subscriptions** binding external objects to
|
|
9
|
+
* sessions/threads, and per-`(org, user)` **settings**. This built-in domain
|
|
10
|
+
* stores all three in shared collections keyed by `integration_id`, so an
|
|
11
|
+
* integration author writes zero schema, zero DDL, and zero queries — and
|
|
12
|
+
* org scoping is enforced by construction through the pre-scoped handle
|
|
13
|
+
* returned by {@link IntegrationStorage.forIntegration}.
|
|
14
|
+
*
|
|
15
|
+
* Source-control integrations additionally use the shared, provider-scoped
|
|
16
|
+
* source-control domain for installations, projects, worktrees, and sandboxes.
|
|
17
|
+
*
|
|
18
|
+
* Payloads (`data` / `config`) are JSON documents typed per-integration via
|
|
19
|
+
* the handle's generics. JSON round-trips exactly what JSON can represent:
|
|
20
|
+
* store timestamps inside payloads as epoch millis or ISO strings, not
|
|
21
|
+
* `Date`s.
|
|
22
|
+
*/
|
|
23
|
+
const INTEGRATION_CONNECTIONS_SCHEMA = {
|
|
24
|
+
name: "integration_connections",
|
|
25
|
+
columns: {
|
|
26
|
+
id: { type: "uuid-pk" },
|
|
27
|
+
integration_id: { type: "text" },
|
|
28
|
+
org_id: { type: "text" },
|
|
29
|
+
/** Who connected it (audit only — never scopes reads). */
|
|
30
|
+
user_id: {
|
|
31
|
+
type: "text",
|
|
32
|
+
nullable: true
|
|
33
|
+
},
|
|
34
|
+
data: { type: "json" },
|
|
35
|
+
metadata: { type: "json" },
|
|
36
|
+
created_at: { type: "timestamp" },
|
|
37
|
+
updated_at: { type: "timestamp" }
|
|
38
|
+
},
|
|
39
|
+
uniqueIndexes: [{
|
|
40
|
+
name: "integration_connections_integration_org_unique",
|
|
41
|
+
columns: ["integration_id", "org_id"]
|
|
42
|
+
}]
|
|
17
43
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
const INTEGRATION_SUBSCRIPTIONS_SCHEMA = {
|
|
45
|
+
name: "integration_subscriptions",
|
|
46
|
+
columns: {
|
|
47
|
+
id: { type: "uuid-pk" },
|
|
48
|
+
integration_id: { type: "text" },
|
|
49
|
+
org_id: { type: "text" },
|
|
50
|
+
/** Canonical external-object key the integration derives (e.g. `pr:{installation}:{repo}:{number}`). */
|
|
51
|
+
target_key: { type: "text" },
|
|
52
|
+
session_id: {
|
|
53
|
+
type: "text",
|
|
54
|
+
nullable: true
|
|
55
|
+
},
|
|
56
|
+
resource_id: {
|
|
57
|
+
type: "text",
|
|
58
|
+
nullable: true
|
|
59
|
+
},
|
|
60
|
+
thread_id: {
|
|
61
|
+
type: "text",
|
|
62
|
+
nullable: true
|
|
63
|
+
},
|
|
64
|
+
session_scope: {
|
|
65
|
+
type: "text",
|
|
66
|
+
nullable: true
|
|
67
|
+
},
|
|
68
|
+
status: { type: "text" },
|
|
69
|
+
data: { type: "json" },
|
|
70
|
+
created_at: { type: "timestamp" },
|
|
71
|
+
updated_at: { type: "timestamp" }
|
|
72
|
+
},
|
|
73
|
+
indexes: [{
|
|
74
|
+
name: "integration_subscriptions_target_idx",
|
|
75
|
+
columns: ["integration_id", "target_key"]
|
|
76
|
+
}, {
|
|
77
|
+
name: "integration_subscriptions_org_idx",
|
|
78
|
+
columns: ["integration_id", "org_id"]
|
|
79
|
+
}]
|
|
39
80
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
81
|
+
const INTEGRATION_SETTINGS_SCHEMA = {
|
|
82
|
+
name: "integration_settings",
|
|
83
|
+
columns: {
|
|
84
|
+
id: { type: "uuid-pk" },
|
|
85
|
+
integration_id: { type: "text" },
|
|
86
|
+
org_id: { type: "text" },
|
|
87
|
+
user_id: { type: "text" },
|
|
88
|
+
config: { type: "json" },
|
|
89
|
+
created_at: { type: "timestamp" },
|
|
90
|
+
updated_at: { type: "timestamp" }
|
|
91
|
+
},
|
|
92
|
+
uniqueIndexes: [{
|
|
93
|
+
name: "integration_settings_integration_org_user_unique",
|
|
94
|
+
columns: [
|
|
95
|
+
"integration_id",
|
|
96
|
+
"org_id",
|
|
97
|
+
"user_id"
|
|
98
|
+
]
|
|
99
|
+
}]
|
|
54
100
|
};
|
|
101
|
+
/**
|
|
102
|
+
* The built-in generic integration domain, written once against the
|
|
103
|
+
* `FactoryStorageOps` surface — works on any `FactoryStorage` backend.
|
|
104
|
+
*/
|
|
55
105
|
var IntegrationStorage = class extends FactoryStorageDomain {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
106
|
+
constructor() {
|
|
107
|
+
super("integrations");
|
|
108
|
+
}
|
|
109
|
+
async init() {
|
|
110
|
+
await this.ensureCollections([
|
|
111
|
+
INTEGRATION_CONNECTIONS_SCHEMA,
|
|
112
|
+
INTEGRATION_SUBSCRIPTIONS_SCHEMA,
|
|
113
|
+
INTEGRATION_SETTINGS_SCHEMA
|
|
114
|
+
]);
|
|
115
|
+
}
|
|
116
|
+
async dangerouslyClearAll() {
|
|
117
|
+
await this.ops.deleteMany("integration_subscriptions", {});
|
|
118
|
+
await this.ops.deleteMany("integration_settings", {});
|
|
119
|
+
await this.ops.deleteMany("integration_connections", {});
|
|
120
|
+
}
|
|
121
|
+
get #db() {
|
|
122
|
+
return this.ops;
|
|
123
|
+
}
|
|
124
|
+
/** A typed handle pre-scoped to `integrationId`. */
|
|
125
|
+
forIntegration(integrationId) {
|
|
126
|
+
if (!integrationId) throw new Error("[IntegrationStorage] integrationId is required.");
|
|
127
|
+
const db = () => this.#db;
|
|
128
|
+
const scoped = { integration_id: integrationId };
|
|
129
|
+
const mapConnection = (row) => ({
|
|
130
|
+
id: row.id,
|
|
131
|
+
orgId: row.org_id,
|
|
132
|
+
userId: row.user_id,
|
|
133
|
+
data: row.data,
|
|
134
|
+
metadata: row.metadata ?? {},
|
|
135
|
+
createdAt: row.created_at,
|
|
136
|
+
updatedAt: row.updated_at
|
|
137
|
+
});
|
|
138
|
+
const mapSubscription = (row) => ({
|
|
139
|
+
id: row.id,
|
|
140
|
+
orgId: row.org_id,
|
|
141
|
+
targetKey: row.target_key,
|
|
142
|
+
sessionId: row.session_id,
|
|
143
|
+
resourceId: row.resource_id,
|
|
144
|
+
threadId: row.thread_id,
|
|
145
|
+
sessionScope: row.session_scope,
|
|
146
|
+
status: row.status,
|
|
147
|
+
data: row.data,
|
|
148
|
+
createdAt: row.created_at,
|
|
149
|
+
updatedAt: row.updated_at
|
|
150
|
+
});
|
|
151
|
+
return {
|
|
152
|
+
integrationId,
|
|
153
|
+
connections: {
|
|
154
|
+
get: async (orgId) => {
|
|
155
|
+
const row = await db().findOne("integration_connections", {
|
|
156
|
+
...scoped,
|
|
157
|
+
org_id: orgId
|
|
158
|
+
});
|
|
159
|
+
return row ? mapConnection(row) : null;
|
|
160
|
+
},
|
|
161
|
+
upsert: async (orgId, input) => {
|
|
162
|
+
const where = {
|
|
163
|
+
...scoped,
|
|
164
|
+
org_id: orgId
|
|
165
|
+
};
|
|
166
|
+
const update = async () => db().updateMany("integration_connections", where, {
|
|
167
|
+
user_id: input.userId ?? null,
|
|
168
|
+
data: input.data,
|
|
169
|
+
metadata: input.metadata ?? {},
|
|
170
|
+
updated_at: /* @__PURE__ */ new Date()
|
|
171
|
+
});
|
|
172
|
+
let lastError;
|
|
173
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
174
|
+
if (await update() > 0) return;
|
|
175
|
+
const now = /* @__PURE__ */ new Date();
|
|
176
|
+
try {
|
|
177
|
+
await db().insertOne("integration_connections", {
|
|
178
|
+
...where,
|
|
179
|
+
user_id: input.userId ?? null,
|
|
180
|
+
data: input.data,
|
|
181
|
+
metadata: input.metadata ?? {},
|
|
182
|
+
created_at: now,
|
|
183
|
+
updated_at: now
|
|
184
|
+
});
|
|
185
|
+
return;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
if (!(error instanceof UniqueViolationError)) throw error;
|
|
188
|
+
lastError = error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
throw lastError;
|
|
192
|
+
},
|
|
193
|
+
update: async (orgId, fn) => {
|
|
194
|
+
const row = await db().updateAtomic("integration_connections", {
|
|
195
|
+
...scoped,
|
|
196
|
+
org_id: orgId
|
|
197
|
+
}, (current) => ({
|
|
198
|
+
data: fn(current.data),
|
|
199
|
+
updated_at: /* @__PURE__ */ new Date()
|
|
200
|
+
}));
|
|
201
|
+
return row ? mapConnection(row) : null;
|
|
202
|
+
},
|
|
203
|
+
delete: async (orgId) => {
|
|
204
|
+
return await db().deleteMany("integration_connections", {
|
|
205
|
+
...scoped,
|
|
206
|
+
org_id: orgId
|
|
207
|
+
}) > 0;
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
subscriptions: {
|
|
211
|
+
create: async (input) => {
|
|
212
|
+
const now = /* @__PURE__ */ new Date();
|
|
213
|
+
const row = await db().insertOne("integration_subscriptions", {
|
|
214
|
+
...scoped,
|
|
215
|
+
org_id: input.orgId,
|
|
216
|
+
target_key: input.targetKey,
|
|
217
|
+
session_id: input.sessionId ?? null,
|
|
218
|
+
resource_id: input.resourceId ?? null,
|
|
219
|
+
thread_id: input.threadId ?? null,
|
|
220
|
+
session_scope: input.sessionScope ?? null,
|
|
221
|
+
status: input.status ?? "active",
|
|
222
|
+
data: input.data ?? {},
|
|
223
|
+
created_at: now,
|
|
224
|
+
updated_at: now
|
|
225
|
+
});
|
|
226
|
+
return mapSubscription(row);
|
|
227
|
+
},
|
|
228
|
+
listByTarget: async (targetKey, opts) => {
|
|
229
|
+
return (await db().findMany("integration_subscriptions", {
|
|
230
|
+
...scoped,
|
|
231
|
+
target_key: targetKey,
|
|
232
|
+
...opts?.status !== void 0 ? { status: opts.status } : {}
|
|
233
|
+
}, { orderBy: [["created_at", "asc"]] })).map(mapSubscription);
|
|
234
|
+
},
|
|
235
|
+
listBySession: async (sessionId) => {
|
|
236
|
+
return (await db().findMany("integration_subscriptions", {
|
|
237
|
+
...scoped,
|
|
238
|
+
session_id: sessionId
|
|
239
|
+
}, { orderBy: [["created_at", "asc"]] })).map(mapSubscription);
|
|
240
|
+
},
|
|
241
|
+
listByThread: async (resourceId, threadId) => {
|
|
242
|
+
return (await db().findMany("integration_subscriptions", {
|
|
243
|
+
...scoped,
|
|
244
|
+
resource_id: resourceId,
|
|
245
|
+
thread_id: threadId
|
|
246
|
+
}, { orderBy: [["created_at", "asc"]] })).map(mapSubscription);
|
|
247
|
+
},
|
|
248
|
+
updateStatus: async (id, status) => {
|
|
249
|
+
await db().updateMany("integration_subscriptions", {
|
|
250
|
+
...scoped,
|
|
251
|
+
id
|
|
252
|
+
}, {
|
|
253
|
+
status,
|
|
254
|
+
updated_at: /* @__PURE__ */ new Date()
|
|
255
|
+
});
|
|
256
|
+
},
|
|
257
|
+
delete: async (id) => {
|
|
258
|
+
return await db().deleteMany("integration_subscriptions", {
|
|
259
|
+
...scoped,
|
|
260
|
+
id
|
|
261
|
+
}) > 0;
|
|
262
|
+
},
|
|
263
|
+
deleteWhere: async (where) => {
|
|
264
|
+
return db().deleteMany("integration_subscriptions", {
|
|
265
|
+
...scoped,
|
|
266
|
+
org_id: where.orgId,
|
|
267
|
+
...where.targetKey !== void 0 ? { target_key: where.targetKey } : {},
|
|
268
|
+
...where.sessionId !== void 0 ? { session_id: where.sessionId } : {}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
settings: {
|
|
273
|
+
get: async (orgId, userId) => {
|
|
274
|
+
const row = await db().findOne("integration_settings", {
|
|
275
|
+
...scoped,
|
|
276
|
+
org_id: orgId,
|
|
277
|
+
user_id: userId
|
|
278
|
+
});
|
|
279
|
+
return row ? structuredClone(row.config) : null;
|
|
280
|
+
},
|
|
281
|
+
save: async (orgId, userId, config) => {
|
|
282
|
+
const where = {
|
|
283
|
+
...scoped,
|
|
284
|
+
org_id: orgId,
|
|
285
|
+
user_id: userId
|
|
286
|
+
};
|
|
287
|
+
const update = async () => db().updateMany("integration_settings", where, {
|
|
288
|
+
config,
|
|
289
|
+
updated_at: /* @__PURE__ */ new Date()
|
|
290
|
+
});
|
|
291
|
+
let lastError;
|
|
292
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
293
|
+
if (await update() > 0) return;
|
|
294
|
+
const now = /* @__PURE__ */ new Date();
|
|
295
|
+
try {
|
|
296
|
+
await db().insertOne("integration_settings", {
|
|
297
|
+
...where,
|
|
298
|
+
config,
|
|
299
|
+
created_at: now,
|
|
300
|
+
updated_at: now
|
|
301
|
+
});
|
|
302
|
+
return;
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if (!(error instanceof UniqueViolationError)) throw error;
|
|
305
|
+
lastError = error;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
throw lastError;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
}
|
|
251
313
|
};
|
|
314
|
+
//#endregion
|
|
315
|
+
export { INTEGRATION_CONNECTIONS_SCHEMA, INTEGRATION_SETTINGS_SCHEMA, INTEGRATION_SUBSCRIPTIONS_SCHEMA, IntegrationStorage };
|
|
316
|
+
|
|
252
317
|
//# sourceMappingURL=base.js.map
|