@ziggy-ai/client-sdk 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +15 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +87 -0
- package/dist/types.d.ts +6 -0
- package/dist/visitor-token.d.ts +28 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# @ziggy-ai/client-sdk changelog
|
|
2
2
|
|
|
3
|
+
## 1.6.0
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `getVisitorToken` in the client config, and `createVisitorToken({ appId, ziggyUrl })` to make it. For an app whose own server calls Ziggy (a self-host proxy): the browser gets a token from Ziggy, the SDK sends it as `X-Ziggy-Visitor-Token`, and your proxy passes it on. With it the assistant can be shown a person's own earlier exchanges; without it your app works as before. Nothing is fetched or stored unless you set it. Update your proxy to forward the header (docs/guides/easy-integrate.md). A proxy on another origin must also allow the header in its CORS answer.
|
|
7
|
+
|
|
3
8
|
## 1.5.0
|
|
4
9
|
|
|
5
10
|
Includes the unpublished 1.4.0 changes: `client.app.getAvailability()` and the `plan` field on `VerifyResponse`.
|
package/README.md
CHANGED
|
@@ -56,9 +56,24 @@ const ziggy = new ZiggyClient({
|
|
|
56
56
|
appId: string; // Your app identifier
|
|
57
57
|
apiKey: string; // API key (from App node in Ziggy)
|
|
58
58
|
timeout?: number; // Request timeout in ms (default: 30000)
|
|
59
|
+
getVisitorToken?: () => Promise<string | null>; // Self-host proxies only, see below
|
|
59
60
|
});
|
|
60
61
|
```
|
|
61
62
|
|
|
63
|
+
#### Visitor token (self-host proxies)
|
|
64
|
+
|
|
65
|
+
If your own server calls Ziggy, Ziggy takes its word for the user id and cannot tell two people sent under one id apart. Let the browser prove which one it is:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// Once, outside render. `ziggyUrl` is Ziggy's API, not your proxy.
|
|
69
|
+
const getVisitorToken = createVisitorToken({ appId: 'your-app', ziggyUrl: 'https://ziggy-production-e3be.up.railway.app' });
|
|
70
|
+
|
|
71
|
+
const ziggy = new ZiggyClient({ baseUrl: '/api/ziggy', appId: 'your-app', apiKey: 'proxy-managed', getVisitorToken });
|
|
72
|
+
// or <ZiggyProvider ... getVisitorToken={getVisitorToken}>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Your site must be one of the app's allowed origins, and your proxy must forward `X-Ziggy-Visitor-Token` from the same request. The token is kept in the browser's localStorage, so the same person is recognized when they come back. It is not a sign-in and opens nothing; it only lets the assistant be shown that person's own earlier exchanges. With the managed proxy you do not need it.
|
|
76
|
+
|
|
62
77
|
#### Lifecycle
|
|
63
78
|
|
|
64
79
|
```typescript
|
package/dist/index.d.ts
CHANGED
|
@@ -138,6 +138,8 @@ export type { RecommendationDiscussionReference, BrowserReviewDiscussionReferenc
|
|
|
138
138
|
export { ZiggyError, ZiggyConnectionError } from './errors.js';
|
|
139
139
|
export { LocalStoragePersistence } from './persistence.js';
|
|
140
140
|
export type { PersistenceAdapter } from './persistence.js';
|
|
141
|
+
export { createVisitorToken } from './visitor-token.js';
|
|
142
|
+
export type { VisitorTokenOptions } from './visitor-token.js';
|
|
141
143
|
export { parseNdjsonStream, SequenceTracker } from './stream-parser.js';
|
|
142
144
|
export { CallbackHandler, verifyCallbackSignature, DEFAULT_CALLBACK_TOLERANCE_MS } from './callback-handler.js';
|
|
143
145
|
export { ObserverClient } from './observer.js';
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,74 @@ var ZiggyConnectionError = class extends ZiggyError {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
// src/visitor-token.ts
|
|
20
|
+
var VISITOR_ID = /^visitor_[a-f0-9]{32}$/;
|
|
21
|
+
var FIRST_TOKEN_WAIT_MS = 2e3;
|
|
22
|
+
var RETRY_AFTER_MS = 5 * 6e4;
|
|
23
|
+
function createVisitorToken(options) {
|
|
24
|
+
const storageKey = `ziggy:visitor-proof:${options.appId}`;
|
|
25
|
+
const url = `${options.ziggyUrl.replace(/\/+$/, "")}/browser/${encodeURIComponent(options.appId)}/visitor`;
|
|
26
|
+
let token = null;
|
|
27
|
+
let renewing = null;
|
|
28
|
+
let askAt = 0;
|
|
29
|
+
const renew = async () => {
|
|
30
|
+
askAt = Date.now() + RETRY_AFTER_MS;
|
|
31
|
+
const controller = new AbortController();
|
|
32
|
+
const timer = setTimeout(() => controller.abort(), 1e4);
|
|
33
|
+
try {
|
|
34
|
+
const res = await fetch(url, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
|
37
|
+
signal: controller.signal
|
|
38
|
+
});
|
|
39
|
+
if (!res.ok) {
|
|
40
|
+
if (res.status === 403 || res.status === 404 || res.status === 409) askAt = Infinity;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const issued = tokenIn(await res.json());
|
|
44
|
+
if (!issued) return;
|
|
45
|
+
token = issued;
|
|
46
|
+
askAt = Infinity;
|
|
47
|
+
try {
|
|
48
|
+
window.localStorage.setItem(storageKey, issued);
|
|
49
|
+
} catch {
|
|
50
|
+
}
|
|
51
|
+
} catch {
|
|
52
|
+
} finally {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
return async () => {
|
|
57
|
+
if (typeof window === "undefined") return null;
|
|
58
|
+
if (!token) {
|
|
59
|
+
try {
|
|
60
|
+
const kept = window.localStorage.getItem(storageKey);
|
|
61
|
+
if (isVisitorToken(kept)) token = kept;
|
|
62
|
+
} catch {
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (!renewing && Date.now() >= askAt) {
|
|
66
|
+
renewing = renew().finally(() => {
|
|
67
|
+
renewing = null;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (!token && renewing) {
|
|
71
|
+
await Promise.race([renewing, new Promise((done) => setTimeout(done, FIRST_TOKEN_WAIT_MS))]);
|
|
72
|
+
}
|
|
73
|
+
return token;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function isVisitorToken(value) {
|
|
77
|
+
return typeof value === "string" && /^[A-Za-z0-9._~+/=-]{1,512}$/.test(value);
|
|
78
|
+
}
|
|
79
|
+
function tokenIn(value) {
|
|
80
|
+
if (!value || typeof value !== "object") return null;
|
|
81
|
+
const { userId, token } = value;
|
|
82
|
+
return typeof userId === "string" && VISITOR_ID.test(userId) && isVisitorToken(token) ? token : null;
|
|
83
|
+
}
|
|
84
|
+
|
|
19
85
|
// src/auth.ts
|
|
86
|
+
var VISITOR_TOKEN_WAIT_MS = 3e3;
|
|
20
87
|
async function buildHeaders(config, extra) {
|
|
21
88
|
const headers = {
|
|
22
89
|
"Content-Type": "application/json",
|
|
@@ -30,8 +97,27 @@ async function buildHeaders(config, extra) {
|
|
|
30
97
|
headers["Authorization"] = `Bearer ${token}`;
|
|
31
98
|
}
|
|
32
99
|
}
|
|
100
|
+
const visitorToken = await visitorTokenOf(config);
|
|
101
|
+
if (visitorToken) headers["X-Ziggy-Visitor-Token"] = visitorToken;
|
|
33
102
|
return headers;
|
|
34
103
|
}
|
|
104
|
+
async function visitorTokenOf(config) {
|
|
105
|
+
if (!config.getVisitorToken) return null;
|
|
106
|
+
let timer;
|
|
107
|
+
try {
|
|
108
|
+
const token = await Promise.race([
|
|
109
|
+
config.getVisitorToken(),
|
|
110
|
+
new Promise((done) => {
|
|
111
|
+
timer = setTimeout(() => done(null), VISITOR_TOKEN_WAIT_MS);
|
|
112
|
+
})
|
|
113
|
+
]);
|
|
114
|
+
return isVisitorToken(token) ? token : null;
|
|
115
|
+
} catch {
|
|
116
|
+
return null;
|
|
117
|
+
} finally {
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
35
121
|
|
|
36
122
|
// src/connection.ts
|
|
37
123
|
var Connection = class {
|
|
@@ -2625,6 +2711,7 @@ export {
|
|
|
2625
2711
|
ZiggyConnectionError,
|
|
2626
2712
|
ZiggyError,
|
|
2627
2713
|
captureAndUpload,
|
|
2714
|
+
createVisitorToken,
|
|
2628
2715
|
isProductMemoryEventType,
|
|
2629
2716
|
parseNdjsonStream,
|
|
2630
2717
|
verifyCallbackSignature
|
package/dist/types.d.ts
CHANGED
|
@@ -14,6 +14,12 @@ export interface ZiggyClientConfig {
|
|
|
14
14
|
timeout?: number;
|
|
15
15
|
/** Optional async token getter for proxy auth (e.g., Clerk JWT) */
|
|
16
16
|
getAuthToken?: () => Promise<string | null>;
|
|
17
|
+
/**
|
|
18
|
+
* For an app whose own server calls Ziggy (a self-host proxy): the browser's visitor token, from
|
|
19
|
+
* `createVisitorToken`. Sent as `X-Ziggy-Visitor-Token`, which your proxy passes on. Not used with
|
|
20
|
+
* the managed proxy, which verifies the person itself.
|
|
21
|
+
*/
|
|
22
|
+
getVisitorToken?: () => Promise<string | null>;
|
|
17
23
|
/**
|
|
18
24
|
* Optional persistence adapter for conversation continuity across page reloads.
|
|
19
25
|
* When configured, the SDK stores conversationId + sessionId and auto-restores
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The token that proves "the same browser as before", for an app whose own
|
|
3
|
+
* server calls Ziggy (a self-host proxy).
|
|
4
|
+
*
|
|
5
|
+
* Ziggy takes such a server's word for the user id, so it cannot tell two
|
|
6
|
+
* people sent under one id apart. The browser gets this token from Ziggy
|
|
7
|
+
* itself, the SDK sends it along, and the app's proxy passes it on. Ziggy uses
|
|
8
|
+
* it only to decide which of a person's own earlier exchanges the assistant may
|
|
9
|
+
* be shown. It is not a sign-in and changes nothing the person can see or do.
|
|
10
|
+
*
|
|
11
|
+
* Only ever in a browser: on a server it would be one token for everybody.
|
|
12
|
+
*/
|
|
13
|
+
export interface VisitorTokenOptions {
|
|
14
|
+
/** Your app's id. */
|
|
15
|
+
appId: string;
|
|
16
|
+
/** Ziggy's API, e.g. `https://ziggy-production-e3be.up.railway.app`. Not your own proxy: the browser asks Ziggy itself. */
|
|
17
|
+
ziggyUrl: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns the function to give `ZiggyClient` or `ZiggyProvider` as
|
|
21
|
+
* `getVisitorToken`. Create it once, outside render.
|
|
22
|
+
*
|
|
23
|
+
* The site's address must be one of the app's allowed origins. Any failure
|
|
24
|
+
* means no token, and requests go on without it.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createVisitorToken(options: VisitorTokenOptions): () => Promise<string | null>;
|
|
27
|
+
/** Whether a value can be a visitor token, and so can be sent as a header: setting one that cannot throws. */
|
|
28
|
+
export declare function isVisitorToken(value: unknown): value is string;
|