pi-antigravity-bridge 0.1.11 → 0.1.12

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.1.12] - 2026-11-22
4
+
5
+ ### Fixed
6
+
7
+ - 修复 `pi-tools` 模式下通过宿主工具(如 `read`、`context_recall` 等)读取大于 10 KB 的图片时,因 `agy` 内置 MCP 媒体大小上限导致模型看不到画面内容的问题;大尺寸工具图片现会在当前热会话的工具边界经多模态输入完整送达模型,并保留多工具并发执行与会话缓存。
8
+
3
9
  ## [0.1.11] - 2026-11-22
4
10
 
5
11
  ### Changed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-antigravity-bridge",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "Google Antigravity CLI (agy acp) agent backend bridge for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -3,9 +3,11 @@ import {
3
3
  type Api,
4
4
  type AssistantMessage,
5
5
  type AssistantMessageEventStream,
6
+ type ImageContent,
6
7
  type Message,
7
8
  type Model,
8
9
  type SimpleStreamOptions,
10
+ type TextContent,
9
11
  type Tool,
10
12
  type ToolResultMessage,
11
13
  type TranscriptContext,
@@ -138,11 +140,61 @@ class AsyncEventQueue<T> {
138
140
  return this.items[0];
139
141
  }
140
142
 
143
+ some(predicate: (item: T) => boolean): boolean {
144
+ return this.items.some(predicate);
145
+ }
146
+
141
147
  drainAll(): T[] {
142
148
  return this.items.splice(0, this.items.length);
143
149
  }
144
150
  }
145
151
 
