@mastra/code-sdk 1.21.0-alpha.8 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ import { CODE_AGENT_ID, DEFAULT_OM_MODEL_ID, validateConfigDirName } from "./con
21
21
  import { resolveAgentStreamsOff, resolveExperimentalAgent, validateExperimentalAgent, wrapExperimentalAgent } from "./experimental-agent.js";
22
22
  import "./hooks/index.js";
23
23
  import { KnowledgeInspectorError, createKnowledgeInspector } from "./knowledge-inspector.js";
24
+ import { createSubmitPlanTool } from "./tools/submit-plan.js";
24
25
  import { LOCAL_KNOWLEDGE_ORG_ID, resolveKnowledgeSessionScopes } from "./knowledge-scope.js";
25
26
  import "./mcp/index.js";
26
27
  import { getAvailableOmPacks, resolveAutoOMModelId } from "./onboarding/packs.js";
@@ -32,22 +33,23 @@ import { PlanRejectionAbortProcessor } from "./processors/plan-rejection-abort.j
32
33
  import { createAmazonBedrockGateway } from "./providers/amazon-bedrock-gateway.js";
33
34
  import { setAuthStorage } from "./providers/claude-max.js";
34
35
  import { createGitHubCopilotGateway } from "./providers/github-copilot-gateway.js";
35
- import { HookManager } from "./hooks/manager.js";
36
36
  import { setAuthStorage as setAuthStorage$1 } from "./providers/github-copilot.js";
37
+ import { HookManager } from "./hooks/manager.js";
37
38
  import { createGoogleVertexGateway } from "./providers/google-vertex-gateway.js";
38
- import { createMcpManager } from "./mcp/manager.js";
39
39
  import { setAuthStorage as setAuthStorage$2 } from "./providers/kimi-coding.js";
40
+ import { createMcpManager } from "./mcp/manager.js";
40
41
  import { parseCodexModelSelection } from "./providers/model-ids.js";
41
42
  import { setAuthStorage as setAuthStorage$3 } from "./providers/openai-codex.js";
42
43
  import { setAuthStorage as setAuthStorage$4 } from "./providers/xai.js";
43
44
  import "./schedules/index.js";
44
45
  import { stateSchema } from "./schema.js";
46
+ import { mastraBrand } from "./theme-palette.js";
45
47
  import { assembleSchedulePrompt } from "./schedules/prompt.js";
46
48
  import { ThreadScheduler, scheduleSignalAttributes, shortScheduleId } from "./schedules/scheduler.js";
47
49
  import { runScript } from "./schedules/run-script.js";
48
50
  import { createScheduleTools } from "./schedules/tools.js";
49
- import { mastraBrand } from "./theme-palette.js";
50
51
  import { isThinkingLevelSetting } from "./thinking.js";
52
+ import "./tools/index.js";
51
53
  import { DiscardingScoresStorage } from "./utils/discarding-scores-storage.js";
52
54
  import { syncGateways } from "./utils/gateway-sync.js";
53
55
  import { openLocalObservabilityStore } from "./utils/local-observability.js";
@@ -979,6 +981,8 @@ async function createMastraCodeAgentController(config) {
979
981
  backgroundToolsEnabled,
980
982
  plansDir: config?.plansDir
981
983
  });
984
+ const registersSubmitPlanTool = !disabledBuiltinTools.has("submit_plan");
985
+ const controllerDisabledBuiltinTools = registersSubmitPlanTool ? [...config?.disableBuiltinTools ?? [], "submit_plan"] : config?.disableBuiltinTools;
982
986
  let controller;
