|
| 1 | +import { tool } from "@langchain/core/tools"; |
| 2 | +import type { DynamicStructuredTool, ToolRuntime } from "@langchain/core/tools"; |
| 3 | + |
| 4 | +import { StateFileSystem } from "./utils/StateFileSystem.js"; |
| 5 | +import { CommandHandler } from "./utils/CommandHandler.js"; |
| 6 | +import { handleMemoryCommand } from "./utils/index.js"; |
| 7 | +import type { FileData } from "./utils/FileData.js"; |
| 8 | +import type { FileSystem } from "./utils/FileSystem.js"; |
| 9 | +import type { MemoryTool20250818Options } from "./types.js"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Creates an Anthropic memory tool that can be used with ChatAnthropic. |
| 13 | + * |
| 14 | + * The memory tool enables Claude to store and retrieve information across conversations |
| 15 | + * through a memory file directory. Claude can create, read, update, and delete files that |
| 16 | + * persist between sessions, allowing it to build knowledge over time without keeping |
| 17 | + * everything in the context window. |
| 18 | + * |
| 19 | + * @example |
| 20 | + * ```typescript |
| 21 | + * import { ChatAnthropic, memory_20250818 } from "@langchain/anthropic"; |
| 22 | + * |
| 23 | + * const llm = new ChatAnthropic({ |
| 24 | + * model: "claude-sonnet-4-5-20250929", |
| 25 | + * clientOptions: { |
| 26 | + * defaultHeaders: { |
| 27 | + * "anthropic-beta": "context-management-2025-06-27", |
| 28 | + * }, |
| 29 | + * }, |
| 30 | + * }); |
| 31 | + * |
| 32 | + * const memory = memory_20250818(); |
| 33 | + * const llmWithMemory = llm.bindTools([memory]); |
| 34 | + * |
| 35 | + * const response = await llmWithMemory.invoke("Remember that I like Python"); |
| 36 | + * ``` |
| 37 | + * |
| 38 | + * @param options - Optional configuration for the memory tool (currently unused) |
| 39 | + * @returns The memory tool object that can be passed to `bindTools` |
| 40 | + * |
| 41 | + * @see https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/memory-tool |
| 42 | + */ |
| 43 | +export function memory_20250818( |
| 44 | + options?: MemoryTool20250818Options |
| 45 | +): DynamicStructuredTool { |
| 46 | + const memoryTool = tool( |
| 47 | + options?.execute as ( |
| 48 | + input: unknown, |
| 49 | + runtime: ToolRuntime<unknown, unknown> |
| 50 | + ) => string | Promise<string>, |
| 51 | + { |
| 52 | + name: "memory", |
| 53 | + description: "Memory tool", |
| 54 | + schema: { |
| 55 | + type: "object", |
| 56 | + properties: { |
| 57 | + command: { |
| 58 | + type: "string", |
| 59 | + enum: [ |
| 60 | + "view", |
| 61 | + "create", |
| 62 | + "str_replace", |
| 63 | + "insert", |
| 64 | + "delete", |
| 65 | + "rename", |
| 66 | + ], |
| 67 | + }, |
| 68 | + }, |
| 69 | + required: ["command"], |
| 70 | + }, |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + memoryTool.metadata = { |
| 75 | + providerToolDefinition: { |
| 76 | + type: "memory_20250818", |
| 77 | + name: "memory", |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + return memoryTool; |
| 82 | +} |
| 83 | + |
| 84 | +interface MemoryToolOptions { |
| 85 | + allowedPrefixes?: string[]; |
| 86 | + filesystem?: FileSystem; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * useful implementation |
| 91 | + */ |
| 92 | +export function memory(options?: MemoryToolOptions): DynamicStructuredTool { |
| 93 | + const filesystem: Record<string, FileData> = {}; |
| 94 | + return memory_20250818({ |
| 95 | + execute: async (args) => { |
| 96 | + const updates: Record<string, FileData | null> = {}; |
| 97 | + const fileSystem = |
| 98 | + options?.filesystem ?? |
| 99 | + new StateFileSystem(filesystem, options?.allowedPrefixes, (files) => { |
| 100 | + Object.assign(updates, files); |
| 101 | + }); |
| 102 | + const commandHandler = new CommandHandler(fileSystem); |
| 103 | + return await handleMemoryCommand(commandHandler, args); |
| 104 | + }, |
| 105 | + }); |
| 106 | +} |
0 commit comments