-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
150 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
feat: expose streamUtils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from "./converters"; | ||
|
||
export * from "./streams/utils"; | ||
|
||
export { useEdgeRuntime, type EdgeRuntimeOptions } from "./useEdgeRuntime"; | ||
export { EdgeChatAdapter } from "./EdgeChatAdapter"; | ||
export type { EdgeRuntimeRequestOptions } from "./EdgeRuntimeRequestOptions"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/react/src/runtimes/edge/streams/utils/PipeableTransformStream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class PipeableTransformStream<I, O> extends TransformStream<I, O> { | ||
constructor(transform: (readable: ReadableStream<I>) => ReadableStream<O>) { | ||
super(); | ||
const readable = transform(super.readable as any); | ||
Object.defineProperty(this, "readable", { | ||
value: readable, | ||
writable: false, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type StreamPart<T extends Record<string, unknown>> = { | ||
[K in keyof T]: { type: K; value: T[K] }; | ||
}[keyof T]; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { streamPartDecoderStream } from "./streamPartDecoderStream"; | ||
import { streamPartEncoderStream } from "./streamPartEncoderStream"; | ||
import { StreamPart } from "./StreamPart"; | ||
|
||
export declare namespace StreamUtils { | ||
export { StreamPart }; | ||
} | ||
|
||
export const streamUtils = { | ||
streamPartEncoderStream, | ||
streamPartDecoderStream, | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/react/src/runtimes/edge/streams/utils/streamPartDecoderStream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { chunkByLineStream } from "./chunkByLineStream"; | ||
import { PipeableTransformStream } from "./PipeableTransformStream"; | ||
import { StreamPart } from "./StreamPart"; | ||
|
||
const decodeStreamPart = <T extends Record<string, unknown>>( | ||
part: string, | ||
): StreamPart<T> => { | ||
const index = part.indexOf(":"); | ||
if (index === -1) throw new Error("Invalid stream part"); | ||
return { | ||
type: part.slice(0, index), | ||
value: JSON.parse(part.slice(index + 1)), | ||
}; | ||
}; | ||
|
||
export function streamPartDecoderStream<T extends Record<string, unknown>>() { | ||
const decodeStream = new TransformStream<string, StreamPart<T>>({ | ||
transform(chunk, controller) { | ||
controller.enqueue(decodeStreamPart<T>(chunk)); | ||
}, | ||
}); | ||
|
||
return new PipeableTransformStream((readable) => { | ||
return readable | ||
.pipeThrough(new TextDecoderStream()) | ||
.pipeThrough(chunkByLineStream()) | ||
.pipeThrough(decodeStream); | ||
}); | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/react/src/runtimes/edge/streams/utils/streamPartEncoderStream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { PipeableTransformStream } from "./PipeableTransformStream"; | ||
import { StreamPart } from "./StreamPart"; | ||
|
||
function encodeStreamPart<T extends Record<string, unknown>>({ | ||
type, | ||
value, | ||
}: StreamPart<T>): string { | ||
return `${type as string}:${JSON.stringify(value)}\n`; | ||
} | ||
|
||
export function streamPartEncoderStream<T extends Record<string, unknown>>() { | ||
const encodeStream = new TransformStream<StreamPart<T>, string>({ | ||
transform(chunk, controller) { | ||
controller.enqueue(encodeStreamPart<T>(chunk)); | ||
}, | ||
}); | ||
|
||
return new PipeableTransformStream((readable) => { | ||
return readable | ||
.pipeThrough(encodeStream) | ||
.pipeThrough(new TextEncoderStream()); | ||
}); | ||
} |