983
987
  controller = new AgentController({
984
988
  id: "mastra-code",
@@ -1003,7 +1007,8 @@ async function createMastraCodeAgentController(config) {
1003
1007
  },
1004
1008
  agent: codeAgent,
1005
1009
  subagents,
1006
- ...config?.disableBuiltinTools?.length ? { disableBuiltinTools: config.disableBuiltinTools } : {},
1010
+ ...controllerDisabledBuiltinTools?.length ? { disableBuiltinTools: controllerDisabledBuiltinTools } : {},
1011
+ ...registersSubmitPlanTool ? { tools: { submit_plan: createSubmitPlanTool({ plansDir: config?.plansDir }) } } : {},
1007
1012
  resolveSubagentModel: (modelId, { requestContext }) => routesToOtherGateway(modelId, Object.values(controller.getMastra()?.listGateways() ?? {}), /* @__PURE__ */ new Set([
1008
1013
  mastraCodeGateway.id,
1009
1014
  amazonBedrockGateway.id,
@@ -3,4 +3,5 @@
3
3
  */
4
4
  export { createConfiguredWebTools, createParallelWebExtractTool, createParallelWebSearchTool, createWebExtractTool, createWebSearchTool, hasParallelKey, hasTavilyKey, resolveWebSearchProvider, } from './web-search.js';
5
5
  export { requestSandboxAccessTool } from './request-sandbox-access.js';
6
+ export { createSubmitPlanTool } from './submit-plan.js';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1,3 +1,4 @@
1
1
  import { createConfiguredWebTools, createParallelWebExtractTool, createParallelWebSearchTool, createWebExtractTool, createWebSearchTool, hasParallelKey, hasTavilyKey, resolveWebSearchProvider } from "./web-search.js";
2
2
  import { requestSandboxAccessTool } from "./request-sandbox-access.js";
3
- export { createConfiguredWebTools, createParallelWebExtractTool, createParallelWebSearchTool, createWebExtractTool, createWebSearchTool, hasParallelKey, hasTavilyKey, requestSandboxAccessTool, resolveWebSearchProvider };
3
+ import { createSubmitPlanTool } from "./submit-plan.js";
4
+ export { createConfiguredWebTools, createParallelWebExtractTool, createParallelWebSearchTool, createSubmitPlanTool, createWebExtractTool, createWebSearchTool, hasParallelKey, hasTavilyKey, requestSandboxAccessTool, resolveWebSearchProvider };
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Host-side `submit_plan` for Mastra Code sessions.
3
+ *
4
+ * The core builtin (`@mastra/core/tools`) is deliberately filesystem-free: it suspends with
5
+ * the plan file `path` and expects the host to validate that path, read the plan, and fill
6
+ * `title`/`plan` for approval rendering and history replay. This module is that host.
7
+ *
8
+ * Reading at suspend time is what keeps the approval card renderable: the plan body travels
9
+ * with the suspension, so the card does not depend on a later file read that can fail once
10
+ * the sandbox is recreated, the plans directory moves, or the file is edited after
11
+ * submission (which would make the approved plan differ from the reviewed one).
12
+ *
13
+ * Only the first pass (validate, read, suspend) lives here. The description, schemas, the
14
+ * resumed pass and the no-`suspend` fallback are the builtin's own, because Mastra Code's
15
+ * rejection abort, Factory rules and the TUI match the builtin's result text.
16
+ *
17
+ * Registered through the AgentController's `tools` option, with `submit_plan` added to its
18
+ * `disableBuiltinTools` so the filesystem-free builtin is removed rather than left to be
19
+ * replaced by name in the toolset merge.
20
+ */
21
+ declare const SUBMIT_PLAN_TOOL_ID = "submit_plan";
22
+ export interface SubmitPlanToolOptions {
23
+ /** Host-configured workspace-relative plans directory (e.g. `.artifacts/plans`). */
24
+ plansDir?: string;
25
+ }
26
+ export declare function createSubmitPlanTool(options?: SubmitPlanToolOptions): any;
27
+ export { SUBMIT_PLAN_TOOL_ID };
28
+ //# sourceMappingURL=submit-plan.d.ts.map
@@ -0,0 +1,104 @@
1
+ import { getLocalPlansDir, getLocalPlansRelativeDir, isPlanFilePath, parsePlanContent, resolvePlanPath } from "../utils/plans.js";
2
+ import * as path$1 from "path";
3
+ import { createTool, submitPlanTool } from "@mastra/core/tools";
4
+ // src/tools/submit-plan.ts
5
+ /**
6
+ * Host-side `submit_plan` for Mastra Code sessions.
7
+ *
8
+ * The core builtin (`@mastra/core/tools`) is deliberately filesystem-free: it suspends with
9
+ * the plan file `path` and expects the host to validate that path, read the plan, and fill
10
+ * `title`/`plan` for approval rendering and history replay. This module is that host.
11
+ *
12
+ * Reading at suspend time is what keeps the approval card renderable: the plan body travels
13
+ * with the suspension, so the card does not depend on a later file read that can fail once
14
+ * the sandbox is recreated, the plans directory moves, or the file is edited after
15
+ * submission (which would make the approved plan differ from the reviewed one).
16
+ *
17
+ * Only the first pass (validate, read, suspend) lives here. The description, schemas, the
18
+ * resumed pass and the no-`suspend` fallback are the builtin's own, because Mastra Code's
19
+ * rejection abort, Factory rules and the TUI match the builtin's result text.
20
+ *
21
+ * Registered through the AgentController's `tools` option, with `submit_plan` added to its
22
+ * `disableBuiltinTools` so the filesystem-free builtin is removed rather than left to be
23
+ * replaced by name in the toolset merge.
24
+ */
25
+ const SUBMIT_PLAN_TOOL_ID = "submit_plan";
26
+ /**
27
+ * Where to write a plan and which path to submit. Named in every rejection so the agent can
28
+ * correct itself in one step instead of guessing.
29
+ */
30
+ function plansDirGuidance(plansDir, plansDirAbs) {
31
+ return `Write the plan to "${plansDirAbs}/<name>.md" and submit "${plansDir}/<name>.md" (e.g. "${plansDir}/add-dark-mode.md").`;
32
+ }
33
+ function planPathError(detail, guidance) {
34
+ return `Cannot submit plan: ${detail} ${guidance}`;
35
+ }
36
+ function createSubmitPlanTool(options = {}) {
37
+ return createTool({
38
+ id: SUBMIT_PLAN_TOOL_ID,
39
+ description: submitPlanTool.description,
40
+ inputSchema: submitPlanTool.inputSchema,
41
+ suspendSchema: submitPlanTool.suspendSchema,
42
+ resumeSchema: submitPlanTool.resumeSchema,
43
+ execute: async (input, context) => {
44
+ const suspend = context?.agent?.suspend;
45
+ if (context?.agent?.resumeData !== void 0 || !suspend) return submitPlanTool.execute(input, context);
46
+ const submittedPath = input.path;
47
+ try {
48
+ const agentControllerCtx = context?.requestContext?.get("controller");
49
+ const state = agentControllerCtx?.getState();
50
+ const projectPath = state?.projectPath?.trim();
51
+ if (!projectPath) return {
52
+ content: "Cannot submit plan: this session has no workspace root, so the plan path cannot be resolved. Submit the plan once the session has a workspace, with the file inside the plans directory your instructions name.",
53
+ isError: true
54
+ };
55
+ const planOptions = {
56
+ factoryProjectId: state?.factoryProjectId,
57
+ plansDir: options.plansDir
58
+ };
59
+ const plansDir = getLocalPlansRelativeDir(planOptions);
60
+ const plansDirAbs = getLocalPlansDir(projectPath, planOptions);
61
+ const guidance = plansDirGuidance(plansDir, plansDirAbs);
62
+ if (!isPlanFilePath(projectPath, submittedPath, planOptions)) return {
63
+ content: planPathError(`"${submittedPath}" resolves to "${resolvePlanPath(projectPath, submittedPath) ?? submittedPath}", which is not a markdown file directly inside the plans directory "${plansDirAbs}".`, `${guidance} The plans directory is relative to the workspace root ("${projectPath}"), so it must not be prefixed with a repository directory name.`),
64
+ isError: true
65
+ };
66
+ const resolvedPath = resolvePlanPath(projectPath, submittedPath);
67
+ const relativePath = path$1.relative(projectPath, resolvedPath);
68
+ const readPath = relativePath && !relativePath.startsWith("..") && !path$1.isAbsolute(relativePath) ? relativePath : resolvedPath;
69
+ const filesystem = agentControllerCtx?.workspace?.filesystem ?? context?.workspace?.filesystem;
70
+ if (!filesystem) return {
71
+ content: planPathError(`the session workspace filesystem is not available, so "${resolvedPath}" could not be read.`, guidance),
72
+ isError: true
73
+ };
74
+ let raw;
75
+ try {
76
+ const content = await filesystem.readFile(readPath, { encoding: "utf-8" });
77
+ raw = typeof content === "string" ? content : content.toString("utf-8");
78
+ } catch (error) {
79
+ return {
80
+ content: planPathError(`"${resolvedPath}" could not be read (${error instanceof Error ? error.message : "unknown error"}).`, `${guidance} Write the plan file first, then submit its path.`),
81
+ isError: true
82
+ };
83
+ }
84
+ const parsed = parsePlanContent(raw);
85
+ if (parsed.plan.trim().length === 0) return {
86
+ content: planPathError(`"${resolvedPath}" is empty, so there is nothing to review.`, guidance),
87
+ isError: true
88
+ };
89
+ await suspend({
90
+ toolId: SUBMIT_PLAN_TOOL_ID,
91
+ path: submittedPath,
92
+ title: parsed.title,
93
+ plan: parsed.plan
94
+ });
95
+ } catch (error) {
96
+ return {
97
+ content: `Failed to submit plan: ${error instanceof Error ? error.message : "Unknown error"}`,
98
+ isError: true
99
+ };
100
+ }
101
+ }
102
+ });
103
+ }
104
+ export { SUBMIT_PLAN_TOOL_ID, createSubmitPlanTool };
@@ -34,6 +34,17 @@ export declare function savePlanToDisk(opts: {
34
34
  resourceId: string;
35
35
  plansDir?: string;
36
36
  }): Promise<void>;
37
+ /**
38
+ * Split plan markdown into its title and body.
39
+ *
40
+ * The leading `# <title>` heading (if present) is the title and the remaining content is
41
+ * the plan body. Used both when reading a plan from local disk and when a host reads one
42
+ * through a session workspace filesystem.
43
+ */
44
+ export declare function parsePlanContent(raw: string): {
45
+ title: string;
46
+ plan: string;
47
+ };
37
48
  /**
38
49
  * Read a plan markdown file by absolute path.
39
50
  *
@@ -49,13 +60,20 @@ export declare function readPlanFile(absPath: string): Promise<{
49
60
  * archive so approved plans are findable later. The local named plan file is left in
50
61
  * place so the user can review every plan made over time.
51
62
  *
63
+ * Pass `plan` — the text the user actually reviewed — whenever the caller has it.
64
+ * The file can be edited, moved or removed while the approval card is open, so
65
+ * re-reading it can archive something the user never approved. Reading the file
66
+ * stays as the fallback for callers with no reviewed text.
67
+ *
52
68
  * Returns the local plan filename (e.g. `add-dark-mode.md`), or `undefined` when there
53
- * was no plan file to approve.
69
+ * was no plan to approve.
54
70
  */
55
71
  export declare function approvePlanFile(opts: {
56
72
  planPath: string;
57
73
  title: string;
58
74
  resourceId: string;
59
75
  plansDir?: string;
76
+ /** The reviewed plan text. Preferred over re-reading the file at `planPath`. */
77
+ plan?: string;
60
78
  }): Promise<string | undefined>;
61
79
  //# sourceMappingURL=plans.d.ts.map
@@ -73,18 +73,13 @@ async function savePlanToDisk(opts) {
73
73
  await fs.writeFile(path.join(dir, filename), content, "utf-8");
74
74
  }
75
75
  /**
76
- * Read a plan markdown file by absolute path.
76
+ * Split plan markdown into its title and body.
77
77
  *
78
- * The leading `# <title>` heading (if present) is parsed as the title and the remaining
79
- * content is returned as the plan body. Returns `undefined` when the file does not exist.
78
+ * The leading `# <title>` heading (if present) is the title and the remaining content is
79
+ * the plan body. Used both when reading a plan from local disk and when a host reads one
80
+ * through a session workspace filesystem.
80
81
  */
81
- async function readPlanFile(absPath) {
82
- let raw;
83
- try {
84
- raw = await fs.readFile(absPath, "utf-8");
85
- } catch {
86
- return;
87
- }
82
+ function parsePlanContent(raw) {
88
83
  const lines = raw.split(/\r?\n/);
89
84
  const headingIndex = lines.findIndex((line) => line.trim().length > 0);
90
85
  const heading = headingIndex >= 0 ? lines[headingIndex] : void 0;
@@ -98,23 +93,45 @@ async function readPlanFile(absPath) {
98
93
  };
99
94
  }
100
95
  /**
96
+ * Read a plan markdown file by absolute path.
97
+ *
98
+ * The leading `# <title>` heading (if present) is parsed as the title and the remaining
99
+ * content is returned as the plan body. Returns `undefined` when the file does not exist.
100
+ */
101
+ async function readPlanFile(absPath) {
102
+ let raw;
103
+ try {
104
+ raw = await fs.readFile(absPath, "utf-8");
105
+ } catch {
106
+ return;
107
+ }
108
+ return parsePlanContent(raw);
109
+ }
110
+ /**
101
111
  * Approve the plan file at `planPath`: write a timestamped copy to the global plans
102
112
  * archive so approved plans are findable later. The local named plan file is left in
103
113
  * place so the user can review every plan made over time.
104
114
  *
115
+ * Pass `plan` — the text the user actually reviewed — whenever the caller has it.
116
+ * The file can be edited, moved or removed while the approval card is open, so
117
+ * re-reading it can archive something the user never approved. Reading the file
118
+ * stays as the fallback for callers with no reviewed text.
119
+ *
105
120
  * Returns the local plan filename (e.g. `add-dark-mode.md`), or `undefined` when there
106
- * was no plan file to approve.
121
+ * was no plan to approve.
107
122
  */
108
123
  async function approvePlanFile(opts) {
109
- const { planPath, title, resourceId, plansDir } = opts;
110
- const current = await readPlanFile(planPath);
111
- if (!current) return;
124
+ const { planPath, title, resourceId, plansDir, plan } = opts;
125
+ const reviewed = plan?.trim() ? plan : void 0;
126
+ const current = reviewed ? void 0 : await readPlanFile(planPath);
127
+ const approvedPlan = reviewed ?? current?.plan;
128
+ if (approvedPlan === void 0) return;
112
129
  await savePlanToDisk({
113
- title: title || current.title || "Implementation Plan",
114
- plan: current.plan,
130
+ title: title || current?.title || "Implementation Plan",
131
+ plan: approvedPlan,
115
132
  resourceId,
116
133
  plansDir
117
134
  });
118
135
  return path.basename(planPath);
119
136
  }
120
- export { approvePlanFile, getLocalPlansDir, getLocalPlansRelativeDir, getPlanFilename, getPlansDir, getSuggestedPlanRelativePath, isPlanFilePath, readPlanFile, resolvePlanPath, savePlanToDisk };
137
+ export { approvePlanFile, getLocalPlansDir, getLocalPlansRelativeDir, getPlanFilename, getPlansDir, getSuggestedPlanRelativePath, isPlanFilePath, parsePlanContent, readPlanFile, resolvePlanPath, savePlanToDisk };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/code-sdk",
3
- "version": "1.21.0-alpha.8",
3
+ "version": "1.21.0",
4
4
  "description": "Mastra Code SDK: the agent core behind Mastra Code (everything except the TUI) — build your own UIs and surfaces on top of it",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -64,23 +64,23 @@
64
64
  "vscode-languageserver-protocol": "^3.18.5",
65
65
  "yaml": "^2.9.1",
66
66
  "zod": "^4.6.7",
67
- "@mastra/agent-browser": "0.7.0-alpha.1",
68
- "@mastra/client-js": "1.61.2-alpha.7",
69
- "@mastra/duckdb": "1.19.2-alpha.0",
70
- "@mastra/fastembed": "1.4.0-alpha.1",
71
- "@mastra/github-signals": "0.5.4-alpha.0",
72
- "@mastra/libsql": "1.32.0-alpha.1",
73
- "@mastra/mcp": "2.11.0-alpha.1",
74
- "@mastra/memory": "1.41.3-alpha.3",
75
- "@mastra/observability": "1.24.3-alpha.1",
76
- "@mastra/parallel": "0.1.6-alpha.0",
77
- "@mastra/pg": "1.37.1-alpha.2",
78
- "@mastra/schema-compat": "1.4.1-alpha.0",
79
- "@mastra/stagehand": "0.4.1-alpha.0",
80
- "@mastra/tavily": "1.1.4-alpha.0",
81
- "@mastra/voice-deepgram": "0.13.5-alpha.0",
82
- "@mastra/voice-mistral": "0.1.4-alpha.0",
83
- "@mastra/voice-openai": "0.13.5-alpha.0"
67
+ "@mastra/agent-browser": "0.7.0",
68
+ "@mastra/client-js": "1.62.0",
69
+ "@mastra/duckdb": "1.19.2",
70
+ "@mastra/fastembed": "1.4.0",
71
+ "@mastra/github-signals": "0.5.4",
72
+ "@mastra/libsql": "1.32.0",
73
+ "@mastra/mcp": "2.11.0",
74
+ "@mastra/memory": "1.41.3",
75
+ "@mastra/observability": "1.24.3",
76
+ "@mastra/parallel": "0.1.6",
77
+ "@mastra/pg": "1.38.0",
78
+ "@mastra/schema-compat": "1.4.1",
79
+ "@mastra/stagehand": "0.4.1",
80
+ "@mastra/tavily": "1.1.4",
81
+ "@mastra/voice-deepgram": "0.13.5",
82
+ "@mastra/voice-mistral": "0.1.4",
83
+ "@mastra/voice-openai": "0.13.5"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@ai-sdk/provider": "^4.0.38",
@@ -92,10 +92,10 @@
92
92
  "tsx": "^4.23.15",
93
93
  "typescript": "^6.0.3",
94
94
  "vitest": "5.1.0",
95
- "@mastra/core": "1.86.0-alpha.7",
96
- "@internal/types-builder": "0.0.126",
97
- "@internal/lint": "0.0.151",
98
- "@internal/workspace-test-utils": "0.0.95"
95
+ "@internal/lint": "0.0.152",
96
+ "@internal/types-builder": "0.0.127",
97
+ "@internal/workspace-test-utils": "0.0.96",
98
+ "@mastra/core": "1.86.0"
99
99
  },
100
100
  "peerDependencies": {
101
101
  "@mastra/core": ">=1.86.0-0 <2.0.0-0"