Repository navigation
Expand file tree
/
Copy pathevaluateResultTool.ts
More file actions
326 lines (305 loc) · 12.6 KB
/
Copy pathevaluateResultTool.ts
File metadata and controls
326 lines (305 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import type { MastraModelOutput } from '@mastra/core/stream'
import type { ChunkType } from '@mastra/core/stream'
import type { InferUITool } from '@mastra/core/tools'
import { createTool } from '@mastra/core/tools'
import { SpanType, getOrCreateSpan } from '@mastra/core/observability'
import { z } from 'zod'
import { evaluationAgent } from '../agents/evaluationAgent'
import { log } from '../config/logger'
const evaluateResultOutputSchema = z.object({
isRelevant: z.boolean(),
reason: z.string(),
})
type EvaluateResultOutput = z.infer<typeof evaluateResultOutputSchema>
const evaluateResultFallbackOutput: EvaluateResultOutput = {
isRelevant: false,
reason: '',
}
export const evaluateResultTool = createTool({
id: 'evaluate-result',
description:
'Evaluate if a search result is relevant to the research query',
inputSchema: z.object({
query: z.string().describe('The original research query'),
result: z
.object({
title: z.string(),
url: z.string(),
content: z.string(),
})
.describe('The search result to evaluate'),
existingUrls: z
.array(z.string())
.describe('URLs that have already been processed')
.optional(),
}),
outputSchema: evaluateResultOutputSchema,
strict: true,
onInputStart: ({ toolCallId }) => {
log.info('Evaluate result tool input streaming started', {
toolCallId,
hook: 'onInputStart',
})
},
onInputDelta: ({ inputTextDelta, toolCallId }) => {
log.info('Evaluate result received input chunk', {
toolCallId,
inputTextDelta,
hook: 'onInputDelta',
})
},
onInputAvailable: ({ input, toolCallId }) => {
log.info('Evaluate result received complete input', {
toolCallId,
query: input.query,
url: input.result.url,
existingUrlsCount: input.existingUrls?.length ?? 0,
hook: 'onInputAvailable',
})
},
execute: async (inputData, context) => {
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'in-progress',
message:
'🤔 Evaluating relevance of result: ' +
inputData.result.title,
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
const evalSpan = getOrCreateSpan({
type: SpanType.AGENT_RUN,
name: 'evaluate_result',
input: inputData,
requestContext: context?.requestContext,
tracingContext: context?.tracingContext,
metadata: {
'tool.id': 'evaluate-result',
query: inputData.query,
url: inputData.result.url,
operation: 'evaluate_result',
},
})
try {
const { query, result, existingUrls = [] } = inputData
log.info('Evaluating result', { inputData })
// Check if URL already exists (only if existingUrls was provided)
if (existingUrls?.includes(result.url)) {
evalSpan?.update({
output: {
isRelevant: false,
reason: 'URL already processed',
},
metadata: { 'tool.output.status': 'already-processed' },
})
evalSpan?.end()
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'done',
message: 'URL already processed',
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
return {
isRelevant: false,
reason: 'URL already processed',
}
}
// Use Mastra agent directly (follow nested-agent-stream-tool pattern)
const agent =
context?.mastra?.getAgent?.('evaluationAgent') ??
evaluationAgent
let responseObject: unknown = {}
if (typeof agent.stream === 'function') {
try {
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'in-progress',
message: '🔁 Streaming evaluation from agent',
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
let streamedText = ''
const stream = (await agent.stream(
`Evaluate whether this search result is relevant and will help answer the query: "${query}".\n\nSearch result:\nTitle: ${result.title}\nURL: ${result.url}\nContent snippet: ${result.content.substring(0, 500)}...\n\nRespond with a JSON object containing:\n- isRelevant: boolean indicating if the result is relevant\n- reason: brief explanation of your decision`,
{
structuredOutput: {
schema: evaluateResultOutputSchema,
},
onChunk: (chunk: ChunkType<EvaluateResultOutput>) => {
if (
chunk.type === 'text-delta' &&
typeof chunk.payload?.text === 'string'
) {
streamedText += chunk.payload.text
}
},
}
)) as MastraModelOutput<EvaluateResultOutput> | undefined
if (stream?.textStream && context?.writer) {
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'in-progress',
message:
'🔁 Streaming text from evaluation agent',
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
await stream.textStream.pipeTo(
context.writer as unknown as WritableStream
)
} else if (stream?.fullStream && context?.writer) {
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'in-progress',
message:
'🔁 Streaming from evaluation agent (full stream)',
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
await stream.fullStream.pipeTo(
context.writer as unknown as WritableStream
)
}
try {
responseObject = await stream?.object
} catch {
const text = (await stream?.text) ?? streamedText
try {
responseObject = text ? JSON.parse(text) : {}
} catch {
responseObject = {}
}
}
} catch (err) {
const errorMessage =
err instanceof Error ? err.message : String(err)
evalSpan?.error({
error:
err instanceof Error
? err
: new Error(errorMessage),
endSpan: true,
})
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'done',
message: `❌ Error streaming evaluation: ${errorMessage}`,
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
return {
isRelevant: false,
reason: 'Error in evaluation',
}
}
} else {
const response = await agent.generate(
`Evaluate whether this search result is relevant and will help answer the query: "${query}".\n\nSearch result:\nTitle: ${result.title}\nURL: ${result.url}\nContent snippet: ${result.content.substring(0, 500)}...\n\nRespond with a JSON object containing:\n- isRelevant: boolean indicating if the result is relevant\n- reason: brief explanation of your decision`
)
responseObject =
response.object ??
(() => {
try {
const parsedJson: unknown = JSON.parse(
response.text
)
return parsedJson
} catch {
return {}
}
})()
}
const parsed = evaluateResultOutputSchema.safeParse(responseObject)
if (!parsed.success) {
log.warn('Evaluation agent returned unexpected shape', {
response: responseObject,
})
const error = 'Invalid response format from evaluation agent'
evalSpan?.error({
error: new Error(error),
endSpan: true,
})
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'done',
message: error,
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
return {
isRelevant: false,
reason: error,
}
}
evalSpan?.end()
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'done',
message: parsed.data.isRelevant
? '✅ Result is relevant'
: '❌ Result is not relevant',
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
return parsed.data
} catch (error) {
log.error('Error evaluating result:', {
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
})
const errorMessage =
error instanceof Error ? error.message : String(error)
evalSpan?.error({
error: error instanceof Error ? error : new Error(errorMessage),
endSpan: true,
})
// Emit final progress event with error message
await context?.writer?.custom({
type: 'data-tool-progress',
data: {
status: 'done',
message: `❌ Error evaluating result: ${errorMessage}`,
stage: 'evaluate-result',
},
id: 'evaluate-result',
})
return {
isRelevant: false,
reason: 'Error in evaluation',
}
}
},
toModelOutput: (output: EvaluateResultOutput) => ({
type: 'text',
value: `${output.isRelevant ? 'Relevant' : 'Not relevant'}: ${output.reason}`,
}),
onOutput: ({ output, toolCallId, toolName }) => {
log.info('Evaluate result completed', {
toolCallId,
toolName,
outputData: {
isRelevant: output.isRelevant,
reason: output.reason,
},
hook: 'onOutput',
})
},
})
export type EvaluateResultUITool = InferUITool<typeof evaluateResultTool>