@onkernel/sdk 0.1.0-alpha.12 → 0.1.0-alpha.13

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.
Files changed (94) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/client.d.mts +2 -2
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +2 -2
  5. package/client.d.ts.map +1 -1
  6. package/client.js.map +1 -1
  7. package/client.mjs.map +1 -1
  8. package/core/app-framework.d.mts +1 -1
  9. package/core/app-framework.d.mts.map +1 -1
  10. package/core/app-framework.d.ts +1 -1
  11. package/core/app-framework.d.ts.map +1 -1
  12. package/core/streaming.d.mts +31 -0
  13. package/core/streaming.d.mts.map +1 -0
  14. package/core/streaming.d.ts +31 -0
  15. package/core/streaming.d.ts.map +1 -0
  16. package/core/streaming.js +257 -0
  17. package/core/streaming.js.map +1 -0
  18. package/core/streaming.mjs +252 -0
  19. package/core/streaming.mjs.map +1 -0
  20. package/internal/decoders/line.d.mts +17 -0
  21. package/internal/decoders/line.d.mts.map +1 -0
  22. package/internal/decoders/line.d.ts +17 -0
  23. package/internal/decoders/line.d.ts.map +1 -0
  24. package/internal/decoders/line.js +113 -0
  25. package/internal/decoders/line.js.map +1 -0
  26. package/internal/decoders/line.mjs +108 -0
  27. package/internal/decoders/line.mjs.map +1 -0
  28. package/internal/parse.d.mts.map +1 -1
  29. package/internal/parse.d.ts.map +1 -1
  30. package/internal/parse.js +10 -0
  31. package/internal/parse.js.map +1 -1
  32. package/internal/parse.mjs +10 -0
  33. package/internal/parse.mjs.map +1 -1
  34. package/internal/request-options.d.mts +2 -0
  35. package/internal/request-options.d.mts.map +1 -1
  36. package/internal/request-options.d.ts +2 -0
  37. package/internal/request-options.d.ts.map +1 -1
  38. package/internal/request-options.js.map +1 -1
  39. package/internal/request-options.mjs.map +1 -1
  40. package/internal/tslib.js +17 -17
  41. package/package.json +11 -1
  42. package/resources/apps/apps.d.mts +53 -2
  43. package/resources/apps/apps.d.mts.map +1 -1
  44. package/resources/apps/apps.d.ts +53 -2
  45. package/resources/apps/apps.d.ts.map +1 -1
  46. package/resources/apps/apps.js +12 -0
  47. package/resources/apps/apps.js.map +1 -1
  48. package/resources/apps/apps.mjs +13 -1
  49. package/resources/apps/apps.mjs.map +1 -1
  50. package/resources/apps/deployments.d.mts +70 -1
  51. package/resources/apps/deployments.d.mts.map +1 -1
  52. package/resources/apps/deployments.d.ts +70 -1
  53. package/resources/apps/deployments.d.ts.map +1 -1
  54. package/resources/apps/deployments.js +19 -0
  55. package/resources/apps/deployments.js.map +1 -1
  56. package/resources/apps/deployments.mjs +19 -0
  57. package/resources/apps/deployments.mjs.map +1 -1
  58. package/resources/apps/index.d.mts +2 -2
  59. package/resources/apps/index.d.mts.map +1 -1
  60. package/resources/apps/index.d.ts +2 -2
  61. package/resources/apps/index.d.ts.map +1 -1
  62. package/resources/apps/index.js.map +1 -1
  63. package/resources/apps/index.mjs +1 -1
  64. package/resources/apps/index.mjs.map +1 -1
  65. package/resources/index.d.mts +1 -1
  66. package/resources/index.d.mts.map +1 -1
  67. package/resources/index.d.ts +1 -1
  68. package/resources/index.d.ts.map +1 -1
  69. package/resources/index.js.map +1 -1
  70. package/resources/index.mjs.map +1 -1
  71. package/src/client.ts +2 -2
  72. package/src/core/app-framework.ts +1 -1
  73. package/src/core/streaming.ts +301 -0
  74. package/src/internal/decoders/line.ts +135 -0
  75. package/src/internal/parse.ts +14 -0
  76. package/src/internal/request-options.ts +2 -0
  77. package/src/resources/apps/apps.ts +70 -1
  78. package/src/resources/apps/deployments.ts +93 -0
  79. package/src/resources/apps/index.ts +7 -2
  80. package/src/resources/index.ts +1 -1
  81. package/src/streaming.ts +2 -0
  82. package/src/version.ts +1 -1
  83. package/streaming.d.mts +2 -0
  84. package/streaming.d.mts.map +1 -0
  85. package/streaming.d.ts +2 -0
  86. package/streaming.d.ts.map +1 -0
  87. package/streaming.js +6 -0
  88. package/streaming.js.map +1 -0
  89. package/streaming.mjs +2 -0
  90. package/streaming.mjs.map +1 -0
  91. package/version.d.mts +1 -1
  92. package/version.d.ts +1 -1
  93. package/version.js +1 -1
  94. package/version.mjs +1 -1
