@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 CHANGED
@@ -183,8 +183,9 @@ const results = await client.query({
183
183
  query: 'important document'
184
184
  },
185
185
  limit: 50,
186
- facets: {
186
+ aggregations: {
187
187
  category: {
188
+ type: 'terms',
188
189
  field: 'category',
189
190
  size: 10
190
191
  }
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 (config.auth) {
194
- const auth = btoa(`${config.auth.username}:${config.auth.password}`);
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
- if (config.auth) {
405
- const auth = btoa(`${config.auth.username}:${config.auth.password}`);
406
- headers["Authorization"] = `Basic ${auth}`;
419
+ const authHeader = this.getAuthHeader();
420
+ if (authHeader) {
421
+ headers["Authorization"] = authHeader;
407
422
  }
408
- this.client = (0, import_openapi_fetch.default)({
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
- * Update authentication credentials
421
- */
422
- setAuth(username, password) {
423
- this.config.auth = { username, password };
424
- const auth = btoa(`${username}:${password}`);
425
- this.client = (0, import_openapi_fetch.default)({
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
@@ -503,136 +506,20 @@ var AntflyClient = class {
503
506
  return this.performMultiquery("/query", requests);
504
507
  }
505
508
  /**
506
- * Private helper for RAG requests to avoid code duplication
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
509
+ * Private helper for Retrieval Agent requests to handle streaming and non-streaming responses
623
510
  */
624
- async performAnswerAgent(path, request, callbacks) {
511
+ async performRetrievalAgent(request, callbacks) {
625
512
  const headers = {
626
513
  "Content-Type": "application/json",
627
514
  Accept: "text/event-stream, application/json"
628
515
  };
629
- if (this.config.auth) {
630
- const auth = btoa(`${this.config.auth.username}:${this.config.auth.password}`);
631
- headers["Authorization"] = `Basic ${auth}`;
516
+ const authHeader = this.getAuthHeader();
517
+ if (authHeader) {
518
+ headers["Authorization"] = authHeader;
632
519
  }
633
520
  Object.assign(headers, this.config.headers);
634
521
  const abortController = new AbortController();
635
- const response = await fetch(`${this.config.baseUrl}${path}`, {
522
+ const response = await fetch(`${this.config.baseUrl}/agents/retrieval`, {
636
523
  method: "POST",
637
524
  headers,
638
525
  body: JSON.stringify(request),
@@ -640,7 +527,7 @@ var AntflyClient = class {
640
527
  });
641
528
  if (!response.ok) {
642
529
  const errorText = await response.text();
643
- throw new Error(`Answer agent request failed: ${response.status} ${errorText}`);
530
+ throw new Error(`Retrieval agent request failed: ${response.status} ${errorText}`);
644
531
  }
645
532
  if (!response.body) {
646
533
  throw new Error("Response body is null");
@@ -677,8 +564,7 @@ var AntflyClient = class {
677
564
  switch (currentEvent) {
678
565
  case "classification":
679
566
  if (callbacks.onClassification) {
680
- const classData = JSON.parse(data);
681
- callbacks.onClassification(classData);
567
+ callbacks.onClassification(JSON.parse(data));
682
568
  }
683
569
  break;
684
570
  case "reasoning":
@@ -686,56 +572,64 @@ var AntflyClient = class {
686
572
  callbacks.onReasoning(JSON.parse(data));
687
573
  }
688
574
  break;
689
- case "hits_start":
690
- if (callbacks.onHitsStart) {
691
- const hitsStartData = JSON.parse(data);
692
- callbacks.onHitsStart(hitsStartData);
575
+ case "clarification_required":
576
+ if (callbacks.onClarificationRequired) {
577
+ callbacks.onClarificationRequired(JSON.parse(data));
578
+ }
579
+ break;
580
+ case "filter_applied":
581
+ if (callbacks.onFilterApplied) {
582
+ callbacks.onFilterApplied(JSON.parse(data));
583
+ }
584
+ break;
585
+ case "search_executed":
586
+ if (callbacks.onSearchExecuted) {
587
+ callbacks.onSearchExecuted(JSON.parse(data));
693
588
  }
694
589
  break;
695
590
  case "hit":
696
591
  if (callbacks.onHit) {
697
- const hit = JSON.parse(data);
698
- callbacks.onHit(hit);
592
+ callbacks.onHit(JSON.parse(data));
593
+ }
594
+ break;
595
+ case "generation":
596
+ if (callbacks.onGeneration) {
597
+ callbacks.onGeneration(JSON.parse(data));
699
598
  }
700
599
  break;
701
- case "hits_end":
702
- if (callbacks.onHitsEnd) {
703
- const hitsEndData = JSON.parse(data);
704
- callbacks.onHitsEnd(hitsEndData);
600
+ case "step_started":
601
+ if (callbacks.onStepStarted) {
602
+ callbacks.onStepStarted(JSON.parse(data));
705
603
  }
706
604
  break;
707
- case "answer":
708
- if (callbacks.onAnswer) {
709
- callbacks.onAnswer(JSON.parse(data));
605
+ case "step_completed":
606
+ if (callbacks.onStepCompleted) {
607
+ callbacks.onStepCompleted(JSON.parse(data));
710
608
  }
711
609
  break;
712
610
  case "confidence":
713
611
  if (callbacks.onConfidence) {
714
- const confidence = JSON.parse(data);
715
- callbacks.onConfidence(confidence);
612
+ callbacks.onConfidence(JSON.parse(data));
716
613
  }
717
614
  break;
718
- case "followup_question":
719
- if (callbacks.onFollowUpQuestion) {
720
- callbacks.onFollowUpQuestion(JSON.parse(data));
615
+ case "followup":
616
+ if (callbacks.onFollowup) {
617
+ callbacks.onFollowup(JSON.parse(data));
721
618
  }
722
619
  break;
723
620
  case "eval":
724
621
  if (callbacks.onEvalResult) {
725
- const evalResult = JSON.parse(data);
726
- callbacks.onEvalResult(evalResult);
622
+ callbacks.onEvalResult(JSON.parse(data));
727
623
  }
728
624
  break;
729
625
  case "done":
730
626
  if (callbacks.onDone) {
731
- const doneData = JSON.parse(data);
732
- callbacks.onDone(doneData);
627
+ callbacks.onDone(JSON.parse(data));
733
628
  }
734
629
  return;
735
630
  case "error":
736
631
  if (callbacks.onError) {
737
- const error = JSON.parse(data);
738
- callbacks.onError(error);
632
+ callbacks.onError(JSON.parse(data));
739
633
  }
740
634
  throw new Error(data);
741
635
  }
@@ -747,7 +641,7 @@ var AntflyClient = class {
747
641
  }
748
642
  } catch (error) {
749
643
  if (error.name !== "AbortError") {
750
- console.error("Answer agent streaming error:", error);
644
+ console.error("Retrieval agent streaming error:", error);
751
645
  }
752
646
  }
753
647
  })();
@@ -755,23 +649,83 @@ var AntflyClient = class {
755
649
  return abortController;
756
650
  }
757
651
  /**
758
- * RAG (Retrieval-Augmented Generation) query with streaming or citations
759
- * @param request - RAG request with query and summarizer config (set with_streaming: true to enable streaming)
760
- * @param callbacks - Optional callbacks for structured SSE events (hit, summary, citation, done, error)
761
- * @returns Promise with RAG result (JSON) or AbortController (when streaming)
652
+ * Retrieval Agent - Unified retrieval pipeline with optional classification, generation, and eval
653
+ * Supports pipeline mode (structured queries) and agentic mode (tool-calling with LLM)
654
+ * Configure steps.classification, steps.answer, steps.eval to enable additional pipeline stages
655
+ * @param request - Retrieval agent request with query, mode, and optional step configs
656
+ * @param callbacks - Optional callbacks for SSE events (classification, reasoning, hit, answer, citation, confidence, followup_question, eval, done, error)
657
+ * @returns Promise with RetrievalAgentResult (JSON) or AbortController (when streaming)
762
658
  */
763
- async rag(request, callbacks) {
764
- return this.performRag("/rag", request, callbacks);
659
+ async retrievalAgent(request, callbacks) {
660
+ return this.performRetrievalAgent(request, callbacks);
765
661
  }
766
662
  /**
767
- * Answer Agent - Intelligent query routing and generation
768
- * Automatically classifies queries, generates optimal searches, and provides answers
769
- * @param request - Answer agent request with query and generator config
770
- * @param callbacks - Optional callbacks for SSE events (classification, hits_start, hit, hits_end, reasoning, answer, followup_question, done, error)
771
- * @returns Promise with AnswerAgentResult (JSON) or AbortController (when streaming)
663
+ * Chat Agent - Multi-turn conversational retrieval with message history management.
664
+ * Wraps the retrieval agent with automatic message accumulation.
665
+ * @param userMessage - The user's message for this turn
666
+ * @param config - Chat configuration (generator, table, indexes, etc.)
667
+ * @param history - Previous conversation messages (pass result.messages from prior turns)
668
+ * @param callbacks - Optional streaming callbacks including chat-specific events
669
+ * @returns For streaming: { abortController, messages } where messages is a Promise.
670
+ * For non-streaming: { result, messages }
772
671
  */
773
- async answerAgent(request, callbacks) {
774
- return this.performAnswerAgent("/agents/answer", request, callbacks);
672
+ async chatAgent(userMessage, config, history = [], callbacks) {
673
+ const request = {
674
+ query: userMessage,
675
+ queries: [
676
+ {
677
+ table: config.table,
678
+ semantic_search: userMessage,
679
+ indexes: config.semanticIndexes,
680
+ limit: config.limit ?? 10
681
+ }
682
+ ],
683
+ generator: config.generator,
684
+ messages: [...history, { role: "user", content: userMessage }],
685
+ max_iterations: config.maxIterations ?? 5,
686
+ stream: !!callbacks,
687
+ agent_knowledge: config.agentKnowledge
688
+ };
689
+ if (config.steps) {
690
+ request.steps = config.steps;
691
+ }
692
+ if (callbacks) {
693
+ let answerText = "";
694
+ let resolveMessages;
695
+ const messagesPromise = new Promise((resolve) => {
696
+ resolveMessages = resolve;
697
+ });
698
+ const wrappedCallbacks = {
699
+ ...callbacks,
700
+ onGeneration: (chunk) => {
701
+ answerText += chunk;
702
+ callbacks.onGeneration?.(chunk);
703
+ },
704
+ onDone: (data) => {
705
+ const updatedMessages2 = [
706
+ ...history,
707
+ { role: "user", content: userMessage },
708
+ { role: "assistant", content: answerText }
709
+ ];
710
+ callbacks.onAssistantMessage?.(answerText);
711
+ callbacks.onMessagesUpdated?.(updatedMessages2);
712
+ callbacks.onDone?.(data);
713
+ resolveMessages(updatedMessages2);
714
+ }
715
+ };
716
+ const abortController = await this.performRetrievalAgent(
717
+ request,
718
+ wrappedCallbacks
719
+ );
720
+ return { abortController, messages: messagesPromise };
721
+ }
722
+ const result = await this.performRetrievalAgent(request);
723
+ const updatedMessages = result.messages?.length ? result.messages : [
724
+ ...history,
725
+ { role: "user", content: userMessage },
726
+ ...result.generation ? [{ role: "assistant", content: result.generation }] : []
727
+ ];
728
+ return { result, messages: updatedMessages };
775
729
  }
776
730
  /**
777
731
  * Query Builder Agent - Translates natural language into structured search queries
@@ -787,146 +741,17 @@ var AntflyClient = class {
787
741
  return data;
788
742
  }
789
743
  /**
790
- * Private helper for Chat Agent requests to handle streaming and non-streaming responses
744
+ * Standalone evaluation for testing evaluators without running a query.
745
+ * Evaluates a generated output against ground truth using LLM-as-judge metrics.
746
+ * @param request - Eval request with evaluators, judge config, query, output, and ground truth
747
+ * @returns Evaluation result with scores for each evaluator
791
748
  */
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
749
+ async evaluate(request) {
750
+ const { data, error } = await this.client.POST("/eval", {
751
+ body: request
808
752
  });
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);
753
+ if (error) throw new Error(`Evaluation failed: ${error.error}`);
754
+ return data;
930
755
  }
931
756
  /**
932
757
  * Get the underlying OpenAPI client for advanced use cases
@@ -1094,8 +919,11 @@ var generatorProviders = [
1094
919
  "ollama",
1095
920
  "gemini",
1096
921
  "openai",
1097
- "bedrock",
1098
- "anthropic"
922
+ "anthropic",
923
+ "vertex",
924
+ "cohere",
925
+ "termite",
926
+ "openrouter"
1099
927
  ];
1100
928
 
1101
929
  // src/index.ts