Skip to content

Commit

Permalink
tool for getting search results
Browse files Browse the repository at this point in the history
  • Loading branch information
nl0 committed Jul 8, 2024
1 parent e6712dd commit 6da6414
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 29 deletions.
2 changes: 1 addition & 1 deletion catalog/app/components/Assistant/Model/GlobalTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function useStartSearch() {
status: 'success',
content: [
Content.ToolResultContentBlock.Text({
text: `navigating to the search page and starting the search session`,
text: 'Navigating to the search page and starting the search session. Use catalog_search_getResults tool to get the search results.',
}),
],
}),
Expand Down
2 changes: 1 addition & 1 deletion catalog/app/components/Assistant/UI/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default function Chat({ state, dispatch }: ChatProps) {
<div className={classes.historyContainer}>
<div className={classes.history}>
<MessageContainer role="assistant">
Hi! I'm Qurator. How can I help you?
Hi! I'm Qurator, your AI assistant. How can I help you?
</MessageContainer>
{state.events.map(
Model.Conversation.Event.$match({
Expand Down
154 changes: 127 additions & 27 deletions catalog/app/containers/Search/AssistantContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as Eff from 'effect'
import * as React from 'react'
import { Schema as S } from '@effect/schema'

Check warning on line 3 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L1-L3

Added lines #L1 - L3 were not covered by tests

import * as Assistant from 'components/Assistant'
import * as Model from 'model'
import assertNever from 'utils/assertNever'
import useConstant from 'utils/useConstant'

Check warning on line 7 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L5-L7

Added lines #L5 - L7 were not covered by tests

import * as SearchUIModel from './model'

Check warning on line 9 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L9

Added line #L9 was not covered by tests

Expand Down Expand Up @@ -33,32 +34,8 @@ const intro = (model: SearchUIModel.SearchUIModel) => {
return lines.join('\n')

Check warning on line 34 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L32-L34

Added lines #L32 - L34 were not covered by tests
}

const results = (model: SearchUIModel.SearchUIModel) => {
const r = model.firstPageQuery
switch (r._tag) {
case 'fetching':
return 'Search results are still loading...'
case 'error':
return `Search request failed with error:\n${JSON.stringify(r.error)}`
case 'data':
switch (r.data.__typename) {
case 'InvalidInput':
return `Search request failed with error:\n${JSON.stringify(r.data)}`
case 'EmptySearchResultSet':
return 'Search request returned no results'
case 'ObjectsSearchResultSet':
case 'PackagesSearchResultSet':
return `Search request returned ${r.data.stats.total} results`
default:
assertNever(r.data)
}
default:
assertNever(r)
}
}

function useMessages(model: SearchUIModel.SearchUIModel) {
return [intro(model), results(model)]
return [intro(model)]

Check warning on line 38 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L37-L38

Added lines #L37 - L38 were not covered by tests
}

const RefineSearchSchema = S.Struct({

Check warning on line 41 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L41

Added line #L41 was not covered by tests
Expand All @@ -79,6 +56,119 @@ const RefineSearchSchema = S.Struct({
'Refine current search by adjusting search parameters. Dont provide a parameter to keep it as is',
})

const GetResultsSchema = S.Struct({

Check warning on line 59 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L59

Added line #L59 was not covered by tests
dummy: S.optional(S.String).annotations({
description: 'not used',
}),
}).annotations({
description: 'Get current search results',
})

function useGetResults(model: SearchUIModel.SearchUIModel) {
const result: Eff.Option.Option<Assistant.Model.Tool.Result> = React.useMemo(
() =>
Eff.Match.value(model.firstPageQuery).pipe(
Eff.Match.tag('fetching', () => Eff.Option.none()),
Eff.Match.tag('error', (r) =>
Eff.Option.some(

Check warning on line 73 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L67-L73

Added lines #L67 - L73 were not covered by tests
Assistant.Model.Tool.Result({
status: 'error',
content: [
Assistant.Model.Content.text(
`Search request failed with error:\n${JSON.stringify(r.error)}`,
),
],
}),
),
),
Eff.Match.tag('data', (r) =>
Eff.Match.value(r.data).pipe(
Eff.Match.when({ __typename: 'InvalidInput' }, (data) =>
Eff.Option.some(

Check warning on line 87 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L84-L87

Added lines #L84 - L87 were not covered by tests
Assistant.Model.Tool.Result({
status: 'error',
content: [
Assistant.Model.Content.text(
`Search request failed with error:\n${JSON.stringify(data)}`,
),
],
}),
),
),
Eff.Match.when({ __typename: 'EmptySearchResultSet' }, () =>
Eff.Option.some(

Check warning on line 99 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L98-L99

Added lines #L98 - L99 were not covered by tests
Assistant.Model.Tool.Result({
status: 'success',
content: [
Assistant.Model.Content.text('Search request returned no results'),
],
}),
),
),
Eff.Match.orElse((data) => {

Check warning on line 108 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L108

Added line #L108 was not covered by tests
// 'ObjectsSearchResultSet'
// 'PackagesSearchResultSet'
const label =
data.__typename === 'ObjectsSearchResultSet' ? 'objects' : 'packages'
return Eff.Option.some(

Check warning on line 113 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L113

Added line #L113 was not covered by tests
Assistant.Model.Tool.Result({
status: 'success',
content: [
Assistant.Model.Content.text(
`Found ${data.stats.total} ${label}. First page follows:`,
),
...data.firstPage.hits.map((hit, i) =>
Assistant.Model.Content.text(

Check warning on line 121 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L120-L121

Added lines #L120 - L121 were not covered by tests
`<search-result index=${i}>\n${JSON.stringify(
hit,
null,
2,
)}\n</search-result>`,
),
),
],
}),
)
}),
),
),
Eff.Match.exhaustive,
),
[model.firstPageQuery],
)

const ref = useConstant(() => Eff.Effect.runSync(Eff.SubscriptionRef.make(result)))

Check warning on line 140 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L140

Added line #L140 was not covered by tests

React.useEffect(() => {
Eff.Effect.runFork(Eff.SubscriptionRef.set(ref, result))

Check warning on line 143 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L142-L143

Added lines #L142 - L143 were not covered by tests
}, [result, ref])

return Assistant.Model.Tool.useMakeTool(

Check warning on line 146 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L146

Added line #L146 was not covered by tests
GetResultsSchema,
() =>
Eff.Effect.gen(function* () {

Check warning on line 149 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L148-L149

Added lines #L148 - L149 were not covered by tests
yield* Eff.Console.debug('tool: get search results')
// wait til results are Some
const lastOpt = yield* ref.changes.pipe(
Eff.Stream.takeUntil((x) => Eff.Option.isSome(x)),

Check warning on line 153 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L152-L153

Added lines #L152 - L153 were not covered by tests
Eff.Stream.runLast,
)
const last = Eff.Option.flatten(lastOpt)

Check warning on line 156 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L156

Added line #L156 was not covered by tests
// should be some
const res = Eff.Option.match(last, {
onSome: (value) => value,
onNone: () =>
Assistant.Model.Tool.Result({

Check warning on line 161 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L158-L161

Added lines #L158 - L161 were not covered by tests
status: 'error',
content: [Assistant.Model.Content.text('Got empty results')],
}),
})
return Eff.Option.some(res)

Check warning on line 166 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L166

Added line #L166 was not covered by tests
}),
[ref],
)
}

const withPrefix = <T extends Record<string, any>>(prefix: string, obj: T) =>
Object.entries(obj).reduce((acc, [k, v]) => ({ ...acc, [prefix + k]: v }), {})

Check warning on line 173 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L172-L173

Added lines #L172 - L173 were not covered by tests

Expand Down Expand Up @@ -114,7 +204,16 @@ function useTools(model: SearchUIModel.SearchUIModel) {
yield* Eff.Effect.sync(() =>
updateUrlState((s) => ({ ...s, ...(params as any) })),

Check warning on line 205 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L205

Added line #L205 was not covered by tests
)
return Eff.Option.none()
return Eff.Option.some(

Check warning on line 207 in catalog/app/containers/Search/AssistantContext.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Search/AssistantContext.tsx#L207

Added line #L207 was not covered by tests
Assistant.Model.Tool.Result({
status: 'success',
content: [
Assistant.Model.Content.text(
'Search parameters updated. Use catalog_search_getResults tool to get the search results.',
),
],
}),
)
}),
[updateUrlState],
),
Expand All @@ -133,6 +232,7 @@ function useTools(model: SearchUIModel.SearchUIModel) {
//
// clearFilters,
// reset,
getResults: useGetResults(model),
})
}

Expand Down

0 comments on commit 6da6414

Please sign in to comment.