@@ -1,4 +1,4 @@
1
- export { Apps } from "./apps.mjs";
2
- export { Deployments, type DeploymentCreateResponse, type DeploymentCreateParams } from "./deployments.mjs";
1
+ export { Apps, type AppListResponse, type AppListParams } from "./apps.mjs";
2
+ export { Deployments, type DeploymentCreateResponse, type DeploymentFollowResponse, type DeploymentCreateParams, } from "./deployments.mjs";
3
3
  export { Invocations, type InvocationCreateResponse, type InvocationRetrieveResponse, type InvocationCreateParams, } from "./invocations.mjs";
4
4
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE;OACR,EAAE,WAAW,EAAE,KAAK,wBAAwB,EAAE,KAAK,sBAAsB,EAAE;OAC3E,EACL,WAAW,EACX,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,KAAK,sBAAsB,GAC5B"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE;OAClD,EACL,WAAW,EACX,KAAK,wBAAwB,EAC7B,KAAK,wBAAwB,EAC7B,KAAK,sBAAsB,GAC5B;OACM,EACL,WAAW,EACX,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,KAAK,sBAAsB,GAC5B"}
@@ -1,4 +1,4 @@
1
- export { Apps } from "./apps.js";
2
- export { Deployments, type DeploymentCreateResponse, type DeploymentCreateParams } from "./deployments.js";
1
+ export { Apps, type AppListResponse, type AppListParams } from "./apps.js";
2
+ export { Deployments, type DeploymentCreateResponse, type DeploymentFollowResponse, type DeploymentCreateParams, } from "./deployments.js";
3
3
  export { Invocations, type InvocationCreateResponse, type InvocationRetrieveResponse, type InvocationCreateParams, } from "./invocations.js";
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE;OACR,EAAE,WAAW,EAAE,KAAK,wBAAwB,EAAE,KAAK,sBAAsB,EAAE;OAC3E,EACL,WAAW,EACX,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,KAAK,sBAAsB,GAC5B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE;OAClD,EACL,WAAW,EACX,KAAK,wBAAwB,EAC7B,KAAK,wBAAwB,EAC7B,KAAK,sBAAsB,GAC5B;OACM,EACL,WAAW,EACX,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,KAAK,sBAAsB,GAC5B"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kCAA8B;AAArB,4FAAA,IAAI,OAAA;AACb,gDAAwG;AAA/F,0GAAA,WAAW,OAAA;AACpB,gDAKuB;AAJrB,0GAAA,WAAW,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kCAAwE;AAA/D,4FAAA,IAAI,OAAA;AACb,gDAKuB;AAJrB,0GAAA,WAAW,OAAA;AAKb,gDAKuB;AAJrB,0GAAA,WAAW,OAAA"}
@@ -1,5 +1,5 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
  export { Apps } from "./apps.mjs";
3
- export { Deployments } from "./deployments.mjs";
3
+ export { Deployments, } from "./deployments.mjs";
4
4
  export { Invocations, } from "./invocations.mjs";
5
5
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,IAAI,EAAE;OACR,EAAE,WAAW,EAA8D;OAC3E,EACL,WAAW,GAIZ"}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/resources/apps/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,IAAI,EAA4C;OAClD,EACL,WAAW,GAIZ;OACM,EACL,WAAW,GAIZ"}
@@ -1,3 +1,3 @@
1
- export { Apps } from "./apps/apps.mjs";
1
+ export { Apps, type AppListResponse, type AppListParams } from "./apps/apps.mjs";
2
2
  export { Browsers, type BrowserCreateResponse, type BrowserRetrieveResponse, type BrowserCreateParams, } from "./browsers.mjs";
