-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Refactor tool imports and improve progress logging #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,7 +39,7 @@ async function* asyncIterableFromReadableStream<T>( | |||||||||||||||||
| try { | ||||||||||||||||||
| while (true) { | ||||||||||||||||||
| const { done, value } = await reader.read(); | ||||||||||||||||||
| if (done) break; | ||||||||||||||||||
| if (done) {break;} | ||||||||||||||||||
| yield value; | ||||||||||||||||||
| } | ||||||||||||||||||
| } finally { | ||||||||||||||||||
|
|
@@ -49,16 +49,16 @@ async function* asyncIterableFromReadableStream<T>( | |||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Creates a streaming Response for Next.js API routes using server-side Mastra agent. | ||||||||||||||||||
| * | ||||||||||||||||||
| * | ||||||||||||||||||
| * IMPORTANT: This should be used in API 5stra instance, | ||||||||||||||||||
| * not the client SDK. The client SDK (MastraClient) is for frontend use only. | ||||||||||||||||||
| * | ||||||||||||||||||
| * | ||||||||||||||||||
| * @example | ||||||||||||||||||
| * ```ts | ||||||||||||||||||
| * // app/api/chat/route.ts | ||||||||||||||||||
| * import { mastra } from "@/src/mastra"; | ||||||||||||||||||
| * import { createAgentStreamResponse } from "@/lib/server/agent-stream"; | ||||||||||||||||||
| * | ||||||||||||||||||
| * | ||||||||||||||||||
| * export async function POST(req: Request) { | ||||||||||||||||||
| * const { messages, agentId, threadId, resourceId, memory } = await req.json(); | ||||||||||||||||||
| * return createAgentStreamResponse(mastra, agentId, messages, { | ||||||||||||||||||
|
|
@@ -68,7 +68,7 @@ async function* asyncIterableFromReadableStream<T>( | |||||||||||||||||
| * }); | ||||||||||||||||||
| * } | ||||||||||||||||||
| * ``` | ||||||||||||||||||
| * | ||||||||||||||||||
| * | ||||||||||||||||||
| * @see https://mastra.ai/docs/frameworks/agentic-uis/ai-sdk | ||||||||||||||||||
| */ | ||||||||||||||||||
| export async function createAgentStreamResponse( | ||||||||||||||||||
|
|
@@ -78,7 +78,7 @@ export async function createAgentStreamResponse( | |||||||||||||||||
| options?: AgentStreamOptions | ||||||||||||||||||
| ): Promise<Response> { | ||||||||||||||||||
| const agent = mastra.getAgent(agentId); | ||||||||||||||||||
|
|
||||||||||||||||||
| const streamOptions = { | ||||||||||||||||||
| format: options?.format ?? "aisdk", | ||||||||||||||||||
| threadId: options?.threadId, | ||||||||||||||||||
|
|
@@ -100,12 +100,12 @@ export async function createAgentStreamResponse( | |||||||||||||||||
| const uiMessageStream = createUIMessageStream({ | ||||||||||||||||||
| execute: async ({ writer }) => { | ||||||||||||||||||
| const aiSdkResult = toAISdkFormat(stream, { from: "agent" }); | ||||||||||||||||||
|
|
||||||||||||||||||
| // Handle both ReadableStream and AsyncIterable | ||||||||||||||||||
| const iterable: AsyncIterable<unknown> = isReadableStream(aiSdkResult) | ||||||||||||||||||
| ? asyncIterableFromReadableStream(aiSdkResult) | ||||||||||||||||||
| : aiSdkResult; | ||||||||||||||||||
|
|
||||||||||||||||||
| for await (const value of iterable) { | ||||||||||||||||||
| writer.write(value as Parameters<typeof writer.write>[0]); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
@@ -120,11 +120,11 @@ export async function createAgentStreamResponse( | |||||||||||||||||
| * This export exists for backward compatibility only. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export async function createMastraStreamResponse( | ||||||||||||||||||
| _client: unknown, | ||||||||||||||||||
| _options: StreamToAISdkOptions | ||||||||||||||||||
| client: unknown, | ||||||||||||||||||
| options: StreamToAISdkOptions | ||||||||||||||||||
|
Comment on lines
+123
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: parameter names changed from
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: lib/client-stream-to-ai-sdk.ts
Line: 123:124
Comment:
**style:** parameter names changed from `_client` and `_options` to `client` and `options`, but they're still unused in the function that throws an error - consider keeping the underscore prefix to indicate intentionally unused parameters
```suggestion
_client: unknown,
_options: StreamToAISdkOptions
```
How can I resolve this? If you propose a fix, please make it concise.
Comment on lines
+123
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, check if the file exists and view the function context
wc -l lib/client-stream-to-ai-sdk.tsRepository: ssdeanx/AgentStack Length of output: 96 🏁 Script executed: # View the function containing lines 123-124 with context
sed -n '110,140p' lib/client-stream-to-ai-sdk.tsRepository: ssdeanx/AgentStack Length of output: 713 🏁 Script executed: # Check for linting configuration
ls -la | grep -E "(eslint|prettier|tsconfig)"
cat tsconfig.json | grep -A 5 "compilerOptions"Repository: ssdeanx/AgentStack Length of output: 437 🏁 Script executed: # Check if eslint is available and run it on the file
if command -v eslint &> /dev/null; then
echo "=== ESLint check ==="
eslint lib/client-stream-to-ai-sdk.ts 2>&1 | head -50
else
echo "ESLint not found"
fiRepository: ssdeanx/AgentStack Length of output: 78 🏁 Script executed: # View the complete tsconfig.json file
cat tsconfig.jsonRepository: ssdeanx/AgentStack Length of output: 1559 🏁 Script executed: # Check if there are any compilation errors or warnings
if command -v tsc &> /dev/null; then
echo "=== TypeScript compiler check ==="
tsc --noEmit 2>&1 | grep -i "client\|options\|unused" | head -20
else
echo "TypeScript compiler not available in sandbox"
fiRepository: ssdeanx/AgentStack Length of output: 106 🏁 Script executed: # Search for other uses of _client or _options pattern in the codebase to understand the convention
rg "_client|_options" --type ts --type tsx -B 2 -A 2 | head -50Repository: ssdeanx/AgentStack Length of output: 89 🏁 Script executed: # Double-check for unused variable warnings in the ESLint config
cat eslint.config.cjs | grep -A 5 -B 5 "no-unused"Repository: ssdeanx/AgentStack Length of output: 415 🏁 Script executed: # Check git history to understand if underscores were actually removed in this PR
git log --oneline -n 20 lib/client-stream-to-ai-sdk.ts 2>/dev/null || echo "git log not available"Repository: ssdeanx/AgentStack Length of output: 126 Add underscore prefix to unused function parameters to suppress ESLint warnings. The function parameters Change to: export async function createMastraStreamResponse(
_client: unknown,
_options: StreamToAISdkOptions
): Promise<Response> {🤖 Prompt for AI Agents
Comment on lines
+123
to
+124
|
||||||||||||||||||
| client: unknown, | |
| options: StreamToAISdkOptions | |
| _client: unknown, | |
| _options: StreamToAISdkOptions |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,7 +86,8 @@ import { telephoneGameWorkflow } from './workflows/telephone-game'; | |||||||||
| import { weatherWorkflow } from './workflows/weather-workflow'; | ||||||||||
| import { repoIngestionWorkflow } from './workflows/repo-ingestion-workflow'; | ||||||||||
| import { specGenerationWorkflow } from './workflows/spec-generation-workflow'; | ||||||||||
|
|
||||||||||
| import { ResearchRuntimeContext } from './agents/index'; | ||||||||||
|
||||||||||
| import { ResearchRuntimeContext } from './agents/index'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: unused imports - ResearchRuntimeContext and metadata are imported but never used
| import { ResearchRuntimeContext } from './agents/index'; | |
| import { metadata } from '../../app/docs/layout'; | |
| import { specGenerationWorkflow } from './workflows/spec-generation-workflow'; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/mastra/index.ts
Line: 89:90
Comment:
**syntax:** unused imports - `ResearchRuntimeContext` and `metadata` are imported but never used
```suggestion
import { specGenerationWorkflow } from './workflows/spec-generation-workflow';
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify if ResearchRuntimeContext and metadata imports are used
echo "=== File content (focusing on imports and usage) ==="
# Show the imports first
head -95 src/mastra/index.ts | tail -10
echo -e "\n=== Searching for ResearchRuntimeContext usage (excluding import) ==="
rg -n "ResearchRuntimeContext" src/mastra/index.ts
echo -e "\n=== Searching for metadata variable usage (excluding import and object keys) ==="
rg -n "metadata" src/mastra/index.ts | head -30Repository: ssdeanx/AgentStack
Length of output: 1017
Remove unused imports on lines 89-90.
The imports ResearchRuntimeContext and metadata are not used anywhere in this file. They should be removed to keep the codebase clean.
🤖 Prompt for AI Agents
In src/mastra/index.ts around lines 89-90, the imports "ResearchRuntimeContext"
and "metadata" are unused; remove these two named imports from the import
statements (or delete the whole import lines if they become empty) and save the
file so linters/formatters reflect the change; run the project linter/build to
confirm no remaining references.
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import appears unrelated to the PR purpose and seems to be unused. The metadata import from '../../app/docs/layout' should be removed unless it's being used elsewhere in this file.
| import { metadata } from '../../app/docs/layout'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Network memory thread metadata and save-per-step behavior may not match the intended semantics.
Two specific checks for this networkRoute config:
metadata: { agentId: ':agentId' }will persist the literal string":agentId", not the route param value. If you plan to readagentIdfrom metadata later, this likely needs to be the resolved param.- The previous config had
savePerStep: trueindefaultOptions, but that’s now missing. If per‑step persistence is still required for network runs, please re‑add it; otherwise this is an intentional behavioral change that may affect any code assuming step‑level saves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Verify the high maxSteps value is intentional.
The maxSteps: 200 for the network route is 4x higher than the maxSteps: 50 used for chat routes. While networks may legitimately require more steps, this high value could impact API latency, resource consumption, and cost. Please verify this is intentional and consider documenting the rationale.
🤖 Prompt for AI Agents
In src/mastra/index.ts around line 248, the network route sets maxSteps: 200
which is much higher than the chat routes' maxSteps: 50; verify whether 200 is
intentional and either (A) if it is intentional, add an inline comment/docstring
near this setting explaining the rationale (why networks need 4x steps and the
expected trade-offs for latency/cost), or (B) if unintentional, reduce the value
to the intended threshold (e.g., 50 or a shared constant) and update any related
tests/configs to use the consistent maxSteps constant; ensure the final change
either documents the reason or consolidates the value into a shared constant for
clarity.
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The workflow routes have been significantly reorganized and simplified in a way that appears unrelated to the PR's stated purpose of "refactoring tool imports and improving progress logging". The change from specific workflow paths (e.g., /workflow/weatherWorkflow) to a generic path pattern (/workflow/:workflowId) and the removal of workflow-specific route definitions is a major API design change that should be in a separate PR with proper discussion. This could break existing clients expecting the old routes.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,19 @@ | |
| * Not working | ||
| */ | ||
| import { AISpanType, InternalSpans } from '@mastra/core/ai-tracing'; | ||
| import { InferUITool, createTool } from "@mastra/core/tools"; | ||
| import type { InferUITool} from "@mastra/core/tools"; | ||
| import { createTool } from "@mastra/core/tools"; | ||
| import { MDocument } from '@mastra/rag'; | ||
| import { Browser, chromium } from 'playwright-core'; | ||
| import type { Browser} from 'playwright-core'; | ||
| import { chromium } from 'playwright-core'; | ||
| import { z } from 'zod'; | ||
| import { log } from '../config/logger'; | ||
|
|
||
| // Browser instance cache for reuse | ||
| let browserInstance: Browser | null = null; | ||
|
|
||
| async function getBrowser(): Promise<Browser> { | ||
| if (!browserInstance || !browserInstance.isConnected()) { | ||
| if (!browserInstance?.isConnected()) { | ||
| browserInstance = await chromium.launch({ | ||
| headless: true, | ||
| chromiumSandbox: false, | ||
|
|
@@ -423,7 +425,7 @@ export const extractTablesTool = createTool({ | |
| bodyRows.forEach(row => { | ||
| const rowData: string[] = []; | ||
| row.querySelectorAll('td').forEach(cell => rowData.push(cell.textContent?.trim() ?? '')); | ||
| if (rowData.length > 0) rows.push(rowData); | ||
| if (rowData.length > 0) {rows.push(rowData);} | ||
| }); | ||
|
|
||
| result.push({ headers, rows }); | ||
|
|
@@ -480,7 +482,7 @@ export const monitorPageTool = createTool({ | |
| const selector = context.selector ?? 'body'; | ||
|
|
||
| await page.goto(context.url, { waitUntil: 'domcontentloaded' }); | ||
| let previousContent = await page.$eval(selector, el => el.textContent ?? ''); | ||
| const previousContent = await page.$eval(selector, el => el.textContent ?? ''); | ||
|
||
|
|
||
| let checkCount = 0; | ||
| let changed = false; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Use idiomatic brace formatting.
The formatting
if (done) {break;}is unusual. For consistency with common style guides, either omit braces for single statements or use multi-line formatting.Apply this diff for the single-statement style (more common in TypeScript):
Or use multi-line formatting if braces are required by your linter:
📝 Committable suggestion
🤖 Prompt for AI Agents