@mastra/code-sdk 1.11.0-alpha.4 → 1.11.0-alpha.5

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.
@@ -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(_request: InitializeRequest): Promise<InitializeResponse>;
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;
@@ -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,mBAA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Aa7B,OAAO,CAAC,aAAa;IAqCrB,OAAO,CAAC,aAAa;IAMf,sBAAsB,CAAC,MAAM,EAAE,6BAA6B,GAAG,OAAO,CAAC,8BAA8B,CAAC;IA0BtG,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(_request) {
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
- throw RequestError.invalidParams(void 0, "Configure authentication through Mastra Code before starting ACP");
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
- let models;
92
- try {
93
- const available = await runtime.controller.listAvailableModels();
94
- models = {
95
- currentModelId: runtime.session.model.get() ?? "",
96
- availableModels: includeCurrentModel([...new Map(available.filter((model) => model.hasApiKey || model.id === runtime.session.model.get()).map((model) => [model.id, {
97
- modelId: model.id,
98
- name: model.hasApiKey ? model.id : `${model.id} (provider not configured)`
99
- }])).values()], runtime.session.model.get() ?? "")
100
- };
101
- } catch {}
164
+ const defaultModelId = credentialedDefaultModel(available, runtime.session.model.get() ?? "");
165
+ if (defaultModelId) await runtime.session.model.switch({ modelId: 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?.availableModels ?? [],
176
+ models: models.availableModels,
106
177
  state: null,
107
178
  queue: Promise.resolve(),
108
179
  turns: /* @__PURE__ */ new Set(),
@@ -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,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AAY;GACpC,IAAI,KAAK,UAAU;GACnB,IACE,CAAC,oBAAoB,MAAM,QAAQ,MAAM,QAAQ,MAAM,IAAI,KAAK,EAAE,CAAC,CAAC,MAClE,UAAS,MAAM,YAAY,OAAO,OACpC,GAEA,MAAM,aAAa,cACjB,KAAA,GACA,iFACF;GAEF,MAAM,MAAM,QAAQ,MAAM,OAAO,EAAE,SAAS,OAAO,QAAQ,CAAC;EAC9D,CAAC;CACH;AACF;AAEA,SAAS,oBAAoB,QAAgC,gBAAgD;CAE3G,OAAO,kBAAkB,CAAC,OAAO,MAAK,UAAS,MAAM,YAAY,cAAc,IAC3E,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({ modelId: 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): 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":";;;;;;;;;;;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,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AAM,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,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
@@ -1,7 +1,7 @@
1
- import { isThinkingLevelSetting } from "../thinking.js";
1
+ import { listBuiltinModePacks, resolveModePackFallbackChain } from "../onboarding/packs.js";
2
2
  import { MASTRA_GATEWAY_PREFIX, remapOpenAIModelForCodexOAuth, stripMastraGatewayPrefix } from "../providers/model-ids.js";
3
3
  import { ProviderAuthRequiredError } from "../auth/provider-auth-error.js";
4
- import { listBuiltinModePacks, resolveModePackFallbackChain } from "../onboarding/packs.js";
4
+ import { isThinkingLevelSetting } from "../thinking.js";
5
5
  import { findModePackForModel, loadSettings, resolveDefaultThinkingLevel, resolveModePackModels, stripMastraCodeCustomProviderPrefix } from "../onboarding/settings.js";
6
6
  import { hasCredentialStoreProvider, resolveCredentialStore, resolveTenantFromRequestContext, setCredentialStoreProvider } from "./credential-resolver.js";
7
7
  import { getRequestAccountSelection, isRequestAccountRoutingExhausted } from "../auth/account-routing-context.js";
@@ -1,7 +1,7 @@
1
1
  import { DEFAULT_CONFIG_DIR } from "../../constants.js";
2
2
  import { dirname, isAbsolute, join, normalize, relative, resolve, sep } from "path";
3
- import { execFileSync } from "child_process";
4
3
  import { existsSync, readFileSync, realpathSync, statSync } from "fs";
4
+ import { execFileSync } from "child_process";
5
5
  import { homedir } from "os";
6
6
  //#region src/agents/prompts/agent-instructions.ts
7
7
  /**
@@ -1,6 +1,6 @@
1
1
  import { getOAuthProviders } from "./storage.js";
2
- import { ProviderAuthRequiredError } from "./provider-auth-error.js";
3
2
  import { resolveModePackFallbackChain } from "../onboarding/packs.js";
3
+ import { ProviderAuthRequiredError } from "./provider-auth-error.js";
4
4
  import { findModePackForModel, loadSettings, resolveModePackModels } from "../onboarding/settings.js";
5
5
  import { resolveCredentialStore } from "../agents/credential-resolver.js";
6
6
  import { getRequestAccountSelection, isRequestAccountRoutingExhausted, markRequestAccountRoutingExhausted, setRequestAccountSelection } from "./account-routing-context.js";
@@ -170,7 +170,8 @@ async function loginKimiCoding(callbacks) {
170
170
  const pending = await startKimiCodingDeviceLogin({ signal: callbacks.signal });
171
171
  callbacks.onAuth({
172
172
  url: pending.url,
173
- instructions: pending.instructions
173
+ instructions: pending.instructions,
174
+ userCode: pending.userCode
174
175
  });
175
176
  callbacks.onProgress?.("Waiting for Kimi For Coding device authorization...");
176
177
  return pollDeviceCodeUntilComplete({
@@ -1 +1 @@
1
- {"version":3,"file":"kimi-coding.js","names":[],"sources":["../../../src/auth/providers/kimi-coding.ts"],"sourcesContent":["import { arch, hostname, platform, release } from 'node:os';\nimport { getCurrentVersion } from '../../utils/update-check.js';\nimport {\n abortableSleep,\n createDeviceCodePollState,\n pollDeviceCodeUntilComplete,\n stepDeviceCodePoll,\n} from '../device-code.js';\nimport type { DeviceCodePollOutcome, DeviceCodePollState } from '../device-code.js';\nimport type { OAuthCredentials, OAuthLoginCallbacks, OAuthProviderInterface } from '../types.js';\n\nconst CLIENT_ID = '17e5f671-d194-4dfb-9706-5516cb48c098';\nconst OAUTH_HOST = 'https://auth.kimi.com';\nconst DEVICE_ID_PATTERN = /^[0-9a-f]{32}$/;\nconst DEFAULT_EXPIRES_IN_SECONDS = 15 * 60;\nconst DEFAULT_POLL_INTERVAL_SECONDS = 5;\nconst REQUEST_TIMEOUT_MS = 30_000;\nconst REFRESH_MAX_RETRIES = 3;\n\nfunction asciiHeaderValue(value: string): string {\n const sanitized = value.replace(/[^\\x20-\\x7E]/g, '').trim();\n return sanitized || 'unknown';\n}\n\nconst KIMI_DEVICE_DETAILS = {\n 'X-Msh-Platform': 'mastracode',\n 'X-Msh-Version': asciiHeaderValue(getCurrentVersion()),\n 'X-Msh-Device-Name': asciiHeaderValue(hostname()),\n 'X-Msh-Device-Model': asciiHeaderValue(`${platform()} ${arch()}`),\n 'X-Msh-Os-Version': asciiHeaderValue(release()),\n};\n\nexport function createKimiCodingDeviceId(): string {\n return globalThis.crypto.randomUUID().replaceAll('-', '');\n}\n\nexport function isKimiCodingDeviceId(value: unknown): value is string {\n return typeof value === 'string' && DEVICE_ID_PATTERN.test(value);\n}\n\nexport function getKimiCodingDeviceHeaders(deviceId: string): Record<string, string> {\n if (!isKimiCodingDeviceId(deviceId)) {\n throw new Error('Kimi For Coding credentials have an invalid device ID. Please reconnect the account.');\n }\n return { ...KIMI_DEVICE_DETAILS, 'X-Msh-Device-Id': deviceId };\n}\n\nfunction requestSignal(signal?: AbortSignal): AbortSignal {\n const timeout = AbortSignal.timeout(REQUEST_TIMEOUT_MS);\n return signal ? AbortSignal.any([timeout, signal]) : timeout;\n}\n\nfunction trustedHttpUrl(value: unknown): string | null {\n if (typeof value !== 'string' || !value) return null;\n try {\n const url = new URL(value);\n return url.protocol === 'https:' ? url.href : null;\n } catch {\n return null;\n }\n}\n\nasync function readJson(response: Response): Promise<Record<string, unknown> | null> {\n try {\n const value = (await response.json()) as unknown;\n return value && typeof value === 'object' ? (value as Record<string, unknown>) : null;\n } catch {\n return null;\n }\n}\n\nfunction credentialsFromTokenResponse(value: unknown, operation: string, deviceId: string): OAuthCredentials {\n const data = (value ?? {}) as Record<string, unknown>;\n const access = data.access_token;\n const refresh = data.refresh_token;\n const expiresIn = data.expires_in;\n if (\n typeof access !== 'string' ||\n !access ||\n typeof refresh !== 'string' ||\n !refresh ||\n typeof expiresIn !== 'number' ||\n !Number.isFinite(expiresIn) ||\n expiresIn <= 0\n ) {\n throw new Error(`Kimi For Coding token ${operation} response missing fields`);\n }\n return { access, refresh, expires: Date.now() + expiresIn * 1000, deviceId };\n}\n\nexport interface KimiCodingDeviceLoginPending {\n clientId: string;\n deviceId: string;\n deviceCode: string;\n userCode: string;\n url: string;\n instructions: string;\n state: DeviceCodePollState;\n}\n\nexport type KimiCodingDevicePollResult =\n | { status: 'complete'; credentials: OAuthCredentials }\n | { status: 'pending'; nextPollMs: number; pending: KimiCodingDeviceLoginPending }\n | { status: 'failed'; error: string };\n\nexport async function startKimiCodingDeviceLogin(options?: {\n signal?: AbortSignal;\n}): Promise<KimiCodingDeviceLoginPending> {\n const clientId = CLIENT_ID;\n const deviceId = createKimiCodingDeviceId();\n const response = await fetch(`${OAUTH_HOST}/api/oauth/device_authorization`, {\n method: 'POST',\n headers: {\n ...getKimiCodingDeviceHeaders(deviceId),\n 'Content-Type': 'application/x-www-form-urlencoded',\n Accept: 'application/json',\n },\n body: new URLSearchParams({ client_id: clientId }).toString(),\n signal: requestSignal(options?.signal),\n });\n if (!response.ok) {\n throw new Error(`Kimi For Coding device authorization failed: ${response.status}`);\n }\n\n const data = await readJson(response);\n const deviceCode = data?.device_code;\n const userCode = data?.user_code;\n const verificationUri = trustedHttpUrl(data?.verification_uri);\n const verificationUriComplete = trustedHttpUrl(data?.verification_uri_complete);\n if (\n typeof deviceCode !== 'string' ||\n !deviceCode ||\n typeof userCode !== 'string' ||\n !userCode ||\n !verificationUri ||\n !verificationUriComplete\n ) {\n throw new Error('Invalid Kimi For Coding device authorization response');\n }\n\n const interval = data?.interval;\n const expiresIn = data?.expires_in;\n return {\n clientId,\n deviceId,\n deviceCode,\n userCode,\n url: verificationUriComplete,\n instructions: `Enter code: ${userCode}`,\n state: createDeviceCodePollState({\n intervalSeconds:\n typeof interval === 'number' && Number.isFinite(interval) && interval > 0\n ? interval\n : DEFAULT_POLL_INTERVAL_SECONDS,\n expiresInSeconds:\n typeof expiresIn === 'number' && Number.isFinite(expiresIn) && expiresIn > 0\n ? expiresIn\n : DEFAULT_EXPIRES_IN_SECONDS,\n }),\n };\n}\n\nasync function pollKimiCodingTokenOnce(\n pending: KimiCodingDeviceLoginPending,\n signal?: AbortSignal,\n): Promise<DeviceCodePollOutcome<OAuthCredentials>> {\n const response = await fetch(`${OAUTH_HOST}/api/oauth/token`, {\n method: 'POST',\n headers: {\n ...getKimiCodingDeviceHeaders(pending.deviceId),\n 'Content-Type': 'application/x-www-form-urlencoded',\n Accept: 'application/json',\n },\n body: new URLSearchParams({\n client_id: pending.clientId,\n device_code: pending.deviceCode,\n grant_type: 'urn:ietf:params:oauth:grant-type:device_code',\n }).toString(),\n signal: requestSignal(signal),\n });\n const data = await readJson(response);\n if (response.ok && typeof data?.access_token === 'string') {\n try {\n return { status: 'complete', result: credentialsFromTokenResponse(data, 'poll', pending.deviceId) };\n } catch (error) {\n return { status: 'failed', error: error instanceof Error ? error.message : String(error) };\n }\n }\n\n const error = data?.error;\n if (error === 'authorization_pending') return { status: 'pending' };\n if (error === 'slow_down') {\n return {\n status: 'slow_down',\n intervalSeconds: typeof data?.interval === 'number' && data.interval > 0 ? data.interval : undefined,\n };\n }\n if (error === 'expired_token') {\n return { status: 'failed', error: 'Kimi For Coding authorization expired. Please restart login.' };\n }\n if (error === 'access_denied') return { status: 'failed', error: 'Kimi For Coding login was denied.' };\n return {\n status: 'failed',\n error: `Kimi For Coding token request failed: ${response.status}`,\n };\n}\n\nexport async function pollKimiCodingDeviceLogin(\n pending: KimiCodingDeviceLoginPending,\n options?: { signal?: AbortSignal },\n): Promise<KimiCodingDevicePollResult> {\n const step = await stepDeviceCodePoll(pending.state, () => pollKimiCodingTokenOnce(pending, options?.signal));\n if (step.status === 'complete') return { status: 'complete', credentials: step.result };\n if (step.status === 'failed') return { status: 'failed', error: step.error };\n return {\n status: 'pending',\n nextPollMs: step.nextPollMs,\n pending: { ...pending, state: step.state },\n };\n}\n\nexport async function loginKimiCoding(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {\n const pending = await startKimiCodingDeviceLogin({ signal: callbacks.signal });\n callbacks.onAuth({ url: pending.url, instructions: pending.instructions });\n callbacks.onProgress?.('Waiting for Kimi For Coding device authorization...');\n return pollDeviceCodeUntilComplete({\n state: pending.state,\n pollOnce: () => pollKimiCodingTokenOnce(pending, callbacks.signal),\n signal: callbacks.signal,\n });\n}\n\nexport async function refreshKimiCodingToken(\n refreshToken: string,\n options: { signal?: AbortSignal; deviceId: string },\n): Promise<OAuthCredentials> {\n const clientId = CLIENT_ID;\n const deviceHeaders = getKimiCodingDeviceHeaders(options.deviceId);\n let lastError: Error | undefined;\n for (let attempt = 0; attempt <= REFRESH_MAX_RETRIES; attempt++) {\n if (attempt > 0) await abortableSleep(1000 * 2 ** (attempt - 1), options.signal);\n let response: Response;\n try {\n response = await fetch(`${OAUTH_HOST}/api/oauth/token`, {\n method: 'POST',\n headers: {\n ...deviceHeaders,\n 'Content-Type': 'application/x-www-form-urlencoded',\n Accept: 'application/json',\n },\n body: new URLSearchParams({\n client_id: clientId,\n grant_type: 'refresh_token',\n refresh_token: refreshToken,\n }).toString(),\n signal: requestSignal(options.signal),\n });\n } catch (error) {\n lastError = error instanceof Error ? error : new Error(String(error));\n if (attempt === REFRESH_MAX_RETRIES) throw lastError;\n continue;\n }\n const data = await readJson(response);\n if (response.ok) return credentialsFromTokenResponse(data, 'refresh', options.deviceId);\n\n const error = typeof data?.error === 'string' ? ` ${data.error}` : '';\n lastError = new Error(`Kimi For Coding token refresh failed: ${response.status}${error}`);\n const retryable = response.status === 429 || response.status >= 500;\n if (!retryable || attempt === REFRESH_MAX_RETRIES) throw lastError;\n }\n throw lastError ?? new Error('Kimi For Coding token refresh failed');\n}\n\nexport const kimiCodingOAuthProvider: OAuthProviderInterface = {\n id: 'kimi-for-coding',\n name: 'Kimi For Coding',\n login: loginKimiCoding,\n refreshToken: credentials =>\n refreshKimiCodingToken(credentials.refresh, {\n deviceId: typeof credentials.deviceId === 'string' ? credentials.deviceId : '',\n }),\n getApiKey: credentials => credentials.access,\n};\n"],"mappings":";;;;AAWA,MAAM,YAAY;AAClB,MAAM,aAAa;AACnB,MAAM,oBAAoB;AAC1B,MAAM,6BAA6B;AACnC,MAAM,gCAAgC;AACtC,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAE5B,SAAS,iBAAiB,OAAuB;CAE/C,OADkB,MAAM,QAAQ,iBAAiB,EAAE,CAAC,CAAC,KACtC,KAAK;AACtB;AAEA,MAAM,sBAAsB;CAC1B,kBAAkB;CAClB,iBAAiB,iBAAiB,kBAAkB,CAAC;CACrD,qBAAqB,iBAAiB,SAAS,CAAC;CAChD,sBAAsB,iBAAiB,GAAG,SAAS,EAAE,GAAG,KAAK,GAAG;CAChE,oBAAoB,iBAAiB,QAAQ,CAAC;AAChD;AAEA,SAAgB,2BAAmC;CACjD,OAAO,WAAW,OAAO,WAAW,CAAC,CAAC,WAAW,KAAK,EAAE;AAC1D;AAEA,SAAgB,qBAAqB,OAAiC;CACpE,OAAO,OAAO,UAAU,YAAY,kBAAkB,KAAK,KAAK;AAClE;AAEA,SAAgB,2BAA2B,UAA0C;CACnF,IAAI,CAAC,qBAAqB,QAAQ,GAChC,MAAM,IAAI,MAAM,sFAAsF;CAExG,OAAO;EAAE,GAAG;EAAqB,mBAAmB;CAAS;AAC/D;AAEA,SAAS,cAAc,QAAmC;CACxD,MAAM,UAAU,YAAY,QAAQ,kBAAkB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GACF,WAAW,MAAM,MAAM,GAAG,WAAW,mBAAmB;IACtD,QAAQ;IACR,SAAS;KACP,GAAG;KACH,gBAAgB;KAChB,QAAQ;IACV;IACA,MAAM,IAAI,gBAAgB;KACxB,WAAW;KACX,YAAY;KACZ,eAAe;IACjB,CAAC,CAAC,CAAC,SAAS;IACZ,QAAQ,cAAc,QAAQ,MAAM;GACtC,CAAC;EACH,SAAS,OAAO;GACd,YAAY,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;GACpE,IAAI,YAAY,qBAAqB,MAAM;GAC3C;EACF;EACA,MAAM,OAAO,MAAM,SAAS,QAAQ;EACpC,IAAI,SAAS,IAAI,OAAO,6BAA6B,MAAM,WAAW,QAAQ,QAAQ;EAEtF,MAAM,QAAQ,OAAO,MAAM,UAAU,WAAW,IAAI,KAAK,UAAU;EACnE,4BAAY,IAAI,MAAM,yCAAyC,SAAS,SAAS,OAAO;EAExF,IAAI,EADc,SAAS,WAAW,OAAO,SAAS,UAAU,QAC9C,YAAY,qBAAqB,MAAM;CAC3D;CACA,MAAM,6BAAa,IAAI,MAAM,sCAAsC;AACrE;AAEA,MAAa,0BAAkD;CAC7D,IAAI;CACJ,MAAM;CACN,OAAO;CACP,eAAc,gBACZ,uBAAuB,YAAY,SAAS,EAC1C,UAAU,OAAO,YAAY,aAAa,WAAW,YAAY,WAAW,GAC9E,CAAC;CACH,YAAW,gBAAe,YAAY;AACxC"}
1
+ {"version":3,"file":"kimi-coding.js","names":[],"sources":["../../../src/auth/providers/kimi-coding.ts"],"sourcesContent":["import { arch, hostname, platform, release } from 'node:os';\nimport { getCurrentVersion } from '../../utils/update-check.js';\nimport {\n abortableSleep,\n createDeviceCodePollState,\n pollDeviceCodeUntilComplete,\n stepDeviceCodePoll,\n} from '../device-code.js';\nimport type { DeviceCodePollOutcome, DeviceCodePollState } from '../device-code.js';\nimport type { OAuthCredentials, OAuthLoginCallbacks, OAuthProviderInterface } from '../types.js';\n\nconst CLIENT_ID = '17e5f671-d194-4dfb-9706-5516cb48c098';\nconst OAUTH_HOST = 'https://auth.kimi.com';\nconst DEVICE_ID_PATTERN = /^[0-9a-f]{32}$/;\nconst DEFAULT_EXPIRES_IN_SECONDS = 15 * 60;\nconst DEFAULT_POLL_INTERVAL_SECONDS = 5;\nconst REQUEST_TIMEOUT_MS = 30_000;\nconst REFRESH_MAX_RETRIES = 3;\n\nfunction asciiHeaderValue(value: string): string {\n const sanitized = value.replace(/[^\\x20-\\x7E]/g, '').trim();\n return sanitized || 'unknown';\n}\n\nconst KIMI_DEVICE_DETAILS = {\n 'X-Msh-Platform': 'mastracode',\n 'X-Msh-Version': asciiHeaderValue(getCurrentVersion()),\n 'X-Msh-Device-Name': asciiHeaderValue(hostname()),\n 'X-Msh-Device-Model': asciiHeaderValue(`${platform()} ${arch()}`),\n 'X-Msh-Os-Version': asciiHeaderValue(release()),\n};\n\nexport function createKimiCodingDeviceId(): string {\n return globalThis.crypto.randomUUID().replaceAll('-', '');\n}\n\nexport function isKimiCodingDeviceId(value: unknown): value is string {\n return typeof value === 'string' && DEVICE_ID_PATTERN.test(value);\n}\n\nexport function getKimiCodingDeviceHeaders(deviceId: string): Record<string, string> {\n if (!isKimiCodingDeviceId(deviceId)) {\n throw new Error('Kimi For Coding credentials have an invalid device ID. Please reconnect the account.');\n }\n return { ...KIMI_DEVICE_DETAILS, 'X-Msh-Device-Id': deviceId };\n}\n\nfunction requestSignal(signal?: AbortSignal): AbortSignal {\n const timeout = AbortSignal.timeout(REQUEST_TIMEOUT_MS);\n return signal ? AbortSignal.any([timeout, signal]) : timeout;\n}\n\nfunction trustedHttpUrl(value: unknown): string | null {\n if (typeof value !== 'string' || !value) return null;\n try {\n const url = new URL(value);\n return url.protocol === 'https:' ? url.href : null;\n } catch {\n return null;\n }\n}\n\nasync function readJson(response: Response): Promise<Record<string, unknown> | null> {\n try {\n const value = (await response.json()) as unknown;\n return value && typeof value === 'object' ? (value as Record<string, unknown>) : null;\n } catch {\n return null;\n }\n}\n\nfunction credentialsFromTokenResponse(value: unknown, operation: string, deviceId: string): OAuthCredentials {\n const data = (value ?? {}) as Record<string, unknown>;\n const access = data.access_token;\n const refresh = data.refresh_token;\n const expiresIn = data.expires_in;\n if (\n typeof access !== 'string' ||\n !access ||\n typeof refresh !== 'string' ||\n !refresh ||\n typeof expiresIn !== 'number' ||\n !Number.isFinite(expiresIn) ||\n expiresIn <= 0\n ) {\n throw new Error(`Kimi For Coding token ${operation} response missing fields`);\n }\n return { access, refresh, expires: Date.now() + expiresIn * 1000, deviceId };\n}\n\nexport interface KimiCodingDeviceLoginPending {\n clientId: string;\n deviceId: string;\n deviceCode: string;\n userCode: string;\n url: string;\n instructions: string;\n state: DeviceCodePollState;\n}\n\nexport type KimiCodingDevicePollResult =\n | { status: 'complete'; credentials: OAuthCredentials }\n | { status: 'pending'; nextPollMs: number; pending: KimiCodingDeviceLoginPending }\n | { status: 'failed'; error: string };\n\nexport async function startKimiCodingDeviceLogin(options?: {\n signal?: AbortSignal;\n}): Promise<KimiCodingDeviceLoginPending> {\n const clientId = CLIENT_ID;\n const deviceId = createKimiCodingDeviceId();\n const response = await fetch(`${OAUTH_HOST}/api/oauth/device_authorization`, {\n method: 'POST',\n headers: {\n ...getKimiCodingDeviceHeaders(deviceId),\n 'Content-Type': 'application/x-www-form-urlencoded',\n Accept: 'application/json',\n },\n body: new URLSearchParams({ client_id: clientId }).toString(),\n signal: requestSignal(options?.signal),\n });\n if (!response.ok) {\n throw new Error(`Kimi For Coding device authorization failed: ${response.status}`);\n }\n\n const data = await readJson(response);\n const deviceCode = data?.device_code;\n const userCode = data?.user_code;\n const verificationUri = trustedHttpUrl(data?.verification_uri);\n const verificationUriComplete = trustedHttpUrl(data?.verification_uri_complete);\n if (\n typeof deviceCode !== 'string' ||\n !deviceCode ||\n typeof userCode !== 'string' ||\n !userCode ||\n !verificationUri ||\n !verificationUriComplete\n ) {\n throw new Error('Invalid Kimi For Coding device authorization response');\n }\n\n const interval = data?.interval;\n const expiresIn = data?.expires_in;\n return {\n clientId,\n deviceId,\n deviceCode,\n userCode,\n url: verificationUriComplete,\n instructions: `Enter code: ${userCode}`,\n state: createDeviceCodePollState({\n intervalSeconds:\n typeof interval === 'number' && Number.isFinite(interval) && interval > 0\n ? interval\n : DEFAULT_POLL_INTERVAL_SECONDS,\n expiresInSeconds:\n typeof expiresIn === 'number' && Number.isFinite(expiresIn) && expiresIn > 0\n ? expiresIn\n : DEFAULT_EXPIRES_IN_SECONDS,\n }),\n };\n}\n\nasync function pollKimiCodingTokenOnce(\n pending: KimiCodingDeviceLoginPending,\n signal?: AbortSignal,\n): Promise<DeviceCodePollOutcome<OAuthCredentials>> {\n const response = await fetch(`${OAUTH_HOST}/api/oauth/token`, {\n method: 'POST',\n headers: {\n ...getKimiCodingDeviceHeaders(pending.deviceId),\n 'Content-Type': 'application/x-www-form-urlencoded',\n Accept: 'application/json',\n },\n body: new URLSearchParams({\n client_id: pending.clientId,\n device_code: pending.deviceCode,\n grant_type: 'urn:ietf:params:oauth:grant-type:device_code',\n }).toString(),\n signal: requestSignal(signal),\n });\n const data = await readJson(response);\n if (response.ok && typeof data?.access_token === 'string') {\n try {\n return { status: 'complete', result: credentialsFromTokenResponse(data, 'poll', pending.deviceId) };\n } catch (error) {\n return { status: 'failed', error: error instanceof Error ? error.message : String(error) };\n }\n }\n\n const error = data?.error;\n if (error === 'authorization_pending') return { status: 'pending' };\n if (error === 'slow_down') {\n return {\n status: 'slow_down',\n intervalSeconds: typeof data?.interval === 'number' && data.interval > 0 ? data.interval : undefined,\n };\n }\n if (error === 'expired_token') {\n return { status: 'failed', error: 'Kimi For Coding authorization expired. Please restart login.' };\n }\n if (error === 'access_denied') return { status: 'failed', error: 'Kimi For Coding login was denied.' };\n return {\n status: 'failed',\n error: `Kimi For Coding token request failed: ${response.status}`,\n };\n}\n\nexport async function pollKimiCodingDeviceLogin(\n pending: KimiCodingDeviceLoginPending,\n options?: { signal?: AbortSignal },\n): Promise<KimiCodingDevicePollResult> {\n const step = await stepDeviceCodePoll(pending.state, () => pollKimiCodingTokenOnce(pending, options?.signal));\n if (step.status === 'complete') return { status: 'complete', credentials: step.result };\n if (step.status === 'failed') return { status: 'failed', error: step.error };\n return {\n status: 'pending',\n nextPollMs: step.nextPollMs,\n pending: { ...pending, state: step.state },\n };\n}\n\nexport async function loginKimiCoding(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {\n const pending = await startKimiCodingDeviceLogin({ signal: callbacks.signal });\n callbacks.onAuth({ url: pending.url, instructions: pending.instructions, userCode: pending.userCode });\n callbacks.onProgress?.('Waiting for Kimi For Coding device authorization...');\n return pollDeviceCodeUntilComplete({\n state: pending.state,\n pollOnce: () => pollKimiCodingTokenOnce(pending, callbacks.signal),\n signal: callbacks.signal,\n });\n}\n\nexport async function refreshKimiCodingToken(\n refreshToken: string,\n options: { signal?: AbortSignal; deviceId: string },\n): Promise<OAuthCredentials> {\n const clientId = CLIENT_ID;\n const deviceHeaders = getKimiCodingDeviceHeaders(options.deviceId);\n let lastError: Error | undefined;\n for (let attempt = 0; attempt <= REFRESH_MAX_RETRIES; attempt++) {\n if (attempt > 0) await abortableSleep(1000 * 2 ** (attempt - 1), options.signal);\n let response: Response;\n try {\n response = await fetch(`${OAUTH_HOST}/api/oauth/token`, {\n method: 'POST',\n headers: {\n ...deviceHeaders,\n 'Content-Type': 'application/x-www-form-urlencoded',\n Accept: 'application/json',\n },\n body: new URLSearchParams({\n client_id: clientId,\n grant_type: 'refresh_token',\n refresh_token: refreshToken,\n }).toString(),\n signal: requestSignal(options.signal),\n });\n } catch (error) {\n lastError = error instanceof Error ? error : new Error(String(error));\n if (attempt === REFRESH_MAX_RETRIES) throw lastError;\n continue;\n }\n const data = await readJson(response);\n if (response.ok) return credentialsFromTokenResponse(data, 'refresh', options.deviceId);\n\n const error = typeof data?.error === 'string' ? ` ${data.error}` : '';\n lastError = new Error(`Kimi For Coding token refresh failed: ${response.status}${error}`);\n const retryable = response.status === 429 || response.status >= 500;\n if (!retryable || attempt === REFRESH_MAX_RETRIES) throw lastError;\n }\n throw lastError ?? new Error('Kimi For Coding token refresh failed');\n}\n\nexport const kimiCodingOAuthProvider: OAuthProviderInterface = {\n id: 'kimi-for-coding',\n name: 'Kimi For Coding',\n login: loginKimiCoding,\n refreshToken: credentials =>\n refreshKimiCodingToken(credentials.refresh, {\n deviceId: typeof credentials.deviceId === 'string' ? credentials.deviceId : '',\n }),\n getApiKey: credentials => credentials.access,\n};\n"],"mappings":";;;;AAWA,MAAM,YAAY;AAClB,MAAM,aAAa;AACnB,MAAM,oBAAoB;AAC1B,MAAM,6BAA6B;AACnC,MAAM,gCAAgC;AACtC,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAE5B,SAAS,iBAAiB,OAAuB;CAE/C,OADkB,MAAM,QAAQ,iBAAiB,EAAE,CAAC,CAAC,KACtC,KAAK;AACtB;AAEA,MAAM,sBAAsB;CAC1B,kBAAkB;CAClB,iBAAiB,iBAAiB,kBAAkB,CAAC;CACrD,qBAAqB,iBAAiB,SAAS,CAAC;CAChD,sBAAsB,iBAAiB,GAAG,SAAS,EAAE,GAAG,KAAK,GAAG;CAChE,oBAAoB,iBAAiB,QAAQ,CAAC;AAChD;AAEA,SAAgB,2BAAmC;CACjD,OAAO,WAAW,OAAO,WAAW,CAAC,CAAC,WAAW,KAAK,EAAE;AAC1D;AAEA,SAAgB,qBAAqB,OAAiC;CACpE,OAAO,OAAO,UAAU,YAAY,kBAAkB,KAAK,KAAK;AAClE;AAEA,SAAgB,2BAA2B,UAA0C;CACnF,IAAI,CAAC,qBAAqB,QAAQ,GAChC,MAAM,IAAI,MAAM,sFAAsF;CAExG,OAAO;EAAE,GAAG;EAAqB,mBAAmB;CAAS;AAC/D;AAEA,SAAS,cAAc,QAAmC;CACxD,MAAM,UAAU,YAAY,QAAQ,kBAAkB;CACtD,OAAO,SAAS,YAAY,IAAI,CAAC,SAAS,MAAM,CAAC,IAAI;AACvD;AAEA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Q;IACR,SAAS;KACP,GAAG;KACH,gBAAgB;KAChB,QAAQ;IACV;IACA,MAAM,IAAI,gBAAgB;KACxB,WAAW;KACX,YAAY;KACZ,eAAe;IACjB,CAAC,CAAC,CAAC,SAAS;IACZ,QAAQ,cAAc,QAAQ,MAAM;GACtC,CAAC;EACH,SAAS,OAAO;GACd,YAAY,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;GACpE,IAAI,YAAY,qBAAqB,MAAM;GAC3C;EACF;EACA,MAAM,OAAO,MAAM,SAAS,QAAQ;EACpC,IAAI,SAAS,IAAI,OAAO,6BAA6B,MAAM,WAAW,QAAQ,QAAQ;EAEtF,MAAM,QAAQ,OAAO,MAAM,UAAU,WAAW,IAAI,KAAK,UAAU;EACnE,4BAAY,IAAI,MAAM,yCAAyC,SAAS,SAAS,OAAO;EAExF,IAAI,EADc,SAAS,WAAW,OAAO,SAAS,UAAU,QAC9C,YAAY,qBAAqB,MAAM;CAC3D;CACA,MAAM,6BAAa,IAAI,MAAM,sCAAsC;AACrE;AAEA,MAAa,0BAAkD;CAC7D,IAAI;CACJ,MAAM;CACN,OAAO;CACP,eAAc,gBACZ,uBAAuB,YAAY,SAAS,EAC1C,UAAU,OAAO,YAAY,aAAa,WAAW,YAAY,WAAW,GAC9E,CAAC;CACH,YAAW,gBAAe,YAAY;AACxC"}
@@ -162,7 +162,8 @@ async function loginXAI(callbacks) {
162
162
  const pending = await startXAIDeviceLogin({ signal: callbacks.signal });
163
163
  callbacks.onAuth({
164
164
  url: pending.url,
165
- instructions: pending.instructions
165
+ instructions: pending.instructions,
166
+ userCode: pending.userCode
166
167
  });
167
168
  callbacks.onProgress?.("Waiting for xAI device authorization...");
168
169
  return pollDeviceCodeUntilComplete({
@@ -1 +1 @@
1
- {"version":3,"file":"xai.js","names":[],"sources":["../../../src/auth/providers/xai.ts"],"sourcesContent":["/**\n * xAI OAuth flow (Grok)\n *\n * Ported from pi-mono's xAI OAuth implementation:\n * https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/utils/oauth/xai.ts\n *\n * xAI uses a standard RFC 8628 device authorization grant, so the flow needs\n * no inbound connection to the server. The primitives are split into\n * `startXAIDeviceLogin()` / `pollXAIDeviceLogin()` so web routes can persist\n * the JSON-serializable pending state between HTTP requests; the blocking\n * `loginXAI()` wraps them for TUI use.\n */\n\nimport { createDeviceCodePollState, pollDeviceCodeUntilComplete, stepDeviceCodePoll } from '../device-code.js';\nimport type { DeviceCodePollOutcome, DeviceCodePollState } from '../device-code.js';\nimport type { OAuthCredentials, OAuthLoginCallbacks, OAuthProviderInterface } from '../types.js';\n\nconst CLIENT_ID = 'b1a00492-073a-47ea-816f-4c329264a828';\nconst DEVICE_CODE_URL = 'https://auth.x.ai/oauth2/device/code';\nconst TOKEN_URL = 'https://auth.x.ai/oauth2/token';\nconst SCOPE = 'openid profile email offline_access grok-cli:access api:access';\nconst DEVICE_CODE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';\nconst DEFAULT_TOKEN_EXPIRES_IN_SECONDS = 3600;\nconst REQUEST_TIMEOUT_MS = 30_000;\n// Refresh 5 minutes before actual expiry (same skew as Anthropic).\nconst REFRESH_SKEW_MS = 5 * 60 * 1000;\n\nasync function postForm(url: string, params: Record<string, string>, signal?: AbortSignal): Promise<Response> {\n const timeoutSignal = AbortSignal.timeout(REQUEST_TIMEOUT_MS);\n return fetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams(params).toString(),\n signal: signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal,\n });\n}\n\n/** The verification URI is opened by the user; only accept https URLs. */\nfunction validateVerificationUri(raw: string): string {\n let parsed: URL;\n try {\n parsed = new URL(raw);\n } catch {\n throw new Error('xAI device authorization returned an invalid verification_uri');\n }\n if (parsed.protocol !== 'https:') {\n throw new Error('xAI device authorization returned a non-https verification_uri');\n }\n return parsed.toString();\n}\n\nfunction credentialsFromTokenResponse(\n data: unknown,\n previousRefreshToken?: string,\n previousIdToken?: string,\n): OAuthCredentials {\n const record = (data ?? {}) as Record<string, unknown>;\n const access = record.access_token;\n if (typeof access !== 'string' || access.length === 0) {\n throw new Error('xAI token response missing access_token');\n }\n\n // xAI may not rotate the refresh token on refresh; keep the previous one.\n const refresh =\n typeof record.refresh_token === 'string' && record.refresh_token.length > 0\n ? record.refresh_token\n : previousRefreshToken;\n if (!refresh) {\n throw new Error('xAI token response missing refresh_token');\n }\n\n const expiresIn =\n typeof record.expires_in === 'number' && record.expires_in > 0\n ? record.expires_in\n : DEFAULT_TOKEN_EXPIRES_IN_SECONDS;\n\n return {\n access,\n refresh,\n expires: Date.now() + expiresIn * 1000 - REFRESH_SKEW_MS,\n // Kept so the account label can resolve the email claim later; xAI's\n // refresh responses re-issue it, and the previous one is carried forward\n // when they don't.\n idToken: typeof record.id_token === 'string' && record.id_token.length > 0 ? record.id_token : previousIdToken,\n };\n}\n\n/**\n * Serializable pending state for an xAI device-code login. Safe to persist\n * (e.g. a `pending jsonb` column) so polling can span HTTP requests.\n */\nexport interface XAIDeviceLoginPending {\n deviceCode: string;\n userCode: string;\n /** Verification URL for the user to open (https-only, validated). */\n url: string;\n instructions: string;\n /** RFC 8628 poll-loop state (interval growth, deadline, slow_down count). */\n state: DeviceCodePollState;\n}\n\nexport type XAIDevicePollResult =\n | { status: 'complete'; credentials: OAuthCredentials }\n | { status: 'pending'; nextPollMs: number; pending: XAIDeviceLoginPending }\n | { status: 'failed'; error: string };\n\n/**\n * Start an xAI device-code login: request a user code and return the\n * serializable pending state for subsequent polls.\n */\nexport async function startXAIDeviceLogin(options?: { signal?: AbortSignal }): Promise<XAIDeviceLoginPending> {\n const response = await postForm(DEVICE_CODE_URL, { client_id: CLIENT_ID, scope: SCOPE }, options?.signal);\n\n if (!response.ok) {\n throw new Error(`Failed to initiate xAI device authorization: ${response.status}`);\n }\n\n const data = (await response.json()) as {\n device_code?: string;\n user_code?: string;\n verification_uri?: string;\n verification_uri_complete?: string;\n interval?: number;\n expires_in?: number;\n };\n\n if (!data.device_code || !data.user_code || !data.verification_uri) {\n throw new Error('xAI device authorization response missing required fields');\n }\n\n const url = validateVerificationUri(data.verification_uri_complete ?? data.verification_uri);\n\n return {\n deviceCode: data.device_code,\n userCode: data.user_code,\n url,\n instructions: `Enter code: ${data.user_code}`,\n state: createDeviceCodePollState({\n intervalSeconds: data.interval,\n expiresInSeconds: typeof data.expires_in === 'number' && data.expires_in > 0 ? data.expires_in : 600,\n }),\n };\n}\n\nasync function pollXAITokenOnce(\n pending: XAIDeviceLoginPending,\n signal?: AbortSignal,\n): Promise<DeviceCodePollOutcome<OAuthCredentials>> {\n const response = await postForm(\n TOKEN_URL,\n {\n grant_type: DEVICE_CODE_GRANT_TYPE,\n device_code: pending.deviceCode,\n client_id: CLIENT_ID,\n },\n signal,\n );\n\n if (response.ok) {\n const data = (await response.json()) as unknown;\n try {\n return { status: 'complete', result: credentialsFromTokenResponse(data) };\n } catch (error) {\n return { status: 'failed', error: error instanceof Error ? error.message : String(error) };\n }\n }\n\n const text = await response.text().catch(() => '');\n let body: { error?: string; interval?: number } = {};\n try {\n body = JSON.parse(text) as { error?: string; interval?: number };\n } catch {\n // Non-JSON upstream bodies are intentionally omitted from user-visible errors.\n }\n\n switch (body.error) {\n case 'authorization_pending':\n return { status: 'pending', intervalSeconds: body.interval };\n case 'slow_down':\n return { status: 'slow_down', intervalSeconds: body.interval };\n case 'access_denied':\n case 'authorization_denied':\n return { status: 'failed', error: 'xAI authorization was denied' };\n case 'expired_token':\n return { status: 'failed', error: 'xAI device code expired before authorization completed' };\n default:\n // Status only: `body.error` is untrusted upstream text (RFC 6749 does not\n // constrain it), so echoing it would render provider-controlled content\n // in the TUI. The known flow codes above are handled explicitly.\n return {\n status: 'failed',\n error: `xAI device authorization failed: ${response.status}`,\n };\n }\n}\n\n/**\n * Perform exactly one upstream poll for a pending xAI device login.\n * Returns the updated pending state so callers (e.g. web routes) can persist\n * slow_down interval growth between polls. Never throws for flow-level\n * conditions.\n */\nexport async function pollXAIDeviceLogin(\n pending: XAIDeviceLoginPending,\n options?: { signal?: AbortSignal },\n): Promise<XAIDevicePollResult> {\n const step = await stepDeviceCodePoll(pending.state, () => pollXAITokenOnce(pending, options?.signal));\n\n switch (step.status) {\n case 'complete':\n return { status: 'complete', credentials: step.result };\n case 'failed':\n return { status: 'failed', error: step.error };\n case 'pending':\n case 'slow_down':\n return {\n status: 'pending',\n nextPollMs: step.nextPollMs,\n pending: { ...pending, state: step.state },\n };\n }\n}\n\n/**\n * Login with xAI OAuth (device-code flow), blocking until authorized.\n */\nexport async function loginXAI(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {\n const pending = await startXAIDeviceLogin({ signal: callbacks.signal });\n\n callbacks.onAuth({ url: pending.url, instructions: pending.instructions });\n callbacks.onProgress?.('Waiting for xAI device authorization...');\n\n return pollDeviceCodeUntilComplete({\n state: pending.state,\n pollOnce: () => pollXAITokenOnce(pending, callbacks.signal),\n signal: callbacks.signal,\n });\n}\n\n/**\n * Refresh xAI OAuth token\n */\nexport async function refreshXAIToken(\n refreshToken: string,\n signal?: AbortSignal,\n previousIdToken?: string,\n): Promise<OAuthCredentials> {\n const response = await postForm(\n TOKEN_URL,\n {\n grant_type: 'refresh_token',\n client_id: CLIENT_ID,\n refresh_token: refreshToken,\n },\n signal,\n );\n\n if (!response.ok) {\n throw new Error(`xAI token refresh failed: ${response.status}`);\n }\n\n return credentialsFromTokenResponse((await response.json()) as unknown, refreshToken, previousIdToken);\n}\n\n/** Decode the email claim out of an id_token JWT payload (no verification). */\nfunction emailFromIdToken(idToken: string | undefined): string | undefined {\n if (typeof idToken !== 'string') return undefined;\n const parts = idToken.split('.');\n if (parts.length !== 3) return undefined;\n try {\n const padded = (parts[1] ?? '').replace(/-/g, '+').replace(/_/g, '/');\n const json = JSON.parse(Buffer.from(padded, 'base64').toString('utf8')) as { email?: unknown };\n return typeof json.email === 'string' && json.email.length > 0 ? json.email : undefined;\n } catch {\n return undefined;\n }\n}\n\nexport const xaiOAuthProvider: OAuthProviderInterface = {\n id: 'xai',\n name: 'xAI (Grok)',\n\n async login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {\n return loginXAI(callbacks);\n },\n\n async refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {\n return refreshXAIToken(credentials.refresh, undefined, credentials.idToken as string | undefined);\n },\n\n getApiKey(credentials: OAuthCredentials): string {\n return credentials.access;\n },\n\n getAccountLabel(credentials: OAuthCredentials): Promise<string | undefined> {\n return Promise.resolve(emailFromIdToken(credentials.idToken as string | undefined));\n },\n\n /**\n * The account's email, read from the id token — one subscription per email,\n * so this distinguishes two xAI accounts from a re-authorization of one.\n */\n getAccountIdentity(credentials: OAuthCredentials): string | undefined {\n return emailFromIdToken(credentials.idToken as string | undefined);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;AAiBA,MAAM,YAAY;AAClB,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,QAAQ;AACd,MAAM,yBAAyB;AAC/B,MAAM,mCAAmC;AACzC,MAAM,qBAAqB;AAE3B,MAAM,kBAAkB,MAAS;AAEjC,eAAe,SAAS,KAAa,QAAgC,QAAyC;CAC5G,MAAM,gBAAgB,YAAY,QAAQ,kBAAkB;CAC5D,OAAO,MAAM,KAAK;EAChB,QAAQ;EACR,SAAS,EAAE,gBAAgB,oCAAoC;EAC/D,MAAM,IAAI,gBAAgB,MAAM,CAAC,CAAC,SAAS;EAC3C,QAAQ,SAAS,YAAY,IAAI,CAAC,QAAQ,aAAa,CAAC,IAAI;CAC9D,CAAC;AACH;;AAGA,SAAS,wBAAwB,KAAqB;CACpD,IAAI;CACJ,IAAI;EACF,SAAS,IAAI,IAAI,GAAG;CACtB,QAAQ;EACN,MAAM,IAAI,MAAM,+DAA+D;CACjF;CACA,IAAI,OAAO,aAAa,UACtB,MAAM,IAAI,MAAM,gEAAgE;CAElF,OAAO,OAAO,SAAS;AACzB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;;;;CAMA,mBAAmB,aAAmD;EACpE,OAAO,iBAAiB,YAAY,OAA6B;CACnE;AACF"}
1
+ {"version":3,"file":"xai.js","names":[],"sources":["../../../src/auth/providers/xai.ts"],"sourcesContent":["/**\n * xAI OAuth flow (Grok)\n *\n * Ported from pi-mono's xAI OAuth implementation:\n * https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/utils/oauth/xai.ts\n *\n * xAI uses a standard RFC 8628 device authorization grant, so the flow needs\n * no inbound connection to the server. The primitives are split into\n * `startXAIDeviceLogin()` / `pollXAIDeviceLogin()` so web routes can persist\n * the JSON-serializable pending state between HTTP requests; the blocking\n * `loginXAI()` wraps them for TUI use.\n */\n\nimport { createDeviceCodePollState, pollDeviceCodeUntilComplete, stepDeviceCodePoll } from '../device-code.js';\nimport type { DeviceCodePollOutcome, DeviceCodePollState } from '../device-code.js';\nimport type { OAuthCredentials, OAuthLoginCallbacks, OAuthProviderInterface } from '../types.js';\n\nconst CLIENT_ID = 'b1a00492-073a-47ea-816f-4c329264a828';\nconst DEVICE_CODE_URL = 'https://auth.x.ai/oauth2/device/code';\nconst TOKEN_URL = 'https://auth.x.ai/oauth2/token';\nconst SCOPE = 'openid profile email offline_access grok-cli:access api:access';\nconst DEVICE_CODE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code';\nconst DEFAULT_TOKEN_EXPIRES_IN_SECONDS = 3600;\nconst REQUEST_TIMEOUT_MS = 30_000;\n// Refresh 5 minutes before actual expiry (same skew as Anthropic).\nconst REFRESH_SKEW_MS = 5 * 60 * 1000;\n\nasync function postForm(url: string, params: Record<string, string>, signal?: AbortSignal): Promise<Response> {\n const timeoutSignal = AbortSignal.timeout(REQUEST_TIMEOUT_MS);\n return fetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams(params).toString(),\n signal: signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal,\n });\n}\n\n/** The verification URI is opened by the user; only accept https URLs. */\nfunction validateVerificationUri(raw: string): string {\n let parsed: URL;\n try {\n parsed = new URL(raw);\n } catch {\n throw new Error('xAI device authorization returned an invalid verification_uri');\n }\n if (parsed.protocol !== 'https:') {\n throw new Error('xAI device authorization returned a non-https verification_uri');\n }\n return parsed.toString();\n}\n\nfunction credentialsFromTokenResponse(\n data: unknown,\n previousRefreshToken?: string,\n previousIdToken?: string,\n): OAuthCredentials {\n const record = (data ?? {}) as Record<string, unknown>;\n const access = record.access_token;\n if (typeof access !== 'string' || access.length === 0) {\n throw new Error('xAI token response missing access_token');\n }\n\n // xAI may not rotate the refresh token on refresh; keep the previous one.\n const refresh =\n typeof record.refresh_token === 'string' && record.refresh_token.length > 0\n ? record.refresh_token\n : previousRefreshToken;\n if (!refresh) {\n throw new Error('xAI token response missing refresh_token');\n }\n\n const expiresIn =\n typeof record.expires_in === 'number' && record.expires_in > 0\n ? record.expires_in\n : DEFAULT_TOKEN_EXPIRES_IN_SECONDS;\n\n return {\n access,\n refresh,\n expires: Date.now() + expiresIn * 1000 - REFRESH_SKEW_MS,\n // Kept so the account label can resolve the email claim later; xAI's\n // refresh responses re-issue it, and the previous one is carried forward\n // when they don't.\n idToken: typeof record.id_token === 'string' && record.id_token.length > 0 ? record.id_token : previousIdToken,\n };\n}\n\n/**\n * Serializable pending state for an xAI device-code login. Safe to persist\n * (e.g. a `pending jsonb` column) so polling can span HTTP requests.\n */\nexport interface XAIDeviceLoginPending {\n deviceCode: string;\n userCode: string;\n /** Verification URL for the user to open (https-only, validated). */\n url: string;\n instructions: string;\n /** RFC 8628 poll-loop state (interval growth, deadline, slow_down count). */\n state: DeviceCodePollState;\n}\n\nexport type XAIDevicePollResult =\n | { status: 'complete'; credentials: OAuthCredentials }\n | { status: 'pending'; nextPollMs: number; pending: XAIDeviceLoginPending }\n | { status: 'failed'; error: string };\n\n/**\n * Start an xAI device-code login: request a user code and return the\n * serializable pending state for subsequent polls.\n */\nexport async function startXAIDeviceLogin(options?: { signal?: AbortSignal }): Promise<XAIDeviceLoginPending> {\n const response = await postForm(DEVICE_CODE_URL, { client_id: CLIENT_ID, scope: SCOPE }, options?.signal);\n\n if (!response.ok) {\n throw new Error(`Failed to initiate xAI device authorization: ${response.status}`);\n }\n\n const data = (await response.json()) as {\n device_code?: string;\n user_code?: string;\n verification_uri?: string;\n verification_uri_complete?: string;\n interval?: number;\n expires_in?: number;\n };\n\n if (!data.device_code || !data.user_code || !data.verification_uri) {\n throw new Error('xAI device authorization response missing required fields');\n }\n\n const url = validateVerificationUri(data.verification_uri_complete ?? data.verification_uri);\n\n return {\n deviceCode: data.device_code,\n userCode: data.user_code,\n url,\n instructions: `Enter code: ${data.user_code}`,\n state: createDeviceCodePollState({\n intervalSeconds: data.interval,\n expiresInSeconds: typeof data.expires_in === 'number' && data.expires_in > 0 ? data.expires_in : 600,\n }),\n };\n}\n\nasync function pollXAITokenOnce(\n pending: XAIDeviceLoginPending,\n signal?: AbortSignal,\n): Promise<DeviceCodePollOutcome<OAuthCredentials>> {\n const response = await postForm(\n TOKEN_URL,\n {\n grant_type: DEVICE_CODE_GRANT_TYPE,\n device_code: pending.deviceCode,\n client_id: CLIENT_ID,\n },\n signal,\n );\n\n if (response.ok) {\n const data = (await response.json()) as unknown;\n try {\n return { status: 'complete', result: credentialsFromTokenResponse(data) };\n } catch (error) {\n return { status: 'failed', error: error instanceof Error ? error.message : String(error) };\n }\n }\n\n const text = await response.text().catch(() => '');\n let body: { error?: string; interval?: number } = {};\n try {\n body = JSON.parse(text) as { error?: string; interval?: number };\n } catch {\n // Non-JSON upstream bodies are intentionally omitted from user-visible errors.\n }\n\n switch (body.error) {\n case 'authorization_pending':\n return { status: 'pending', intervalSeconds: body.interval };\n case 'slow_down':\n return { status: 'slow_down', intervalSeconds: body.interval };\n case 'access_denied':\n case 'authorization_denied':\n return { status: 'failed', error: 'xAI authorization was denied' };\n case 'expired_token':\n return { status: 'failed', error: 'xAI device code expired before authorization completed' };\n default:\n // Status only: `body.error` is untrusted upstream text (RFC 6749 does not\n // constrain it), so echoing it would render provider-controlled content\n // in the TUI. The known flow codes above are handled explicitly.\n return {\n status: 'failed',\n error: `xAI device authorization failed: ${response.status}`,\n };\n }\n}\n\n/**\n * Perform exactly one upstream poll for a pending xAI device login.\n * Returns the updated pending state so callers (e.g. web routes) can persist\n * slow_down interval growth between polls. Never throws for flow-level\n * conditions.\n */\nexport async function pollXAIDeviceLogin(\n pending: XAIDeviceLoginPending,\n options?: { signal?: AbortSignal },\n): Promise<XAIDevicePollResult> {\n const step = await stepDeviceCodePoll(pending.state, () => pollXAITokenOnce(pending, options?.signal));\n\n switch (step.status) {\n case 'complete':\n return { status: 'complete', credentials: step.result };\n case 'failed':\n return { status: 'failed', error: step.error };\n case 'pending':\n case 'slow_down':\n return {\n status: 'pending',\n nextPollMs: step.nextPollMs,\n pending: { ...pending, state: step.state },\n };\n }\n}\n\n/**\n * Login with xAI OAuth (device-code flow), blocking until authorized.\n */\nexport async function loginXAI(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {\n const pending = await startXAIDeviceLogin({ signal: callbacks.signal });\n\n callbacks.onAuth({ url: pending.url, instructions: pending.instructions, userCode: pending.userCode });\n callbacks.onProgress?.('Waiting for xAI device authorization...');\n\n return pollDeviceCodeUntilComplete({\n state: pending.state,\n pollOnce: () => pollXAITokenOnce(pending, callbacks.signal),\n signal: callbacks.signal,\n });\n}\n\n/**\n * Refresh xAI OAuth token\n */\nexport async function refreshXAIToken(\n refreshToken: string,\n signal?: AbortSignal,\n previousIdToken?: string,\n): Promise<OAuthCredentials> {\n const response = await postForm(\n TOKEN_URL,\n {\n grant_type: 'refresh_token',\n client_id: CLIENT_ID,\n refresh_token: refreshToken,\n },\n signal,\n );\n\n if (!response.ok) {\n throw new Error(`xAI token refresh failed: ${response.status}`);\n }\n\n return credentialsFromTokenResponse((await response.json()) as unknown, refreshToken, previousIdToken);\n}\n\n/** Decode the email claim out of an id_token JWT payload (no verification). */\nfunction emailFromIdToken(idToken: string | undefined): string | undefined {\n if (typeof idToken !== 'string') return undefined;\n const parts = idToken.split('.');\n if (parts.length !== 3) return undefined;\n try {\n const padded = (parts[1] ?? '').replace(/-/g, '+').replace(/_/g, '/');\n const json = JSON.parse(Buffer.from(padded, 'base64').toString('utf8')) as { email?: unknown };\n return typeof json.email === 'string' && json.email.length > 0 ? json.email : undefined;\n } catch {\n return undefined;\n }\n}\n\nexport const xaiOAuthProvider: OAuthProviderInterface = {\n id: 'xai',\n name: 'xAI (Grok)',\n\n async login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {\n return loginXAI(callbacks);\n },\n\n async refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {\n return refreshXAIToken(credentials.refresh, undefined, credentials.idToken as string | undefined);\n },\n\n getApiKey(credentials: OAuthCredentials): string {\n return credentials.access;\n },\n\n getAccountLabel(credentials: OAuthCredentials): Promise<string | undefined> {\n return Promise.resolve(emailFromIdToken(credentials.idToken as string | undefined));\n },\n\n /**\n * The account's email, read from the id token — one subscription per email,\n * so this distinguishes two xAI accounts from a re-authorization of one.\n */\n getAccountIdentity(credentials: OAuthCredentials): string | undefined {\n return emailFromIdToken(credentials.idToken as string | undefined);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;AAiBA,MAAM,YAAY;AAClB,MAAM,kBAAkB;AACxB,MAAM,YAAY;AAClB,MAAM,QAAQ;AACd,MAAM,yBAAyB;AAC/B,MAAM,mCAAmC;AACzC,MAAM,qBAAqB;AAE3B,MAAM,kBAAkB,MAAS;AAEjC,eAAe,SAAS,KAAa,QAAgC,QAAyC;CAC5G,MAAM,gBAAgB,YAAY,QAAQ,kBAAkB;CAC5D,OAAO,MAAM,KAAK;EAChB,QAAQ;EACR,SAAS,EAAE,gBAAgB,oCAAoC;EAC/D,MAAM,IAAI,gBAAgB,MAAM,CAAC,CAAC,SAAS;EAC3C,QAAQ,SAAS,YAAY,IAAI,CAAC,QAAQ,aAAa,CAAC,IAAI;CAC9D,CAAC;AACH;;A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S,SAAS;CAC3B;CAEA,MAAM,aAAa,aAA0D;EAC3E,OAAO,gBAAgB,YAAY,SAAS,KAAA,GAAW,YAAY,OAA6B;CAClG;CAEA,UAAU,aAAuC;EAC/C,OAAO,YAAY;CACrB;CAEA,gBAAgB,aAA4D;EAC1E,OAAO,QAAQ,QAAQ,iBAAiB,YAAY,OAA6B,CAAC;CACpF;;;;;CAMA,mBAAmB,aAAmD;EACpE,OAAO,iBAAiB,YAAY,OAA6B;CACnE;AACF"}
@@ -5,8 +5,8 @@ import { kimiCodingOAuthProvider } from "./providers/kimi-coding.js";
5
5
  import { openaiCodexOAuthProvider } from "./providers/openai-codex.js";
6
6
  import { xaiOAuthProvider } from "./providers/xai.js";
7
7
  import { dirname, join } from "path";
8
- import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
9
8
  import { createHash } from "crypto";
9
+ import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
10
10
  //#region src/auth/storage.ts
11
11
  /**
12
12
  * Credential storage for API keys and OAuth tokens.
@@ -11,6 +11,7 @@ export type OAuthProviderId = string;
11
11
  export interface OAuthAuthInfo {
12
12
  url: string;
13
13
  instructions?: string;
14
+ userCode?: string;
14
15
  }
15
16
  export interface OAuthPrompt {
16
17
  message: string;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAErC,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,M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,QAAQ,EAAE,MAAM,EAAE,iBAAiB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,uBAAuB,GAAG,SAAS,CAAC,CAAC;IAEhH;;;OAGG;IACH,YAAY,CAAC,CAAC,UAAU,EAAE,MAAM,GAAG,kBAAkB,EAAE,CAAC;IAExD,kEAAkE;IAClE,gBAAgB,CAAC,CAAC,UAAU,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS,CAAC;IAEtE;;;;;;OAMG;IACH,eAAe,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS,CAAC;IAE1F,mFAAmF;IACnF,aAAa,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9D"}
@@ -1,6 +1,6 @@
1
1
  import { join } from "path";
2
- import { execSync } from "child_process";
3
2
  import { readFileSync, unlinkSync } from "fs";
3
+ import { execSync } from "child_process";
4
4
  import { tmpdir } from "os";
5
5
  //#region src/clipboard/index.ts
6
6
  /**
package/dist/index.js CHANGED
@@ -2,9 +2,10 @@ import { validateConfigDirName } from "./constants.js";
2
2
  import { detectProject, getObservabilityDatabasePath, getResourceIdOverride, getStorageConfig } from "./utils/project.js";
3
3
  import { isKimiCodingDeviceId } from "./auth/providers/kimi-coding.js";
4
4
  import { AuthStorage } from "./auth/storage.js";
5
- import { setAuthStorage } from "./providers/openai-codex.js";
6
5
  import { getAvailableModePacks, getAvailableOmPacks, selectPreferredOMPack } from "./onboarding/packs.js";
6
+ import { setAuthStorage } from "./providers/openai-codex.js";
7
7
  import { MASTRA_GATEWAY_PROVIDER, OBSERVABILITY_AUTH_PREFIX, THREAD_ACTIVE_MODEL_PACK_ID_KEY, loadSettings, resolveModelDefaults, resolveOmRoleModel, saveSettings } from "./onboarding/settings.js";
8
+ import { hasExplicitOMConfiguration } from "./onboarding/om-settings.js";
8
9
  import { resolveExperimentalAgent, validateExperimentalAgent, wrapExperimentalAgent } from "./experimental-agent.js";
9
10
  import { createSessionThreadAdvertisement } from "./agent-connections/session-advertisement.js";
10
11
  import { AgentConnectionsSignalProvider } from "./agent-connections/signal-provider.js";
@@ -39,7 +40,6 @@ import "./hooks/index.js";
39
40
  import { KnowledgeInspectorError, createKnowledgeInspector } from "./knowledge-inspector.js";
40
41
  import { createMcpManager } from "./mcp/manager.js";
41
42
  import "./mcp/index.js";
42
- import { hasExplicitOMConfiguration } from "./onboarding/om-settings.js";
43
43
  import { getToolCategory } from "./permissions.js";
44
44
  import { PluginManager } from "./plugins/manager.js";
45
45
  import { PluginSignalLane } from "./plugins/signal-lane.js";
@@ -66,9 +66,9 @@ import { formatHuman, formatJsonl, renderJsonResult, renderTextResult } from "./
66
66
  import { hasHeadlessFlag, runMCCli } from "./headless/cli.js";
67
67
  import "./headless/index.js";
68
68
  import path from "path";
69
+ import { createHash, randomUUID } from "crypto";
69
70
  import { statSync } from "fs";
70
71
  import { homedir, hostname } from "os";
71
- import { createHash, randomUUID } from "crypto";
72
72
  import { readFile } from "fs/promises";
73
73
  import { AgentController } from "@mastra/core/agent-controller";
74
74
  import { createCodingAgent } from "@mastra/core/coding-agent";
@@ -1,8 +1,8 @@
1
1
  import { getLanguageId } from "./language.js";
2
2
  import { createRequire } from "module";
3
3
  import path, { join } from "path";
4
- import { spawn } from "child_process";
5
4
  import { existsSync } from "fs";
5
+ import { spawn } from "child_process";
6
6
  import { pathToFileURL } from "url";
7
7
  //#region src/lsp/server.ts
8
8
  /**
@@ -9,6 +9,7 @@ import type { GlobalSettings } from './settings.js';
9
9
  export declare function hasExplicitOMConfiguration(settings: GlobalSettings): boolean;
10
10
  /** Seed a built-in OM pack unless the user already chose one; true when settings changed. */
11
11
  export declare function applyOMDefaultIfUnconfigured(settings: GlobalSettings, pack: OMPack): boolean;
12
+ export declare function seedProviderOMDefault(providerId: string): OMPack | undefined;
12
13
  /**
13
14
  * Apply a role-specific OM model override to an in-memory `GlobalSettings`.
14
15
  *
@@ -1 +1 @@
1
- {"version":3,"file":"om-settings.d.ts","sourceRoot":"","sources":["../../src/onboarding/om-settings.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAGpD,0EAA0E;AAC1E,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAO5E;AAED,6FAA6F;AAC7F,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAO5F;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,UAAU,GAAG,WAAW,EAC9B,OAAO,EAAE,MAAM,EACf,uBAAuB,EAAE,MAAM,GAAG,IAAI,GACrC,IAAI,CAgBN;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAIzE"}
1
+ {"version":3,"file":"om-settings.d.ts","sourceRoot":"","sources":["../../src/onboarding/om-settings.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAGpD,0EAA0E;AAC1E,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAO5E;AAED,6FAA6F;AAC7F,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAO5F;AAED,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAW5E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,UAAU,GAAG,WAAW,EAC9B,OAAO,EAAE,MAAM,EACf,uBAAuB,EAAE,MAAM,GAAG,IAAI,GACrC,IAAI,CAgBN;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAIzE"}
@@ -1,3 +1,4 @@
1
+ import { resolveProviderOMDefault } from "./packs.js";
1
2
  import { loadSettings, saveSettings } from "./settings.js";
2
3
  //#region src/onboarding/om-settings.ts
3
4
  /** Whether the user has already chosen any persisted OM model or pack. */
@@ -14,6 +15,14 @@ function applyOMDefaultIfUnconfigured(settings, pack) {
14
15
  settings.models.omModelOverride = null;
15
16
  return true;
16
17
  }
18
+ function seedProviderOMDefault(providerId) {
19
+ const pack = resolveProviderOMDefault(providerId);
20
+ if (pack.id === "custom") return void 0;
21
+ const settings = loadSettings();
22
+ if (!applyOMDefaultIfUnconfigured(settings, pack)) return void 0;
23
+ saveSettings(settings);
24
+ return pack;
25
+ }
17
26
  /**
18
27
  * Apply a role-specific OM model override to an in-memory `GlobalSettings`.
19
28
  *
@@ -42,6 +51,6 @@ function persistOmObserveAttachments(value) {
42
51
  saveSettings(settings);
43
52
  }
44
53
  //#endregion
45
- export { applyOMDefaultIfUnconfigured, applyOmRoleOverride, hasExplicitOMConfiguration, persistOmObserveAttachments };
54
+ export { applyOMDefaultIfUnconfigured, applyOmRoleOverride, hasExplicitOMConfiguration, persistOmObserveAttachments, seedProviderOMDefault };
46
55
 
47
56
  //# sourceMappingURL=om-settings.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"om-settings.js","names":[],"sources":["../../src/onboarding/om-settings.ts"],"sourcesContent":["/**\n * Pure `GlobalSettings` mutation helpers for observational-memory (OM)\n * configuration. Shared by the TUI `/om` command and the web settings\n * routes — no UI dependencies.\n */\nimport type { OMPack } from './packs.js';\nimport type { GlobalSettings } from './settings.js';\nimport { loadSettings, saveSettings } from './settings.js';\n\n/** Whether the user has already chosen any persisted OM model or pack. */\nexport function hasExplicitOMConfiguration(settings: GlobalSettings): boolean {\n const { activeOmPackId, omModelOverride, observerModelOverride, reflectorModelOverride } = settings.models;\n if (omModelOverride || observerModelOverride || reflectorModelOverride) return true;\n\n // 'custom' without a model is what onboarding persists when no provider was\n // reachable — a forced non-choice, not a preference worth preserving.\n return [settings.onboarding.omPackId, activeOmPackId].some(packId => packId && packId !== 'custom');\n}\n\n/** Seed a built-in OM pack unless the user already chose one; true when settings changed. */\nexport function applyOMDefaultIfUnconfigured(settings: GlobalSettings, pack: OMPack): boolean {\n if (hasExplicitOMConfiguration(settings)) return false;\n\n settings.onboarding.omPackId = pack.id;\n settings.models.activeOmPackId = pack.id;\n settings.models.omModelOverride = null;\n return true;\n}\n\n/**\n * Apply a role-specific OM model override to an in-memory `GlobalSettings`.\n *\n * When switching `activeOmPackId` from a built-in pack to `'custom'` we also\n * snapshot the *other* role's currently-resolved model into its override\n * field. Without this, the other role would silently lose its model on next\n * startup because `resolveOmRoleModel` would no longer resolve it from the\n * (now-overridden) pack.\n *\n * Exported for unit testing; `persistOmRoleOverride` is the disk-backed wrapper.\n */\nexport function applyOmRoleOverride(\n settings: GlobalSettings,\n role: 'observer' | 'reflector',\n modelId: string,\n otherRoleCurrentModelId: string | null,\n): void {\n const wasBuiltinPack = settings.models.activeOmPackId !== null && settings.models.activeOmPackId !== 'custom';\n\n if (role === 'observer') {\n if (wasBuiltinPack && otherRoleCurrentModelId && !settings.models.reflectorModelOverride) {\n settings.models.reflectorModelOverride = otherRoleCurrentModelId;\n }\n settings.models.observerModelOverride = modelId;\n } else {\n if (wasBuiltinPack && otherRoleCurrentModelId && !settings.models.observerModelOverride) {\n settings.models.observerModelOverride = otherRoleCurrentModelId;\n }\n settings.models.reflectorModelOverride = modelId;\n }\n\n settings.models.activeOmPackId = 'custom';\n}\n\nexport function persistOmObserveAttachments(value: 'auto' | boolean): void {\n const settings = loadSettings();\n settings.models.omObserveAttachments = value;\n saveSettings(settings);\n}\n"],"mappings":";;;AAUA,SAAgB,2BAA2B,UAAmC;CAC5E,MAAM,EAAE,gBAAgB,iBAAiB,uBAAuB,2BAA2B,SAAS;CACpG,IAAI,mBAAmB,yBAAyB,wBAAwB,OAAO;CAI/E,OAAO,CAAC,SAAS,WAAW,UAAU,cAAc,CAAC,CAAC,MAAK,WAAU,UAAU,WAAW,QAAQ;AACpG;;AAGA,SAAgB,6BAA6B,UAA0B,MAAuB;CAC5F,IAAI,2BAA2B,QAAQ,GAAG,OAAO;CAEjD,SAAS,WAAW,WAAW,KAAK;CACpC,SAAS,OAAO,iBAAiB,KAAK;CACtC,SAAS,OAAO,kBAAkB;CAClC,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgB,oBACd,UACA,MACA,SACA,yBACM;CACN,MAAM,iBAAiB,SAAS,OAAO,mBAAmB,QAAQ,SAAS,OAAO,mBAAmB;CAErG,IAAI,SAAS,YAAY;EACvB,IAAI,kBAAkB,2BAA2B,CAAC,SAAS,OAAO,wBAChE,SAAS,OAAO,yBAAyB;EAE3C,SAAS,OAAO,wBAAwB;CAC1C,OAAO;EACL,IAAI,kBAAkB,2BAA2B,CAAC,SAAS,OAAO,uBAChE,SAAS,OAAO,wBAAwB;EAE1C,SAAS,OAAO,yBAAyB;CAC3C;CAEA,SAAS,OAAO,iBAAiB;AACnC;AAEA,SAAgB,4BAA4B,OAA+B;CACzE,MAAM,WAAW,aAAa;CAC9B,SAAS,OAAO,uBAAuB;CACvC,aAAa,QAAQ;AACvB"}
1
+ {"version":3,"file":"om-settings.js","names":[],"sources":["../../src/onboarding/om-settings.ts"],"sourcesContent":["/**\n * Pure `GlobalSettings` mutation helpers for observational-memory (OM)\n * configuration. Shared by the TUI `/om` command and the web settings\n * routes — no UI dependencies.\n */\nimport type { OMPack } from './packs.js';\nimport { resolveProviderOMDefault } from './packs.js';\nimport type { GlobalSettings } from './settings.js';\nimport { loadSettings, saveSettings } from './settings.js';\n\n/** Whether the user has already chosen any persisted OM model or pack. */\nexport function hasExplicitOMConfiguration(settings: GlobalSettings): boolean {\n const { activeOmPackId, omModelOverride, observerModelOverride, reflectorModelOverride } = settings.models;\n if (omModelOverride || observerModelOverride || reflectorModelOverride) return true;\n\n // 'custom' without a model is what onboarding persists when no provider was\n // reachable — a forced non-choice, not a preference worth preserving.\n return [settings.onboarding.omPackId, activeOmPackId].some(packId => packId && packId !== 'custom');\n}\n\n/** Seed a built-in OM pack unless the user already chose one; true when settings changed. */\nexport function applyOMDefaultIfUnconfigured(settings: GlobalSettings, pack: OMPack): boolean {\n if (hasExplicitOMConfiguration(settings)) return false;\n\n settings.onboarding.omPackId = pack.id;\n settings.models.activeOmPackId = pack.id;\n settings.models.omModelOverride = null;\n return true;\n}\n\nexport function seedProviderOMDefault(providerId: string): OMPack | undefined {\n const pack = resolveProviderOMDefault(providerId);\n // No cheap OM pack for this provider — leave OM open for a later login rather\n // than pinning observation and reflection to a full-size coding model.\n if (pack.id === 'custom') return undefined;\n\n const settings = loadSettings();\n if (!applyOMDefaultIfUnconfigured(settings, pack)) return undefined;\n\n saveSettings(settings);\n return pack;\n}\n\n/**\n * Apply a role-specific OM model override to an in-memory `GlobalSettings`.\n *\n * When switching `activeOmPackId` from a built-in pack to `'custom'` we also\n * snapshot the *other* role's currently-resolved model into its override\n * field. Without this, the other role would silently lose its model on next\n * startup because `resolveOmRoleModel` would no longer resolve it from the\n * (now-overridden) pack.\n *\n * Exported for unit testing; `persistOmRoleOverride` is the disk-backed wrapper.\n */\nexport function applyOmRoleOverride(\n settings: GlobalSettings,\n role: 'observer' | 'reflector',\n modelId: string,\n otherRoleCurrentModelId: string | null,\n): void {\n const wasBuiltinPack = settings.models.activeOmPackId !== null && settings.models.activeOmPackId !== 'custom';\n\n if (role === 'observer') {\n if (wasBuiltinPack && otherRoleCurrentModelId && !settings.models.reflectorModelOverride) {\n settings.models.reflectorModelOverride = otherRoleCurrentModelId;\n }\n settings.models.observerModelOverride = modelId;\n } else {\n if (wasBuiltinPack && otherRoleCurrentModelId && !settings.models.observerModelOverride) {\n settings.models.observerModelOverride = otherRoleCurrentModelId;\n }\n settings.models.reflectorModelOverride = modelId;\n }\n\n settings.models.activeOmPackId = 'custom';\n}\n\nexport function persistOmObserveAttachments(value: 'auto' | boolean): void {\n const settings = loadSettings();\n settings.models.omObserveAttachments = value;\n saveSettings(settings);\n}\n"],"mappings":";;;;AAWA,SAAgB,2BAA2B,UAAmC;CAC5E,MAAM,EAAE,gBAAgB,iBAAiB,uBAAuB,2BAA2B,SAAS;CACpG,IAAI,mBAAmB,yBAAyB,wBAAwB,OAAO;CAI/E,OAAO,CAAC,SAAS,WAAW,UAAU,cAAc,CAAC,CAAC,MAAK,WAAU,UAAU,WAAW,QAAQ;AACpG;;AAGA,SAAgB,6BAA6B,UAA0B,MAAuB;CAC5F,IAAI,2BAA2B,QAAQ,GAAG,OAAO;CAEjD,SAAS,WAAW,WAAW,KAAK;CACpC,SAAS,OAAO,iBAAiB,KAAK;CACtC,SAAS,OAAO,kBAAkB;CAClC,OAAO;AACT;AAEA,SAAgB,sBAAsB,YAAwC;CAC5E,MAAM,OAAO,yBAAyB,UAAU;CAGhD,IAAI,KAAK,OAAO,UAAU,OAAO,KAAA;CAEjC,MAAM,WAAW,aAAa;CAC9B,IAAI,CAAC,6BAA6B,UAAU,IAAI,GAAG,OAAO,KAAA;CAE1D,aAAa,QAAQ;CACrB,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgB,oBACd,UACA,MACA,SACA,yBACM;CACN,MAAM,iBAAiB,SAAS,OAAO,mBAAmB,QAAQ,SAAS,OAAO,mBAAmB;CAErG,IAAI,SAAS,YAAY;EACvB,IAAI,kBAAkB,2BAA2B,CAAC,SAAS,OAAO,wBAChE,SAAS,OAAO,yBAAyB;EAE3C,SAAS,OAAO,wBAAwB;CAC1C,OAAO;EACL,IAAI,kBAAkB,2BAA2B,CAAC,SAAS,OAAO,uBAChE,SAAS,OAAO,wBAAwB;EAE1C,SAAS,OAAO,yBAAyB;CAC3C;CAEA,SAAS,OAAO,iBAAiB;AACnC;AAEA,SAAgB,4BAA4B,OAA+B;CACzE,MAAM,WAAW,aAAa;CAC9B,SAAS,OAAO,uBAAuB;CACvC,aAAa,QAAQ;AACvB"}
@@ -1,10 +1,10 @@
1
- import { THINKING_LEVEL_VALUES, isThinkingLevelSetting, resolveDefaultThinkingLevel as resolveDefaultThinkingLevel$1 } from "../thinking.js";
2
1
  import { getAppDataDir } from "../utils/project.js";
3
2
  import { AuthStorage, PROVIDER_DEFAULT_MODELS } from "../auth/storage.js";
3
+ import { pruneUnknownModePackFallbacks, pruneUnknownPackAccountPreferences } from "./packs.js";
4
4
  import { OPENAI_PREFIX, normalizeAnthropicModelId, remapOpenAIModelForCodexOAuth, stripMastraGatewayPrefix } from "../providers/model-ids.js";
5
+ import { THINKING_LEVEL_VALUES, isThinkingLevelSetting, resolveDefaultThinkingLevel as resolveDefaultThinkingLevel$1 } from "../thinking.js";
5
6
  import { buildCodexStagehandFetch, createCodexMiddleware } from "../providers/openai-codex.js";
6
7
  import { DEFAULT_STT_PROVIDER, resolveSTTModel } from "../voice/stt-registry.js";
7
- import { pruneUnknownModePackFallbacks, pruneUnknownPackAccountPreferences } from "./packs.js";
8
8
  import { dirname, join } from "path";
9
9
  import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "fs";
10
10
  //#region src/onboarding/settings.ts
@@ -1,6 +1,6 @@
1
- import { supportsMaxReasoningEffort } from "../thinking.js";
2
1
  import { AuthStorage } from "../auth/storage.js";
3
2
  import { ProviderAuthRequiredError } from "../auth/provider-auth-error.js";
3
+ import { supportsMaxReasoningEffort } from "../thinking.js";
4
4
  import { createOpenAI } from "@ai-sdk/openai";
5
5
  import { wrapLanguageModel } from "ai";
6
6
  //#region src/providers/openai-codex.ts
@@ -1,6 +1,6 @@
1
1
  import * as path$1 from "path";
2
- import { execFile } from "child_process";
3
2
  import * as fs$1 from "fs";
3
+ import { execFile } from "child_process";
4
4
  //#region src/schedules/run-script.ts
5
5
  /**
6
6
  * Runs a schedule's script file at fire time.
package/dist/schema.js CHANGED
@@ -1,5 +1,5 @@
1
- import { THINKING_LEVEL_VALUES } from "./thinking.js";
2
1
  import { DEFAULT_CONFIG_DIR, DEFAULT_OM_MODEL_ID } from "./constants.js";
2
+ import { THINKING_LEVEL_VALUES } from "./thinking.js";
3
3
  import { z } from "zod";
4
4
  //#region src/schema.ts
5
5
  const stateSchema = z.object({
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Best-effort browser opening for OAuth and other login flows.
3
+ */
4
+ /**
5
+ * Open a URL in the default browser without going through a shell.
6
+ * Only well-formed http(s) URLs are opened; anything else is ignored
7
+ * (the URL is still displayed for the user to open manually).
8
+ */
9
+ export declare function openUrlInBrowser(url: string): void;
10
+ //# sourceMappingURL=open-url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"open-url.d.ts","sourceRoot":"","sources":["../../src/utils/open-url.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAsBlD"}
@@ -0,0 +1,30 @@
1
+ import { spawn } from "child_process";
2
+ //#region src/utils/open-url.ts
3
+ /**
4
+ * Best-effort browser opening for OAuth and other login flows.
5
+ */
6
+ /**
7
+ * Open a URL in the default browser without going through a shell.
8
+ * Only well-formed http(s) URLs are opened; anything else is ignored
9
+ * (the URL is still displayed for the user to open manually).
10
+ */
11
+ function openUrlInBrowser(url) {
12
+ let parsed;
13
+ try {
14
+ parsed = new URL(url);
15
+ } catch {
16
+ return;
17
+ }
18
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return;
19
+ const [cmd, args] = process.platform === "darwin" ? ["open", [url]] : process.platform === "win32" ? ["rundll32", ["url.dll,FileProtocolHandler", url]] : ["xdg-open", [url]];
20
+ const child = spawn(cmd, args, {
21
+ stdio: "ignore",
22
+ detached: true
23
+ });
24
+ child.on("error", () => {});
25
+ child.unref();
26
+ }
27
+ //#endregion
28
+ export { openUrlInBrowser };
29
+
30
+ //# sourceMappingURL=open-url.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"open-url.js","names":[],"sources":["../../src/utils/open-url.ts"],"sourcesContent":["/**\n * Best-effort browser opening for OAuth and other login flows.\n */\n\nimport { spawn } from 'node:child_process';\n\n/**\n * Open a URL in the default browser without going through a shell.\n * Only well-formed http(s) URLs are opened; anything else is ignored\n * (the URL is still displayed for the user to open manually).\n */\nexport function openUrlInBrowser(url: string): void {\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n return;\n }\n if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {\n return;\n }\n\n const [cmd, args]: [string, string[]] =\n process.platform === 'darwin'\n ? ['open', [url]]\n : process.platform === 'win32'\n ? ['rundll32', ['url.dll,FileProtocolHandler', url]]\n : ['xdg-open', [url]];\n\n const child = spawn(cmd, args, { stdio: 'ignore', detached: true });\n // Opening the browser is best-effort — the URL is shown to the user as well.\n child.on('error', () => {});\n child.unref();\n}\n"],"mappings":";;;;;;;;;;AAWA,SAAgB,iBAAiB,KAAmB;CAClD,IAAI;CACJ,IAAI;EACF,SAAS,IAAI,IAAI,GAAG;CACtB,QAAQ;EACN;CACF;CACA,IAAI,OAAO,aAAa,WAAW,OAAO,aAAa,UACrD;CAGF,MAAM,CAAC,KAAK,QACV,QAAQ,aAAa,WACjB,CAAC,QAAQ,CAAC,GAAG,CAAC,IACd,QAAQ,aAAa,UACnB,CAAC,YAAY,CAAC,+BAA+B,GAAG,CAAC,IACjD,CAAC,YAAY,CAAC,GAAG,CAAC;CAE1B,MAAM,QAAQ,MAAM,KAAK,MAAM;EAAE,OAAO;EAAU,UAAU;CAAK,CAAC;CAElE,MAAM,GAAG,eAAe,CAAC,CAAC;CAC1B,MAAM,MAAM;AACd"}
@@ -1,9 +1,9 @@
1
1
  import { DEFAULT_CONFIG_DIR } from "../constants.js";
2
2
  import path from "path";
3
- import { execFile, execSync } from "child_process";
3
+ import { createHash } from "crypto";
4
4
  import fs from "fs";
5
+ import { execFile, execSync } from "child_process";
5
6
  import os from "os";
6
- import { createHash } from "crypto";
7
7
  //#region src/utils/project.ts
8
8
  /**
9
9
  * Project detection utilities
@@ -1,6 +1,6 @@
1
1
  import * as path$1 from "path";
2
- import { execSync } from "child_process";
3
2
  import { promises } from "fs";
3
+ import { execSync } from "child_process";
4
4
  //#region src/utils/slash-command-processor.ts
5
5
  /**
6
6
  * Process a slash command by replacing variables and executing shell commands
@@ -1,6 +1,6 @@
1
1
  import { dirname, join, resolve, sep } from "path";
2
- import { execFile } from "child_process";
3
2
  import { readFileSync, realpathSync } from "fs";
3
+ import { execFile } from "child_process";
4
4
  import { homedir } from "os";
5
5
  import { fileURLToPath } from "url";
6
6
  //#region src/utils/update-check.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/code-sdk",
3
- "version": "1.11.0-alpha.4",
3
+ "version": "1.11.0-alpha.5",
4
4
  "description": "Mastra Code SDK: the agent core behind Mastra Code (everything except the TUI) — build your own UIs and surfaces on top of it",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -63,19 +63,19 @@
63
63
  "yaml": "^2.7.1",
64
64
  "zod": "^4.6.4",
65
65
  "@mastra/agent-browser": "0.5.3",
66
- "@mastra/duckdb": "1.13.0-alpha.1",
67
- "@mastra/core": "1.75.0-alpha.3",
68
- "@mastra/github-signals": "0.5.1",
69
66
  "@mastra/fastembed": "1.3.2",
70
- "@mastra/libsql": "1.25.1-alpha.0",
67
+ "@mastra/github-signals": "0.5.1",
71
68
  "@mastra/mcp": "2.2.0-alpha.0",
69
+ "@mastra/libsql": "1.25.1-alpha.0",
70
+ "@mastra/duckdb": "1.13.0-alpha.1",
72
71
  "@mastra/memory": "1.36.0-alpha.2",
73
- "@mastra/parallel": "0.1.2",
74
- "@mastra/observability": "1.18.3",
75
72
  "@mastra/pg": "1.30.0-alpha.2",
73
+ "@mastra/observability": "1.18.3",
74
+ "@mastra/parallel": "0.1.2",
76
75
  "@mastra/stagehand": "0.3.5",
77
76
  "@mastra/schema-compat": "1.3.13",
78
- "@mastra/tavily": "1.1.2"
77
+ "@mastra/tavily": "1.1.2",
78
+ "@mastra/core": "1.75.0-alpha.3"
79
79
  },
80
80
  "devDependencies": {
81
81
  "@libsql/client": "^0.18.0",