Skip to content

Commit

Permalink
resolve all snippets, add read scope
Browse files Browse the repository at this point in the history
  • Loading branch information
scazan committed Nov 22, 2023
1 parent 9bbb7c4 commit bcc4712
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
1 change: 1 addition & 0 deletions integrations/slack/gitbook-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ scopes:
- space:content:read
- space:metadata:read
- capture:write
- snippets:read
summary: |
# Overview
With the GitBook Slack integration, your teams have instant access to your GitBook knowledge base, and can get AI-summarized answers about your content. Plus, if you solve a problem in a thread, you can ask GitBook AI to summarize it into useable documentation.
Expand Down
69 changes: 44 additions & 25 deletions integrations/slack/src/actions/queryLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
RevisionPage,
RevisionPageGroup,
SearchAIAnswerSource,
Capture,
} from '@gitbook/api';

import {
Expand Down Expand Up @@ -35,7 +34,11 @@ function extractAllPages(rootPages: Array<RevisionPage>) {

return result;
}

export type RelatedSource = {
id: string;
sourceUrl: string;
page: { path: string; title: string };
};
/*
* Pulls out the top related pages from page IDs returned from Lens and resolves them using a provided GitBook API client.
*/
Expand All @@ -44,7 +47,7 @@ async function getRelatedPages(params: {
client: GitBookAPI;
environment: SlackRuntimeEnvironment;
organization: string;
}) {
}): Promise<RelatedSource[]> {
const { pages, client, organization } = params;

if (!pages || pages.length === 0) {
Expand Down Expand Up @@ -87,56 +90,72 @@ async function getRelatedPages(params: {
const revisionPage = allRevisionPages.find((revPage) => revPage.id === page.page);

return {
id: page.page,
sourceUrl,
page: revisionPage,
};
} as RelatedSource;
}
};

const getResolvedSnippet = async (
source: SearchAIAnswerSource & { type: 'snippet' | 'capture' }
) => {
): Promise<RelatedSource> => {
try {
const snippetRequest = await client.orgs.getCapture(organization, source.captureId);
const snippet = snippetRequest.data;
// const sourceUrl = snippet.urls?.public || snippet.urls.app;
const sourceUrl = snippet.externalURL;
console.log('URLS', snippet.urls);

const publicUrl = `https://app.gitbook.com/o/${organization}`; // TODO replace with actual org public url
const sourceUrl = `${publicUrl}/snippets/${snippet.id}`;

return {
id: snippet.id,
sourceUrl,
page: { path: '', title: snippet.title },
};
} catch (e) {
console.log(e);
throw e;
}
};

const resolvedSnippets = await Promise.allSettled(
const resolvedSnippetsPromises = await Promise.allSettled(
sourcePages
.filter((source) => source.type === 'capture' || source.type === 'snippet')
.map(getResolvedSnippet)
);

const resolvedSnippets = resolvedSnippetsPromises.reduce((accum, result) => {
if (result.status === 'fulfilled') {
accum.push(result.value);
}

return accum;
}, [] as RelatedSource[]);

// { type: 'capture', captureId: '72', source: 'slack' }
// extract all related pages from the Revisions along with the related public URL
const relatedSources: Array<{ sourceUrl: string; page: SearchAIAnswerSource }> =
sourcePages.reduce((accum, source) => {
switch (source.type) {
case 'page':
const resolvedPage = getResolvedPage(source);
accum.push(resolvedPage);
break;

case 'snippet':
case 'capture':
const resolvedSnippet = resolvedSnippets.find(
(snippet) => snippet.sourceUrl === source.captureId
);
const relatedSources: Array<RelatedSource> = sourcePages.reduce((accum, source) => {
switch (source.type) {
case 'page':
const resolvedPage = getResolvedPage(source);
accum.push(resolvedPage);
break;

case 'snippet':
case 'capture':
const resolvedSnippet = resolvedSnippets.find(
(snippet) => snippet.id === source.captureId
);

if (resolvedSnippet) {
accum.push(resolvedSnippet);
break;
}
}
break;
}

return accum;
}, []);
return accum;
}, []);

// filter related pages from current revision
return relatedSources;
Expand Down
8 changes: 3 additions & 5 deletions integrations/slack/src/ui/blocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RevisionPage, SearchAIAnswer } from '@gitbook/api';
import { RelatedSource } from '../../actions/queryLens';

// Slack only encodes these specific characters so we need to remove them in the output (specifically used for inputs to slack)
export function decodeSlackEscapeChars(text: string) {
Expand All @@ -11,7 +12,7 @@ export function decodeSlackEscapeChars(text: string) {
}, text);
}

export function PageBlock(page: RevisionPage, sourceUrl: string) {
export function PageBlock(page: { path: string; title: string }, sourceUrl: string) {
// TODO: note for review. is this the best way to do this?
const nonRevisionPublicUrl = sourceUrl.split('~/')[0];
const url = `${nonRevisionPublicUrl}${page.path}`;
Expand All @@ -21,10 +22,7 @@ export function PageBlock(page: RevisionPage, sourceUrl: string) {
};
}

export function PagesBlock(params: {
title?: string;
items: Array<{ sourceUrl: string; page: SearchAIAnswer }>;
}) {
export function PagesBlock(params: { title?: string; items: Array<RelatedSource> }) {
const { title, items } = params;

const blocks = items.reduce<Array<any>>((acc, pageData) => {
Expand Down

0 comments on commit bcc4712

Please sign in to comment.