@promte/app-library 0.1.0 → 0.1.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/dist/index.d.ts +3 -0
- package/dist/index.mjs +55 -46
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ export declare type PromteClient = {
|
|
|
38
38
|
declare type PromteCtxValue = {
|
|
39
39
|
settings: PromteSettings | null;
|
|
40
40
|
token: string | null;
|
|
41
|
+
isDarkMode: boolean | null;
|
|
41
42
|
ready: boolean;
|
|
42
43
|
state: PromteHandshakeState;
|
|
43
44
|
error: string | null;
|
|
@@ -57,6 +58,8 @@ export declare function usePromte(): PromteCtxValue;
|
|
|
57
58
|
|
|
58
59
|
export declare function usePromteClient(): PromteClient;
|
|
59
60
|
|
|
61
|
+
export declare function usePromteDarkMode(): boolean;
|
|
62
|
+
|
|
60
63
|
export declare function usePromteSettings(): PromteSettings;
|
|
61
64
|
|
|
62
65
|
export declare function usePromteToken(): string;
|
package/dist/index.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { createContext as
|
|
3
|
-
const
|
|
4
|
-
function
|
|
1
|
+
import { jsx as T } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as U, useState as w, useMemo as p, useCallback as j, useEffect as g, useContext as x } from "react";
|
|
3
|
+
const P = U(null);
|
|
4
|
+
function L({
|
|
5
5
|
children: u,
|
|
6
|
-
expectedParentOrigin:
|
|
6
|
+
expectedParentOrigin: o,
|
|
7
7
|
initTargetOrigin: f,
|
|
8
8
|
retryInit: n = { enabled: !0, intervalMs: 500, maxAttempts: 20 }
|
|
9
9
|
}) {
|
|
10
|
-
const [t, c] =
|
|
10
|
+
const [t, c] = w(null), [e, i] = w(null), [v, E] = w(null), [m, k] = w(null), y = p(() => m ? "booting" : t ? e ? "ready" : "waiting_for_token" : "waiting_for_settings", [t, e, m]), b = y === "ready", M = p(() => f || o || "*", [f, o]), d = j(() => {
|
|
11
11
|
var l;
|
|
12
12
|
try {
|
|
13
|
-
const
|
|
14
|
-
(l = window.parent) == null || l.postMessage(
|
|
15
|
-
} catch (
|
|
16
|
-
|
|
13
|
+
const s = { type: "init" };
|
|
14
|
+
(l = window.parent) == null || l.postMessage(s, M);
|
|
15
|
+
} catch (s) {
|
|
16
|
+
k((s == null ? void 0 : s.message) || "Failed to post init");
|
|
17
17
|
}
|
|
18
|
-
}, [
|
|
18
|
+
}, [M]);
|
|
19
19
|
g(() => {
|
|
20
20
|
d();
|
|
21
21
|
}, [d]), g(() => {
|
|
22
22
|
if (!(n != null && n.enabled) || t) return;
|
|
23
|
-
const l = n.intervalMs ?? 500,
|
|
24
|
-
let
|
|
23
|
+
const l = n.intervalMs ?? 500, s = n.maxAttempts ?? 20;
|
|
24
|
+
let r = 0;
|
|
25
25
|
const a = window.setInterval(() => {
|
|
26
26
|
if (t) {
|
|
27
27
|
window.clearInterval(a);
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
|
-
if (
|
|
30
|
+
if (r += 1, r > s) {
|
|
31
31
|
window.clearInterval(a);
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
@@ -35,59 +35,67 @@ function _({
|
|
|
35
35
|
}, l);
|
|
36
36
|
return () => window.clearInterval(a);
|
|
37
37
|
}, [n, t, d]), g(() => {
|
|
38
|
-
const l = (
|
|
39
|
-
if (
|
|
40
|
-
const
|
|
41
|
-
if (!(!
|
|
42
|
-
if (
|
|
43
|
-
const a =
|
|
44
|
-
if (!(a != null && a.backendUrl) || a.parentOrigin &&
|
|
38
|
+
const l = (s) => {
|
|
39
|
+
if (o && s.origin !== o) return;
|
|
40
|
+
const r = s.data;
|
|
41
|
+
if (!(!r || typeof r != "object")) {
|
|
42
|
+
if (r.type === "set-settings") {
|
|
43
|
+
const a = r.value;
|
|
44
|
+
if (!(a != null && a.backendUrl) || a.parentOrigin && o && a.parentOrigin !== o)
|
|
45
45
|
return;
|
|
46
|
-
c(a),
|
|
46
|
+
c(a), k(null);
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
|
-
if (
|
|
50
|
-
i(
|
|
49
|
+
if (r.type === "set-token" && typeof r.value == "string") {
|
|
50
|
+
i(r.value), k(null);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (r.type === "set-dark-mode" && typeof r.value == "boolean") {
|
|
54
|
+
E(r.value);
|
|
51
55
|
return;
|
|
52
56
|
}
|
|
53
57
|
}
|
|
54
58
|
};
|
|
55
59
|
return window.addEventListener("message", l), () => window.removeEventListener("message", l);
|
|
56
|
-
}, [
|
|
57
|
-
const C =
|
|
60
|
+
}, [o]);
|
|
61
|
+
const C = p(() => D(
|
|
58
62
|
() => t,
|
|
59
63
|
() => e
|
|
60
|
-
), [t, e]),
|
|
64
|
+
), [t, e]), A = p(
|
|
61
65
|
() => ({
|
|
62
66
|
settings: t,
|
|
63
67
|
token: e,
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
isDarkMode: v,
|
|
69
|
+
ready: b,
|
|
70
|
+
state: y,
|
|
66
71
|
error: m,
|
|
67
72
|
client: C
|
|
68
73
|
}),
|
|
69
|
-
[t, e,
|
|
74
|
+
[t, e, v, b, y, m, C]
|
|
70
75
|
);
|
|
71
|
-
return /* @__PURE__ */
|
|
76
|
+
return /* @__PURE__ */ T(P.Provider, { value: A, children: u });
|
|
72
77
|
}
|
|
73
|
-
function
|
|
74
|
-
const u =
|
|
78
|
+
function h() {
|
|
79
|
+
const u = x(P);
|
|
75
80
|
if (!u)
|
|
76
81
|
throw new Error("usePromte must be used inside <PromteAppProvider>");
|
|
77
82
|
return u;
|
|
78
83
|
}
|
|
79
|
-
function L() {
|
|
80
|
-
return y().settings;
|
|
81
|
-
}
|
|
82
84
|
function R() {
|
|
83
|
-
return
|
|
85
|
+
return h().settings;
|
|
84
86
|
}
|
|
85
87
|
function B() {
|
|
86
|
-
return
|
|
88
|
+
return h().token;
|
|
89
|
+
}
|
|
90
|
+
function F() {
|
|
91
|
+
return h().isDarkMode;
|
|
92
|
+
}
|
|
93
|
+
function $() {
|
|
94
|
+
return h().client;
|
|
87
95
|
}
|
|
88
|
-
function
|
|
96
|
+
function D(u, o) {
|
|
89
97
|
async function f(n, t) {
|
|
90
|
-
const c = u(), e =
|
|
98
|
+
const c = u(), e = o();
|
|
91
99
|
if (!c) throw new Error("Promte settings not received yet");
|
|
92
100
|
if (!e) throw new Error("Promte token not received yet");
|
|
93
101
|
const i = new URL(c.backendUrl + "/" + n).toString();
|
|
@@ -101,7 +109,7 @@ function j(u, r) {
|
|
|
101
109
|
}
|
|
102
110
|
return {
|
|
103
111
|
isReady() {
|
|
104
|
-
return !!u() && !!
|
|
112
|
+
return !!u() && !!o();
|
|
105
113
|
},
|
|
106
114
|
getBackendUrl() {
|
|
107
115
|
var n;
|
|
@@ -128,9 +136,10 @@ function j(u, r) {
|
|
|
128
136
|
};
|
|
129
137
|
}
|
|
130
138
|
export {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
R as
|
|
139
|
+
L as PromteAppProvider,
|
|
140
|
+
h as usePromte,
|
|
141
|
+
$ as usePromteClient,
|
|
142
|
+
F as usePromteDarkMode,
|
|
143
|
+
R as usePromteSettings,
|
|
144
|
+
B as usePromteToken
|
|
136
145
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(r,m){typeof exports=="object"&&typeof module<"u"?m(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],m):(r=typeof globalThis<"u"?globalThis:r||self,m(r.PromteAppLibrary={},r.jsxRuntime,r.react))})(this,function(r,m,o){"use strict";const g=o.createContext(null);function E({children:l,expectedParentOrigin:u,initTargetOrigin:w,retryInit:n={enabled:!0,intervalMs:500,maxAttempts:20}}){const[e,f]=o.useState(null),[t,c]=o.useState(null),[y,U]=o.useState(null),[h,k]=o.useState(null),P=o.useMemo(()=>h?"booting":e?t?"ready":"waiting_for_token":"waiting_for_settings",[e,t,h]),M=P==="ready",b=o.useMemo(()=>w||u||"*",[w,u]),v=o.useCallback(()=>{var d;try{const i={type:"init"};(d=window.parent)==null||d.postMessage(i,b)}catch(i){k((i==null?void 0:i.message)||"Failed to post init")}},[b]);o.useEffect(()=>{v()},[v]),o.useEffect(()=>{if(!(n!=null&&n.enabled)||e)return;const d=n.intervalMs??500,i=n.maxAttempts??20;let s=0;const a=window.setInterval(()=>{if(e){window.clearInterval(a);return}if(s+=1,s>i){window.clearInterval(a);return}v()},d);return()=>window.clearInterval(a)},[n,e,v]),o.useEffect(()=>{const d=i=>{if(u&&i.origin!==u)return;const s=i.data;if(!(!s||typeof s!="object")){if(s.type==="set-settings"){const a=s.value;if(!(a!=null&&a.backendUrl)||a.parentOrigin&&u&&a.parentOrigin!==u)return;f(a),k(null);return}if(s.type==="set-token"&&typeof s.value=="string"){c(s.value),k(null);return}if(s.type==="set-dark-mode"&&typeof s.value=="boolean"){U(s.value);return}}};return window.addEventListener("message",d),()=>window.removeEventListener("message",d)},[u]);const C=o.useMemo(()=>D(()=>e,()=>t),[e,t]),L=o.useMemo(()=>({settings:e,token:t,isDarkMode:y,ready:M,state:P,error:h,client:C}),[e,t,y,M,P,h,C]);return m.jsx(g.Provider,{value:L,children:l})}function p(){const l=o.useContext(g);if(!l)throw new Error("usePromte must be used inside <PromteAppProvider>");return l}function S(){return p().settings}function j(){return p().token}function T(){return p().isDarkMode}function A(){return p().client}function D(l,u){async function w(n,e){const f=l(),t=u();if(!f)throw new Error("Promte settings not received yet");if(!t)throw new Error("Promte token not received yet");const c=new URL(f.backendUrl+"/"+n).toString();return await fetch(c,{...e,headers:{...e.headers||{},Authorization:`Bearer ${t}`}})}return{isReady(){return!!l()&&!!u()},getBackendUrl(){var n;return((n=l())==null?void 0:n.backendUrl)??null},async chatCompletions(n){var c;const e=await w("/openai/chat/completions",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({...n,stream:!1})}),t=(e.headers.get("content-type")||"").includes("application/json")?await e.json():await e.text();if(!e.ok)throw new Error(typeof t=="string"?t:((c=t==null?void 0:t.error)==null?void 0:c.message)||`Request failed (${e.status})`);return t},async chatText(n){var f,t,c;const e=await this.chatCompletions(n);return((c=(t=(f=e==null?void 0:e.choices)==null?void 0:f[0])==null?void 0:t.message)==null?void 0:c.content)??""}}}r.PromteAppProvider=E,r.usePromte=p,r.usePromteClient=A,r.usePromteDarkMode=T,r.usePromteSettings=S,r.usePromteToken=j,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|