|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Mastra agent definitions for the `mastra` runtime-portability topic |
| 3 | +// (cockpit/runtimes/mastra/). This service IS that topic's backend — the |
| 4 | +// registry entry has no pythonDir, so unlike the Python lane there is no |
| 5 | +// per-example module staged into a generated deployment; the agent lives |
| 6 | +// here, next to the HTTP shim that serves it. |
| 7 | +// |
| 8 | +// The agent exercises every green cell of the measured matrix on one route: |
| 9 | +// - streaming chat (TEXT_MESSAGE_CHUNK) |
| 10 | +// - one backend tool (`check_conditions` → TOOL_CALL_* + TOOL_CALL_RESULT) |
| 11 | +// - shared state via working memory (STATE_SNAPSHOT + STATE_DELTA — Mastra |
| 12 | +// emits real JSON-Patch deltas, measured in the spike's 04a capture) |
| 13 | +// - suspend/resume human-in-the-loop (`reserve_campsite` → CUSTOM |
| 14 | +// on_interrupt + RUN_FINISHED outcome interrupt; resume arrives as |
| 15 | +// forwardedProps.command.interruptEvent{toolCallId,runId}) |
| 16 | +import { Agent } from '@mastra/core/agent'; |
| 17 | +import { Mastra } from '@mastra/core/mastra'; |
| 18 | +import { createTool } from '@mastra/core/tools'; |
| 19 | +import { Memory } from '@mastra/memory'; |
| 20 | +import { LibSQLStore } from '@mastra/libsql'; |
| 21 | +import { z } from 'zod'; |
| 22 | + |
| 23 | +/** Model string resolved by Mastra's model router. It honors the standard |
| 24 | + * OPENAI_API_KEY and OPENAI_BASE_URL env vars, which is what lets the |
| 25 | + * aimock e2e harness intercept calls without any code fork. */ |
| 26 | +const MODEL = 'openai/gpt-4o-mini'; |
| 27 | + |
| 28 | +const NIGHTLY_RATE_USD = 45; |
| 29 | + |
| 30 | +/** Deterministic backend tool — no external calls, stable for fixtures. */ |
| 31 | +const checkConditionsTool = createTool({ |
| 32 | + id: 'check_conditions', |
| 33 | + description: 'Check current trail and weather conditions for a location.', |
| 34 | + inputSchema: z.object({ location: z.string().describe('Park or trailhead name') }), |
| 35 | + outputSchema: z.object({ |
| 36 | + location: z.string(), |
| 37 | + forecast: z.string(), |
| 38 | + high_c: z.number(), |
| 39 | + low_c: z.number(), |
| 40 | + }), |
| 41 | + execute: async (inputData) => ({ |
| 42 | + location: inputData.location, |
| 43 | + forecast: 'Clear skies, light afternoon breeze', |
| 44 | + high_c: 18, |
| 45 | + low_c: 4, |
| 46 | + }), |
| 47 | +}); |
| 48 | + |
| 49 | +/** |
| 50 | + * Human-in-the-loop tool. First call suspends the run (persisted to LibSQL — |
| 51 | + * suspend/resume REQUIRES persistent storage, a spike finding); the frontend |
| 52 | + * shows an approval card and resumes with `{ approved: boolean }`. |
| 53 | + */ |
| 54 | +const reserveCampsiteTool = createTool({ |
| 55 | + id: 'reserve_campsite', |
| 56 | + description: |
| 57 | + 'Reserve a campsite. Requires explicit user approval: the tool pauses the run and shows the user a confirmation card before booking.', |
| 58 | + inputSchema: z.object({ |
| 59 | + site: z.string().describe('Campsite name'), |
| 60 | + nights: z.number().int().min(1).describe('Number of nights'), |
| 61 | + }), |
| 62 | + suspendSchema: z.object({ |
| 63 | + site: z.string(), |
| 64 | + nights: z.number(), |
| 65 | + total_usd: z.number(), |
| 66 | + }), |
| 67 | + resumeSchema: z.object({ |
| 68 | + approved: z.boolean().optional(), |
| 69 | + }), |
| 70 | + execute: async (inputData, context) => { |
| 71 | + const { resumeData, suspend } = context?.agent ?? {}; |
| 72 | + if (!resumeData) { |
| 73 | + return suspend?.({ |
| 74 | + site: inputData.site, |
| 75 | + nights: inputData.nights, |
| 76 | + total_usd: inputData.nights * NIGHTLY_RATE_USD, |
| 77 | + }); |
| 78 | + } |
| 79 | + if (resumeData.approved) { |
| 80 | + return `Reserved ${inputData.site} for ${inputData.nights} night(s) — total $${ |
| 81 | + inputData.nights * NIGHTLY_RATE_USD |
| 82 | + }. Confirmation TP-${String(inputData.nights).padStart(2, '0')}88.`; |
| 83 | + } |
| 84 | + return `Reservation for ${inputData.site} was declined by the user. Nothing was booked.`; |
| 85 | + }, |
| 86 | +}); |
| 87 | + |
| 88 | +/** |
| 89 | + * Build the Mastra instance for this service. |
| 90 | + * |
| 91 | + * @param {string} dbUrl LibSQL url (`file:/path/to/mastra.db`). File-backed |
| 92 | + * storage is required: Mastra persists suspended-run snapshots there, and |
| 93 | + * resume loads them back — an in-memory store would break resume across |
| 94 | + * HTTP requests (and across restarts on Railway, hence the volume). |
| 95 | + */ |
| 96 | +export function createMastra(dbUrl) { |
| 97 | + const store = (id) => new LibSQLStore({ id, url: dbUrl }); |
| 98 | + |
| 99 | + const tripAgent = new Agent({ |
| 100 | + id: 'mastra', |
| 101 | + name: 'mastra', |
| 102 | + instructions: `You are a terse camping trip planner. |
| 103 | +The packing list in working memory is the user's shared state: whenever the user adds, removes, or changes items (or starts a list), update working memory to match. 'items' is an array of {name, qty}. Never mention memory or the list mechanics. |
| 104 | +For questions about weather or trail conditions you MUST call check_conditions. |
| 105 | +When the user asks to reserve or book a campsite you MUST call reserve_campsite; after it resumes, confirm the outcome. |
| 106 | +Always answer in one short sentence.`, |
| 107 | + model: MODEL, |
| 108 | + tools: { |
| 109 | + check_conditions: checkConditionsTool, |
| 110 | + reserve_campsite: reserveCampsiteTool, |
| 111 | + }, |
| 112 | + memory: new Memory({ |
| 113 | + storage: store('mastra-topic-memory'), |
| 114 | + options: { |
| 115 | + workingMemory: { |
| 116 | + enabled: true, |
| 117 | + schema: z.object({ |
| 118 | + packing_list: z.object({ |
| 119 | + title: z.string().describe('Packing list title'), |
| 120 | + items: z |
| 121 | + .array(z.object({ name: z.string(), qty: z.number() })) |
| 122 | + .describe('All items on the list'), |
| 123 | + }), |
| 124 | + }), |
| 125 | + }, |
| 126 | + }, |
| 127 | + }), |
| 128 | + }); |
| 129 | + |
| 130 | + return new Mastra({ |
| 131 | + agents: { mastra: tripAgent }, |
| 132 | + storage: store('mastra-instance'), |
| 133 | + }); |
| 134 | +} |
0 commit comments