File tree Expand file tree Collapse file tree 3 files changed +51
-33
lines changed
Expand file tree Collapse file tree 3 files changed +51
-33
lines changed Original file line number Diff line number Diff line change @@ -33,11 +33,9 @@ type PerplexityResponse = {
3333 } ;
3434} ;
3535
36- type AskResult = {
37- answer : string ;
38- citations : string [ ] ;
39- model : string ;
40- } ;
36+ type AskResult =
37+ | { success : true ; data : { answer : string ; citations : string [ ] ; model : string } }
38+ | { success : false ; error : { message : string } } ;
4139
4240export type PerplexityAskCoreInput = {
4341 question : string ;
@@ -60,7 +58,10 @@ async function stepHandler(
6058 const apiKey = credentials . PERPLEXITY_API_KEY ;
6159
6260 if ( ! apiKey ) {
63- throw new Error ( "Perplexity API Key is not configured." ) ;
61+ return {
62+ success : false ,
63+ error : { message : "Perplexity API Key is not configured." } ,
64+ } ;
6465 }
6566
6667 try {
@@ -99,7 +100,10 @@ async function stepHandler(
99100
100101 if ( ! response . ok ) {
101102 const errorText = await response . text ( ) ;
102- throw new Error ( `HTTP ${ response . status } : ${ errorText } ` ) ;
103+ return {
104+ success : false ,
105+ error : { message : `HTTP ${ response . status } : ${ errorText } ` } ,
106+ } ;
103107 }
104108
105109 const result = ( await response . json ( ) ) as PerplexityResponse ;
@@ -110,12 +114,14 @@ async function stepHandler(
110114 ) ;
111115
112116 return {
113- answer,
114- citations,
115- model : result . model ,
117+ success : true ,
118+ data : { answer, citations, model : result . model } ,
116119 } ;
117120 } catch ( error ) {
118- throw new Error ( `Failed to ask: ${ getErrorMessage ( error ) } ` ) ;
121+ return {
122+ success : false ,
123+ error : { message : `Failed to ask: ${ getErrorMessage ( error ) } ` } ,
124+ } ;
119125 }
120126}
121127
Original file line number Diff line number Diff line change @@ -33,11 +33,9 @@ type PerplexityResponse = {
3333 } ;
3434} ;
3535
36- type ResearchResult = {
37- report : string ;
38- citations : string [ ] ;
39- model : string ;
40- } ;
36+ type ResearchResult =
37+ | { success : true ; data : { report : string ; citations : string [ ] ; model : string } }
38+ | { success : false ; error : { message : string } } ;
4139
4240export type PerplexityResearchCoreInput = {
4341 topic : string ;
@@ -59,7 +57,10 @@ async function stepHandler(
5957 const apiKey = credentials . PERPLEXITY_API_KEY ;
6058
6159 if ( ! apiKey ) {
62- throw new Error ( "Perplexity API Key is not configured." ) ;
60+ return {
61+ success : false ,
62+ error : { message : "Perplexity API Key is not configured." } ,
63+ } ;
6364 }
6465
6566 const depthInstructions = getDepthInstructions ( input . depth ) ;
@@ -89,7 +90,10 @@ async function stepHandler(
8990
9091 if ( ! response . ok ) {
9192 const errorText = await response . text ( ) ;
92- throw new Error ( `HTTP ${ response . status } : ${ errorText } ` ) ;
93+ return {
94+ success : false ,
95+ error : { message : `HTTP ${ response . status } : ${ errorText } ` } ,
96+ } ;
9397 }
9498
9599 const result = ( await response . json ( ) ) as PerplexityResponse ;
@@ -100,12 +104,14 @@ async function stepHandler(
100104 ) ;
101105
102106 return {
103- report,
104- citations,
105- model : result . model ,
107+ success : true ,
108+ data : { report, citations, model : result . model } ,
106109 } ;
107110 } catch ( error ) {
108- throw new Error ( `Failed to research: ${ getErrorMessage ( error ) } ` ) ;
111+ return {
112+ success : false ,
113+ error : { message : `Failed to research: ${ getErrorMessage ( error ) } ` } ,
114+ } ;
109115 }
110116}
111117
Original file line number Diff line number Diff line change @@ -33,11 +33,9 @@ type PerplexityResponse = {
3333 } ;
3434} ;
3535
36- type SearchResult = {
37- answer : string ;
38- citations : string [ ] ;
39- model : string ;
40- } ;
36+ type SearchResult =
37+ | { success : true ; data : { answer : string ; citations : string [ ] ; model : string } }
38+ | { success : false ; error : { message : string } } ;
4139
4240export type PerplexitySearchCoreInput = {
4341 query : string ;
@@ -59,7 +57,10 @@ async function stepHandler(
5957 const apiKey = credentials . PERPLEXITY_API_KEY ;
6058
6159 if ( ! apiKey ) {
62- throw new Error ( "Perplexity API Key is not configured." ) ;
60+ return {
61+ success : false ,
62+ error : { message : "Perplexity API Key is not configured." } ,
63+ } ;
6364 }
6465
6566 try {
@@ -89,7 +90,10 @@ async function stepHandler(
8990
9091 if ( ! response . ok ) {
9192 const errorText = await response . text ( ) ;
92- throw new Error ( `HTTP ${ response . status } : ${ errorText } ` ) ;
93+ return {
94+ success : false ,
95+ error : { message : `HTTP ${ response . status } : ${ errorText } ` } ,
96+ } ;
9397 }
9498
9599 const result = ( await response . json ( ) ) as PerplexityResponse ;
@@ -100,12 +104,14 @@ async function stepHandler(
100104 ) ;
101105
102106 return {
103- answer,
104- citations,
105- model : result . model ,
107+ success : true ,
108+ data : { answer, citations, model : result . model } ,
106109 } ;
107110 } catch ( error ) {
108- throw new Error ( `Failed to search: ${ getErrorMessage ( error ) } ` ) ;
111+ return {
112+ success : false ,
113+ error : { message : `Failed to search: ${ getErrorMessage ( error ) } ` } ,
114+ } ;
109115 }
110116}
111117
You can’t perform that action at this time.
0 commit comments