@lessly/sdk-app 48.0.4 → 49.0.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.cts CHANGED
@@ -12,9 +12,61 @@ interface Binding {
12
12
  readOnly: boolean;
13
13
  params?: ParamSpec[];
14
14
  }
15
+ /**
16
+ * The streaming counterpart of `Binding`: the upgrade route of a ws-bound tool. No method (an
17
+ * upgrade is always a GET) and no `readOnly` (a socket is neither a read nor a write — the tool's
18
+ * own description defines its framing).
19
+ */
20
+ interface WsBinding {
21
+ path: string;
22
+ params?: ParamSpec[];
23
+ }
15
24
  type ActionBindings = Record<string, Binding>;
16
25
  type ResourceBindings = Record<string, ActionBindings>;
17
26
  type BindingsMap = Record<string, ResourceBindings>;
27
+ type WsActionBindings = Record<string, WsBinding>;
28
+ type WsResourceBindings = Record<string, WsActionBindings>;
29
+ type WsBindingsMap = Record<string, WsResourceBindings>;
30
+ /** Close information, as delivered to every `onClose` subscriber. */
31
+ interface StreamCloseInfo {
32
+ code: number;
33
+ reason: string;
34
+ wasClean: boolean;
35
+ }
36
+ /** Frame payload, exactly as the socket delivered it — never parsed by the SDK. */
37
+ type StreamData = string | ArrayBufferLike | ArrayBufferView | Blob;
38
+ /**
39
+ * The handle returned by `connectStream`. Deliberately small: no reconnection, no backoff, no
40
+ * heartbeat and no framing. A dropped connection surfaces once, through `onClose` with the close
41
+ * code; deciding whether and how to reconnect is the App's business, not the SDK's.
42
+ */
43
+ interface LesslyStream {
44
+ /** Queued until the socket is OPEN, then flushed in order. Ignored once the stream is closed. */
45
+ send(data: StreamData): void;
46
+ close(code?: number, reason?: string): void;
47
+ /** Delivers `MessageEvent.data` as-is. Returns an unsubscribe function. */
48
+ onMessage(cb: (data: StreamData) => void): () => void;
49
+ /** Returns an unsubscribe function. */
50
+ onClose(cb: (info: StreamCloseInfo) => void): () => void;
51
+ /** The underlying socket's readyState (0 CONNECTING, 1 OPEN, 2 CLOSING, 3 CLOSED). */
52
+ readonly readyState: number;
53
+ }
54
+ /**
55
+ * What a generated `<tool>Connect` factory needs from the client: the ability to open a stream
56
+ * for a binding. `createLesslyApp`'s client implements it (see `LesslyClientBase`).
57
+ */
58
+ interface LesslyStreamOpener {
59
+ openStream(binding: WsBinding, input?: Record<string, unknown>): LesslyStream;
60
+ }
61
+ /** The shape of `globalThis.WebSocket` this SDK relies on — injectable, like `fetch`. */
62
+ type WebSocketCtor = new (url: string) => WebSocketLike;
63
+ interface WebSocketLike {
64
+ readonly readyState: number;
65
+ send(data: StreamData): void;
66
+ close(code?: number, reason?: string): void;
67
+ addEventListener(type: string, cb: (ev: never) => void): void;
68
+ removeEventListener(type: string, cb: (ev: never) => void): void;
69
+ }
18
70
  interface LesslyAppOptions {
19
71
  /**
20
72
  * Absolute origin ("https://api.lessly.dev") or a path relative to the page
@@ -25,13 +77,30 @@ interface LesslyAppOptions {
25
77
  productId: string;
26
78
  getCsrfToken?: () => string | null | undefined;
27
79
  fetch?: typeof fetch;
80
+ /** Injectable WebSocket constructor for `connectStream`; defaults to `globalThis.WebSocket`. */
81
+ WebSocket?: WebSocketCtor;
28
82
  }
29
83
 
