Skip to content

Commit

Permalink
feat: add AI summarization button for package files
Browse files Browse the repository at this point in the history
  • Loading branch information
nl0 committed Oct 10, 2024
1 parent 98f3ebf commit cd52f78
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 20 deletions.
21 changes: 11 additions & 10 deletions catalog/app/containers/Bucket/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ export default function File() {

return (
<FileView.Root>
<AssistantContext.CurrentVersionContext {...{ version, objExistsData, versionExistsData }} />
<AssistantContext.CurrentVersionContext
{...{ version, objExistsData, versionExistsData }}
/>

<MetaTitle>{[path || 'Files', bucket]}</MetaTitle>

Expand Down Expand Up @@ -509,15 +511,14 @@ export default function File() {
{downloadable && (
<FileView.DownloadButton className={classes.button} handle={handle} />
)}
{cfg.qurator &&
BucketPreferences.Result.match(
{
// XXX: only show this when the object exists?
Ok: ({ ui: { blocks } }) => (blocks.qurator ? <SummarizeButton /> : null),
_: () => null,
},
prefs,
)}
{BucketPreferences.Result.match(
{
// XXX: only show this when the object exists?
Ok: ({ ui: { blocks } }) => (blocks.qurator ? <SummarizeButton /> : null),
_: () => null,

Check warning on line 518 in catalog/app/containers/Bucket/File.js

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/File.js#L518

Added line #L518 was not covered by tests
},
prefs,
)}
</div>
</div>
{objExistsData.case({
Expand Down
76 changes: 66 additions & 10 deletions catalog/app/containers/Bucket/PackageTree/PackageTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as urql from 'urql'
import * as M from '@material-ui/core'
import * as Lab from '@material-ui/lab'

import * as Assistant from 'components/Assistant'

Check warning on line 9 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L9

Added line #L9 was not covered by tests
import * as BreadCrumbs from 'components/BreadCrumbs'
import * as Buttons from 'components/Buttons'
import * as FileEditor from 'components/FileEditor'
Expand All @@ -24,6 +25,7 @@ import * as GQL from 'utils/GraphQL'
import * as LogicalKeyResolver from 'utils/LogicalKeyResolver'
import MetaTitle from 'utils/MetaTitle'
import * as NamedRoutes from 'utils/NamedRoutes'
import * as XML from 'utils/XML'

Check warning on line 28 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L28

Added line #L28 was not covered by tests
import assertNever from 'utils/assertNever'
import parseSearch from 'utils/parseSearch'
import * as s3paths from 'utils/s3paths'
Expand Down Expand Up @@ -544,6 +546,55 @@ const useFileDisplayStyles = M.makeStyles((t) => ({
},
}))

interface FileContextProps {
pkg: {
bucket: string
name: string
hash: string
}
file: Model.GQLTypes.PackageFile
}

const FileContext = Assistant.Context.LazyContext(({ pkg, file }: FileContextProps) => {
const msg = React.useMemo(() => {
const s3loc = s3paths.parseS3Url(file.physicalKey)
return XML.tag('viewport')

Check warning on line 561 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L558-L561

Added lines #L558 - L561 were not covered by tests
.children(
'You are currently viewing a file in a package.',
XML.tag(
'package',
pkg,
XML.tag(
'package-entry',
{ path: file.path, size: file.size },
'You can use the physicalLocation to access the file in S3 (always provide version if available)',
XML.tag('physicalLocation', {}, JSON.stringify(s3loc, null, 2)),
file.metadata &&
XML.tag('metadata', {}, JSON.stringify(file.metadata, null, 2)),

Check warning on line 573 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L573

Added line #L573 was not covered by tests
),
),
)
.toString()
}, [file, pkg])

return {

Check warning on line 580 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L580

Added line #L580 was not covered by tests
messages: [msg],
}
})

function SummarizeButton() {
const assist = Assistant.use()

Check warning on line 586 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L585-L586

Added lines #L585 - L586 were not covered by tests
if (!assist) return null
const msg = 'Summarize this document'
return (
<M.IconButton color="primary" onClick={() => assist(msg)} edge="end">

Check warning on line 590 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L588-L590

Added lines #L588 - L590 were not covered by tests
<M.Tooltip title="Summarize and chat with AI">
<M.Icon>assistant</M.Icon>
</M.Tooltip>
</M.IconButton>
)
}

interface FileDisplayProps extends FileDisplayQueryProps {
file: Model.GQLTypes.PackageFile
}
Expand Down Expand Up @@ -626,6 +677,7 @@ function FileDisplay({
Ok: requests.ObjectExistence.case({
Exists: ({ archived, deleted, lastModified, size }: ObjectAttrs) => (
<>
<FileContext file={file} pkg={packageHandle} />
<TopBar crumbs={crumbs}>
<FileProperties
className={classes.fileProperties}
Expand Down Expand Up @@ -664,16 +716,20 @@ function FileDisplay({
)}
{BucketPreferences.Result.match(
{
Ok: ({ ui: { actions } }) =>
!cfg.noDownload &&
!deleted &&
!archived &&
actions.downloadPackage && (
<FileView.DownloadButton
className={classes.button}
handle={handle}
/>
),
Ok: ({ ui: { actions, blocks } }) => (

Check warning on line 719 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L719

Added line #L719 was not covered by tests
<>
{!cfg.noDownload &&
!deleted &&
!archived &&
actions.downloadPackage && (
<FileView.DownloadButton

Check warning on line 725 in catalog/app/containers/Bucket/PackageTree/PackageTree.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageTree/PackageTree.tsx#L722-L725

Added lines #L722 - L725 were not covered by tests
className={classes.button}
handle={handle}
/>
)}
{blocks.qurator && !deleted && !archived && <SummarizeButton />}
</>
),
Pending: () => (
<Buttons.Skeleton className={classes.button} size="small" />
),
Expand Down

0 comments on commit cd52f78

Please sign in to comment.