@tipsy-studio/sdk 0.0.2 → 0.0.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/dist/react.d.cts CHANGED
@@ -1,101 +1,8 @@
1
+ import { TipsyBridgeRequestMap, TipsyChatCompletionController, TipsyRequestController, TipsyBridgeResponseMap, TipsyChatCompletionInput, TipsyChatCompletionResult } from './index.cjs';
2
+ export { CHAT_COMPLETIONS_METHOD, TIPSY_BRIDGE_PROTOCOL, TipsyAbortMessage, TipsyBridgeClientError, TipsyBridgeError, TipsyBridgeMessage, TipsyChatCompletionChunk, TipsyChatCompletionRawChunk, TipsyChatMessage, TipsyStudioClient, TipsyStudioClientOptions, createTipsyStudioClient } from './index.cjs';
1
3
  import * as react from 'react';
2
4
  import { PropsWithChildren } from 'react';
3
5
 
4
- declare const TIPSY_BRIDGE_PROTOCOL = "tipsy-bridge-v1";
5
- declare const CHAT_COMPLETIONS_METHOD = "chat.completions";
6
- type TipsyRole = "system" | "user" | "assistant";
7
- type TipsyChatMessage = {
8
- role: TipsyRole;
9
- content: string;
10
- };
11
- type TipsyChatCompletionInput = {
12
- messages: TipsyChatMessage[];
13
- stream: true;
14
- };
15
- type TipsyChatCompletionRawChunk = {
16
- type?: string;
17
- choices?: Array<{
18
- delta?: {
19
- content?: string;
20
- reasoning?: string;
21
- };
22
- }>;
23
- isFinished?: boolean;
24
- [key: string]: unknown;
25
- };
26
- type TipsyChatCompletionChunk = {
27
- type: "stream";
28
- chunk: string;
29
- raw: TipsyChatCompletionRawChunk;
30
- isFinished: false;
31
- };
32
- type TipsyChatCompletionResult = {
33
- type: "done";
34
- raw: TipsyChatCompletionRawChunk;
35
- isFinished: true;
36
- };
37
- type TipsyBridgeErrorCode = "UNAUTHORIZED" | "BAD_REQUEST" | "NETWORK_ERROR" | "STREAM_PARSE_ERROR" | "ORIGIN_MISMATCH" | "UNSUPPORTED_METHOD" | "INTERNAL_ERROR";
38
- type TipsyBridgeError = {
39
- code: TipsyBridgeErrorCode;
40
- message: string;
41
- };
42
- type TipsyBridgeRequestMap = {
43
- [CHAT_COMPLETIONS_METHOD]: TipsyChatCompletionInput;
44
- };
45
- type TipsyBridgeResponseMap = {
46
- [CHAT_COMPLETIONS_METHOD]: TipsyChatCompletionResult;
47
- };
48
- type TipsyInitMessage = {
49
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
50
- type: "tipsy:init";
51
- };
52
- type TipsyInitAckMessage = {
53
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
54
- type: "tipsy:init:ack";
55
- };
56
- type TipsyRequestMessage<M extends keyof TipsyBridgeRequestMap> = {
57
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
58
- type: "tipsy:request";
59
- requestId: string;
60
- method: M;
61
- payload: TipsyBridgeRequestMap[M];
62
- };
63
- type TipsyStreamMessage = {
64
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
65
- type: "tipsy:stream";
66
- requestId: string;
67
- payload: TipsyChatCompletionChunk;
68
- };
69
- type TipsyResponseMessage<M extends keyof TipsyBridgeResponseMap> = {
70
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
71
- type: "tipsy:response";
72
- requestId: string;
73
- payload: TipsyBridgeResponseMap[M];
74
- };
75
- type TipsyErrorMessage = {
76
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
77
- type: "tipsy:error";
78
- requestId?: string;
79
- error: TipsyBridgeError;
80
- };
81
- type TipsyAbortMessage = {
82
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
83
- type: "tipsy:abort";
84
- requestId: string;
85
- };
86
- type TipsyBridgeMessage = TipsyInitMessage | TipsyInitAckMessage | TipsyRequestMessage<keyof TipsyBridgeRequestMap> | TipsyStreamMessage | TipsyResponseMessage<keyof TipsyBridgeResponseMap> | TipsyErrorMessage | TipsyAbortMessage;
87
- type TipsyRequestController = {
88
- signal?: AbortSignal;
89
- };
90
- type TipsyChatCompletionController = TipsyRequestController & {
91
- onData?: (chunk: TipsyChatCompletionChunk) => void;
92
- onComplete?: (result: TipsyChatCompletionResult) => void;
93
- };
94
- declare class TipsyBridgeClientError extends Error {
95
- code: TipsyBridgeErrorCode;
96
- constructor(error: TipsyBridgeError);
97
- }
98
-
99
6
  type TipsyStudioProviderProps = PropsWithChildren<{
100
7
  targetOrigin?: string;
101
8
  }>;
@@ -110,4 +17,4 @@ declare function useTipsyChat(): {
110
17
  completions(input: TipsyChatCompletionInput, controller?: TipsyChatCompletionController): Promise<TipsyChatCompletionResult>;
111
18
  };
112
19
 
