valyu-js 2.2.0 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -6,14 +6,14 @@ type ResponseLength = "short" | "medium" | "large" | "max" | number;
6
6
  interface SearchResult {
7
7
  title: string;
8
8
  url: string;
9
- content: string;
9
+ content: string | object | any[];
10
10
  description?: string;
11
11
  source: string;
12
- price: number;
12
+ source_type?: string;
13
+ data_type?: DataType;
14
+ date?: string;
13
15
  length: number;
14
16
  relevance_score?: number;
15
- data_type?: DataType;
16
- source_type?: string;
17
17
  publication_date?: string;
18
18
  id?: string;
19
19
  image_url?: Record<string, string>;
@@ -95,6 +95,7 @@ interface AnswerOptions {
95
95
  startDate?: string;
96
96
  endDate?: string;
97
97
  fastMode?: boolean;
98
+ streaming?: boolean;
98
99
  }
99
100
  interface SearchMetadata {
100
101
  tx_ids: string[];
@@ -108,11 +109,20 @@ interface AIUsage {
108
109
  interface Cost {
109
110
  total_deduction_dollars: number;
110
111
  search_deduction_dollars: number;
112
+ contents_deduction_dollars?: number;
111
113
  ai_deduction_dollars: number;
112
114
  }
115
+ interface ExtractionMetadata {
116
+ urls_requested: number;
117
+ urls_processed: number;
118
+ urls_failed: number;
119
+ total_characters: number;
120
+ response_length: string;
121
+ extract_effort: string;
122
+ }
113
123
  interface AnswerSuccessResponse {
114
124
  success: true;
115
- ai_tx_id: string;
125
+ tx_id: string;
116
126
  original_query: string;
117
127
  contents: string | Record<string, any>;
118
128
  data_type: "structured" | "unstructured";
@@ -120,15 +130,32 @@ interface AnswerSuccessResponse {
120
130
  search_metadata: SearchMetadata;
121
131
  ai_usage: AIUsage;
122
132
  cost: Cost;
133
+ extraction_metadata?: ExtractionMetadata;
123
134
  }
124
135
  interface AnswerErrorResponse {
125
136
  success: false;
126
137
  error: string;
127
138
  }
128
139
  type AnswerResponse = AnswerSuccessResponse | AnswerErrorResponse;
140
+ type AnswerStreamChunkType = "search_results" | "content" | "metadata" | "done" | "error";
141
+ interface AnswerStreamChunk {
142
+ type: AnswerStreamChunkType;
143
+ search_results?: SearchResult[];
144
+ content?: string;
145
+ finish_reason?: string;
146
+ tx_id?: string;
147
+ original_query?: string;
148
+ data_type?: "structured" | "unstructured";
149
+ search_metadata?: SearchMetadata;
150
+ ai_usage?: AIUsage;
151
+ cost?: Cost;
152
+ extraction_metadata?: ExtractionMetadata;
153
+ error?: string;
154
+ }
129
155
  type DeepResearchMode = "lite" | "heavy";
130
156
  type DeepResearchStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
131
- type DeepResearchOutputFormat = "markdown" | "pdf";
157
+ type DeepResearchOutputFormat = "markdown" | "pdf" | Record<string, any>;
158
+ type DeepResearchOutputType = "markdown" | "json";
132
159
  type ImageType = "chart" | "ai_generated";
133
160
  type ChartType = "line" | "bar" | "area";
134
161
  interface FileAttachment {
@@ -238,7 +265,8 @@ interface DeepResearchStatusResponse {
238
265
  progress?: Progress;
239
266
  messages?: any[];
240
267
  completed_at?: number;
241
- output?: string;
268
+ output?: string | Record<string, any>;
269
+ output_type?: DeepResearchOutputType;
242
270
  pdf_url?: string;
243
271
  images?: ImageMetadata[];
244
272
  sources?: DeepResearchSource[];
@@ -419,9 +447,30 @@ declare class Valyu {
419
447
  * @param options.startDate - Start date filter (YYYY-MM-DD format)
420
448
  * @param options.endDate - End date filter (YYYY-MM-DD format)
421
449
  * @param options.fastMode - Fast mode for quicker but shorter results (default: false)
422
- * @returns Promise resolving to answer response
450
+ * @param options.streaming - Enable streaming mode (default: false)
451
+ * @returns Promise resolving to answer response, or AsyncGenerator for streaming
452
+ */
453
+ answer(query: string, options?: AnswerOptions): Promise<AnswerResponse | AsyncGenerator<AnswerStreamChunk, void, unknown>>;
454
+ /**
455
+ * Validate answer parameters
456
+ */
457
+ private validateAnswerParams;
458
+ /**
459
+ * Build payload for answer API
460
+ */
461
+ private buildAnswerPayload;
462
+ /**
463
+ * Fetch answer (non-streaming mode)
464
+ */
465
+ private fetchAnswer;
466
+ /**
467
+ * Stream answer using SSE
468
+ */
469
+ private streamAnswer;
470
+ /**
471
+ * Create an error generator for streaming errors
423
472
  */
424
- answer(query: string, options?: AnswerOptions): Promise<AnswerResponse>;
473
+ private createErrorGenerator;
425
474
  }
426
475
 
427
- export { type AIUsage, type AnswerErrorResponse, type AnswerOptions, type AnswerResponse, type AnswerSuccessResponse, type ChartDataPoint, type ChartDataSeries, type ChartType, type ContentResponseLength, type ContentResult, type ContentsOptions, type ContentsResponse, type Cost, type CountryCode, type DeepResearchCancelResponse, type DeepResearchCreateOptions, type DeepResearchCreateResponse, type DeepResearchDeleteResponse, type DeepResearchListResponse, type DeepResearchMode, type DeepResearchOutputFormat, type DeepResearchSearchConfig, type DeepResearchSource, type DeepResearchStatus, type DeepResearchStatusResponse, type DeepResearchTaskListItem, type DeepResearchTogglePublicResponse, type DeepResearchUpdateResponse, type DeepResearchUsage, type ExtractEffort, type FeedbackResponse, type FeedbackSentiment, type FileAttachment, type ImageMetadata, type ImageType, type ListOptions, type MCPServerConfig, type Progress, type ResponseLength, type SearchMetadata, type SearchOptions, type SearchResponse, type SearchType, type StreamCallback, Valyu, type WaitOptions };
476
+ export { type AIUsage, type AnswerErrorResponse, type AnswerOptions, type AnswerResponse, type AnswerStreamChunk, type AnswerStreamChunkType, type AnswerSuccessResponse, type ChartDataPoint, type ChartDataSeries, type ChartType, type ContentResponseLength, type ContentResult, type ContentsOptions, type ContentsResponse, type Cost, type CountryCode, type DeepResearchCancelResponse, type DeepResearchCreateOptions, type DeepResearchCreateResponse, type DeepResearchDeleteResponse, type DeepResearchListResponse, type DeepResearchMode, type DeepResearchOutputFormat, type DeepResearchSearchConfig, type DeepResearchSource, type DeepResearchStatus, type DeepResearchStatusResponse, type DeepResearchTaskListItem, type DeepResearchTogglePublicResponse, type DeepResearchUpdateResponse, type DeepResearchUsage, type ExtractEffort, type ExtractionMetadata, type FeedbackResponse, type FeedbackSentiment, type FileAttachment, type ImageMetadata, type ImageType, type ListOptions, type MCPServerConfig, type Progress, type ResponseLength, type SearchMetadata, type SearchOptions, type SearchResponse, type SearchResult, type SearchType, type StreamCallback, Valyu, type WaitOptions };
package/dist/index.d.ts CHANGED
@@ -6,14 +6,14 @@ type ResponseLength = "short" | "medium" | "large" | "max" | number;
6
6
  interface SearchResult {
7
7
  title: string;
8
8
  url: string;
9
- content: string;
9
+ content: string | object | any[];
10
10
  description?: string;
11
11
  source: string;
12
- price: number;
12
+ source_type?: string;
13
+ data_type?: DataType;
14
+ date?: string;
13
15
  length: number;
14
16
  relevance_score?: number;
15
- data_type?: DataType;
16
- source_type?: string;
17
17
  publication_date?: string;
18
18
  id?: string;
19
19
  image_url?: Record<string, string>;
@@ -95,6 +95,7 @@ interface AnswerOptions {
95
95
  startDate?: string;
96
96
  endDate?: string;
97
97
  fastMode?: boolean;
98
+ streaming?: boolean;
98
99
  }
99
100
  interface SearchMetadata {
100
101
  tx_ids: string[];
@@ -108,11 +109,20 @@ interface AIUsage {
108
109
  interface Cost {
109
110
  total_deduction_dollars: number;
110
111
  search_deduction_dollars: number;
112
+ contents_deduction_dollars?: number;
111
113
  ai_deduction_dollars: number;
112
114
  }
115
+ interface ExtractionMetadata {
116
+ urls_requested: number;
117
+ urls_processed: number;
118
+ urls_failed: number;
119
+ total_characters: number;
120
+ response_length: string;
121
+ extract_effort: string;
122
+ }
113
123
  interface AnswerSuccessResponse {
114
124
  success: true;
115
- ai_tx_id: string;
125
+ tx_id: string;
116
126
  original_query: string;
117
127
  contents: string | Record<string, any>;
118
128
  data_type: "structured" | "unstructured";
@@ -120,15 +130,32 @@ interface AnswerSuccessResponse {
120
130
  search_metadata: SearchMetadata;
121
131
  ai_usage: AIUsage;
122
132
  cost: Cost;
133
+ extraction_metadata?: ExtractionMetadata;
123
134
  }
124
135
  interface AnswerErrorResponse {
125
136
  success: false;
126
137
  error: string;
127
138
  }
128
139
  type AnswerResponse = AnswerSuccessResponse | AnswerErrorResponse;
140
+ type AnswerStreamChunkType = "search_results" | "content" | "metadata" | "done" | "error";
141
+ interface AnswerStreamChunk {
142
+ type: AnswerStreamChunkType;
143
+ search_results?: SearchResult[];
144
+ content?: string;
145
+ finish_reason?: string;
146
+ tx_id?: string;
147
+ original_query?: string;
148
+ data_type?: "structured" | "unstructured";
149
+ search_metadata?: SearchMetadata;
150
+ ai_usage?: AIUsage;
151
+ cost?: Cost;
152
+ extraction_metadata?: ExtractionMetadata;
153
+ error?: string;
154
+ }
129
155
  type DeepResearchMode = "lite" | "heavy";
130
156
  type DeepResearchStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
131
- type DeepResearchOutputFormat = "markdown" | "pdf";
157
+ type DeepResearchOutputFormat = "markdown" | "pdf" | Record<string, any>;
158
+ type DeepResearchOutputType = "markdown" | "json";
132
159
  type ImageType = "chart" | "ai_generated";
133
160
  type ChartType = "line" | "bar" | "area";
134
161
  interface FileAttachment {
@@ -238,7 +265,8 @@ interface DeepResearchStatusResponse {
238
265
  progress?: Progress;
239
266
  messages?: any[];
240
267
  completed_at?: number;
241
- output?: string;
268
+ output?: string | Record<string, any>;
269
+ output_type?: DeepResearchOutputType;
242
270
  pdf_url?: string;
243
271
  images?: ImageMetadata[];
244
272
  sources?: DeepResearchSource[];
@@ -419,9 +447,30 @@ declare class Valyu {
419
447
  * @param options.startDate - Start date filter (YYYY-MM-DD format)
420
448
  * @param options.endDate - End date filter (YYYY-MM-DD format)
421
449
  * @param options.fastMode - Fast mode for quicker but shorter results (default: false)
422
- * @returns Promise resolving to answer response
450
+ * @param options.streaming - Enable streaming mode (default: false)
451
+ * @returns Promise resolving to answer response, or AsyncGenerator for streaming
452
+ */
453
+ answer(query: string, options?: AnswerOptions): Promise<AnswerResponse | AsyncGenerator<AnswerStreamChunk, void, unknown>>;
454
+ /**
455
+ * Validate answer parameters
456
+ */
457
+ private validateAnswerParams;
458
+ /**
459
+ * Build payload for answer API
460
+ */
461
+ private buildAnswerPayload;
462
+ /**
463
+ * Fetch answer (non-streaming mode)
464
+ */
465
+ private fetchAnswer;
466
+ /**
467
+ * Stream answer using SSE
468
+ */
469
+ private streamAnswer;
470
+ /**
471
+ * Create an error generator for streaming errors
423
472
  */
424
- answer(query: string, options?: AnswerOptions): Promise<AnswerResponse>;
473
+ private createErrorGenerator;
425
474
  }
426
475
 
427
- export { type AIUsage, type AnswerErrorResponse, type AnswerOptions, type AnswerResponse, type AnswerSuccessResponse, type ChartDataPoint, type ChartDataSeries, type ChartType, type ContentResponseLength, type ContentResult, type ContentsOptions, type ContentsResponse, type Cost, type CountryCode, type DeepResearchCancelResponse, type DeepResearchCreateOptions, type DeepResearchCreateResponse, type DeepResearchDeleteResponse, type DeepResearchListResponse, type DeepResearchMode, type DeepResearchOutputFormat, type DeepResearchSearchConfig, type DeepResearchSource, type DeepResearchStatus, type DeepResearchStatusResponse, type DeepResearchTaskListItem, type DeepResearchTogglePublicResponse, type DeepResearchUpdateResponse, type DeepResearchUsage, type ExtractEffort, type FeedbackResponse, type FeedbackSentiment, type FileAttachment, type ImageMetadata, type ImageType, type ListOptions, type MCPServerConfig, type Progress, type ResponseLength, type SearchMetadata, type SearchOptions, type SearchResponse, type SearchType, type StreamCallback, Valyu, type WaitOptions };
476
+ export { type AIUsage, type AnswerErrorResponse, type AnswerOptions, type AnswerResponse, type AnswerStreamChunk, type AnswerStreamChunkType, type AnswerSuccessResponse, type ChartDataPoint, type ChartDataSeries, type ChartType, type ContentResponseLength, type ContentResult, type ContentsOptions, type ContentsResponse, type Cost, type CountryCode, type DeepResearchCancelResponse, type DeepResearchCreateOptions, type DeepResearchCreateResponse, type DeepResearchDeleteResponse, type DeepResearchListResponse, type DeepResearchMode, type DeepResearchOutputFormat, type DeepResearchSearchConfig, type DeepResearchSource, type DeepResearchStatus, type DeepResearchStatusResponse, type DeepResearchTaskListItem, type DeepResearchTogglePublicResponse, type DeepResearchUpdateResponse, type DeepResearchUsage, type ExtractEffort, type ExtractionMetadata, type FeedbackResponse, type FeedbackSentiment, type FileAttachment, type ImageMetadata, type ImageType, type ListOptions, type MCPServerConfig, type Progress, type ResponseLength, type SearchMetadata, type SearchOptions, type SearchResponse, type SearchResult, type SearchType, type StreamCallback, Valyu, type WaitOptions };
package/dist/index.js CHANGED
@@ -721,164 +721,269 @@ var Valyu = class {
721
721
  * @param options.startDate - Start date filter (YYYY-MM-DD format)
722
722
  * @param options.endDate - End date filter (YYYY-MM-DD format)
723
723
  * @param options.fastMode - Fast mode for quicker but shorter results (default: false)
724
- * @returns Promise resolving to answer response
724
+ * @param options.streaming - Enable streaming mode (default: false)
725
+ * @returns Promise resolving to answer response, or AsyncGenerator for streaming
725
726
  */
726
727
  async answer(query, options = {}) {
727
- try {
728
- const defaultSearchType = "all";
729
- if (!query || typeof query !== "string" || query.trim().length === 0) {
730
- return {
731
- success: false,
732
- error: "Query is required and must be a non-empty string"
733
- };
734
- }
735
- let finalSearchType = defaultSearchType;
736
- const providedSearchTypeString = options.searchType?.toLowerCase();
737
- if (providedSearchTypeString === "web" || providedSearchTypeString === "proprietary" || providedSearchTypeString === "all" || providedSearchTypeString === "news") {
738
- finalSearchType = providedSearchTypeString;
739
- } else if (options.searchType !== void 0) {
740
- return {
741
- success: false,
742
- error: "Invalid searchType provided. Must be one of: all, web, proprietary, news"
743
- };
728
+ const validationError = this.validateAnswerParams(query, options);
729
+ if (validationError) {
730
+ if (options.streaming) {
731
+ return this.createErrorGenerator(validationError);
744
732
  }
745
- if (options.systemInstructions !== void 0) {
746
- if (typeof options.systemInstructions !== "string") {
747
- return {
748
- success: false,
749
- error: "systemInstructions must be a string"
750
- };
751
- }
752
- const trimmed = options.systemInstructions.trim();
753
- if (trimmed.length === 0) {
754
- return {
755
- success: false,
756
- error: "systemInstructions cannot be empty when provided"
757
- };
758
- }
759
- if (trimmed.length > 2e3) {
760
- return {
761
- success: false,
762
- error: "systemInstructions must be 2000 characters or less"
763
- };
764
- }
765
- }
766
- if (options.dataMaxPrice !== void 0) {
767
- if (typeof options.dataMaxPrice !== "number" || options.dataMaxPrice <= 0) {
768
- return {
769
- success: false,
770
- error: "dataMaxPrice must be a positive number"
771
- };
772
- }
773
- }
774
- if (options.startDate && !this.validateDateFormat(options.startDate)) {
775
- return {
776
- success: false,
777
- error: "Invalid startDate format. Must be YYYY-MM-DD"
778
- };
779
- }
780
- if (options.endDate && !this.validateDateFormat(options.endDate)) {
781
- return {
782
- success: false,
783
- error: "Invalid endDate format. Must be YYYY-MM-DD"
784
- };
785
- }
786
- if (options.startDate && options.endDate) {
787
- const startDate = new Date(options.startDate);
788
- const endDate = new Date(options.endDate);
789
- if (startDate > endDate) {
790
- return {
791
- success: false,
792
- error: "startDate must be before endDate"
793
- };
794
- }
795
- }
796
- if (options.includedSources !== void 0) {
797
- if (!Array.isArray(options.includedSources)) {
798
- return {
799
- success: false,
800
- error: "includedSources must be an array"
801
- };
802
- }
803
- const includedSourcesValidation = this.validateSources(
804
- options.includedSources
805
- );
806
- if (!includedSourcesValidation.valid) {
807
- return {
808
- success: false,
809
- error: `Invalid includedSources format. Invalid sources: ${includedSourcesValidation.invalidSources.join(
810
- ", "
811
- )}. Sources must be valid URLs, domains (with optional paths), or dataset identifiers in 'provider/dataset' format.`
812
- };
813
- }
814
- }
815
- if (options.excludedSources !== void 0) {
816
- if (!Array.isArray(options.excludedSources)) {
817
- return {
818
- success: false,
819
- error: "excludedSources must be an array"
820
- };
821
- }
822
- const excludedSourcesValidation = this.validateSources(
823
- options.excludedSources
824
- );
825
- if (!excludedSourcesValidation.valid) {
826
- return {
827
- success: false,
828
- error: `Invalid excludedSources format. Invalid sources: ${excludedSourcesValidation.invalidSources.join(
829
- ", "
830
- )}. Sources must be valid URLs, domains (with optional paths), or dataset identifiers in 'provider/dataset' format.`
831
- };
832
- }
833
- }
834
- const payload = {
835
- query: query.trim(),
836
- search_type: finalSearchType
837
- };
838
- if (options.dataMaxPrice !== void 0) {
839
- payload.data_max_price = options.dataMaxPrice;
733
+ return { success: false, error: validationError };
734
+ }
735
+ const payload = this.buildAnswerPayload(query, options);
736
+ if (options.streaming) {
737
+ return this.streamAnswer(payload);
738
+ } else {
739
+ return this.fetchAnswer(payload);
740
+ }
741
+ }
742
+ /**
743
+ * Validate answer parameters
744
+ */
745
+ validateAnswerParams(query, options) {
746
+ if (!query || typeof query !== "string" || query.trim().length === 0) {
747
+ return "Query is required and must be a non-empty string";
748
+ }
749
+ const providedSearchTypeString = options.searchType?.toLowerCase();
750
+ if (providedSearchTypeString !== void 0 && providedSearchTypeString !== "web" && providedSearchTypeString !== "proprietary" && providedSearchTypeString !== "all" && providedSearchTypeString !== "news") {
751
+ return "Invalid searchType provided. Must be one of: all, web, proprietary, news";
752
+ }
753
+ if (options.systemInstructions !== void 0) {
754
+ if (typeof options.systemInstructions !== "string") {
755
+ return "systemInstructions must be a string";
840
756
  }
841
- if (options.structuredOutput !== void 0) {
842
- payload.structured_output = options.structuredOutput;
757
+ const trimmed = options.systemInstructions.trim();
758
+ if (trimmed.length === 0) {
759
+ return "systemInstructions cannot be empty when provided";
843
760
  }
844
- if (options.systemInstructions !== void 0) {
845
- payload.system_instructions = options.systemInstructions.trim();
761
+ if (trimmed.length > 2e3) {
762
+ return "systemInstructions must be 2000 characters or less";
846
763
  }
847
- if (options.countryCode !== void 0) {
848
- payload.country_code = options.countryCode;
764
+ }
765
+ if (options.dataMaxPrice !== void 0) {
766
+ if (typeof options.dataMaxPrice !== "number" || options.dataMaxPrice <= 0) {
767
+ return "dataMaxPrice must be a positive number";
849
768
  }
850
- if (options.includedSources !== void 0) {
851
- payload.included_sources = options.includedSources;
769
+ }
770
+ if (options.startDate && !this.validateDateFormat(options.startDate)) {
771
+ return "Invalid startDate format. Must be YYYY-MM-DD";
772
+ }
773
+ if (options.endDate && !this.validateDateFormat(options.endDate)) {
774
+ return "Invalid endDate format. Must be YYYY-MM-DD";
775
+ }
776
+ if (options.startDate && options.endDate) {
777
+ const startDate = new Date(options.startDate);
778
+ const endDate = new Date(options.endDate);
779
+ if (startDate > endDate) {
780
+ return "startDate must be before endDate";
852
781
  }
853
- if (options.excludedSources !== void 0) {
854
- payload.excluded_sources = options.excludedSources;
782
+ }
783
+ if (options.includedSources !== void 0) {
784
+ if (!Array.isArray(options.includedSources)) {
785
+ return "includedSources must be an array";
855
786
  }
856
- if (options.startDate !== void 0) {
857
- payload.start_date = options.startDate;
787
+ const validation = this.validateSources(options.includedSources);
788
+ if (!validation.valid) {
789
+ return `Invalid includedSources format. Invalid sources: ${validation.invalidSources.join(", ")}.`;
858
790
  }
859
- if (options.endDate !== void 0) {
860
- payload.end_date = options.endDate;
791
+ }
792
+ if (options.excludedSources !== void 0) {
793
+ if (!Array.isArray(options.excludedSources)) {
794
+ return "excludedSources must be an array";
861
795
  }
862
- if (options.fastMode !== void 0) {
863
- payload.fast_mode = options.fastMode;
796
+ const validation = this.validateSources(options.excludedSources);
797
+ if (!validation.valid) {
798
+ return `Invalid excludedSources format. Invalid sources: ${validation.invalidSources.join(", ")}.`;
864
799
  }
865
- const response = await import_axios.default.post(`${this.baseUrl}/answer`, payload, {
866
- headers: this.headers
800
+ }
801
+ return null;
802
+ }
803
+ /**
804
+ * Build payload for answer API
805
+ */
806
+ buildAnswerPayload(query, options) {
807
+ const defaultSearchType = "all";
808
+ const providedSearchTypeString = options.searchType?.toLowerCase();
809
+ let finalSearchType = defaultSearchType;
810
+ if (providedSearchTypeString === "web" || providedSearchTypeString === "proprietary" || providedSearchTypeString === "all" || providedSearchTypeString === "news") {
811
+ finalSearchType = providedSearchTypeString;
812
+ }
813
+ const payload = {
814
+ query: query.trim(),
815
+ search_type: finalSearchType
816
+ };
817
+ if (options.dataMaxPrice !== void 0) payload.data_max_price = options.dataMaxPrice;
818
+ if (options.structuredOutput !== void 0) payload.structured_output = options.structuredOutput;
819
+ if (options.systemInstructions !== void 0) payload.system_instructions = options.systemInstructions.trim();
820
+ if (options.countryCode !== void 0) payload.country_code = options.countryCode;
821
+ if (options.includedSources !== void 0) payload.included_sources = options.includedSources;
822
+ if (options.excludedSources !== void 0) payload.excluded_sources = options.excludedSources;
823
+ if (options.startDate !== void 0) payload.start_date = options.startDate;
824
+ if (options.endDate !== void 0) payload.end_date = options.endDate;
825
+ if (options.fastMode !== void 0) payload.fast_mode = options.fastMode;
826
+ return payload;
827
+ }
828
+ /**
829
+ * Fetch answer (non-streaming mode)
830
+ */
831
+ async fetchAnswer(payload) {
832
+ try {
833
+ const response = await fetch(`${this.baseUrl}/answer`, {
834
+ method: "POST",
835
+ headers: {
836
+ ...this.headers,
837
+ "Accept": "text/event-stream"
838
+ },
839
+ body: JSON.stringify(payload)
867
840
  });
868
- if (!response.status || response.status < 200 || response.status >= 300) {
841
+ if (!response.ok) {
842
+ const errorData = await response.json().catch(() => ({}));
869
843
  return {
870
844
  success: false,
871
- error: response.data?.error || "Request failed"
845
+ error: errorData.error || `HTTP Error: ${response.status}`
872
846
  };
873
847
  }
874
- return response.data;
848
+ let fullContent = "";
849
+ let searchResults = [];
850
+ let finalMetadata = {};
851
+ const reader = response.body?.getReader();
852
+ const decoder = new TextDecoder();
853
+ let buffer = "";
854
+ if (reader) {
855
+ while (true) {
856
+ const { done, value } = await reader.read();
857
+ if (done) break;
858
+ buffer += decoder.decode(value, { stream: true });
859
+ const lines = buffer.split("\n");
860
+ buffer = lines.pop() || "";
861
+ for (const line of lines) {
862
+ if (!line.startsWith("data: ")) continue;
863
+ const dataStr = line.slice(6);
864
+ if (dataStr === "[DONE]") continue;
865
+ try {
866
+ const parsed = JSON.parse(dataStr);
867
+ if (parsed.search_results && !parsed.success) {
868
+ searchResults = [...searchResults, ...parsed.search_results];
869
+ } else if (parsed.choices) {
870
+ const content = parsed.choices[0]?.delta?.content || "";
871
+ if (content) fullContent += content;
872
+ } else if (parsed.success !== void 0) {
873
+ finalMetadata = parsed;
874
+ }
875
+ } catch {
876
+ continue;
877
+ }
878
+ }
879
+ }
880
+ }
881
+ if (finalMetadata.success) {
882
+ const finalSearchResults = finalMetadata.search_results || searchResults;
883
+ const response2 = {
884
+ success: true,
885
+ tx_id: finalMetadata.tx_id || "",
886
+ original_query: finalMetadata.original_query || payload.query,
887
+ contents: fullContent || finalMetadata.contents || "",
888
+ data_type: finalMetadata.data_type || "unstructured",
889
+ search_results: finalSearchResults,
890
+ search_metadata: finalMetadata.search_metadata || { tx_ids: [], number_of_results: 0, total_characters: 0 },
891
+ ai_usage: finalMetadata.ai_usage || { input_tokens: 0, output_tokens: 0 },
892
+ cost: finalMetadata.cost || { total_deduction_dollars: 0, search_deduction_dollars: 0, ai_deduction_dollars: 0 }
893
+ };
894
+ if (finalMetadata.extraction_metadata) {
895
+ response2.extraction_metadata = finalMetadata.extraction_metadata;
896
+ }
897
+ return response2;
898
+ }
899
+ return {
900
+ success: false,
901
+ error: finalMetadata.error || "Unknown error occurred"
902
+ };
875
903
  } catch (e) {
876
904
  return {
877
905
  success: false,
878
- error: e.response?.data?.error || e.message
906
+ error: e.message || "Request failed"
879
907
  };
880
908
  }
881
909
  }
910
+ /**
911
+ * Stream answer using SSE
912
+ */
913
+ async *streamAnswer(payload) {
914
+ try {
915
+ const response = await fetch(`${this.baseUrl}/answer`, {
916
+ method: "POST",
917
+ headers: {
918
+ ...this.headers,
919
+ "Accept": "text/event-stream"
920
+ },
921
+ body: JSON.stringify(payload)
922
+ });
923
+ if (!response.ok) {
924
+ const errorData = await response.json().catch(() => ({}));
925
+ yield { type: "error", error: errorData.error || `HTTP Error: ${response.status}` };
926
+ return;
927
+ }
928
+ const reader = response.body?.getReader();
929
+ const decoder = new TextDecoder();
930
+ let buffer = "";
931
+ if (!reader) {
932
+ yield { type: "error", error: "No response body" };
933
+ return;
934
+ }
935
+ while (true) {
936
+ const { done, value } = await reader.read();
937
+ if (done) break;
938
+ buffer += decoder.decode(value, { stream: true });
939
+ const lines = buffer.split("\n");
940
+ buffer = lines.pop() || "";
941
+ for (const line of lines) {
942
+ if (!line.startsWith("data: ")) continue;
943
+ const dataStr = line.slice(6);
944
+ if (dataStr === "[DONE]") {
945
+ yield { type: "done" };
946
+ continue;
947
+ }
948
+ try {
949
+ const parsed = JSON.parse(dataStr);
950
+ if (parsed.search_results && parsed.success === void 0) {
951
+ yield { type: "search_results", search_results: parsed.search_results };
952
+ } else if (parsed.choices) {
953
+ const delta = parsed.choices[0]?.delta || {};
954
+ const content = delta.content || "";
955
+ const finishReason = parsed.choices[0]?.finish_reason;
956
+ if (content || finishReason) {
957
+ yield { type: "content", content, finish_reason: finishReason };
958
+ }
959
+ } else if (parsed.success !== void 0) {
960
+ yield {
961
+ type: "metadata",
962
+ tx_id: parsed.tx_id,
963
+ original_query: parsed.original_query,
964
+ data_type: parsed.data_type,
965
+ search_results: parsed.search_results,
966
+ search_metadata: parsed.search_metadata,
967
+ ai_usage: parsed.ai_usage,
968
+ cost: parsed.cost,
969
+ extraction_metadata: parsed.extraction_metadata
970
+ };
971
+ }
972
+ } catch {
973
+ continue;
974
+ }
975
+ }
976
+ }
977
+ } catch (e) {
978
+ yield { type: "error", error: e.message || "Stream failed" };
979
+ }
980
+ }
981
+ /**
982
+ * Create an error generator for streaming errors
983
+ */
984
+ async *createErrorGenerator(error) {
985
+ yield { type: "error", error };
986
+ }
882
987
  };
883
988
  // Annotate the CommonJS export names for ESM import in node:
884
989
  0 && (module.exports = {