clivly 0.7.2 → 0.7.3
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 +159 -103
- package/drizzle.d.cts +1 -9
- package/drizzle.d.mts +1 -9
- package/index.cjs +708 -106
- package/index.d.cts +6 -0
- package/index.d.mts +6 -0
- package/index.mjs +709 -107
- package/internal-entity-heuristics.cjs +27 -7
- package/internal-entity-heuristics.mjs +27 -7
- package/namespace-probe-B4WE45TH.d.mts +372 -0
- package/namespace-probe-DRzOtaRT.d.cts +372 -0
- package/package.json +1 -1
- package/{sdk-iqOXeWIp.cjs → sdk-BrQYA-GT.cjs} +125 -27
- package/{sdk-C6WANW8M.mjs → sdk-C2VL28du.mjs} +125 -27
- package/sdk.cjs +1 -1
- package/sdk.d.cts +36 -1
- package/sdk.d.mts +36 -1
- package/sdk.mjs +1 -1
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
- package/types-CsL0rFYh.d.cts +0 -155
- package/types-CsL0rFYh.d.mts +0 -155
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { a as ClivlyEntitiesConfig } from "./entity-config-DoUCXtq5.cjs";
|
|
2
|
+
//#region ../sdk/src/drizzle-meta.d.ts
|
|
3
|
+
declare const DRIZZLE_META: "__clivly_drizzle_source_meta__";
|
|
4
|
+
interface DrizzleSourceMeta {
|
|
5
|
+
/** Drizzle property keys of every column on the source table. */
|
|
6
|
+
columnKeys: string[];
|
|
7
|
+
/** Schema-v2 typed column metadata derived from the Drizzle table. */
|
|
8
|
+
columnsMeta?: DiscoveredColumn[];
|
|
9
|
+
cursorField: string;
|
|
10
|
+
idField: string;
|
|
11
|
+
/** The backing table's real name, so discovery can key metadata by table. */
|
|
12
|
+
tableName?: string;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region ../sdk/src/types.d.ts
|
|
16
|
+
interface DiscoveredColumn {
|
|
17
|
+
isForeignKey?: boolean;
|
|
18
|
+
isPrimaryKey?: boolean;
|
|
19
|
+
name: string;
|
|
20
|
+
nullable?: boolean;
|
|
21
|
+
/** The column this FK points at, when it is a foreign key. */
|
|
22
|
+
references?: {
|
|
23
|
+
table: string;
|
|
24
|
+
column: string;
|
|
25
|
+
};
|
|
26
|
+
/** SQL/driver type, e.g. "text", "integer", "timestamp". */
|
|
27
|
+
type?: string;
|
|
28
|
+
}
|
|
29
|
+
interface DiscoveredTable {
|
|
30
|
+
columns: string[];
|
|
31
|
+
/** Schema-v2 typed column metadata (types, PK/FK, nullability). */
|
|
32
|
+
columnsMeta?: DiscoveredColumn[];
|
|
33
|
+
name: string;
|
|
34
|
+
/** Approximate source row count, when cheaply derivable. */
|
|
35
|
+
rowCount?: number;
|
|
36
|
+
}
|
|
37
|
+
interface ClivlySDKConfig {
|
|
38
|
+
/** Org-scoped API token (sk_live_…) from Settings → API keys. */
|
|
39
|
+
apiKey: string;
|
|
40
|
+
/** Clivly API base URL. Defaults to https://api.clivly.com. */
|
|
41
|
+
apiUrl?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The host table Clivly treats as the contact entity.
|
|
44
|
+
* @deprecated Prefer `entities` — the same mapping-derived shape
|
|
45
|
+
* `crm_entity_mappings` (and `@clivly/core`'s `mappingsToEntitiesConfig`)
|
|
46
|
+
* produce, so the SDK, the mappings table, and the sync engine share one
|
|
47
|
+
* model. A bare `contactEntity` is treated as a single contact entity.
|
|
48
|
+
*/
|
|
49
|
+
contactEntity?: string;
|
|
50
|
+
/** Which columns of the contact entity Clivly may read. */
|
|
51
|
+
contactFields?: string[];
|
|
52
|
+
/**
|
|
53
|
+
* Where per-entity delta cursors are persisted between syncs. Defaults to an
|
|
54
|
+
* in-memory store (fine for a long-lived process). On serverless/edge, pass a
|
|
55
|
+
* store backed by your own KV/DB so a cold start resumes delta sync instead of
|
|
56
|
+
* re-paging the whole entity. See `CursorStore`.
|
|
57
|
+
*/
|
|
58
|
+
cursorStore?: CursorStore;
|
|
59
|
+
/**
|
|
60
|
+
* Declarative entity config — the single source of truth shared with
|
|
61
|
+
* `crm_entity_mappings`. When set, discovery and heartbeat are derived from
|
|
62
|
+
* it (each entity's `source` table + mapped columns), superseding the flat
|
|
63
|
+
* `contactEntity`.
|
|
64
|
+
*/
|
|
65
|
+
entities?: ClivlyEntitiesConfig;
|
|
66
|
+
/**
|
|
67
|
+
* Environment to read platform variables from when discovering the public
|
|
68
|
+
* trigger URL. Defaults to `process.env`. Injectable so tests and non-Node
|
|
69
|
+
* runtimes can supply their own.
|
|
70
|
+
*/
|
|
71
|
+
env?: Record<string, string | undefined>;
|
|
72
|
+
/**
|
|
73
|
+
* 🚧 Not implemented in 0.1.x. Reserved options for tunnel mode and CRM
|
|
74
|
+
* write-back — they have NO effect yet and land with the Cloudflare
|
|
75
|
+
* infrastructure. Grouped under `experimental` so the stable config surface
|
|
76
|
+
* above is unambiguous; nothing here is part of the supported API.
|
|
77
|
+
*/
|
|
78
|
+
experimental?: {
|
|
79
|
+
/** Cloudflare Tunnel token — reserved for tunnel mode. Not wired up yet. */
|
|
80
|
+
tunnelToken?: string;
|
|
81
|
+
/** CRM write-back target — reserved. Not wired up yet. */
|
|
82
|
+
writeBack?: {
|
|
83
|
+
enabled?: boolean;
|
|
84
|
+
/** Only crm_* tables are ever accepted; this narrows further. */
|
|
85
|
+
tables?: string[];
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/** Reported to the portal so it can show the detected stack. */
|
|
89
|
+
framework?: string;
|
|
90
|
+
/** Heartbeat cadence in ms. Defaults to 60s. */
|
|
91
|
+
heartbeatIntervalMs?: number;
|
|
92
|
+
/**
|
|
93
|
+
* Optional live namespace probe, used on-demand by `doctor()`'s "Namespace
|
|
94
|
+
* reachable" check to verify every required source table + column is
|
|
95
|
+
* genuinely reachable. Not consulted by the heartbeat. Build one with
|
|
96
|
+
* `drizzleIntrospector(db)` from `@clivly/sdk/drizzle`.
|
|
97
|
+
*/
|
|
98
|
+
introspector?: NamespaceIntrospector;
|
|
99
|
+
/**
|
|
100
|
+
* Self-reported readiness sent on every heartbeat as-is (no live check).
|
|
101
|
+
* For a real reachability check, configure `introspector` and run
|
|
102
|
+
* `doctor()`.
|
|
103
|
+
*/
|
|
104
|
+
namespaceReady?: boolean;
|
|
105
|
+
orm?: string;
|
|
106
|
+
/**
|
|
107
|
+
* Network retry policy for outbound calls (heartbeat + sync push). Transient
|
|
108
|
+
* failures (network errors, HTTP 429, HTTP 5xx) are retried with exponential
|
|
109
|
+
* backoff + jitter; a `Retry-After` header on a 429 is honoured. Non-transient
|
|
110
|
+
* responses (e.g. 4xx other than 429) are returned immediately, not retried.
|
|
111
|
+
*/
|
|
112
|
+
retry?: {
|
|
113
|
+
/** Total attempts including the first. Defaults to 3. Min 1. */
|
|
114
|
+
maxAttempts?: number;
|
|
115
|
+
/** Base backoff in ms (doubled each attempt). Defaults to 500. */
|
|
116
|
+
baseDelayMs?: number;
|
|
117
|
+
};
|
|
118
|
+
/** Tables/columns to report for entity discovery + mapping. */
|
|
119
|
+
schema?: DiscoveredTable[];
|
|
120
|
+
sdkVersion?: string;
|
|
121
|
+
/** Host entities to mirror into Clivly Cloud. */
|
|
122
|
+
source?: {
|
|
123
|
+
contacts: SyncSource;
|
|
124
|
+
companies?: SyncSource;
|
|
125
|
+
deals?: SyncSource;
|
|
126
|
+
/** Org-defined custom objects to mirror. Each carries its object-type slug. */
|
|
127
|
+
custom?: Array<SyncSource & {
|
|
128
|
+
objectType: string;
|
|
129
|
+
}>;
|
|
130
|
+
};
|
|
131
|
+
sync?: {
|
|
132
|
+
onBoot?: boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Sync cadence in ms for the background keep-alive loop. Defaults to
|
|
135
|
+
* `heartbeatIntervalMs` (60s) so sync and heartbeat share a tick unless you
|
|
136
|
+
* decouple them — e.g. a slow full-mirror alongside frequent heartbeats.
|
|
137
|
+
*/
|
|
138
|
+
intervalMs?: number;
|
|
139
|
+
batchSize?: number;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* The remote sync trigger: how Clivly Cloud asks this app to run a sync.
|
|
143
|
+
*
|
|
144
|
+
* Without it, cloud mode is push-only — Clivly can watch the app but can't
|
|
145
|
+
* start anything, so the dashboard's "Run sync" has nothing to call. With it,
|
|
146
|
+
* the SDK reports `url` on each heartbeat and requires every trigger request to
|
|
147
|
+
* be signed with `secret`.
|
|
148
|
+
*/
|
|
149
|
+
syncTrigger?: {
|
|
150
|
+
/**
|
|
151
|
+
* Public URL of the route where `createClivlyHandler()` is mounted, e.g.
|
|
152
|
+
* `https://app.example.com/api/clivly/tick`. Reported to Clivly on each
|
|
153
|
+
* heartbeat; a URL set in the dashboard overrides it.
|
|
154
|
+
*
|
|
155
|
+
* Optional — leave it unset and the SDK discovers the URL itself, from the
|
|
156
|
+
* hosting platform's environment or from the first request the route
|
|
157
|
+
* serves. Set it only to pin an address the SDK can't infer.
|
|
158
|
+
*/
|
|
159
|
+
url?: string;
|
|
160
|
+
/**
|
|
161
|
+
* Path where `createClivlyHandler()` is mounted, e.g. `/api/clivly/tick`.
|
|
162
|
+
* Written by `clivly init`. Combined with the origin detected from the
|
|
163
|
+
* hosting platform so the SDK can report a trigger URL on its very first
|
|
164
|
+
* heartbeat, before any request has arrived to observe.
|
|
165
|
+
*/
|
|
166
|
+
path?: string;
|
|
167
|
+
/**
|
|
168
|
+
* Signing secret (`whsec_…`) issued by Clivly under Integrations → Remote
|
|
169
|
+
* sync. When set, `createClivlyHandler()` rejects any request that isn't
|
|
170
|
+
* validly signed. Leave unset only if the route is already unreachable from
|
|
171
|
+
* the public internet.
|
|
172
|
+
*/
|
|
173
|
+
secret?: string;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Shared secret Clivly Cloud presents (as `Authorization: Bearer <token>`) when
|
|
177
|
+
* calling the `/clivly/auth/verify` endpoint. Required for the verify handler to
|
|
178
|
+
* authenticate the caller; without it the handler fails closed (HTTP 503). No
|
|
179
|
+
* cloud consumer calls this endpoint yet — it is an inert primitive reserved for
|
|
180
|
+
* a future CRM/dashboard auth-passthrough.
|
|
181
|
+
*/
|
|
182
|
+
verifyToken?: string;
|
|
183
|
+
}
|
|
184
|
+
interface ClivlyUser {
|
|
185
|
+
email: string;
|
|
186
|
+
id: string;
|
|
187
|
+
name?: string;
|
|
188
|
+
}
|
|
189
|
+
interface ChatWidgetVisitor {
|
|
190
|
+
email: string;
|
|
191
|
+
name: string;
|
|
192
|
+
}
|
|
193
|
+
interface ChatSessionRequestBody {
|
|
194
|
+
visitor: ChatWidgetVisitor;
|
|
195
|
+
visitorToken?: string | null;
|
|
196
|
+
widgetId: string;
|
|
197
|
+
}
|
|
198
|
+
interface CreateChatSessionHandlerOptions {
|
|
199
|
+
allowedOrigins?: string[];
|
|
200
|
+
apiKey: string;
|
|
201
|
+
apiUrl?: string;
|
|
202
|
+
fetchImpl?: typeof fetch;
|
|
203
|
+
/**
|
|
204
|
+
* Optional host-auth hook. When provided, it runs on the incoming browser
|
|
205
|
+
* request (session cookies intact) and, on a non-null `ClivlyUser`, the host
|
|
206
|
+
* vouches for that verified identity to Clivly Cloud. Absent/null/throwing =
|
|
207
|
+
* the visitor stays self-reported. Never fails the session.
|
|
208
|
+
*/
|
|
209
|
+
resolveUser?: (req: Request) => Promise<ClivlyUser | null> | ClivlyUser | null;
|
|
210
|
+
}
|
|
211
|
+
interface DoctorCheck {
|
|
212
|
+
/** Human-readable outcome — what passed, or why it failed. */
|
|
213
|
+
detail: string;
|
|
214
|
+
/** Short label, e.g. "API key present". */
|
|
215
|
+
name: string;
|
|
216
|
+
/** True when the check passed. Ignored when `skipped` is true. */
|
|
217
|
+
ok: boolean;
|
|
218
|
+
/** True when the check could not run (e.g. a hand-written source). */
|
|
219
|
+
skipped?: boolean;
|
|
220
|
+
/**
|
|
221
|
+
* True when the setup works but carries a risk worth stating.
|
|
222
|
+
*
|
|
223
|
+
* Distinct from `ok: false`, which means "this is broken, fix it before you
|
|
224
|
+
* rely on it". A warning is for a configuration that will run and sync, yet
|
|
225
|
+
* behaves in a way the developer probably did not intend — the cursor check
|
|
226
|
+
* below is the motivating case: syncing from an `id` column succeeds, mirrors
|
|
227
|
+
* every insert, and silently never re-syncs an edited row.
|
|
228
|
+
*
|
|
229
|
+
* Warnings do not fail `doctor`. Reporting them as failures would train
|
|
230
|
+
* people to ignore a red ladder on a working setup, which is worse than not
|
|
231
|
+
* reporting them at all.
|
|
232
|
+
*/
|
|
233
|
+
warn?: boolean;
|
|
234
|
+
}
|
|
235
|
+
interface DoctorReport {
|
|
236
|
+
checks: DoctorCheck[];
|
|
237
|
+
/** AND of every non-skipped check's `ok`. */
|
|
238
|
+
ok: boolean;
|
|
239
|
+
}
|
|
240
|
+
type ClivlyConnectReason = "ok" | "invalid_api_key" | "forbidden" | "rate_limited" | "server_error" | "client_error" | "server_too_old" | "network_error";
|
|
241
|
+
interface ClivlyConnectResult {
|
|
242
|
+
/**
|
|
243
|
+
* When the org's app last heartbeated, or null if it never has. Set only by
|
|
244
|
+
* `ping()` — `connect()` IS a heartbeat, so the question is meaningless there.
|
|
245
|
+
*/
|
|
246
|
+
appLastSeenAt?: string | null;
|
|
247
|
+
ok: boolean;
|
|
248
|
+
/** Org resolved from a successful heartbeat, when the server returns it. */
|
|
249
|
+
org?: {
|
|
250
|
+
id?: string;
|
|
251
|
+
name?: string;
|
|
252
|
+
};
|
|
253
|
+
reason: ClivlyConnectReason;
|
|
254
|
+
/** Whether a retry could plausibly succeed (network, 429, 5xx). */
|
|
255
|
+
retryable: boolean;
|
|
256
|
+
/** HTTP status, or null when the request never completed (network error). */
|
|
257
|
+
status: number | null;
|
|
258
|
+
}
|
|
259
|
+
interface SyncCursor {
|
|
260
|
+
/** The row's id, breaking ties when timestamps collide. */
|
|
261
|
+
id: string;
|
|
262
|
+
/** The row's cursor-field value (typically `updated_at`). */
|
|
263
|
+
time: Date;
|
|
264
|
+
}
|
|
265
|
+
interface SyncSource {
|
|
266
|
+
cursorField: string;
|
|
267
|
+
entity: string;
|
|
268
|
+
fetchPage(args: {
|
|
269
|
+
since: SyncCursor | null;
|
|
270
|
+
limit: number;
|
|
271
|
+
}): Promise<Record<string, unknown>[]>;
|
|
272
|
+
/** Stable, unique tie-breaker column for keyset paging. Defaults to "id". */
|
|
273
|
+
idField?: string;
|
|
274
|
+
/** Set by `fromDrizzle`; lets `doctor` introspect the backing table. */
|
|
275
|
+
[DRIZZLE_META]?: DrizzleSourceMeta;
|
|
276
|
+
}
|
|
277
|
+
type SyncMode = "delta" | "full";
|
|
278
|
+
interface CursorStore {
|
|
279
|
+
get(entity: string): Promise<SyncCursor | null> | SyncCursor | null;
|
|
280
|
+
set(entity: string, cursor: SyncCursor | null): Promise<void> | void;
|
|
281
|
+
}
|
|
282
|
+
interface ClivlySDK {
|
|
283
|
+
/**
|
|
284
|
+
* Send a single heartbeat and report the outcome richly: `ok`, the HTTP
|
|
285
|
+
* `status` (null on network failure), a coarse `reason`, whether it's
|
|
286
|
+
* `retryable`, and the resolved `org` when the server returns it. Never throws
|
|
287
|
+
* — inspect the result, or turn a failure into an exception with
|
|
288
|
+
* `ClivlyError.from(result)`. Prefer this over `heartbeat()` for onboarding,
|
|
289
|
+
* health checks, and one-shot serverless calls.
|
|
290
|
+
*/
|
|
291
|
+
connect(): Promise<ClivlyConnectResult>;
|
|
292
|
+
/**
|
|
293
|
+
* Build the `/clivly/auth/verify` handler the developer mounts in their app.
|
|
294
|
+
* Clivly's cloud calls it to validate a CRM user's identity against the host
|
|
295
|
+
* app's session. Pass a resolver that reads the host session from the request.
|
|
296
|
+
*
|
|
297
|
+
* The handler authenticates the caller against `config.verifyToken` (presented
|
|
298
|
+
* as `Authorization: Bearer <token>`) before invoking the resolver, so the
|
|
299
|
+
* endpoint can't be used by third parties to probe host sessions. It fails
|
|
300
|
+
* closed with HTTP 503 if `verifyToken` is unset (a missing config value, not a
|
|
301
|
+
* server fault — so it won't trip the host's 5xx error alerting).
|
|
302
|
+
*/
|
|
303
|
+
createAuthVerifyHandler(resolveUser: (req: Request) => Promise<ClivlyUser | null> | ClivlyUser | null): (req: Request) => Promise<Response>;
|
|
304
|
+
/**
|
|
305
|
+
* A fetch handler to mount at a route. Each request runs one `runScheduled()`
|
|
306
|
+
* and returns JSON: 200 when the sync succeeded, 500 otherwise.
|
|
307
|
+
*
|
|
308
|
+
* Two callers use it: Clivly Cloud (when someone clicks "Run sync" — those
|
|
309
|
+
* requests are HMAC-signed and carry a run id), and the host's own scheduler
|
|
310
|
+
* (Workers cron, an uptime ping — no body).
|
|
311
|
+
*
|
|
312
|
+
* When `syncTrigger.secret` is set the handler rejects unsigned or
|
|
313
|
+
* badly-signed requests with 401. When it isn't, the route stays open to
|
|
314
|
+
* anyone who can reach it — set a secret if it's publicly routable.
|
|
315
|
+
*/
|
|
316
|
+
createClivlyHandler(): (req: Request) => Promise<Response>;
|
|
317
|
+
/** Validate this setup end-to-end (read-only) before the first sync. */
|
|
318
|
+
doctor(): Promise<DoctorReport>;
|
|
319
|
+
/**
|
|
320
|
+
* Send a single heartbeat now. Returns true on success. Kept for the keep-alive
|
|
321
|
+
* loop and back-compat; use `connect()` when you need the failure reason.
|
|
322
|
+
*/
|
|
323
|
+
heartbeat(): Promise<boolean>;
|
|
324
|
+
/**
|
|
325
|
+
* Read-only diagnostic probe. Proves the API key is valid and Clivly is
|
|
326
|
+
* reachable, and resolves the org — without reporting anything about the
|
|
327
|
+
* host app. Use this for `clivly status` and other diagnostics; use
|
|
328
|
+
* `connect()` only from the running app, which is entitled to report its
|
|
329
|
+
* own state.
|
|
330
|
+
*/
|
|
331
|
+
ping(): Promise<ClivlyConnectResult>;
|
|
332
|
+
/**
|
|
333
|
+
* One-shot heartbeat + a single sync, with no keep-alive timers. The correct
|
|
334
|
+
* entry point on serverless/edge (Workers, Vercel, Lambda), where `start()`'s
|
|
335
|
+
* `setInterval` loop can't persist between invocations — call this from a
|
|
336
|
+
* scheduled/cron trigger. Throws if a sync push fails (so the run is marked
|
|
337
|
+
* failed); heartbeat is best-effort.
|
|
338
|
+
*/
|
|
339
|
+
runScheduled(options?: {
|
|
340
|
+
mode?: SyncMode;
|
|
341
|
+
runId?: string;
|
|
342
|
+
}): Promise<void>;
|
|
343
|
+
/**
|
|
344
|
+
* Send the initial heartbeat and start the keep-alive loop. For long-lived
|
|
345
|
+
* hosts only — on serverless/edge the interval can't persist, so prefer
|
|
346
|
+
* `runScheduled()` / `createClivlyHandler()`. Emits a warning if a serverless
|
|
347
|
+
* runtime is detected.
|
|
348
|
+
*/
|
|
349
|
+
start(): Promise<void>;
|
|
350
|
+
/** Stop the keep-alive loop. */
|
|
351
|
+
stop(): void;
|
|
352
|
+
/**
|
|
353
|
+
* Sync all configured sources now. `delta` (default) pages changed rows since
|
|
354
|
+
* the last cursor; `full` pushes each complete entity so the server can
|
|
355
|
+
* reconcile leavers (archive-on-leave). Throws if a push fails, so callers see
|
|
356
|
+
* the failure — the background keep-alive loop swallows and retries instead.
|
|
357
|
+
*/
|
|
358
|
+
sync(options?: {
|
|
359
|
+
mode?: SyncMode;
|
|
360
|
+
runId?: string;
|
|
361
|
+
}): Promise<void>;
|
|
362
|
+
}
|
|
363
|
+
//#endregion
|
|
364
|
+
//#region ../sdk/src/namespace-probe.d.ts
|
|
365
|
+
interface NamespaceIntrospector {
|
|
366
|
+
/** All base table names in the reachable schema. */
|
|
367
|
+
listTables(): Promise<string[]>;
|
|
368
|
+
/** Prove a table is genuinely selectable (SELECT ... LIMIT 1). Throws if unreachable/denied. */
|
|
369
|
+
sampleSelect(table: string): Promise<void>;
|
|
370
|
+
}
|
|
371
|
+
//#endregion
|
|
372
|
+
export { ClivlyConnectResult as a, ClivlyUser as c, DiscoveredTable as d, DoctorCheck as f, ClivlyConnectReason as i, CreateChatSessionHandlerOptions as l, SyncSource as m, ChatSessionRequestBody as n, ClivlySDK as o, DoctorReport as p, ChatWidgetVisitor as r, ClivlySDKConfig as s, NamespaceIntrospector as t, CursorStore as u };
|
package/package.json
CHANGED
|
@@ -276,6 +276,46 @@ function errorMessage(error) {
|
|
|
276
276
|
}
|
|
277
277
|
//#endregion
|
|
278
278
|
//#region ../sdk/src/doctor.ts
|
|
279
|
+
const TEMPORAL_CURSOR_TYPES = [
|
|
280
|
+
"timestamp",
|
|
281
|
+
"timestamptz",
|
|
282
|
+
"datetime",
|
|
283
|
+
"date",
|
|
284
|
+
"time"
|
|
285
|
+
];
|
|
286
|
+
/**
|
|
287
|
+
* Warn when the sync cursor cannot observe an update.
|
|
288
|
+
*
|
|
289
|
+
* `clivly init` falls back to the primary key when a table has no
|
|
290
|
+
* `updated_at`-like column, and emits a TODO saying "confirm it is monotonic".
|
|
291
|
+
* A serial `id` *is* monotonic, so the note reads as satisfied — but monotonic
|
|
292
|
+
* is only half the requirement. An `id` never changes, so every insert mirrors
|
|
293
|
+
* and **no edit ever does**: a contact who changes their email keeps the old one
|
|
294
|
+
* in the CRM forever, with nothing anywhere reporting a problem.
|
|
295
|
+
*
|
|
296
|
+
* A code comment could not enforce this; a real integrator shipped exactly this
|
|
297
|
+
* configuration to production. This is the check that says so out loud.
|
|
298
|
+
*/
|
|
299
|
+
function cursorMonotonicityCheck(source) {
|
|
300
|
+
const meta = source[require_drizzle_meta.DRIZZLE_META];
|
|
301
|
+
if (!meta?.columnsMeta) return null;
|
|
302
|
+
const index = meta.columnKeys.indexOf(meta.cursorField);
|
|
303
|
+
const type = meta.columnsMeta[index]?.type?.toLowerCase();
|
|
304
|
+
if (!type) return null;
|
|
305
|
+
const temporal = TEMPORAL_CURSOR_TYPES.some((t) => type.startsWith(t));
|
|
306
|
+
const name = `Cursor tracks updates (${source.entity})`;
|
|
307
|
+
if (temporal) return {
|
|
308
|
+
name,
|
|
309
|
+
ok: true,
|
|
310
|
+
detail: `"${source.entity}" pages on ${meta.cursorField} (${type}) — edits re-sync.`
|
|
311
|
+
};
|
|
312
|
+
return {
|
|
313
|
+
name,
|
|
314
|
+
ok: true,
|
|
315
|
+
warn: true,
|
|
316
|
+
detail: `"${source.entity}" pages on ${meta.cursorField} (${type}), which does not change when a row is edited — inserts will sync, edits will not. Add an updated_at column to "${meta.tableName ?? source.entity}" and set it as the cursor, or accept insert-only mirroring.`
|
|
317
|
+
};
|
|
318
|
+
}
|
|
279
319
|
function configuredSources(config) {
|
|
280
320
|
const sources = [];
|
|
281
321
|
if (config.source?.contacts) sources.push(config.source.contacts);
|
|
@@ -304,32 +344,12 @@ async function mappingDryRunChecks(config) {
|
|
|
304
344
|
return checks;
|
|
305
345
|
}
|
|
306
346
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
347
|
+
* Confirm each drizzle source actually has the cursor and id columns the sync
|
|
348
|
+
* pages on. Hand-written sources have no column metadata, so they are skipped
|
|
349
|
+
* rather than failed.
|
|
310
350
|
*/
|
|
311
|
-
|
|
312
|
-
const { config, connect } = args;
|
|
351
|
+
function cursorColumnChecks(sources) {
|
|
313
352
|
const checks = [];
|
|
314
|
-
const sources = configuredSources(config);
|
|
315
|
-
const hasKey = Boolean(config.apiKey);
|
|
316
|
-
checks.push({
|
|
317
|
-
name: "API key present",
|
|
318
|
-
ok: hasKey,
|
|
319
|
-
detail: hasKey ? "CLIVLY_API_KEY is set." : "apiKey is empty — set CLIVLY_API_KEY."
|
|
320
|
-
});
|
|
321
|
-
const entityCount = config.entities ? Object.keys(config.entities.entities).length : 0;
|
|
322
|
-
checks.push({
|
|
323
|
-
name: "Entities configured",
|
|
324
|
-
ok: entityCount > 0,
|
|
325
|
-
detail: entityCount > 0 ? `${entityCount} entit${entityCount === 1 ? "y" : "ies"} configured.` : "No `entities` config — pass defineClivlyConfig(...) as `entities`."
|
|
326
|
-
});
|
|
327
|
-
checks.push({
|
|
328
|
-
name: "Sources configured",
|
|
329
|
-
ok: sources.length > 0,
|
|
330
|
-
detail: sources.length > 0 ? `${sources.length} source(s) to mirror.` : "No `source.contacts` — nothing to sync."
|
|
331
|
-
});
|
|
332
|
-
checks.push(checkConceptCoverage(config));
|
|
333
353
|
for (const source of sources) {
|
|
334
354
|
const meta = source[require_drizzle_meta.DRIZZLE_META];
|
|
335
355
|
if (!meta) {
|
|
@@ -349,6 +369,14 @@ async function runDoctor(args) {
|
|
|
349
369
|
detail: missing.length === 0 ? `"${source.entity}": ${meta.cursorField} + ${idField} present.` : `"${source.entity}" missing column(s): ${missing.join(", ")}.`
|
|
350
370
|
});
|
|
351
371
|
}
|
|
372
|
+
return checks;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Read one row from each source to prove the query works and that rows actually
|
|
376
|
+
* carry the cursor key. Fetch errors are captured as failures, never thrown.
|
|
377
|
+
*/
|
|
378
|
+
async function sampleFetchChecks(sources) {
|
|
379
|
+
const checks = [];
|
|
352
380
|
for (const source of sources) try {
|
|
353
381
|
const rows = await source.fetchPage({
|
|
354
382
|
since: null,
|
|
@@ -368,10 +396,45 @@ async function runDoctor(args) {
|
|
|
368
396
|
detail: describeFetchError(source.entity, error)
|
|
369
397
|
});
|
|
370
398
|
}
|
|
399
|
+
return checks;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Run the read-only setup checks and return a structured report. Every check
|
|
403
|
+
* captures its own error into `detail` rather than throwing, so one failure never
|
|
404
|
+
* hides the rest. `ok` is the AND of all non-skipped checks.
|
|
405
|
+
*/
|
|
406
|
+
async function runDoctor(args) {
|
|
407
|
+
const { config, probe } = args;
|
|
408
|
+
const checks = [];
|
|
409
|
+
const sources = configuredSources(config);
|
|
410
|
+
const hasKey = Boolean(config.apiKey);
|
|
411
|
+
checks.push({
|
|
412
|
+
name: "API key present",
|
|
413
|
+
ok: hasKey,
|
|
414
|
+
detail: hasKey ? "CLIVLY_API_KEY is set." : "apiKey is empty — set CLIVLY_API_KEY."
|
|
415
|
+
});
|
|
416
|
+
const entityCount = config.entities ? Object.keys(config.entities.entities).length : 0;
|
|
417
|
+
checks.push({
|
|
418
|
+
name: "Entities configured",
|
|
419
|
+
ok: entityCount > 0,
|
|
420
|
+
detail: entityCount > 0 ? `${entityCount} entit${entityCount === 1 ? "y" : "ies"} configured.` : "No `entities` config — pass defineClivlyConfig(...) as `entities`."
|
|
421
|
+
});
|
|
422
|
+
checks.push({
|
|
423
|
+
name: "Sources configured",
|
|
424
|
+
ok: sources.length > 0,
|
|
425
|
+
detail: sources.length > 0 ? `${sources.length} source(s) to mirror.` : "No `source.contacts` — nothing to sync."
|
|
426
|
+
});
|
|
427
|
+
checks.push(checkConceptCoverage(config));
|
|
428
|
+
checks.push(...cursorColumnChecks(sources));
|
|
429
|
+
for (const source of sources) {
|
|
430
|
+
const check = cursorMonotonicityCheck(source);
|
|
431
|
+
if (check) checks.push(check);
|
|
432
|
+
}
|
|
433
|
+
checks.push(...await sampleFetchChecks(sources));
|
|
371
434
|
checks.push(...await mappingDryRunChecks(config));
|
|
372
|
-
const result = await
|
|
435
|
+
const result = await probe();
|
|
373
436
|
checks.push({
|
|
374
|
-
name: "
|
|
437
|
+
name: "Clivly reachable",
|
|
375
438
|
ok: result.ok,
|
|
376
439
|
detail: result.ok ? `Reached Clivly — org "${result.org?.name ?? "(unnamed)"}".` : `Could not connect (${result.reason}).`
|
|
377
440
|
});
|
|
@@ -810,6 +873,7 @@ const UNAUTHORIZED_STATUS = 401;
|
|
|
810
873
|
const FORBIDDEN_STATUS = 403;
|
|
811
874
|
const CLIENT_ERROR_FLOOR = 400;
|
|
812
875
|
const SERVICE_UNAVAILABLE_STATUS = 503;
|
|
876
|
+
const NOT_FOUND_STATUS = 404;
|
|
813
877
|
function connectReason(status) {
|
|
814
878
|
if (status === UNAUTHORIZED_STATUS) return "invalid_api_key";
|
|
815
879
|
if (status === FORBIDDEN_STATUS) return "forbidden";
|
|
@@ -818,6 +882,10 @@ function connectReason(status) {
|
|
|
818
882
|
if (status >= CLIENT_ERROR_FLOOR) return "client_error";
|
|
819
883
|
return "server_error";
|
|
820
884
|
}
|
|
885
|
+
function pingReason(status) {
|
|
886
|
+
if (status === NOT_FOUND_STATUS) return "server_too_old";
|
|
887
|
+
return connectReason(status);
|
|
888
|
+
}
|
|
821
889
|
async function resolveOrg(response) {
|
|
822
890
|
try {
|
|
823
891
|
return (await response.json())?.org;
|
|
@@ -1049,9 +1117,38 @@ function createClivlySDK(config) {
|
|
|
1049
1117
|
retryable: isRetryableStatus(response.status)
|
|
1050
1118
|
};
|
|
1051
1119
|
};
|
|
1120
|
+
const ping = async () => {
|
|
1121
|
+
const response = await fetcher(`${apiUrl}/clivly/sdk/ping`, {
|
|
1122
|
+
method: "GET",
|
|
1123
|
+
headers: { authorization: `Bearer ${config.apiKey}` }
|
|
1124
|
+
});
|
|
1125
|
+
if (response === null) return {
|
|
1126
|
+
ok: false,
|
|
1127
|
+
status: null,
|
|
1128
|
+
reason: "network_error",
|
|
1129
|
+
retryable: true
|
|
1130
|
+
};
|
|
1131
|
+
if (response.ok) {
|
|
1132
|
+
const body = await response.json().catch(() => ({}));
|
|
1133
|
+
return {
|
|
1134
|
+
ok: true,
|
|
1135
|
+
status: response.status,
|
|
1136
|
+
reason: "ok",
|
|
1137
|
+
retryable: false,
|
|
1138
|
+
org: body?.org,
|
|
1139
|
+
..."appLastSeenAt" in body ? { appLastSeenAt: body.appLastSeenAt ?? null } : {}
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
return {
|
|
1143
|
+
ok: false,
|
|
1144
|
+
status: response.status,
|
|
1145
|
+
reason: pingReason(response.status),
|
|
1146
|
+
retryable: isRetryableStatus(response.status)
|
|
1147
|
+
};
|
|
1148
|
+
};
|
|
1052
1149
|
const doctor = () => runDoctor({
|
|
1053
1150
|
config,
|
|
1054
|
-
|
|
1151
|
+
probe: ping
|
|
1055
1152
|
});
|
|
1056
1153
|
const runScheduled = async (options) => {
|
|
1057
1154
|
await heartbeat();
|
|
@@ -1144,6 +1241,7 @@ function createClivlySDK(config) {
|
|
|
1144
1241
|
stop,
|
|
1145
1242
|
heartbeat,
|
|
1146
1243
|
connect,
|
|
1244
|
+
ping,
|
|
1147
1245
|
doctor,
|
|
1148
1246
|
runScheduled,
|
|
1149
1247
|
createClivlyHandler,
|