@caoguo/maplibre-ai 0.0.2 → 0.0.4

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.
@@ -0,0 +1,160 @@
1
+ import { DeepSeekClient } from '../llm/index.cjs';
2
+
3
+ /**
4
+ * NLPG 自然语言 → PostGIS SQL 生成(PRD phase-0 §5.7 N-1/N-2/N-3)
5
+ *
6
+ * 支持:
7
+ * - 属性过滤(字段 + 操作符 + 值,AND/OR 组合)
8
+ * - 空间关系(ST_DWithin / ST_Within / ST_Intersects / ST_Contains / ST_Buffer)
9
+ * - 混合查询(空间 + 属性)
10
+ *
11
+ * v1 采用「字段词典 + 规则匹配」方案(不依赖大模型),SQL 生成后必须经
12
+ * `validateSql` 安全校验层通过方可执行(见 sqlValidator.ts)。
13
+ */
14
+ type NlpgIntent = 'attribute_filter' | 'spatial_nearby' | 'spatial_within' | 'mixed' | 'unknown';
15
+ type Operator = '>' | '<' | '>=' | '<=' | '=' | '!=' | 'LIKE';
16
+ interface AttributeCondition {
17
+ /** 字段名(白名单字段) */
18
+ field: string;
19
+ operator: Operator;
20
+ /** 字面量值 */
21
+ value: string | number;
22
+ }
23
+ interface SpatialCondition {
24
+ /** 空间关系 */
25
+ relation: 'dwithin' | 'within' | 'intersects' | 'contains' | 'buffer';
26
+ /** 参考点 [lng, lat](dwithin/buffer 用) */
27
+ point?: [number, number];
28
+ /** 距离(米,dwithin 用) */
29
+ radius?: number;
30
+ /** 目标几何字段 */
31
+ geometryColumn: string;
32
+ }
33
+ interface GeneratedQuery {
34
+ intent: NlpgIntent;
35
+ /** 目标表(白名单表) */
36
+ table: string;
37
+ /** 属性条件 */
38
+ conditions: AttributeCondition[];
39
+ /** 空间条件 */
40
+ spatial: SpatialCondition | null;
41
+ /** 生成的 SQL */
42
+ sql: string;
43
+ /** 置信度 0-1 */
44
+ confidence: number;
45
+ }
46
+ declare function detectTable(text: string): string;
47
+ declare function detectField(text: string): string | null;
48
+ declare function detectValue(text: string, field: string): string | number | null;
49
+ declare function detectSpatial(text: string, geometryColumn?: string): SpatialCondition | null;
50
+ interface GenerateOptions {
51
+ /** 参考点(空间查询用,默认武汉中心) */
52
+ center?: [number, number];
53
+ /** 目标几何字段 */
54
+ geometryColumn?: string;
55
+ }
56
+ declare function generatePostGISQuery(text: string, opts?: GenerateOptions): GeneratedQuery;
57
+
58
+ /**
59
+ * NLPG SQL 安全校验层(PRD phase-0 §5.7 N-5 / §5.7.3)
60
+ *
61
+ * 所有 LLM/规则生成的 SQL 必须经过以下校验方可执行:
62
+ * 1. 语法校验(引号/括号配对、非空)
63
+ * 2. 白名单表检查(只允许查询授权表)
64
+ * 3. 危险操作拦截(DROP/DELETE/UPDATE/INSERT/ALTER/TRUNCATE/GRANT 等全部拒绝)
65
+ * 4. 仅允许 SELECT 只读查询
66
+ * 5. 参数化占位(防止 SQL 注入的字面量转义)
67
+ *
68
+ * 纯函数,可在 Node/浏览器两侧运行。
69
+ */
70
+ interface ValidationIssue {
71
+ severity: 'error' | 'warning';
72
+ rule: string;
73
+ message: string;
74
+ }
75
+ interface ValidationResult {
76
+ /** 是否通过(无 error 级问题) */
77
+ valid: boolean;
78
+ issues: ValidationIssue[];
79
+ }
80
+ /** 默认白名单表(只读授权) */
81
+ declare const DEFAULT_ALLOWED_TABLES: string[];
82
+ /**
83
+ * 校验 SQL。
84
+ * @param sql 待校验 SQL
85
+ * @param allowedTables 白名单表(默认 DEFAULT_ALLOWED_TABLES)
86
+ */
87
+ declare function validateSql(sql: string, allowedTables?: string[]): ValidationResult;
88
+ /** 参数化:把 SQL 中的字符串字面量替换为 $n 占位符(防注入) */
89
+ declare function parameterize(sql: string): {
90
+ sql: string;
91
+ params: string[];
92
+ };
93
+
94
+ /**
95
+ * NLPG v1 主入口(PRD phase-0 §5.7)
96
+ *
97
+ * 自然语言 → PostGIS SQL → 安全校验 → 可执行结果。
98
+ *
99
+ * 用法:
100
+ * const result = nlpgQuery('查出使用超过 20 年的铸铁燃气管');
101
+ * result.sql // 生成的 SQL
102
+ * result.valid // 是否通过安全校验
103
+ * result.validation // 校验详情
104
+ */
105
+
106
+ interface NlpgResult {
107
+ /** 生成的查询 */
108
+ query: GeneratedQuery;
109
+ /** 是否通过安全校验 */
110
+ valid: boolean;
111
+ /** 校验详情 */
112
+ validation: ValidationResult;
113
+ /** 参数化后的 SQL($n 占位符) */
114
+ parameterized?: {
115
+ sql: string;
116
+ params: string[];
117
+ };
118
+ }
119
+ /**
120
+ * 自然语言查询入口。
121
+ */
122
+ declare function nlpgQuery(text: string, opts?: GenerateOptions): NlpgResult;
123
+
124
+ /**
125
+ * NLPG LLM 模式(PRD phase-0 §5.7 N-6「LLM 生成 SQL」)
126
+ *
127
+ * 在规则引擎(sqlGenerator.ts)基础上,接入 DeepSeek 实现:
128
+ * - 复杂自然语言 → PostGIS SQL(突破固定字段/空间模板)
129
+ * - LLM 优先,失败自动降级规则引擎
130
+ * - 生成的 SQL 必经 sqlValidator 安全校验层方可执行
131
+ */
132
+
133
+ interface LlmNlpgConfig {
134
+ client: DeepSeekClient;
135
+ enabled?: boolean;
136
+ /** 授权表白名单(传给校验层) */
137
+ allowedTables?: string[];
138
+ }
139
+ declare class LlmNlpg {
140
+ private client;
141
+ private enabled;
142
+ private allowedTables;
143
+ constructor(config: LlmNlpgConfig);
144
+ /**
145
+ * 自然语言查询。
146
+ * LLM 优先,失败或校验不过时降级到规则引擎。
147
+ */
148
+ query(text: string): Promise<{
149
+ query: GeneratedQuery;
150
+ valid: boolean;
151
+ parameterized?: {
152
+ sql: string;
153
+ params: string[];
154
+ };
155
+ }>;
156
+ private queryWithLlm;
157
+ private queryWithRules;
158
+ }
159
+
160
+ export { type AttributeCondition, DEFAULT_ALLOWED_TABLES, type GenerateOptions, type GeneratedQuery, LlmNlpg, type LlmNlpgConfig, type NlpgIntent, type NlpgResult, type Operator, type SpatialCondition, type ValidationIssue, type ValidationResult, detectField, detectSpatial, detectTable, detectValue, generatePostGISQuery, nlpgQuery, parameterize, validateSql };
@@ -0,0 +1,160 @@
1
+ import { DeepSeekClient } from '../llm/index.js';
2
+
3
+ /**
4
+ * NLPG 自然语言 → PostGIS SQL 生成(PRD phase-0 §5.7 N-1/N-2/N-3)
5
+ *
6
+ * 支持:
7
+ * - 属性过滤(字段 + 操作符 + 值,AND/OR 组合)
8
+ * - 空间关系(ST_DWithin / ST_Within / ST_Intersects / ST_Contains / ST_Buffer)
9
+ * - 混合查询(空间 + 属性)
10
+ *
11
+ * v1 采用「字段词典 + 规则匹配」方案(不依赖大模型),SQL 生成后必须经
12
+ * `validateSql` 安全校验层通过方可执行(见 sqlValidator.ts)。
13
+ */
14
+ type NlpgIntent = 'attribute_filter' | 'spatial_nearby' | 'spatial_within' | 'mixed' | 'unknown';
15
+ type Operator = '>' | '<' | '>=' | '<=' | '=' | '!=' | 'LIKE';
16
+ interface AttributeCondition {
17
+ /** 字段名(白名单字段) */
18
+ field: string;
19
+ operator: Operator;
20
+ /** 字面量值 */
21
+ value: string | number;
22
+ }
23
+ interface SpatialCondition {
24
+ /** 空间关系 */
25
+ relation: 'dwithin' | 'within' | 'intersects' | 'contains' | 'buffer';
26
+ /** 参考点 [lng, lat](dwithin/buffer 用) */
27
+ point?: [number, number];
28
+ /** 距离(米,dwithin 用) */
29
+ radius?: number;
30
+ /** 目标几何字段 */
31
+ geometryColumn: string;
32
+ }
33
+ interface GeneratedQuery {
34
+ intent: NlpgIntent;
35
+ /** 目标表(白名单表) */
36
+ table: string;
37
+ /** 属性条件 */
38
+ conditions: AttributeCondition[];
39
+ /** 空间条件 */
40
+ spatial: SpatialCondition | null;
41
+ /** 生成的 SQL */
42
+ sql: string;
43
+ /** 置信度 0-1 */
44
+ confidence: number;
45
+ }
46
+ declare function detectTable(text: string): string;
47
+ declare function detectField(text: string): string | null;
48
+ declare function detectValue(text: string, field: string): string | number | null;
49
+ declare function detectSpatial(text: string, geometryColumn?: string): SpatialCondition | null;
50
+ interface GenerateOptions {
51
+ /** 参考点(空间查询用,默认武汉中心) */
52
+ center?: [number, number];
53
+ /** 目标几何字段 */
54
+ geometryColumn?: string;
55
+ }
56
+ declare function generatePostGISQuery(text: string, opts?: GenerateOptions): GeneratedQuery;
57
+
58
+ /**
59
+ * NLPG SQL 安全校验层(PRD phase-0 §5.7 N-5 / §5.7.3)
60
+ *
61
+ * 所有 LLM/规则生成的 SQL 必须经过以下校验方可执行:
62
+ * 1. 语法校验(引号/括号配对、非空)
63
+ * 2. 白名单表检查(只允许查询授权表)
64
+ * 3. 危险操作拦截(DROP/DELETE/UPDATE/INSERT/ALTER/TRUNCATE/GRANT 等全部拒绝)
65
+ * 4. 仅允许 SELECT 只读查询
66
+ * 5. 参数化占位(防止 SQL 注入的字面量转义)
67
+ *
68
+ * 纯函数,可在 Node/浏览器两侧运行。
69
+ */
70
+ interface ValidationIssue {
71
+ severity: 'error' | 'warning';
72
+ rule: string;
73
+ message: string;
74
+ }
75
+ interface ValidationResult {
76
+ /** 是否通过(无 error 级问题) */
77
+ valid: boolean;
78
+ issues: ValidationIssue[];
79
+ }
80
+ /** 默认白名单表(只读授权) */
81
+ declare const DEFAULT_ALLOWED_TABLES: string[];
82
+ /**
83
+ * 校验 SQL。
84
+ * @param sql 待校验 SQL
85
+ * @param allowedTables 白名单表(默认 DEFAULT_ALLOWED_TABLES)
86
+ */
87
+ declare function validateSql(sql: string, allowedTables?: string[]): ValidationResult;
88
+ /** 参数化:把 SQL 中的字符串字面量替换为 $n 占位符(防注入) */
89
+ declare function parameterize(sql: string): {
90
+ sql: string;
91
+ params: string[];
92
+ };
93
+
94
+ /**
95
+ * NLPG v1 主入口(PRD phase-0 §5.7)
96
+ *
97
+ * 自然语言 → PostGIS SQL → 安全校验 → 可执行结果。
98
+ *
99
+ * 用法:
100
+ * const result = nlpgQuery('查出使用超过 20 年的铸铁燃气管');
101
+ * result.sql // 生成的 SQL
102
+ * result.valid // 是否通过安全校验
103
+ * result.validation // 校验详情
104
+ */
105
+
106
+ interface NlpgResult {
107
+ /** 生成的查询 */
108
+ query: GeneratedQuery;
109
+ /** 是否通过安全校验 */
110
+ valid: boolean;
111
+ /** 校验详情 */
112
+ validation: ValidationResult;
113
+ /** 参数化后的 SQL($n 占位符) */
114
+ parameterized?: {
115
+ sql: string;
116
+ params: string[];
117
+ };
118
+ }
119
+ /**
120
+ * 自然语言查询入口。
121
+ */
122
+ declare function nlpgQuery(text: string, opts?: GenerateOptions): NlpgResult;
123
+
124
+ /**
125
+ * NLPG LLM 模式(PRD phase-0 §5.7 N-6「LLM 生成 SQL」)
126
+ *
127
+ * 在规则引擎(sqlGenerator.ts)基础上,接入 DeepSeek 实现:
128
+ * - 复杂自然语言 → PostGIS SQL(突破固定字段/空间模板)
129
+ * - LLM 优先,失败自动降级规则引擎
130
+ * - 生成的 SQL 必经 sqlValidator 安全校验层方可执行
131
+ */
132
+
133
+ interface LlmNlpgConfig {
134
+ client: DeepSeekClient;
135
+ enabled?: boolean;
136
+ /** 授权表白名单(传给校验层) */
137
+ allowedTables?: string[];
138
+ }
139
+ declare class LlmNlpg {
140
+ private client;
141
+ private enabled;
142
+ private allowedTables;
143
+ constructor(config: LlmNlpgConfig);
144
+ /**
145
+ * 自然语言查询。
146
+ * LLM 优先,失败或校验不过时降级到规则引擎。
147
+ */
148
+ query(text: string): Promise<{
149
+ query: GeneratedQuery;
150
+ valid: boolean;
151
+ parameterized?: {
152
+ sql: string;
153
+ params: string[];
154
+ };
155
+ }>;
156
+ private queryWithLlm;
157
+ private queryWithRules;
158
+ }
159
+
160
+ export { type AttributeCondition, DEFAULT_ALLOWED_TABLES, type GenerateOptions, type GeneratedQuery, LlmNlpg, type LlmNlpgConfig, type NlpgIntent, type NlpgResult, type Operator, type SpatialCondition, type ValidationIssue, type ValidationResult, detectField, detectSpatial, detectTable, detectValue, generatePostGISQuery, nlpgQuery, parameterize, validateSql };
@@ -0,0 +1,3 @@
1
+ export { DEFAULT_ALLOWED_TABLES, LlmNlpg, detectField, detectSpatial, detectTable, detectValue, generatePostGISQuery, nlpgQuery, parameterize, validateSql } from '../chunk-ARYB2X4B.js';
2
+ //# sourceMappingURL=index.js.map
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@caoguo/maplibre-ai",
3
- "version": "0.0.2",
4
- "description": "草果地图 AI 工具链 - 性能诊断 / 样式生成器 v2",
3
+ "version": "0.0.4",
4
+ "description": "草果地图 AI 工具链 - MapCopilot 代码生成 / GeoAI 数据入图 / NLPG 自然语言查询 / DeepSeek LLM 接入 / 性能诊断 / 样式生成器 v2",
5
5
  "license": "Apache-2.0",
