contract.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import type {
  2. McpAppsAppToolCallRequest,
  3. McpAppsDisplayMode,
  4. McpToolUiCsp,
  5. PluginSandboxHandle,
  6. PluginUiScopeRef,
  7. } from "@zcode/shared/mcp-apps";
  8. import { buildPluginSandboxScopeId } from "@zcode/shared/mcp-apps";
  9. import type { AttachmentRef, ConversationInputSource } from "@zcode/shared/zcode-protocol-v4";
  10. import type { ReactNode } from "react";
  11. import type { PluginUiImageBlock } from "./domain/pluginUiImageBlocks.js";
  12. /**
  13. * 从 ToolCallRow.display.ui 读出的插件 UI 呈现信息;与 agent 侧 `display.ui` schema 同构,
  14. * 由 `readPluginUiPresentation` 解析,解析失败返回 null 并回退到普通 McpToolCallBlock。
  15. */
  16. export interface PluginUiPresentation {
  17. /** display.serverName:UI 回调工具时的 namespaced server,归属由 agent 侧校验。 */
  18. serverName: string;
  19. pluginId: string;
  20. resourceUri: string;
  21. preferredDisplayMode?: McpAppsDisplayMode;
  22. prefersBorder?: boolean;
  23. csp?: McpToolUiCsp;
  24. structuredContent?: string;
  25. widgetMeta?: string;
  26. /** 原始 content 数组的 JSON 文本;缺省时 tool-result 退回工具输出纯文本。 */
  27. content?: string;
  28. truncated?: boolean;
  29. truncatedBytes?: number;
  30. /** MCP 结果 isError(v4 行仍是 success);tool-result 带 isError,面板不用它覆盖成功快照。 */
  31. isError?: true;
  32. /** 工具级强制常驻(display.ui.showInline)。 */
  33. showInline?: true;
  34. /** 工具结果归属的面板 id;命中同会话已打开的面板 tab 时结果路由进面板。 */
  35. surface?: string;
  36. }
  37. /**
  38. * 打开侧栏 plugin-ui tab 的请求;工具卡片给 toolCallId,面板入口(launcher / surface 路由)给 surfaceId + serverName。
  39. * 两者恰有其一。
  40. */
  41. export type PluginUiOpenTarget =
  42. | { toolCallId: string; surfaceId?: never; serverName?: string }
  43. | { surfaceId: string; serverName: string; toolCallId?: never };
  44. export type OpenPluginUiSideTabRequest = PluginUiOpenTarget & {
  45. parentSessionId: string;
  46. pluginId: string;
  47. resourceUri: string;
  48. title: string;
  49. workspacePath: string;
  50. workspaceIdentity?: string;
  51. remoteSessionId?: string | null;
  52. };
  53. /**
  54. * 侧栏 tab;工具卡片 id 为 `plugin-ui:<ws>:<session>:<pluginId>:<toolCallId>`,面板为 `plugin-ui:<ws>:<session>:<pluginId>:surface:<surfaceId>`;
  55. * 按 parentSessionId 作用域可见。
  56. */
  57. export interface PluginUiSidePaneTab {
  58. id: string;
  59. type: "plugin-ui";
  60. ownerTaskId?: string | null;
  61. workspaceKey?: string | null;
  62. openedAt?: number;
  63. parentSessionId: string;
  64. toolCallId?: string;
  65. surfaceId?: string;
  66. serverName?: string;
  67. pluginId: string;
  68. resourceUri: string;
  69. title: string;
  70. workspacePath: string;
  71. workspaceIdentity?: string;
  72. remoteSessionId?: string | null;
  73. }
  74. export const PLUGIN_UI_CARD_DEFAULT_HEIGHT_PX = 240;
  75. /** DOM 位置属于视图,页面实例属于窗口 owner;跨容器移动只能走保活承载层。 */
  76. export interface SandboxPagePlacement {
  77. node: HTMLElement;
  78. kind: "flow" | "inline" | "sidebar";
  79. container?: HTMLElement;
  80. layer?: number;
  81. }
  82. export const PLUGIN_UI_CARD_MIN_HEIGHT_PX = 80;
  83. export const PLUGIN_UI_CARD_MAX_HEIGHT_PX = 1200;
  84. /**
  85. * 侧栏面板头部(AnimatedSidePanePanel 的 TabsList,`!h-12` = 48px)。fullscreen 时经 hostContext.titleBar
  86. * 告诉插件页面视口上方被宿主占用的高度;页面视口本身不与它重叠,所以 inset 都是 0。
  87. */
  88. export const PLUGIN_UI_SIDE_PANE_TITLE_BAR = {
  89. height: 48,
  90. leftInset: 0,
  91. viewportLeftInset: 0,
  92. } as const;
  93. /** 请求 / tab 的作用域:surfaceId 优先,否则 toolCallId;两者都缺视为非法请求。 */
  94. export function readPluginUiScopeRef(
  95. input: Pick<OpenPluginUiSideTabRequest, "toolCallId" | "surfaceId">,
  96. ): PluginUiScopeRef | null {
  97. if (input.surfaceId) return { kind: "surface", surfaceId: input.surfaceId };
  98. if (input.toolCallId) return { kind: "toolCall", toolCallId: input.toolCallId };
  99. return null;
  100. }
  101. /** 全部宿主绑定使用同一工作区身份;路径只用于执行和显示。 */
  102. export interface PluginUiSessionScope {
  103. workspacePath: string;
  104. workspaceIdentity?: string;
  105. remoteSessionId?: string | null;
  106. sessionId: string;
  107. }
  108. /**
  109. * SessionPane 登记给插件 UI 的会话动作。4a-0 起只剩发送后续消息:widgetState 是宿主内存
  110. * (`app/pluginUiWidgetStateStore`),不再经会话命令持久化。
  111. */
  112. export interface PluginUiSessionActions {
  113. /** images 先经会话的附件上传入口变成 AttachmentRef,再随 sendText 发送。 */
  114. sendFollowUp(input: {
  115. prompt: string;
  116. source: ConversationInputSource;
  117. images?: readonly PluginUiImageBlock[];
  118. }): Promise<void>;
  119. }
  120. /** 适配既有 SessionPane 发送入口,不重新实现 admission / owner / ACK。 */
  121. export interface PluginUiSessionCommandPort {
  122. sendText(
  123. prompt: string,
  124. options: { source: ConversationInputSource; attachments?: AttachmentRef[] },
  125. ): Promise<"sent" | "blocked" | "confirmationRequired" | undefined>;
  126. /** 内联图片 → 会话附件引用(走 v4 attachmentPut,与 composer 粘贴截图同一条链路)。 */
  127. uploadAttachment(input: {
  128. fileName: string;
  129. mime: string;
  130. dataBase64: string;
  131. }): Promise<AttachmentRef>;
  132. }
  133. export type PluginUiOpenSurfaceRequest = PluginUiOpenTarget & {
  134. pluginId: string;
  135. resourceUri: string;
  136. title: string;
  137. };
  138. export interface PluginUiToolBinding {
  139. scope: PluginUiSessionScope & { toolCallId: string };
  140. onOpenSidePane?: (request: PluginUiOpenSurfaceRequest) => void;
  141. }
  142. export function buildPluginUiSessionKey(target: PluginUiSessionScope): string {
  143. return JSON.stringify([
  144. target.workspaceIdentity?.trim() || target.workspacePath,
  145. target.sessionId,
  146. ]);
  147. }
  148. export function buildPluginUiSurfaceKey(
  149. target: PluginUiSessionScope & { pluginId: string; serverName?: string; resourceUri?: string },
  150. scope: PluginUiScopeRef,
  151. ): string {
  152. // 修复:surfaceId 由插件自行命名,必须同时隔离工作区、会话和插件。
  153. return JSON.stringify([
  154. buildPluginUiSessionKey(target),
  155. target.pluginId,
  156. target.serverName ?? "",
  157. target.resourceUri ?? "",
  158. buildPluginSandboxScopeId(scope),
  159. ]);
  160. }
  161. export function buildPluginUiSidePaneTabId(request: OpenPluginUiSideTabRequest): string {
  162. const scope = readPluginUiScopeRef(request);
  163. if (!scope) throw new Error("plugin-ui side tab needs toolCallId or surfaceId");
  164. return [
  165. "plugin-ui",
  166. request.workspaceIdentity?.trim() || request.workspacePath,
  167. request.parentSessionId,
  168. request.pluginId,
  169. request.serverName ?? "",
  170. request.resourceUri,
  171. ...(scope.kind === "toolCall" ? [scope.toolCallId] : ["surface", scope.surfaceId]),
  172. ]
  173. .map(encodeURIComponent)
  174. .join(":");
  175. }
  176. export interface PluginUiSidePaneViewProps {
  177. scopeRef: PluginUiScopeRef;
  178. host: {
  179. pageKey: string;
  180. handle: PluginSandboxHandle | null;
  181. phase: "idle" | "preparing" | "mounted" | "running" | "error" | "disposed";
  182. height: number;
  183. error: string | null;
  184. /** 错误态"重新加载"。 */
  185. reload(): void;
  186. };
  187. loadingLabel: string;
  188. errorLabel: string;
  189. retryLabel: string;
  190. onWidthChange(width: number): void;
  191. }
  192. export interface PluginUiToolCallViewProps {
  193. supported: boolean;
  194. fallback: ReactNode;
  195. surface?: string;
  196. preferredDisplayMode: McpAppsDisplayMode;
  197. sidePaneActive: boolean;
  198. host: PluginUiSidePaneViewProps["host"];
  199. prefersBorder: boolean;
  200. /** 被同一逻辑 UI 的较新成功结果替代;只留普通工具结果,不建沙箱或自动开侧栏。 */
  201. superseded: boolean;
  202. /** 手动固定 / 收起(undefined 按默认规则);缺省无入口时不显示按钮。 */
  203. pinned?: boolean;
  204. pinLabel: string;
  205. unpinLabel: string;
  206. onTogglePin?: () => void;
  207. loadingLabel: string;
  208. errorLabel: string;
  209. retryLabel: string;
  210. openedLabel: string;
  211. openLabel: string;
  212. onWidthChange(width: number): void;
  213. onOpenSidePane?: () => void;
  214. }
  215. /**
  216. * 一个沙箱实例接收 server 资源通知的入口。宿主按 (会话, 插件, scopeId, generation=initId)
  217. * 登记,投影里的 `pluginUi.resourceUpdated / resourceListChanged` 增量按 subscribers 路由到对应实例。
  218. */
  219. export interface PluginUiResourceNotificationTarget {
  220. notifyInstanceClosed?(): void;
  221. notifyResourceUpdated(uri: string): void;
  222. notifyResourceListChanged(): void;
  223. /** App-Provided Tools 的实例信箱:模型发起的页面工具调用到达本实例。 */
  224. notifyAppToolCall(call: McpAppsAppToolCallRequest): void;
  225. }