Skip to content

Commit

Permalink
feat(js-sdk): Support CJS (#1742)
Browse files Browse the repository at this point in the history
* feat(js-sdk): Support CJS

* Bump version
  • Loading branch information
jacoblee93 authored Sep 17, 2024
1 parent c4d4d61 commit 47a68c1
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 15 deletions.
3 changes: 1 addition & 2 deletions libs/sdk-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/langgraph-sdk",
"version": "0.0.9",
"version": "0.0.10",
"description": "Client library for interacting with the LangGraph API",
"type": "module",
"packageManager": "[email protected]",
Expand All @@ -17,7 +17,6 @@
"license": "MIT",
"dependencies": {
"@types/json-schema": "^7.0.15",
"eventsource-parser": "^1.1.2",
"p-queue": "^6.6.2",
"p-retry": "4",
"uuid": "^9.0.0"
Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-js/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const generateFiles = () => {
const compiledPath = `${relativePath}dist/${value}`;
return [
[`${key}.cjs`, `module.exports = require('${compiledPath}.cjs');`],
[`${key}.js`, `export * from '${compiledPath}.mjs'`],
[`${key}.d.ts`, `export * from '${compiledPath}.mjs'`],
[`${key}.js`, `export * from '${compiledPath}.js'`],
[`${key}.d.ts`, `export * from '${compiledPath}.js'`],
[`${key}.d.cts`, `export * from '${compiledPath}.cjs'`],
];
},
Expand Down
11 changes: 7 additions & 4 deletions libs/sdk-js/src/client.mts → libs/sdk-js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import {
ThreadState,
Cron,
} from "./schema.js";
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.mjs";
import { EventSourceParser, createParser } from "eventsource-parser";
import { IterableReadableStream } from "./utils/stream.mjs";
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.js";
import {
EventSourceParser,
createParser,
} from "./utils/eventsource-parser/index.js";
import { IterableReadableStream } from "./utils/stream.js";
import {
RunsCreatePayload,
RunsStreamPayload,
RunsWaitPayload,
StreamEvent,
CronsCreatePayload,
OnConflictBehavior,
} from "./types.mjs";
} from "./types.js";

interface ClientConfig {
apiUrl?: string;
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-js/src/index.mts → libs/sdk-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Client } from "./client.mjs";
export { Client } from "./client.js";

export type {
Assistant,
Expand Down
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions libs/sdk-js/src/utils/eventsource-parser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Espen Hovlandsdal <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions libs/sdk-js/src/utils/eventsource-parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// From https://github.com/rexxars/eventsource-parser
// Inlined due to CJS import issues

export { createParser } from "./parse.js";
export type {
EventSourceParseCallback,
EventSourceParser,
ParsedEvent,
ParseEvent,
ReconnectInterval,
} from "./types.js";
185 changes: 185 additions & 0 deletions libs/sdk-js/src/utils/eventsource-parser/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/**
* EventSource/Server-Sent Events parser
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
*
* Based on code from the {@link https://github.com/EventSource/eventsource | EventSource module},
* which is licensed under the MIT license. And copyrighted the EventSource GitHub organisation.
*/
import type { EventSourceParseCallback, EventSourceParser } from "./types.js";

/**
* Creates a new EventSource parser.
*
* @param onParse - Callback to invoke when a new event is parsed, or a new reconnection interval
* has been sent from the server
*
* @returns A new EventSource parser, with `parse` and `reset` methods.
* @public
*/
export function createParser(
onParse: EventSourceParseCallback,
): EventSourceParser {
// Processing state
let isFirstChunk: boolean;
let buffer: string;
let startingPosition: number;
let startingFieldLength: number;

// Event state
let eventId: string | undefined;
let eventName: string | undefined;
let data: string;

reset();
return { feed, reset };

function reset(): void {
isFirstChunk = true;
buffer = "";
startingPosition = 0;
startingFieldLength = -1;

eventId = undefined;
eventName = undefined;
data = "";
}

function feed(chunk: string): void {
buffer = buffer ? buffer + chunk : chunk;

// Strip any UTF8 byte order mark (BOM) at the start of the stream.
// Note that we do not strip any non - UTF8 BOM, as eventsource streams are
// always decoded as UTF8 as per the specification.
if (isFirstChunk && hasBom(buffer)) {
buffer = buffer.slice(BOM.length);
}

isFirstChunk = false;

// Set up chunk-specific processing state
const length = buffer.length;
let position = 0;
let discardTrailingNewline = false;

// Read the current buffer byte by byte
while (position < length) {
// EventSource allows for carriage return + line feed, which means we
// need to ignore a linefeed character if the previous character was a
// carriage return
// @todo refactor to reduce nesting, consider checking previous byte?
// @todo but consider multiple chunks etc
if (discardTrailingNewline) {
if (buffer[position] === "\n") {
++position;
}
discardTrailingNewline = false;
}

let lineLength = -1;
let fieldLength = startingFieldLength;
let character: string;

for (
let index = startingPosition;
lineLength < 0 && index < length;
++index
) {
character = buffer[index];
if (character === ":" && fieldLength < 0) {
fieldLength = index - position;
} else if (character === "\r") {
discardTrailingNewline = true;
lineLength = index - position;
} else if (character === "\n") {
lineLength = index - position;
}
}

if (lineLength < 0) {
startingPosition = length - position;
startingFieldLength = fieldLength;
break;
} else {
startingPosition = 0;
startingFieldLength = -1;
}

parseEventStreamLine(buffer, position, fieldLength, lineLength);

position += lineLength + 1;
}

if (position === length) {
// If we consumed the entire buffer to read the event, reset the buffer
buffer = "";
} else if (position > 0) {
// If there are bytes left to process, set the buffer to the unprocessed
// portion of the buffer only
buffer = buffer.slice(position);
}
}

function parseEventStreamLine(
lineBuffer: string,
index: number,
fieldLength: number,
lineLength: number,
) {
if (lineLength === 0) {
// We reached the last line of this event
if (data.length > 0) {
onParse({
type: "event",
id: eventId,
event: eventName || undefined,
data: data.slice(0, -1), // remove trailing newline
});

data = "";
eventId = undefined;
}
eventName = undefined;
return;
}

const noValue = fieldLength < 0;
const field = lineBuffer.slice(
index,
index + (noValue ? lineLength : fieldLength),
);
let step = 0;

if (noValue) {
step = lineLength;
} else if (lineBuffer[index + fieldLength + 1] === " ") {
step = fieldLength + 2;
} else {
step = fieldLength + 1;
}

const position = index + step;
const valueLength = lineLength - step;
const value = lineBuffer.slice(position, position + valueLength).toString();

if (field === "data") {
data += value ? `${value}\n` : "\n";
} else if (field === "event") {
eventName = value;
} else if (field === "id" && !value.includes("\u0000")) {
eventId = value;
} else if (field === "retry") {
const retry = parseInt(value, 10);
if (!Number.isNaN(retry)) {
onParse({ type: "reconnect-interval", value: retry });
}
}
}
}

const BOM = [239, 187, 191];

function hasBom(buffer: string) {
return BOM.every(
(charCode: number, index: number) => buffer.charCodeAt(index) === charCode,
);
}
38 changes: 38 additions & 0 deletions libs/sdk-js/src/utils/eventsource-parser/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createParser } from "./parse.js";
import type { EventSourceParser, ParsedEvent } from "./types.js";

/**
* A TransformStream that ingests a stream of strings and produces a stream of ParsedEvents.
*
* @example
* ```
* const eventStream =
* response.body
* .pipeThrough(new TextDecoderStream())
* .pipeThrough(new EventSourceParserStream())
* ```
* @public
*/
export class EventSourceParserStream extends TransformStream<
string,
ParsedEvent
> {
constructor() {
let parser!: EventSourceParser;

super({
start(controller) {
parser = createParser((event: any) => {
if (event.type === "event") {
controller.enqueue(event);
}
});
},
transform(chunk) {
parser.feed(chunk);
},
});
}
}

export type { ParsedEvent } from "./types.js";
Loading

0 comments on commit 47a68c1

Please sign in to comment.