@tangle-network/agent-integrations 0.1.0 → 0.1.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/README.md +9 -0
- package/dist/index.d.ts +579 -1
- package/dist/index.js +277 -5
- package/dist/index.js.map +1 -1
- package/docs/provider-decision-matrix.md +164 -0
- package/package.json +11 -9
package/README.md
CHANGED
|
@@ -101,6 +101,12 @@ const provider = createHttpIntegrationProvider({
|
|
|
101
101
|
The HTTP adapter keeps product code stable while the backing provider can be
|
|
102
102
|
Nango, Pipedream, Activepieces, a Zapier-style service, or an internal gateway.
|
|
103
103
|
|
|
104
|
+
See [Provider Decision Matrix](./docs/provider-decision-matrix.md) for the
|
|
105
|
+
build-vs-buy policy. The short version: use a vendor gateway only to compress
|
|
106
|
+
time-to-coverage, but keep all product and sandbox code on this package's
|
|
107
|
+
contracts so high-volume or strategic connectors can be moved first-party
|
|
108
|
+
without changing agent code.
|
|
109
|
+
|
|
104
110
|
## Security Defaults
|
|
105
111
|
|
|
106
112
|
- Capabilities expire.
|
|
@@ -109,3 +115,6 @@ Nango, Pipedream, Activepieces, a Zapier-style service, or an internal gateway.
|
|
|
109
115
|
- Write/destructive actions can be policy-gated.
|
|
110
116
|
- Action invocation checks connection ownership, status, scopes, allowed
|
|
111
117
|
actions, and expiration.
|
|
118
|
+
- Optional `IntegrationActionGuard` wraps every action invocation for
|
|
119
|
+
idempotency, audit logging, conflict detection, rate limits, and
|
|
120
|
+
approval gates.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,540 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connector primitives — the contract a concrete first-party integration
|
|
3
|
+
* (Google Calendar, HubSpot, Stripe, ...) implements. Lower level than the
|
|
4
|
+
* hub-side `IntegrationProvider` interface from `../index.ts`: a single
|
|
5
|
+
* `IntegrationProvider` typically wraps several connectors (e.g., a
|
|
6
|
+
* "first-party" provider that lists all your shipped connectors as a
|
|
7
|
+
* single catalog).
|
|
8
|
+
*
|
|
9
|
+
* Layering:
|
|
10
|
+
*
|
|
11
|
+
* IntegrationHub — vendor-neutral facade (../index.ts)
|
|
12
|
+
* ↓
|
|
13
|
+
* IntegrationProvider — one per vendor (Nango/Composio/first-party)
|
|
14
|
+
* ↓
|
|
15
|
+
* ConnectorAdapter (this file) — one per integration (Google Calendar, ...)
|
|
16
|
+
* ↓
|
|
17
|
+
* upstream HTTP API — vendor SDK / fetch / OAuth
|
|
18
|
+
*
|
|
19
|
+
* Three load-bearing decisions encoded here:
|
|
20
|
+
*
|
|
21
|
+
* 1. Capabilities are typed (`read` vs `mutation`). Every mutation MUST
|
|
22
|
+
* declare a CAS strategy. Conflict resolution is the SDK's job, not the
|
|
23
|
+
* connector's. `validateConnectorManifest()` rejects unsafe manifests
|
|
24
|
+
* before a connector is registered.
|
|
25
|
+
*
|
|
26
|
+
* 2. ConsistencyModel pins what the rest of the system can assume:
|
|
27
|
+
* authoritative → the source IS the truth (Calendar, payments)
|
|
28
|
+
* cache → we mirror with TTL and may serve stale (price list)
|
|
29
|
+
* advisory → informational only (FAQ doc)
|
|
30
|
+
* Agent planners can (and should) refuse to promise outcomes based on
|
|
31
|
+
* `cache`/`advisory` data without a live `authoritative` confirmation.
|
|
32
|
+
*
|
|
33
|
+
* 3. Capabilities surface to the calling agent's tool registry by
|
|
34
|
+
* transformation, not by hand-wiring. Adding a connector automatically
|
|
35
|
+
* expands the agent's toolbelt for that specific user without touching
|
|
36
|
+
* the prompt or runner.
|
|
37
|
+
*/
|
|
38
|
+
/** Minimal JSON-schema shape used for capability arg validation. We
|
|
39
|
+
* intentionally don't pull `@types/json-schema` — most consumers already
|
|
40
|
+
* declare parameters as `Record<string, unknown>` and the
|
|
41
|
+
* shape is whatever the LLM SDK's structured-output expects. Keep the
|
|
42
|
+
* contract loose at the boundary; tighten via runtime zod where needed. */
|
|
43
|
+
type CapabilityParameterSchema = Record<string, unknown>;
|
|
44
|
+
/** What the rest of the system is allowed to assume about freshness. */
|
|
45
|
+
type ConsistencyModel = 'authoritative' | 'cache' | 'advisory';
|
|
46
|
+
/** Capability classes. `read` is safe to retry; `mutation` must go through
|
|
47
|
+
* MutationGuard (CAS + idempotency). `subscribe` is reserved for future
|
|
48
|
+
* push-driven sources (webhook callbacks) and is not yet wired. */
|
|
49
|
+
type CapabilityClass = 'read' | 'mutation' | 'subscribe';
|
|
50
|
+
/** Compare-and-swap strategy a mutation uses to detect conflicts. */
|
|
51
|
+
type CASStrategy =
|
|
52
|
+
/** Upstream returns an etag/sequence on read, accepts If-Match on write
|
|
53
|
+
* (Google Calendar, GitHub, GDocs revision_id). The connector returns
|
|
54
|
+
* 412 / Precondition Failed on conflict; the SDK maps to ResourceContention. */
|
|
55
|
+
'etag-if-match'
|
|
56
|
+
/** Upstream guarantees exactly-once-per-key (Stripe, idempotent webhooks).
|
|
57
|
+
* The SDK passes the idempotency key through; no etag check. */
|
|
58
|
+
| 'native-idempotency'
|
|
59
|
+
/** No upstream concurrency control. Connector MUST do read-then-write
|
|
60
|
+
* and verify nothing changed in-between (best-effort). Suitable only
|
|
61
|
+
* for low-contention single-user resources; rejected for any
|
|
62
|
+
* consistencyModel='authoritative' write that may race. */
|
|
63
|
+
| 'optimistic-read-verify'
|
|
64
|
+
/** Source is not contended (e.g. logging, telemetry). Mutations are
|
|
65
|
+
* fire-and-forget. Marks the capability as not eligible for
|
|
66
|
+
* authoritative writes. */
|
|
67
|
+
| 'none';
|
|
68
|
+
interface CapabilityRead {
|
|
69
|
+
name: string;
|
|
70
|
+
class: 'read';
|
|
71
|
+
description: string;
|
|
72
|
+
/** JSON-schema for the tool args the agent passes when invoking. */
|
|
73
|
+
parameters: CapabilityParameterSchema;
|
|
74
|
+
/** Optional: declare which scopes (per the connector manifest) this
|
|
75
|
+
* capability requires. The capability is hidden from the agent's
|
|
76
|
+
* tool registry if the user's grant didn't include them. */
|
|
77
|
+
requiredScopes?: string[];
|
|
78
|
+
}
|
|
79
|
+
interface CapabilityMutation {
|
|
80
|
+
name: string;
|
|
81
|
+
class: 'mutation';
|
|
82
|
+
description: string;
|
|
83
|
+
parameters: CapabilityParameterSchema;
|
|
84
|
+
/** Mandatory: how does the connector guarantee at-most-once + conflict-detect? */
|
|
85
|
+
cas: CASStrategy;
|
|
86
|
+
/** True for capabilities that affect resources outside the calling user
|
|
87
|
+
* (e.g. booking against a shared calendar, charging a card). The agent's
|
|
88
|
+
* planner treats these specially: requires explicit caller confirmation
|
|
89
|
+
* before the call. */
|
|
90
|
+
externalEffect: boolean;
|
|
91
|
+
requiredScopes?: string[];
|
|
92
|
+
}
|
|
93
|
+
type Capability = CapabilityRead | CapabilityMutation;
|
|
94
|
+
/** OAuth2 scope catalog the user has granted us, plus arbitrary metadata
|
|
95
|
+
* the connector pinned at connect-time (calendar id, sheet id, webhook
|
|
96
|
+
* url, …). `metadata` MUST NOT contain secrets — those go in the
|
|
97
|
+
* encrypted credentials envelope. */
|
|
98
|
+
interface DataSourceMetadata {
|
|
99
|
+
scopes: string[];
|
|
100
|
+
[key: string]: unknown;
|
|
101
|
+
}
|
|
102
|
+
/** A connected, authenticated, ready-to-call data source for a project.
|
|
103
|
+
* Persistence shape mirrors the product's connection/source row but normalized — the
|
|
104
|
+
* encrypted credentials envelope is decrypted at hand-out time and held
|
|
105
|
+
* in memory only for the duration of the call. */
|
|
106
|
+
interface ResolvedDataSource {
|
|
107
|
+
id: string;
|
|
108
|
+
projectId: string;
|
|
109
|
+
publishedAgentId: string | null;
|
|
110
|
+
kind: string;
|
|
111
|
+
label: string;
|
|
112
|
+
consistencyModel: ConsistencyModel;
|
|
113
|
+
scopes: string[];
|
|
114
|
+
metadata: Record<string, unknown>;
|
|
115
|
+
/** Unwrapped credentials handed to the connector at call-time. Never
|
|
116
|
+
* persisted in this shape; never logged. */
|
|
117
|
+
credentials: ConnectorCredentials;
|
|
118
|
+
status: 'active' | 'revoked' | 'error';
|
|
119
|
+
}
|
|
120
|
+
/** Discriminated union of credential shapes. Connectors that need new
|
|
121
|
+
* shapes extend this union — `kind` is sealed via the tagged pattern so
|
|
122
|
+
* TypeScript catches an exhaustiveness gap at compile time. */
|
|
123
|
+
type ConnectorCredentials = {
|
|
124
|
+
kind: 'oauth2';
|
|
125
|
+
accessToken: string;
|
|
126
|
+
refreshToken?: string;
|
|
127
|
+
expiresAt?: number;
|
|
128
|
+
} | {
|
|
129
|
+
kind: 'api-key';
|
|
130
|
+
apiKey: string;
|
|
131
|
+
} | {
|
|
132
|
+
kind: 'hmac';
|
|
133
|
+
secret: string;
|
|
134
|
+
} | {
|
|
135
|
+
kind: 'none';
|
|
136
|
+
};
|
|
137
|
+
/** Result of a read capability invocation. */
|
|
138
|
+
interface CapabilityReadResult {
|
|
139
|
+
/** Free-form payload — the connector's data shape. The agent receives
|
|
140
|
+
* this as the tool result; planners consume it via JSON-shape contract
|
|
141
|
+
* declared in the capability's `parameters` (output schema). */
|
|
142
|
+
data: unknown;
|
|
143
|
+
/** Optional etag/sequence the caller can reuse for a subsequent CAS
|
|
144
|
+
* mutation. */
|
|
145
|
+
etag?: string;
|
|
146
|
+
/** When this read happened (UTC ms since epoch). */
|
|
147
|
+
fetchedAt: number;
|
|
148
|
+
}
|
|
149
|
+
/** Result of a mutation capability invocation. Either committed (with the
|
|
150
|
+
* resulting etag/sequence so the caller can chain mutations), or
|
|
151
|
+
* contended (the upstream rejected with a state mismatch — the agent
|
|
152
|
+
* should re-read and retry, or surface alternatives to the caller). */
|
|
153
|
+
type CapabilityMutationResult = {
|
|
154
|
+
status: 'committed';
|
|
155
|
+
data: unknown;
|
|
156
|
+
etagAfter?: string;
|
|
157
|
+
committedAt: number;
|
|
158
|
+
/** True iff this commit was returned from the idempotency store
|
|
159
|
+
* rather than executed against upstream. The caller can use this
|
|
160
|
+
* to suppress confirmation messages on retry. */
|
|
161
|
+
idempotentReplay: boolean;
|
|
162
|
+
} | {
|
|
163
|
+
status: 'conflict';
|
|
164
|
+
/** Best-effort alternative options the upstream surfaced (e.g.,
|
|
165
|
+
* next-available calendar slots after a booking conflict). */
|
|
166
|
+
alternatives: unknown[];
|
|
167
|
+
/** The current authoritative state, if the connector could re-read
|
|
168
|
+
* cheaply. */
|
|
169
|
+
currentState?: unknown;
|
|
170
|
+
message: string;
|
|
171
|
+
} | {
|
|
172
|
+
status: 'rate-limited';
|
|
173
|
+
/** Wall-clock ms the caller should wait before retrying. The SDK
|
|
174
|
+
* computes this from the bucket's refill schedule so the agent
|
|
175
|
+
* doesn't have to guess. */
|
|
176
|
+
retryAfterMs: number;
|
|
177
|
+
message: string;
|
|
178
|
+
};
|
|
179
|
+
/** Inputs the SDK passes into the connector's executeRead / executeMutation. */
|
|
180
|
+
interface ConnectorInvocation {
|
|
181
|
+
source: ResolvedDataSource;
|
|
182
|
+
capabilityName: string;
|
|
183
|
+
args: Record<string, unknown>;
|
|
184
|
+
/** Idempotency key the caller (or the SDK's defaulting policy) supplied.
|
|
185
|
+
* Always present at the connector boundary — the SDK manufactures one
|
|
186
|
+
* if the agent didn't pass one. */
|
|
187
|
+
idempotencyKey: string;
|
|
188
|
+
/** Optional caller-supplied etag the connector should send as If-Match. */
|
|
189
|
+
expectedEtag?: string;
|
|
190
|
+
/** Product/session id (if any) for forensic logging. */
|
|
191
|
+
callSessionId?: string;
|
|
192
|
+
}
|
|
193
|
+
/** A single inbound event extracted from a push payload. The webhook
|
|
194
|
+
* receiver persists one `InboundEvent` row per entry the connector returns. */
|
|
195
|
+
interface InboundEvent {
|
|
196
|
+
eventType: string;
|
|
197
|
+
providerEventId?: string;
|
|
198
|
+
payload: Record<string, unknown>;
|
|
199
|
+
}
|
|
200
|
+
/** Adapter response from an inbound-webhook dispatch. The receiver persists
|
|
201
|
+
* every `events[]` entry, then either honors the connector's `response`
|
|
202
|
+
* override (Slack `url_verification` echo, provider-specific 2xx body) or
|
|
203
|
+
* defaults to `{status: 200, body: {received: true, count: events.length}}`. */
|
|
204
|
+
interface EventHandlerResult {
|
|
205
|
+
events: InboundEvent[];
|
|
206
|
+
/** Optional: how to respond to the provider. Stripe wants 200 within
|
|
207
|
+
* 30s; Slack wants the challenge param echoed. */
|
|
208
|
+
response?: {
|
|
209
|
+
status: number;
|
|
210
|
+
body: unknown;
|
|
211
|
+
headers?: Record<string, string>;
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Connector adapter — one per integration kind. Stateless. The SDK holds
|
|
216
|
+
* the persistence + crypto + mutation-guard concerns; the adapter only
|
|
217
|
+
* knows how to talk to its upstream.
|
|
218
|
+
*/
|
|
219
|
+
interface ConnectorAdapter {
|
|
220
|
+
/** Manifest entry the registry uses to render UI + validate args. */
|
|
221
|
+
manifest: ConnectorManifest;
|
|
222
|
+
/** Read invocation. Required when manifest.capabilities contains reads.
|
|
223
|
+
* Should return whatever shape the capability declared
|
|
224
|
+
* in its parameters output schema. */
|
|
225
|
+
executeRead?(inv: ConnectorInvocation): Promise<CapabilityReadResult>;
|
|
226
|
+
/** Mutation invocation. Required when manifest.capabilities contains mutations.
|
|
227
|
+
* Throws ResourceContention on a CAS miss; throws
|
|
228
|
+
* any other Error for upstream failures. The MutationGuard wraps this
|
|
229
|
+
* with idempotency-key short-circuit + audit logging — adapters do
|
|
230
|
+
* NOT manage their own dedup. */
|
|
231
|
+
executeMutation?(inv: ConnectorInvocation): Promise<CapabilityMutationResult>;
|
|
232
|
+
/** Inbound webhook signature verifier. Called BEFORE handleInboundEvent.
|
|
233
|
+
* MUST use constant-time comparison (`crypto.timingSafeEqual`) for any
|
|
234
|
+
* HMAC check. The receiver returns 401 on `valid=false` without invoking
|
|
235
|
+
* handleInboundEvent. Optional: connectors that don't accept push events
|
|
236
|
+
* omit this method and the receiver returns 405 for the kind. */
|
|
237
|
+
verifySignature?(input: {
|
|
238
|
+
rawBody: string;
|
|
239
|
+
headers: Record<string, string | string[] | undefined>;
|
|
240
|
+
source: ResolvedDataSource;
|
|
241
|
+
}): {
|
|
242
|
+
valid: boolean;
|
|
243
|
+
reason?: string;
|
|
244
|
+
};
|
|
245
|
+
/** Inbound webhook dispatch. Called AFTER verifySignature passes. The
|
|
246
|
+
* adapter parses the provider payload and emits zero-or-more
|
|
247
|
+
* `InboundEvent` rows; the receiver persists them as one row each (modulo
|
|
248
|
+
* the (dataSourceId, providerEventId) dedup unique). The optional
|
|
249
|
+
* `response` overrides the receiver's default 200 (Slack `url_verification`
|
|
250
|
+
* needs to echo the challenge in the body to pass Slack's app-config check). */
|
|
251
|
+
handleInboundEvent?(input: {
|
|
252
|
+
source: ResolvedDataSource;
|
|
253
|
+
rawBody: string;
|
|
254
|
+
headers: Record<string, string | string[] | undefined>;
|
|
255
|
+
}): Promise<EventHandlerResult>;
|
|
256
|
+
/** OAuth callback handler — exchanges the auth code for tokens, returns
|
|
257
|
+
* the credentials envelope + scopes + metadata. Only present for
|
|
258
|
+
* oauth2-style adapters. */
|
|
259
|
+
exchangeOAuth?(input: {
|
|
260
|
+
code: string;
|
|
261
|
+
state: string;
|
|
262
|
+
codeVerifier: string;
|
|
263
|
+
redirectUri: string;
|
|
264
|
+
}): Promise<{
|
|
265
|
+
credentials: ConnectorCredentials;
|
|
266
|
+
scopes: string[];
|
|
267
|
+
metadata: Record<string, unknown>;
|
|
268
|
+
}>;
|
|
269
|
+
/** Refresh access token. Only required for oauth2 adapters with
|
|
270
|
+
* short-lived access tokens. */
|
|
271
|
+
refreshToken?(input: ConnectorCredentials): Promise<ConnectorCredentials>;
|
|
272
|
+
/** Health check — invoked when the user clicks "Test connection" in the
|
|
273
|
+
* UI. Should perform the cheapest possible read that proves the grant
|
|
274
|
+
* is still valid. Returns `{ok: false, reason}` rather than throwing
|
|
275
|
+
* for the common case (token expired, scope missing). */
|
|
276
|
+
test(source: ResolvedDataSource): Promise<{
|
|
277
|
+
ok: true;
|
|
278
|
+
} | {
|
|
279
|
+
ok: false;
|
|
280
|
+
reason: string;
|
|
281
|
+
}>;
|
|
282
|
+
}
|
|
283
|
+
/** Static manifest a connector module exports. Drives the UI catalog,
|
|
284
|
+
* scope display, capability discovery for the agent's tool registry. */
|
|
285
|
+
interface ConnectorManifest {
|
|
286
|
+
/** Stable kind id used as the foreign key in DataSource.kind. */
|
|
287
|
+
kind: string;
|
|
288
|
+
/** Human label shown in the UI catalog. */
|
|
289
|
+
displayName: string;
|
|
290
|
+
/** One-paragraph description shown next to the connect button. */
|
|
291
|
+
description: string;
|
|
292
|
+
/** Auth shape this connector requires. */
|
|
293
|
+
auth: AuthSpec;
|
|
294
|
+
/** Capability catalog — the agent's tool registry derives ToolDefinition
|
|
295
|
+
* entries from this list at request time. */
|
|
296
|
+
capabilities: Capability[];
|
|
297
|
+
/** ConsistencyModel default for this kind — overridable per DataSource
|
|
298
|
+
* if a particular instance is special (e.g., a user marks a sheet as
|
|
299
|
+
* `cache` because they refresh it nightly). */
|
|
300
|
+
defaultConsistencyModel: ConsistencyModel;
|
|
301
|
+
/** Connector category for UI grouping. */
|
|
302
|
+
category: 'calendar' | 'spreadsheet' | 'crm' | 'doc' | 'webhook' | 'storage' | 'comms' | 'commerce' | 'other';
|
|
303
|
+
/** Optional icon URL or named icon. */
|
|
304
|
+
icon?: string;
|
|
305
|
+
/** Optional per-kind rate-limit budget. The SDK enforces it inside
|
|
306
|
+
* `executeGuardedMutation` and the read path of `/invoke`. Omit to
|
|
307
|
+
* leave the connector unrestricted. */
|
|
308
|
+
rateLimit?: RateLimitSpec;
|
|
309
|
+
}
|
|
310
|
+
/** Token-bucket budget the SDK enforces against the connector's upstream.
|
|
311
|
+
* We meter on OUR side rather than letting the upstream reject so a
|
|
312
|
+
* chatty agent can't burn quota that's shared across customers (almost
|
|
313
|
+
* every OAuth client is). */
|
|
314
|
+
interface RateLimitSpec {
|
|
315
|
+
/** Max requests per window. */
|
|
316
|
+
requests: number;
|
|
317
|
+
/** Window in ms. */
|
|
318
|
+
windowMs: number;
|
|
319
|
+
/** Whether to apply across all DataSources sharing the same OAuth
|
|
320
|
+
* client (true; default), or per-DataSource (false). The former
|
|
321
|
+
* matches how upstreams meter (per-app), so almost always pick true. */
|
|
322
|
+
scope?: 'oauth-client' | 'data-source';
|
|
323
|
+
}
|
|
324
|
+
type AuthSpec = {
|
|
325
|
+
kind: 'oauth2';
|
|
326
|
+
/** Authorization endpoint URL. */
|
|
327
|
+
authorizationUrl: string;
|
|
328
|
+
/** Token endpoint URL. */
|
|
329
|
+
tokenUrl: string;
|
|
330
|
+
/** Scopes requested in the authorization grant. The user UI shows
|
|
331
|
+
* these so the customer knows what's being shared. */
|
|
332
|
+
scopes: string[];
|
|
333
|
+
/** Whether the connector supports incremental authorization (Google
|
|
334
|
+
* does; many don't). */
|
|
335
|
+
incremental?: boolean;
|
|
336
|
+
/** Env-var name holding the OAuth client_id. */
|
|
337
|
+
clientIdEnv: string;
|
|
338
|
+
/** Env-var name holding the OAuth client_secret. */
|
|
339
|
+
clientSecretEnv: string;
|
|
340
|
+
/** Optional extra params attached to the authorization URL (e.g.,
|
|
341
|
+
* Google's `access_type=offline&prompt=consent` to obtain refresh
|
|
342
|
+
* tokens). */
|
|
343
|
+
extraAuthParams?: Record<string, string>;
|
|
344
|
+
} | {
|
|
345
|
+
kind: 'api-key';
|
|
346
|
+
/** UI hint shown when collecting the key. */
|
|
347
|
+
hint: string;
|
|
348
|
+
} | {
|
|
349
|
+
kind: 'hmac';
|
|
350
|
+
} | {
|
|
351
|
+
kind: 'none';
|
|
352
|
+
};
|
|
353
|
+
/** Thrown by `executeMutation` when upstream rejects on CAS — caught and
|
|
354
|
+
* rewrapped by MutationGuard. */
|
|
355
|
+
declare class ResourceContention extends Error {
|
|
356
|
+
readonly alternatives: unknown[];
|
|
357
|
+
readonly currentState?: unknown | undefined;
|
|
358
|
+
readonly name = "ResourceContention";
|
|
359
|
+
constructor(message: string, alternatives?: unknown[], currentState?: unknown | undefined);
|
|
360
|
+
}
|
|
361
|
+
/** Thrown when the connector finds the user's grant has been revoked or
|
|
362
|
+
* the access token is no longer valid AND refresh failed. Surfaces to
|
|
363
|
+
* the UI as "Reconnect required". */
|
|
364
|
+
declare class CredentialsExpired extends Error {
|
|
365
|
+
readonly dataSourceId: string;
|
|
366
|
+
readonly name = "CredentialsExpired";
|
|
367
|
+
constructor(message: string, dataSourceId: string);
|
|
368
|
+
}
|
|
369
|
+
interface ConnectorManifestValidationIssue {
|
|
370
|
+
path: string;
|
|
371
|
+
message: string;
|
|
372
|
+
}
|
|
373
|
+
interface ConnectorManifestValidationResult {
|
|
374
|
+
ok: boolean;
|
|
375
|
+
issues: ConnectorManifestValidationIssue[];
|
|
376
|
+
}
|
|
377
|
+
/** Validate the static connector manifest before a provider registers it.
|
|
378
|
+
* This catches the expensive mistakes early: duplicate capability names,
|
|
379
|
+
* mutation capabilities without CAS, authoritative fire-and-forget writes,
|
|
380
|
+
* and invalid rate-limit specs. */
|
|
381
|
+
declare function validateConnectorManifest(manifest: ConnectorManifest): ConnectorManifestValidationResult;
|
|
382
|
+
declare function assertValidConnectorManifest(manifest: ConnectorManifest): void;
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Generic OAuth2 helper used by every oauth-shaped connector (Google
|
|
386
|
+
* Calendar, Sheets, Drive, HubSpot, Salesforce, Zoom, ...).
|
|
387
|
+
*
|
|
388
|
+
* Everything PKCE-aware. Opaque-state CSRF guard. Refresh-token aware.
|
|
389
|
+
* No connector-specific logic lives here — adapters hand a `clientId`,
|
|
390
|
+
* `clientSecret`, `tokenUrl`, optional `extraAuthParams` and the rest is
|
|
391
|
+
* mechanical.
|
|
392
|
+
*
|
|
393
|
+
* State and code_verifier are kept in a short-TTL flow store keyed by the
|
|
394
|
+
* opaque `state` we round-trip through the provider. The default store is
|
|
395
|
+
* in-memory for local/dev and tests. Production deployments should inject a
|
|
396
|
+
* durable store backed by KV/Redis/D1/etc. so callbacks can land on any worker.
|
|
397
|
+
*/
|
|
398
|
+
interface PendingOAuthFlow {
|
|
399
|
+
/** code_verifier for PKCE. */
|
|
400
|
+
codeVerifier: string;
|
|
401
|
+
/** Opaque-state value also returned in the OAuth redirect. */
|
|
402
|
+
state: string;
|
|
403
|
+
/** Project the user is connecting under. */
|
|
404
|
+
projectId: string;
|
|
405
|
+
/** Connector kind (e.g. 'google-calendar'). */
|
|
406
|
+
kind: string;
|
|
407
|
+
/** Operator-supplied label that becomes DataSource.label. */
|
|
408
|
+
label: string;
|
|
409
|
+
/** When we drop the entry. */
|
|
410
|
+
expiresAt: number;
|
|
411
|
+
/** The redirectUri we used in the start step — must match exactly on
|
|
412
|
+
* the callback exchange. */
|
|
413
|
+
redirectUri: string;
|
|
414
|
+
}
|
|
415
|
+
interface OAuthFlowStore {
|
|
416
|
+
put(state: string, flow: PendingOAuthFlow): Promise<void> | void;
|
|
417
|
+
consume(state: string): Promise<PendingOAuthFlow | undefined> | PendingOAuthFlow | undefined;
|
|
418
|
+
sweep?(now: number): Promise<void> | void;
|
|
419
|
+
clear?(): Promise<void> | void;
|
|
420
|
+
}
|
|
421
|
+
declare class InMemoryOAuthFlowStore implements OAuthFlowStore {
|
|
422
|
+
private readonly pendingFlows;
|
|
423
|
+
put(state: string, flow: PendingOAuthFlow): void;
|
|
424
|
+
consume(state: string): PendingOAuthFlow | undefined;
|
|
425
|
+
sweep(now: number): void;
|
|
426
|
+
clear(): void;
|
|
427
|
+
}
|
|
428
|
+
interface StartOAuthInput {
|
|
429
|
+
projectId: string;
|
|
430
|
+
kind: string;
|
|
431
|
+
label: string;
|
|
432
|
+
authorizationUrl: string;
|
|
433
|
+
scopes: string[];
|
|
434
|
+
clientId: string;
|
|
435
|
+
redirectUri: string;
|
|
436
|
+
/** Optional extra query params; Google needs `access_type=offline` and
|
|
437
|
+
* `prompt=consent` to issue refresh tokens reliably. */
|
|
438
|
+
extraAuthParams?: Record<string, string>;
|
|
439
|
+
/** Optional flow store. Use a durable store in distributed production
|
|
440
|
+
* runtimes; omitted means local in-memory storage. */
|
|
441
|
+
store?: OAuthFlowStore;
|
|
442
|
+
/** Override clock for tests. */
|
|
443
|
+
now?: number;
|
|
444
|
+
}
|
|
445
|
+
interface StartOAuthOutput {
|
|
446
|
+
/** URL the SPA should redirect the user to. */
|
|
447
|
+
authorizationUrl: string;
|
|
448
|
+
/** State token — caller stashes this in localStorage to verify on
|
|
449
|
+
* callback. */
|
|
450
|
+
state: string;
|
|
451
|
+
}
|
|
452
|
+
/** Build the authorization URL + state. SPA navigates the user there;
|
|
453
|
+
* user consents; provider redirects back to redirectUri with `code` +
|
|
454
|
+
* `state`. The caller's callback then invokes `consumePendingFlow`. */
|
|
455
|
+
declare function startOAuthFlow(input: StartOAuthInput): StartOAuthOutput;
|
|
456
|
+
/** Look up + remove the pending flow record. Throws if state is unknown
|
|
457
|
+
* or expired (CSRF guard / replay protection). */
|
|
458
|
+
declare function consumePendingFlow(state: string, store?: OAuthFlowStore): Promise<PendingOAuthFlow>;
|
|
459
|
+
interface ExchangeCodeInput {
|
|
460
|
+
tokenUrl: string;
|
|
461
|
+
clientId: string;
|
|
462
|
+
clientSecret: string;
|
|
463
|
+
code: string;
|
|
464
|
+
codeVerifier: string;
|
|
465
|
+
redirectUri: string;
|
|
466
|
+
}
|
|
467
|
+
interface OAuthTokens {
|
|
468
|
+
accessToken: string;
|
|
469
|
+
refreshToken?: string;
|
|
470
|
+
expiresIn?: number;
|
|
471
|
+
scope?: string;
|
|
472
|
+
tokenType?: string;
|
|
473
|
+
}
|
|
474
|
+
/** POST authorization code → token endpoint. Provider-agnostic; if a
|
|
475
|
+
* provider returns a non-standard JSON shape, the adapter wraps this
|
|
476
|
+
* call rather than reaching into the helper. */
|
|
477
|
+
declare function exchangeAuthorizationCode(input: ExchangeCodeInput): Promise<OAuthTokens>;
|
|
478
|
+
interface RefreshInput {
|
|
479
|
+
tokenUrl: string;
|
|
480
|
+
clientId: string;
|
|
481
|
+
clientSecret: string;
|
|
482
|
+
refreshToken: string;
|
|
483
|
+
}
|
|
484
|
+
/** Refresh an access token. Returns the new tokens — the connector layer
|
|
485
|
+
* is responsible for re-encrypting + persisting the envelope. */
|
|
486
|
+
declare function refreshAccessToken(input: RefreshInput): Promise<OAuthTokens>;
|
|
487
|
+
/** Test-only — drop pending flows between unit-test runs. */
|
|
488
|
+
declare function _resetPendingFlowsForTests(): void;
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Inbound webhook signature verifiers — provider-specific HMAC schemes.
|
|
492
|
+
*
|
|
493
|
+
* Each signature scheme is a pure function:
|
|
494
|
+
* (rawBody: string, headers, secret, now?) → boolean
|
|
495
|
+
*
|
|
496
|
+
* Constant-time comparison via `crypto.timingSafeEqual`. Timestamps are
|
|
497
|
+
* checked against a configurable tolerance to bound replay risk; the default
|
|
498
|
+
* mirrors the upstream provider's documented window (Stripe: 5 min, Slack: 5 min).
|
|
499
|
+
*
|
|
500
|
+
* These verifiers are the building blocks for any inbound-webhook receiver
|
|
501
|
+
* (a route + a `verify` call + a per-event handler). They live in this
|
|
502
|
+
* package so every consumer of the integration substrate gets correct
|
|
503
|
+
* verification — not just one product reimplementing it.
|
|
504
|
+
*/
|
|
505
|
+
/** Default replay-protection window. Providers commonly use 5 minutes. */
|
|
506
|
+
declare const DEFAULT_SIGNATURE_TOLERANCE_SECONDS: number;
|
|
507
|
+
interface ParsedStripeSignatureHeader {
|
|
508
|
+
t: number;
|
|
509
|
+
sigs: string[];
|
|
510
|
+
}
|
|
511
|
+
declare function parseStripeSignatureHeader(header: string): ParsedStripeSignatureHeader | null;
|
|
512
|
+
interface StripeVerifyOptions {
|
|
513
|
+
/** Replay-protection window in seconds. Default 300. */
|
|
514
|
+
toleranceSeconds?: number;
|
|
515
|
+
/** Override `now()` for tests. UTC seconds. */
|
|
516
|
+
now?: number;
|
|
517
|
+
}
|
|
518
|
+
/** Verify a Stripe webhook signature against the raw request body. */
|
|
519
|
+
declare function verifyStripeSignature(rawBody: string, signatureHeader: string, secret: string, options?: StripeVerifyOptions): boolean;
|
|
520
|
+
interface SlackVerifyOptions {
|
|
521
|
+
toleranceSeconds?: number;
|
|
522
|
+
now?: number;
|
|
523
|
+
}
|
|
524
|
+
declare function verifySlackSignature(rawBody: string, signatureHeader: string, timestampHeader: string, secret: string, options?: SlackVerifyOptions): boolean;
|
|
525
|
+
interface GenericHmacVerifyOptions {
|
|
526
|
+
/** sha256 (default) | sha1 | sha512 — matches the algorithm the receiver
|
|
527
|
+
* computed at sign time. */
|
|
528
|
+
algorithm?: 'sha256' | 'sha1' | 'sha512';
|
|
529
|
+
/** Optional prefix the receiver prepends to the signature in the header
|
|
530
|
+
* (e.g., `'sha256='`). Stripped before constant-time comparison. */
|
|
531
|
+
signaturePrefix?: string;
|
|
532
|
+
/** Lowercase comparison (most providers emit hex-lowercase). Default true. */
|
|
533
|
+
lowercaseHex?: boolean;
|
|
534
|
+
}
|
|
535
|
+
declare function verifyHmacSignature(rawBody: string, signatureHeader: string, secret: string, options?: GenericHmacVerifyOptions): boolean;
|
|
536
|
+
declare function firstHeader(headers: Record<string, string | string[] | undefined>, name: string): string | undefined;
|
|
537
|
+
|
|
1
538
|
type IntegrationProviderKind = 'first_party' | 'nango' | 'pipedream' | 'zapier' | 'activepieces' | 'executor' | 'custom';
|
|
2
539
|
type IntegrationConnectorCategory = 'email' | 'calendar' | 'chat' | 'crm' | 'storage' | 'docs' | 'database' | 'webhook' | 'workflow' | 'internal' | 'other';
|
|
3
540
|
type IntegrationActionRisk = 'read' | 'write' | 'destructive';
|
|
@@ -158,10 +695,50 @@ interface IssuedIntegrationCapability {
|
|
|
158
695
|
capability: IntegrationCapability;
|
|
159
696
|
token: string;
|
|
160
697
|
}
|
|
698
|
+
/**
|
|
699
|
+
* Wraps every action invocation with cross-cutting discipline (idempotency,
|
|
700
|
+
* conflict detection, rate-limiting, audit logging). Optional. When set on
|
|
701
|
+
* the hub, runs BEFORE provider.invokeAction; can short-circuit (return a
|
|
702
|
+
* result directly) or pass through (call `proceed()` to invoke the provider).
|
|
703
|
+
*
|
|
704
|
+
* Why this hook exists: production deployments need conflict-resolution
|
|
705
|
+
* guarantees that span every provider — first-party, Nango, Composio,
|
|
706
|
+
* webhook receivers — and providers shouldn't re-implement them. The
|
|
707
|
+
* canonical implementation is a "MutationGuard" that:
|
|
708
|
+
* 1. Short-circuits on a known idempotency key (returns recorded response).
|
|
709
|
+
* 2. Refuses same-key-different-args (drift detection).
|
|
710
|
+
* 3. Wraps `proceed()` and audit-logs the outcome.
|
|
711
|
+
* 4. Translates upstream conflict signals into a structured result with
|
|
712
|
+
* alternatives the agent can act on.
|
|
713
|
+
*
|
|
714
|
+
* Implementations live in consumers (every product has different
|
|
715
|
+
* persistence + telemetry needs); this interface is the contract.
|
|
716
|
+
*/
|
|
717
|
+
interface IntegrationActionGuard {
|
|
718
|
+
/** Wrap an invokeAction call. Implementations MUST call `proceed()` to
|
|
719
|
+
* invoke the underlying provider unless they're returning a cached or
|
|
720
|
+
* short-circuited result.
|
|
721
|
+
*
|
|
722
|
+
* @param ctx connection + request the hub is about to dispatch
|
|
723
|
+
* @param proceed call to invoke the wrapped provider; returns the
|
|
724
|
+
* underlying IntegrationActionResult
|
|
725
|
+
* @returns the result the hub should return to the caller
|
|
726
|
+
*/
|
|
727
|
+
invokeAction(ctx: IntegrationGuardContext, proceed: () => Promise<IntegrationActionResult>): Promise<IntegrationActionResult>;
|
|
728
|
+
}
|
|
729
|
+
interface IntegrationGuardContext {
|
|
730
|
+
connection: IntegrationConnection;
|
|
731
|
+
request: IntegrationActionRequest;
|
|
732
|
+
/** The action descriptor from the connector manifest, if discovered. */
|
|
733
|
+
action?: IntegrationConnectorAction;
|
|
734
|
+
}
|
|
161
735
|
interface IntegrationHubOptions {
|
|
162
736
|
providers: IntegrationProvider[];
|
|
163
737
|
store: IntegrationConnectionStore;
|
|
164
738
|
capabilitySecret: string;
|
|
739
|
+
/** Optional cross-cutting guard. If provided, every invokeAction call
|
|
740
|
+
* passes through it before reaching the provider. See {@link IntegrationActionGuard}. */
|
|
741
|
+
guard?: IntegrationActionGuard;
|
|
165
742
|
now?: () => Date;
|
|
166
743
|
}
|
|
167
744
|
interface HttpIntegrationProviderOptions {
|
|
@@ -190,6 +767,7 @@ declare class IntegrationHub {
|
|
|
190
767
|
private readonly providers;
|
|
191
768
|
private readonly store;
|
|
192
769
|
private readonly capabilitySecret;
|
|
770
|
+
private readonly guard;
|
|
193
771
|
private readonly now;
|
|
194
772
|
constructor(options: IntegrationHubOptions);
|
|
195
773
|
listConnectors(): Promise<IntegrationConnector[]>;
|
|
@@ -215,4 +793,4 @@ declare function createHttpIntegrationProvider(options: HttpIntegrationProviderO
|
|
|
215
793
|
declare function signCapability(capability: IntegrationCapability, secret: string): string;
|
|
216
794
|
declare function verifyCapabilityToken(token: string, secret: string): IntegrationCapability;
|
|
217
795
|
|
|
218
|
-
export { type CompleteAuthRequest, type HttpIntegrationProviderOptions, InMemoryConnectionStore, type IntegrationActionRequest, type IntegrationActionResult, type IntegrationActionRisk, type IntegrationActor, type IntegrationCapability, type IntegrationConnection, type IntegrationConnectionStore, type IntegrationConnector, type IntegrationConnectorAction, type IntegrationConnectorCategory, type IntegrationConnectorTrigger, type IntegrationDataClass, IntegrationError, IntegrationHub, type IntegrationHubOptions, type IntegrationProvider, type IntegrationProviderKind, type IntegrationTriggerEvent, type IntegrationTriggerSubscription, type InvokeWithCapabilityRequest, type IssueCapabilityRequest, type IssuedIntegrationCapability, type SecretRef, type StartAuthRequest, type StartAuthResult, createHttpIntegrationProvider, createMockIntegrationProvider, sanitizeConnection, signCapability, verifyCapabilityToken };
|
|
796
|
+
export { type AuthSpec, type CASStrategy, type Capability, type CapabilityClass, type CapabilityMutation, type CapabilityMutationResult, type CapabilityParameterSchema, type CapabilityRead, type CapabilityReadResult, type CompleteAuthRequest, type ConnectorAdapter, type ConnectorCredentials, type ConnectorInvocation, type ConnectorManifest, type ConnectorManifestValidationIssue, type ConnectorManifestValidationResult, type ConsistencyModel, CredentialsExpired, DEFAULT_SIGNATURE_TOLERANCE_SECONDS, type DataSourceMetadata, type EventHandlerResult, type ExchangeCodeInput, type GenericHmacVerifyOptions, type HttpIntegrationProviderOptions, InMemoryConnectionStore, InMemoryOAuthFlowStore, type InboundEvent, type IntegrationActionGuard, type IntegrationActionRequest, type IntegrationActionResult, type IntegrationActionRisk, type IntegrationActor, type IntegrationCapability, type IntegrationConnection, type IntegrationConnectionStore, type IntegrationConnector, type IntegrationConnectorAction, type IntegrationConnectorCategory, type IntegrationConnectorTrigger, type IntegrationDataClass, IntegrationError, type IntegrationGuardContext, IntegrationHub, type IntegrationHubOptions, type IntegrationProvider, type IntegrationProviderKind, type IntegrationTriggerEvent, type IntegrationTriggerSubscription, type InvokeWithCapabilityRequest, type IssueCapabilityRequest, type IssuedIntegrationCapability, type OAuthFlowStore, type OAuthTokens, type ParsedStripeSignatureHeader, type PendingOAuthFlow, type RateLimitSpec, type RefreshInput, type ResolvedDataSource, ResourceContention, type SecretRef, type SlackVerifyOptions, type StartAuthRequest, type StartAuthResult, type StartOAuthInput, type StartOAuthOutput, type StripeVerifyOptions, _resetPendingFlowsForTests, assertValidConnectorManifest, consumePendingFlow, createHttpIntegrationProvider, createMockIntegrationProvider, exchangeAuthorizationCode, firstHeader, parseStripeSignatureHeader, refreshAccessToken, sanitizeConnection, signCapability, startOAuthFlow, validateConnectorManifest, verifyCapabilityToken, verifyHmacSignature, verifySlackSignature, verifyStripeSignature };
|