@zieglar/pi-chat-exchange 0.3.1 → 0.3.2

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.
@@ -24,7 +24,7 @@ import { PING_TOOL_NAME, PingArgsSchema, PingResultSchema, defaultHostLabel, pin
24
24
  import { callMessagingTool, CE_ACK_TOOL, isAckToolName, isMessagingToolName, MESSAGING_TOOLS, } from "./messaging-tools.js";
25
25
  import { callEndpointDiscovery, CE_ENDPOINTS_TOOL, CE_ENDPOINTS_TOOL_NAME } from "./endpoint-discovery.js";
26
26
  export const SERVER_NAME = "pi-chat-exchange";
27
- export const SERVER_VERSION = "0.3.1";
27
+ export const SERVER_VERSION = "0.3.2";
28
28
  const PING_TOOL = {
29
29
  name: PING_TOOL_NAME,
30
30
  description: "Read-only liveness probe for the pi-chat-exchange service. " +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zieglar/pi-chat-exchange",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -26,6 +26,7 @@
26
26
  * seam from packages/pi-chat-exchange/dist.
27
27
  */
28
28
  import { createNativeInbound, isNativeFeedback, type NativeInboundToolContext } from "./native-inbound.js";
29
+ import { createNativeReadProtocol } from "./native-read-protocol.js";
29
30
  import { createOwnershipFeedbackBridge } from "./ownership-feedback.js";
30
31
  import { createCaptureScheduler, createDeliveryGuard, createDesktopTargetBridge, deliverAfterDesktopCapture, type DesktopTargetBridge } from "./desktop-target.js";
31
32
  import { runSessionConfiguration } from "./multi-session-commands.js";
@@ -761,6 +762,7 @@ export default function ceChatExchange(pi: ExtensionApiView): void {
761
762
  failure: CeHostRefusal | undefined;
762
763
  };
763
764
  let generation: Generation | undefined;
765
+ const nativeReadProtocol = createNativeReadProtocol();
764
766
  let nativeInbound: ReturnType<typeof createNativeInbound> | undefined;
765
767
  let inboundEnabled = false;
766
768
  const closeNativeInbound = (): void => {
@@ -1532,7 +1534,8 @@ export default function ceChatExchange(pi: ExtensionApiView): void {
1532
1534
  if (generation === undefined || generation.stopping || generation.failure !== undefined || nativeInbound === undefined) throw Error("INBOUND_DISABLED");
1533
1535
  const messageId = (params as { messageId?: unknown }).messageId;
1534
1536
  if (typeof messageId !== "string") throw Error("INBOUND_INVALID_MESSAGE_ID");
1535
- const task = await nativeInbound.receive(messageId, ctx);
1537
+ const receiver = nativeInbound;
1538
+ const task = await nativeReadProtocol.run(_toolCallId, messageId, ctx, protocol => receiver.receive(messageId, protocol));
1536
1539
  if (ctx.sessionManager.getSessionId() !== generation.piSessionId || endpointId !== generation.endpointId) throw Error("INBOUND_SESSION_CHANGED");
1537
1540
  if (!("text" in task)) {
1538
1541
  // Pi extensions.md: terminate suppresses the automatic follow-up when every tool in the batch agrees.
@@ -1595,7 +1598,7 @@ export default function ceChatExchange(pi: ExtensionApiView): void {
1595
1598
  try {
1596
1599
  const endpointId = guardedEndpoint(ctx);
1597
1600
  const result = withHost("metadata", () => getHost().readMetadata(endpointId, params));
1598
- return {
1601
+ return nativeReadProtocol.capture(_toolCallId, "ce_read", result.messageId, {
1599
1602
  content: [
1600
1603
  {
1601
1604
  type: "text",
@@ -1606,7 +1609,7 @@ export default function ceChatExchange(pi: ExtensionApiView): void {
1606
1609
  },
1607
1610
  ],
1608
1611
  details: { ...result },
1609
- };
1612
+ });
1610
1613
  } catch (error) {
1611
1614
  return refusalResult(error);
1612
1615
  }
@@ -1708,7 +1711,7 @@ export default function ceChatExchange(pi: ExtensionApiView): void {
1708
1711
  }
1709
1712
  // No await between context validation and the synchronous Host call.
1710
1713
  const result = withHost("payload", () => getHost().readPayload(endpointId, { messageId: field.value }));
1711
- return {
1714
+ return nativeReadProtocol.capture(_toolCallId, "ce_read_payload", result.messageId, {
1712
1715
  content: [
1713
1716
  {
1714
1717
  type: "text",
@@ -1735,7 +1738,7 @@ export default function ceChatExchange(pi: ExtensionApiView): void {
1735
1738
  createdAt: result.createdAt,
1736
1739
  auditSequence: result.auditSequence,
1737
1740
  },
1738
- };
1741
+ });
1739
1742
  } catch (error) {
1740
1743
  return refusalResult(error);
1741
1744
  }
@@ -0,0 +1,44 @@
1
+ import type { NativeInboundToolContext } from "./native-inbound.js";
2
+
3
+ type ReadName = "ce_read" | "ce_read_payload";
4
+ type ReadResult = { content: Array<{ type: "text"; text: string }>; details: unknown };
5
+
6
+ /** Exact, invocation-local CE protocol data, independent of model-facing presentation.
7
+ * Registered tools and all SDK call/result hooks still run. Refusals are never restored.
8
+ */
9
+ export function createNativeReadProtocol() {
10
+ const scopes = new Map<string, { messageId: string; results: Map<string, { name: ReadName; result: ReadResult }> }>();
11
+ return {
12
+ capture<T extends ReadResult>(toolCallId: string, name: ReadName, messageId: string, result: T): T {
13
+ // Pi owns nested identities: <parent toolCallId>/<ordinal>. Ordinary model reads
14
+ // have no active native receive scope and must never retain a private payload copy.
15
+ const separator = toolCallId.lastIndexOf("/");
16
+ const scope = scopes.get(toolCallId.slice(0, separator));
17
+ if (separator > 0 && scope?.messageId === messageId && /^[1-9]\d*$/.test(toolCallId.slice(separator + 1))) {
18
+ scope.results.set(toolCallId, { name, result: structuredClone(result) });
19
+ }
20
+ return result;
21
+ },
22
+ async run<T>(parentId: string, messageId: string, ctx: NativeInboundToolContext, receive: (ctx: NativeInboundToolContext) => Promise<T>): Promise<T> {
23
+ if (scopes.has(parentId)) throw Error("INBOUND_PROTOCOL_SCOPE_ACTIVE");
24
+ const scope = { messageId, results: new Map<string, { name: ReadName; result: ReadResult }>() };
25
+ scopes.set(parentId, scope);
26
+ try {
27
+ return await receive({
28
+ executeTool: async (name, args, options) => {
29
+ const outcome = await ctx.executeTool(name, args, options);
30
+ const exact = scope.results.get(outcome.toolCall.id);
31
+ scope.results.delete(outcome.toolCall.id);
32
+ if (outcome.isError || exact?.name !== name) return outcome;
33
+ // Only the native consumer receives the original owned protocol result.
34
+ // ce_receive_task's final response still goes through normal presentation hooks.
35
+ return { ...outcome, result: exact.result };
36
+ },
37
+ });
38
+ } finally {
39
+ scope.results.clear();
40
+ scopes.delete(parentId);
41
+ }
42
+ },
43
+ };
44
+ }