// An agent that gets an exe.dev VM of its own for each conversation. // From https://exe.dev/deploy/mastra/vm-agent.ts import { Agent } from '@mastra/core/agent'; import { MASTRA_THREAD_ID_KEY, type RequestContext } from '@mastra/core/request-context'; import { Workspace } from '@mastra/core/workspace'; import { parseMemoryRequestContext } from '@mastra/core/memory'; import { Memory } from '@mastra/memory'; import { ExeSandbox } from '../exe-sandbox'; // The memory thread this request belongs to. Calls without one share a VM named for 'default'. const thread = (requestContext: RequestContext) => String(requestContext.get(MASTRA_THREAD_ID_KEY) || parseMemoryRequestContext(requestContext)?.thread?.id || 'default'); export const vmAgent = new Agent({ id: 'vm-agent', name: 'VM Agent', description: 'Builds and runs things on an exe.dev VM of its own, one VM per conversation.', instructions: 'You have a Linux VM of your own for this conversation. Do the task by running commands on it, check that it works, and say what you did. When you serve something, give the link.', model: 'openai/gpt-6.1-sol', defaultOptions: { maxSteps: 50 }, memory: new Memory(), workspace: new Workspace({ id: 'vm-workspace', name: 'VM workspace', sandbox: ({ requestContext }) => new ExeSandbox({ id: thread(requestContext) }), sandboxCacheKey: ({ requestContext }) => thread(requestContext), // Resolving only names the VM; it is made when the first command runs. instructions: { dynamicSandbox: 'resolve' }, }), });