Skip to content

Commit

Permalink
feat: improve hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Feb 18, 2025
1 parent 0c4136a commit b99d540
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/runtime/server/routes/llms.txt.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export default eventHandler(async (event) => {

const llms = JSON.parse(JSON.stringify(options))

const sections = llms.sections || []

await llmsHooks.callHook('llms', event, llms, sections)
await llmsHooks.callHook('generate', event, llms)

const document = [
`# ${llms.title || 'Documentation'}`,
Expand All @@ -20,7 +18,7 @@ export default eventHandler(async (event) => {
document.push(`> ${options.description}`)
}

for (const section of sections) {
for (const section of llms.sections) {
document.push(`## ${section.title}`)
if (section.description) {
document.push(section.description)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/routes/llms_full.txt.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default eventHandler(async (event) => {
const contents = [] as string[]
const llms = JSON.parse(JSON.stringify(options))

await llmsHooks.callHook('llms:full', event, llms, contents)
await llmsHooks.callHook('generate:full', event, llms, contents)

const document = [
`# ${llms.llmsFull?.title || 'Documentation'}`,
Expand Down
18 changes: 16 additions & 2 deletions src/runtime/server/utils/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ import type { H3Event } from 'h3'
import type { ModuleOptions } from '@nuxtjs/llms'

export interface LLMSHooks {
'llms': (event: H3Event, options: ModuleOptions, sections: ModuleOptions['sections']) => void
'llms:full': (event: H3Event, options: ModuleOptions, contents: string[]) => void
'generate': (event: H3Event, options: ModuleOptions) => void
'generate:full': (event: H3Event, options: ModuleOptions, contents: string[]) => void
}

export const llmsHooks = createHooks<LLMSHooks>()

/**
* Run a callback when LLMs is being generated.
*/
export function onLLMsGenerate(cb: LLMSHooks['generate']) {
return llmsHooks.hook('generate', cb)
}

/**
* Run a callback when Full LLMs is being generated.
*/
export function onLLMsGenerateFull(cb: LLMSHooks['generate:full']) {
return llmsHooks.hook('generate:full', cb)
}

0 comments on commit b99d540

Please sign in to comment.