Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.1.0-alpha.12"
".": "0.1.0-alpha.13"
}
8 changes: 4 additions & 4 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 5
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel%2Fkernel-1fe396b957ced73281fc0a61a69b630836aa5c89a8dccce2c5a1716bc9775e80.yml
openapi_spec_hash: 9a0d67fb0781be034b77839584109638
config_hash: df889df131f7438197abd59faace3c77
configured_endpoints: 7
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel%2Fkernel-c9d64df733f286f09d2203f4e3d820ce57e8d4c629c5e2db4e2bfac91fbc1598.yml
openapi_spec_hash: fa407611fc566d55f403864fbfaa6c23
config_hash: 7f67c5b95af1e4b39525515240b72275
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 0.1.0-alpha.13 (2025-05-19)

Full Changelog: [v0.1.0-alpha.12...v0.1.0-alpha.13](https://github.com/onkernel/kernel-node-sdk/compare/v0.1.0-alpha.12...v0.1.0-alpha.13)

### Features

* **api:** update via SDK Studio ([bba7f08](https://github.com/onkernel/kernel-node-sdk/commit/bba7f08386eb92c2fcc5087a414e6d29f2ece821))
* **api:** update via SDK Studio ([4c42d7c](https://github.com/onkernel/kernel-node-sdk/commit/4c42d7cdd5c0d25d48b2c5ea4bb1db9af009b279))

## 0.1.0-alpha.12 (2025-05-19)

Full Changelog: [v0.1.0-alpha.11...v0.1.0-alpha.12](https://github.com/onkernel/kernel-node-sdk/compare/v0.1.0-alpha.11...v0.1.0-alpha.12)
Expand Down
10 changes: 10 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# Apps

Types:

- <code><a href="./src/resources/apps/apps.ts">AppListResponse</a></code>

Methods:

- <code title="get /apps">client.apps.<a href="./src/resources/apps/apps.ts">list</a>({ ...params }) -> AppListResponse</code>

## Deployments

Types:

- <code><a href="./src/resources/apps/deployments.ts">DeploymentCreateResponse</a></code>
- <code><a href="./src/resources/apps/deployments.ts">DeploymentFollowResponse</a></code>

Methods:

- <code title="post /deploy">client.apps.deployments.<a href="./src/resources/apps/deployments.ts">create</a>({ ...params }) -> DeploymentCreateResponse</code>
- <code title="get /apps/{id}/events">client.apps.deployments.<a href="./src/resources/apps/deployments.ts">follow</a>(id) -> DeploymentFollowResponse</code>

## Invocations

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onkernel/sdk",
"version": "0.1.0-alpha.12",
"version": "0.1.0-alpha.13",
"description": "The official TypeScript library for the Kernel API",
"author": "Kernel <>",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { readEnv } from './internal/utils/env';
import { formatRequestDetails, loggerFor } from './internal/utils/log';
import { isEmptyObj } from './internal/utils/values';
import { KernelApp } from './core/app-framework';
import { Apps } from './resources/apps/apps';
import { AppListParams, AppListResponse, Apps } from './resources/apps/apps';

const environments = {
production: 'https://api.onkernel.com/',
Expand Down Expand Up @@ -740,7 +740,7 @@ Kernel.Browsers = Browsers;
export declare namespace Kernel {
export type RequestOptions = Opts.RequestOptions;

export { Apps as Apps };
export { Apps as Apps, type AppListResponse as AppListResponse, type AppListParams as AppListParams };

export {
Browsers as Browsers,
Expand Down
2 changes: 1 addition & 1 deletion src/core/app-framework.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface KernelContext {
invocationId: string;
invocation_id: string;
}

export interface KernelAction {
Expand Down
301 changes: 301 additions & 0 deletions src/core/streaming.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
import { KernelError } from './error';
import { type ReadableStream } from '../internal/shim-types';
import { makeReadableStream } from '../internal/shims';
import { findDoubleNewlineIndex, LineDecoder } from '../internal/decoders/line';
import { ReadableStreamToAsyncIterable } from '../internal/shims';
import { isAbortError } from '../internal/errors';
import { encodeUTF8 } from '../internal/utils/bytes';

type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;

export type ServerSentEvent = {
event: string | null;
data: string;
raw: string[];
};

export class Stream<Item> implements AsyncIterable<Item> {
controller: AbortController;

constructor(
private iterator: () => AsyncIterator<Item>,
controller: AbortController,
) {
this.controller = controller;
}

static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item> {
let consumed = false;

async function* iterator(): AsyncIterator<Item, any, undefined> {
if (consumed) {
throw new KernelError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
}
consumed = true;
let done = false;
try {
for await (const sse of _iterSSEMessages(response, controller)) {
try {
yield JSON.parse(sse.data);
} catch (e) {
console.error(`Could not parse message into JSON:`, sse.data);
console.error(`From chunk:`, sse.raw);
throw e;
}
}
done = true;
} catch (e) {
// If the user calls `stream.controller.abort()`, we should exit without throwing.
if (isAbortError(e)) return;
throw e;
} finally {
// If the user `break`s, abort the ongoing request.
if (!done) controller.abort();
}
}

return new Stream(iterator, controller);
}

/**
* Generates a Stream from a newline-separated ReadableStream
* where each item is a JSON value.
*/
static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item> {
let consumed = false;

async function* iterLines(): AsyncGenerator<string, void, unknown> {
const lineDecoder = new LineDecoder();

const iter = ReadableStreamToAsyncIterable<Bytes>(readableStream);
for await (const chunk of iter) {
for (const line of lineDecoder.decode(chunk)) {
yield line;
}
}

for (const line of lineDecoder.flush()) {
yield line;
}
}

async function* iterator(): AsyncIterator<Item, any, undefined> {
if (consumed) {
throw new KernelError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
}
consumed = true;
let done = false;
try {
for await (const line of iterLines()) {
if (done) continue;
if (line) yield JSON.parse(line);
}
done = true;
} catch (e) {
// If the user calls `stream.controller.abort()`, we should exit without throwing.
if (isAbortError(e)) return;
throw e;
} finally {
// If the user `break`s, abort the ongoing request.
if (!done) controller.abort();
}
}

return new Stream(iterator, controller);
}

[Symbol.asyncIterator](): AsyncIterator<Item> {
return this.iterator();
}

/**
* Splits the stream into two streams which can be
* independently read from at different speeds.
*/
tee(): [Stream<Item>, Stream<Item>] {
const left: Array<Promise<IteratorResult<Item>>> = [];
const right: Array<Promise<IteratorResult<Item>>> = [];
const iterator = this.iterator();

const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {
return {
next: () => {
if (queue.length === 0) {
const result = iterator.next();
left.push(result);
right.push(result);
}
return queue.shift()!;
},
};
};

return [
new Stream(() => teeIterator(left), this.controller),
new Stream(() => teeIterator(right), this.controller),
];
}

/**
* Converts this stream to a newline-separated ReadableStream of
* JSON stringified values in the stream
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
*/
toReadableStream(): ReadableStream {
const self = this;
let iter: AsyncIterator<Item>;

return makeReadableStream({
async start() {
iter = self[Symbol.asyncIterator]();
},
async pull(ctrl: any) {
try {
const { value, done } = await iter.next();
if (done) return ctrl.close();

const bytes = encodeUTF8(JSON.stringify(value) + '\n');

ctrl.enqueue(bytes);
} catch (err) {
ctrl.error(err);
}
},
async cancel() {
await iter.return?.();
},
});
}
}

export async function* _iterSSEMessages(
response: Response,
controller: AbortController,
): AsyncGenerator<ServerSentEvent, void, unknown> {
if (!response.body) {
controller.abort();
if (
typeof (globalThis as any).navigator !== 'undefined' &&
(globalThis as any).navigator.product === 'ReactNative'
) {
throw new KernelError(
`The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`,
);
}
throw new KernelError(`Attempted to iterate over a response with no body`);
}

const sseDecoder = new SSEDecoder();
const lineDecoder = new LineDecoder();

const iter = ReadableStreamToAsyncIterable<Bytes>(response.body);
for await (const sseChunk of iterSSEChunks(iter)) {
for (const line of lineDecoder.decode(sseChunk)) {
const sse = sseDecoder.decode(line);
if (sse) yield sse;
}
}

for (const line of lineDecoder.flush()) {
const sse = sseDecoder.decode(line);
if (sse) yield sse;
}
}

/**
* Given an async iterable iterator, iterates over it and yields full
* SSE chunks, i.e. yields when a double new-line is encountered.
*/
async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
let data = new Uint8Array();

for await (const chunk of iterator) {
if (chunk == null) {
continue;
}

const binaryChunk =
chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
: typeof chunk === 'string' ? encodeUTF8(chunk)
: chunk;

let newData = new Uint8Array(data.length + binaryChunk.length);
newData.set(data);
newData.set(binaryChunk, data.length);
data = newData;

let patternIndex;
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
yield data.slice(0, patternIndex);
data = data.slice(patternIndex);
}
}

if (data.length > 0) {
yield data;
}
}

class SSEDecoder {
private data: string[];
private event: string | null;
private chunks: string[];

constructor() {
this.event = null;
this.data = [];
this.chunks = [];
}

decode(line: string) {
if (line.endsWith('\r')) {
line = line.substring(0, line.length - 1);
}

if (!line) {
// empty line and we didn't previously encounter any messages
if (!this.event && !this.data.length) return null;

const sse: ServerSentEvent = {
event: this.event,
data: this.data.join('\n'),
raw: this.chunks,
};

this.event = null;
this.data = [];
this.chunks = [];

return sse;
}

this.chunks.push(line);

if (line.startsWith(':')) {
return null;
}

let [fieldname, _, value] = partition(line, ':');

if (value.startsWith(' ')) {
value = value.substring(1);
}

if (fieldname === 'event') {
this.event = value;
} else if (fieldname === 'data') {
this.data.push(value);
}

return null;
}
}

function partition(str: string, delimiter: string): [string, string, string] {
const index = str.indexOf(delimiter);
if (index !== -1) {
return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
}

return [str, '', ''];
}
Loading