File size: 26,869 Bytes
3647b02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use client';

import { useState, useRef } from 'react';
import PromptBar from '@/components/PromptBar';
import ResearchProgressList, { ResearchEventType } from '@/components/ResearchProgressList';
import ReportViewer from '@/components/ReportViewer';
import ResearchStrategyEditor from '@/components/ResearchStrategyEditor';
import { ApplicationState } from '@/types/ApplicationState';
import { v4 as uuidv4 } from 'uuid';
import config, { getApiEndpoint } from '@/config';

// Define interfaces for API data structures
interface ApiEventData {
  event: {
    type: string;
    description?: string;
    report?: string;
    deltaSearchCount?: number;
    deltaQueryCount?: number;
    hidden?: boolean;
  };
  session_key?: string;
}

interface ApiRequestBody {
  prompt?: string;
  session_key?: string;
  strategy_id?: string;
  strategy_content?: string;
  dry: boolean;
  start_from: 'research' | 'reporting';
}

export default function Home() {
  const [state, setState] = useState<ApplicationState>({
    type: 'idle',
    sessionKey: "",
    researchStartTimestamp: 0,
    researchEndTimestamp: 0,
    searchCount: 0,
    queryCount: 0,
    finalizationStartTimestamp: 0,
    finalizationEndTimestamp: 0,
    events: []
  });
  const [reportContent, setReportContent] = useState<string>('');
  const [editedStrategyId, setEditedStrategyId] = useState('');
  const [strategyContents, setStrategyContents] = useState<Record<string, string>>(initialStrategyContents);

  const abortControllerRef = useRef<AbortController | null>(null);

  // Shared API communication function with proper typing
  const communicateWithAPI = async (
    requestBody: ApiRequestBody,
    initialStateUpdate: (prev: ApplicationState) => ApplicationState,
    eventHandlers: {
      onStarted?: (data: ApiEventData, prev: ApplicationState) => Partial<ApplicationState>,
      onCompleted?: (data: ApiEventData, prev: ApplicationState) => Partial<ApplicationState>,
      onCancelled?: (data: ApiEventData, prev: ApplicationState) => Partial<ApplicationState>,
      onReportBuilding?: (data: ApiEventData, prev: ApplicationState) => Partial<ApplicationState>,
      onReportDone?: (data: ApiEventData, prev: ApplicationState) => Partial<ApplicationState>,
      onOtherEvent?: (data: ApiEventData, prev: ApplicationState) => Partial<ApplicationState>,
      onServerError?: (data: ApiEventData, prev: ApplicationState) => Partial<ApplicationState>,
    }
  ) => {
    // Create new AbortController for this request
    abortControllerRef.current = new AbortController();

    try {
      const response = await fetch(getApiEndpoint(), {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(requestBody),
        signal: abortControllerRef.current.signal
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.detail || 'Failed to communicate with server');
      }

      if (!response.body) {
        throw new Error('No response body received');
      }

      // Apply initial state update
      setState(initialStateUpdate);

      const reader = response.body.getReader();
      const decoder = new TextDecoder();
      let buffer = '';

      while (true) {
        const { done, value } = await reader.read();

        if (done) break;

        buffer += decoder.decode(value, { stream: true });

        const lines = buffer.split('\n');
        buffer = lines.pop() || ''; // Keep the last incomplete line in the buffer

        for (const line of lines) {
          if (line.trim()) {
            try {
              const data = JSON.parse(line) as ApiEventData;

              if (data.event.type === 'started' && eventHandlers.onStarted) {
                setState(prev => ({
                  ...prev,
                  ...eventHandlers.onStarted?.(data, prev)
                }) as ApplicationState);
              } else if (data.event.type === 'completed' && eventHandlers.onCompleted) {
                setState(prev => ({
                  ...prev,
                  ...eventHandlers.onCompleted?.(data, prev)
                }) as ApplicationState);

                if (abortControllerRef.current) {
                  abortControllerRef.current = null;
                }
                break;
              } else if (data.event.type === 'cancelled' && eventHandlers.onCancelled) {
                setState(prev => ({
                  ...prev,
                  ...eventHandlers.onCancelled?.(data, prev)
                }) as ApplicationState);

                if (abortControllerRef.current) {
                  abortControllerRef.current = null;
                }
                break;
              } else if (data.event.type === 'report_building' && eventHandlers.onReportBuilding) {
                setState(prev => ({
                  ...prev,
                  ...eventHandlers.onReportBuilding?.(data, prev)
                }) as ApplicationState);
              } else if (data.event.type === 'report_done' && eventHandlers.onReportDone) {
                // Save the report content if available
                if (data.event.report) {
                  setReportContent(data.event.report);
                }

                setState(prev => ({
                  ...prev,
                  ...eventHandlers.onReportDone?.(data, prev)
                }) as ApplicationState);

                if (abortControllerRef.current) {
                  abortControllerRef.current = null;
                }
                break;
              } else if (data.event.type === 'error' && eventHandlers.onServerError) {
                console.error('Server error:', data);
                setState(prev => ({
                  ...prev,
                  ...eventHandlers.onServerError?.(data, prev)
                }) as ApplicationState);
                break;
              } else if (eventHandlers.onOtherEvent) {
                setState(prev => ({
                  ...prev,
                  ...eventHandlers.onOtherEvent?.(data, prev)
                }) as ApplicationState);
              }
            } catch (e) {
              console.error('Failed to parse event:', e);
            }
          }
        }
      }
    } catch (error) {
      if (error instanceof Error) {
        if (error.name === 'AbortError') {
          console.log('AbortError -- normal interruption');
          return;
        }
        handleError(error.message);
      } else {
        handleError('An unknown error occurred');
      }
    }
  };

  const handleStartResearch = async (query: string, strategyId: string) => {
    if (state.type === 'researching' || state.type === 'finalizing') return;

    await communicateWithAPI(
      {
        prompt: query.trim(),
        strategy_id: strategyId,
        strategy_content: strategyContents[strategyId] || '',
        dry: config.runtime.dryRun,
        start_from: 'research'
      },
      (prev) => ({
        type: 'researching',
        sessionKey: prev.sessionKey,
        researchStartTimestamp: Date.now(),
        researchEndTimestamp: 0,
        searchCount: 0,
        queryCount: 1,
        finalizationStartTimestamp: 0,
        finalizationEndTimestamp: 0,
        events: []
      }),
      {
        onStarted: (data, prev) => ({
          type: 'researching',
          researchStartTimestamp: Date.now(),
          researchEndTimestamp: 0,
          sessionKey: data.session_key || prev.sessionKey || "",
          searchCount: 0,
          queryCount: 0,
          finalizationStartTimestamp: 0,
          finalizationEndTimestamp: 0,
          events: []
        }),
        onCompleted: (data, prev) => ({
          type: 'done',
          sessionKey: data.session_key || prev.sessionKey || "",
          finalizationEndTimestamp: Date.now()
        }),
        onCancelled: (data, prev) => ({
          type: 'stopped',
          sessionKey: data.session_key || prev.sessionKey || "",
          researchEndTimestamp: prev.finalizationStartTimestamp > 0 ? prev.researchEndTimestamp : Date.now(),
          finalizationEndTimestamp: prev.finalizationStartTimestamp > 0 ? Date.now() : 0
        }),
        onReportBuilding: (data, prev) => ({
          type: 'finalizing',
          researchEndTimestamp: prev.researchEndTimestamp > 0 ? prev.researchEndTimestamp : Date.now(),
          finalizationStartTimestamp: Date.now(),
          finalizationEndTimestamp: 0,
          events: data.event.hidden ? prev.events : [
            ...prev.events,
            {
              id: uuidv4(),
              type: (data.event.type || 'search') as ResearchEventType,
              description: data.event.description || 'No description given',
              timestamp: Date.now(),
            },
          ],
          searchCount: prev.searchCount + (data.event.deltaSearchCount || 0),
          queryCount: prev.queryCount + (data.event.deltaQueryCount || 0)
        }),
        onReportDone: (data, prev) => ({
          type: 'done',
          sessionKey: data.session_key || prev.sessionKey || "",
          finalizationEndTimestamp: Date.now()
        }),
        onServerError: (data, prev) => ({
          type: 'error',
          error: data.event.description || 'Unknown error',
          events: data.event.hidden ? prev.events : [
            ...prev.events,
            {
              id: uuidv4(),
              type: 'error',
              description: data.event.description || 'Unknown error',
              timestamp: Date.now(),
            },
          ],
          searchCount: prev.searchCount + (data.event.deltaSearchCount || 0),
          queryCount: prev.queryCount + (data.event.deltaQueryCount || 0)
        }),
        onOtherEvent: (data, prev) => ({
          sessionKey: data.session_key || prev.sessionKey || "",
          events: data.event.hidden ? prev.events : [
            ...prev.events,
            {
              id: uuidv4(),
              type: (data.event.type || 'search') as ResearchEventType,
              description: data.event.description || 'No description given',
              timestamp: Date.now(),
            },
          ],
          searchCount: prev.searchCount + (data.event.deltaSearchCount || 0),
          queryCount: prev.queryCount + (data.event.deltaQueryCount || 0)
        })
      }
    );
  };

  const handleStartFinalizing = async () => {
    if (state.type === 'researching' || state.type === 'finalizing') return;

    await communicateWithAPI(
      {
        session_key: state.sessionKey,
        dry: config.runtime.dryRun,
        start_from: 'reporting'
      },
      (prev) => ({
        ...prev,
        type: 'finalizing',
        sessionKey: prev.sessionKey || "",
        finalizationStartTimestamp: Date.now(),
        finalizationEndTimestamp: 0
      }),
      {
        onCompleted: (data, prev) => ({
          type: 'done',
          sessionKey: data.session_key || prev.sessionKey || "",
          finalizationEndTimestamp: Date.now()
        }),
        onCancelled: (data, prev) => ({
          type: 'stopped',
          sessionKey: data.session_key || prev.sessionKey || "",
          finalizationEndTimestamp: Date.now()
        }),
        onServerError: (data, prev) => ({
          type: 'error',
          error: data.event.description || 'Unknown error',
          events: data.event.hidden ? prev.events : [
            ...prev.events,
            {
              id: uuidv4(),
              type: 'error',
              description: data.event.description || 'Unknown error',
              timestamp: Date.now(),
            },
          ],
        }),
        onOtherEvent: (data, prev) => ({
          events: data.event.hidden || data.event.type === '__final' ? prev.events : [
            ...prev.events,
            {
              id: uuidv4(),
              type: (data.event.type || 'search') as ResearchEventType,
              description: data.event.description || 'No description given',
              timestamp: Date.now(),
              deltaSearchCount: data.event.deltaSearchCount || 0,
              deltaQueryCount: data.event.deltaQueryCount || 0,
            },
          ]
        })
      }
    );
  };

  const handleStop = () => {
    if (state.type !== 'researching' && state.type !== 'finalizing') return;

    // Abort the ongoing request if it exists
    if (abortControllerRef.current) {
      abortControllerRef.current.abort();
      abortControllerRef.current = null;
    }

    setState(prev => ({
      type: 'stopped',
      sessionKey: prev.sessionKey,
      researchStartTimestamp: prev.researchStartTimestamp,
      researchEndTimestamp: prev.researchEndTimestamp > 0 ? prev.researchEndTimestamp : Date.now(),
      searchCount: prev.searchCount,
      queryCount: prev.queryCount,
      finalizationStartTimestamp: prev.finalizationStartTimestamp,
      finalizationEndTimestamp: prev.finalizationStartTimestamp > 0 ? Date.now() : 0,
      events: prev.events,
    }));
  };

  const handleClear = () => {
    setReportContent('');
    setState({
      type: 'idle',
      sessionKey: "",
      researchStartTimestamp: 0,
      researchEndTimestamp: 0,
      searchCount: 0,
      queryCount: 0,
      finalizationStartTimestamp: 0,
      finalizationEndTimestamp: 0,
      events: []
    });
  };

  const handleViewError = () => {
    // TODO: Implement scroll to error functionality
    console.log('Scrolling to error...');
  };

  const handleError = (error: string) => {
    setState(prev => ({
      ...prev,
      type: 'error',
      error: error
    }));
  };

  const handleStartEditingStrategy = (strategyId: string) => {
    setEditedStrategyId(strategyId);
  };

  const handleStrategyAccept = (editedStrategyContent: string) => {
    setStrategyContents(prev => ({
      ...prev,
      [editedStrategyId]: editedStrategyContent
    }));
    setEditedStrategyId('');
    console.log('Strategy updated:', editedStrategyId, editedStrategyContent);
  };

  const handleStrategyRevert = () => {
    setEditedStrategyId('');
    console.log('Strategy reverted:', strategyContents[editedStrategyId]);
  };

  return (
    <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
      <main className="row-start-2 flex flex-col items-center justify-center gap-8 w-full">
        <div className="w-[min(90%,max(200pt,40%))] min-w-[30rem] flex flex-col items-center justify-center gap-8">
          <h1 className="text-4xl font-bold">NVR Universal Deep Research</h1>
          <PromptBar
            onResearch={handleStartResearch}
            onEditStrategy={handleStartEditingStrategy}
            state={state}
            isAStrategyBeingEdited={editedStrategyId !== ''}
          />
          {editedStrategyId !== '' && (
            <ResearchStrategyEditor
              editedStrategyInitialContent={strategyContents[editedStrategyId] || ''}
              onAccept={handleStrategyAccept}
              onRevert={handleStrategyRevert}
              state={state}
            />
          )}
          <ResearchProgressList
            state={state}
            onStop={handleStop}
            onClear={handleClear}
            onFinalize={handleStartFinalizing}
            onViewError={handleViewError}
          />
        </div>
        {state.type === 'done' && reportContent !== '' && (
          <div className="w-[min(90%,70%)]">
            <ReportViewer report={reportContent} isVisible={true} />
          </div>
        )}
      </main>
    </div>
  );
}