113
- export { CHAT_COMPLETIONS_METHOD, TIPSY_BRIDGE_PROTOCOL, type TipsyAbortMessage, TipsyBridgeClientError, type TipsyBridgeError, type TipsyBridgeMessage, type TipsyBridgeRequestMap, type TipsyBridgeResponseMap, type TipsyChatCompletionChunk, type TipsyChatCompletionController, type TipsyChatCompletionInput, type TipsyChatCompletionRawChunk, type TipsyChatCompletionResult, type TipsyChatMessage, TipsyStudioProvider, type TipsyStudioProviderProps, useTipsyChat, useTipsyStudio };
20
+ export { TipsyBridgeRequestMap, TipsyBridgeResponseMap, TipsyChatCompletionController, TipsyChatCompletionInput, TipsyChatCompletionResult, TipsyStudioProvider, type TipsyStudioProviderProps, useTipsyChat, useTipsyStudio };
package/dist/react.d.ts CHANGED
@@ -1,101 +1,8 @@
1
+ import { TipsyBridgeRequestMap, TipsyChatCompletionController, TipsyRequestController, TipsyBridgeResponseMap, TipsyChatCompletionInput, TipsyChatCompletionResult } from './index.js';
2
+ export { CHAT_COMPLETIONS_METHOD, TIPSY_BRIDGE_PROTOCOL, TipsyAbortMessage, TipsyBridgeClientError, TipsyBridgeError, TipsyBridgeMessage, TipsyChatCompletionChunk, TipsyChatCompletionRawChunk, TipsyChatMessage, TipsyStudioClient, TipsyStudioClientOptions, createTipsyStudioClient } from './index.js';
1
3
  import * as react from 'react';
2
4
  import { PropsWithChildren } from 'react';
3
5
 
4
- declare const TIPSY_BRIDGE_PROTOCOL = "tipsy-bridge-v1";
5
- declare const CHAT_COMPLETIONS_METHOD = "chat.completions";
6
- type TipsyRole = "system" | "user" | "assistant";
7
- type TipsyChatMessage = {
8
- role: TipsyRole;
9
- content: string;
10
- };
11
- type TipsyChatCompletionInput = {
12
- messages: TipsyChatMessage[];
13
- stream: true;
14
- };
15
- type TipsyChatCompletionRawChunk = {
16
- type?: string;
17
- choices?: Array<{
18
- delta?: {
19
- content?: string;
20
- reasoning?: string;
21
- };
22
- }>;
23
- isFinished?: boolean;
24
- [key: string]: unknown;
25
- };
26
- type TipsyChatCompletionChunk = {
27
- type: "stream";
28
- chunk: string;
29
- raw: TipsyChatCompletionRawChunk;
30
- isFinished: false;
31
- };
32
- type TipsyChatCompletionResult = {
33
- type: "done";
34
- raw: TipsyChatCompletionRawChunk;
35
- isFinished: true;
36
- };
37
- type TipsyBridgeErrorCode = "UNAUTHORIZED" | "BAD_REQUEST" | "NETWORK_ERROR" | "STREAM_PARSE_ERROR" | "ORIGIN_MISMATCH" | "UNSUPPORTED_METHOD" | "INTERNAL_ERROR";
38
- type TipsyBridgeError = {
39
- code: TipsyBridgeErrorCode;
40
- message: string;
41
- };
42
- type TipsyBridgeRequestMap = {
43
- [CHAT_COMPLETIONS_METHOD]: TipsyChatCompletionInput;
44
- };
45
- type TipsyBridgeResponseMap = {
46
- [CHAT_COMPLETIONS_METHOD]: TipsyChatCompletionResult;
47
- };
48
- type TipsyInitMessage = {
49
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
50
- type: "tipsy:init";
51
- };
52
- type TipsyInitAckMessage = {
53
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
54
- type: "tipsy:init:ack";
55
- };
56
- type TipsyRequestMessage<M extends keyof TipsyBridgeRequestMap> = {
57
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
58
- type: "tipsy:request";
59
- requestId: string;
60
- method: M;
61
- payload: TipsyBridgeRequestMap[M];
62
- };
63
- type TipsyStreamMessage = {
64
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
65
- type: "tipsy:stream";
66
- requestId: string;
67
- payload: TipsyChatCompletionChunk;
68
- };
69
- type TipsyResponseMessage<M extends keyof TipsyBridgeResponseMap> = {
70
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
71
- type: "tipsy:response";
72
- requestId: string;
73
- payload: TipsyBridgeResponseMap[M];
74
- };
75
- type TipsyErrorMessage = {
76
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
77
- type: "tipsy:error";
78
- requestId?: string;
79
- error: TipsyBridgeError;
80
- };
81
- type TipsyAbortMessage = {
82
- protocol: typeof TIPSY_BRIDGE_PROTOCOL;
83
- type: "tipsy:abort";
84
- requestId: string;
85
- };
86
- type TipsyBridgeMessage = TipsyInitMessage | TipsyInitAckMessage | TipsyRequestMessage<keyof TipsyBridgeRequestMap> | TipsyStreamMessage | TipsyResponseMessage<keyof TipsyBridgeResponseMap> | TipsyErrorMessage | TipsyAbortMessage;
87
- type TipsyRequestController = {
88
- signal?: AbortSignal;
89
- };
90
- type TipsyChatCompletionController = TipsyRequestController & {
91
- onData?: (chunk: TipsyChatCompletionChunk) => void;
92
- onComplete?: (result: TipsyChatCompletionResult) => void;
93
- };
94
- declare class TipsyBridgeClientError extends Error {
95
- code: TipsyBridgeErrorCode;
96
- constructor(error: TipsyBridgeError);
97
- }
98
-
99
6
  type TipsyStudioProviderProps = PropsWithChildren<{
100
7
  targetOrigin?: string;
101
8
  }>;
@@ -110,4 +17,4 @@ declare function useTipsyChat(): {
110
17
  completions(input: TipsyChatCompletionInput, controller?: TipsyChatCompletionController): Promise<TipsyChatCompletionResult>;
111
18
  };
112
19
 
