11import { WeaviateClient } from "weaviate-client" ;
2- import { QueryAgentResponse , ProgressMessage , StreamedTokens } from "./response/response.js" ;
3- import { mapResponse , mapProgressMessageFromSSE , mapStreamedTokensFromSSE , mapResponseFromSSE } from "./response/response-mapping.js" ;
2+ import {
3+ QueryAgentResponse ,
4+ ProgressMessage ,
5+ StreamedTokens ,
6+ } from "./response/response.js" ;
7+ import {
8+ mapResponse ,
9+ mapProgressMessageFromSSE ,
10+ mapStreamedTokensFromSSE ,
11+ mapResponseFromSSE ,
12+ } from "./response/response-mapping.js" ;
413import { mapApiResponse } from "./response/api-response-mapping.js" ;
514import { fetchServerSentEvents } from "./response/server-sent-events.js" ;
615import { mapCollections , QueryAgentCollectionConfig } from "./collection.js" ;
16+ import { handleError } from "./response/error.js" ;
717
818/**
919 * An agent for executing agentic queries against Weaviate.
@@ -74,7 +84,7 @@ export class QueryAgent {
7484 } ) ;
7585
7686 if ( ! response . ok ) {
77- throw Error ( `Query agent failed. ${ await response . text ( ) } ` ) ;
87+ await handleError ( response ) ;
7888 }
7989
8090 return mapResponse ( await response . json ( ) ) ;
@@ -100,22 +110,25 @@ export class QueryAgent {
100110 const { host, bearerToken, headers } =
101111 await this . client . getConnectionDetails ( ) ;
102112
103- const sseStream = fetchServerSentEvents ( `${ this . agentsHost } /agent/stream_query` , {
104- method : "POST" ,
105- headers : {
106- "Content-Type" : "application/json" ,
107- Authorization : bearerToken ! ,
108- "X-Weaviate-Cluster-Url" : host ,
109- } ,
110- body : JSON . stringify ( {
111- headers,
112- query,
113- collections : mapCollections ( targetCollections ) ,
114- system_prompt : this . systemPrompt ,
115- previous_response : context ? mapApiResponse ( context ) : undefined ,
116- include_progress : includeProgress ?? true ,
117- } ) ,
118- } ) ;
113+ const sseStream = fetchServerSentEvents (
114+ `${ this . agentsHost } /agent/stream_query` ,
115+ {
116+ method : "POST" ,
117+ headers : {
118+ "Content-Type" : "application/json" ,
119+ Authorization : bearerToken ! ,
120+ "X-Weaviate-Cluster-Url" : host ,
121+ } ,
122+ body : JSON . stringify ( {
123+ headers,
124+ query,
125+ collections : mapCollections ( targetCollections ) ,
126+ system_prompt : this . systemPrompt ,
127+ previous_response : context ? mapApiResponse ( context ) : undefined ,
128+ include_progress : includeProgress ?? true ,
129+ } ) ,
130+ }
131+ ) ;
119132
120133 for await ( const event of sseStream ) {
121134 let output : ProgressMessage | StreamedTokens | QueryAgentResponse ;
0 commit comments