@langchain/classic 1.0.33 → 1.0.35
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/CHANGELOG.md +14 -0
- package/dist/agents/chat/index.cjs.map +1 -1
- package/dist/agents/chat/index.js.map +1 -1
- package/dist/agents/chat/outputParser.cjs.map +1 -1
- package/dist/agents/chat/outputParser.js.map +1 -1
- package/dist/agents/chat_convo/index.cjs.map +1 -1
- package/dist/agents/chat_convo/index.js.map +1 -1
- package/dist/agents/chat_convo/outputParser.cjs.map +1 -1
- package/dist/agents/chat_convo/outputParser.js.map +1 -1
- package/dist/agents/executor.cjs.map +1 -1
- package/dist/agents/executor.js.map +1 -1
- package/dist/agents/format_scratchpad/log.cjs.map +1 -1
- package/dist/agents/format_scratchpad/log.js.map +1 -1
- package/dist/agents/format_scratchpad/log_to_message.cjs.map +1 -1
- package/dist/agents/format_scratchpad/log_to_message.js.map +1 -1
- package/dist/agents/initialize.cjs.map +1 -1
- package/dist/agents/initialize.js.map +1 -1
- package/dist/agents/mrkl/index.cjs.map +1 -1
- package/dist/agents/mrkl/index.js.map +1 -1
- package/dist/agents/mrkl/outputParser.cjs.map +1 -1
- package/dist/agents/mrkl/outputParser.js.map +1 -1
- package/dist/agents/openai_functions/index.cjs.map +1 -1
- package/dist/agents/openai_functions/index.js.map +1 -1
- package/dist/agents/openai_tools/index.cjs.map +1 -1
- package/dist/agents/openai_tools/index.js.map +1 -1
- package/dist/agents/react/index.cjs.map +1 -1
- package/dist/agents/react/index.js.map +1 -1
- package/dist/agents/react/output_parser.cjs.map +1 -1
- package/dist/agents/react/output_parser.js.map +1 -1
- package/dist/agents/structured_chat/index.cjs.map +1 -1
- package/dist/agents/structured_chat/index.js.map +1 -1
- package/dist/agents/structured_chat/outputParser.cjs.map +1 -1
- package/dist/agents/structured_chat/outputParser.js.map +1 -1
- package/dist/agents/tool_calling/index.cjs.map +1 -1
- package/dist/agents/tool_calling/index.js.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/openai_functions.cjs.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/openai_functions.js.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.cjs.map +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.js.map +1 -1
- package/dist/agents/toolkits/json/json.cjs.map +1 -1
- package/dist/agents/toolkits/json/json.js.map +1 -1
- package/dist/agents/toolkits/openapi/openapi.cjs.map +1 -1
- package/dist/agents/toolkits/openapi/openapi.js.map +1 -1
- package/dist/agents/toolkits/sql/sql.cjs.map +1 -1
- package/dist/agents/toolkits/sql/sql.js.map +1 -1
- package/dist/agents/toolkits/vectorstore/vectorstore.cjs.map +1 -1
- package/dist/agents/toolkits/vectorstore/vectorstore.js.map +1 -1
- package/dist/agents/xml/index.cjs.map +1 -1
- package/dist/agents/xml/index.js.map +1 -1
- package/dist/chains/analyze_documents_chain.cjs.map +1 -1
- package/dist/chains/analyze_documents_chain.js.map +1 -1
- package/dist/chains/api/api_chain.cjs.map +1 -1
- package/dist/chains/api/api_chain.js.map +1 -1
- package/dist/chains/api/prompts.js.map +1 -1
- package/dist/chains/base.cjs.map +1 -1
- package/dist/chains/base.js.map +1 -1
- package/dist/chains/chat_vector_db_chain.cjs.map +1 -1
- package/dist/chains/chat_vector_db_chain.js.map +1 -1
- package/dist/chains/combine_docs_chain.cjs.map +1 -1
- package/dist/chains/combine_docs_chain.js.map +1 -1
- package/dist/chains/combine_documents/base.cjs.map +1 -1
- package/dist/chains/combine_documents/base.js.map +1 -1
- package/dist/chains/combine_documents/reduce.cjs.map +1 -1
- package/dist/chains/combine_documents/reduce.js.map +1 -1
- package/dist/chains/constitutional_ai/constitutional_prompts.cjs.map +1 -1
- package/dist/chains/constitutional_ai/constitutional_prompts.js.map +1 -1
- package/dist/chains/conversational_retrieval_chain.cjs.map +1 -1
- package/dist/chains/conversational_retrieval_chain.js.map +1 -1
- package/dist/chains/graph_qa/cypher.cjs.map +1 -1
- package/dist/chains/graph_qa/cypher.js.map +1 -1
- package/dist/chains/graph_qa/prompts.cjs.map +1 -1
- package/dist/chains/graph_qa/prompts.js.map +1 -1
- package/dist/chains/history_aware_retriever.cjs.map +1 -1
- package/dist/chains/history_aware_retriever.js.map +1 -1
- package/dist/chains/llm_chain.cjs.map +1 -1
- package/dist/chains/llm_chain.js.map +1 -1
- package/dist/chains/openai_functions/base.cjs.map +1 -1
- package/dist/chains/openai_functions/base.js.map +1 -1
- package/dist/chains/openai_functions/openapi.cjs.map +1 -1
- package/dist/chains/openai_functions/openapi.js.map +1 -1
- package/dist/chains/query_constructor/index.cjs.map +1 -1
- package/dist/chains/query_constructor/index.js.map +1 -1
- package/dist/chains/query_constructor/parser.cjs.map +1 -1
- package/dist/chains/query_constructor/parser.js.map +1 -1
- package/dist/chains/query_constructor/prompt.cjs.map +1 -1
- package/dist/chains/query_constructor/prompt.js.map +1 -1
- package/dist/chains/question_answering/load.cjs.map +1 -1
- package/dist/chains/question_answering/load.js.map +1 -1
- package/dist/chains/question_answering/map_reduce_prompts.cjs.map +1 -1
- package/dist/chains/question_answering/map_reduce_prompts.js.map +1 -1
- package/dist/chains/question_answering/refine_prompts.cjs.map +1 -1
- package/dist/chains/question_answering/refine_prompts.js.map +1 -1
- package/dist/chains/question_answering/stuff_prompts.cjs.map +1 -1
- package/dist/chains/question_answering/stuff_prompts.js.map +1 -1
- package/dist/chains/retrieval.cjs.map +1 -1
- package/dist/chains/retrieval.js.map +1 -1
- package/dist/chains/router/multi_prompt.cjs.map +1 -1
- package/dist/chains/router/multi_prompt.js.map +1 -1
- package/dist/chains/router/multi_retrieval_qa.cjs.map +1 -1
- package/dist/chains/router/multi_retrieval_qa.js.map +1 -1
- package/dist/chains/sql_db/sql_db_chain.cjs.map +1 -1
- package/dist/chains/sql_db/sql_db_chain.js.map +1 -1
- package/dist/chains/summarization/load.cjs.map +1 -1
- package/dist/chains/summarization/load.js.map +1 -1
- package/dist/chains/summarization/refine_prompts.cjs.map +1 -1
- package/dist/chains/summarization/refine_prompts.js.map +1 -1
- package/dist/chains/summarization/stuff_prompts.cjs.map +1 -1
- package/dist/chains/summarization/stuff_prompts.js.map +1 -1
- package/dist/chat_models/universal.cjs.map +1 -1
- package/dist/chat_models/universal.js.map +1 -1
- package/dist/document_loaders/fs/json.cjs.map +1 -1
- package/dist/document_loaders/fs/json.js.map +1 -1
- package/dist/document_loaders/fs/multi_file.cjs.map +1 -1
- package/dist/document_loaders/fs/multi_file.js.map +1 -1
- package/dist/document_transformers/openai_functions.cjs.map +1 -1
- package/dist/document_transformers/openai_functions.js.map +1 -1
- package/dist/evaluation/agents/prompt.cjs.map +1 -1
- package/dist/evaluation/agents/prompt.js.map +1 -1
- package/dist/evaluation/comparison/pairwise.cjs.map +1 -1
- package/dist/evaluation/comparison/pairwise.js.map +1 -1
- package/dist/evaluation/comparison/prompt.cjs.map +1 -1
- package/dist/evaluation/comparison/prompt.js.map +1 -1
- package/dist/evaluation/criteria/criteria.cjs.map +1 -1
- package/dist/evaluation/criteria/criteria.js.map +1 -1
- package/dist/evaluation/criteria/prompt.cjs.map +1 -1
- package/dist/evaluation/criteria/prompt.js.map +1 -1
- package/dist/evaluation/embedding_distance/base.cjs.map +1 -1
- package/dist/evaluation/embedding_distance/base.js.map +1 -1
- package/dist/evaluation/qa/eval_chain.cjs.map +1 -1
- package/dist/evaluation/qa/eval_chain.js.map +1 -1
- package/dist/evaluation/qa/prompt.cjs.map +1 -1
- package/dist/evaluation/qa/prompt.js.map +1 -1
- package/dist/experimental/autogpt/agent.cjs.map +1 -1
- package/dist/experimental/autogpt/agent.js.map +1 -1
- package/dist/experimental/autogpt/prompt.cjs.map +1 -1
- package/dist/experimental/autogpt/prompt.js.map +1 -1
- package/dist/experimental/autogpt/prompt_generator.cjs.map +1 -1
- package/dist/experimental/autogpt/prompt_generator.js.map +1 -1
- package/dist/experimental/babyagi/agent.cjs.map +1 -1
- package/dist/experimental/babyagi/agent.js.map +1 -1
- package/dist/experimental/babyagi/task_creation.cjs.map +1 -1
- package/dist/experimental/babyagi/task_creation.js.map +1 -1
- package/dist/experimental/babyagi/task_execution.cjs.map +1 -1
- package/dist/experimental/babyagi/task_execution.js.map +1 -1
- package/dist/experimental/babyagi/task_prioritization.cjs.map +1 -1
- package/dist/experimental/babyagi/task_prioritization.js.map +1 -1
- package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.cjs.map +1 -1
- package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.js.map +1 -1
- package/dist/experimental/generative_agents/generative_agent.cjs.map +1 -1
- package/dist/experimental/generative_agents/generative_agent.js.map +1 -1
- package/dist/experimental/generative_agents/generative_agent_memory.cjs.map +1 -1
- package/dist/experimental/generative_agents/generative_agent_memory.js.map +1 -1
- package/dist/experimental/masking/regex_masking_transformer.cjs.map +1 -1
- package/dist/experimental/masking/regex_masking_transformer.js.map +1 -1
- package/dist/experimental/openai_assistant/index.cjs.map +1 -1
- package/dist/experimental/openai_assistant/index.js.map +1 -1
- package/dist/experimental/plan_and_execute/agent_executor.cjs.map +1 -1
- package/dist/experimental/plan_and_execute/agent_executor.js.map +1 -1
- package/dist/experimental/plan_and_execute/base.cjs.map +1 -1
- package/dist/experimental/plan_and_execute/base.js.map +1 -1
- package/dist/experimental/prompts/handlebars.cjs.map +1 -1
- package/dist/experimental/prompts/handlebars.js.map +1 -1
- package/dist/hub/base.cjs.map +1 -1
- package/dist/hub/base.js.map +1 -1
- package/dist/hub/index.cjs.map +1 -1
- package/dist/hub/index.js.map +1 -1
- package/dist/hub/node.cjs.map +1 -1
- package/dist/hub/node.js.map +1 -1
- package/dist/memory/buffer_memory.cjs.map +1 -1
- package/dist/memory/buffer_memory.js.map +1 -1
- package/dist/memory/buffer_token_memory.cjs.map +1 -1
- package/dist/memory/buffer_token_memory.js.map +1 -1
- package/dist/memory/buffer_window_memory.cjs.map +1 -1
- package/dist/memory/buffer_window_memory.js.map +1 -1
- package/dist/memory/entity_memory.cjs.map +1 -1
- package/dist/memory/entity_memory.js.map +1 -1
- package/dist/memory/prompt.cjs.map +1 -1
- package/dist/memory/prompt.js.map +1 -1
- package/dist/memory/summary.cjs.map +1 -1
- package/dist/memory/summary.js.map +1 -1
- package/dist/output_parsers/expression.cjs.map +1 -1
- package/dist/output_parsers/expression.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/base.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/factory.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/factory.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/identifier_handler.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js.map +1 -1
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs.map +1 -1
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.js.map +1 -1
- package/dist/output_parsers/fix.cjs.map +1 -1
- package/dist/output_parsers/fix.js.map +1 -1
- package/dist/output_parsers/http_response.cjs.map +1 -1
- package/dist/output_parsers/http_response.js.map +1 -1
- package/dist/output_parsers/openai_functions.cjs.map +1 -1
- package/dist/output_parsers/openai_functions.js.map +1 -1
- package/dist/output_parsers/openai_tools.cjs.map +1 -1
- package/dist/output_parsers/openai_tools.js.map +1 -1
- package/dist/output_parsers/prompts.cjs.map +1 -1
- package/dist/output_parsers/prompts.js.map +1 -1
- package/dist/output_parsers/structured.cjs.map +1 -1
- package/dist/output_parsers/structured.js.map +1 -1
- package/dist/retrievers/contextual_compression.cjs.map +1 -1
- package/dist/retrievers/contextual_compression.js.map +1 -1
- package/dist/retrievers/document_compressors/chain_extract.cjs.map +1 -1
- package/dist/retrievers/document_compressors/chain_extract.js.map +1 -1
- package/dist/retrievers/ensemble.cjs.map +1 -1
- package/dist/retrievers/ensemble.js.map +1 -1
- package/dist/retrievers/hyde.cjs.map +1 -1
- package/dist/retrievers/hyde.js.map +1 -1
- package/dist/retrievers/multi_query.cjs.map +1 -1
- package/dist/retrievers/multi_query.js.map +1 -1
- package/dist/retrievers/multi_vector.cjs.map +1 -1
- package/dist/retrievers/multi_vector.js.map +1 -1
- package/dist/retrievers/parent_document.cjs.map +1 -1
- package/dist/retrievers/parent_document.js.map +1 -1
- package/dist/retrievers/score_threshold.cjs.map +1 -1
- package/dist/retrievers/score_threshold.js.map +1 -1
- package/dist/smith/runner_utils.cjs.map +1 -1
- package/dist/smith/runner_utils.js.map +1 -1
- package/dist/storage/encoder_backed.cjs.map +1 -1
- package/dist/storage/encoder_backed.js.map +1 -1
- package/dist/storage/file_system.cjs.map +1 -1
- package/dist/storage/file_system.js.map +1 -1
- package/dist/tools/json.cjs.map +1 -1
- package/dist/tools/json.js.map +1 -1
- package/dist/tools/requests.cjs.map +1 -1
- package/dist/tools/requests.js.map +1 -1
- package/dist/tools/retriever.cjs.map +1 -1
- package/dist/tools/retriever.js.map +1 -1
- package/dist/tools/sql.cjs.map +1 -1
- package/dist/tools/sql.js.map +1 -1
- package/dist/tools/webbrowser.cjs.map +1 -1
- package/dist/tools/webbrowser.js.map +1 -1
- package/dist/util/hub.cjs.map +1 -1
- package/dist/util/hub.js.map +1 -1
- package/dist/util/load.cjs.map +1 -1
- package/dist/util/load.js.map +1 -1
- package/dist/util/openapi.cjs.map +1 -1
- package/dist/util/openapi.js.map +1 -1
- package/dist/util/sql_utils.cjs.map +1 -1
- package/dist/util/sql_utils.js.map +1 -1
- package/dist/vectorstores/memory.cjs.map +1 -1
- package/dist/vectorstores/memory.js.map +1 -1
- package/package.json +48 -49
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requests.js","names":[],"sources":["../../src/tools/requests.ts"],"sourcesContent":["import { Tool } from \"@langchain/core/tools\";\n\nexport interface Headers {\n [key: string]: string;\n}\n\n/**\n * Interface for HTTP request tools. Contains properties for headers and\n * maximum output length.\n */\nexport interface RequestTool {\n headers: Headers;\n maxOutputLength: number;\n}\n\n/**\n * Class for making GET requests. Extends the Tool class and implements\n * the RequestTool interface. The input should be a URL string, and the\n * output will be the text response of the GET request.\n */\nexport class RequestsGetTool extends Tool implements RequestTool {\n static lc_name() {\n return \"RequestsGetTool\";\n }\n\n name = \"requests_get\";\n\n maxOutputLength = 2000;\n\n constructor(\n public headers: Headers = {},\n { maxOutputLength }: { maxOutputLength?: number } = {}\n ) {\n super(...arguments);\n\n this.maxOutputLength = maxOutputLength ?? this.maxOutputLength;\n }\n\n /** @ignore */\n async _call(input: string) {\n const res = await fetch(input, {\n headers: this.headers,\n });\n const text = await res.text();\n return text.slice(0, this.maxOutputLength);\n }\n\n description = `A portal to the internet. Use this when you need to get specific content from a website.\n Input should be a url string (i.e. \"https://www.google.com\"). The output will be the text response of the GET request.`;\n}\n\n/**\n * Class for making POST requests. Extends the Tool class and implements\n * the RequestTool interface. The input should be a JSON string with two\n * keys: 'url' and 'data'. The output will be the text response of the\n * POST request.\n */\nexport class RequestsPostTool extends Tool implements RequestTool {\n static lc_name() {\n return \"RequestsPostTool\";\n }\n\n name = \"requests_post\";\n\n maxOutputLength = Infinity;\n\n constructor(\n public headers: Headers = {},\n { maxOutputLength }: { maxOutputLength?: number } = {}\n ) {\n super(...arguments);\n\n this.maxOutputLength = maxOutputLength ?? this.maxOutputLength;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n const { url, data } = JSON.parse(input);\n const res = await fetch(url, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(data),\n });\n const text = await res.text();\n return text.slice(0, this.maxOutputLength);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Use this when you want to POST to a website.\n Input should be a json string with two keys: \"url\" and \"data\".\n The value of \"url\" should be a string, and the value of \"data\" should be a dictionary of\n key-value pairs you want to POST to the url as a JSON body.\n Be careful to always use double quotes for strings in the json string\n The output will be the text response of the POST request.`;\n}\n"],"mappings":";;;;;;;AAoBA,IAAa,kBAAb,cAAqC,KAA4B;CAC/D,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP,kBAAkB;CAElB,YACE,UAA0B,EAAE,EAC5B,EAAE,oBAAkD,EAAE,EACtD;AACA,QAAM,GAAG,UAAU;AAHZ,OAAA,UAAA;AAKP,OAAK,kBAAkB,mBAAmB,KAAK;;;CAIjD,MAAM,MAAM,OAAe;AAKzB,
|
|
1
|
+
{"version":3,"file":"requests.js","names":[],"sources":["../../src/tools/requests.ts"],"sourcesContent":["import { Tool } from \"@langchain/core/tools\";\n\nexport interface Headers {\n [key: string]: string;\n}\n\n/**\n * Interface for HTTP request tools. Contains properties for headers and\n * maximum output length.\n */\nexport interface RequestTool {\n headers: Headers;\n maxOutputLength: number;\n}\n\n/**\n * Class for making GET requests. Extends the Tool class and implements\n * the RequestTool interface. The input should be a URL string, and the\n * output will be the text response of the GET request.\n */\nexport class RequestsGetTool extends Tool implements RequestTool {\n static lc_name() {\n return \"RequestsGetTool\";\n }\n\n name = \"requests_get\";\n\n maxOutputLength = 2000;\n\n constructor(\n public headers: Headers = {},\n { maxOutputLength }: { maxOutputLength?: number } = {}\n ) {\n super(...arguments);\n\n this.maxOutputLength = maxOutputLength ?? this.maxOutputLength;\n }\n\n /** @ignore */\n async _call(input: string) {\n const res = await fetch(input, {\n headers: this.headers,\n });\n const text = await res.text();\n return text.slice(0, this.maxOutputLength);\n }\n\n description = `A portal to the internet. Use this when you need to get specific content from a website.\n Input should be a url string (i.e. \"https://www.google.com\"). The output will be the text response of the GET request.`;\n}\n\n/**\n * Class for making POST requests. Extends the Tool class and implements\n * the RequestTool interface. The input should be a JSON string with two\n * keys: 'url' and 'data'. The output will be the text response of the\n * POST request.\n */\nexport class RequestsPostTool extends Tool implements RequestTool {\n static lc_name() {\n return \"RequestsPostTool\";\n }\n\n name = \"requests_post\";\n\n maxOutputLength = Infinity;\n\n constructor(\n public headers: Headers = {},\n { maxOutputLength }: { maxOutputLength?: number } = {}\n ) {\n super(...arguments);\n\n this.maxOutputLength = maxOutputLength ?? this.maxOutputLength;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n const { url, data } = JSON.parse(input);\n const res = await fetch(url, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(data),\n });\n const text = await res.text();\n return text.slice(0, this.maxOutputLength);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Use this when you want to POST to a website.\n Input should be a json string with two keys: \"url\" and \"data\".\n The value of \"url\" should be a string, and the value of \"data\" should be a dictionary of\n key-value pairs you want to POST to the url as a JSON body.\n Be careful to always use double quotes for strings in the json string\n The output will be the text response of the POST request.`;\n}\n"],"mappings":";;;;;;;AAoBA,IAAa,kBAAb,cAAqC,KAA4B;CAC/D,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP,kBAAkB;CAElB,YACE,UAA0B,EAAE,EAC5B,EAAE,oBAAkD,EAAE,EACtD;AACA,QAAM,GAAG,UAAU;AAHZ,OAAA,UAAA;AAKP,OAAK,kBAAkB,mBAAmB,KAAK;;;CAIjD,MAAM,MAAM,OAAe;AAKzB,UAAO,OADY,MAHD,MAAM,OAAO,EAC7B,SAAS,KAAK,SACf,CAAC,EACqB,MAAM,EACjB,MAAM,GAAG,KAAK,gBAAgB;;CAG5C,cAAc;;;;;;;;;AAUhB,IAAa,mBAAb,cAAsC,KAA4B;CAChE,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP,kBAAkB;CAElB,YACE,UAA0B,EAAE,EAC5B,EAAE,oBAAkD,EAAE,EACtD;AACA,QAAM,GAAG,UAAU;AAHZ,OAAA,UAAA;AAKP,OAAK,kBAAkB,mBAAmB,KAAK;;;CAIjD,MAAM,MAAM,OAAe;AACzB,MAAI;GACF,MAAM,EAAE,KAAK,SAAS,KAAK,MAAM,MAAM;AAOvC,WAAO,OADY,MALD,MAAM,KAAK;IAC3B,QAAQ;IACR,SAAS,KAAK;IACd,MAAM,KAAK,UAAU,KAAK;IAC3B,CAAC,EACqB,MAAM,EACjB,MAAM,GAAG,KAAK,gBAAgB;WACnC,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retriever.cjs","names":["formatDocumentsAsString","z","DynamicStructuredTool"],"sources":["../../src/tools/retriever.ts"],"sourcesContent":["import type { BaseRetrieverInterface } from \"@langchain/core/retrievers\";\nimport { z } from \"zod/v3\";\nimport { CallbackManagerForToolRun } from \"@langchain/core/callbacks/manager\";\nimport {\n DynamicStructuredTool,\n type DynamicStructuredToolInput,\n} from \"@langchain/core/tools\";\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport function createRetrieverTool(\n retriever: BaseRetrieverInterface,\n input: Omit<DynamicStructuredToolInput, \"func\" | \"schema\">\n) {\n const func = async (\n { query }: { query: string },\n runManager?: CallbackManagerForToolRun\n ) => {\n const docs = await retriever.invoke(\n query,\n runManager?.getChild(\"retriever\")\n );\n return formatDocumentsAsString(docs);\n };\n const schema = z.object({\n query: z.string().describe(\"query to look up in retriever\"),\n });\n return new DynamicStructuredTool({ ...input, func, schema });\n}\n"],"mappings":";;;;;;;AASA,SAAgB,oBACd,WACA,OACA;CACA,MAAM,OAAO,OACX,EAAE,SACF,eACG;AAKH,SAAOA,sBAAAA,
|
|
1
|
+
{"version":3,"file":"retriever.cjs","names":["formatDocumentsAsString","z","DynamicStructuredTool"],"sources":["../../src/tools/retriever.ts"],"sourcesContent":["import type { BaseRetrieverInterface } from \"@langchain/core/retrievers\";\nimport { z } from \"zod/v3\";\nimport { CallbackManagerForToolRun } from \"@langchain/core/callbacks/manager\";\nimport {\n DynamicStructuredTool,\n type DynamicStructuredToolInput,\n} from \"@langchain/core/tools\";\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport function createRetrieverTool(\n retriever: BaseRetrieverInterface,\n input: Omit<DynamicStructuredToolInput, \"func\" | \"schema\">\n) {\n const func = async (\n { query }: { query: string },\n runManager?: CallbackManagerForToolRun\n ) => {\n const docs = await retriever.invoke(\n query,\n runManager?.getChild(\"retriever\")\n );\n return formatDocumentsAsString(docs);\n };\n const schema = z.object({\n query: z.string().describe(\"query to look up in retriever\"),\n });\n return new DynamicStructuredTool({ ...input, func, schema });\n}\n"],"mappings":";;;;;;;AASA,SAAgB,oBACd,WACA,OACA;CACA,MAAM,OAAO,OACX,EAAE,SACF,eACG;AAKH,SAAOA,sBAAAA,wBAAwB,MAJZ,UAAU,OAC3B,OACA,YAAY,SAAS,YAAY,CAClC,CACmC;;CAEtC,MAAM,SAASC,OAAAA,EAAE,OAAO,EACtB,OAAOA,OAAAA,EAAE,QAAQ,CAAC,SAAS,gCAAgC,EAC5D,CAAC;AACF,QAAO,IAAIC,sBAAAA,sBAAsB;EAAE,GAAG;EAAO;EAAM;EAAQ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retriever.js","names":[],"sources":["../../src/tools/retriever.ts"],"sourcesContent":["import type { BaseRetrieverInterface } from \"@langchain/core/retrievers\";\nimport { z } from \"zod/v3\";\nimport { CallbackManagerForToolRun } from \"@langchain/core/callbacks/manager\";\nimport {\n DynamicStructuredTool,\n type DynamicStructuredToolInput,\n} from \"@langchain/core/tools\";\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport function createRetrieverTool(\n retriever: BaseRetrieverInterface,\n input: Omit<DynamicStructuredToolInput, \"func\" | \"schema\">\n) {\n const func = async (\n { query }: { query: string },\n runManager?: CallbackManagerForToolRun\n ) => {\n const docs = await retriever.invoke(\n query,\n runManager?.getChild(\"retriever\")\n );\n return formatDocumentsAsString(docs);\n };\n const schema = z.object({\n query: z.string().describe(\"query to look up in retriever\"),\n });\n return new DynamicStructuredTool({ ...input, func, schema });\n}\n"],"mappings":";;;;;;AASA,SAAgB,oBACd,WACA,OACA;CACA,MAAM,OAAO,OACX,EAAE,SACF,eACG;AAKH,SAAO,
|
|
1
|
+
{"version":3,"file":"retriever.js","names":[],"sources":["../../src/tools/retriever.ts"],"sourcesContent":["import type { BaseRetrieverInterface } from \"@langchain/core/retrievers\";\nimport { z } from \"zod/v3\";\nimport { CallbackManagerForToolRun } from \"@langchain/core/callbacks/manager\";\nimport {\n DynamicStructuredTool,\n type DynamicStructuredToolInput,\n} from \"@langchain/core/tools\";\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport function createRetrieverTool(\n retriever: BaseRetrieverInterface,\n input: Omit<DynamicStructuredToolInput, \"func\" | \"schema\">\n) {\n const func = async (\n { query }: { query: string },\n runManager?: CallbackManagerForToolRun\n ) => {\n const docs = await retriever.invoke(\n query,\n runManager?.getChild(\"retriever\")\n );\n return formatDocumentsAsString(docs);\n };\n const schema = z.object({\n query: z.string().describe(\"query to look up in retriever\"),\n });\n return new DynamicStructuredTool({ ...input, func, schema });\n}\n"],"mappings":";;;;;;AASA,SAAgB,oBACd,WACA,OACA;CACA,MAAM,OAAO,OACX,EAAE,SACF,eACG;AAKH,SAAO,wBAAwB,MAJZ,UAAU,OAC3B,OACA,YAAY,SAAS,YAAY,CAClC,CACmC;;CAEtC,MAAM,SAAS,EAAE,OAAO,EACtB,OAAO,EAAE,QAAQ,CAAC,SAAS,gCAAgC,EAC5D,CAAC;AACF,QAAO,IAAI,sBAAsB;EAAE,GAAG;EAAO;EAAM;EAAQ,CAAC"}
|
package/dist/tools/sql.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql.cjs","names":["Tool","PromptTemplate","LLMChain"],"sources":["../../src/tools/sql.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Tool } from \"@langchain/core/tools\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { LLMChain } from \"../chains/llm_chain.js\";\nimport type { SqlDatabase } from \"../sql_db.js\";\nimport { SqlTable } from \"../util/sql_utils.js\";\n\n/**\n * Interface for SQL tools. It has a `db` property which is a SQL\n * database.\n */\ninterface SqlTool {\n db: SqlDatabase;\n}\n\n/**\n * A tool for executing SQL queries. It takes a SQL database as a\n * parameter and assigns it to the `db` property. The `_call` method is\n * used to run the SQL query and return the result. If the query is\n * incorrect, an error message is returned.\n */\nexport class QuerySqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"QuerySqlTool\";\n }\n\n name = \"query-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super(...arguments);\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n return await this.db.run(input);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a detailed and correct SQL query, output is a result from the database.\n If the query is not correct, an error message will be returned.\n If an error is returned, rewrite the query, check the query, and try again.`;\n}\n\n/**\n * A tool for retrieving information about SQL tables. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to retrieve the schema and sample rows for the\n * specified tables. If the tables do not exist, an error message is\n * returned.\n */\nexport class InfoSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"InfoSqlTool\";\n }\n\n name = \"info-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n const tables = input.split(\",\").map((table) => table.trim());\n return await this.db.getTableInfo(tables);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\n Be sure that the tables actually exist by calling list-tables-sql first!\n\n Example Input: \"table1, table2, table3.`;\n}\n\n/**\n * A tool for listing all tables in a SQL database. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to return a comma-separated list of all tables\n * in the database.\n */\nexport class ListTablesSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"ListTablesSqlTool\";\n }\n\n name = \"list-tables-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n async _call(_: string) {\n try {\n let selectedTables: SqlTable[] = this.db.allTables;\n\n if (this.db.includesTables.length > 0) {\n selectedTables = selectedTables.filter((currentTable) =>\n this.db.includesTables.includes(currentTable.tableName)\n );\n }\n\n if (this.db.ignoreTables.length > 0) {\n selectedTables = selectedTables.filter(\n (currentTable) =>\n !this.db.ignoreTables.includes(currentTable.tableName)\n );\n }\n\n const tables = selectedTables.map((table: SqlTable) => table.tableName);\n return tables.join(\", \");\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input is an empty string, output is a comma-separated list of tables in the database.`;\n}\n\n/**\n * Arguments for the QueryCheckerTool class.\n */\ntype QueryCheckerToolArgs = {\n llmChain?: LLMChain;\n llm: BaseLanguageModelInterface;\n _chainType?: never;\n};\n\n/**\n * A tool for checking SQL queries for common mistakes. It takes a\n * LLMChain or QueryCheckerToolArgs as a parameter. The `_call` method is\n * used to check the input query for common mistakes and returns a\n * prediction.\n */\nexport class QueryCheckerTool extends Tool {\n static lc_name() {\n return \"QueryCheckerTool\";\n }\n\n name = \"query-checker\";\n\n template = `\n {query}\nDouble check the SQL query above for common mistakes, including:\n- Using NOT IN with NULL values\n- Using UNION when UNION ALL should have been used\n- Using BETWEEN for exclusive ranges\n- Data type mismatch in predicates\n- Properly quoting identifiers\n- Using the correct number of arguments for functions\n- Casting to the correct data type\n- Using the proper columns for joins\n\nIf there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.`;\n\n llmChain: LLMChain;\n\n constructor(llmChainOrOptions?: LLMChain | QueryCheckerToolArgs) {\n super();\n if (typeof llmChainOrOptions?._chainType === \"function\") {\n this.llmChain = llmChainOrOptions as LLMChain;\n } else {\n const options = llmChainOrOptions as QueryCheckerToolArgs;\n if (options?.llmChain !== undefined) {\n this.llmChain = options.llmChain;\n } else {\n const prompt = new PromptTemplate({\n template: this.template,\n inputVariables: [\"query\"],\n });\n const llm = options.llm;\n this.llmChain = new LLMChain({ llm, prompt });\n }\n }\n }\n\n /** @ignore */\n async _call(input: string) {\n return this.llmChain.predict({ query: input });\n }\n\n description = `Use this tool to double check if your query is correct before executing it.\n Always use this tool before executing a query with query-sql!`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqBA,IAAa,eAAb,cAAkCA,sBAAAA,KAAwB;CACxD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,QAAM,GAAG,UAAU;AACnB,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;AACF,UAAO,MAAM,KAAK,GAAG,IAAI,MAAM;WACxB,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,cAAb,cAAiCA,sBAAAA,KAAwB;CACvD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;GACF,MAAM,SAAS,MAAM,MAAM,IAAI,CAAC,KAAK,UAAU,MAAM,MAAM,CAAC;AAC5D,UAAO,MAAM,KAAK,GAAG,aAAa,OAAO;WAClC,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,oBAAb,cAAuCA,sBAAAA,KAAwB;CAC7D,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;CAGZ,MAAM,MAAM,GAAW;AACrB,MAAI;GACF,IAAI,iBAA6B,KAAK,GAAG;AAEzC,OAAI,KAAK,GAAG,eAAe,SAAS,EAClC,kBAAiB,eAAe,QAAQ,iBACtC,KAAK,GAAG,eAAe,SAAS,aAAa,UAAU,CACxD;AAGH,OAAI,KAAK,GAAG,aAAa,SAAS,EAChC,kBAAiB,eAAe,QAC7B,iBACC,CAAC,KAAK,GAAG,aAAa,SAAS,aAAa,UAAU,CACzD;AAIH,UADe,eAAe,KAAK,UAAoB,MAAM,
|
|
1
|
+
{"version":3,"file":"sql.cjs","names":["Tool","PromptTemplate","LLMChain"],"sources":["../../src/tools/sql.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Tool } from \"@langchain/core/tools\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { LLMChain } from \"../chains/llm_chain.js\";\nimport type { SqlDatabase } from \"../sql_db.js\";\nimport { SqlTable } from \"../util/sql_utils.js\";\n\n/**\n * Interface for SQL tools. It has a `db` property which is a SQL\n * database.\n */\ninterface SqlTool {\n db: SqlDatabase;\n}\n\n/**\n * A tool for executing SQL queries. It takes a SQL database as a\n * parameter and assigns it to the `db` property. The `_call` method is\n * used to run the SQL query and return the result. If the query is\n * incorrect, an error message is returned.\n */\nexport class QuerySqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"QuerySqlTool\";\n }\n\n name = \"query-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super(...arguments);\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n return await this.db.run(input);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a detailed and correct SQL query, output is a result from the database.\n If the query is not correct, an error message will be returned.\n If an error is returned, rewrite the query, check the query, and try again.`;\n}\n\n/**\n * A tool for retrieving information about SQL tables. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to retrieve the schema and sample rows for the\n * specified tables. If the tables do not exist, an error message is\n * returned.\n */\nexport class InfoSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"InfoSqlTool\";\n }\n\n name = \"info-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n const tables = input.split(\",\").map((table) => table.trim());\n return await this.db.getTableInfo(tables);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\n Be sure that the tables actually exist by calling list-tables-sql first!\n\n Example Input: \"table1, table2, table3.`;\n}\n\n/**\n * A tool for listing all tables in a SQL database. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to return a comma-separated list of all tables\n * in the database.\n */\nexport class ListTablesSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"ListTablesSqlTool\";\n }\n\n name = \"list-tables-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n async _call(_: string) {\n try {\n let selectedTables: SqlTable[] = this.db.allTables;\n\n if (this.db.includesTables.length > 0) {\n selectedTables = selectedTables.filter((currentTable) =>\n this.db.includesTables.includes(currentTable.tableName)\n );\n }\n\n if (this.db.ignoreTables.length > 0) {\n selectedTables = selectedTables.filter(\n (currentTable) =>\n !this.db.ignoreTables.includes(currentTable.tableName)\n );\n }\n\n const tables = selectedTables.map((table: SqlTable) => table.tableName);\n return tables.join(\", \");\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input is an empty string, output is a comma-separated list of tables in the database.`;\n}\n\n/**\n * Arguments for the QueryCheckerTool class.\n */\ntype QueryCheckerToolArgs = {\n llmChain?: LLMChain;\n llm: BaseLanguageModelInterface;\n _chainType?: never;\n};\n\n/**\n * A tool for checking SQL queries for common mistakes. It takes a\n * LLMChain or QueryCheckerToolArgs as a parameter. The `_call` method is\n * used to check the input query for common mistakes and returns a\n * prediction.\n */\nexport class QueryCheckerTool extends Tool {\n static lc_name() {\n return \"QueryCheckerTool\";\n }\n\n name = \"query-checker\";\n\n template = `\n {query}\nDouble check the SQL query above for common mistakes, including:\n- Using NOT IN with NULL values\n- Using UNION when UNION ALL should have been used\n- Using BETWEEN for exclusive ranges\n- Data type mismatch in predicates\n- Properly quoting identifiers\n- Using the correct number of arguments for functions\n- Casting to the correct data type\n- Using the proper columns for joins\n\nIf there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.`;\n\n llmChain: LLMChain;\n\n constructor(llmChainOrOptions?: LLMChain | QueryCheckerToolArgs) {\n super();\n if (typeof llmChainOrOptions?._chainType === \"function\") {\n this.llmChain = llmChainOrOptions as LLMChain;\n } else {\n const options = llmChainOrOptions as QueryCheckerToolArgs;\n if (options?.llmChain !== undefined) {\n this.llmChain = options.llmChain;\n } else {\n const prompt = new PromptTemplate({\n template: this.template,\n inputVariables: [\"query\"],\n });\n const llm = options.llm;\n this.llmChain = new LLMChain({ llm, prompt });\n }\n }\n }\n\n /** @ignore */\n async _call(input: string) {\n return this.llmChain.predict({ query: input });\n }\n\n description = `Use this tool to double check if your query is correct before executing it.\n Always use this tool before executing a query with query-sql!`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqBA,IAAa,eAAb,cAAkCA,sBAAAA,KAAwB;CACxD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,QAAM,GAAG,UAAU;AACnB,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;AACF,UAAO,MAAM,KAAK,GAAG,IAAI,MAAM;WACxB,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,cAAb,cAAiCA,sBAAAA,KAAwB;CACvD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;GACF,MAAM,SAAS,MAAM,MAAM,IAAI,CAAC,KAAK,UAAU,MAAM,MAAM,CAAC;AAC5D,UAAO,MAAM,KAAK,GAAG,aAAa,OAAO;WAClC,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,oBAAb,cAAuCA,sBAAAA,KAAwB;CAC7D,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;CAGZ,MAAM,MAAM,GAAW;AACrB,MAAI;GACF,IAAI,iBAA6B,KAAK,GAAG;AAEzC,OAAI,KAAK,GAAG,eAAe,SAAS,EAClC,kBAAiB,eAAe,QAAQ,iBACtC,KAAK,GAAG,eAAe,SAAS,aAAa,UAAU,CACxD;AAGH,OAAI,KAAK,GAAG,aAAa,SAAS,EAChC,kBAAiB,eAAe,QAC7B,iBACC,CAAC,KAAK,GAAG,aAAa,SAAS,aAAa,UAAU,CACzD;AAIH,UADe,eAAe,KAAK,UAAoB,MAAM,UAChD,CAAC,KAAK,KAAK;WACjB,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;AAkBhB,IAAa,mBAAb,cAAsCA,sBAAAA,KAAK;CACzC,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP,WAAW;;;;;;;;;;;;;CAcX;CAEA,YAAY,mBAAqD;AAC/D,SAAO;AACP,MAAI,OAAO,mBAAmB,eAAe,WAC3C,MAAK,WAAW;OACX;GACL,MAAM,UAAU;AAChB,OAAI,SAAS,aAAa,KAAA,EACxB,MAAK,WAAW,QAAQ;QACnB;IACL,MAAM,SAAS,IAAIC,wBAAAA,eAAe;KAChC,UAAU,KAAK;KACf,gBAAgB,CAAC,QAAQ;KAC1B,CAAC;IACF,MAAM,MAAM,QAAQ;AACpB,SAAK,WAAW,IAAIC,kBAAAA,SAAS;KAAE;KAAK;KAAQ,CAAC;;;;;CAMnD,MAAM,MAAM,OAAe;AACzB,SAAO,KAAK,SAAS,QAAQ,EAAE,OAAO,OAAO,CAAC;;CAGhD,cAAc"}
|
package/dist/tools/sql.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql.js","names":[],"sources":["../../src/tools/sql.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Tool } from \"@langchain/core/tools\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { LLMChain } from \"../chains/llm_chain.js\";\nimport type { SqlDatabase } from \"../sql_db.js\";\nimport { SqlTable } from \"../util/sql_utils.js\";\n\n/**\n * Interface for SQL tools. It has a `db` property which is a SQL\n * database.\n */\ninterface SqlTool {\n db: SqlDatabase;\n}\n\n/**\n * A tool for executing SQL queries. It takes a SQL database as a\n * parameter and assigns it to the `db` property. The `_call` method is\n * used to run the SQL query and return the result. If the query is\n * incorrect, an error message is returned.\n */\nexport class QuerySqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"QuerySqlTool\";\n }\n\n name = \"query-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super(...arguments);\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n return await this.db.run(input);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a detailed and correct SQL query, output is a result from the database.\n If the query is not correct, an error message will be returned.\n If an error is returned, rewrite the query, check the query, and try again.`;\n}\n\n/**\n * A tool for retrieving information about SQL tables. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to retrieve the schema and sample rows for the\n * specified tables. If the tables do not exist, an error message is\n * returned.\n */\nexport class InfoSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"InfoSqlTool\";\n }\n\n name = \"info-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n const tables = input.split(\",\").map((table) => table.trim());\n return await this.db.getTableInfo(tables);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\n Be sure that the tables actually exist by calling list-tables-sql first!\n\n Example Input: \"table1, table2, table3.`;\n}\n\n/**\n * A tool for listing all tables in a SQL database. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to return a comma-separated list of all tables\n * in the database.\n */\nexport class ListTablesSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"ListTablesSqlTool\";\n }\n\n name = \"list-tables-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n async _call(_: string) {\n try {\n let selectedTables: SqlTable[] = this.db.allTables;\n\n if (this.db.includesTables.length > 0) {\n selectedTables = selectedTables.filter((currentTable) =>\n this.db.includesTables.includes(currentTable.tableName)\n );\n }\n\n if (this.db.ignoreTables.length > 0) {\n selectedTables = selectedTables.filter(\n (currentTable) =>\n !this.db.ignoreTables.includes(currentTable.tableName)\n );\n }\n\n const tables = selectedTables.map((table: SqlTable) => table.tableName);\n return tables.join(\", \");\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input is an empty string, output is a comma-separated list of tables in the database.`;\n}\n\n/**\n * Arguments for the QueryCheckerTool class.\n */\ntype QueryCheckerToolArgs = {\n llmChain?: LLMChain;\n llm: BaseLanguageModelInterface;\n _chainType?: never;\n};\n\n/**\n * A tool for checking SQL queries for common mistakes. It takes a\n * LLMChain or QueryCheckerToolArgs as a parameter. The `_call` method is\n * used to check the input query for common mistakes and returns a\n * prediction.\n */\nexport class QueryCheckerTool extends Tool {\n static lc_name() {\n return \"QueryCheckerTool\";\n }\n\n name = \"query-checker\";\n\n template = `\n {query}\nDouble check the SQL query above for common mistakes, including:\n- Using NOT IN with NULL values\n- Using UNION when UNION ALL should have been used\n- Using BETWEEN for exclusive ranges\n- Data type mismatch in predicates\n- Properly quoting identifiers\n- Using the correct number of arguments for functions\n- Casting to the correct data type\n- Using the proper columns for joins\n\nIf there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.`;\n\n llmChain: LLMChain;\n\n constructor(llmChainOrOptions?: LLMChain | QueryCheckerToolArgs) {\n super();\n if (typeof llmChainOrOptions?._chainType === \"function\") {\n this.llmChain = llmChainOrOptions as LLMChain;\n } else {\n const options = llmChainOrOptions as QueryCheckerToolArgs;\n if (options?.llmChain !== undefined) {\n this.llmChain = options.llmChain;\n } else {\n const prompt = new PromptTemplate({\n template: this.template,\n inputVariables: [\"query\"],\n });\n const llm = options.llm;\n this.llmChain = new LLMChain({ llm, prompt });\n }\n }\n }\n\n /** @ignore */\n async _call(input: string) {\n return this.llmChain.predict({ query: input });\n }\n\n description = `Use this tool to double check if your query is correct before executing it.\n Always use this tool before executing a query with query-sql!`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAqBA,IAAa,eAAb,cAAkC,KAAwB;CACxD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,QAAM,GAAG,UAAU;AACnB,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;AACF,UAAO,MAAM,KAAK,GAAG,IAAI,MAAM;WACxB,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,cAAb,cAAiC,KAAwB;CACvD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;GACF,MAAM,SAAS,MAAM,MAAM,IAAI,CAAC,KAAK,UAAU,MAAM,MAAM,CAAC;AAC5D,UAAO,MAAM,KAAK,GAAG,aAAa,OAAO;WAClC,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,oBAAb,cAAuC,KAAwB;CAC7D,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;CAGZ,MAAM,MAAM,GAAW;AACrB,MAAI;GACF,IAAI,iBAA6B,KAAK,GAAG;AAEzC,OAAI,KAAK,GAAG,eAAe,SAAS,EAClC,kBAAiB,eAAe,QAAQ,iBACtC,KAAK,GAAG,eAAe,SAAS,aAAa,UAAU,CACxD;AAGH,OAAI,KAAK,GAAG,aAAa,SAAS,EAChC,kBAAiB,eAAe,QAC7B,iBACC,CAAC,KAAK,GAAG,aAAa,SAAS,aAAa,UAAU,CACzD;AAIH,UADe,eAAe,KAAK,UAAoB,MAAM,
|
|
1
|
+
{"version":3,"file":"sql.js","names":[],"sources":["../../src/tools/sql.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Tool } from \"@langchain/core/tools\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { LLMChain } from \"../chains/llm_chain.js\";\nimport type { SqlDatabase } from \"../sql_db.js\";\nimport { SqlTable } from \"../util/sql_utils.js\";\n\n/**\n * Interface for SQL tools. It has a `db` property which is a SQL\n * database.\n */\ninterface SqlTool {\n db: SqlDatabase;\n}\n\n/**\n * A tool for executing SQL queries. It takes a SQL database as a\n * parameter and assigns it to the `db` property. The `_call` method is\n * used to run the SQL query and return the result. If the query is\n * incorrect, an error message is returned.\n */\nexport class QuerySqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"QuerySqlTool\";\n }\n\n name = \"query-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super(...arguments);\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n return await this.db.run(input);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a detailed and correct SQL query, output is a result from the database.\n If the query is not correct, an error message will be returned.\n If an error is returned, rewrite the query, check the query, and try again.`;\n}\n\n/**\n * A tool for retrieving information about SQL tables. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to retrieve the schema and sample rows for the\n * specified tables. If the tables do not exist, an error message is\n * returned.\n */\nexport class InfoSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"InfoSqlTool\";\n }\n\n name = \"info-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n /** @ignore */\n async _call(input: string) {\n try {\n const tables = input.split(\",\").map((table) => table.trim());\n return await this.db.getTableInfo(tables);\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\n Be sure that the tables actually exist by calling list-tables-sql first!\n\n Example Input: \"table1, table2, table3.`;\n}\n\n/**\n * A tool for listing all tables in a SQL database. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to return a comma-separated list of all tables\n * in the database.\n */\nexport class ListTablesSqlTool extends Tool implements SqlTool {\n static lc_name() {\n return \"ListTablesSqlTool\";\n }\n\n name = \"list-tables-sql\";\n\n db: SqlDatabase;\n\n constructor(db: SqlDatabase) {\n super();\n this.db = db;\n }\n\n async _call(_: string) {\n try {\n let selectedTables: SqlTable[] = this.db.allTables;\n\n if (this.db.includesTables.length > 0) {\n selectedTables = selectedTables.filter((currentTable) =>\n this.db.includesTables.includes(currentTable.tableName)\n );\n }\n\n if (this.db.ignoreTables.length > 0) {\n selectedTables = selectedTables.filter(\n (currentTable) =>\n !this.db.ignoreTables.includes(currentTable.tableName)\n );\n }\n\n const tables = selectedTables.map((table: SqlTable) => table.tableName);\n return tables.join(\", \");\n } catch (error) {\n return `${error}`;\n }\n }\n\n description = `Input is an empty string, output is a comma-separated list of tables in the database.`;\n}\n\n/**\n * Arguments for the QueryCheckerTool class.\n */\ntype QueryCheckerToolArgs = {\n llmChain?: LLMChain;\n llm: BaseLanguageModelInterface;\n _chainType?: never;\n};\n\n/**\n * A tool for checking SQL queries for common mistakes. It takes a\n * LLMChain or QueryCheckerToolArgs as a parameter. The `_call` method is\n * used to check the input query for common mistakes and returns a\n * prediction.\n */\nexport class QueryCheckerTool extends Tool {\n static lc_name() {\n return \"QueryCheckerTool\";\n }\n\n name = \"query-checker\";\n\n template = `\n {query}\nDouble check the SQL query above for common mistakes, including:\n- Using NOT IN with NULL values\n- Using UNION when UNION ALL should have been used\n- Using BETWEEN for exclusive ranges\n- Data type mismatch in predicates\n- Properly quoting identifiers\n- Using the correct number of arguments for functions\n- Casting to the correct data type\n- Using the proper columns for joins\n\nIf there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.`;\n\n llmChain: LLMChain;\n\n constructor(llmChainOrOptions?: LLMChain | QueryCheckerToolArgs) {\n super();\n if (typeof llmChainOrOptions?._chainType === \"function\") {\n this.llmChain = llmChainOrOptions as LLMChain;\n } else {\n const options = llmChainOrOptions as QueryCheckerToolArgs;\n if (options?.llmChain !== undefined) {\n this.llmChain = options.llmChain;\n } else {\n const prompt = new PromptTemplate({\n template: this.template,\n inputVariables: [\"query\"],\n });\n const llm = options.llm;\n this.llmChain = new LLMChain({ llm, prompt });\n }\n }\n }\n\n /** @ignore */\n async _call(input: string) {\n return this.llmChain.predict({ query: input });\n }\n\n description = `Use this tool to double check if your query is correct before executing it.\n Always use this tool before executing a query with query-sql!`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAqBA,IAAa,eAAb,cAAkC,KAAwB;CACxD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,QAAM,GAAG,UAAU;AACnB,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;AACF,UAAO,MAAM,KAAK,GAAG,IAAI,MAAM;WACxB,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,cAAb,cAAiC,KAAwB;CACvD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;GACF,MAAM,SAAS,MAAM,MAAM,IAAI,CAAC,KAAK,UAAU,MAAM,MAAM,CAAC;AAC5D,UAAO,MAAM,KAAK,GAAG,aAAa,OAAO;WAClC,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,oBAAb,cAAuC,KAAwB;CAC7D,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;CAGZ,MAAM,MAAM,GAAW;AACrB,MAAI;GACF,IAAI,iBAA6B,KAAK,GAAG;AAEzC,OAAI,KAAK,GAAG,eAAe,SAAS,EAClC,kBAAiB,eAAe,QAAQ,iBACtC,KAAK,GAAG,eAAe,SAAS,aAAa,UAAU,CACxD;AAGH,OAAI,KAAK,GAAG,aAAa,SAAS,EAChC,kBAAiB,eAAe,QAC7B,iBACC,CAAC,KAAK,GAAG,aAAa,SAAS,aAAa,UAAU,CACzD;AAIH,UADe,eAAe,KAAK,UAAoB,MAAM,UAChD,CAAC,KAAK,KAAK;WACjB,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;AAkBhB,IAAa,mBAAb,cAAsC,KAAK;CACzC,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP,WAAW;;;;;;;;;;;;;CAcX;CAEA,YAAY,mBAAqD;AAC/D,SAAO;AACP,MAAI,OAAO,mBAAmB,eAAe,WAC3C,MAAK,WAAW;OACX;GACL,MAAM,UAAU;AAChB,OAAI,SAAS,aAAa,KAAA,EACxB,MAAK,WAAW,QAAQ;QACnB;IACL,MAAM,SAAS,IAAI,eAAe;KAChC,UAAU,KAAK;KACf,gBAAgB,CAAC,QAAQ;KAC1B,CAAC;IACF,MAAM,MAAM,QAAQ;AACpB,SAAK,WAAW,IAAI,SAAS;KAAE;KAAK;KAAQ,CAAC;;;;;CAMnD,MAAM,MAAM,OAAe;AACzB,SAAO,KAAK,SAAS,QAAQ,EAAE,OAAO,OAAO,CAAC;;CAGhD,cAAc"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webbrowser.cjs","names":["Tool","RecursiveCharacterTextSplitter","Document","formatDocumentsAsString","MemoryVectorStore","RunnableSequence","StringOutputParser"],"sources":["../../src/tools/webbrowser.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\n\nimport * as cheerio from \"cheerio\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n} from \"@langchain/core/callbacks/manager\";\n\nimport { Tool, ToolParams } from \"@langchain/core/tools\";\nimport { RunnableSequence } from \"@langchain/core/runnables\";\nimport { StringOutputParser } from \"@langchain/core/output_parsers\";\nimport {\n RecursiveCharacterTextSplitter,\n TextSplitter,\n} from \"@langchain/textsplitters\";\nimport { MemoryVectorStore } from \"../vectorstores/memory.js\";\n\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport const parseInputs = (inputs: string): [string, string] => {\n const [baseUrl, task] = inputs.split(\",\").map((input) => {\n let t = input.trim();\n t = t.startsWith('\"') ? t.slice(1) : t;\n t = t.endsWith('\"') ? t.slice(0, -1) : t;\n // it likes to put / at the end of urls, wont matter for task\n t = t.endsWith(\"/\") ? t.slice(0, -1) : t;\n return t.trim();\n });\n\n return [baseUrl, task];\n};\n\nexport const getText = (\n html: string,\n baseUrl: string,\n summary: boolean\n): string => {\n // scriptingEnabled so noscript elements are parsed\n const $ = cheerio.load(html, { scriptingEnabled: true });\n\n let text = \"\";\n\n // lets only get the body if its a summary, dont need to summarize header or footer etc\n const rootElement = summary ? \"body \" : \"*\";\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n $(`${rootElement}:not(style):not(script):not(svg)`).each((_i, elem: any) => {\n // we dont want duplicated content as we drill down so remove children\n let content = $(elem).clone().children().remove().end().text().trim();\n const $el = $(elem);\n\n // if its an ahref, print the content and url\n let href = $el.attr(\"href\");\n if ($el.prop(\"tagName\")?.toLowerCase() === \"a\" && href) {\n if (!href.startsWith(\"http\")) {\n try {\n href = new URL(href, baseUrl).toString();\n } catch {\n // if this fails thats fine, just no url for this\n href = \"\";\n }\n }\n\n const imgAlt = $el.find(\"img[alt]\").attr(\"alt\")?.trim();\n if (imgAlt) {\n content += ` ${imgAlt}`;\n }\n\n text += ` [${content}](${href})`;\n }\n // otherwise just print the content\n else if (content !== \"\") {\n text += ` ${content}`;\n }\n });\n\n return text.trim().replace(/\\n+/g, \" \");\n};\n\nconst getHtml = async (baseUrl: string, h: Headers, config: RequestConfig) => {\n const domain = new URL(baseUrl).hostname;\n\n const headers = { ...h };\n // these appear to be positional, which means they have to exist in the headers passed in\n headers.Host = domain;\n headers[\"Alt-Used\"] = domain;\n\n let htmlResponse;\n try {\n const fetchOptions: RequestInit = {\n method: \"GET\",\n headers,\n credentials: config.withCredentials ? \"include\" : \"same-origin\",\n ...config,\n };\n\n htmlResponse = await fetch(baseUrl, fetchOptions);\n\n if (!htmlResponse.ok) {\n throw new Error(`http response ${htmlResponse.status}`);\n }\n } catch (e) {\n if (\n e &&\n typeof e === \"object\" &&\n \"message\" in e &&\n typeof e.message === \"string\" &&\n e.message.includes(\"http response\")\n ) {\n throw e;\n }\n throw e;\n }\n\n const allowedContentTypes = [\n \"text/html\",\n \"application/json\",\n \"application/xml\",\n \"application/javascript\",\n \"text/plain\",\n ];\n\n const contentType = htmlResponse.headers.get(\"content-type\") || \"\";\n const contentTypeArray = contentType.split(\";\");\n if (\n contentTypeArray[0] &&\n !allowedContentTypes.includes(contentTypeArray[0])\n ) {\n throw new Error(\"returned page was not utf8\");\n }\n return htmlResponse.text();\n};\n\nconst DEFAULT_HEADERS = {\n Accept:\n \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\",\n \"Accept-Encoding\": \"gzip, deflate\",\n \"Accept-Language\": \"en-US,en;q=0.5\",\n \"Alt-Used\": \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Connection: \"keep-alive\",\n Host: \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Referer: \"https://www.google.com/\",\n \"Sec-Fetch-Dest\": \"document\",\n \"Sec-Fetch-Mode\": \"navigate\",\n \"Sec-Fetch-Site\": \"cross-site\",\n \"Upgrade-Insecure-Requests\": \"1\",\n \"User-Agent\":\n \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0\",\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\ntype Headers = Record<string, any>;\n\n/**\n * Configuration options for fetch requests, similar to axios config but for fetch\n */\nexport interface RequestConfig extends Omit<RequestInit, \"headers\"> {\n withCredentials?: boolean;\n}\n\n/**\n * Defines the arguments that can be passed to the WebBrowser constructor.\n * It extends the ToolParams interface and includes properties for a\n * language model, embeddings, HTTP headers, an Axios configuration, a\n * callback manager, and a text splitter.\n */\nexport interface WebBrowserArgs extends ToolParams {\n model: BaseLanguageModelInterface;\n\n embeddings: EmbeddingsInterface;\n\n headers?: Headers;\n\n requestConfig?: RequestConfig;\n\n /** @deprecated */\n callbackManager?: CallbackManager;\n\n textSplitter?: TextSplitter;\n}\n\n/**\n * A class designed to interact with web pages, either to extract\n * information from them or to summarize their content. It uses the native\n * fetch API to send HTTP requests and the cheerio library to parse the\n * returned HTML.\n * @example\n * ```typescript\n * const browser = new WebBrowser({\n * model: new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0 }),\n * embeddings: new OpenAIEmbeddings({}),\n * });\n * const result = await browser.invoke(\"https:exampleurl.com\");\n * ```\n */\nexport class WebBrowser extends Tool {\n static lc_name() {\n return \"WebBrowser\";\n }\n\n get lc_namespace() {\n return [...super.lc_namespace, \"webbrowser\"];\n }\n\n private model: BaseLanguageModelInterface;\n\n private embeddings: EmbeddingsInterface;\n\n private headers: Headers;\n\n private requestConfig: RequestConfig;\n\n private textSplitter: TextSplitter;\n\n constructor({\n model,\n headers,\n embeddings,\n requestConfig,\n textSplitter,\n }: WebBrowserArgs) {\n super(...arguments);\n\n this.model = model;\n this.embeddings = embeddings;\n this.headers = headers ?? DEFAULT_HEADERS;\n this.requestConfig = {\n withCredentials: true,\n ...requestConfig,\n };\n this.textSplitter =\n textSplitter ??\n new RecursiveCharacterTextSplitter({\n chunkSize: 2000,\n chunkOverlap: 200,\n });\n }\n\n /** @ignore */\n async _call(inputs: string, runManager?: CallbackManagerForToolRun) {\n const [baseUrl, task] = parseInputs(inputs);\n const doSummary = !task;\n\n let text;\n try {\n const html = await getHtml(baseUrl, this.headers, this.requestConfig);\n text = getText(html, baseUrl, doSummary);\n } catch (e) {\n if (e) {\n return e.toString();\n }\n return \"There was a problem connecting to the site\";\n }\n\n const texts = await this.textSplitter.splitText(text);\n\n let context;\n // if we want a summary grab first 4\n if (doSummary) {\n context = texts.slice(0, 4).join(\"\\n\");\n }\n // search term well embed and grab top 4\n else {\n const docs = texts.map(\n (pageContent) =>\n new Document({\n pageContent,\n metadata: [],\n })\n );\n\n const vectorStore = await MemoryVectorStore.fromDocuments(\n docs,\n this.embeddings\n );\n const results = await vectorStore.similaritySearch(\n task,\n 4,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n context = formatDocumentsAsString(results);\n }\n\n const input = `Text:${context}\\n\\nI need ${\n doSummary ? \"a summary\" : task\n } from the above text, also provide up to 5 markdown links from within that would be of interest (always including URL and text). Links should be provided, if present, in markdown syntax as a list under the heading \"Relevant Links:\".`;\n\n const chain = RunnableSequence.from([this.model, new StringOutputParser()]);\n return chain.invoke(input, runManager?.getChild());\n }\n\n name = \"web-browser\";\n\n description = `useful for when you need to find something on or summarize a webpage. input should be a comma separated list of \"ONE valid http URL including protocol\",\"what you want to find on the page or empty string for a summary\".`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAqBA,MAAa,eAAe,WAAqC;CAC/D,MAAM,CAAC,SAAS,QAAQ,OAAO,MAAM,IAAI,CAAC,KAAK,UAAU;EACvD,IAAI,IAAI,MAAM,MAAM;AACpB,MAAI,EAAE,WAAW,KAAI,GAAG,EAAE,MAAM,EAAE,GAAG;AACrC,MAAI,EAAE,SAAS,KAAI,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG;AAEvC,MAAI,EAAE,SAAS,IAAI,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG;AACvC,SAAO,EAAE,MAAM;GACf;AAEF,QAAO,CAAC,SAAS,KAAK;;AAGxB,MAAa,WACX,MACA,SACA,YACW;CAEX,MAAM,IAAI,Q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AC,KAAK,KAAK;OAGnC;GACH,MAAM,OAAO,MAAM,KAChB,gBACC,IAAIC,0BAAAA,SAAS;IACX;IACA,UAAU,EAAE;IACb,CAAC,CACL;AAYD,aAAUC,sBAAAA,wBANM,OAJI,MAAMC,4BAAAA,kBAAkB,cAC1C,MACA,KAAK,WACN,EACiC,iBAChC,MACA,GACA,KAAA,GACA,YAAY,SAAS,cAAc,CACpC,CACyC;;EAG5C,MAAM,QAAQ,QAAQ,QAAQ,aAC5B,YAAY,cAAc,KAC3B;AAGD,SADcC,0BAAAA,iBAAiB,KAAK,CAAC,KAAK,OAAO,IAAIC,+BAAAA,oBAAoB,CAAC,CAAC,CAC9D,OAAO,OAAO,YAAY,UAAU,CAAC;;CAGpD,OAAO;CAEP,cAAc"}
|
|
1
|
+
{"version":3,"file":"webbrowser.cjs","names":["Tool","RecursiveCharacterTextSplitter","Document","formatDocumentsAsString","MemoryVectorStore","RunnableSequence","StringOutputParser"],"sources":["../../src/tools/webbrowser.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\n\nimport * as cheerio from \"cheerio\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n} from \"@langchain/core/callbacks/manager\";\n\nimport { Tool, ToolParams } from \"@langchain/core/tools\";\nimport { RunnableSequence } from \"@langchain/core/runnables\";\nimport { StringOutputParser } from \"@langchain/core/output_parsers\";\nimport {\n RecursiveCharacterTextSplitter,\n TextSplitter,\n} from \"@langchain/textsplitters\";\nimport { MemoryVectorStore } from \"../vectorstores/memory.js\";\n\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport const parseInputs = (inputs: string): [string, string] => {\n const [baseUrl, task] = inputs.split(\",\").map((input) => {\n let t = input.trim();\n t = t.startsWith('\"') ? t.slice(1) : t;\n t = t.endsWith('\"') ? t.slice(0, -1) : t;\n // it likes to put / at the end of urls, wont matter for task\n t = t.endsWith(\"/\") ? t.slice(0, -1) : t;\n return t.trim();\n });\n\n return [baseUrl, task];\n};\n\nexport const getText = (\n html: string,\n baseUrl: string,\n summary: boolean\n): string => {\n // scriptingEnabled so noscript elements are parsed\n const $ = cheerio.load(html, { scriptingEnabled: true });\n\n let text = \"\";\n\n // lets only get the body if its a summary, dont need to summarize header or footer etc\n const rootElement = summary ? \"body \" : \"*\";\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n $(`${rootElement}:not(style):not(script):not(svg)`).each((_i, elem: any) => {\n // we dont want duplicated content as we drill down so remove children\n let content = $(elem).clone().children().remove().end().text().trim();\n const $el = $(elem);\n\n // if its an ahref, print the content and url\n let href = $el.attr(\"href\");\n if ($el.prop(\"tagName\")?.toLowerCase() === \"a\" && href) {\n if (!href.startsWith(\"http\")) {\n try {\n href = new URL(href, baseUrl).toString();\n } catch {\n // if this fails thats fine, just no url for this\n href = \"\";\n }\n }\n\n const imgAlt = $el.find(\"img[alt]\").attr(\"alt\")?.trim();\n if (imgAlt) {\n content += ` ${imgAlt}`;\n }\n\n text += ` [${content}](${href})`;\n }\n // otherwise just print the content\n else if (content !== \"\") {\n text += ` ${content}`;\n }\n });\n\n return text.trim().replace(/\\n+/g, \" \");\n};\n\nconst getHtml = async (baseUrl: string, h: Headers, config: RequestConfig) => {\n const domain = new URL(baseUrl).hostname;\n\n const headers = { ...h };\n // these appear to be positional, which means they have to exist in the headers passed in\n headers.Host = domain;\n headers[\"Alt-Used\"] = domain;\n\n let htmlResponse;\n try {\n const fetchOptions: RequestInit = {\n method: \"GET\",\n headers,\n credentials: config.withCredentials ? \"include\" : \"same-origin\",\n ...config,\n };\n\n htmlResponse = await fetch(baseUrl, fetchOptions);\n\n if (!htmlResponse.ok) {\n throw new Error(`http response ${htmlResponse.status}`);\n }\n } catch (e) {\n if (\n e &&\n typeof e === \"object\" &&\n \"message\" in e &&\n typeof e.message === \"string\" &&\n e.message.includes(\"http response\")\n ) {\n throw e;\n }\n throw e;\n }\n\n const allowedContentTypes = [\n \"text/html\",\n \"application/json\",\n \"application/xml\",\n \"application/javascript\",\n \"text/plain\",\n ];\n\n const contentType = htmlResponse.headers.get(\"content-type\") || \"\";\n const contentTypeArray = contentType.split(\";\");\n if (\n contentTypeArray[0] &&\n !allowedContentTypes.includes(contentTypeArray[0])\n ) {\n throw new Error(\"returned page was not utf8\");\n }\n return htmlResponse.text();\n};\n\nconst DEFAULT_HEADERS = {\n Accept:\n \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\",\n \"Accept-Encoding\": \"gzip, deflate\",\n \"Accept-Language\": \"en-US,en;q=0.5\",\n \"Alt-Used\": \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Connection: \"keep-alive\",\n Host: \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Referer: \"https://www.google.com/\",\n \"Sec-Fetch-Dest\": \"document\",\n \"Sec-Fetch-Mode\": \"navigate\",\n \"Sec-Fetch-Site\": \"cross-site\",\n \"Upgrade-Insecure-Requests\": \"1\",\n \"User-Agent\":\n \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0\",\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\ntype Headers = Record<string, any>;\n\n/**\n * Configuration options for fetch requests, similar to axios config but for fetch\n */\nexport interface RequestConfig extends Omit<RequestInit, \"headers\"> {\n withCredentials?: boolean;\n}\n\n/**\n * Defines the arguments that can be passed to the WebBrowser constructor.\n * It extends the ToolParams interface and includes properties for a\n * language model, embeddings, HTTP headers, an Axios configuration, a\n * callback manager, and a text splitter.\n */\nexport interface WebBrowserArgs extends ToolParams {\n model: BaseLanguageModelInterface;\n\n embeddings: EmbeddingsInterface;\n\n headers?: Headers;\n\n requestConfig?: RequestConfig;\n\n /** @deprecated */\n callbackManager?: CallbackManager;\n\n textSplitter?: TextSplitter;\n}\n\n/**\n * A class designed to interact with web pages, either to extract\n * information from them or to summarize their content. It uses the native\n * fetch API to send HTTP requests and the cheerio library to parse the\n * returned HTML.\n * @example\n * ```typescript\n * const browser = new WebBrowser({\n * model: new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0 }),\n * embeddings: new OpenAIEmbeddings({}),\n * });\n * const result = await browser.invoke(\"https:exampleurl.com\");\n * ```\n */\nexport class WebBrowser extends Tool {\n static lc_name() {\n return \"WebBrowser\";\n }\n\n get lc_namespace() {\n return [...super.lc_namespace, \"webbrowser\"];\n }\n\n private model: BaseLanguageModelInterface;\n\n private embeddings: EmbeddingsInterface;\n\n private headers: Headers;\n\n private requestConfig: RequestConfig;\n\n private textSplitter: TextSplitter;\n\n constructor({\n model,\n headers,\n embeddings,\n requestConfig,\n textSplitter,\n }: WebBrowserArgs) {\n super(...arguments);\n\n this.model = model;\n this.embeddings = embeddings;\n this.headers = headers ?? DEFAULT_HEADERS;\n this.requestConfig = {\n withCredentials: true,\n ...requestConfig,\n };\n this.textSplitter =\n textSplitter ??\n new RecursiveCharacterTextSplitter({\n chunkSize: 2000,\n chunkOverlap: 200,\n });\n }\n\n /** @ignore */\n async _call(inputs: string, runManager?: CallbackManagerForToolRun) {\n const [baseUrl, task] = parseInputs(inputs);\n const doSummary = !task;\n\n let text;\n try {\n const html = await getHtml(baseUrl, this.headers, this.requestConfig);\n text = getText(html, baseUrl, doSummary);\n } catch (e) {\n if (e) {\n return e.toString();\n }\n return \"There was a problem connecting to the site\";\n }\n\n const texts = await this.textSplitter.splitText(text);\n\n let context;\n // if we want a summary grab first 4\n if (doSummary) {\n context = texts.slice(0, 4).join(\"\\n\");\n }\n // search term well embed and grab top 4\n else {\n const docs = texts.map(\n (pageContent) =>\n new Document({\n pageContent,\n metadata: [],\n })\n );\n\n const vectorStore = await MemoryVectorStore.fromDocuments(\n docs,\n this.embeddings\n );\n const results = await vectorStore.similaritySearch(\n task,\n 4,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n context = formatDocumentsAsString(results);\n }\n\n const input = `Text:${context}\\n\\nI need ${\n doSummary ? \"a summary\" : task\n } from the above text, also provide up to 5 markdown links from within that would be of interest (always including URL and text). Links should be provided, if present, in markdown syntax as a list under the heading \"Relevant Links:\".`;\n\n const chain = RunnableSequence.from([this.model, new StringOutputParser()]);\n return chain.invoke(input, runManager?.getChild());\n }\n\n name = \"web-browser\";\n\n description = `useful for when you need to find something on or summarize a webpage. input should be a comma separated list of \"ONE valid http URL including protocol\",\"what you want to find on the page or empty string for a summary\".`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAqBA,MAAa,eAAe,WAAqC;CAC/D,MAAM,CAAC,SAAS,QAAQ,OAAO,MAAM,IAAI,CAAC,KAAK,UAAU;EACvD,IAAI,IAAI,MAAM,MAAM;AACpB,MAAI,EAAE,WAAW,KAAI,GAAG,EAAE,MAAM,EAAE,GAAG;AACrC,MAAI,EAAE,SAAS,KAAI,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG;AAEvC,MAAI,EAAE,SAAS,IAAI,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG;AACvC,SAAO,EAAE,MAAM;GACf;AAEF,QAAO,CAAC,SAAS,KAAK;;AAGxB,MAAa,WACX,MACA,SACA,YACW;CAEX,MAAM,IAAI,QAAQ,KAAK,MAAM,EAAE,kBAAkB,MAAM,CAAC;CAExD,IAAI,OAAO;AAMX,GAAE,GAHkB,UAAU,UAAU,IAGvB,kCAAkC,CAAC,MAAM,IAAI,SAAc;EAE1E,IAAI,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM;EACrE,MAAM,MAAM,EAAE,KAAK;EAGnB,IAAI,OAAO,IAAI,KAAK,OAAO;AAC3B,MAAI,IAAI,KAAK,UAAU,EAAE,aAAa,KAAK,OAAO,MAAM;AACtD,OAAI,CAAC,KAAK,WAAW,OAAO,CAC1B,KAAI;AACF,WAAO,IAAI,IAAI,MAAM,QAAQ,CAAC,UAAU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AAO;CAEP,cAAc"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webbrowser.js","names":[],"sources":["../../src/tools/webbrowser.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\n\nimport * as cheerio from \"cheerio\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n} from \"@langchain/core/callbacks/manager\";\n\nimport { Tool, ToolParams } from \"@langchain/core/tools\";\nimport { RunnableSequence } from \"@langchain/core/runnables\";\nimport { StringOutputParser } from \"@langchain/core/output_parsers\";\nimport {\n RecursiveCharacterTextSplitter,\n TextSplitter,\n} from \"@langchain/textsplitters\";\nimport { MemoryVectorStore } from \"../vectorstores/memory.js\";\n\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport const parseInputs = (inputs: string): [string, string] => {\n const [baseUrl, task] = inputs.split(\",\").map((input) => {\n let t = input.trim();\n t = t.startsWith('\"') ? t.slice(1) : t;\n t = t.endsWith('\"') ? t.slice(0, -1) : t;\n // it likes to put / at the end of urls, wont matter for task\n t = t.endsWith(\"/\") ? t.slice(0, -1) : t;\n return t.trim();\n });\n\n return [baseUrl, task];\n};\n\nexport const getText = (\n html: string,\n baseUrl: string,\n summary: boolean\n): string => {\n // scriptingEnabled so noscript elements are parsed\n const $ = cheerio.load(html, { scriptingEnabled: true });\n\n let text = \"\";\n\n // lets only get the body if its a summary, dont need to summarize header or footer etc\n const rootElement = summary ? \"body \" : \"*\";\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n $(`${rootElement}:not(style):not(script):not(svg)`).each((_i, elem: any) => {\n // we dont want duplicated content as we drill down so remove children\n let content = $(elem).clone().children().remove().end().text().trim();\n const $el = $(elem);\n\n // if its an ahref, print the content and url\n let href = $el.attr(\"href\");\n if ($el.prop(\"tagName\")?.toLowerCase() === \"a\" && href) {\n if (!href.startsWith(\"http\")) {\n try {\n href = new URL(href, baseUrl).toString();\n } catch {\n // if this fails thats fine, just no url for this\n href = \"\";\n }\n }\n\n const imgAlt = $el.find(\"img[alt]\").attr(\"alt\")?.trim();\n if (imgAlt) {\n content += ` ${imgAlt}`;\n }\n\n text += ` [${content}](${href})`;\n }\n // otherwise just print the content\n else if (content !== \"\") {\n text += ` ${content}`;\n }\n });\n\n return text.trim().replace(/\\n+/g, \" \");\n};\n\nconst getHtml = async (baseUrl: string, h: Headers, config: RequestConfig) => {\n const domain = new URL(baseUrl).hostname;\n\n const headers = { ...h };\n // these appear to be positional, which means they have to exist in the headers passed in\n headers.Host = domain;\n headers[\"Alt-Used\"] = domain;\n\n let htmlResponse;\n try {\n const fetchOptions: RequestInit = {\n method: \"GET\",\n headers,\n credentials: config.withCredentials ? \"include\" : \"same-origin\",\n ...config,\n };\n\n htmlResponse = await fetch(baseUrl, fetchOptions);\n\n if (!htmlResponse.ok) {\n throw new Error(`http response ${htmlResponse.status}`);\n }\n } catch (e) {\n if (\n e &&\n typeof e === \"object\" &&\n \"message\" in e &&\n typeof e.message === \"string\" &&\n e.message.includes(\"http response\")\n ) {\n throw e;\n }\n throw e;\n }\n\n const allowedContentTypes = [\n \"text/html\",\n \"application/json\",\n \"application/xml\",\n \"application/javascript\",\n \"text/plain\",\n ];\n\n const contentType = htmlResponse.headers.get(\"content-type\") || \"\";\n const contentTypeArray = contentType.split(\";\");\n if (\n contentTypeArray[0] &&\n !allowedContentTypes.includes(contentTypeArray[0])\n ) {\n throw new Error(\"returned page was not utf8\");\n }\n return htmlResponse.text();\n};\n\nconst DEFAULT_HEADERS = {\n Accept:\n \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\",\n \"Accept-Encoding\": \"gzip, deflate\",\n \"Accept-Language\": \"en-US,en;q=0.5\",\n \"Alt-Used\": \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Connection: \"keep-alive\",\n Host: \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Referer: \"https://www.google.com/\",\n \"Sec-Fetch-Dest\": \"document\",\n \"Sec-Fetch-Mode\": \"navigate\",\n \"Sec-Fetch-Site\": \"cross-site\",\n \"Upgrade-Insecure-Requests\": \"1\",\n \"User-Agent\":\n \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0\",\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\ntype Headers = Record<string, any>;\n\n/**\n * Configuration options for fetch requests, similar to axios config but for fetch\n */\nexport interface RequestConfig extends Omit<RequestInit, \"headers\"> {\n withCredentials?: boolean;\n}\n\n/**\n * Defines the arguments that can be passed to the WebBrowser constructor.\n * It extends the ToolParams interface and includes properties for a\n * language model, embeddings, HTTP headers, an Axios configuration, a\n * callback manager, and a text splitter.\n */\nexport interface WebBrowserArgs extends ToolParams {\n model: BaseLanguageModelInterface;\n\n embeddings: EmbeddingsInterface;\n\n headers?: Headers;\n\n requestConfig?: RequestConfig;\n\n /** @deprecated */\n callbackManager?: CallbackManager;\n\n textSplitter?: TextSplitter;\n}\n\n/**\n * A class designed to interact with web pages, either to extract\n * information from them or to summarize their content. It uses the native\n * fetch API to send HTTP requests and the cheerio library to parse the\n * returned HTML.\n * @example\n * ```typescript\n * const browser = new WebBrowser({\n * model: new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0 }),\n * embeddings: new OpenAIEmbeddings({}),\n * });\n * const result = await browser.invoke(\"https:exampleurl.com\");\n * ```\n */\nexport class WebBrowser extends Tool {\n static lc_name() {\n return \"WebBrowser\";\n }\n\n get lc_namespace() {\n return [...super.lc_namespace, \"webbrowser\"];\n }\n\n private model: BaseLanguageModelInterface;\n\n private embeddings: EmbeddingsInterface;\n\n private headers: Headers;\n\n private requestConfig: RequestConfig;\n\n private textSplitter: TextSplitter;\n\n constructor({\n model,\n headers,\n embeddings,\n requestConfig,\n textSplitter,\n }: WebBrowserArgs) {\n super(...arguments);\n\n this.model = model;\n this.embeddings = embeddings;\n this.headers = headers ?? DEFAULT_HEADERS;\n this.requestConfig = {\n withCredentials: true,\n ...requestConfig,\n };\n this.textSplitter =\n textSplitter ??\n new RecursiveCharacterTextSplitter({\n chunkSize: 2000,\n chunkOverlap: 200,\n });\n }\n\n /** @ignore */\n async _call(inputs: string, runManager?: CallbackManagerForToolRun) {\n const [baseUrl, task] = parseInputs(inputs);\n const doSummary = !task;\n\n let text;\n try {\n const html = await getHtml(baseUrl, this.headers, this.requestConfig);\n text = getText(html, baseUrl, doSummary);\n } catch (e) {\n if (e) {\n return e.toString();\n }\n return \"There was a problem connecting to the site\";\n }\n\n const texts = await this.textSplitter.splitText(text);\n\n let context;\n // if we want a summary grab first 4\n if (doSummary) {\n context = texts.slice(0, 4).join(\"\\n\");\n }\n // search term well embed and grab top 4\n else {\n const docs = texts.map(\n (pageContent) =>\n new Document({\n pageContent,\n metadata: [],\n })\n );\n\n const vectorStore = await MemoryVectorStore.fromDocuments(\n docs,\n this.embeddings\n );\n const results = await vectorStore.similaritySearch(\n task,\n 4,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n context = formatDocumentsAsString(results);\n }\n\n const input = `Text:${context}\\n\\nI need ${\n doSummary ? \"a summary\" : task\n } from the above text, also provide up to 5 markdown links from within that would be of interest (always including URL and text). Links should be provided, if present, in markdown syntax as a list under the heading \"Relevant Links:\".`;\n\n const chain = RunnableSequence.from([this.model, new StringOutputParser()]);\n return chain.invoke(input, runManager?.getChild());\n }\n\n name = \"web-browser\";\n\n description = `useful for when you need to find something on or summarize a webpage. input should be a comma separated list of \"ONE valid http URL including protocol\",\"what you want to find on the page or empty string for a summary\".`;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAqBA,MAAa,eAAe,WAAqC;CAC/D,MAAM,CAAC,SAAS,QAAQ,OAAO,MAAM,IAAI,CAAC,KAAK,UAAU;EACvD,IAAI,IAAI,MAAM,MAAM;AACpB,MAAI,EAAE,WAAW,KAAI,GAAG,EAAE,MAAM,EAA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M,MAAM,QAAQ,SAAS,KAAK,SAAS,KAAK,cAAc,EAChD,SAAS,UAAU;WACjC,GAAG;AACV,OAAI,EACF,QAAO,EAAE,UAAU;AAErB,UAAO;;EAGT,MAAM,QAAQ,MAAM,KAAK,aAAa,UAAU,KAAK;EAErD,IAAI;AAEJ,MAAI,UACF,WAAU,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,KAAK;OAGnC;GACH,MAAM,OAAO,MAAM,KAChB,gBACC,IAAI,SAAS;IACX;IACA,UAAU,EAAE;IACb,CAAC,CACL;AAYD,aAAU,wBANM,OAJI,MAAM,kBAAkB,cAC1C,MACA,KAAK,WACN,EACiC,iBAChC,MACA,GACA,KAAA,GACA,YAAY,SAAS,cAAc,CACpC,CACyC;;EAG5C,MAAM,QAAQ,QAAQ,QAAQ,aAC5B,YAAY,cAAc,KAC3B;AAGD,SADc,iBAAiB,KAAK,CAAC,KAAK,OAAO,IAAI,oBAAoB,CAAC,CAAC,CAC9D,OAAO,OAAO,YAAY,UAAU,CAAC;;CAGpD,OAAO;CAEP,cAAc"}
|
|
1
|
+
{"version":3,"file":"webbrowser.js","names":[],"sources":["../../src/tools/webbrowser.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport type { EmbeddingsInterface } from \"@langchain/core/embeddings\";\n\nimport * as cheerio from \"cheerio\";\nimport {\n CallbackManager,\n CallbackManagerForToolRun,\n} from \"@langchain/core/callbacks/manager\";\n\nimport { Tool, ToolParams } from \"@langchain/core/tools\";\nimport { RunnableSequence } from \"@langchain/core/runnables\";\nimport { StringOutputParser } from \"@langchain/core/output_parsers\";\nimport {\n RecursiveCharacterTextSplitter,\n TextSplitter,\n} from \"@langchain/textsplitters\";\nimport { MemoryVectorStore } from \"../vectorstores/memory.js\";\n\nimport { formatDocumentsAsString } from \"../util/document.js\";\n\nexport const parseInputs = (inputs: string): [string, string] => {\n const [baseUrl, task] = inputs.split(\",\").map((input) => {\n let t = input.trim();\n t = t.startsWith('\"') ? t.slice(1) : t;\n t = t.endsWith('\"') ? t.slice(0, -1) : t;\n // it likes to put / at the end of urls, wont matter for task\n t = t.endsWith(\"/\") ? t.slice(0, -1) : t;\n return t.trim();\n });\n\n return [baseUrl, task];\n};\n\nexport const getText = (\n html: string,\n baseUrl: string,\n summary: boolean\n): string => {\n // scriptingEnabled so noscript elements are parsed\n const $ = cheerio.load(html, { scriptingEnabled: true });\n\n let text = \"\";\n\n // lets only get the body if its a summary, dont need to summarize header or footer etc\n const rootElement = summary ? \"body \" : \"*\";\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n $(`${rootElement}:not(style):not(script):not(svg)`).each((_i, elem: any) => {\n // we dont want duplicated content as we drill down so remove children\n let content = $(elem).clone().children().remove().end().text().trim();\n const $el = $(elem);\n\n // if its an ahref, print the content and url\n let href = $el.attr(\"href\");\n if ($el.prop(\"tagName\")?.toLowerCase() === \"a\" && href) {\n if (!href.startsWith(\"http\")) {\n try {\n href = new URL(href, baseUrl).toString();\n } catch {\n // if this fails thats fine, just no url for this\n href = \"\";\n }\n }\n\n const imgAlt = $el.find(\"img[alt]\").attr(\"alt\")?.trim();\n if (imgAlt) {\n content += ` ${imgAlt}`;\n }\n\n text += ` [${content}](${href})`;\n }\n // otherwise just print the content\n else if (content !== \"\") {\n text += ` ${content}`;\n }\n });\n\n return text.trim().replace(/\\n+/g, \" \");\n};\n\nconst getHtml = async (baseUrl: string, h: Headers, config: RequestConfig) => {\n const domain = new URL(baseUrl).hostname;\n\n const headers = { ...h };\n // these appear to be positional, which means they have to exist in the headers passed in\n headers.Host = domain;\n headers[\"Alt-Used\"] = domain;\n\n let htmlResponse;\n try {\n const fetchOptions: RequestInit = {\n method: \"GET\",\n headers,\n credentials: config.withCredentials ? \"include\" : \"same-origin\",\n ...config,\n };\n\n htmlResponse = await fetch(baseUrl, fetchOptions);\n\n if (!htmlResponse.ok) {\n throw new Error(`http response ${htmlResponse.status}`);\n }\n } catch (e) {\n if (\n e &&\n typeof e === \"object\" &&\n \"message\" in e &&\n typeof e.message === \"string\" &&\n e.message.includes(\"http response\")\n ) {\n throw e;\n }\n throw e;\n }\n\n const allowedContentTypes = [\n \"text/html\",\n \"application/json\",\n \"application/xml\",\n \"application/javascript\",\n \"text/plain\",\n ];\n\n const contentType = htmlResponse.headers.get(\"content-type\") || \"\";\n const contentTypeArray = contentType.split(\";\");\n if (\n contentTypeArray[0] &&\n !allowedContentTypes.includes(contentTypeArray[0])\n ) {\n throw new Error(\"returned page was not utf8\");\n }\n return htmlResponse.text();\n};\n\nconst DEFAULT_HEADERS = {\n Accept:\n \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\",\n \"Accept-Encoding\": \"gzip, deflate\",\n \"Accept-Language\": \"en-US,en;q=0.5\",\n \"Alt-Used\": \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Connection: \"keep-alive\",\n Host: \"LEAVE-THIS-KEY-SET-BY-TOOL\",\n Referer: \"https://www.google.com/\",\n \"Sec-Fetch-Dest\": \"document\",\n \"Sec-Fetch-Mode\": \"navigate\",\n \"Sec-Fetch-Site\": \"cross-site\",\n \"Upgrade-Insecure-Requests\": \"1\",\n \"User-Agent\":\n \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0\",\n};\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\ntype Headers = Record<string, any>;\n\n/**\n * Configuration options for fetch requests, similar to axios config but for fetch\n */\nexport interface RequestConfig extends Omit<RequestInit, \"headers\"> {\n withCredentials?: boolean;\n}\n\n/**\n * Defines the arguments that can be passed to the WebBrowser constructor.\n * It extends the ToolParams interface and includes properties for a\n * language model, embeddings, HTTP headers, an Axios configuration, a\n * callback manager, and a text splitter.\n */\nexport interface WebBrowserArgs extends ToolParams {\n model: BaseLanguageModelInterface;\n\n embeddings: EmbeddingsInterface;\n\n headers?: Headers;\n\n requestConfig?: RequestConfig;\n\n /** @deprecated */\n callbackManager?: CallbackManager;\n\n textSplitter?: TextSplitter;\n}\n\n/**\n * A class designed to interact with web pages, either to extract\n * information from them or to summarize their content. It uses the native\n * fetch API to send HTTP requests and the cheerio library to parse the\n * returned HTML.\n * @example\n * ```typescript\n * const browser = new WebBrowser({\n * model: new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0 }),\n * embeddings: new OpenAIEmbeddings({}),\n * });\n * const result = await browser.invoke(\"https:exampleurl.com\");\n * ```\n */\nexport class WebBrowser extends Tool {\n static lc_name() {\n return \"WebBrowser\";\n }\n\n get lc_namespace() {\n return [...super.lc_namespace, \"webbrowser\"];\n }\n\n private model: BaseLanguageModelInterface;\n\n private embeddings: EmbeddingsInterface;\n\n private headers: Headers;\n\n private requestConfig: RequestConfig;\n\n private textSplitter: TextSplitter;\n\n constructor({\n model,\n headers,\n embeddings,\n requestConfig,\n textSplitter,\n }: WebBrowserArgs) {\n super(...arguments);\n\n this.model = model;\n this.embeddings = embeddings;\n this.headers = headers ?? DEFAULT_HEADERS;\n this.requestConfig = {\n withCredentials: true,\n ...requestConfig,\n };\n this.textSplitter =\n textSplitter ??\n new RecursiveCharacterTextSplitter({\n chunkSize: 2000,\n chunkOverlap: 200,\n });\n }\n\n /** @ignore */\n async _call(inputs: string, runManager?: CallbackManagerForToolRun) {\n const [baseUrl, task] = parseInputs(inputs);\n const doSummary = !task;\n\n let text;\n try {\n const html = await getHtml(baseUrl, this.headers, this.requestConfig);\n text = getText(html, baseUrl, doSummary);\n } catch (e) {\n if (e) {\n return e.toString();\n }\n return \"There was a problem connecting to the site\";\n }\n\n const texts = await this.textSplitter.splitText(text);\n\n let context;\n // if we want a summary grab first 4\n if (doSummary) {\n context = texts.slice(0, 4).join(\"\\n\");\n }\n // search term well embed and grab top 4\n else {\n const docs = texts.map(\n (pageContent) =>\n new Document({\n pageContent,\n metadata: [],\n })\n );\n\n const vectorStore = await MemoryVectorStore.fromDocuments(\n docs,\n this.embeddings\n );\n const results = await vectorStore.similaritySearch(\n task,\n 4,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n context = formatDocumentsAsString(results);\n }\n\n const input = `Text:${context}\\n\\nI need ${\n doSummary ? \"a summary\" : task\n } from the above text, also provide up to 5 markdown links from within that would be of interest (always including URL and text). Links should be provided, if present, in markdown syntax as a list under the heading \"Relevant Links:\".`;\n\n const chain = RunnableSequence.from([this.model, new StringOutputParser()]);\n return chain.invoke(input, runManager?.getChild());\n }\n\n name = \"web-browser\";\n\n description = `useful for when you need to find something on or summarize a webpage. input should be a comma separated list of \"ONE valid http URL including protocol\",\"what you want to find on the page or empty string for a summary\".`;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAqBA,MAAa,eAAe,WAAqC;CAC/D,MAAM,CAAC,SAAS,QAAQ,OAAO,MAAM,IAAI,CAAC,KAAK,UAAU;EACvD,IAAI,IAAI,MAAM,MAAM;AACpB,MAAI,EAAE,WAAW,KAAI,GAAG,EAAE,MAAM,EAAE,GAAG;AACrC,MAAI,EAAE,SAAS,KAAI,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG;AAEvC,MAAI,EAAE,SAAS,IAAI,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG;AACvC,SAAO,EAAE,MAAM;GACf;AAEF,QAAO,CAAC,SAAS,KAAK;;AAGxB,MAAa,WACX,MACA,SACA,YACW;CAEX,MAAM,IAAI,QAAQ,KAAK,MAAM,EAAE,kBAAkB,MAAM,CAAC;CAExD,IAAI,OAAO;AAMX,GAAE,GAHkB,UAAU,UAAU,IAGvB,kCAAkC,CAAC,MAAM,IAAI,SAAc;EAE1E,IAAI,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM;EACrE,MAAM,MAAM,EAAE,KAAK;EAGnB,IAAI,OAAO,IAAI,KAAK,OAAO;AAC3B,MAAI,IAAI,KAAK,UAAU,EAAE,aAAa,KAAK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C,OAAO,OAAO,YAAY,UAAU,CAAC;;CAGpD,OAAO;CAEP,cAAc"}
|
package/dist/util/hub.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hub.cjs","names":["extname","pRetry"],"sources":["../../src/util/hub.ts"],"sourcesContent":["import pRetry from \"./p-retry/index.js\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { FileLoader, LoadValues } from \"./load.js\";\nimport { extname } from \"./extname.js\";\n\nconst fetchWithTimeout = async (\n url: string,\n init: Omit<RequestInit, \"signal\"> & { timeout: number }\n) => {\n const { timeout, ...rest } = init;\n const res = await fetch(url, {\n ...rest,\n signal: AbortSignal.timeout(timeout),\n });\n return res;\n};\n\nconst HUB_PATH_REGEX = /lc(@[^:]+)?:\\/\\/(.*)/;\n\nconst URL_PATH_SEPARATOR = \"/\";\n\nexport const loadFromHub = async <T>(\n uri: string,\n loader: FileLoader<T>,\n validPrefix: string,\n validSuffixes: Set<string>,\n values: LoadValues = {}\n): Promise<T | undefined> => {\n const LANGCHAIN_HUB_DEFAULT_REF =\n getEnvironmentVariable(\"LANGCHAIN_HUB_DEFAULT_REF\") ?? \"master\";\n const LANGCHAIN_HUB_URL_BASE =\n getEnvironmentVariable(\"LANGCHAIN_HUB_URL_BASE\") ??\n \"https://raw.githubusercontent.com/hwchase17/langchain-hub/\";\n\n const match = uri.match(HUB_PATH_REGEX);\n if (!match) {\n return undefined;\n }\n const [rawRef, remotePath] = match.slice(1);\n const ref = rawRef ? rawRef.slice(1) : LANGCHAIN_HUB_DEFAULT_REF;\n const parts = remotePath.split(URL_PATH_SEPARATOR);\n if (parts[0] !== validPrefix) {\n return undefined;\n }\n\n if (!validSuffixes.has(extname(remotePath).slice(1))) {\n throw new Error(\"Unsupported file type.\");\n }\n\n const url = [LANGCHAIN_HUB_URL_BASE, ref, remotePath].join(\"/\");\n const res = await pRetry(() => fetchWithTimeout(url, { timeout: 5000 }), {\n retries: 6,\n });\n if (res.status !== 200) {\n throw new Error(`Could not find file at ${url}`);\n }\n\n return loader(await res.text(), remotePath, values);\n};\n"],"mappings":";;;;;AAKA,MAAM,mBAAmB,OACvB,KACA,SACG;CACH,MAAM,EAAE,SAAS,GAAG,SAAS;AAK7B,
|
|
1
|
+
{"version":3,"file":"hub.cjs","names":["extname","pRetry"],"sources":["../../src/util/hub.ts"],"sourcesContent":["import pRetry from \"./p-retry/index.js\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { FileLoader, LoadValues } from \"./load.js\";\nimport { extname } from \"./extname.js\";\n\nconst fetchWithTimeout = async (\n url: string,\n init: Omit<RequestInit, \"signal\"> & { timeout: number }\n) => {\n const { timeout, ...rest } = init;\n const res = await fetch(url, {\n ...rest,\n signal: AbortSignal.timeout(timeout),\n });\n return res;\n};\n\nconst HUB_PATH_REGEX = /lc(@[^:]+)?:\\/\\/(.*)/;\n\nconst URL_PATH_SEPARATOR = \"/\";\n\nexport const loadFromHub = async <T>(\n uri: string,\n loader: FileLoader<T>,\n validPrefix: string,\n validSuffixes: Set<string>,\n values: LoadValues = {}\n): Promise<T | undefined> => {\n const LANGCHAIN_HUB_DEFAULT_REF =\n getEnvironmentVariable(\"LANGCHAIN_HUB_DEFAULT_REF\") ?? \"master\";\n const LANGCHAIN_HUB_URL_BASE =\n getEnvironmentVariable(\"LANGCHAIN_HUB_URL_BASE\") ??\n \"https://raw.githubusercontent.com/hwchase17/langchain-hub/\";\n\n const match = uri.match(HUB_PATH_REGEX);\n if (!match) {\n return undefined;\n }\n const [rawRef, remotePath] = match.slice(1);\n const ref = rawRef ? rawRef.slice(1) : LANGCHAIN_HUB_DEFAULT_REF;\n const parts = remotePath.split(URL_PATH_SEPARATOR);\n if (parts[0] !== validPrefix) {\n return undefined;\n }\n\n if (!validSuffixes.has(extname(remotePath).slice(1))) {\n throw new Error(\"Unsupported file type.\");\n }\n\n const url = [LANGCHAIN_HUB_URL_BASE, ref, remotePath].join(\"/\");\n const res = await pRetry(() => fetchWithTimeout(url, { timeout: 5000 }), {\n retries: 6,\n });\n if (res.status !== 200) {\n throw new Error(`Could not find file at ${url}`);\n }\n\n return loader(await res.text(), remotePath, values);\n};\n"],"mappings":";;;;;AAKA,MAAM,mBAAmB,OACvB,KACA,SACG;CACH,MAAM,EAAE,SAAS,GAAG,SAAS;AAK7B,QAAO,MAJW,MAAM,KAAK;EAC3B,GAAG;EACH,QAAQ,YAAY,QAAQ,QAAQ;EACrC,CAAC;;AAIJ,MAAM,iBAAiB;AAEvB,MAAM,qBAAqB;AAE3B,MAAa,cAAc,OACzB,KACA,QACA,aACA,eACA,SAAqB,EAAE,KACI;CAC3B,MAAM,6BAAA,GAAA,0BAAA,wBACmB,4BAA4B,IAAI;CACzD,MAAM,0BAAA,GAAA,0BAAA,wBACmB,yBAAyB,IAChD;CAEF,MAAM,QAAQ,IAAI,MAAM,eAAe;AACvC,KAAI,CAAC,MACH;CAEF,MAAM,CAAC,QAAQ,cAAc,MAAM,MAAM,EAAE;CAC3C,MAAM,MAAM,SAAS,OAAO,MAAM,EAAE,GAAG;AAEvC,KADc,WAAW,MAAM,mBACtB,CAAC,OAAO,YACf;AAGF,KAAI,CAAC,cAAc,IAAIA,gBAAAA,QAAQ,WAAW,CAAC,MAAM,EAAE,CAAC,CAClD,OAAM,IAAI,MAAM,yBAAyB;CAG3C,MAAM,MAAM;EAAC;EAAwB;EAAK;EAAW,CAAC,KAAK,IAAI;CAC/D,MAAM,MAAM,MAAMC,cAAAA,cAAa,iBAAiB,KAAK,EAAE,SAAS,KAAM,CAAC,EAAE,EACvE,SAAS,GACV,CAAC;AACF,KAAI,IAAI,WAAW,IACjB,OAAM,IAAI,MAAM,0BAA0B,MAAM;AAGlD,QAAO,OAAO,MAAM,IAAI,MAAM,EAAE,YAAY,OAAO"}
|
package/dist/util/hub.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hub.js","names":[],"sources":["../../src/util/hub.ts"],"sourcesContent":["import pRetry from \"./p-retry/index.js\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { FileLoader, LoadValues } from \"./load.js\";\nimport { extname } from \"./extname.js\";\n\nconst fetchWithTimeout = async (\n url: string,\n init: Omit<RequestInit, \"signal\"> & { timeout: number }\n) => {\n const { timeout, ...rest } = init;\n const res = await fetch(url, {\n ...rest,\n signal: AbortSignal.timeout(timeout),\n });\n return res;\n};\n\nconst HUB_PATH_REGEX = /lc(@[^:]+)?:\\/\\/(.*)/;\n\nconst URL_PATH_SEPARATOR = \"/\";\n\nexport const loadFromHub = async <T>(\n uri: string,\n loader: FileLoader<T>,\n validPrefix: string,\n validSuffixes: Set<string>,\n values: LoadValues = {}\n): Promise<T | undefined> => {\n const LANGCHAIN_HUB_DEFAULT_REF =\n getEnvironmentVariable(\"LANGCHAIN_HUB_DEFAULT_REF\") ?? \"master\";\n const LANGCHAIN_HUB_URL_BASE =\n getEnvironmentVariable(\"LANGCHAIN_HUB_URL_BASE\") ??\n \"https://raw.githubusercontent.com/hwchase17/langchain-hub/\";\n\n const match = uri.match(HUB_PATH_REGEX);\n if (!match) {\n return undefined;\n }\n const [rawRef, remotePath] = match.slice(1);\n const ref = rawRef ? rawRef.slice(1) : LANGCHAIN_HUB_DEFAULT_REF;\n const parts = remotePath.split(URL_PATH_SEPARATOR);\n if (parts[0] !== validPrefix) {\n return undefined;\n }\n\n if (!validSuffixes.has(extname(remotePath).slice(1))) {\n throw new Error(\"Unsupported file type.\");\n }\n\n const url = [LANGCHAIN_HUB_URL_BASE, ref, remotePath].join(\"/\");\n const res = await pRetry(() => fetchWithTimeout(url, { timeout: 5000 }), {\n retries: 6,\n });\n if (res.status !== 200) {\n throw new Error(`Could not find file at ${url}`);\n }\n\n return loader(await res.text(), remotePath, values);\n};\n"],"mappings":";;;;AAKA,MAAM,mBAAmB,OACvB,KACA,SACG;CACH,MAAM,EAAE,SAAS,GAAG,SAAS;AAK7B,
|
|
1
|
+
{"version":3,"file":"hub.js","names":[],"sources":["../../src/util/hub.ts"],"sourcesContent":["import pRetry from \"./p-retry/index.js\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { FileLoader, LoadValues } from \"./load.js\";\nimport { extname } from \"./extname.js\";\n\nconst fetchWithTimeout = async (\n url: string,\n init: Omit<RequestInit, \"signal\"> & { timeout: number }\n) => {\n const { timeout, ...rest } = init;\n const res = await fetch(url, {\n ...rest,\n signal: AbortSignal.timeout(timeout),\n });\n return res;\n};\n\nconst HUB_PATH_REGEX = /lc(@[^:]+)?:\\/\\/(.*)/;\n\nconst URL_PATH_SEPARATOR = \"/\";\n\nexport const loadFromHub = async <T>(\n uri: string,\n loader: FileLoader<T>,\n validPrefix: string,\n validSuffixes: Set<string>,\n values: LoadValues = {}\n): Promise<T | undefined> => {\n const LANGCHAIN_HUB_DEFAULT_REF =\n getEnvironmentVariable(\"LANGCHAIN_HUB_DEFAULT_REF\") ?? \"master\";\n const LANGCHAIN_HUB_URL_BASE =\n getEnvironmentVariable(\"LANGCHAIN_HUB_URL_BASE\") ??\n \"https://raw.githubusercontent.com/hwchase17/langchain-hub/\";\n\n const match = uri.match(HUB_PATH_REGEX);\n if (!match) {\n return undefined;\n }\n const [rawRef, remotePath] = match.slice(1);\n const ref = rawRef ? rawRef.slice(1) : LANGCHAIN_HUB_DEFAULT_REF;\n const parts = remotePath.split(URL_PATH_SEPARATOR);\n if (parts[0] !== validPrefix) {\n return undefined;\n }\n\n if (!validSuffixes.has(extname(remotePath).slice(1))) {\n throw new Error(\"Unsupported file type.\");\n }\n\n const url = [LANGCHAIN_HUB_URL_BASE, ref, remotePath].join(\"/\");\n const res = await pRetry(() => fetchWithTimeout(url, { timeout: 5000 }), {\n retries: 6,\n });\n if (res.status !== 200) {\n throw new Error(`Could not find file at ${url}`);\n }\n\n return loader(await res.text(), remotePath, values);\n};\n"],"mappings":";;;;AAKA,MAAM,mBAAmB,OACvB,KACA,SACG;CACH,MAAM,EAAE,SAAS,GAAG,SAAS;AAK7B,QAAO,MAJW,MAAM,KAAK;EAC3B,GAAG;EACH,QAAQ,YAAY,QAAQ,QAAQ;EACrC,CAAC;;AAIJ,MAAM,iBAAiB;AAEvB,MAAM,qBAAqB;AAE3B,MAAa,cAAc,OACzB,KACA,QACA,aACA,eACA,SAAqB,EAAE,KACI;CAC3B,MAAM,4BACJ,uBAAuB,4BAA4B,IAAI;CACzD,MAAM,yBACJ,uBAAuB,yBAAyB,IAChD;CAEF,MAAM,QAAQ,IAAI,MAAM,eAAe;AACvC,KAAI,CAAC,MACH;CAEF,MAAM,CAAC,QAAQ,cAAc,MAAM,MAAM,EAAE;CAC3C,MAAM,MAAM,SAAS,OAAO,MAAM,EAAE,GAAG;AAEvC,KADc,WAAW,MAAM,mBACtB,CAAC,OAAO,YACf;AAGF,KAAI,CAAC,cAAc,IAAI,QAAQ,WAAW,CAAC,MAAM,EAAE,CAAC,CAClD,OAAM,IAAI,MAAM,yBAAyB;CAG3C,MAAM,MAAM;EAAC;EAAwB;EAAK;EAAW,CAAC,KAAK,IAAI;CAC/D,MAAM,MAAM,MAAM,aAAa,iBAAiB,KAAK,EAAE,SAAS,KAAM,CAAC,EAAE,EACvE,SAAS,GACV,CAAC;AACF,KAAI,IAAI,WAAW,IACjB,OAAM,IAAI,MAAM,0BAA0B,MAAM;AAGlD,QAAO,OAAO,MAAM,IAAI,MAAM,EAAE,YAAY,OAAO"}
|
package/dist/util/load.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.cjs","names":[],"sources":["../../src/util/load.ts"],"sourcesContent":["// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nexport type FileLoader<T> = (\n text: string,\n filePath: string,\n values: LoadValues\n) => Promise<T>;\n\nexport const loadFromFile = async <T>(\n uri: string,\n loader: FileLoader<T>,\n values: LoadValues = {}\n): Promise<T> => {\n try {\n const fs = await import(\"node:fs/promises\");\n return loader(await fs.readFile(uri, { encoding: \"utf-8\" }), uri, values);\n } catch (e) {\n console.error(e);\n throw new Error(`Could not load file at ${uri}`);\n }\n};\n"],"mappings":";AASA,MAAa,eAAe,OAC1B,KACA,QACA,SAAqB,EAAE,KACR;AACf,KAAI;AAEF,SAAO,OAAO,
|
|
1
|
+
{"version":3,"file":"load.cjs","names":[],"sources":["../../src/util/load.ts"],"sourcesContent":["// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nexport type FileLoader<T> = (\n text: string,\n filePath: string,\n values: LoadValues\n) => Promise<T>;\n\nexport const loadFromFile = async <T>(\n uri: string,\n loader: FileLoader<T>,\n values: LoadValues = {}\n): Promise<T> => {\n try {\n const fs = await import(\"node:fs/promises\");\n return loader(await fs.readFile(uri, { encoding: \"utf-8\" }), uri, values);\n } catch (e) {\n console.error(e);\n throw new Error(`Could not load file at ${uri}`);\n }\n};\n"],"mappings":";AASA,MAAa,eAAe,OAC1B,KACA,QACA,SAAqB,EAAE,KACR;AACf,KAAI;AAEF,SAAO,OAAO,OAAM,MADH,OAAO,qBACD,SAAS,KAAK,EAAE,UAAU,SAAS,CAAC,EAAE,KAAK,OAAO;UAClE,GAAG;AACV,UAAQ,MAAM,EAAE;AAChB,QAAM,IAAI,MAAM,0BAA0B,MAAM"}
|
package/dist/util/load.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.js","names":[],"sources":["../../src/util/load.ts"],"sourcesContent":["// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nexport type FileLoader<T> = (\n text: string,\n filePath: string,\n values: LoadValues\n) => Promise<T>;\n\nexport const loadFromFile = async <T>(\n uri: string,\n loader: FileLoader<T>,\n values: LoadValues = {}\n): Promise<T> => {\n try {\n const fs = await import(\"node:fs/promises\");\n return loader(await fs.readFile(uri, { encoding: \"utf-8\" }), uri, values);\n } catch (e) {\n console.error(e);\n throw new Error(`Could not load file at ${uri}`);\n }\n};\n"],"mappings":";AASA,MAAa,eAAe,OAC1B,KACA,QACA,SAAqB,EAAE,KACR;AACf,KAAI;AAEF,SAAO,OAAO,
|
|
1
|
+
{"version":3,"file":"load.js","names":[],"sources":["../../src/util/load.ts"],"sourcesContent":["// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nexport type FileLoader<T> = (\n text: string,\n filePath: string,\n values: LoadValues\n) => Promise<T>;\n\nexport const loadFromFile = async <T>(\n uri: string,\n loader: FileLoader<T>,\n values: LoadValues = {}\n): Promise<T> => {\n try {\n const fs = await import(\"node:fs/promises\");\n return loader(await fs.readFile(uri, { encoding: \"utf-8\" }), uri, values);\n } catch (e) {\n console.error(e);\n throw new Error(`Could not load file at ${uri}`);\n }\n};\n"],"mappings":";AASA,MAAa,eAAe,OAC1B,KACA,QACA,SAAqB,EAAE,KACR;AACf,KAAI;AAEF,SAAO,OAAO,OAAM,MADH,OAAO,qBACD,SAAS,KAAK,EAAE,UAAU,SAAS,CAAC,EAAE,KAAK,OAAO;UAClE,GAAG;AACV,UAAQ,MAAM,EAAE;AAChB,QAAM,IAAI,MAAM,0BAA0B,MAAM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.cjs","names":["yaml"],"sources":["../../src/util/openapi.ts"],"sourcesContent":["import * as yaml from \"js-yaml\";\nimport { OpenAPIV3, OpenAPIV3_1 } from \"openapi-types\";\n\nexport class OpenAPISpec {\n constructor(public document: OpenAPIV3_1.Document) {}\n\n get baseUrl() {\n return this.document.servers ? this.document.servers[0].url : undefined;\n }\n\n getPathsStrict() {\n if (!this.document.paths) {\n throw new Error(\"No paths found in spec\");\n }\n return this.document.paths;\n }\n\n getParametersStrict() {\n if (!this.document.components?.parameters) {\n throw new Error(\"No parameters found in spec\");\n }\n return this.document.components.parameters;\n }\n\n getSchemasStrict() {\n if (!this.document.components?.schemas) {\n throw new Error(\"No schemas found in spec.\");\n }\n return this.document.components.schemas;\n }\n\n getRequestBodiesStrict() {\n if (!this.document.components?.requestBodies) {\n throw new Error(\"No request body found in spec.\");\n }\n return this.document.components.requestBodies;\n }\n\n getPathStrict(path: string) {\n const pathItem = this.getPathsStrict()[path];\n if (pathItem === undefined) {\n throw new Error(`No path found for \"${path}\".`);\n }\n return pathItem;\n }\n\n getReferencedParameter(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n if (this.getParametersStrict()[refName] === undefined) {\n throw new Error(`No parameter found for \"${refName}\".`);\n }\n return this.getParametersStrict()[refName];\n }\n\n getRootReferencedParameter(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.ParameterObject {\n let parameter = this.getReferencedParameter(ref);\n while ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n parameter = this.getReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedSchema(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.SchemaObject {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const schema = this.getSchemasStrict()[refName];\n if (schema === undefined) {\n throw new Error(`No schema found for \"${refName}\".`);\n }\n return schema;\n }\n\n getSchema(\n schema: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject\n ): OpenAPIV3_1.SchemaObject {\n if ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema;\n }\n\n getRootReferencedSchema(ref: OpenAPIV3_1.ReferenceObject) {\n let schema = this.getReferencedSchema(ref);\n while ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n schema = this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const requestBodies = this.getRequestBodiesStrict();\n if (requestBodies[refName] === undefined) {\n throw new Error(`No request body found for \"${refName}\"`);\n }\n return requestBodies[refName];\n }\n\n getRootReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n let requestBody = this.getReferencedRequestBody(ref);\n while ((requestBody as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n requestBody = this.getReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n getMethodsForPath(path: string): OpenAPIV3.HttpMethods[] {\n const pathItem = this.getPathStrict(path);\n // This is an enum in the underlying package.\n // Werestate here to allow \"import type\" above and not cause warnings in certain envs.\n const possibleMethods = [\n \"get\",\n \"put\",\n \"post\",\n \"delete\",\n \"options\",\n \"head\",\n \"patch\",\n \"trace\",\n ];\n return possibleMethods.filter(\n (possibleMethod) =>\n pathItem[possibleMethod as OpenAPIV3.HttpMethods] !== undefined\n ) as OpenAPIV3.HttpMethods[];\n }\n\n getParametersForPath(path: string) {\n const pathItem = this.getPathStrict(path);\n if (pathItem.parameters === undefined) {\n return [];\n }\n return pathItem.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getOperation(path: string, method: OpenAPIV3.HttpMethods) {\n const pathItem = this.getPathStrict(path);\n if (pathItem[method] === undefined) {\n throw new Error(`No ${method} method found for \"path\".`);\n }\n return pathItem[method];\n }\n\n getParametersForOperation(operation: OpenAPIV3_1.OperationObject) {\n if (operation.parameters === undefined) {\n return [];\n }\n return operation.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getRequestBodyForOperation(\n operation: OpenAPIV3_1.OperationObject\n ): OpenAPIV3_1.RequestBodyObject {\n const { requestBody } = operation;\n if ((requestBody as OpenAPIV3_1.ReferenceObject)?.$ref !== undefined) {\n return this.getRootReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n static getCleanedOperationId(\n operation: OpenAPIV3_1.OperationObject,\n path: string,\n method: OpenAPIV3_1.HttpMethods\n ) {\n let { operationId } = operation;\n if (operationId === undefined) {\n const updatedPath = path.replaceAll(/[^a-zA-Z0-9]/g, \"_\");\n operationId = `${\n updatedPath.startsWith(\"/\") ? updatedPath.slice(1) : updatedPath\n }_${method}`;\n }\n return operationId\n .replaceAll(/-/g, \"_\")\n .replaceAll(/\\./g, \"_\")\n .replaceAll(/\\//g, \"_\");\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static alertUnsupportedSpec(document: Record<string, any>) {\n const warningMessage =\n \"This may result in degraded performance. Convert your OpenAPI spec to 3.1.0 for better support.\";\n const swaggerVersion = document.swagger;\n const openAPIVersion = document.openapi;\n if (openAPIVersion !== undefined && openAPIVersion !== \"3.1.0\") {\n console.warn(\n `Attempting to load an OpenAPI ${openAPIVersion} spec. ${warningMessage}`\n );\n } else if (swaggerVersion !== undefined) {\n console.warn(\n `Attempting to load a Swagger ${swaggerVersion} spec. ${warningMessage}`\n );\n } else {\n throw new Error(\n `Attempting to load an unsupported spec:\\n\\n${JSON.stringify(\n document,\n null,\n 2\n )}.`\n );\n }\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static fromObject(document: Record<string, any>) {\n OpenAPISpec.alertUnsupportedSpec(document);\n return new OpenAPISpec(document as OpenAPIV3_1.Document);\n }\n\n static fromString(rawString: string) {\n let document;\n try {\n document = JSON.parse(rawString);\n } catch {\n document = yaml.load(rawString);\n }\n return OpenAPISpec.fromObject(document);\n }\n\n static async fromURL(url: string) {\n const response = await fetch(url);\n const rawDocument = await response.text();\n return OpenAPISpec.fromString(rawDocument);\n }\n}\n"],"mappings":";;;;AAGA,IAAa,cAAb,MAAa,YAAY;CACvB,YAAY,UAAuC;AAAhC,OAAA,WAAA;;CAEnB,IAAI,UAAU;AACZ,SAAO,KAAK,SAAS,UAAU,KAAK,SAAS,QAAQ,GAAG,MAAM,KAAA;;CAGhE,iBAAiB;AACf,MAAI,CAAC,KAAK,SAAS,MACjB,OAAM,IAAI,MAAM,yBAAyB;AAE3C,SAAO,KAAK,SAAS;;CAGvB,sBAAsB;AACpB,MAAI,CAAC,KAAK,SAAS,YAAY,WAC7B,OAAM,IAAI,MAAM,8BAA8B;AAEhD,SAAO,KAAK,SAAS,WAAW;;CAGlC,mBAAmB;AACjB,MAAI,CAAC,KAAK,SAAS,YAAY,QAC7B,OAAM,IAAI,MAAM,4BAA4B;AAE9C,SAAO,KAAK,SAAS,WAAW;;CAGlC,yBAAyB;AACvB,MAAI,CAAC,KAAK,SAAS,YAAY,cAC7B,OAAM,IAAI,MAAM,iCAAiC;AAEnD,SAAO,KAAK,SAAS,WAAW;;CAGlC,cAAc,MAAc;EAC1B,MAAM,WAAW,KAAK,gBAAgB,CAAC;AACvC,MAAI,aAAa,KAAA,EACf,OAAM,IAAI,MAAM,sBAAsB,KAAK,IAAI;AAEjD,SAAO;;CAGT,uBAAuB,KAAkC;EACvD,MAAM,gBAAgB,IAAI,KAAK,MAAM,IAAI;EACzC,MAAM,UAAU,cAAc,cAAc,SAAS;AACrD,MAAI,KAAK,qBAAqB,CAAC,aAAa,KAAA,EAC1C,OAAM,IAAI,MAAM,2BAA2B,QAAQ,IAAI;AAEzD,SAAO,KAAK,qBAAqB,CAAC;;CAGpC,2BACE,KAC6B;EAC7B,IAAI,YAAY,KAAK,uBAAuB,IAAI;AAChD,SAAQ,UAA0C,SAAS,KAAA,EACzD,a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,IAAI;AACJ,MAAI;AACF,cAAW,KAAK,MAAM,UAAU;UAC1B;AACN,cAAWA,QAAK,KAAK,UAAU;;AAEjC,SAAO,YAAY,WAAW,SAAS;;CAGzC,aAAa,QAAQ,KAAa;EAEhC,MAAM,cAAc,OADH,MAAM,MAAM,IAAI,EACE,MAAM;AACzC,SAAO,YAAY,WAAW,YAAY"}
|
|
1
|
+
{"version":3,"file":"openapi.cjs","names":["yaml"],"sources":["../../src/util/openapi.ts"],"sourcesContent":["import * as yaml from \"js-yaml\";\nimport { OpenAPIV3, OpenAPIV3_1 } from \"openapi-types\";\n\nexport class OpenAPISpec {\n constructor(public document: OpenAPIV3_1.Document) {}\n\n get baseUrl() {\n return this.document.servers ? this.document.servers[0].url : undefined;\n }\n\n getPathsStrict() {\n if (!this.document.paths) {\n throw new Error(\"No paths found in spec\");\n }\n return this.document.paths;\n }\n\n getParametersStrict() {\n if (!this.document.components?.parameters) {\n throw new Error(\"No parameters found in spec\");\n }\n return this.document.components.parameters;\n }\n\n getSchemasStrict() {\n if (!this.document.components?.schemas) {\n throw new Error(\"No schemas found in spec.\");\n }\n return this.document.components.schemas;\n }\n\n getRequestBodiesStrict() {\n if (!this.document.components?.requestBodies) {\n throw new Error(\"No request body found in spec.\");\n }\n return this.document.components.requestBodies;\n }\n\n getPathStrict(path: string) {\n const pathItem = this.getPathsStrict()[path];\n if (pathItem === undefined) {\n throw new Error(`No path found for \"${path}\".`);\n }\n return pathItem;\n }\n\n getReferencedParameter(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n if (this.getParametersStrict()[refName] === undefined) {\n throw new Error(`No parameter found for \"${refName}\".`);\n }\n return this.getParametersStrict()[refName];\n }\n\n getRootReferencedParameter(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.ParameterObject {\n let parameter = this.getReferencedParameter(ref);\n while ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n parameter = this.getReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedSchema(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.SchemaObject {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const schema = this.getSchemasStrict()[refName];\n if (schema === undefined) {\n throw new Error(`No schema found for \"${refName}\".`);\n }\n return schema;\n }\n\n getSchema(\n schema: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject\n ): OpenAPIV3_1.SchemaObject {\n if ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema;\n }\n\n getRootReferencedSchema(ref: OpenAPIV3_1.ReferenceObject) {\n let schema = this.getReferencedSchema(ref);\n while ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n schema = this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const requestBodies = this.getRequestBodiesStrict();\n if (requestBodies[refName] === undefined) {\n throw new Error(`No request body found for \"${refName}\"`);\n }\n return requestBodies[refName];\n }\n\n getRootReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n let requestBody = this.getReferencedRequestBody(ref);\n while ((requestBody as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n requestBody = this.getReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n getMethodsForPath(path: string): OpenAPIV3.HttpMethods[] {\n const pathItem = this.getPathStrict(path);\n // This is an enum in the underlying package.\n // Werestate here to allow \"import type\" above and not cause warnings in certain envs.\n const possibleMethods = [\n \"get\",\n \"put\",\n \"post\",\n \"delete\",\n \"options\",\n \"head\",\n \"patch\",\n \"trace\",\n ];\n return possibleMethods.filter(\n (possibleMethod) =>\n pathItem[possibleMethod as OpenAPIV3.HttpMethods] !== undefined\n ) as OpenAPIV3.HttpMethods[];\n }\n\n getParametersForPath(path: string) {\n const pathItem = this.getPathStrict(path);\n if (pathItem.parameters === undefined) {\n return [];\n }\n return pathItem.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getOperation(path: string, method: OpenAPIV3.HttpMethods) {\n const pathItem = this.getPathStrict(path);\n if (pathItem[method] === undefined) {\n throw new Error(`No ${method} method found for \"path\".`);\n }\n return pathItem[method];\n }\n\n getParametersForOperation(operation: OpenAPIV3_1.OperationObject) {\n if (operation.parameters === undefined) {\n return [];\n }\n return operation.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getRequestBodyForOperation(\n operation: OpenAPIV3_1.OperationObject\n ): OpenAPIV3_1.RequestBodyObject {\n const { requestBody } = operation;\n if ((requestBody as OpenAPIV3_1.ReferenceObject)?.$ref !== undefined) {\n return this.getRootReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n static getCleanedOperationId(\n operation: OpenAPIV3_1.OperationObject,\n path: string,\n method: OpenAPIV3_1.HttpMethods\n ) {\n let { operationId } = operation;\n if (operationId === undefined) {\n const updatedPath = path.replaceAll(/[^a-zA-Z0-9]/g, \"_\");\n operationId = `${\n updatedPath.startsWith(\"/\") ? updatedPath.slice(1) : updatedPath\n }_${method}`;\n }\n return operationId\n .replaceAll(/-/g, \"_\")\n .replaceAll(/\\./g, \"_\")\n .replaceAll(/\\//g, \"_\");\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static alertUnsupportedSpec(document: Record<string, any>) {\n const warningMessage =\n \"This may result in degraded performance. Convert your OpenAPI spec to 3.1.0 for better support.\";\n const swaggerVersion = document.swagger;\n const openAPIVersion = document.openapi;\n if (openAPIVersion !== undefined && openAPIVersion !== \"3.1.0\") {\n console.warn(\n `Attempting to load an OpenAPI ${openAPIVersion} spec. ${warningMessage}`\n );\n } else if (swaggerVersion !== undefined) {\n console.warn(\n `Attempting to load a Swagger ${swaggerVersion} spec. ${warningMessage}`\n );\n } else {\n throw new Error(\n `Attempting to load an unsupported spec:\\n\\n${JSON.stringify(\n document,\n null,\n 2\n )}.`\n );\n }\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static fromObject(document: Record<string, any>) {\n OpenAPISpec.alertUnsupportedSpec(document);\n return new OpenAPISpec(document as OpenAPIV3_1.Document);\n }\n\n static fromString(rawString: string) {\n let document;\n try {\n document = JSON.parse(rawString);\n } catch {\n document = yaml.load(rawString);\n }\n return OpenAPISpec.fromObject(document);\n }\n\n static async fromURL(url: string) {\n const response = await fetch(url);\n const rawDocument = await response.text();\n return OpenAPISpec.fromString(rawDocument);\n }\n}\n"],"mappings":";;;;AAGA,IAAa,cAAb,MAAa,YAAY;CACvB,YAAY,UAAuC;AAAhC,OAAA,WAAA;;CAEnB,IAAI,UAAU;AACZ,SAAO,KAAK,SAAS,UAAU,KAAK,SAAS,QAAQ,GAAG,MAAM,KAAA;;CAGhE,iBAAiB;AACf,MAAI,CAAC,KAAK,SAAS,MACjB,OAAM,IAAI,MAAM,yBAAyB;AAE3C,SAAO,KAAK,SAAS;;CAGvB,sBAAsB;AACpB,MAAI,CAAC,KAAK,SAAS,YAAY,WAC7B,OAAM,IAAI,MAAM,8BAA8B;AAEhD,SAAO,KAAK,SAAS,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;EACA,IAAI,EAAE,gBAAgB;AACtB,MAAI,gBAAgB,KAAA,GAAW;GAC7B,MAAM,cAAc,KAAK,WAAW,iBAAiB,IAAI;AACzD,iBAAc,GACZ,YAAY,WAAW,IAAI,GAAG,YAAY,MAAM,EAAE,GAAG,YACtD,GAAG;;AAEN,SAAO,YACJ,WAAW,MAAM,IAAI,CACrB,WAAW,OAAO,IAAI,CACtB,WAAW,OAAO,IAAI;;CAI3B,OAAO,qBAAqB,UAA+B;EACzD,MAAM,iBACJ;EACF,MAAM,iBAAiB,SAAS;EAChC,MAAM,iBAAiB,SAAS;AAChC,MAAI,mBAAmB,KAAA,KAAa,mBAAmB,QACrD,SAAQ,KACN,iCAAiC,eAAe,SAAS,iBAC1D;WACQ,mBAAmB,KAAA,EAC5B,SAAQ,KACN,gCAAgC,eAAe,SAAS,iBACzD;MAED,OAAM,IAAI,MACR,8CAA8C,KAAK,UACjD,UACA,MACA,EACD,CAAC,GACH;;CAKL,OAAO,WAAW,UAA+B;AAC/C,cAAY,qBAAqB,SAAS;AAC1C,SAAO,IAAI,YAAY,SAAiC;;CAG1D,OAAO,WAAW,WAAmB;EACnC,IAAI;AACJ,MAAI;AACF,cAAW,KAAK,MAAM,UAAU;UAC1B;AACN,cAAWA,QAAK,KAAK,UAAU;;AAEjC,SAAO,YAAY,WAAW,SAAS;;CAGzC,aAAa,QAAQ,KAAa;EAEhC,MAAM,cAAc,OAAM,MADH,MAAM,IAAI,EACE,MAAM;AACzC,SAAO,YAAY,WAAW,YAAY"}
|
package/dist/util/openapi.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.js","names":[],"sources":["../../src/util/openapi.ts"],"sourcesContent":["import * as yaml from \"js-yaml\";\nimport { OpenAPIV3, OpenAPIV3_1 } from \"openapi-types\";\n\nexport class OpenAPISpec {\n constructor(public document: OpenAPIV3_1.Document) {}\n\n get baseUrl() {\n return this.document.servers ? this.document.servers[0].url : undefined;\n }\n\n getPathsStrict() {\n if (!this.document.paths) {\n throw new Error(\"No paths found in spec\");\n }\n return this.document.paths;\n }\n\n getParametersStrict() {\n if (!this.document.components?.parameters) {\n throw new Error(\"No parameters found in spec\");\n }\n return this.document.components.parameters;\n }\n\n getSchemasStrict() {\n if (!this.document.components?.schemas) {\n throw new Error(\"No schemas found in spec.\");\n }\n return this.document.components.schemas;\n }\n\n getRequestBodiesStrict() {\n if (!this.document.components?.requestBodies) {\n throw new Error(\"No request body found in spec.\");\n }\n return this.document.components.requestBodies;\n }\n\n getPathStrict(path: string) {\n const pathItem = this.getPathsStrict()[path];\n if (pathItem === undefined) {\n throw new Error(`No path found for \"${path}\".`);\n }\n return pathItem;\n }\n\n getReferencedParameter(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n if (this.getParametersStrict()[refName] === undefined) {\n throw new Error(`No parameter found for \"${refName}\".`);\n }\n return this.getParametersStrict()[refName];\n }\n\n getRootReferencedParameter(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.ParameterObject {\n let parameter = this.getReferencedParameter(ref);\n while ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n parameter = this.getReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedSchema(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.SchemaObject {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const schema = this.getSchemasStrict()[refName];\n if (schema === undefined) {\n throw new Error(`No schema found for \"${refName}\".`);\n }\n return schema;\n }\n\n getSchema(\n schema: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject\n ): OpenAPIV3_1.SchemaObject {\n if ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema;\n }\n\n getRootReferencedSchema(ref: OpenAPIV3_1.ReferenceObject) {\n let schema = this.getReferencedSchema(ref);\n while ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n schema = this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const requestBodies = this.getRequestBodiesStrict();\n if (requestBodies[refName] === undefined) {\n throw new Error(`No request body found for \"${refName}\"`);\n }\n return requestBodies[refName];\n }\n\n getRootReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n let requestBody = this.getReferencedRequestBody(ref);\n while ((requestBody as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n requestBody = this.getReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n getMethodsForPath(path: string): OpenAPIV3.HttpMethods[] {\n const pathItem = this.getPathStrict(path);\n // This is an enum in the underlying package.\n // Werestate here to allow \"import type\" above and not cause warnings in certain envs.\n const possibleMethods = [\n \"get\",\n \"put\",\n \"post\",\n \"delete\",\n \"options\",\n \"head\",\n \"patch\",\n \"trace\",\n ];\n return possibleMethods.filter(\n (possibleMethod) =>\n pathItem[possibleMethod as OpenAPIV3.HttpMethods] !== undefined\n ) as OpenAPIV3.HttpMethods[];\n }\n\n getParametersForPath(path: string) {\n const pathItem = this.getPathStrict(path);\n if (pathItem.parameters === undefined) {\n return [];\n }\n return pathItem.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getOperation(path: string, method: OpenAPIV3.HttpMethods) {\n const pathItem = this.getPathStrict(path);\n if (pathItem[method] === undefined) {\n throw new Error(`No ${method} method found for \"path\".`);\n }\n return pathItem[method];\n }\n\n getParametersForOperation(operation: OpenAPIV3_1.OperationObject) {\n if (operation.parameters === undefined) {\n return [];\n }\n return operation.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getRequestBodyForOperation(\n operation: OpenAPIV3_1.OperationObject\n ): OpenAPIV3_1.RequestBodyObject {\n const { requestBody } = operation;\n if ((requestBody as OpenAPIV3_1.ReferenceObject)?.$ref !== undefined) {\n return this.getRootReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n static getCleanedOperationId(\n operation: OpenAPIV3_1.OperationObject,\n path: string,\n method: OpenAPIV3_1.HttpMethods\n ) {\n let { operationId } = operation;\n if (operationId === undefined) {\n const updatedPath = path.replaceAll(/[^a-zA-Z0-9]/g, \"_\");\n operationId = `${\n updatedPath.startsWith(\"/\") ? updatedPath.slice(1) : updatedPath\n }_${method}`;\n }\n return operationId\n .replaceAll(/-/g, \"_\")\n .replaceAll(/\\./g, \"_\")\n .replaceAll(/\\//g, \"_\");\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static alertUnsupportedSpec(document: Record<string, any>) {\n const warningMessage =\n \"This may result in degraded performance. Convert your OpenAPI spec to 3.1.0 for better support.\";\n const swaggerVersion = document.swagger;\n const openAPIVersion = document.openapi;\n if (openAPIVersion !== undefined && openAPIVersion !== \"3.1.0\") {\n console.warn(\n `Attempting to load an OpenAPI ${openAPIVersion} spec. ${warningMessage}`\n );\n } else if (swaggerVersion !== undefined) {\n console.warn(\n `Attempting to load a Swagger ${swaggerVersion} spec. ${warningMessage}`\n );\n } else {\n throw new Error(\n `Attempting to load an unsupported spec:\\n\\n${JSON.stringify(\n document,\n null,\n 2\n )}.`\n );\n }\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static fromObject(document: Record<string, any>) {\n OpenAPISpec.alertUnsupportedSpec(document);\n return new OpenAPISpec(document as OpenAPIV3_1.Document);\n }\n\n static fromString(rawString: string) {\n let document;\n try {\n document = JSON.parse(rawString);\n } catch {\n document = yaml.load(rawString);\n }\n return OpenAPISpec.fromObject(document);\n }\n\n static async fromURL(url: string) {\n const response = await fetch(url);\n const rawDocument = await response.text();\n return OpenAPISpec.fromString(rawDocument);\n }\n}\n"],"mappings":";;AAGA,IAAa,cAAb,MAAa,YAAY;CACvB,YAAY,UAAuC;AAAhC,OAAA,WAAA;;CAEnB,IAAI,UAAU;AACZ,SAAO,KAAK,SAAS,UAAU,KAAK,SAAS,QAAQ,GAAG,MAAM,KAAA;;CAGhE,iBAAiB;AACf,MAAI,CAAC,KAAK,SAAS,MACjB,OAAM,IAAI,MAAM,yBAAyB;AAE3C,SAAO,KAAK,SAAS;;CAGvB,sBAAsB;AACpB,MAAI,CAAC,KAAK,SAAS,YAAY,WAC7B,OAAM,IAAI,MAAM,8BAA8B;AAEhD,SAAO,KAAK,SAAS,WAAW;;CAGlC,mBAAmB;AACjB,MAAI,CAAC,KAAK,SAAS,YAAY,QAC7B,OAAM,IAAI,MAAM,4BAA4B;AAE9C,SAAO,KAAK,SAAS,WAAW;;CAGlC,yBAAyB;AACvB,MAAI,CAAC,KAAK,SAAS,YAAY,cAC7B,OAAM,IAAI,MAAM,iCAAiC;AAEnD,SAAO,KAAK,SAAS,WAAW;;CAGlC,cAAc,MAAc;EAC1B,MAAM,WAAW,KAAK,gBAAgB,CAAC;AACvC,MAAI,aAAa,KAAA,EACf,OAAM,IAAI,MAAM,sBAAsB,KAAK,IAAI;AAEjD,SAAO;;CAGT,uBAAuB,KAAkC;EACvD,MAAM,gBAAgB,IAAI,KAAK,MAAM,IAAI;EACzC,MAAM,UAAU,cAAc,cAAc,SA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5B,SAAQ,KACN,gCAAgC,eAAe,SAAS,iBACzD;MAED,OAAM,IAAI,MACR,8CAA8C,KAAK,UACjD,UACA,MACA,EACD,CAAC,GACH;;CAKL,OAAO,WAAW,UAA+B;AAC/C,cAAY,qBAAqB,SAAS;AAC1C,SAAO,IAAI,YAAY,SAAiC;;CAG1D,OAAO,WAAW,WAAmB;EACnC,IAAI;AACJ,MAAI;AACF,cAAW,KAAK,MAAM,UAAU;UAC1B;AACN,cAAW,KAAK,KAAK,UAAU;;AAEjC,SAAO,YAAY,WAAW,SAAS;;CAGzC,aAAa,QAAQ,KAAa;EAEhC,MAAM,cAAc,OADH,MAAM,MAAM,IAAI,EACE,MAAM;AACzC,SAAO,YAAY,WAAW,YAAY"}
|
|
1
|
+
{"version":3,"file":"openapi.js","names":[],"sources":["../../src/util/openapi.ts"],"sourcesContent":["import * as yaml from \"js-yaml\";\nimport { OpenAPIV3, OpenAPIV3_1 } from \"openapi-types\";\n\nexport class OpenAPISpec {\n constructor(public document: OpenAPIV3_1.Document) {}\n\n get baseUrl() {\n return this.document.servers ? this.document.servers[0].url : undefined;\n }\n\n getPathsStrict() {\n if (!this.document.paths) {\n throw new Error(\"No paths found in spec\");\n }\n return this.document.paths;\n }\n\n getParametersStrict() {\n if (!this.document.components?.parameters) {\n throw new Error(\"No parameters found in spec\");\n }\n return this.document.components.parameters;\n }\n\n getSchemasStrict() {\n if (!this.document.components?.schemas) {\n throw new Error(\"No schemas found in spec.\");\n }\n return this.document.components.schemas;\n }\n\n getRequestBodiesStrict() {\n if (!this.document.components?.requestBodies) {\n throw new Error(\"No request body found in spec.\");\n }\n return this.document.components.requestBodies;\n }\n\n getPathStrict(path: string) {\n const pathItem = this.getPathsStrict()[path];\n if (pathItem === undefined) {\n throw new Error(`No path found for \"${path}\".`);\n }\n return pathItem;\n }\n\n getReferencedParameter(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n if (this.getParametersStrict()[refName] === undefined) {\n throw new Error(`No parameter found for \"${refName}\".`);\n }\n return this.getParametersStrict()[refName];\n }\n\n getRootReferencedParameter(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.ParameterObject {\n let parameter = this.getReferencedParameter(ref);\n while ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n parameter = this.getReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedSchema(\n ref: OpenAPIV3_1.ReferenceObject\n ): OpenAPIV3_1.SchemaObject {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const schema = this.getSchemasStrict()[refName];\n if (schema === undefined) {\n throw new Error(`No schema found for \"${refName}\".`);\n }\n return schema;\n }\n\n getSchema(\n schema: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject\n ): OpenAPIV3_1.SchemaObject {\n if ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema;\n }\n\n getRootReferencedSchema(ref: OpenAPIV3_1.ReferenceObject) {\n let schema = this.getReferencedSchema(ref);\n while ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n schema = this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n }\n return schema as OpenAPIV3_1.ParameterObject;\n }\n\n getReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n const refComponents = ref.$ref.split(\"/\");\n const refName = refComponents[refComponents.length - 1];\n const requestBodies = this.getRequestBodiesStrict();\n if (requestBodies[refName] === undefined) {\n throw new Error(`No request body found for \"${refName}\"`);\n }\n return requestBodies[refName];\n }\n\n getRootReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n let requestBody = this.getReferencedRequestBody(ref);\n while ((requestBody as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n requestBody = this.getReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n getMethodsForPath(path: string): OpenAPIV3.HttpMethods[] {\n const pathItem = this.getPathStrict(path);\n // This is an enum in the underlying package.\n // Werestate here to allow \"import type\" above and not cause warnings in certain envs.\n const possibleMethods = [\n \"get\",\n \"put\",\n \"post\",\n \"delete\",\n \"options\",\n \"head\",\n \"patch\",\n \"trace\",\n ];\n return possibleMethods.filter(\n (possibleMethod) =>\n pathItem[possibleMethod as OpenAPIV3.HttpMethods] !== undefined\n ) as OpenAPIV3.HttpMethods[];\n }\n\n getParametersForPath(path: string) {\n const pathItem = this.getPathStrict(path);\n if (pathItem.parameters === undefined) {\n return [];\n }\n return pathItem.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getOperation(path: string, method: OpenAPIV3.HttpMethods) {\n const pathItem = this.getPathStrict(path);\n if (pathItem[method] === undefined) {\n throw new Error(`No ${method} method found for \"path\".`);\n }\n return pathItem[method];\n }\n\n getParametersForOperation(operation: OpenAPIV3_1.OperationObject) {\n if (operation.parameters === undefined) {\n return [];\n }\n return operation.parameters.map((parameter) => {\n if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n return this.getRootReferencedParameter(\n parameter as OpenAPIV3_1.ReferenceObject\n );\n }\n return parameter as OpenAPIV3_1.ParameterObject;\n });\n }\n\n getRequestBodyForOperation(\n operation: OpenAPIV3_1.OperationObject\n ): OpenAPIV3_1.RequestBodyObject {\n const { requestBody } = operation;\n if ((requestBody as OpenAPIV3_1.ReferenceObject)?.$ref !== undefined) {\n return this.getRootReferencedRequestBody(\n requestBody as OpenAPIV3_1.ReferenceObject\n );\n }\n return requestBody as OpenAPIV3_1.RequestBodyObject;\n }\n\n static getCleanedOperationId(\n operation: OpenAPIV3_1.OperationObject,\n path: string,\n method: OpenAPIV3_1.HttpMethods\n ) {\n let { operationId } = operation;\n if (operationId === undefined) {\n const updatedPath = path.replaceAll(/[^a-zA-Z0-9]/g, \"_\");\n operationId = `${\n updatedPath.startsWith(\"/\") ? updatedPath.slice(1) : updatedPath\n }_${method}`;\n }\n return operationId\n .replaceAll(/-/g, \"_\")\n .replaceAll(/\\./g, \"_\")\n .replaceAll(/\\//g, \"_\");\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static alertUnsupportedSpec(document: Record<string, any>) {\n const warningMessage =\n \"This may result in degraded performance. Convert your OpenAPI spec to 3.1.0 for better support.\";\n const swaggerVersion = document.swagger;\n const openAPIVersion = document.openapi;\n if (openAPIVersion !== undefined && openAPIVersion !== \"3.1.0\") {\n console.warn(\n `Attempting to load an OpenAPI ${openAPIVersion} spec. ${warningMessage}`\n );\n } else if (swaggerVersion !== undefined) {\n console.warn(\n `Attempting to load a Swagger ${swaggerVersion} spec. ${warningMessage}`\n );\n } else {\n throw new Error(\n `Attempting to load an unsupported spec:\\n\\n${JSON.stringify(\n document,\n null,\n 2\n )}.`\n );\n }\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n static fromObject(document: Record<string, any>) {\n OpenAPISpec.alertUnsupportedSpec(document);\n return new OpenAPISpec(document as OpenAPIV3_1.Document);\n }\n\n static fromString(rawString: string) {\n let document;\n try {\n document = JSON.parse(rawString);\n } catch {\n document = yaml.load(rawString);\n }\n return OpenAPISpec.fromObject(document);\n }\n\n static async fromURL(url: string) {\n const response = await fetch(url);\n const rawDocument = await response.text();\n return OpenAPISpec.fromString(rawDocument);\n }\n}\n"],"mappings":";;AAGA,IAAa,cAAb,MAAa,YAAY;CACvB,YAAY,UAAuC;AAAhC,OAAA,WAAA;;CAEnB,IAAI,UAAU;AACZ,SAAO,KAAK,SAAS,UAAU,KAAK,SAAS,QAAQ,GAAG,MAAM,KA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RES_PROMPT","SQL_SQLITE_PROMPT","SQL_MYSQL_PROMPT","SQL_MSSQL_PROMPT","SQL_SAP_HANA_PROMPT","SQL_ORACLE_PROMPT","DEFAULT_SQL_DATABASE_PROMPT"],"sources":["../../src/util/sql_utils.ts"],"sourcesContent":["import type { DataSource, DataSourceOptions } from \"typeorm\";\nimport type { PromptTemplate } from \"@langchain/core/prompts\";\nimport {\n DEFAULT_SQL_DATABASE_PROMPT,\n SQL_SAP_HANA_PROMPT,\n SQL_MSSQL_PROMPT,\n SQL_MYSQL_PROMPT,\n SQL_POSTGRES_PROMPT,\n SQL_SQLITE_PROMPT,\n SQL_ORACLE_PROMPT,\n} from \"../chains/sql_db/sql_db_prompt.js\";\n\ninterface RawResultTableAndColumn {\n table_name: string;\n column_name: string;\n data_type: string | undefined;\n is_nullable: string;\n}\n\nexport interface SqlDatabaseParams {\n includesTables?: Array<string>;\n ignoreTables?: Array<string>;\n sampleRowsInTableInfo?: number;\n customDescription?: Record<string, string>;\n}\n\nexport interface SqlDatabaseOptionsParams extends SqlDatabaseParams {\n appDataSourceOptions: DataSourceOptions;\n}\n\nexport interface SqlDatabaseDataSourceParams extends SqlDatabaseParams {\n appDataSource: DataSource;\n}\n\nexport type SerializedSqlDatabase = SqlDatabaseOptionsParams & {\n _type: string;\n};\n\nexport interface SqlTable {\n tableName: string;\n columns: SqlColumn[];\n}\n\nexport interface SqlColumn {\n columnName: string;\n dataType?: string;\n isNullable?: boolean;\n}\n\nexport const verifyListTablesExistInDatabase = (\n tablesFromDatabase: Array<SqlTable>,\n listTables: Array<string>,\n errorPrefixMsg: string\n): void => {\n const onlyTableNames: Array<string> = tablesFromDatabase.map(\n (table: SqlTable) => table.tableName\n );\n if (listTables.length > 0) {\n for (const tableName of listTables) {\n if (!onlyTableNames.includes(tableName)) {\n throw new Error(\n `${errorPrefixMsg} the table ${tableName} was not found in the database`\n );\n }\n }\n }\n};\n\nexport const verifyIncludeTablesExistInDatabase = (\n tablesFromDatabase: Array<SqlTable>,\n includeTables: Array<string>\n): void => {\n verifyListTablesExistInDatabase(\n tablesFromDatabase,\n includeTables,\n \"Include tables not found in database:\"\n );\n};\n\nexport const verifyIgnoreTablesExistInDatabase = (\n tablesFromDatabase: Array<SqlTable>,\n ignoreTables: Array<string>\n): void => {\n verifyListTablesExistInDatabase(\n tablesFromDatabase,\n ignoreTables,\n \"Ignore tables not found in database:\"\n );\n};\n\nconst formatToSqlTable = (\n rawResultsTableAndColumn: Array<RawResultTableAndColumn>\n): Array<SqlTable> => {\n const sqlTable: Array<SqlTable> = [];\n for (const oneResult of rawResultsTableAndColumn) {\n const sqlColumn = {\n columnName: oneResult.column_name,\n dataType: oneResult.data_type,\n isNullable: oneResult.is_nullable === \"YES\",\n };\n const currentTable = sqlTable.find(\n (oneTable) => oneTable.tableName === oneResult.table_name\n );\n if (currentTable) {\n currentTable.columns.push(sqlColumn);\n } else {\n const newTable = {\n tableName: oneResult.table_name,\n columns: [sqlColumn],\n };\n sqlTable.push(newTable);\n }\n }\n\n return sqlTable;\n};\n\nexport const getTableAndColumnsName = async (\n appDataSource: DataSource\n): Promise<Array<SqlTable>> => {\n let sql;\n if (\n appDataSource.options.type === \"postgres\" ||\n appDataSource.options.type === \"aurora-postgres\"\n ) {\n const schema =\n appDataSource.options.type === \"postgres\"\n ? (appDataSource.options?.schema ?? \"public\")\n : \"public\";\n sql = `SELECT \n t.table_name, \n c.* \n FROM \n information_schema.tables t \n JOIN information_schema.columns c \n ON t.table_name = c.table_name \n WHERE \n t.table_schema = '${schema}' \n AND c.table_schema = '${schema}' \n ORDER BY \n t.table_name,\n c.ordinal_position;`;\n const rep = await appDataSource.query(sql);\n\n return formatToSqlTable(rep);\n }\n\n if (\n appDataSource.options.type === \"sqlite\" ||\n appDataSource.options.type === \"better-sqlite3\" ||\n appDataSource.options.type === \"sqljs\"\n ) {\n sql =\n \"SELECT \\n\" +\n \" m.name AS table_name,\\n\" +\n \" p.name AS column_name,\\n\" +\n \" p.type AS data_type,\\n\" +\n \" CASE \\n\" +\n \" WHEN p.\\\"notnull\\\" = 0 THEN 'YES' \\n\" +\n \" ELSE 'NO' \\n\" +\n \" END AS is_nullable \\n\" +\n \"FROM \\n\" +\n \" sqlite_master m \\n\" +\n \"JOIN \\n\" +\n \" pragma_table_info(m.name) p \\n\" +\n \"WHERE \\n\" +\n \" m.type = 'table' AND \\n\" +\n \" m.name NOT LIKE 'sqlite_%';\\n\";\n\n const rep = await appDataSource.query(sql);\n\n return formatToSqlTable(rep);\n }\n\n if (\n appDataSource.options.type === \"mysql\" ||\n appDataSource.options.type === \"aurora-mysql\"\n ) {\n sql =\n \"SELECT \" +\n \"TABLE_NAME AS table_name, \" +\n \"COLUMN_NAME AS column_name, \" +\n \"DATA_TYPE AS data_type, \" +\n \"IS_NULLABLE AS is_nullable \" +\n \"FROM INFORMATION_SCHEMA.COLUMNS \" +\n `WHERE TABLE_SCHEMA = '${appDataSource.options.database}';`;\n\n const rep = await appDataSource.query(sql);\n\n return formatToSqlTable(rep);\n }\n\n if (appDataSource.options.type === \"mssql\") {\n const schema = appDataSource.options?.schema;\n const sql = `SELECT\n TABLE_NAME AS table_name,\n COLUMN_NAME AS column_name,\n DATA_TYPE AS data_type,\n IS_NULLABLE AS is_nullable\n FROM INFORMATION_SCHEMA.COLUMNS\n ${schema && `WHERE TABLE_SCHEMA = '${schema}'`} \nORDER BY TABLE_NAME, ORDINAL_POSITION;`;\n\n const rep = await appDataSource.query(sql);\n return formatToSqlTable(rep);\n }\n\n if (appDataSource.options.type === \"sap\") {\n const schema = appDataSource.options?.schema ?? \"public\";\n sql = `SELECT\n TABLE_NAME,\n COLUMN_NAME,\n DATA_TYPE_NAME AS data_type,\n CASE WHEN IS_NULLABLE='TRUE' THEN 'YES' ELSE 'NO' END AS is_nullable\n FROM TABLE_COLUMNS\n WHERE SCHEMA_NAME='${schema}'`;\n\n const rep: Array<{ [key: string]: string }> =\n await appDataSource.query(sql);\n\n const repLowerCase: Array<RawResultTableAndColumn> = [];\n rep.forEach((_rep) =>\n repLowerCase.push({\n table_name: _rep.TABLE_NAME,\n column_name: _rep.COLUMN_NAME,\n data_type: _rep.DATA_TYPE,\n is_nullable: _rep.IS_NULLABLE,\n })\n );\n\n return formatToSqlTable(repLowerCase);\n }\n if (appDataSource.options.type === \"oracle\") {\n const schemaName = appDataSource.options.schema;\n const sql = ` \n SELECT\n TABLE_NAME AS \"table_name\",\n COLUMN_NAME AS \"column_name\",\n DATA_TYPE AS \"data_type\",\n NULLABLE AS \"is_nullable\"\n FROM ALL_TAB_COLS\n WHERE\n OWNER = UPPER('${schemaName}')`;\n const rep = await appDataSource.query(sql);\n return formatToSqlTable(rep);\n }\n throw new Error(\"Database type not implemented yet\");\n};\n\nconst formatSqlResponseToSimpleTableString = (rawResult: unknown): string => {\n if (!rawResult || !Array.isArray(rawResult) || rawResult.length === 0) {\n return \"\";\n }\n\n let globalString = \"\";\n for (const oneRow of rawResult) {\n globalString += `${Object.values(oneRow).reduce(\n (completeString, columnValue) => `${completeString} ${columnValue}`,\n \"\"\n )}\\n`;\n }\n\n return globalString;\n};\n\nexport const generateTableInfoFromTables = async (\n tables: Array<SqlTable> | undefined,\n appDataSource: DataSource,\n nbSampleRow: number,\n customDescription?: Record<string, string>\n): Promise<string> => {\n if (!tables) {\n return \"\";\n }\n\n let globalString = \"\";\n for (const currentTable of tables) {\n // Add the custom info of the table\n const tableCustomDescription =\n customDescription &&\n Object.keys(customDescription).includes(currentTable.tableName)\n ? `${customDescription[currentTable.tableName]}\\n`\n : \"\";\n // Add the creation of the table in SQL\n let schema = null;\n if (appDataSource.options.type === \"postgres\") {\n schema = appDataSource.options?.schema ?? \"public\";\n } else if (appDataSource.options.type === \"aurora-postgres\") {\n schema = \"public\";\n } else if (appDataSource.options.type === \"mssql\") {\n schema = appDataSource.options?.schema;\n } else if (appDataSource.options.type === \"sap\") {\n schema =\n appDataSource.options?.schema ??\n appDataSource.options?.username ??\n \"public\";\n } else if (appDataSource.options.type === \"oracle\") {\n schema = appDataSource.options.schema;\n }\n let sqlCreateTableQuery = schema\n ? `CREATE TABLE \"${schema}\".\"${currentTable.tableName}\" (\\n`\n : `CREATE TABLE ${currentTable.tableName} (\\n`;\n for (const [key, currentColumn] of currentTable.columns.entries()) {\n if (key > 0) {\n sqlCreateTableQuery += \", \";\n }\n sqlCreateTableQuery += `${currentColumn.columnName} ${\n currentColumn.dataType\n } ${currentColumn.isNullable ? \"\" : \"NOT NULL\"}`;\n }\n sqlCreateTableQuery += \") \\n\";\n\n let sqlSelectInfoQuery;\n if (appDataSource.options.type === \"mysql\") {\n // We use backticks to quote the table names and thus allow for example spaces in table names\n sqlSelectInfoQuery = `SELECT * FROM \\`${currentTable.tableName}\\` LIMIT ${nbSampleRow};\\n`;\n } else if (appDataSource.options.type === \"postgres\") {\n const schema =\n appDataSource.options.type === \"postgres\"\n ? (appDataSource.options?.schema ?? \"public\")\n : \"public\";\n sqlSelectInfoQuery = `SELECT * FROM \"${schema}\".\"${currentTable.tableName}\" LIMIT ${nbSampleRow};\\n`;\n } else if (appDataSource.options.type === \"mssql\") {\n const schema = appDataSource.options?.schema;\n sqlSelectInfoQuery = schema\n ? `SELECT TOP ${nbSampleRow} * FROM ${schema}.[${currentTable.tableName}];\\n`\n : `SELECT TOP ${nbSampleRow} * FROM [${currentTable.tableName}];\\n`;\n } else if (appDataSource.options.type === \"sap\") {\n const schema =\n appDataSource.options?.schema ??\n appDataSource.options?.username ??\n \"public\";\n sqlSelectInfoQuery = `SELECT * FROM \"${schema}\".\"${currentTable.tableName}\" LIMIT ${nbSampleRow};\\n`;\n } else if (appDataSource.options.type === \"oracle\") {\n sqlSelectInfoQuery = `SELECT * FROM \"${schema}\".\"${currentTable.tableName}\" WHERE ROWNUM <= '${nbSampleRow}'`;\n } else {\n sqlSelectInfoQuery = `SELECT * FROM \"${currentTable.tableName}\" LIMIT ${nbSampleRow};\\n`;\n }\n\n const columnNamesConcatString = `${currentTable.columns.reduce(\n (completeString, column) => `${completeString} ${column.columnName}`,\n \"\"\n )}\\n`;\n\n let sample = \"\";\n try {\n const infoObjectResult = nbSampleRow\n ? await appDataSource.query(sqlSelectInfoQuery)\n : null;\n sample = formatSqlResponseToSimpleTableString(infoObjectResult);\n } catch (error) {\n // If the request fails we catch it and only display a log message\n console.log(error);\n }\n\n globalString = globalString.concat(\n tableCustomDescription +\n sqlCreateTableQuery +\n sqlSelectInfoQuery +\n columnNamesConcatString +\n sample\n );\n }\n\n return globalString;\n};\n\nexport const getPromptTemplateFromDataSource = (\n appDataSource: DataSource\n): PromptTemplate => {\n if (\n appDataSource.options.type === \"postgres\" ||\n appDataSource.options.type === \"aurora-postgres\"\n ) {\n return SQL_POSTGRES_PROMPT;\n }\n\n if (appDataSource.options.type === \"sqlite\") {\n return SQL_SQLITE_PROMPT;\n }\n\n if (appDataSource.options.type === \"mysql\") {\n return SQL_MYSQL_PROMPT;\n }\n\n if (appDataSource.options.type === \"mssql\") {\n return SQL_MSSQL_PROMPT;\n }\n\n if (appDataSource.options.type === \"sap\") {\n return SQL_SAP_HANA_PROMPT;\n }\n\n if (appDataSource.options.type === \"oracle\") {\n return SQL_ORACLE_PROMPT;\n }\n\n return DEFAULT_SQL_DATABASE_PROMPT;\n};\n"],"mappings":";;AAiDA,MAAa,mCACX,oBACA,YACA,mBACS;CACT,MAAM,iBAAgC,mBAAmB,KACtD,UAAoB,MAAM,UAC5B;AACD,KAAI,WAAW,SAAS;OACjB,MAAM,aAAa,WACtB,KAAI,CAAC,eAAe,SAAS,UAAU,CACrC,OAAM,IAAI,MACR,GAAG,eAAe,aAAa,UAAU,gCAC1C;;;AAMT,MAAa,sCACX,oBACA,kBACS;AACT,iCACE,oBACA,eACA,wCACD;;AAGH,MAAa,qCACX,oBACA,iBACS;AACT,iCACE,oBACA,cACA,uCACD;;AAGH,MAAM,oBACJ,6BACoB;CACpB,MAAM,WAA4B,EAAE;AACpC,MAAK,MAAM,aAAa,0BAA0B;EAChD,MAAM,YAAY;GAChB,YAAY,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AAA;AAGT,KAAI,cAAc,QAAQ,SAAS,QACjC,QAAOC,sBAAAA;AAGT,KAAI,cAAc,QAAQ,SAAS,QACjC,QAAOC,sBAAAA;AAGT,KAAI,cAAc,QAAQ,SAAS,MACjC,QAAOC,sBAAAA;AAGT,KAAI,cAAc,QAAQ,SAAS,SACjC,QAAOC,sBAAAA;AAGT,QAAOC,sBAAAA"}
|
|
1
|
+
{"version":3,"file":"sql_utils.cjs","names":["SQL_POSTGRES_PROMPT","SQL_SQLITE_PROMPT","SQL_MYSQL_PROMPT","SQL_MSSQL_PROMPT","SQL_SAP_HANA_PROMPT","SQL_ORACLE_PROMPT","DEFAULT_SQL_DATABASE_PROMPT"],"sources":["../../src/util/sql_utils.ts"],"sourcesContent":["import type { DataSource, DataSourceOptions } from \"typeorm\";\nimport type { PromptTemplate } from \"@langchain/core/prompts\";\nimport {\n DEFAULT_SQL_DATABASE_PROMPT,\n SQL_SAP_HANA_PROMPT,\n SQL_MSSQL_PROMPT,\n SQL_MYSQL_PROMPT,\n SQL_POSTGRES_PROMPT,\n SQL_SQLITE_PROMPT,\n SQL_ORACLE_PROMPT,\n} from \"../chains/sql_db/sql_db_prompt.js\";\n\ninterface RawResultTableAndColumn {\n table_name: string;\n column_name: string;\n data_type: string | undefined;\n is_nullable: string;\n}\n\nexport interface SqlDatabaseParams {\n includesTables?: Array<string>;\n ignoreTables?: Array<string>;\n sampleRowsInTableInfo?: number;\n customDescription?: Record<string, string>;\n}\n\nexport interface SqlDatabaseOptionsParams extends SqlDatabaseParams {\n appDataSourceOptions: DataSourceOptions;\n}\n\nexport interface SqlDatabaseDataSourceParams extends SqlDatabaseParams {\n appDataSource: DataSource;\n}\n\nexport type SerializedSqlDatabase = SqlDatabaseOptionsParams & {\n _type: string;\n};\n\nexport interface SqlTable {\n tableName: string;\n columns: SqlColumn[];\n}\n\nexport interface SqlColumn {\n columnName: string;\n dataType?: string;\n isNullable?: boolean;\n}\n\nexport const verifyListTablesExistInDatabase = (\n tablesFromDatabase: Array<SqlTable>,\n listTables: Array<string>,\n errorPrefixMsg: string\n): void => {\n const onlyTableNames: Array<string> = tablesFromDatabase.map(\n (table: SqlTable) => table.tableName\n );\n if (listTables.length > 0) {\n for (const tableName of listTables) {\n if (!onlyTableNames.includes(tableName)) {\n throw new Error(\n `${errorPrefixMsg} the table ${tableName} was not found in the database`\n );\n }\n }\n }\n};\n\nexport const verifyIncludeTablesExistInDatabase = (\n tablesFromDatabase: Array<SqlTable>,\n includeTables: Array<string>\n): void => {\n verifyListTablesExistInDatabase(\n tablesFromDatabase,\n includeTables,\n \"Include tables not found in database:\"\n );\n};\n\nexport const verifyIgnoreTablesExistInDatabase = (\n tablesFromDatabase: Array<SqlTable>,\n ignoreTables: Array<string>\n): void => {\n verifyListTablesExistInDatabase(\n tablesFromDatabase,\n ignoreTables,\n \"Ignore tables not found in database:\"\n );\n};\n\nconst formatToSqlTable = (\n rawResultsTableAndColumn: Array<RawResultTableAndColumn>\n): Array<SqlTable> => {\n const sqlTable: Array<SqlTable> = [];\n for (const oneResult of rawResultsTableAndColumn) {\n const sqlColumn = {\n columnName: oneResult.column_name,\n dataType: oneResult.data_type,\n isNullable: oneResult.is_nullable === \"YES\",\n };\n const currentTable = sqlTable.find(\n (oneTable) => oneTable.tableName === oneResult.table_name\n );\n if (currentTable) {\n currentTable.columns.push(sqlColumn);\n } else {\n const newTable = {\n tableName: oneResult.table_name,\n columns: [sqlColumn],\n };\n sqlTable.push(newTable);\n }\n }\n\n return sqlTable;\n};\n\nexport const getTableAndColumnsName = async (\n appDataSource: DataSource\n): Promise<Array<SqlTable>> => {\n let sql;\n if (\n appDataSource.options.type === \"postgres\" ||\n appDataSource.options.type === \"aurora-postgres\"\n ) {\n const schema =\n appDataSource.options.type === \"postgres\"\n ? (appDataSource.options?.schema ?? \"public\")\n : \"public\";\n sql = `SELECT \n t.table_name, \n c.* \n FROM \n information_schema.tables t \n JOIN information_schema.columns c \n ON t.table_name = c.table_name \n WHERE \n t.table_schema = '${schema}' \n AND c.table_schema = '${schema}' \n ORDER BY \n t.table_name,\n c.ordinal_position;`;\n const rep = await appDataSource.query(sql);\n\n return formatToSqlTable(rep);\n }\n\n if (\n appDataSource.options.type === \"sqlite\" ||\n appDataSource.options.type === \"better-sqlite3\" ||\n appDataSource.options.type === \"sqljs\"\n ) {\n sql =\n \"SELECT \\n\" +\n \" m.name AS table_name,\\n\" +\n \" p.name AS column_name,\\n\" +\n \" p.type AS data_type,\\n\" +\n \" CASE \\n\" +\n \" WHEN p.\\\"notnull\\\" = 0 THEN 'YES' \\n\" +\n \" ELSE 'NO' \\n\" +\n \" END AS is_nullable \\n\" +\n \"FROM \\n\" +\n \" sqlite_master m \\n\" +\n \"JOIN \\n\" +\n \" pragma_table_info(m.name) p \\n\" +\n \"WHERE \\n\" +\n \" m.type = 'table' AND \\n\" +\n \" m.name NOT LIKE 'sqlite_%';\\n\";\n\n const rep = await appDataSource.query(sql);\n\n return formatToSqlTable(rep);\n }\n\n if (\n appDataSource.options.type === \"mysql\" ||\n appDataSource.options.type === \"aurora-mysql\"\n ) {\n sql =\n \"SELECT \" +\n \"TABLE_NAME AS table_name, \" +\n \"COLUMN_NAME AS column_name, \" +\n \"DATA_TYPE AS data_type, \" +\n \"IS_NULLABLE AS is_nullable \" +\n \"FROM INFORMATION_SCHEMA.COLUMNS \" +\n `WHERE TABLE_SCHEMA = '${appDataSource.options.database}';`;\n\n const rep = await appDataSource.query(sql);\n\n return formatToSqlTable(rep);\n }\n\n if (appDataSource.options.type === \"mssql\") {\n const schema = appDataSource.options?.schema;\n const sql = `SELECT\n TABLE_NAME AS table_name,\n COLUMN_NAME AS column_name,\n DATA_TYPE AS data_type,\n IS_NULLABLE AS is_nullable\n FROM INFORMATION_SCHEMA.COLUMNS\n ${schema && `WHERE TABLE_SCHEMA = '${schema}'`} \nORDER BY TABLE_NAME, ORDINAL_POSITION;`;\n\n const rep = await appDataSource.query(sql);\n return formatToSqlTable(rep);\n }\n\n if (appDataSource.options.type === \"sap\") {\n const schema = appDataSource.options?.schema ?? \"public\";\n sql = `SELECT\n TABLE_NAME,\n COLUMN_NAME,\n DATA_TYPE_NAME AS data_type,\n CASE WHEN IS_NULLABLE='TRUE' THEN 'YES' ELSE 'NO' END AS is_nullable\n FROM TABLE_COLUMNS\n WHERE SCHEMA_NAME='${schema}'`;\n\n const rep: Array<{ [key: string]: string }> =\n await appDataSource.query(sql);\n\n const repLowerCase: Array<RawResultTableAndColumn> = [];\n rep.forEach((_rep) =>\n repLowerCase.push({\n table_name: _rep.TABLE_NAME,\n column_name: _rep.COLUMN_NAME,\n data_type: _rep.DATA_TYPE,\n is_nullable: _rep.IS_NULLABLE,\n })\n );\n\n return formatToSqlTable(repLowerCase);\n }\n if (appDataSource.options.type === \"oracle\") {\n const schemaName = appDataSource.options.schema;\n const sql = ` \n SELECT\n TABLE_NAME AS \"table_name\",\n COLUMN_NAME AS \"column_name\",\n DATA_TYPE AS \"data_type\",\n NULLABLE AS \"is_nullable\"\n FROM ALL_TAB_COLS\n WHERE\n OWNER = UPPER('${schemaName}')`;\n const rep = await appDataSource.query(sql);\n return formatToSqlTable(rep);\n }\n throw new Error(\"Database type not implemented yet\");\n};\n\nconst formatSqlResponseToSimpleTableString = (rawResult: unknown): string => {\n if (!rawResult || !Array.isArray(rawResult) || rawResult.length === 0) {\n return \"\";\n }\n\n let globalString = \"\";\n for (const oneRow of rawResult) {\n globalString += `${Object.values(oneRow).reduce(\n (completeString, columnValue) => `${completeString} ${columnValue}`,\n \"\"\n )}\\n`;\n }\n\n return globalString;\n};\n\nexport const generateTableInfoFromTables = async (\n tables: Array<SqlTable> | undefined,\n appDataSource: DataSource,\n nbSampleRow: number,\n customDescription?: Record<string, string>\n): Promise<string> => {\n if (!tables) {\n return \"\";\n }\n\n let globalString = \"\";\n for (const currentTable of tables) {\n // Add the custom info of the table\n const tableCustomDescription =\n customDescription &&\n Object.keys(customDescription).includes(currentTable.tableName)\n ? `${customDescription[currentTable.tableName]}\\n`\n : \"\";\n // Add the creation of the table in SQL\n let schema = null;\n if (appDataSource.options.type === \"postgres\") {\n schema = appDataSource.options?.schema ?? \"public\";\n } else if (appDataSource.options.type === \"aurora-postgres\") {\n schema = \"public\";\n } else if (appDataSource.options.type === \"mssql\") {\n schema = appDataSource.options?.schema;\n } else if (appDataSource.options.type === \"sap\") {\n schema =\n appDataSource.options?.schema ??\n appDataSource.options?.username ??\n \"public\";\n } else if (appDataSource.options.type === \"oracle\") {\n schema = appDataSource.options.schema;\n }\n let sqlCreateTableQuery = schema\n ? `CREATE TABLE \"${schema}\".\"${currentTable.tableName}\" (\\n`\n : `CREATE TABLE ${currentTable.tableName} (\\n`;\n for (const [key, currentColumn] of currentTable.columns.entries()) {\n if (key > 0) {\n sqlCreateTableQuery += \", \";\n }\n sqlCreateTableQuery += `${currentColumn.columnName} ${\n currentColumn.dataType\n } ${currentColumn.isNullable ? \"\" : \"NOT NULL\"}`;\n }\n sqlCreateTableQuery += \") \\n\";\n\n let sqlSelectInfoQuery;\n if (appDataSource.options.type === \"mysql\") {\n // We use backticks to quote the table names and thus allow for example spaces in table names\n sqlSelectInfoQuery = `SELECT * FROM \\`${currentTable.tableName}\\` LIMIT ${nbSampleRow};\\n`;\n } else if (appDataSource.options.type === \"postgres\") {\n const schema =\n appDataSource.options.type === \"postgres\"\n ? (appDataSource.options?.schema ?? \"public\")\n : \"public\";\n sqlSelectInfoQuery = `SELECT * FROM \"${schema}\".\"${currentTable.tableName}\" LIMIT ${nbSampleRow};\\n`;\n } else if (appDataSource.options.type === \"mssql\") {\n const schema = appDataSource.options?.schema;\n sqlSelectInfoQuery = schema\n ? `SELECT TOP ${nbSampleRow} * FROM ${schema}.[${currentTable.tableName}];\\n`\n : `SELECT TOP ${nbSampleRow} * FROM [${currentTable.tableName}];\\n`;\n } else if (appDataSource.options.type === \"sap\") {\n const schema =\n appDataSource.options?.schema ??\n appDataSource.options?.username ??\n \"public\";\n sqlSelectInfoQuery = `SELECT * FROM \"${schema}\".\"${currentTable.tableName}\" LIMIT ${nbSampleRow};\\n`;\n } else if (appDataSource.options.type === \"oracle\") {\n sqlSelectInfoQuery = `SELECT * FROM \"${schema}\".\"${currentTable.tableName}\" WHERE ROWNUM <= '${nbSampleRow}'`;\n } else {\n sqlSelectInfoQuery = `SELECT * FROM \"${currentTable.tableName}\" LIMIT ${nbSampleRow};\\n`;\n }\n\n const columnNamesConcatString = `${currentTable.columns.reduce(\n (completeString, column) => `${completeString} ${column.columnName}`,\n \"\"\n )}\\n`;\n\n let sample = \"\";\n try {\n const infoObjectResult = nbSampleRow\n ? await appDataSource.query(sqlSelectInfoQuery)\n : null;\n sample = formatSqlResponseToSimpleTableString(infoObjectResult);\n } catch (error) {\n // If the request fails we catch it and only display a log message\n console.log(error);\n }\n\n globalString = globalString.concat(\n tableCustomDescription +\n sqlCreateTableQuery +\n sqlSelectInfoQuery +\n columnNamesConcatString +\n sample\n );\n }\n\n return globalString;\n};\n\nexport const getPromptTemplateFromDataSource = (\n appDataSource: DataSource\n): PromptTemplate => {\n if (\n appDataSource.options.type === \"postgres\" ||\n appDataSource.options.type === \"aurora-postgres\"\n ) {\n return SQL_POSTGRES_PROMPT;\n }\n\n if (appDataSource.options.type === \"sqlite\") {\n return SQL_SQLITE_PROMPT;\n }\n\n if (appDataSource.options.type === \"mysql\") {\n return SQL_MYSQL_PROMPT;\n }\n\n if (appDataSource.options.type === \"mssql\") {\n return SQL_MSSQL_PROMPT;\n }\n\n if (appDataSource.options.type === \"sap\") {\n return SQL_SAP_HANA_PROMPT;\n }\n\n if (appDataSource.options.type === \"oracle\") {\n return SQL_ORACLE_PROMPT;\n }\n\n return DEFAULT_SQL_DATABASE_PROMPT;\n};\n"],"mappings":";;AAiDA,MAAa,mCACX,oBACA,YACA,mBACS;CACT,MAAM,iBAAgC,mBAAmB,KACtD,UAAoB,MAAM,UAC5B;AACD,KAAI,WAAW,SAAS;OACjB,MAAM,aAAa,WACtB,KAAI,CAAC,eAAe,SAAS,UAAU,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