3
3
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE;OACR,EACL,QAAQ,EACR,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,GACzB"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE;OAClD,EACL,QAAQ,EACR,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,GACzB"}
@@ -1,3 +1,3 @@
1
- export { Apps } from "./apps/apps.js";
1
+ export { Apps, type AppListResponse, type AppListParams } from "./apps/apps.js";
2
2
  export { Browsers, type BrowserCreateResponse, type BrowserRetrieveResponse, type BrowserCreateParams, } from "./browsers.js";
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE;OACR,EACL,QAAQ,EACR,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,GACzB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"OAEO,EAAE,IAAI,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE;OAClD,EACL,QAAQ,EACR,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,GACzB"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,uCAAmC;AAA1B,4FAAA,IAAI,OAAA;AACb,0CAKoB;AAJlB,oGAAA,QAAQ,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,uCAA6E;AAApE,4FAAA,IAAI,OAAA;AACb,0CAKoB;AAJlB,oGAAA,QAAQ,OAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,IAAI,EAAE;OACR,EACL,QAAQ,GAIT"}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,IAAI,EAA4C;OAClD,EACL,QAAQ,GAIT"}
package/src/client.ts CHANGED
@@ -30,7 +30,7 @@ import { readEnv } from './internal/utils/env';
30
30
  import { formatRequestDetails, loggerFor } from './internal/utils/log';
31
31
  import { isEmptyObj } from './internal/utils/values';
32
32
  import { KernelApp } from './core/app-framework';
33
- import { Apps } from './resources/apps/apps';
33
+ import { AppListParams, AppListResponse, Apps } from './resources/apps/apps';
34
34
 
