@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.cjs CHANGED
@@ -126,6 +126,65 @@ async function executeRequest(opts, binding, input) {
126
126
  return parsed;
127
127
  }
128
128
 
129
+ // src/runtime/connectStream.ts
130
+ function toWsUrl(url) {
131
+ if (url.startsWith("https:")) return `wss:${url.slice("https:".length)}`;
132
+ if (url.startsWith("http:")) return `ws:${url.slice("http:".length)}`;
133
+ return url;
134
+ }
135
+ function connectStream(opts, binding, input) {
136
+ const restLike = { method: "GET", path: binding.path, ...binding.params ? { params: binding.params } : {} };
137
+ const { path, query, body } = resolveRequest(restLike, input);
138
+ const url = toWsUrl(buildUrl(opts.baseUrl, path, { ...query, ...body ?? {} }));
139
+ const Ctor = opts.WebSocket ?? globalThis.WebSocket;
140
+ if (!Ctor) {
141
+ throw new Error(
142
+ `@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.`
143
+ );
144
+ }
145
+ const socket = new Ctor(url);
146
+ const messageSubs = /* @__PURE__ */ new Set();
147
+ const closeSubs = /* @__PURE__ */ new Set();
148
+ let pending = [];
149
+ socket.addEventListener("open", () => {
150
+ const queued = pending ?? [];
151
+ pending = null;
152
+ for (const frame of queued) socket.send(frame);
153
+ });
154
+ socket.addEventListener("message", (ev) => {
155
+ for (const cb of [...messageSubs]) cb(ev.data);
156
+ });
157
+ socket.addEventListener("close", (ev) => {
158
+ pending = null;
159
+ const info = { code: ev.code ?? 1006, reason: ev.reason ?? "", wasClean: ev.wasClean ?? false };
160
+ for (const cb of [...closeSubs]) cb(info);
161
+ });
162
+ return {
163
+ send(data) {
164
+ if (pending !== null) {
165
+ pending.push(data);
166
+ return;
167
+ }
168
+ if (socket.readyState === 1) socket.send(data);
169
+ },
170
+ close(code, reason) {
171
+ pending = null;
172
+ socket.close(code, reason);
173
+ },
174
+ onMessage(cb) {
175
+ messageSubs.add(cb);
176
+ return () => messageSubs.delete(cb);
177
+ },
178
+ onClose(cb) {
179
+ closeSubs.add(cb);
180
+ return () => closeSubs.delete(cb);
181
+ },
182
+ get readyState() {
183
+ return socket.readyState;
184
+ }
185
+ };
186
+ }
187
+
129
188
  // src/runtime/name.ts
130
189
  var ROOT_RESOURCE = "$root";
131
190
  function splitToolName(name) {
@@ -8181,6 +8240,13 @@ function createLesslyApp(opts) {
8181
8240
  const map = bindings;
8182
8241
  const nsCache = {};
8183
8242
  const base = {
8243
+ // The whole streaming surface of the client. Streams are NOT reachable through the request
8244
+ // proxy: a socket is not a promise-returning call, and hanging one off `sdk.<ns>.<res>.<act>`
8245
+ // would make two very different lifetimes look identical at the call site. The generated
8246
+ // `<tool>Connect` factories carry the binding and call this.
8247
+ openStream(binding, input) {
8248
+ return connectStream(opts, binding, input);
8249
+ },
8184
8250
  call(toolName, input) {
8185
8251
  let split;
8186
8252
  try {
@@ -8206,6 +8272,7 @@ function createLesslyApp(opts) {
8206
8272
  }
8207
8273
 
8208
8274
  exports.LesslyApiError = LesslyApiError;
8275
+ exports.connectStream = connectStream;
8209
8276
  exports.createLesslyApp = createLesslyApp;
8210
8277
  //# sourceMappingURL=index.cjs.map
8211
8278
  //# sourceMappingURL=index.cjs.map