Skip to content

Commit

Permalink
expose route/location info to the assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
nl0 committed Jul 31, 2024
1 parent faef06d commit 8476e6f
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 135 deletions.
2 changes: 2 additions & 0 deletions catalog/app/components/Assistant/Model/Assistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as Actor from 'utils/Actor'
import * as Bedrock from './Bedrock'
import * as Context from './Context'
import * as Conversation from './Conversation'
import * as GlobalContext from './GlobalContext'
import * as GlobalTools from './GlobalTools'

Check warning on line 13 in catalog/app/components/Assistant/Model/Assistant.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/Assistant.tsx#L9-L13

Added lines #L9 - L13 were not covered by tests

function usePassThru<T>(val: T) {
Expand Down Expand Up @@ -36,6 +37,7 @@ function useConstructAssistantAPI() {

Context.usePushContext({

Check warning on line 38 in catalog/app/components/Assistant/Model/Assistant.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/Assistant.tsx#L38

Added line #L38 was not covered by tests
tools: GlobalTools.use(),
messages: GlobalContext.use(),
})

// XXX: move this to actor state?
Expand Down
49 changes: 49 additions & 0 deletions catalog/app/components/Assistant/Model/GlobalContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from 'react'

Check warning on line 1 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L1

Added line #L1 was not covered by tests

import { useCurrentRoute } from '../navigation'

Check warning on line 3 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L3

Added line #L3 was not covered by tests

function useRouteContext() {
const { match, loc } = useCurrentRoute()

Check warning on line 6 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L5-L6

Added lines #L5 - L6 were not covered by tests

const description = React.useMemo(() => {

Check warning on line 8 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L8

Added line #L8 was not covered by tests
if (!match) return ''
const params = match.decoded?.params
? `

Check warning on line 11 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L11

Added line #L11 was not covered by tests
<parameters>
${JSON.stringify(match.decoded.params, null, 2)}
</parameters>
`
: ''
return `

Check warning on line 17 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L16-L17

Added lines #L16 - L17 were not covered by tests
<route-info>
Name: "${match.descriptor.name}"
<description>
${match.descriptor.description}
</description>
${params}
</route-info>
`
}, [match])

const msg = React.useMemo(
() =>
`

Check warning on line 30 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L28-L30

Added lines #L28 - L30 were not covered by tests
<viewport>
<current-location>
${JSON.stringify(loc, null, 2)}
</current-location>
${description}
Refer to "navigate" tool schema for navigable routes and their parameters.
</viewport>
`,
[description, loc],
)

return msg

Check warning on line 42 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L42

Added line #L42 was not covered by tests
}

export function useGlobalContext() {
return [useRouteContext()]

Check warning on line 46 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L45-L46

Added lines #L45 - L46 were not covered by tests
}

export { useGlobalContext as use }

Check warning on line 49 in catalog/app/components/Assistant/Model/GlobalContext/index.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/GlobalContext/index.ts#L49

Added line #L49 was not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,31 @@ exports[`components/Assistant/Model/GlobalTools/navigate NavigateSchema produced
],
"type": "object",
},
{
"additionalProperties": false,
"description": "Installation page",
"properties": {
"name": {
"const": "install",
},
"params": {
"$id": "/schemas/{}",
"oneOf": [
{
"type": "object",
},
{
"type": "array",
},
],
},
},
"required": [
"name",
"params",
],
"type": "object",
},
{
"additionalProperties": false,
"description": "Search page",
Expand Down Expand Up @@ -578,6 +603,56 @@ exports[`components/Assistant/Model/GlobalTools/navigate NavigateSchema produced
],
"type": "object",
},
{
"additionalProperties": false,
"description": "TBD",
"properties": {
"name": {
"const": "activate",
},
"params": {
"$id": "/schemas/{}",
"oneOf": [
{
"type": "object",
},
{
"type": "array",
},
],
},
},
"required": [
"name",
"params",
],
"type": "object",
},
{
"additionalProperties": false,
"description": "Bucket root page",
"properties": {
"name": {
"const": "bucket",
},
"params": {
"$id": "/schemas/{}",
"oneOf": [
{
"type": "object",
},
{
"type": "array",
},
],
},
},
"required": [
"name",
"params",
],
"type": "object",
},
],
},
},
Expand Down
139 changes: 124 additions & 15 deletions catalog/app/components/Assistant/Model/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Eff from 'effect'
import * as React from 'react'
import * as RR from 'react-router-dom'
import { Schema as S } from '@effect/schema'

Expand All @@ -9,21 +10,90 @@ import * as Nav from 'utils/Navigation'

const MODULE = 'Assistant/Model/navigation'

const home = Nav.makeRoute({
name: 'home',
path: ROUTES.home.path,
description: 'Home page',
// searchParams: S.Struct({
// // XXX: passing this param doesn't actually work bc of how it's implemented in
// // website/pages/Landing/Buckets/Buckets.js
// q: SearchParamLastOpt.annotations({
// title: 'bucket filter query',
// description: 'filter buckets in the bucket grid',
// }),
// }),
})

const routeList = [home, search] as const
// the routes are in the order of matching
const routeList = [
Nav.makeRoute({
name: 'home',
path: ROUTES.home.path,
description: 'Home page',
// searchParams: S.Struct({
// // XXX: passing this param doesn't actually work bc of how it's implemented in
// // website/pages/Landing/Buckets/Buckets.js
// q: SearchParamLastOpt.annotations({
// title: 'bucket filter query',
// description: 'filter buckets in the bucket grid',
// }),
// }),
}),
Nav.makeRoute({
name: 'install',
path: ROUTES.install.path,
description: 'Installation page',
}),
search,
Nav.makeRoute({
name: 'activate',
path: ROUTES.activate.path,
description: 'TBD',
}),
// <Route path={paths.signIn} exact>
// <AuthSignIn />
// </Route>
// <Route path="/login" exact>
// <RedirectTo path={urls.signIn()} />
// </Route>
//
// <Route path={paths.signOut} exact>
// <AuthSignOut />
// </Route>
//
// {(cfg.passwordAuth === true || cfg.ssoAuth === true) && (
// <Route path={paths.signUp} exact>
// <AuthSignUp />
// </Route>
// )}
// {!!cfg.passwordAuth && (
// <Route path={paths.passReset} exact>
// <AuthPassReset />
// </Route>
// )}
// {!!cfg.passwordAuth && (
// <Route path={paths.passChange} exact>
// <AuthPassChange />
// </Route>
// )}
//
// <Route path={paths.code} exact>
// <AuthCode />
// </Route>
//
// <Route path={paths.activationError} exact>
// <AuthActivationError />
// </Route>
//
// {cfg.mode === 'OPEN' && (
// // XXX: show profile in all modes?
// <Route path={paths.profile} exact>
// <OpenProfile />
// </Route>
// )}
//
// <Route path={paths.admin}>
// <Admin />
// </Route>
//
// <Route path={paths.uriResolver}>
// <UriResolver />
// </Route>
//
Nav.makeRoute({
name: 'bucket',
path: ROUTES.bucketRoot.path,
description: 'Bucket root page',
// XXX: this should hold the bucket name and subroute info (e.g. package vs file view)
}),
] as const

type KnownRoute = (typeof routeList)[number]
type KnownRouteMap = {
[K in KnownRoute['name']]: Extract<KnownRoute, { name: K }>
Expand Down Expand Up @@ -51,3 +121,42 @@ export const navigate = (route: NavigableRoute, history: History) =>
Eff.Effect.andThen((loc) => Eff.Effect.sync(() => history.push(loc))),

Check warning on line 121 in catalog/app/components/Assistant/Model/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/navigation.ts#L120-L121

Added lines #L120 - L121 were not covered by tests
),
)

interface Match {
descriptor: KnownRoute
decoded: NavigableRoute | null
}

const matchLocation = (loc: typeof Nav.Location.Type): Match | null =>
Eff.pipe(
Eff.Array.findFirst(routeList, (route) =>

Check warning on line 132 in catalog/app/components/Assistant/Model/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/navigation.ts#L131-L132

Added lines #L131 - L132 were not covered by tests
RR.matchPath(loc.pathname, { path: route.path, exact: true })
? Eff.Option.some(route)
: Eff.Option.none(),

Check warning on line 135 in catalog/app/components/Assistant/Model/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/navigation.ts#L134-L135

Added lines #L134 - L135 were not covered by tests
),
Eff.Option.map((descriptor) => ({

Check warning on line 137 in catalog/app/components/Assistant/Model/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/navigation.ts#L137

Added line #L137 was not covered by tests
descriptor,
decoded: Eff.pipe(
loc,
// @ts-expect-error
S.decodeOption(descriptor.paramsSchema),
Eff.Option.map((params) => ({ name: descriptor.name, params }) as NavigableRoute),

Check warning on line 143 in catalog/app/components/Assistant/Model/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/navigation.ts#L143

Added line #L143 was not covered by tests
Eff.Option.getOrNull,
),
})),
Eff.Option.getOrNull,
)

interface LocationInfo {
loc: typeof Nav.Location.Type
match: Match | null
}

export function useCurrentRoute(): LocationInfo {
const loc = RR.useLocation()
const match = React.useMemo(
() => matchLocation({ pathname: loc.pathname, search: loc.search, hash: '' }),

Check warning on line 158 in catalog/app/components/Assistant/Model/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/navigation.ts#L156-L158

Added lines #L156 - L158 were not covered by tests
[loc.pathname, loc.search],
)
return { match, loc }

Check warning on line 161 in catalog/app/components/Assistant/Model/navigation.ts

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/components/Assistant/Model/navigation.ts#L161

Added line #L161 was not covered by tests
}
Loading

0 comments on commit 8476e6f

Please sign in to comment.