const initialStrategyContents = {
  default: ``,
  minimal: `1. Send a notification of type "prompt_received" with description saying what PROMPT has been received, e.g. "Received research request: {PROMPT}"
2. Send a notification of type "prompt_analysis_started", with description indicating that we are now analyzing the research request.
3. Take the PROMPT and ask a language model to produce 3 search phrases that could help with retrieving results from search engine for the purpose of compiling a report the user asks for in the PROMPT. The search phrases should be simple and objective, e.g. "important events 1972" or "energy consumption composition in India today". Use a long prompt for the model that describes in detail what is supposed to be performed and the expected output format. Instruct the model to return the search phrases on one line each. Tell the model not to output any other text -- just the newline-separated phrases. Then, parse the output of the language model line by line and save the resulting search phrases as "phrases" for further research, skipping over empty lines.
4. Send a notification of type "prompt_analysis_completed", with a description saying as much.
4.1 Send a notification of type "task_analysis_completed", informing the user that the search plan has been completed and informing them how many search phrases will be invoked, e.g.  "Search planning completed. Will be searching through {len(topics)}+ terms."
5. For each phrase in phrases output by step 3., perform the following:
    - Send a notification of type "search_started", with the description indicating what search phrase we are using for the search, e.g. "Searching for phrase '{phrase}'"
    - Perform search with the phrase.
    - Once the search returns some results, append their contents to CONTEXT one by one, separating them by double newlines from what is already present in the CONTEXT.
    - Send a notification of type "search_result_processing_completed", indicating in its description that the search results for term {term} have been processed.
6. Send a notification to the user with type "research_completed", indicating that the "Research phase is now completed.".
7. Send a notification with type "report_building", with the description indicating that the report is being built.
8. Take CONTEXT. Call the language model, instructing it to take CONTEXT (to be appended into the LM call) and produce a deep research report on the topic requested in PROMPT. The resulting report should go into detail wherever possible, rely only on the information available in CONTEXT, address the instruction given in the PROMPT, and be formatted in Markdown. This is to be communicated in the prompt. Do not shy away from using long, detailed and descriptive prompts! Tell the model not to output any other text, just the report. The result produced by the language model is to be called REPORT.
9. Send a notification with type "report_done", indicating that the report has been completed. Add "report" as a field containing the REPORT to be an additional payload to the notification.
`,
  expansive: `1. Send a notification of type "prompt_received" with description saying what PROMPT has been received, e.g. "Received research request: {PROMPT}"
2. Send a notification of type "prompt_analysis_started", with description indicating that we are now analyzing the research request.
3. Take the PROMPT and ask a language model to produce 2 topics that could be useful to investigate in order to produce the report requested in the PROMPT. The topics should be simple and sufficiently different from each other, e.g. "important events of 1972" or "energy consumption composition in India today". Instruct the model to return the topics on one line each. Tell the model not to output any other text. Then, parse the output of the language model line by line and save the resulting topics as "topics" for further research.
4. Send a notification of type "prompt_analysis_completed", with description saying as much.
5. Throughout the search and report generation process, we shall rely on a single storage of context. Lets refer to it just as to "context" from now on. Initially, there is no context.
6. For each topic in topics, perform the following
  6.1. Take the PROMPT and the topic, and ask a language model to produce up to 2 search phrases that could be useful to collect information on the particular topic. Each search phrase should be simple and directly relate to the topic e.g., for topic "important events of 1972", the search phrases could be "what happened in 1972", "1972 events worldwide", "important events 1971-1973". For topic "energy consumption composition in India today", the search phrases could be "renewable energy production in India today", "fossil fuel energy reliance India", "energy security India". Call the returned phrases simply "phrases" from now on.
  6.2. For each phrase in phrases output by step 6.1., perform the following:
    - Send a notification of type "search_started", with the description indicating what search phrase we are using for the search, e.g. "Searching for phrase '{phrase}'"
    - Perform search with the phrase. Once the search returns some results, append their contents to context one by one, separating them by double newlines from what is already present in the context.
    - Send a notification of type "search_result_processing_completed", indicating in its description that the search results for term {term} have been processed.
7. Send a notification with type "report_building", with the description indicating that the report is being built.
8. Take CONTEXT. Call the language model, instructing it to take context (to be appended into the LM call) and produce a deep research report on the topic requested in PROMPT. The resulting report should go into detail wherever possible, rely only on the information available in context, address the instruction given in the PROMPT, and be formatted in Markdown. This is to be communicated in the prompt. Do not shy away from using long, detailed and descriptive prompts! Tell the model not to output any other text, just the report. The result produced by the language model is to be called REPORT.
9. Send a notification with type "report_done", indicating that the report has been completed. Add "report" as a field containing the REPORT to be an additional payload to the notification.
`,
  intensive: `1. Send a notification of type "prompt_received" with description saying what PROMPT has been received, e.g. "Received research request: {PROMPT}"
2. Send a notification of type "prompt_analysis_started", with description indicating that we are now analyzing the research request.
3. Throughout the search and report generation process, we shall rely on two storages of context. One shall be called "supercontext" and contain all contexts of all resources read throughout the search phase. The other one shall be called "subcontext" and pertain to only one interation of the search process. At the beginning, both the supercontext and subcontext are empty.
4. Take the PROMPT and ask a language model to produce 2 search phrases that could help with retrieving results from search engine for the purpose of compiling a report the user asks for in the PROMPT. The search phrases should be simple and objective, e.g. "important events 1972" or "energy consumption composition in India today". Use a long prompt for the model that describes in detail what is supposed to be performed and the expected output format. Instruct the model to return the search phrases on one line each. Tell the model not to output any other text -- just the newline-separated phrases. Then, parse the output of the language model line by line and save the resulting search phrases as "phrases" for further research, skipping over empty lines.
4.1. Send a notification of type "prompt_analysis_completed", with a description saying as much.
5. Perform the following 2 times:
 - Clear the subcontext.
 - For each phrase in phrases, perform the following:
    * Send a notification of type "search_started", with the description indicating what search phrase we are using for the search, e.g. "Searching for phrase '{phrase}'"
    * Perform search with the phrase. Once the search returns some results, append their contents to subcontext one by one, separating them by double newlines from what is already present in the subcontext.
    * Send a notification of type "search_result_processing_completed", indicating in its description that the search results for term {term} have been processed.
 - Once the subcontext has been put together by aggregating the contributions due to all search phrases, ask a language model, given the subcontext and the PROMPT given by the user, to come up with 2 more phrases (distinct to phrases that are already in phrases) on the basis of the new subcontext being available. Again, the search phrases should be simple and objective, e.g. "important events 1972" or "energy consumption composition in India today". Use a long prompt for the model that describes in detail what is supposed to be performed and the expected output format. Instruct the model to return the search phrases on one line each. Tell the model not to output any other text -- just the newline-separated phrases. Then, parse the output of the language model line by line and save the resulting search phrases as "phrases" for further research, skipping over empty lines. Clear all the old phrases and let the newly returned phrases by the phrases for the next iteration of this loop.
6. Send a notification with type "report_building", with the description indicating that the report is being built.
7. Take CONTEXT. Call the language model, instructing it to take CONTEXT (to be appended into the LM call) and produce a deep research report on the topic requested in PROMPT. The resulting report should go into detail wherever possible, rely only on the information available in CONTEXT, address the instruction given in the PROMPT, and be formatted in Markdown. This is to be communicated in the prompt. Do not shy away from using long, detailed and descriptive prompts! Tell the model not to output any other text, just the report. The result produced by the language model is to be called REPORT.
8. Send a notification with type "report_done", indicating that the report has been completed. Add "report" as a field containing the REPORT to be an additional payload to the notification.
`,
}