-
Notifications
You must be signed in to change notification settings - Fork 1
feat(search): client side fts #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
larbish
wants to merge
16
commits into
main
Choose a base branch
from
feat/client-side-fts-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8069af1
feat(search): client side fts
larbish 2f703a0
feat: add agent skills discovery via `/.well-known/skills` (#19)
atinux 44c8e0e
fix(responsive): remove playground in hero on mobile
larbish 8ea6404
run on worker
larbish c9a22f7
Merge branch 'main' into feat/client-side-fts-search
larbish d26bc6f
use comark-cms latest
larbish 06e5eec
app search nav groups
larbish a783a63
debug system
larbish 74dfd6e
Merge branch 'main' into feat/client-side-fts-search
larbish d1b2cbc
pnpm lock file
larbish 060607b
up
larbish 4017871
up
larbish 6b95a04
use resolveContentSha
larbish 0c602ab
Merge branch 'main' into feat/client-side-fts-search
larbish fccb570
up tests
larbish ba0354a
fix lock
larbish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <script setup lang="ts"> | ||
| import type { NavigationItem } from 'comark-content' | ||
| const props = defineProps<{ | ||
| navigation: NavigationItem[] | ||
| }>() | ||
| const { search, status } = useSearch() | ||
| const appConfig = useAppConfig() | ||
| interface PageItem { | ||
| label: string | ||
| prefix?: string | ||
| suffix?: string | ||
| to: string | ||
| icon: string | ||
| } | ||
| /** Leaf pages, flattened; ancestor titles become the `Section > Page` prefix the palette renders. */ | ||
| function pageItems(items: NavigationItem[], ancestors: string[] = []): PageItem[] { | ||
| return items.flatMap((item) => { | ||
| if (item.children?.length) return pageItems(item.children, [...ancestors, item.title]) | ||
| if (!item.path || item.page === false) return [] | ||
| return [{ | ||
| label: item.title, | ||
| prefix: ancestors.length ? `${ancestors.join(' > ')} >` : undefined, | ||
| suffix: item.description, | ||
| to: item.path, | ||
| icon: (item.icon as string | undefined) || appConfig.ui.icons.file, | ||
| }] | ||
| }) | ||
| } | ||
| function browseOnly(query: string, items?: PageItem[]): PageItem[] { | ||
| return query ? [] : (items ?? []) | ||
| } | ||
| // One group per top-level section, mirroring how `UContentSearch` groups navigation when it can. | ||
| const groups = computed(() => { | ||
| if (props.navigation.some((item) => item.children?.length)) { | ||
| return props.navigation | ||
| .filter((section) => section.children?.length) | ||
| .map((section) => ({ | ||
| id: section.path, | ||
| label: section.title, | ||
| items: pageItems(section.children ?? []), | ||
| postFilter: browseOnly, | ||
| })) | ||
| .filter((group) => group.items.length > 0) | ||
| } | ||
| return [{ id: 'docs', items: pageItems(props.navigation), postFilter: browseOnly }] | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
| <ClientOnly> | ||
| <LazyUContentSearch | ||
| :search="search" | ||
| :search-status="status" | ||
| :navigation="navigation" | ||
| :groups="groups" | ||
| :transition="false" | ||
| :loading="status === 'loading'" | ||
| /> | ||
| </ClientOnly> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import type { SearchOptions, SearchResult } from 'comark-content' | ||
| import type { SearchWorkerPayload, SearchWorkerResponse } from '../types/search-worker' | ||
|
|
||
| type SearchStatus = 'idle' | 'loading' | 'ready' | 'error' | ||
|
|
||
| const status = ref<SearchStatus>('idle') | ||
|
|
||
| let worker: Worker | undefined | ||
| let nextId = 0 | ||
| const pending = new Map<number, { resolve: (results: SearchResult[]) => void, reject: (error: Error) => void }>() | ||
|
|
||
| /** | ||
| * Hydration logging switch: `?debug=search` | ||
| */ | ||
| function searchDebug(): boolean { | ||
| if (!import.meta.client) return false | ||
| return new URLSearchParams(location.search).get('debug') === 'search' | ||
| } | ||
|
|
||
| function getWorker(): Worker { | ||
| if (worker) return worker | ||
|
|
||
| worker = new Worker(new URL('../workers/search.worker.ts', import.meta.url), { type: 'module' }) | ||
|
|
||
| worker.onmessage = (event: MessageEvent<SearchWorkerResponse>) => { | ||
| const message = event.data | ||
| if (message.type === 'status') { | ||
| status.value = message.value | ||
| if (searchDebug()) console.info(`[search] status -> ${message.value}`) | ||
| return | ||
| } | ||
| const settle = pending.get(message.id) | ||
| if (!settle) return | ||
| pending.delete(message.id) | ||
| if (message.type === 'result') settle.resolve(message.results) | ||
| else { | ||
| if (searchDebug()) console.error(`[search] request ${message.id} failed:`, message.message) | ||
| settle.reject(new Error(message.message)) | ||
| } | ||
| } | ||
|
|
||
| worker.onerror = () => { | ||
| status.value = 'error' | ||
| for (const { reject } of pending.values()) reject(new Error('[search] the search worker failed to load')) | ||
| pending.clear() | ||
| } | ||
|
|
||
| return worker | ||
| } | ||
|
|
||
| function request(message: SearchWorkerPayload): Promise<SearchResult[]> { | ||
| const id = ++nextId | ||
| return new Promise<SearchResult[]>((resolve, reject) => { | ||
| pending.set(id, { resolve, reject }) | ||
| try { | ||
| getWorker().postMessage({ ...message, id }) | ||
| } catch (error) { | ||
| pending.delete(id) | ||
| reject(error instanceof Error ? error : new Error(String(error))) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Client-side full-text search over production content (sqlite-wasm FTS5) hydrated from the | ||
| * per-commit snapshot artifacts. | ||
| */ | ||
| export function useSearch() { | ||
| const { data: headSha } = useAsyncData( | ||
| 'content-head-sha', | ||
| () => $fetch<{ sha: string | null }>('/api/content/head').then(({ sha }) => sha), | ||
| { default: () => null } | ||
| ) | ||
|
|
||
| /** | ||
| * Load the database ahead of the first keystroke. No-op once loading or ready; retries after a | ||
| * failure — the worker holds that guard, since this side's `status` lags a message behind. | ||
| */ | ||
| async function warmup(): Promise<void> { | ||
| try { | ||
| if (!headSha.value && !import.meta.dev) { | ||
| throw new Error('[search] /api/content/head returned no commit pin') | ||
| } | ||
|
|
||
| // Immutable per-commit artifacts, CDN-cached forever. Only unpinned in dev, per the guard above. | ||
| const apiBase = headSha.value ? `/api/content/blob/${headSha.value}` : '/api/content' | ||
|
|
||
| const debug = searchDebug() | ||
| if (debug) console.info(`[search] warmup from ${apiBase} (head ${headSha.value ?? 'unpinned'})`) | ||
|
|
||
| await request({ type: 'warmup', apiBase, origin: location.origin, debug }) | ||
| } catch (error) { | ||
| status.value = 'error' | ||
| console.error('[search] could not load the search database', error) | ||
| } | ||
| } | ||
|
|
||
| if (import.meta.client) { | ||
| onNuxtReady(warmup) | ||
| } | ||
|
|
||
| async function search(query: string, opts?: SearchOptions): Promise<SearchResult[]> { | ||
| return request({ type: 'search', query, opts }) | ||
| } | ||
|
|
||
| return { search, status: readonly(status), warmup } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { SearchOptions, SearchResult } from 'comark-content' | ||
|
|
||
| /** | ||
| * Protocol between `useSearch` and `app/workers/search.worker.ts`. | ||
| * | ||
| * Every request carries an `id` and gets exactly one `result`/`error` reply — `warmup` answers | ||
| * with an empty array — so the caller can drain its pending map uniformly. | ||
| */ | ||
| export type SearchWorkerPayload = | ||
| | { | ||
| type: 'warmup' | ||
| apiBase: string | ||
| origin: string | ||
| /** Turns on the worker's hydration logging. Resolved on the main thread, which owns `?debug=search`. */ | ||
| debug?: boolean | ||
| } | ||
| | { | ||
| type: 'search', | ||
| query: string, | ||
| opts?: SearchOptions | ||
| } | ||
|
|
||
| /** | ||
| * Intersected rather than spread into each member: `Omit<Union, 'id'>` would collapse to the | ||
| * union's common keys, dropping every payload field. | ||
| */ | ||
| export type SearchWorkerRequest = SearchWorkerPayload & { id: number } | ||
|
|
||
| /** `status` arrives unsolicited: the worker owns the hydration lifecycle, the caller mirrors it. */ | ||
| export type SearchWorkerResponse = | ||
| | { type: 'status', value: 'loading' | 'ready' | 'error' } | ||
| | { type: 'result', id: number, results: SearchResult[] } | ||
| | { type: 'error', id: number, message: string } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.