@ai-sdk/devtools 1.0.8 → 1.0.10

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.
@@ -13,6 +13,7 @@ import type {
13
13
  ParsedUsage,
14
14
  ContentPart,
15
15
  ToolCallContentPart,
16
+ ToolResultContentPart,
16
17
  PromptMessage,
17
18
  ParseJson,
18
19
  } from './types';
@@ -60,6 +61,49 @@ export function truncateText(text: string, maxLength: number = 30): string {
60
61
  return text.slice(0, maxLength).trim() + '…';
61
62
  }
62
63
 
64
+ function getToolResultsFromMessages(
65
+ messages: PromptMessage[] | undefined,
66
+ ): ToolResultContentPart[] {
67
+ return (
68
+ messages
69
+ ?.filter(message => message.role === 'tool')
70
+ .flatMap(message =>
71
+ Array.isArray(message.content)
72
+ ? message.content.filter(
73
+ (part): part is ToolResultContentPart =>
74
+ part.type === 'tool-result',
75
+ )
76
+ : [],
77
+ ) ?? []
78
+ );
79
+ }
80
+
81
+ export function getOutputToolResults(
82
+ output: ParsedOutput | null,
83
+ fallbackToolResults: ContentPart[] = [],
84
+ ): ToolResultContentPart[] {
85
+ const candidates = [
86
+ ...getToolResultsFromMessages(output?.response?.messages),
87
+ // Retain compatibility with captures created by the initial media-preview
88
+ // implementation, which persisted transformed results separately.
89
+ ...(output?.toolResults ?? []),
90
+ ...fallbackToolResults.filter(
91
+ (part): part is ToolResultContentPart => part.type === 'tool-result',
92
+ ),
93
+ ...(output?.content?.filter(
94
+ (part): part is ToolResultContentPart => part.type === 'tool-result',
95
+ ) ?? []),
96
+ ];
97
+ const seenToolCallIds = new Set<string>();
98
+
99
+ return candidates.filter(result => {
100
+ if (result.toolCallId == null) return true;
101
+ if (seenToolCallIds.has(result.toolCallId)) return false;
102
+ seenToolCallIds.add(result.toolCallId);
103
+ return true;
104
+ });
105
+ }
106
+
63
107
  function extractTextFromMessage(message: PromptMessage): string | null {
64
108
  const { content } = message;
65
109
  if (typeof content === 'string') return content;