-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f41dc3
commit c7e6261
Showing
17 changed files
with
208 additions
and
175 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { knowledgeModule } from '@magickml/knowledge' | ||
import { nodeModule } from '@magickml/nodes' | ||
import { portalModule } from '@magickml/portal' | ||
import { schemasModule } from '@magickml/schemas' | ||
// import { knowledgeModule } from '@magickml/knowledge' | ||
// import { nodeModule } from '@magickml/nodes' | ||
// import { portalModule } from '@magickml/portal' | ||
// import { schemasModule } from '@magickml/schemas' | ||
import { spellsModule } from '@magickml/spells' | ||
import { toolsModule } from '@magickml/tools' | ||
// import { toolsModule } from '@magickml/tools' | ||
import type { NitroModule } from 'nitro/types' | ||
|
||
export const modules: NitroModule[] = [ | ||
knowledgeModule, | ||
nodeModule, | ||
portalModule, | ||
schemasModule, | ||
// knowledgeModule, | ||
// nodeModule, | ||
// portalModule, | ||
// schemasModule, | ||
spellsModule, | ||
toolsModule, | ||
// toolsModule, | ||
] |
This file contains 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 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 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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
export const spellFeatures = { | ||
spells: "spells", | ||
} as const; | ||
// spells: "spells", // | ||
} as const | ||
|
||
export type SpellFeatures = typeof spellFeatures; | ||
export type SpellFeatures = typeof spellFeatures | ||
|
||
export * from './scan-json' | ||
export * from './rollup-json' |
This file contains 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,55 @@ | ||
import { virtual } from '@gtc-nova/kit/vendor' | ||
import { fileURLToPath } from 'node:url' | ||
import type { Nitro } from 'nitro/types' | ||
import { normalize } from 'path' | ||
|
||
export const runtimeDir = fileURLToPath( | ||
new URL('dist/runtime/', import.meta.url) | ||
) | ||
|
||
interface SpellDefinition { | ||
path: string | ||
data: any | ||
name: string | ||
} | ||
|
||
/** | ||
* Creates a Rollup plugin for JSON data. | ||
* @param moduleName The name of the module. | ||
* @param spells An array of spell definitions. | ||
* @returns A function that creates the Rollup plugin. | ||
*/ | ||
export function createRollupJsonPlugin( | ||
moduleName: string, | ||
spells: SpellDefinition[] | ||
) { | ||
return function (nitro: Nitro) { | ||
const generateSpellsCode = (): string => { | ||
const imports = spells.map( | ||
(spell, index) => `import spell${index} from '${spell.path}';` | ||
) | ||
const spellsArray = spells.map( | ||
(spell, index) => ` | ||
{ | ||
path: ${JSON.stringify(normalize(spell.path))}, | ||
data: spell${index}, | ||
name: ${JSON.stringify(spell.name)} | ||
}` | ||
) | ||
|
||
return ` | ||
${imports.join('\n')} | ||
export const spells = [ | ||
${spellsArray.join(',\n')} | ||
]; | ||
`.trim() | ||
} | ||
|
||
const virtualFiles: Record<string, () => string> = { | ||
[`#${moduleName}-virtual/spells`]: generateSpellsCode, | ||
} | ||
|
||
return virtual(virtualFiles, nitro.vfs) | ||
} | ||
} |
This file contains 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,42 @@ | ||
import { normalize, join } from 'pathe' | ||
import fg from 'fast-glob' | ||
import { readFile } from 'fs/promises' | ||
|
||
type ScanJsonFilesOptions = { | ||
fileFilter?: (file: string) => boolean | ||
filePatterns?: string[] | ||
cwd?: string | ||
} | ||
|
||
export async function scanJsonFilesFromDir( | ||
dir: string | string[], | ||
options?: ScanJsonFilesOptions | ||
): Promise<string[]> { | ||
const dirs = (Array.isArray(dir) ? dir : [dir]).map(d => normalize(d)) | ||
const fileFilter = options?.fileFilter || (() => true) | ||
const filePatterns = options?.filePatterns || ['*.spell.json'] | ||
|
||
const result = await Promise.all( | ||
dirs.map(async i => { | ||
const patterns = [i, ...filePatterns.map(p => join(i, p))] | ||
return fg(patterns, { | ||
absolute: true, | ||
cwd: options?.cwd || process.cwd(), | ||
onlyFiles: true, | ||
followSymbolicLinks: true, | ||
}) | ||
}) | ||
) | ||
|
||
const allFiles = result | ||
.flat() | ||
.map(f => normalize(f)) | ||
.sort() | ||
|
||
return Array.from(new Set(allFiles)).filter(fileFilter) | ||
} | ||
|
||
export async function readJsonFile(filePath: string): Promise<any> { | ||
const content = await readFile(filePath, 'utf-8') | ||
return JSON.parse(content) | ||
} |
This file contains 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 |
---|---|---|
@@ -1,17 +1,60 @@ | ||
import { defineNovaModule } from '@gtc-nova/kit' | ||
import { spellFeatures, type SpellFeatures } from './features' | ||
import { normalize } from 'pathe' | ||
import { | ||
scanJsonFilesFromDir, | ||
readJsonFile, | ||
createRollupJsonPlugin, | ||
} from './features' | ||
import { createRollupPlugin } from '@gtc-nova/kit/rollup' | ||
import type { Import } from 'unimport' | ||
|
||
export const spellsModule = defineNovaModule<SpellFeatures>({ | ||
const f = { | ||
spells: 'spells', | ||
} | ||
|
||
export const spellsModule = defineNovaModule<{}>({ | ||
name: 'spells', | ||
features: spellFeatures, | ||
featureTypeFunctions: { | ||
spells: () => { | ||
console.log('spells') | ||
}, | ||
}, | ||
pluginsDir: './../src/runtime/plugins', | ||
features: {}, | ||
featureTypeFunctions: {}, | ||
metaUrl: import.meta.url, | ||
pluginsDir: './../src/runtime/plugins', | ||
|
||
hooks: [], | ||
async setup(nitro) { | ||
const spellFiles = await scanJsonFilesFromDir('spells') | ||
const spellJSON = await Promise.all( | ||
spellFiles.map(async spellFile => { | ||
const spellData = await readJsonFile(spellFile) | ||
return { | ||
path: normalize(spellFile), | ||
data: spellData, | ||
name: normalize(spellFile).split('/').pop() as string, | ||
} | ||
}) | ||
) | ||
|
||
const scannedSpells: Import[] = spellJSON.map(spell => ({ | ||
name: spell.name as string, | ||
from: spell.path, | ||
as: 'default', | ||
})) | ||
|
||
// @ts-ignore | ||
nitro['scannedSpells'] = scannedSpells | ||
|
||
nitro.hooks.hook('rollup:before', async (nit, config) => { | ||
// @ts-ignore | ||
config.plugins.push(createRollupJsonPlugin(this.name, spellJSON)(nitro)) | ||
}) | ||
// nitro.hooks.hook('rollup:before', async (nitro, rollupConfig) => { | ||
// const rollupPlugin = createRollupPlugin('spells', ['spells']) | ||
|
||
// // @ts-ignore | ||
// rollupConfig.plugins?.push(rollupPlugin) | ||
// }) | ||
|
||
console.log('Scanned spells:', scannedSpells) | ||
}, | ||
}) | ||
|
||
export type { Spell, SerializedSpell, SpellRegistry } from './types' |
This file contains 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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
// @ts-ignore | ||
import { handlers as spells } from "#spells-virtual/spells"; | ||
import { spells } from '#spells-virtual/spells' | ||
|
||
import type { BaseVirtualHandler } from "@gtc-nova/kit/runtime"; | ||
import type { Spell } from "../types"; | ||
import type { Spell } from '../types' | ||
|
||
export const getVirtualSpells = (): BaseVirtualHandler<Spell>[] => spells; | ||
interface ScannedSpell { | ||
path: string | ||
data: Spell | ||
name: string | ||
} | ||
|
||
export const getVirtualSpells = (): ScannedSpell[] => spells |
66 changes: 15 additions & 51 deletions
66
grimoire/modules/spells/src/runtime/plugins/spells-plugin.ts
This file contains 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 |
---|---|---|
@@ -1,65 +1,29 @@ | ||
import { defineNovaPlugin } from '@gtc-nova/kit/runtime' | ||
import { spellRegistry } from '../utils/registry' | ||
import { SpellRegistryManager } from '../utils/registry' | ||
import { useRuntimeConfig } from 'nitro/runtime' | ||
import type { SpellFeatures } from '../../features' | ||
import { getVirtualSpells } from '../exports' | ||
import { readGraphFromJSON, writeGraphToJSON } from '@magickml/behave-graph' | ||
import type { Spell, SerializedSpell } from '../../types' | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
|
||
export default defineNovaPlugin<SpellFeatures, any, any, any, any>({ | ||
export default defineNovaPlugin<SpellFeatures, any>({ | ||
useRuntimeConfig, | ||
initialize: (nitro, config) => { | ||
const spellOptions = config.spells || {} | ||
return { spellOptions } | ||
}, | ||
before: async (nitro, br) => {}, | ||
runtimeSetup: { | ||
spells: { | ||
getVirtualHandlers: getVirtualSpells, | ||
initFeatureHandlers: async (nitro, handlers) => { | ||
for (const handler of handlers) { | ||
const spellDefinition = (await handler.handler()).default | ||
console.log('feature spell scanned:', spellDefinition) | ||
} | ||
// const spellsDir = path.join(process.cwd(), 'server', 'spells'); | ||
// await loadSpellsFromDirectory(spellsDir); | ||
// for (const handler of handlers) { | ||
// const spellDefinition = (await handler.handler()).default; | ||
// spellRegistry.add(spellDefinition); | ||
// } | ||
}, | ||
}, | ||
before: async (nitro, br) => { | ||
nitro.spellRegistry = new SpellRegistryManager() | ||
const virtualSpells = getVirtualSpells() | ||
|
||
console.log('Adding virtual spells to registry') | ||
|
||
for (const spell of virtualSpells) { | ||
nitro.spellRegistry.add(spell.data) | ||
} | ||
}, | ||
runtimeSetup: {}, // Since spells are serialized, we can skip this step | ||
after: (nitro, br) => { | ||
// @ts-ignore todo: declare | ||
nitro.spellRegistry = spellRegistry | ||
console.info( | ||
'Spell module initialized. Use nitro.spellRegistry to access spells.' | ||
) | ||
nitro.spellRegistry.getAll().forEach(spell => { | ||
console.log(spell) | ||
}) | ||
}, | ||
}) | ||
|
||
async function loadSpellsFromDirectory(directory: string) { | ||
try { | ||
const files = await fs.readdir(directory) | ||
for (const file of files) { | ||
if (path.extname(file) === '.json') { | ||
const filePath = path.join(directory, file) | ||
const content = await fs.readFile(filePath, 'utf-8') | ||
const serializedSpell: SerializedSpell = JSON.parse(content) | ||
const spell: Spell = { | ||
...serializedSpell, | ||
graph: readGraphFromJSON({ | ||
graphJson: serializedSpell.graph, | ||
registry: useRuntimeConfig().registry, | ||
}), | ||
} | ||
spellRegistry.add(spell) | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Error loading spells from directory:', error) | ||
} | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.