113
- export { CHAT_COMPLETIONS_METHOD, TIPSY_BRIDGE_PROTOCOL, type TipsyAbortMessage, TipsyBridgeClientError, type TipsyBridgeError, type TipsyBridgeMessage, type TipsyBridgeRequestMap, type TipsyBridgeResponseMap, type TipsyChatCompletionChunk, type TipsyChatCompletionController, type TipsyChatCompletionInput, type TipsyChatCompletionRawChunk, type TipsyChatCompletionResult, type TipsyChatMessage, TipsyStudioProvider, type TipsyStudioProviderProps, useTipsyChat, useTipsyStudio };
20
+ export { TipsyBridgeRequestMap, TipsyBridgeResponseMap, TipsyChatCompletionController, TipsyChatCompletionInput, TipsyChatCompletionResult, TipsyStudioProvider, type TipsyStudioProviderProps, useTipsyChat, useTipsyStudio };
package/dist/react.js CHANGED
@@ -1,61 +1,7 @@
1
- import { createContext, useMemo, useState, useRef, useEffect, createElement, useContext } from 'react';
1
+ import { normalizeTargetOrigin, createTipsyStudioClient, TipsyBridgeClientError, CHAT_COMPLETIONS_METHOD } from './chunk-2TOC6J52.js';
2
+ export { CHAT_COMPLETIONS_METHOD, TIPSY_BRIDGE_PROTOCOL, TipsyBridgeClientError, createTipsyStudioClient } from './chunk-2TOC6J52.js';
3
+ import { createContext, useMemo, useState, useEffect, createElement, useContext } from 'react';
2
4
 
