-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeep-research.agent.ts
More file actions
170 lines (146 loc) · 5.28 KB
/
deep-research.agent.ts
File metadata and controls
170 lines (146 loc) · 5.28 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
import { hatchet } from "@/hatchet.client";
import { search } from "@/agents/deep-research/tasks/search";
import { planSearch } from "@/agents/deep-research/tasks/plan-search";
import { websiteToMd } from "@/utils/ai/website-to-md";
import { summarize } from "@/agents/deep-research/tasks/summarize";
import { judgeResults } from "@/agents/deep-research/tasks/judge-results";
import { extractFacts } from "@/agents/deep-research/tasks/extract-facts";
import { judgeFacts } from "@/agents/deep-research/tasks/judge-facts";
type Message = {
message: string;
};
type Source = {
url: string;
title?: string;
index: number;
};
type Fact = {
text: string;
sourceIndex: number;
};
export const deepResearchAgent = hatchet.task({
name: "deep-research-agent",
executionTimeout: "15m",
fn: async (input: Message, ctx) => {
ctx.log(`Starting deep research agent with query: ${input.message}`);
let iteration = 0;
const maxIterations = 3;
const allFacts: Fact[] = [];
const allSources: Source[] = [];
let missingAspects: string[] = [];
while (!ctx.cancelled && iteration < maxIterations) {
iteration++;
ctx.log(`Starting iteration ${iteration}/${maxIterations}`);
// Plan the search based on the query, existing facts, and missing aspects
ctx.log(
`Planning search with ${allFacts.length} existing facts and ${missingAspects.length} missing aspects`
);
const plan = await ctx.runChild(planSearch, {
query: input.message,
existingFacts: allFacts.map((f) => f.text),
missingAspects: missingAspects,
});
ctx.log(
`Search plan for iteration ${iteration}: ${plan.reasoning}. Queries:`
);
for (const query of plan.queries) {
ctx.log(`${query}`);
}
ctx.log(`Executing ${plan.queries.length} search queries`);
const results = await ctx.bulkRunChildren(
plan.queries.map((query: string) => ({
workflow: search,
input: { query },
}))
);
// Flatten and deduplicate sources
const newSources = results.flatMap((result) => result.sources);
const uniqueSources = new Map(
newSources.map((source, index) => [source.url, { ...source, index }])
);
ctx.log(
`Found ${newSources.length} new sources, ${uniqueSources.size} unique sources`
);
// Add new sources to all sources
allSources.push(...Array.from(uniqueSources.values()));
// Convert sources to markdown
ctx.log(`Converting ${uniqueSources.size} sources to markdown`);
const mdResults = await ctx.bulkRunChildren(
Array.from(uniqueSources.values())
.sort((a, b) => a.index - b.index)
.map((source) => ({
workflow: websiteToMd,
input: {
url: source.url,
index: source.index,
title: source.title,
},
}))
);
// Extract facts from each source
ctx.log("Extracting facts from markdown content");
const factsResults = await ctx.bulkRunChildren(
mdResults.map((result) => ({
workflow: extractFacts,
input: {
source: result.markdown,
query: input.message,
sourceInfo: {
url: result.url,
title: result.title,
index: result.index,
},
},
}))
);
// Add new facts to all facts
const newFacts = factsResults.flatMap((result) => result.facts);
allFacts.push(...newFacts);
ctx.log(
`Extracted ${newFacts.length} new facts, total facts: ${allFacts.length}`
);
// Judge if we have enough facts
ctx.log("Judging if we have enough facts");
const factsJudgment = await ctx.runChild(judgeFacts, {
query: input.message,
facts: allFacts.map((f) => f.text),
});
// Update missing aspects for next iteration
missingAspects = factsJudgment.missingAspects;
ctx.log(`Missing aspects: ${missingAspects.join(", ")}`);
// If we have enough facts or reached max iterations, generate final summary
if (factsJudgment.hasEnoughFacts || iteration >= maxIterations) {
ctx.log(
`Generating final summary (hasEnoughFacts: ${
factsJudgment.hasEnoughFacts
}, reachedMaxIterations: ${iteration >= maxIterations})`
);
const summarizeResult = await ctx.runChild(summarize, {
text: input.message,
facts: allFacts,
sources: allSources,
});
ctx.log("Judging final results");
const judgeResult = await ctx.runChild(judgeResults, {
query: input.message,
result: summarizeResult.summary,
});
ctx.log(
`Deep research complete (isComplete: ${judgeResult.isComplete}, totalFacts: ${allFacts.length}, totalSources: ${allSources.length}, iterations: ${iteration})`
);
return {
result: {
isComplete: judgeResult.isComplete,
reason: judgeResult.reason,
sources: allSources,
summary: summarizeResult.summary,
facts: allFacts,
iterations: iteration,
factsJudgment: factsJudgment,
searchPlans: plan.reasoning,
},
};
}
}
},
});