Skip to content

Commit 662cca8

Browse files
committed
feat: update tool execution signatures and improve TypeScript compatibility
- refactor: change import statements in write-note.ts to use namespace imports for consistency. - chore: update tsconfig.json to enhance type acquisition settings and include/exclude patterns for better type management. - docs: add context, design, PRD, and task documentation for fixing tool execute signature errors, outlining the problem, solution options, and affected files. - fix: remove explicit type annotations from execute function parameters in various tool files to resolve TypeScript errors related to optional properties. - feat: implement Upstash storage and vector query tools in upstash.ts for enhanced memory management and semantic search capabilities.
1 parent 908431e commit 662cca8

52 files changed

Lines changed: 7848 additions & 7365 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

eslint.config.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,20 @@ module.exports = [
9797
'lib/**/*.{tsx,ts}',
9898
'ui/**/*.{tsx,ts}',
9999
'hooks/**/*.{tsx,ts}',
100+
'tests/'
100101

101102

102103
],
103104
},
104105
{
105106
ignores: [
107+
"node_modules/**/*.d.ts",
106108
'dist/**',
109+
'.vscode/mcp.json',
110+
'.vscode',
111+
'memory-bank',
112+
'memory-bank/',
113+
'node_modules',
107114
'node_modules/**',
108115
'.mastra/**',
109116
'eslint.config.js',
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Context: Tool Execute Signature Fix
2+
3+
## Phase: READY FOR IMPLEMENTATION
4+
5+
## Session: 2025-12-02
6+
7+
### Problem Summary
8+
9+
All `createTool()` calls with explicit type annotations on the `execute` function parameter cause TypeScript errors because they declare optional properties (`writer`, `tracingContext`, `mastra`, `runtimeContext`) as required.
10+
11+
### Fix Pattern
12+
13+
Remove explicit type annotations from `execute` function parameters:
14+
15+
```typescript
16+
// BEFORE (broken):
17+
execute: async ({ context, writer, tracingContext }: {
18+
context: { url: string; ... },
19+
writer?: any,
20+
tracingContext?: TracingContext
21+
}) => {
22+
23+
// AFTER (fixed):
24+
execute: async ({ context, writer, tracingContext }) => {
25+
```
26+
27+
### Decisions Made
28+
29+
| Decision | Choice | Rationale |
30+
|----------|--------|-----------|
31+
| Fix approach | Remove type annotations | Let TS infer from ToolExecutionContext |
32+
| Scope | All tools with explicit annotations | Consistent fix across codebase |
33+
34+
### Files to Fix
35+
36+
- [ ] web-scraper-tool.ts (multiple tools inside)
37+
- [ ] alpha-vantage.tool.ts
38+
- [ ] arxiv.tool.ts
39+
- [ ] browser-tool.ts
40+
- [ ] copywriter-agent-tool.ts
41+
- [ ] data-file-manager.ts
42+
- [ ] document-chunking.tool.ts
43+
- [ ] editor-agent-tool.ts
44+
- [ ] extractLearningsTool.ts
45+
- [ ] jwt-auth.tool.ts
46+
- [ ] pdf-data-conversion.tool.ts
47+
48+
### Search Pattern to Find All Occurrences
49+
50+
```bash
51+
grep -rn "execute: async ({ context.*}: {" src/mastra/tools/
52+
```
53+
54+
### Next Steps
55+
56+
1. Run search to find all affected execute functions
57+
2. Remove explicit type annotations from each
58+
3. Run `npx tsc --noEmit` to verify
59+
4. Run tests to confirm functionality
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Design: Tool Execute Signature Fix
2+
3+
## Root Cause
4+
5+
The `createTool` function expects `execute` to receive a `ToolExecutionContext` object:
6+
7+
```typescript
8+
execute: (context: ToolExecutionContext<TInput, TOutput, TContext>) => Promise<TOutput>
9+
```
10+
11+
Where `ToolExecutionContext` has this shape:
12+
13+
```typescript
14+
interface ToolExecutionContext<TInput, TOutput, TContext> {
15+
context: z.infer<TInput>; // REQUIRED - the parsed input
16+
runtimeContext?: RuntimeContext; // OPTIONAL
17+
tracingContext?: TracingContext; // OPTIONAL
18+
writer?: StreamWriter; // OPTIONAL
19+
mastra?: Mastra; // OPTIONAL
20+
}
21+
```
22+
23+
## The Problem
24+
25+
**Current (WRONG):**
26+
27+
```typescript
28+
execute: async ({ context, writer, tracingContext }: {
29+
context: any;
30+
writer: any; // <-- Treated as REQUIRED
31+
tracingContext: any; // <-- Treated as REQUIRED
32+
}) => { ... }
33+
```
34+
35+
This explicit type annotation declares `writer` and `tracingContext` as required properties, conflicting with `ToolExecutionContext`.
36+
37+
## Solution Options
38+
39+
### Option A: Remove explicit type annotation (RECOMMENDED)
40+
41+
```typescript
42+
execute: async ({ context, writer, tracingContext }) => {
43+
await writer?.write({ ... }); // Still use optional chaining
44+
}
45+
```
46+
47+
TypeScript will infer the correct types from `ToolExecutionContext`.
48+
49+
### Option B: Mark properties as optional in annotation
50+
51+
```typescript
52+
execute: async ({ context, writer, tracingContext }: {
53+
context: any;
54+
writer?: any; // <-- Add ?
55+
tracingContext?: any; // <-- Add ?
56+
}) => { ... }
57+
```
58+
59+
### Option C: Use intermediate variable
60+
61+
```typescript
62+
execute: async (execContext) => {
63+
const { context, writer, tracingContext } = execContext;
64+
await writer?.write({ ... });
65+
}
66+
```
67+
68+
## Decision
69+
70+
**Option A** - Remove explicit type annotations.
71+
72+
- Cleanest solution
73+
- Lets TypeScript infer correct types
74+
- Requires fewest code changes
75+
- Already working in tools without explicit annotations
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# PRD: Fix Tool Execute Signature Errors
2+
3+
## Problem Statement
4+
5+
All tools in `src/mastra/tools/` have TypeScript compilation errors due to incorrect destructuring of the `execute` function parameter. The `ToolExecutionContext` type defines `writer`, `tracingContext`, `mastra`, and `runtimeContext` as **optional** properties, but the current code destructures them as required.
6+
7+
## Error Pattern
8+
9+
```
10+
Type '({ context, writer, tracingContext }: { context: any; writer: any; tracingContext: any; })'
11+
is not assignable to type '(context: ToolExecutionContext<...>)'
12+
Property 'writer' is optional in type 'ToolExecutionContext' but required in type '{ ... }'
13+
```
14+
15+
## Affected Files
16+
17+
- `alpha-vantage.tool.ts`
18+
- `arxiv.tool.ts`
19+
- `browser-tool.ts`
20+
- `copywriter-agent-tool.ts`
21+
- `data-file-manager.ts`
22+
- `document-chunking.tool.ts`
23+
- `editor-agent-tool.ts`
24+
- `extractLearningsTool.ts`
25+
- `jwt-auth.tool.ts`
26+
- `pdf-data-conversion.tool.ts`
27+
- `web-scraper-tool.ts`
28+
29+
## Goals
30+
31+
1. Fix all TypeScript errors in tools folder
32+
2. Maintain backward compatibility
33+
3. Preserve existing functionality
34+
35+
## Success Criteria
36+
37+
- [ ] `npx tsc --noEmit` passes with no errors in `src/mastra/tools/`
38+
- [ ] All tools execute correctly
39+
- [ ] Optional chaining (`?.`) still works for optional properties
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Tasks: Fix Tool Execute Signatures
2+
3+
## TOOL-FIX-001: Fix web-scraper-tool.ts
4+
5+
**Status:** Not Started
6+
**Effort:** S
7+
**Files:** `src/mastra/tools/web-scraper-tool.ts`
8+
9+
**Change Pattern:**
10+
11+
```typescript
12+
// FROM:
13+
execute: async ({ context, writer, tracingContext }: { context: {...}, writer?: any, tracingContext?: TracingContext }) => {
14+
15+
// TO:
16+
execute: async ({ context, writer, tracingContext }) => {
17+
```
18+
19+
**Acceptance:**
20+
21+
- GIVEN the tool file
22+
- WHEN compiled with `tsc --noEmit`
23+
- THEN no type errors
24+
25+
---
26+
27+
## TOOL-FIX-002: Fix alpha-vantage.tool.ts
28+
29+
**Status:** Not Started
30+
**Effort:** S
31+
**Files:** `src/mastra/tools/alpha-vantage.tool.ts`
32+
33+
---
34+
35+
## TOOL-FIX-003: Fix arxiv.tool.ts
36+
37+
**Status:** Not Started
38+
**Effort:** S
39+
**Files:** `src/mastra/tools/arxiv.tool.ts`
40+
41+
---
42+
43+
## TOOL-FIX-004: Fix browser-tool.ts
44+
45+
**Status:** Not Started
46+
**Effort:** S
47+
**Files:** `src/mastra/tools/browser-tool.ts`
48+
49+
---
50+
51+
## TOOL-FIX-005: Fix copywriter-agent-tool.ts
52+
53+
**Status:** Not Started
54+
**Effort:** S
55+
**Files:** `src/mastra/tools/copywriter-agent-tool.ts`
56+
57+
---
58+
59+
## TOOL-FIX-006: Fix data-file-manager.ts
60+
61+
**Status:** Not Started
62+
**Effort:** S
63+
**Files:** `src/mastra/tools/data-file-manager.ts`
64+
65+
---
66+
67+
## TOOL-FIX-007: Fix document-chunking.tool.ts
68+
69+
**Status:** Not Started
70+
**Effort:** S
71+
**Files:** `src/mastra/tools/document-chunking.tool.ts`
72+
73+
---
74+
75+
## TOOL-FIX-008: Fix editor-agent-tool.ts
76+
77+
**Status:** Not Started
78+
**Effort:** S
79+
**Files:** `src/mastra/tools/editor-agent-tool.ts`
80+
81+
---
82+
83+
## TOOL-FIX-009: Fix extractLearningsTool.ts
84+
85+
**Status:** Not Started
86+
**Effort:** S
87+
**Files:** `src/mastra/tools/extractLearningsTool.ts`
88+
89+
---
90+
91+
## TOOL-FIX-010: Fix jwt-auth.tool.ts
92+
93+
**Status:** Not Started
94+
**Effort:** S
95+
**Files:** `src/mastra/tools/jwt-auth.tool.ts`
96+
97+
---
98+
99+
## TOOL-FIX-011: Fix pdf-data-conversion.tool.ts
100+
101+
**Status:** Not Started
102+
**Effort:** S
103+
**Files:** `src/mastra/tools/pdf-data-conversion.tool.ts`
104+
105+
---
106+
107+
## TOOL-FIX-012: Validate all fixes
108+
109+
**Status:** Not Started
110+
**Effort:** S
111+
**Dependencies:** TOOL-FIX-001 through TOOL-FIX-011
112+
113+
**Verification:**
114+
115+
```bash
116+
npx tsc --noEmit
117+
npm test -- --grep tool
118+
```

src/client-stream-to-ai-sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createUIMessageStream } from "ai";
21
import { toAISdkFormat } from "@mastra/ai-sdk";
32
import type { ChunkType, MastraModelOutput } from "@mastra/core/stream";
3+
import { createUIMessageStream } from "ai";
44
import { mastra } from "./mastra";
55

66
// Client SDK agent stream
@@ -33,4 +33,4 @@ const uiMessageStream = createUIMessageStream({
3333

3434
for await (const part of uiMessageStream) {
3535
console.log(part);
36-
}
36+
}

src/mastra/agents/editorAgent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { googleAI } from '../config/google';
77
import { log } from '../config/logger';
88
import { pgMemory } from '../config/pg-storage';
99
import { responseQualityScorer, structureScorer, summaryQualityScorer, toneConsistencyScorer } from '../scorers';
10+
1011
export type UserTier = 'free' | 'pro' | 'enterprise'
1112
export type EditorRuntimeContext = {
1213
'user-tier': UserTier

src/mastra/agents/evaluationAgent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { UserTier } from './editorAgent';
12
import { Agent } from '@mastra/core/agent'
23
import { InternalSpans } from '@mastra/core/ai-tracing'
34
import { googleAIFlashLite } from '../config/google'
@@ -7,7 +8,6 @@ import { responseQualityScorer, structureScorer, taskCompletionScorer } from '..
78

89
export type UserTier = 'free' | 'pro' | 'enterprise'
910
export type EvaluationContext = {
10-
userId?: string
1111
'user-tier': UserTier
1212
language: 'en' | 'es' | 'ja' | 'fr'
1313
}
@@ -20,12 +20,13 @@ export const evaluationAgent = new Agent({
2020
description:
2121
'An expert evaluation agent. Your task is to evaluate whether search results are relevant to a research query.',
2222
instructions: ({ runtimeContext }) => {
23-
const userId = runtimeContext.get('userId')
23+
const UserTier = runtimeContext.get('user-tier') as UserTier;
24+
2425
return {
2526
role: 'system',
2627
content: `
2728
<role>
28-
User: ${userId ?? 'anonymous'}
29+
User: ${UserTier ?? 'anonymous'}
2930
You are an expert evaluation agent. Your task is to evaluate whether a given search result is relevant to a specific research query.
3031
</role>
3132

src/mastra/config/google.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { createGoogleGenerativeAI } from '@ai-sdk/google'
2-
import { withSupermemory } from "@supermemory/tools/ai-sdk"
32
import { logError } from './logger'
43
//import { GoogleAICacheManager } from '@google/generative-ai/server';
54
//import { GoogleVoice } from "@mastra/voice-google";

0 commit comments

Comments
 (0)