Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/mastra/tools/alpha-vantage.tool.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { InferUITool } from '@mastra/core/tools'
import { createTool } from '@mastra/core/tools'
import type { InferUITool } from '@mastra/core/tools'
import { trace } from '@opentelemetry/api'
import { z } from 'zod'
import { log } from '../config/logger'

import type { RequestContext } from '@mastra/core/request-context'

/**
* Alpha Vantage Tools
*
Expand All @@ -14,6 +16,13 @@ import { log } from '../config/logger'
* Requires ALPHA_VANTAGE_API_KEY environment variable
*/

/**
* Base Request Context for Alpha Vantage
*/
export interface AlphaVantageRequestContext extends RequestContext {
apiKey?: string
}

// In-memory counter to track tool calls per request
// Add this line at the beginning of each tool's execute function to track usage:
// toolCallCounters.set('tool-id', (toolCallCounters.get('tool-id') ?? 0) + 1)
Expand Down Expand Up @@ -83,6 +92,7 @@ export const alphaVantageCryptoTool = createTool({
.optional(),
}),
execute: async (inputData, context) => {
const requestContext = context?.requestContext as AlphaVantageRequestContext
const span = trace
.getTracer('alpha-vantage-crypto-tool', '1.0.0')
.startSpan('alpha-vantage-crypto', {
Expand All @@ -103,7 +113,7 @@ export const alphaVantageCryptoTool = createTool({
},
id: 'alpha-vantage-crypto',
})
const apiKey = process.env.ALPHA_VANTAGE_API_KEY
const apiKey = requestContext?.apiKey ?? process.env.ALPHA_VANTAGE_API_KEY

if (typeof apiKey !== 'string' || apiKey.trim() === '') {
await context?.writer?.custom({
Expand Down Expand Up @@ -313,7 +323,7 @@ export const alphaVantageCryptoTool = createTool({
})
},
onOutput: ({ output, toolCallId, toolName, abortSignal }) => {
const dataKeys = output.data ? Object.keys(output.data).length : 0
const dataKeys = (output.data !== null && typeof output.data === 'object') ? Object.keys(output.data as object).length : 0
log.info('Alpha Vantage crypto completed', {
toolCallId,
toolName,
Expand Down Expand Up @@ -412,6 +422,7 @@ export const alphaVantageStockTool = createTool({
}),

execute: async (inputData, context) => {
const requestContext = context?.requestContext as AlphaVantageRequestContext
const span = trace
.getTracer('alpha-vantage-stock-tool', '1.0.0')
.startSpan('alpha-vantage-stock', {
Expand All @@ -431,7 +442,7 @@ export const alphaVantageStockTool = createTool({
},
id: 'alpha-vantage-stock',
})
const apiKey = process.env.ALPHA_VANTAGE_API_KEY
const apiKey = requestContext?.apiKey ?? process.env.ALPHA_VANTAGE_API_KEY

if (typeof apiKey !== 'string' || apiKey.trim() === '') {
await context?.writer?.custom({
Expand Down Expand Up @@ -674,7 +685,7 @@ export const alphaVantageStockTool = createTool({
})
},
onOutput: ({ output, toolCallId, toolName, abortSignal }) => {
const dataKeys = output.data ? Object.keys(output.data).length : 0
const dataKeys = (output.data !== null && typeof output.data === 'object') ? Object.keys(output.data as object).length : 0
log.info('Alpha Vantage stock completed', {
toolCallId,
toolName,
Expand Down Expand Up @@ -804,6 +815,7 @@ export const alphaVantageTool = createTool({
.optional(),
}),
execute: async (inputData, context) => {
const requestContext = context?.requestContext as AlphaVantageRequestContext
const span = trace
.getTracer('alpha-vantage-tool', '1.0.0')
.startSpan('alpha-vantage', {
Expand All @@ -823,7 +835,7 @@ export const alphaVantageTool = createTool({
},
id: 'alpha-vantage',
})
const apiKey = process.env.ALPHA_VANTAGE_API_KEY
const apiKey = requestContext?.apiKey ?? process.env.ALPHA_VANTAGE_API_KEY

if (typeof apiKey !== 'string' || apiKey.trim() === '') {
await context?.writer?.custom({
Expand Down
Loading
Loading