@polpo-ai/react 0.17.2 → 0.17.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
CHANGED
|
@@ -78,9 +78,21 @@ Root provider component that manages connection and state.
|
|
|
78
78
|
**Props**:
|
|
79
79
|
- `baseUrl`: OpenPolpo server URL
|
|
80
80
|
- `apiKey?`: Optional API key for authentication
|
|
81
|
+
- `environment?`: `"development"` or `"production"`; when omitted, the credential or endpoint determines the target
|
|
81
82
|
- `autoConnect?`: Auto-connect on mount (default: true)
|
|
82
83
|
- `eventFilter?`: Array of SSE event type patterns to subscribe to
|
|
83
84
|
|
|
85
|
+
Changing the environment, endpoint, API key, API prefix or authenticated fetch
|
|
86
|
+
function creates a new client and store, disconnects the previous event stream,
|
|
87
|
+
and remounts the provider's children. Hook-local sessions and drafts are reset
|
|
88
|
+
along with the store; late results from the old environment cannot populate the
|
|
89
|
+
new view. Put UI state that should survive this switch above the provider.
|
|
90
|
+
Keep custom fetch functions stable between ordinary renders.
|
|
91
|
+
|
|
92
|
+
Two providers can target different environments simultaneously. The provider
|
|
93
|
+
does not persist a global environment in a cookie or local storage. Changing
|
|
94
|
+
only `user` retains the existing client, store and stream as before.
|
|
95
|
+
|
|
84
96
|
### Hooks
|
|
85
97
|
|
|
86
98
|
#### usePolpo()
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
|
+
import type { ProjectEnvironment } from "@polpo-ai/sdk";
|
|
2
3
|
export interface PolpoProviderProps {
|
|
3
4
|
baseUrl: string;
|
|
4
5
|
/** @deprecated No longer used. Kept for backwards compatibility. */
|
|
5
6
|
projectId?: string;
|
|
6
7
|
apiKey?: string;
|
|
8
|
+
/** Explicit target. Omit to use the environment bound to the credential/endpoint. */
|
|
9
|
+
environment?: ProjectEnvironment;
|
|
7
10
|
/**
|
|
8
11
|
* Override the fetch implementation used by the underlying `PolpoClient`.
|
|
9
12
|
* Useful for cookie-auth scenarios (`credentials: "include"`), request
|
|
@@ -41,5 +44,5 @@ export interface PolpoProviderProps {
|
|
|
41
44
|
autoConnect?: boolean;
|
|
42
45
|
eventFilter?: string[];
|
|
43
46
|
}
|
|
44
|
-
export declare function PolpoProvider({ baseUrl, apiKey, fetch, apiPrefix, user, children, autoConnect, eventFilter, }: PolpoProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
47
|
+
export declare function PolpoProvider({ baseUrl, apiKey, environment, fetch, apiPrefix, user, children, autoConnect, eventFilter, }: PolpoProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
45
48
|
//# sourceMappingURL=polpo-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polpo-provider.d.ts","sourceRoot":"","sources":["../../src/provider/polpo-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"polpo-provider.d.ts","sourceRoot":"","sources":["../../src/provider/polpo-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAI5D,OAAO,KAAK,EAAqB,kBAAkB,EAAY,MAAM,eAAe,CAAC;AAIrF,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,oEAAoE;IACpE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qFAAqF;IACrF,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;;;;;;;;;OAeG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,SAAS,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,wBAAgB,aAAa,CAAC,EAC5B,OAAO,EACP,MAAM,EACN,WAAW,EACX,KAAK,EACL,SAAS,EACT,IAAI,EACJ,QAAQ,EACR,WAAkB,EAClB,WAAW,GACZ,EAAE,kBAAkB,2CA+EpB"}
|
|
@@ -1,27 +1,23 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useEffect,
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
3
|
import { useStableValue } from "../hooks/use-stable-value.js";
|
|
4
4
|
import { PolpoClient } from "@polpo-ai/sdk";
|
|
5
5
|
import { EventSourceManager } from "@polpo-ai/sdk";
|
|
6
6
|
import { PolpoStore } from "@polpo-ai/sdk";
|
|
7
7
|
import { PolpoContext } from "./polpo-context.js";
|
|
8
|
-
export function PolpoProvider({ baseUrl, apiKey, fetch, apiPrefix, user, children, autoConnect = true, eventFilter, }) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
lastConfigKey.current = configKey;
|
|
20
|
-
clientRef.current = new PolpoClient({ baseUrl, apiKey, fetch, apiPrefix, user });
|
|
21
|
-
storeRef.current = new PolpoStore();
|
|
8
|
+
export function PolpoProvider({ baseUrl, apiKey, environment, fetch, apiPrefix, user, children, autoConnect = true, eventFilter, }) {
|
|
9
|
+
const config = { baseUrl, apiKey, environment, fetch, apiPrefix, user };
|
|
10
|
+
const [scope, setScope] = useState(() => createScope(config, 0));
|
|
11
|
+
// Adjust during render so React retries before rendering children with a
|
|
12
|
+
// mismatched client. A keyed boundary also resets hook-local sessions,
|
|
13
|
+
// drafts and pending mutations, which are not held in PolpoStore.
|
|
14
|
+
// User changes deliberately retain the current scope for compatibility.
|
|
15
|
+
if (scope.config.baseUrl !== baseUrl || scope.config.apiKey !== apiKey
|
|
16
|
+
|| scope.config.environment !== environment || scope.config.fetch !== fetch
|
|
17
|
+
|| scope.config.apiPrefix !== apiPrefix) {
|
|
18
|
+
setScope(createScope(config, scope.generation + 1));
|
|
22
19
|
}
|
|
23
|
-
const client =
|
|
24
|
-
const store = storeRef.current;
|
|
20
|
+
const { client, store } = scope.value;
|
|
25
21
|
const stableEventFilter = useStableValue(eventFilter);
|
|
26
22
|
// Sync the default user without rebuilding the client. Cheap; no-op when
|
|
27
23
|
// unchanged. Lets a chat session survive a token-refresh round-trip.
|
|
@@ -32,9 +28,12 @@ export function PolpoProvider({ baseUrl, apiKey, fetch, apiPrefix, user, childre
|
|
|
32
28
|
useEffect(() => {
|
|
33
29
|
if (!autoConnect)
|
|
34
30
|
return;
|
|
31
|
+
let active = true;
|
|
35
32
|
let pendingEvents = [];
|
|
36
33
|
let batchScheduled = false;
|
|
37
34
|
const flushBatch = () => {
|
|
35
|
+
if (!active)
|
|
36
|
+
return;
|
|
38
37
|
if (pendingEvents.length > 0) {
|
|
39
38
|
store.applyEventBatch(pendingEvents);
|
|
40
39
|
pendingEvents = [];
|
|
@@ -44,6 +43,8 @@ export function PolpoProvider({ baseUrl, apiKey, fetch, apiPrefix, user, childre
|
|
|
44
43
|
const es = new EventSourceManager({
|
|
45
44
|
url: client.getEventsUrl(stableEventFilter),
|
|
46
45
|
onEvent: (event) => {
|
|
46
|
+
if (!active)
|
|
47
|
+
return;
|
|
47
48
|
pendingEvents.push(event);
|
|
48
49
|
if (!batchScheduled) {
|
|
49
50
|
batchScheduled = true;
|
|
@@ -51,14 +52,20 @@ export function PolpoProvider({ baseUrl, apiKey, fetch, apiPrefix, user, childre
|
|
|
51
52
|
}
|
|
52
53
|
},
|
|
53
54
|
onStatusChange: (status) => {
|
|
55
|
+
if (!active)
|
|
56
|
+
return;
|
|
54
57
|
store.setConnectionStatus(status);
|
|
55
58
|
if (status === "connected") {
|
|
56
59
|
// Re-fetch all resources to fill any SSE gaps
|
|
57
60
|
Promise.all([
|
|
58
|
-
client.getTasks().then((t) =>
|
|
59
|
-
|
|
60
|
-
client.
|
|
61
|
-
|
|
61
|
+
client.getTasks().then((t) => { if (active)
|
|
62
|
+
store.setTasks(t); }),
|
|
63
|
+
client.getMissions().then((m) => { if (active)
|
|
64
|
+
store.setMissions(m); }),
|
|
65
|
+
client.getAgents().then((a) => { if (active)
|
|
66
|
+
store.setAgents(a); }),
|
|
67
|
+
client.getProcesses().then((p) => { if (active)
|
|
68
|
+
store.setProcesses(p); }),
|
|
62
69
|
]).catch(() => {
|
|
63
70
|
/* individual errors handled by hooks */
|
|
64
71
|
});
|
|
@@ -66,9 +73,19 @@ export function PolpoProvider({ baseUrl, apiKey, fetch, apiPrefix, user, childre
|
|
|
66
73
|
},
|
|
67
74
|
});
|
|
68
75
|
es.connect();
|
|
69
|
-
return () =>
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
return () => {
|
|
77
|
+
active = false;
|
|
78
|
+
pendingEvents = [];
|
|
79
|
+
es.disconnect();
|
|
80
|
+
};
|
|
81
|
+
}, [client, store, autoConnect, stableEventFilter]);
|
|
82
|
+
return (_jsx(PolpoContext.Provider, { value: scope.value, children: children }, scope.generation));
|
|
83
|
+
}
|
|
84
|
+
function createScope(config, generation) {
|
|
85
|
+
return {
|
|
86
|
+
config,
|
|
87
|
+
generation,
|
|
88
|
+
value: { client: new PolpoClient(config), store: new PolpoStore() },
|
|
89
|
+
};
|
|
73
90
|
}
|
|
74
91
|
//# sourceMappingURL=polpo-provider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polpo-provider.js","sourceRoot":"","sources":["../../src/provider/polpo-provider.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,
|
|
1
|
+
{"version":3,"file":"polpo-provider.js","sourceRoot":"","sources":["../../src/provider/polpo-provider.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAkB,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAE,cA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AAC,EAAE,KAAK,EAAE,IAAI,UAAU,EAAE,EAAE;KACpE,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polpo-ai/react",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.3",
|
|
4
4
|
"description": "React SDK for OpenPolpo — hooks with real-time SSE updates, built on @polpo-ai/sdk",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@polpo-ai/sdk": "0.17.
|
|
20
|
+
"@polpo-ai/sdk": "0.17.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"react": "^18.0.0 || ^19.0.0"
|