@@ -12,10 +12,15 @@ import {
1212} from "./response/response-mapping.js" ;
1313import { mapApiResponse } from "./response/api-response-mapping.js" ;
1414import { fetchServerSentEvents } from "./response/server-sent-events.js" ;
15- import { mapCollections , QueryAgentCollectionConfig } from "./collection.js" ;
15+ import {
16+ mapCollections ,
17+ QueryAgentCollection ,
18+ QueryAgentCollectionConfig ,
19+ } from "./collection.js" ;
1620import { handleError } from "./response/error.js" ;
1721import { QueryAgentSearcher } from "./search.js" ;
1822import { SearchModeResponse } from "./response/response.js" ;
23+ import { getHeaders } from "./connection.js" ;
1924
2025/**
2126 * An agent for executing agentic queries against Weaviate.
@@ -97,32 +102,22 @@ export class QueryAgent {
97102 /**
98103 * Ask query agent a question.
99104 *
100- * @param query - The natural language query string for the agent.
105+ * @param query - The natural language query string or conversation context for the agent.
101106 * @param options - Additional options for the run.
102107 * @returns The response from the query agent.
103108 */
104109 async ask (
105- query : string ,
110+ query : QueryAgentQuery ,
106111 { collections } : QueryAgentAskOptions = { } ,
107112 ) : Promise < QueryAgentResponse > {
108- const targetCollections = collections ?? this . collections ;
109- if ( ! targetCollections ) {
110- throw Error ( "No collections provided to the query agent." ) ;
111- }
112-
113- const { host, bearerToken, headers } =
114- await this . client . getConnectionDetails ( ) ;
113+ const targetCollections = this . validateCollections ( collections ) ;
114+ const { requestHeaders, connectionHeaders } = await getHeaders ( this . client ) ;
115115
116116 const response = await fetch ( `${ this . agentsHost } /agent/query` , {
117117 method : "POST" ,
118- headers : {
119- "Content-Type" : "application/json" ,
120- Authorization : bearerToken ! ,
121- "X-Weaviate-Cluster-Url" : host ,
122- "X-Agent-Request-Origin" : "typescript-client" ,
123- } ,
118+ headers : requestHeaders ,
124119 body : JSON . stringify ( {
125- headers,
120+ headers : connectionHeaders ,
126121 query,
127122 collections : mapCollections ( targetCollections ) ,
128123 system_prompt : this . systemPrompt ,
@@ -238,40 +233,40 @@ export class QueryAgent {
238233 /**
239234 * Ask query agent a question and stream the response.
240235 *
241- * @param query - The natural language query string for the agent.
236+ * @param query - The natural language query string or conversation context for the agent.
242237 * @param options - Additional options for the run.
243238 * @returns The response from the query agent.
244239 */
245240 askStream (
246- query : string ,
241+ query : QueryAgentQuery ,
247242 options : QueryAgentAskStreamOptions & {
248243 includeProgress : false ;
249244 includeFinalState : false ;
250245 } ,
251246 ) : AsyncGenerator < StreamedTokens > ;
252247 askStream (
253- query : string ,
248+ query : QueryAgentQuery ,
254249 options : QueryAgentAskStreamOptions & {
255250 includeProgress : false ;
256251 includeFinalState ?: true ;
257252 } ,
258253 ) : AsyncGenerator < StreamedTokens | QueryAgentResponse > ;
259254 askStream (
260- query : string ,
255+ query : QueryAgentQuery ,
261256 options : QueryAgentAskStreamOptions & {
262257 includeProgress ?: true ;
263258 includeFinalState : false ;
264259 } ,
265260 ) : AsyncGenerator < ProgressMessage | StreamedTokens > ;
266261 askStream (
267- query : string ,
262+ query : QueryAgentQuery ,
268263 options ?: QueryAgentAskStreamOptions & {
269264 includeProgress ?: true ;
270265 includeFinalState ?: true ;
271266 } ,
272267 ) : AsyncGenerator < ProgressMessage | StreamedTokens | QueryAgentResponse > ;
273268 async * askStream (
274- query : string ,
269+ query : QueryAgentQuery ,
275270 {
276271 collections,
277272 includeProgress,
@@ -336,28 +331,54 @@ export class QueryAgent {
336331 * reuses the same underlying searches to ensure consistency across pages.
337332 */
338333 async search (
339- query : string ,
334+ query : QueryAgentQuery ,
340335 { limit = 20 , collections } : QueryAgentSearchOnlyOptions = { } ,
341336 ) : Promise < SearchModeResponse > {
342- const searcher = new QueryAgentSearcher ( this . client , query , {
343- collections : collections ?? this . collections ,
344- systemPrompt : this . systemPrompt ,
345- agentsHost : this . agentsHost ,
346- } ) ;
337+ const searcher = new QueryAgentSearcher (
338+ this . client ,
339+ query ,
340+ this . validateCollections ( collections ) ,
341+ this . systemPrompt ,
342+ this . agentsHost ,
343+ ) ;
344+
347345 return searcher . run ( { limit, offset : 0 } ) ;
348346 }
347+
348+ private validateCollections = (
349+ collections ?: QueryAgentCollection [ ] ,
350+ ) : QueryAgentCollection [ ] => {
351+ const targetCollections = collections ?? this . collections ;
352+
353+ if ( ! targetCollections ) {
354+ throw Error ( "No collections provided to the query agent." ) ;
355+ }
356+
357+ return targetCollections ;
358+ } ;
349359}
350360
351361/** Options for the QueryAgent. */
352362export type QueryAgentOptions = {
353363 /** List of collections to query. Will be overriden if passed in the `run` method. */
354- collections ?: ( string | QueryAgentCollectionConfig ) [ ] ;
364+ collections ?: QueryAgentCollection [ ] ;
355365 /** System prompt to guide the agent's behavior. */
356366 systemPrompt ?: string ;
357367 /** Host of the agents service. */
358368 agentsHost ?: string ;
359369} ;
360370
371+ export type QueryAgentQuery = string | ConversationContext ;
372+
373+ export type ConversationContext = {
374+ messages : ChatMessage [ ] ;
375+ } ;
376+
377+ export type ChatMessage = {
378+ role : "user" | "assistant" ;
379+ content : string ;
380+ } ;
381+
361382/** Options for the QueryAgent run. */
362383export type QueryAgentRunOptions = {
363384 /** List of collections to query. Will override any collections if passed in the constructor. */
0 commit comments