@proveanything/smartlinks 1.8.1 → 1.8.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.
@@ -0,0 +1,308 @@
1
+ # Iframe Streaming Parent Changes
2
+
3
+ This note describes the parent-side changes needed to support AI streaming when an embedded SmartLinks app is running in iframe proxy mode.
4
+
5
+ If you are using the SDK `IframeResponder` directly, this is already implemented in the SDK changes. You only need this document if your parent application has its own iframe proxy handler and does not rely on `IframeResponder`.
6
+
7
+ ## Goal
8
+
9
+ Keep the existing architecture:
10
+
11
+ - local mode: child calls API directly
12
+ - iframe proxy mode: child never owns auth state and streams through the parent
13
+
14
+ This keeps user/session authority in the parent while making AI streaming behave like the rest of the SDK transport.
15
+
16
+ ## What changed
17
+
18
+ Previously, proxy mode only supported one-shot request/response messages:
19
+
20
+ - `_smartlinksProxyRequest`
21
+ - `_smartlinksProxyResponse`
22
+
23
+ Streaming now adds a second protocol for long-lived responses:
24
+
25
+ - `_smartlinksProxyStreamRequest`
26
+ - `_smartlinksProxyStream`
27
+ - `_smartlinksProxyStreamAbort`
28
+
29
+ ## New parent message handling
30
+
31
+ ### 1. Listen for stream requests
32
+
33
+ The iframe child may now send this message:
34
+
35
+ ```ts
36
+ {
37
+ _smartlinksProxyStreamRequest: true,
38
+ id: string,
39
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
40
+ path: string,
41
+ body?: any,
42
+ headers?: Record<string, string>
43
+ }
44
+ ```
45
+
46
+ Parent behavior:
47
+
48
+ - treat this like a proxied API request
49
+ - build the real API URL from your configured base URL plus `path`
50
+ - send the request using the parent's current auth/session context
51
+ - expect an SSE / streaming response body
52
+ - keep the request open until the stream ends or is aborted
53
+
54
+ ### 2. Forward stream lifecycle messages back to the child
55
+
56
+ The parent should send messages back to the iframe using this envelope:
57
+
58
+ ```ts
59
+ {
60
+ _smartlinksProxyStream: true,
61
+ id: string,
62
+ phase: 'open' | 'event' | 'end' | 'error',
63
+ data?: any,
64
+ error?: string,
65
+ status?: number
66
+ }
67
+ ```
68
+
69
+ Phases:
70
+
71
+ - `open`
72
+ - optional but recommended
73
+ - indicates the upstream streaming request was accepted and a body exists
74
+ - `event`
75
+ - contains one parsed JSON event from an SSE `data:` frame
76
+ - send one message per logical event payload
77
+ - `end`
78
+ - sent once when the stream finishes normally
79
+ - `error`
80
+ - sent if the upstream request fails before or during streaming
81
+
82
+ ### 3. Support abort from the child
83
+
84
+ The child may stop reading early and send:
85
+
86
+ ```ts
87
+ {
88
+ _smartlinksProxyStreamAbort: true,
89
+ id: string
90
+ }
91
+ ```
92
+
93
+ Parent behavior:
94
+
95
+ - look up the active stream by `id`
96
+ - abort the underlying fetch / reader
97
+ - clean up any local state for that stream
98
+ - do not keep streaming after abort
99
+
100
+ ## SSE forwarding rules
101
+
102
+ The upstream AI endpoints return SSE-like frames. The parent should:
103
+
104
+ - read the response body as a stream
105
+ - buffer text until line boundaries
106
+ - collect `data:` lines for a single event
107
+ - join multi-line `data:` payloads with `\n`
108
+ - ignore blank events
109
+ - stop on `data: [DONE]`
110
+ - JSON-parse each event payload
111
+ - forward parsed payloads to the iframe as `_smartlinksProxyStream` with `phase: 'event'`
112
+
113
+ Minimal parsing behavior:
114
+
115
+ 1. accumulate bytes into text
116
+ 2. split on `\r?\n`
117
+ 3. collect each `data:` line
118
+ 4. on blank line, finalize the event
119
+ 5. if payload is `[DONE]`, finish
120
+ 6. otherwise `JSON.parse(payload)` and forward
121
+
122
+ ## Auth and session expectations
123
+
124
+ The parent remains the source of truth for auth.
125
+
126
+ That means the parent stream handler should:
127
+
128
+ - use the same auth headers/token source as normal proxied requests
129
+ - not require the iframe to know the bearer token or API key
130
+ - naturally pick up the current logged-in user when the stream starts
131
+ - cancel active streams if your app invalidates session state on logout or account switch
132
+
133
+ In practice, the stream request should use the same header-building logic as your normal parent proxy transport.
134
+
135
+ ## Error handling expectations
136
+
137
+ If the upstream fetch returns a non-2xx status:
138
+
139
+ - try to read the JSON error body
140
+ - derive a useful message
141
+ - send one `_smartlinksProxyStream` message with `phase: 'error'`
142
+ - include `status` when available
143
+ - do not send `end` afterward
144
+
145
+ If the stream body is missing unexpectedly:
146
+
147
+ - send `phase: 'error'`
148
+
149
+ If JSON parsing fails for a single event chunk:
150
+
151
+ - safest behavior is to ignore that malformed chunk and continue
152
+
153
+ ## State the parent should keep
154
+
155
+ Track active streams in a map keyed by `id`:
156
+
157
+ ```ts
158
+ Map<string, AbortController>
159
+ ```
160
+
161
+ Recommended cleanup points:
162
+
163
+ - on normal stream end
164
+ - on error
165
+ - on child abort
166
+ - on iframe detach/unmount
167
+ - on parent auth reset/logout if you want all in-flight streams cancelled immediately
168
+
169
+ ## Parent implementation outline
170
+
171
+ ```ts
172
+ const activeStreams = new Map<string, AbortController>()
173
+
174
+ window.addEventListener('message', async (event) => {
175
+ const msg = event.data
176
+
177
+ if (msg?._smartlinksProxyStreamAbort && msg.id) {
178
+ activeStreams.get(msg.id)?.abort()
179
+ activeStreams.delete(msg.id)
180
+ return
181
+ }
182
+
183
+ if (msg?._smartlinksProxyStreamRequest && msg.id) {
184
+ const controller = new AbortController()
185
+ activeStreams.set(msg.id, controller)
186
+
187
+ try {
188
+ const response = await fetch(buildUrl(msg.path), {
189
+ method: msg.method,
190
+ headers: msg.headers,
191
+ body: msg.body ? JSON.stringify(msg.body) : undefined,
192
+ signal: controller.signal,
193
+ })
194
+
195
+ if (!response.ok || !response.body) {
196
+ postError(...)
197
+ return
198
+ }
199
+
200
+ postOpen(...)
201
+ await forwardSse(response.body, parsed => postEvent(...parsed))
202
+ postEnd(...)
203
+ } catch (err) {
204
+ if (err?.name !== 'AbortError') postError(...)
205
+ } finally {
206
+ activeStreams.delete(msg.id)
207
+ }
208
+ }
209
+ })
210
+ ```
211
+
212
+ ## Exact protocol summary
213
+
214
+ ### Child → parent
215
+
216
+ Standard stream request:
217
+
218
+ ```ts
219
+ {
220
+ _smartlinksProxyStreamRequest: true,
221
+ id,
222
+ method,
223
+ path,
224
+ body,
225
+ headers
226
+ }
227
+ ```
228
+
229
+ Abort request:
230
+
231
+ ```ts
232
+ {
233
+ _smartlinksProxyStreamAbort: true,
234
+ id
235
+ }
236
+ ```
237
+
238
+ ### Parent → child
239
+
240
+ Open:
241
+
242
+ ```ts
243
+ {
244
+ _smartlinksProxyStream: true,
245
+ id,
246
+ phase: 'open'
247
+ }
248
+ ```
249
+
250
+ Event:
251
+
252
+ ```ts
253
+ {
254
+ _smartlinksProxyStream: true,
255
+ id,
256
+ phase: 'event',
257
+ data: parsedJsonEvent
258
+ }
259
+ ```
260
+
261
+ End:
262
+
263
+ ```ts
264
+ {
265
+ _smartlinksProxyStream: true,
266
+ id,
267
+ phase: 'end'
268
+ }
269
+ ```
270
+
271
+ Error:
272
+
273
+ ```ts
274
+ {
275
+ _smartlinksProxyStream: true,
276
+ id,
277
+ phase: 'error',
278
+ error: 'message',
279
+ status?: number
280
+ }
281
+ ```
282
+
283
+ ## What does not change
284
+
285
+ These parts of the parent iframe integration stay the same:
286
+
287
+ - normal `_smartlinksProxyRequest` request/response flow
288
+ - upload proxy flow
289
+ - auth login/logout postMessage handling
290
+ - route/deep-link handling
291
+ - resize handling
292
+
293
+ This is an additive protocol, not a replacement.
294
+
295
+ ## Current SDK reference
296
+
297
+ The SDK implementation lives in:
298
+
299
+ - [src/http.ts](src/http.ts)
300
+ - [src/iframeResponder.ts](src/iframeResponder.ts)
301
+ - [src/types/iframeResponder.ts](src/types/iframeResponder.ts)
302
+ - [src/api/ai.ts](src/api/ai.ts)
303
+
304
+ ## Practical recommendation
305
+
306
+ If your parent already uses `IframeResponder`, prefer upgrading to the SDK version with these changes instead of re-implementing the protocol manually.
307
+
308
+ If your parent has a custom iframe bridge, implement exactly the three new message types above and reuse your existing auth/header logic from normal proxied requests.
package/openapi.yaml CHANGED
@@ -15478,6 +15478,69 @@ components:
15478
15478
  required:
