@mastra/code-sdk 1.7.3-alpha.0 → 1.8.0-alpha.10
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/agent-connections/tools.d.ts.map +1 -1
- package/dist/agent-connections/tools.js +26 -8
- package/dist/agent-connections/tools.js.map +1 -1
- package/dist/agents/background-completion-events.d.ts +16 -0
- package/dist/agents/background-completion-events.d.ts.map +1 -0
- package/dist/agents/background-completion-events.js +19 -0
- package/dist/agents/background-completion-events.js.map +1 -0
- package/dist/agents/background-completion.d.ts +9 -0
- package/dist/agents/background-completion.d.ts.map +1 -0
- package/dist/agents/background-completion.js +59 -0
- package/dist/agents/background-completion.js.map +1 -0
- package/dist/agents/mastracode-gateway.d.ts +2 -0
- package/dist/agents/mastracode-gateway.d.ts.map +1 -1
- package/dist/agents/mastracode-gateway.js +29 -10
- package/dist/agents/mastracode-gateway.js.map +1 -1
- package/dist/agents/memory.d.ts +1 -1
- package/dist/agents/memory.d.ts.map +1 -1
- package/dist/agents/memory.js +32 -25
- package/dist/agents/memory.js.map +1 -1
- package/dist/agents/model.d.ts +41 -1
- package/dist/agents/model.d.ts.map +1 -1
- package/dist/agents/model.js +141 -7
- package/dist/agents/model.js.map +1 -1
- package/dist/agents/tool-availability.d.ts +2 -0
- package/dist/agents/tool-availability.d.ts.map +1 -1
- package/dist/agents/tool-availability.js +33 -4
- package/dist/agents/tool-availability.js.map +1 -1
- package/dist/agents/tools.d.ts +1 -1
- package/dist/agents/tools.d.ts.map +1 -1
- package/dist/agents/tools.js +30 -2
- package/dist/agents/tools.js.map +1 -1
- package/dist/agents/workspace.d.ts +2 -1
- package/dist/agents/workspace.d.ts.map +1 -1
- package/dist/agents/workspace.js +3 -3
- package/dist/agents/workspace.js.map +1 -1
- package/dist/auth/account-rotation-processor.d.ts +207 -0
- package/dist/auth/account-rotation-processor.d.ts.map +1 -0
- package/dist/auth/account-rotation-processor.js +669 -0
- package/dist/auth/account-rotation-processor.js.map +1 -0
- package/dist/auth/account-routing-context.d.ts +13 -0
- package/dist/auth/account-routing-context.d.ts.map +1 -0
- package/dist/auth/account-routing-context.js +41 -0
- package/dist/auth/account-routing-context.js.map +1 -0
- package/dist/auth/index.d.ts +1 -0
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +2 -1
- package/dist/auth/providers/anthropic.d.ts.map +1 -1
- package/dist/auth/providers/anthropic.js +2 -8
- package/dist/auth/providers/anthropic.js.map +1 -1
- package/dist/auth/providers/github-copilot.d.ts.map +1 -1
- package/dist/auth/providers/github-copilot.js +34 -12
- package/dist/auth/providers/github-copilot.js.map +1 -1
- package/dist/auth/providers/kimi-coding.d.ts.map +1 -1
- package/dist/auth/providers/kimi-coding.js +2 -6
- package/dist/auth/providers/kimi-coding.js.map +1 -1
- package/dist/auth/providers/openai-codex.d.ts +3 -1
- package/dist/auth/providers/openai-codex.d.ts.map +1 -1
- package/dist/auth/providers/openai-codex.js +50 -24
- package/dist/auth/providers/openai-codex.js.map +1 -1
- package/dist/auth/providers/xai.d.ts +1 -1
- package/dist/auth/providers/xai.d.ts.map +1 -1
- package/dist/auth/providers/xai.js +36 -16
- package/dist/auth/providers/xai.js.map +1 -1
- package/dist/auth/storage.d.ts +130 -8
- package/dist/auth/storage.d.ts.map +1 -1
- package/dist/auth/storage.js +602 -39
- package/dist/auth/storage.js.map +1 -1
- package/dist/auth/types.d.ts +69 -2
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/goal-signal.d.ts +1 -1
- package/dist/goal-signal.d.ts.map +1 -1
- package/dist/goal-signal.js +1 -1
- package/dist/goal-signal.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +155 -38
- package/dist/index.js.map +1 -1
- package/dist/mcp/config.d.ts +17 -2
- package/dist/mcp/config.d.ts.map +1 -1
- package/dist/mcp/config.js +18 -3
- package/dist/mcp/config.js.map +1 -1
- package/dist/mcp/manager.d.ts +2 -1
- package/dist/mcp/manager.d.ts.map +1 -1
- package/dist/mcp/manager.js +13 -8
- package/dist/mcp/manager.js.map +1 -1
- package/dist/mcp/types.d.ts +15 -3
- package/dist/mcp/types.d.ts.map +1 -1
- package/dist/onboarding/custom-packs.d.ts.map +1 -1
- package/dist/onboarding/custom-packs.js +3 -0
- package/dist/onboarding/custom-packs.js.map +1 -1
- package/dist/onboarding/index.js +1 -1
- package/dist/onboarding/packs.d.ts +44 -0
- package/dist/onboarding/packs.d.ts.map +1 -1
- package/dist/onboarding/packs.js +80 -2
- package/dist/onboarding/packs.js.map +1 -1
- package/dist/onboarding/settings.d.ts +33 -0
- package/dist/onboarding/settings.d.ts.map +1 -1
- package/dist/onboarding/settings.js +88 -8
- package/dist/onboarding/settings.js.map +1 -1
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js.map +1 -1
- package/dist/plugins/loader.d.ts.map +1 -1
- package/dist/plugins/loader.js +2 -1
- package/dist/plugins/loader.js.map +1 -1
- package/dist/providers/claude-max.d.ts.map +1 -1
- package/dist/providers/claude-max.js +4 -5
- package/dist/providers/claude-max.js.map +1 -1
- package/dist/providers/github-copilot.d.ts +1 -1
- package/dist/providers/github-copilot.d.ts.map +1 -1
- package/dist/providers/github-copilot.js +89 -37
- package/dist/providers/github-copilot.js.map +1 -1
- package/dist/providers/kimi-coding.d.ts.map +1 -1
- package/dist/providers/kimi-coding.js +16 -2
- package/dist/providers/kimi-coding.js.map +1 -1
- package/dist/providers/openai-codex.d.ts.map +1 -1
- package/dist/providers/openai-codex.js +42 -27
- package/dist/providers/openai-codex.js.map +1 -1
- package/dist/schema.d.ts +28 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +9 -0
- package/dist/schema.js.map +1 -1
- package/dist/tools/workflows/save-workflow.d.ts +1 -1
- package/dist/tools/workflows/save-workflow.d.ts.map +1 -1
- package/dist/workflows/register-primitives.js +2 -2
- package/dist/workflows/register-primitives.js.map +1 -1
- package/package.json +17 -17
package/dist/index.js
CHANGED
|
@@ -3,9 +3,12 @@ import { detectProject, getObservabilityDatabasePath, getResourceIdOverride, get
|
|
|
3
3
|
import { isKimiCodingDeviceId } from "./auth/providers/kimi-coding.js";
|
|
4
4
|
import { AuthStorage } from "./auth/storage.js";
|
|
5
5
|
import { setAuthStorage } from "./providers/openai-codex.js";
|
|
6
|
-
import {
|
|
6
|
+
import { getAvailableModePacks, getAvailableOmPacks, selectPreferredOMPack } from "./onboarding/packs.js";
|
|
7
|
+
import { MASTRA_GATEWAY_PROVIDER, OBSERVABILITY_AUTH_PREFIX, THREAD_ACTIVE_MODEL_PACK_ID_KEY, loadSettings, resolveModelDefaults, resolveOmRoleModel, saveSettings } from "./onboarding/settings.js";
|
|
7
8
|
import { createThreadOwnershipManager } from "./agent-connections/ownership.js";
|
|
8
9
|
import { AgentConnectionsSignalProvider } from "./agent-connections/signal-provider.js";
|
|
10
|
+
import { createBackgroundCompletionEvents } from "./agents/background-completion-events.js";
|
|
11
|
+
import { createBackgroundCompletionCallbacks } from "./agents/background-completion.js";
|
|
9
12
|
import { hasCredentialStoreProvider } from "./agents/credential-resolver.js";
|
|
10
13
|
import { getDynamicWorkspace, getGoalJudgeTools } from "./agents/workspace.js";
|
|
11
14
|
import { createGitRefInstructionReader, createGitRefReminderReader, getStaticallyLoadedInstructionPaths } from "./agents/prompts/agent-instructions.js";
|
|
@@ -26,6 +29,7 @@ import { exploreSubagent } from "./agents/subagents/explore.js";
|
|
|
26
29
|
import { planSubagent } from "./agents/subagents/plan.js";
|
|
27
30
|
import { attachOMThreadStatePersistence, restoreOMThreadStateForCurrentThread } from "./agents/thread-caveman-state.js";
|
|
28
31
|
import { createDynamicTools, createToolHooks } from "./agents/tools.js";
|
|
32
|
+
import { AccountRotationProcessor, AccountStartNoticeProcessor, PACK_FALLBACK_STATE_KEY } from "./auth/account-rotation-processor.js";
|
|
29
33
|
import { createOutcomeScorer } from "./evals/scorers/outcome.js";
|
|
30
34
|
import { createEfficiencyScorer } from "./evals/scorers/efficiency.js";
|
|
31
35
|
import "./evals/scorers/index.js";
|
|
@@ -35,7 +39,6 @@ import { KnowledgeInspectorError, createKnowledgeInspector } from "./knowledge-i
|
|
|
35
39
|
import { createMcpManager } from "./mcp/manager.js";
|
|
36
40
|
import "./mcp/index.js";
|
|
37
41
|
import { hasExplicitOMConfiguration } from "./onboarding/om-settings.js";
|
|
38
|
-
import { getAvailableModePacks, getAvailableOmPacks, selectPreferredOMPack } from "./onboarding/packs.js";
|
|
39
42
|
import { getToolCategory } from "./permissions.js";
|
|
40
43
|
import { PluginManager } from "./plugins/manager.js";
|
|
41
44
|
import { PluginSignalLane } from "./plugins/signal-lane.js";
|
|
@@ -62,7 +65,7 @@ import { createCodingAgent } from "@mastra/core/coding-agent";
|
|
|
62
65
|
import { PROVIDER_REGISTRY } from "@mastra/core/llm";
|
|
63
66
|
import { Mastra } from "@mastra/core/mastra";
|
|
64
67
|
import { defaultNotificationDeliveryDecision } from "@mastra/core/notifications";
|
|
65
|
-
import { AgentsMDInjector, PrefillErrorHandler, ProviderHistoryCompat, StreamErrorRetryProcessor, isBadRequestError } from "@mastra/core/processors";
|
|
68
|
+
import { AgentsMDInjector, PrefillErrorHandler, ProviderHistoryCompat, StreamErrorRetryProcessor, createBackgroundWorkSignalProcessor, isBadRequestError } from "@mastra/core/processors";
|
|
66
69
|
import { RequestContext } from "@mastra/core/request-context";
|
|
67
70
|
import { InMemoryHarness, MastraCompositeStore } from "@mastra/core/storage";
|
|
68
71
|
import { DEFAULT_GOAL_JUDGE_PROMPT } from "@mastra/core/tools";
|
|
@@ -74,15 +77,12 @@ import { PostgresStore } from "@mastra/pg";
|
|
|
74
77
|
//#region src/index.ts
|
|
75
78
|
const CODE_AGENT_ID = "code-agent";
|
|
76
79
|
const MASTRACODE_TRANSIENT_CONNECTION_MAX_RETRIES = 10;
|
|
80
|
+
const MASTRACODE_MAX_PROCESSOR_RETRIES = 64;
|
|
77
81
|
const MASTRACODE_TRANSIENT_CONNECTION_RETRY_INITIAL_DELAY_MS = 500;
|
|
78
82
|
const MASTRACODE_TRANSIENT_CONNECTION_RETRY_MAX_DELAY_MS = 3e4;
|
|
79
83
|
const TRANSIENT_CONNECTION_ERROR_CODES = /* @__PURE__ */ new Set(["ECONNRESET", "EPIPE"]);
|
|
80
84
|
const TRANSIENT_CONNECTION_MESSAGE_PATTERN = /econnreset|socket hang up|write epipe|other side closed/i;
|
|
81
|
-
const
|
|
82
|
-
500,
|
|
83
|
-
502,
|
|
84
|
-
503
|
|
85
|
-
]);
|
|
85
|
+
const isTransientServerStatus = (status) => status >= 500 && status < 600;
|
|
86
86
|
const TRANSIENT_SERVER_ERROR_MESSAGE_PATTERN = /internal server|server error|api may be experiencing issues/i;
|
|
87
87
|
/**
|
|
88
88
|
* Matcher for transient connection failures. Cause-chain traversal is handled
|
|
@@ -107,7 +107,7 @@ function isTransientConnectionError(error) {
|
|
|
107
107
|
function isTransientServerError(error) {
|
|
108
108
|
if (!error) return false;
|
|
109
109
|
const errorObj = typeof error === "object" ? error : void 0;
|
|
110
|
-
if (typeof errorObj?.status === "number" &&
|
|
110
|
+
if (typeof errorObj?.status === "number" && isTransientServerStatus(errorObj.status) || typeof errorObj?.statusCode === "number" && isTransientServerStatus(errorObj.statusCode)) return true;
|
|
111
111
|
const message = error instanceof Error ? error.message : void 0;
|
|
112
112
|
return typeof message === "string" && TRANSIENT_SERVER_ERROR_MESSAGE_PATTERN.test(message);
|
|
113
113
|
}
|
|
@@ -221,6 +221,7 @@ async function createMastraCodeAgentController(config) {
|
|
|
221
221
|
} catch {}
|
|
222
222
|
const authStorage = createAuthStorage();
|
|
223
223
|
const globalSettings = loadSettings(config?.settingsPath);
|
|
224
|
+
const backgroundToolsEnabled = globalSettings.backgroundTools?.enabled ?? false;
|
|
224
225
|
const storedGatewayKey = authStorage.getStoredApiKey(MASTRA_GATEWAY_PROVIDER);
|
|
225
226
|
const storedGatewayUrl = globalSettings.memoryGateway?.baseUrl;
|
|
226
227
|
if (storedGatewayKey) process.env["MASTRA_GATEWAY_API_KEY"] ??= storedGatewayKey;
|
|
@@ -327,7 +328,7 @@ async function createMastraCodeAgentController(config) {
|
|
|
327
328
|
localDbFiles: storageConfig ? resolveLocalDbFiles(storageConfig, storageResult.backend) : [],
|
|
328
329
|
closeVector: vector instanceof LibSQLVector ? () => vector.close() : void 0
|
|
329
330
|
});
|
|
330
|
-
const memory = config?.memory === false ? void 0 : config?.memory ?? getDynamicMemory(storage, vector);
|
|
331
|
+
const memory = config?.memory === false ? void 0 : config?.memory ?? getDynamicMemory(storage, vector, config?.settingsPath);
|
|
331
332
|
const hasSubconscious = config?.memory === void 0 ? (state) => hasSubconsciousTools(vector, state) : false;
|
|
332
333
|
const mcpManager = config?.disableMcp ? void 0 : createMcpManager(project.rootPath, configDir, config?.mcpServers, globalSettings.mcp);
|
|
333
334
|
const hookManager = config?.disableHooks ? void 0 : new HookManager(project.rootPath, "session-init", configDir, homeDir, project.isWorktree ? {
|
|
@@ -342,7 +343,12 @@ async function createMastraCodeAgentController(config) {
|
|
|
342
343
|
});
|
|
343
344
|
pluginManager?.setRuntime({
|
|
344
345
|
getController: () => pluginRuntimeController,
|
|
345
|
-
getActiveSession: () => activeSession
|
|
346
|
+
getActiveSession: () => activeSession,
|
|
347
|
+
getStorage: () => ({
|
|
348
|
+
storage,
|
|
349
|
+
storageBackend: storageResult.backend,
|
|
350
|
+
vector
|
|
351
|
+
})
|
|
346
352
|
});
|
|
347
353
|
const loadedPlugins = pluginManager ? await pluginManager.reload() : [];
|
|
348
354
|
const pluginTools = pluginManager?.getPluginTools() ?? {};
|
|
@@ -351,15 +357,63 @@ async function createMastraCodeAgentController(config) {
|
|
|
351
357
|
const getNotificationStreamOptions = async ({ resourceId, threadId }) => {
|
|
352
358
|
const session = await controller.getSessionByResource(resourceId) ?? activeSession;
|
|
353
359
|
if (!session) return void 0;
|
|
354
|
-
const
|
|
355
|
-
const
|
|
356
|
-
const
|
|
360
|
+
const targetThread = await session.thread.getById({ threadId });
|
|
361
|
+
const metadata = targetThread?.resourceId === resourceId ? targetThread.metadata : void 0;
|
|
362
|
+
const modes = controller.listModes();
|
|
363
|
+
const savedModeId = metadata?.currentModeId;
|
|
364
|
+
const defaultMode = modes.find((mode) => mode.default) ?? modes[0];
|
|
365
|
+
const modeId = typeof savedModeId === "string" && modes.some((mode) => mode.id === savedModeId) ? savedModeId : defaultMode?.id ?? session.mode.get();
|
|
366
|
+
const savedModeModelId = metadata?.[`modeModelId_${modeId}`];
|
|
367
|
+
const legacyModelId = metadata?.currentModelId;
|
|
368
|
+
const defaultModeModelId = modes.find((mode) => mode.id === modeId)?.defaultModelId;
|
|
369
|
+
const modelId = (typeof savedModeModelId === "string" ? savedModeModelId : void 0) ?? (typeof legacyModelId === "string" ? legacyModelId : void 0) ?? defaultModeModelId ?? session.model.get() ?? "";
|
|
370
|
+
const baseState = { ...session.state.get() };
|
|
371
|
+
delete baseState.activeModelPackId;
|
|
372
|
+
delete baseState.mastracodePendingPackFallback;
|
|
373
|
+
const persistedSandboxPaths = metadata?.sandboxAllowedPaths;
|
|
374
|
+
baseState.sandboxAllowedPaths = Array.isArray(persistedSandboxPaths) && persistedSandboxPaths.every((path) => typeof path === "string") ? persistedSandboxPaths : [];
|
|
375
|
+
const persistedStateKeys = [
|
|
376
|
+
"thinkingLevel",
|
|
377
|
+
"notifications",
|
|
378
|
+
THREAD_ACTIVE_MODEL_PACK_ID_KEY,
|
|
379
|
+
PACK_FALLBACK_STATE_KEY
|
|
380
|
+
];
|
|
381
|
+
for (const key of persistedStateKeys) {
|
|
382
|
+
const value = metadata?.[key];
|
|
383
|
+
if (value !== void 0) baseState[key] = value;
|
|
384
|
+
}
|
|
385
|
+
let notificationState = baseState;
|
|
386
|
+
const getNotificationState = () => ({ ...notificationState });
|
|
387
|
+
const setNotificationState = async (updates) => {
|
|
388
|
+
notificationState = {
|
|
389
|
+
...notificationState,
|
|
390
|
+
...updates
|
|
391
|
+
};
|
|
392
|
+
if (session.thread.getId() === threadId) await session.state.set(updates);
|
|
393
|
+
};
|
|
394
|
+
const updateNotificationState = async (updater) => {
|
|
395
|
+
const update = await updater(getNotificationState());
|
|
396
|
+
if (update.updates) await setNotificationState(update.updates);
|
|
397
|
+
for (const event of update.events ?? []) if (session.thread.getId() === threadId) session.emit(event);
|
|
398
|
+
return update.result;
|
|
399
|
+
};
|
|
357
400
|
const requestContext = new RequestContext();
|
|
358
401
|
const agentControllerContext = {
|
|
359
402
|
controllerId: controller.id,
|
|
360
|
-
state:
|
|
361
|
-
getState:
|
|
362
|
-
setState:
|
|
403
|
+
state: getNotificationState(),
|
|
404
|
+
getState: getNotificationState,
|
|
405
|
+
setState: setNotificationState,
|
|
406
|
+
updateState: updateNotificationState,
|
|
407
|
+
getThreadSetting: (key) => session.thread.getSettingOn({
|
|
408
|
+
threadId,
|
|
409
|
+
key
|
|
410
|
+
}),
|
|
411
|
+
setThreadSetting: (setting) => session.thread.setSettingOn({
|
|
412
|
+
threadId,
|
|
413
|
+
key: setting.key,
|
|
414
|
+
value: setting.value
|
|
415
|
+
}),
|
|
416
|
+
isThreadActive: () => session.thread.getId() === threadId,
|
|
363
417
|
threadId,
|
|
364
418
|
resourceId,
|
|
365
419
|
session: {
|
|
@@ -368,13 +422,23 @@ async function createMastraCodeAgentController(config) {
|
|
|
368
422
|
modeId,
|
|
369
423
|
modelId,
|
|
370
424
|
state: {
|
|
371
|
-
get:
|
|
372
|
-
set:
|
|
373
|
-
update:
|
|
425
|
+
get: getNotificationState,
|
|
426
|
+
set: setNotificationState,
|
|
427
|
+
update: updateNotificationState
|
|
374
428
|
}
|
|
375
429
|
},
|
|
376
430
|
workspace: session.getWorkspace(),
|
|
377
|
-
|
|
431
|
+
emitEvent: (event) => {
|
|
432
|
+
if (session.thread.getId() === threadId) session.emit(event);
|
|
433
|
+
},
|
|
434
|
+
getSubagentModelId: (params) => {
|
|
435
|
+
const agentType = params?.agentType;
|
|
436
|
+
const perType = agentType ? metadata?.[`subagentModelId_${agentType}`] : void 0;
|
|
437
|
+
const global = metadata?.subagentModelId;
|
|
438
|
+
if (typeof perType === "string") return perType;
|
|
439
|
+
if (typeof global === "string") return global;
|
|
440
|
+
return session.thread.getId() === threadId ? session.subagents.model.get(params ?? {}) : null;
|
|
441
|
+
}
|
|
378
442
|
};
|
|
379
443
|
requestContext.set("controller", agentControllerContext);
|
|
380
444
|
return {
|
|
@@ -385,7 +449,7 @@ async function createMastraCodeAgentController(config) {
|
|
|
385
449
|
requestContext,
|
|
386
450
|
maxSteps: 1e3,
|
|
387
451
|
savePerStep: false,
|
|
388
|
-
requireToolApproval:
|
|
452
|
+
requireToolApproval: notificationState.yolo !== true,
|
|
389
453
|
modelSettings: { temperature: 1 }
|
|
390
454
|
};
|
|
391
455
|
};
|
|
@@ -398,6 +462,7 @@ async function createMastraCodeAgentController(config) {
|
|
|
398
462
|
const mastraCodeInputProcessors = [
|
|
399
463
|
...config?.inputProcessors ?? [],
|
|
400
464
|
new PlanRejectionAbortProcessor(),
|
|
465
|
+
...backgroundToolsEnabled ? [createBackgroundWorkSignalProcessor()] : [],
|
|
401
466
|
new AgentsMDInjector({
|
|
402
467
|
isEnabled: ({ requestContext }) => {
|
|
403
468
|
const state = getInjectorSessionState(requestContext);
|
|
@@ -462,7 +527,6 @@ async function createMastraCodeAgentController(config) {
|
|
|
462
527
|
hasSubagents: subagents.length > 0
|
|
463
528
|
});
|
|
464
529
|
},
|
|
465
|
-
maxProcessorRetries: MASTRACODE_TRANSIENT_CONNECTION_MAX_RETRIES,
|
|
466
530
|
model: (ctx) => getDynamicModel(ctx, config?.settingsPath),
|
|
467
531
|
notifications: { deliveryPolicy: { decide: async (input) => {
|
|
468
532
|
const decision = defaultNotificationDeliveryDecision(input);
|
|
@@ -476,7 +540,7 @@ async function createMastraCodeAgentController(config) {
|
|
|
476
540
|
streamOptions
|
|
477
541
|
} : decision;
|
|
478
542
|
} } },
|
|
479
|
-
tools: createDynamicTools(mcpManager, config?.extraTools, config?.disabledTools, storage, pluginTools),
|
|
543
|
+
tools: createDynamicTools(mcpManager, config?.extraTools, config?.disabledTools, storage, pluginTools, backgroundToolsEnabled),
|
|
480
544
|
hooks: createToolHooks(hookManager, config?.postToolObserver),
|
|
481
545
|
scorers: {
|
|
482
546
|
outcome: {
|
|
@@ -505,6 +569,10 @@ async function createMastraCodeAgentController(config) {
|
|
|
505
569
|
},
|
|
506
570
|
inputProcessors: () => [
|
|
507
571
|
...mastraCodeInputProcessors,
|
|
572
|
+
new AccountStartNoticeProcessor({
|
|
573
|
+
credentialStore: authStorage,
|
|
574
|
+
settingsPath: config?.settingsPath
|
|
575
|
+
}),
|
|
508
576
|
...readPluginProcessors().input.map((entry) => entry.value),
|
|
509
577
|
...pluginSignalLane?.getInputProcessors() ?? []
|
|
510
578
|
],
|
|
@@ -530,8 +598,14 @@ async function createMastraCodeAgentController(config) {
|
|
|
530
598
|
onRetry: ({ error, retryCount, delayMs, requestContext }) => emitTransientRetry(error, retryCount, delayMs, requestContext)
|
|
531
599
|
}
|
|
532
600
|
] }),
|
|
533
|
-
new PrefillErrorHandler()
|
|
534
|
-
|
|
601
|
+
new PrefillErrorHandler(),
|
|
602
|
+
new AccountRotationProcessor({
|
|
603
|
+
credentialStore: authStorage,
|
|
604
|
+
maxProcessorRetries: MASTRACODE_MAX_PROCESSOR_RETRIES,
|
|
605
|
+
settingsPath: config?.settingsPath
|
|
606
|
+
})
|
|
607
|
+
],
|
|
608
|
+
maxProcessorRetries: MASTRACODE_MAX_PROCESSOR_RETRIES
|
|
535
609
|
});
|
|
536
610
|
const defaultModes = [
|
|
537
611
|
{
|
|
@@ -633,10 +707,17 @@ async function createMastraCodeAgentController(config) {
|
|
|
633
707
|
for (const [key, modelId] of Object.entries(globalSettings.models.subagentModels)) if (key === "default" || key === "_default") globalInitialState.subagentModelId = modelId;
|
|
634
708
|
else globalInitialState[`subagentModelId_${key}`] = modelId;
|
|
635
709
|
const typedStateSchema = stateSchema;
|
|
636
|
-
const
|
|
710
|
+
const backgroundCompletionEvents = backgroundToolsEnabled ? createBackgroundCompletionEvents() : void 0;
|
|
711
|
+
let controller;
|
|
712
|
+
controller = new AgentController({
|
|
637
713
|
id: "mastra-code",
|
|
638
714
|
resourceId: project.resourceId,
|
|
639
715
|
storage,
|
|
716
|
+
...backgroundToolsEnabled ? { backgroundTasks: {
|
|
717
|
+
enabled: true,
|
|
718
|
+
recoverStaleTasksOnStart: false,
|
|
719
|
+
...createBackgroundCompletionCallbacks(() => controller, backgroundCompletionEvents)
|
|
720
|
+
} } : {},
|
|
640
721
|
observability,
|
|
641
722
|
memory,
|
|
642
723
|
pubsub: signalsPubSub,
|
|
@@ -644,7 +725,10 @@ async function createMastraCodeAgentController(config) {
|
|
|
644
725
|
agent: codeAgent,
|
|
645
726
|
subagents,
|
|
646
727
|
gateways: [amazonBedrockGateway, mastraCodeGateway],
|
|
647
|
-
workspace: config?.workspace ?? ((args) => getDynamicWorkspace(
|
|
728
|
+
workspace: config?.workspace ?? ((args) => getDynamicWorkspace({
|
|
729
|
+
...args,
|
|
730
|
+
backgroundToolsEnabled
|
|
731
|
+
})),
|
|
648
732
|
browser: config?.browser,
|
|
649
733
|
idGenerator: config?.idGenerator,
|
|
650
734
|
toolCategoryResolver: getToolCategory,
|
|
@@ -680,15 +764,28 @@ async function createMastraCodeAgentController(config) {
|
|
|
680
764
|
const sessionPeerCleanup = /* @__PURE__ */ new WeakMap();
|
|
681
765
|
if (useCrossAgentSignals) {
|
|
682
766
|
controller.onSessionCreated(async (session) => {
|
|
683
|
-
const
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
767
|
+
const latestObservedTitles = /* @__PURE__ */ new Map();
|
|
768
|
+
const threadOwnership = createThreadOwnershipManager(async (threadId) => {
|
|
769
|
+
const revisionAtStart = latestObservedTitles.get(threadId)?.revision ?? 0;
|
|
770
|
+
const thread = await session.thread.getById({ threadId });
|
|
771
|
+
const agent = controller.getCurrentAgent(session);
|
|
772
|
+
const claim = await agent.claimThreadOwnership({
|
|
773
|
+
threadId,
|
|
774
|
+
resourceId: session.identity.getResourceId(),
|
|
775
|
+
streamOptions: () => session.machinery.buildStreamOptions({}),
|
|
776
|
+
peer: {
|
|
777
|
+
label: project.name,
|
|
778
|
+
...thread?.title ? { title: thread.title } : {}
|
|
779
|
+
}
|
|
780
|
+
});
|
|
781
|
+
const observedTitle = latestObservedTitles.get(threadId);
|
|
782
|
+
if (claim.claimed && observedTitle && observedTitle.revision !== revisionAtStart) agent.updateThreadPeerAdvertisement({
|
|
783
|
+
resourceId: session.identity.getResourceId(),
|
|
784
|
+
threadId,
|
|
785
|
+
peer: { title: observedTitle.title }
|
|
786
|
+
});
|
|
787
|
+
return claim;
|
|
788
|
+
});
|
|
692
789
|
const claimThreadOwnership = async (threadId) => {
|
|
693
790
|
try {
|
|
694
791
|
await threadOwnership.claim(threadId);
|
|
@@ -699,6 +796,19 @@ async function createMastraCodeAgentController(config) {
|
|
|
699
796
|
const unsubscribeSession = session.subscribe((event) => {
|
|
700
797
|
if (event.type === "thread_changed") claimThreadOwnership(event.threadId);
|
|
701
798
|
else if (event.type === "thread_created") claimThreadOwnership(event.thread.id);
|
|
799
|
+
else if (event.type === "thread_title_updated" || event.type === "om_thread_title_updated") {
|
|
800
|
+
const title = event.type === "thread_title_updated" ? event.title : event.newTitle;
|
|
801
|
+
const revision = (latestObservedTitles.get(event.threadId)?.revision ?? 0) + 1;
|
|
802
|
+
latestObservedTitles.set(event.threadId, {
|
|
803
|
+
revision,
|
|
804
|
+
title
|
|
805
|
+
});
|
|
806
|
+
controller.getCurrentAgent(session).updateThreadPeerAdvertisement({
|
|
807
|
+
resourceId: session.identity.getResourceId(),
|
|
808
|
+
threadId: event.threadId,
|
|
809
|
+
peer: { title }
|
|
810
|
+
});
|
|
811
|
+
}
|
|
702
812
|
});
|
|
703
813
|
sessionPeerCleanup.set(session, () => {
|
|
704
814
|
unsubscribeSession();
|
|
@@ -743,6 +853,8 @@ async function createMastraCodeAgentController(config) {
|
|
|
743
853
|
builtinOmPacks,
|
|
744
854
|
effectiveDefaults,
|
|
745
855
|
githubSignals,
|
|
856
|
+
backgroundToolsEnabled,
|
|
857
|
+
backgroundCompletionEvents,
|
|
746
858
|
sessionId,
|
|
747
859
|
ownerId,
|
|
748
860
|
projectPath: project.rootPath,
|
|
@@ -916,7 +1028,7 @@ async function mountAgentControllerOnMastra(config) {
|
|
|
916
1028
|
*/
|
|
917
1029
|
async function prepareAgentControllerMount(config) {
|
|
918
1030
|
const base = await createMastraCodeAgentController(config);
|
|
919
|
-
const { controller, storage, authStorage, projectPath, codeAgent, mcpManager } = base;
|
|
1031
|
+
const { controller, storage, authStorage, projectPath, codeAgent, mcpManager, backgroundToolsEnabled, backgroundCompletionEvents } = base;
|
|
920
1032
|
const controllerId = config?.controllerId ?? controller.id;
|
|
921
1033
|
const apiRoutes = config?.buildApiRoutes?.({
|
|
922
1034
|
controller,
|
|
@@ -934,6 +1046,11 @@ async function prepareAgentControllerMount(config) {
|
|
|
934
1046
|
const mastraArgs = {
|
|
935
1047
|
agentControllers: { [controllerId]: controller },
|
|
936
1048
|
storage,
|
|
1049
|
+
...backgroundToolsEnabled ? { backgroundTasks: {
|
|
1050
|
+
enabled: true,
|
|
1051
|
+
recoverStaleTasksOnStart: false,
|
|
1052
|
+
...createBackgroundCompletionCallbacks(() => controller, backgroundCompletionEvents)
|
|
1053
|
+
} } : {},
|
|
937
1054
|
...base.signalsPubSub ? { pubsub: base.signalsPubSub } : {},
|
|
938
1055
|
...Object.keys(serverConfig).length ? { server: serverConfig } : {}
|
|
939
1056
|
};
|