@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.js
CHANGED
|
@@ -130,14 +130,14 @@ var AntflyClient = class {
|
|
|
130
130
|
*/
|
|
131
131
|
scan: (tableName, request) => {
|
|
132
132
|
const config = this.config;
|
|
133
|
+
const authHeader = this.getAuthHeader();
|
|
133
134
|
async function* scanGenerator() {
|
|
134
135
|
const headers = {
|
|
135
136
|
"Content-Type": "application/json",
|
|
136
137
|
Accept: "application/x-ndjson"
|
|
137
138
|
};
|
|
138
|
-
if (
|
|
139
|
-
|
|
140
|
-
headers["Authorization"] = `Basic ${auth}`;
|
|
139
|
+
if (authHeader) {
|
|
140
|
+
headers.Authorization = authHeader;
|
|
141
141
|
}
|
|
142
142
|
Object.assign(headers, config.headers);
|
|
143
143
|
const response = await fetch(`${config.baseUrl}/tables/${tableName}/lookup`, {
|
|
@@ -186,16 +186,6 @@ var AntflyClient = class {
|
|
|
186
186
|
results.push(doc);
|
|
187
187
|
}
|
|
188
188
|
return results;
|
|
189
|
-
},
|
|
190
|
-
/**
|
|
191
|
-
* RAG (Retrieval-Augmented Generation) query on a specific table with streaming or citations
|
|
192
|
-
* @param tableName - Name of the table to query
|
|
193
|
-
* @param request - RAG request with query and summarizer config (set with_streaming: true to enable streaming)
|
|
194
|
-
* @param callbacks - Optional callbacks for structured SSE events (hit, summary, citation, done, error)
|
|
195
|
-
* @returns Promise with RAG result (JSON) or AbortController (when streaming)
|
|
196
|
-
*/
|
|
197
|
-
rag: async (tableName, request, callbacks) => {
|
|
198
|
-
return this.performRag(`/tables/${tableName}/rag`, request, callbacks);
|
|
199
189
|
}
|
|
200
190
|
};
|
|
201
191
|
/**
|
|
@@ -342,16 +332,41 @@ var AntflyClient = class {
|
|
|
342
332
|
}
|
|
343
333
|
};
|
|
344
334
|
this.config = config;
|
|
335
|
+
this.client = this.buildClient();
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Build the Authorization header value from the auth config.
|
|
339
|
+
* Returns undefined if no auth is configured.
|
|
340
|
+
*/
|
|
341
|
+
getAuthHeader() {
|
|
342
|
+
const auth = this.config.auth;
|
|
343
|
+
if (!auth) return void 0;
|
|
344
|
+
if ("type" in auth) {
|
|
345
|
+
switch (auth.type) {
|
|
346
|
+
case "basic":
|
|
347
|
+
return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
|
|
348
|
+
case "apiKey":
|
|
349
|
+
return `ApiKey ${btoa(`${auth.keyId}:${auth.keySecret}`)}`;
|
|
350
|
+
case "bearer":
|
|
351
|
+
return `Bearer ${auth.token}`;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Build the openapi-fetch client with current config.
|
|
358
|
+
*/
|
|
359
|
+
buildClient() {
|
|
345
360
|
const headers = {
|
|
346
361
|
"Content-Type": "application/json",
|
|
347
|
-
...config.headers
|
|
362
|
+
...this.config.headers
|
|
348
363
|
};
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
headers
|
|
364
|
+
const authHeader = this.getAuthHeader();
|
|
365
|
+
if (authHeader) {
|
|
366
|
+
headers.Authorization = authHeader;
|
|
352
367
|
}
|
|
353
|
-
|
|
354
|
-
baseUrl: config.baseUrl,
|
|
368
|
+
return createClient({
|
|
369
|
+
baseUrl: this.config.baseUrl,
|
|
355
370
|
headers,
|
|
356
371
|
bodySerializer: (body) => {
|
|
357
372
|
if (typeof body === "string") {
|
|
@@ -361,25 +376,13 @@ var AntflyClient = class {
|
|
|
361
376
|
}
|
|
362
377
|
});
|
|
363
378
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
this.client =
|
|
371
|
-
baseUrl: this.config.baseUrl,
|
|
372
|
-
headers: {
|
|
373
|
-
...this.config.headers,
|
|
374
|
-
Authorization: `Basic ${auth}`
|
|
375
|
-
},
|
|
376
|
-
bodySerializer: (body) => {
|
|
377
|
-
if (typeof body === "string") {
|
|
378
|
-
return body;
|
|
379
|
-
}
|
|
380
|
-
return JSON.stringify(body);
|
|
381
|
-
}
|
|
382
|
-
});
|
|
379
|
+
setAuth(authOrUsername, password) {
|
|
380
|
+
if (typeof authOrUsername === "string" && password !== void 0) {
|
|
381
|
+
this.config.auth = { username: authOrUsername, password };
|
|
382
|
+
} else {
|
|
383
|
+
this.config.auth = authOrUsername;
|
|
384
|
+
}
|
|
385
|
+
this.client = this.buildClient();
|
|
383
386
|
}
|
|
384
387
|
/**
|
|
385
388
|
* Get cluster status
|
|
@@ -412,7 +415,8 @@ var AntflyClient = class {
|
|
|
412
415
|
* Private helper for multiquery requests to avoid code duplication
|
|
413
416
|
*/
|
|
414
417
|
async performMultiquery(path, requests, tableName) {
|
|
415
|
-
const ndjson = requests.map((request) => JSON.stringify(request)).join("\n")
|
|
418
|
+
const ndjson = `${requests.map((request) => JSON.stringify(request)).join("\n")}
|
|
419
|
+
`;
|
|
416
420
|
if (path === "/tables/{tableName}/query" && tableName) {
|
|
417
421
|
const { data, error } = await this.client.POST("/tables/{tableName}/query", {
|
|
418
422
|
params: { path: { tableName } },
|
|
@@ -448,136 +452,20 @@ var AntflyClient = class {
|
|
|
448
452
|
return this.performMultiquery("/query", requests);
|
|
449
453
|
}
|
|
450
454
|
/**
|
|
451
|
-
* Private helper for
|
|
452
|
-
*/
|
|
453
|
-
async performRag(path, request, callbacks) {
|
|
454
|
-
const headers = {
|
|
455
|
-
"Content-Type": "application/json",
|
|
456
|
-
Accept: "text/event-stream, application/json"
|
|
457
|
-
};
|
|
458
|
-
if (this.config.auth) {
|
|
459
|
-
const auth = btoa(`${this.config.auth.username}:${this.config.auth.password}`);
|
|
460
|
-
headers["Authorization"] = `Basic ${auth}`;
|
|
461
|
-
}
|
|
462
|
-
Object.assign(headers, this.config.headers);
|
|
463
|
-
const abortController = new AbortController();
|
|
464
|
-
const response = await fetch(`${this.config.baseUrl}${path}`, {
|
|
465
|
-
method: "POST",
|
|
466
|
-
headers,
|
|
467
|
-
body: JSON.stringify(request),
|
|
468
|
-
signal: abortController.signal
|
|
469
|
-
});
|
|
470
|
-
if (!response.ok) {
|
|
471
|
-
const errorText = await response.text();
|
|
472
|
-
throw new Error(`RAG request failed: ${response.status} ${errorText}`);
|
|
473
|
-
}
|
|
474
|
-
if (!response.body) {
|
|
475
|
-
throw new Error("Response body is null");
|
|
476
|
-
}
|
|
477
|
-
const contentType = response.headers.get("content-type") || "";
|
|
478
|
-
const isJSON = contentType.includes("application/json");
|
|
479
|
-
if (isJSON) {
|
|
480
|
-
const ragResult = await response.json();
|
|
481
|
-
return ragResult;
|
|
482
|
-
}
|
|
483
|
-
if (callbacks) {
|
|
484
|
-
const reader = response.body.getReader();
|
|
485
|
-
const decoder = new TextDecoder();
|
|
486
|
-
let buffer = "";
|
|
487
|
-
let currentEvent = "";
|
|
488
|
-
(async () => {
|
|
489
|
-
try {
|
|
490
|
-
while (true) {
|
|
491
|
-
const { done, value } = await reader.read();
|
|
492
|
-
if (done) break;
|
|
493
|
-
buffer += decoder.decode(value, { stream: true });
|
|
494
|
-
const lines = buffer.split("\n");
|
|
495
|
-
buffer = lines.pop() || "";
|
|
496
|
-
for (const line of lines) {
|
|
497
|
-
if (!line.trim()) {
|
|
498
|
-
currentEvent = "";
|
|
499
|
-
continue;
|
|
500
|
-
}
|
|
501
|
-
if (line.startsWith("event: ")) {
|
|
502
|
-
currentEvent = line.slice(7).trim();
|
|
503
|
-
} else if (line.startsWith("data: ")) {
|
|
504
|
-
const data = line.slice(6).trim();
|
|
505
|
-
try {
|
|
506
|
-
switch (currentEvent) {
|
|
507
|
-
case "hits_start":
|
|
508
|
-
if (callbacks.onHitsStart) {
|
|
509
|
-
const hitsStartData = JSON.parse(data);
|
|
510
|
-
callbacks.onHitsStart(hitsStartData);
|
|
511
|
-
}
|
|
512
|
-
break;
|
|
513
|
-
case "hit":
|
|
514
|
-
if (callbacks.onHit) {
|
|
515
|
-
const hit = JSON.parse(data);
|
|
516
|
-
callbacks.onHit(hit);
|
|
517
|
-
}
|
|
518
|
-
break;
|
|
519
|
-
case "hits_end":
|
|
520
|
-
if (callbacks.onHitsEnd) {
|
|
521
|
-
const hitsEndData = JSON.parse(data);
|
|
522
|
-
callbacks.onHitsEnd(hitsEndData);
|
|
523
|
-
}
|
|
524
|
-
break;
|
|
525
|
-
case "table_result":
|
|
526
|
-
if (callbacks.onHit) {
|
|
527
|
-
const result = JSON.parse(data);
|
|
528
|
-
const hits = result?.hits?.hits || [];
|
|
529
|
-
hits.forEach((hit) => callbacks.onHit(hit));
|
|
530
|
-
}
|
|
531
|
-
break;
|
|
532
|
-
case "summary":
|
|
533
|
-
if (callbacks.onSummary) {
|
|
534
|
-
const chunk = JSON.parse(data);
|
|
535
|
-
callbacks.onSummary(chunk);
|
|
536
|
-
}
|
|
537
|
-
break;
|
|
538
|
-
case "done":
|
|
539
|
-
if (callbacks.onDone) {
|
|
540
|
-
const doneData = JSON.parse(data);
|
|
541
|
-
callbacks.onDone(doneData);
|
|
542
|
-
}
|
|
543
|
-
return;
|
|
544
|
-
case "error":
|
|
545
|
-
if (callbacks.onError) {
|
|
546
|
-
const error = JSON.parse(data);
|
|
547
|
-
callbacks.onError(error);
|
|
548
|
-
}
|
|
549
|
-
throw new Error(data);
|
|
550
|
-
}
|
|
551
|
-
} catch (e) {
|
|
552
|
-
console.warn("Failed to parse SSE data:", currentEvent, data, e);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
} catch (error) {
|
|
558
|
-
if (error.name !== "AbortError") {
|
|
559
|
-
console.error("RAG streaming error:", error);
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
})();
|
|
563
|
-
}
|
|
564
|
-
return abortController;
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Private helper for Answer Agent requests to avoid code duplication
|
|
455
|
+
* Private helper for Retrieval Agent requests to handle streaming and non-streaming responses
|
|
568
456
|
*/
|
|
569
|
-
async
|
|
457
|
+
async performRetrievalAgent(request, callbacks) {
|
|
570
458
|
const headers = {
|
|
571
459
|
"Content-Type": "application/json",
|
|
572
460
|
Accept: "text/event-stream, application/json"
|
|
573
461
|
};
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
headers
|
|
462
|
+
const authHeader = this.getAuthHeader();
|
|
463
|
+
if (authHeader) {
|
|
464
|
+
headers.Authorization = authHeader;
|
|
577
465
|
}
|
|
578
466
|
Object.assign(headers, this.config.headers);
|
|
579
467
|
const abortController = new AbortController();
|
|
580
|
-
const response = await fetch(`${this.config.baseUrl}
|
|
468
|
+
const response = await fetch(`${this.config.baseUrl}/agents/retrieval`, {
|
|
581
469
|
method: "POST",
|
|
582
470
|
headers,
|
|
583
471
|
body: JSON.stringify(request),
|
|
@@ -585,7 +473,7 @@ var AntflyClient = class {
|
|
|
585
473
|
});
|
|
586
474
|
if (!response.ok) {
|
|
587
475
|
const errorText = await response.text();
|
|
588
|
-
throw new Error(`
|
|
476
|
+
throw new Error(`Retrieval agent request failed: ${response.status} ${errorText}`);
|
|
589
477
|
}
|
|
590
478
|
if (!response.body) {
|
|
591
479
|
throw new Error("Response body is null");
|
|
@@ -622,8 +510,7 @@ var AntflyClient = class {
|
|
|
622
510
|
switch (currentEvent) {
|
|
623
511
|
case "classification":
|
|
624
512
|
if (callbacks.onClassification) {
|
|
625
|
-
|
|
626
|
-
callbacks.onClassification(classData);
|
|
513
|
+
callbacks.onClassification(JSON.parse(data));
|
|
627
514
|
}
|
|
628
515
|
break;
|
|
629
516
|
case "reasoning":
|
|
@@ -631,56 +518,69 @@ var AntflyClient = class {
|
|
|
631
518
|
callbacks.onReasoning(JSON.parse(data));
|
|
632
519
|
}
|
|
633
520
|
break;
|
|
634
|
-
case "
|
|
635
|
-
if (callbacks.
|
|
636
|
-
|
|
637
|
-
|
|
521
|
+
case "clarification_required":
|
|
522
|
+
if (callbacks.onClarificationRequired) {
|
|
523
|
+
callbacks.onClarificationRequired(JSON.parse(data));
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
case "filter_applied":
|
|
527
|
+
if (callbacks.onFilterApplied) {
|
|
528
|
+
callbacks.onFilterApplied(JSON.parse(data));
|
|
529
|
+
}
|
|
530
|
+
break;
|
|
531
|
+
case "search_executed":
|
|
532
|
+
if (callbacks.onSearchExecuted) {
|
|
533
|
+
callbacks.onSearchExecuted(JSON.parse(data));
|
|
638
534
|
}
|
|
639
535
|
break;
|
|
640
536
|
case "hit":
|
|
641
537
|
if (callbacks.onHit) {
|
|
642
|
-
|
|
643
|
-
|
|
538
|
+
callbacks.onHit(JSON.parse(data));
|
|
539
|
+
}
|
|
540
|
+
break;
|
|
541
|
+
case "generation":
|
|
542
|
+
if (callbacks.onGeneration) {
|
|
543
|
+
callbacks.onGeneration(JSON.parse(data));
|
|
544
|
+
}
|
|
545
|
+
break;
|
|
546
|
+
case "step_started":
|
|
547
|
+
if (callbacks.onStepStarted) {
|
|
548
|
+
callbacks.onStepStarted(JSON.parse(data));
|
|
644
549
|
}
|
|
645
550
|
break;
|
|
646
|
-
case "
|
|
647
|
-
if (callbacks.
|
|
648
|
-
|
|
649
|
-
callbacks.onHitsEnd(hitsEndData);
|
|
551
|
+
case "step_progress":
|
|
552
|
+
if (callbacks.onStepProgress) {
|
|
553
|
+
callbacks.onStepProgress(JSON.parse(data));
|
|
650
554
|
}
|
|
651
555
|
break;
|
|
652
|
-
case "
|
|
653
|
-
if (callbacks.
|
|
654
|
-
callbacks.
|
|
556
|
+
case "step_completed":
|
|
557
|
+
if (callbacks.onStepCompleted) {
|
|
558
|
+
callbacks.onStepCompleted(JSON.parse(data));
|
|
655
559
|
}
|
|
656
560
|
break;
|
|
657
561
|
case "confidence":
|
|
658
562
|
if (callbacks.onConfidence) {
|
|
659
|
-
|
|
660
|
-
callbacks.onConfidence(confidence);
|
|
563
|
+
callbacks.onConfidence(JSON.parse(data));
|
|
661
564
|
}
|
|
662
565
|
break;
|
|
663
|
-
case "
|
|
664
|
-
if (callbacks.
|
|
665
|
-
callbacks.
|
|
566
|
+
case "followup":
|
|
567
|
+
if (callbacks.onFollowup) {
|
|
568
|
+
callbacks.onFollowup(JSON.parse(data));
|
|
666
569
|
}
|
|
667
570
|
break;
|
|
668
571
|
case "eval":
|
|
669
572
|
if (callbacks.onEvalResult) {
|
|
670
|
-
|
|
671
|
-
callbacks.onEvalResult(evalResult);
|
|
573
|
+
callbacks.onEvalResult(JSON.parse(data));
|
|
672
574
|
}
|
|
673
575
|
break;
|
|
674
576
|
case "done":
|
|
675
577
|
if (callbacks.onDone) {
|
|
676
|
-
|
|
677
|
-
callbacks.onDone(doneData);
|
|
578
|
+
callbacks.onDone(JSON.parse(data));
|
|
678
579
|
}
|
|
679
580
|
return;
|
|
680
581
|
case "error":
|
|
681
582
|
if (callbacks.onError) {
|
|
682
|
-
|
|
683
|
-
callbacks.onError(error);
|
|
583
|
+
callbacks.onError(JSON.parse(data));
|
|
684
584
|
}
|
|
685
585
|
throw new Error(data);
|
|
686
586
|
}
|
|
@@ -692,7 +592,7 @@ var AntflyClient = class {
|
|
|
692
592
|
}
|
|
693
593
|
} catch (error) {
|
|
694
594
|
if (error.name !== "AbortError") {
|
|
695
|
-
console.error("
|
|
595
|
+
console.error("Retrieval agent streaming error:", error);
|
|
696
596
|
}
|
|
697
597
|
}
|
|
698
598
|
})();
|
|
@@ -700,23 +600,83 @@ var AntflyClient = class {
|
|
|
700
600
|
return abortController;
|
|
701
601
|
}
|
|
702
602
|
/**
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
*
|
|
706
|
-
* @
|
|
603
|
+
* Retrieval Agent - Unified retrieval pipeline with optional classification, generation, and eval
|
|
604
|
+
* Supports pipeline mode (structured queries) and agentic mode (tool-calling with LLM)
|
|
605
|
+
* Configure steps.classification, steps.answer, steps.eval to enable additional pipeline stages
|
|
606
|
+
* @param request - Retrieval agent request with query, mode, and optional step configs
|
|
607
|
+
* @param callbacks - Optional callbacks for SSE events (classification, reasoning, hit, answer, citation, confidence, followup_question, eval, done, error)
|
|
608
|
+
* @returns Promise with RetrievalAgentResult (JSON) or AbortController (when streaming)
|
|
707
609
|
*/
|
|
708
|
-
async
|
|
709
|
-
return this.
|
|
610
|
+
async retrievalAgent(request, callbacks) {
|
|
611
|
+
return this.performRetrievalAgent(request, callbacks);
|
|
710
612
|
}
|
|
711
613
|
/**
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
* @param
|
|
715
|
-
* @param
|
|
716
|
-
* @
|
|
614
|
+
* Chat Agent - Multi-turn conversational retrieval with message history management.
|
|
615
|
+
* Wraps the retrieval agent with automatic message accumulation.
|
|
616
|
+
* @param userMessage - The user's message for this turn
|
|
617
|
+
* @param config - Chat configuration (generator, table, indexes, etc.)
|
|
618
|
+
* @param history - Previous conversation messages (pass result.messages from prior turns)
|
|
619
|
+
* @param callbacks - Optional streaming callbacks including chat-specific events
|
|
620
|
+
* @returns For streaming: { abortController, messages } where messages is a Promise.
|
|
621
|
+
* For non-streaming: { result, messages }
|
|
717
622
|
*/
|
|
718
|
-
async
|
|
719
|
-
|
|
623
|
+
async chatAgent(userMessage, config, history = [], callbacks) {
|
|
624
|
+
const request = {
|
|
625
|
+
query: userMessage,
|
|
626
|
+
queries: [
|
|
627
|
+
{
|
|
628
|
+
table: config.table,
|
|
629
|
+
semantic_search: userMessage,
|
|
630
|
+
indexes: config.semanticIndexes,
|
|
631
|
+
limit: config.limit ?? 10
|
|
632
|
+
}
|
|
633
|
+
],
|
|
634
|
+
generator: config.generator,
|
|
635
|
+
messages: [...history, { role: "user", content: userMessage }],
|
|
636
|
+
max_iterations: config.maxIterations ?? 5,
|
|
637
|
+
stream: !!callbacks,
|
|
638
|
+
agent_knowledge: config.agentKnowledge
|
|
639
|
+
};
|
|
640
|
+
if (config.steps) {
|
|
641
|
+
request.steps = config.steps;
|
|
642
|
+
}
|
|
643
|
+
if (callbacks) {
|
|
644
|
+
let answerText = "";
|
|
645
|
+
let resolveMessages;
|
|
646
|
+
const messagesPromise = new Promise((resolve) => {
|
|
647
|
+
resolveMessages = resolve;
|
|
648
|
+
});
|
|
649
|
+
const wrappedCallbacks = {
|
|
650
|
+
...callbacks,
|
|
651
|
+
onGeneration: (chunk) => {
|
|
652
|
+
answerText += chunk;
|
|
653
|
+
callbacks.onGeneration?.(chunk);
|
|
654
|
+
},
|
|
655
|
+
onDone: (data) => {
|
|
656
|
+
const updatedMessages2 = [
|
|
657
|
+
...history,
|
|
658
|
+
{ role: "user", content: userMessage },
|
|
659
|
+
{ role: "assistant", content: answerText }
|
|
660
|
+
];
|
|
661
|
+
callbacks.onAssistantMessage?.(answerText);
|
|
662
|
+
callbacks.onMessagesUpdated?.(updatedMessages2);
|
|
663
|
+
callbacks.onDone?.(data);
|
|
664
|
+
resolveMessages(updatedMessages2);
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
const abortController = await this.performRetrievalAgent(
|
|
668
|
+
request,
|
|
669
|
+
wrappedCallbacks
|
|
670
|
+
);
|
|
671
|
+
return { abortController, messages: messagesPromise };
|
|
672
|
+
}
|
|
673
|
+
const result = await this.performRetrievalAgent(request);
|
|
674
|
+
const updatedMessages = result.messages?.length ? result.messages : [
|
|
675
|
+
...history,
|
|
676
|
+
{ role: "user", content: userMessage },
|
|
677
|
+
...result.generation ? [{ role: "assistant", content: result.generation }] : []
|
|
678
|
+
];
|
|
679
|
+
return { result, messages: updatedMessages };
|
|
720
680
|
}
|
|
721
681
|
/**
|
|
722
682
|
* Query Builder Agent - Translates natural language into structured search queries
|
|
@@ -731,148 +691,6 @@ var AntflyClient = class {
|
|
|
731
691
|
if (error) throw new Error(`Query builder agent failed: ${error.error}`);
|
|
732
692
|
return data;
|
|
733
693
|
}
|
|
734
|
-
/**
|
|
735
|
-
* Private helper for Chat Agent requests to handle streaming and non-streaming responses
|
|
736
|
-
*/
|
|
737
|
-
async performChatAgent(path, request, callbacks) {
|
|
738
|
-
const headers = {
|
|
739
|
-
"Content-Type": "application/json",
|
|
740
|
-
Accept: "text/event-stream, application/json"
|
|
741
|
-
};
|
|
742
|
-
if (this.config.auth) {
|
|
743
|
-
const auth = btoa(`${this.config.auth.username}:${this.config.auth.password}`);
|
|
744
|
-
headers["Authorization"] = `Basic ${auth}`;
|
|
745
|
-
}
|
|
746
|
-
Object.assign(headers, this.config.headers);
|
|
747
|
-
const abortController = new AbortController();
|
|
748
|
-
const response = await fetch(`${this.config.baseUrl}${path}`, {
|
|
749
|
-
method: "POST",
|
|
750
|
-
headers,
|
|
751
|
-
body: JSON.stringify(request),
|
|
752
|
-
signal: abortController.signal
|
|
753
|
-
});
|
|
754
|
-
if (!response.ok) {
|
|
755
|
-
const errorText = await response.text();
|
|
756
|
-
throw new Error(`Chat agent request failed: ${response.status} ${errorText}`);
|
|
757
|
-
}
|
|
758
|
-
if (!response.body) {
|
|
759
|
-
throw new Error("Response body is null");
|
|
760
|
-
}
|
|
761
|
-
const contentType = response.headers.get("content-type") || "";
|
|
762
|
-
const isJSON = contentType.includes("application/json");
|
|
763
|
-
if (isJSON) {
|
|
764
|
-
const result = await response.json();
|
|
765
|
-
return result;
|
|
766
|
-
}
|
|
767
|
-
if (callbacks) {
|
|
768
|
-
const reader = response.body.getReader();
|
|
769
|
-
const decoder = new TextDecoder();
|
|
770
|
-
let buffer = "";
|
|
771
|
-
let currentEvent = "";
|
|
772
|
-
(async () => {
|
|
773
|
-
try {
|
|
774
|
-
while (true) {
|
|
775
|
-
const { done, value } = await reader.read();
|
|
776
|
-
if (done) break;
|
|
777
|
-
buffer += decoder.decode(value, { stream: true });
|
|
778
|
-
const lines = buffer.split("\n");
|
|
779
|
-
buffer = lines.pop() || "";
|
|
780
|
-
for (const line of lines) {
|
|
781
|
-
if (!line.trim()) {
|
|
782
|
-
currentEvent = "";
|
|
783
|
-
continue;
|
|
784
|
-
}
|
|
785
|
-
if (line.startsWith("event: ")) {
|
|
786
|
-
currentEvent = line.slice(7).trim();
|
|
787
|
-
} else if (line.startsWith("data: ")) {
|
|
788
|
-
const data = line.slice(6).trim();
|
|
789
|
-
try {
|
|
790
|
-
switch (currentEvent) {
|
|
791
|
-
case "classification":
|
|
792
|
-
if (callbacks.onClassification) {
|
|
793
|
-
const classData = JSON.parse(data);
|
|
794
|
-
callbacks.onClassification(classData);
|
|
795
|
-
}
|
|
796
|
-
break;
|
|
797
|
-
case "clarification_required":
|
|
798
|
-
if (callbacks.onClarificationRequired) {
|
|
799
|
-
const clarification = JSON.parse(data);
|
|
800
|
-
callbacks.onClarificationRequired(clarification);
|
|
801
|
-
}
|
|
802
|
-
break;
|
|
803
|
-
case "filter_applied":
|
|
804
|
-
if (callbacks.onFilterApplied) {
|
|
805
|
-
const filter = JSON.parse(data);
|
|
806
|
-
callbacks.onFilterApplied(filter);
|
|
807
|
-
}
|
|
808
|
-
break;
|
|
809
|
-
case "search_executed":
|
|
810
|
-
if (callbacks.onSearchExecuted) {
|
|
811
|
-
const searchData = JSON.parse(data);
|
|
812
|
-
callbacks.onSearchExecuted(searchData);
|
|
813
|
-
}
|
|
814
|
-
break;
|
|
815
|
-
case "websearch_executed":
|
|
816
|
-
if (callbacks.onWebSearchExecuted) {
|
|
817
|
-
const webSearchData = JSON.parse(data);
|
|
818
|
-
callbacks.onWebSearchExecuted(webSearchData);
|
|
819
|
-
}
|
|
820
|
-
break;
|
|
821
|
-
case "fetch_executed":
|
|
822
|
-
if (callbacks.onFetchExecuted) {
|
|
823
|
-
const fetchData = JSON.parse(data);
|
|
824
|
-
callbacks.onFetchExecuted(fetchData);
|
|
825
|
-
}
|
|
826
|
-
break;
|
|
827
|
-
case "hit":
|
|
828
|
-
if (callbacks.onHit) {
|
|
829
|
-
const hit = JSON.parse(data);
|
|
830
|
-
callbacks.onHit(hit);
|
|
831
|
-
}
|
|
832
|
-
break;
|
|
833
|
-
case "answer":
|
|
834
|
-
if (callbacks.onAnswer) {
|
|
835
|
-
callbacks.onAnswer(JSON.parse(data));
|
|
836
|
-
}
|
|
837
|
-
break;
|
|
838
|
-
case "done":
|
|
839
|
-
if (callbacks.onDone) {
|
|
840
|
-
const doneData = JSON.parse(data);
|
|
841
|
-
callbacks.onDone(doneData);
|
|
842
|
-
}
|
|
843
|
-
return;
|
|
844
|
-
case "error":
|
|
845
|
-
if (callbacks.onError) {
|
|
846
|
-
const error = JSON.parse(data);
|
|
847
|
-
callbacks.onError(error);
|
|
848
|
-
}
|
|
849
|
-
throw new Error(data);
|
|
850
|
-
}
|
|
851
|
-
} catch (e) {
|
|
852
|
-
console.warn("Failed to parse SSE data:", currentEvent, data, e);
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
} catch (error) {
|
|
858
|
-
if (error.name !== "AbortError") {
|
|
859
|
-
console.error("Chat agent streaming error:", error);
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
})();
|
|
863
|
-
}
|
|
864
|
-
return abortController;
|
|
865
|
-
}
|
|
866
|
-
/**
|
|
867
|
-
* Chat Agent - Conversational RAG with multi-turn history and tool calling
|
|
868
|
-
* Supports filter refinement, clarification requests, and streaming responses
|
|
869
|
-
* @param request - Chat agent request with conversation history and generator config
|
|
870
|
-
* @param callbacks - Optional callbacks for SSE events (classification, clarification_required, filter_applied, hit, answer, done, error)
|
|
871
|
-
* @returns Promise with ChatAgentResult (JSON) or AbortController (when streaming)
|
|
872
|
-
*/
|
|
873
|
-
async chatAgent(request, callbacks) {
|
|
874
|
-
return this.performChatAgent("/agents/chat", request, callbacks);
|
|
875
|
-
}
|
|
876
694
|
/**
|
|
877
695
|
* Standalone evaluation for testing evaluators without running a query.
|
|
878
696
|
* Evaluates a generated output against ground truth using LLM-as-judge metrics.
|
|
@@ -1052,8 +870,11 @@ var generatorProviders = [
|
|
|
1052
870
|
"ollama",
|
|
1053
871
|
"gemini",
|
|
1054
872
|
"openai",
|
|
1055
|
-
"
|
|
1056
|
-
"
|
|
873
|
+
"anthropic",
|
|
874
|
+
"vertex",
|
|
875
|
+
"cohere",
|
|
876
|
+
"termite",
|
|
877
|
+
"openrouter"
|
|
1057
878
|
];
|
|
1058
879
|
|
|
1059
880
|
// src/index.ts
|