@mastra/datadog 1.2.1-alpha.1 → 1.2.1-alpha.3

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/index.js CHANGED
@@ -98,7 +98,7 @@ function toDate(value) {
98
98
  }
99
99
  function safeStringify(data) {
100
100
  try {
101
- return JSON.stringify(data);
101
+ return JSON.stringify(data) ?? "";
102
102
  } catch {
103
103
  if (typeof data === "object" && data !== null) {
104
104
  return `[Non-serializable ${data.constructor?.name || "Object"}]`;
@@ -107,15 +107,50 @@ function safeStringify(data) {
107
107
  }
108
108
  }
109
109
  function isMessageArray(data) {
110
- return Array.isArray(data) && data.every((m) => m?.role && m?.content !== void 0);
110
+ return Array.isArray(data) && data.every((m) => m?.role && (m?.content !== void 0 || Array.isArray(m.toolCalls)));
111
+ }
112
+ function isModelDataSpan(spanType) {
113
+ return spanType === SpanType.MODEL_GENERATION || spanType === SpanType.MODEL_STEP || spanType === SpanType.MODEL_INFERENCE;
111
114
  }
112
115
  function isGeminiContentArray(data) {
113
116
  return Array.isArray(data) && data.every((m) => m?.role && Array.isArray(m?.parts));
114
117
  }
118
+ function toMessageContent(content) {
119
+ return typeof content === "string" ? content : safeStringify(content);
120
+ }
115
121
  function toDatadogMessages(messages) {
116
- return messages.map((m) => ({
117
- role: m.role,
118
- content: typeof m.content === "string" ? m.content : safeStringify(m.content)
122
+ return messages.map((m) => {
123
+ const message = {
124
+ role: m.role,
125
+ content: m.content == null ? "" : toMessageContent(m.content)
126
+ };
127
+ if (m.toolCalls) {
128
+ message.toolCalls = m.toolCalls;
129
+ }
130
+ return message;
131
+ }).filter((m) => !(m.role === "user" && m.content.trim().length === 0));
132
+ }
133
+ function parseToolArguments(args) {
134
+ if (args == null) return void 0;
135
+ if (typeof args === "string") {
136
+ try {
137
+ const parsed = JSON.parse(args);
138
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
139
+ return parsed;
140
+ }
141
+ } catch {
142
+ }
143
+ return { value: args };
144
+ }
145
+ if (typeof args === "object" && !Array.isArray(args)) return args;
146
+ return { value: args };
147
+ }
148
+ function toDatadogToolCalls(toolCalls) {
149
+ return toolCalls.map((c) => ({
150
+ name: c?.toolName ?? c?.name ?? c?.function?.name ?? "unknown",
151
+ arguments: parseToolArguments(c?.args ?? c?.input ?? c?.function?.arguments),
152
+ toolId: c?.toolCallId ?? c?.toolId ?? c?.id,
153
+ type: c?.type === "function" ? c.type : "function"
119
154
  }));
120
155
  }
121
156
  function geminiContentToMessage(item) {
@@ -129,34 +164,34 @@ function geminiContentToMessage(item) {
129
164
  return { role: item.role, content: text };
130
165
  }
131
166
  function formatInput(input, spanType) {
132
- if (spanType === SpanType.MODEL_GENERATION || spanType === SpanType.MODEL_STEP) {
167
+ if (isModelDataSpan(spanType)) {
133
168
  if (isMessageArray(input)) {
134
169
  return toDatadogMessages(input);
135
170
  }
136
171
  if (isGeminiContentArray(input)) {
137
- return input.map(geminiContentToMessage);
172
+ return toDatadogMessages(input.map(geminiContentToMessage));
138
173
  }
139
174
  if (input && typeof input === "object" && !Array.isArray(input)) {
140
175
  if (isMessageArray(input.messages)) {
141
176
  return toDatadogMessages(input.messages);
142
177
  }
143
178
  if (isGeminiContentArray(input.messages)) {
144
- return input.messages.map(geminiContentToMessage);
179
+ return toDatadogMessages(input.messages.map(geminiContentToMessage));
145
180
  }
146
181
  if (isGeminiContentArray(input.contents)) {
147
- return input.contents.map(geminiContentToMessage);
182
+ return toDatadogMessages(input.contents.map(geminiContentToMessage));
148
183
  }
149
184
  }
150
185
  if (typeof input === "string") {
151
- return [{ role: "user", content: input }];
186
+ return toDatadogMessages([{ role: "user", content: input }]);
152
187
  }
153
- return [{ role: "user", content: safeStringify(input) }];
188
+ return toDatadogMessages([{ role: "user", content: safeStringify(input) }]);
154
189
  }
155
190
  if (typeof input === "string" || Array.isArray(input)) return input;
156
191
  return safeStringify(input);
157
192
  }
158
193
  function formatOutput(output, spanType) {
159
- if (spanType === SpanType.MODEL_GENERATION || spanType === SpanType.MODEL_STEP) {
194
+ if (isModelDataSpan(spanType)) {
160
195
  if (isMessageArray(output)) {
161
196
  return toDatadogMessages(output);
162
197
  }
@@ -164,16 +199,21 @@ function formatOutput(output, spanType) {
164
199
  return [{ role: "assistant", content: output }];
165
200
  }
166
201
  if (output && typeof output === "object") {
202
+ if (Array.isArray(output.toolCalls) && output.toolCalls.length > 0) {
203
+ return [
204
+ {
205
+ role: "assistant",
206
+ content: typeof output.text === "string" ? output.text : "",
207
+ toolCalls: toDatadogToolCalls(output.toolCalls)
208
+ }
209
+ ];
210
+ }
167
211
  if (typeof output.text === "string" && output.text.length > 0) {
168
212
  return [{ role: "assistant", content: output.text }];
169
213
  }
170
214
  if (output.object !== void 0) {
171
215
  return [{ role: "assistant", content: safeStringify(output.object) }];
172
216
  }
173
- if (Array.isArray(output.toolCalls) && output.toolCalls.length > 0) {
174
- const summary = output.toolCalls.map((c) => `[tool: ${c?.toolName ?? c?.name ?? "unknown"}]`).join("");
175
- return [{ role: "assistant", content: summary }];
176
- }
177
217
  }
178
218
  return [{ role: "assistant", content: safeStringify(output) }];
179
219
  }