30
- interface LesslyClientBase {
84
+ interface LesslyClientBase extends LesslyStreamOpener {
31
85
  call(toolName: string, input?: Record<string, unknown>): Promise<unknown>;
32
86
  }
33
87
  declare function createLesslyApp(opts: LesslyAppOptions): GeneratedClient & LesslyClientBase;
34
88
 
89
+ /**
90
+ * Open the sanctioned streaming connection for a ws-bound tool.
91
+ *
92
+ * Session cookies ride the upgrade automatically, so this sends NOTHING extra: no CSRF token (an
93
+ * upgrade is not an HTTP mutation) and no product header — a browser WebSocket cannot set headers
94
+ * at all, and the gateway's upgrade path authenticates from the session alone.
95
+ *
96
+ * Params are placed exactly as for a GET request: `:token` segments are substituted into the
97
+ * path, everything else becomes a query parameter. A param declared `in: 'body'` has no meaning
98
+ * on an upgrade and is treated as a query parameter rather than silently dropped.
99
+ *
100
+ * There is no reconnection, backoff or heartbeat logic here on purpose (see `LesslyStream`).
101
+ */
102
+ declare function connectStream(opts: LesslyAppOptions, binding: WsBinding, input?: Record<string, unknown>): LesslyStream;
103
+
35
104
  declare class LesslyApiError extends Error {
36
105
  readonly status: number;
37
106
  readonly code: string | null;
@@ -39,4 +108,4 @@ declare class LesslyApiError extends Error {
39
108
  constructor(status: number, code: string | null, message: string, body: unknown);
40
109
  }
41
110
 
42
- export { type Binding, type BindingsMap, GeneratedClient, type HttpMethod, LesslyApiError, type LesslyAppOptions, type LesslyClientBase, type ParamIn, type ParamSpec, createLesslyApp };
111
+ export { type Binding, type BindingsMap, GeneratedClient, type HttpMethod, LesslyApiError, type LesslyAppOptions, type LesslyClientBase, type LesslyStream, type LesslyStreamOpener, type ParamIn, type ParamSpec, type StreamCloseInfo, type StreamData, type WebSocketCtor, type WebSocketLike, type WsBinding, type WsBindingsMap, connectStream, createLesslyApp };
package/dist/index.d.ts CHANGED
@@ -12,9 +12,61 @@ interface Binding {
12
12
  readOnly: boolean;
13
13
  params?: ParamSpec[];
14
14
  }
15
+ /**
16
+ * The streaming counterpart of `Binding`: the upgrade route of a ws-bound tool. No method (an
17
+ * upgrade is always a GET) and no `readOnly` (a socket is neither a read nor a write — the tool's
18
+ * own description defines its framing).
19
+ */
20
+ interface WsBinding {
21
+ path: string;
22
+ params?: ParamSpec[];
23
+ }
15
24
  type ActionBindings = Record<string, Binding>;
16
25
  type ResourceBindings = Record<string, ActionBindings>;
17
26
  type BindingsMap = Record<string, ResourceBindings>;
27
+ type WsActionBindings = Record<string, WsBinding>;
28
+ type WsResourceBindings = Record<string, WsActionBindings>;
29
+ type WsBindingsMap = Record<string, WsResourceBindings>;
30
+ /** Close information, as delivered to every `onClose` subscriber. */
31
+ interface StreamCloseInfo {
32
+ code: number;
33
+ reason: string;
34
+ wasClean: boolean;
35
+ }
36
+ /** Frame payload, exactly as the socket delivered it — never parsed by the SDK. */
37
+ type StreamData = string | ArrayBufferLike | ArrayBufferView | Blob;
38
+ /**
39
+ * The handle returned by `connectStream`. Deliberately small: no reconnection, no backoff, no
40
+ * heartbeat and no framing. A dropped connection surfaces once, through `onClose` with the close
41
+ * code; deciding whether and how to reconnect is the App's business, not the SDK's.
42
+ */
43
+ interface LesslyStream {
44
+ /** Queued until the socket is OPEN, then flushed in order. Ignored once the stream is closed. */
45
+ send(data: StreamData): void;
46
+ close(code?: number, reason?: string): void;
47
+ /** Delivers `MessageEvent.data` as-is. Returns an unsubscribe function. */
48
+ onMessage(cb: (data: StreamData) => void): () => void;
49
+ /** Returns an unsubscribe function. */
50
+ onClose(cb: (info: StreamCloseInfo) => void): () => void;
51
+ /** The underlying socket's readyState (0 CONNECTING, 1 OPEN, 2 CLOSING, 3 CLOSED). */
52
+ readonly readyState: number;
53
+ }
54
+ /**
55
+ * What a generated `<tool>Connect` factory needs from the client: the ability to open a stream
56
+ * for a binding. `createLesslyApp`'s client implements it (see `LesslyClientBase`).
57
+ */
58
+ interface LesslyStreamOpener {
59
+ openStream(binding: WsBinding, input?: Record<string, unknown>): LesslyStream;
60
+ }
61
+ /** The shape of `globalThis.WebSocket` this SDK relies on — injectable, like `fetch`. */
62
+ type WebSocketCtor = new (url: string) => WebSocketLike;
63
+ interface WebSocketLike {
64
+ readonly readyState: number;
65
+ send(data: StreamData): void;
66
+ close(code?: number, reason?: string): void;
67
+ addEventListener(type: string, cb: (ev: never) => void): void;
68
+ removeEventListener(type: string, cb: (ev: never) => void): void;
69
+ }
18
70
  interface LesslyAppOptions {
19
71
  /**
20
72
  * Absolute origin ("https://api.lessly.dev") or a path relative to the page
@@ -25,13 +77,30 @@ interface LesslyAppOptions {
25
77
  productId: string;
26
78
  getCsrfToken?: () => string | null | undefined;
27
79
  fetch?: typeof fetch;
80
+ /** Injectable WebSocket constructor for `connectStream`; defaults to `globalThis.WebSocket`. */
81
+ WebSocket?: WebSocketCtor;
28
82
  }
29
83
 
30
- interface LesslyClientBase {
84
+ interface LesslyClientBase extends LesslyStreamOpener {
31
85
  call(toolName: string, input?: Record<string, unknown>): Promise<unknown>;
32
86
  }
33
87
  declare function createLesslyApp(opts: LesslyAppOptions): GeneratedClient & LesslyClientBase;
34
88
 
89
+ /**
90
+ * Open the sanctioned streaming connection for a ws-bound tool.
91
+ *
92
+ * Session cookies ride the upgrade automatically, so this sends NOTHING extra: no CSRF token (an
93
+ * upgrade is not an HTTP mutation) and no product header — a browser WebSocket cannot set headers
94
+ * at all, and the gateway's upgrade path authenticates from the session alone.
95
+ *
96
+ * Params are placed exactly as for a GET request: `:token` segments are substituted into the
97
+ * path, everything else becomes a query parameter. A param declared `in: 'body'` has no meaning
98
+ * on an upgrade and is treated as a query parameter rather than silently dropped.
99
+ *
100
+ * There is no reconnection, backoff or heartbeat logic here on purpose (see `LesslyStream`).
101
+ */
102
+ declare function connectStream(opts: LesslyAppOptions, binding: WsBinding, input?: Record<string, unknown>): LesslyStream;
103
+
35
104
  declare class LesslyApiError extends Error {
36
105
  readonly status: number;
37
106
  readonly code: string | null;
@@ -39,4 +108,4 @@ declare class LesslyApiError extends Error {
39
108
  constructor(status: number, code: string | null, message: string, body: unknown);
40
109
  }
41
110
 
42
- export { type Binding, type BindingsMap, GeneratedClient, type HttpMethod, LesslyApiError, type LesslyAppOptions, type LesslyClientBase, type ParamIn, type ParamSpec, createLesslyApp };
111
+ export { type Binding, type BindingsMap, GeneratedClient, type HttpMethod, LesslyApiError, type LesslyAppOptions, type LesslyClientBase, type LesslyStream, type LesslyStreamOpener, type ParamIn, type ParamSpec, type StreamCloseInfo, type StreamData, type WebSocketCtor, type WebSocketLike, type WsBinding, type WsBindingsMap, connectStream, createLesslyApp };
package/dist/index.js CHANGED
@@ -124,6 +124,65 @@ async function executeRequest(opts, binding, input) {
124
124
  return parsed;
125
125
  }
126
126
 
127
+ // src/runtime/connectStream.ts
128
+ function toWsUrl(url) {
129
+ if (url.startsWith("https:")) return `wss:${url.slice("https:".length)}`;
130
+ if (url.startsWith("http:")) return `ws:${url.slice("http:".length)}`;
131
+ return url;
132
+ }
133
+ function connectStream(opts, binding, input) {
134
+ const restLike = { method: "GET", path: binding.path, ...binding.params ? { params: binding.params } : {} };
135
+ const { path, query, body } = resolveRequest(restLike, input);
136
+ const url = toWsUrl(buildUrl(opts.baseUrl, path, { ...query, ...body ?? {} }));
137
+ const Ctor = opts.WebSocket ?? globalThis.WebSocket;
138
+ if (!Ctor) {
139
+ throw new Error(
140
+ `@lessly/sdk-app: WebSocket is unavailable in this environment, so the stream to "${binding.path}" cannot be opened. Streaming requires a browser; pass options.WebSocket to supply an implementation.`
141
+ );
142
+ }
143
+ const socket = new Ctor(url);
144
+ const messageSubs = /* @__PURE__ */ new Set();
145
+ const closeSubs = /* @__PURE__ */ new Set();
146
+ let pending = [];
147
+ socket.addEventListener("open", () => {
148
+ const queued = pending ?? [];
149
+ pending = null;
150
+ for (const frame of queued) socket.send(frame);
151
+ });
152
+ socket.addEventListener("message", (ev) => {
153
+ for (const cb of [...messageSubs]) cb(ev.data);
154
+ });
155
+ socket.addEventListener("close", (ev) => {
156
+ pending = null;
157
+ const info = { code: ev.code ?? 1006, reason: ev.reason ?? "", wasClean: ev.wasClean ?? false };
158
+ for (const cb of [...closeSubs]) cb(info);
159
+ });
160
+ return {
161
+ send(data) {
162
+ if (pending !== null) {
163
+ pending.push(data);
164
+ return;
165
+ }
166
+ if (socket.readyState === 1) socket.send(data);
167
+ },
168
+ close(code, reason) {
169
+ pending = null;
170
+ socket.close(code, reason);
171
+ },
172
+ onMessage(cb) {
173
+ messageSubs.add(cb);
174
+ return () => messageSubs.delete(cb);
175
+ },
176
+ onClose(cb) {
177
+ closeSubs.add(cb);
178
+ return () => closeSubs.delete(cb);
179
+ },
180
+ get readyState() {
181
+ return socket.readyState;
182
+ }
183
+ };
184
+ }
185
+
127
186
  // src/runtime/name.ts
128
187
  var ROOT_RESOURCE = "$root";
129
188
  function splitToolName(name) {
@@ -8179,6 +8238,13 @@ function createLesslyApp(opts) {
8179
8238
  const map = bindings;
8180
8239
  const nsCache = {};
8181
8240
  const base = {
8241
+ // The whole streaming surface of the client. Streams are NOT reachable through the request
8242
+ // proxy: a socket is not a promise-returning call, and hanging one off `sdk.<ns>.<res>.<act>`
8243
+ // would make two very different lifetimes look identical at the call site. The generated
8244
+ // `<tool>Connect` factories carry the binding and call this.
8245
+ openStream(binding, input) {
8246
+ return connectStream(opts, binding, input);
8247
+ },
8182
8248
  call(toolName, input) {
8183
8249
  let split;
8184
8250
  try {
@@ -8203,6 +8269,6 @@ function createLesslyApp(opts) {
8203
8269
  });
8204
8270
  }
8205
8271
 
8206
- export { LesslyApiError, createLesslyApp };
8272
+ export { LesslyApiError, connectStream, createLesslyApp };
8207
8273
  //# sourceMappingURL=index.js.map
8208
8274
  //# sourceMappingURL=index.js.map