@tyvm/knowhow 0.0.1
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/.vscode/launch.json +14 -0
- package/CONFIG.md +336 -0
- package/README.md +49 -0
- package/autodoc/chat.mdx +20 -0
- package/autodoc/cli.mdx +11 -0
- package/autodoc/plugins/asana.mdx +47 -0
- package/autodoc/plugins/downloader/downloader.mdx +38 -0
- package/autodoc/plugins/downloader/plugin.mdx +37 -0
- package/autodoc/plugins/downloader/types.mdx +42 -0
- package/autodoc/plugins/embedding.mdx +41 -0
- package/autodoc/plugins/figma.mdx +45 -0
- package/autodoc/plugins/github.mdx +40 -0
- package/autodoc/plugins/jira.mdx +46 -0
- package/autodoc/plugins/language.mdx +37 -0
- package/autodoc/plugins/linear.mdx +35 -0
- package/autodoc/plugins/notion.mdx +38 -0
- package/autodoc/plugins/plugins.mdx +59 -0
- package/autodoc/plugins/types.mdx +51 -0
- package/autodoc/plugins/vim.mdx +39 -0
- package/autodoc/tools/addInternalTools.mdx +1 -0
- package/autodoc/tools/agentCall.mdx +1 -0
- package/autodoc/tools/asana/definitions.mdx +10 -0
- package/autodoc/tools/asana/index.mdx +12 -0
- package/autodoc/tools/askHuman.mdx +1 -0
- package/autodoc/tools/callPlugin.mdx +1 -0
- package/autodoc/tools/embeddingSearch.mdx +1 -0
- package/autodoc/tools/execCommand.mdx +1 -0
- package/autodoc/tools/fileSearch.mdx +1 -0
- package/autodoc/tools/finalAnswer.mdx +1 -0
- package/autodoc/tools/github/definitions.mdx +6 -0
- package/autodoc/tools/github/index.mdx +8 -0
- package/autodoc/tools/index.mdx +14 -0
- package/autodoc/tools/lintFile.mdx +7 -0
- package/autodoc/tools/list.mdx +16 -0
- package/autodoc/tools/modifyFile.mdx +7 -0
- package/autodoc/tools/patch.mdx +9 -0
- package/autodoc/tools/readBlocks.mdx +1 -0
- package/autodoc/tools/readFile.mdx +1 -0
- package/autodoc/tools/scanFile.mdx +1 -0
- package/autodoc/tools/textSearch.mdx +6 -0
- package/autodoc/tools/types/fileblock.mdx +1 -0
- package/autodoc/tools/visionTool.mdx +1 -0
- package/autodoc/tools/writeFile.mdx +1 -0
- package/jest.config.js +18 -0
- package/package.json +89 -0
- package/src/agents/base/base.ts +619 -0
- package/src/agents/base/prompt.ts +26 -0
- package/src/agents/configurable/ConfigAgent.ts +23 -0
- package/src/agents/developer/developer.ts +69 -0
- package/src/agents/index.ts +8 -0
- package/src/agents/interface.ts +11 -0
- package/src/agents/patcher/codebase.md +27 -0
- package/src/agents/patcher/patcher.ts +110 -0
- package/src/agents/researcher/researcher.ts +109 -0
- package/src/agents/tools/addInternalTools.ts +21 -0
- package/src/agents/tools/agentCall.ts +18 -0
- package/src/agents/tools/aiClient.ts +36 -0
- package/src/agents/tools/asana/definitions.ts +199 -0
- package/src/agents/tools/asana/index.ts +108 -0
- package/src/agents/tools/askHuman.ts +8 -0
- package/src/agents/tools/callPlugin.ts +4 -0
- package/src/agents/tools/embeddingSearch.ts +5 -0
- package/src/agents/tools/execCommand.ts +26 -0
- package/src/agents/tools/fileSearch.ts +33 -0
- package/src/agents/tools/finalAnswer.ts +4 -0
- package/src/agents/tools/github/definitions.ts +89 -0
- package/src/agents/tools/github/index.ts +67 -0
- package/src/agents/tools/googleSearch.ts +242 -0
- package/src/agents/tools/index.ts +24 -0
- package/src/agents/tools/language/definitions.ts +97 -0
- package/src/agents/tools/language/index.ts +19 -0
- package/src/agents/tools/lintFile.ts +17 -0
- package/src/agents/tools/list.ts +514 -0
- package/src/agents/tools/loadWebpage.ts +129 -0
- package/src/agents/tools/modifyFile.ts +53 -0
- package/src/agents/tools/patch.ts +672 -0
- package/src/agents/tools/readBlocks.ts +41 -0
- package/src/agents/tools/readFile.ts +39 -0
- package/src/agents/tools/scanFile.ts +16 -0
- package/src/agents/tools/textSearch.ts +21 -0
- package/src/agents/tools/types/fileblock.ts +5 -0
- package/src/agents/tools/visionTool.ts +5 -0
- package/src/agents/tools/writeFile.ts +46 -0
- package/src/agents/vim/vim.ts +153 -0
- package/src/ai.ts +167 -0
- package/src/chat.ts +412 -0
- package/src/cli.ts +85 -0
- package/src/clients/anthropic.ts +399 -0
- package/src/clients/gemini.ts +486 -0
- package/src/clients/http.ts +107 -0
- package/src/clients/index.ts +183 -0
- package/src/clients/knowhow.ts +33 -0
- package/src/clients/openai.ts +253 -0
- package/src/clients/types.ts +91 -0
- package/src/clients/xai.ts +132 -0
- package/src/config.ts +211 -0
- package/src/conversion.ts +140 -0
- package/src/dataset/diffs/README.md +12 -0
- package/src/dataset/diffs/debug-errors.ts +29 -0
- package/src/dataset/diffs/debug.ts +64 -0
- package/src/dataset/diffs/generate.ts +71 -0
- package/src/dataset/diffs/jsonl.ts +54 -0
- package/src/dataset/diffs/test.ts +193 -0
- package/src/embeddings.ts +411 -0
- package/src/hashes.ts +67 -0
- package/src/index.ts +376 -0
- package/src/login.ts +78 -0
- package/src/microphone.ts +136 -0
- package/src/modules/index.ts +37 -0
- package/src/modules/types.ts +48 -0
- package/src/plugins/asana.ts +134 -0
- package/src/plugins/downloader/downloader.ts +204 -0
- package/src/plugins/downloader/plugin.ts +85 -0
- package/src/plugins/downloader/types.ts +85 -0
- package/src/plugins/embedding.ts +42 -0
- package/src/plugins/figma.ts +137 -0
- package/src/plugins/github.ts +153 -0
- package/src/plugins/jira.ts +104 -0
- package/src/plugins/language.ts +100 -0
- package/src/plugins/linear.ts +219 -0
- package/src/plugins/notion.ts +170 -0
- package/src/plugins/plugins.ts +56 -0
- package/src/plugins/types.ts +5 -0
- package/src/plugins/url.ts +63 -0
- package/src/plugins/vim.ts +73 -0
- package/src/prompts/BasicCodeDocumenter.ts +1 -0
- package/src/prompts/BasicProjectDocumenter.ts +1 -0
- package/src/prompts/EmbeddingSeachHelper.ts +1 -0
- package/src/prompts/index.ts +9 -0
- package/src/server/index.ts +14 -0
- package/src/services/AgentService.ts +98 -0
- package/src/services/EmbeddingService.ts +410 -0
- package/src/services/EventService.ts +20 -0
- package/src/services/GitHub.ts +60 -0
- package/src/services/KnowhowClient.ts +113 -0
- package/src/services/Mcp.ts +270 -0
- package/src/services/McpServer.ts +119 -0
- package/src/services/McpWebsocketTransport.ts +64 -0
- package/src/services/S3.ts +126 -0
- package/src/services/Tools.ts +65 -0
- package/src/services/flags.ts +52 -0
- package/src/services/index.ts +8 -0
- package/src/types.ts +225 -0
- package/src/utils/index.ts +108 -0
- package/src/worker.ts +80 -0
- package/tests/integration/figma.test.ts +70 -0
- package/tests/integration/fileblocks/readwrite.test.ts +95 -0
- package/tests/integration/patching/input.txt +145 -0
- package/tests/integration/patching/output.txt +145 -0
- package/tests/integration/patching/patch.txt +15 -0
- package/tests/integration/patching/unseen.txt +80 -0
- package/tests/integration/patching.test.ts +136 -0
- package/tests/languagePlugin.test.ts +74 -0
- package/tests/patching/corrupted.test.ts +78 -0
- package/tests/patching/imports.patch.txt +11 -0
- package/tests/patching/imports.test.ts +43 -0
- package/tests/patching/imports.txt +11 -0
- package/tests/patching/interface.patch.txt +15 -0
- package/tests/patching/interface.txt +7 -0
- package/tests/test.spec.ts +132 -0
- package/ts_build/src/agents/base/base.d.ts +88 -0
- package/ts_build/src/agents/base/base.js +464 -0
- package/ts_build/src/agents/base/base.js.map +1 -0
- package/ts_build/src/agents/base/prompt.d.ts +1 -0
- package/ts_build/src/agents/base/prompt.js +30 -0
- package/ts_build/src/agents/base/prompt.js.map +1 -0
- package/ts_build/src/agents/configurable/ConfigAgent.d.ts +10 -0
- package/ts_build/src/agents/configurable/ConfigAgent.js +25 -0
- package/ts_build/src/agents/configurable/ConfigAgent.js.map +1 -0
- package/ts_build/src/agents/configurable/OpenAIAgent.d.ts +0 -0
- package/ts_build/src/agents/configurable/OpenAIAgent.js +1 -0
- package/ts_build/src/agents/configurable/OpenAIAgent.js.map +1 -0
- package/ts_build/src/agents/developer/developer.d.ts +9 -0
- package/ts_build/src/agents/developer/developer.js +69 -0
- package/ts_build/src/agents/developer/developer.js.map +1 -0
- package/ts_build/src/agents/index.d.ts +7 -0
- package/ts_build/src/agents/index.js +38 -0
- package/ts_build/src/agents/index.js.map +1 -0
- package/ts_build/src/agents/interface.d.ts +10 -0
- package/ts_build/src/agents/interface.js +3 -0
- package/ts_build/src/agents/interface.js.map +1 -0
- package/ts_build/src/agents/patcher/patcher.d.ts +9 -0
- package/ts_build/src/agents/patcher/patcher.js +106 -0
- package/ts_build/src/agents/patcher/patcher.js.map +1 -0
- package/ts_build/src/agents/researcher/researcher.d.ts +9 -0
- package/ts_build/src/agents/researcher/researcher.js +107 -0
- package/ts_build/src/agents/researcher/researcher.js.map +1 -0
- package/ts_build/src/agents/tools/addInternalTools.d.ts +5 -0
- package/ts_build/src/agents/tools/addInternalTools.js +18 -0
- package/ts_build/src/agents/tools/addInternalTools.js.map +1 -0
- package/ts_build/src/agents/tools/agentCall.d.ts +1 -0
- package/ts_build/src/agents/tools/agentCall.js +21 -0
- package/ts_build/src/agents/tools/agentCall.js.map +1 -0
- package/ts_build/src/agents/tools/aiClient.d.ts +6 -0
- package/ts_build/src/agents/tools/aiClient.js +25 -0
- package/ts_build/src/agents/tools/aiClient.js.map +1 -0
- package/ts_build/src/agents/tools/asana/definitions.d.ts +202 -0
- package/ts_build/src/agents/tools/asana/definitions.js +197 -0
- package/ts_build/src/agents/tools/asana/definitions.js.map +1 -0
- package/ts_build/src/agents/tools/asana/index.d.ts +8 -0
- package/ts_build/src/agents/tools/asana/index.js +98 -0
- package/ts_build/src/agents/tools/asana/index.js.map +1 -0
- package/ts_build/src/agents/tools/askHuman.d.ts +1 -0
- package/ts_build/src/agents/tools/askHuman.js +15 -0
- package/ts_build/src/agents/tools/askHuman.js.map +1 -0
- package/ts_build/src/agents/tools/callPlugin.d.ts +1 -0
- package/ts_build/src/agents/tools/callPlugin.js +9 -0
- package/ts_build/src/agents/tools/callPlugin.js.map +1 -0
- package/ts_build/src/agents/tools/client.d.ts +5 -0
- package/ts_build/src/agents/tools/client.js +21 -0
- package/ts_build/src/agents/tools/client.js.map +1 -0
- package/ts_build/src/agents/tools/embeddingSearch.d.ts +1 -0
- package/ts_build/src/agents/tools/embeddingSearch.js +9 -0
- package/ts_build/src/agents/tools/embeddingSearch.js.map +1 -0
- package/ts_build/src/agents/tools/execCommand.d.ts +1 -0
- package/ts_build/src/agents/tools/execCommand.js +25 -0
- package/ts_build/src/agents/tools/execCommand.js.map +1 -0
- package/ts_build/src/agents/tools/fileSearch.d.ts +1 -0
- package/ts_build/src/agents/tools/fileSearch.js +31 -0
- package/ts_build/src/agents/tools/fileSearch.js.map +1 -0
- package/ts_build/src/agents/tools/finalAnswer.d.ts +1 -0
- package/ts_build/src/agents/tools/finalAnswer.js +8 -0
- package/ts_build/src/agents/tools/finalAnswer.js.map +1 -0
- package/ts_build/src/agents/tools/github/definitions.d.ts +47 -0
- package/ts_build/src/agents/tools/github/definitions.js +86 -0
- package/ts_build/src/agents/tools/github/definitions.js.map +1 -0
- package/ts_build/src/agents/tools/github/index.d.ts +789 -0
- package/ts_build/src/agents/tools/github/index.js +56 -0
- package/ts_build/src/agents/tools/github/index.js.map +1 -0
- package/ts_build/src/agents/tools/googleSearch.d.ts +93 -0
- package/ts_build/src/agents/tools/googleSearch.js +117 -0
- package/ts_build/src/agents/tools/googleSearch.js.map +1 -0
- package/ts_build/src/agents/tools/googleSearchTypes.d.ts +74 -0
- package/ts_build/src/agents/tools/googleSearchTypes.js +3 -0
- package/ts_build/src/agents/tools/googleSearchTypes.js.map +1 -0
- package/ts_build/src/agents/tools/index.d.ts +24 -0
- package/ts_build/src/agents/tools/index.js +41 -0
- package/ts_build/src/agents/tools/index.js.map +1 -0
- package/ts_build/src/agents/tools/language/definitions.d.ts +89 -0
- package/ts_build/src/agents/tools/language/definitions.js +95 -0
- package/ts_build/src/agents/tools/language/definitions.js.map +1 -0
- package/ts_build/src/agents/tools/language/index.d.ts +4 -0
- package/ts_build/src/agents/tools/language/index.js +22 -0
- package/ts_build/src/agents/tools/language/index.js.map +1 -0
- package/ts_build/src/agents/tools/lintFile.d.ts +1 -0
- package/ts_build/src/agents/tools/lintFile.js +22 -0
- package/ts_build/src/agents/tools/lintFile.js.map +1 -0
- package/ts_build/src/agents/tools/list.d.ts +2 -0
- package/ts_build/src/agents/tools/list.js +505 -0
- package/ts_build/src/agents/tools/list.js.map +1 -0
- package/ts_build/src/agents/tools/loadWebpage.d.ts +8 -0
- package/ts_build/src/agents/tools/loadWebpage.js +97 -0
- package/ts_build/src/agents/tools/loadWebpage.js.map +1 -0
- package/ts_build/src/agents/tools/modifyFile.d.ts +2 -0
- package/ts_build/src/agents/tools/modifyFile.js +74 -0
- package/ts_build/src/agents/tools/modifyFile.js.map +1 -0
- package/ts_build/src/agents/tools/patch.d.ts +21 -0
- package/ts_build/src/agents/tools/patch.js +443 -0
- package/ts_build/src/agents/tools/patch.js.map +1 -0
- package/ts_build/src/agents/tools/readBlocks.d.ts +2 -0
- package/ts_build/src/agents/tools/readBlocks.js +59 -0
- package/ts_build/src/agents/tools/readBlocks.js.map +1 -0
- package/ts_build/src/agents/tools/readFile.d.ts +1 -0
- package/ts_build/src/agents/tools/readFile.js +46 -0
- package/ts_build/src/agents/tools/readFile.js.map +1 -0
- package/ts_build/src/agents/tools/scanFile.d.ts +1 -0
- package/ts_build/src/agents/tools/scanFile.js +36 -0
- package/ts_build/src/agents/tools/scanFile.js.map +1 -0
- package/ts_build/src/agents/tools/textSearch.d.ts +1 -0
- package/ts_build/src/agents/tools/textSearch.js +22 -0
- package/ts_build/src/agents/tools/textSearch.js.map +1 -0
- package/ts_build/src/agents/tools/types/fileblock.d.ts +5 -0
- package/ts_build/src/agents/tools/types/fileblock.js +3 -0
- package/ts_build/src/agents/tools/types/fileblock.js.map +1 -0
- package/ts_build/src/agents/tools/visionTool.d.ts +1 -0
- package/ts_build/src/agents/tools/visionTool.js +9 -0
- package/ts_build/src/agents/tools/visionTool.js.map +1 -0
- package/ts_build/src/agents/tools/writeFile.d.ts +2 -0
- package/ts_build/src/agents/tools/writeFile.js +61 -0
- package/ts_build/src/agents/tools/writeFile.js.map +1 -0
- package/ts_build/src/agents/vim/vim.d.ts +14 -0
- package/ts_build/src/agents/vim/vim.js +171 -0
- package/ts_build/src/agents/vim/vim.js.map +1 -0
- package/ts_build/src/ai.d.ts +10 -0
- package/ts_build/src/ai.js +102 -0
- package/ts_build/src/ai.js.map +1 -0
- package/ts_build/src/chat.d.ts +13 -0
- package/ts_build/src/chat.js +319 -0
- package/ts_build/src/chat.js.map +1 -0
- package/ts_build/src/cli.d.ts +2 -0
- package/ts_build/src/cli.js +98 -0
- package/ts_build/src/cli.js.map +1 -0
- package/ts_build/src/clients/anthropic.d.ts +31 -0
- package/ts_build/src/clients/anthropic.js +344 -0
- package/ts_build/src/clients/anthropic.js.map +1 -0
- package/ts_build/src/clients/gemini.d.ts +20 -0
- package/ts_build/src/clients/gemini.js +347 -0
- package/ts_build/src/clients/gemini.js.map +1 -0
- package/ts_build/src/clients/http.d.ts +12 -0
- package/ts_build/src/clients/http.js +91 -0
- package/ts_build/src/clients/http.js.map +1 -0
- package/ts_build/src/clients/index.d.ts +47 -0
- package/ts_build/src/clients/index.js +143 -0
- package/ts_build/src/clients/index.js.map +1 -0
- package/ts_build/src/clients/knowhow.d.ts +10 -0
- package/ts_build/src/clients/knowhow.js +24 -0
- package/ts_build/src/clients/knowhow.js.map +1 -0
- package/ts_build/src/clients/openai.d.ts +20 -0
- package/ts_build/src/clients/openai.js +221 -0
- package/ts_build/src/clients/openai.js.map +1 -0
- package/ts_build/src/clients/types.d.ts +89 -0
- package/ts_build/src/clients/types.js +3 -0
- package/ts_build/src/clients/types.js.map +1 -0
- package/ts_build/src/clients/xai.d.ts +22 -0
- package/ts_build/src/clients/xai.js +108 -0
- package/ts_build/src/clients/xai.js.map +1 -0
- package/ts_build/src/config.d.ts +9 -0
- package/ts_build/src/config.js +216 -0
- package/ts_build/src/config.js.map +1 -0
- package/ts_build/src/conversion.d.ts +7 -0
- package/ts_build/src/conversion.js +114 -0
- package/ts_build/src/conversion.js.map +1 -0
- package/ts_build/src/dataset/diffs/debug-errors.d.ts +1 -0
- package/ts_build/src/dataset/diffs/debug-errors.js +28 -0
- package/ts_build/src/dataset/diffs/debug-errors.js.map +1 -0
- package/ts_build/src/dataset/diffs/debug.d.ts +1 -0
- package/ts_build/src/dataset/diffs/debug.js +50 -0
- package/ts_build/src/dataset/diffs/debug.js.map +1 -0
- package/ts_build/src/dataset/diffs/generate.d.ts +1 -0
- package/ts_build/src/dataset/diffs/generate.js +70 -0
- package/ts_build/src/dataset/diffs/generate.js.map +1 -0
- package/ts_build/src/dataset/diffs/jsonl.d.ts +1 -0
- package/ts_build/src/dataset/diffs/jsonl.js +47 -0
- package/ts_build/src/dataset/diffs/jsonl.js.map +1 -0
- package/ts_build/src/dataset/diffs/test.d.ts +1 -0
- package/ts_build/src/dataset/diffs/test.js +114 -0
- package/ts_build/src/dataset/diffs/test.js.map +1 -0
- package/ts_build/src/embeddings.d.ts +19 -0
- package/ts_build/src/embeddings.js +322 -0
- package/ts_build/src/embeddings.js.map +1 -0
- package/ts_build/src/hashes.d.ts +6 -0
- package/ts_build/src/hashes.js +78 -0
- package/ts_build/src/hashes.js.map +1 -0
- package/ts_build/src/index.d.ts +14 -0
- package/ts_build/src/index.js +258 -0
- package/ts_build/src/index.js.map +1 -0
- package/ts_build/src/login.d.ts +2 -0
- package/ts_build/src/login.js +63 -0
- package/ts_build/src/login.js.map +1 -0
- package/ts_build/src/microphone.d.ts +9 -0
- package/ts_build/src/microphone.js +131 -0
- package/ts_build/src/microphone.js.map +1 -0
- package/ts_build/src/modules/index.d.ts +3 -0
- package/ts_build/src/modules/index.js +34 -0
- package/ts_build/src/modules/index.js.map +1 -0
- package/ts_build/src/modules/types.d.ts +37 -0
- package/ts_build/src/modules/types.js +3 -0
- package/ts_build/src/modules/types.js.map +1 -0
- package/ts_build/src/plugins/asana.d.ts +16 -0
- package/ts_build/src/plugins/asana.js +114 -0
- package/ts_build/src/plugins/asana.js.map +1 -0
- package/ts_build/src/plugins/downloader/downloader.d.ts +12 -0
- package/ts_build/src/plugins/downloader/downloader.js +174 -0
- package/ts_build/src/plugins/downloader/downloader.js.map +1 -0
- package/ts_build/src/plugins/downloader/index.d.ts +3 -0
- package/ts_build/src/plugins/downloader/index.js +41 -0
- package/ts_build/src/plugins/downloader/index.js.map +1 -0
- package/ts_build/src/plugins/downloader/plugin.d.ts +8 -0
- package/ts_build/src/plugins/downloader/plugin.js +83 -0
- package/ts_build/src/plugins/downloader/plugin.js.map +1 -0
- package/ts_build/src/plugins/downloader/types.d.ts +82 -0
- package/ts_build/src/plugins/downloader/types.js +79 -0
- package/ts_build/src/plugins/downloader/types.js.map +1 -0
- package/ts_build/src/plugins/embedding.d.ts +5 -0
- package/ts_build/src/plugins/embedding.js +28 -0
- package/ts_build/src/plugins/embedding.js.map +1 -0
- package/ts_build/src/plugins/figma.d.ts +20 -0
- package/ts_build/src/plugins/figma.js +94 -0
- package/ts_build/src/plugins/figma.js.map +1 -0
- package/ts_build/src/plugins/github.d.ts +1223 -0
- package/ts_build/src/plugins/github.js +115 -0
- package/ts_build/src/plugins/github.js.map +1 -0
- package/ts_build/src/plugins/jira.d.ts +15 -0
- package/ts_build/src/plugins/jira.js +94 -0
- package/ts_build/src/plugins/jira.js.map +1 -0
- package/ts_build/src/plugins/language.d.ts +8 -0
- package/ts_build/src/plugins/language.js +69 -0
- package/ts_build/src/plugins/language.js.map +1 -0
- package/ts_build/src/plugins/linear.d.ts +23 -0
- package/ts_build/src/plugins/linear.js +184 -0
- package/ts_build/src/plugins/linear.js.map +1 -0
- package/ts_build/src/plugins/notion.d.ts +32 -0
- package/ts_build/src/plugins/notion.js +131 -0
- package/ts_build/src/plugins/notion.js.map +1 -0
- package/ts_build/src/plugins/plugins.d.ts +11 -0
- package/ts_build/src/plugins/plugins.js +51 -0
- package/ts_build/src/plugins/plugins.js.map +1 -0
- package/ts_build/src/plugins/types.d.ts +5 -0
- package/ts_build/src/plugins/types.js +3 -0
- package/ts_build/src/plugins/types.js.map +1 -0
- package/ts_build/src/plugins/url.d.ts +8 -0
- package/ts_build/src/plugins/url.js +50 -0
- package/ts_build/src/plugins/url.js.map +1 -0
- package/ts_build/src/plugins/vim.d.ts +11 -0
- package/ts_build/src/plugins/vim.js +63 -0
- package/ts_build/src/plugins/vim.js.map +1 -0
- package/ts_build/src/prompts/BasicCodeDocumenter.d.ts +2 -0
- package/ts_build/src/prompts/BasicCodeDocumenter.js +4 -0
- package/ts_build/src/prompts/BasicCodeDocumenter.js.map +1 -0
- package/ts_build/src/prompts/BasicProjectDocumenter.d.ts +2 -0
- package/ts_build/src/prompts/BasicProjectDocumenter.js +4 -0
- package/ts_build/src/prompts/BasicProjectDocumenter.js.map +1 -0
- package/ts_build/src/prompts/EmbeddingSeachHelper.d.ts +2 -0
- package/ts_build/src/prompts/EmbeddingSeachHelper.js +4 -0
- package/ts_build/src/prompts/EmbeddingSeachHelper.js.map +1 -0
- package/ts_build/src/prompts/index.d.ts +13 -0
- package/ts_build/src/prompts/index.js +15 -0
- package/ts_build/src/prompts/index.js.map +1 -0
- package/ts_build/src/server/index.d.ts +1 -0
- package/ts_build/src/server/index.js +17 -0
- package/ts_build/src/server/index.js.map +1 -0
- package/ts_build/src/services/AgentService.d.ts +14 -0
- package/ts_build/src/services/AgentService.js +87 -0
- package/ts_build/src/services/AgentService.js.map +1 -0
- package/ts_build/src/services/EmbeddingService.d.ts +39 -0
- package/ts_build/src/services/EmbeddingService.js +307 -0
- package/ts_build/src/services/EmbeddingService.js.map +1 -0
- package/ts_build/src/services/EventService.d.ts +9 -0
- package/ts_build/src/services/EventService.js +20 -0
- package/ts_build/src/services/EventService.js.map +1 -0
- package/ts_build/src/services/GitHub.d.ts +11 -0
- package/ts_build/src/services/GitHub.js +52 -0
- package/ts_build/src/services/GitHub.js.map +1 -0
- package/ts_build/src/services/KnowhowClient.d.ts +18 -0
- package/ts_build/src/services/KnowhowClient.js +85 -0
- package/ts_build/src/services/KnowhowClient.js.map +1 -0
- package/ts_build/src/services/Mcp.d.ts +377 -0
- package/ts_build/src/services/Mcp.js +221 -0
- package/ts_build/src/services/Mcp.js.map +1 -0
- package/ts_build/src/services/McpServer.d.ts +19 -0
- package/ts_build/src/services/McpServer.js +104 -0
- package/ts_build/src/services/McpServer.js.map +1 -0
- package/ts_build/src/services/McpWebsocketTransport.d.ts +13 -0
- package/ts_build/src/services/McpWebsocketTransport.js +59 -0
- package/ts_build/src/services/McpWebsocketTransport.js.map +1 -0
- package/ts_build/src/services/S3.d.ts +9 -0
- package/ts_build/src/services/S3.js +116 -0
- package/ts_build/src/services/S3.js.map +1 -0
- package/ts_build/src/services/Tools.d.ts +19 -0
- package/ts_build/src/services/Tools.js +51 -0
- package/ts_build/src/services/Tools.js.map +1 -0
- package/ts_build/src/services/flags.d.ts +13 -0
- package/ts_build/src/services/flags.js +48 -0
- package/ts_build/src/services/flags.js.map +1 -0
- package/ts_build/src/services/index.d.ts +8 -0
- package/ts_build/src/services/index.js +38 -0
- package/ts_build/src/services/index.js.map +1 -0
- package/ts_build/src/terminal.d.ts +1 -0
- package/ts_build/src/terminal.js +35 -0
- package/ts_build/src/terminal.js.map +1 -0
- package/ts_build/src/types.d.ts +181 -0
- package/ts_build/src/types.js +94 -0
- package/ts_build/src/types.js.map +1 -0
- package/ts_build/src/utils/index.d.ts +24 -0
- package/ts_build/src/utils/index.js +116 -0
- package/ts_build/src/utils/index.js.map +1 -0
- package/ts_build/src/worker.d.ts +1 -0
- package/ts_build/src/worker.js +68 -0
- package/ts_build/src/worker.js.map +1 -0
- package/ts_build/tests/integration/figma.test.d.ts +1 -0
- package/ts_build/tests/integration/figma.test.js +47 -0
- package/ts_build/tests/integration/figma.test.js.map +1 -0
- package/ts_build/tests/integration/fileblocks/readwrite.test.d.ts +1 -0
- package/ts_build/tests/integration/fileblocks/readwrite.test.js +95 -0
- package/ts_build/tests/integration/fileblocks/readwrite.test.js.map +1 -0
- package/ts_build/tests/integration/patching.test.d.ts +1 -0
- package/ts_build/tests/integration/patching.test.js +117 -0
- package/ts_build/tests/integration/patching.test.js.map +1 -0
- package/ts_build/tests/languagePlugin.test.d.ts +1 -0
- package/ts_build/tests/languagePlugin.test.js +78 -0
- package/ts_build/tests/languagePlugin.test.js.map +1 -0
- package/ts_build/tests/patching/corrupted.test.d.ts +1 -0
- package/ts_build/tests/patching/corrupted.test.js +53 -0
- package/ts_build/tests/patching/corrupted.test.js.map +1 -0
- package/ts_build/tests/patching/imports.test.d.ts +1 -0
- package/ts_build/tests/patching/imports.test.js +61 -0
- package/ts_build/tests/patching/imports.test.js.map +1 -0
- package/ts_build/tests/test.spec.d.ts +1 -0
- package/ts_build/tests/test.spec.js +101 -0
- package/ts_build/tests/test.spec.js.map +1 -0
- package/tsconfig.json +31 -0
- package/tslint.json +11 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import "source-map-support/register";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ChatCompletionMessageParam,
|
|
5
|
+
ChatCompletionToolMessageParam,
|
|
6
|
+
ChatCompletionMessageToolCall,
|
|
7
|
+
} from "openai/resources/chat";
|
|
8
|
+
import { parsePatch, diffLines, applyPatch } from "diff";
|
|
9
|
+
import { PatchingAgent } from "../../agents/patcher/patcher";
|
|
10
|
+
import { ask, writeFile, mkdir } from "../../utils";
|
|
11
|
+
import {
|
|
12
|
+
fixPatch,
|
|
13
|
+
parseHunks,
|
|
14
|
+
categorizeHunks,
|
|
15
|
+
hunksToPatch,
|
|
16
|
+
} from "../../agents/tools/patch";
|
|
17
|
+
import { md5Hash } from "../../hashes";
|
|
18
|
+
const dataset = [];
|
|
19
|
+
|
|
20
|
+
class PatchTestAgent extends PatchingAgent {
|
|
21
|
+
name = "PatchTestAgent";
|
|
22
|
+
|
|
23
|
+
async getInitialMessages(userInput: string) {
|
|
24
|
+
const baseMessages = await super.getInitialMessages(userInput);
|
|
25
|
+
|
|
26
|
+
baseMessages.push({
|
|
27
|
+
content:
|
|
28
|
+
"We are testing the patch tool, attempt to use only that tool with the input provided in the user's message.",
|
|
29
|
+
role: "user",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return baseMessages;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async processToolMessages(toolCall: ChatCompletionMessageToolCall) {
|
|
36
|
+
const toolMessages = await super.processToolMessages(toolCall);
|
|
37
|
+
|
|
38
|
+
toolMessages.push({
|
|
39
|
+
name: "finalAnswer",
|
|
40
|
+
content: "Done",
|
|
41
|
+
});
|
|
42
|
+
return toolMessages;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function testDataset() {
|
|
47
|
+
const patchAgent = new PatchTestAgent();
|
|
48
|
+
|
|
49
|
+
let successCount = 0;
|
|
50
|
+
let attempts = 0;
|
|
51
|
+
const testHash = "";
|
|
52
|
+
|
|
53
|
+
const shuffled = dataset
|
|
54
|
+
.map((value) => ({ value, sort: Math.random() }))
|
|
55
|
+
.sort((a, b) => a.sort - b.sort)
|
|
56
|
+
.map(({ value }) => value);
|
|
57
|
+
|
|
58
|
+
for (const patchData of shuffled) {
|
|
59
|
+
const hash = await md5Hash(patchData.patch);
|
|
60
|
+
|
|
61
|
+
if (testHash && testHash !== hash) {
|
|
62
|
+
// We are loooking to test one case
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const changes = parseHunks(patchData.patch);
|
|
67
|
+
const totalChanges = changes
|
|
68
|
+
.map((hunk) => hunk.additions.length + hunk.subtractions.length)
|
|
69
|
+
.reduce((a, b) => a + b, 0);
|
|
70
|
+
|
|
71
|
+
// Only test small changes
|
|
72
|
+
if (totalChanges > 10) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function testPatchFile(
|
|
77
|
+
filePath: string,
|
|
78
|
+
patch: string
|
|
79
|
+
): Promise<string> {
|
|
80
|
+
// console.log("AI Suggested", filePath, { patch, answer: patchData.patch });
|
|
81
|
+
//
|
|
82
|
+
//
|
|
83
|
+
attempts++;
|
|
84
|
+
|
|
85
|
+
const originalContent = patchData.before;
|
|
86
|
+
const fixedPatch = fixPatch(patchData.before, patch);
|
|
87
|
+
const { validHunks, invalidHunks } = categorizeHunks(
|
|
88
|
+
originalContent,
|
|
89
|
+
patch
|
|
90
|
+
);
|
|
91
|
+
const validPatch = hunksToPatch(validHunks);
|
|
92
|
+
|
|
93
|
+
const updatedContent = applyPatch(originalContent, validPatch);
|
|
94
|
+
const success = !!updatedContent && validHunks.length;
|
|
95
|
+
await saveToolDiagnosticFiles(
|
|
96
|
+
patchData,
|
|
97
|
+
patch,
|
|
98
|
+
fixedPatch,
|
|
99
|
+
validPatch,
|
|
100
|
+
success
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (success) {
|
|
104
|
+
successCount++;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (
|
|
108
|
+
updatedContent === patchData.after ||
|
|
109
|
+
fixedPatch === patchData.patch
|
|
110
|
+
) {
|
|
111
|
+
console.log("SUCCESS");
|
|
112
|
+
console.log("EXACT MATCH");
|
|
113
|
+
} else {
|
|
114
|
+
console.log("AI SUGGESTED");
|
|
115
|
+
console.log(patch);
|
|
116
|
+
|
|
117
|
+
console.log("FIXED PATCH");
|
|
118
|
+
console.log(fixedPatch);
|
|
119
|
+
|
|
120
|
+
console.log("ACTUAL");
|
|
121
|
+
console.log(patchData.patch);
|
|
122
|
+
|
|
123
|
+
console.log("DID PATCH APPLY?", !!updatedContent);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!success) {
|
|
127
|
+
// await ask("Proceed?");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const invalidPatch = hunksToPatch(invalidHunks);
|
|
131
|
+
const invalidHunksMessage = invalidHunks.length
|
|
132
|
+
? `Patch Partially Applied: \n Invalid Hunks: \n${invalidPatch} `
|
|
133
|
+
: "";
|
|
134
|
+
|
|
135
|
+
const appliedMessage = validHunks.length
|
|
136
|
+
? `Valid Hunks Applied: \n${validPatch}`
|
|
137
|
+
: "";
|
|
138
|
+
|
|
139
|
+
return `${invalidHunksMessage} \n ${appliedMessage}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
patchAgent.tools.setFunction("patchFile", testPatchFile);
|
|
143
|
+
|
|
144
|
+
const parsedAnswer = parsePatch(patchData.patch);
|
|
145
|
+
const fileName = parsedAnswer[0].oldFileName;
|
|
146
|
+
|
|
147
|
+
await patchAgent.call(`Can you modify this file:
|
|
148
|
+
file path: ${fileName}
|
|
149
|
+
${patchData.before}
|
|
150
|
+
|
|
151
|
+
so that it would look like this:
|
|
152
|
+
${patchData.after}?`);
|
|
153
|
+
|
|
154
|
+
/*
|
|
155
|
+
*const toolResponses = await patchAgent.processToolMessages({
|
|
156
|
+
* id: "test",
|
|
157
|
+
* type: "function",
|
|
158
|
+
* function: {
|
|
159
|
+
* name: "patchFile",
|
|
160
|
+
* arguments: JSON.stringify({
|
|
161
|
+
* filePath: fileName,
|
|
162
|
+
* patch: patchData.patch,
|
|
163
|
+
* }),
|
|
164
|
+
* },
|
|
165
|
+
*});
|
|
166
|
+
*/
|
|
167
|
+
console.log({ successCount, attempts, total: dataset.length });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function saveToolDiagnosticFiles(
|
|
172
|
+
patchData: typeof dataset[0],
|
|
173
|
+
patch: string,
|
|
174
|
+
fixedPatch: string,
|
|
175
|
+
validPatch: string,
|
|
176
|
+
success
|
|
177
|
+
) {
|
|
178
|
+
const failureDir = success ? "success" : "fail";
|
|
179
|
+
const patchHash = await md5Hash(patchData.patch);
|
|
180
|
+
const dirName = `./src/dataset/diffs/test_files/${failureDir}/${patchHash}`;
|
|
181
|
+
await mkdir(dirName, { recursive: true });
|
|
182
|
+
|
|
183
|
+
await writeFile(`${dirName}/before.ignore.ts`, patchData.before);
|
|
184
|
+
await writeFile(`${dirName}/ai-patch.diff`, patch);
|
|
185
|
+
await writeFile(`${dirName}/actual-patch.diff`, patchData.patch);
|
|
186
|
+
await writeFile(`${dirName}/after.ignore.ts`, patchData.after);
|
|
187
|
+
await writeFile(`${dirName}/ai-fixed-patch.diff`, fixedPatch);
|
|
188
|
+
await writeFile(`${dirName}/ai-valid-patch.diff`, validPatch);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (require.main === module) {
|
|
192
|
+
testDataset();
|
|
193
|
+
}
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import glob from "glob";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { getConfig, loadPrompt } from "./config";
|
|
4
|
+
import { Config, Hashes, Embeddable, EmbeddingBase, Models } from "./types";
|
|
5
|
+
import {
|
|
6
|
+
readFile,
|
|
7
|
+
writeFile,
|
|
8
|
+
fileExists,
|
|
9
|
+
fileStat,
|
|
10
|
+
cosineSimilarity,
|
|
11
|
+
takeFirstNWords,
|
|
12
|
+
} from "./utils";
|
|
13
|
+
import { summarizeTexts, openai, chunkText } from "./ai";
|
|
14
|
+
import { Plugins } from "./plugins/plugins";
|
|
15
|
+
import { md5Hash } from "./hashes";
|
|
16
|
+
import { convertToText } from "./conversion";
|
|
17
|
+
import { Clients } from "./clients";
|
|
18
|
+
|
|
19
|
+
export { cosineSimilarity };
|
|
20
|
+
|
|
21
|
+
export async function loadEmbedding(filePath: string) {
|
|
22
|
+
if (await fileExists(filePath)) {
|
|
23
|
+
return JSON.parse(await readFile(filePath, "utf8")) as Embeddable[];
|
|
24
|
+
}
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function getConfiguredEmbeddingMap() {
|
|
29
|
+
const config = await getConfig();
|
|
30
|
+
const files = Array.from(new Set(config.embedSources.map((s) => s.output)));
|
|
31
|
+
const embeddings: { [filePath: string]: Embeddable[] } = {};
|
|
32
|
+
for (const file of files) {
|
|
33
|
+
if (!embeddings[file]) {
|
|
34
|
+
embeddings[file] = [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const fileEmbeddings = await loadEmbedding(file);
|
|
38
|
+
embeddings[file].push(...fileEmbeddings);
|
|
39
|
+
}
|
|
40
|
+
return embeddings;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function getConfiguredEmbeddings() {
|
|
44
|
+
const config = await getConfig();
|
|
45
|
+
const files = Array.from(new Set(config.embedSources.map((s) => s.output)));
|
|
46
|
+
const embeddings: Embeddable[] = [];
|
|
47
|
+
for (const file of files) {
|
|
48
|
+
const fileEmbeddings = await loadEmbedding(file);
|
|
49
|
+
embeddings.push(...fileEmbeddings);
|
|
50
|
+
}
|
|
51
|
+
return embeddings;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getChunkId(id: string, index: number, chunkSize: number) {
|
|
55
|
+
const split = id.split("-");
|
|
56
|
+
if (
|
|
57
|
+
split.length > 1 &&
|
|
58
|
+
Number.isInteger(parseInt(split[split.length - 1], 10))
|
|
59
|
+
) {
|
|
60
|
+
// already has chunkId
|
|
61
|
+
return id;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return chunkSize ? `${id}-${index}` : id;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function embedSource(
|
|
68
|
+
model: Config["embeddingModel"],
|
|
69
|
+
source: Config["embedSources"][0],
|
|
70
|
+
ignorePattern: string[]
|
|
71
|
+
) {
|
|
72
|
+
if (!source.input) {
|
|
73
|
+
console.log("Skipping", source.output, "with blank input property");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
console.log("Embedding", source.input, "to", source.output);
|
|
78
|
+
let files = await glob.sync(source.input, { ignore: ignorePattern });
|
|
79
|
+
|
|
80
|
+
if (source.kind && files.length === 0) {
|
|
81
|
+
files = [source.input];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
console.log(`Found ${files.length} files`);
|
|
85
|
+
if (files.length > 100) {
|
|
86
|
+
console.error(
|
|
87
|
+
"woah there, that's a lot of files. I'm not going to embed that many"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
console.log(files);
|
|
91
|
+
const embeddings: Embeddable[] = await loadEmbedding(source.output);
|
|
92
|
+
let batch = [];
|
|
93
|
+
let index = 0;
|
|
94
|
+
for (const file of files) {
|
|
95
|
+
index++;
|
|
96
|
+
const shouldSave = batch.length > 20 || index === files.length;
|
|
97
|
+
if (shouldSave) {
|
|
98
|
+
await Promise.all(batch);
|
|
99
|
+
batch = [];
|
|
100
|
+
}
|
|
101
|
+
batch.push(embedKind(model, file, source, embeddings, shouldSave));
|
|
102
|
+
}
|
|
103
|
+
if (batch.length > 0) {
|
|
104
|
+
await Promise.all(batch);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Save one last time just in case
|
|
108
|
+
await saveEmbedding(source.output, embeddings);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function embed(
|
|
112
|
+
model: string,
|
|
113
|
+
id: string,
|
|
114
|
+
text: string,
|
|
115
|
+
metadata: any,
|
|
116
|
+
embeddings: Embeddable[],
|
|
117
|
+
prompt?: string,
|
|
118
|
+
chunkSize?: number,
|
|
119
|
+
uploadMode?: boolean,
|
|
120
|
+
minLength?: number
|
|
121
|
+
): Promise<string[]> {
|
|
122
|
+
let chunks = [text];
|
|
123
|
+
|
|
124
|
+
if (!text) {
|
|
125
|
+
console.log("Skipping", id, "with blank text");
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (chunkSize) {
|
|
130
|
+
chunks = await chunkText(text, chunkSize);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const MAX_CHUNKS = 300;
|
|
134
|
+
|
|
135
|
+
if (chunks.length <= MAX_CHUNKS) {
|
|
136
|
+
// Only use the first few chunks
|
|
137
|
+
chunks = chunks.slice(0, MAX_CHUNKS);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const dontPrune = [];
|
|
141
|
+
const updates = new Array<string>();
|
|
142
|
+
for (let index = 0; index < chunks.length; index++) {
|
|
143
|
+
const chunkId = getChunkId(id, index, chunkSize);
|
|
144
|
+
let textOfChunk = chunks[index];
|
|
145
|
+
|
|
146
|
+
const tooShort = minLength && textOfChunk.length < minLength;
|
|
147
|
+
if (tooShort) {
|
|
148
|
+
console.log("Skipping (too short)", chunkId);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
dontPrune.push(chunkId);
|
|
153
|
+
const alreadyEmbedded = embeddings.find(
|
|
154
|
+
(e) => e.id === chunkId && e.text === textOfChunk
|
|
155
|
+
);
|
|
156
|
+
if (alreadyEmbedded) {
|
|
157
|
+
console.log("Skipping", chunkId);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (prompt) {
|
|
162
|
+
console.log("Summarizing", textOfChunk);
|
|
163
|
+
textOfChunk = await summarizeTexts([textOfChunk], prompt);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let vector = [];
|
|
167
|
+
if (!uploadMode) {
|
|
168
|
+
console.log("Embedding", chunkId);
|
|
169
|
+
const providerEmbeddings = await Clients.createEmbedding("", {
|
|
170
|
+
input: textOfChunk,
|
|
171
|
+
model: model || Models.openai.EmbeddingAda2,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
vector = providerEmbeddings.data[0].embedding;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const embeddable: Embeddable = {
|
|
178
|
+
id: chunkId,
|
|
179
|
+
text: textOfChunk,
|
|
180
|
+
vector,
|
|
181
|
+
metadata: {
|
|
182
|
+
...metadata,
|
|
183
|
+
...(prompt && { text: chunkText }),
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
embeddings.push(embeddable);
|
|
188
|
+
updates.push(chunkId);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// mutate the embedding array
|
|
192
|
+
pruneEmbedding(id, dontPrune, embeddings);
|
|
193
|
+
return updates;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export async function isEmbeddingFile(inputFile: string) {
|
|
197
|
+
const filePath = path.join(__dirname, inputFile);
|
|
198
|
+
const sourceJson = JSON.parse(
|
|
199
|
+
await readFile(inputFile, "utf8")
|
|
200
|
+
) as Embeddable[];
|
|
201
|
+
|
|
202
|
+
const isEmbedding =
|
|
203
|
+
Array.isArray(sourceJson) &&
|
|
204
|
+
sourceJson.every((e) => e.id && e.text && e.metadata);
|
|
205
|
+
|
|
206
|
+
console.log(`Checking file ${inputFile} for embeddings: ${isEmbedding}`);
|
|
207
|
+
return isEmbedding;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function embedJson(
|
|
211
|
+
model: string,
|
|
212
|
+
inputFile: string,
|
|
213
|
+
source: Config["embedSources"][0]
|
|
214
|
+
) {
|
|
215
|
+
const { prompt, output, uploadMode, chunkSize } = source;
|
|
216
|
+
// get all the files in .knowhow/docs
|
|
217
|
+
const filePath = path.join(__dirname, inputFile);
|
|
218
|
+
const sourceJson = JSON.parse(
|
|
219
|
+
await readFile(inputFile, "utf8")
|
|
220
|
+
) as Embeddable[];
|
|
221
|
+
|
|
222
|
+
const embeddings: Embeddable[] = await loadEmbedding(output);
|
|
223
|
+
let updates = [];
|
|
224
|
+
let batch = [];
|
|
225
|
+
|
|
226
|
+
for (const row of sourceJson) {
|
|
227
|
+
if (embeddings.find((e) => e.id === row.id)) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
console.log("Embedding", row.id);
|
|
232
|
+
batch.push(
|
|
233
|
+
embed(
|
|
234
|
+
model,
|
|
235
|
+
row.id,
|
|
236
|
+
row.text,
|
|
237
|
+
row.metadata,
|
|
238
|
+
embeddings,
|
|
239
|
+
prompt,
|
|
240
|
+
chunkSize,
|
|
241
|
+
uploadMode,
|
|
242
|
+
source.minLength
|
|
243
|
+
)
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
let embedded = [];
|
|
247
|
+
if (batch.length > 20) {
|
|
248
|
+
embedded = (await Promise.all(batch)).flat();
|
|
249
|
+
batch = [];
|
|
250
|
+
}
|
|
251
|
+
updates.push(...embedded);
|
|
252
|
+
|
|
253
|
+
if (updates.length > 20) {
|
|
254
|
+
await saveEmbedding(output, embeddings);
|
|
255
|
+
updates = [];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// save in case we missed some
|
|
260
|
+
await Promise.all(batch);
|
|
261
|
+
await saveEmbedding(output, embeddings);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export async function embedKind(
|
|
265
|
+
model: Config["embeddingModel"],
|
|
266
|
+
id: string,
|
|
267
|
+
source: Config["embedSources"][0],
|
|
268
|
+
embeddings = [] as Embeddable[],
|
|
269
|
+
save = true
|
|
270
|
+
) {
|
|
271
|
+
const { prompt, output, uploadMode, chunkSize } = source;
|
|
272
|
+
|
|
273
|
+
const stat = !source.kind && (await fileStat(id));
|
|
274
|
+
if (stat && stat.isDirectory()) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (id.endsWith(".json") && (await isEmbeddingFile(id))) {
|
|
279
|
+
console.log("Embedding JSON", id);
|
|
280
|
+
return embedJson(model, id, source);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const toEmbed = await handleAllKinds(id, source);
|
|
284
|
+
|
|
285
|
+
const updates = [];
|
|
286
|
+
for (const row of toEmbed) {
|
|
287
|
+
const { id: rowId, text, metadata } = row;
|
|
288
|
+
const embedded = await embed(
|
|
289
|
+
model,
|
|
290
|
+
rowId,
|
|
291
|
+
text,
|
|
292
|
+
metadata,
|
|
293
|
+
embeddings,
|
|
294
|
+
prompt,
|
|
295
|
+
chunkSize,
|
|
296
|
+
uploadMode
|
|
297
|
+
);
|
|
298
|
+
updates.push(...embedded);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (save && updates.length > 0) {
|
|
302
|
+
await saveEmbedding(output, embeddings);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async function handleFileKind(filePath: string) {
|
|
307
|
+
return [
|
|
308
|
+
{
|
|
309
|
+
id: filePath,
|
|
310
|
+
text: await convertToText(filePath),
|
|
311
|
+
metadata: {
|
|
312
|
+
filepath: filePath,
|
|
313
|
+
date: new Date().toISOString(),
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async function handleTextKind(contents: string) {
|
|
320
|
+
const hash = await md5Hash(contents);
|
|
321
|
+
return [
|
|
322
|
+
{
|
|
323
|
+
id: hash,
|
|
324
|
+
text: contents,
|
|
325
|
+
metadata: {
|
|
326
|
+
date: new Date().toISOString(),
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export async function handleAllKinds(
|
|
333
|
+
id: string,
|
|
334
|
+
source: Config["embedSources"][0]
|
|
335
|
+
): Promise<Partial<Embeddable>[]> {
|
|
336
|
+
const { input, kind } = source;
|
|
337
|
+
const contents = "";
|
|
338
|
+
const ids = [];
|
|
339
|
+
|
|
340
|
+
if (Plugins.isPlugin(kind)) {
|
|
341
|
+
console.log("Embedding with plugin", kind);
|
|
342
|
+
return Plugins.embed(kind, input);
|
|
343
|
+
}
|
|
344
|
+
switch (kind) {
|
|
345
|
+
case "text":
|
|
346
|
+
return handleTextKind(source.input);
|
|
347
|
+
case "file":
|
|
348
|
+
default:
|
|
349
|
+
return handleFileKind(id);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export async function saveEmbedding(output: string, embeddings: Embeddable[]) {
|
|
354
|
+
const fileString =
|
|
355
|
+
"[" + embeddings.map((e) => JSON.stringify(e)).join(",") + "]";
|
|
356
|
+
await writeFile(output, fileString);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function pruneEmbedding(
|
|
360
|
+
id: string,
|
|
361
|
+
chunkIds: string[],
|
|
362
|
+
embeddings: Embeddable[]
|
|
363
|
+
) {
|
|
364
|
+
const relatedChunks = embeddings.filter((e) => e.id.startsWith(id));
|
|
365
|
+
for (const chunk of relatedChunks) {
|
|
366
|
+
if (!chunkIds.includes(chunk.id)) {
|
|
367
|
+
console.log("Removing", chunk.id);
|
|
368
|
+
const index = embeddings.findIndex((e) => e.id === chunk.id);
|
|
369
|
+
embeddings.splice(index, 1);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return embeddings;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function pruneVector(embeddings: Embeddable[]) {
|
|
376
|
+
for (const entry of embeddings) {
|
|
377
|
+
delete entry.vector;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export function pruneMetadata(embeddings: Embeddable[], characterLimit = 5000) {
|
|
382
|
+
for (const entry of embeddings) {
|
|
383
|
+
for (const key of Object.keys(entry.metadata)) {
|
|
384
|
+
// Remove large metadata to prevent context from being too large
|
|
385
|
+
if (JSON.stringify(entry.metadata[key]).length > characterLimit) {
|
|
386
|
+
delete entry.metadata[key];
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export async function queryEmbedding<E>(
|
|
393
|
+
query: string,
|
|
394
|
+
embeddings: Embeddable<E>[],
|
|
395
|
+
model = Models.openai.EmbeddingAda2
|
|
396
|
+
) {
|
|
397
|
+
const providerEmbeddings = await Clients.createEmbedding("", {
|
|
398
|
+
input: takeFirstNWords(query, 5000),
|
|
399
|
+
model,
|
|
400
|
+
});
|
|
401
|
+
const queryVector = providerEmbeddings.data[0].embedding;
|
|
402
|
+
const results = new Array<EmbeddingBase<E>>();
|
|
403
|
+
for (const embedding of embeddings) {
|
|
404
|
+
const similarity = cosineSimilarity(embedding.vector, queryVector);
|
|
405
|
+
results.push({
|
|
406
|
+
...embedding,
|
|
407
|
+
similarity,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
return results.sort((a, b) => b.similarity - a.similarity);
|
|
411
|
+
}
|
package/src/hashes.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as crypto from "crypto";
|
|
2
|
+
import { Hashes } from "./types";
|
|
3
|
+
import { readFile, writeFile } from "./utils";
|
|
4
|
+
import { convertToText } from "./conversion";
|
|
5
|
+
|
|
6
|
+
export async function getHashes() {
|
|
7
|
+
const hashes = JSON.parse(await readFile(".knowhow/.hashes.json", "utf8"));
|
|
8
|
+
return hashes as Hashes;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function saveHashes(hashes: any) {
|
|
12
|
+
await writeFile(".knowhow/.hashes.json", JSON.stringify(hashes, null, 2));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function md5Hash(str: string) {
|
|
16
|
+
return crypto.createHash("md5").update(str).digest("hex");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function checkNoFilesChanged(
|
|
20
|
+
files: string[],
|
|
21
|
+
promptHash: string,
|
|
22
|
+
hashes: any
|
|
23
|
+
) {
|
|
24
|
+
for (const file of files) {
|
|
25
|
+
// get the hash of the file
|
|
26
|
+
const fileContent = await convertToText(file);
|
|
27
|
+
const fileHash = crypto.createHash("md5").update(fileContent).digest("hex");
|
|
28
|
+
|
|
29
|
+
if (!hashes[file]) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
hashes[file].promptHash === promptHash &&
|
|
35
|
+
hashes[file].fileHash === fileHash
|
|
36
|
+
) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (hashes[file][promptHash] === fileHash) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function saveAllFileHashes(files: string[], promptHash: string) {
|
|
51
|
+
const hashes = await getHashes();
|
|
52
|
+
|
|
53
|
+
for (const file of files) {
|
|
54
|
+
const fileContent = await convertToText(file);
|
|
55
|
+
const fileHash = crypto.createHash("md5").update(fileContent).digest("hex");
|
|
56
|
+
|
|
57
|
+
if (!hashes[file]) {
|
|
58
|
+
hashes[file] = {
|
|
59
|
+
fileHash,
|
|
60
|
+
promptHash,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
hashes[file][promptHash] = fileHash;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
await saveHashes(hashes);
|
|
67
|
+
}
|