@gi-base/ai 0.0.0-stage → 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+ Copyright (c) 2026-present GI Base
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,20 @@
1
- # Temporary Holding Version
1
+ # @gi-base/ai
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ GI UI 的 AI 语义运行时,提供目标注册表、命令执行器、策略校验和引导引擎。
4
+
5
+ ```ts
6
+ import { createAiRuntime } from '@gi-base/ai'
7
+
8
+ const { registry, executor, guide } = createAiRuntime({
9
+ policy: { destructive: 'confirm' }
10
+ })
11
+ ```
12
+
13
+ ## 能力
14
+
15
+ - `AiTargetRegistryImpl`:按语义码解析组件目标,支持 read / set / click / command / focus。
16
+ - `UiExecutor`:执行 AI 动作并统一处理策略、确认和结果。
17
+ - `GuideEngine`:按步骤驱动引导流程。
18
+ - `policy`:区分只读、写入和破坏性动作,破坏性动作可要求二次确认。
19
+
20
+ 组件通过 `@gi-base/utils` 的 `useAiTarget` 注册,未挂载运行时不会产生副作用。
@@ -0,0 +1,34 @@
1
+ import { AiTargetRegistryImpl } from './registry';
2
+ import { AiAction, AiCommandResult, AiContext, AiPolicy } from './types';
3
+ export interface UiExecutorOptions {
4
+ registry: AiTargetRegistryImpl;
5
+ context?: AiContext;
6
+ policy?: AiPolicy;
7
+ onResult?: (action: AiAction, result: AiCommandResult) => void;
8
+ }
9
+ export declare class UiExecutor {
10
+ private registry;
11
+ private context;
12
+ private policy;
13
+ private onResult?;
14
+ constructor(options: UiExecutorOptions);
15
+ getContext(): {
16
+ appId?: string;
17
+ page?: string;
18
+ route?: string;
19
+ locale?: string;
20
+ user?: Record<string, unknown>;
21
+ metadata?: Record<string, unknown>;
22
+ };
23
+ setContext(patch: Partial<AiContext>): void;
24
+ setPolicy(policy: AiPolicy): void;
25
+ getPolicy(): {
26
+ destructive?: "confirm" | "deny";
27
+ readonly?: boolean;
28
+ allowedActions?: import('./types').AiActionType[];
29
+ requireConfirmation?: import('./types').AiActionType[];
30
+ };
31
+ run(action: AiAction): Promise<AiCommandResult>;
32
+ private runUnsafe;
33
+ private recordsOf;
34
+ }
@@ -0,0 +1,16 @@
1
+ import { AiGuidePlan, AiGuideState } from './types';
2
+ type Listener = (state: AiGuideState) => void;
3
+ export declare class GuideEngine {
4
+ private state;
5
+ private listeners;
6
+ getState(): AiGuideState;
7
+ subscribe(listener: Listener): () => boolean;
8
+ start(plan: AiGuidePlan): void;
9
+ next(): AiGuideState;
10
+ prev(): AiGuideState;
11
+ goto(index: number): AiGuideState;
12
+ stop(): void;
13
+ reset(): void;
14
+ private emit;
15
+ }
16
+ export {};
@@ -0,0 +1,18 @@
1
+ import { GuideEngine } from './guide';
2
+ import { UiExecutor } from './executor';
3
+ import { AiTargetRegistryImpl } from './registry';
4
+ import { AiContext, AiPolicy } from './types';
5
+ export interface AiRuntimeOptions {
6
+ context?: AiContext;
7
+ policy?: AiPolicy;
8
+ }
9
+ export declare function createAiRuntime(options?: AiRuntimeOptions): {
10
+ registry: AiTargetRegistryImpl;
11
+ executor: UiExecutor;
12
+ guide: GuideEngine;
13
+ };
14
+ export * from './types';
15
+ export { AiTargetRegistryImpl } from './registry';
16
+ export { UiExecutor } from './executor';
17
+ export { GuideEngine } from './guide';
18
+ export { evaluatePolicy, canRun, normalizePolicy, isDestructiveAction } from './policy';
package/dist/index.mjs ADDED
@@ -0,0 +1,236 @@
1
+ class g {
2
+ constructor() {
3
+ this.state = {
4
+ plan: null,
5
+ index: -1,
6
+ running: !1,
7
+ current: null,
8
+ progress: 0
9
+ }, this.listeners = /* @__PURE__ */ new Set();
10
+ }
11
+ getState() {
12
+ return { ...this.state };
13
+ }
14
+ subscribe(e) {
15
+ return this.listeners.add(e), e(this.getState()), () => this.listeners.delete(e);
16
+ }
17
+ start(e) {
18
+ this.state = {
19
+ plan: e,
20
+ index: e.steps.length ? 0 : -1,
21
+ running: e.steps.length > 0,
22
+ current: e.steps[0] || null,
23
+ progress: e.steps.length ? 1 / e.steps.length : 0
24
+ }, this.emit();
25
+ }
26
+ next() {
27
+ return this.state.plan ? (this.goto(this.state.index + 1), this.getState()) : this.getState();
28
+ }
29
+ prev() {
30
+ return this.state.plan ? (this.goto(this.state.index - 1), this.getState()) : this.getState();
31
+ }
32
+ goto(e) {
33
+ const t = this.state.plan;
34
+ if (!t) return this.getState();
35
+ const r = Math.min(Math.max(e, 0), t.steps.length - 1);
36
+ return this.state = {
37
+ ...this.state,
38
+ index: r,
39
+ running: !0,
40
+ current: t.steps[r] || null,
41
+ progress: t.steps.length ? (r + 1) / t.steps.length : 0
42
+ }, this.emit(), this.getState();
43
+ }
44
+ stop() {
45
+ this.state = {
46
+ ...this.state,
47
+ running: !1,
48
+ current: null
49
+ }, this.emit();
50
+ }
51
+ reset() {
52
+ this.state = {
53
+ plan: null,
54
+ index: -1,
55
+ running: !1,
56
+ current: null,
57
+ progress: 0
58
+ }, this.emit();
59
+ }
60
+ emit() {
61
+ const e = this.getState();
62
+ this.listeners.forEach((t) => t(e));
63
+ }
64
+ }
65
+ const y = ["delete", "remove", "batchDelete", "clear"];
66
+ function p(s) {
67
+ return s.type === "command" ? y.includes(String(s.command)) : !1;
68
+ }
69
+ function f(s, e = {}) {
70
+ var t, r;
71
+ if (e.readonly && s.type !== "read" && s.type !== "focus")
72
+ return { ok: !1, reason: "readonly" };
73
+ if ((t = e.allowedActions) != null && t.length && !e.allowedActions.includes(s.type))
74
+ return { ok: !1, reason: "policy" };
75
+ if (p(s)) {
76
+ if (e.destructive === "deny") return { ok: !1, reason: "policy" };
77
+ if (e.destructive === "confirm" && !s.confirmed)
78
+ return { ok: !1, reason: "confirmRequired" };
79
+ }
80
+ return (r = e.requireConfirmation) != null && r.includes(s.type) && !s.confirmed ? { ok: !1, reason: "confirmRequired" } : { ok: !0 };
81
+ }
82
+ function h(s = {}) {
83
+ return {
84
+ destructive: s.destructive || "confirm",
85
+ readonly: !!s.readonly,
86
+ ...s
87
+ };
88
+ }
89
+ function S(s, e = {}) {
90
+ return f({ type: s }, e).ok;
91
+ }
92
+ class m {
93
+ constructor(e) {
94
+ this.registry = e.registry, this.context = e.context || {}, this.policy = h(e.policy), this.onResult = e.onResult;
95
+ }
96
+ getContext() {
97
+ return { ...this.context };
98
+ }
99
+ setContext(e) {
100
+ this.context = { ...this.context, ...e };
101
+ }
102
+ setPolicy(e) {
103
+ this.policy = h(e);
104
+ }
105
+ getPolicy() {
106
+ return { ...this.policy };
107
+ }
108
+ async run(e) {
109
+ var r;
110
+ const t = await this.runUnsafe(e);
111
+ return (r = this.onResult) == null || r.call(this, e, t), t;
112
+ }
113
+ async runUnsafe(e) {
114
+ var n, i, o, l, d, c;
115
+ if (!e || !e.type) return a("invalidAction");
116
+ const t = f(e, this.policy);
117
+ if (!t.ok)
118
+ return {
119
+ ok: !1,
120
+ reason: t.reason,
121
+ ...t.reason === "confirmRequired" ? { records: this.recordsOf(e) } : {}
122
+ };
123
+ if (!e.target) return a("invalidAction");
124
+ const r = this.registry.resolve(e.target);
125
+ if (!r) return a("notFound");
126
+ if (r.handle.isEnabled && !r.handle.isEnabled()) return a("disabled");
127
+ try {
128
+ if (e.type === "read")
129
+ return { ok: !0, data: (i = (n = r.handle).getValue) == null ? void 0 : i.call(n) };
130
+ if (e.type === "focus")
131
+ return (l = (o = r.handle).focus) == null || l.call(o), { ok: !0 };
132
+ if (e.type === "set")
133
+ return r.handle.setValue ? (await r.handle.setValue(e.value), { ok: !0, data: (c = (d = r.handle).getValue) == null ? void 0 : c.call(d) }) : a("unsupported");
134
+ if (e.type === "click")
135
+ return r.handle.trigger ? (r.handle.trigger(e.command), { ok: !0 }) : a("unsupported");
136
+ if (e.type === "command") {
137
+ if (!r.handle.execute) return a("unsupported");
138
+ const u = await r.handle.execute(
139
+ String(e.command || ""),
140
+ e.payload
141
+ );
142
+ return x(u) ? {
143
+ ok: !1,
144
+ reason: u.reason || "failed",
145
+ message: String(u.message || ""),
146
+ data: u,
147
+ ...Array.isArray(u.records) ? { records: u.records } : {}
148
+ } : { ok: !0, data: u };
149
+ }
150
+ return a("invalidAction");
151
+ } catch (u) {
152
+ return {
153
+ ok: !1,
154
+ reason: "failed",
155
+ message: u instanceof Error ? u.message : String(u)
156
+ };
157
+ }
158
+ }
159
+ recordsOf(e) {
160
+ var n, i, o, l, d;
161
+ if (!e.target) return [];
162
+ const t = this.registry.resolve(e.target);
163
+ if (!t) return [];
164
+ const r = ((i = (n = t.handle).getRecords) == null ? void 0 : i.call(n, e)) || ((d = (l = (o = t.handle).getValue) == null ? void 0 : l.call(o)) == null ? void 0 : d.records);
165
+ return Array.isArray(r) ? r : [];
166
+ }
167
+ }
168
+ function a(s) {
169
+ return { ok: !1, reason: s };
170
+ }
171
+ function x(s) {
172
+ return !!(s && typeof s == "object" && "ok" in s && s.ok === !1);
173
+ }
174
+ let k = 0;
175
+ class v {
176
+ constructor() {
177
+ this.records = /* @__PURE__ */ new Map(), this.handleIds = /* @__PURE__ */ new WeakMap();
178
+ }
179
+ register(e) {
180
+ const t = this.handleIds.get(e) || `ai-target-${++k}`;
181
+ this.handleIds.set(e, t);
182
+ const r = String(e.code || e.field || t);
183
+ return this.records.set(t, {
184
+ id: t,
185
+ code: r,
186
+ entity: e.entity,
187
+ field: e.field,
188
+ handle: e
189
+ }), t;
190
+ }
191
+ unregister(e) {
192
+ const t = this.handleIds.get(e);
193
+ t && (this.records.delete(t), this.handleIds.delete(e));
194
+ }
195
+ list() {
196
+ return [...this.records.values()];
197
+ }
198
+ resolve(e) {
199
+ const t = this.list().filter((r) => r.code === e);
200
+ return t.length ? t.find(
201
+ (r) => {
202
+ var n, i, o, l;
203
+ return (((i = (n = r.handle).isEnabled) == null ? void 0 : i.call(n)) ?? !0) && (((l = (o = r.handle).isVisible) == null ? void 0 : l.call(o)) ?? !0);
204
+ }
205
+ ) || t.find((r) => {
206
+ var n, i;
207
+ return ((i = (n = r.handle).isEnabled) == null ? void 0 : i.call(n)) ?? !0;
208
+ }) || t[0] : null;
209
+ }
210
+ read(e) {
211
+ var r, n;
212
+ const t = this.resolve(e);
213
+ return t ? { record: t, value: (n = (r = t.handle).getValue) == null ? void 0 : n.call(r) } : { record: null, value: void 0 };
214
+ }
215
+ clear() {
216
+ this.records.clear();
217
+ }
218
+ }
219
+ function A(s = {}) {
220
+ const e = new v(), t = new g(), r = new m({
221
+ registry: e,
222
+ context: s.context,
223
+ policy: s.policy
224
+ });
225
+ return { registry: e, executor: r, guide: t };
226
+ }
227
+ export {
228
+ v as AiTargetRegistryImpl,
229
+ g as GuideEngine,
230
+ m as UiExecutor,
231
+ S as canRun,
232
+ A as createAiRuntime,
233
+ f as evaluatePolicy,
234
+ p as isDestructiveAction,
235
+ h as normalizePolicy
236
+ };
@@ -0,0 +1,10 @@
1
+ import { AiAction, AiActionType, AiPolicy, AiResultReason } from './types';
2
+ export declare function isDestructiveAction(action: AiAction): boolean;
3
+ export declare function evaluatePolicy(action: AiAction, policy?: AiPolicy): {
4
+ ok: true;
5
+ } | {
6
+ ok: false;
7
+ reason: AiResultReason;
8
+ };
9
+ export declare function normalizePolicy(policy?: AiPolicy): Required<Pick<AiPolicy, 'destructive' | 'readonly'>> & AiPolicy;
10
+ export declare function canRun(actionType: AiActionType, policy?: AiPolicy): boolean;
@@ -0,0 +1,22 @@
1
+ import { AiTargetHandle, AiTargetRegistry } from '@gi-base/utils';
2
+ import { AiTargetRecord } from './types';
3
+ /**
4
+ * AI 语义目标注册表:组件通过 useAiTarget 注册,执行器按语义码寻址。
5
+ * 不依赖 DOM 选择器,同一语义码出现多个时优先可见且启用的实例。
6
+ */
7
+ export declare class AiTargetRegistryImpl implements AiTargetRegistry {
8
+ private records;
9
+ private handleIds;
10
+ register(handle: AiTargetHandle): string;
11
+ unregister(handle: AiTargetHandle): void;
12
+ list(): AiTargetRecord[];
13
+ resolve(code: string): AiTargetRecord | null;
14
+ read(code: string): {
15
+ record: null;
16
+ value: undefined;
17
+ } | {
18
+ record: AiTargetRecord;
19
+ value: unknown;
20
+ };
21
+ clear(): void;
22
+ }
@@ -0,0 +1,81 @@
1
+ import { AiTargetHandle } from '@gi-base/utils';
2
+ export type AiActionType = 'read' | 'set' | 'focus' | 'click' | 'command';
3
+ export interface AiContext {
4
+ appId?: string;
5
+ page?: string;
6
+ route?: string;
7
+ locale?: string;
8
+ user?: Record<string, unknown>;
9
+ metadata?: Record<string, unknown>;
10
+ }
11
+ export interface AiPolicy {
12
+ /** 删除类动作:必须确认或直接拒绝 */
13
+ destructive?: 'confirm' | 'deny';
14
+ /** 只读模式,禁止写入与命令执行 */
15
+ readonly?: boolean;
16
+ /** 允许的动作集合,缺省全部允许 */
17
+ allowedActions?: AiActionType[];
18
+ /** 需要二次确认的动作 */
19
+ requireConfirmation?: AiActionType[];
20
+ }
21
+ export interface AiCapability {
22
+ code: string;
23
+ label?: string;
24
+ description?: string;
25
+ actions?: AiActionType[];
26
+ entity?: string;
27
+ fields?: string[];
28
+ policy?: AiPolicy;
29
+ }
30
+ export interface AiAction {
31
+ type: AiActionType;
32
+ /** 语义目标码 */
33
+ target?: string;
34
+ value?: unknown;
35
+ /** 传给目标 execute 的命令名 */
36
+ command?: string;
37
+ payload?: Record<string, unknown>;
38
+ /** 已由上层确认 */
39
+ confirmed?: boolean;
40
+ }
41
+ export type AiResultReason = 'readonly' | 'policy' | 'confirmRequired' | 'notFound' | 'disabled' | 'unsupported' | 'invalidAction' | 'failed';
42
+ export interface AiCommandResult {
43
+ ok: boolean;
44
+ reason?: AiResultReason;
45
+ message?: string;
46
+ data?: unknown;
47
+ records?: Record<string, unknown>[];
48
+ }
49
+ export interface AiTargetRecord {
50
+ id: string;
51
+ code: string;
52
+ entity?: string;
53
+ field?: string;
54
+ handle: AiTargetHandle & {
55
+ execute?: (command: string, payload?: Record<string, unknown>) => unknown;
56
+ trigger?: (action?: string) => void;
57
+ getSchema?: () => unknown;
58
+ getRecords?: () => Record<string, unknown>[];
59
+ };
60
+ }
61
+ export interface AiGuideStep {
62
+ id: string;
63
+ title: string;
64
+ description?: string;
65
+ target?: string;
66
+ action?: AiAction;
67
+ tips?: string;
68
+ optional?: boolean;
69
+ }
70
+ export interface AiGuidePlan {
71
+ id: string;
72
+ title?: string;
73
+ steps: AiGuideStep[];
74
+ }
75
+ export interface AiGuideState {
76
+ plan: AiGuidePlan | null;
77
+ index: number;
78
+ running: boolean;
79
+ current: AiGuideStep | null;
80
+ progress: number;
81
+ }
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@gi-base/ai",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.1",
4
+ "description": "GI Base AI capability runtime: target registry, executor, policy and guide engine",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "main": "./dist/index.mjs",
11
+ "module": "./dist/index.mjs",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs"
17
+ }
18
+ },
19
+ "dependencies": {
20
+ "@gi-base/utils": "^0.0.1"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org/"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/gi-base/gi-ui.git",
29
+ "directory": "packages/ai"
30
+ },
31
+ "scripts": {
32
+ "build": "vite build",
33
+ "test": "vitest run --passWithNoTests",
34
+ "typecheck": "tsc --noEmit -p tsconfig.json"
35
+ }
6
36
  }