1
- const startResearch = ( ) => {
2
- // Clear output and reportContainer divs
3
-
4
- document . getElementById ( "output " ) . innerHTML = "" ;
5
- document . getElementById ( "reportContainer" ) . innerHTML = "" ;
6
-
7
- addAgentResponse ( { output : "🤔 Thinking about research questions for the task..." } ) ;
8
-
9
- listenToSockEvents ( ) ;
10
- }
11
-
12
- const listenToSockEvents = ( ) => {
13
- const { protocol, host , pathname } = window . location ;
14
- const ws_uri = ` ${ protocol === 'https:' ? 'wss:' : 'ws:' } // ${ host } ${ pathname } ws` ;
15
- const converter = new showdown . Converter ( ) ;
16
- const socket = new WebSocket ( ws_uri ) ;
17
- socket . onmessage = ( event ) => {
1
+ const GPTResearcher = ( ( ) => {
2
+ const startResearch = ( ) => {
3
+ document . getElementById ( "output" ) . innerHTML = "" ;
4
+ document . getElementById ( "reportContainer " ) . innerHTML = "" ;
5
+
6
+ addAgentResponse ( { output : "🤔 Thinking about research questions for the task..." } ) ;
7
+
8
+ listenToSockEvents ( ) ;
9
+ } ;
10
+
11
+ const listenToSockEvents = ( ) => {
12
+ const { protocol , host , pathname } = window . location ;
13
+ const ws_uri = ` $ {protocol === 'https:' ? 'wss:' : 'ws:' } // ${ host } ${ pathname } ws` ;
14
+ const converter = new showdown . Converter ( ) ;
15
+ const socket = new WebSocket ( ws_uri ) ;
16
+
17
+ socket . onmessage = ( event ) => {
18
18
const data = JSON . parse ( event . data ) ;
19
19
if ( data . type === 'logs' ) {
20
- addAgentResponse ( data ) ;
20
+ addAgentResponse ( data ) ;
21
21
} else if ( data . type === 'report' ) {
22
- writeReport ( data , converter ) ;
22
+ writeReport ( data , converter ) ;
23
23
} else if ( data . type === 'path' ) {
24
- updateDownloadLink ( data ) ;
24
+ updateDownloadLink ( data ) ;
25
25
}
26
+ } ;
27
+
28
+ socket . onopen = ( event ) => {
29
+ const task = document . querySelector ( 'input[name="task"]' ) . value ;
30
+ const report_type = document . querySelector ( 'select[name="report_type"]' ) . value ;
31
+ const agent = document . querySelector ( 'input[name="agent"]:checked' ) . value ;
32
+
33
+ const requestData = {
34
+ task : task ,
35
+ report_type : report_type ,
36
+ agent : agent ,
37
+ } ;
38
+
39
+ socket . send ( `start ${ JSON . stringify ( requestData ) } ` ) ;
40
+ } ;
41
+ } ;
42
+
43
+ const addAgentResponse = ( data ) => {
44
+ const output = document . getElementById ( "output" ) ;
45
+ output . innerHTML += '<div class="agent_response">' + data . output + '</div>' ;
46
+ output . scrollTop = output . scrollHeight ;
47
+ output . style . display = "block" ;
48
+ updateScroll ( ) ;
49
+ } ;
50
+
51
+ const writeReport = ( data , converter ) => {
52
+ const reportContainer = document . getElementById ( "reportContainer" ) ;
53
+ const markdownOutput = converter . makeHtml ( data . output ) ;
54
+ reportContainer . innerHTML += markdownOutput ;
55
+ updateScroll ( ) ;
56
+ } ;
57
+
58
+ const updateDownloadLink = ( data ) => {
59
+ const path = data . output ;
60
+ const downloadLink = document . getElementById ( "downloadLink" ) ;
61
+ downloadLink . href = path ;
62
+ } ;
63
+
64
+ const updateScroll = ( ) => {
65
+ window . scrollTo ( 0 , document . body . scrollHeight ) ;
66
+ } ;
67
+
68
+ const copyToClipboard = ( ) => {
69
+ const textarea = document . createElement ( 'textarea' ) ;
70
+ textarea . id = 'temp_element' ;
71
+ textarea . style . height = 0 ;
72
+ document . body . appendChild ( textarea ) ;
73
+ textarea . value = document . getElementById ( 'reportContainer' ) . innerText ;
74
+ const selector = document . querySelector ( '#temp_element' ) ;
75
+ selector . select ( ) ;
76
+ document . execCommand ( 'copy' ) ;
77
+ document . body . removeChild ( textarea ) ;
26
78
} ;
27
- socket . onopen = ( event ) => {
28
- let task = document . querySelector ( 'input[name="task"]' ) . value ;
29
- let report_type = document . querySelector ( 'select[name="report_type"]' ) . value ;
30
- let agent = document . querySelector ( 'input[name="agent"]:checked' ) . value ; // Corrected line
31
- let data = "start " + JSON . stringify ( { task : task , report_type : report_type , agent : agent } ) ;
32
- socket . send ( data ) ;
79
+
80
+ return {
81
+ startResearch,
82
+ copyToClipboard,
33
83
} ;
34
- }
35
-
36
- const addAgentResponse = ( data ) => {
37
- const output = document . getElementById ( "output" ) ;
38
- output . innerHTML += '<div class="agent_response">' + data . output + '</div>' ;
39
- output . scrollTop = output . scrollHeight ; // Scroll to the bottom of the output
40
- output . style . display = "block" ;
41
- updateScroll ( ) ;
42
- }
43
-
44
- const writeReport = ( data , converter ) => {
45
- const reportContainer = document . getElementById ( "reportContainer" ) ;
46
- const markdownOutput = converter . makeHtml ( data . output ) ;
47
- reportContainer . innerHTML += markdownOutput ;
48
- updateScroll ( ) ;
49
- }
50
-
51
- const updateDownloadLink = ( data ) => {
52
- const path = data . output ;
53
- const downloadLink = document . getElementById ( "downloadLink" ) ;
54
- downloadLink . href = path ;
55
- }
56
-
57
- const updateScroll = ( ) => {
58
- window . scrollTo ( 0 , document . body . scrollHeight ) ;
59
- }
60
-
61
- const copyToClipboard = ( ) => {
62
- const textarea = document . createElement ( 'textarea' ) ;
63
- textarea . id = 'temp_element' ;
64
- textarea . style . height = 0 ;
65
- document . body . appendChild ( textarea ) ;
66
- textarea . value = document . getElementById ( 'reportContainer' ) . innerText ;
67
- const selector = document . querySelector ( '#temp_element' ) ;
68
- selector . select ( ) ;
69
- document . execCommand ( 'copy' ) ;
70
- document . body . removeChild ( textarea ) ;
71
- }
84
+ } ) ( ) ;
85
+
86
+ // Initialize the research process
87
+ GPTResearcher . startResearch ( ) ;
88
+
0 commit comments