Skip to content

Commit 6091646

Browse files
committed
feat: Enhance tools with improved context handling and logging
- Added a new `gitToolContextSchema` to define optional properties for Git tools, including `defaultBranch`, `allowForce`, `allowAmend`, `maxCommits`, and `timeout`. - Updated the `cwd` assignment in multiple Git tools to use nullish coalescing (`??`) for better handling of undefined values. - Improved timeout handling in the `gitBranchTool` to use a default value if not provided. - Refactored the `jsonToCsvTool` to introduce a new `JsonToCsvRequestContext` interface, allowing for better context management. - Removed redundant logging hooks in `jsonToCsvTool` and restructured the execution method for clarity. - Updated the `randomGeneratorTool` to use a more structured `RandomToolContext` interface, enhancing type safety and clarity. - Added detailed logging hooks for input and output events in `randomGeneratorTool`. - Introduced a new `ScraperToolContext` interface in `web-scraper-tool.ts` to manage context more effectively, including properties like `allowedDomains`, `userAgent`, and `timeout`. - Enhanced logging for various scraping tools, including detailed input and output logging for better traceability. - Added logging hooks for input and output events across multiple tools, including `htmlToMarkdownTool`, `linkExtractorTool`, and `dataExporterTool`, to improve monitoring and debugging capabilities.
1 parent 48b5949 commit 6091646

17 files changed

Lines changed: 2124 additions & 1071 deletions

src/mastra/tools/alpha-vantage.tool.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { InferUITool } from '@mastra/core/tools'
21
import { createTool } from '@mastra/core/tools'
2+
import type { InferUITool } from '@mastra/core/tools'
33
import { trace } from '@opentelemetry/api'
44
import { z } from 'zod'
55
import { log } from '../config/logger'
66

7+
import type { RequestContext } from '@mastra/core/request-context'
8+
79
/**
810
* Alpha Vantage Tools
911
*
@@ -14,6 +16,13 @@ import { log } from '../config/logger'
1416
* Requires ALPHA_VANTAGE_API_KEY environment variable
1517
*/
1618

19+
/**
20+
* Base Request Context for Alpha Vantage
21+
*/
22+
export interface AlphaVantageRequestContext extends RequestContext {
23+
apiKey?: string
24+
}
25+
1726
// In-memory counter to track tool calls per request
1827
// Add this line at the beginning of each tool's execute function to track usage:
1928
// toolCallCounters.set('tool-id', (toolCallCounters.get('tool-id') ?? 0) + 1)
@@ -83,6 +92,7 @@ export const alphaVantageCryptoTool = createTool({
8392
.optional(),
8493
}),
8594
execute: async (inputData, context) => {
95+
const requestContext = context?.requestContext as AlphaVantageRequestContext
8696
const span = trace
8797
.getTracer('alpha-vantage-crypto-tool', '1.0.0')
8898
.startSpan('alpha-vantage-crypto', {
@@ -103,7 +113,7 @@ export const alphaVantageCryptoTool = createTool({
103113
},
104114
id: 'alpha-vantage-crypto',
105115
})
106-
const apiKey = process.env.ALPHA_VANTAGE_API_KEY
116+
const apiKey = requestContext?.apiKey ?? process.env.ALPHA_VANTAGE_API_KEY
107117

108118
if (typeof apiKey !== 'string' || apiKey.trim() === '') {
109119
await context?.writer?.custom({
@@ -313,7 +323,7 @@ export const alphaVantageCryptoTool = createTool({
313323
})
314324
},
315325
onOutput: ({ output, toolCallId, toolName, abortSignal }) => {
316-
const dataKeys = output.data ? Object.keys(output.data).length : 0
326+
const dataKeys = (output.data !== null && typeof output.data === 'object') ? Object.keys(output.data as object).length : 0
317327
log.info('Alpha Vantage crypto completed', {
318328
toolCallId,
319329
toolName,
@@ -412,6 +422,7 @@ export const alphaVantageStockTool = createTool({
412422
}),
413423

414424
execute: async (inputData, context) => {
425+
const requestContext = context?.requestContext as AlphaVantageRequestContext
415426
const span = trace
416427
.getTracer('alpha-vantage-stock-tool', '1.0.0')
417428
.startSpan('alpha-vantage-stock', {
@@ -431,7 +442,7 @@ export const alphaVantageStockTool = createTool({
431442
},
432443
id: 'alpha-vantage-stock',
433444
})
434-
const apiKey = process.env.ALPHA_VANTAGE_API_KEY
445+
const apiKey = requestContext?.apiKey ?? process.env.ALPHA_VANTAGE_API_KEY
435446

436447
if (typeof apiKey !== 'string' || apiKey.trim() === '') {
437448
await context?.writer?.custom({
@@ -674,7 +685,7 @@ export const alphaVantageStockTool = createTool({
674685
})
675686
},
676687
onOutput: ({ output, toolCallId, toolName, abortSignal }) => {
677-
const dataKeys = output.data ? Object.keys(output.data).length : 0
688+
const dataKeys = (output.data !== null && typeof output.data === 'object') ? Object.keys(output.data as object).length : 0
678689
log.info('Alpha Vantage stock completed', {
679690
toolCallId,
680691
toolName,
@@ -804,6 +815,7 @@ export const alphaVantageTool = createTool({
804815
.optional(),
805816
}),
806817
execute: async (inputData, context) => {
818+
const requestContext = context?.requestContext as AlphaVantageRequestContext
807819
const span = trace
808820
.getTracer('alpha-vantage-tool', '1.0.0')
809821
.startSpan('alpha-vantage', {
@@ -823,7 +835,7 @@ export const alphaVantageTool = createTool({
823835
},
824836
id: 'alpha-vantage',
825837
})
826-
const apiKey = process.env.ALPHA_VANTAGE_API_KEY
838+
const apiKey = requestContext?.apiKey ?? process.env.ALPHA_VANTAGE_API_KEY
827839

828840
if (typeof apiKey !== 'string' || apiKey.trim() === '') {
829841
await context?.writer?.custom({

0 commit comments

Comments
 (0)