@aliyunrds/ctxdb 1.0.8-beta.4 → 1.0.8

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.
@@ -1,17 +1,17 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  listKnowledgeBases
4
- } from "./chunk-ZW7YXNJ6.js";
4
+ } from "./chunk-LZD55CWE.js";
5
5
  import {
6
6
  isConnectionError,
7
7
  resetCircuit,
8
8
  tripCircuit
9
- } from "./chunk-5DGRNP55.js";
9
+ } from "./chunk-MWAFAK5M.js";
10
10
  import {
11
11
  CtxdbError,
12
12
  isDataApiReady,
13
13
  resolveDataApiCredential
14
- } from "./chunk-SVO6H62S.js";
14
+ } from "./chunk-3FAQREIU.js";
15
15
 
16
16
  // src/lib/kb-catalog.ts
17
17
  function sanitizeKeyEntities(raw) {
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  fetchKbCatalogBlock,
4
4
  recallTurn
5
- } from "./chunk-WJTV7JNR.js";
5
+ } from "./chunk-AAWG5SPO.js";
6
6
 
7
7
  // src/lib/user-prompt-submit-compose.ts
8
8
  async function composeUserPromptSubmit(cfg, agent, client, prompt, sessionId = null) {
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  CtxdbError
4
- } from "./chunk-SVO6H62S.js";
4
+ } from "./chunk-3FAQREIU.js";
5
5
 
6
6
  // src/lib/kb.ts
7
7
  import {
@@ -90,14 +90,44 @@ async function listKnowledgeBases(client) {
90
90
  }
91
91
  return [];
92
92
  }
