pluginUiResourceNotificationBus.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import type { McpAppInstance } from "@zcode/shared/mcp-apps";
  2. import type { PluginUiInstanceDelta } from "@zcode/shared/zcode-protocol-v4";
  3. import type { PluginUiResourceNotificationTarget } from "../contract.js";
  4. import { buildPluginUiSessionKey } from "../contract.js";
  5. import type { PluginUiHostControllerDeps, PluginUiHostScope } from "./pluginUiHostController.js";
  6. /**
  7. * server 资源通知的路由表(renderer 内存)。
  8. * 键 = (会话键, 插件, 沙箱作用域, 实例代际);投影增量里的 subscribers 携带完整运行凭证,
  9. * 会话与插件由投喂方(该会话的投影 store)补上。没有登记的订阅者直接丢弃——实例已销毁或已换代。
  10. */
  11. const targets = new Map<string, PluginUiResourceNotificationTarget>();
  12. function buildKey(
  13. sessionKey: string,
  14. pluginId: string,
  15. scopeId: string,
  16. generation: number,
  17. instance: McpAppInstance,
  18. ) {
  19. return JSON.stringify([
  20. sessionKey,
  21. pluginId,
  22. scopeId,
  23. generation,
  24. instance.runtimeId,
  25. instance.appIdentity,
  26. instance.token,
  27. ]);
  28. }
  29. export function registerPluginUiResourceTarget(
  30. sessionKey: string,
  31. pluginId: string,
  32. scopeId: string,
  33. generation: number,
  34. target: PluginUiResourceNotificationTarget,
  35. instance: McpAppInstance,
  36. ): () => void {
  37. const key = buildKey(sessionKey, pluginId, scopeId, generation, instance);
  38. targets.set(key, target);
  39. return () => {
  40. if (targets.get(key) === target) targets.delete(key);
  41. };
  42. }
  43. /** 返回实际投递到的实例数(单测与日志用)。 */
  44. export function dispatchPluginUiResourceDelta(
  45. sessionKey: string,
  46. delta: PluginUiInstanceDelta,
  47. ): number {
  48. let delivered = 0;
  49. for (const subscriber of delta.subscribers) {
  50. const target = targets.get(
  51. buildKey(
  52. sessionKey,
  53. delta.serverName,
  54. subscriber.scopeId,
  55. subscriber.generation,
  56. subscriber.instance,
  57. ),
  58. );
  59. if (!target) continue;
  60. delivered += 1;
  61. if (delta.op === "pluginUi.instanceClosed") target.notifyInstanceClosed?.();
  62. else if (delta.op === "pluginUi.resourceUpdated") target.notifyResourceUpdated(delta.uri);
  63. else if (delta.op === "pluginUi.appToolCall") {
  64. target.notifyAppToolCall({
  65. activity: delta.activity,
  66. cancelled: delta.cancelled,
  67. callId: delta.callId,
  68. toolName: delta.toolName,
  69. arguments: delta.arguments,
  70. });
  71. } else target.notifyResourceListChanged();
  72. }
  73. return delivered;
  74. }
  75. /** 给 controller 的登记入口:实例身份(会话 / 插件 / 作用域)固定,代际由 controller 在端口到达时给出。 */
  76. export function createPluginUiResourceNotificationRegistrar(
  77. scope: PluginUiHostScope,
  78. pluginId: string,
  79. ): NonNullable<PluginUiHostControllerDeps["resourceNotifications"]> {
  80. const sessionKey = buildPluginUiSessionKey(scope);
  81. return {
  82. register: (generation, target, instance) =>
  83. registerPluginUiResourceTarget(
  84. sessionKey,
  85. pluginId,
  86. instance.token,
  87. generation,
  88. target,
  89. instance,
  90. ),
  91. };
  92. }
  93. export function resetPluginUiResourceTargetsForTest(): void {
  94. targets.clear();
  95. }