import * as z from "zod";import { tool } from "langchain";import { type ToolRuntime } from "@langchain/core/tools";const contextSchema = z.object({ userName: z.string(),});const fetchUserEmailPreferences = tool( async (_, runtime: ToolRuntime<any, typeof contextSchema>) => { const userName = runtime.context?.userName; if (!userName) { throw new Error("userName is required"); } let preferences = "The user prefers you to write a brief and polite email."; if (runtime.store) { const memory = await runtime.store?.get(["users"], userName); if (memory) { preferences = memory.value.preferences; } } return preferences; }, { name: "fetch_user_email_preferences", description: "Fetch the user's email preferences.", schema: z.object({}), });
import { tool } from "langchain";import * as z from "zod";const contextAwareTool = tool( async (_input, runtime) => { // Access thread and run IDs const info = runtime.executionInfo; console.log(`Thread: ${info.threadId}, Run: ${info.runId}`); // Access server info (only available on LangGraph Server) const server = runtime.serverInfo; if (server != null) { console.log(`Assistant: ${server.assistantId}`); if (server.user != null) { console.log(`User: ${server.user.identity}`); } } return "done"; }, { name: "context_aware_tool", description: "A tool that uses execution and server info.", schema: z.object({}), });
当不在 LangGraph Server 上运行(例如本地开发期间)时,serverInfo 为 null。