35
35
  const environments = {
36
36
  production: 'https://api.onkernel.com/',
@@ -740,7 +740,7 @@ Kernel.Browsers = Browsers;
740
740
  export declare namespace Kernel {
741
741
  export type RequestOptions = Opts.RequestOptions;
742
742
 
743
- export { Apps as Apps };
743
+ export { Apps as Apps, type AppListResponse as AppListResponse, type AppListParams as AppListParams };
744
744
 
745
745
  export {
746
746
  Browsers as Browsers,
@@ -1,5 +1,5 @@
1
1
  export interface KernelContext {
2
- invocationId: string;
2
+ invocation_id: string;
3
3
  }
4
4
 
5
5
  export interface KernelAction {
@@ -0,0 +1,301 @@
1
+ import { KernelError } from './error';
2
+ import { type ReadableStream } from '../internal/shim-types';
3
+ import { makeReadableStream } from '../internal/shims';
4
+ import { findDoubleNewlineIndex, LineDecoder } from '../internal/decoders/line';
5
+ import { ReadableStreamToAsyncIterable } from '../internal/shims';
6
+ import { isAbortError } from '../internal/errors';
7
+ import { encodeUTF8 } from '../internal/utils/bytes';
8
+
9
+ type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
10
+
11
+ export type ServerSentEvent = {
12
+ event: string | null;
13
+ data: string;
14
+ raw: string[];
15
+ };
16
+
17
+ export class Stream<Item> implements AsyncIterable<Item> {
18
+ controller: AbortController;
19
+
20
+ constructor(
21
+ private iterator: () => AsyncIterator<Item>,
22
+ controller: AbortController,
23
+ ) {
24
+ this.controller = controller;
25
+ }
26
+
27
+ static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item> {
28
+ let consumed = false;
29
+
30
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
31
+ if (consumed) {
32
+ throw new KernelError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
33
+ }
34
+ consumed = true;
35
+ let done = false;
36
+ try {
37
+ for await (const sse of _iterSSEMessages(response, controller)) {
38
+ try {
39
+ yield JSON.parse(sse.data);
40
+ } catch (e) {
41
+ console.error(`Could not parse message into JSON:`, sse.data);
42
+ console.error(`From chunk:`, sse.raw);
43
+ throw e;
44
+ }
45
+ }
46
+ done = true;
47
+ } catch (e) {
48
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
49
+ if (isAbortError(e)) return;
50
+ throw e;
51
+ } finally {
52
+ // If the user `break`s, abort the ongoing request.
53
+ if (!done) controller.abort();
54
+ }
55
+ }
56
+
57
+ return new Stream(iterator, controller);
58
+ }
59
+
60
+ /**
61
+ * Generates a Stream from a newline-separated ReadableStream
62
+ * where each item is a JSON value.
63
+ */
64
+ static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item> {
65
+ let consumed = false;
66
+
67
+ async function* iterLines(): AsyncGenerator<string, void, unknown> {
68
+ const lineDecoder = new LineDecoder();
69
+
70
+ const iter = ReadableStreamToAsyncIterable<Bytes>(readableStream);
71
+ for await (const chunk of iter) {
72
+ for (const line of lineDecoder.decode(chunk)) {
73
+ yield line;
74
+ }
75
+ }
76
+
77
+ for (const line of lineDecoder.flush()) {
78
+ yield line;
79
+ }
80
+ }
81
+
82
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
83
+ if (consumed) {
84
+ throw new KernelError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
85
+ }
86
+ consumed = true;
87
+ let done = false;
88
+ try {
89
+ for await (const line of iterLines()) {
90
+ if (done) continue;
91
+ if (line) yield JSON.parse(line);
92
+ }
93
+ done = true;
94
+ } catch (e) {
95
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
96
+ if (isAbortError(e)) return;
97
+ throw e;
98
+ } finally {
99
+ // If the user `break`s, abort the ongoing request.
100
+ if (!done) controller.abort();
101
+ }
102
+ }
103
+
104
+ return new Stream(iterator, controller);
105
+ }
106
+
107
+ [Symbol.asyncIterator](): AsyncIterator<Item> {
108
+ return this.iterator();
109
+ }
110
+
111
+ /**
112
+ * Splits the stream into two streams which can be
113
+ * independently read from at different speeds.
114
+ */
115
+ tee(): [Stream<Item>, Stream<Item>] {
116
+ const left: Array<Promise<IteratorResult<Item>>> = [];
117
+ const right: Array<Promise<IteratorResult<Item>>> = [];
118
+ const iterator = this.iterator();
119
+
120
+ const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {
121
+ return {
122
+ next: () => {
123
+ if (queue.length === 0) {
124
+ const result = iterator.next();
125
+ left.push(result);
126
+ right.push(result);
127
+ }
128
+ return queue.shift()!;
129
+ },
130
+ };
131
+ };
132
+
133
+ return [
134
+ new Stream(() => teeIterator(left), this.controller),
135
+ new Stream(() => teeIterator(right), this.controller),
136
+ ];
137
+ }
138
+
139
+ /**
140
+ * Converts this stream to a newline-separated ReadableStream of
141
+ * JSON stringified values in the stream
142
+ * which can be turned back into a Stream with `Stream.fromReadableStream()`.
143
+ */
144
+ toReadableStream(): ReadableStream {
145
+ const self = this;
146
+ let iter: AsyncIterator<Item>;
147
+
148
+ return makeReadableStream({
149
+ async start() {
150
+ iter = self[Symbol.asyncIterator]();
151
+ },
152
+ async pull(ctrl: any) {
153
+ try {
154
+ const { value, done } = await iter.next();
155
+ if (done) return ctrl.close();
156
+
157
+ const bytes = encodeUTF8(JSON.stringify(value) + '\n');
158
+
159
+ ctrl.enqueue(bytes);
160
+ } catch (err) {
161
+ ctrl.error(err);
162
+ }
163
+ },
164
+ async cancel() {
165
+ await iter.return?.();
166
+ },
167
+ });
168
+ }
169
+ }
170
+
171
+ export async function* _iterSSEMessages(
172
+ response: Response,
173
+ controller: AbortController,
174
+ ): AsyncGenerator<ServerSentEvent, void, unknown> {
175
+ if (!response.body) {
176
+ controller.abort();
177
+ if (
178
+ typeof (globalThis as any).navigator !== 'undefined' &&
179
+ (globalThis as any).navigator.product === 'ReactNative'
180
+ ) {
181
+ throw new KernelError(
182
+ `The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`,
183
+ );
184
+ }
185
+ throw new KernelError(`Attempted to iterate over a response with no body`);
186
+ }
187
+
188
+ const sseDecoder = new SSEDecoder();
189
+ const lineDecoder = new LineDecoder();
190
+
191
+ const iter = ReadableStreamToAsyncIterable<Bytes>(response.body);
192
+ for await (const sseChunk of iterSSEChunks(iter)) {
193
+ for (const line of lineDecoder.decode(sseChunk)) {
194
+ const sse = sseDecoder.decode(line);
195
+ if (sse) yield sse;
196
+ }
197
+ }
198
+
199
+ for (const line of lineDecoder.flush()) {
200
+ const sse = sseDecoder.decode(line);
201
+ if (sse) yield sse;
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Given an async iterable iterator, iterates over it and yields full
207
+ * SSE chunks, i.e. yields when a double new-line is encountered.
208
+ */
209
+ async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
210
+ let data = new Uint8Array();
211
+
212
+ for await (const chunk of iterator) {
213
+ if (chunk == null) {
214
+ continue;
215
+ }
216
+
217
+ const binaryChunk =
218
+ chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
219
+ : typeof chunk === 'string' ? encodeUTF8(chunk)
220
+ : chunk;
221
+
222
+ let newData = new Uint8Array(data.length + binaryChunk.length);
223
+ newData.set(data);
224
+ newData.set(binaryChunk, data.length);
225
+ data = newData;
226
+
227
+ let patternIndex;
228
+ while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
229
+ yield data.slice(0, patternIndex);
230
+ data = data.slice(patternIndex);
231
+ }
232
+ }
233
+
234
+ if (data.length > 0) {
235
+ yield data;
236
+ }
237
+ }
238
+
239
+ class SSEDecoder {
240
+ private data: string[];
241
+ private event: string | null;
242
+ private chunks: string[];
243
+
244
+ constructor() {
245
+ this.event = null;
246
+ this.data = [];
247
+ this.chunks = [];
248
+ }
249
+
250
+ decode(line: string) {
251
+ if (line.endsWith('\r')) {
252
+ line = line.substring(0, line.length - 1);
253
+ }
254
+
255
+ if (!line) {
256
+ // empty line and we didn't previously encounter any messages
257
+ if (!this.event && !this.data.length) return null;
258
+
259
+ const sse: ServerSentEvent = {
260
+ event: this.event,
261
+ data: this.data.join('\n'),
262
+ raw: this.chunks,
263
+ };
264
+
265
+ this.event = null;
266
+ this.data = [];
267
+ this.chunks = [];
268
+
269
+ return sse;
270
+ }
271
+
272
+ this.chunks.push(line);
273
+
274
+ if (line.startsWith(':')) {
275
+ return null;
276
+ }
277
+
278
+ let [fieldname, _, value] = partition(line, ':');
279
+
280
+ if (value.startsWith(' ')) {
281
+ value = value.substring(1);
282
+ }
283
+
284
+ if (fieldname === 'event') {
285
+ this.event = value;
286
+ } else if (fieldname === 'data') {
287
+ this.data.push(value);
288
+ }
289
+
290
+ return null;
291
+ }
292
+ }
293
+
294
+ function partition(str: string, delimiter: string): [string, string, string] {
295
+ const index = str.indexOf(delimiter);
296
+ if (index !== -1) {
297
+ return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
298
+ }
299
+
300
+ return [str, '', ''];
301
+ }
@@ -0,0 +1,135 @@
1
+ import { concatBytes, decodeUTF8, encodeUTF8 } from '../utils/bytes';
2
+
3
+ export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
4
+
5
+ /**
6
+ * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
7
+ * reading lines from text.
8
+ *
9
+ * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
10
+ */
11
+ export class LineDecoder {
12
+ // prettier-ignore
13
+ static NEWLINE_CHARS = new Set(['\n', '\r']);
14
+ static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
15
+
16
+ #buffer: Uint8Array;
17
+ #carriageReturnIndex: number | null;
18
+
19
+ constructor() {
20
+ this.#buffer = new Uint8Array();
21
+ this.#carriageReturnIndex = null;
22
+ }
23
+
24
+ decode(chunk: Bytes): string[] {
25
+ if (chunk == null) {
26
+ return [];
27
+ }
28
+
29
+ const binaryChunk =
30
+ chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
31
+ : typeof chunk === 'string' ? encodeUTF8(chunk)
32
+ : chunk;
33
+
34
+ this.#buffer = concatBytes([this.#buffer, binaryChunk]);
35
+
36
+ const lines: string[] = [];
37
+ let patternIndex;
38
+ while ((patternIndex = findNewlineIndex(this.#buffer, this.#carriageReturnIndex)) != null) {
39
+ if (patternIndex.carriage && this.#carriageReturnIndex == null) {
40
+ // skip until we either get a corresponding `\n`, a new `\r` or nothing
41
+ this.#carriageReturnIndex = patternIndex.index;
42
+ continue;
43
+ }
44
+
45
+ // we got double \r or \rtext\n
46
+ if (
47
+ this.#carriageReturnIndex != null &&
48
+ (patternIndex.index !== this.#carriageReturnIndex + 1 || patternIndex.carriage)
49
+ ) {
50
+ lines.push(decodeUTF8(this.#buffer.subarray(0, this.#carriageReturnIndex - 1)));
51
+ this.#buffer = this.#buffer.subarray(this.#carriageReturnIndex);
52
+ this.#carriageReturnIndex = null;
53
+ continue;
54
+ }
55
+
56
+ const endIndex =
57
+ this.#carriageReturnIndex !== null ? patternIndex.preceding - 1 : patternIndex.preceding;
58
+
59
+ const line = decodeUTF8(this.#buffer.subarray(0, endIndex));
60
+ lines.push(line);
61
+
62
+ this.#buffer = this.#buffer.subarray(patternIndex.index);
63
+ this.#carriageReturnIndex = null;
64
+ }
65
+
66
+ return lines;
67
+ }
68
+
69
+ flush(): string[] {
70
+ if (!this.#buffer.length) {
71
+ return [];
72
+ }
73
+ return this.decode('\n');
74
+ }
75
+ }
76
+
77
+ /**
78
+ * This function searches the buffer for the end patterns, (\r or \n)
79
+ * and returns an object with the index preceding the matched newline and the
80
+ * index after the newline char. `null` is returned if no new line is found.
81
+ *
82
+ * ```ts
83
+ * findNewLineIndex('abc\ndef') -> { preceding: 2, index: 3 }
84
+ * ```
85
+ */
86
+ function findNewlineIndex(
87
+ buffer: Uint8Array,
88
+ startIndex: number | null,
89
+ ): { preceding: number; index: number; carriage: boolean } | null {
90
+ const newline = 0x0a; // \n
91
+ const carriage = 0x0d; // \r
92
+
93
+ for (let i = startIndex ?? 0; i < buffer.length; i++) {
94
+ if (buffer[i] === newline) {
95
+ return { preceding: i, index: i + 1, carriage: false };
96
+ }
97
+
98
+ if (buffer[i] === carriage) {
99
+ return { preceding: i, index: i + 1, carriage: true };
100
+ }
101
+ }
102
+
103
+ return null;
104
+ }
105
+
106
+ export function findDoubleNewlineIndex(buffer: Uint8Array): number {
107
+ // This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
108
+ // and returns the index right after the first occurrence of any pattern,
109
+ // or -1 if none of the patterns are found.
110
+ const newline = 0x0a; // \n
111
+ const carriage = 0x0d; // \r
112
+
113
+ for (let i = 0; i < buffer.length - 1; i++) {
114
+ if (buffer[i] === newline && buffer[i + 1] === newline) {
115
+ // \n\n
116
+ return i + 2;
117
+ }
118
+ if (buffer[i] === carriage && buffer[i + 1] === carriage) {
119
+ // \r\r
120
+ return i + 2;
121
+ }
122
+ if (
123
+ buffer[i] === carriage &&
124
+ buffer[i + 1] === newline &&
125
+ i + 3 < buffer.length &&
126
+ buffer[i + 2] === carriage &&
127
+ buffer[i + 3] === newline
128
+ ) {
129
+ // \r\n\r\n
130
+ return i + 4;
131
+ }
132
+ }
133
+
134
+ return -1;
135
+ }
@@ -1,6 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
3
  import type { FinalRequestOptions } from './request-options';
4
+ import { Stream } from '../core/streaming';
4
5
  import { type Kernel } from '../client';
5
6
  import { formatRequestDetails, loggerFor } from './utils/log';
6
7
 
@@ -16,6 +17,19 @@ export type APIResponseProps = {
16
17
  export async function defaultParseResponse<T>(client: Kernel, props: APIResponseProps): Promise<T> {
17
18
  const { response, requestLogID, retryOfRequestLogID, startTime } = props;
18
19
  const body = await (async () => {
20
+ if (props.options.stream) {
21
+ loggerFor(client).debug('response', response.status, response.url, response.headers, response.body);
22
+
23
+ // Note: there is an invariant here that isn't represented in the type system
24
+ // that if you set `stream: true` the response type must also be `Stream<T>`
25
+
26
+ if (props.options.__streamClass) {
27
+ return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
28
+ }
29
+
30
+ return Stream.fromSSEResponse(response, props.controller) as any;
31
+ }
32
+
19
33
  // fetch refuses to read the body when the status code is 204.
20
34
  if (response.status === 204) {
21
35
  return null as T;
@@ -3,6 +3,7 @@
3
3
  import { NullableHeaders } from './headers';
4
4
 
5
5
  import type { BodyInit } from './builtin-types';
6
+ import { Stream } from '../core/streaming';
6
7
  import type { HTTPMethod, MergedRequestInit } from './types';
7
8
  import { type HeadersLike } from './headers';
8
9
 
@@ -22,6 +23,7 @@ export type RequestOptions = {
22
23
  idempotencyKey?: string;
23
24
 
24
25
  __binaryResponse?: boolean | undefined;
26
+ __streamClass?: typeof Stream;
25
27
  };
26
28
 
27
29
  export type EncodedContent = { bodyHeaders: HeadersLike; body: BodyInit };
@@ -2,7 +2,12 @@
2
2
 
3
3
  import { APIResource } from '../../core/resource';
4
4
  import * as DeploymentsAPI from './deployments';
5
- import { DeploymentCreateParams, DeploymentCreateResponse, Deployments } from './deployments';
5
+ import {
6
+ DeploymentCreateParams,
7
+ DeploymentCreateResponse,
8
+ DeploymentFollowResponse,
9
+ Deployments,
10
+ } from './deployments';
6
11
  import * as InvocationsAPI from './invocations';
7
12
  import {
8
13
  InvocationCreateParams,
@@ -10,19 +15,83 @@ import {
10
15
  InvocationRetrieveResponse,
11
16
  Invocations,
12
17
  } from './invocations';
18
+ import { APIPromise } from '../../core/api-promise';
19
+ import { RequestOptions } from '../../internal/request-options';
13
20
 
14
21
  export class Apps extends APIResource {
15
22
  deployments: DeploymentsAPI.Deployments = new DeploymentsAPI.Deployments(this._client);
16
23
  invocations: InvocationsAPI.Invocations = new InvocationsAPI.Invocations(this._client);
24
+
25
+ /**
26
+ * List application versions for the authenticated user. Optionally filter by app
27
+ * name and/or version label.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const apps = await client.apps.list();
32
+ * ```
33
+ */
34
+ list(query: AppListParams | null | undefined = {}, options?: RequestOptions): APIPromise<AppListResponse> {
35
+ return this._client.get('/apps', { query, ...options });
36
+ }
37
+ }
38
+
39
+ export type AppListResponse = Array<AppListResponse.AppListResponseItem>;
40
+
41
+ export namespace AppListResponse {
42
+ /**
43
+ * Summary of an application version.
44
+ */
45
+ export interface AppListResponseItem {
46
+ /**
47
+ * Unique identifier for the app version
48
+ */
49
+ id: string;
50
+
51
+ /**
52
+ * Name of the application
53
+ */
54
+ app_name: string;
55
+
56
+ /**
57
+ * Deployment region code
58
+ */
59
+ region: string;
60
+
61
+ /**
62
+ * Version label for the application
63
+ */
64
+ version: string;
65
+
66
+ /**
67
+ * Environment variables configured for this app version
68
+ */
69
+ env_vars?: Record<string, string>;
70
+ }
71
+ }
72
+
73
+ export interface AppListParams {
74
+ /**
75
+ * Filter results by application name.
76
+ */
77
+ app_name?: string;
78
+
79
+ /**
80
+ * Filter results by version label.
81
+ */
82
+ version?: string;
17
83
  }
18
84
 
19
85
  Apps.Deployments = Deployments;
20
86
  Apps.Invocations = Invocations;
21
87
 
22
88
  export declare namespace Apps {
89
+ export { type AppListResponse as AppListResponse, type AppListParams as AppListParams };
90
+
23
91
  export {
24
92
  Deployments as Deployments,
25
93
  type DeploymentCreateResponse as DeploymentCreateResponse,
94
+ type DeploymentFollowResponse as DeploymentFollowResponse,
26
95
  type DeploymentCreateParams as DeploymentCreateParams,
27
96
  };
28
97