langchain
Version:
Typescript bindings for langchain
1 lines • 3.23 kB
Source Map (JSON)
{"version":3,"file":"dynamicSystemPrompt.cjs","names":["fn: DynamicSystemPromptMiddlewareConfig<TContextSchema>","createMiddleware","SystemMessage"],"sources":["../../../src/agents/middleware/dynamicSystemPrompt.ts"],"sourcesContent":["import { SystemMessage } from \"@langchain/core/messages\";\nimport { createMiddleware } from \"../middleware.js\";\nimport type { Runtime, AgentBuiltInState } from \"../runtime.js\";\n\nexport type DynamicSystemPromptMiddlewareConfig<TContextSchema> = (\n state: AgentBuiltInState,\n runtime: Runtime<TContextSchema>\n) => string | SystemMessage | Promise<string | SystemMessage>;\n\n/**\n * Dynamic System Prompt Middleware\n *\n * Allows setting the system prompt dynamically right before each model invocation.\n * Useful when the prompt depends on the current agent state or per-invocation context.\n *\n * @typeParam TContextSchema - The shape of the runtime context available at invocation time.\n * If your agent defines a `contextSchema`, pass the inferred type here to get full type-safety\n * for `runtime.context`.\n *\n * @param fn - Function that receives the current agent `state` and `runtime`, and\n * returns the system prompt for the next model call as a string.\n *\n * @returns A middleware instance that sets `systemPrompt` for the next model call.\n *\n * @example Basic usage with typed context\n * ```ts\n * import { z } from \"zod\";\n * import { dynamicSystemPrompt } from \"langchain\";\n * import { createAgent, SystemMessage } from \"langchain\";\n *\n * const contextSchema = z.object({ region: z.string().optional() });\n *\n * const middleware = dynamicSystemPrompt<z.infer<typeof contextSchema>>(\n * (_state, runtime) => `You are a helpful assistant. Region: ${runtime.context.region ?? \"n/a\"}`\n * );\n *\n * const agent = createAgent({\n * model: \"anthropic:claude-3-5-sonnet\",\n * contextSchema,\n * middleware: [middleware],\n * });\n *\n * await agent.invoke({ messages }, { context: { region: \"EU\" } });\n * ```\n *\n * @public\n */\nexport function dynamicSystemPromptMiddleware<TContextSchema = unknown>(\n fn: DynamicSystemPromptMiddlewareConfig<TContextSchema>\n) {\n return createMiddleware({\n name: \"DynamicSystemPromptMiddleware\",\n wrapModelCall: async (request, handler) => {\n const systemPrompt = await fn(\n request.state as AgentBuiltInState,\n request.runtime as Runtime<TContextSchema>\n );\n\n const isExpectedType =\n typeof systemPrompt === \"string\" ||\n SystemMessage.isInstance(systemPrompt);\n if (!isExpectedType) {\n throw new Error(\n \"dynamicSystemPromptMiddleware function must return a string or SystemMessage\"\n );\n }\n\n return handler({\n ...request,\n systemMessage: request.systemMessage.concat(systemPrompt),\n });\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,SAAgB,8BACdA,IACA;AACA,QAAOC,oCAAiB;EACtB,MAAM;EACN,eAAe,OAAO,SAAS,YAAY;GACzC,MAAM,eAAe,MAAM,GACzB,QAAQ,OACR,QAAQ,QACT;GAED,MAAM,iBACJ,OAAO,iBAAiB,YACxBC,wCAAc,WAAW,aAAa;AACxC,OAAI,CAAC,eACH,OAAM,IAAI,MACR;AAIJ,UAAO,QAAQ;IACb,GAAG;IACH,eAAe,QAAQ,cAAc,OAAO,aAAa;GAC1D,EAAC;EACH;CACF,EAAC;AACH"}