Skip to content

Commit

Permalink
WIP adding in snippets to sources used display
Browse files Browse the repository at this point in the history
  • Loading branch information
scazan committed Nov 13, 2023
1 parent 0634853 commit fbbf3ff
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions integrations/slack/src/actions/queryLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Revision,
RevisionPage,
RevisionPageGroup,
SearchAIAnswerSource,
} from '@gitbook/api';

import {
Expand Down Expand Up @@ -38,7 +39,7 @@ function extractAllPages(rootPages: Array<RevisionPage>) {
* Pulls out the top related pages from page IDs returned from Lens and resolves them using a provided GitBook API client.
*/
async function getRelatedPages(params: {
pages?: SearchAIAnswer['pages'];
pages?: SearchAIAnswer['sources'];
client: GitBookAPI;
environment: SlackRuntimeEnvironment;
}) {
Expand All @@ -53,7 +54,9 @@ async function getRelatedPages(params: {

// collect all spaces from page results (and de-dupe)
const allSpaces = sourcePages.reduce((accum, page) => {
accum.add(page.space);
if (page.type === 'page') {
accum.add(page.space);
}

return accum;
}, new Set<string>());
Expand All @@ -70,26 +73,50 @@ async function getRelatedPages(params: {
return accum;
}, []);

const getResolvedPage = (page: SearchAIAnswerSource & { type: 'page' }) => {
// TODO: we can probably combine finding the currentRevision with extracting the appropriate page
const currentRevision = allRevisions.find((revision: Revision) =>
extractAllPages(revision.pages).find((revisionPage) => revisionPage.id === page.page)
);

if (currentRevision) {
const sourceUrl = currentRevision.urls.public || currentRevision.urls.app;

const allRevisionPages = extractAllPages(currentRevision.pages);
const revisionPage = allRevisionPages.find((revPage) => revPage.id === page.page);

return {
sourceUrl,
page: revisionPage,
};
}
};

const getResolvedSnippet = (
snippet: SearchAIAnswerSource & { type: 'snippet' | 'capture' }
) => {
const sourceUrl = snippet.urls.public || snippet.urls.app;

return {
sourceUrl,
page: revisionPage,
};
};

// extract all related pages from the Revisions along with the related public URL
const relatedPages: Array<{ sourceUrl: string; page: RevisionPage }> = sourcePages.reduce(
const relatedSources: Array<{ sourceUrl: string; page: RevisionPage }> = sourcePages.reduce(
(accum, page) => {
// TODO: we can probably combine finding the currentRevision with extracting the appropriate page
const currentRevision = allRevisions.find((revision: Revision) =>
extractAllPages(revision.pages).find(
(revisionPage) => revisionPage.id === page.page
)
);

if (currentRevision) {
const sourceUrl = currentRevision.urls.public || currentRevision.urls.app;

const allRevisionPages = extractAllPages(currentRevision.pages);
const revisionPage = allRevisionPages.find((revPage) => revPage.id === page.page);

accum.push({
sourceUrl,
page: revisionPage,
});
switch (page.type) {
case 'page':
const resolvedPage = getResolvedPage(page);
accum.push(resolvedPage);
break;

case 'snippet':
case 'capture':
const resolvedSnippet = getResolvedSnippet(page);
accum.push(resolvedSnippet);
break;
}

return accum;
Expand All @@ -98,7 +125,7 @@ async function getRelatedPages(params: {
);

// filter related pages from current revision
return relatedPages;
return relatedSources;
}

const capitalizeFirstLetter = (text: string) =>
Expand Down Expand Up @@ -173,6 +200,7 @@ export async function queryLens({
const messageTypePath = messageType === 'ephemeral' ? 'chat.postEphemeral' : 'chat.postMessage';

if (answer && answer.text) {
console.log('==========', JSON.stringify(answer));
const relatedPages = await getRelatedPages({
pages: answer.pages,
client,
Expand Down

0 comments on commit fbbf3ff

Please sign in to comment.