@ty_krystal/sei-ai 0.1.8 → 0.1.12
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 +1 -21
- package/dist/commands/api-docs.d.ts +1 -1
- package/dist/commands/api-docs.js +1 -1
- package/dist/commands/call.d.ts +1 -1
- package/dist/commands/call.js +1 -1
- package/dist/commands/dict/add-category.d.ts +1 -1
- package/dist/commands/dict/add-category.js +1 -1
- package/dist/commands/dict/add-item.d.ts +1 -1
- package/dist/commands/dict/add-item.js +1 -1
- package/dist/commands/dict/list.d.ts +1 -1
- package/dist/commands/dict/list.js +1 -1
- package/dist/commands/init/base-data.d.ts +1 -1
- package/dist/commands/init/base-data.js +1 -1
- package/dist/commands/query-sql.d.ts +1 -1
- package/dist/commands/query-sql.js +1 -1
- package/dist/commands/query.d.ts +1 -1
- package/dist/commands/query.js +1 -1
- package/dist/commands/relogin.d.ts +1 -1
- package/dist/commands/relogin.js +1 -1
- package/dist/commands/save.d.ts +1 -1
- package/dist/commands/save.js +1 -1
- package/dist/commands.d.ts +24 -0
- package/dist/commands.js +23 -0
- package/dist/core/cli-actions.d.ts +94 -0
- package/dist/core/cli-actions.js +155 -0
- package/dist/core/cli-helpers.d.ts +39 -0
- package/dist/core/cli-helpers.js +246 -0
- package/dist/core/command-base/context.d.ts +6 -0
- package/dist/core/command-base/context.js +10 -0
- package/dist/core/command-base/output.d.ts +2 -0
- package/dist/core/command-base/output.js +6 -0
- package/dist/core/command-base/payload.d.ts +7 -0
- package/dist/core/command-base/payload.js +33 -0
- package/dist/core/command-base/sei-command.d.ts +42 -0
- package/dist/core/command-base/sei-command.js +88 -0
- package/dist/core/config.d.ts +3 -0
- package/dist/core/config.js +82 -0
- package/dist/core/constants.d.ts +35 -0
- package/dist/core/constants.js +48 -0
- package/dist/core/env.d.ts +1 -0
- package/dist/core/env.js +55 -0
- package/dist/core/errors.d.ts +10 -0
- package/dist/core/errors.js +55 -0
- package/dist/core/index.d.ts +14 -0
- package/dist/core/index.js +14 -0
- package/dist/core/logger.d.ts +2 -0
- package/dist/core/logger.js +71 -0
- package/dist/core/openapi.d.ts +2 -0
- package/dist/core/openapi.js +261 -0
- package/dist/core/sei-client.d.ts +25 -0
- package/dist/core/sei-client.js +535 -0
- package/dist/core/types.d.ts +139 -0
- package/dist/core/types.js +1 -0
- package/dist/core/utils.d.ts +12 -0
- package/dist/core/utils.js +53 -0
- package/oclif.manifest.json +376 -328
- package/package.json +14 -8
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { BASE_SEED_DEFAULT_ADMIN_PASSWORD, BASE_SEED_DEFAULT_ADMIN_ROLE, BASE_SEED_DEFAULT_ADMIN_UID, BASE_SEED_DEFAULT_SYSID, DICT_DEFAULT_SYSID, DICT_FIELDS, DICT_MODULE, } from './constants.js';
|
|
3
|
+
import { SeiMcpError } from './errors.js';
|
|
4
|
+
export function buildDictQueryPayload(typeCode, keyword, size = 1000) {
|
|
5
|
+
return {
|
|
6
|
+
head: {
|
|
7
|
+
module: DICT_MODULE,
|
|
8
|
+
},
|
|
9
|
+
option: {
|
|
10
|
+
keyField: true,
|
|
11
|
+
privilege: true,
|
|
12
|
+
fields: DICT_FIELDS.join(','),
|
|
13
|
+
filter: buildDictQueryFilter(typeCode, keyword),
|
|
14
|
+
order: '_SORT ASC,_CODE ASC',
|
|
15
|
+
page: 1,
|
|
16
|
+
size,
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function buildDictSavePayload(row) {
|
|
21
|
+
return {
|
|
22
|
+
head: {
|
|
23
|
+
module: DICT_MODULE,
|
|
24
|
+
},
|
|
25
|
+
data: [
|
|
26
|
+
{
|
|
27
|
+
action: 'add',
|
|
28
|
+
option: {
|
|
29
|
+
keyVal: true,
|
|
30
|
+
},
|
|
31
|
+
rows: [
|
|
32
|
+
{
|
|
33
|
+
row,
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function buildDictCategoryRow(input) {
|
|
41
|
+
const typeCode = requiredText(input.typeCode, '字典类别');
|
|
42
|
+
const name = requiredText(input.name, '字典名称');
|
|
43
|
+
const uuid = requiredText(input.uuid || typeCode, '字典编号');
|
|
44
|
+
const code = requiredText(input.code || typeCode, '字典代码');
|
|
45
|
+
return {
|
|
46
|
+
_UUID: uuid,
|
|
47
|
+
_PID: '',
|
|
48
|
+
_SYSID: requiredText(input.sysid || DICT_DEFAULT_SYSID, '所属系统'),
|
|
49
|
+
_TYPE: typeCode,
|
|
50
|
+
_CTYPE: normalizeText(input.ctype),
|
|
51
|
+
_CODE: code,
|
|
52
|
+
_NAME: name,
|
|
53
|
+
_ENAME: normalizeText(input.ename),
|
|
54
|
+
_DICT: normalizeDictFlag(input.dictFlag),
|
|
55
|
+
_SORT: normalizeSort(input.sort),
|
|
56
|
+
_BODY: normalizeText(input.body),
|
|
57
|
+
_MEMO: normalizeText(input.memo),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export function buildDictItemRow(input) {
|
|
61
|
+
const typeCode = requiredText(input.typeCode, '字典类别');
|
|
62
|
+
const code = requiredText(input.code, '字典代码');
|
|
63
|
+
const name = requiredText(input.name, '字典名称');
|
|
64
|
+
const parent = requiredText(input.parent || typeCode, '父级编号');
|
|
65
|
+
const uuid = requiredText(input.uuid || `${parent}_${code}`, '字典编号');
|
|
66
|
+
return {
|
|
67
|
+
_UUID: uuid,
|
|
68
|
+
_PID: parent,
|
|
69
|
+
_SYSID: requiredText(input.sysid || DICT_DEFAULT_SYSID, '所属系统'),
|
|
70
|
+
_TYPE: typeCode,
|
|
71
|
+
_CTYPE: normalizeText(input.ctype),
|
|
72
|
+
_CODE: code,
|
|
73
|
+
_NAME: name,
|
|
74
|
+
_ENAME: normalizeText(input.ename),
|
|
75
|
+
_DICT: normalizeDictFlag(input.dictFlag),
|
|
76
|
+
_SORT: normalizeSort(input.sort),
|
|
77
|
+
_BODY: normalizeText(input.body),
|
|
78
|
+
_MEMO: normalizeText(input.memo),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function buildDictTree(rows) {
|
|
82
|
+
if (!Array.isArray(rows)) {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
const recordMap = new Map();
|
|
86
|
+
const childrenMap = new Map();
|
|
87
|
+
const roots = [];
|
|
88
|
+
for (const row of rows) {
|
|
89
|
+
if (!isObject(row)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const key = normalizeText(row._UUID);
|
|
93
|
+
if (!key) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
recordMap.set(key, { ...row });
|
|
97
|
+
}
|
|
98
|
+
for (const record of recordMap.values()) {
|
|
99
|
+
const parentId = normalizeText(record._PID);
|
|
100
|
+
if (!parentId || !recordMap.has(parentId)) {
|
|
101
|
+
roots.push(record);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const children = childrenMap.get(parentId) ?? [];
|
|
105
|
+
children.push(record);
|
|
106
|
+
childrenMap.set(parentId, children);
|
|
107
|
+
}
|
|
108
|
+
for (const [key, record] of recordMap.entries()) {
|
|
109
|
+
const children = childrenMap.get(key);
|
|
110
|
+
if (children && children.length > 0) {
|
|
111
|
+
record.children = children.sort(dictSortKey);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return roots.sort(dictSortKey);
|
|
115
|
+
}
|
|
116
|
+
export function buildBaseSeedSqlVars(input) {
|
|
117
|
+
const adminPassword = input.adminPassword || BASE_SEED_DEFAULT_ADMIN_PASSWORD;
|
|
118
|
+
const passwordHash = createHash('md5').update(adminPassword, 'utf8').digest('hex');
|
|
119
|
+
const adminUid = input.adminUid || BASE_SEED_DEFAULT_ADMIN_UID;
|
|
120
|
+
const adminRole = input.adminRole || BASE_SEED_DEFAULT_ADMIN_ROLE;
|
|
121
|
+
const resetSql = input.resetAdminPassword === true
|
|
122
|
+
? `UPDATE _SYS_USER\nSET _PWD = ${sqlLiteral(passwordHash)}\nWHERE _UID = ${sqlLiteral(adminUid)};`
|
|
123
|
+
: '';
|
|
124
|
+
return {
|
|
125
|
+
SYSID: sqlLiteral(input.sysid || BASE_SEED_DEFAULT_SYSID),
|
|
126
|
+
ADMIN_UID: sqlLiteral(adminUid),
|
|
127
|
+
ADMIN_NAME: sqlLiteral(input.adminName || '管理员'),
|
|
128
|
+
ADMIN_ROLE: sqlLiteral(adminRole),
|
|
129
|
+
ADMIN_ROLE_NAME: sqlLiteral(input.adminRoleName || '管理员'),
|
|
130
|
+
ADMIN_PASSWORD_MD5: sqlLiteral(passwordHash),
|
|
131
|
+
RESET_ADMIN_PASSWORD_SQL: resetSql,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
export function renderBaseSeedSql(template, values) {
|
|
135
|
+
let rendered = template;
|
|
136
|
+
for (const [key, value] of Object.entries(values)) {
|
|
137
|
+
rendered = rendered.replaceAll(`{{${key}}}`, value);
|
|
138
|
+
}
|
|
139
|
+
const missing = [...rendered.matchAll(/\{\{[A-Z0-9_]+\}\}/g)].map((item) => item[0]);
|
|
140
|
+
if (missing.length > 0) {
|
|
141
|
+
throw new SeiMcpError(`初始化 SQL 存在未替换变量:${[...new Set(missing)].join(', ')}`, 'INVALID_PARAMS');
|
|
142
|
+
}
|
|
143
|
+
return rendered;
|
|
144
|
+
}
|
|
145
|
+
export function removeSqlCommentLines(sql) {
|
|
146
|
+
return sql
|
|
147
|
+
.split('\n')
|
|
148
|
+
.filter((line) => !line.trimStart().startsWith('--'))
|
|
149
|
+
.join('\n');
|
|
150
|
+
}
|
|
151
|
+
export function splitSqlStatements(sql) {
|
|
152
|
+
const statements = [];
|
|
153
|
+
let current = '';
|
|
154
|
+
let quote = null;
|
|
155
|
+
let escaped = false;
|
|
156
|
+
for (const char of sql) {
|
|
157
|
+
if (escaped) {
|
|
158
|
+
current += char;
|
|
159
|
+
escaped = false;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (char === '\\' && quote !== null) {
|
|
163
|
+
current += char;
|
|
164
|
+
escaped = true;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (quote !== null) {
|
|
168
|
+
current += char;
|
|
169
|
+
if (char === quote) {
|
|
170
|
+
quote = null;
|
|
171
|
+
}
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (char === '\'' || char === '"' || char === '`') {
|
|
175
|
+
current += char;
|
|
176
|
+
quote = char;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (char === ';') {
|
|
180
|
+
const statement = current.trim();
|
|
181
|
+
if (statement) {
|
|
182
|
+
statements.push(statement);
|
|
183
|
+
}
|
|
184
|
+
current = '';
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
current += char;
|
|
188
|
+
}
|
|
189
|
+
const finalStatement = current.trim();
|
|
190
|
+
if (finalStatement) {
|
|
191
|
+
statements.push(finalStatement);
|
|
192
|
+
}
|
|
193
|
+
return statements;
|
|
194
|
+
}
|
|
195
|
+
function buildDictQueryFilter(typeCode, keyword) {
|
|
196
|
+
const filters = [];
|
|
197
|
+
const normalizedType = normalizeText(typeCode);
|
|
198
|
+
const normalizedKeyword = normalizeText(keyword);
|
|
199
|
+
if (normalizedType) {
|
|
200
|
+
filters.push({ _TYPE: { '=': normalizedType } });
|
|
201
|
+
}
|
|
202
|
+
if (normalizedKeyword) {
|
|
203
|
+
filters.push({
|
|
204
|
+
OR: [
|
|
205
|
+
{ _UUID: { '*LIKE*': normalizedKeyword } },
|
|
206
|
+
{ _TYPE: { '*LIKE*': normalizedKeyword } },
|
|
207
|
+
{ _CODE: { '*LIKE*': normalizedKeyword } },
|
|
208
|
+
{ _NAME: { '*LIKE*': normalizedKeyword } },
|
|
209
|
+
],
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return filters;
|
|
213
|
+
}
|
|
214
|
+
function dictSortKey(a, b) {
|
|
215
|
+
const sortA = normalizeSort(typeof a._SORT === 'number' ? a._SORT : Number(a._SORT ?? 0));
|
|
216
|
+
const sortB = normalizeSort(typeof b._SORT === 'number' ? b._SORT : Number(b._SORT ?? 0));
|
|
217
|
+
if (sortA !== sortB) {
|
|
218
|
+
return sortA - sortB;
|
|
219
|
+
}
|
|
220
|
+
return normalizeText(a._CODE).localeCompare(normalizeText(b._CODE), 'zh-CN');
|
|
221
|
+
}
|
|
222
|
+
function normalizeDictFlag(value) {
|
|
223
|
+
return value === 1 ? 1 : 0;
|
|
224
|
+
}
|
|
225
|
+
function normalizeSort(value) {
|
|
226
|
+
return Number.isInteger(value) && Number(value) >= 0 ? Number(value) : 100;
|
|
227
|
+
}
|
|
228
|
+
function requiredText(value, label) {
|
|
229
|
+
const normalized = normalizeText(value);
|
|
230
|
+
if (!normalized) {
|
|
231
|
+
throw new SeiMcpError(`${label} 不能为空`, 'INVALID_PARAMS');
|
|
232
|
+
}
|
|
233
|
+
return normalized;
|
|
234
|
+
}
|
|
235
|
+
function normalizeText(value) {
|
|
236
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
237
|
+
}
|
|
238
|
+
function sqlLiteral(value) {
|
|
239
|
+
if (value === null || value === undefined) {
|
|
240
|
+
return 'NULL';
|
|
241
|
+
}
|
|
242
|
+
return `'${String(value).replace(/'/g, "''")}'`;
|
|
243
|
+
}
|
|
244
|
+
function isObject(value) {
|
|
245
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
246
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { loadConfig } from '../config.js';
|
|
2
|
+
import { DEFAULT_LOG_LEVEL } from '../constants.js';
|
|
3
|
+
import { loadWorkingDirEnv } from '../env.js';
|
|
4
|
+
import { createLogger } from '../logger.js';
|
|
5
|
+
export async function loadCliContext(argv = process.argv.slice(2), env = process.env) {
|
|
6
|
+
await loadWorkingDirEnv();
|
|
7
|
+
const logger = createLogger(env.SEI_MCP_LOG_LEVEL ?? DEFAULT_LOG_LEVEL);
|
|
8
|
+
const config = await loadConfig(argv, env);
|
|
9
|
+
return { config, logger };
|
|
10
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { SeiMcpError } from '../errors.js';
|
|
3
|
+
export async function loadPayload(options) {
|
|
4
|
+
const sources = [options.stdin, options.jsonText !== undefined, options.filePath !== undefined].filter(Boolean).length;
|
|
5
|
+
if (sources > 1) {
|
|
6
|
+
throw new SeiMcpError('只能选择一种载荷来源:--json、--file 或 --stdin', 'INVALID_PARAMS');
|
|
7
|
+
}
|
|
8
|
+
if (options.jsonText !== undefined) {
|
|
9
|
+
return parseJson(options.jsonText);
|
|
10
|
+
}
|
|
11
|
+
if (options.filePath !== undefined) {
|
|
12
|
+
return parseJson(await readFile(options.filePath, 'utf8'));
|
|
13
|
+
}
|
|
14
|
+
if (options.stdin) {
|
|
15
|
+
return parseJson(await readStdin());
|
|
16
|
+
}
|
|
17
|
+
return options.fallback;
|
|
18
|
+
}
|
|
19
|
+
export function parseJson(text) {
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(text);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
throw new SeiMcpError(`载荷不是合法 JSON:${error instanceof Error ? error.message : String(error)}`, 'INVALID_PARAMS');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function readStdin() {
|
|
28
|
+
const chunks = [];
|
|
29
|
+
for await (const chunk of process.stdin) {
|
|
30
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
31
|
+
}
|
|
32
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
33
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
import type { CliSeiClient } from '../sei-client.js';
|
|
3
|
+
import type { Logger, SeiMcpConfig } from '../types.js';
|
|
4
|
+
export declare abstract class SeiCommand extends Command {
|
|
5
|
+
static baseFlags: {
|
|
6
|
+
config: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
7
|
+
'base-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
'ai-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
role: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
account: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
'account-project': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
'account-checkcode': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
token: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
timeout: import("@oclif/core/interfaces").OptionFlag<number | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
15
|
+
};
|
|
16
|
+
protected logger: Logger;
|
|
17
|
+
protected configData: SeiMcpConfig;
|
|
18
|
+
protected init(): Promise<void>;
|
|
19
|
+
protected resolveConfig(): Promise<SeiMcpConfig>;
|
|
20
|
+
protected createCliClient(options?: Partial<SeiAuthSettingsOptions>): Promise<CliSeiClient>;
|
|
21
|
+
protected loadPayload(input: {
|
|
22
|
+
fallback?: unknown;
|
|
23
|
+
filePath?: string;
|
|
24
|
+
jsonText?: string;
|
|
25
|
+
stdin?: boolean;
|
|
26
|
+
}): Promise<unknown>;
|
|
27
|
+
protected printJson(value: unknown): void;
|
|
28
|
+
protected writeText(text: string): void;
|
|
29
|
+
protected createAuthOptions(flags: Record<string, unknown>): SeiAuthSettingsOptions;
|
|
30
|
+
protected createClientFromFlags(flags: Record<string, unknown>): Promise<CliSeiClient>;
|
|
31
|
+
protected catch(error: unknown): Promise<unknown>;
|
|
32
|
+
}
|
|
33
|
+
export interface SeiAuthSettingsOptions {
|
|
34
|
+
baseUrl?: string;
|
|
35
|
+
token?: string;
|
|
36
|
+
aiKey?: string;
|
|
37
|
+
role?: string;
|
|
38
|
+
account?: string;
|
|
39
|
+
accountProject?: string;
|
|
40
|
+
accountCheckcode?: string;
|
|
41
|
+
timeoutMs?: number;
|
|
42
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Command, Flags } from '@oclif/core';
|
|
2
|
+
import { loadConfig } from '../config.js';
|
|
3
|
+
import { DEFAULT_LOG_LEVEL } from '../constants.js';
|
|
4
|
+
import { formatCliError, toErrorLogMeta } from '../errors.js';
|
|
5
|
+
import { loadWorkingDirEnv } from '../env.js';
|
|
6
|
+
import { createLogger } from '../logger.js';
|
|
7
|
+
import { createCliSeiClient, createSeiAuthSettings } from '../sei-client.js';
|
|
8
|
+
import { loadPayload } from './payload.js';
|
|
9
|
+
import { printJson, writeText } from './output.js';
|
|
10
|
+
export class SeiCommand extends Command {
|
|
11
|
+
static baseFlags = {
|
|
12
|
+
config: Flags.string({ description: '配置文件路径' }),
|
|
13
|
+
'base-url': Flags.string({ description: 'SEI 基础地址' }),
|
|
14
|
+
'ai-key': Flags.string({ description: 'AI 登录密钥' }),
|
|
15
|
+
role: Flags.string({ aliases: ['profile'], description: 'AI 登录角色' }),
|
|
16
|
+
account: Flags.string({ description: 'AI 登录账号' }),
|
|
17
|
+
'account-project': Flags.string({ description: 'AI 登录项目' }),
|
|
18
|
+
'account-checkcode': Flags.string({ description: 'AI 登录验证码' }),
|
|
19
|
+
token: Flags.string({ description: '固定 token' }),
|
|
20
|
+
timeout: Flags.integer({ description: '请求超时(毫秒)' }),
|
|
21
|
+
};
|
|
22
|
+
logger;
|
|
23
|
+
configData;
|
|
24
|
+
async init() {
|
|
25
|
+
await loadWorkingDirEnv();
|
|
26
|
+
this.logger = createLogger(process.env.SEI_MCP_LOG_LEVEL ?? DEFAULT_LOG_LEVEL);
|
|
27
|
+
this.configData = await loadConfig(this.argv, process.env);
|
|
28
|
+
await super.init();
|
|
29
|
+
}
|
|
30
|
+
async resolveConfig() {
|
|
31
|
+
return this.configData;
|
|
32
|
+
}
|
|
33
|
+
async createCliClient(options = {}) {
|
|
34
|
+
const config = await this.resolveConfig();
|
|
35
|
+
const settings = createSeiAuthSettings(config, {
|
|
36
|
+
baseUrl: options.baseUrl,
|
|
37
|
+
token: options.token,
|
|
38
|
+
aiKey: options.aiKey,
|
|
39
|
+
role: options.role,
|
|
40
|
+
account: options.account,
|
|
41
|
+
accountProject: options.accountProject,
|
|
42
|
+
accountCheckcode: options.accountCheckcode,
|
|
43
|
+
timeoutMs: options.timeoutMs,
|
|
44
|
+
});
|
|
45
|
+
return createCliSeiClient(settings, this.logger, globalThis.fetch);
|
|
46
|
+
}
|
|
47
|
+
async loadPayload(input) {
|
|
48
|
+
return loadPayload(input);
|
|
49
|
+
}
|
|
50
|
+
printJson(value) {
|
|
51
|
+
printJson(value);
|
|
52
|
+
}
|
|
53
|
+
writeText(text) {
|
|
54
|
+
writeText(text);
|
|
55
|
+
}
|
|
56
|
+
createAuthOptions(flags) {
|
|
57
|
+
return {
|
|
58
|
+
baseUrl: stringFlag(flags, 'base-url'),
|
|
59
|
+
token: stringFlag(flags, 'token'),
|
|
60
|
+
aiKey: stringFlag(flags, 'ai-key'),
|
|
61
|
+
role: stringFlag(flags, 'role') ?? stringFlag(flags, 'profile'),
|
|
62
|
+
account: stringFlag(flags, 'account'),
|
|
63
|
+
accountProject: stringFlag(flags, 'account-project'),
|
|
64
|
+
accountCheckcode: stringFlag(flags, 'account-checkcode'),
|
|
65
|
+
timeoutMs: numberFlag(flags, 'timeout'),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async createClientFromFlags(flags) {
|
|
69
|
+
return this.createCliClient(this.createAuthOptions(flags));
|
|
70
|
+
}
|
|
71
|
+
async catch(error) {
|
|
72
|
+
this.logger?.error('CLI command failed', {
|
|
73
|
+
argv: this.argv,
|
|
74
|
+
error: toErrorLogMeta(error),
|
|
75
|
+
});
|
|
76
|
+
process.stderr.write(`${formatCliError(error)}\n`);
|
|
77
|
+
process.exitCode = 1;
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function stringFlag(flags, name) {
|
|
82
|
+
const value = flags[name];
|
|
83
|
+
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
84
|
+
}
|
|
85
|
+
function numberFlag(flags, name) {
|
|
86
|
+
const value = flags[name];
|
|
87
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
88
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { DEFAULT_BASE_URL } from './constants.js';
|
|
3
|
+
import { SeiMcpError } from './errors.js';
|
|
4
|
+
import { isObject, toTrimmedString } from './utils.js';
|
|
5
|
+
export async function loadConfig(argv = process.argv.slice(2), env = process.env) {
|
|
6
|
+
const configPath = resolveArg(argv, '--config') ?? (toTrimmedString(env.SEI_AI_MCP_CONFIG) || null);
|
|
7
|
+
const fileConfig = configPath ? await readConfigFile(configPath) : {};
|
|
8
|
+
return normalizeConfig(fileConfig, env);
|
|
9
|
+
}
|
|
10
|
+
export function normalizeConfig(input = {}, env = process.env) {
|
|
11
|
+
const config = isObject(input) ? input : {};
|
|
12
|
+
const auth = isObject(config.auth) ? config.auth : {};
|
|
13
|
+
const tools = isObject(config.tools) ? config.tools : {};
|
|
14
|
+
const targets = isObject(config.targets) ? config.targets : {};
|
|
15
|
+
return {
|
|
16
|
+
baseUrl: normalizeBaseUrl(config.baseUrl, env.SEI_BASE_URL),
|
|
17
|
+
auth: {
|
|
18
|
+
mode: normalizeMode(auth.mode, env.SEI_AI_MCP_AUTH_MODE),
|
|
19
|
+
token: stringOrEnv(auth.token, env.SEI_TOKEN),
|
|
20
|
+
tokenEnv: stringOrEnv(auth.tokenEnv, env.SEI_TOKEN ? 'SEI_TOKEN' : 'SEI_TOKEN'),
|
|
21
|
+
aiKey: stringOrEnv(auth.aiKey, env.SEI_AI_LOGIN_KEY),
|
|
22
|
+
aiKeyEnv: stringOrEnv(auth.aiKeyEnv, env.SEI_AI_LOGIN_KEY ? 'SEI_AI_LOGIN_KEY' : 'SEI_AI_LOGIN_KEY'),
|
|
23
|
+
account: stringOrEnv(auth.account, env.SEI_AI_LOGIN_ACCOUNT),
|
|
24
|
+
accountEnv: stringOrEnv(auth.accountEnv, env.SEI_AI_LOGIN_ACCOUNT ? 'SEI_AI_LOGIN_ACCOUNT' : 'SEI_AI_LOGIN_ACCOUNT'),
|
|
25
|
+
role: stringOrEnv(auth.role, env.SEI_AI_LOGIN_ROLE),
|
|
26
|
+
roleEnv: stringOrEnv(auth.roleEnv, env.SEI_AI_LOGIN_ROLE ? 'SEI_AI_LOGIN_ROLE' : 'SEI_AI_LOGIN_ROLE'),
|
|
27
|
+
},
|
|
28
|
+
tools: {
|
|
29
|
+
query: tools.query !== false,
|
|
30
|
+
save: tools.save !== false,
|
|
31
|
+
schema: tools.schema !== false,
|
|
32
|
+
querySql: tools.querySql === true,
|
|
33
|
+
},
|
|
34
|
+
targets: {
|
|
35
|
+
modules: toTargetMap(targets.modules),
|
|
36
|
+
tables: toTargetMap(targets.tables),
|
|
37
|
+
sources: toTargetMap(targets.sources),
|
|
38
|
+
views: toTargetMap(targets.views),
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function readConfigFile(path) {
|
|
43
|
+
try {
|
|
44
|
+
const text = await readFile(path, 'utf8');
|
|
45
|
+
return JSON.parse(text);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new SeiMcpError(`无法读取配置文件: ${path}`, 'INVALID_REQUEST', {
|
|
49
|
+
path,
|
|
50
|
+
cause: error instanceof Error ? error.message : String(error),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function resolveArg(argv, name) {
|
|
55
|
+
const index = argv.indexOf(name);
|
|
56
|
+
if (index === -1) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
const next = argv[index + 1];
|
|
60
|
+
return next && !next.startsWith('-') ? next : null;
|
|
61
|
+
}
|
|
62
|
+
function normalizeBaseUrl(configValue, envValue) {
|
|
63
|
+
const raw = stringOrEnv(configValue, envValue);
|
|
64
|
+
return raw || DEFAULT_BASE_URL;
|
|
65
|
+
}
|
|
66
|
+
function normalizeMode(configValue, envValue) {
|
|
67
|
+
const raw = stringOrEnv(configValue, envValue).toLowerCase();
|
|
68
|
+
return raw === 'token' ? 'token' : 'ai-login';
|
|
69
|
+
}
|
|
70
|
+
function stringOrEnv(value, envValue) {
|
|
71
|
+
const candidate = toTrimmedString(value);
|
|
72
|
+
if (candidate) {
|
|
73
|
+
return candidate;
|
|
74
|
+
}
|
|
75
|
+
return toTrimmedString(envValue);
|
|
76
|
+
}
|
|
77
|
+
function toTargetMap(value) {
|
|
78
|
+
if (!isObject(value)) {
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
return Object.fromEntries(Object.entries(value).filter(([, item]) => isObject(item)));
|
|
82
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare const PACKAGE_NAME = "sei-ai";
|
|
2
|
+
export declare const BIN_NAME = "sei-ai";
|
|
3
|
+
export declare const SERVER_NAME = "sei-ai-mcp-server";
|
|
4
|
+
export declare const SERVER_VERSION = "0.2.0";
|
|
5
|
+
export declare const DEFAULT_BASE_URL = "http://127.0.0.1:8081";
|
|
6
|
+
export declare const DEFAULT_AI_KEY = "dev-ai-secret";
|
|
7
|
+
export declare const DEFAULT_TIMEOUT_MS = 30000;
|
|
8
|
+
export declare const DEFAULT_LOG_LEVEL = "info";
|
|
9
|
+
export declare const DEFAULT_QUERY_LIMIT = 20;
|
|
10
|
+
export declare const DEFAULT_QUERY_SQL_PAGE = 1;
|
|
11
|
+
export declare const DEFAULT_QUERY_SQL_SIZE = 20;
|
|
12
|
+
export declare const MAX_RESPONSE_TEXT = 20000;
|
|
13
|
+
export declare const MAX_LOG_TEXT = 2000;
|
|
14
|
+
export declare const LOGIN_PATH = "/api/sei/public/login";
|
|
15
|
+
export declare const ACCOUNT_CHECK_CODE_PATH = "/api/sei/public/checkCode";
|
|
16
|
+
export declare const QUERY_PATH = "/api/sei/data/public/query";
|
|
17
|
+
export declare const SAVE_PATH = "/api/sei/data/public/save";
|
|
18
|
+
export declare const QUERY_SQL_PATH = "/api/sei/data/querySQL";
|
|
19
|
+
export declare const DDL_EXECUTE_PATH = "/api/sei/mcp/ddl/execute";
|
|
20
|
+
export declare const OPENAPI_DOCS_PATH = "/v3/api-docs";
|
|
21
|
+
export declare const DICT_MODULE = "sys_dic";
|
|
22
|
+
export declare const DICT_DEFAULT_SYSID = "sys";
|
|
23
|
+
export declare const DICT_FIELDS: readonly ["_UUID", "_PID", "_SYSID", "_TYPE", "_CTYPE", "_CODE", "_NAME", "_ENAME", "_DICT", "_SORT", "_BODY", "_MEMO"];
|
|
24
|
+
export declare const BASE_SEED_DEFAULT_SYSID = "sys";
|
|
25
|
+
export declare const BASE_SEED_DEFAULT_ADMIN_UID = "admin";
|
|
26
|
+
export declare const BASE_SEED_DEFAULT_ADMIN_ROLE = "admin";
|
|
27
|
+
export declare const BASE_SEED_DEFAULT_ADMIN_PASSWORD = "123456";
|
|
28
|
+
export declare const ACCOUNT_LOGIN_DEFAULT_PROJECT = "sys";
|
|
29
|
+
export declare const ACCOUNT_LOGIN_DEFAULT_CHECKCODE = "abcd";
|
|
30
|
+
export declare const ACCOUNT_LOGIN_DEFAULT_TYPE = "login";
|
|
31
|
+
export declare const ACCOUNT_LOGIN_DEFAULT_OS = 0;
|
|
32
|
+
export declare const TOKEN_CACHE_DIR_NAME = "sei-ai";
|
|
33
|
+
export declare const TOKEN_CACHE_FILE_PREFIX = "token-";
|
|
34
|
+
export declare const TOKEN_CACHE_FILE_SUFFIX = ".json";
|
|
35
|
+
export declare const TOKEN_CACHE_VERSION = 1;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export const PACKAGE_NAME = 'sei-ai';
|
|
2
|
+
export const BIN_NAME = 'sei-ai';
|
|
3
|
+
export const SERVER_NAME = 'sei-ai-mcp-server';
|
|
4
|
+
export const SERVER_VERSION = '0.2.0';
|
|
5
|
+
export const DEFAULT_BASE_URL = 'http://127.0.0.1:8081';
|
|
6
|
+
export const DEFAULT_AI_KEY = 'dev-ai-secret';
|
|
7
|
+
export const DEFAULT_TIMEOUT_MS = 30_000;
|
|
8
|
+
export const DEFAULT_LOG_LEVEL = 'info';
|
|
9
|
+
export const DEFAULT_QUERY_LIMIT = 20;
|
|
10
|
+
export const DEFAULT_QUERY_SQL_PAGE = 1;
|
|
11
|
+
export const DEFAULT_QUERY_SQL_SIZE = 20;
|
|
12
|
+
export const MAX_RESPONSE_TEXT = 20_000;
|
|
13
|
+
export const MAX_LOG_TEXT = 2_000;
|
|
14
|
+
export const LOGIN_PATH = '/api/sei/public/login';
|
|
15
|
+
export const ACCOUNT_CHECK_CODE_PATH = '/api/sei/public/checkCode';
|
|
16
|
+
export const QUERY_PATH = '/api/sei/data/public/query';
|
|
17
|
+
export const SAVE_PATH = '/api/sei/data/public/save';
|
|
18
|
+
export const QUERY_SQL_PATH = '/api/sei/data/querySQL';
|
|
19
|
+
export const DDL_EXECUTE_PATH = '/api/sei/mcp/ddl/execute';
|
|
20
|
+
export const OPENAPI_DOCS_PATH = '/v3/api-docs';
|
|
21
|
+
export const DICT_MODULE = 'sys_dic';
|
|
22
|
+
export const DICT_DEFAULT_SYSID = 'sys';
|
|
23
|
+
export const DICT_FIELDS = [
|
|
24
|
+
'_UUID',
|
|
25
|
+
'_PID',
|
|
26
|
+
'_SYSID',
|
|
27
|
+
'_TYPE',
|
|
28
|
+
'_CTYPE',
|
|
29
|
+
'_CODE',
|
|
30
|
+
'_NAME',
|
|
31
|
+
'_ENAME',
|
|
32
|
+
'_DICT',
|
|
33
|
+
'_SORT',
|
|
34
|
+
'_BODY',
|
|
35
|
+
'_MEMO',
|
|
36
|
+
];
|
|
37
|
+
export const BASE_SEED_DEFAULT_SYSID = 'sys';
|
|
38
|
+
export const BASE_SEED_DEFAULT_ADMIN_UID = 'admin';
|
|
39
|
+
export const BASE_SEED_DEFAULT_ADMIN_ROLE = 'admin';
|
|
40
|
+
export const BASE_SEED_DEFAULT_ADMIN_PASSWORD = '123456';
|
|
41
|
+
export const ACCOUNT_LOGIN_DEFAULT_PROJECT = 'sys';
|
|
42
|
+
export const ACCOUNT_LOGIN_DEFAULT_CHECKCODE = 'abcd';
|
|
43
|
+
export const ACCOUNT_LOGIN_DEFAULT_TYPE = 'login';
|
|
44
|
+
export const ACCOUNT_LOGIN_DEFAULT_OS = 0;
|
|
45
|
+
export const TOKEN_CACHE_DIR_NAME = 'sei-ai';
|
|
46
|
+
export const TOKEN_CACHE_FILE_PREFIX = 'token-';
|
|
47
|
+
export const TOKEN_CACHE_FILE_SUFFIX = '.json';
|
|
48
|
+
export const TOKEN_CACHE_VERSION = 1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function loadWorkingDirEnv(cwd?: string, env?: NodeJS.ProcessEnv): Promise<void>;
|