-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
382726a
commit 096da9f
Showing
28 changed files
with
382 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...nsion/webview-api/chat-context-processor/strategies/chat-strategy/nodes/web-visit-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { LangchainTool } from '@extension/webview-api/chat-context-processor/types/langchain-message' | ||
import { DocCrawler } from '@extension/webview-api/chat-context-processor/utils/doc-crawler' | ||
import { findCurrentToolsCallParams } from '@extension/webview-api/chat-context-processor/utils/find-current-tools-call-params' | ||
import type { ToolMessage } from '@langchain/core/messages' | ||
import { DynamicStructuredTool } from '@langchain/core/tools' | ||
import { z } from 'zod' | ||
|
||
import { | ||
ChatGraphToolName, | ||
type ChatGraphNode, | ||
type ChatGraphState | ||
} from './state' | ||
|
||
interface WebVisitToolResult { | ||
contents: { url: string; content: string }[] | ||
} | ||
|
||
// eslint-disable-next-line unused-imports/no-unused-vars | ||
export const createWebVisitTool = async (state: ChatGraphState) => { | ||
const getPageContents = async ( | ||
urls: string[] | ||
): Promise<{ url: string; content: string }[]> => { | ||
const docCrawler = new DocCrawler(urls![0]!) | ||
const promises = await Promise.allSettled( | ||
urls.map(async url => ({ | ||
url, | ||
content: | ||
(await docCrawler.getPageContent(url)) || 'Failed to retrieve content' | ||
})) | ||
) | ||
return promises | ||
.filter(promise => promise.status === 'fulfilled') | ||
.map( | ||
promise => | ||
(promise as PromiseFulfilledResult<{ url: string; content: string }>) | ||
.value | ||
) | ||
} | ||
|
||
return new DynamicStructuredTool({ | ||
name: ChatGraphToolName.WebVisit, | ||
description: | ||
'Visit specific web pages and retrieve their content. Use this tool when you need to access and analyze the content of one or more web pages.', | ||
func: async ({ urls }): Promise<WebVisitToolResult> => { | ||
const contents = await getPageContents(urls) | ||
return { contents } | ||
}, | ||
schema: z.object({ | ||
urls: z | ||
.array(z.string().url()) | ||
.describe( | ||
'An array of URLs to visit and retrieve content from. Each URL should be a valid web address.' | ||
) | ||
}) | ||
}) | ||
} | ||
|
||
export const webVisitNode: ChatGraphNode = async state => { | ||
const { messages, chatContext } = state | ||
const { conversations } = chatContext | ||
const lastConversation = conversations.at(-1) | ||
const docContext = lastConversation?.attachments?.docContext | ||
|
||
if (!docContext) return {} | ||
|
||
const webVisitTool = await createWebVisitTool(state) | ||
|
||
if (!webVisitTool) return {} | ||
|
||
const tools: LangchainTool[] = [webVisitTool] | ||
const lastMessage = messages.at(-1) | ||
const toolCalls = findCurrentToolsCallParams(lastMessage, tools) | ||
|
||
if (!toolCalls.length) return {} | ||
|
||
const toolCallsPromises = toolCalls.map(async toolCall => { | ||
const toolMessage = (await webVisitTool.invoke(toolCall)) as ToolMessage | ||
|
||
const result = JSON.parse( | ||
toolMessage?.lc_kwargs.content | ||
) as WebVisitToolResult | ||
|
||
lastConversation.attachments!.docContext.relevantDocs = [ | ||
...lastConversation.attachments!.docContext.relevantDocs, | ||
...result.contents.map(item => ({ | ||
path: item.url, | ||
content: item.content | ||
})) | ||
] | ||
}) | ||
|
||
await Promise.allSettled(toolCallsPromises) | ||
|
||
return { | ||
chatContext | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
src/extension/webview-api/chat-context-processor/types/chat-context/doc-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import type { BaseToolContext } from './base-tool-context' | ||
|
||
export interface DocInfo { | ||
content: string | ||
path: string // file path or url | ||
} | ||
|
||
export interface DocContext extends BaseToolContext { | ||
allowSearchDocSiteUrls: string[] | ||
export interface DocContext { | ||
allowSearchDocSiteNames: string[] | ||
relevantDocs: DocInfo[] | ||
} |
Oops, something went wrong.