3
- // src/react.ts
4
-
5
- // src/bridge.ts
6
- var TIPSY_BRIDGE_PROTOCOL = "tipsy-bridge-v1";
7
- var TIPSY_INIT_RETRY_MS = 500;
8
- var CHAT_COMPLETIONS_METHOD = "chat.completions";
9
- var TipsyBridgeClientError = class extends Error {
10
- constructor(error) {
11
- super(error.message);
12
- this.name = "TipsyBridgeClientError";
13
- this.code = error.code;
14
- }
15
- };
16
- function getParentOriginFromReferrer() {
17
- if (typeof document === "undefined" || !document.referrer) {
18
- return null;
19
- }
20
- try {
21
- return new URL(document.referrer).origin;
22
- } catch {
23
- return null;
24
- }
25
- }
26
- function normalizeTargetOrigin(value) {
27
- if (value && value.trim()) {
28
- return value;
29
- }
30
- return getParentOriginFromReferrer() ?? "*";
31
- }
32
- function isBridgeMessage(value) {
33
- if (!value || typeof value !== "object") {
34
- return false;
35
- }
36
- const candidate = value;
37
- if (candidate.protocol !== TIPSY_BRIDGE_PROTOCOL) {
38
- return false;
39
- }
40
- const validTypes = /* @__PURE__ */ new Set([
41
- "tipsy:init",
42
- "tipsy:init:ack",
43
- "tipsy:request",
44
- "tipsy:stream",
45
- "tipsy:response",
46
- "tipsy:error",
47
- "tipsy:abort"
48
- ]);
49
- return typeof candidate.type === "string" && validTypes.has(candidate.type);
50
- }
51
- function createRequestId() {
52
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
53
- return crypto.randomUUID();
54
- }
55
- return `tipsy_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
56
- }
57
-
58
- // src/react.ts
59
5
  var TipsyStudioContext = createContext(null);
60
6
  function TipsyStudioProvider({
61
7
  children,
@@ -66,108 +12,29 @@ function TipsyStudioProvider({
66
12
  [targetOrigin]
67
13
  );
68
14
  const [isReady, setIsReady] = useState(false);
69
- const pendingRef = useRef(/* @__PURE__ */ new Map());
70
- const readyRef = useRef(false);
71
- const targetOriginRef = useRef(resolvedTargetOrigin);
72
- useEffect(() => {
73
- targetOriginRef.current = resolvedTargetOrigin;
74
- }, [resolvedTargetOrigin]);
15
+ const [client, setClient] = useState(null);
75
16
  useEffect(() => {
76
- if (typeof window === "undefined") {
77
- return void 0;
78
- }
79
- let cancelled = false;
80
- const rejectPending = (error) => {
81
- const pendingRequests = Array.from(pendingRef.current.values());
82
- pendingRef.current.clear();
83
- for (const pending of pendingRequests) {
84
- pending.reject(new TipsyBridgeClientError(error));
85
- }
86
- };
87
- const postInit = () => {
88
- if (cancelled || readyRef.current || window.parent === window) {
89
- return;
90
- }
91
- const initMessage = {
92
- protocol: TIPSY_BRIDGE_PROTOCOL,
93
- type: "tipsy:init"
94
- };
95
- window.parent.postMessage(initMessage, targetOriginRef.current);
96
- };
97
- const intervalId = window.setInterval(postInit, TIPSY_INIT_RETRY_MS);
98
- postInit();
99
- const handleMessage = (event) => {
100
- if (window.parent === window || event.source !== window.parent) {
101
- return;
102
- }
103
- if (targetOriginRef.current !== "*" && event.origin !== targetOriginRef.current) {
104
- return;
105
- }
106
- if (!isBridgeMessage(event.data)) {
107
- return;
108
- }
109
- if (event.data.type === "tipsy:init:ack") {
110
- readyRef.current = true;
111
- setIsReady(true);
112
- return;
113
- }
114
- if (event.data.type === "tipsy:error") {
115
- if (!event.data.requestId) {
116
- return;
117
- }
118
- const pending = pendingRef.current.get(event.data.requestId);
119
- if (!pending) {
120
- return;
121
- }
122
- pendingRef.current.delete(event.data.requestId);
123
- pending.reject(new TipsyBridgeClientError(event.data.error));
124
- return;
125
- }
126
- if (event.data.type === "tipsy:stream") {
127
- const pending = pendingRef.current.get(event.data.requestId);
128
- if (!pending || pending.method !== CHAT_COMPLETIONS_METHOD) {
129
- return;
130
- }
131
- pending.onData?.(event.data.payload);
132
- return;
133
- }
134
- if (event.data.type === "tipsy:response") {
135
- const pending = pendingRef.current.get(event.data.requestId);
136
- if (!pending) {
137
- return;
138
- }
139
- pendingRef.current.delete(event.data.requestId);
140
- pending.onComplete?.(event.data.payload);
141
- pending.resolve(event.data.payload);
142
- }
143
- };
144
- window.addEventListener("message", handleMessage);
17
+ const nextClient = createTipsyStudioClient({ targetOrigin });
18
+ setClient(nextClient);
19
+ setIsReady(nextClient.isReady());
20
+ const unsubscribe = nextClient.subscribe((nextIsReady) => {
21
+ setIsReady(nextIsReady);
22
+ });
145
23
  return () => {
146
- cancelled = true;
147
- readyRef.current = false;
24
+ unsubscribe();
25
+ nextClient.destroy();
26
+ setClient(
27
+ (currentClient) => currentClient === nextClient ? null : currentClient
28
+ );
148
29
  setIsReady(false);
149
- window.clearInterval(intervalId);
150
- window.removeEventListener("message", handleMessage);
151
- rejectPending({
152
- code: "NETWORK_ERROR",
153
- message: "Tipsy bridge was disconnected."
154
- });
155
30
  };
156
- }, []);
31
+ }, [targetOrigin]);
157
32
  const contextValue = useMemo(() => {
158
33
  return {
159
34
  isReady,
160
35
  targetOrigin: resolvedTargetOrigin,
161
36
  call(method, payload, controller) {
162
- if (typeof window === "undefined" || window.parent === window) {
163
- return Promise.reject(
164
- new TipsyBridgeClientError({
165
- code: "NETWORK_ERROR",
166
- message: "Tipsy bridge is only available inside an iframe."
167
- })
168
- );
169
- }
170
- if (!readyRef.current) {
37
+ if (!client) {
171
38
  return Promise.reject(
172
39
  new TipsyBridgeClientError({
173
40
  code: "NETWORK_ERROR",
@@ -175,51 +42,10 @@ function TipsyStudioProvider({
175
42
  })
176
43
  );
177
44
  }
178
- const requestId = createRequestId();
179
- return new Promise(
180
- (resolve, reject) => {
181
- const pending = {
182
- method,
183
- resolve: (value) => resolve(value),
184
- reject,
185
- onData: method === CHAT_COMPLETIONS_METHOD ? controller?.onData : void 0,
186
- onComplete: method === CHAT_COMPLETIONS_METHOD ? controller?.onComplete : void 0
187
- };
188
- pendingRef.current.set(requestId, pending);
189
- const abortSignal = controller?.signal;
190
- const handleAbort = () => {
191
- if (!pendingRef.current.has(requestId)) {
192
- return;
193
- }
194
- pendingRef.current.delete(requestId);
195
- const abortMessage = {
196
- protocol: TIPSY_BRIDGE_PROTOCOL,
197
- type: "tipsy:abort",
198
- requestId
199
- };
200
- window.parent.postMessage(abortMessage, targetOriginRef.current);
201
- reject(
202
- new DOMException("The operation was aborted.", "AbortError")
203
- );
204
- };
205
- if (abortSignal?.aborted) {
206
- handleAbort();
207
- return;
208
- }
209
- abortSignal?.addEventListener("abort", handleAbort, { once: true });
210
- const requestMessage = {
211
- protocol: TIPSY_BRIDGE_PROTOCOL,
212
- type: "tipsy:request",
213
- requestId,
214
- method,
215
- payload
216
- };
217
- window.parent.postMessage(requestMessage, targetOriginRef.current);
218
- }
219
- );
45
+ return client.call(method, payload, controller);
220
46
  }
221
47
  };
222
- }, [isReady, resolvedTargetOrigin]);
48
+ }, [client, isReady, resolvedTargetOrigin]);
223
49
  return createElement(
224
50
  TipsyStudioContext.Provider,
225
51
  { value: contextValue },
@@ -244,6 +70,6 @@ function useTipsyChat() {
244
70
  }, [studio]);
245
71
  }
246
72
 
247
- export { CHAT_COMPLETIONS_METHOD, TIPSY_BRIDGE_PROTOCOL, TipsyBridgeClientError, TipsyStudioProvider, useTipsyChat, useTipsyStudio };
73
+ export { TipsyStudioProvider, useTipsyChat, useTipsyStudio };
248
74
  //# sourceMappingURL=react.js.map
249
75
  //# sourceMappingURL=react.js.map
package/dist/react.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/bridge.ts","../src/react.ts"],"names":[],"mappings":";;;;;AAAO,IAAM,qBAAA,GAAwB;AAC9B,IAAM,mBAAA,GAAsB,GAAA;AAC5B,IAAM,uBAAA,GAA0B;AAqIhC,IAAM,sBAAA,GAAN,cAAqC,KAAA,CAAM;AAAA,EAGhD,YAAY,KAAA,EAAyB;AACnC,IAAA,KAAA,CAAM,MAAM,OAAO,CAAA;AACnB,IAAA,IAAA,CAAK,IAAA,GAAO,wBAAA;AACZ,IAAA,IAAA,CAAK,OAAO,KAAA,CAAM,IAAA;AAAA,EACpB;AACF;AAEO,SAAS,2BAAA,GAA6C;AAC3D,EAAA,IAAI,OAAO,QAAA,KAAa,WAAA,IAAe,CAAC,SAAS,QAAA,EAAU;AACzD,IAAA,OAAO,IAAA;AAAA,EACT;AAEA,EAAA,IAAI;AACF,IAAA,OAAO,IAAI,GAAA,CAAI,QAAA,CAAS,QAAQ,CAAA,CAAE,MAAA;AAAA,EACpC,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,IAAA;AAAA,EACT;AACF;AAEO,SAAS,sBAAsB,KAAA,EAAwB;AAC5D,EAAA,IAAI,KAAA,IAAS,KAAA,CAAM,IAAA,EAAK,EAAG;AACzB,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,OAAO,6BAA4B,IAAK,GAAA;AAC1C;AAEO,SAAS,gBAAgB,KAAA,EAA6C;AAC3E,EAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,MAAM,SAAA,GAAY,KAAA;AAClB,EAAA,IAAI,SAAA,CAAU,aAAa,qBAAA,EAAuB;AAChD,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,MAAM,UAAA,uBAAiB,GA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AAA,EAAa,EAAE,IAAA,EAAM,MAAM,CAAA;AAElE,YAAA,MAAM,cAAA,GAAqD;AAAA,cACzD,QAAA,EAAU,qBAAA;AAAA,cACV,IAAA,EAAM,eAAA;AAAA,cACN,SAAA;AAAA,cACA,MAAA;AAAA,cACA;AAAA,aACF;AAEA,YAAA,MAAA,CAAO,MAAA,CAAO,WAAA,CAAY,cAAA,EAAgB,eAAA,CAAgB,OAAO,CAAA;AAAA,UACnE;AAAA,SACF;AAAA,MACF;AAAA,KACF;AAAA,EACF,CAAA,EAAG,CAAC,OAAA,EAAS,oBAAoB,CAAC,CAAA;AAElC,EAAA,OAAO,aAAA;AAAA,IACL,kBAAA,CAAmB,QAAA;AAAA,IACnB,EAAE,OAAO,YAAA,EAAa;AAAA,IACtB;AAAA,GACF;AACF;AAEO,SAAS,cAAA,GAAiB;AAC/B,EAAA,MAAM,OAAA,GAAU,WAAW,kBAAkB,CAAA;AAE7C,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,yDAAyD,CAAA;AAAA,EAC3E;AAEA,EAAA,OAAO,OAAA;AACT;AAEO,SAAS,YAAA,GAAe;AAC7B,EAAA,MAAM,SAAS,cAAA,EAAe;AAE9B,EAAA,OAAO,QAAQ,MAAM;AACnB,IAAA,OAAO;AAAA,MACL,WAAA,CACE,OACA,UAAA,EACA;AACA,QAAA,OAAO,MAAA,CAAO,IAAA,CAAK,uBAAA,EAAyB,KAAA,EAAO,UAAU,CAAA;AAAA,MAC/D;AAAA,KACF;AAAA,EACF,CAAA,EAAG,CAAC,MAAM,CAAC,CAAA;AACb","file":"react.js","sourcesContent":["export const TIPSY_BRIDGE_PROTOCOL = \"tipsy-bridge-v1\";\nexport const TIPSY_INIT_RETRY_MS = 500;\nexport const CHAT_COMPLETIONS_METHOD = \"chat.completions\";\n\ntype TipsyBridgeMessageType =\n | \"tipsy:init\"\n | \"tipsy:init:ack\"\n | \"tipsy:request\"\n | \"tipsy:stream\"\n | \"tipsy:response\"\n | \"tipsy:error\"\n | \"tipsy:abort\";\n\ntype TipsyRole = \"system\" | \"user\" | \"assistant\";\n\nexport type TipsyChatMessage = {\n role: TipsyRole;\n content: string;\n};\n\nexport type TipsyChatCompletionInput = {\n messages: TipsyChatMessage[];\n stream: true;\n};\n\nexport type TipsyChatCompletionRawChunk = {\n type?: string;\n choices?: Array<{\n delta?: {\n content?: string;\n reasoning?: string;\n };\n }>;\n isFinished?: boolean;\n [key: string]: unknown;\n};\n\nexport type TipsyChatCompletionChunk = {\n type: \"stream\";\n chunk: string;\n raw: TipsyChatCompletionRawChunk;\n isFinished: false;\n};\n\nexport type TipsyChatCompletionResult = {\n type: \"done\";\n raw: TipsyChatCompletionRawChunk;\n isFinished: true;\n};\n\nexport type TipsyBridgeErrorCode =\n | \"UNAUTHORIZED\"\n | \"BAD_REQUEST\"\n | \"NETWORK_ERROR\"\n | \"STREAM_PARSE_ERROR\"\n | \"ORIGIN_MISMATCH\"\n | \"UNSUPPORTED_METHOD\"\n | \"INTERNAL_ERROR\";\n\nexport type TipsyBridgeError = {\n code: TipsyBridgeErrorCode;\n message: string;\n};\n\nexport type TipsyBridgeRequestMap = {\n [CHAT_COMPLETIONS_METHOD]: TipsyChatCompletionInput;\n};\n\nexport type TipsyBridgeResponseMap = {\n [CHAT_COMPLETIONS_METHOD]: TipsyChatCompletionResult;\n};\n\nexport type TipsyInitMessage = {\n protocol: typeof TIPSY_BRIDGE_PROTOCOL;\n type: \"tipsy:init\";\n};\n\ntype TipsyInitAckMessage = {\n protocol: typeof TIPSY_BRIDGE_PROTOCOL;\n type: \"tipsy:init:ack\";\n};\n\nexport type TipsyRequestMessage<M extends keyof TipsyBridgeRequestMap> = {\n protocol: typeof TIPSY_BRIDGE_PROTOCOL;\n type: \"tipsy:request\";\n requestId: string;\n method: M;\n payload: TipsyBridgeRequestMap[M];\n};\n\ntype TipsyStreamMessage = {\n protocol: typeof TIPSY_BRIDGE_PROTOCOL;\n type: \"tipsy:stream\";\n requestId: string;\n payload: TipsyChatCompletionChunk;\n};\n\ntype TipsyResponseMessage<M extends keyof TipsyBridgeResponseMap> = {\n protocol: typeof TIPSY_BRIDGE_PROTOCOL;\n type: \"tipsy:response\";\n requestId: string;\n payload: TipsyBridgeResponseMap[M];\n};\n\ntype TipsyErrorMessage = {\n protocol: typeof TIPSY_BRIDGE_PROTOCOL;\n type: \"tipsy:error\";\n requestId?: string;\n error: TipsyBridgeError;\n};\n\nexport type TipsyAbortMessage = {\n protocol: typeof TIPSY_BRIDGE_PROTOCOL;\n type: \"tipsy:abort\";\n requestId: string;\n};\n\nexport type TipsyBridgeMessage =\n | TipsyInitMessage\n | TipsyInitAckMessage\n | TipsyRequestMessage<keyof TipsyBridgeRequestMap>\n | TipsyStreamMessage\n | TipsyResponseMessage<keyof TipsyBridgeResponseMap>\n | TipsyErrorMessage\n | TipsyAbortMessage;\n\nexport type TipsyRequestController = {\n signal?: AbortSignal;\n};\n\nexport type TipsyChatCompletionController = TipsyRequestController & {\n onData?: (chunk: TipsyChatCompletionChunk) => void;\n onComplete?: (result: TipsyChatCompletionResult) => void;\n};\n\nexport class TipsyBridgeClientError extends Error {\n code: TipsyBridgeErrorCode;\n\n constructor(error: TipsyBridgeError) {\n super(error.message);\n this.name = \"TipsyBridgeClientError\";\n this.code = error.code;\n }\n}\n\nexport function getParentOriginFromReferrer(): string | null {\n if (typeof document === \"undefined\" || !document.referrer) {\n return null;\n }\n\n try {\n return new URL(document.referrer).origin;\n } catch {\n return null;\n }\n}\n\nexport function normalizeTargetOrigin(value?: string): string {\n if (value && value.trim()) {\n return value;\n }\n\n return getParentOriginFromReferrer() ?? \"*\";\n}\n\nexport function isBridgeMessage(value: unknown): value is TipsyBridgeMessage {\n if (!value || typeof value !== \"object\") {\n return false;\n }\n\n const candidate = value as Partial<TipsyBridgeMessage>;\n if (candidate.protocol !== TIPSY_BRIDGE_PROTOCOL) {\n return false;\n }\n\n const validTypes = new Set<TipsyBridgeMessageType>([\n \"tipsy:init\",\n \"tipsy:init:ack\",\n \"tipsy:request\",\n \"tipsy:stream\",\n \"tipsy:response\",\n \"tipsy:error\",\n \"tipsy:abort\",\n ]);\n\n return typeof candidate.type === \"string\" && validTypes.has(candidate.type);\n}\n\nexport function createRequestId() {\n if (\n typeof crypto !== \"undefined\" &&\n typeof crypto.randomUUID === \"function\"\n ) {\n return crypto.randomUUID();\n }\n\n return `tipsy_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;\n}\n","import {\n createElement,\n createContext,\n type PropsWithChildren,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport {\n CHAT_COMPLETIONS_METHOD,\n TIPSY_BRIDGE_PROTOCOL,\n TIPSY_INIT_RETRY_MS,\n type TipsyAbortMessage,\n type TipsyBridgeError,\n type TipsyBridgeRequestMap,\n type TipsyBridgeResponseMap,\n type TipsyInitMessage,\n type TipsyRequestController,\n type TipsyRequestMessage,\n TipsyBridgeClientError,\n type TipsyChatCompletionChunk,\n type TipsyChatCompletionController,\n type TipsyChatCompletionInput,\n type TipsyChatCompletionResult,\n createRequestId,\n isBridgeMessage,\n normalizeTargetOrigin,\n} from \"./bridge\";\nexport {\n CHAT_COMPLETIONS_METHOD,\n TIPSY_BRIDGE_PROTOCOL,\n TipsyBridgeClientError,\n type TipsyAbortMessage,\n type TipsyBridgeError,\n type TipsyBridgeMessage,\n type TipsyBridgeRequestMap,\n type TipsyBridgeResponseMap,\n type TipsyChatCompletionChunk,\n type TipsyChatCompletionController,\n type TipsyChatCompletionInput,\n type TipsyChatCompletionRawChunk,\n type TipsyChatCompletionResult,\n type TipsyChatMessage,\n} from \"./bridge\";\n\nexport type TipsyStudioProviderProps = PropsWithChildren<{\n targetOrigin?: string;\n}>;\n\ntype PendingRequest = {\n method: keyof TipsyBridgeRequestMap;\n resolve: (value: TipsyChatCompletionResult) => void;\n reject: (reason?: unknown) => void;\n onData?: (chunk: TipsyChatCompletionChunk) => void;\n onComplete?: (result: TipsyChatCompletionResult) => void;\n};\n\ntype TipsyStudioContextValue = {\n isReady: boolean;\n targetOrigin: string;\n call: <M extends keyof TipsyBridgeRequestMap>(\n method: M,\n payload: TipsyBridgeRequestMap[M],\n controller?: M extends \"chat.completions\"\n ? TipsyChatCompletionController\n : TipsyRequestController,\n ) => Promise<TipsyBridgeResponseMap[M]>;\n};\n\nconst TipsyStudioContext = createContext<TipsyStudioContextValue | null>(null);\n\nexport function TipsyStudioProvider({\n children,\n targetOrigin,\n}: TipsyStudioProviderProps) {\n const resolvedTargetOrigin = useMemo(\n () => normalizeTargetOrigin(targetOrigin),\n [targetOrigin],\n );\n const [isReady, setIsReady] = useState(false);\n const pendingRef = useRef<Map<string, PendingRequest>>(new Map());\n const readyRef = useRef(false);\n const targetOriginRef = useRef(resolvedTargetOrigin);\n\n useEffect(() => {\n targetOriginRef.current = resolvedTargetOrigin;\n }, [resolvedTargetOrigin]);\n\n useEffect(() => {\n if (typeof window === \"undefined\") {\n return undefined;\n }\n\n let cancelled = false;\n\n const rejectPending = (error: TipsyBridgeError) => {\n const pendingRequests = Array.from(pendingRef.current.values());\n pendingRef.current.clear();\n for (const pending of pendingRequests) {\n pending.reject(new TipsyBridgeClientError(error));\n }\n };\n\n const postInit = () => {\n if (cancelled || readyRef.current || window.parent === window) {\n return;\n }\n\n const initMessage: TipsyInitMessage = {\n protocol: TIPSY_BRIDGE_PROTOCOL,\n type: \"tipsy:init\",\n };\n\n window.parent.postMessage(initMessage, targetOriginRef.current);\n };\n\n const intervalId = window.setInterval(postInit, TIPSY_INIT_RETRY_MS);\n postInit();\n\n const handleMessage = (event: MessageEvent) => {\n if (window.parent === window || event.source !== window.parent) {\n return;\n }\n\n if (\n targetOriginRef.current !== \"*\" &&\n event.origin !== targetOriginRef.current\n ) {\n return;\n }\n\n if (!isBridgeMessage(event.data)) {\n return;\n }\n\n if (event.data.type === \"tipsy:init:ack\") {\n readyRef.current = true;\n setIsReady(true);\n return;\n }\n\n if (event.data.type === \"tipsy:error\") {\n if (!event.data.requestId) {\n return;\n }\n\n const pending = pendingRef.current.get(event.data.requestId);\n if (!pending) {\n return;\n }\n\n pendingRef.current.delete(event.data.requestId);\n pending.reject(new TipsyBridgeClientError(event.data.error));\n return;\n }\n\n if (event.data.type === \"tipsy:stream\") {\n const pending = pendingRef.current.get(event.data.requestId);\n if (!pending || pending.method !== CHAT_COMPLETIONS_METHOD) {\n return;\n }\n\n pending.onData?.(event.data.payload);\n return;\n }\n\n if (event.data.type === \"tipsy:response\") {\n const pending = pendingRef.current.get(event.data.requestId);\n if (!pending) {\n return;\n }\n\n pendingRef.current.delete(event.data.requestId);\n pending.onComplete?.(event.data.payload as TipsyChatCompletionResult);\n pending.resolve(event.data.payload as TipsyChatCompletionResult);\n }\n };\n\n window.addEventListener(\"message\", handleMessage);\n\n return () => {\n cancelled = true;\n readyRef.current = false;\n setIsReady(false);\n window.clearInterval(intervalId);\n window.removeEventListener(\"message\", handleMessage);\n rejectPending({\n code: \"NETWORK_ERROR\",\n message: \"Tipsy bridge was disconnected.\",\n });\n };\n }, []);\n\n const contextValue = useMemo<TipsyStudioContextValue>(() => {\n return {\n isReady,\n targetOrigin: resolvedTargetOrigin,\n call(method, payload, controller) {\n if (typeof window === \"undefined\" || window.parent === window) {\n return Promise.reject(\n new TipsyBridgeClientError({\n code: \"NETWORK_ERROR\",\n message: \"Tipsy bridge is only available inside an iframe.\",\n }),\n );\n }\n\n if (!readyRef.current) {\n return Promise.reject(\n new TipsyBridgeClientError({\n code: \"NETWORK_ERROR\",\n message: \"Tipsy bridge is not ready.\",\n }),\n );\n }\n\n const requestId = createRequestId();\n\n return new Promise<TipsyBridgeResponseMap[typeof method]>(\n (resolve, reject) => {\n const pending: PendingRequest = {\n method,\n resolve: (value) =>\n resolve(value as TipsyBridgeResponseMap[typeof method]),\n reject,\n onData:\n method === CHAT_COMPLETIONS_METHOD\n ? (controller as TipsyChatCompletionController | undefined)\n ?.onData\n : undefined,\n onComplete:\n method === CHAT_COMPLETIONS_METHOD\n ? (controller as TipsyChatCompletionController | undefined)\n ?.onComplete\n : undefined,\n };\n\n pendingRef.current.set(requestId, pending);\n\n const abortSignal = controller?.signal;\n const handleAbort = () => {\n if (!pendingRef.current.has(requestId)) {\n return;\n }\n\n pendingRef.current.delete(requestId);\n const abortMessage: TipsyAbortMessage = {\n protocol: TIPSY_BRIDGE_PROTOCOL,\n type: \"tipsy:abort\",\n requestId,\n };\n window.parent.postMessage(abortMessage, targetOriginRef.current);\n reject(\n new DOMException(\"The operation was aborted.\", \"AbortError\"),\n );\n };\n\n if (abortSignal?.aborted) {\n handleAbort();\n return;\n }\n\n abortSignal?.addEventListener(\"abort\", handleAbort, { once: true });\n\n const requestMessage: TipsyRequestMessage<typeof method> = {\n protocol: TIPSY_BRIDGE_PROTOCOL,\n type: \"tipsy:request\",\n requestId,\n method,\n payload,\n };\n\n window.parent.postMessage(requestMessage, targetOriginRef.current);\n },\n );\n },\n };\n }, [isReady, resolvedTargetOrigin]);\n\n return createElement(\n TipsyStudioContext.Provider,\n { value: contextValue },\n children,\n );\n}\n\nexport function useTipsyStudio() {\n const context = useContext(TipsyStudioContext);\n\n if (!context) {\n throw new Error(\"useTipsyStudio must be used within TipsyStudioProvider.\");\n }\n\n return context;\n}\n\nexport function useTipsyChat() {\n const studio = useTipsyStudio();\n\n return useMemo(() => {\n return {\n completions(\n input: TipsyChatCompletionInput,\n controller?: TipsyChatCompletionController,\n ) {\n return studio.call(CHAT_COMPLETIONS_METHOD, input, controller);\n },\n };\n }, [studio]);\n}\n"]}
1
+ {"version":3,"sources":["../src/react.ts"],"names":[],"mappings":";;;;AAsDA,IAAM,kBAAA,GAAqB,cAA8C,IAAI,CAAA;AAEtE,SAAS,mBAAA,CAAoB;AAAA,EAClC,QAAA;AAAA,EACA;AACF,CAAA,EAA6B;AAC3B,EAAA,MAAM,oBAAA,GAAuB,OAAA;AAAA,IAC3B,MAAM,sBAAsB,YAAY,CAAA;AAAA,IACxC,CAAC,YAAY;AA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,EAAG,CAAC,MAAA,EAAQ,OAAA,EAAS,oBAAoB,CAAC,CAAA;AAE1C,EAAA,OAAO,aAAA;AAAA,IACL,kBAAA,CAAmB,QAAA;AAAA,IACnB,EAAE,OAAO,YAAA,EAAa;AAAA,IACtB;AAAA,GACF;AACF;AAEO,SAAS,cAAA,GAAiB;AAC/B,EAAA,MAAM,OAAA,GAAU,WAAW,kBAAkB,CAAA;AAE7C,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,yDAAyD,CAAA;AAAA,EAC3E;AAEA,EAAA,OAAO,OAAA;AACT;AAEO,SAAS,YAAA,GAAe;AAC7B,EAAA,MAAM,SAAS,cAAA,EAAe;AAE9B,EAAA,OAAO,QAAQ,MAAM;AACnB,IAAA,OAAO;AAAA,MACL,WAAA,CACE,OACA,UAAA,EACA;AACA,QAAA,OAAO,MAAA,CAAO,IAAA,CAAK,uBAAA,EAAyB,KAAA,EAAO,UAAU,CAAA;AAAA,MAC/D;AAAA,KACF;AAAA,EACF,CAAA,EAAG,CAAC,MAAM,CAAC,CAAA;AACb","file":"react.js","sourcesContent":["import {\n createElement,\n createContext,\n type PropsWithChildren,\n useContext,\n useEffect,\n useMemo,\n useState,\n} from \"react\";\nimport {\n CHAT_COMPLETIONS_METHOD,\n TipsyBridgeClientError,\n type TipsyBridgeRequestMap,\n type TipsyBridgeResponseMap,\n type TipsyRequestController,\n type TipsyChatCompletionController,\n type TipsyChatCompletionInput,\n normalizeTargetOrigin,\n} from \"./bridge\";\nimport { createTipsyStudioClient, type TipsyStudioClient } from \"./client\";\nexport {\n CHAT_COMPLETIONS_METHOD,\n TIPSY_BRIDGE_PROTOCOL,\n TipsyBridgeClientError,\n type TipsyAbortMessage,\n type TipsyBridgeError,\n type TipsyBridgeMessage,\n type TipsyBridgeRequestMap,\n type TipsyBridgeResponseMap,\n type TipsyChatCompletionChunk,\n type TipsyChatCompletionController,\n type TipsyChatCompletionInput,\n type TipsyChatCompletionRawChunk,\n type TipsyChatCompletionResult,\n type TipsyChatMessage,\n} from \"./bridge\";\nexport { createTipsyStudioClient, type TipsyStudioClient, type TipsyStudioClientOptions } from \"./client\";\n\nexport type TipsyStudioProviderProps = PropsWithChildren<{\n targetOrigin?: string;\n}>;\n\ntype TipsyStudioContextValue = {\n isReady: boolean;\n targetOrigin: string;\n call: <M extends keyof TipsyBridgeRequestMap>(\n method: M,\n payload: TipsyBridgeRequestMap[M],\n controller?: M extends \"chat.completions\"\n ? TipsyChatCompletionController\n : TipsyRequestController,\n ) => Promise<TipsyBridgeResponseMap[M]>;\n};\n\nconst TipsyStudioContext = createContext<TipsyStudioContextValue | null>(null);\n\nexport function TipsyStudioProvider({\n children,\n targetOrigin,\n}: TipsyStudioProviderProps) {\n const resolvedTargetOrigin = useMemo(\n () => normalizeTargetOrigin(targetOrigin),\n [targetOrigin],\n );\n const [isReady, setIsReady] = useState(false);\n const [client, setClient] = useState<TipsyStudioClient | null>(null);\n\n useEffect(() => {\n const nextClient = createTipsyStudioClient({ targetOrigin });\n setClient(nextClient);\n setIsReady(nextClient.isReady());\n\n const unsubscribe = nextClient.subscribe((nextIsReady) => {\n setIsReady(nextIsReady);\n });\n\n return () => {\n unsubscribe();\n nextClient.destroy();\n setClient((currentClient) =>\n currentClient === nextClient ? null : currentClient,\n );\n setIsReady(false);\n };\n }, [targetOrigin]);\n\n const contextValue = useMemo<TipsyStudioContextValue>(() => {\n return {\n isReady,\n targetOrigin: resolvedTargetOrigin,\n call(method, payload, controller) {\n if (!client) {\n return Promise.reject(\n new TipsyBridgeClientError({\n code: \"NETWORK_ERROR\",\n message: \"Tipsy bridge is not ready.\",\n }),\n );\n }\n\n return client.call(method, payload, controller);\n },\n };\n }, [client, isReady, resolvedTargetOrigin]);\n\n return createElement(\n TipsyStudioContext.Provider,\n { value: contextValue },\n children,\n );\n}\n\nexport function useTipsyStudio() {\n const context = useContext(TipsyStudioContext);\n\n if (!context) {\n throw new Error(\"useTipsyStudio must be used within TipsyStudioProvider.\");\n }\n\n return context;\n}\n\nexport function useTipsyChat() {\n const studio = useTipsyStudio();\n\n return useMemo(() => {\n return {\n completions(\n input: TipsyChatCompletionInput,\n controller?: TipsyChatCompletionController,\n ) {\n return studio.call(CHAT_COMPLETIONS_METHOD, input, controller);\n },\n };\n }, [studio]);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tipsy-studio/sdk",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "React sdk for Tipsy Studio apps.",
5
5
  "license": "MIT",
6
6
  "type": "module",