Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Last active May 7, 2025 07:16
Show Gist options
  • Save kuc-arc-f/d50a9d777eab089ac62ad53fcf730bed to your computer and use it in GitHub Desktop.
Save kuc-arc-f/d50a9d777eab089ac62ad53fcf730bed to your computer and use it in GitHub Desktop.
mastra.ai , example
import { google } from '@ai-sdk/google';
import { Agent } from '@mastra/core/agent';
import { Memory } from '@mastra/memory';
import { LibSQLStore } from '@mastra/libsql';
import { weatherTool } from '../tools';
import { getNumber } from '../tools/getNumber';
import { getSpreadSheet } from '../tools/getSpreadSheet';
import { getSheetSearchUp } from '../tools/getSheetSearchUp';
const MODEL_NAME = 'gemini-2.0-flash';
export const firstAgent = new Agent({
name: 'first-Agent',
instructions: `複数のtool に対応、表示機能、登録機能になります。`,
model: google(MODEL_NAME),
tools: { weatherTool, getNumber , getSpreadSheet , getSheetSearchUp },
memory: new Memory({
storage: new LibSQLStore({
url: 'file:../mastra.db', // path is relative to the .mastra/output directory
}),
options: {
lastMessages: 10,
semanticRecall: false,
threads: {
generateTitle: false,
},
},
}),
});
import { createTool } from '@mastra/core/tools';
import { z } from 'zod';
// getSheetSearchUp を使って、価格 100 JPY以上のリストを。markdown記法の表形式で表示して欲しい。
export const getSheetSearchUp = createTool({
id: 'getSheetSearchUp',
description: "購入品リスト、markdown記法の表形式で表示します。",
inputSchema: z.object({
price: z.number().min(1).describe("価格の範囲値の最小値").optional().default(6),
}),
execute: async ({ context }) => {
const sheetId = process.env.SPREADSHEET_ID_2;
const apiKey = process.env.GOOGLE_AUTH_API_KEY;
const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/シート1!A1:B100?key=${apiKey}`;
const response = await fetch(url);
if(response.ok === false){
throw new Error("Error, response <> OK:");
}
const json = await response.json();
let rowMd = "";
json.values.forEach((row, idx) => {
const price = Number(row[1]);
if(idx > 0 && price >= context.price) {
let target = "| " + row[0] + " | " + row[1] + " | " + "\n";
rowMd += target;
}
});
let text = `***
| NAME | PRICE |
|:----:|
${rowMd}
***
`;
//console.log(text);
return text;
},
});
import { Mastra } from '@mastra/core/mastra';
import { createLogger } from '@mastra/core/logger';
import { LibSQLStore } from '@mastra/libsql';
import { weatherWorkflow } from './workflows';
import { stepWorkflowWork } from './workflows/stepWorkflowWork';
import { weatherAgent } from './agents';
import { firstAgent } from './agents/firstAgent';
//console.log("SPREADSHEET_ID_2=", process.env.SPREADSHEET_ID_2);
//console.log("GOOGLE_AUTH_API_KEY=", process.env.GOOGLE_AUTH_API_KEY);
export const mastra = new Mastra({
workflows: { stepWorkflowWork },
agents: { weatherAgent, firstAgent },
storage: new LibSQLStore({
// stores telemetry, evals, ... into memory storage, if it needs to persist, change to file:../mastra.db
url: ":memory:",
}),
logger: createLogger({
name: 'Mastra',
level: 'info',
}),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment