@langgraph-js/pro
Version:
The Pro SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces and build complex AI workflows
51 lines (50 loc) • 1.49 kB
JavaScript
import { AIMessage, ToolMessage } from "@langchain/core/messages";
import { createMessagesQuery } from "./query.js";
export * from "./query.js";
/**
* 获取最后一条人类发送的消息
* @example
* const lastHumanMessage = getLastHumanMessage(state.messages);
* console.log(lastHumanMessage);
*/
export function getLastHumanMessage(messages) {
// 如果没有找到 HumanMessage,则返回 undefined
return createMessagesQuery().isHuman().build().messages(messages).last();
}
export const getThreadId = (context) => {
return context?.configurable?.thread_id;
};
export const getToolCallId = (context) => {
return context?.toolCall?.id;
};
/**
* 创建一对 toolCall 数据
* @example
* ```typescript
* const [aiMessage, toolMessage] = createToolCall("toolName", { input: "input" }, "this is tool outputs");
* ```
*/
export const createToolCall = (toolName, input, output) => {
const aiId = crypto.randomUUID();
const toolCallId = crypto.randomUUID();
return [
new AIMessage({
content: ``,
id: aiId,
tool_calls: [
{
id: toolCallId,
name: toolName,
args: input,
type: "tool_call",
},
],
}),
new ToolMessage({
id: crypto.randomUUID(),
content: output,
tool_call_id: toolCallId,
name: toolName,
}),
];
};