15479
15479
  - _smartlinksProxyResponse
15480
15480
  - id
15481
+ ProxyStreamRequest:
15482
+ type: object
15483
+ properties:
15484
+ _smartlinksProxyStreamRequest:
15485
+ type: object
15486
+ additionalProperties: true
15487
+ id:
15488
+ type: string
15489
+ method:
15490
+ type: string
15491
+ enum:
15492
+ - GET
15493
+ - POST
15494
+ - PUT
15495
+ - PATCH
15496
+ - DELETE
15497
+ path:
15498
+ type: string
15499
+ body: {}
15500
+ headers:
15501
+ type: object
15502
+ additionalProperties:
15503
+ type: string
15504
+ required:
15505
+ - _smartlinksProxyStreamRequest
15506
+ - id
15507
+ - method
15508
+ - path
15509
+ ProxyStreamAbortMessage:
15510
+ type: object
15511
+ properties:
15512
+ _smartlinksProxyStreamAbort:
15513
+ type: object
15514
+ additionalProperties: true
15515
+ id:
15516
+ type: string
15517
+ required:
15518
+ - _smartlinksProxyStreamAbort
15519
+ - id
15520
+ ProxyStreamMessage:
15521
+ type: object
15522
+ properties:
15523
+ _smartlinksProxyStream:
15524
+ type: object
15525
+ additionalProperties: true
15526
+ id:
15527
+ type: string
15528
+ phase:
15529
+ type: string
15530
+ enum:
15531
+ - open
15532
+ - event
15533
+ - end
15534
+ - error
15535
+ data: {}
15536
+ error:
15537
+ type: string
15538
+ status:
15539
+ type: number
15540
+ required:
15541
+ - _smartlinksProxyStream
15542
+ - id
15543
+ - phase
15481
15544
  UploadStartMessage:
15482
15545
  type: object
15483
15546
  properties:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proveanything/smartlinks",
3
- "version": "1.8.1",
3
+ "version": "1.8.3",
4
4
  "description": "Official JavaScript/TypeScript SDK for the Smartlinks API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",