@antfly/sdk 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +167 -346
- package/dist/index.d.cts +28760 -1183
- package/dist/index.d.ts +28760 -1183
- package/dist/index.js +167 -346
- package/package.json +11 -12
package/dist/index.cjs
CHANGED
|
@@ -185,14 +185,14 @@ var AntflyClient = class {
|
|
|
185
185
|
*/
|
|
186
186
|
scan: (tableName, request) => {
|
|
187
187
|
const config = this.config;
|
|
188
|
+
const authHeader = this.getAuthHeader();
|
|
188
189
|
async function* scanGenerator() {
|
|
189
190
|
const headers = {
|
|
190
191
|
"Content-Type": "application/json",
|
|
191
192
|
Accept: "application/x-ndjson"
|
|
192
193
|
};
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
headers["Authorization"] = `Basic ${auth}`;
|
|
194
|
+
if (authHeader) {
|
|
195
|
+
headers.Authorization = authHeader;
|
|
196
196
|
}
|
|
197
197
|
Object.assign(headers, config.headers);
|
|
198
198
|
const response = await fetch(`${config.baseUrl}/tables/${tableName}/lookup`, {
|
|
@@ -241,16 +241,6 @@ var AntflyClient = class {
|
|
|
241
241
|
results.push(doc);
|
|
242
242
|
}
|
|
243
243
|
return results;
|
|
244
|
-
},
|
|
245
|
-
/**
|
|
246
|
-
* RAG (Retrieval-Augmented Generation) query on a specific table with streaming or citations
|
|
247
|
-
* @param tableName - Name of the table to query
|
|
248
|
-
* @param request - RAG request with query and summarizer config (set with_streaming: true to enable streaming)
|
|
249
|
-
* @param callbacks - Optional callbacks for structured SSE events (hit, summary, citation, done, error)
|
|
250
|
-
* @returns Promise with RAG result (JSON) or AbortController (when streaming)
|
|
251
|
-
*/
|
|
252
|
-
rag: async (tableName, request, callbacks) => {
|
|
253
|
-
return this.performRag(`/tables/${tableName}/rag`, request, callbacks);
|
|
254
244
|
}
|
|
255
245
|
};
|
|
256
246
|
/**
|
|
@@ -397,16 +387,41 @@ var AntflyClient = class {
|
|
|
397
387
|
}
|
|
398
388
|
};
|
|
399
389
|
this.config = config;
|
|
390
|
+
this.client = this.buildClient();
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Build the Authorization header value from the auth config.
|
|
394
|
+
* Returns undefined if no auth is configured.
|
|
395
|
+
*/
|
|
396
|
+
getAuthHeader() {
|
|
397
|
+
const auth = this.config.auth;
|
|
398
|
+
if (!auth) return void 0;
|
|
399
|
+
if ("type" in auth) {
|
|
400
|
+
switch (auth.type) {
|
|
401
|
+
case "basic":
|
|
402
|
+
return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
|
|
403
|
+
case "apiKey":
|
|
404
|
+
return `ApiKey ${btoa(`${auth.keyId}:${auth.keySecret}`)}`;
|
|
405
|
+
case "bearer":
|
|
406
|
+
return `Bearer ${auth.token}`;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Build the openapi-fetch client with current config.
|
|
413
|
+
*/
|
|
414
|
+
buildClient() {
|
|
400
415
|
const headers = {
|
|
401
416
|
"Content-Type": "application/json",
|
|
402
|
-
...config.headers
|
|
417
|
+
...this.config.headers
|
|
403
418
|
};
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
headers
|
|
419
|
+
const authHeader = this.getAuthHeader();
|
|
420
|
+
if (authHeader) {
|
|
421
|
+
headers.Authorization = authHeader;
|
|
407
422
|
}
|
|
408
|
-
|
|
409
|
-
baseUrl: config.baseUrl,
|
|
423
|
+
return (0, import_openapi_fetch.default)({
|
|
424
|
+
baseUrl: this.config.baseUrl,
|
|
410
425
|
headers,
|
|
411
426
|
bodySerializer: (body) => {
|
|
412
427
|
if (typeof body === "string") {
|
|
@@ -416,25 +431,13 @@ var AntflyClient = class {
|
|
|
416
431
|
}
|
|
417
432
|
});
|
|
418
433
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
this.client = (
|
|
426
|
-
baseUrl: this.config.baseUrl,
|
|
427
|
-
headers: {
|
|
428
|
-
...this.config.headers,
|
|
429
|
-
Authorization: `Basic ${auth}`
|
|
430
|
-
},
|
|
431
|
-
bodySerializer: (body) => {
|
|
432
|
-
if (typeof body === "string") {
|
|
433
|
-
return body;
|
|
434
|
-
}
|
|
435
|
-
return JSON.stringify(body);
|
|
436
|
-
}
|
|
437
|
-
});
|
|
434
|
+
setAuth(authOrUsername, password) {
|
|
435
|
+
if (typeof authOrUsername === "string" && password !== void 0) {
|
|
436
|
+
this.config.auth = { username: authOrUsername, password };
|
|
437
|
+
} else {
|
|
438
|
+
this.config.auth = authOrUsername;
|
|
439
|
+
}
|
|
440
|
+
this.client = this.buildClient();
|
|
438
441
|
}
|
|
439
442
|
/**
|
|
440
443
|
* Get cluster status
|
|
@@ -467,7 +470,8 @@ var AntflyClient = class {
|
|
|
467
470
|
* Private helper for multiquery requests to avoid code duplication
|
|
468
471
|
*/
|
|
469
472
|
async performMultiquery(path, requests, tableName) {
|
|
470
|
-
const ndjson = requests.map((request) => JSON.stringify(request)).join("\n")
|
|
473
|
+
const ndjson = `${requests.map((request) => JSON.stringify(request)).join("\n")}
|
|
474
|
+
`;
|
|
471
475
|
if (path === "/tables/{tableName}/query" && tableName) {
|
|
472
476
|
const { data, error } = await this.client.POST("/tables/{tableName}/query", {
|
|
473
477
|
params: { path: { tableName } },
|
|
@@ -503,136 +507,20 @@ var AntflyClient = class {
|
|
|
503
507
|
return this.performMultiquery("/query", requests);
|
|
504
508
|
}
|
|
505
509
|
/**
|
|
506
|
-
* Private helper for
|
|
507
|
-
*/
|
|
508
|
-
async performRag(path, request, callbacks) {
|
|
509
|
-
const headers = {
|
|
510
|
-
"Content-Type": "application/json",
|
|
511
|
-
Accept: "text/event-stream, application/json"
|
|
512
|
-
};
|
|
513
|
-
if (this.config.auth) {
|
|
514
|
-
const auth = btoa(`${this.config.auth.username}:${this.config.auth.password}`);
|
|
515
|
-
headers["Authorization"] = `Basic ${auth}`;
|
|
516
|
-
}
|
|
517
|
-
Object.assign(headers, this.config.headers);
|
|
518
|
-
const abortController = new AbortController();
|
|
519
|
-
const response = await fetch(`${this.config.baseUrl}${path}`, {
|
|
520
|
-
method: "POST",
|
|
521
|
-
headers,
|
|
522
|
-
body: JSON.stringify(request),
|
|
523
|
-
signal: abortController.signal
|
|
524
|
-
});
|
|
525
|
-
if (!response.ok) {
|
|
526
|
-
const errorText = await response.text();
|
|
527
|
-
throw new Error(`RAG request failed: ${response.status} ${errorText}`);
|
|
528
|
-
}
|
|
529
|
-
if (!response.body) {
|
|
530
|
-
throw new Error("Response body is null");
|
|
531
|
-
}
|
|
532
|
-
const contentType = response.headers.get("content-type") || "";
|
|
533
|
-
const isJSON = contentType.includes("application/json");
|
|
534
|
-
if (isJSON) {
|
|
535
|
-
const ragResult = await response.json();
|
|
536
|
-
return ragResult;
|
|
537
|
-
}
|
|
538
|
-
if (callbacks) {
|
|
539
|
-
const reader = response.body.getReader();
|
|
540
|
-
const decoder = new TextDecoder();
|
|
541
|
-
let buffer = "";
|
|
542
|
-
let currentEvent = "";
|
|
543
|
-
(async () => {
|
|
544
|
-
try {
|
|
545
|
-
while (true) {
|
|
546
|
-
const { done, value } = await reader.read();
|
|
547
|
-
if (done) break;
|
|
548
|
-
buffer += decoder.decode(value, { stream: true });
|
|
549
|
-
const lines = buffer.split("\n");
|
|
550
|
-
buffer = lines.pop() || "";
|
|
551
|
-
for (const line of lines) {
|
|
552
|
-
if (!line.trim()) {
|
|
553
|
-
currentEvent = "";
|
|
554
|
-
continue;
|
|
555
|
-
}
|
|
556
|
-
if (line.startsWith("event: ")) {
|
|
557
|
-
currentEvent = line.slice(7).trim();
|
|
558
|
-
} else if (line.startsWith("data: ")) {
|
|
559
|
-
const data = line.slice(6).trim();
|
|
560
|
-
try {
|
|
561
|
-
switch (currentEvent) {
|
|
562
|
-
case "hits_start":
|
|
563
|
-
if (callbacks.onHitsStart) {
|
|
564
|
-
const hitsStartData = JSON.parse(data);
|
|
565
|
-
callbacks.onHitsStart(hitsStartData);
|
|
566
|
-
}
|
|
567
|
-
break;
|
|
568
|
-
case "hit":
|
|
569
|
-
if (callbacks.onHit) {
|
|
570
|
-
const hit = JSON.parse(data);
|
|
571
|
-
callbacks.onHit(hit);
|
|
572
|
-
}
|
|
573
|
-
break;
|
|
574
|
-
case "hits_end":
|
|
575
|
-
if (callbacks.onHitsEnd) {
|
|
576
|
-
const hitsEndData = JSON.parse(data);
|
|
577
|
-
callbacks.onHitsEnd(hitsEndData);
|
|
578
|
-
}
|
|
579
|
-
break;
|
|
580
|
-
case "table_result":
|
|
581
|
-
if (callbacks.onHit) {
|
|
582
|
-
const result = JSON.parse(data);
|
|
583
|
-
const hits = result?.hits?.hits || [];
|
|
584
|
-
hits.forEach((hit) => callbacks.onHit(hit));
|
|
585
|
-
}
|
|
586
|
-
break;
|
|
587
|
-
case "summary":
|
|
588
|
-
if (callbacks.onSummary) {
|
|
589
|
-
const chunk = JSON.parse(data);
|
|
590
|
-
callbacks.onSummary(chunk);
|
|
591
|
-
}
|
|
592
|
-
break;
|
|
593
|
-
case "done":
|
|
594
|
-
if (callbacks.onDone) {
|
|
595
|
-
const doneData = JSON.parse(data);
|
|
596
|
-
callbacks.onDone(doneData);
|
|
597
|
-
}
|
|
598
|
-
return;
|
|
599
|
-
case "error":
|
|
600
|
-
if (callbacks.onError) {
|
|
601
|
-
const error = JSON.parse(data);
|
|
602
|
-
callbacks.onError(error);
|
|
603
|
-
}
|
|
604
|
-
throw new Error(data);
|
|
605
|
-
}
|
|
606
|
-
} catch (e) {
|
|
607
|
-
console.warn("Failed to parse SSE data:", currentEvent, data, e);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
} catch (error) {
|
|
613
|
-
if (error.name !== "AbortError") {
|
|
614
|
-
console.error("RAG streaming error:", error);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
})();
|
|
618
|
-
}
|
|
619
|
-
return abortController;
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Private helper for Answer Agent requests to avoid code duplication
|
|
510
|
+
* Private helper for Retrieval Agent requests to handle streaming and non-streaming responses
|
|
623
511
|
*/
|
|
624
|
-
async
|
|
512
|
+
async performRetrievalAgent(request, callbacks) {
|
|
625
513
|
const headers = {
|
|
626
514
|
"Content-Type": "application/json",
|
|
627
515
|
Accept: "text/event-stream, application/json"
|
|
628
516
|
};
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
headers
|
|
517
|
+
const authHeader = this.getAuthHeader();
|
|
518
|
+
if (authHeader) {
|
|
519
|
+
headers.Authorization = authHeader;
|
|
632
520
|
}
|
|
633
521
|
Object.assign(headers, this.config.headers);
|
|
634
522
|
const abortController = new AbortController();
|
|
635
|
-
const response = await fetch(`${this.config.baseUrl}
|
|
523
|
+
const response = await fetch(`${this.config.baseUrl}/agents/retrieval`, {
|
|
636
524
|
method: "POST",
|
|
637
525
|
headers,
|
|
638
526
|
body: JSON.stringify(request),
|
|
@@ -640,7 +528,7 @@ var AntflyClient = class {
|
|
|
640
528
|
});
|
|
641
529
|
if (!response.ok) {
|
|
642
530
|
const errorText = await response.text();
|
|
643
|
-
throw new Error(`
|
|
531
|
+
throw new Error(`Retrieval agent request failed: ${response.status} ${errorText}`);
|
|
644
532
|
}
|
|
645
533
|
if (!response.body) {
|
|
646
534
|
throw new Error("Response body is null");
|
|
@@ -677,8 +565,7 @@ var AntflyClient = class {
|
|
|
677
565
|
switch (currentEvent) {
|
|
678
566
|
case "classification":
|
|
679
567
|
if (callbacks.onClassification) {
|
|
680
|
-
|
|
681
|
-
callbacks.onClassification(classData);
|
|
568
|
+
callbacks.onClassification(JSON.parse(data));
|
|
682
569
|
}
|
|
683
570
|
break;
|
|
684
571
|
case "reasoning":
|
|
@@ -686,56 +573,69 @@ var AntflyClient = class {
|
|
|
686
573
|
callbacks.onReasoning(JSON.parse(data));
|
|
687
574
|
}
|
|
688
575
|
break;
|
|
689
|
-
case "
|
|
690
|
-
if (callbacks.
|
|
691
|
-
|
|
692
|
-
|
|
576
|
+
case "clarification_required":
|
|
577
|
+
if (callbacks.onClarificationRequired) {
|
|
578
|
+
callbacks.onClarificationRequired(JSON.parse(data));
|
|
579
|
+
}
|
|
580
|
+
break;
|
|
581
|
+
case "filter_applied":
|
|
582
|
+
if (callbacks.onFilterApplied) {
|
|
583
|
+
callbacks.onFilterApplied(JSON.parse(data));
|
|
584
|
+
}
|
|
585
|
+
break;
|
|
586
|
+
case "search_executed":
|
|
587
|
+
if (callbacks.onSearchExecuted) {
|
|
588
|
+
callbacks.onSearchExecuted(JSON.parse(data));
|
|
693
589
|
}
|
|
694
590
|
break;
|
|
695
591
|
case "hit":
|
|
696
592
|
if (callbacks.onHit) {
|
|
697
|
-
|
|
698
|
-
|
|
593
|
+
callbacks.onHit(JSON.parse(data));
|
|
594
|
+
}
|
|
595
|
+
break;
|
|
596
|
+
case "generation":
|
|
597
|
+
if (callbacks.onGeneration) {
|
|
598
|
+
callbacks.onGeneration(JSON.parse(data));
|
|
599
|
+
}
|
|
600
|
+
break;
|
|
601
|
+
case "step_started":
|
|
602
|
+
if (callbacks.onStepStarted) {
|
|
603
|
+
callbacks.onStepStarted(JSON.parse(data));
|
|
699
604
|
}
|
|
700
605
|
break;
|
|
701
|
-
case "
|
|
702
|
-
if (callbacks.
|
|
703
|
-
|
|
704
|
-
callbacks.onHitsEnd(hitsEndData);
|
|
606
|
+
case "step_progress":
|
|
607
|
+
if (callbacks.onStepProgress) {
|
|
608
|
+
callbacks.onStepProgress(JSON.parse(data));
|
|
705
609
|
}
|
|
706
610
|
break;
|
|
707
|
-
case "
|
|
708
|
-
if (callbacks.
|
|
709
|
-
callbacks.
|
|
611
|
+
case "step_completed":
|
|
612
|
+
if (callbacks.onStepCompleted) {
|
|
613
|
+
callbacks.onStepCompleted(JSON.parse(data));
|
|
710
614
|
}
|
|
711
615
|
break;
|
|
712
616
|
case "confidence":
|
|
713
617
|
if (callbacks.onConfidence) {
|
|
714
|
-
|
|
715
|
-
callbacks.onConfidence(confidence);
|
|
618
|
+
callbacks.onConfidence(JSON.parse(data));
|
|
716
619
|
}
|
|
717
620
|
break;
|
|
718
|
-
case "
|
|
719
|
-
if (callbacks.
|
|
720
|
-
callbacks.
|
|
621
|
+
case "followup":
|
|
622
|
+
if (callbacks.onFollowup) {
|
|
623
|
+
callbacks.onFollowup(JSON.parse(data));
|
|
721
624
|
}
|
|
722
625
|
break;
|
|
723
626
|
case "eval":
|
|
724
627
|
if (callbacks.onEvalResult) {
|
|
725
|
-
|
|
726
|
-
callbacks.onEvalResult(evalResult);
|
|
628
|
+
callbacks.onEvalResult(JSON.parse(data));
|
|
727
629
|
}
|
|
728
630
|
break;
|
|
729
631
|
case "done":
|
|
730
632
|
if (callbacks.onDone) {
|
|
731
|
-
|
|
732
|
-
callbacks.onDone(doneData);
|
|
633
|
+
callbacks.onDone(JSON.parse(data));
|
|
733
634
|
}
|
|
734
635
|
return;
|
|
735
636
|
case "error":
|
|
736
637
|
if (callbacks.onError) {
|
|
737
|
-
|
|
738
|
-
callbacks.onError(error);
|
|
638
|
+
callbacks.onError(JSON.parse(data));
|
|
739
639
|
}
|
|
740
640
|
throw new Error(data);
|
|
741
641
|
}
|
|
@@ -747,7 +647,7 @@ var AntflyClient = class {
|
|
|
747
647
|
}
|
|
748
648
|
} catch (error) {
|
|
749
649
|
if (error.name !== "AbortError") {
|
|
750
|
-
console.error("
|
|
650
|
+
console.error("Retrieval agent streaming error:", error);
|
|
751
651
|
}
|
|
752
652
|
}
|
|
753
653
|
})();
|
|
@@ -755,23 +655,83 @@ var AntflyClient = class {
|
|
|
755
655
|
return abortController;
|
|
756
656
|
}
|
|
757
657
|
/**
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
* @
|
|
658
|
+
* Retrieval Agent - Unified retrieval pipeline with optional classification, generation, and eval
|
|
659
|
+
* Supports pipeline mode (structured queries) and agentic mode (tool-calling with LLM)
|
|
660
|
+
* Configure steps.classification, steps.answer, steps.eval to enable additional pipeline stages
|
|
661
|
+
* @param request - Retrieval agent request with query, mode, and optional step configs
|
|
662
|
+
* @param callbacks - Optional callbacks for SSE events (classification, reasoning, hit, answer, citation, confidence, followup_question, eval, done, error)
|
|
663
|
+
* @returns Promise with RetrievalAgentResult (JSON) or AbortController (when streaming)
|
|
762
664
|
*/
|
|
763
|
-
async
|
|
764
|
-
return this.
|
|
665
|
+
async retrievalAgent(request, callbacks) {
|
|
666
|
+
return this.performRetrievalAgent(request, callbacks);
|
|
765
667
|
}
|
|
766
668
|
/**
|
|
767
|
-
*
|
|
768
|
-
*
|
|
769
|
-
* @param
|
|
770
|
-
* @param
|
|
771
|
-
* @
|
|
669
|
+
* Chat Agent - Multi-turn conversational retrieval with message history management.
|
|
670
|
+
* Wraps the retrieval agent with automatic message accumulation.
|
|
671
|
+
* @param userMessage - The user's message for this turn
|
|
672
|
+
* @param config - Chat configuration (generator, table, indexes, etc.)
|
|
673
|
+
* @param history - Previous conversation messages (pass result.messages from prior turns)
|
|
674
|
+
* @param callbacks - Optional streaming callbacks including chat-specific events
|
|
675
|
+
* @returns For streaming: { abortController, messages } where messages is a Promise.
|
|
676
|
+
* For non-streaming: { result, messages }
|
|
772
677
|
*/
|
|
773
|
-
async
|
|
774
|
-
|
|
678
|
+
async chatAgent(userMessage, config, history = [], callbacks) {
|
|
679
|
+
const request = {
|
|
680
|
+
query: userMessage,
|
|
681
|
+
queries: [
|
|
682
|
+
{
|
|
683
|
+
table: config.table,
|
|
684
|
+
semantic_search: userMessage,
|
|
685
|
+
indexes: config.semanticIndexes,
|
|
686
|
+
limit: config.limit ?? 10
|
|
687
|
+
}
|
|
688
|
+
],
|
|
689
|
+
generator: config.generator,
|
|
690
|
+
messages: [...history, { role: "user", content: userMessage }],
|
|
691
|
+
max_iterations: config.maxIterations ?? 5,
|
|
692
|
+
stream: !!callbacks,
|
|
693
|
+
agent_knowledge: config.agentKnowledge
|
|
694
|
+
};
|
|
695
|
+
if (config.steps) {
|
|
696
|
+
request.steps = config.steps;
|
|
697
|
+
}
|
|
698
|
+
if (callbacks) {
|
|
699
|
+
let answerText = "";
|
|
700
|
+
let resolveMessages;
|
|
701
|
+
const messagesPromise = new Promise((resolve) => {
|
|
702
|
+
resolveMessages = resolve;
|
|
703
|
+
});
|
|
704
|
+
const wrappedCallbacks = {
|
|
705
|
+
...callbacks,
|
|
706
|
+
onGeneration: (chunk) => {
|
|
707
|
+
answerText += chunk;
|
|
708
|
+
callbacks.onGeneration?.(chunk);
|
|
709
|
+
},
|
|
710
|
+
onDone: (data) => {
|
|
711
|
+
const updatedMessages2 = [
|
|
712
|
+
...history,
|
|
713
|
+
{ role: "user", content: userMessage },
|
|
714
|
+
{ role: "assistant", content: answerText }
|
|
715
|
+
];
|
|
716
|
+
callbacks.onAssistantMessage?.(answerText);
|
|
717
|
+
callbacks.onMessagesUpdated?.(updatedMessages2);
|
|
718
|
+
callbacks.onDone?.(data);
|
|
719
|
+
resolveMessages(updatedMessages2);
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
const abortController = await this.performRetrievalAgent(
|
|
723
|
+
request,
|
|
724
|
+
wrappedCallbacks
|
|
725
|
+
);
|
|
726
|
+
return { abortController, messages: messagesPromise };
|
|
727
|
+
}
|
|
728
|
+
const result = await this.performRetrievalAgent(request);
|
|
729
|
+
const updatedMessages = result.messages?.length ? result.messages : [
|
|
730
|
+
...history,
|
|
731
|
+
{ role: "user", content: userMessage },
|
|
732
|
+
...result.generation ? [{ role: "assistant", content: result.generation }] : []
|
|
733
|
+
];
|
|
734
|
+
return { result, messages: updatedMessages };
|
|
775
735
|
}
|
|
776
736
|
/**
|
|
777
737
|
* Query Builder Agent - Translates natural language into structured search queries
|
|
@@ -786,148 +746,6 @@ var AntflyClient = class {
|
|
|
786
746
|
if (error) throw new Error(`Query builder agent failed: ${error.error}`);
|
|
787
747
|
return data;
|
|
788
748
|
}
|
|
789
|
-
/**
|
|
790
|
-
* Private helper for Chat Agent requests to handle streaming and non-streaming responses
|
|
791
|
-
*/
|
|
792
|
-
async performChatAgent(path, request, callbacks) {
|
|
793
|
-
const headers = {
|
|
794
|
-
"Content-Type": "application/json",
|
|
795
|
-
Accept: "text/event-stream, application/json"
|
|
796
|
-
};
|
|
797
|
-
if (this.config.auth) {
|
|
798
|
-
const auth = btoa(`${this.config.auth.username}:${this.config.auth.password}`);
|
|
799
|
-
headers["Authorization"] = `Basic ${auth}`;
|
|
800
|
-
}
|
|
801
|
-
Object.assign(headers, this.config.headers);
|
|
802
|
-
const abortController = new AbortController();
|
|
803
|
-
const response = await fetch(`${this.config.baseUrl}${path}`, {
|
|
804
|
-
method: "POST",
|
|
805
|
-
headers,
|
|
806
|
-
body: JSON.stringify(request),
|
|
807
|
-
signal: abortController.signal
|
|
808
|
-
});
|
|
809
|
-
if (!response.ok) {
|
|
810
|
-
const errorText = await response.text();
|
|
811
|
-
throw new Error(`Chat agent request failed: ${response.status} ${errorText}`);
|
|
812
|
-
}
|
|
813
|
-
if (!response.body) {
|
|
814
|
-
throw new Error("Response body is null");
|
|
815
|
-
}
|
|
816
|
-
const contentType = response.headers.get("content-type") || "";
|
|
817
|
-
const isJSON = contentType.includes("application/json");
|
|
818
|
-
if (isJSON) {
|
|
819
|
-
const result = await response.json();
|
|
820
|
-
return result;
|
|
821
|
-
}
|
|
822
|
-
if (callbacks) {
|
|
823
|
-
const reader = response.body.getReader();
|
|
824
|
-
const decoder = new TextDecoder();
|
|
825
|
-
let buffer = "";
|
|
826
|
-
let currentEvent = "";
|
|
827
|
-
(async () => {
|
|
828
|
-
try {
|
|
829
|
-
while (true) {
|
|
830
|
-
const { done, value } = await reader.read();
|
|
831
|
-
if (done) break;
|
|
832
|
-
buffer += decoder.decode(value, { stream: true });
|
|
833
|
-
const lines = buffer.split("\n");
|
|
834
|
-
buffer = lines.pop() || "";
|
|
835
|
-
for (const line of lines) {
|
|
836
|
-
if (!line.trim()) {
|
|
837
|
-
currentEvent = "";
|
|
838
|
-
continue;
|
|
839
|
-
}
|
|
840
|
-
if (line.startsWith("event: ")) {
|
|
841
|
-
currentEvent = line.slice(7).trim();
|
|
842
|
-
} else if (line.startsWith("data: ")) {
|
|
843
|
-
const data = line.slice(6).trim();
|
|
844
|
-
try {
|
|
845
|
-
switch (currentEvent) {
|
|
846
|
-
case "classification":
|
|
847
|
-
if (callbacks.onClassification) {
|
|
848
|
-
const classData = JSON.parse(data);
|
|
849
|
-
callbacks.onClassification(classData);
|
|
850
|
-
}
|
|
851
|
-
break;
|
|
852
|
-
case "clarification_required":
|
|
853
|
-
if (callbacks.onClarificationRequired) {
|
|
854
|
-
const clarification = JSON.parse(data);
|
|
855
|
-
callbacks.onClarificationRequired(clarification);
|
|
856
|
-
}
|
|
857
|
-
break;
|
|
858
|
-
case "filter_applied":
|
|
859
|
-
if (callbacks.onFilterApplied) {
|
|
860
|
-
const filter = JSON.parse(data);
|
|
861
|
-
callbacks.onFilterApplied(filter);
|
|
862
|
-
}
|
|
863
|
-
break;
|
|
864
|
-
case "search_executed":
|
|
865
|
-
if (callbacks.onSearchExecuted) {
|
|
866
|
-
const searchData = JSON.parse(data);
|
|
867
|
-
callbacks.onSearchExecuted(searchData);
|
|
868
|
-
}
|
|
869
|
-
break;
|
|
870
|
-
case "websearch_executed":
|
|
871
|
-
if (callbacks.onWebSearchExecuted) {
|
|
872
|
-
const webSearchData = JSON.parse(data);
|
|
873
|
-
callbacks.onWebSearchExecuted(webSearchData);
|
|
874
|
-
}
|
|
875
|
-
break;
|
|
876
|
-
case "fetch_executed":
|
|
877
|
-
if (callbacks.onFetchExecuted) {
|
|
878
|
-
const fetchData = JSON.parse(data);
|
|
879
|
-
callbacks.onFetchExecuted(fetchData);
|
|
880
|
-
}
|
|
881
|
-
break;
|
|
882
|
-
case "hit":
|
|
883
|
-
if (callbacks.onHit) {
|
|
884
|
-
const hit = JSON.parse(data);
|
|
885
|
-
callbacks.onHit(hit);
|
|
886
|
-
}
|
|
887
|
-
break;
|
|
888
|
-
case "answer":
|
|
889
|
-
if (callbacks.onAnswer) {
|
|
890
|
-
callbacks.onAnswer(JSON.parse(data));
|
|
891
|
-
}
|
|
892
|
-
break;
|
|
893
|
-
case "done":
|
|
894
|
-
if (callbacks.onDone) {
|
|
895
|
-
const doneData = JSON.parse(data);
|
|
896
|
-
callbacks.onDone(doneData);
|
|
897
|
-
}
|
|
898
|
-
return;
|
|
899
|
-
case "error":
|
|
900
|
-
if (callbacks.onError) {
|
|
901
|
-
const error = JSON.parse(data);
|
|
902
|
-
callbacks.onError(error);
|
|
903
|
-
}
|
|
904
|
-
throw new Error(data);
|
|
905
|
-
}
|
|
906
|
-
} catch (e) {
|
|
907
|
-
console.warn("Failed to parse SSE data:", currentEvent, data, e);
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
} catch (error) {
|
|
913
|
-
if (error.name !== "AbortError") {
|
|
914
|
-
console.error("Chat agent streaming error:", error);
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
})();
|
|
918
|
-
}
|
|
919
|
-
return abortController;
|
|
920
|
-
}
|
|
921
|
-
/**
|
|
922
|
-
* Chat Agent - Conversational RAG with multi-turn history and tool calling
|
|
923
|
-
* Supports filter refinement, clarification requests, and streaming responses
|
|
924
|
-
* @param request - Chat agent request with conversation history and generator config
|
|
925
|
-
* @param callbacks - Optional callbacks for SSE events (classification, clarification_required, filter_applied, hit, answer, done, error)
|
|
926
|
-
* @returns Promise with ChatAgentResult (JSON) or AbortController (when streaming)
|
|
927
|
-
*/
|
|
928
|
-
async chatAgent(request, callbacks) {
|
|
929
|
-
return this.performChatAgent("/agents/chat", request, callbacks);
|
|
930
|
-
}
|
|
931
749
|
/**
|
|
932
750
|
* Standalone evaluation for testing evaluators without running a query.
|
|
933
751
|
* Evaluates a generated output against ground truth using LLM-as-judge metrics.
|
|
@@ -1107,8 +925,11 @@ var generatorProviders = [
|
|
|
1107
925
|
"ollama",
|
|
1108
926
|
"gemini",
|
|
1109
927
|
"openai",
|
|
1110
|
-
"
|
|
1111
|
-
"
|
|
928
|
+
"anthropic",
|
|
929
|
+
"vertex",
|
|
930
|
+
"cohere",
|
|
931
|
+
"termite",
|
|
932
|
+
"openrouter"
|
|
1112
933
|
];
|
|
1113
934
|
|
|
1114
935
|
// src/index.ts
|