process-local-application-contract.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import type {
  2. GlobalEvent,
  3. GlobalThreadGoal,
  4. } from "@mavis/shared/global-events";
  5. import type { ThreadGoalAttachment, ThreadGoalPatchInput } from "@mavis/goal";
  6. import type {
  7. AskQuestionnaireReplyAnswer,
  8. AskQuestionnaireRequest,
  9. } from "@mavis/shared/questionnaire";
  10. import type { LocalSkillService } from "@mavis/local-runtime";
  11. import type { LocalMcpPublicFacade } from "../../service/mcp/index.js";
  12. import type {
  13. ListInstalledPluginsInput as ListInstalledPluginsReq,
  14. ListInstalledPluginsResult as ListInstalledPluginsResp,
  15. ListMarketplacePluginsInput as ListMarketplacePluginsReq,
  16. ListMarketplacePluginsResult as ListMarketplacePluginsResp,
  17. ListRuntimeSkillsInput as ListRuntimeSkillsReq,
  18. ListRuntimeSkillsResult as ListRuntimeSkillsResp,
  19. MutatePluginInput as MutatePluginReq,
  20. MutatePluginResult as MutatePluginResp,
  21. } from "@mavis/protocol/local";
  22. import type { MiniAppSurfaceSummary } from "@mavis/shared/miniapp-surface";
  23. import type { SessionReportManifest } from "../../service/session-system/index.js";
  24. import type {
  25. ByokProviderPresetView,
  26. CodexOAuthStartResult,
  27. CodexOAuthLoginOptions,
  28. CodexOAuthStatus,
  29. UserModelInputView,
  30. } from "../../service/model-system/index.js";
  31. export type ProcessLocalModelInput = Omit<UserModelInputView, "modalities"> & {
  32. modalities?: { input?: readonly string[]; output?: readonly string[] };
  33. };
  34. export interface ProcessLocalMiniAppLaunch {
  35. readonly miniApp: MiniAppSurfaceSummary;
  36. /** Main-process-only launch coordinates. Never forward this object to a renderer. */
  37. readonly launch: {
  38. readonly origin: string;
  39. readonly surfacePath: string;
  40. readonly runId: string;
  41. };
  42. }
  43. /** Host-owned presentation capability used by the Agent MiniApp control tool. */
  44. export interface MiniAppPresenter {
  45. open(input: {
  46. readonly sessionId: string;
  47. readonly pluginId: string;
  48. readonly signal?: AbortSignal;
  49. }): Promise<void>;
  50. /** Closes every managed native surface for one plugin after its runtime has stopped. */
  51. close(input: { readonly pluginId: string }): Promise<void>;
  52. }
  53. type LocalRuntimeBackgroundTaskStatus =
  54. | "queued"
  55. | "running"
  56. | "stopping"
  57. | "succeeded"
  58. | "failed"
  59. | "canceled"
  60. | "lost";
  61. interface LocalRuntimeBackgroundTaskView {
  62. readonly taskId: string;
  63. readonly kind: "bash" | "subagent" | "workflow" | "custom";
  64. readonly status: LocalRuntimeBackgroundTaskStatus;
  65. readonly ownerSessionId: string;
  66. readonly description?: string;
  67. /** Persisted execution metadata, including the full Bash command when available. */
  68. readonly metadata?: Readonly<Record<string, unknown>>;
  69. readonly createdAt: number;
  70. readonly updatedAt: number;
  71. readonly startedAt?: number;
  72. readonly endedAt?: number;
  73. readonly deliveredAt?: number;
  74. readonly lastError?: { readonly message: string };
  75. }
  76. interface LocalRuntimeBackgroundTaskQuery {
  77. readonly ownerSessionId: string;
  78. readonly statuses?: readonly LocalRuntimeBackgroundTaskStatus[];
  79. readonly kinds?: readonly LocalRuntimeBackgroundTaskView["kind"][];
  80. readonly undeliveredOnly?: boolean;
  81. readonly limit?: number;
  82. }
  83. /** Internal peripheral Application surface consumed by process-local delivery. */
  84. export interface LocalRuntimeApplication {
  85. readonly events: {
  86. watch(signal?: AbortSignal): AsyncGenerator<GlobalEvent>;
  87. };
  88. readonly usage?: {
  89. watchCommits(signal?: AbortSignal): AsyncGenerator<string>;
  90. };
  91. readonly skills: Pick<LocalSkillService, "listSkills"> & {
  92. listRuntimeSkills(
  93. req: ListRuntimeSkillsReq,
  94. ): Promise<ListRuntimeSkillsResp>;
  95. };
  96. readonly plugins: {
  97. refresh(): Promise<void>;
  98. listMarketplacePlugins(
  99. req: ListMarketplacePluginsReq,
  100. ): Promise<ListMarketplacePluginsResp>;
  101. listInstalledPlugins(
  102. req: ListInstalledPluginsReq,
  103. ): Promise<ListInstalledPluginsResp>;
  104. installPlugin(req: MutatePluginReq): Promise<MutatePluginResp>;
  105. uninstallPlugin(req: MutatePluginReq): Promise<MutatePluginResp>;
  106. enablePlugin(req: MutatePluginReq): Promise<MutatePluginResp>;
  107. disablePlugin(req: MutatePluginReq): Promise<MutatePluginResp>;
  108. };
  109. readonly miniApps?: {
  110. list(): Promise<{ readonly miniApps: readonly MiniAppSurfaceSummary[] }>;
  111. open(input: {
  112. readonly pluginId: string;
  113. readonly signal?: AbortSignal;
  114. }): Promise<ProcessLocalMiniAppLaunch>;
  115. stop(input: {
  116. readonly pluginId: string;
  117. readonly signal?: AbortSignal;
  118. }): Promise<void>;
  119. };
  120. readonly goals?: {
  121. isEnabled(): boolean;
  122. get(sessionId: string): Promise<GlobalThreadGoal | undefined>;
  123. create(input: {
  124. sessionId: string;
  125. objective: string;
  126. tokenBudget?: number | null;
  127. kickoffAttachments?: readonly ThreadGoalAttachment[];
  128. }): Promise<GlobalThreadGoal>;
  129. patch(
  130. sessionId: string,
  131. patch: Pick<ThreadGoalPatchInput, "status" | "objective" | "tokenBudget">,
  132. ): Promise<GlobalThreadGoal>;
  133. clear(sessionId: string): Promise<boolean>;
  134. };
  135. readonly mcp?: Pick<
  136. LocalMcpPublicFacade,
  137. | "listLocalMcpServers"
  138. | "listMcpCapabilities"
  139. | "configureSessionServers"
  140. | "clearSessionServers"
  141. | "inspectProjectMcp"
  142. >;
  143. readonly questionnaires?: {
  144. getPending(input: {
  145. agentName: string;
  146. sessionId: string;
  147. }): Promise<AskQuestionnaireRequest | undefined>;
  148. getLatestPlanReview(input: {
  149. agentName: string;
  150. sessionId: string;
  151. }): Promise<AskQuestionnaireRequest | undefined>;
  152. reply(input: {
  153. agentName: string;
  154. requestId: string;
  155. answers: AskQuestionnaireReplyAnswer[];
  156. submittedAt: number;
  157. }): Promise<{
  158. ok: true;
  159. requestId: string;
  160. sessionId: string;
  161. agentName?: string;
  162. answeredAt: number;
  163. }>;
  164. dismiss(input: { agentName: string; requestId: string }): Promise<{
  165. ok: true;
  166. requestId: string;
  167. sessionId: string;
  168. agentName?: string;
  169. dismissedAt: number;
  170. }>;
  171. };
  172. readonly plan?: {
  173. isEntryEnabled(): boolean;
  174. };
  175. readonly backgroundTasks?: {
  176. list(
  177. input: LocalRuntimeBackgroundTaskQuery,
  178. ): Promise<readonly LocalRuntimeBackgroundTaskView[]>;
  179. };
  180. readonly workspace?: {
  181. readonly git: {
  182. getMetadata(workspaceDir: string): Promise<Record<string, unknown>>;
  183. getReviewLink?(
  184. workspaceDir: string,
  185. branch: string,
  186. ): Promise<Record<string, unknown> | undefined>;
  187. };
  188. listFileTree?(input: {
  189. workspaceDir: string;
  190. path?: string;
  191. signal?: AbortSignal;
  192. }): Promise<readonly unknown[]>;
  193. searchFiles?(input: {
  194. workspaceDir: string;
  195. query: string;
  196. limit: number;
  197. signal?: AbortSignal;
  198. }): Promise<readonly string[]>;
  199. };
  200. readonly permissions?: {
  201. listPending(input?: {
  202. signal?: AbortSignal;
  203. }): Promise<readonly LocalRuntimePendingPermission[]>;
  204. reply(input: {
  205. requestIds: readonly string[];
  206. decision: "allowAlways" | "allowOnce" | "deny";
  207. }): Promise<{ processed: readonly string[]; skipped: readonly string[] }>;
  208. };
  209. readonly account?: {
  210. getStatus(input?: {
  211. sessionId?: string;
  212. model?: string;
  213. }): Promise<Record<string, unknown>>;
  214. };
  215. readonly diagnostics?: {
  216. getRuntimeSnapshot(): Promise<Record<string, unknown>>;
  217. collectSessionReport?(sessionId: string): Promise<SessionReportManifest>;
  218. };
  219. readonly instructions?: {
  220. listSources(input: {
  221. workspaceDir: string;
  222. }): Promise<
  223. readonly { readonly scope: "global" | "project"; readonly path: string }[]
  224. >;
  225. };
  226. readonly configuration?: {
  227. getPermissionMode(): Promise<unknown>;
  228. setPermissionMode(input: {
  229. mode: "default" | "acceptEdits" | "bypassPermissions" | "auto" | "off";
  230. }): Promise<unknown>;
  231. };
  232. readonly models?: {
  233. list(input?: { sessionId?: string }): Promise<readonly unknown[]>;
  234. select(input: {
  235. providerId: string;
  236. modelId: string;
  237. variant?: string;
  238. sessionId?: string;
  239. }): Promise<boolean>;
  240. };
  241. readonly modelProviders?: {
  242. listProviderPresets(): Promise<readonly ByokProviderPresetView[]>;
  243. getCodexOAuthStatus(): Promise<CodexOAuthStatus>;
  244. startCodexOAuthLogin(
  245. options?: CodexOAuthLoginOptions,
  246. ): Promise<CodexOAuthStartResult>;
  247. cancelCodexOAuthLogin(loginId: string): Promise<CodexOAuthStatus>;
  248. listUser(): Promise<readonly Record<string, unknown>[]>;
  249. getMiniMaxApiKeyStatus(): Promise<Record<string, unknown>>;
  250. getMiniMaxModelSource(): Promise<"token_plan" | "minimax_api_key">;
  251. setMiniMaxModelSource(input: {
  252. source: "token_plan" | "minimax_api_key";
  253. }): Promise<"token_plan" | "minimax_api_key">;
  254. upsertMiniMaxApiKey(input: {
  255. apiKey: string;
  256. saveAndUse?: boolean;
  257. }): Promise<unknown>;
  258. create(input: {
  259. name?: string;
  260. baseUrl: string;
  261. apiKey: string;
  262. apiFormat?: string;
  263. models?: readonly ProcessLocalModelInput[];
  264. saveAndUse?: boolean;
  265. }): Promise<unknown>;
  266. discoverCandidate(input: {
  267. providerId: string;
  268. expectedRevision: string;
  269. baseUrl: string;
  270. }): Promise<readonly { modelId: string; displayName?: string }[]>;
  271. saveCandidate(input: {
  272. candidate: {
  273. providerId?: string;
  274. expectedRevision?: string;
  275. name?: string;
  276. baseUrl: string;
  277. apiKey?: string;
  278. apiFormat?: string;
  279. models?: readonly ProcessLocalModelInput[];
  280. };
  281. modelId?: string;
  282. saveAndUse?: boolean;
  283. skipConnectionTest?: boolean;
  284. }): Promise<{
  285. success: boolean;
  286. status?: Record<string, unknown>;
  287. provider?: Record<string, unknown>;
  288. }>;
  289. update(input: {
  290. providerId: string;
  291. name?: string;
  292. baseUrl?: string;
  293. apiKey?: string;
  294. apiFormat?: string;
  295. enabled?: boolean;
  296. models?: readonly ProcessLocalModelInput[];
  297. saveAndUse?: boolean;
  298. }): Promise<unknown>;
  299. delete(input: { providerId: string }): Promise<void>;
  300. testProvider(input: { providerId: string }): Promise<unknown>;
  301. testModel(input: { providerId: string; modelId: string }): Promise<unknown>;
  302. };
  303. }
  304. export interface LocalRuntimePendingPermission {
  305. readonly requestId: string;
  306. readonly sessionId: string;
  307. readonly agentName: string;
  308. readonly toolName: string;
  309. readonly ruleContents: readonly string[];
  310. readonly toolInput?: string;
  311. readonly toolDescription?: string;
  312. readonly reason: string;
  313. readonly allowAlwaysSupported: boolean;
  314. readonly createdAt: number;
  315. }