@@ -3,11 +3,14 @@ import type { Env } from '../lib/types'
33import {
44 getNotebook ,
55 getSource ,
6+ listNotebookChunks ,
67 listNotebooks ,
78 listSources ,
89 patchNotebook ,
910 softDeleteNotebook ,
1011} from '../lib/v2-store'
12+ import { generateQueryEmbedding } from '../lib/embeddings'
13+ import { VectorStore } from '../lib/vectorize'
1114
1215const notebooksV2Manage = new Hono < { Bindings : Env } > ( )
1316
@@ -113,4 +116,57 @@ notebooksV2Manage.get('/:notebookId/sources/:sourceId', async (c) => {
113116 return c . json ( source )
114117} )
115118
119+ notebooksV2Manage . post ( '/:notebookId/search' , async ( c ) => {
120+ const workspaceId = requireParam ( c . req . param ( 'workspaceId' ) , 'workspaceId' )
121+ const notebookId = requireParam ( c . req . param ( 'notebookId' ) , 'notebookId' )
122+ const body = await c . req . json < {
123+ query : string
124+ limit ?: number
125+ threshold ?: number
126+ } > ( )
127+
128+ if ( ! body . query ?. trim ( ) ) {
129+ return c . json ( { error : 'query is required' } , 400 )
130+ }
131+
132+ const notebook = await getNotebook ( c . env , workspaceId , notebookId )
133+ if ( ! notebook ) return c . json ( { error : 'notebook not found' } , 404 )
134+
135+ const queryVector = await generateQueryEmbedding ( body . query , c . env )
136+ const store = new VectorStore ( c . env )
137+ const limit = Math . min ( Math . max ( body . limit ?? 10 , 1 ) , 50 )
138+ const threshold = body . threshold ?? 0
139+
140+ const results = await store . search ( queryVector , limit , threshold , {
141+ hydrateFullText : true ,
142+ notebookId,
143+ } )
144+
145+ return c . json ( {
146+ query : body . query ,
147+ notebookId,
148+ count : results . length ,
149+ results,
150+ } )
151+ } )
152+
153+ notebooksV2Manage . get ( '/:notebookId/chunks' , async ( c ) => {
154+ const workspaceId = requireParam ( c . req . param ( 'workspaceId' ) , 'workspaceId' )
155+ const notebookId = requireParam ( c . req . param ( 'notebookId' ) , 'notebookId' )
156+ const limit = Math . min (
157+ Math . max ( parseInt ( c . req . query ( 'limit' ) ?? '50' , 10 ) || 50 , 1 ) ,
158+ 200
159+ )
160+
161+ const notebook = await getNotebook ( c . env , workspaceId , notebookId )
162+ if ( ! notebook ) return c . json ( { error : 'notebook not found' } , 404 )
163+
164+ const chunks = await listNotebookChunks ( c . env , workspaceId , notebookId , limit )
165+ return c . json ( {
166+ notebookId,
167+ count : chunks . length ,
168+ chunks,
169+ } )
170+ } )
171+
116172export { notebooksV2Manage }
0 commit comments