6
6
  "author": "caoguo <caoguo@hb.cn>",
7
7
  "homepage": "https://github.com/caoguo/map/blob/main/packages/ai/README.md",
@@ -18,12 +18,24 @@
18
18
  "caoguo",
19
19
  "gis",
20
20
  "ai",
21
+ "copilot",
22
+ "code-generation",
23
+ "geoai",
24
+ "geocoding",
25
+ "address-parsing",
26
+ "crs-conversion",
27
+ "nlpg",
28
+ "natural-language",
29
+ "postgis",
30
+ "sql-generator",
31
+ "sql-validator",
21
32
  "performance-profiler",
22
33
  "tile-monitor",
23
34
  "memory-leak",
24
35
  "style-generator",
25
36
  "palette-extraction",
26
- "industry-template"
37
+ "industry-template",
38
+ "china"
27
39
  ],
28
40
  "type": "module",
29
41
  "main": "./dist/index.cjs",
@@ -44,13 +56,33 @@
44
56
  "types": "./dist/stylegen/index.d.ts",
45
57
  "import": "./dist/stylegen/index.js",
46
58
  "require": "./dist/stylegen/index.cjs"
59
+ },
60
+ "./copilot": {
61
+ "types": "./dist/copilot/index.d.ts",
62
+ "import": "./dist/copilot/index.js",
63
+ "require": "./dist/copilot/index.cjs"
64
+ },
65
+ "./geoai": {
66
+ "types": "./dist/geoai/index.d.ts",
67
+ "import": "./dist/geoai/index.js",
68
+ "require": "./dist/geoai/index.cjs"
69
+ },
70
+ "./nlpg": {
71
+ "types": "./dist/nlpg/index.d.ts",
72
+ "import": "./dist/nlpg/index.js",
73
+ "require": "./dist/nlpg/index.cjs"
74
+ },
75
+ "./llm": {
76
+ "types": "./dist/llm/index.d.ts",
77
+ "import": "./dist/llm/index.js",
78
+ "require": "./dist/llm/index.cjs"
47
79
  }
48
80
  },
49
81
  "files": [
50
82
  "dist"
51
83
  ],
52
84
  "dependencies": {
53
- "@caoguo/theme": "0.0.2"
85
+ "@caoguo/theme": "0.0.4"
54
86
  },
55
87
  "publishConfig": {
56
88
  "access": "public",