|
| 1 | +import { ChatAnthropic } from "@langchain/anthropic"; |
| 2 | +import { HumanMessage } from "@langchain/core/messages"; |
| 3 | + |
| 4 | +import { MemorySaver } from "@langchain/langgraph-checkpoint"; |
| 5 | +import { createAgent } from "./agents/middlewareAgent/index.js"; |
| 6 | +import { anthropicCodeExecutionMiddleware } from "./agents/middlewareAgent/middleware/anthropicCodeExecution.js"; |
| 7 | +import { |
| 8 | + downloadFileAnthropic, |
| 9 | + extractGeneratedFilesAnthropic, |
| 10 | + getFileMetadataAnthropic, |
| 11 | + uploadFileAnthropic, |
| 12 | +} from "./agents/middlewareAgent/middleware/anthropicHelpers.js"; |
| 13 | + |
| 14 | +// Initial setup |
| 15 | +const model = new ChatAnthropic({ |
| 16 | + model: "claude-sonnet-4-20250514", |
| 17 | +}); |
| 18 | +const client = model.createClient({}); |
| 19 | +const thread = { |
| 20 | + configurable: { |
| 21 | + thread_id: "test-123", |
| 22 | + }, |
| 23 | +}; |
| 24 | +const agent = createAgent({ |
| 25 | + model, |
| 26 | + middleware: [anthropicCodeExecutionMiddleware()], |
| 27 | + checkpointSaver: new MemorySaver(), |
| 28 | +}); |
| 29 | + |
| 30 | +// Upload the file to Anthropic |
| 31 | +const uploadedFile = await uploadFileAnthropic(client, "test_data.csv"); |
| 32 | + |
| 33 | +// First invocation - should create container and analyze uploaded data |
| 34 | +const response1 = await agent.invoke( |
| 35 | + { |
| 36 | + messages: new HumanMessage({ |
| 37 | + content: [ |
| 38 | + { |
| 39 | + type: "text", |
| 40 | + text: "Filter to just widget A", |
| 41 | + }, |
| 42 | + { type: "container_upload", file_id: uploadedFile.fileId }, |
| 43 | + ], |
| 44 | + }), |
| 45 | + }, |
| 46 | + thread |
| 47 | +); |
| 48 | +console.log("Response 1:", response1.messages); |
| 49 | + |
| 50 | +// Second invocation - should reuse container and previous analysis |
| 51 | +const response2 = await agent.invoke( |
| 52 | + { |
| 53 | + messages: new HumanMessage( |
| 54 | + "Turn that into a graph of sales and units over time." |
| 55 | + ), |
| 56 | + }, |
| 57 | + thread |
| 58 | +); |
| 59 | +console.log("Response 2:", response2.messages); |
| 60 | + |
| 61 | +// Extract and download generated files |
| 62 | +for (const fileId of extractGeneratedFilesAnthropic(response2)) { |
| 63 | + const metadata = await getFileMetadataAnthropic(client, fileId); |
| 64 | + await downloadFileAnthropic(client, fileId, metadata.filename); |
| 65 | + console.log(`Downloaded generated file: ${metadata.filename}`); |
| 66 | +} |
0 commit comments