@fangyb/ahchat-bridge 0.1.38 → 0.1.40

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/dist/cli.cjs CHANGED
@@ -117376,6 +117376,29 @@ function extractTodosFromInput(input) {
117376
117376
  function isObjectRecord(value) {
117377
117377
  return value != null && typeof value === "object" && !Array.isArray(value);
117378
117378
  }
117379
+ function readString(value) {
117380
+ return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
117381
+ }
117382
+ function findToolNameByUseId(contentBlocks, toolUseId) {
117383
+ if (!toolUseId) return null;
117384
+ for (let i = contentBlocks.length - 1; i >= 0; i--) {
117385
+ const block = contentBlocks[i];
117386
+ if (block.type === "tool_use" && block.toolUseId === toolUseId) {
117387
+ return block.toolName;
117388
+ }
117389
+ }
117390
+ return null;
117391
+ }
117392
+ function findToolUseBlockById(contentBlocks, toolUseId) {
117393
+ if (!toolUseId) return null;
117394
+ for (let i = contentBlocks.length - 1; i >= 0; i--) {
117395
+ const block = contentBlocks[i];
117396
+ if (block.type === "tool_use" && block.toolUseId === toolUseId) {
117397
+ return block;
117398
+ }
117399
+ }
117400
+ return null;
117401
+ }
117379
117402
  function readTrimmedString(input, key) {
117380
117403
  const value = input[key];
117381
117404
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
@@ -117648,24 +117671,29 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117648
117671
  } else if (block.type === "tool_use") {
117649
117672
  proc.currentBlockType = "tool_use";
117650
117673
  proc.currentToolName = block.name ?? "unknown";
117674
+ proc.currentToolUseId = readString(block.id);
117651
117675
  proc.activeToolUseStartedAt = Date.now();
117652
117676
  proc.accumulatedToolInput = "";
117653
117677
  const toolName = block.name ?? "unknown";
117678
+ const toolUseId = proc.currentToolUseId;
117654
117679
  proc.suppressCurrentToolUse = proc.officialMediaGenerationSatisfied === true && isOfficialMediaGenerationToolName(toolName);
117655
117680
  const isMcpTool = parseMcpRuntimeToolName(toolName) != null;
117656
117681
  proc.currentMcpInvocationId = isMcpTool ? createMcpToolInvocationId() : null;
117657
117682
  proc.currentMcpInvocationStartedAt = isMcpTool ? (/* @__PURE__ */ new Date()).toISOString() : null;
117683
+ proc.currentMcpInvocationToolUseId = isMcpTool ? toolUseId ?? null : null;
117658
117684
  if (shouldStreamInternals(proc) && !proc.suppressCurrentToolUse && toolName !== "ExitPlanMode" && !isAskUserQuestionToolName(toolName)) {
117659
117685
  emit({
117660
117686
  type: "agent:tool_use",
117661
117687
  payload: {
117662
117688
  ...wireBase(base),
117689
+ ...toolUseId ? { toolUseId } : {},
117663
117690
  toolName,
117664
117691
  input: {}
117665
117692
  }
117666
117693
  });
117667
117694
  proc.contentBlocks.push({
117668
117695
  type: "tool_use",
117696
+ ...toolUseId ? { toolUseId } : {},
117669
117697
  toolName,
117670
117698
  input: {},
117671
117699
  status: "running"
@@ -117696,6 +117724,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117696
117724
  type: "agent:tool_input_update",
117697
117725
  payload: {
117698
117726
  ...wireBase(base),
117727
+ ...proc.currentToolUseId ? { toolUseId: proc.currentToolUseId } : {},
117699
117728
  toolName: proc.currentToolName,
117700
117729
  input: liveInput
117701
117730
  }
@@ -117755,7 +117784,9 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117755
117784
  }
117756
117785
  }
117757
117786
  if (!proc.suppressCurrentToolUse) {
117758
- const lastToolUse = [...proc.contentBlocks].reverse().find((bl) => bl.type === "tool_use");
117787
+ const lastToolUse = [...proc.contentBlocks].reverse().find(
117788
+ (bl) => bl.type === "tool_use" && (proc.currentToolUseId ? bl.toolUseId === proc.currentToolUseId : true)
117789
+ );
117759
117790
  if (lastToolUse && lastToolUse.type === "tool_use") {
117760
117791
  lastToolUse.input = parsedInput;
117761
117792
  }
@@ -117764,6 +117795,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117764
117795
  type: "agent:tool_input_update",
117765
117796
  payload: {
117766
117797
  ...wireBase(base),
117798
+ ...proc.currentToolUseId ? { toolUseId: proc.currentToolUseId } : {},
117767
117799
  toolName: proc.currentToolName,
117768
117800
  input: parsedInput
117769
117801
  }
@@ -117835,6 +117867,14 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117835
117867
  }
117836
117868
  if (proc.currentToolName && !proc.suppressCurrentToolUse && proc.currentMcpInvocationId && proc.currentMcpInvocationStartedAt && parseMcpRuntimeToolName(proc.currentToolName)) {
117837
117869
  try {
117870
+ logger12.info("MCP audit start paired with tool_use", {
117871
+ agentId: proc.agentId,
117872
+ replyMessageId: base.replyMessageId,
117873
+ traceId: base.traceId,
117874
+ toolUseId: proc.currentMcpInvocationToolUseId ?? null,
117875
+ runtimeToolName: proc.currentToolName,
117876
+ mcpInvocationId: proc.currentMcpInvocationId
117877
+ });
117838
117878
  proc.mcpAuditRecorder?.recordStart({
117839
117879
  id: proc.currentMcpInvocationId,
117840
117880
  runtimeToolName: proc.currentToolName,
@@ -117858,6 +117898,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117858
117898
  }
117859
117899
  proc.suppressCurrentToolUse = false;
117860
117900
  proc.currentBlockType = null;
117901
+ proc.currentToolUseId = null;
117861
117902
  break;
117862
117903
  }
117863
117904
  default:
@@ -117900,7 +117941,8 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117900
117941
  for (const block of content) {
117901
117942
  const b = block;
117902
117943
  if (b.type === "tool_result") {
117903
- const toolName = proc.currentToolName ?? "unknown";
117944
+ const toolUseId = readString(b.tool_use_id);
117945
+ const toolName = findToolNameByUseId(proc.contentBlocks, toolUseId) ?? proc.currentToolName ?? "unknown";
117904
117946
  const isError = toolName === "ExitPlanMode" ? false : !!b.is_error;
117905
117947
  const output = typeof b.content === "string" ? b.content : JSON.stringify(b.content);
117906
117948
  const duplicateOfficialMediaDeny = isError && isOfficialMediaGenerationToolName(toolName) && isOfficialMediaGenerationDuplicateDenyMessage(output);
@@ -117913,8 +117955,10 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117913
117955
  });
117914
117956
  proc.currentMcpInvocationId = null;
117915
117957
  proc.currentMcpInvocationStartedAt = null;
117958
+ proc.currentMcpInvocationToolUseId = null;
117916
117959
  proc.activeToolUseStartedAt = void 0;
117917
117960
  proc.currentToolName = null;
117961
+ proc.currentToolUseId = null;
117918
117962
  continue;
117919
117963
  }
117920
117964
  if (isSuccessfulOfficialMediaOutput(toolName, output)) {
@@ -117923,10 +117967,21 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117923
117967
  if (isAskUserQuestionToolName(toolName)) {
117924
117968
  proc.activeToolUseStartedAt = void 0;
117925
117969
  proc.currentToolName = null;
117970
+ proc.currentToolUseId = null;
117926
117971
  continue;
117927
117972
  }
117928
117973
  if (proc.currentMcpInvocationId && parseMcpRuntimeToolName(toolName)) {
117929
117974
  try {
117975
+ logger12.info("MCP audit result paired with tool_result", {
117976
+ agentId: proc.agentId,
117977
+ replyMessageId: base.replyMessageId,
117978
+ traceId: base.traceId,
117979
+ toolUseId,
117980
+ startedToolUseId: proc.currentMcpInvocationToolUseId ?? null,
117981
+ runtimeToolName: toolName,
117982
+ mcpInvocationId: proc.currentMcpInvocationId,
117983
+ isError
117984
+ });
117930
117985
  proc.mcpAuditRecorder?.recordResult({
117931
117986
  id: proc.currentMcpInvocationId,
117932
117987
  runtimeToolName: toolName,
@@ -117948,12 +118003,14 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117948
118003
  }
117949
118004
  proc.currentMcpInvocationId = null;
117950
118005
  proc.currentMcpInvocationStartedAt = null;
118006
+ proc.currentMcpInvocationToolUseId = null;
117951
118007
  }
117952
118008
  if (shouldStreamInternals(proc)) {
117953
118009
  emit({
117954
118010
  type: "agent:tool_result",
117955
118011
  payload: {
117956
118012
  ...wireBase(base),
118013
+ ...toolUseId ? { toolUseId } : {},
117957
118014
  toolName,
117958
118015
  output,
117959
118016
  isError
@@ -117961,11 +118018,12 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117961
118018
  });
117962
118019
  proc.contentBlocks.push({
117963
118020
  type: "tool_result",
118021
+ ...toolUseId ? { toolUseId } : {},
117964
118022
  toolName,
117965
118023
  output,
117966
118024
  isError
117967
118025
  });
117968
- const lastToolUse = [...proc.contentBlocks].reverse().find((bl) => bl.type === "tool_use");
118026
+ const lastToolUse = findToolUseBlockById(proc.contentBlocks, toolUseId) ?? [...proc.contentBlocks].reverse().find((bl) => bl.type === "tool_use");
117969
118027
  if (lastToolUse && lastToolUse.type === "tool_use") {
117970
118028
  lastToolUse.status = isError ? "error" : "done";
117971
118029
  if (lastToolUse.toolName === "ExitPlanMode") {
@@ -117974,6 +118032,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
117974
118032
  }
117975
118033
  }
117976
118034
  proc.activeToolUseStartedAt = void 0;
118035
+ proc.currentToolUseId = null;
117977
118036
  }
117978
118037
  }
117979
118038
  }
@@ -118355,8 +118414,10 @@ function resetAccumulators(proc) {
118355
118414
  proc.contentBlocks = [];
118356
118415
  proc.currentBlockType = null;
118357
118416
  proc.currentToolName = null;
118417
+ proc.currentToolUseId = null;
118358
118418
  proc.currentMcpInvocationId = null;
118359
118419
  proc.currentMcpInvocationStartedAt = null;
118420
+ proc.currentMcpInvocationToolUseId = null;
118360
118421
  proc.activeToolUseStartedAt = void 0;
118361
118422
  proc.segmentBuffer = "";
118362
118423
  proc.segmentCount = 0;
package/dist/index.js CHANGED
@@ -28601,6 +28601,29 @@ function extractTodosFromInput(input) {
28601
28601
  function isObjectRecord(value) {
28602
28602
  return value != null && typeof value === "object" && !Array.isArray(value);
28603
28603
  }
28604
+ function readString(value) {
28605
+ return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
28606
+ }
28607
+ function findToolNameByUseId(contentBlocks, toolUseId) {
28608
+ if (!toolUseId) return null;
28609
+ for (let i = contentBlocks.length - 1; i >= 0; i--) {
28610
+ const block = contentBlocks[i];
28611
+ if (block.type === "tool_use" && block.toolUseId === toolUseId) {
28612
+ return block.toolName;
28613
+ }
28614
+ }
28615
+ return null;
28616
+ }
28617
+ function findToolUseBlockById(contentBlocks, toolUseId) {
28618
+ if (!toolUseId) return null;
28619
+ for (let i = contentBlocks.length - 1; i >= 0; i--) {
28620
+ const block = contentBlocks[i];
28621
+ if (block.type === "tool_use" && block.toolUseId === toolUseId) {
28622
+ return block;
28623
+ }
28624
+ }
28625
+ return null;
28626
+ }
28604
28627
  function readTrimmedString(input, key) {
28605
28628
  const value = input[key];
28606
28629
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
@@ -28873,24 +28896,29 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
28873
28896
  } else if (block.type === "tool_use") {
28874
28897
  proc.currentBlockType = "tool_use";
28875
28898
  proc.currentToolName = block.name ?? "unknown";
28899
+ proc.currentToolUseId = readString(block.id);
28876
28900
  proc.activeToolUseStartedAt = Date.now();
28877
28901
  proc.accumulatedToolInput = "";
28878
28902
  const toolName = block.name ?? "unknown";
28903
+ const toolUseId = proc.currentToolUseId;
28879
28904
  proc.suppressCurrentToolUse = proc.officialMediaGenerationSatisfied === true && isOfficialMediaGenerationToolName(toolName);
28880
28905
  const isMcpTool = parseMcpRuntimeToolName(toolName) != null;
28881
28906
  proc.currentMcpInvocationId = isMcpTool ? createMcpToolInvocationId() : null;
28882
28907
  proc.currentMcpInvocationStartedAt = isMcpTool ? (/* @__PURE__ */ new Date()).toISOString() : null;
28908
+ proc.currentMcpInvocationToolUseId = isMcpTool ? toolUseId ?? null : null;
28883
28909
  if (shouldStreamInternals(proc) && !proc.suppressCurrentToolUse && toolName !== "ExitPlanMode" && !isAskUserQuestionToolName(toolName)) {
28884
28910
  emit({
28885
28911
  type: "agent:tool_use",
28886
28912
  payload: {
28887
28913
  ...wireBase(base),
28914
+ ...toolUseId ? { toolUseId } : {},
28888
28915
  toolName,
28889
28916
  input: {}
28890
28917
  }
28891
28918
  });
28892
28919
  proc.contentBlocks.push({
28893
28920
  type: "tool_use",
28921
+ ...toolUseId ? { toolUseId } : {},
28894
28922
  toolName,
28895
28923
  input: {},
28896
28924
  status: "running"
@@ -28921,6 +28949,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
28921
28949
  type: "agent:tool_input_update",
28922
28950
  payload: {
28923
28951
  ...wireBase(base),
28952
+ ...proc.currentToolUseId ? { toolUseId: proc.currentToolUseId } : {},
28924
28953
  toolName: proc.currentToolName,
28925
28954
  input: liveInput
28926
28955
  }
@@ -28980,7 +29009,9 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
28980
29009
  }
28981
29010
  }
28982
29011
  if (!proc.suppressCurrentToolUse) {
28983
- const lastToolUse = [...proc.contentBlocks].reverse().find((bl) => bl.type === "tool_use");
29012
+ const lastToolUse = [...proc.contentBlocks].reverse().find(
29013
+ (bl) => bl.type === "tool_use" && (proc.currentToolUseId ? bl.toolUseId === proc.currentToolUseId : true)
29014
+ );
28984
29015
  if (lastToolUse && lastToolUse.type === "tool_use") {
28985
29016
  lastToolUse.input = parsedInput;
28986
29017
  }
@@ -28989,6 +29020,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
28989
29020
  type: "agent:tool_input_update",
28990
29021
  payload: {
28991
29022
  ...wireBase(base),
29023
+ ...proc.currentToolUseId ? { toolUseId: proc.currentToolUseId } : {},
28992
29024
  toolName: proc.currentToolName,
28993
29025
  input: parsedInput
28994
29026
  }
@@ -29060,6 +29092,14 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29060
29092
  }
29061
29093
  if (proc.currentToolName && !proc.suppressCurrentToolUse && proc.currentMcpInvocationId && proc.currentMcpInvocationStartedAt && parseMcpRuntimeToolName(proc.currentToolName)) {
29062
29094
  try {
29095
+ logger11.info("MCP audit start paired with tool_use", {
29096
+ agentId: proc.agentId,
29097
+ replyMessageId: base.replyMessageId,
29098
+ traceId: base.traceId,
29099
+ toolUseId: proc.currentMcpInvocationToolUseId ?? null,
29100
+ runtimeToolName: proc.currentToolName,
29101
+ mcpInvocationId: proc.currentMcpInvocationId
29102
+ });
29063
29103
  proc.mcpAuditRecorder?.recordStart({
29064
29104
  id: proc.currentMcpInvocationId,
29065
29105
  runtimeToolName: proc.currentToolName,
@@ -29083,6 +29123,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29083
29123
  }
29084
29124
  proc.suppressCurrentToolUse = false;
29085
29125
  proc.currentBlockType = null;
29126
+ proc.currentToolUseId = null;
29086
29127
  break;
29087
29128
  }
29088
29129
  default:
@@ -29125,7 +29166,8 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29125
29166
  for (const block of content) {
29126
29167
  const b = block;
29127
29168
  if (b.type === "tool_result") {
29128
- const toolName = proc.currentToolName ?? "unknown";
29169
+ const toolUseId = readString(b.tool_use_id);
29170
+ const toolName = findToolNameByUseId(proc.contentBlocks, toolUseId) ?? proc.currentToolName ?? "unknown";
29129
29171
  const isError = toolName === "ExitPlanMode" ? false : !!b.is_error;
29130
29172
  const output = typeof b.content === "string" ? b.content : JSON.stringify(b.content);
29131
29173
  const duplicateOfficialMediaDeny = isError && isOfficialMediaGenerationToolName(toolName) && isOfficialMediaGenerationDuplicateDenyMessage(output);
@@ -29138,8 +29180,10 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29138
29180
  });
29139
29181
  proc.currentMcpInvocationId = null;
29140
29182
  proc.currentMcpInvocationStartedAt = null;
29183
+ proc.currentMcpInvocationToolUseId = null;
29141
29184
  proc.activeToolUseStartedAt = void 0;
29142
29185
  proc.currentToolName = null;
29186
+ proc.currentToolUseId = null;
29143
29187
  continue;
29144
29188
  }
29145
29189
  if (isSuccessfulOfficialMediaOutput(toolName, output)) {
@@ -29148,10 +29192,21 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29148
29192
  if (isAskUserQuestionToolName(toolName)) {
29149
29193
  proc.activeToolUseStartedAt = void 0;
29150
29194
  proc.currentToolName = null;
29195
+ proc.currentToolUseId = null;
29151
29196
  continue;
29152
29197
  }
29153
29198
  if (proc.currentMcpInvocationId && parseMcpRuntimeToolName(toolName)) {
29154
29199
  try {
29200
+ logger11.info("MCP audit result paired with tool_result", {
29201
+ agentId: proc.agentId,
29202
+ replyMessageId: base.replyMessageId,
29203
+ traceId: base.traceId,
29204
+ toolUseId,
29205
+ startedToolUseId: proc.currentMcpInvocationToolUseId ?? null,
29206
+ runtimeToolName: toolName,
29207
+ mcpInvocationId: proc.currentMcpInvocationId,
29208
+ isError
29209
+ });
29155
29210
  proc.mcpAuditRecorder?.recordResult({
29156
29211
  id: proc.currentMcpInvocationId,
29157
29212
  runtimeToolName: toolName,
@@ -29173,12 +29228,14 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29173
29228
  }
29174
29229
  proc.currentMcpInvocationId = null;
29175
29230
  proc.currentMcpInvocationStartedAt = null;
29231
+ proc.currentMcpInvocationToolUseId = null;
29176
29232
  }
29177
29233
  if (shouldStreamInternals(proc)) {
29178
29234
  emit({
29179
29235
  type: "agent:tool_result",
29180
29236
  payload: {
29181
29237
  ...wireBase(base),
29238
+ ...toolUseId ? { toolUseId } : {},
29182
29239
  toolName,
29183
29240
  output,
29184
29241
  isError
@@ -29186,11 +29243,12 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29186
29243
  });
29187
29244
  proc.contentBlocks.push({
29188
29245
  type: "tool_result",
29246
+ ...toolUseId ? { toolUseId } : {},
29189
29247
  toolName,
29190
29248
  output,
29191
29249
  isError
29192
29250
  });
29193
- const lastToolUse = [...proc.contentBlocks].reverse().find((bl) => bl.type === "tool_use");
29251
+ const lastToolUse = findToolUseBlockById(proc.contentBlocks, toolUseId) ?? [...proc.contentBlocks].reverse().find((bl) => bl.type === "tool_use");
29194
29252
  if (lastToolUse && lastToolUse.type === "tool_use") {
29195
29253
  lastToolUse.status = isError ? "error" : "done";
29196
29254
  if (lastToolUse.toolName === "ExitPlanMode") {
@@ -29199,6 +29257,7 @@ function mapSDKMessage(proc, message, rawEmit, sessionStore, onCompleted, onPost
29199
29257
  }
29200
29258
  }
29201
29259
  proc.activeToolUseStartedAt = void 0;
29260
+ proc.currentToolUseId = null;
29202
29261
  }
29203
29262
  }
29204
29263
  }
@@ -29580,8 +29639,10 @@ function resetAccumulators(proc) {
29580
29639
  proc.contentBlocks = [];
29581
29640
  proc.currentBlockType = null;
29582
29641
  proc.currentToolName = null;
29642
+ proc.currentToolUseId = null;
29583
29643
  proc.currentMcpInvocationId = null;
29584
29644
  proc.currentMcpInvocationStartedAt = null;
29645
+ proc.currentMcpInvocationToolUseId = null;
29585
29646
  proc.activeToolUseStartedAt = void 0;
29586
29647
  proc.segmentBuffer = "";
29587
29648
  proc.segmentCount = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fangyb/ahchat-bridge",
3
- "version": "0.1.38",
3
+ "version": "0.1.40",
4
4
  "files": [
5
5
  "dist"
6
6
  ],