langchain 0.0.154 → 0.0.155

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.
Files changed (53) hide show
  1. package/dist/callbacks/base.d.ts +42 -28
  2. package/dist/callbacks/handlers/log_stream.cjs +283 -0
  3. package/dist/callbacks/handlers/log_stream.d.ts +99 -0
  4. package/dist/callbacks/handlers/log_stream.js +277 -0
  5. package/dist/callbacks/handlers/tracer.cjs +34 -18
  6. package/dist/callbacks/handlers/tracer.d.ts +18 -16
  7. package/dist/callbacks/handlers/tracer.js +34 -18
  8. package/dist/document_loaders/web/notionapi.cjs +8 -4
  9. package/dist/document_loaders/web/notionapi.js +8 -4
  10. package/dist/document_loaders/web/searchapi.cjs +134 -0
  11. package/dist/document_loaders/web/searchapi.d.ts +65 -0
  12. package/dist/document_loaders/web/searchapi.js +130 -0
  13. package/dist/load/import_constants.cjs +1 -0
  14. package/dist/load/import_constants.js +1 -0
  15. package/dist/load/import_map.cjs +3 -2
  16. package/dist/load/import_map.d.ts +1 -0
  17. package/dist/load/import_map.js +1 -0
  18. package/dist/schema/runnable/base.cjs +64 -5
  19. package/dist/schema/runnable/base.d.ts +13 -0
  20. package/dist/schema/runnable/base.js +64 -5
  21. package/dist/tools/index.cjs +3 -1
  22. package/dist/tools/index.d.ts +1 -0
  23. package/dist/tools/index.js +1 -0
  24. package/dist/tools/searchapi.cjs +139 -0
  25. package/dist/tools/searchapi.d.ts +64 -0
  26. package/dist/tools/searchapi.js +135 -0
  27. package/dist/util/fast-json-patch/index.cjs +48 -0
  28. package/dist/util/fast-json-patch/index.d.ts +21 -0
  29. package/dist/util/fast-json-patch/index.js +15 -0
  30. package/dist/util/fast-json-patch/src/core.cjs +469 -0
  31. package/dist/util/fast-json-patch/src/core.d.ts +111 -0
  32. package/dist/util/fast-json-patch/src/core.js +459 -0
  33. package/dist/util/fast-json-patch/src/helpers.cjs +194 -0
  34. package/dist/util/fast-json-patch/src/helpers.d.ts +36 -0
  35. package/dist/util/fast-json-patch/src/helpers.js +181 -0
  36. package/dist/util/googlevertexai-webauth.cjs +6 -2
  37. package/dist/util/googlevertexai-webauth.d.ts +1 -0
  38. package/dist/util/googlevertexai-webauth.js +6 -2
  39. package/dist/util/stream.cjs +2 -40
  40. package/dist/util/stream.d.ts +1 -2
  41. package/dist/util/stream.js +1 -38
  42. package/dist/vectorstores/pgvector.cjs +1 -1
  43. package/dist/vectorstores/pgvector.js +1 -1
  44. package/dist/vectorstores/vercel_postgres.cjs +300 -0
  45. package/dist/vectorstores/vercel_postgres.d.ts +145 -0
  46. package/dist/vectorstores/vercel_postgres.js +296 -0
  47. package/document_loaders/web/searchapi.cjs +1 -0
  48. package/document_loaders/web/searchapi.d.ts +1 -0
  49. package/document_loaders/web/searchapi.js +1 -0
  50. package/package.json +22 -1
  51. package/vectorstores/vercel_postgres.cjs +1 -0
  52. package/vectorstores/vercel_postgres.d.ts +1 -0
  53. package/vectorstores/vercel_postgres.js +1 -0
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
7
7
  const p_retry_1 = __importDefault(require("p-retry"));
8
8
  const manager_js_1 = require("../../callbacks/manager.cjs");
9
+ const log_stream_js_1 = require("../../callbacks/handlers/log_stream.cjs");
9
10
  const serializable_js_1 = require("../../load/serializable.cjs");
10
11
  const stream_js_1 = require("../../util/stream.cjs");
