@langchain/langgraph-sdk
Version:
Client library for interacting with the LangGraph API
55 lines (54 loc) • 1.39 kB
JavaScript
import { v4 } from "@langchain/core/utils/uuid";
//#region src/react-ui/server/server.ts
/**
* Helper to send and persist UI messages. Accepts a map of component names to React components
* as type argument to provide type safety. Will also write to the `options?.stateKey` state.
*
* @param config LangGraphRunnableConfig
* @param options
* @returns
*/
const typedUi = (config, options) => {
const items = [];
const stateKey = options?.stateKey ?? "ui";
const runId = config.metadata?.run_id ?? config.runId;
if (!runId) throw new Error("run_id is required");
function handlePush(message, options) {
const evt = {
type: "ui",
id: message?.id ?? v4(),
name: message?.name,
props: message?.props,
metadata: {
merge: options?.merge || void 0,
run_id: runId,
tags: config.tags,
name: config.runName,
...message?.metadata,
...options?.message ? { id: options.message.id } : null
}
};
items.push(evt);
config.writer?.(evt);
config.configurable?.__pregel_send?.([[stateKey, evt]]);
return evt;
}
const handleDelete = (id) => {
const evt = {
type: "remove-ui",
id
};
items.push(evt);
config.writer?.(evt);
config.configurable?.__pregel_send?.([[stateKey, evt]]);
return evt;
};
return {
push: handlePush,
delete: handleDelete,
items
};
};
//#endregion
export { typedUi };
//# sourceMappingURL=server.js.map