Skip to content

Commit 02cd15c

Browse files
committed
feat(collections): create studio collections for AI if detected
1 parent d9e8a6a commit 02cd15c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/utils/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { loadConfig, watchConfig, createDefineConfig } from 'c12'
22
import { relative } from 'pathe'
3+
import { hasNuxtModule } from '@nuxt/kit'
34
import type { Nuxt } from '@nuxt/schema'
45
import type { DefinedCollection, ModuleOptions } from '../types'
56
import { defineCollection, resolveCollections } from './collection'
67
import { logger } from './dev'
8+
import { resolveStudioCollection } from './studio'
79

810
type NuxtContentConfig = {
911
collections: Record<string, DefinedCollection>
@@ -63,6 +65,11 @@ export async function loadContentConfig(nuxt: Nuxt, options?: ModuleOptions) {
6365
return acc
6466
}, {} as Record<string, DefinedCollection>)
6567

68+
// If nuxt-studio is installed, automatically configure studio collection
69+
if (hasNuxtModule('nuxt-studio')) {
70+
resolveStudioCollection(nuxt, collectionsConfig)
71+
}
72+
6673
const hasNoCollections = Object.keys(collectionsConfig || {}).length === 0
6774
if (hasNoCollections) {
6875
logger.warn('No content configuration found, falling back to default collection. In order to have full control over your collections, create the config file in project root. See: https://content.nuxt.com/docs/getting-started/installation')

src/utils/studio.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { z } from 'zod'
2+
import type { Nuxt } from '@nuxt/schema'
3+
import type { DefinedCollection } from '../types'
4+
import { defineCollection } from './collection'
5+
6+
interface StudioAIConfig {
7+
apiKey?: string
8+
context?: {
9+
title?: string
10+
description?: string
11+
tone?: string
12+
style?: string
13+
collection?: {
14+
name?: string
15+
folder?: string
16+
}
17+
}
18+
}
19+
20+
const DEFAULT_STUDIO_COLLECTION_NAME = 'studio'
21+
const DEFAULT_STUDIO_COLLECTION_FOLDER = '.studio'
22+
23+
/**
24+
* Resolves studio collection configuration when nuxt-studio is installed.
25+
* Automatically creates a studio collection and adds exclude patterns to other collections.
26+
*/
27+
export function resolveStudioCollection(
28+
nuxt: Nuxt,
29+
collectionsConfig: Record<string, DefinedCollection>,
30+
): void {
31+
/* @ts-expect-error - studio is not typed */
32+
const studioAIConfig: StudioAIConfig = nuxt.options.studio?.ai || {}
33+
if (!studioAIConfig.apiKey) {
34+
return
35+
}
36+
37+
const studioCollectionName = studioAIConfig.context?.collection?.name || DEFAULT_STUDIO_COLLECTION_NAME
38+
const studioFolder = studioAIConfig.context?.collection?.folder || DEFAULT_STUDIO_COLLECTION_FOLDER
39+
const studioPattern = `${studioFolder}/**`
40+
41+
// Add studio collection if it doesn't exist
42+
if (!collectionsConfig[studioCollectionName]) {
43+
collectionsConfig[studioCollectionName] = defineCollection({
44+
type: 'data',
45+
source: studioPattern,
46+
schema: z.object({
47+
rawbody: z.string(),
48+
}),
49+
})
50+
}
51+
52+
// Add exclude pattern to all existing collections except studio
53+
for (const [name, collection] of Object.entries(collectionsConfig)) {
54+
if (name === studioCollectionName || !collection.source) {
55+
continue
56+
}
57+
58+
// Add exclude pattern to each source
59+
for (const source of collection.source) {
60+
if (!source.exclude) {
61+
source.exclude = []
62+
}
63+
if (!source.exclude.includes(studioPattern)) {
64+
source.exclude.push(studioPattern)
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)