Skip to content

Commit 8d6f475

Browse files
committed
Add trimPrompt utility
1 parent ab8e01f commit 8d6f475

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

src/services/deep-research/DeepResearchService.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import { ExtensionMessage } from "../../shared/ExtensionMessage"
99
import { generateSerpQueries, processSerpResult } from "./utils/serp"
1010
import { ClineProvider } from "../../core/webview/ClineProvider"
1111

12+
export type ResearchStep = {
13+
query: string
14+
breadth: number
15+
depth: number
16+
learnings?: string[]
17+
visitedUrls?: string[]
18+
onProgress?: (progress: ResearchProgress) => void
19+
}
20+
1221
export type ResearchProgress = {
1322
currentDepth: number
1423
totalDepth: number
@@ -56,7 +65,7 @@ export class DeepResearchService {
5665
private systemPrompt() {
5766
const now = new Date().toISOString()
5867

59-
return `
68+
return this.trimPrompt(`
6069
You are an expert researcher. Today is ${now}. Follow these instructions when responding:
6170
- You may be asked to research subjects that is after your knowledge cutoff, assume the user is right when presented with news.
6271
- The user is a highly experienced analyst, no need to simplify it, be as detailed as possible and make sure your response is correct.
@@ -69,20 +78,14 @@ export class DeepResearchService {
6978
- Value good arguments over authorities, the source is irrelevant.
7079
- Consider new technologies and contrarian ideas, not just the conventional wisdom.
7180
- You may use high levels of speculation or prediction, just flag it for me.
72-
`
73-
.split("\n")
74-
.map((line) => line.trim())
75-
.join("\n")
81+
`)
7682
}
7783

7884
private feedbackPrompt({ query, count }: { query: string; count: number }) {
79-
return `
85+
return this.trimPrompt(`
8086
Given the following query from the user, ask some follow up questions to clarify the research direction.
8187
Return a maximum of ${count} questions, but feel free to return less if the original query is clear: <query>${query}</query>
82-
`
83-
.split("\n")
84-
.map((line) => line.trim())
85-
.join("\n")
88+
`)
8689
}
8790

8891
private async withLoading<T>(operation: () => Promise<T>): Promise<T> {
@@ -129,14 +132,11 @@ export class DeepResearchService {
129132
if (text) {
130133
await this.postMessage({ type: "research.question", text })
131134
} else {
132-
this.combinedQuery = `
135+
this.combinedQuery = this.trimPrompt(`
133136
Initial Query: ${this.initialQuery}
134137
Follow-up Questions and Answers:
135138
${this.questions.map((q: string, i: number) => `Q: ${q}\nA: ${this.answers[i]}`).join("\n")}
136-
`
137-
.split("\n")
138-
.map((line) => line.trim())
139-
.join("\n")
139+
`)
140140

141141
await this.withLoading(() =>
142142
this.deepResearch({
@@ -145,9 +145,7 @@ export class DeepResearchService {
145145
depth: this.depth,
146146
learnings: [],
147147
visitedUrls: [],
148-
onProgress: (progress) => {
149-
console.log("progress", progress)
150-
},
148+
onProgress: (progress) => console.log("progress", progress),
151149
}),
152150
)
153151
}
@@ -160,14 +158,7 @@ export class DeepResearchService {
160158
learnings = [],
161159
visitedUrls = [],
162160
onProgress,
163-
}: {
164-
query: string
165-
breadth: number
166-
depth: number
167-
learnings?: string[]
168-
visitedUrls?: string[]
169-
onProgress?: (progress: ResearchProgress) => void
170-
}): Promise<ResearchResult> {
161+
}: ResearchStep): Promise<ResearchResult> {
171162
this.status = "research"
172163

173164
const progress: ResearchProgress = {
@@ -209,9 +200,8 @@ export class DeepResearchService {
209200
scrapeOptions: { formats: ["markdown"] },
210201
})
211202

212-
// Collect URLs from this search.
213203
const newUrls = result.data
214-
.map((item) => item.url)
204+
.map(({ url }) => url)
215205
.filter((url): url is string => url !== undefined)
216206

217207
const newBreadth = Math.ceil(breadth / 2)
@@ -238,10 +228,10 @@ export class DeepResearchService {
238228
currentQuery: serpQuery.query,
239229
})
240230

241-
const nextQuery = `
231+
const nextQuery = this.trimPrompt(`
242232
Previous research goal: ${serpQuery.researchGoal}
243233
Follow-up research directions: ${newLearnings.followUpQuestions.map((q) => `\n${q}`).join("")}
244-
`.trim()
234+
`)
245235

246236
return this.deepResearch({
247237
query: nextQuery,
@@ -340,4 +330,11 @@ export class DeepResearchService {
340330
private postMessage(message: ExtensionMessage) {
341331
this.providerRef.deref()?.postMessageToWebview(message)
342332
}
333+
334+
private trimPrompt(prompt: string): string {
335+
return prompt
336+
.split("\n")
337+
.map((line) => line.trim())
338+
.join("\n")
339+
}
343340
}

0 commit comments

Comments
 (0)