@xpert-ai/plugin-sdk 3.6.3 → 3.6.5

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/index.cjs.js CHANGED
@@ -25,6 +25,7 @@ var documents = require('@langchain/core/documents');
25
25
  var chat_models = require('@langchain/core/language_models/chat_models');
26
26
  var messages = require('@langchain/core/messages');
27
27
  var env = require('@langchain/core/utils/env');
28
+ var jsTiktoken = require('js-tiktoken');
28
29
 
29
30
  function _interopNamespaceDefault(e) {
30
31
  var n = Object.create(null);
@@ -936,7 +937,25 @@ exports.DataSourceStrategyRegistry = __decorate([
936
937
  * Helper base class to wrap existing adapter query runner implementations
937
938
  * into datasource strategies consumable by the plugin SDK.
938
939
  */ class AdapterDataSourceStrategy {
939
- async create(options) {
940
+ /**
941
+ * Get from cache or create new instance of DB adapter.
942
+ *
943
+ * @param options
944
+ * @param id
945
+ * @returns
946
+ */ async create(options, id) {
947
+ if (id) {
948
+ if (this.runners.has(id)) {
949
+ return this.runners.get(id);
950
+ }
951
+ const runner = this.instantiateRunner(options);
952
+ this.runners.set(id, runner);
953
+ // If the Runner supports initPool, then initialize the connection pool.
954
+ if (typeof runner.initPool === 'function') {
955
+ await runner.initPool(options);
956
+ }
957
+ return runner;
958
+ }
940
959
  return this.instantiateRunner(options);
941
960
  }
942
961
  async configurationSchema() {
@@ -962,6 +981,7 @@ exports.DataSourceStrategyRegistry = __decorate([
962
981
  constructor(runnerClass, extraArgs = []){
963
982
  this.runnerClass = runnerClass;
964
983
  this.extraArgs = extraArgs;
984
+ this.runners = new Map();
965
985
  }
966
986
  }
967
987
 
@@ -995,6 +1015,12 @@ class BaseQueryRunner {
995
1015
  async dropTable(name, options) {
996
1016
  this.runQuery(`DROP TABLE ${name}`, options);
997
1017
  }
1018
+ async tableOp(action, params, options) {
1019
+ throw new Error(`Unimplemented method`);
1020
+ }
1021
+ async tableDataOp(action, params, options) {
1022
+ throw new Error(`Unimplemented tableDataOp`);
1023
+ }
998
1024
  constructor(options){
999
1025
  this.options = options;
1000
1026
  }
@@ -1060,6 +1086,39 @@ class BaseSQLQueryRunner extends BaseQueryRunner {
1060
1086
  this.protocol = "sql";
1061
1087
  }
1062
1088
  }
1089
+ exports.DBTableAction = void 0;
1090
+ (function(DBTableAction) {
1091
+ DBTableAction["LIST_TABLES"] = "listTables";
1092
+ DBTableAction["TABLE_EXISTS"] = "tableExists";
1093
+ DBTableAction["CREATE_TABLE"] = "createTable";
1094
+ DBTableAction["DROP_TABLE"] = "dropTable";
1095
+ DBTableAction["RENAME_TABLE"] = "renameTable";
1096
+ DBTableAction["TRUNCATE_TABLE"] = "truncateTable";
1097
+ DBTableAction["ADD_COLUMN"] = "addColumn";
1098
+ DBTableAction["DROP_COLUMN"] = "dropColumn";
1099
+ DBTableAction["MODIFY_COLUMN"] = "modifyColumn";
1100
+ DBTableAction["CREATE_INDEX"] = "createIndex";
1101
+ DBTableAction["DROP_INDEX"] = "dropIndex";
1102
+ DBTableAction["GET_TABLE_INFO"] = "getTableInfo";
1103
+ DBTableAction["CLONE_TABLE_STRUCTURE"] = "cloneTableStructure";
1104
+ DBTableAction["CLONE_TABLE"] = "cloneTable";
1105
+ DBTableAction["OPTIMIZE_TABLE"] = "optimizeTable";
1106
+ })(exports.DBTableAction || (exports.DBTableAction = {}));
1107
+ exports.DBCreateTableMode = void 0;
1108
+ (function(DBCreateTableMode) {
1109
+ DBCreateTableMode["ERROR"] = "error";
1110
+ DBCreateTableMode["IGNORE"] = "ignore";
1111
+ DBCreateTableMode["UPGRADE"] = "upgrade"; // 表已存在 → 自动升级表结构
1112
+ })(exports.DBCreateTableMode || (exports.DBCreateTableMode = {}));
1113
+ exports.DBTableDataAction = void 0;
1114
+ (function(DBTableDataAction) {
1115
+ DBTableDataAction["INSERT"] = "insert";
1116
+ DBTableDataAction["UPDATE"] = "update";
1117
+ DBTableDataAction["UPSERT"] = "upsert";
1118
+ DBTableDataAction["DELETE"] = "delete";
1119
+ DBTableDataAction["SELECT"] = "select";
1120
+ DBTableDataAction["BULK_INSERT"] = "bulkInsert";
1121
+ })(exports.DBTableDataAction || (exports.DBTableDataAction = {}));
1063
1122
 
1064
1123
  const AI_MODEL_PROVIDER = 'AI_MODEL_PROVIDER';
1065
1124
  function AIModelProviderStrategy(provider) {
@@ -1109,6 +1168,8 @@ class AiModelNotFoundException extends common.NotFoundException {
1109
1168
  }
1110
1169
  class CredentialsValidateFailedError extends common.ForbiddenException {
1111
1170
  }
1171
+ class AIModelProviderNotFoundException extends common.NotFoundException {
1172
+ }
1112
1173
 
1113
1174
  exports.ModelProvider = class ModelProvider {
1114
1175
  get name() {
@@ -1980,6 +2041,58 @@ class Speech2TextChatModel extends chat_models.BaseChatModel {
1980
2041
  }
1981
2042
  }
1982
2043
 
2044
+ /**
2045
+ * Fallback token estimation method.
2046
+ *
2047
+ * 思路:
2048
+ * - 英文:约 4 chars / token
2049
+ * - 中文:约 1.5 chars / token
2050
+ * - Mixed:按字符区分
2051
+ */ function estimateTokens(text) {
2052
+ if (!text) return 0;
2053
+ let cn = 0;
2054
+ let en = 0;
2055
+ for (const ch of text){
2056
+ if (/[\u4e00-\u9fa5]/.test(ch)) {
2057
+ cn++;
2058
+ } else {
2059
+ en++;
2060
+ }
2061
+ }
2062
+ // 中文 1 token ≈ 1.5 chars
2063
+ const cnTokens = cn / 1.5;
2064
+ // 英文 1 token ≈ 4 chars
2065
+ const enTokens = en / 4;
2066
+ return Math.ceil(cnTokens + enTokens);
2067
+ }
2068
+ /**
2069
+ * Count tokens in text
2070
+ * 1) Preferred: js-tiktoken precise encoding
2071
+ * 2) Fallback: estimated token count
2072
+ */ function countTokensSafe(text, opts) {
2073
+ if (!text) return 0;
2074
+ let resolvedEncoding;
2075
+ // Decide encoding name
2076
+ if (opts == null ? void 0 : opts.encodingName) {
2077
+ resolvedEncoding = String(opts.encodingName);
2078
+ } else if (opts == null ? void 0 : opts.model) {
2079
+ var _getEncodingNameForModel;
2080
+ resolvedEncoding = (_getEncodingNameForModel = jsTiktoken.getEncodingNameForModel(opts.model)) != null ? _getEncodingNameForModel : 'cl100k_base';
2081
+ } else {
2082
+ resolvedEncoding = 'cl100k_base';
2083
+ }
2084
+ try {
2085
+ // Prefer exact token count via js-tiktoken
2086
+ const enc = (opts == null ? void 0 : opts.model) ? jsTiktoken.encodingForModel(opts.model) : jsTiktoken.getEncoding(resolvedEncoding);
2087
+ const tokens = enc.encode(text);
2088
+ return tokens.length;
2089
+ } catch (e) {
2090
+ // Fallback
2091
+ console.warn('[countTokensSafe] tiktoken failed, fallback estimate', e);
2092
+ return estimateTokens(text);
2093
+ }
2094
+ }
2095
+
1983
2096
  Object.defineProperty(exports, "IColumnDef", {
1984
2097
  enumerable: true,
1985
2098
  get: function () { return contracts.IColumnDef; }
@@ -1996,6 +2109,7 @@ Object.defineProperty(exports, "TDocumentAsset", {
1996
2109
  enumerable: true,
1997
2110
  get: function () { return contracts.TDocumentAsset; }
1998
2111
  });
2112
+ exports.AIModelProviderNotFoundException = AIModelProviderNotFoundException;
1999
2113
  exports.AIModelProviderStrategy = AIModelProviderStrategy;
2000
2114
  exports.AI_MODEL_PROVIDER = AI_MODEL_PROVIDER;
2001
2115
  exports.AdapterDataSourceStrategy = AdapterDataSourceStrategy;
@@ -2053,6 +2167,7 @@ exports.XpFileSystem = XpFileSystem;
2053
2167
  exports.XpertServerPlugin = XpertServerPlugin;
2054
2168
  exports.als = als;
2055
2169
  exports.calcTokenUsage = calcTokenUsage;
2170
+ exports.countTokensSafe = countTokensSafe;
2056
2171
  exports.createI18nInstance = createI18nInstance;
2057
2172
  exports.createPluginLogger = createPluginLogger;
2058
2173
  exports.downloadRemoteFile = downloadRemoteFile;
package/index.esm.js CHANGED
@@ -27,6 +27,7 @@ import { Document } from '@langchain/core/documents';
27
27
  import { BaseChatModel } from '@langchain/core/language_models/chat_models';
28
28
  import { AIMessage } from '@langchain/core/messages';
29
29
  import { getEnvironmentVariable } from '@langchain/core/utils/env';
30
+ import { getEncodingNameForModel, encodingForModel, getEncoding } from 'js-tiktoken';
30
31
 
31
32
  /**
32
33
  * Metadata keys used in plugins for defining various aspects like entities, subscribers, and configurations.
@@ -916,7 +917,25 @@ DataSourceStrategyRegistry = __decorate([
916
917
  * Helper base class to wrap existing adapter query runner implementations
917
918
  * into datasource strategies consumable by the plugin SDK.
918
919
  */ class AdapterDataSourceStrategy {
919
- async create(options) {
920
+ /**
921
+ * Get from cache or create new instance of DB adapter.
922
+ *
923
+ * @param options
924
+ * @param id
925
+ * @returns
926
+ */ async create(options, id) {
927
+ if (id) {
928
+ if (this.runners.has(id)) {
929
+ return this.runners.get(id);
930
+ }
931
+ const runner = this.instantiateRunner(options);
932
+ this.runners.set(id, runner);
933
+ // If the Runner supports initPool, then initialize the connection pool.
934
+ if (typeof runner.initPool === 'function') {
935
+ await runner.initPool(options);
936
+ }
937
+ return runner;
938
+ }
920
939
  return this.instantiateRunner(options);
921
940
  }
922
941
  async configurationSchema() {
@@ -942,6 +961,7 @@ DataSourceStrategyRegistry = __decorate([
942
961
  constructor(runnerClass, extraArgs = []){
943
962
  this.runnerClass = runnerClass;
944
963
  this.extraArgs = extraArgs;
964
+ this.runners = new Map();
945
965
  }
946
966
  }
947
967
 
@@ -975,6 +995,12 @@ class BaseQueryRunner {
975
995
  async dropTable(name, options) {
976
996
  this.runQuery(`DROP TABLE ${name}`, options);
977
997
  }
998
+ async tableOp(action, params, options) {
999
+ throw new Error(`Unimplemented method`);
1000
+ }
1001
+ async tableDataOp(action, params, options) {
1002
+ throw new Error(`Unimplemented tableDataOp`);
1003
+ }
978
1004
  constructor(options){
979
1005
  this.options = options;
980
1006
  }
@@ -1040,6 +1066,39 @@ class BaseSQLQueryRunner extends BaseQueryRunner {
1040
1066
  this.protocol = "sql";
1041
1067
  }
1042
1068
  }
1069
+ var DBTableAction;
1070
+ (function(DBTableAction) {
1071
+ DBTableAction["LIST_TABLES"] = "listTables";
1072
+ DBTableAction["TABLE_EXISTS"] = "tableExists";
1073
+ DBTableAction["CREATE_TABLE"] = "createTable";
1074
+ DBTableAction["DROP_TABLE"] = "dropTable";
1075
+ DBTableAction["RENAME_TABLE"] = "renameTable";
1076
+ DBTableAction["TRUNCATE_TABLE"] = "truncateTable";
1077
+ DBTableAction["ADD_COLUMN"] = "addColumn";
1078
+ DBTableAction["DROP_COLUMN"] = "dropColumn";
1079
+ DBTableAction["MODIFY_COLUMN"] = "modifyColumn";
1080
+ DBTableAction["CREATE_INDEX"] = "createIndex";
1081
+ DBTableAction["DROP_INDEX"] = "dropIndex";
1082
+ DBTableAction["GET_TABLE_INFO"] = "getTableInfo";
1083
+ DBTableAction["CLONE_TABLE_STRUCTURE"] = "cloneTableStructure";
1084
+ DBTableAction["CLONE_TABLE"] = "cloneTable";
1085
+ DBTableAction["OPTIMIZE_TABLE"] = "optimizeTable";
1086
+ })(DBTableAction || (DBTableAction = {}));
1087
+ var DBCreateTableMode;
1088
+ (function(DBCreateTableMode) {
1089
+ DBCreateTableMode["ERROR"] = "error";
1090
+ DBCreateTableMode["IGNORE"] = "ignore";
1091
+ DBCreateTableMode["UPGRADE"] = "upgrade"; // 表已存在 → 自动升级表结构
1092
+ })(DBCreateTableMode || (DBCreateTableMode = {}));
1093
+ var DBTableDataAction;
1094
+ (function(DBTableDataAction) {
1095
+ DBTableDataAction["INSERT"] = "insert";
1096
+ DBTableDataAction["UPDATE"] = "update";
1097
+ DBTableDataAction["UPSERT"] = "upsert";
1098
+ DBTableDataAction["DELETE"] = "delete";
1099
+ DBTableDataAction["SELECT"] = "select";
1100
+ DBTableDataAction["BULK_INSERT"] = "bulkInsert";
1101
+ })(DBTableDataAction || (DBTableDataAction = {}));
1043
1102
 
1044
1103
  const AI_MODEL_PROVIDER = 'AI_MODEL_PROVIDER';
1045
1104
  function AIModelProviderStrategy(provider) {
@@ -1089,6 +1148,8 @@ class AiModelNotFoundException extends NotFoundException {
1089
1148
  }
1090
1149
  class CredentialsValidateFailedError extends ForbiddenException {
1091
1150
  }
1151
+ class AIModelProviderNotFoundException extends NotFoundException {
1152
+ }
1092
1153
 
1093
1154
  let ModelProvider = class ModelProvider {
1094
1155
  get name() {
@@ -1960,4 +2021,56 @@ class Speech2TextChatModel extends BaseChatModel {
1960
2021
  }
1961
2022
  }
1962
2023
 
1963
- export { AIModelProviderRegistry, AIModelProviderStrategy, AI_MODEL_PROVIDER, AdapterDataSourceStrategy, AiModelNotFoundException, BaseHTTPQueryRunner, BaseQueryRunner, BaseSQLQueryRunner, BaseTool, BaseToolset, BuiltinToolset, ChatOAICompatReasoningModel, CommonParameterRules, CredentialsValidateFailedError, DATASOURCE_STRATEGY, DBProtocolEnum, DBSyntaxEnum, DOCUMENT_SOURCE_STRATEGY, DOCUMENT_TRANSFORMER_STRATEGY, DataSourceStrategy, DataSourceStrategyRegistry, DocumentSourceRegistry, DocumentSourceStrategy, DocumentTransformerRegistry, DocumentTransformerStrategy, IMAGE_UNDERSTANDING_STRATEGY, INTEGRATION_STRATEGY, ImageUnderstandingRegistry, ImageUnderstandingStrategy, IntegrationStrategyKey, IntegrationStrategyRegistry, KNOWLEDGE_STRATEGY, KnowledgeStrategyKey, KnowledgeStrategyRegistry, LLMUsage, LargeLanguageModel, ModelProvider, OpenAICompatibleReranker, PLUGIN_METADATA, PROVIDE_AI_MODEL_LLM, PROVIDE_AI_MODEL_MODERATION, PROVIDE_AI_MODEL_RERANK, PROVIDE_AI_MODEL_SPEECH2TEXT, PROVIDE_AI_MODEL_TEXT_EMBEDDING, PROVIDE_AI_MODEL_TTS, RETRIEVER_STRATEGY, RequestContext, RequestContextMiddleware, RerankModel, RetrieverRegistry, RetrieverStrategy, Speech2TextChatModel, SpeechToTextModel, TEXT_SPLITTER_STRATEGY, TOOLSET_STRATEGY, TextEmbeddingModelManager, TextSplitterRegistry, TextSplitterStrategy, TextToSpeechModel, ToolsetRegistry, ToolsetStrategy, VECTOR_STORE_STRATEGY, VectorStoreRegistry, VectorStoreStrategy, WORKFLOW_NODE_STRATEGY, WORKFLOW_TRIGGER_STRATEGY, WorkflowNodeRegistry, WorkflowNodeStrategy, WorkflowTriggerRegistry, WorkflowTriggerStrategy, XpFileSystem, XpertServerPlugin, als, calcTokenUsage, createI18nInstance, createPluginLogger, downloadRemoteFile, getErrorMessage, getPositionList, getPositionMap, getRequestContext, isRemoteFile, loadYamlFile, mergeCredentials, mergeParentChildChunks, runWithRequestContext, sumTokenUsage };
2024
+ /**
2025
+ * Fallback token estimation method.
2026
+ *
2027
+ * 思路:
2028
+ * - 英文:约 4 chars / token
2029
+ * - 中文:约 1.5 chars / token
2030
+ * - Mixed:按字符区分
2031
+ */ function estimateTokens(text) {
2032
+ if (!text) return 0;
2033
+ let cn = 0;
2034
+ let en = 0;
2035
+ for (const ch of text){
2036
+ if (/[\u4e00-\u9fa5]/.test(ch)) {
2037
+ cn++;
2038
+ } else {
2039
+ en++;
2040
+ }
2041
+ }
2042
+ // 中文 1 token ≈ 1.5 chars
2043
+ const cnTokens = cn / 1.5;
2044
+ // 英文 1 token ≈ 4 chars
2045
+ const enTokens = en / 4;
2046
+ return Math.ceil(cnTokens + enTokens);
2047
+ }
2048
+ /**
2049
+ * Count tokens in text
2050
+ * 1) Preferred: js-tiktoken precise encoding
2051
+ * 2) Fallback: estimated token count
2052
+ */ function countTokensSafe(text, opts) {
2053
+ if (!text) return 0;
2054
+ let resolvedEncoding;
2055
+ // Decide encoding name
2056
+ if (opts == null ? void 0 : opts.encodingName) {
2057
+ resolvedEncoding = String(opts.encodingName);
2058
+ } else if (opts == null ? void 0 : opts.model) {
2059
+ var _getEncodingNameForModel;
2060
+ resolvedEncoding = (_getEncodingNameForModel = getEncodingNameForModel(opts.model)) != null ? _getEncodingNameForModel : 'cl100k_base';
2061
+ } else {
2062
+ resolvedEncoding = 'cl100k_base';
2063
+ }
2064
+ try {
2065
+ // Prefer exact token count via js-tiktoken
2066
+ const enc = (opts == null ? void 0 : opts.model) ? encodingForModel(opts.model) : getEncoding(resolvedEncoding);
2067
+ const tokens = enc.encode(text);
2068
+ return tokens.length;
2069
+ } catch (e) {
2070
+ // Fallback
2071
+ console.warn('[countTokensSafe] tiktoken failed, fallback estimate', e);
2072
+ return estimateTokens(text);
2073
+ }
2074
+ }
2075
+
2076
+ export { AIModelProviderNotFoundException, AIModelProviderRegistry, AIModelProviderStrategy, AI_MODEL_PROVIDER, AdapterDataSourceStrategy, AiModelNotFoundException, BaseHTTPQueryRunner, BaseQueryRunner, BaseSQLQueryRunner, BaseTool, BaseToolset, BuiltinToolset, ChatOAICompatReasoningModel, CommonParameterRules, CredentialsValidateFailedError, DATASOURCE_STRATEGY, DBCreateTableMode, DBProtocolEnum, DBSyntaxEnum, DBTableAction, DBTableDataAction, DOCUMENT_SOURCE_STRATEGY, DOCUMENT_TRANSFORMER_STRATEGY, DataSourceStrategy, DataSourceStrategyRegistry, DocumentSourceRegistry, DocumentSourceStrategy, DocumentTransformerRegistry, DocumentTransformerStrategy, IMAGE_UNDERSTANDING_STRATEGY, INTEGRATION_STRATEGY, ImageUnderstandingRegistry, ImageUnderstandingStrategy, IntegrationStrategyKey, IntegrationStrategyRegistry, KNOWLEDGE_STRATEGY, KnowledgeStrategyKey, KnowledgeStrategyRegistry, LLMUsage, LargeLanguageModel, ModelProvider, OpenAICompatibleReranker, PLUGIN_METADATA, PROVIDE_AI_MODEL_LLM, PROVIDE_AI_MODEL_MODERATION, PROVIDE_AI_MODEL_RERANK, PROVIDE_AI_MODEL_SPEECH2TEXT, PROVIDE_AI_MODEL_TEXT_EMBEDDING, PROVIDE_AI_MODEL_TTS, RETRIEVER_STRATEGY, RequestContext, RequestContextMiddleware, RerankModel, RetrieverRegistry, RetrieverStrategy, Speech2TextChatModel, SpeechToTextModel, TEXT_SPLITTER_STRATEGY, TOOLSET_STRATEGY, TextEmbeddingModelManager, TextSplitterRegistry, TextSplitterStrategy, TextToSpeechModel, ToolsetRegistry, ToolsetStrategy, VECTOR_STORE_STRATEGY, VectorStoreRegistry, VectorStoreStrategy, WORKFLOW_NODE_STRATEGY, WORKFLOW_TRIGGER_STRATEGY, WorkflowNodeRegistry, WorkflowNodeStrategy, WorkflowTriggerRegistry, WorkflowTriggerStrategy, XpFileSystem, XpertServerPlugin, als, calcTokenUsage, countTokensSafe, createI18nInstance, createPluginLogger, downloadRemoteFile, getErrorMessage, getPositionList, getPositionMap, getRequestContext, isRemoteFile, loadYamlFile, mergeCredentials, mergeParentChildChunks, runWithRequestContext, sumTokenUsage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xpert-ai/plugin-sdk",
3
- "version": "3.6.3",
3
+ "version": "3.6.5",
4
4
  "license": "AGPL-3.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,12 +9,14 @@
9
9
  "bugs": {
10
10
  "url": "https://github.com/xpert-ai/xpert/issues"
11
11
  },
12
- "dependencies": {},
13
12
  "main": "./index.cjs.js",
14
13
  "module": "./index.esm.js",
15
14
  "publishConfig": {
16
15
  "access": "public"
17
16
  },
17
+ "dependencies": {
18
+ "js-tiktoken": "^1.0.21"
19
+ },
18
20
  "peerDependencies": {
19
21
  "@langchain/core": "*",
20
22
  "@metad/contracts": "*",
@@ -28,6 +30,7 @@
28
30
  "path": "*",
29
31
  "passport-jwt": "*",
30
32
  "stream": "*",
33
+ "yaml": "*",
31
34
  "zod": "*"
32
35
  }
33
36
  }
@@ -3,3 +3,5 @@ export declare class AiModelNotFoundException extends NotFoundException {
3
3
  }
4
4
  export declare class CredentialsValidateFailedError extends ForbiddenException {
5
5
  }
6
+ export declare class AIModelProviderNotFoundException extends NotFoundException {
7
+ }
@@ -6,3 +6,4 @@ export * from './types';
6
6
  export * from './llm';
7
7
  export * from './errors';
8
8
  export * from './openai-compatible';
9
+ export * from './utils/index';
@@ -0,0 +1 @@
1
+ export * from './tokenizer';
@@ -0,0 +1,10 @@
1
+ import type { TiktokenEncoding } from 'js-tiktoken';
2
+ /**
3
+ * Count tokens in text
4
+ * 1) Preferred: js-tiktoken precise encoding
5
+ * 2) Fallback: estimated token count
6
+ */
7
+ export declare function countTokensSafe(text: string, opts?: {
8
+ model?: string;
9
+ encodingName?: TiktokenEncoding | string;
10
+ }): number;
@@ -1,7 +1,10 @@
1
1
  /// <reference types="node" />
2
+ import { IUser } from '@metad/contracts';
2
3
  import { NestMiddleware } from '@nestjs/common';
3
4
  import { IncomingMessage, ServerResponse } from 'node:http';
4
5
  export declare class RequestContextMiddleware implements NestMiddleware {
5
6
  use(req: IncomingMessage, _res: ServerResponse, next: () => void): void;
6
7
  }
7
- export declare function runWithRequestContext(req: IncomingMessage, res: ServerResponse, next: () => void): void;
8
+ export declare function runWithRequestContext(req: Partial<IncomingMessage> & {
9
+ user?: IUser;
10
+ }, res: Partial<ServerResponse>, next: () => void): void;
@@ -11,8 +11,16 @@ export declare abstract class AdapterDataSourceStrategy<TOptions extends Adapter
11
11
  abstract readonly name: string;
12
12
  readonly description?: string;
13
13
  private configurationSchemaCache;
14
+ private runners;
14
15
  protected constructor(runnerClass: DBQueryRunnerType, extraArgs?: unknown[]);
15
- create(options: TOptions): Promise<DBQueryRunner>;
16
+ /**
17
+ * Get from cache or create new instance of DB adapter.
18
+ *
19
+ * @param options
20
+ * @param id
21
+ * @returns
22
+ */
23
+ create(options: TOptions, id?: string): Promise<DBQueryRunner>;
16
24
  configurationSchema(): Promise<Record<string, unknown>>;
17
25
  teardown(runner: DBQueryRunner): Promise<void>;
18
26
  protected instantiateRunner(options: TOptions | undefined): DBQueryRunner;
@@ -6,7 +6,7 @@ export interface IDataSourceStrategy<TOptions extends AdapterBaseOptions = Adapt
6
6
  /**
7
7
  * Create a query runner for the given data source options.
8
8
  */
9
- create(options: TOptions): Promise<DBQueryRunner> | DBQueryRunner;
9
+ create(options: TOptions, id?: string): Promise<DBQueryRunner> | DBQueryRunner;
10
10
  /**
11
11
  * Optional configuration schema description for UI generation.
12
12
  */
@@ -36,6 +36,7 @@ export declare enum DBProtocolEnum {
36
36
  export interface QueryOptions {
37
37
  catalog?: string;
38
38
  headers?: Record<string, string>;
39
+ params?: Record<string, any>[];
39
40
  }
40
41
  export interface QueryResult<T = unknown> {
41
42
  status: 'OK' | 'ERROR';
@@ -59,6 +60,7 @@ export interface DBQueryRunner {
59
60
  jdbcDriver: string;
60
61
  configurationSchema: Record<string, unknown>;
61
62
  jdbcUrl(schema?: string): string;
63
+ initPool?(options: AdapterBaseOptions): Promise<void>;
62
64
  /**
63
65
  * Execute a sql query
64
66
  *
@@ -116,6 +118,11 @@ export interface DBQueryRunner {
116
118
  * @param options
117
119
  */
118
120
  dropTable(name: string, options?: QueryOptions): Promise<void>;
121
+ /**
122
+ * Unified table operation executor
123
+ */
124
+ tableOp(action: DBTableAction, params: DBTableOperationParams, options?: QueryOptions): Promise<any>;
125
+ tableDataOp<T = any>(action: DBTableDataAction, params: DBTableDataParams, options?: QueryOptions): Promise<T[] | number | any>;
119
126
  /**
120
127
  * Teardown all resources:
121
128
  * - close connection
@@ -135,12 +142,22 @@ export interface ColumnDef {
135
142
  fieldName: string;
136
143
  /**
137
144
  * Object value type, convert to db type
145
+ * - string
146
+ * - number
147
+ * - boolean
148
+ * - date
149
+ * - datetime
150
+ * - object
138
151
  */
139
152
  type: string;
140
153
  /**
141
154
  * Is primary key column
142
155
  */
143
156
  isKey: boolean;
157
+ /**
158
+ * Is required column
159
+ */
160
+ required?: boolean;
144
161
  /**
145
162
  * length of type for column: varchar, decimal ...
146
163
  */
@@ -190,6 +207,8 @@ export declare abstract class BaseQueryRunner<T extends AdapterBaseOptions = Ada
190
207
  catalog?: string;
191
208
  }): Promise<void>;
192
209
  dropTable(name: string, options?: any): Promise<void>;
210
+ tableOp(action: DBTableAction, params: DBTableOperationParams, options?: QueryOptions): Promise<any>;
211
+ tableDataOp(action: DBTableDataAction, params: DBTableDataParams, options?: QueryOptions): Promise<any>;
193
212
  abstract teardown(): Promise<void>;
194
213
  }
195
214
  export interface HttpAdapterOptions extends AdapterBaseOptions {
@@ -253,3 +272,63 @@ export interface File {
253
272
  /** `MemoryStorage` only: A Buffer containing the entire file. */
254
273
  buffer: Buffer;
255
274
  }
275
+ export declare enum DBTableAction {
276
+ LIST_TABLES = "listTables",
277
+ TABLE_EXISTS = "tableExists",
278
+ CREATE_TABLE = "createTable",
279
+ DROP_TABLE = "dropTable",
280
+ RENAME_TABLE = "renameTable",
281
+ TRUNCATE_TABLE = "truncateTable",
282
+ ADD_COLUMN = "addColumn",
283
+ DROP_COLUMN = "dropColumn",
284
+ MODIFY_COLUMN = "modifyColumn",
285
+ CREATE_INDEX = "createIndex",
286
+ DROP_INDEX = "dropIndex",
287
+ GET_TABLE_INFO = "getTableInfo",
288
+ CLONE_TABLE_STRUCTURE = "cloneTableStructure",
289
+ CLONE_TABLE = "cloneTable",
290
+ OPTIMIZE_TABLE = "optimizeTable"
291
+ }
292
+ export interface DBTableOperationParams {
293
+ schema?: string;
294
+ table?: string;
295
+ newTable?: string;
296
+ columns?: ColumnDef[];
297
+ column?: ColumnDef;
298
+ columnName?: string;
299
+ index?: DBIndexDefinition;
300
+ indexName?: string;
301
+ createMode?: DBCreateTableMode;
302
+ }
303
+ export interface DBIndexDefinition {
304
+ name: string;
305
+ columns: string[];
306
+ unique?: boolean;
307
+ type?: 'btree' | 'hash' | 'gin' | 'bitmap' | 'fulltext' | string;
308
+ }
309
+ export declare enum DBCreateTableMode {
310
+ ERROR = "error",// 表已存在 → 报错
311
+ IGNORE = "ignore",// 表已存在 → 什么也不做
312
+ UPGRADE = "upgrade"
313
+ }
314
+ export declare enum DBTableDataAction {
315
+ INSERT = "insert",
316
+ UPDATE = "update",
317
+ UPSERT = "upsert",
318
+ DELETE = "delete",
319
+ SELECT = "select",
320
+ BULK_INSERT = "bulkInsert"
321
+ }
322
+ export interface DBTableDataParams {
323
+ schema?: string;
324
+ table: string;
325
+ columns?: Partial<ColumnDef>[];
326
+ where?: Record<string, any> | string;
327
+ orderBy?: string;
328
+ limit?: number;
329
+ offset?: number;
330
+ values?: Record<string, any> | Array<Record<string, any>>;
331
+ set?: Record<string, any>;
332
+ deleteWhere?: Record<string, any> | string;
333
+ conflictKeys?: string[];
334
+ }