valyu-js 2.3.2 → 2.4.0
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/README.md +83 -3
- package/dist/index.d.mts +177 -2
- package/dist/index.d.ts +177 -2
- package/dist/index.js +235 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +235 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +54 -54
package/dist/index.mjs
CHANGED
|
@@ -24,6 +24,15 @@ var Valyu = class {
|
|
|
24
24
|
delete: this._deepresearchDelete.bind(this),
|
|
25
25
|
togglePublic: this._deepresearchTogglePublic.bind(this)
|
|
26
26
|
};
|
|
27
|
+
this.batch = {
|
|
28
|
+
create: this._batchCreate.bind(this),
|
|
29
|
+
status: this._batchStatus.bind(this),
|
|
30
|
+
addTasks: this._batchAddTasks.bind(this),
|
|
31
|
+
listTasks: this._batchListTasks.bind(this),
|
|
32
|
+
cancel: this._batchCancel.bind(this),
|
|
33
|
+
list: this._batchList.bind(this),
|
|
34
|
+
waitForCompletion: this._batchWaitForCompletion.bind(this)
|
|
35
|
+
};
|
|
27
36
|
}
|
|
28
37
|
/**
|
|
29
38
|
* Validates date format (YYYY-MM-DD)
|
|
@@ -316,9 +325,10 @@ var Valyu = class {
|
|
|
316
325
|
* @param urls - Array of URLs to process (max 10)
|
|
317
326
|
* @param options - Content extraction configuration options
|
|
318
327
|
* @param options.summary - AI summary configuration: false (raw), true (auto), string (custom), or JSON schema
|
|
319
|
-
* @param options.extractEffort - Extraction thoroughness: "normal" or "
|
|
328
|
+
* @param options.extractEffort - Extraction thoroughness: "normal", "high", or "auto"
|
|
320
329
|
* @param options.responseLength - Content length per URL
|
|
321
330
|
* @param options.maxPriceDollars - Maximum cost limit in USD
|
|
331
|
+
* @param options.screenshot - Request page screenshots (default: false)
|
|
322
332
|
* @returns Promise resolving to content extraction results
|
|
323
333
|
*/
|
|
324
334
|
async contents(urls, options = {}) {
|
|
@@ -413,6 +423,9 @@ var Valyu = class {
|
|
|
413
423
|
if (options.maxPriceDollars !== void 0) {
|
|
414
424
|
payload.max_price_dollars = options.maxPriceDollars;
|
|
415
425
|
}
|
|
426
|
+
if (options.screenshot !== void 0) {
|
|
427
|
+
payload.screenshot = options.screenshot;
|
|
428
|
+
}
|
|
416
429
|
const response = await axios.post(`${this.baseUrl}/contents`, payload, {
|
|
417
430
|
headers: this.headers
|
|
418
431
|
});
|
|
@@ -455,7 +468,7 @@ var Valyu = class {
|
|
|
455
468
|
}
|
|
456
469
|
const payload = {
|
|
457
470
|
input: options.input,
|
|
458
|
-
model: options.model || "
|
|
471
|
+
model: options.model || "fast",
|
|
459
472
|
output_formats: options.outputFormats || ["markdown"],
|
|
460
473
|
code_execution: options.codeExecution !== false
|
|
461
474
|
};
|
|
@@ -674,6 +687,184 @@ var Valyu = class {
|
|
|
674
687
|
};
|
|
675
688
|
}
|
|
676
689
|
}
|
|
690
|
+
/**
|
|
691
|
+
* Batch: Create a new batch
|
|
692
|
+
* @param options - Batch configuration options
|
|
693
|
+
* @param options.name - Optional name for the batch
|
|
694
|
+
* @param options.model - DeepResearch mode: "fast", "standard", or "heavy" (default: "standard")
|
|
695
|
+
* @param options.outputFormats - Output formats for tasks (default: ["markdown"])
|
|
696
|
+
* @param options.search - Search configuration for all tasks in batch
|
|
697
|
+
* @param options.webhookUrl - Optional HTTPS URL for completion notification
|
|
698
|
+
* @param options.metadata - Optional metadata key-value pairs
|
|
699
|
+
* @returns Promise resolving to batch creation response with batch_id and webhook_secret
|
|
700
|
+
*/
|
|
701
|
+
async _batchCreate(options = {}) {
|
|
702
|
+
try {
|
|
703
|
+
const payload = {};
|
|
704
|
+
if (options.name) payload.name = options.name;
|
|
705
|
+
if (options.model) payload.model = options.model;
|
|
706
|
+
if (options.outputFormats) payload.output_formats = options.outputFormats;
|
|
707
|
+
if (options.search) {
|
|
708
|
+
payload.search = {
|
|
709
|
+
search_type: options.search.searchType,
|
|
710
|
+
included_sources: options.search.includedSources
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
if (options.webhookUrl) payload.webhook_url = options.webhookUrl;
|
|
714
|
+
if (options.metadata) payload.metadata = options.metadata;
|
|
715
|
+
const response = await axios.post(
|
|
716
|
+
`${this.baseUrl}/deepresearch/batches`,
|
|
717
|
+
payload,
|
|
718
|
+
{ headers: this.headers }
|
|
719
|
+
);
|
|
720
|
+
return { success: true, ...response.data };
|
|
721
|
+
} catch (e) {
|
|
722
|
+
return {
|
|
723
|
+
success: false,
|
|
724
|
+
error: e.response?.data?.error || e.message
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Batch: Get batch status
|
|
730
|
+
* @param batchId - The batch ID to query
|
|
731
|
+
* @returns Promise resolving to batch status with counts and usage
|
|
732
|
+
*/
|
|
733
|
+
async _batchStatus(batchId) {
|
|
734
|
+
try {
|
|
735
|
+
const response = await axios.get(
|
|
736
|
+
`${this.baseUrl}/deepresearch/batches/${batchId}`,
|
|
737
|
+
{ headers: this.headers }
|
|
738
|
+
);
|
|
739
|
+
return { success: true, batch: response.data };
|
|
740
|
+
} catch (e) {
|
|
741
|
+
return {
|
|
742
|
+
success: false,
|
|
743
|
+
error: e.response?.data?.error || e.message
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Batch: Add tasks to a batch
|
|
749
|
+
* @param batchId - The batch ID to add tasks to
|
|
750
|
+
* @param options - Task configuration options
|
|
751
|
+
* @param options.tasks - Array of task inputs
|
|
752
|
+
* @returns Promise resolving to response with added_count and task_ids
|
|
753
|
+
*/
|
|
754
|
+
async _batchAddTasks(batchId, options) {
|
|
755
|
+
try {
|
|
756
|
+
if (!options.tasks || !Array.isArray(options.tasks)) {
|
|
757
|
+
return {
|
|
758
|
+
success: false,
|
|
759
|
+
error: "tasks must be an array"
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
if (options.tasks.length === 0) {
|
|
763
|
+
return {
|
|
764
|
+
success: false,
|
|
765
|
+
error: "tasks array cannot be empty"
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
const response = await axios.post(
|
|
769
|
+
`${this.baseUrl}/deepresearch/batches/${batchId}/tasks`,
|
|
770
|
+
{ tasks: options.tasks },
|
|
771
|
+
{ headers: this.headers }
|
|
772
|
+
);
|
|
773
|
+
return { success: true, ...response.data };
|
|
774
|
+
} catch (e) {
|
|
775
|
+
return {
|
|
776
|
+
success: false,
|
|
777
|
+
error: e.response?.data?.error || e.message
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Batch: List all tasks in a batch
|
|
783
|
+
* @param batchId - The batch ID to query
|
|
784
|
+
* @returns Promise resolving to list of tasks with their status
|
|
785
|
+
*/
|
|
786
|
+
async _batchListTasks(batchId) {
|
|
787
|
+
try {
|
|
788
|
+
const response = await axios.get(
|
|
789
|
+
`${this.baseUrl}/deepresearch/batches/${batchId}/tasks`,
|
|
790
|
+
{ headers: this.headers }
|
|
791
|
+
);
|
|
792
|
+
return { success: true, ...response.data };
|
|
793
|
+
} catch (e) {
|
|
794
|
+
return {
|
|
795
|
+
success: false,
|
|
796
|
+
error: e.response?.data?.error || e.message
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Batch: Cancel a batch and all its pending tasks
|
|
802
|
+
* @param batchId - The batch ID to cancel
|
|
803
|
+
* @returns Promise resolving to cancellation confirmation
|
|
804
|
+
*/
|
|
805
|
+
async _batchCancel(batchId) {
|
|
806
|
+
try {
|
|
807
|
+
const response = await axios.post(
|
|
808
|
+
`${this.baseUrl}/deepresearch/batches/${batchId}/cancel`,
|
|
809
|
+
{},
|
|
810
|
+
{ headers: this.headers }
|
|
811
|
+
);
|
|
812
|
+
return { success: true, ...response.data };
|
|
813
|
+
} catch (e) {
|
|
814
|
+
return {
|
|
815
|
+
success: false,
|
|
816
|
+
error: e.response?.data?.error || e.message
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Batch: List all batches
|
|
822
|
+
* @returns Promise resolving to list of all batches
|
|
823
|
+
*/
|
|
824
|
+
async _batchList() {
|
|
825
|
+
try {
|
|
826
|
+
const response = await axios.get(`${this.baseUrl}/deepresearch/batches`, {
|
|
827
|
+
headers: this.headers
|
|
828
|
+
});
|
|
829
|
+
return { success: true, batches: response.data };
|
|
830
|
+
} catch (e) {
|
|
831
|
+
return {
|
|
832
|
+
success: false,
|
|
833
|
+
error: e.response?.data?.error || e.message
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Batch: Wait for batch completion with polling
|
|
839
|
+
* @param batchId - The batch ID to wait for
|
|
840
|
+
* @param options - Wait configuration options
|
|
841
|
+
* @param options.pollInterval - Polling interval in milliseconds (default: 10000)
|
|
842
|
+
* @param options.maxWaitTime - Maximum wait time in milliseconds (default: 7200000)
|
|
843
|
+
* @param options.onProgress - Callback for progress updates
|
|
844
|
+
* @returns Promise resolving to final batch status
|
|
845
|
+
*/
|
|
846
|
+
async _batchWaitForCompletion(batchId, options = {}) {
|
|
847
|
+
const pollInterval = options.pollInterval || 1e4;
|
|
848
|
+
const maxWaitTime = options.maxWaitTime || 72e5;
|
|
849
|
+
const startTime = Date.now();
|
|
850
|
+
while (true) {
|
|
851
|
+
const statusResponse = await this._batchStatus(batchId);
|
|
852
|
+
if (!statusResponse.success || !statusResponse.batch) {
|
|
853
|
+
throw new Error(statusResponse.error || "Failed to get batch status");
|
|
854
|
+
}
|
|
855
|
+
const batch = statusResponse.batch;
|
|
856
|
+
if (options.onProgress) {
|
|
857
|
+
options.onProgress(batch);
|
|
858
|
+
}
|
|
859
|
+
if (batch.status === "completed" || batch.status === "completed_with_errors" || batch.status === "cancelled") {
|
|
860
|
+
return batch;
|
|
861
|
+
}
|
|
862
|
+
if (Date.now() - startTime > maxWaitTime) {
|
|
863
|
+
throw new Error("Maximum wait time exceeded");
|
|
864
|
+
}
|
|
865
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
866
|
+
}
|
|
867
|
+
}
|
|
677
868
|
/**
|
|
678
869
|
* Get AI-powered answers using the Valyu Answer API
|
|
679
870
|
* @param query - The question or query string
|
|
@@ -753,7 +944,9 @@ var Valyu = class {
|
|
|
753
944
|
}
|
|
754
945
|
const validation = this.validateSources(options.includedSources);
|
|
755
946
|
if (!validation.valid) {
|
|
756
|
-
return `Invalid includedSources format. Invalid sources: ${validation.invalidSources.join(
|
|
947
|
+
return `Invalid includedSources format. Invalid sources: ${validation.invalidSources.join(
|
|
948
|
+
", "
|
|
949
|
+
)}.`;
|
|
757
950
|
}
|
|
758
951
|
}
|
|
759
952
|
if (options.excludedSources !== void 0) {
|
|
@@ -762,7 +955,9 @@ var Valyu = class {
|
|
|
762
955
|
}
|
|
763
956
|
const validation = this.validateSources(options.excludedSources);
|
|
764
957
|
if (!validation.valid) {
|
|
765
|
-
return `Invalid excludedSources format. Invalid sources: ${validation.invalidSources.join(
|
|
958
|
+
return `Invalid excludedSources format. Invalid sources: ${validation.invalidSources.join(
|
|
959
|
+
", "
|
|
960
|
+
)}.`;
|
|
766
961
|
}
|
|
767
962
|
}
|
|
768
963
|
return null;
|
|
@@ -781,12 +976,18 @@ var Valyu = class {
|
|
|
781
976
|
query: query.trim(),
|
|
782
977
|
search_type: finalSearchType
|
|
783
978
|
};
|
|
784
|
-
if (options.dataMaxPrice !== void 0)
|
|
785
|
-
|
|
786
|
-
if (options.
|
|
787
|
-
|
|
788
|
-
if (options.
|
|
789
|
-
|
|
979
|
+
if (options.dataMaxPrice !== void 0)
|
|
980
|
+
payload.data_max_price = options.dataMaxPrice;
|
|
981
|
+
if (options.structuredOutput !== void 0)
|
|
982
|
+
payload.structured_output = options.structuredOutput;
|
|
983
|
+
if (options.systemInstructions !== void 0)
|
|
984
|
+
payload.system_instructions = options.systemInstructions.trim();
|
|
985
|
+
if (options.countryCode !== void 0)
|
|
986
|
+
payload.country_code = options.countryCode;
|
|
987
|
+
if (options.includedSources !== void 0)
|
|
988
|
+
payload.included_sources = options.includedSources;
|
|
989
|
+
if (options.excludedSources !== void 0)
|
|
990
|
+
payload.excluded_sources = options.excludedSources;
|
|
790
991
|
if (options.startDate !== void 0) payload.start_date = options.startDate;
|
|
791
992
|
if (options.endDate !== void 0) payload.end_date = options.endDate;
|
|
792
993
|
if (options.fastMode !== void 0) payload.fast_mode = options.fastMode;
|
|
@@ -801,7 +1002,7 @@ var Valyu = class {
|
|
|
801
1002
|
method: "POST",
|
|
802
1003
|
headers: {
|
|
803
1004
|
...this.headers,
|
|
804
|
-
|
|
1005
|
+
Accept: "text/event-stream"
|
|
805
1006
|
},
|
|
806
1007
|
body: JSON.stringify(payload)
|
|
807
1008
|
});
|
|
@@ -854,9 +1055,20 @@ var Valyu = class {
|
|
|
854
1055
|
contents: fullContent || finalMetadata.contents || "",
|
|
855
1056
|
data_type: finalMetadata.data_type || "unstructured",
|
|
856
1057
|
search_results: finalSearchResults,
|
|
857
|
-
search_metadata: finalMetadata.search_metadata || {
|
|
858
|
-
|
|
859
|
-
|
|
1058
|
+
search_metadata: finalMetadata.search_metadata || {
|
|
1059
|
+
tx_ids: [],
|
|
1060
|
+
number_of_results: 0,
|
|
1061
|
+
total_characters: 0
|
|
1062
|
+
},
|
|
1063
|
+
ai_usage: finalMetadata.ai_usage || {
|
|
1064
|
+
input_tokens: 0,
|
|
1065
|
+
output_tokens: 0
|
|
1066
|
+
},
|
|
1067
|
+
cost: finalMetadata.cost || {
|
|
1068
|
+
total_deduction_dollars: 0,
|
|
1069
|
+
search_deduction_dollars: 0,
|
|
1070
|
+
ai_deduction_dollars: 0
|
|
1071
|
+
}
|
|
860
1072
|
};
|
|
861
1073
|
if (finalMetadata.extraction_metadata) {
|
|
862
1074
|
response2.extraction_metadata = finalMetadata.extraction_metadata;
|
|
@@ -883,13 +1095,16 @@ var Valyu = class {
|
|
|
883
1095
|
method: "POST",
|
|
884
1096
|
headers: {
|
|
885
1097
|
...this.headers,
|
|
886
|
-
|
|
1098
|
+
Accept: "text/event-stream"
|
|
887
1099
|
},
|
|
888
1100
|
body: JSON.stringify(payload)
|
|
889
1101
|
});
|
|
890
1102
|
if (!response.ok) {
|
|
891
1103
|
const errorData = await response.json().catch(() => ({}));
|
|
892
|
-
yield {
|
|
1104
|
+
yield {
|
|
1105
|
+
type: "error",
|
|
1106
|
+
error: errorData.error || `HTTP Error: ${response.status}`
|
|
1107
|
+
};
|
|
893
1108
|
return;
|
|
894
1109
|
}
|
|
895
1110
|
const reader = response.body?.getReader();
|
|
@@ -915,7 +1130,10 @@ var Valyu = class {
|
|
|
915
1130
|
try {
|
|
916
1131
|
const parsed = JSON.parse(dataStr);
|
|
917
1132
|
if (parsed.search_results && parsed.success === void 0) {
|
|
918
|
-
yield {
|
|
1133
|
+
yield {
|
|
1134
|
+
type: "search_results",
|
|
1135
|
+
search_results: parsed.search_results
|
|
1136
|
+
};
|
|
919
1137
|
} else if (parsed.choices) {
|
|
920
1138
|
const delta = parsed.choices[0]?.delta || {};
|
|
921
1139
|
const content = delta.content || "";
|