-
Notifications
You must be signed in to change notification settings - Fork 83
Add support for ingesting conversation in Slack integration #1030
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
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2b9b03a
Add conversations:ingest scope to manifest
spastorelli 459e4bc
Move actions/tasks related types to specific file
spastorelli fb62225
Add utils to retrieve Slack thread and parse convo permalinks
spastorelli b22266b
Add shortcut & command handling for conversation ingestion
spastorelli 7cdd5e8
Fix message sending and improve message copy
spastorelli 7db80d8
Improve notification message when conv is ingested from permalink
spastorelli d67fcca
Fix subcommand text parsing
spastorelli a672aba
Merge branch 'main' into steeve/add-ingest-conv-slack-integration
spastorelli f63545b
Add changeset
spastorelli 9a2bd6b
review: call ingestion API directly from exec thread instead of using…
spastorelli d4fb25a
review: add comment to getActionNameAndType
spastorelli 4efec1e
review: Add comments to ingest actions params
spastorelli b916635
Merge branch 'main' into steeve/add-ingest-conv-slack-integration
spastorelli 28a5933
review: remove gitbook ingest command handler for now
spastorelli 76227dd
Merge branch 'main' into steeve/add-ingest-conv-slack-integration
spastorelli f9a4bbf
Use AI to detect intent of user when using Slack app mention (#1031)
spastorelli 3fbc3ae
Feature flag conversations ingestion in Slack integration (#1043)
spastorelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@gitbook/integration-slack': minor | ||
| --- | ||
|
|
||
| Add support for ingesting conversation to Docs Agents in Slack integration |
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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export * from './queryAskAI'; | ||
| export * from './ingestConversation'; | ||
| export * from './types'; |
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 |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| import { ConversationInput, ConversationPart, IntegrationInstallation } from '@gitbook/api'; | ||
| import { SlackInstallationConfiguration, SlackRuntimeContext } from '../configuration'; | ||
| import { | ||
| getSlackThread, | ||
| parseSlackConversationPermalink, | ||
| slackAPI, | ||
| SlackConversationThread, | ||
| } from '../slack'; | ||
| import { getInstallationApiClient, getIntegrationInstallationForTeam } from '../utils'; | ||
| import { IngestSlackConversationActionParams, IntegrationTaskIngestConversation } from './types'; | ||
| import { Logger } from '@gitbook/runtime'; | ||
|
|
||
| const logger = Logger('slack:actions:ingestConversation'); | ||
|
|
||
| /** | ||
| * Ingest the slack conversation to GitBook aiming at improving the organization docs. | ||
| */ | ||
| export async function ingestSlackConversation(params: IngestSlackConversationActionParams) { | ||
| const { responseUrl, channelId, channelName, userId, threadId, context, teamId } = params; | ||
|
|
||
| const installation = await getIntegrationInstallationForTeam(context, teamId); | ||
| if (!installation) { | ||
| throw new Error('Installation not found'); | ||
| } | ||
|
|
||
| const accessToken = (installation.configuration as SlackInstallationConfiguration) | ||
| .oauth_credentials?.access_token; | ||
|
|
||
| const permalink = params.text; | ||
| const isIngestedFromLink = !!permalink; | ||
spastorelli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const conversationToIngest: IngestSlackConversationActionParams['conversationToIngest'] = | ||
| (() => { | ||
| if (permalink) { | ||
| try { | ||
| return parseSlackConversationPermalink(permalink); | ||
| } catch (error) { | ||
| logger.debug( | ||
| `⚠️ We couldn’t understand that link. Please check it and try again.`, | ||
| error, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return params.conversationToIngest; | ||
| })(); | ||
|
|
||
| if (!conversationToIngest) { | ||
| await slackAPI( | ||
| context, | ||
| { | ||
| method: 'POST', | ||
| path: 'chat.postMessage', | ||
| payload: { | ||
| channel: channelId, | ||
| text: `⚠️ We couldn’t get the conversation details. Please try again.`, | ||
| }, | ||
| }, | ||
| { | ||
| accessToken, | ||
| }, | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| await Promise.all([ | ||
| slackAPI( | ||
| context, | ||
| { | ||
| method: 'POST', | ||
| path: 'chat.postMessage', | ||
| payload: { | ||
| channel: channelId, | ||
| ...(isIngestedFromLink | ||
| ? { | ||
| markdown_text: `🚀 Sharing [this conversation](${permalink}) with Docs Agent to improve your docs...`, | ||
| } | ||
| : { | ||
| text: `🚀 Sharing this conversation with Docs Agent to improve your docs...`, | ||
| }), | ||
| thread_ts: threadId, | ||
| unfurl_links: isIngestedFromLink, | ||
| }, | ||
| }, | ||
| { | ||
| accessToken, | ||
| }, | ||
| ), | ||
| queueIngestSlackConversationTask({ | ||
spastorelli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| channelId, | ||
| channelName, | ||
| teamId, | ||
| responseUrl, | ||
| userId, | ||
| threadId, | ||
| context, | ||
| accessToken, | ||
| installation, | ||
| conversationToIngest, | ||
| }), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Queues an integration task to ingest the Slack conversation asynchronously. | ||
| */ | ||
| async function queueIngestSlackConversationTask( | ||
| params: IngestSlackConversationActionParams & { | ||
| installation: IntegrationInstallation; | ||
| accessToken: string | undefined; | ||
| conversationToIngest: { | ||
| channelId: string; | ||
| messageTs: string; | ||
| }; | ||
| }, | ||
| ) { | ||
| const { accessToken, installation, context, conversationToIngest } = params; | ||
|
|
||
| const task: IntegrationTaskIngestConversation = { | ||
| type: 'ingest:conversation', | ||
| payload: { | ||
| channelId: params.channelId, | ||
| teamId: params.teamId, | ||
| channelName: params.channelName, | ||
| responseUrl: params.responseUrl, | ||
| threadId: params.threadId, | ||
| userId: params.userId, | ||
| organizationId: installation.target.organization, | ||
| installationId: installation.id, | ||
| accessToken, | ||
| conversationToIngest, | ||
| }, | ||
| }; | ||
|
|
||
| await context.api.integrations.queueIntegrationTask(context.environment.integration.name, { | ||
| task, | ||
| }); | ||
|
|
||
| logger.info(`Queue task ${task.type} for installation: ${task.payload.installationId})`); | ||
| } | ||
|
|
||
| /** | ||
| * Handle the integration task to ingest a slack conversation. | ||
| */ | ||
| export async function handleIngestSlackConversationTask( | ||
| task: IntegrationTaskIngestConversation, | ||
| context: SlackRuntimeContext, | ||
| ) { | ||
| const { | ||
| payload: { | ||
| channelId, | ||
| userId, | ||
| responseUrl, | ||
| threadId, | ||
| organizationId, | ||
| installationId, | ||
| accessToken, | ||
| conversationToIngest, | ||
| }, | ||
| } = task; | ||
|
|
||
| const [client, conversation] = await Promise.all([ | ||
| getInstallationApiClient(context, installationId), | ||
| (async () => { | ||
| const slackThread = await getSlackThread(context, conversationToIngest, { | ||
| accessToken, | ||
| }); | ||
| return parseSlackThreadAsGitBookConversation(slackThread); | ||
| })(), | ||
| ]); | ||
|
|
||
| try { | ||
| await client.orgs.ingestConversation(organizationId, conversation); | ||
| await slackAPI( | ||
| context, | ||
| { | ||
| method: 'POST', | ||
| path: 'chat.postMessage', | ||
| payload: { | ||
| channel: channelId, | ||
| text: `🤖 Got it! Docs Agent is on it. We'll analyze this and suggest changes if needed.`, | ||
spastorelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| thread_ts: threadId, | ||
| }, | ||
| }, | ||
| { | ||
| accessToken, | ||
| }, | ||
| ); | ||
| } catch { | ||
| await slackAPI( | ||
| context, | ||
| { | ||
| method: 'POST', | ||
| path: 'chat.postMessage', | ||
| payload: { | ||
| channel: channelId, | ||
| text: `⚠️ Something went wrong while sending this conversation to Docs Agent.`, | ||
| thread_ts: threadId, | ||
| }, | ||
| }, | ||
| { | ||
| accessToken, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parse a Slack threaded conversation into a GitBook conversation. | ||
| */ | ||
| function parseSlackThreadAsGitBookConversation( | ||
| slackThread: SlackConversationThread, | ||
| ): ConversationInput { | ||
| return { | ||
| id: `${slackThread.channelId}-${slackThread.messageTs}`, | ||
| metadata: { | ||
| url: slackThread.link, | ||
| attributes: { | ||
| channelId: slackThread.channelId, | ||
| messageTs: slackThread.messageTs, | ||
| }, | ||
| createdAt: new Date(slackThread.createdAt).toISOString(), | ||
| }, | ||
| parts: slackThread.messages | ||
| .map((message) => | ||
| message.text | ||
| ? ({ | ||
| type: 'message', | ||
| role: 'user', | ||
spastorelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| body: message.text, | ||
| } as ConversationPart) | ||
| : null, | ||
| ) | ||
| .filter((part) => part !== null), | ||
| }; | ||
| } | ||
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
Oops, something went wrong.
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.