pi-extensions-tool-display 0.2.2 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-extensions-tool-display",
3
- "version": "0.2.2",
3
+ "version": "1.0.1",
4
4
  "description": "Pi tool display host and shared result-rendering protocol for extensions",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
@@ -66,10 +66,14 @@ export function registerResultRenderMiddleware(
66
66
  const api = getApi();
67
67
  if (typeof api?.registerResultRenderMiddleware === "function") {
68
68
  api.registerResultRenderMiddleware(registration);
69
- } else {
70
- queueRegistration(registration);
71
69
  }
72
70
 
71
+ // Keep the registration in the shared protocol queue even when a host is
72
+ // already available. The host can be replaced during /reload or when a new
73
+ // session creates a new ExtensionAPI instance; the new host must be able to
74
+ // rehydrate registrations that were attached to the previous API object.
75
+ queueRegistration(registration);
76
+
73
77
  return () => {
74
78
  getApi()?.unregisterResultRenderMiddleware?.(registration.id);
75
79
  removeQueuedRegistration(registration.id);
@@ -1525,8 +1525,11 @@ function drainPendingResultRenderMiddlewares(api: ToolDisplayApi): void {
1525
1525
  const pending = globalWithApi[TOOL_DISPLAY_PENDING_RESULT_MIDDLEWARES_KEY];
1526
1526
  if (!Array.isArray(pending) || pending.length === 0) return;
1527
1527
 
1528
- const entries = pending.splice(0);
1529
- for (const entry of entries) {
1528
+ // This queue is the cross-host registry for external middleware, not a
1529
+ // one-shot bootstrap queue. Keep it populated so a replacement host during
1530
+ // /reload or a new session can rehydrate middleware registered on the old
1531
+ // API instance.
1532
+ for (const entry of pending) {
1530
1533
  if (!entry?.id || !entry.toolName || typeof entry.middleware !== "function") continue;
1531
1534
  api.registerResultRenderMiddleware(entry);
1532
1535
  }
@@ -53,19 +53,25 @@ export function registerToolResultRenderMiddleware(toolName, middleware, options
53
53
  }
54
54
 
55
55
  const id = options.id || `external-result-middleware-${++nextResultMiddlewareId}`;
56
+ const registration = { id, toolName, middleware };
56
57
  const api = getToolDisplayApi();
57
58
  if (typeof api?.registerResultRenderMiddleware === "function") {
58
- return api.registerResultRenderMiddleware({ id, toolName, middleware });
59
+ const registeredId = api.registerResultRenderMiddleware(registration);
60
+ queueResultRenderMiddleware(registration);
61
+ return registeredId;
59
62
  }
60
63
 
64
+ queueResultRenderMiddleware(registration);
65
+ return id;
66
+ }
67
+
68
+ function queueResultRenderMiddleware(registration) {
61
69
  const existing = globalThis[TOOL_DISPLAY_PENDING_RESULT_MIDDLEWARES_KEY];
62
70
  const queue = Array.isArray(existing) ? existing : [];
63
- const registration = { id, toolName, middleware };
64
- const index = queue.findIndex((entry) => entry?.id === id);
71
+ const index = queue.findIndex((entry) => entry?.id === registration.id);
65
72
  if (index >= 0) queue[index] = registration;
66
73
  else queue.push(registration);
67
74
  globalThis[TOOL_DISPLAY_PENDING_RESULT_MIDDLEWARES_KEY] = queue;
68
- return id;
69
75
  }
70
76
 
71
77
  export function unregisterToolResultRenderMiddleware(id) {