11
12
  const config_js_1 = require("./config.cjs");
@@ -281,7 +282,7 @@ class Runnable extends serializable_js_1.Serializable {
281
282
  async *transform(generator, options) {
282
283
  let finalChunk;
283
284
  for await (const chunk of generator) {
284
- if (!finalChunk) {
285
+ if (finalChunk === undefined) {
285
286
  finalChunk = chunk;
286
287
  }
287
288
  else {
@@ -293,6 +294,65 @@ class Runnable extends serializable_js_1.Serializable {
293
294
  }
294
295
  yield* this._streamIterator(finalChunk, options);
295
296
  }
297
+ /**
298
+ * Stream all output from a runnable, as reported to the callback system.
299
+ * This includes all inner runs of LLMs, Retrievers, Tools, etc.
300
+ * Output is streamed as Log objects, which include a list of
301
+ * jsonpatch ops that describe how the state of the run has changed in each
302
+ * step, and the final state of the run.
303
+ * The jsonpatch ops can be applied in order to construct state.
304
+ * @param input
305
+ * @param options
306
+ * @param streamOptions
307
+ */
308
+ async *streamLog(input, options, streamOptions) {
309
+ const stream = new log_stream_js_1.LogStreamCallbackHandler({
310
+ ...streamOptions,
311
+ autoClose: false,
312
+ });
313
+ const config = options ?? {};
314
+ const { callbacks } = config;
315
+ if (callbacks === undefined) {
316
+ config.callbacks = [stream];
317
+ }
318
+ else if (Array.isArray(callbacks)) {
319
+ config.callbacks = callbacks.concat([stream]);
320
+ }
321
+ else {
322
+ const copiedCallbacks = callbacks.copy();
323
+ copiedCallbacks.inheritableHandlers.push(stream);
324
+ config.callbacks = copiedCallbacks;
325
+ }
326
+ const runnableStream = await this.stream(input, config);
327
+ async function consumeRunnableStream() {
328
+ try {
329
+ for await (const chunk of runnableStream) {
330
+ const patch = new log_stream_js_1.RunLogPatch({
331
+ ops: [
332
+ {
333
+ op: "add",
334
+ path: "/streamed_output/-",
335
+ value: chunk,
336
+ },
337
+ ],
338
+ });
339
+ await stream.writer.write(patch);
340
+ }
341
+ }
342
+ finally {
343
+ await stream.writer.close();
344
+ }
345
+ }
346
+ const runnableStreamPromise = consumeRunnableStream();
347
+ try {
348
+ for await (const log of stream) {
349
+ yield log;
350
+ }
351
+ }
352
+ finally {
353
+ await runnableStreamPromise;
354
+ }
355
+ }
296
356
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
297
357
  static isRunnable(thing) {
298
358
  return thing.lc_runnable;
@@ -808,10 +868,9 @@ class RunnableMap extends Runnable {
808
868
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
809
869
  const output = {};
810
870
  try {
811
- for (const [key, runnable] of Object.entries(this.steps)) {
812
- const result = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild()));
813
- output[key] = result;
814
- }
871
+ await Promise.all(Object.entries(this.steps).map(async ([key, runnable]) => {
872
+ output[key] = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild(key)));
873
+ }));
815
874
  }
816
875
  catch (e) {
817
876
  await runManager?.handleChainError(e);
@@ -1,4 +1,5 @@
1
1
  import { CallbackManager, CallbackManagerForChainRun, BaseCallbackConfig } from "../../callbacks/manager.js";
2
+ import { LogStreamCallbackHandlerInput, RunLogPatch } from "../../callbacks/handlers/log_stream.js";
2
3
  import { Serializable } from "../../load/serializable.js";
3
4
  import { IterableReadableStream } from "../../util/stream.js";
4
5
  import { RunnableConfig } from "./config.js";
@@ -123,6 +124,18 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
123
124
  * @param options
124
125
  */
125
126
  transform(generator: AsyncGenerator<RunInput>, options: Partial<CallOptions>): AsyncGenerator<RunOutput>;
127
+ /**
128
+ * Stream all output from a runnable, as reported to the callback system.
129
+ * This includes all inner runs of LLMs, Retrievers, Tools, etc.
130
+ * Output is streamed as Log objects, which include a list of
131
+ * jsonpatch ops that describe how the state of the run has changed in each
132
+ * step, and the final state of the run.
133
+ * The jsonpatch ops can be applied in order to construct state.
134
+ * @param input
135
+ * @param options
136
+ * @param streamOptions
137
+ */
138
+ streamLog(input: RunInput, options?: Partial<CallOptions>, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<RunLogPatch>;
126
139
  static isRunnable(thing: any): thing is Runnable;
127
140
  }
128
141
  export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig> = {
@@ -1,5 +1,6 @@
1
1
  import pRetry from "p-retry";
2
2
  import { CallbackManager, } from "../../callbacks/manager.js";
3
+ import { LogStreamCallbackHandler, RunLogPatch, } from "../../callbacks/handlers/log_stream.js";
3
4
  import { Serializable } from "../../load/serializable.js";
4
5
  import { IterableReadableStream } from "../../util/stream.js";
5
6
  import { getCallbackMangerForConfig } from "./config.js";
@@ -275,7 +276,7 @@ export class Runnable extends Serializable {
275
276
  async *transform(generator, options) {
276
277
  let finalChunk;
277
278
  for await (const chunk of generator) {
278
- if (!finalChunk) {
279
+ if (finalChunk === undefined) {
279
280
  finalChunk = chunk;
280
281
  }
281
282
  else {
@@ -287,6 +288,65 @@ export class Runnable extends Serializable {
287
288
  }
288
289
  yield* this._streamIterator(finalChunk, options);
289
290
  }
291
+ /**
292
+ * Stream all output from a runnable, as reported to the callback system.
293
+ * This includes all inner runs of LLMs, Retrievers, Tools, etc.
294
+ * Output is streamed as Log objects, which include a list of
295
+ * jsonpatch ops that describe how the state of the run has changed in each
296
+ * step, and the final state of the run.
297
+ * The jsonpatch ops can be applied in order to construct state.
298
+ * @param input
299
+ * @param options
300
+ * @param streamOptions
301
+ */
302
+ async *streamLog(input, options, streamOptions) {
303
+ const stream = new LogStreamCallbackHandler({
304
+ ...streamOptions,
305
+ autoClose: false,
306
+ });
307
+ const config = options ?? {};
308
+ const { callbacks } = config;
309
+ if (callbacks === undefined) {
310
+ config.callbacks = [stream];
311
+ }
312
+ else if (Array.isArray(callbacks)) {
313
+ config.callbacks = callbacks.concat([stream]);
314
+ }
315
+ else {
316
+ const copiedCallbacks = callbacks.copy();
317
+ copiedCallbacks.inheritableHandlers.push(stream);
318
+ config.callbacks = copiedCallbacks;
319
+ }
320
+ const runnableStream = await this.stream(input, config);
321
+ async function consumeRunnableStream() {
322
+ try {
323
+ for await (const chunk of runnableStream) {
324
+ const patch = new RunLogPatch({
325
+ ops: [
326
+ {
327
+ op: "add",
328
+ path: "/streamed_output/-",
329
+ value: chunk,
330
+ },
331
+ ],
332
+ });
333
+ await stream.writer.write(patch);
334
+ }
335
+ }
336
+ finally {
337
+ await stream.writer.close();
338
+ }
339
+ }
340
+ const runnableStreamPromise = consumeRunnableStream();
341
+ try {
342
+ for await (const log of stream) {
343
+ yield log;
344
+ }
345
+ }
346
+ finally {
347
+ await runnableStreamPromise;
348
+ }
349
+ }
290
350
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
291
351
  static isRunnable(thing) {
292
352
  return thing.lc_runnable;
@@ -797,10 +857,9 @@ export class RunnableMap extends Runnable {
797
857
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
798
858
  const output = {};
799
859
  try {
800
- for (const [key, runnable] of Object.entries(this.steps)) {
801
- const result = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild()));
802
- output[key] = result;
803
- }
860
+ await Promise.all(Object.entries(this.steps).map(async ([key, runnable]) => {
861
+ output[key] = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild(key)));
862
+ }));
804
863
  }
805
864
  catch (e) {
806
865
  await runManager?.handleChainError(e);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SearxngSearch = exports.DataForSeoAPISearch = exports.WolframAlphaTool = exports.WikipediaQueryRun = exports.BraveSearch = exports.WriteFileTool = exports.ReadFileTool = exports.AIPluginTool = exports.GoogleCustomSearch = exports.Serper = exports.ZapierNLAWrapper = exports.ZapierNLARunAction = exports.VectorStoreQATool = exports.RequestsPostTool = exports.RequestsGetTool = exports.JsonGetValueTool = exports.JsonListKeysTool = exports.JsonSpec = exports.ChainTool = exports.IFTTTWebhook = exports.DynamicStructuredTool = exports.DynamicTool = exports.StructuredTool = exports.Tool = exports.BingSerpAPI = exports.DadJokeAPI = exports.SerpAPI = void 0;
3
+ exports.SearchApi = exports.SearxngSearch = exports.DataForSeoAPISearch = exports.WolframAlphaTool = exports.WikipediaQueryRun = exports.BraveSearch = exports.WriteFileTool = exports.ReadFileTool = exports.AIPluginTool = exports.GoogleCustomSearch = exports.Serper = exports.ZapierNLAWrapper = exports.ZapierNLARunAction = exports.VectorStoreQATool = exports.RequestsPostTool = exports.RequestsGetTool = exports.JsonGetValueTool = exports.JsonListKeysTool = exports.JsonSpec = exports.ChainTool = exports.IFTTTWebhook = exports.DynamicStructuredTool = exports.DynamicTool = exports.StructuredTool = exports.Tool = exports.BingSerpAPI = exports.DadJokeAPI = exports.SerpAPI = void 0;
4
4
  var serpapi_js_1 = require("./serpapi.cjs");
5
5
  Object.defineProperty(exports, "SerpAPI", { enumerable: true, get: function () { return serpapi_js_1.SerpAPI; } });
6
6
  var dadjokeapi_js_1 = require("./dadjokeapi.cjs");
@@ -48,3 +48,5 @@ var dataforseo_api_search_js_1 = require("./dataforseo_api_search.cjs");
48
48
  Object.defineProperty(exports, "DataForSeoAPISearch", { enumerable: true, get: function () { return dataforseo_api_search_js_1.DataForSeoAPISearch; } });
49
49
  var searxng_search_js_1 = require("./searxng_search.cjs");
50
50
  Object.defineProperty(exports, "SearxngSearch", { enumerable: true, get: function () { return searxng_search_js_1.SearxngSearch; } });
51
+ var searchapi_js_1 = require("./searchapi.cjs");
52
+ Object.defineProperty(exports, "SearchApi", { enumerable: true, get: function () { return searchapi_js_1.SearchApi; } });
@@ -18,3 +18,4 @@ export { WikipediaQueryRun, WikipediaQueryRunParams, } from "./wikipedia_query_r
18
18
  export { WolframAlphaTool } from "./wolframalpha.js";
19
19
  export { DataForSeoAPISearch, DataForSeoApiConfig, } from "./dataforseo_api_search.js";
20
20
  export { SearxngSearch } from "./searxng_search.js";
21
+ export { SearchApi, SearchApiParameters } from "./searchapi.js";
@@ -18,3 +18,4 @@ export { WikipediaQueryRun, } from "./wikipedia_query_run.js";
18
18
  export { WolframAlphaTool } from "./wolframalpha.js";
19
19
  export { DataForSeoAPISearch, } from "./dataforseo_api_search.js";
20
20
  export { SearxngSearch } from "./searxng_search.js";
21
+ export { SearchApi } from "./searchapi.js";
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SearchApi = void 0;
4
+ const env_js_1 = require("../util/env.cjs");
5
+ const base_js_1 = require("./base.cjs");
6
+ function isJSONObject(value) {
7
+ return value !== null && typeof value === "object" && !Array.isArray(value);
8
+ }
9
+ /**
10
+ * SearchApi Class Definition.
11
+ *
12
+ * Provides a wrapper around the SearchApi.
13
+ *
14
+ * Ensure you've set the SEARCHAPI_API_KEY environment variable for authentication.
15
+ * You can obtain a free API key from https://www.searchapi.io/.
16
+ */
17
+ class SearchApi extends base_js_1.Tool {
18
+ static lc_name() {
19
+ return "SearchApi";
20
+ }
21
+ /**
22
+ * Converts the SearchApi instance to JSON. This method is not implemented
23
+ * and will throw an error if called.
24
+ * @returns Throws an error.
25
+ */
26
+ toJSON() {
27
+ return this.toJSONNotImplemented();
28
+ }
29
+ constructor(apiKey = (0, env_js_1.getEnvironmentVariable)("SEARCHAPI_API_KEY"), params = {}) {
30
+ super(...arguments);
31
+ Object.defineProperty(this, "apiKey", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ Object.defineProperty(this, "params", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: void 0
42
+ });
43
+ Object.defineProperty(this, "name", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: "search"
48
+ });
49
+ Object.defineProperty(this, "description", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: "a search engine. useful for when you need to answer questions about current events. input should be a search query."
54
+ });
55
+ if (!apiKey) {
56
+ throw new Error("SearchApi requires an API key. Please set it as SEARCHAPI_API_KEY in your .env file, or pass it as a parameter to the SearchApi constructor.");
57
+ }
58
+ this.apiKey = apiKey;
59
+ this.params = params;
60
+ }
61
+ /**
62
+ * Builds a URL for the SearchApi request.
63
+ * @param parameters The parameters for the request.
64
+ * @returns A string representing the built URL.
65
+ */
66
+ buildUrl(searchQuery) {
67
+ const preparedParams = Object.entries({
68
+ engine: "google",
69
+ api_key: this.apiKey,
70
+ ...this.params,
71
+ q: searchQuery,
72
+ })
73
+ .filter(([key, value]) => value !== undefined && value !== null && key !== "apiKey")
74
+ .map(([key, value]) => [key, `${value}`]);
75
+ const searchParams = new URLSearchParams(preparedParams);
76
+ return `https://www.searchapi.io/api/v1/search?${searchParams}`;
77
+ }
78
+ /** @ignore */
79
+ /**
80
+ * Calls the SearchAPI.
81
+ *
82
+ * Accepts an input query and fetches the result from SearchApi.
83
+ *
84
+ * @param {string} input - Search query.
85
+ * @returns {string} - Formatted search results or an error message.
86
+ *
87
+ * NOTE: This method is the core search handler and processes various types
88
+ * of search results including Google organic results, videos, jobs, and images.
89
+ */
90
+ async _call(input) {
91
+ const resp = await fetch(this.buildUrl(input));
92
+ const json = await resp.json();
93
+ if (json.error) {
94
+ throw new Error(`Failed to load search results from SearchApi due to: ${json.error}`);
95
+ }
96
+ // Google Search results
97
+ if (json.answer_box?.answer) {
98
+ return json.answer_box.answer;
99
+ }
100
+ if (json.answer_box?.snippet) {
101
+ return json.answer_box.snippet;
102
+ }
103
+ if (json.knowledge_graph?.description) {
104
+ return json.knowledge_graph.description;
105
+ }
106
+ // Organic results (Google, Google News)
107
+ if (json.organic_results) {
108
+ const snippets = json.organic_results
109
+ .filter((r) => r.snippet)
110
+ .map((r) => r.snippet);
111
+ return snippets.join("\n");
112
+ }
113
+ // Google Jobs results
114
+ if (json.jobs) {
115
+ const jobDescriptions = json.jobs
116
+ .slice(0, 1)
117
+ .filter((r) => r.description)
118
+ .map((r) => r.description);
119
+ return jobDescriptions.join("\n");
120
+ }
121
+ // Google Videos results
122
+ if (json.videos) {
123
+ const videoInfo = json.videos
124
+ .filter((r) => r.title && r.link)
125
+ .map((r) => `Title: "${r.title}" Link: ${r.link}`);
126
+ return videoInfo.join("\n");
127
+ }
128
+ // Google Images results
129
+ if (json.images) {
130
+ const image_results = json.images.slice(0, 15);
131
+ const imageInfo = image_results
132
+ .filter((r) => r.title && r.original && isJSONObject(r.original) && r.original.link)
133
+ .map((r) => `Title: "${r.title}" Link: ${r.original.link}`);
134
+ return imageInfo.join("\n");
135
+ }
136
+ return "No good search result found";
137
+ }
138
+ }
139
+ exports.SearchApi = SearchApi;
@@ -0,0 +1,64 @@
1
+ import { Tool } from "./base.js";
2
+ type JSONPrimitive = string | number | boolean | null;
3
+ type JSONValue = JSONPrimitive | JSONObject | JSONArray;
4
+ interface JSONObject {
5
+ [key: string]: JSONValue;
6
+ }
7
+ interface JSONArray extends Array<JSONValue> {
8
+ }
9
+ /**
10
+ * SearchApiParameters Type Definition.
11
+ *
12
+ * For more parameters and supported search engines, refer specific engine documentation:
13
+ * Google - https://www.searchapi.io/docs/google
14
+ * Google News - https://www.searchapi.io/docs/google-news
15
+ * Google Scholar - https://www.searchapi.io/docs/google-scholar
16
+ * YouTube Transcripts - https://www.searchapi.io/docs/youtube-transcripts
17
+ * and others.
18
+ *
19
+ */
20
+ export type SearchApiParameters = {
21
+ [key: string]: JSONValue;
22
+ };
23
+ /**
24
+ * SearchApi Class Definition.
25
+ *
26
+ * Provides a wrapper around the SearchApi.
27
+ *
28
+ * Ensure you've set the SEARCHAPI_API_KEY environment variable for authentication.
29
+ * You can obtain a free API key from https://www.searchapi.io/.
30
+ */
31
+ export declare class SearchApi extends Tool {
32
+ static lc_name(): string;
33
+ /**
34
+ * Converts the SearchApi instance to JSON. This method is not implemented
35
+ * and will throw an error if called.
36
+ * @returns Throws an error.
37
+ */
38
+ toJSON(): import("../load/serializable.js").SerializedNotImplemented;
39
+ protected apiKey: string;
40
+ protected params: Partial<SearchApiParameters>;
41
+ constructor(apiKey?: string | undefined, params?: Partial<SearchApiParameters>);
42
+ name: string;
43
+ /**
44
+ * Builds a URL for the SearchApi request.
45
+ * @param parameters The parameters for the request.
46
+ * @returns A string representing the built URL.
47
+ */
48
+ protected buildUrl(searchQuery: string): string;
49
+ /** @ignore */
50
+ /**
51
+ * Calls the SearchAPI.
52
+ *
53
+ * Accepts an input query and fetches the result from SearchApi.
54
+ *
55
+ * @param {string} input - Search query.
56
+ * @returns {string} - Formatted search results or an error message.
57
+ *
58
+ * NOTE: This method is the core search handler and processes various types
59
+ * of search results including Google organic results, videos, jobs, and images.
60
+ */
61
+ _call(input: string): Promise<any>;
62
+ description: string;
63
+ }
64
+ export {};
@@ -0,0 +1,135 @@
1
+ import { getEnvironmentVariable } from "../util/env.js";
2
+ import { Tool } from "./base.js";
3
+ function isJSONObject(value) {
4
+ return value !== null && typeof value === "object" && !Array.isArray(value);
5
+ }
6
+ /**
7
+ * SearchApi Class Definition.
8
+ *
9
+ * Provides a wrapper around the SearchApi.
10
+ *
11
+ * Ensure you've set the SEARCHAPI_API_KEY environment variable for authentication.
12
+ * You can obtain a free API key from https://www.searchapi.io/.
13
+ */
14
+ export class SearchApi extends Tool {
15
+ static lc_name() {
16
+ return "SearchApi";
17
+ }
18
+ /**
19
+ * Converts the SearchApi instance to JSON. This method is not implemented
20
+ * and will throw an error if called.
21
+ * @returns Throws an error.
22
+ */
23
+ toJSON() {
24
+ return this.toJSONNotImplemented();
25
+ }
26
+ constructor(apiKey = getEnvironmentVariable("SEARCHAPI_API_KEY"), params = {}) {
27
+ super(...arguments);
28
+ Object.defineProperty(this, "apiKey", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ Object.defineProperty(this, "params", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ Object.defineProperty(this, "name", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: "search"
45
+ });
46
+ Object.defineProperty(this, "description", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: "a search engine. useful for when you need to answer questions about current events. input should be a search query."
51
+ });
52
+ if (!apiKey) {
53
+ throw new Error("SearchApi requires an API key. Please set it as SEARCHAPI_API_KEY in your .env file, or pass it as a parameter to the SearchApi constructor.");
54
+ }
55
+ this.apiKey = apiKey;
56
+ this.params = params;
57
+ }
58
+ /**
59
+ * Builds a URL for the SearchApi request.
60
+ * @param parameters The parameters for the request.
61
+ * @returns A string representing the built URL.
62
+ */
63
+ buildUrl(searchQuery) {
64
+ const preparedParams = Object.entries({
65
+ engine: "google",
66
+ api_key: this.apiKey,
67
+ ...this.params,
68
+ q: searchQuery,
69
+ })
70
+ .filter(([key, value]) => value !== undefined && value !== null && key !== "apiKey")
71
+ .map(([key, value]) => [key, `${value}`]);
72
+ const searchParams = new URLSearchParams(preparedParams);
73
+ return `https://www.searchapi.io/api/v1/search?${searchParams}`;
74
+ }
75
+ /** @ignore */
76
+ /**
77
+ * Calls the SearchAPI.
78
+ *
79
+ * Accepts an input query and fetches the result from SearchApi.
80
+ *
81
+ * @param {string} input - Search query.
82
+ * @returns {string} - Formatted search results or an error message.
83
+ *
84
+ * NOTE: This method is the core search handler and processes various types
85
+ * of search results including Google organic results, videos, jobs, and images.
86
+ */
87
+ async _call(input) {
88
+ const resp = await fetch(this.buildUrl(input));
89
+ const json = await resp.json();
90
+ if (json.error) {
91
+ throw new Error(`Failed to load search results from SearchApi due to: ${json.error}`);
92
+ }
93
+ // Google Search results
94
+ if (json.answer_box?.answer) {
95
+ return json.answer_box.answer;
96
+ }
97
+ if (json.answer_box?.snippet) {
98
+ return json.answer_box.snippet;
99
+ }
100
+ if (json.knowledge_graph?.description) {
101
+ return json.knowledge_graph.description;
102
+ }
103
+ // Organic results (Google, Google News)
104
+ if (json.organic_results) {
105
+ const snippets = json.organic_results
106
+ .filter((r) => r.snippet)
107
+ .map((r) => r.snippet);
108
+ return snippets.join("\n");
109
+ }
110
+ // Google Jobs results
111
+ if (json.jobs) {
112
+ const jobDescriptions = json.jobs
113
+ .slice(0, 1)
114
+ .filter((r) => r.description)
115
+ .map((r) => r.description);
116
+ return jobDescriptions.join("\n");
117
+ }
118
+ // Google Videos results
119
+ if (json.videos) {
120
+ const videoInfo = json.videos
121
+ .filter((r) => r.title && r.link)
122
+ .map((r) => `Title: "${r.title}" Link: ${r.link}`);
123
+ return videoInfo.join("\n");
124
+ }
125
+ // Google Images results
126
+ if (json.images) {
127
+ const image_results = json.images.slice(0, 15);
128
+ const imageInfo = image_results
129
+ .filter((r) => r.title && r.original && isJSONObject(r.original) && r.original.link)
130
+ .map((r) => `Title: "${r.title}" Link: ${r.original.link}`);
131
+ return imageInfo.join("\n");
132
+ }
133
+ return "No good search result found";
134
+ }
135
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
+ __setModuleDefault(result, mod);
26
+ return result;
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.unescapePathComponent = exports.escapePathComponent = exports.deepClone = exports.JsonPatchError = void 0;
30
+ __exportStar(require("./src/core.cjs"), exports);
31
+ var helpers_js_1 = require("./src/helpers.cjs");
32
+ Object.defineProperty(exports, "JsonPatchError", { enumerable: true, get: function () { return helpers_js_1.PatchError; } });
33
+ Object.defineProperty(exports, "deepClone", { enumerable: true, get: function () { return helpers_js_1._deepClone; } });
34
+ Object.defineProperty(exports, "escapePathComponent", { enumerable: true, get: function () { return helpers_js_1.escapePathComponent; } });
35
+ Object.defineProperty(exports, "unescapePathComponent", { enumerable: true, get: function () { return helpers_js_1.unescapePathComponent; } });
36
+ /**
37
+ * Default export for backwards compat
38
+ */
39
+ const core = __importStar(require("./src/core.cjs"));
40
+ const helpers_js_2 = require("./src/helpers.cjs");
41
+ exports.default = {
42
+ ...core,
43
+ // ...duplex,
44
+ JsonPatchError: helpers_js_2.PatchError,
45
+ deepClone: helpers_js_2._deepClone,
46
+ escapePathComponent: helpers_js_2.escapePathComponent,
47
+ unescapePathComponent: helpers_js_2.unescapePathComponent,
48
+ };
@@ -0,0 +1,21 @@
1
+ export * from "./src/core.js";
2
+ export { PatchError as JsonPatchError, _deepClone as deepClone, escapePathComponent, unescapePathComponent, } from "./src/helpers.js";
3
+ /**
4
+ * Default export for backwards compat
5
+ */
6
+ import * as core from "./src/core.js";
7
+ import { PatchError as JsonPatchError, _deepClone as deepClone, escapePathComponent, unescapePathComponent } from "./src/helpers.js";
8
+ declare const _default: {
9
+ JsonPatchError: typeof JsonPatchError;
10
+ deepClone: typeof deepClone;
11
+ escapePathComponent: typeof escapePathComponent;
12
+ unescapePathComponent: typeof unescapePathComponent;
13
+ getValueByPointer(document: any, pointer: string): any;
14
+ applyOperation<T>(document: T, operation: core.Operation, validateOperation?: boolean | core.Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean, index?: number): core.OperationResult<T>;
15
+ applyPatch<T_1>(document: T_1, patch: readonly core.Operation[], validateOperation?: boolean | core.Validator<T_1> | undefined, mutateDocument?: boolean, banPrototypeModifications?: boolean): core.PatchResult<T_1>;
16
+ applyReducer<T_2>(document: T_2, operation: core.Operation, index: number): T_2;
17
+ validator(operation: core.Operation, index: number, document?: any, existingPathFragment?: string | undefined): void;
18
+ validate<T_3>(sequence: readonly core.Operation[], document?: T_3 | undefined, externalValidator?: core.Validator<T_3> | undefined): JsonPatchError;
19
+ _areEquals(a: any, b: any): boolean;
20
+ };
21
+ export default _default;
@@ -0,0 +1,15 @@
1
+ export * from "./src/core.js";
2
+ export { PatchError as JsonPatchError, _deepClone as deepClone, escapePathComponent, unescapePathComponent, } from "./src/helpers.js";
3
+ /**
4
+ * Default export for backwards compat
5
+ */
6
+ import * as core from "./src/core.js";
7
+ import { PatchError as JsonPatchError, _deepClone as deepClone, escapePathComponent, unescapePathComponent, } from "./src/helpers.js";
8
+ export default {
9
+ ...core,
10
+ // ...duplex,
11
+ JsonPatchError,
12
+ deepClone,
13
+ escapePathComponent,
14
+ unescapePathComponent,
15
+ };