@collective-intelligence/external-apps-client-sdk 0.2.0 → 0.2.1
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/AGENTS.md +34 -0
- package/docs/effect.md +82 -0
- package/docs/errors.md +36 -0
- package/docs/realtime.md +58 -0
- package/package.json +6 -3
- package/skills/client-sdk-move-to-effect/SKILL.md +34 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# `@collective-intelligence/external-apps-client-sdk`: agent guide
|
|
2
|
+
|
|
3
|
+
The browser-safe, React-free SDK of Collective Intelligence (CI) external
|
|
4
|
+
applications: federated sign-in, typed CI operations, transports, resource
|
|
5
|
+
clients and the realtime client. It holds no confidential credential. The
|
|
6
|
+
package `README.md` holds the full API.
|
|
7
|
+
|
|
8
|
+
## Rules
|
|
9
|
+
|
|
10
|
+
- Import each Effect API from `/effect`, and the portable resource contracts
|
|
11
|
+
from `/resources`. The root `.` is the Vanilla Facade (Promise API) over the
|
|
12
|
+
same Effect Core.
|
|
13
|
+
- Keep one `effect` (`^4.1.0`) in the app. It is a required peer.
|
|
14
|
+
- Call `close()` on a client that you made when its owner goes away; it
|
|
15
|
+
disposes the client runtime.
|
|
16
|
+
- Give `createRealtimeClient` an `onReset` that refetches every
|
|
17
|
+
realtime-driven query. Without it, a change can be lost.
|
|
18
|
+
- Treat realtime event ids as opaque: store and resend them unchanged.
|
|
19
|
+
- Branch on an error by its class (`instanceof`, `Effect.catchTag`) and its
|
|
20
|
+
`code`, not by its `message`.
|
|
21
|
+
- Keep server credentials out of this package: they belong to
|
|
22
|
+
`external-apps-server-sdk`.
|
|
23
|
+
|
|
24
|
+
## Index
|
|
25
|
+
|
|
26
|
+
- [docs/realtime.md](docs/realtime.md): read when you subscribe to Realtime
|
|
27
|
+
Topics, recover from a gap, or refresh server-cached data on a change.
|
|
28
|
+
- [docs/effect.md](docs/effect.md): read when you call CI from Effect code:
|
|
29
|
+
services, layers, `Config`, realtime streams.
|
|
30
|
+
- [docs/errors.md](docs/errors.md): read when you handle a failed CI call or
|
|
31
|
+
sign-in.
|
|
32
|
+
- [skills/client-sdk-move-to-effect/SKILL.md](skills/client-sdk-move-to-effect/SKILL.md):
|
|
33
|
+
run when Effect code wraps the Promise API (`Effect.tryPromise`,
|
|
34
|
+
`Effect.promise`) of this SDK.
|
package/docs/effect.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Effect
|
|
2
|
+
|
|
3
|
+
Each capability of the SDK runs on one Effect Core. `/effect` gives it as
|
|
4
|
+
services; the root `.` is the Vanilla Facade over it, with Promise
|
|
5
|
+
signatures.
|
|
6
|
+
|
|
7
|
+
## Convention
|
|
8
|
+
|
|
9
|
+
- Import Effect APIs only from `/effect`. `.` and `/effect` export the same
|
|
10
|
+
error classes, so `instanceof` gives one answer for both.
|
|
11
|
+
- `effect` `^4.1.0` is a required peer: keep one version in the app.
|
|
12
|
+
- In Effect code, use the services of `/effect`, not the Promise API wrapped
|
|
13
|
+
in `Effect.tryPromise`. To move code, run
|
|
14
|
+
[the move skill](../skills/client-sdk-move-to-effect/SKILL.md).
|
|
15
|
+
|
|
16
|
+
## Services and layers
|
|
17
|
+
|
|
18
|
+
Each client is one `Context.Service`, with a layer and a `...Config` layer that
|
|
19
|
+
reads Effect `Config`.
|
|
20
|
+
|
|
21
|
+
| Service | Layers | Promise API at `.` |
|
|
22
|
+
| ------------------ | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
|
|
23
|
+
| `CiClient` | `layerCiClientDirect`, `layerCiClientProxy`, `layerCiClientFromExecutor`, `...Config` | `createClient`, `createDirectTransport`, `createProxyTransport` |
|
|
24
|
+
| `Runs` | `layerRuns` (needs `CiClient`) | `followRun`, `client.runs.follow` |
|
|
25
|
+
| `FederatedAuth` | `layerFederatedAuth`, `layerServerSessionAuth`, `...Config` | `createFederatedAuth`, `createServerSessionAuth` |
|
|
26
|
+
| `FixedWorkflows` | `layerFixedWorkflows`, `layerFixedWorkflowsConfig` | `createFixedWorkflowClient` |
|
|
27
|
+
| `DelegatedRuntime` | `layerDelegatedRuntime`, `layerDelegatedRuntimeConfig` | `createDelegatedApplicationRuntime` |
|
|
28
|
+
| `Resources` | `layerResources`, `layerResourcesConfig`, `layerResourcesFromEntityTransport` | `createResourceClient` (`protocol: "entity"`), `createEntityHttpTransport` |
|
|
29
|
+
| `LegacyResources` | `layerLegacyResources`, `layerLegacyResourcesConfig` | `createResourceClient` over a `ResourceTransport` |
|
|
30
|
+
| `RealtimeClient` | `layerRealtimeClient`, `layerRealtimeClientConfig` | `createRealtimeClient` |
|
|
31
|
+
|
|
32
|
+
- `CiClient.execute` puts the `CiError` of CI in the error channel;
|
|
33
|
+
`executeResult` keeps the `CiResult` envelope with the correlation id.
|
|
34
|
+
- A `...Config` layer takes `Config.Wrap` of the layer options: give each
|
|
35
|
+
field as a `Config`, and a function field as `Config.succeed(fn)`.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import * as Effect from "effect/Effect"
|
|
39
|
+
import {
|
|
40
|
+
CiClient,
|
|
41
|
+
layerCiClientDirect
|
|
42
|
+
} from "@collective-intelligence/external-apps-client-sdk/effect"
|
|
43
|
+
|
|
44
|
+
const program = CiClient.use((ci) =>
|
|
45
|
+
ci.execute("context_get", {}, { organizationId })
|
|
46
|
+
).pipe(
|
|
47
|
+
Effect.catchTag("CiError", (error) => Effect.succeed(error.code)),
|
|
48
|
+
Effect.provide(layerCiClientDirect({ auth, tenantUrl }))
|
|
49
|
+
)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Realtime stream
|
|
53
|
+
|
|
54
|
+
`subscribe(topic)` is a `Stream` of `RealtimeChange | RealtimeResync`, on
|
|
55
|
+
`op`. A Resync (`{ op: "resync" }`) replaces `onReset`: refetch the topic.
|
|
56
|
+
`access-changed` also gives each subscription one Resync; `accessChanges`
|
|
57
|
+
emits at once, so check the session. The streams end when the layer scope
|
|
58
|
+
closes. Connection rules: [realtime.md](realtime.md).
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import {
|
|
62
|
+
layerRealtimeClient,
|
|
63
|
+
subscribe
|
|
64
|
+
} from "@collective-intelligence/external-apps-client-sdk/effect"
|
|
65
|
+
import * as Stream from "effect/Stream"
|
|
66
|
+
|
|
67
|
+
const leads = subscribe({ schema: "crm.lead" }).pipe(
|
|
68
|
+
Stream.runForEach((update) =>
|
|
69
|
+
update.op === "resync" ? refetchLeads : invalidateLead(update.id)
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
// Provide layerRealtimeClient({ ticketUrl: "/api/realtime/ticket" }).
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
- Timers use `Clock` and the jitter uses `Random`: tests use `TestClock` and
|
|
76
|
+
a fixed `Random`.
|
|
77
|
+
- Outside a browser, give an absolute `ticketUrl`.
|
|
78
|
+
|
|
79
|
+
## In React
|
|
80
|
+
|
|
81
|
+
React apps run these services through the Effect bridge of
|
|
82
|
+
`external-apps-client-sdk-react/effect` (`fromEffect`, `QueryCache`).
|
package/docs/errors.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Errors
|
|
2
|
+
|
|
3
|
+
The SDK defines its Expected Errors as `Schema.TaggedError` classes. Each
|
|
4
|
+
class extends `Error`, and `.` and `/effect` export the same class.
|
|
5
|
+
|
|
6
|
+
| Class | Fields | When |
|
|
7
|
+
| -------------------- | ------------------------------------------ | ------------------------------------------------------------ |
|
|
8
|
+
| `CiError` | `code`, `message`, `retryable`, `details?` | CI refused an operation |
|
|
9
|
+
| `FederatedAuthError` | `code`, `message` | The federated sign-in failed |
|
|
10
|
+
| `CiTransportError` | `cause` | The request did not reach CI, or the answer was not readable |
|
|
11
|
+
|
|
12
|
+
## What each call gives
|
|
13
|
+
|
|
14
|
+
| Failure | `.` call that returns `CiResult` | `.` call that rejects | `/effect` |
|
|
15
|
+
| ----------------------------- | --------------------------------------------- | --------------------------------------------------------- | ----------------------------- |
|
|
16
|
+
| CI returns an error | resolves `{ ok: false, error }` (a `CiError`) | rejects with `CiError` (`FederatedAuthError` for sign-in) | fails with the same class |
|
|
17
|
+
| The request does not reach CI | rejects with `CiTransportError` | rejects with `CiTransportError` | fails with `CiTransportError` |
|
|
18
|
+
| The caller aborts its signal | rejects with `signal.reason` | rejects with `signal.reason` | interruption |
|
|
19
|
+
| Bad SDK input | throws `TypeError` | throws `TypeError` | dies with `TypeError` |
|
|
20
|
+
|
|
21
|
+
- The calls that return `CiResult` are the domain methods of `createClient`,
|
|
22
|
+
`client.execute`, and the `execute` of the direct and proxy transports.
|
|
23
|
+
- `createResourceClient` and `createEntityHttpTransport` keep the
|
|
24
|
+
`ResourceResult` envelope: a transport failure gives `transport_error`, with
|
|
25
|
+
`outcome` `rejected` for a read and `uncertain` for a write.
|
|
26
|
+
- A run that does not finish before `deadlineMs` rejects with `CiError`
|
|
27
|
+
`unavailable` (`retryable: true`).
|
|
28
|
+
|
|
29
|
+
## Rules
|
|
30
|
+
|
|
31
|
+
- Branch with `instanceof` (Promise) or `Effect.catchTag` (Effect), then on
|
|
32
|
+
`code`. Do not parse `message`.
|
|
33
|
+
- `CiTransportError` has an empty `message`: show the user your own text.
|
|
34
|
+
- Retry only when `retryable` is `true`, and only for an idempotent call.
|
|
35
|
+
- Decode a wire code with the `CiErrorCode` or `FederatedAuthErrorCode`
|
|
36
|
+
Schema of `/effect`.
|
package/docs/realtime.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Realtime
|
|
2
|
+
|
|
3
|
+
`createRealtimeClient` keeps one realtime connection per tab for every
|
|
4
|
+
subscribed Realtime Topic. A topic is the records of one entity schema,
|
|
5
|
+
optionally filtered by one data path: `{ schema, filter?: { field, eq } }`.
|
|
6
|
+
Events carry ids only: refetch the records through your own data routes.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { createRealtimeClient } from "@collective-intelligence/external-apps-client-sdk"
|
|
10
|
+
|
|
11
|
+
const realtime = createRealtimeClient({
|
|
12
|
+
document,
|
|
13
|
+
onAccessChanged: () => session.refresh(),
|
|
14
|
+
onReset: () => queryClient.invalidateQueries(),
|
|
15
|
+
revalidateUrl: "/api/realtime/revalidate",
|
|
16
|
+
ticketUrl: "/api/realtime/ticket",
|
|
17
|
+
window
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const unsubscribe = realtime.subscribe({ schema: "crm.lead" }, (changes) =>
|
|
21
|
+
invalidateLeads(changes.map((change) => change.id))
|
|
22
|
+
)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Rules
|
|
26
|
+
|
|
27
|
+
- `ticketUrl` is the ticket route of the server SDK BFF. Each connection gets
|
|
28
|
+
a new ticket, never the user's token.
|
|
29
|
+
- Pass `onReset` and refetch every realtime-driven query in it. The client
|
|
30
|
+
calls it when the server cannot replay a gap, and once after each stream
|
|
31
|
+
opens without a last event id (expect one extra refetch per page load).
|
|
32
|
+
- `onAccessChanged` runs at once when the user's access changes: check the
|
|
33
|
+
session again.
|
|
34
|
+
- Pass the page `document` and `window` in a browser. The connection opens
|
|
35
|
+
only while the page is visible and a topic has a listener.
|
|
36
|
+
- Listeners get changes in batches: one delivery 250 to 1000 ms after the
|
|
37
|
+
first pending change, with the latest change of each record.
|
|
38
|
+
- Event ids are opaque Position Tokens: store and resend them unchanged; never
|
|
39
|
+
parse or order them.
|
|
40
|
+
- `close()` ends the connection and the page listeners.
|
|
41
|
+
|
|
42
|
+
## Server cache refresh
|
|
43
|
+
|
|
44
|
+
With `revalidateUrl` (the Revalidate Route of the server SDK), each delivery
|
|
45
|
+
first posts its changes there, so the server expires the Next.js cache tags
|
|
46
|
+
of those records. Then, at most 1 second later, the client calls the
|
|
47
|
+
listeners. A listener that reads through the server cache, for example with
|
|
48
|
+
`router.refresh()`, thus reads the fresh entry.
|
|
49
|
+
|
|
50
|
+
- The post carries the `grantToken` of the last ticket; the client keeps it
|
|
51
|
+
in memory and renews it 8 minutes after each ticket.
|
|
52
|
+
- A ticket route without `secrets` gives no token: the client posts nothing.
|
|
53
|
+
- A failed post logs a warning and does not hold the listeners.
|
|
54
|
+
|
|
55
|
+
## Effect
|
|
56
|
+
|
|
57
|
+
`/effect` gives the same connection as the `RealtimeClient` service; see
|
|
58
|
+
[effect.md](effect.md#realtime-stream).
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@collective-intelligence/external-apps-client-sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Framework-neutral client for Collective Intelligence external applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"files": [
|
|
8
8
|
"dist",
|
|
9
|
-
"README.md"
|
|
9
|
+
"README.md",
|
|
10
|
+
"AGENTS.md",
|
|
11
|
+
"docs",
|
|
12
|
+
"skills"
|
|
10
13
|
],
|
|
11
14
|
"main": "./dist/index.js",
|
|
12
15
|
"types": "./dist/index.d.ts",
|
|
@@ -68,5 +71,5 @@
|
|
|
68
71
|
"ultracite": "7.10.7",
|
|
69
72
|
"vitest": "5.0.2"
|
|
70
73
|
},
|
|
71
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "7b3d604c39db3969749b14174d660a814a1f3a72"
|
|
72
75
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: client-sdk-move-to-effect
|
|
3
|
+
description: Move Effect code that calls `@collective-intelligence/external-apps-client-sdk` through its Promise API to the services of its `/effect` entry. Use when Effect code wraps SDK calls in `Effect.tryPromise` or `Effect.promise`, maps SDK rejections by hand, or lint reports `ci-solution/effect-subpath`.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Move client SDK calls to `/effect`
|
|
7
|
+
|
|
8
|
+
Reference: [effect.md](../../docs/effect.md), [errors.md](../../docs/errors.md).
|
|
9
|
+
|
|
10
|
+
## Steps
|
|
11
|
+
|
|
12
|
+
1. **Find the wrapped calls.** Search Effect modules for `Effect.tryPromise`,
|
|
13
|
+
`Effect.promise` and `Effect.callback` around calls of `createClient`,
|
|
14
|
+
`followRun`, `createFederatedAuth`, `createResourceClient`,
|
|
15
|
+
`createRealtimeClient` and the other Promise clients. Done when you have
|
|
16
|
+
each wrapped call site.
|
|
17
|
+
2. **Pick the service** of each call with the table in
|
|
18
|
+
[effect.md](../../docs/effect.md#services-and-layers). Done when each call
|
|
19
|
+
site has one service.
|
|
20
|
+
3. **Provide the layer once** at the composition root of the Effect program:
|
|
21
|
+
the layer, or its `...Config` form when the options come from
|
|
22
|
+
configuration. Done when no module below the root makes an SDK client.
|
|
23
|
+
4. **Rewrite each call** as `Service.use(...)` or `yield* Service`. Replace the
|
|
24
|
+
hand-written error mapping with `Effect.catchTag("CiError", ...)` and
|
|
25
|
+
`Effect.catchTag("CiTransportError", ...)`. Done when step 1 finds no
|
|
26
|
+
wrapped SDK call.
|
|
27
|
+
5. **Replace realtime callbacks** with the `subscribe` Stream; handle
|
|
28
|
+
`op: "resync"` where the code had `onReset`. Done when no Effect module
|
|
29
|
+
calls `createRealtimeClient`.
|
|
30
|
+
6. **Verify.** Run the type-check and the tests. With
|
|
31
|
+
`@collective-intelligence/lint` `recommended`, oxlint reports no
|
|
32
|
+
`ci-solution/effect-subpath` finding. Done when all pass.
|
|
33
|
+
|
|
34
|
+
Code outside Effect keeps the Promise API of `.`; it stays supported.
|