@antfly/sdk 0.0.11 → 0.0.13
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/README.md +2 -1
- package/dist/index.cjs +169 -341
- package/dist/index.d.cts +28630 -1052
- package/dist/index.d.ts +28630 -1052
- package/dist/index.js +169 -341
- package/package.json +6 -6
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["Authorization"] =
|
|
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
|
|
@@ -448,136 +451,20 @@ var AntflyClient = class {
|
|
|
448
451
|
return this.performMultiquery("/query", requests);
|
|
449
452
|
}
|
|
450
453
|
/**
|
|
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
|
|
454
|
+
* Private helper for Retrieval Agent requests to handle streaming and non-streaming responses
|
|
568
455
|
*/
|
|
569
|
-
async
|
|
456
|
+
async performRetrievalAgent(request, callbacks) {
|
|
570
457
|
const headers = {
|
|
571
458
|
"Content-Type": "application/json",
|
|
572
459
|
Accept: "text/event-stream, application/json"
|
|
573
460
|
};
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
headers["Authorization"] =
|
|
461
|
+
const authHeader = this.getAuthHeader();
|
|
462
|
+
if (authHeader) {
|
|
463
|
+
headers["Authorization"] = authHeader;
|
|
577
464
|
}
|
|
578
465
|
Object.assign(headers, this.config.headers);
|
|
579
466
|
const abortController = new AbortController();
|
|
580
|
-
const response = await fetch(`${this.config.baseUrl}
|
|
467
|
+
const response = await fetch(`${this.config.baseUrl}/agents/retrieval`, {
|
|
581
468
|
method: "POST",
|
|
582
469
|
headers,
|
|
583
470
|
body: JSON.stringify(request),
|
|
@@ -585,7 +472,7 @@ var AntflyClient = class {
|
|
|
585
472
|
});
|
|
586
473
|
if (!response.ok) {
|
|
587
474
|
const errorText = await response.text();
|
|
588
|
-
throw new Error(`
|
|
475
|
+
throw new Error(`Retrieval agent request failed: ${response.status} ${errorText}`);
|
|
589
476
|
}
|
|
590
477
|
if (!response.body) {
|
|
591
478
|
throw new Error("Response body is null");
|
|
@@ -622,8 +509,7 @@ var AntflyClient = class {
|
|
|
622
509
|
switch (currentEvent) {
|
|
623
510
|
case "classification":
|
|
624
511
|
if (callbacks.onClassification) {
|
|
625
|
-
|
|
626
|
-
callbacks.onClassification(classData);
|
|
512
|
+
callbacks.onClassification(JSON.parse(data));
|
|
627
513
|
}
|
|
628
514
|
break;
|
|
629
515
|
case "reasoning":
|
|
@@ -631,56 +517,64 @@ var AntflyClient = class {
|
|
|
631
517
|
callbacks.onReasoning(JSON.parse(data));
|
|
632
518
|
}
|
|
633
519
|
break;
|
|
634
|
-
case "
|
|
635
|
-
if (callbacks.
|
|
636
|
-
|
|
637
|
-
|
|
520
|
+
case "clarification_required":
|
|
521
|
+
if (callbacks.onClarificationRequired) {
|
|
522
|
+
callbacks.onClarificationRequired(JSON.parse(data));
|
|
523
|
+
}
|
|
524
|
+
break;
|
|
525
|
+
case "filter_applied":
|
|
526
|
+
if (callbacks.onFilterApplied) {
|
|
527
|
+
callbacks.onFilterApplied(JSON.parse(data));
|
|
528
|
+
}
|
|
529
|
+
break;
|
|
530
|
+
case "search_executed":
|
|
531
|
+
if (callbacks.onSearchExecuted) {
|
|
532
|
+
callbacks.onSearchExecuted(JSON.parse(data));
|
|
638
533
|
}
|
|
639
534
|
break;
|
|
640
535
|
case "hit":
|
|
641
536
|
if (callbacks.onHit) {
|
|
642
|
-
|
|
643
|
-
|
|
537
|
+
callbacks.onHit(JSON.parse(data));
|
|
538
|
+
}
|
|
539
|
+
break;
|
|
540
|
+
case "generation":
|
|
541
|
+
if (callbacks.onGeneration) {
|
|
542
|
+
callbacks.onGeneration(JSON.parse(data));
|
|
644
543
|
}
|
|
645
544
|
break;
|
|
646
|
-
case "
|
|
647
|
-
if (callbacks.
|
|
648
|
-
|
|
649
|
-
callbacks.onHitsEnd(hitsEndData);
|
|
545
|
+
case "step_started":
|
|
546
|
+
if (callbacks.onStepStarted) {
|
|
547
|
+
callbacks.onStepStarted(JSON.parse(data));
|
|
650
548
|
}
|
|
651
549
|
break;
|
|
652
|
-
case "
|
|
653
|
-
if (callbacks.
|
|
654
|
-
callbacks.
|
|
550
|
+
case "step_completed":
|
|
551
|
+
if (callbacks.onStepCompleted) {
|
|
552
|
+
callbacks.onStepCompleted(JSON.parse(data));
|
|
655
553
|
}
|
|
656
554
|
break;
|
|
657
555
|
case "confidence":
|
|
658
556
|
if (callbacks.onConfidence) {
|
|
659
|
-
|
|
660
|
-
callbacks.onConfidence(confidence);
|
|
557
|
+
callbacks.onConfidence(JSON.parse(data));
|
|
661
558
|
}
|
|
662
559
|
break;
|
|
663
|
-
case "
|
|
664
|
-
if (callbacks.
|
|
665
|
-
callbacks.
|
|
560
|
+
case "followup":
|
|
561
|
+
if (callbacks.onFollowup) {
|
|
562
|
+
callbacks.onFollowup(JSON.parse(data));
|
|
666
563
|
}
|
|
667
564
|
break;
|
|
668
565
|
case "eval":
|
|
669
566
|
if (callbacks.onEvalResult) {
|
|
670
|
-
|
|
671
|
-
callbacks.onEvalResult(evalResult);
|
|
567
|
+
callbacks.onEvalResult(JSON.parse(data));
|
|
672
568
|
}
|
|
673
569
|
break;
|
|
674
570
|
case "done":
|
|
675
571
|
if (callbacks.onDone) {
|
|
676
|
-
|
|
677
|
-
callbacks.onDone(doneData);
|
|
572
|
+
callbacks.onDone(JSON.parse(data));
|
|
678
573
|
}
|
|
679
574
|
return;
|
|
680
575
|
case "error":
|
|
681
576
|
if (callbacks.onError) {
|
|
682
|
-
|
|
683
|
-
callbacks.onError(error);
|
|
577
|
+
callbacks.onError(JSON.parse(data));
|
|
684
578
|
}
|
|
685
579
|
throw new Error(data);
|
|
686
580
|
}
|
|
@@ -692,7 +586,7 @@ var AntflyClient = class {
|
|
|
692
586
|
}
|
|
693
587
|
} catch (error) {
|
|
694
588
|
if (error.name !== "AbortError") {
|
|
695
|
-
console.error("
|
|
589
|
+
console.error("Retrieval agent streaming error:", error);
|
|
696
590
|
}
|
|
697
591
|
}
|
|
698
592
|
})();
|
|
@@ -700,23 +594,83 @@ var AntflyClient = class {
|
|
|
700
594
|
return abortController;
|
|
701
595
|
}
|
|
702
596
|
/**
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
*
|
|
706
|
-
* @
|
|
597
|
+
* Retrieval Agent - Unified retrieval pipeline with optional classification, generation, and eval
|
|
598
|
+
* Supports pipeline mode (structured queries) and agentic mode (tool-calling with LLM)
|
|
599
|
+
* Configure steps.classification, steps.answer, steps.eval to enable additional pipeline stages
|
|
600
|
+
* @param request - Retrieval agent request with query, mode, and optional step configs
|
|
601
|
+
* @param callbacks - Optional callbacks for SSE events (classification, reasoning, hit, answer, citation, confidence, followup_question, eval, done, error)
|
|
602
|
+
* @returns Promise with RetrievalAgentResult (JSON) or AbortController (when streaming)
|
|
707
603
|
*/
|
|
708
|
-
async
|
|
709
|
-
return this.
|
|
604
|
+
async retrievalAgent(request, callbacks) {
|
|
605
|
+
return this.performRetrievalAgent(request, callbacks);
|
|
710
606
|
}
|
|
711
607
|
/**
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
* @param
|
|
715
|
-
* @param
|
|
716
|
-
* @
|
|
608
|
+
* Chat Agent - Multi-turn conversational retrieval with message history management.
|
|
609
|
+
* Wraps the retrieval agent with automatic message accumulation.
|
|
610
|
+
* @param userMessage - The user's message for this turn
|
|
611
|
+
* @param config - Chat configuration (generator, table, indexes, etc.)
|
|
612
|
+
* @param history - Previous conversation messages (pass result.messages from prior turns)
|
|
613
|
+
* @param callbacks - Optional streaming callbacks including chat-specific events
|
|
614
|
+
* @returns For streaming: { abortController, messages } where messages is a Promise.
|
|
615
|
+
* For non-streaming: { result, messages }
|
|
717
616
|
*/
|
|
718
|
-
async
|
|
719
|
-
|
|
617
|
+
async chatAgent(userMessage, config, history = [], callbacks) {
|
|
618
|
+
const request = {
|
|
619
|
+
query: userMessage,
|
|
620
|
+
queries: [
|
|
621
|
+
{
|
|
622
|
+
table: config.table,
|
|
623
|
+
semantic_search: userMessage,
|
|
624
|
+
indexes: config.semanticIndexes,
|
|
625
|
+
limit: config.limit ?? 10
|
|
626
|
+
}
|
|
627
|
+
],
|
|
628
|
+
generator: config.generator,
|
|
629
|
+
messages: [...history, { role: "user", content: userMessage }],
|
|
630
|
+
max_iterations: config.maxIterations ?? 5,
|
|
631
|
+
stream: !!callbacks,
|
|
632
|
+
agent_knowledge: config.agentKnowledge
|
|
633
|
+
};
|
|
634
|
+
if (config.steps) {
|
|
635
|
+
request.steps = config.steps;
|
|
636
|
+
}
|
|
637
|
+
if (callbacks) {
|
|
638
|
+
let answerText = "";
|
|
639
|
+
let resolveMessages;
|
|
640
|
+
const messagesPromise = new Promise((resolve) => {
|
|
641
|
+
resolveMessages = resolve;
|
|
642
|
+
});
|
|
643
|
+
const wrappedCallbacks = {
|
|
644
|
+
...callbacks,
|
|
645
|
+
onGeneration: (chunk) => {
|
|
646
|
+
answerText += chunk;
|
|
647
|
+
callbacks.onGeneration?.(chunk);
|
|
648
|
+
},
|
|
649
|
+
onDone: (data) => {
|
|
650
|
+
const updatedMessages2 = [
|
|
651
|
+
...history,
|
|
652
|
+
{ role: "user", content: userMessage },
|
|
653
|
+
{ role: "assistant", content: answerText }
|
|
654
|
+
];
|
|
655
|
+
callbacks.onAssistantMessage?.(answerText);
|
|
656
|
+
callbacks.onMessagesUpdated?.(updatedMessages2);
|
|
657
|
+
callbacks.onDone?.(data);
|
|
658
|
+
resolveMessages(updatedMessages2);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
const abortController = await this.performRetrievalAgent(
|
|
662
|
+
request,
|
|
663
|
+
wrappedCallbacks
|
|
664
|
+
);
|
|
665
|
+
return { abortController, messages: messagesPromise };
|
|
666
|
+
}
|
|
667
|
+
const result = await this.performRetrievalAgent(request);
|
|
668
|
+
const updatedMessages = result.messages?.length ? result.messages : [
|
|
669
|
+
...history,
|
|
670
|
+
{ role: "user", content: userMessage },
|
|
671
|
+
...result.generation ? [{ role: "assistant", content: result.generation }] : []
|
|
672
|
+
];
|
|
673
|
+
return { result, messages: updatedMessages };
|
|
720
674
|
}
|
|
721
675
|
/**
|
|
722
676
|
* Query Builder Agent - Translates natural language into structured search queries
|
|
@@ -732,146 +686,17 @@ var AntflyClient = class {
|
|
|
732
686
|
return data;
|
|
733
687
|
}
|
|
734
688
|
/**
|
|
735
|
-
*
|
|
689
|
+
* Standalone evaluation for testing evaluators without running a query.
|
|
690
|
+
* Evaluates a generated output against ground truth using LLM-as-judge metrics.
|
|
691
|
+
* @param request - Eval request with evaluators, judge config, query, output, and ground truth
|
|
692
|
+
* @returns Evaluation result with scores for each evaluator
|
|
736
693
|
*/
|
|
737
|
-
async
|
|
738
|
-
const
|
|
739
|
-
|
|
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
|
|
694
|
+
async evaluate(request) {
|
|
695
|
+
const { data, error } = await this.client.POST("/eval", {
|
|
696
|
+
body: request
|
|
753
697
|
});
|
|
754
|
-
if (
|
|
755
|
-
|
|
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);
|
|
698
|
+
if (error) throw new Error(`Evaluation failed: ${error.error}`);
|
|
699
|
+
return data;
|
|
875
700
|
}
|
|
876
701
|
/**
|
|
877
702
|
* Get the underlying OpenAPI client for advanced use cases
|
|
@@ -1039,8 +864,11 @@ var generatorProviders = [
|
|
|
1039
864
|
"ollama",
|
|
1040
865
|
"gemini",
|
|
1041
866
|
"openai",
|
|
1042
|
-
"
|
|
1043
|
-
"
|
|
867
|
+
"anthropic",
|
|
868
|
+
"vertex",
|
|
869
|
+
"cohere",
|
|
870
|
+
"termite",
|
|
871
|
+
"openrouter"
|
|
1044
872
|
];
|
|
1045
873
|
|
|
1046
874
|
// src/index.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antfly/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "TypeScript SDK for Antfly API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -40,15 +40,15 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://github.com/antflydb/antfly-ts/tree/main/packages/sdk#readme",
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@types/node": "^25.
|
|
44
|
-
"@vitest/ui": "^4.0.
|
|
45
|
-
"openapi-typescript": "^7.
|
|
43
|
+
"@types/node": "^25.3.3",
|
|
44
|
+
"@vitest/ui": "^4.0.18",
|
|
45
|
+
"openapi-typescript": "^7.13.0",
|
|
46
46
|
"tsup": "^8.5.1",
|
|
47
47
|
"tsx": "^4.21.0",
|
|
48
|
-
"vitest": "^4.0.
|
|
48
|
+
"vitest": "^4.0.18"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"openapi-fetch": "^0.
|
|
51
|
+
"openapi-fetch": "^0.17.0"
|
|
52
52
|
},
|
|
53
53
|
"engines": {
|
|
54
54
|
"node": ">=18"
|