Skip to content

Commit

Permalink
wip: supbase search
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Apr 22, 2024
1 parent 6b52ef7 commit 3ad4c70
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
4 changes: 2 additions & 2 deletions apps/masterbots.ai/app/(browse)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function HomePage({ searchParams }: HomePageProps) {
const query = searchParams.query ? decodeQuery(searchParams.query) : null
const limit = searchParams.limit ? parseInt(searchParams.limit) : 20
const page = searchParams.page ? parseInt(searchParams.page) : 1
console.log('hey hey hey', { query, limit, page })
// console.log({ query, limit, page })
const formData = new FormData()

// Append the limit parameter to the FormData
Expand All @@ -34,7 +34,7 @@ export default async function HomePage({ searchParams }: HomePageProps) {
<div>Your query: {query}</div>
<ul>
{threads.map(t => (
<li key={t.threadId}>{t.messages[0]?.content || 'not found'}</li>
<li key={t.threadId}>{t.message[0]?.content || 'not found'}</li>
))}
</ul>

Expand Down
46 changes: 34 additions & 12 deletions apps/masterbots.ai/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use server'

import { createSupabaseServerClient } from '@/services/supabase'
import { getErrorMessage } from '@repo/mb-lib'
import { Dub } from 'dub'
import { objectToCamel } from 'ts-case-convert'

const dub = new Dub({
projectSlug: 'bitcash'
Expand Down Expand Up @@ -47,22 +49,42 @@ export async function getThreads(formData: FormData) {
const supabase = await createSupabaseServerClient()
const query = formData.get('query')

let supaQuery = supabase.from('thread').select(
`
const threadsQuery = supabase
.from('thread')
.select(
`
*,
message (
* order(created_at desc)
*
)
`
)

if (query) {
supaQuery = supaQuery
.filter('message.role', 'neq', 'system')
.textSearch('message.content', query.toString())
}
)
.range(0, 20)
const { data, error } = await threadsQuery
if (error) throw new Error(getErrorMessage(error))

const { data, error } = await supaQuery
// Filter to get the first assistant and user message
const filteredData = objectToCamel(data).map(thread => {
const firstAssistantMessage = thread.message.find(
msg => msg.role === 'assistant'
)
const firstUserMessage = thread.message.find(msg => msg.role === 'user')
return {
...thread,
// new props in response for easier reference
firstUserMessage,
firstAssistantMessage,
// for backward campat with current code
message: [firstUserMessage, firstAssistantMessage].filter(Boolean), // Removes undefined entries
messageCount: thread.message.length
}
})

return error ? null : data
return error ? null : filteredData
}

// if (query) {
// supaQuery = supaQuery
// .filter('message.role', 'neq', 'system')
// .textSearch('message.content', query.toString())
// }
1 change: 1 addition & 0 deletions apps/masterbots.ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"react-use": "^17.5.0",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
"ts-case-convert": "^2.0.7",
"use-debounce": "^10.0.0",
"zod": "^3.22.4"
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
"*.rs": [
"cargo fmt --"
]
},
"dependencies": {
"ts-case-convert": "^2.0.7"
}
}

0 comments on commit 3ad4c70

Please sign in to comment.