Skip to content

Commit

Permalink
Add streaming support for tuple chat widget (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Mar 1, 2024
1 parent b6ec1e8 commit 81ebc7c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,47 @@ export const ChatMessageTuplesControlRenderer = withJsonFormsControlProps(
(props) => {
const data: Array<MessageTuple> = props.data ?? [];

useStreamCallback("onSuccess", (ctx) => {
useStreamCallback("onChunk", (_chunk, aggregatedState) => {
if (!isJsonSchemaExtra(props.schema)) return;
const widget = props.schema.extra.widget;
if (!("input" in widget) && !("output" in widget)) return;

const inputPath = getNormalizedJsonPath(widget.input ?? "");
const outputPath = getNormalizedJsonPath(widget.output ?? "");

const isSingleOutputKey =
ctx.output != null &&
Object.keys(ctx.output).length === 1 &&
Object.keys(ctx.output)[0] === "output";
aggregatedState?.final_output != null &&
Object.keys(aggregatedState?.final_output).length === 1 &&
Object.keys(aggregatedState?.final_output)[0] === "output";

const human = traverseNaiveJsonPath(ctx.input, inputPath);
let ai = traverseNaiveJsonPath(ctx.output, outputPath);
let ai = traverseNaiveJsonPath(aggregatedState?.final_output, outputPath);

if (isSingleOutputKey) {
ai = traverseNaiveJsonPath(ai, ["output", ...outputPath]) ?? ai;
}

ai = getMessageContent(ai);
if (typeof human === "string" && typeof ai === "string") {
props.handleChange(props.path, [...data, [human, ai]]);
if (typeof ai === "string") {
props.handleChange(props.path, [...data.slice(0, -1), [data[data.length - 1][0], ai]]);
}
});

useStreamCallback("onStart", (ctx) => {
if (!isJsonSchemaExtra(props.schema)) return;
const widget = props.schema.extra.widget;
if (!("input" in widget) && !("output" in widget)) return;

const inputPath = getNormalizedJsonPath(widget.input ?? "");

const human = traverseNaiveJsonPath(ctx.input, inputPath);
if (typeof human === "string") {
props.handleChange(props.path, [...data, [human, ""]]);
}
});

useStreamCallback("onError", () => {
props.handleChange(props.path, [...data.slice(0, -1)]);
});

return (
<div className="control">
<div className="flex items-center justify-between">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export const ChatMessagesControlRenderer = withJsonFormsControlProps(
props.handleChange(props.path, [...data, { content: "", type: "human" }]);
});

useStreamCallback("onError", () => {
if (data.length && data[data.length - 1].type === "ai") {
props.handleChange(props.path, [...data.slice(0, -1)]);
}
});

return (
<div className="control">
<div className="flex items-center justify-between">
Expand Down

0 comments on commit 81ebc7c

Please sign in to comment.