152
+ /**
153
+ * `agy acp` 内部将 `McpToolConfig.MaxOutputBytes`(默认 10,240 字节)复用于 MCP 媒体内联上限,
154
+ * 超过该阈值的宿主 MCP 返回图片会被 `agy` 丢弃内联像素仅留 offload 文件路径。
155
+ * 留出安全余量:解码后大于 9,600 字节的 MCP 返回图片在工具边界自动切入同一热会话的 `session/prompt` 多模态块投递。
156
+ */
157
+ const AGY_MCP_INLINE_IMAGE_SAFE_BYTES = 9_600;
158
+
159
+ interface DeferredMcpImagePayload {
160
+ toolCallId: string;
161
+ toolName: string;
162
+ isError: boolean;
163
+ images: ImageContent[];
164
+ }
165
+
166
+ function getDecodedBase64Bytes(data: string): number {
167
+ return Buffer.byteLength(data, "base64");
168
+ }
169
+
170
+ function buildDeferredMcpImagePromptBlocks(
171
+ payloads: readonly DeferredMcpImagePayload[],
172
+ includeContinueNote: boolean,
173
+ ): AcpPromptBlock[] {
174
+ const blocks: AcpPromptBlock[] = [];
175
+ for (const item of payloads) {
176
+ const status = item.isError ? "error" : "ok";
177
+ blocks.push({
178
+ type: "text",
179
+ text: `<tool_result id="${item.toolCallId}" tool="${item.toolName}" status="${status}">\n[Attached ${item.images.length} image(s) from tool "${item.toolName}" delivered via multimodal prompt]\n</tool_result>`,
180
+ });
181
+ for (const img of item.images) {
182
+ blocks.push({
183
+ type: "image",
184
+ data: img.data,
185
+ mimeType: img.mimeType,
186
+ });
187
+ }
188
+ }
189
+ if (includeContinueNote && blocks.length > 0) {
190
+ blocks.push({
191
+ type: "text",
192
+ text: "Please inspect the attached tool result image(s) above and continue.",
193
+ });
194
+ }
195
+ return blocks;
196
+ }
197
+
146
198
  interface ActiveTurnPump {
147
199
  readonly queue: AsyncEventQueue<PumpQueueItem>;
148
200
  finished: boolean;
@@ -159,6 +211,8 @@ interface ActiveTurnPump {
159
211
  pendingAnnouncedMcpCount: number;
160
212
  /** 已向 Pi 推理流发出的宿主 MCP `callId` 集合,防止误结算仍在队列中待消费的后续工具请求 */
161
213
  readonly emittedMcpCallIds: Set<string>;
214
+ /** 本轮宿主 MCP 工具返回中因超过 `agy` 10KB 媒体内联上限而延迟到工具边界多模态 `session/prompt` 投递的图片 */
215
+ readonly deferredMcpImages: DeferredMcpImagePayload[];
162
216
  }
163
217
 
164
218
  interface PendingGenSlicePool {
@@ -1536,45 +1590,109 @@ export class TurnCoordinator {
1536
1590
  const entriesToSettle = Array.from(ledger.parkedCalls.entries()).filter(
1537
1591
  ([callId]) => pump.emittedMcpCallIds.has(callId) || recentToolResults.has(callId),
1538
1592
  );
1593
+ const settlingIdSet = new Set(entriesToSettle.map(([callId]) => callId));
1594
+ const hasRemainingParkedCalls =
1595
+ Array.from(ledger.parkedCalls.keys()).some((id) => !settlingIdSet.has(id)) ||
1596
+ pump.queue.some((item) => item.kind === "mcp_parked" && !settlingIdSet.has(item.call.callId));
1597
+
1598
+ // 检测本次结算的宿主 MCP 工具结果是否包含超过 agy 10KB 内联上限的图片,若是则将大图延迟至同会话 session/prompt 投递
1599
+ const settlePayloads: Array<{
1600
+ callId: string;
1601
+ parked: ParkedToolCall;
1602
+ content: Array<TextContent | ImageContent>;
1603
+ isError: boolean;
1604
+ }> = [];
1605
+ let hasDeferredImagesInSettledBatch = false;
1606
+ for (const [callId, parked] of entriesToSettle) {
1607
+ const tr = recentToolResults.get(callId);
1608
+ if (!tr) {
1609
+ settlePayloads.push({
1610
+ callId,
1611
+ parked,
1612
+ content: [{ type: "text", text: "(tool call skipped)" }],
1613
+ isError: true,
1614
+ });
1615
+ continue;
1616
+ }
1617
+ const mcpContent: Array<TextContent | ImageContent> = [];
1618
+ const oversizedImages: ImageContent[] = [];
1619
+ for (const block of tr.content) {
1620
+ if (
1621
+ block.type === "image" &&
1622
+ getDecodedBase64Bytes(block.data) > AGY_MCP_INLINE_IMAGE_SAFE_BYTES
1623
+ ) {
1624
+ oversizedImages.push(block);
1625
+ } else {
1626
+ mcpContent.push(block);
1627
+ }
1628
+ }
1629
+ if (oversizedImages.length > 0) {
1630
+ hasDeferredImagesInSettledBatch = true;
1631
+ pump.deferredMcpImages.push({
1632
+ toolCallId: tr.toolCallId,
1633
+ toolName: tr.toolName,
1634
+ isError: tr.isError,
1635
+ images: oversizedImages,
1636
+ });
1637
+ mcpContent.push({
1638
+ type: "text",
1639
+ text: `[${oversizedImages.length} image(s) (>10KB) delivered via follow-up multimodal prompt block.]`,
1640
+ });
1641
+ }
1642
+ settlePayloads.push({
1643
+ callId,
1644
+ parked,
1645
+ content: mcpContent,
1646
+ isError: tr.isError,
1647
+ });
1648
+ }
1539
1649
 
1540
- // 若即将执行工具边界切入(用户 steer 或无 SSE 下的动态工具重载),先开启切入保护标志,
1541
- // 防止 settle 后毫秒级冲入的同批第 2 个 MCP 工具被挂起拖慢收口。
1650
+ // 若即将执行工具边界切入(用户 steer、无 SSE 下的动态工具重载、或同批工具已全数结算且存在待多模态投递的大图),
1651
+ // 先开启切入保护标志,防止 settle 后毫秒级冲入的同批后续 MCP 工具被挂起拖慢收口。
1542
1652
  const willCutInForSteer = trailingUsers.length > 0;
1543
1653
  const willCutInForToolsReload =
1544
1654
  !willCutInForSteer &&
1545
1655
  needsMidTurnToolsReload &&
1546
1656
  Boolean(ledger.conn.capabilities?.sessionCapabilities?.resume) &&
1547
1657
  hasLocalConversationDb(this.currentConfig, ledger.agySessionId);
1658
+ const willCutInForDeferredImages =
1659
+ !willCutInForSteer &&
1660
+ !willCutInForToolsReload &&
1661
+ !hasRemainingParkedCalls &&
1662
+ pump.deferredMcpImages.length > 0;
1548
1663
 
1549
- if (willCutInForSteer || willCutInForToolsReload) {
1664
+ if (willCutInForSteer || willCutInForToolsReload || willCutInForDeferredImages) {
1550
1665
  ledger.steeringCutInPending = true;
1551
1666
  }
1552
1667
 
1553
1668
  const settledMcpIds: string[] = [];
1554
- for (const [callId, parked] of entriesToSettle) {
1555
- const tr = recentToolResults.get(callId);
1556
- ledger.parkedCalls.delete(callId);
1557
- pump.emittedMcpCallIds.delete(callId);
1558
- settledMcpIds.push(callId);
1559
-
1560
- parked.settle({
1561
- content: tr ? [...tr.content] : [{ type: "text", text: "(tool call skipped)" }],
1562
- isError: tr ? tr.isError : true,
1669
+ for (const item of settlePayloads) {
1670
+ ledger.parkedCalls.delete(item.callId);
1671
+ pump.emittedMcpCallIds.delete(item.callId);
1672
+ settledMcpIds.push(item.callId);
1673
+
1674
+ item.parked.settle({
1675
+ content: item.content,
1676
+ isError: item.isError,
1563
1677
  });
1564
1678
  }
1565
1679
 
1566
1680
  if (options?.onResponse && settledMcpIds.length > 0) {
1567
1681
  const currentProjection = inspectTrafficOptProjectionState(visibleMessages);
1682
+ const responseHeaders: Record<string, string> = {
1683
+ "x-acp-session-id": ledger.agySessionId,
1684
+ "x-acp-transport": "acp-mcp-result",
1685
+ "x-acp-settled-mcp-ids": settledMcpIds.join(","),
1686
+ "x-acp-projection-synced":
1687
+ currentProjection.projectionDigest === ledger.syncedProjectionDigest ? "1" : "0",
1688
+ };
1689
+ if (hasDeferredImagesInSettledBatch) {
1690
+ responseHeaders["x-acp-mcp-images-deferred"] = String(AGY_MCP_INLINE_IMAGE_SAFE_BYTES);
1691
+ }
1568
1692
  await options.onResponse(
1569
1693
  {
1570
1694
  status: 200,
1571
- headers: {
1572
- "x-acp-session-id": ledger.agySessionId,
1573
- "x-acp-transport": "acp-mcp-result",
1574
- "x-acp-settled-mcp-ids": settledMcpIds.join(","),
1575
- "x-acp-projection-synced":
1576
- currentProjection.projectionDigest === ledger.syncedProjectionDigest ? "1" : "0",
1577
- },
1695
+ headers: responseHeaders,
1578
1696
  },
1579
1697
  model,
1580
1698
  );
@@ -1583,6 +1701,10 @@ export class TurnCoordinator {
1583
1701
  // 若用户在工具执行期间按 Enter 发送了转向消息(steer):
1584
1702
  // 先等待刚结算的工具输出落盘至本地会话库,随即在当前工具边界收口后台自动循环,立即在同一热缓存会话上以真实用户身份发送转向消息!
1585
1703
  if (willCutInForSteer) {
1704
+ const deferredImgBlocks = buildDeferredMcpImagePromptBlocks(
1705
+ pump.deferredMcpImages.splice(0),
1706
+ false,
1707
+ );
1586
1708
  try {
1587
1709
  await waitForSqliteToolStepsSettled(
1588
1710
  this.currentConfig,
@@ -1644,7 +1766,7 @@ export class TurnCoordinator {
1644
1766
  this.httpMcpBridge.clearSessionCancelled(ledger.agySessionId);
1645
1767
  await this.executePromptOnLedger(
1646
1768
  ledger,
1647
- convertUserMessagesToPromptBlocks(trailingUsers),
1769
+ [...deferredImgBlocks, ...convertUserMessagesToPromptBlocks(trailingUsers)],
1648
1770
  visibleMessages,
1649
1771
  writer,
1650
1772
  model,
@@ -1656,6 +1778,10 @@ export class TurnCoordinator {
1656
1778
  // 默认 processPerSession: true(无 SSE 订阅)下工具集动态变化(如 tool_search 解锁新工具):
1657
1779
  // 在当前工具落盘后通过原地 close + session/resume 重拉 tools/list,使模型当轮立即可见并调用新解锁工具!
1658
1780
  if (willCutInForToolsReload) {
1781
+ const deferredImgBlocks = buildDeferredMcpImagePromptBlocks(
1782
+ pump.deferredMcpImages.splice(0),
1783
+ false,
1784
+ );
1659
1785
  try {
1660
1786
  await waitForSqliteToolStepsSettled(
1661
1787
  this.currentConfig,
@@ -1680,6 +1806,7 @@ export class TurnCoordinator {
1680
1806
  await this.executePromptOnLedger(
1681
1807
  ledger,
1682
1808
  [
1809
+ ...deferredImgBlocks,
1683
1810
  {
1684
1811
  type: "text",
1685
1812
  text: "The newly activated tools are now loaded and available. Please continue.",
@@ -1726,6 +1853,75 @@ export class TurnCoordinator {
1726
1853
  return;
1727
1854
  }
1728
1855
 
1856
+ // 宿主 MCP 工具返回 > 9.6KB 图片:在当前工具落盘后收口旧后台循环,在同一热缓存会话上以多模态 session/prompt 投递完整图片,
1857
+ // 彻底规避 agy McpToolConfig.MaxOutputBytes (10KB) 导致的图片丢弃盲区,同时保持 100% 热前缀缓存!
1858
+ if (willCutInForDeferredImages) {
1859
+ const deferredImgBlocks = buildDeferredMcpImagePromptBlocks(
1860
+ pump.deferredMcpImages.splice(0),
1861
+ true,
1862
+ );
1863
+ try {
1864
+ await waitForSqliteToolStepsSettled(
1865
+ this.currentConfig,
1866
+ ledger.agySessionId,
1867
+ 160,
1868
+ options?.signal,
1869
+ );
1870
+ } finally {
1871
+ ledger.steeringCutInPending = false;
1872
+ }
1873
+ this.abortLedgerTurnNonBlocking(ledger);
1874
+ const idleOk = await this.waitForInFlightPrompt(ledger, 2_500, options?.signal);
1875
+ if (!idleOk) {
1876
+ if (options?.signal?.aborted) {
1877
+ writer.fail("aborted", "Request aborted by user");
1878
+ return;
1879
+ }
1880
+ await this.discardWedgedLedger(ledger);
1881
+ const cwd = this.activeContext?.cwd ?? process.cwd();
1882
+ const isProjectTrusted = this.activeContext?.isProjectTrusted?.() ?? false;
1883
+ const mountInfo = prepareCustomizationMount(fullContextMessages, this.currentConfig, cwd);
1884
+ const restored = await this.bootstrapOrRestoreLedger({
1885
+ piSessionKey: ledger.piSessionKey,
1886
+ cwd,
1887
+ isProjectTrusted,
1888
+ visibleMessages,
1889
+ hostMcpTools,
1890
+ toolsDigest,
1891
+ mountedPluginPaths: mountInfo.mountedPluginPaths,
1892
+ mountDigest: mountInfo.mountDigest,
1893
+ additionalDirectories: mountInfo.additionalDirectories,
1894
+ workspaceContextBlock: mountInfo.workspaceContextBlock,
1895
+ modelContextWindow: model.contextWindow,
1896
+ signal: options?.signal,
1897
+ });
1898
+ await this.executePromptOnLedger(
1899
+ restored.ledger,
1900
+ restored.initialPromptBlocks,
1901
+ visibleMessages,
1902
+ writer,
1903
+ model,
1904
+ options,
1905
+ );
1906
+ return;
1907
+ }
1908
+
1909
+ ledger.lastConsumedStepIdx = Math.max(
1910
+ ledger.lastConsumedStepIdx,
1911
+ await readCurrentMaxSqliteStepIdx(this.currentConfig, ledger.agySessionId),
1912
+ );
1913
+ this.httpMcpBridge.clearSessionCancelled(ledger.agySessionId);
1914
+ await this.executePromptOnLedger(
1915
+ ledger,
1916
+ deferredImgBlocks,
1917
+ visibleMessages,
1918
+ writer,
1919
+ model,
1920
+ options,
1921
+ );
1922
+ return;
1923
+ }
1924
+
1729
1925
  writer.setResponseMetadata({
1730
1926
  responseId: ledger.agySessionId,
1731
1927
  providerThinkingLevel: ledger.appliedThoughtLevel,
@@ -2373,6 +2569,7 @@ export class TurnCoordinator {
2373
2569
  hasSqliteGenTelemetry: false,
2374
2570
  pendingAnnouncedMcpCount: 0,
2375
2571
  emittedMcpCallIds: new Set<string>(),
2572
+ deferredMcpImages: [],
2376
2573
  };
2377
2574
  ledger.activePump = pump;
2378
2575
  this.startTrackedSessionPrompt(ledger, pump, {