93
- async function createKb(client, kbName, description = "") {
94
- return client.postJson(KB_COLLECTION, { name: kbName, description: description || "" });
93
+ async function createKb(client, kbName, descriptionOrOptions = "") {
94
+ const options = typeof descriptionOrOptions === "string" ? { description: descriptionOrOptions } : descriptionOrOptions;
95
+ const body = {
96
+ name: kbName,
97
+ description: options.description ?? ""
98
+ };
99
+ if (options.graphEnabled !== void 0) body.graph_enabled = options.graphEnabled;
100
+ if (options.reviewEnabled !== void 0) body.review_enabled = options.reviewEnabled;
101
+ return client.postJson(KB_COLLECTION, body);
95
102
  }
96
- async function uploadText(client, kbName, docName, text, mimeType = "text/plain", filePath) {
97
- return client.postJson(
98
- DOCUMENTS,
99
- buildTextDocumentBody(kbName, docName, text, mimeType, filePath)
100
- );
103
+ function knowledgeBaseLocatorParams(locator) {
104
+ if ("knowledgeBaseId" in locator && locator.knowledgeBaseId.trim() !== "") {
105
+ return { knowledge_base_id: locator.knowledgeBaseId };
106
+ }
107
+ if ("knowledgeBaseName" in locator && locator.knowledgeBaseName.trim() !== "") {
108
+ return { knowledge_base_name: locator.knowledgeBaseName };
109
+ }
110
+ throw new CtxdbError("knowledge base locator must be a non-empty ID or name");
111
+ }
112
+ function getKb(client, locator) {
113
+ return client.get(KB_DETAIL, knowledgeBaseLocatorParams(locator));
114
+ }
115
+ function updateKb(client, knowledgeBaseId, options) {
116
+ const body = {};
117
+ if (options.knowledgeBaseName !== void 0) body.knowledge_base_name = options.knowledgeBaseName;
118
+ if (options.description !== void 0) body.description = options.description;
119
+ if (options.graphEnabled !== void 0) body.graph_enabled = options.graphEnabled;
120
+ if (options.reviewEnabled !== void 0) body.review_enabled = options.reviewEnabled;
121
+ if (Object.keys(body).length === 0) throw new CtxdbError("KB update requires at least one field");
122
+ return client.putJson(KB_COLLECTION, body, { knowledge_base_id: knowledgeBaseId });
123
+ }
124
+ function deleteKb(client, locator) {
125
+ return client.delete(KB_COLLECTION, knowledgeBaseLocatorParams(locator));
126
+ }
127
+ async function uploadText(client, kbName, docName, text, mimeType = "text/plain", filePath, reviewEnabled) {
128
+ const body = buildTextDocumentBody(kbName, docName, text, mimeType, filePath);
129
+ if (reviewEnabled !== void 0) body.review_enabled = reviewEnabled;
130
+ return client.postJson(DOCUMENTS, body);
101
131
  }
102
132
  async function updateText(client, locator, text) {
103
133
  const body = {
@@ -155,7 +185,8 @@ async function uploadFile(client, kbName, localPath, options = {}) {
155
185
  mode: "create",
156
186
  kbName,
157
187
  docName: options.docName,
158
- filePath: options.filePath
188
+ filePath: options.filePath,
189
+ reviewEnabled: options.reviewEnabled
159
190
  },
160
191
  localPath,
161
192
  options.timeoutMs
@@ -178,7 +209,8 @@ async function writeFile(client, target, localPath, requestedTimeoutMs) {
178
209
  const fields = target.mode === "create" ? {
179
210
  knowledge_base_name: target.kbName,
180
211
  name: target.docName ?? filename,
181
- ...target.filePath ? { file_path: target.filePath } : {}
212
+ ...target.filePath ? { file_path: target.filePath } : {},
213
+ ...target.reviewEnabled === void 0 ? {} : { review_enabled: String(target.reviewEnabled) }
182
214
  } : buildUpdateLocatorFields(target.locator);
183
215
  const timeoutMs = requestedTimeoutMs ?? DEFAULT_FILE_UPLOAD_TIMEOUT_MS;
184
216
  try {
@@ -237,6 +269,7 @@ async function writeFileInChunks(client, options) {
237
269
  initParams.knowledge_base_id = expectedKnowledgeBaseId;
238
270
  initParams.name = options.target.docName ?? options.filename;
239
271
  if (options.target.filePath) initParams.file_path = options.target.filePath;
272
+ if (options.target.reviewEnabled !== void 0) initParams.review_enabled = options.target.reviewEnabled;
240
273
  } else if ("documentId" in options.target.locator) {
241
274
  initParams.document_id = options.target.locator.documentId;
242
275
  } else {
@@ -614,6 +647,233 @@ function minimalChunk(raw) {
614
647
  function minimalKbQueryResponse(raw) {
615
648
  return projectKbQueryResponse(raw, minimalChunk);
616
649
  }
650
+ var GRAPH_ENTITIES = "/v1/knowledge/entities";
651
+ var GRAPH_ENTITY_DETAIL = "/v1/knowledge/entities/detail";
652
+ var GRAPH_ENTITY_CHUNKS = "/v1/knowledge/entities/chunks";
653
+ var GRAPH_ENTITY_RELATIONS = "/v1/knowledge/entity-relations";
654
+ async function listKnowledgeEntities(client, params = {}) {
655
+ return client.get(GRAPH_ENTITIES, params);
656
+ }
657
+ async function getKnowledgeEntity(client, knowledgeBaseId, entityId) {
658
+ return client.get(GRAPH_ENTITY_DETAIL, {
659
+ knowledge_base_id: knowledgeBaseId,
660
+ entity_id: entityId
661
+ });
662
+ }
663
+ async function listKnowledgeEntityChunks(client, knowledgeBaseId, entityIds, limit) {
664
+ const params = {
665
+ knowledge_base_id: knowledgeBaseId,
666
+ entity_ids: entityIds.join(",")
667
+ };
668
+ if (limit !== void 0) params.limit = limit;
669
+ return client.get(GRAPH_ENTITY_CHUNKS, params);
670
+ }
671
+ async function listKnowledgeEntityRelations(client, params = {}) {
672
+ const { entity_ids, ...rest } = params;
673
+ return client.get(GRAPH_ENTITY_RELATIONS, {
674
+ ...rest,
675
+ ...entity_ids !== void 0 ? { entity_ids: entity_ids.join(",") } : {}
676
+ });
677
+ }
678
+ function graphRecord(value) {
679
+ return value && typeof value === "object" && !Array.isArray(value) ? value : {};
680
+ }
681
+ function graphFieldArray(record, field) {
682
+ return Array.isArray(record[field]) ? record[field] : [];
683
+ }
684
+ function graphString(record, field) {
685
+ const value = record[field];
686
+ return typeof value === "string" ? value : "";
687
+ }
688
+ function graphStringArray(record, field) {
689
+ return graphFieldArray(record, field).filter(
690
+ (item) => typeof item === "string"
691
+ );
692
+ }
693
+ function graphNumber(record, field) {
694
+ const value = record[field];
695
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
696
+ }
697
+ function graphBoolean(record, field) {
698
+ const value = record[field];
699
+ return typeof value === "boolean" ? value : void 0;
700
+ }
701
+ function minimalGraphEntity(raw) {
702
+ const r = graphRecord(raw);
703
+ const out = {
704
+ entity_id: graphString(r, "entity_id"),
705
+ entity_name: graphString(r, "entity_name"),
706
+ entity_type: graphString(r, "entity_type")
707
+ };
708
+ const matchType = graphString(r, "match_type");
709
+ if (matchType !== "") out.match_type = matchType;
710
+ const score = graphNumber(r, "score");
711
+ if (score !== void 0) out.score = score;
712
+ return out;
713
+ }
714
+ function compactGraphEntity(raw) {
715
+ const r = graphRecord(raw);
716
+ const out = {
717
+ ...minimalGraphEntity(raw),
718
+ knowledge_base_id: graphString(r, "knowledge_base_id"),
719
+ description: graphString(r, "description"),
720
+ aliases: graphStringArray(r, "aliases"),
721
+ source_document_ids: graphStringArray(r, "source_document_ids")
722
+ };
723
+ const sourceDocumentCount = graphNumber(r, "source_document_count");
724
+ if (sourceDocumentCount !== void 0) out.source_document_count = sourceDocumentCount;
725
+ const pagerank = graphNumber(r, "pagerank");
726
+ if (pagerank !== void 0) out.pagerank = pagerank;
727
+ const updatedAt = graphString(r, "updated_at");
728
+ if (updatedAt !== "") out.updated_at = updatedAt;
729
+ return out;
730
+ }
731
+ function projectGraphEntityList(raw, projectItem) {
732
+ const r = graphRecord(raw);
733
+ const out = {
734
+ entities: graphFieldArray(r, "entities").map(projectItem)
735
+ };
736
+ const total = graphNumber(r, "total");
737
+ if (total !== void 0) out.total = total;
738
+ const page = graphNumber(r, "page");
739
+ if (page !== void 0) out.page = page;
740
+ const pageSize = graphNumber(r, "page_size");
741
+ if (pageSize !== void 0) out.page_size = pageSize;
742
+ return out;
743
+ }
744
+ function minimalGraphEntityListResponse(raw) {
745
+ return projectGraphEntityList(raw, minimalGraphEntity);
746
+ }
747
+ function compactGraphEntityListResponse(raw) {
748
+ return projectGraphEntityList(raw, compactGraphEntity);
749
+ }
750
+ function minimalGraphEntityDetail(response) {
751
+ const data = graphRecord(unwrapBox(response, "entity detail"));
752
+ return {
753
+ entity_id: graphString(data, "entity_id"),
754
+ entity_name: graphString(data, "entity_name"),
755
+ entity_type: graphString(data, "entity_type"),
756
+ description: graphString(data, "description")
757
+ };
758
+ }
759
+ function compactGraphEntityDetail(response) {
760
+ const data = graphRecord(unwrapBox(response, "entity detail"));
761
+ const out = {
762
+ ...minimalGraphEntityDetail(response),
763
+ knowledge_base_id: graphString(data, "knowledge_base_id"),
764
+ aliases: graphStringArray(data, "aliases"),
765
+ source_document_ids: graphStringArray(data, "source_document_ids")
766
+ };
767
+ const updatedAt = graphString(data, "updated_at");
768
+ if (updatedAt !== "") out.updated_at = updatedAt;
769
+ return out;
770
+ }
771
+ function minimalEntitySourceChunk(raw) {
772
+ const r = graphRecord(raw);
773
+ const out = { content: graphString(r, "content") };
774
+ const matched = graphStringArray(r, "matched_entity_ids");
775
+ if (matched.length > 0) out.matched_entity_ids = matched;
776
+ return out;
777
+ }
778
+ function compactEntitySourceChunk(raw) {
779
+ const r = graphRecord(raw);
780
+ const out = {
781
+ ...minimalEntitySourceChunk(raw),
782
+ chunk_id: graphString(r, "chunk_id"),
783
+ document_id: graphString(r, "document_id"),
784
+ document_name: graphString(r, "document_name"),
785
+ knowledge_base_id: graphString(r, "knowledge_base_id")
786
+ };
787
+ const chunkIndex = graphNumber(r, "chunk_index");
788
+ if (chunkIndex !== void 0) out.chunk_index = chunkIndex;
789
+ const tocPath = graphString(r, "toc_path");
790
+ if (tocPath !== "") out.toc_path = tocPath;
791
+ return out;
792
+ }
793
+ function projectEntityChunks(raw, projectChunk) {
794
+ const r = graphRecord(raw);
795
+ const out = {
796
+ chunks: graphFieldArray(r, "chunks").map(projectChunk)
797
+ };
798
+ const knowledgeBaseId = graphString(r, "knowledge_base_id");
799
+ if (knowledgeBaseId !== "") out.knowledge_base_id = knowledgeBaseId;
800
+ const entityIds = graphStringArray(r, "entity_ids");
801
+ if (entityIds.length > 0) out.entity_ids = entityIds;
802
+ const total = graphNumber(r, "total");
803
+ if (total !== void 0) out.total = total;
804
+ const candidateTotal = graphNumber(r, "candidate_total");
805
+ if (candidateTotal !== void 0) out.candidate_total = candidateTotal;
806
+ const truncated = graphBoolean(r, "truncated");
807
+ if (truncated !== void 0) out.truncated = truncated;
808
+ return out;
809
+ }
810
+ function minimalEntityChunksResponse(raw) {
811
+ return projectEntityChunks(raw, minimalEntitySourceChunk);
812
+ }
813
+ function compactEntityChunksResponse(raw) {
814
+ return projectEntityChunks(raw, compactEntitySourceChunk);
815
+ }
816
+ function minimalGraphRelation(raw) {
817
+ const r = graphRecord(raw);
818
+ return {
819
+ source_entity_id: graphString(r, "source_entity_id"),
820
+ source_entity_name: graphString(r, "source_entity_name"),
821
+ relation_type: graphString(r, "relation_type"),
822
+ target_entity_id: graphString(r, "target_entity_id"),
823
+ target_entity_name: graphString(r, "target_entity_name")
824
+ };
825
+ }
826
+ function compactGraphRelation(raw) {
827
+ const r = graphRecord(raw);
828
+ const out = {
829
+ ...minimalGraphRelation(raw),
830
+ knowledge_base_id: graphString(r, "knowledge_base_id"),
831
+ relation_id: graphString(r, "relation_id"),
832
+ description: graphString(r, "description"),
833
+ keywords: graphStringArray(r, "keywords")
834
+ };
835
+ const weight = graphNumber(r, "weight");
836
+ if (weight !== void 0) out.weight = weight;
837
+ const updatedAt = graphString(r, "updated_at");
838
+ if (updatedAt !== "") out.updated_at = updatedAt;
839
+ return out;
840
+ }
841
+ function graphRelationNode(raw) {
842
+ const r = graphRecord(raw);
843
+ const out = {
844
+ knowledge_base_id: graphString(r, "knowledge_base_id"),
845
+ entity_id: graphString(r, "entity_id"),
846
+ entity_name: graphString(r, "entity_name"),
847
+ entity_type: graphString(r, "entity_type")
848
+ };
849
+ const description = graphString(r, "description");
850
+ if (description !== "") out.description = description;
851
+ const sourceDocumentIds = graphStringArray(r, "source_document_ids");
852
+ if (sourceDocumentIds.length > 0) out.source_document_ids = sourceDocumentIds;
853
+ return out;
854
+ }
855
+ function projectGraphRelationList(raw, projectItem, options) {
856
+ const r = graphRecord(raw);
857
+ const out = {
858
+ relations: graphFieldArray(r, "relations").map(projectItem)
859
+ };
860
+ const total = graphNumber(r, "total");
861
+ if (total !== void 0) out.total = total;
862
+ const page = graphNumber(r, "page");
863
+ if (page !== void 0) out.page = page;
864
+ const pageSize = graphNumber(r, "page_size");
865
+ if (pageSize !== void 0) out.page_size = pageSize;
866
+ if (options.includeNodes) {
867
+ out.nodes = graphFieldArray(r, "nodes").map(graphRelationNode);
868
+ }
869
+ return out;
870
+ }
871
+ function minimalGraphRelationListResponse(raw) {
872
+ return projectGraphRelationList(raw, minimalGraphRelation, { includeNodes: false });
873
+ }
874
+ function compactGraphRelationListResponse(raw) {
875
+ return projectGraphRelationList(raw, compactGraphRelation, { includeNodes: true });
876
+ }
617
877
 
618
878
  export {
619
879
  DEFAULT_INGEST_TIMEOUT_MS,
@@ -621,6 +881,9 @@ export {
621
881
  DEFAULT_FILE_UPLOAD_TIMEOUT_MS,
622
882
  listKnowledgeBases,
623
883
  createKb,
884
+ getKb,
885
+ updateKb,
886
+ deleteKb,
624
887
  uploadText,
625
888
  updateText,
626
889
  uploadFile,
@@ -631,5 +894,17 @@ export {
631
894
  pollIngest,
632
895
  pollIngestByKnowledgeBaseId,
633
896
  compactKbQueryResponse,
634
- minimalKbQueryResponse
897
+ minimalKbQueryResponse,
898
+ listKnowledgeEntities,
899
+ getKnowledgeEntity,
900
+ listKnowledgeEntityChunks,
901
+ listKnowledgeEntityRelations,
902
+ minimalGraphEntityListResponse,
903
+ compactGraphEntityListResponse,
904
+ minimalGraphEntityDetail,
905
+ compactGraphEntityDetail,
906
+ minimalEntityChunksResponse,
907
+ compactEntityChunksResponse,
908
+ minimalGraphRelationListResponse,
909
+ compactGraphRelationListResponse
635
910
  };
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  configDir
4
- } from "./chunk-SVO6H62S.js";
4
+ } from "./chunk-3FAQREIU.js";
5
5
 
6
6
  // src/lib/circuit.ts
7
7
  import { statSync, writeFileSync, unlinkSync, mkdirSync, readdirSync, readFileSync } from "fs";
@@ -2,10 +2,10 @@
2
2
  import {
3
3
  fetchKbCatalogBlock,
4
4
  recallTurn
5
- } from "./chunk-WJTV7JNR.js";
5
+ } from "./chunk-AAWG5SPO.js";
6
6
  import {
7
7
  debug
8
- } from "./chunk-SVO6H62S.js";
8
+ } from "./chunk-3FAQREIU.js";
9
9
 
10
10
  // src/lib/warmup-recall.ts
11
11
  import { execSync } from "child_process";
@@ -4,13 +4,13 @@ import {
4
4
  isConnectionError,
5
5
  resetCircuit,
6
6
  tripCircuit
7
- } from "./chunk-5DGRNP55.js";
7
+ } from "./chunk-MWAFAK5M.js";
8
8
  import {
9
9
  CtxdbError,
10
10
  debug,
11
11
  isDataApiReady,
12
12
  isDebug
13
- } from "./chunk-SVO6H62S.js";
13
+ } from "./chunk-3FAQREIU.js";
14
14
 
15
15
  // src/lib/capture-orchestrator.ts
16
16
  import {