-
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
Open
spastorelli
wants to merge
15
commits into
main
Choose a base branch
from
steeve/add-ingest-conv-slack-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 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 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,145 @@ | ||
| import { ConversationInput, ConversationPart, IntegrationInstallation } from '@gitbook/api'; | ||
| import { SlackInstallationConfiguration, SlackRuntimeContext } from '../configuration'; | ||
| import { getSlackThread, slackAPI, SlackConversationThread } from '../slack'; | ||
| import { getInstallationApiClient, getIntegrationInstallationForTeam } from '../utils'; | ||
| import { IngestSlackConversationActionParams } 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 { channelId, threadId, context, teamId, conversationToIngest } = 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; | ||
|
|
||
| await Promise.all([ | ||
| slackAPI( | ||
| context, | ||
| { | ||
| method: 'POST', | ||
| path: 'chat.postMessage', | ||
| payload: { | ||
| channel: channelId, | ||
| text: `🚀 Sharing this conversation with Docs Agent to improve your docs...`, | ||
| thread_ts: threadId, | ||
| }, | ||
| }, | ||
| { | ||
| accessToken, | ||
| }, | ||
| ), | ||
| handleIngestSlackConversationAction( | ||
| { | ||
| channelId, | ||
| threadId, | ||
| installation, | ||
| accessToken, | ||
| conversationToIngest, | ||
| }, | ||
| context, | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Handle the integration action to ingest a slack conversation. | ||
| */ | ||
| export async function handleIngestSlackConversationAction( | ||
| params: { | ||
| channelId: IngestSlackConversationActionParams['channelId']; | ||
| threadId: IngestSlackConversationActionParams['threadId']; | ||
| installation: IntegrationInstallation; | ||
| accessToken: string | undefined; | ||
| conversationToIngest: { | ||
| channelId: string; | ||
| messageTs: string; | ||
| }; | ||
| }, | ||
| context: SlackRuntimeContext, | ||
| ) { | ||
| const { channelId, threadId, installation, accessToken, conversationToIngest } = params; | ||
|
|
||
| const [client, conversation] = await Promise.all([ | ||
| getInstallationApiClient(context, installation.id), | ||
| (async () => { | ||
| const slackThread = await getSlackThread(context, conversationToIngest, { | ||
| accessToken, | ||
| }); | ||
| return parseSlackThreadAsGitBookConversation(slackThread); | ||
| })(), | ||
| ]); | ||
|
|
||
| try { | ||
| await client.orgs.ingestConversation(installation.target.organization, 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
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,57 @@ | ||
| import { SlackRuntimeContext } from '../configuration'; | ||
|
|
||
| interface ActionBaseParams { | ||
| channelId: string; | ||
| channelName?: string; | ||
| responseUrl?: string; | ||
| teamId: string; | ||
|
|
||
| context: SlackRuntimeContext; | ||
|
|
||
| /* needed for postEphemeral */ | ||
| userId?: string; | ||
|
|
||
| /* Get reply in thread */ | ||
| threadId?: string; | ||
| } | ||
|
|
||
| export interface IngestSlackConversationActionParams extends ActionBaseParams { | ||
| /** | ||
| * Used when the ingestion originates from a Slack conversation shortcut. | ||
| * The target conversation in this case is both the conversation to ingest | ||
| * and the one where notifications are sent. | ||
| * Identified by the `channelId` and `messageTs` values. | ||
| */ | ||
| conversationToIngest: { | ||
| channelId: string; | ||
| messageTs: string; | ||
| }; | ||
| } | ||
|
|
||
| export interface AskAIActionParams extends ActionBaseParams { | ||
| queryText: string; | ||
|
|
||
| /* postEphemeral vs postMessage */ | ||
| messageType: 'ephemeral' | 'permanent'; | ||
|
|
||
| authorization?: string; | ||
| } | ||
|
|
||
| export type IntegrationTaskType = 'ask:ai'; | ||
|
|
||
| export type BaseIntegrationTask<Type extends IntegrationTaskType, Payload extends object> = { | ||
| type: Type; | ||
| payload: Payload; | ||
| }; | ||
|
|
||
| export type IntegrationTaskAskAI = BaseIntegrationTask< | ||
| 'ask:ai', | ||
| { | ||
| query: string; | ||
| organizationId: string; | ||
| installationId: string; | ||
| accessToken: string | undefined; | ||
| } & Omit<AskAIActionParams, 'context'> | ||
| >; | ||
|
|
||
| export type IntegrationTask = IntegrationTaskAskAI; |
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.