Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
Improvement(gmail-tools): added search and read#680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -52,25 +52,77 @@ export const gmailSearchTool: ToolConfig<GmailSearchParams, GmailToolResponse> = | ||
| }), | ||
| }, | ||
| transformResponse: async (response) => { | ||
| transformResponse: async (response, params) => { | ||
| const data = await response.json() | ||
| if (!response.ok) { | ||
| throw new Error(data.error?.message || 'Failed to search emails') | ||
| } | ||
| return { | ||
| success: true, | ||
| output: { | ||
| content: `Found ${data.messages?.length || 0} messages`, | ||
| metadata: { | ||
| results: | ||
| data.messages?.map((msg: any) => ({ | ||
| if (!data.messages || data.messages.length === 0) { | ||
| return { | ||
| success: true, | ||
| output: { | ||
| content: 'No messages found matching your search query.', | ||
| metadata: { | ||
| results: [], | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| try { | ||
| // Fetch full message details for each result | ||
| const messagePromises = data.messages.map(async (msg: any) => { | ||
| const messageResponse = await fetch(`${GMAIL_API_BASE}/messages/${msg.id}?format=full`, { | ||
| headers: { | ||
| Authorization: `Bearer ${params?.accessToken || ''}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }) | ||
| if (!messageResponse.ok) { | ||
| throw new Error(`Failed to fetch details for message ${msg.id}`) | ||
| } | ||
| return await messageResponse.json() | ||
| }) | ||
| const messages = await Promise.all(messagePromises) | ||
| // Process all messages and create a summary | ||
| const processedMessages = messages.map(processMessageForSummary) | ||
| return { | ||
| success: true, | ||
| output: { | ||
| content: createMessagesSummary(processedMessages), | ||
| metadata: { | ||
| results: processedMessages.map((msg) => ({ | ||
| id: msg.id, | ||
| threadId: msg.threadId, | ||
| subject: msg.subject, | ||
| from: msg.from, | ||
| date: msg.date, | ||
| snippet: msg.snippet, | ||
| })), | ||
| }, | ||
| }, | ||
| } | ||
| } catch (error: any) { | ||
| console.error('Error fetching message details:', error) | ||
| return { | ||
| success: true, | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| output: { | ||
| content: `Found ${data.messages.length} messages but couldn't retrieve all details: ${error.message || 'Unknown error'}`, | ||
| metadata: { | ||
| results: data.messages.map((msg: any) => ({ | ||
| id: msg.id, | ||
| threadId: msg.threadId, | ||
| })) || [], | ||
| })), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| }, | ||
| @@ -87,3 +139,52 @@ export const gmailSearchTool: ToolConfig<GmailSearchParams, GmailToolResponse> = | ||
| return error.message || 'An unexpected error occurred while searching emails' | ||
| }, | ||
| } | ||
| // Helper function to process a message for summary (without full content) | ||
| function processMessageForSummary(message: any): any { | ||
aadamgough marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (!message || !message.payload) { | ||
| return { | ||
| id: message?.id || '', | ||
| threadId: message?.threadId || '', | ||
| subject: 'Unknown Subject', | ||
| from: 'Unknown Sender', | ||
| date: '', | ||
| snippet: message?.snippet || '', | ||
| } | ||
| } | ||
| const headers = message.payload.headers || [] | ||
| const subject = | ||
| headers.find((h: any) => h.name.toLowerCase() === 'subject')?.value || 'No Subject' | ||
| const from = headers.find((h: any) => h.name.toLowerCase() === 'from')?.value || 'Unknown Sender' | ||
| const date = headers.find((h: any) => h.name.toLowerCase() === 'date')?.value || '' | ||
| return { | ||
| id: message.id, | ||
| threadId: message.threadId, | ||
| subject, | ||
| from, | ||
| date, | ||
| snippet: message.snippet || '', | ||
| } | ||
| } | ||
| // Helper function to create a summary of multiple messages | ||
| function createMessagesSummary(messages: any[]): string { | ||
| if (messages.length === 0) { | ||
| return 'No messages found.' | ||
| } | ||
| let summary = `Found ${messages.length} messages:\n\n` | ||
| messages.forEach((msg, index) => { | ||
| summary += `${index + 1}. Subject: ${msg.subject}\n` | ||
| summary += ` From: ${msg.from}\n` | ||
| summary += ` Date: ${msg.date}\n` | ||
| summary += ` Preview: ${msg.snippet}\n\n` | ||
| }) | ||
| summary += `To read full content of a specific message, use the gmail_read tool with messageId: ${messages.map((m) => m.id).join(', ')}` | ||
| return summary | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.