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
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/mastra/app/lib/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ping = () => 'pong';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default { name: 'bot' };
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { ping } from '../../../../lib/ping.ts';
export const pingTool = ping;
2 changes: 2 additions & 0 deletions packages/knip/fixtures/plugins/mastra/app/mastra/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Mastra } from '@mastra/core';
export const mastra = new Mastra({});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const extra = 3;
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/mastra/lib/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const shared = 1;
2 changes: 2 additions & 0 deletions packages/knip/fixtures/plugins/mastra/lib/tools/alpha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { shared } from '../shared.ts';
export const alpha = shared;
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/mastra/lib/tools/beta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const beta = 2;
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/mastra/lib/unused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const unused = 0;
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions packages/knip/fixtures/plugins/mastra/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@plugins/mastra",
"type": "module",
"scripts": {
"dev": "mastra dev",
"build": "mastra build",
"dev:app": "mastra dev --dir app/mastra",
"build:tools": "mastra build -t lib/tools/*.ts,lib/extra-tools",
"start": "mastra start --dir .mastra/output"
},
"dependencies": {
"@mastra/core": "*",
"@mastra/libsql": "*",
"@mastra/memory": "*",
"zod": "*"
},
"devDependencies": {
"mastra": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { agentConfig } from '@mastra/core/agent';

export default agentConfig({ model: 'openai/gpt-5' });
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You answer support questions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const searchIndex = (query: string) => [query];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const formatAnswer = (text: string) => text.trim();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Memory } from '@mastra/memory';

export default new Memory();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
id: 'redact',
processInput: async ({ messages }: { messages: unknown[] }) => messages,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineSchedule } from '@mastra/core/schedules';

export default defineSchedule({ cron: '0 9 * * *', prompt: 'Review overdue invoices.' });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { agentConfig } from '@mastra/core/agent';

export default agentConfig({ model: 'openai/gpt-5-mini' });
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createTool } from '@mastra/core/tools';
import { z } from 'zod';

export default createTool({
id: 'classify',
description: 'Classify a ticket',
inputSchema: z.object({ ticket: z.string() }),
execute: async () => ({ label: 'billing' }),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createTool } from '@mastra/core/tools';
import { z } from 'zod';
import { searchIndex } from '../lib/docs-index.ts';

export default createTool({
id: 'search_docs',
description: 'Search the documentation',
inputSchema: z.object({ query: z.string() }),
execute: async ({ query }) => searchIndex(query),
});
3 changes: 3 additions & 0 deletions packages/knip/fixtures/plugins/mastra/src/mastra/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Mastra } from '@mastra/core';

export const mastra = new Mastra({});
3 changes: 3 additions & 0 deletions packages/knip/fixtures/plugins/mastra/src/mastra/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LibSQLStore } from '@mastra/libsql';

export default new LibSQLStore({ id: 'storage', url: 'file:./mastra.db' });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { weatherTool } from './weather-tool.ts';

console.log(weatherTool.id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createTool } from '@mastra/core/tools';
import { z } from 'zod';

