1+ import { checkSchemaMatch } from "@/lib/schema-match.ts" ;
2+ import { Spell , SpellSchema } from "../spell.ts" ;
3+ import { Schema } from "jsonschema" ;
4+
15interface SpellSearchResult {
26 spells : Array < {
37 key : string ;
@@ -26,7 +30,7 @@ interface SpellSearchParams {
2630 query : string ;
2731 tags ?: string [ ] ;
2832 referencedKeys : string [ ] ;
29- spells : Record < string , Record < string , unknown > > ;
33+ spells : Record < string , Spell > ;
3034 blobs : Record < string , Record < string , unknown > > ;
3135 options ?: {
3236 limit ?: number ;
@@ -45,7 +49,6 @@ function calculateRank(itemStr: string, tags: string[] = []): number {
4549
4650 return rank ;
4751}
48-
4952export function processSpellSearch ( {
5053 query,
5154 tags = [ ] ,
@@ -54,75 +57,107 @@ export function processSpellSearch({
5457 blobs,
5558 options = { } ,
5659} : SpellSearchParams ) : SpellSearchResult {
57- const spellMatches : SpellSearchResult [ "spells" ] = [ ] ;
58- const blobMatches : SpellSearchResult [ "blobs" ] = [ ] ;
59-
60- const limit = options . limit || 10 ;
61- const searchTerms = query . toLowerCase ( ) . replace ( / @ \w + / g, "" ) . trim ( ) ;
62-
63- // Handle @references first
64- for ( const key of referencedKeys ) {
65- const spellKey = `spell-${ key } ` ;
66- const blobKey = key ;
67-
68- // Check for referenced spells
69- if ( spells [ spellKey ] ) {
70- spellMatches . push ( {
71- key : spellKey ,
72- name : spells [ spellKey ] . name as string || spellKey ,
73- description : spells [ spellKey ] . description as string || "No description" ,
74- matchType : "reference" ,
75- compatibleBlobs : findCompatibleBlobs ( spells [ spellKey ] , blobs ) ,
76- } ) ;
77- }
60+ try {
61+ const spellMatches : SpellSearchResult [ "spells" ] = [ ] ;
62+ const blobMatches : SpellSearchResult [ "blobs" ] = [ ] ;
7863
79- // Check for referenced blobs
80- if ( blobs [ blobKey ] && ! blobKey . startsWith ( "spell-" ) ) {
81- blobMatches . push ( {
82- key : blobKey ,
83- snippet : getRelevantSnippet ( JSON . stringify ( blobs [ blobKey ] ) ) ,
84- matchType : "reference" ,
85- compatibleSpells : findCompatibleSpells ( blobs [ blobKey ] , spells ) ,
86- } ) ;
64+ console . log ( spells ) ;
65+
66+ // Parse spells using schema
67+ const validSpells : Record < string , Spell > = { } ;
68+ for ( const [ key , spell ] of Object . entries ( spells ) ) {
69+ try {
70+ const parsed = SpellSchema . parse ( spell ) ;
71+ validSpells [ key ] = parsed ;
72+ console . log ( "checked spell" , parsed ) ;
73+ } catch ( error : any ) {
74+ console . log ( `Invalid spell ${ key } :` , error . message ) ;
75+ continue ;
76+ }
8777 }
88- }
8978
90- // Perform text search if we haven't hit the limit
91- if ( searchTerms ) {
92- // Search spells with tags
93- if ( spellMatches . length < limit ) {
94- const textSpellMatches = searchSpells (
95- searchTerms ,
96- spells ,
97- blobs ,
98- limit - spellMatches . length ,
99- tags ,
100- ) ;
101- spellMatches . push ( ...textSpellMatches ) ;
79+ console . log ( "Valid spells:" , Object . keys ( validSpells ) ) ;
80+
81+ const limit = options . limit || 10 ;
82+ const searchTerms = query . toLowerCase ( ) . replace ( / @ \w + / g, "" ) . trim ( ) ;
83+
84+ // Handle @references first
85+ for ( const key of referencedKeys ) {
86+ const spellKey = `spell-${ key } ` ;
87+ const blobKey = key ;
88+
89+ // Check for referenced spells
90+ if ( validSpells [ spellKey ] ) {
91+ spellMatches . push ( {
92+ key : spellKey ,
93+ name : validSpells [ spellKey ] . spellbookTitle as string || spellKey ,
94+ description :
95+ validSpells [ spellKey ] . recipe . argumentSchema . description as string ||
96+ "No description" ,
97+ matchType : "reference" ,
98+ compatibleBlobs : findCompatibleBlobs ( validSpells [ spellKey ] , blobs ) ,
99+ } ) ;
100+ }
101+
102+ // Check for referenced blobs
103+ if ( blobs [ blobKey ] && ! blobKey . startsWith ( "spell-" ) ) {
104+ blobMatches . push ( {
105+ key : blobKey ,
106+ snippet : getRelevantSnippet ( JSON . stringify ( blobs [ blobKey ] ) ) ,
107+ matchType : "reference" ,
108+ compatibleSpells : findCompatibleSpells ( blobs [ blobKey ] , validSpells ) ,
109+ } ) ;
110+ }
102111 }
103112
104- // Search blobs with tags
105- if ( blobMatches . length < limit ) {
106- const textBlobMatches = searchBlobs (
107- searchTerms ,
108- blobs ,
109- spells ,
110- limit - blobMatches . length ,
111- tags ,
112- ) ;
113- blobMatches . push ( ...textBlobMatches ) ;
113+ // Perform text search if we haven't hit the limit
114+ if ( searchTerms ) {
115+ // Search spells with tags
116+ if ( spellMatches . length < limit ) {
117+ const textSpellMatches = searchSpells (
118+ searchTerms ,
119+ validSpells ,
120+ blobs ,
121+ limit - spellMatches . length ,
122+ tags ,
123+ ) ;
124+ spellMatches . push ( ...textSpellMatches ) ;
125+ }
126+
127+ // Search blobs with tags
128+ if ( blobMatches . length < limit ) {
129+ const textBlobMatches = searchBlobs (
130+ searchTerms ,
131+ blobs ,
132+ validSpells ,
133+ limit - blobMatches . length ,
134+ tags ,
135+ ) ;
136+ blobMatches . push ( ...textBlobMatches ) ;
137+ }
114138 }
115- }
116139
117- return {
118- spells : spellMatches . slice ( 0 , limit ) ,
119- blobs : blobMatches . slice ( 0 , limit ) ,
120- } ;
140+ return {
141+ spells : spellMatches . slice ( 0 , limit ) ,
142+ blobs : blobMatches . slice ( 0 , limit ) ,
143+ } ;
144+ } catch ( error ) {
145+ console . error ( "Error in processSpellSearch:" , {
146+ error,
147+ errorMessage : error instanceof Error ? error . message : String ( error ) ,
148+ errorStack : error instanceof Error ? error . stack : undefined ,
149+ query,
150+ referencedKeys,
151+ spellKeys : Object . keys ( spells ) ,
152+ blobKeys : Object . keys ( blobs ) ,
153+ } ) ;
154+ return { spells : [ ] , blobs : [ ] } ;
155+ }
121156}
122157
123158function searchSpells (
124159 searchTerms : string ,
125- spells : Record < string , Record < string , unknown > > ,
160+ spells : Record < string , Spell > ,
126161 blobs : Record < string , Record < string , unknown > > ,
127162 limit : number ,
128163 tags : string [ ] = [ ] ,
@@ -135,8 +170,9 @@ function searchSpells(
135170 const rank = calculateRank ( spellStr , tags ) ;
136171 matches . push ( {
137172 key,
138- name : spell . name as string || key ,
139- description : spell . description as string || "No description" ,
173+ name : spell . spellbookTitle as string || key ,
174+ description : spell . recipe . argumentSchema ?. description as string ||
175+ "No description" ,
140176 matchType : "text-match" as const ,
141177 compatibleBlobs : findCompatibleBlobs ( spell , blobs ) ,
142178 rank,
@@ -153,7 +189,7 @@ function searchSpells(
153189function searchBlobs (
154190 searchTerms : string ,
155191 blobs : Record < string , Record < string , unknown > > ,
156- spells : Record < string , Record < string , unknown > > ,
192+ spells : Record < string , Spell > ,
157193 limit : number ,
158194 tags : string [ ] = [ ] ,
159195) : SpellSearchResult [ "blobs" ] {
@@ -183,7 +219,7 @@ function searchBlobs(
183219
184220function findCompatibleSpells (
185221 blob : Record < string , unknown > ,
186- spells : Record < string , Record < string , unknown > > ,
222+ spells : Record < string , Spell > ,
187223) : Array < { key : string ; name : string ; description : string } > {
188224 const compatible : Array < { key : string ; name : string ; description : string } > =
189225 [ ] ;
@@ -192,13 +228,14 @@ function findCompatibleSpells(
192228 for ( const [ key , spell ] of Object . entries ( spells ) ) {
193229 const spellStr = JSON . stringify ( spell ) . toLowerCase ( ) ;
194230 if (
195- hasMatchingSchema ( spell , blob ) ||
231+ checkSchemaMatch ( blob , spell . recipe . argumentSchema . properties ) ||
196232 spellStr . includes ( blob . key ?. toString ( ) . toLowerCase ( ) || "" )
197233 ) {
198234 compatible . push ( {
199235 key,
200- name : spell . name as string || key ,
201- description : spell . description as string || "No description" ,
236+ name : spell . spellbookTitle as string || key ,
237+ description : spell . recipe . argumentSchema . description as string ||
238+ "No description" ,
202239 } ) ;
203240 }
204241 }
@@ -207,7 +244,7 @@ function findCompatibleSpells(
207244}
208245
209246function findCompatibleBlobs (
210- spell : Record < string , unknown > ,
247+ spell : Spell ,
211248 blobs : Record < string , Record < string , unknown > > ,
212249) : Array < { key : string ; snippet : string ; data : unknown } > {
213250 const compatible : Array < { key : string ; snippet : string ; data : unknown } > = [ ] ;
@@ -218,7 +255,7 @@ function findCompatibleBlobs(
218255
219256 const blobStr = JSON . stringify ( blob ) . toLowerCase ( ) ;
220257 if (
221- hasMatchingSchema ( spell , blob ) ||
258+ checkSchemaMatch ( blob , spell . recipe . argumentSchema . properties ) ||
222259 spellStr . includes ( key . toLowerCase ( ) )
223260 ) {
224261 compatible . push ( {
0 commit comments