linkshell-cli 0.2.86 → 0.2.87

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.
@@ -169,11 +169,55 @@ function toolInputFromItem(item: Record<string, unknown>): string | undefined {
169
169
  }
170
170
  if (itemType === "fileChange") {
171
171
  const changes = Array.isArray(item.changes) ? item.changes : [];
172
- return summarizeFileChanges(changes);
172
+ return summarizeFileChanges(changes) ?? firstString(item, ["path", "file", "filePath", "absolutePath", "relativePath"]);
173
173
  }
174
174
  return stringifyDefined(item.arguments ?? item.input ?? item.toolInput);
175
175
  }
176
176
 
177
+ function looksLikeDiff(text: string): boolean {
178
+ const value = text.trim();
179
+ return (
180
+ value.startsWith("diff --git ") ||
181
+ value.startsWith("@@ ") ||
182
+ value.includes("\n@@ ") ||
183
+ (value.includes("\n--- ") && value.includes("\n+++ "))
184
+ );
185
+ }
186
+
187
+ function collectDiffStrings(value: unknown, depth = 0): string[] {
188
+ if (depth > 6 || value === undefined || value === null) return [];
189
+ if (typeof value === "string") return looksLikeDiff(value) ? [value] : [];
190
+ if (Array.isArray(value)) return value.flatMap((entry) => collectDiffStrings(entry, depth + 1));
191
+ const raw = asRecord(value);
192
+ if (!raw) return [];
193
+ const direct: string[] = [];
194
+ const nested: string[] = [];
195
+ for (const [key, entry] of Object.entries(raw)) {
196
+ const lowerKey = key.toLowerCase();
197
+ const isDiffField =
198
+ lowerKey.includes("diff") ||
199
+ lowerKey.includes("patch") ||
200
+ lowerKey.includes("unified");
201
+ if (typeof entry === "string" && isDiffField && entry.trim()) {
202
+ direct.push(entry);
203
+ } else if (typeof entry === "object" && entry) {
204
+ nested.push(...collectDiffStrings(entry, depth + 1));
205
+ }
206
+ }
207
+ return [...direct, ...nested].filter((entry) => looksLikeDiff(entry));
208
+ }
209
+
210
+ function extractDiffText(value: unknown): string | undefined {
211
+ const diffs = collectDiffStrings(value)
212
+ .map((entry) => entry.trim())
213
+ .filter(Boolean);
214
+ if (diffs.length === 0) return undefined;
215
+ return diffs
216
+ .filter((entry, index, array) => array.indexOf(entry) === index)
217
+ .join("\n\n")
218
+ .slice(0, 24_000);
219
+ }
220
+
177
221
  function summarizeFileChanges(changes: unknown[]): string | undefined {
178
222
  const lines = changes
179
223
  .map((change) => {
@@ -183,7 +227,7 @@ function summarizeFileChanges(changes: unknown[]): string | undefined {
183
227
  firstString(raw, ["path", "file", "filePath", "absolutePath", "relativePath"]) ??
184
228
  firstString(asRecord(raw.update) ?? {}, ["path", "file", "filePath"]);
185
229
  const kind = firstString(raw, ["kind", "type", "operation", "action"]);
186
- return [kind, path].filter(Boolean).join(" ");
230
+ return [kind, path].filter(Boolean).join(" ") || path;
187
231
  })
188
232
  .filter((line): line is string => Boolean(line));
189
233
  return lines.length > 0 ? lines.slice(0, 8).join("\n") : undefined;
@@ -449,7 +493,6 @@ export class AgentSessionProxy {
449
493
  method.startsWith("mcpServer/startupStatus/") ||
450
494
  method === "thread/status/changed" ||
451
495
  method === "thread/tokenUsage/updated" ||
452
- method === "turn/diff/updated" ||
453
496
  method === "serverRequest/resolved" ||
454
497
  method === "mcpServer/oauthLogin/completed"
455
498
  ) {
@@ -502,6 +545,9 @@ export class AgentSessionProxy {
502
545
  case "item/fileChange/patchUpdated":
503
546
  this.handleFilePatchUpdated(params);
504
547
  return;
548
+ case "turn/diff/updated":
549
+ this.handleTurnDiffUpdated(params);
550
+ return;
505
551
  case "command/exec/outputDelta":
506
552
  this.handleCommandExecDelta(params);
507
553
  return;
@@ -700,7 +746,7 @@ export class AgentSessionProxy {
700
746
  if (!raw) return;
701
747
  const itemId = firstString(raw, ["itemId", "id"]) ?? id("file");
702
748
  const changes = Array.isArray(raw.changes) ? raw.changes : [];
703
- const output = summarizeFileChanges(changes);
749
+ const output = extractDiffText(raw) ?? summarizeFileChanges(changes);
704
750
  const existing = this.toolCalls.get(itemId);
705
751
  const toolCall: AgentToolCall = {
706
752
  id: itemId,
@@ -714,6 +760,26 @@ export class AgentSessionProxy {
714
760
  this.sendUpdate({ kind: "tool_call", toolCall, status: "running" });
715
761
  }
716
762
 
763
+ private handleTurnDiffUpdated(params: unknown): void {
764
+ const raw = asRecord(params);
765
+ if (!raw) return;
766
+ const diff = extractDiffText(raw);
767
+ if (!diff) return;
768
+ const itemId = firstString(raw, ["itemId", "id", "turnId"]) ?? "workspace-diff";
769
+ const changes = Array.isArray(raw.changes) ? raw.changes : [];
770
+ const existing = this.toolCalls.get(itemId);
771
+ const toolCall: AgentToolCall = {
772
+ id: itemId,
773
+ name: existing?.name ?? "文件修改",
774
+ input: existing?.input ?? summarizeFileChanges(changes),
775
+ output: diff,
776
+ createdAt: existing?.createdAt ?? Date.now(),
777
+ status: existing?.status ?? "running",
778
+ };
779
+ this.toolCalls.set(itemId, toolCall);
780
+ this.sendUpdate({ kind: "tool_call", toolCall, status: "running" });
781
+ }
782
+
717
783
  private handleCommandExecDelta(params: unknown): void {
718
784
  const raw = asRecord(params);
719
785
  if (!raw) return;
@@ -766,15 +832,18 @@ export class AgentSessionProxy {
766
832
  const itemType = firstString(item, ["type"]);
767
833
  const name = toolNameFromItem(item);
768
834
  if (!name && !isToolItemType(itemType)) return undefined;
769
- const output =
835
+ const bufferedOutput = this.toolOutputBuffers.get(itemId);
836
+ const rawOutput =
770
837
  firstString(item, ["aggregatedOutput", "output", "stdout", "stderr"]) ??
771
838
  stringifyDefined(item.result ?? item.error ?? item.contentItems);
772
- const bufferedOutput = this.toolOutputBuffers.get(itemId);
839
+ const output = itemType === "fileChange"
840
+ ? extractDiffText(item) ?? bufferedOutput ?? rawOutput
841
+ : rawOutput ?? bufferedOutput;
773
842
  return {
774
843
  id: itemId,
775
844
  name: name ?? "工具",
776
845
  input: toolInputFromItem(item),
777
- output: output ?? bufferedOutput,
846
+ output,
778
847
  createdAt: Date.now(),
779
848
  status: normalizeToolStatus(item.status, fallbackStatus === "completed"),
780
849
  };
@@ -195,12 +195,56 @@ function summarizeFileChanges(changes: unknown[]): string | undefined {
195
195
  firstString(raw, ["path", "file", "filePath", "absolutePath", "relativePath"]) ??
196
196
  firstString(asRecord(raw.update), ["path", "file", "filePath"]);
197
197
  const kind = firstString(raw, ["kind", "type", "operation", "action"]);
198
- return [kind, path].filter(Boolean).join(" ");
198
+ return [kind, path].filter(Boolean).join(" ") || path;
199
199
  })
200
200
  .filter((line): line is string => Boolean(line));
201
201
  return lines.length > 0 ? lines.slice(0, 8).join("\n") : undefined;
202
202
  }
203
203
 
204
+ function looksLikeDiff(text: string): boolean {
205
+ const value = text.trim();
206
+ return (
207
+ value.startsWith("diff --git ") ||
208
+ value.startsWith("@@ ") ||
209
+ value.includes("\n@@ ") ||
210
+ (value.includes("\n--- ") && value.includes("\n+++ "))
211
+ );
212
+ }
213
+
214
+ function collectDiffStrings(value: unknown, depth = 0): string[] {
215
+ if (depth > 6 || value === undefined || value === null) return [];
216
+ if (typeof value === "string") return looksLikeDiff(value) ? [value] : [];
217
+ if (Array.isArray(value)) return value.flatMap((entry) => collectDiffStrings(entry, depth + 1));
218
+ const raw = asRecord(value);
219
+ if (!raw) return [];
220
+ const direct: string[] = [];
221
+ const nested: string[] = [];
222
+ for (const [key, entry] of Object.entries(raw)) {
223
+ const lowerKey = key.toLowerCase();
224
+ const isDiffField =
225
+ lowerKey.includes("diff") ||
226
+ lowerKey.includes("patch") ||
227
+ lowerKey.includes("unified");
228
+ if (typeof entry === "string" && isDiffField && entry.trim()) {
229
+ direct.push(entry);
230
+ } else if (typeof entry === "object" && entry) {
231
+ nested.push(...collectDiffStrings(entry, depth + 1));
232
+ }
233
+ }
234
+ return [...direct, ...nested].filter((entry) => looksLikeDiff(entry));
235
+ }
236
+
237
+ function extractDiffText(value: unknown): string | undefined {
238
+ const diffs = collectDiffStrings(value)
239
+ .map((entry) => entry.trim())
240
+ .filter(Boolean);
241
+ if (diffs.length === 0) return undefined;
242
+ return diffs
243
+ .filter((entry, index, array) => array.indexOf(entry) === index)
244
+ .join("\n\n")
245
+ .slice(0, 24_000);
246
+ }
247
+
204
248
  function toolInputFromItem(item: Record<string, unknown>): string | undefined {
205
249
  const itemType = firstString(item, ["type"]);
206
250
  if (itemType === "commandExecution") {
@@ -211,7 +255,7 @@ function toolInputFromItem(item: Record<string, unknown>): string | undefined {
211
255
  }
212
256
  if (itemType === "fileChange") {
213
257
  const changes = Array.isArray(item.changes) ? item.changes : [];
214
- return summarizeFileChanges(changes);
258
+ return summarizeFileChanges(changes) ?? firstString(item, ["path", "file", "filePath", "absolutePath", "relativePath"]);
215
259
  }
216
260
  return stringifyDefined(item.arguments ?? item.input ?? item.toolInput);
217
261
  }
@@ -645,7 +689,6 @@ export class AgentWorkspaceProxy {
645
689
  method.startsWith("mcpServer/startupStatus/") ||
646
690
  method === "thread/status/changed" ||
647
691
  method === "thread/tokenUsage/updated" ||
648
- method === "turn/diff/updated" ||
649
692
  method === "serverRequest/resolved" ||
650
693
  method === "mcpServer/oauthLogin/completed"
651
694
  ) {
@@ -705,6 +748,9 @@ export class AgentWorkspaceProxy {
705
748
  case "item/fileChange/patchUpdated":
706
749
  this.handleFilePatchUpdated(params);
707
750
  return;
751
+ case "turn/diff/updated":
752
+ this.handleTurnDiffUpdated(params);
753
+ return;
708
754
  case "command/exec/outputDelta":
709
755
  this.handleCommandExecDelta(params);
710
756
  return;
@@ -863,7 +909,9 @@ export class AgentWorkspaceProxy {
863
909
  this.toolConversationIds.get(itemId) ??
864
910
  this.activeConversationId;
865
911
  if (!conversationId) return;
866
- const output = summarizeFileChanges(Array.isArray(raw.changes) ? raw.changes : []);
912
+ const output =
913
+ extractDiffText(raw) ??
914
+ summarizeFileChanges(Array.isArray(raw.changes) ? raw.changes : []);
867
915
  const existing = this.findTool(conversationId, itemId);
868
916
  this.upsertTool(conversationId, {
869
917
  id: itemId,
@@ -875,6 +923,28 @@ export class AgentWorkspaceProxy {
875
923
  });
876
924
  }
877
925
 
926
+ private handleTurnDiffUpdated(params: unknown): void {
927
+ const raw = asRecord(params);
928
+ if (!raw) return;
929
+ const conversationId = this.conversationIdFromParams(raw) ?? this.activeConversationId;
930
+ if (!conversationId) return;
931
+ const diff = extractDiffText(raw);
932
+ if (!diff) return;
933
+ const itemId =
934
+ firstString(raw, ["itemId", "id", "turnId"]) ??
935
+ `workspace-diff:${conversationId}`;
936
+ const existing = this.findTool(conversationId, itemId);
937
+ const changes = Array.isArray(raw.changes) ? raw.changes : [];
938
+ this.upsertTool(conversationId, {
939
+ id: itemId,
940
+ name: existing?.name ?? "文件修改",
941
+ input: existing?.input ?? summarizeFileChanges(changes),
942
+ output: diff,
943
+ createdAt: existing?.createdAt ?? Date.now(),
944
+ status: existing?.status ?? "running",
945
+ });
946
+ }
947
+
878
948
  private handleCommandExecDelta(params: unknown): void {
879
949
  const raw = asRecord(params);
880
950
  if (!raw) return;
@@ -968,14 +1038,18 @@ export class AgentWorkspaceProxy {
968
1038
  const itemType = firstString(item, ["type"]);
969
1039
  const name = toolNameFromItem(item);
970
1040
  if (!name && !isToolItemType(itemType)) return undefined;
971
- const output =
1041
+ const bufferedOutput = this.toolOutputBuffers.get(itemId);
1042
+ const rawOutput =
972
1043
  firstString(item, ["aggregatedOutput", "output", "stdout", "stderr"]) ??
973
1044
  stringifyDefined(item.result ?? item.error ?? item.contentItems);
1045
+ const output = itemType === "fileChange"
1046
+ ? extractDiffText(item) ?? bufferedOutput ?? rawOutput
1047
+ : rawOutput ?? bufferedOutput;
974
1048
  return {
975
1049
  id: itemId,
976
1050
  name: name ?? "工具",
977
1051
  input: toolInputFromItem(item),
978
- output: output ?? this.toolOutputBuffers.get(itemId),
1052
+ output,
979
1053
  createdAt: Date.now(),
980
1054
  status: normalizeToolStatus(item.status, fallbackStatus === "completed"),
981
1055
  };