export const weatherTool = createTool({
id: 'get-weather',
description: 'Get current weather for a location',
inputSchema: z.object({ location: z.string() }),
execute: async () => ({ temperature: 21 }),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createWorkflow } from '@mastra/core/workflows';
import { z } from 'zod';

export default createWorkflow({
id: 'forecast',
inputSchema: z.object({ city: z.string() }),
outputSchema: z.object({ summary: z.string() }),
}).commit();
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,10 @@
"title": "marko plugin configuration (https://knip.dev/reference/plugins/marko)",
"$ref": "#/definitions/plugin"
},
"mastra": {
"title": "mastra plugin configuration (https://knip.dev/reference/plugins/mastra)",
"$ref": "#/definitions/plugin"
},
"mcp": {
"title": "mcp plugin configuration (https://knip.dev/reference/plugins/mcp)",
"$ref": "#/definitions/plugin"
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { default as lostPixel } from './lost-pixel/index.ts';
import { default as lunaria } from './lunaria/index.ts';
import { default as markdownlint } from './markdownlint/index.ts';
import { default as marko } from './marko/index.ts';
import { default as mastra } from './mastra/index.ts';
import { default as mcp } from './mcp/index.ts';
import { default as mdx } from './mdx/index.ts';
import { default as mdxlint } from './mdxlint/index.ts';
Expand Down Expand Up @@ -268,6 +269,7 @@ export const Plugins = {
lunaria,
markdownlint,
marko,
mastra,
mcp,
mdx,
mdxlint,
Expand Down
76 changes: 76 additions & 0 deletions packages/knip/src/plugins/mastra/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { join } from '../../util/path.ts';
import type { Args } from '../../types/args.ts';
import type { IsPluginEnabled, Plugin } from '../../types/config.ts';
import { isDirectory } from '../../util/fs.ts';
import { toProductionEntry } from '../../util/input.ts';
import { hasDependency } from '../../util/plugin.ts';

// https://mastra.ai/reference/project-structure
// https://mastra.ai/docs/develop
// https://github.com/mastra-ai/mastra/blob/368336f3519ffed7c7b0abdef272249bcd6ce589/packages/deployer/src/build/fs-routing/discover.ts
// https://github.com/mastra-ai/mastra/blob/368336f3519ffed7c7b0abdef272249bcd6ce589/packages/deployer/src/bundler/index.ts#L529-L597

const title = 'Mastra';

const enablers = ['mastra'];

const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);

const extensions = '{js,ts}';

const defaultDir = 'src/mastra';

// `mastra dev` and `mastra build` discover the entry file and the file-based agents, tools and workflows
// from the Mastra folder (`src/mastra`, or `--dir`). `mastra start --dir` points at the build output instead.
const discoveryCommands = new Set(['dev', 'build']);

const getProductionPatterns = (dir: string) => {
const agentRoots = `${dir}/agents/*/*(subagents/*/)`;
return [
`${dir}/index.${extensions}`,
`${dir}/{storage,observability,logger,server,studio}.{js,ts,mjs,mts}`,
`${dir}/workflows/*.${extensions}`,
`${dir}/{,agents/*/}tools/**/*.${extensions}`,
`${agentRoots}{config,instructions,memory,workspace}.${extensions}`,
`${agentRoots}{tools,scorers,skills,processors/input,processors/output}/*.${extensions}`,
`${agentRoots}schedules/**/*.${extensions}`,
`!${dir}/**/*.{test,spec}.${extensions}`,
`!${dir}/**/__tests__/**`,
];
};

const production = getProductionPatterns(defaultDir);

const args: Args = {
binaries: ['mastra'],
string: ['dir', 'tools'],
alias: { dir: ['d'], tools: ['t'] },
resolveInputs: (parsed, { cwd }) => {
if (!discoveryCommands.has(parsed._[0])) return [];
const inputs = [];
if (parsed.dir) {
for (const pattern of getProductionPatterns(parsed.dir)) inputs.push(toProductionEntry(pattern));
}
if (parsed.tools) {
// `--tools` takes a comma-separated list of tool files, globs or directories; Mastra bundles each match
// as its own input and imports them all from the generated `tools.mjs`, so each is an entry file.
for (const tool of String(parsed.tools).split(',')) {
const specifier = tool.trim();
if (!specifier) continue;
const pattern = isDirectory(join(cwd, specifier)) ? `${specifier}/index.${extensions}` : specifier;
inputs.push(toProductionEntry(pattern));
}
}
return inputs;
},
};

const plugin: Plugin = {
title,
enablers,
isEnabled,
production,
args,
};

export default plugin;
1 change: 1 addition & 0 deletions packages/knip/src/schema/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const pluginsSchema = z.object({
lunaria: pluginSchema,
markdownlint: pluginSchema,
marko: pluginSchema,
mastra: pluginSchema,
mcp: pluginSchema,
mdx: pluginSchema,
mdxlint: pluginSchema,
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/src/types/PluginNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type PluginName =
| 'lunaria'
| 'markdownlint'
| 'marko'
| 'mastra'
| 'mcp'
| 'mdx'
| 'mdxlint'
Expand Down Expand Up @@ -269,6 +270,7 @@ export const pluginNames = [
'lunaria',
'markdownlint',
'marko',
'mastra',
'mcp',
'mdx',
'mdxlint',
Expand Down
34 changes: 34 additions & 0 deletions packages/knip/test/plugins/mastra.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { main } from '../../src/index.ts';
import baseCounters from '../helpers/baseCounters.ts';
import { createOptions } from '../helpers/create-options.ts';
import { resolve } from '../helpers/resolve.ts';

const cwd = resolve('fixtures/plugins/mastra');

test('Find dependencies with the Mastra plugin', async () => {
const options = await createOptions({ cwd });
const { issues, counters } = await main(options);

assert('src/mastra/agents/support/lib/format.ts' in issues.files);
assert('src/mastra/tools/weather-tool.test.ts' in issues.files);
assert('lib/unused.ts' in issues.files);

// `mastra dev --dir app/mastra` discovers that folder instead of `src/mastra`
assert(!('app/mastra/index.ts' in issues.files));
assert(!('app/mastra/agents/bot/tools/ping.ts' in issues.files));
assert(!('app/lib/ping.ts' in issues.files));

// `mastra build -t lib/tools/*.ts,lib/extra-tools` bundles each match as a tool entry
assert(!('lib/tools/alpha.ts' in issues.files));
assert(!('lib/shared.ts' in issues.files));
assert(!('lib/extra-tools/index.ts' in issues.files));

assert.deepEqual(counters, {
...baseCounters,
files: 3,
processed: 23,
total: 23,
});
});
Loading