Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ jobs:
run: npx nypm@latest i

- name: Lint
run: npm run lint
run: pnpm lint

- name: Playground prepare
run: npm run dev:prepare
run: pnpm dev:prepare

- name: Test
run: npm run test
run: pnpm test

- name: Typecheck
run: pnpm typecheck

- name: Build
run: pnpm prepack

- name: Publish
# add `--compact` option after releasing on npm
Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The module provides a hooks system that allows you to dynamically extend both do

### Available Hooks

#### `generate:llms(event, options)`
#### `llms:generate(event, options)`

This hook is called for every request to `/llms.txt`. Use this hook to modify the structured documentation, It allows you to add sections, links, and metadata.

Expand All @@ -135,7 +135,7 @@ This hook is called for every request to `/llms.txt`. Use this hook to modify th
- `options`: ModuleOptions - The module options that you can modify to add sections, links, etc.


#### `generate:llms_full(event, options, contents)`
#### `llms:generate:llms_full(event, options, contents)`

This hook is called for every request to `/llms_full.txt`. It allows you to add custom content sections in any format.

Expand All @@ -150,11 +150,9 @@ Create a server plugin in your `server/plugins` directory:

```ts
// server/plugins/llms.ts
import { onLLMsGenerate, onLLMsGenerateFull, llmsHooks } from 'nuxt-llms/runtime'

export default defineNitroPlugin(() => {
export default defineNitroPlugin((nitroApp) => {
// Method 1: Using the hooks directly
llmsHooks.hook('generate', (event, options) => {
nitroApp.hooks.hook('llms:generate', (event, options) => {
// Add a new section to llms.txt
options.sections.push({
title: 'API Documentation',
Expand All @@ -170,7 +168,7 @@ export default defineNitroPlugin(() => {
})

// Method 2: Using the helper function
onLLMsGenerateFull((event, options, contents) => {
nitroApp.hooks.hook('llms:generate:full', (event, options, contents) => {
// Add detailed documentation to llms_full.txt
contents.push(`## API Authentication

Expand Down Expand Up @@ -199,8 +197,8 @@ If you're developing a Nuxt module that needs to extend the LLMs documentation:
1. Create a server plugin in your module:
```ts
// module/runtime/server/plugins/my-module-llms.ts
export default defineNitroPlugin(() => {
onLLMsGenerate((event, options) => {
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('llms:generate', (event, options) => {
options.sections.push({
title: 'My Module',
description: 'Documentation for my module features',
Expand Down
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineNuxtModule, createResolver, addServerHandler, addPrerenderRoutes, addServerImports, useLogger } from '@nuxt/kit'
import type { ModuleOptions } from './types'
import type { ModuleOptions } from './runtime/types'

export type * from './types'
export type * from './runtime/types'

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/server/routes/llms.txt.get.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { eventHandler, setHeader } from 'h3'
import type { ModuleOptions } from 'nuxt-llms'
import { llmsHooks } from 'nuxt-llms/runtime'
import { useRuntimeConfig } from '#imports'
// @ts-expect-error - useNitroApp is not typed
import { useRuntimeConfig, useNitroApp } from '#imports'

export default eventHandler(async (event) => {
const options = useRuntimeConfig(event).llms as ModuleOptions

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

await useNitroApp().hooks.callHook('llms:generate', event, llms)
await llmsHooks.callHook('generate', event, llms)

const document = [
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/server/routes/llms_full.txt.get.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { eventHandler, setHeader } from 'h3'
import type { ModuleOptions } from 'nuxt-llms'
import { llmsHooks } from 'nuxt-llms/runtime'
import { useRuntimeConfig } from '#imports'
// @ts-expect-error - useNitroApp is not typed
import { useRuntimeConfig, useNitroApp } from '#imports'

export default eventHandler(async (event) => {
const options = useRuntimeConfig(event).llms as ModuleOptions

const contents = [] as string[]
const llms: ModuleOptions = JSON.parse(JSON.stringify(options))

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

setHeader(event, 'Content-Type', 'text/plain')
Expand Down
3 changes: 0 additions & 3 deletions src/runtime/server/tsconfig.json

This file was deleted.

25 changes: 21 additions & 4 deletions src/runtime/server/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import { createHooks } from 'hookable'
import type { H3Event } from 'h3'
import type { ModuleOptions } from 'nuxt-llms'
import type { NitroRuntimeHooks } from 'nitropack/types'
// @ts-expect-error - useNitroApp is not typed
import { useNitroApp } from '#imports'

/**
* @deprecated Custom hooks are deprecated in favor of NitroRuntimeHooks.
*/
export interface LLMSHooks {
'generate': (event: H3Event, options: ModuleOptions) => void
'generate:full': (event: H3Event, options: ModuleOptions, contents: string[]) => void
}

/**
* @deprecated Custom hooks are deprecated in favor of NitroRuntimeHooks.
*/
export const llmsHooks = createHooks<LLMSHooks>()

llmsHooks.beforeEach(() => {
console.warn('[nuxt-llms] `llmsHooks` are deprecated and will be removed in future versions. Use `useNitroApp().hooks.hook(\'llms:generate\', (event, options) => {})` instead')
})

/**
* Run a callback when LLMs is being generated.
*
* @deprecated Use `useNitroApp().hooks.hook('llms:generate', (event, options) => {})` instead
*/
export function onLLMsGenerate(cb: LLMSHooks['generate']) {
return llmsHooks.hook('generate', cb)
export function onLLMsGenerate(cb: NitroRuntimeHooks['llms:generate']) {
return useNitroApp().hooks.hook('llms:generate', cb)
}

/**
* Run a callback when Full LLMs is being generated.
*
* @deprecated Use `useNitroApp().hooks.hook('llms:generate:full', (event, options, contents) => {})` instead
*/
export function onLLMsGenerateFull(cb: LLMSHooks['generate:full']) {
return llmsHooks.hook('generate:full', cb)
export function onLLMsGenerateFull(cb: NitroRuntimeHooks['llms:generate:full']) {
return useNitroApp().hooks.hook('llms:generate:full', cb)
}
10 changes: 10 additions & 0 deletions src/types.ts → src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import type { H3Event } from 'h3'

declare module 'nitropack/types' {

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

export interface LLMsSection {
title: string
description?: string
Expand Down