@mastra/code-sdk 1.11.0-alpha.1 → 1.11.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/acp/agent.d.ts +3 -3
- package/dist/acp/agent.d.ts.map +1 -1
- package/dist/acp/agent.js +95 -22
- package/dist/acp/agent.js.map +1 -1
- package/dist/acp/runtime.d.ts.map +1 -1
- package/dist/acp/runtime.js +18 -10
- package/dist/acp/runtime.js.map +1 -1
- package/dist/agents/memory.d.ts +3 -1
- package/dist/agents/memory.d.ts.map +1 -1
- package/dist/agents/memory.js +34 -23
- package/dist/agents/memory.js.map +1 -1
- package/dist/agents/model.d.ts +4 -39
- package/dist/agents/model.d.ts.map +1 -1
- package/dist/agents/model.js +31 -93
- package/dist/agents/model.js.map +1 -1
- package/dist/agents/prompts/agent-instructions.js +1 -1
- package/dist/auth/account-rotation-processor.d.ts +26 -54
- package/dist/auth/account-rotation-processor.d.ts.map +1 -1
- package/dist/auth/account-rotation-processor.js +105 -154
- package/dist/auth/account-rotation-processor.js.map +1 -1
- package/dist/auth/index.js +2 -2
- package/dist/auth/providers/kimi-coding.js +2 -1
- package/dist/auth/providers/kimi-coding.js.map +1 -1
- package/dist/auth/providers/xai.js +2 -1
- package/dist/auth/providers/xai.js.map +1 -1
- package/dist/auth/storage.js +1 -1
- package/dist/auth/types.d.ts +1 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/clipboard/index.js +1 -1
- package/dist/constants.d.ts +2 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +3 -1
- package/dist/constants.js.map +1 -1
- package/dist/headless/cli.d.ts.map +1 -1
- package/dist/headless/cli.js +9 -4
- package/dist/headless/cli.js.map +1 -1
- package/dist/headless/run-mc.d.ts.map +1 -1
- package/dist/headless/run-mc.js +46 -42
- package/dist/headless/run-mc.js.map +1 -1
- package/dist/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -27
- package/dist/index.js.map +1 -1
- package/dist/lsp/server.js +1 -1
- package/dist/onboarding/om-settings.d.ts +1 -0
- package/dist/onboarding/om-settings.d.ts.map +1 -1
- package/dist/onboarding/om-settings.js +10 -1
- package/dist/onboarding/om-settings.js.map +1 -1
- package/dist/onboarding/settings.js +2 -2
- package/dist/providers/openai-codex.js +1 -1
- package/dist/schedules/run-script.js +1 -1
- package/dist/schema.d.ts +34 -19
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +11 -5
- package/dist/schema.js.map +1 -1
- package/dist/utils/open-url.d.ts +10 -0
- package/dist/utils/open-url.d.ts.map +1 -0
- package/dist/utils/open-url.js +30 -0
- package/dist/utils/open-url.js.map +1 -0
- package/dist/utils/project.js +2 -2
- package/dist/utils/signals-pubsub.d.ts +32 -4
- package/dist/utils/signals-pubsub.d.ts.map +1 -1
- package/dist/utils/signals-pubsub.js +207 -28
- package/dist/utils/signals-pubsub.js.map +1 -1
- package/dist/utils/slash-command-processor.js +1 -1
- package/dist/utils/update-check.js +1 -1
- package/package.json +12 -12
package/dist/acp/agent.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Agent, AgentSideConnection, InitializeRequest, InitializeResponse, NewSessionRequest, NewSessionResponse, PromptRequest, PromptResponse, CancelNotification, ContentBlock, SetSessionConfigOptionRequest, SetSessionConfigOptionResponse } from '@agentclientprotocol/sdk';
|
|
1
|
+
import type { Agent, AgentSideConnection, AuthenticateRequest, InitializeRequest, InitializeResponse, NewSessionRequest, NewSessionResponse, PromptRequest, PromptResponse, CancelNotification, ContentBlock, SetSessionConfigOptionRequest, SetSessionConfigOptionResponse } from '@agentclientprotocol/sdk';
|
|
2
2
|
import type { AgentController, AgentControllerMode, Session } from '@mastra/core/agent-controller';
|
|
3
3
|
import type { ThinkingLevelSetting } from '../thinking.js';
|
|
4
4
|
import type { AcpSkills } from './skills.js';
|
|
@@ -24,8 +24,8 @@ export declare class MastraCodeAcpAgent implements Agent {
|
|
|
24
24
|
private getSession;
|
|
25
25
|
private enqueue;
|
|
26
26
|
dispose(): Promise<void>;
|
|
27
|
-
initialize(
|
|
28
|
-
authenticate(): Promise<void>;
|
|
27
|
+
initialize({ clientCapabilities }: InitializeRequest): Promise<InitializeResponse>;
|
|
28
|
+
authenticate({ methodId }: AuthenticateRequest): Promise<void>;
|
|
29
29
|
newSession(request: NewSessionRequest): Promise<NewSessionResponse>;
|
|
30
30
|
private createNewSession;
|
|
31
31
|
private refreshCommands;
|
package/dist/acp/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/acp/agent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,KAAK,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,YAAY,EAEZ,6BAA6B,EAC7B,8BAA8B,EAE/B,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/acp/agent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,KAAK,EACL,mBAAmB,EACnB,mBAAmB,EAEnB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,YAAY,EAEZ,6BAA6B,EAC7B,8BAA8B,EAE/B,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAInG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAO3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AA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sB,CAAC,MAAM,EAAE,6BAA6B,GAAG,OAAO,CAAC,8BAA8B,CAAC;IA6BtG,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC;IA2DvD,MAAM,CAAC,YAAY,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;IA0BvD,cAAc,CAAC,MAAM,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAU5E,wBAAwB,CAAC,MAAM,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAiB9F;AASD,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,CAiB3E;AAED,wBAAgB,aAAa,CAC3B,MAAM,EAAE,UAAU,GAAG,SAAS,GAAG,OAAO,GAAG,WAAW,GACrD,UAAU,GAAG,WAAW,GAAG,YAAY,GAAG,mBAAmB,GAAG,SAAS,CAW3E"}
|
package/dist/acp/agent.js
CHANGED
|
@@ -1,11 +1,63 @@
|
|
|
1
|
-
import { getAvailableThinkingLevelsForModel, isThinkingLevelSetting } from "../thinking.js";
|
|
2
1
|
import { getCurrentVersion } from "../utils/update-check.js";
|
|
2
|
+
import { AuthStorage, PROVIDER_DEFAULT_MODELS, getOAuthProviders } from "../auth/storage.js";
|
|
3
|
+
import { getAvailableThinkingLevelsForModel, isThinkingLevelSetting } from "../thinking.js";
|
|
4
|
+
import { seedProviderOMDefault } from "../onboarding/om-settings.js";
|
|
5
|
+
import { openUrlInBrowser } from "../utils/open-url.js";
|
|
3
6
|
import { withCleanupFailure } from "./errors.js";
|
|
4
7
|
import { handleAgentControllerEvent } from "./event-mapper.js";
|
|
5
8
|
import { expandSkillCommand, listSkillCommands } from "./skills.js";
|
|
6
9
|
import { isAbsolute } from "path";
|
|
7
10
|
import { PROTOCOL_VERSION, RequestError } from "@agentclientprotocol/sdk";
|
|
8
11
|
//#region src/acp/agent.ts
|
|
12
|
+
const TERMINAL_LOGIN_METHOD_ID = "mastracode-login";
|
|
13
|
+
const BROWSER_LOGIN_PROVIDER_IDS = [
|
|
14
|
+
"openai-codex",
|
|
15
|
+
"kimi-for-coding",
|
|
16
|
+
"xai"
|
|
17
|
+
];
|
|
18
|
+
const TERMINAL_LOGIN_PROVIDER_IDS = ["anthropic", "github-copilot"];
|
|
19
|
+
function terminalLogin(id, name, description, loginArgs) {
|
|
20
|
+
return {
|
|
21
|
+
id,
|
|
22
|
+
name,
|
|
23
|
+
description,
|
|
24
|
+
type: "terminal",
|
|
25
|
+
args: loginArgs,
|
|
26
|
+
_meta: { "terminal-auth": {
|
|
27
|
+
command: process.execPath,
|
|
28
|
+
args: [...process.argv.slice(1, 2), ...loginArgs],
|
|
29
|
+
label: name
|
|
30
|
+
} }
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function listAuthMethods({ auth, _meta } = {}) {
|
|
34
|
+
const providers = getOAuthProviders();
|
|
35
|
+
const methods = providers.filter((provider) => BROWSER_LOGIN_PROVIDER_IDS.includes(provider.id)).map((provider) => ({
|
|
36
|
+
id: provider.id,
|
|
37
|
+
name: `Log in with ${provider.name}`,
|
|
38
|
+
description: "Opens your browser to sign in"
|
|
39
|
+
}));
|
|
40
|
+
if (!auth?.terminal && _meta?.["terminal-auth"] !== true) return methods;
|
|
41
|
+
return [
|
|
42
|
+
...methods,
|
|
43
|
+
...providers.filter((provider) => TERMINAL_LOGIN_PROVIDER_IDS.includes(provider.id)).map((provider) => terminalLogin(provider.id, `Log in with ${provider.name}`, "Sign in from a terminal", [
|
|
44
|
+
"login",
|
|
45
|
+
"--provider",
|
|
46
|
+
provider.id
|
|
47
|
+
])),
|
|
48
|
+
terminalLogin(TERMINAL_LOGIN_METHOD_ID, "Log in with Mastra Code", "Sign in to another provider or add an API key from a terminal", ["login"])
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
function hasUsableModel(available, currentModelId) {
|
|
52
|
+
if (available.some((model) => model.hasApiKey)) return true;
|
|
53
|
+
return currentModelId !== "" && !available.some((model) => model.id === currentModelId);
|
|
54
|
+
}
|
|
55
|
+
function credentialedDefaultModel(available, currentModelId) {
|
|
56
|
+
const current = available.find((model) => model.id === currentModelId);
|
|
57
|
+
if (!current || current.hasApiKey) return void 0;
|
|
58
|
+
const credentialed = new Set(available.filter((model) => model.hasApiKey).map((model) => model.id));
|
|
59
|
+
return Object.values(PROVIDER_DEFAULT_MODELS).find((modelId) => credentialed.has(modelId));
|
|
60
|
+
}
|
|
9
61
|
/** One ACP connection, with an independent Mastra Code runtime for each conversation. */
|
|
10
62
|
var MastraCodeAcpAgent = class {
|
|
11
63
|
connection;
|
|
@@ -54,7 +106,7 @@ var MastraCodeAcpAgent = class {
|
|
|
54
106
|
});
|
|
55
107
|
return this.disposal;
|
|
56
108
|
}
|
|
57
|
-
async initialize(
|
|
109
|
+
async initialize({ clientCapabilities }) {
|
|
58
110
|
return {
|
|
59
111
|
protocolVersion: PROTOCOL_VERSION,
|
|
60
112
|
agentInfo: {
|
|
@@ -68,11 +120,30 @@ var MastraCodeAcpAgent = class {
|
|
|
68
120
|
http: true,
|
|
69
121
|
sse: false
|
|
70
122
|
}
|
|
71
|
-
}
|
|
123
|
+
},
|
|
124
|
+
authMethods: listAuthMethods(clientCapabilities)
|
|
72
125
|
};
|
|
73
126
|
}
|
|
74
|
-
async authenticate() {
|
|
75
|
-
|
|
127
|
+
async authenticate({ methodId }) {
|
|
128
|
+
if (methodId === TERMINAL_LOGIN_METHOD_ID || TERMINAL_LOGIN_PROVIDER_IDS.includes(methodId)) return;
|
|
129
|
+
if (!BROWSER_LOGIN_PROVIDER_IDS.includes(methodId)) throw RequestError.invalidParams(void 0, `Unknown authentication method: ${methodId}`);
|
|
130
|
+
const needsTerminal = () => RequestError.internalError(void 0, "Browser sign-in can't finish on its own. Run `mastracode login` in a terminal to sign in.");
|
|
131
|
+
await new AuthStorage().login(methodId, {
|
|
132
|
+
authMode: "browser",
|
|
133
|
+
onAuth: ({ url, userCode }) => {
|
|
134
|
+
if (userCode && !url.includes(userCode)) throw needsTerminal();
|
|
135
|
+
process.stderr.write(`Open ${url} to sign in\n`);
|
|
136
|
+
openUrlInBrowser(url);
|
|
137
|
+
},
|
|
138
|
+
onPrompt: async () => {
|
|
139
|
+
throw needsTerminal();
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
try {
|
|
143
|
+
seedProviderOMDefault(methodId);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
process.stderr.write(`[acp] memory model unchanged after sign-in: ${error}\n`);
|
|
146
|
+
}
|
|
76
147
|
}
|
|
77
148
|
newSession(request) {
|
|
78
149
|
const creating = Promise.resolve().then(() => this.createNewSession(request));
|
|
@@ -86,23 +157,23 @@ var MastraCodeAcpAgent = class {
|
|
|
86
157
|
const runtime = await this.createSession(request);
|
|
87
158
|
try {
|
|
88
159
|
if (this.disposed) throw RequestError.internalError(void 0, "ACP connection is closed");
|
|
160
|
+
const available = await runtime.controller.listAvailableModels();
|
|
161
|
+
if (!hasUsableModel(available, runtime.session.model.get() ?? "")) throw RequestError.authRequired(void 0, "Sign in to a model provider or add an API key to use Mastra Code");
|
|
89
162
|
const thread = await runtime.session.thread.create();
|
|
90
163
|
await runtime.session.thread.switch({ threadId: thread.id });
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
};
|
|
101
|
-
} catch {}
|
|
164
|
+
const defaultModelId = credentialedDefaultModel(available, runtime.session.model.get() ?? "");
|
|
165
|
+
if (defaultModelId) await runtime.session.model.switch(defaultModelId);
|
|
166
|
+
const models = {
|
|
167
|
+
currentModelId: runtime.session.model.get() ?? "",
|
|
168
|
+
availableModels: includeCurrentModel([...new Map(available.filter((model) => model.hasApiKey || model.id === runtime.session.model.get()).map((model) => [model.id, {
|
|
169
|
+
modelId: model.id,
|
|
170
|
+
name: model.hasApiKey ? model.id : `${model.id} (provider not configured)`
|
|
171
|
+
}])).values()], runtime.session.model.get() ?? "")
|
|
172
|
+
};
|
|
102
173
|
if (this.disposed) throw RequestError.internalError(void 0, "ACP connection is closed");
|
|
103
174
|
const entry = {
|
|
104
175
|
...runtime,
|
|
105
|
-
models: models
|
|
176
|
+
models: models.availableModels,
|
|
106
177
|
state: null,
|
|
107
178
|
queue: Promise.resolve(),
|
|
108
179
|
turns: /* @__PURE__ */ new Set(),
|
|
@@ -211,9 +282,9 @@ var MastraCodeAcpAgent = class {
|
|
|
211
282
|
});
|
|
212
283
|
return options;
|
|
213
284
|
}
|
|
214
|
-
thinkingLevel(entry) {
|
|
285
|
+
thinkingLevel(entry, modelId = entry.session.model.get() ?? "") {
|
|
215
286
|
const level = entry.getThinkingLevel?.() ?? "off";
|
|
216
|
-
return getAvailableThinkingLevelsForModel(
|
|
287
|
+
return getAvailableThinkingLevelsForModel(modelId).includes(level) ? level : "xhigh";
|
|
217
288
|
}
|
|
218
289
|
async setSessionConfigOption(params) {
|
|
219
290
|
const entry = this.getSession(params.sessionId);
|
|
@@ -221,8 +292,10 @@ var MastraCodeAcpAgent = class {
|
|
|
221
292
|
this.getSession(params.sessionId);
|
|
222
293
|
const option = this.configOptions(entry).find((option) => option.id === params.configId);
|
|
223
294
|
if (!option || option.type !== "select" || !option.options.some((item) => "value" in item && item.value === params.value)) throw RequestError.invalidParams(void 0, "Unknown session configuration selection");
|
|
224
|
-
if (params.configId === "model")
|
|
225
|
-
|
|
295
|
+
if (params.configId === "model") {
|
|
296
|
+
const modelId = String(params.value);
|
|
297
|
+
await entry.session.model.switch(modelId, { ...entry.getThinkingLevel ? { thinkingLevel: this.thinkingLevel(entry, modelId) } : {} });
|
|
298
|
+
} else if (params.configId === "mode") await entry.session.mode.switch({ modeId: String(params.value) });
|
|
226
299
|
else if (isThinkingLevelSetting(params.value)) await entry.session.state.set({ thinkingLevel: params.value });
|
|
227
300
|
if (entry.getThinkingLevel && entry.getThinkingLevel() !== this.thinkingLevel(entry)) await entry.session.state.set({ thinkingLevel: this.thinkingLevel(entry) });
|
|
228
301
|
return { configOptions: this.configOptions(entry) };
|
|
@@ -317,7 +390,7 @@ var MastraCodeAcpAgent = class {
|
|
|
317
390
|
await this.enqueue(entry, async () => {
|
|
318
391
|
if (this.disposed) return;
|
|
319
392
|
if (!includeCurrentModel(entry.models, entry.session.model.get() ?? "").some((model) => model.modelId === params.modelId)) throw RequestError.invalidParams(void 0, "Model is unavailable or its provider is not configured. Refresh the model list.");
|
|
320
|
-
await entry.session.model.switch(
|
|
393
|
+
await entry.session.model.switch(params.modelId);
|
|
321
394
|
});
|
|
322
395
|
}
|
|
323
396
|
};
|
package/dist/acp/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","names":[],"sources":["../../src/acp/agent.ts"],"sourcesContent":["import { isAbsolute } from 'node:path';\nimport { PROTOCOL_VERSION, RequestError } from '@agentclientprotocol/sdk';\nimport type {\n Agent,\n AgentSideConnection,\n InitializeRequest,\n InitializeResponse,\n NewSessionRequest,\n NewSessionResponse,\n PromptRequest,\n PromptResponse,\n CancelNotification,\n ContentBlock,\n SessionConfigOption,\n SetSessionConfigOptionRequest,\n SetSessionConfigOptionResponse,\n AvailableCommand,\n} from '@agentclientprotocol/sdk';\nimport type { AgentController, AgentControllerMode, Session } from '@mastra/core/agent-controller';\nimport { getAvailableThinkingLevelsForModel, isThinkingLevelSetting } from '../thinking.js';\nimport type { ThinkingLevelSetting } from '../thinking.js';\nimport { getCurrentVersion } from '../utils/update-check.js';\nimport { withCleanupFailure } from './errors.js';\nimport { handleAgentControllerEvent } from './event-mapper.js';\nimport type { PromptState } from './event-mapper.js';\nimport { expandSkillCommand, listSkillCommands } from './skills.js';\nimport type { AcpSkills } from './skills.js';\n\nexport interface AcpSessionRuntime {\n controller: AgentController;\n session: Session;\n modes: AgentControllerMode[];\n getThinkingLevel?: () => ThinkingLevelSetting;\n getSkills?: () => Promise<AcpSkills | undefined>;\n cleanup?: () => Promise<void>;\n}\n\nexport type AcpSessionFactory = (request: NewSessionRequest) => Promise<AcpSessionRuntime>;\n\ninterface SessionEntry extends AcpSessionRuntime {\n models: { modelId: string; name: string }[];\n state: PromptState | null;\n queue: Promise<void>;\n turns: Set<{ cancelled: boolean }>;\n unsubscribe: () => void;\n commands?: AvailableCommand[];\n}\n\n/** One ACP connection, with an independent Mastra Code runtime for each conversation. */\nexport class MastraCodeAcpAgent implements Agent {\n private readonly sessions = new Map<string, SessionEntry>();\n private readonly pendingCreations = new Set<Promise<NewSessionResponse>>();\n private readonly startupCleanupFailures: unknown[] = [];\n private disposed = false;\n private disposal?: Promise<void>;\n\n constructor(\n private readonly connection: AgentSideConnection,\n private readonly createSession: AcpSessionFactory,\n ) {}\n\n private getSession(sessionId: string): SessionEntry {\n const entry = this.sessions.get(sessionId);\n if (!entry || this.disposed) throw RequestError.invalidParams({ sessionId }, 'Unknown ACP session');\n return entry;\n }\n\n private enqueue<T>(entry: SessionEntry, operation: () => Promise<T>): Promise<T> {\n const result = entry.queue.then(operation);\n entry.queue = result.then(\n () => {},\n () => {},\n );\n return result;\n }\n\n dispose(): Promise<void> {\n if (this.disposal) return this.disposal;\n this.disposed = true;\n const entries = [...this.sessions.values()];\n this.sessions.clear();\n const cleanup = Promise.allSettled([\n // A runtime under construction owns its cleanup until it joins sessions.\n Promise.allSettled([...this.pendingCreations]),\n ...entries.map(async entry => {\n for (const turn of entry.turns) turn.cancelled = true;\n if (entry.state) {\n entry.state.cancelled = true;\n entry.session.abort();\n entry.state?.resolve('aborted');\n }\n entry.unsubscribe();\n await entry.cleanup?.();\n }),\n ]).then(results => {\n const failures = results.filter(result => result.status === 'rejected').map(result => result.reason);\n failures.push(...this.startupCleanupFailures);\n if (failures.length) throw new AggregateError(failures, 'ACP session cleanup failed');\n });\n // A provider or MCP startup can stall. Keep its cleanup ownership, but let\n // the host terminate the connection if shutdown cannot finish in time.\n this.disposal = new Promise<void>((resolve, reject) => {\n const timeout = setTimeout(() => reject(new Error('ACP shutdown timed out after 10 seconds')), 10_000);\n void cleanup.then(resolve, reject).finally(() => clearTimeout(timeout));\n });\n return this.disposal;\n }\n\n async initialize(_request: InitializeRequest): Promise<InitializeResponse> {\n return {\n protocolVersion: PROTOCOL_VERSION,\n agentInfo: { name: 'mastracode', title: 'Mastra Code', version: getCurrentVersion() },\n agentCapabilities: { loadSession: false, mcpCapabilities: { http: true, sse: false } },\n };\n }\n\n async authenticate(): Promise<void> {\n throw RequestError.invalidParams(undefined, 'Configure authentication through Mastra Code before starting ACP');\n }\n\n newSession(request: NewSessionRequest): Promise<NewSessionResponse> {\n const creating = Promise.resolve().then(() => this.createNewSession(request));\n this.pendingCreations.add(creating);\n void creating.then(\n () => this.pendingCreations.delete(creating),\n () => this.pendingCreations.delete(creating),\n );\n return creating;\n }\n\n private async createNewSession(request: NewSessionRequest): Promise<NewSessionResponse> {\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n if (!isAbsolute(request.cwd)) throw RequestError.invalidParams(undefined, 'cwd must be an absolute path');\n const runtime = await this.createSession(request);\n try {\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n const thread = await runtime.session.thread.create();\n await runtime.session.thread.switch({ threadId: thread.id });\n let models: NewSessionResponse['models'];\n try {\n const available = await runtime.controller.listAvailableModels();\n models = {\n currentModelId: runtime.session.model.get() ?? '',\n availableModels: includeCurrentModel(\n [\n ...new Map(\n available\n .filter(model => model.hasApiKey || model.id === runtime.session.model.get())\n .map(\n model =>\n [\n model.id,\n {\n modelId: model.id,\n name: model.hasApiKey ? model.id : `${model.id} (provider not configured)`,\n },\n ] as const,\n ),\n ).values(),\n ],\n runtime.session.model.get() ?? '',\n ),\n };\n } catch {\n // Discovery may be unavailable before provider authentication.\n }\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n const entry: SessionEntry = {\n ...runtime,\n models: models?.availableModels ?? [],\n state: null,\n queue: Promise.resolve(),\n turns: new Set(),\n unsubscribe: () => {},\n commands: [],\n };\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n entry.unsubscribe = runtime.session.subscribe(event => {\n handleAgentControllerEvent(event, entry.state, this.connection, entry.session);\n if (event.type === 'mode_changed') {\n void this.connection\n .sessionUpdate({\n sessionId: thread.id,\n update: { sessionUpdate: 'current_mode_update', currentModeId: entry.session.mode.get() },\n })\n .catch(error => process.stderr.write(`[acp] mode update failed: ${error}\\n`));\n }\n if (\n event.type === 'mode_changed' ||\n event.type === 'model_changed' ||\n (event.type === 'state_changed' && event.changedKeys.includes('thinkingLevel'))\n ) {\n void this.connection\n .sessionUpdate({\n sessionId: thread.id,\n update: { sessionUpdate: 'config_option_update', configOptions: this.configOptions(entry) },\n })\n .catch(error => process.stderr.write(`[acp] configuration update failed: ${error}\\n`));\n }\n });\n const response: NewSessionResponse = {\n sessionId: thread.id,\n modes: {\n currentModeId: runtime.session.mode.get(),\n availableModes: runtime.modes.map(mode => ({ id: mode.id, name: mode.name ?? mode.id })),\n },\n models,\n configOptions: this.configOptions(entry),\n };\n this.sessions.set(thread.id, entry);\n // Let the SDK send session/new before clients receive updates for this ID.\n setImmediate(() => {\n void this.enqueue(entry, async () => {\n if (!this.disposed) await this.refreshCommands(thread.id, entry);\n }).catch(error => process.stderr.write(`[acp] command discovery failed: ${error}\\n`));\n });\n return response;\n } catch (error) {\n try {\n await runtime.cleanup?.();\n } catch (cleanupError) {\n if (this.disposed) this.startupCleanupFailures.push(cleanupError);\n throw withCleanupFailure(error, cleanupError);\n }\n throw error;\n }\n }\n\n private async refreshCommands(sessionId: string, entry: SessionEntry): Promise<AcpSkills | undefined> {\n const skills = await entry.getSkills?.();\n const commands = await listSkillCommands(skills);\n if (JSON.stringify(commands) !== JSON.stringify(entry.commands)) {\n await this.connection.sessionUpdate({\n sessionId,\n update: { sessionUpdate: 'available_commands_update', availableCommands: commands },\n });\n entry.commands = commands;\n }\n return skills;\n }\n\n private configOptions(entry: SessionEntry): SessionConfigOption[] {\n const modelId = entry.session.model.get() ?? '';\n const options: SessionConfigOption[] = [];\n const models = includeCurrentModel(entry.models, modelId);\n if (models.length)\n options.push({\n id: 'model',\n name: 'Model',\n category: 'model',\n type: 'select',\n currentValue: modelId,\n options: models.map(model => ({ value: model.modelId, name: model.name })),\n });\n options.push({\n id: 'mode',\n name: 'Mode',\n category: 'mode',\n type: 'select',\n currentValue: entry.session.mode.get(),\n options: entry.modes.map(mode => ({ value: mode.id, name: mode.name ?? mode.id })),\n });\n if (entry.getThinkingLevel)\n options.push({\n id: 'thought_level',\n name: 'Reasoning effort',\n category: 'thought_level',\n type: 'select',\n description: 'Requested reasoning level. The provider may adjust it for the selected model.',\n currentValue: this.thinkingLevel(entry),\n options: getAvailableThinkingLevelsForModel(modelId).map(value => ({\n value,\n name: value[0]!.toUpperCase() + value.slice(1),\n })),\n });\n return options;\n }\n\n private thinkingLevel(entry: SessionEntry): ThinkingLevelSetting {\n const level = entry.getThinkingLevel?.() ?? 'off';\n const levels = getAvailableThinkingLevelsForModel(entry.session.model.get() ?? '');\n return levels.includes(level) ? level : 'xhigh';\n }\n\n async setSessionConfigOption(params: SetSessionConfigOptionRequest): Promise<SetSessionConfigOptionResponse> {\n const entry = this.getSession(params.sessionId);\n return this.enqueue(entry, async () => {\n this.getSession(params.sessionId);\n const option = this.configOptions(entry).find(option => option.id === params.configId);\n if (\n !option ||\n option.type !== 'select' ||\n !option.options.some(item => 'value' in item && item.value === params.value)\n ) {\n throw RequestError.invalidParams(undefined, 'Unknown session configuration selection');\n }\n if (params.configId === 'model') {\n await entry.session.model.switch({ modelId: String(params.value) });\n } else if (params.configId === 'mode') {\n await entry.session.mode.switch({ modeId: String(params.value) });\n } else if (isThinkingLevelSetting(params.value)) {\n await entry.session.state.set({ thinkingLevel: params.value });\n }\n if (entry.getThinkingLevel && entry.getThinkingLevel() !== this.thinkingLevel(entry)) {\n await entry.session.state.set({ thinkingLevel: this.thinkingLevel(entry) });\n }\n return { configOptions: this.configOptions(entry) };\n });\n }\n\n async prompt(request: PromptRequest): Promise<PromptResponse> {\n const entry = this.getSession(request.sessionId);\n const content = extractTextFromContentBlocks(request.prompt);\n const turn = { cancelled: false };\n entry.turns.add(turn);\n try {\n return await this.enqueue(entry, async () => {\n if (turn.cancelled || this.disposed) return { stopReason: 'cancelled' };\n const usage: PromptState['usage'] = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };\n let complete!: PromptState['resolve'];\n const completion = new Promise<Parameters<PromptState['resolve']>[0]>(resolve => {\n complete = resolve;\n });\n const state: PromptState = {\n sessionId: request.sessionId,\n usage,\n resolve: reason => {\n state.finished = true;\n complete(reason);\n },\n };\n entry.state = state;\n try {\n const skills = await this.refreshCommands(request.sessionId, entry);\n const expanded = await expandSkillCommand(content, skills, entry.commands ?? []);\n if (turn.cancelled || this.disposed) return { stopReason: 'cancelled' };\n await entry.session.sendMessage({ content: expanded });\n const reason = await completion;\n if (reason === 'error' && !state.cancelled && !state.stopReason) {\n throw RequestError.internalError(undefined, state.error?.message ?? 'Mastra Code turn failed');\n }\n return {\n stopReason: state.cancelled ? 'cancelled' : (state.stopReason ?? mapStopReason(reason)),\n usage: {\n inputTokens: usage.promptTokens,\n outputTokens: usage.completionTokens,\n totalTokens: usage.totalTokens,\n thoughtTokens: usage.reasoningTokens,\n cachedReadTokens: usage.cachedInputTokens,\n cachedWriteTokens: usage.cacheCreationInputTokens,\n },\n };\n } catch (error) {\n if (state.cancelled) return { stopReason: 'cancelled' };\n if (error instanceof RequestError) throw error;\n throw RequestError.internalError(\n undefined,\n error instanceof Error ? error.message : 'Mastra Code turn failed',\n );\n } finally {\n state.finished = true;\n if (entry.state === state) entry.state = null;\n }\n });\n } finally {\n entry.turns.delete(turn);\n }\n }\n\n async cancel(notification: CancelNotification): Promise<void> {\n const entry = this.sessions.get(notification.sessionId);\n if (!entry) return;\n for (const turn of entry.turns) turn.cancelled = true;\n if (entry.state && !entry.state.finished && !entry.state.cancelled) {\n const state = entry.state;\n state.cancelled = true;\n // Persist denial before aborting, otherwise a parked snapshot can replay on the next turn.\n for (const deny of state.cancelSuspensions?.values() ?? []) {\n try {\n await deny();\n } catch {\n /* Still abort if the suspended run is no longer available. */\n }\n }\n state.cancelSuspensions?.clear();\n if (entry.state !== state) return;\n entry.session.abort();\n // A suspended run has already ended and will not emit another agent_end.\n if (entry.state?.suspended) {\n entry.session.stream.detach();\n entry.state.resolve('aborted');\n }\n }\n }\n\n async setSessionMode(params: { sessionId: string; modeId: string }): Promise<void> {\n const entry = this.getSession(params.sessionId);\n if (!entry.modes.some(mode => mode.id === params.modeId))\n throw RequestError.invalidParams(undefined, 'Unknown mode');\n await this.enqueue(entry, async () => {\n if (this.disposed) return;\n await entry.session.mode.switch({ modeId: params.modeId });\n });\n }\n\n async unstable_setSessionModel(params: { sessionId: string; modelId: string }): Promise<void> {\n const entry = this.getSession(params.sessionId);\n await this.enqueue(entry, async () => {\n if (this.disposed) return;\n if (\n !includeCurrentModel(entry.models, entry.session.model.get() ?? '').some(\n model => model.modelId === params.modelId,\n )\n ) {\n throw RequestError.invalidParams(\n undefined,\n 'Model is unavailable or its provider is not configured. Refresh the model list.',\n );\n }\n await entry.session.model.switch({ modelId: params.modelId });\n });\n }\n}\n\nfunction includeCurrentModel(models: SessionEntry['models'], currentModelId: string): SessionEntry['models'] {\n // Saved/custom model IDs may work even when discovery only returns a gateway-qualified alias.\n return currentModelId && !models.some(model => model.modelId === currentModelId)\n ? [{ modelId: currentModelId, name: currentModelId }, ...models]\n : models;\n}\n\nexport function extractTextFromContentBlocks(blocks: ContentBlock[]): string {\n const parts: string[] = [];\n for (const block of blocks) {\n if (block.type === 'text') {\n parts.push(block.text);\n } else if (block.type === 'resource_link') {\n parts.push(`[resource: ${block.uri}]`);\n } else if (block.type === 'resource') {\n parts.push(`[resource: ${block.resource.uri}]`);\n if (!('text' in block.resource))\n throw RequestError.invalidParams(undefined, 'Binary prompt resources are not supported');\n parts.push(block.resource.text);\n } else {\n throw RequestError.invalidParams(undefined, `Unsupported prompt content: ${block.type}`);\n }\n }\n return parts.join('\\n');\n}\n\nexport function mapStopReason(\n reason: 'complete' | 'aborted' | 'error' | 'suspended',\n): 'end_turn' | 'cancelled' | 'max_tokens' | 'max_turn_requests' | 'refusal' {\n switch (reason) {\n case 'complete':\n return 'end_turn';\n case 'aborted':\n return 'cancelled';\n case 'error':\n throw RequestError.internalError(undefined, 'Mastra Code turn failed');\n case 'suspended':\n return 'end_turn';\n }\n}\n"],"mappings":";;;;;;;;;AAiDA,IAAa,qBAAb,MAAiD;CAQ5B;CACA;CARnB,2BAA4B,IAAI,IAA0B;CAC1D,mCAAoC,IAAI,IAAiC;CACzE,yBAAqD,CAAC;CACtD,WAAmB;CACnB;CAEA,YACE,YACA,eACA;EAFiB,KAAA,aAAA;EACA,KAAA,gBAAA;CAChB;CAEH,WAAmB,WAAiC;EAClD,MAAM,QAAQ,KAAK,SAAS,IAAI,SAAS;EACzC,IAAI,CAAC,SAAS,KAAK,UAAU,MAAM,aAAa,cAAc,EAAE,UAAU,GAAG,qBAAqB;EAClG,OAAO;CACT;CAEA,QAAmB,OAAqB,WAAyC;EAC/E,MAAM,SAAS,MAAM,MAAM,KAAK,SAAS;EACzC,MAAM,QAAQ,OAAO,WACb,CAAC,SACD,CAAC,CACT;EACA,OAAO;CACT;CAEA,UAAyB;EACvB,IAAI,KAAK,UAAU,OAAO,KAAK;EAC/B,KAAK,WAAW;EAChB,MAAM,UAAU,CAAC,GAAG,KAAK,SAAS,OAAO,CAAC;EAC1C,KAAK,SAAS,MAAM;EACpB,MAAM,UAAU,QAAQ,WAAW,CAEjC,QAAQ,WAAW,CAAC,GAAG,KAAK,gBAAgB,CAAC,GAC7C,GAAG,QAAQ,IAAI,OAAM,UAAS;GAC5B,KAAK,MAAM,QAAQ,MAAM,O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E,CAAC;EAAE,SAAS;EAAgB,MAAM;CAAe,GAAG,GAAG,MAAM,IAC7D;AACN;AAEA,SAAgB,6BAA6B,QAAgC;CAC3E,MAAM,QAAkB,CAAC;CACzB,KAAK,MAAM,SAAS,QAClB,IAAI,MAAM,SAAS,QACjB,MAAM,KAAK,MAAM,IAAI;MAChB,IAAI,MAAM,SAAS,iBACxB,MAAM,KAAK,cAAc,MAAM,IAAI,EAAE;MAChC,IAAI,MAAM,SAAS,YAAY;EACpC,MAAM,KAAK,cAAc,MAAM,SAAS,IAAI,EAAE;EAC9C,IAAI,EAAE,UAAU,MAAM,WACpB,MAAM,aAAa,cAAc,KAAA,GAAW,2CAA2C;EACzF,MAAM,KAAK,MAAM,SAAS,IAAI;CAChC,OACE,MAAM,aAAa,cAAc,KAAA,GAAW,+BAA+B,MAAM,MAAM;CAG3F,OAAO,MAAM,KAAK,IAAI;AACxB;AAEA,SAAgB,cACd,QAC2E;CAC3E,QAAQ,QAAR;EACE,KAAK,YACH,OAAO;EACT,KAAK,WACH,OAAO;EACT,KAAK,SACH,MAAM,aAAa,cAAc,KAAA,GAAW,yBAAyB;EACvE,KAAK,aACH,OAAO;CACX;AACF"}
|
|
1
|
+
{"version":3,"file":"agent.js","names":[],"sources":["../../src/acp/agent.ts"],"sourcesContent":["import { isAbsolute } from 'node:path';\nimport { PROTOCOL_VERSION, RequestError } from '@agentclientprotocol/sdk';\nimport type {\n Agent,\n AgentSideConnection,\n AuthenticateRequest,\n AuthMethod,\n InitializeRequest,\n InitializeResponse,\n NewSessionRequest,\n NewSessionResponse,\n PromptRequest,\n PromptResponse,\n CancelNotification,\n ContentBlock,\n SessionConfigOption,\n SetSessionConfigOptionRequest,\n SetSessionConfigOptionResponse,\n AvailableCommand,\n} from '@agentclientprotocol/sdk';\nimport type { AgentController, AgentControllerMode, Session } from '@mastra/core/agent-controller';\nimport { AuthStorage, getOAuthProviders, PROVIDER_DEFAULT_MODELS } from '../auth/storage.js';\nimport { seedProviderOMDefault } from '../onboarding/om-settings.js';\nimport { getAvailableThinkingLevelsForModel, isThinkingLevelSetting } from '../thinking.js';\nimport type { ThinkingLevelSetting } from '../thinking.js';\nimport { openUrlInBrowser } from '../utils/open-url.js';\nimport { getCurrentVersion } from '../utils/update-check.js';\nimport { withCleanupFailure } from './errors.js';\nimport { handleAgentControllerEvent } from './event-mapper.js';\nimport type { PromptState } from './event-mapper.js';\nimport { expandSkillCommand, listSkillCommands } from './skills.js';\nimport type { AcpSkills } from './skills.js';\n\nexport interface AcpSessionRuntime {\n controller: AgentController;\n session: Session;\n modes: AgentControllerMode[];\n getThinkingLevel?: () => ThinkingLevelSetting;\n getSkills?: () => Promise<AcpSkills | undefined>;\n cleanup?: () => Promise<void>;\n}\n\nexport type AcpSessionFactory = (request: NewSessionRequest) => Promise<AcpSessionRuntime>;\n\ninterface SessionEntry extends AcpSessionRuntime {\n models: { modelId: string; name: string }[];\n state: PromptState | null;\n queue: Promise<void>;\n turns: Set<{ cancelled: boolean }>;\n unsubscribe: () => void;\n commands?: AvailableCommand[];\n}\n\nconst TERMINAL_LOGIN_METHOD_ID = 'mastracode-login';\nconst BROWSER_LOGIN_PROVIDER_IDS = ['openai-codex', 'kimi-for-coding', 'xai'];\nconst TERMINAL_LOGIN_PROVIDER_IDS = ['anthropic', 'github-copilot'];\n\nfunction terminalLogin(id: string, name: string, description: string, loginArgs: string[]): AuthMethod {\n return {\n id,\n name,\n description,\n type: 'terminal',\n args: loginArgs,\n _meta: {\n 'terminal-auth': {\n command: process.execPath,\n args: [...process.argv.slice(1, 2), ...loginArgs],\n label: name,\n },\n },\n };\n}\n\nfunction listAuthMethods({ auth, _meta }: InitializeRequest['clientCapabilities'] = {}): AuthMethod[] {\n const providers = getOAuthProviders();\n const methods: AuthMethod[] = providers\n .filter(provider => BROWSER_LOGIN_PROVIDER_IDS.includes(provider.id))\n .map(provider => ({\n id: provider.id,\n name: `Log in with ${provider.name}`,\n description: 'Opens your browser to sign in',\n }));\n if (!auth?.terminal && _meta?.['terminal-auth'] !== true) return methods;\n return [\n ...methods,\n ...providers\n .filter(provider => TERMINAL_LOGIN_PROVIDER_IDS.includes(provider.id))\n .map(provider =>\n terminalLogin(provider.id, `Log in with ${provider.name}`, 'Sign in from a terminal', [\n 'login',\n '--provider',\n provider.id,\n ]),\n ),\n terminalLogin(\n TERMINAL_LOGIN_METHOD_ID,\n 'Log in with Mastra Code',\n 'Sign in to another provider or add an API key from a terminal',\n ['login'],\n ),\n ];\n}\n\nfunction hasUsableModel(available: { id: string; hasApiKey: boolean }[], currentModelId: string): boolean {\n if (available.some(model => model.hasApiKey)) return true;\n return currentModelId !== '' && !available.some(model => model.id === currentModelId);\n}\n\nfunction credentialedDefaultModel(\n available: { id: string; hasApiKey: boolean }[],\n currentModelId: string,\n): string | undefined {\n const current = available.find(model => model.id === currentModelId);\n if (!current || current.hasApiKey) return undefined;\n const credentialed = new Set(available.filter(model => model.hasApiKey).map(model => model.id));\n return Object.values(PROVIDER_DEFAULT_MODELS).find(modelId => credentialed.has(modelId));\n}\n\n/** One ACP connection, with an independent Mastra Code runtime for each conversation. */\nexport class MastraCodeAcpAgent implements Agent {\n private readonly sessions = new Map<string, SessionEntry>();\n private readonly pendingCreations = new Set<Promise<NewSessionResponse>>();\n private readonly startupCleanupFailures: unknown[] = [];\n private disposed = false;\n private disposal?: Promise<void>;\n\n constructor(\n private readonly connection: AgentSideConnection,\n private readonly createSession: AcpSessionFactory,\n ) {}\n\n private getSession(sessionId: string): SessionEntry {\n const entry = this.sessions.get(sessionId);\n if (!entry || this.disposed) throw RequestError.invalidParams({ sessionId }, 'Unknown ACP session');\n return entry;\n }\n\n private enqueue<T>(entry: SessionEntry, operation: () => Promise<T>): Promise<T> {\n const result = entry.queue.then(operation);\n entry.queue = result.then(\n () => {},\n () => {},\n );\n return result;\n }\n\n dispose(): Promise<void> {\n if (this.disposal) return this.disposal;\n this.disposed = true;\n const entries = [...this.sessions.values()];\n this.sessions.clear();\n const cleanup = Promise.allSettled([\n // A runtime under construction owns its cleanup until it joins sessions.\n Promise.allSettled([...this.pendingCreations]),\n ...entries.map(async entry => {\n for (const turn of entry.turns) turn.cancelled = true;\n if (entry.state) {\n entry.state.cancelled = true;\n entry.session.abort();\n entry.state?.resolve('aborted');\n }\n entry.unsubscribe();\n await entry.cleanup?.();\n }),\n ]).then(results => {\n const failures = results.filter(result => result.status === 'rejected').map(result => result.reason);\n failures.push(...this.startupCleanupFailures);\n if (failures.length) throw new AggregateError(failures, 'ACP session cleanup failed');\n });\n // A provider or MCP startup can stall. Keep its cleanup ownership, but let\n // the host terminate the connection if shutdown cannot finish in time.\n this.disposal = new Promise<void>((resolve, reject) => {\n const timeout = setTimeout(() => reject(new Error('ACP shutdown timed out after 10 seconds')), 10_000);\n void cleanup.then(resolve, reject).finally(() => clearTimeout(timeout));\n });\n return this.disposal;\n }\n\n async initialize({ clientCapabilities }: InitializeRequest): Promise<InitializeResponse> {\n return {\n protocolVersion: PROTOCOL_VERSION,\n agentInfo: { name: 'mastracode', title: 'Mastra Code', version: getCurrentVersion() },\n agentCapabilities: { loadSession: false, mcpCapabilities: { http: true, sse: false } },\n authMethods: listAuthMethods(clientCapabilities),\n };\n }\n\n async authenticate({ methodId }: AuthenticateRequest): Promise<void> {\n if (methodId === TERMINAL_LOGIN_METHOD_ID || TERMINAL_LOGIN_PROVIDER_IDS.includes(methodId)) return;\n if (!BROWSER_LOGIN_PROVIDER_IDS.includes(methodId)) {\n throw RequestError.invalidParams(undefined, `Unknown authentication method: ${methodId}`);\n }\n const needsTerminal = () =>\n RequestError.internalError(\n undefined,\n \"Browser sign-in can't finish on its own. Run `mastracode login` in a terminal to sign in.\",\n );\n await new AuthStorage().login(methodId, {\n authMode: 'browser',\n onAuth: ({ url, userCode }) => {\n if (userCode && !url.includes(userCode)) throw needsTerminal();\n process.stderr.write(`Open ${url} to sign in\\n`);\n openUrlInBrowser(url);\n },\n onPrompt: async () => {\n throw needsTerminal();\n },\n });\n try {\n seedProviderOMDefault(methodId);\n } catch (error) {\n process.stderr.write(`[acp] memory model unchanged after sign-in: ${error}\\n`);\n }\n }\n\n newSession(request: NewSessionRequest): Promise<NewSessionResponse> {\n const creating = Promise.resolve().then(() => this.createNewSession(request));\n this.pendingCreations.add(creating);\n void creating.then(\n () => this.pendingCreations.delete(creating),\n () => this.pendingCreations.delete(creating),\n );\n return creating;\n }\n\n private async createNewSession(request: NewSessionRequest): Promise<NewSessionResponse> {\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n if (!isAbsolute(request.cwd)) throw RequestError.invalidParams(undefined, 'cwd must be an absolute path');\n const runtime = await this.createSession(request);\n try {\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n const available = await runtime.controller.listAvailableModels();\n if (!hasUsableModel(available, runtime.session.model.get() ?? '')) {\n throw RequestError.authRequired(undefined, 'Sign in to a model provider or add an API key to use Mastra Code');\n }\n const thread = await runtime.session.thread.create();\n await runtime.session.thread.switch({ threadId: thread.id });\n const defaultModelId = credentialedDefaultModel(available, runtime.session.model.get() ?? '');\n if (defaultModelId) await runtime.session.model.switch(defaultModelId);\n const models: NewSessionResponse['models'] = {\n currentModelId: runtime.session.model.get() ?? '',\n availableModels: includeCurrentModel(\n [\n ...new Map(\n available\n .filter(model => model.hasApiKey || model.id === runtime.session.model.get())\n .map(\n model =>\n [\n model.id,\n {\n modelId: model.id,\n name: model.hasApiKey ? model.id : `${model.id} (provider not configured)`,\n },\n ] as const,\n ),\n ).values(),\n ],\n runtime.session.model.get() ?? '',\n ),\n };\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n const entry: SessionEntry = {\n ...runtime,\n models: models.availableModels,\n state: null,\n queue: Promise.resolve(),\n turns: new Set(),\n unsubscribe: () => {},\n commands: [],\n };\n if (this.disposed) throw RequestError.internalError(undefined, 'ACP connection is closed');\n entry.unsubscribe = runtime.session.subscribe(event => {\n handleAgentControllerEvent(event, entry.state, this.connection, entry.session);\n if (event.type === 'mode_changed') {\n void this.connection\n .sessionUpdate({\n sessionId: thread.id,\n update: { sessionUpdate: 'current_mode_update', currentModeId: entry.session.mode.get() },\n })\n .catch(error => process.stderr.write(`[acp] mode update failed: ${error}\\n`));\n }\n if (\n event.type === 'mode_changed' ||\n event.type === 'model_changed' ||\n (event.type === 'state_changed' && event.changedKeys.includes('thinkingLevel'))\n ) {\n void this.connection\n .sessionUpdate({\n sessionId: thread.id,\n update: { sessionUpdate: 'config_option_update', configOptions: this.configOptions(entry) },\n })\n .catch(error => process.stderr.write(`[acp] configuration update failed: ${error}\\n`));\n }\n });\n const response: NewSessionResponse = {\n sessionId: thread.id,\n modes: {\n currentModeId: runtime.session.mode.get(),\n availableModes: runtime.modes.map(mode => ({ id: mode.id, name: mode.name ?? mode.id })),\n },\n models,\n configOptions: this.configOptions(entry),\n };\n this.sessions.set(thread.id, entry);\n // Let the SDK send session/new before clients receive updates for this ID.\n setImmediate(() => {\n void this.enqueue(entry, async () => {\n if (!this.disposed) await this.refreshCommands(thread.id, entry);\n }).catch(error => process.stderr.write(`[acp] command discovery failed: ${error}\\n`));\n });\n return response;\n } catch (error) {\n try {\n await runtime.cleanup?.();\n } catch (cleanupError) {\n if (this.disposed) this.startupCleanupFailures.push(cleanupError);\n throw withCleanupFailure(error, cleanupError);\n }\n throw error;\n }\n }\n\n private async refreshCommands(sessionId: string, entry: SessionEntry): Promise<AcpSkills | undefined> {\n const skills = await entry.getSkills?.();\n const commands = await listSkillCommands(skills);\n if (JSON.stringify(commands) !== JSON.stringify(entry.commands)) {\n await this.connection.sessionUpdate({\n sessionId,\n update: { sessionUpdate: 'available_commands_update', availableCommands: commands },\n });\n entry.commands = commands;\n }\n return skills;\n }\n\n private configOptions(entry: SessionEntry): SessionConfigOption[] {\n const modelId = entry.session.model.get() ?? '';\n const options: SessionConfigOption[] = [];\n const models = includeCurrentModel(entry.models, modelId);\n if (models.length)\n options.push({\n id: 'model',\n name: 'Model',\n category: 'model',\n type: 'select',\n currentValue: modelId,\n options: models.map(model => ({ value: model.modelId, name: model.name })),\n });\n options.push({\n id: 'mode',\n name: 'Mode',\n category: 'mode',\n type: 'select',\n currentValue: entry.session.mode.get(),\n options: entry.modes.map(mode => ({ value: mode.id, name: mode.name ?? mode.id })),\n });\n if (entry.getThinkingLevel)\n options.push({\n id: 'thought_level',\n name: 'Reasoning effort',\n category: 'thought_level',\n type: 'select',\n description: 'Requested reasoning level. The provider may adjust it for the selected model.',\n currentValue: this.thinkingLevel(entry),\n options: getAvailableThinkingLevelsForModel(modelId).map(value => ({\n value,\n name: value[0]!.toUpperCase() + value.slice(1),\n })),\n });\n return options;\n }\n\n private thinkingLevel(entry: SessionEntry, modelId = entry.session.model.get() ?? ''): ThinkingLevelSetting {\n const level = entry.getThinkingLevel?.() ?? 'off';\n const levels = getAvailableThinkingLevelsForModel(modelId);\n return levels.includes(level) ? level : 'xhigh';\n }\n\n async setSessionConfigOption(params: SetSessionConfigOptionRequest): Promise<SetSessionConfigOptionResponse> {\n const entry = this.getSession(params.sessionId);\n return this.enqueue(entry, async () => {\n this.getSession(params.sessionId);\n const option = this.configOptions(entry).find(option => option.id === params.configId);\n if (\n !option ||\n option.type !== 'select' ||\n !option.options.some(item => 'value' in item && item.value === params.value)\n ) {\n throw RequestError.invalidParams(undefined, 'Unknown session configuration selection');\n }\n if (params.configId === 'model') {\n const modelId = String(params.value);\n await entry.session.model.switch(modelId, {\n ...(entry.getThinkingLevel ? { thinkingLevel: this.thinkingLevel(entry, modelId) } : {}),\n });\n } else if (params.configId === 'mode') {\n await entry.session.mode.switch({ modeId: String(params.value) });\n } else if (isThinkingLevelSetting(params.value)) {\n await entry.session.state.set({ thinkingLevel: params.value });\n }\n if (entry.getThinkingLevel && entry.getThinkingLevel() !== this.thinkingLevel(entry)) {\n await entry.session.state.set({ thinkingLevel: this.thinkingLevel(entry) });\n }\n return { configOptions: this.configOptions(entry) };\n });\n }\n\n async prompt(request: PromptRequest): Promise<PromptResponse> {\n const entry = this.getSession(request.sessionId);\n const content = extractTextFromContentBlocks(request.prompt);\n const turn = { cancelled: false };\n entry.turns.add(turn);\n try {\n return await this.enqueue(entry, async () => {\n if (turn.cancelled || this.disposed) return { stopReason: 'cancelled' };\n const usage: PromptState['usage'] = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };\n let complete!: PromptState['resolve'];\n const completion = new Promise<Parameters<PromptState['resolve']>[0]>(resolve => {\n complete = resolve;\n });\n const state: PromptState = {\n sessionId: request.sessionId,\n usage,\n resolve: reason => {\n state.finished = true;\n complete(reason);\n },\n };\n entry.state = state;\n try {\n const skills = await this.refreshCommands(request.sessionId, entry);\n const expanded = await expandSkillCommand(content, skills, entry.commands ?? []);\n if (turn.cancelled || this.disposed) return { stopReason: 'cancelled' };\n await entry.session.sendMessage({ content: expanded });\n const reason = await completion;\n if (reason === 'error' && !state.cancelled && !state.stopReason) {\n throw RequestError.internalError(undefined, state.error?.message ?? 'Mastra Code turn failed');\n }\n return {\n stopReason: state.cancelled ? 'cancelled' : (state.stopReason ?? mapStopReason(reason)),\n usage: {\n inputTokens: usage.promptTokens,\n outputTokens: usage.completionTokens,\n totalTokens: usage.totalTokens,\n thoughtTokens: usage.reasoningTokens,\n cachedReadTokens: usage.cachedInputTokens,\n cachedWriteTokens: usage.cacheCreationInputTokens,\n },\n };\n } catch (error) {\n if (state.cancelled) return { stopReason: 'cancelled' };\n if (error instanceof RequestError) throw error;\n throw RequestError.internalError(\n undefined,\n error instanceof Error ? error.message : 'Mastra Code turn failed',\n );\n } finally {\n state.finished = true;\n if (entry.state === state) entry.state = null;\n }\n });\n } finally {\n entry.turns.delete(turn);\n }\n }\n\n async cancel(notification: CancelNotification): Promise<void> {\n const entry = this.sessions.get(notification.sessionId);\n if (!entry) return;\n for (const turn of entry.turns) turn.cancelled = true;\n if (entry.state && !entry.state.finished && !entry.state.cancelled) {\n const state = entry.state;\n state.cancelled = true;\n // Persist denial before aborting, otherwise a parked snapshot can replay on the next turn.\n for (const deny of state.cancelSuspensions?.values() ?? []) {\n try {\n await deny();\n } catch {\n /* Still abort if the suspended run is no longer available. */\n }\n }\n state.cancelSuspensions?.clear();\n if (entry.state !== state) return;\n entry.session.abort();\n // A suspended run has already ended and will not emit another agent_end.\n if (entry.state?.suspended) {\n entry.session.stream.detach();\n entry.state.resolve('aborted');\n }\n }\n }\n\n async setSessionMode(params: { sessionId: string; modeId: string }): Promise<void> {\n const entry = this.getSession(params.sessionId);\n if (!entry.modes.some(mode => mode.id === params.modeId))\n throw RequestError.invalidParams(undefined, 'Unknown mode');\n await this.enqueue(entry, async () => {\n if (this.disposed) return;\n await entry.session.mode.switch({ modeId: params.modeId });\n });\n }\n\n async unstable_setSessionModel(params: { sessionId: string; modelId: string }): Promise<void> {\n const entry = this.getSession(params.sessionId);\n await this.enqueue(entry, async () => {\n if (this.disposed) return;\n if (\n !includeCurrentModel(entry.models, entry.session.model.get() ?? '').some(\n model => model.modelId === params.modelId,\n )\n ) {\n throw RequestError.invalidParams(\n undefined,\n 'Model is unavailable or its provider is not configured. Refresh the model list.',\n );\n }\n await entry.session.model.switch(params.modelId);\n });\n }\n}\n\nfunction includeCurrentModel(models: SessionEntry['models'], currentModelId: string): SessionEntry['models'] {\n // Saved/custom model IDs may work even when discovery only returns a gateway-qualified alias.\n return currentModelId && !models.some(model => model.modelId === currentModelId)\n ? [{ modelId: currentModelId, name: currentModelId }, ...models]\n : models;\n}\n\nexport function extractTextFromContentBlocks(blocks: ContentBlock[]): string {\n const parts: string[] = [];\n for (const block of blocks) {\n if (block.type === 'text') {\n parts.push(block.text);\n } else if (block.type === 'resource_link') {\n parts.push(`[resource: ${block.uri}]`);\n } else if (block.type === 'resource') {\n parts.push(`[resource: ${block.resource.uri}]`);\n if (!('text' in block.resource))\n throw RequestError.invalidParams(undefined, 'Binary prompt resources are not supported');\n parts.push(block.resource.text);\n } else {\n throw RequestError.invalidParams(undefined, `Unsupported prompt content: ${block.type}`);\n }\n }\n return parts.join('\\n');\n}\n\nexport function mapStopReason(\n reason: 'complete' | 'aborted' | 'error' | 'suspended',\n): 'end_turn' | 'cancelled' | 'max_tokens' | 'max_turn_requests' | 'refusal' {\n switch (reason) {\n case 'complete':\n return 'end_turn';\n case 'aborted':\n return 'cancelled';\n case 'error':\n throw RequestError.internalError(undefined, 'Mastra Code turn failed');\n case 'suspended':\n return 'end_turn';\n }\n}\n"],"mappings":";;;;;;;;;;;AAqDA,MAAM,2BAA2B;AACjC,MAAM,6BAA6B;CAAC;CAAgB;CAAmB;AAAK;AAC5E,MAAM,8BAA8B,CAAC,aAAa,gBAAgB;AAElE,SAAS,cAAc,IAAY,MAAc,aAAqB,WAAiC;CACrG,OAAO;EACL;EACA;EACA;EACA,MAAM;EACN,MAAM;EACN,OAAO,EACL,iBAAiB;GACf,SAAS,QAAQ;GACjB,MAAM,CAAC,GAAG,QAAQ,KAAK,MAAM,GAAG,CAAC,GAAG,GAAG,SAAS;GAChD,OAAO;EACT,EACF;CACF;AACF;AAEA,SAAS,gBAAgB,EAAE,MAAM,UAAmD,CAAC,GAAiB;CACpG,MAAM,YAAY,kBAAkB;CACpC,MAAM,UAAwB,UAC3B,QAAO,aAAY,2BAA2B,SAAS,SAAS,EAAE,CAAC,CAAC,CACpE,KAAI,cAAa;EAChB,IAAI,SAAS;EACb,MAAM,eAAe,SAAS;EAC9B,aAAa;CACf,EAAE;CACJ,IAAI,CAAC,MAAM,YAAY,QAAQ,qBAAqB,MAAM,OAAO;CACjE,OAAO;EACL,GAAG;EACH,GAAG,UACA,QAAO,aAAY,4BAA4B,SAAS,SAAS,EAAE,CAAC,CAAC,CACrE,KAAI,aACH,cAAc,SAAS,IAAI,eAAe,SAAS,QAAQ,2BAA2B;GACpF;GACA;GACA,SAAS;EACX,CAAC,CACH;EACF,cACE,0BACA,2BACA,iEACA,CAAC,OAAO,CACV;CACF;AACF;AAEA,SAAS,eAAe,WAAiD,gBAAiC;CACxG,IAAI,UAAU,MAAK,UAAS,MAAM,SAAS,GAAG,OAAO;CACrD,OAAO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cAAc,KAAA,GAAW,2CAA2C;EACzF,MAAM,KAAK,MAAM,SAAS,IAAI;CAChC,OACE,MAAM,aAAa,cAAc,KAAA,GAAW,+BAA+B,MAAM,MAAM;CAG3F,OAAO,MAAM,KAAK,IAAI;AACxB;AAEA,SAAgB,cACd,QAC2E;CAC3E,QAAQ,QAAR;EACE,KAAK,YACH,OAAO;EACT,KAAK,WACH,OAAO;EACT,KAAK,SACH,MAAM,aAAa,cAAc,KAAA,GAAW,yBAAyB;EACvE,KAAK,aACH,OAAO;CACX;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/acp/runtime.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAGpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAGpD,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,iBAAiB,EAC1B,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,UAAU,CAAM,GAC/C,OAAO,CAAC,iBAAiB,CAAC,
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/acp/runtime.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAGpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAGpD,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,iBAAiB,EAC1B,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,UAAU,CAAM,GAC/C,OAAO,CAAC,iBAAiB,CAAC,CAiG5B"}
|
package/dist/acp/runtime.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { withCleanupFailure } from "./errors.js";
|
|
2
1
|
import { loadSettings, resolveDefaultThinkingLevel } from "../onboarding/settings.js";
|
|
2
|
+
import { withCleanupFailure } from "./errors.js";
|
|
3
3
|
import { createMastraCode } from "../index.js";
|
|
4
4
|
import { RequestError } from "@agentclientprotocol/sdk";
|
|
5
5
|
//#region src/acp/runtime.ts
|
|
@@ -47,17 +47,25 @@ async function createAcpSession(request, options = {}) {
|
|
|
47
47
|
getSkills: async () => (await result.controller.resolveWorkspace({ session: result.session }))?.skills,
|
|
48
48
|
getThinkingLevel: () => result.session.state.get().thinkingLevel ?? resolveDefaultThinkingLevel(settings, result.session.mode.get()).level,
|
|
49
49
|
cleanup: () => cleanupPromise ??= (async () => {
|
|
50
|
-
result.session.abort();
|
|
51
|
-
result.session.thread.detachFromCurrent();
|
|
52
|
-
result.stopPluginSignalProviders();
|
|
53
|
-
result.githubSignals?.stopAllPolling();
|
|
54
50
|
await Promise.allSettled([
|
|
55
|
-
result.session.
|
|
56
|
-
result.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
result.
|
|
51
|
+
Promise.resolve().then(() => result.session.abort()),
|
|
52
|
+
Promise.resolve().then(() => result.session.thread.detachFromCurrent()),
|
|
53
|
+
Promise.resolve().then(() => result.stopPluginSignalProviders()),
|
|
54
|
+
Promise.resolve().then(() => result.githubSignals?.stopAllPolling()),
|
|
55
|
+
Promise.resolve().then(() => result.threadScheduler.stop())
|
|
56
|
+
]);
|
|
57
|
+
let dispatchTimeout;
|
|
58
|
+
await Promise.race([Promise.resolve().then(() => result.stopNotificationDispatch()).catch(() => {}), new Promise((resolve) => {
|
|
59
|
+
dispatchTimeout = setTimeout(resolve, 2e3);
|
|
60
|
+
dispatchTimeout.unref();
|
|
61
|
+
})]).finally(() => clearTimeout(dispatchTimeout));
|
|
62
|
+
await Promise.allSettled([
|
|
63
|
+
Promise.resolve().then(() => result.session.thread.clearAndReleaseLock()),
|
|
64
|
+
Promise.resolve().then(() => result.mcpManager?.disconnect()),
|
|
65
|
+
Promise.resolve().then(() => result.controller.getMastra()?.stopWorkers()),
|
|
66
|
+
Promise.resolve().then(() => result.controller.stopIntervals())
|
|
60
67
|
]);
|
|
68
|
+
await Promise.resolve().then(() => result.signalsPubSub?.close?.()).catch(() => {});
|
|
61
69
|
await result.storage.close();
|
|
62
70
|
})()
|
|
63
71
|
};
|
package/dist/acp/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.js","names":[],"sources":["../../src/acp/runtime.ts"],"sourcesContent":["import { RequestError } from '@agentclientprotocol/sdk';\nimport type { NewSessionRequest } from '@agentclientprotocol/sdk';\nimport { createMastraCode } from '../index.js';\nimport type { MastraCodeConfig } from '../index.js';\nimport type { McpServerConfig } from '../mcp/types.js';\nimport { loadSettings, resolveDefaultThinkingLevel } from '../onboarding/settings.js';\nimport type { AcpSessionRuntime } from './agent.js';\nimport { withCleanupFailure } from './errors.js';\n\nexport async function createAcpSession(\n request: NewSessionRequest,\n options: Pick<MastraCodeConfig, 'coAuthor'> = {},\n): Promise<AcpSessionRuntime> {\n const names = new Set<string>();\n const servers = request.mcpServers.map(server => {\n if ('type' in server && server.type === 'sse')\n throw RequestError.invalidParams(undefined, 'Legacy SSE MCP servers are unsupported; use HTTP or stdio');\n if (names.has(server.name))\n throw RequestError.invalidParams(undefined, `Duplicate MCP server name: ${server.name}`);\n names.add(server.name);\n const config: McpServerConfig =\n 'command' in server\n ? {\n command: server.command,\n args: server.args,\n env: Object.fromEntries(server.env.map(item => [item.name, item.value])),\n cwd: request.cwd,\n }\n : { url: server.url, headers: Object.fromEntries(server.headers.map(item => [item.name, item.value])) };\n return [server.name, config] as const;\n });\n const result = await createMastraCode({\n coAuthor: options.coAuthor,\n cwd: request.cwd,\n initialState: {\n projectPath: request.cwd,\n yolo: false,\n permissionRules: { categories: {}, tools: { ask_user: 'deny' } },\n },\n mcpServers: Object.fromEntries(servers),\n // ACP clients can answer permission requests, but have no free-text tool response method.\n disabledTools: ['ask_user'],\n unixSocketPubSub: false,\n disableMcp: false,\n disableHooks: false,\n // Project environment files must not mutate other ACP sessions.\n disableEnvFile: true,\n });\n const settings = loadSettings();\n let cleanupPromise: Promise<void> | undefined;\n const runtime: AcpSessionRuntime = {\n controller: result.controller,\n session: result.session,\n modes: result.controller.listModes(),\n getSkills: async () => (await result.controller.resolveWorkspace({ session: result.session }))?.skills,\n getThinkingLevel: () =>\n result.session.state.get().thinkingLevel ??\n resolveDefaultThinkingLevel(settings, result.session.mode.get()).level,\n cleanup: () =>\n (cleanupPromise ??= (async () => {\n result.session.abort()
|
|
1
|
+
{"version":3,"file":"runtime.js","names":[],"sources":["../../src/acp/runtime.ts"],"sourcesContent":["import { RequestError } from '@agentclientprotocol/sdk';\nimport type { NewSessionRequest } from '@agentclientprotocol/sdk';\nimport { createMastraCode } from '../index.js';\nimport type { MastraCodeConfig } from '../index.js';\nimport type { McpServerConfig } from '../mcp/types.js';\nimport { loadSettings, resolveDefaultThinkingLevel } from '../onboarding/settings.js';\nimport type { AcpSessionRuntime } from './agent.js';\nimport { withCleanupFailure } from './errors.js';\n\nexport async function createAcpSession(\n request: NewSessionRequest,\n options: Pick<MastraCodeConfig, 'coAuthor'> = {},\n): Promise<AcpSessionRuntime> {\n const names = new Set<string>();\n const servers = request.mcpServers.map(server => {\n if ('type' in server && server.type === 'sse')\n throw RequestError.invalidParams(undefined, 'Legacy SSE MCP servers are unsupported; use HTTP or stdio');\n if (names.has(server.name))\n throw RequestError.invalidParams(undefined, `Duplicate MCP server name: ${server.name}`);\n names.add(server.name);\n const config: McpServerConfig =\n 'command' in server\n ? {\n command: server.command,\n args: server.args,\n env: Object.fromEntries(server.env.map(item => [item.name, item.value])),\n cwd: request.cwd,\n }\n : { url: server.url, headers: Object.fromEntries(server.headers.map(item => [item.name, item.value])) };\n return [server.name, config] as const;\n });\n const result = await createMastraCode({\n coAuthor: options.coAuthor,\n cwd: request.cwd,\n initialState: {\n projectPath: request.cwd,\n yolo: false,\n permissionRules: { categories: {}, tools: { ask_user: 'deny' } },\n },\n mcpServers: Object.fromEntries(servers),\n // ACP clients can answer permission requests, but have no free-text tool response method.\n disabledTools: ['ask_user'],\n unixSocketPubSub: false,\n disableMcp: false,\n disableHooks: false,\n // Project environment files must not mutate other ACP sessions.\n disableEnvFile: true,\n });\n const settings = loadSettings();\n let cleanupPromise: Promise<void> | undefined;\n const runtime: AcpSessionRuntime = {\n controller: result.controller,\n session: result.session,\n modes: result.controller.listModes(),\n getSkills: async () => (await result.controller.resolveWorkspace({ session: result.session }))?.skills,\n getThinkingLevel: () =>\n result.session.state.get().thinkingLevel ??\n resolveDefaultThinkingLevel(settings, result.session.mode.get()).level,\n cleanup: () =>\n (cleanupPromise ??= (async () => {\n await Promise.allSettled([\n Promise.resolve().then(() => result.session.abort()),\n Promise.resolve().then(() => result.session.thread.detachFromCurrent()),\n Promise.resolve().then(() => result.stopPluginSignalProviders()),\n Promise.resolve().then(() => result.githubSignals?.stopAllPolling()),\n Promise.resolve().then(() => result.threadScheduler.stop()),\n ]);\n // Leave time for the other cleanup phases within ACP's shared 10-second cap.\n // A timed-out dispatch may still finish later; this grace is not cancellation.\n let dispatchTimeout: ReturnType<typeof setTimeout> | undefined;\n await Promise.race([\n Promise.resolve()\n .then(() => result.stopNotificationDispatch())\n .catch(() => {}),\n new Promise<void>(resolve => {\n dispatchTimeout = setTimeout(resolve, 2_000);\n dispatchTimeout.unref();\n }),\n ]).finally(() => clearTimeout(dispatchTimeout));\n await Promise.allSettled([\n Promise.resolve().then(() => result.session.thread.clearAndReleaseLock()),\n Promise.resolve().then(() => result.mcpManager?.disconnect()),\n Promise.resolve().then(() => result.controller.getMastra()?.stopWorkers()),\n Promise.resolve().then(() => result.controller.stopIntervals()),\n ]);\n await Promise.resolve()\n .then(() => (result.signalsPubSub as { close?: () => void | Promise<void> } | undefined)?.close?.())\n .catch(() => {});\n await result.storage.close();\n })()),\n };\n try {\n const status = await result.mcpManager?.initInBackground();\n const failed = status?.failed.filter(server => names.has(server.name)) ?? [];\n if (failed.length)\n throw RequestError.internalError(\n undefined,\n failed.map(server => `MCP server ${server.name}: ${server.error ?? 'connection failed'}`).join('; '),\n );\n\n return runtime;\n } catch (error) {\n try {\n await runtime.cleanup?.();\n } catch (cleanupError) {\n throw withCleanupFailure(error, cleanupError);\n }\n throw error;\n }\n}\n"],"mappings":";;;;;AASA,eAAsB,iBACpB,SACA,UAA8C,CAAC,GACnB;CAC5B,MAAM,wBAAQ,IAAI,IAAY;CAC9B,MAAM,UAAU,QAAQ,WAAW,KAAI,WAAU;EAC/C,IAAI,UAAU,UAAU,OAAO,SAAS,OACtC,MAAM,aAAa,cAAc,KAAA,GAAW,2DAA2D;EACzG,IAAI,MAAM,IAAI,OAAO,IAAI,GACvB,MAAM,aAAa,cAAc,KAAA,GAAW,8BAA8B,OAAO,MAAM;EACzF,MAAM,IAAI,OAAO,IAAI;EACrB,MAAM,SACJ,aAAa,SACT;GACE,SAAS,OAAO;GAChB,MAAM,OAAO;GACb,KAAK,OAAO,YAAY,OAAO,IAAI,KAAI,SAAQ,CAAC,KAAK,MAAM,KAAK,KAAK,CAAC,CAAC;GACvE,KAAK,QAAQ;EACf,IACA;GAAE,KAAK,OAAO;GAAK,SAAS,OAAO,YAAY,OAAO,QAAQ,KAAI,SAAQ,CAAC,KAAK,MAAM,KAAK,KAAK,CAAC,CAAC;EAAE;EAC1G,OAAO,CAAC,OAAO,MAAM,MAAM;CAC7B,CAAC;CACD,MAAM,SAAS,MAAM,iBAAiB;EACpC,UAAU,QAAQ;EAClB,KAAK,QAAQ;EACb,cAAc;GACZ,aAAa,QAAQ;GACrB,MAAM;GACN,iBAAiB;IAAE,YAAY,CAAC;IAAG,OAAO,EAAE,UAAU,OAAO;GAAE;EACjE;EACA,YAAY,OAAO,YAAY,OAAO;EAEtC,eAAe,CAAC,UAAU;EAC1B,kBAAkB;EAClB,YAAY;EACZ,cAAc;EAEd,gBAAgB;CAClB,CAAC;CACD,MAAM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CAAC,CAAC,WAAW,OAAO,WAAW,cAAc,CAAC;GAChE,CAAC;GACD,MAAM,QAAQ,QAAQ,CAAC,CACpB,WAAY,OAAO,eAAsE,QAAQ,CAAC,CAAC,CACnG,YAAY,CAAC,CAAC;GACjB,MAAM,OAAO,QAAQ,MAAM;EAC7B,EAAA,CAAG;CACP;CACA,IAAI;EAEF,MAAM,UAAS,MADM,OAAO,YAAY,iBAAiB,EAAA,EAClC,OAAO,QAAO,WAAU,MAAM,IAAI,OAAO,IAAI,CAAC,KAAK,CAAC;EAC3E,IAAI,OAAO,QACT,MAAM,aAAa,cACjB,KAAA,GACA,OAAO,KAAI,WAAU,cAAc,OAAO,KAAK,IAAI,OAAO,SAAS,qBAAqB,CAAC,CAAC,KAAK,IAAI,CACrG;EAEF,OAAO;CACT,SAAS,OAAO;EACd,IAAI;GACF,MAAM,QAAQ,UAAU;EAC1B,SAAS,cAAc;GACrB,MAAM,mBAAmB,OAAO,YAAY;EAC9C;EACA,MAAM;CACR;AACF"}
|
package/dist/agents/memory.d.ts
CHANGED
|
@@ -24,7 +24,9 @@ export declare function hasSubconsciousTools(vector: MastraVector | undefined, s
|
|
|
24
24
|
* Reads OM thresholds from controller state via requestContext.
|
|
25
25
|
* Model functions also read from requestContext (no mutable bridge needed).
|
|
26
26
|
*/
|
|
27
|
-
export declare function getDynamicMemory(storage: MastraCompositeStore, vector?: MastraVector, settingsPath?: string
|
|
27
|
+
export declare function getDynamicMemory(storage: MastraCompositeStore, vector?: MastraVector, settingsPath?: string, options?: {
|
|
28
|
+
disableSettingsOmSeed?: boolean;
|
|
29
|
+
}): ({ requestContext }: {
|
|
28
30
|
requestContext: RequestContext;
|
|
29
31
|
}) => Memory;
|
|
30
32
|
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/agents/memory.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,MAAM,EAAgB,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/agents/memory.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,MAAM,EAAgB,MAAM,gBAAgB,CAAC;AAOtD,OAAO,EAAE,sBAAsB,EAAiC,MAAM,uBAAuB,CAAC;AAG9F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAoFpD,OAAO,EAAE,sBAAsB,EAAE,CAAC;AA8BlC;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,YAAY,GAAG,SAAS,GAAG,OAAO,CAE/E;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,GAAG,SAAS,EAAE,KAAK,EAAE,eAAe,GAAG,SAAS,GAAG,OAAO,CAElH;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,oBAAoB,EAC7B,MAAM,CAAC,EAAE,YAAY,EACrB,YAAY,CAAC,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE;IAAE,qBAAqB,CAAC,EAAE,OAAO,CAAA;CAAE,IAgBrC,oBAAoB;IAAE,cAAc,EAAE,cAAc,CAAA;CAAE,YA0G/D"}
|
package/dist/agents/memory.js
CHANGED
|
@@ -3,21 +3,12 @@ import { getOmScope } from "../utils/project.js";
|
|
|
3
3
|
import { loadSettings } from "../onboarding/settings.js";
|
|
4
4
|
import { LOCAL_KNOWLEDGE_ORG_ID, resolveKnowledgeScopeIdentity } from "../knowledge-scope.js";
|
|
5
5
|
import "../providers/anthropic-prompt-cache.js";
|
|
6
|
-
import { resolveModel
|
|
6
|
+
import { resolveModel } from "./model.js";
|
|
7
7
|
import { fastembed } from "@mastra/fastembed";
|
|
8
8
|
import { Memory, Subconscious } from "@mastra/memory";
|
|
9
9
|
//#region src/agents/memory.ts
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
* 1. The explicit role override (`observerModelOverride` / `reflectorModelOverride`).
|
|
13
|
-
* 2. The active mode pack's optional `models.memory`, walking the pack's fallback
|
|
14
|
-
* chain so OM fails over alongside (and independently of) the main agent.
|
|
15
|
-
* The pending pack-hop marker wins over the settled pack id so an immediate
|
|
16
|
-
* retrigger observes on the landed pack.
|
|
17
|
-
* 3. The standalone OM configuration seeded into controller state
|
|
18
|
-
* (`observerModelId` / `reflectorModelId`), then the default OM model.
|
|
19
|
-
*/
|
|
20
|
-
function resolveOmRoleModelForRequest(role, requestContext, settingsPath) {
|
|
10
|
+
/** Resolve one OM role's model for this invocation. */
|
|
11
|
+
function resolveOmRoleModelForRequest(role, requestContext, settingsPath, options) {
|
|
21
12
|
const controller = requestContext.get("controller");
|
|
22
13
|
const state = controller?.getState();
|
|
23
14
|
const resolveOptions = {
|
|
@@ -25,15 +16,35 @@ function resolveOmRoleModelForRequest(role, requestContext, settingsPath) {
|
|
|
25
16
|
requestContext,
|
|
26
17
|
anthropicPromptCacheScope: "system"
|
|
27
18
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
19
|
+
if (!options?.disableSettingsOmSeed) {
|
|
20
|
+
const settings = loadSettings(settingsPath);
|
|
21
|
+
const roleOverride = role === "observer" ? settings.models?.observerModelOverride : settings.models?.reflectorModelOverride;
|
|
22
|
+
if (roleOverride) return resolveModel(roleOverride, resolveOptions);
|
|
23
|
+
}
|
|
24
|
+
const pending = state?.mastracodePendingModelFallback;
|
|
25
|
+
const sameThreadPending = pending && (pending.threadId === void 0 || pending.threadId === controller?.threadId) ? pending : void 0;
|
|
26
|
+
const routeEntries = state?.modelRoute?.entries?.slice(0, 32) ?? [];
|
|
27
|
+
const pendingIndex = sameThreadPending ? routeEntries.findIndex((entry) => entry.id === sameThreadPending.toEntryId) : -1;
|
|
28
|
+
const memoryRoute = pendingIndex >= 0 ? routeEntries.slice(pendingIndex) : routeEntries;
|
|
29
|
+
const seenModelIds = /* @__PURE__ */ new Set();
|
|
30
|
+
const appearances = /* @__PURE__ */ new Map();
|
|
31
|
+
const entries = [];
|
|
32
|
+
for (const entry of memoryRoute) {
|
|
33
|
+
if (!entry.memoryModelId || seenModelIds.has(entry.memoryModelId)) continue;
|
|
34
|
+
seenModelIds.add(entry.memoryModelId);
|
|
35
|
+
const occurrence = (appearances.get(entry.id) ?? 0) + 1;
|
|
36
|
+
appearances.set(entry.id, occurrence);
|
|
37
|
+
try {
|
|
38
|
+
entries.push({
|
|
39
|
+
id: `${entry.id}:memory${occurrence === 1 ? "" : `#${occurrence}`}`,
|
|
40
|
+
model: resolveModel(entry.memoryModelId, resolveOptions)
|
|
41
|
+
});
|
|
42
|
+
} catch {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
36
45
|
}
|
|
46
|
+
if (entries.length === 1) return entries[0].model;
|
|
47
|
+
if (entries.length > 1) return entries;
|
|
37
48
|
return resolveModel((role === "observer" ? state?.observerModelId : state?.reflectorModelId) ?? DEFAULT_OM_MODEL_ID, resolveOptions);
|
|
38
49
|
}
|
|
39
50
|
const DYNAMIC_AGENTS_MD_INSTRUCTION = "Messages wrapped in <system-reminder type=\"dynamic-agents-md\" ...>...</system-reminder> are ephemeral project-context instructions injected from files on disk. Do NOT observe or extract information from these messages — they are reloaded automatically when needed and should not be stored in memory.";
|
|
@@ -94,11 +105,11 @@ function hasSubconsciousTools(vector, state) {
|
|
|
94
105
|
* Reads OM thresholds from controller state via requestContext.
|
|
95
106
|
* Model functions also read from requestContext (no mutable bridge needed).
|
|
96
107
|
*/
|
|
97
|
-
function getDynamicMemory(storage, vector, settingsPath) {
|
|
108
|
+
function getDynamicMemory(storage, vector, settingsPath, options) {
|
|
98
109
|
let cachedMemory = null;
|
|
99
110
|
let cachedMemoryKey = null;
|
|
100
|
-
const getObserverModel = ({ requestContext }) => resolveOmRoleModelForRequest("observer", requestContext, settingsPath);
|
|
101
|
-
const getReflectorModel = ({ requestContext }) => resolveOmRoleModelForRequest("reflector", requestContext, settingsPath);
|
|
111
|
+
const getObserverModel = ({ requestContext }) => resolveOmRoleModelForRequest("observer", requestContext, settingsPath, options);
|
|
112
|
+
const getReflectorModel = ({ requestContext }) => resolveOmRoleModelForRequest("reflector", requestContext, settingsPath, options);
|
|
102
113
|
return ({ requestContext }) => {
|
|
103
114
|
const controller = requestContext.get("controller");
|
|
104
115
|
const state = controller?.getState();
|