wu-framework 1.1.6 → 1.1.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.
- package/README.md +511 -977
- package/dist/wu-framework.cjs.js +3 -1
- package/dist/wu-framework.cjs.js.map +1 -0
- package/dist/wu-framework.dev.js +7533 -2761
- package/dist/wu-framework.dev.js.map +1 -1
- package/dist/wu-framework.esm.js +3 -0
- package/dist/wu-framework.esm.js.map +1 -0
- package/dist/wu-framework.umd.js +3 -1
- package/dist/wu-framework.umd.js.map +1 -0
- package/integrations/astro/README.md +127 -0
- package/integrations/astro/WuApp.astro +63 -0
- package/integrations/astro/WuShell.astro +39 -0
- package/integrations/astro/index.js +68 -0
- package/integrations/astro/package.json +38 -0
- package/integrations/astro/types.d.ts +53 -0
- package/package.json +94 -74
- package/src/adapters/angular/ai.js +30 -0
- package/src/adapters/angular/index.d.ts +154 -0
- package/src/adapters/angular/index.js +932 -0
- package/src/adapters/angular.d.ts +3 -154
- package/src/adapters/angular.js +3 -813
- package/src/adapters/index.js +35 -24
- package/src/adapters/lit/ai.js +20 -0
- package/src/adapters/lit/index.d.ts +120 -0
- package/src/adapters/lit/index.js +721 -0
- package/src/adapters/lit.d.ts +3 -120
- package/src/adapters/lit.js +3 -726
- package/src/adapters/preact/ai.js +33 -0
- package/src/adapters/preact/index.d.ts +108 -0
- package/src/adapters/preact/index.js +661 -0
- package/src/adapters/preact.d.ts +3 -108
- package/src/adapters/preact.js +3 -665
- package/src/adapters/react/ai.js +135 -0
- package/src/adapters/react/index.d.ts +246 -0
- package/src/adapters/react/index.js +689 -0
- package/src/adapters/react.d.ts +3 -212
- package/src/adapters/react.js +3 -513
- package/src/adapters/shared.js +64 -0
- package/src/adapters/solid/ai.js +32 -0
- package/src/adapters/solid/index.d.ts +101 -0
- package/src/adapters/solid/index.js +586 -0
- package/src/adapters/solid.d.ts +3 -101
- package/src/adapters/solid.js +3 -591
- package/src/adapters/svelte/ai.js +31 -0
- package/src/adapters/svelte/index.d.ts +166 -0
- package/src/adapters/svelte/index.js +798 -0
- package/src/adapters/svelte.d.ts +3 -166
- package/src/adapters/svelte.js +3 -803
- package/src/adapters/vanilla/ai.js +30 -0
- package/src/adapters/vanilla/index.d.ts +179 -0
- package/src/adapters/vanilla/index.js +785 -0
- package/src/adapters/vanilla.d.ts +3 -179
- package/src/adapters/vanilla.js +3 -791
- package/src/adapters/vue/ai.js +52 -0
- package/src/adapters/vue/index.d.ts +299 -0
- package/src/adapters/vue/index.js +608 -0
- package/src/adapters/vue.d.ts +3 -299
- package/src/adapters/vue.js +3 -611
- package/src/ai/wu-ai-actions.js +261 -0
- package/src/ai/wu-ai-browser.js +663 -0
- package/src/ai/wu-ai-context.js +332 -0
- package/src/ai/wu-ai-conversation.js +554 -0
- package/src/ai/wu-ai-permissions.js +381 -0
- package/src/ai/wu-ai-provider.js +605 -0
- package/src/ai/wu-ai-schema.js +225 -0
- package/src/ai/wu-ai-triggers.js +396 -0
- package/src/ai/wu-ai.js +474 -0
- package/src/core/wu-app.js +50 -8
- package/src/core/wu-cache.js +1 -1
- package/src/core/wu-core.js +645 -677
- package/src/core/wu-html-parser.js +121 -211
- package/src/core/wu-iframe-sandbox.js +328 -0
- package/src/core/wu-mcp-bridge.js +647 -0
- package/src/core/wu-overrides.js +510 -0
- package/src/core/wu-prefetch.js +414 -0
- package/src/core/wu-proxy-sandbox.js +398 -75
- package/src/core/wu-sandbox.js +86 -268
- package/src/core/wu-script-executor.js +79 -182
- package/src/core/wu-snapshot-sandbox.js +149 -106
- package/src/core/wu-strategies.js +13 -0
- package/src/core/wu-style-bridge.js +0 -2
- package/src/index.js +139 -665
- package/dist/wu-framework.hex.js +0 -23
- package/dist/wu-framework.min.js +0 -1
- package/dist/wu-framework.obf.js +0 -1
- package/scripts/build-protected.js +0 -366
- package/scripts/build.js +0 -212
- package/scripts/rollup-plugin-hex.js +0 -143
- package/src/core/wu-registry.js +0 -60
- package/src/core/wu-sandbox-pool.js +0 -390
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WU-AI-SCHEMA: Tool schema generation for function calling
|
|
3
|
+
*
|
|
4
|
+
* Converts wu.ai.action() definitions into the canonical tool format
|
|
5
|
+
* that providers consume. Also handles input sanitization for prompts.
|
|
6
|
+
*
|
|
7
|
+
* Canonical tool format:
|
|
8
|
+
* { name: string, description: string, parameters: JSONSchema }
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// ─── Sanitization ────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
const SENSITIVE_KEYS = ['password', 'token', 'apiKey', 'secret', 'credential', 'authorization', 'cookie', 'session'];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sanitize data before injecting into prompts.
|
|
17
|
+
* Prevents prompt injection and redacts sensitive fields.
|
|
18
|
+
*
|
|
19
|
+
* @param {*} data - Any value to sanitize
|
|
20
|
+
* @param {number} [maxChars=2000] - Max chars per value
|
|
21
|
+
* @returns {string} Safe string representation
|
|
22
|
+
*/
|
|
23
|
+
export function sanitizeForPrompt(data, maxChars = 2000) {
|
|
24
|
+
if (data === null || data === undefined) return 'null';
|
|
25
|
+
if (typeof data === 'function') return '[Function]';
|
|
26
|
+
if (typeof data === 'symbol') return '[Symbol]';
|
|
27
|
+
|
|
28
|
+
if (typeof data === 'string') {
|
|
29
|
+
const truncated = data.length > maxChars ? data.slice(0, maxChars) + '...[truncated]' : data;
|
|
30
|
+
return `<user_data>${truncated}</user_data>`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (typeof data === 'number' || typeof data === 'boolean') {
|
|
34
|
+
return String(data);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (typeof data === 'object') {
|
|
38
|
+
const redacted = redactSensitive(data);
|
|
39
|
+
const json = JSON.stringify(redacted);
|
|
40
|
+
if (json.length > maxChars) {
|
|
41
|
+
return `<user_data>${json.slice(0, maxChars)}...[truncated]</user_data>`;
|
|
42
|
+
}
|
|
43
|
+
return `<user_data>${json}</user_data>`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return String(data).slice(0, maxChars);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Deep-clone an object, replacing sensitive keys with [REDACTED].
|
|
51
|
+
*
|
|
52
|
+
* @param {*} obj
|
|
53
|
+
* @param {number} [depth=0]
|
|
54
|
+
* @returns {*}
|
|
55
|
+
*/
|
|
56
|
+
export function redactSensitive(obj, depth = 0) {
|
|
57
|
+
if (depth > 10) return '[MAX_DEPTH]';
|
|
58
|
+
if (obj === null || obj === undefined) return obj;
|
|
59
|
+
if (typeof obj !== 'object') return obj;
|
|
60
|
+
|
|
61
|
+
if (Array.isArray(obj)) {
|
|
62
|
+
return obj.map(item => redactSensitive(item, depth + 1));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const result = {};
|
|
66
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
67
|
+
const lowerKey = key.toLowerCase();
|
|
68
|
+
if (SENSITIVE_KEYS.some(sk => lowerKey.includes(sk.toLowerCase()))) {
|
|
69
|
+
result[key] = '[REDACTED]';
|
|
70
|
+
} else {
|
|
71
|
+
result[key] = redactSensitive(value, depth + 1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ─── Template Interpolation ──────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Interpolate {{var}} placeholders in a template string.
|
|
81
|
+
* Supports dot-notation: {{data.user.name}}
|
|
82
|
+
*
|
|
83
|
+
* @param {string} template
|
|
84
|
+
* @param {object} vars - Variable map { data: ..., context: ... }
|
|
85
|
+
* @returns {string}
|
|
86
|
+
*/
|
|
87
|
+
export function interpolate(template, vars) {
|
|
88
|
+
return template.replace(/\{\{(\w+(?:\.\w+)*)\}\}/g, (_match, path) => {
|
|
89
|
+
const value = path.split('.').reduce((obj, key) => obj?.[key], vars);
|
|
90
|
+
if (value === undefined || value === null) return '';
|
|
91
|
+
if (typeof value === 'object') return sanitizeForPrompt(value);
|
|
92
|
+
return String(value);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ─── Tool Schema Builder ─────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Build canonical tool definitions from registered actions.
|
|
100
|
+
*
|
|
101
|
+
* @param {Map<string, object>} actions - Map of action name → config
|
|
102
|
+
* @returns {Array<{ name: string, description: string, parameters: object }>}
|
|
103
|
+
*/
|
|
104
|
+
export function buildToolSchemas(actions) {
|
|
105
|
+
const tools = [];
|
|
106
|
+
|
|
107
|
+
for (const [name, config] of actions) {
|
|
108
|
+
tools.push({
|
|
109
|
+
name,
|
|
110
|
+
description: config.description || `Execute action: ${name}`,
|
|
111
|
+
parameters: normalizeParameters(config.parameters),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return tools;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Normalize user-provided parameter definitions into JSON Schema.
|
|
120
|
+
*
|
|
121
|
+
* Accepts two formats:
|
|
122
|
+
* 1. Full JSON Schema: { type: 'object', properties: {...}, required: [...] }
|
|
123
|
+
* 2. Shorthand: { message: { type: 'string', required: true }, count: { type: 'number' } }
|
|
124
|
+
*
|
|
125
|
+
* @param {object} params
|
|
126
|
+
* @returns {object} Valid JSON Schema
|
|
127
|
+
*/
|
|
128
|
+
export function normalizeParameters(params) {
|
|
129
|
+
if (!params || typeof params !== 'object') {
|
|
130
|
+
return { type: 'object', properties: {}, required: [] };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Already a JSON Schema
|
|
134
|
+
if (params.type === 'object' && params.properties) {
|
|
135
|
+
return params;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Shorthand format — convert
|
|
139
|
+
const properties = {};
|
|
140
|
+
const required = [];
|
|
141
|
+
|
|
142
|
+
for (const [key, def] of Object.entries(params)) {
|
|
143
|
+
if (typeof def === 'string') {
|
|
144
|
+
// Simplest: { message: 'string' }
|
|
145
|
+
properties[key] = { type: def };
|
|
146
|
+
} else if (typeof def === 'object') {
|
|
147
|
+
const { required: isRequired, ...rest } = def;
|
|
148
|
+
properties[key] = rest.type ? rest : { type: 'string', ...rest };
|
|
149
|
+
if (isRequired) required.push(key);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return { type: 'object', properties, required };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Validate params against a JSON Schema (lightweight, no external deps).
|
|
158
|
+
* Only checks type, required, and enum — not full JSON Schema validation.
|
|
159
|
+
*
|
|
160
|
+
* @param {object} params - Actual params from LLM
|
|
161
|
+
* @param {object} schema - JSON Schema from normalizeParameters()
|
|
162
|
+
* @returns {{ valid: boolean, errors: string[] }}
|
|
163
|
+
*/
|
|
164
|
+
export function validateParams(params, schema) {
|
|
165
|
+
const errors = [];
|
|
166
|
+
if (!schema || !schema.properties) return { valid: true, errors };
|
|
167
|
+
|
|
168
|
+
// Check required
|
|
169
|
+
for (const key of (schema.required || [])) {
|
|
170
|
+
if (params[key] === undefined || params[key] === null) {
|
|
171
|
+
errors.push(`'${key}' is required`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Check types and enums
|
|
176
|
+
for (const [key, def] of Object.entries(schema.properties)) {
|
|
177
|
+
const value = params[key];
|
|
178
|
+
if (value === undefined || value === null) continue;
|
|
179
|
+
|
|
180
|
+
if (def.type && def.type !== 'any') {
|
|
181
|
+
const actualType = Array.isArray(value) ? 'array' : typeof value;
|
|
182
|
+
if (def.type === 'integer') {
|
|
183
|
+
if (typeof value !== 'number' || !Number.isInteger(value)) {
|
|
184
|
+
errors.push(`'${key}' must be integer, got ${actualType}`);
|
|
185
|
+
}
|
|
186
|
+
} else if (def.type !== actualType) {
|
|
187
|
+
errors.push(`'${key}' must be ${def.type}, got ${actualType}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (def.enum && !def.enum.includes(value)) {
|
|
192
|
+
errors.push(`'${key}' must be one of [${def.enum.join(', ')}], got '${value}'`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return { valid: errors.length === 0, errors };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ─── Context Budget ──────────────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Estimate token count from character count.
|
|
203
|
+
* Rough heuristic: 1 token ≈ 4 chars for English, ≈ 2 chars for CJK.
|
|
204
|
+
*
|
|
205
|
+
* @param {string} text
|
|
206
|
+
* @param {number} [charRatio=4]
|
|
207
|
+
* @returns {number}
|
|
208
|
+
*/
|
|
209
|
+
export function estimateTokens(text, charRatio = 4) {
|
|
210
|
+
return Math.ceil(text.length / charRatio);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Truncate text to fit within a token budget.
|
|
215
|
+
*
|
|
216
|
+
* @param {string} text
|
|
217
|
+
* @param {number} maxTokens
|
|
218
|
+
* @param {number} [charRatio=4]
|
|
219
|
+
* @returns {string}
|
|
220
|
+
*/
|
|
221
|
+
export function truncateToTokenBudget(text, maxTokens, charRatio = 4) {
|
|
222
|
+
const maxChars = maxTokens * charRatio;
|
|
223
|
+
if (text.length <= maxChars) return text;
|
|
224
|
+
return text.slice(0, maxChars) + '\n...[truncated to fit token budget]';
|
|
225
|
+
}
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WU-AI-TRIGGERS: Event-to-AI bridge
|
|
3
|
+
*
|
|
4
|
+
* Listens to wu.eventBus events and automatically triggers LLM interactions.
|
|
5
|
+
* This is the "reactive AI" — the app becomes intelligent by responding
|
|
6
|
+
* to events with AI-generated actions.
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Pattern-based event matching (wu convention: 'cart:*', 'user:login')
|
|
10
|
+
* - Debounce per trigger (avoid flooding LLM)
|
|
11
|
+
* - Conditional execution (only trigger if condition returns true)
|
|
12
|
+
* - Priority batching (high triggers fire immediately, low ones batch)
|
|
13
|
+
* - Template interpolation ({{event.data.user}} in prompts)
|
|
14
|
+
* - Namespace isolation (each trigger uses its own conversation namespace)
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { logger } from '../core/wu-logger.js';
|
|
18
|
+
import { interpolate, sanitizeForPrompt } from './wu-ai-schema.js';
|
|
19
|
+
|
|
20
|
+
// ─── Trigger Config ──────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
const DEFAULT_TRIGGER_CONFIG = {
|
|
23
|
+
enabled: true,
|
|
24
|
+
maxActiveTriggers: 20,
|
|
25
|
+
defaultDebounceMs: 1000,
|
|
26
|
+
batchIntervalMs: 2000,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// ─── Single Trigger ──────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
class Trigger {
|
|
32
|
+
constructor(name, config) {
|
|
33
|
+
this.name = name;
|
|
34
|
+
this.pattern = config.pattern; // event pattern: 'cart:updated', 'user:*'
|
|
35
|
+
this.prompt = config.prompt; // string or function(eventData) → string
|
|
36
|
+
this.condition = config.condition || null; // function(eventData) → boolean
|
|
37
|
+
this.debounceMs = config.debounce ?? DEFAULT_TRIGGER_CONFIG.defaultDebounceMs;
|
|
38
|
+
this.priority = config.priority || 'medium'; // 'high' | 'medium' | 'low'
|
|
39
|
+
this.namespace = config.namespace || `trigger:${name}`;
|
|
40
|
+
this.systemPrompt = config.systemPrompt || null;
|
|
41
|
+
this.onResult = config.onResult || null; // callback(result, eventData) → void
|
|
42
|
+
this.enabled = config.enabled !== false;
|
|
43
|
+
this.maxTokens = config.maxTokens || undefined;
|
|
44
|
+
this.temperature = config.temperature || undefined;
|
|
45
|
+
|
|
46
|
+
// Internal state
|
|
47
|
+
this._debounceTimer = null;
|
|
48
|
+
this._lastFired = 0;
|
|
49
|
+
this._fireCount = 0;
|
|
50
|
+
this._pendingEvent = null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Check if this trigger matches an event name.
|
|
55
|
+
*/
|
|
56
|
+
matches(eventName) {
|
|
57
|
+
if (!this.pattern) return false;
|
|
58
|
+
if (this.pattern === '*') return true;
|
|
59
|
+
if (!this.pattern.includes('*')) return eventName === this.pattern;
|
|
60
|
+
|
|
61
|
+
const regex = new RegExp('^' + this.pattern.replace(/\*/g, '[^:]*') + '$');
|
|
62
|
+
return regex.test(eventName);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Build the prompt for this trigger given event data.
|
|
67
|
+
*/
|
|
68
|
+
buildPrompt(eventData) {
|
|
69
|
+
if (typeof this.prompt === 'function') {
|
|
70
|
+
return this.prompt(eventData);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Template interpolation
|
|
74
|
+
return interpolate(this.prompt, {
|
|
75
|
+
event: eventData,
|
|
76
|
+
data: eventData?.data,
|
|
77
|
+
timestamp: Date.now(),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Check condition (if any).
|
|
83
|
+
*/
|
|
84
|
+
async checkCondition(eventData) {
|
|
85
|
+
if (!this.condition) return true;
|
|
86
|
+
try {
|
|
87
|
+
const result = this.condition(eventData);
|
|
88
|
+
return result instanceof Promise ? await result : result;
|
|
89
|
+
} catch (err) {
|
|
90
|
+
logger.wuDebug(`[wu-ai] Trigger '${this.name}' condition error: ${err.message}`);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ─── Main Triggers Manager ───────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
export class WuAITriggers {
|
|
99
|
+
constructor({ eventBus, conversation, permissions }) {
|
|
100
|
+
this._eventBus = eventBus;
|
|
101
|
+
this._conversation = conversation;
|
|
102
|
+
this._permissions = permissions;
|
|
103
|
+
|
|
104
|
+
this._config = { ...DEFAULT_TRIGGER_CONFIG };
|
|
105
|
+
this._triggers = new Map(); // name → Trigger
|
|
106
|
+
this._listeners = new Map(); // name → unsubscribe function
|
|
107
|
+
this._batchQueue = []; // low-priority triggers pending batch
|
|
108
|
+
this._batchTimer = null;
|
|
109
|
+
this._stats = {
|
|
110
|
+
totalFired: 0,
|
|
111
|
+
totalSkipped: 0,
|
|
112
|
+
totalErrors: 0,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Configure trigger system.
|
|
118
|
+
*/
|
|
119
|
+
configure(config) {
|
|
120
|
+
Object.assign(this._config, config);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Register a trigger.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} name - Trigger name (unique identifier)
|
|
127
|
+
* @param {object} config
|
|
128
|
+
* @param {string} config.pattern - Event pattern to match (e.g., 'cart:*')
|
|
129
|
+
* @param {string|Function} config.prompt - Prompt template or function
|
|
130
|
+
* @param {Function} [config.condition] - Optional condition function
|
|
131
|
+
* @param {number} [config.debounce=1000] - Debounce in ms
|
|
132
|
+
* @param {string} [config.priority='medium'] - 'high' | 'medium' | 'low'
|
|
133
|
+
* @param {string} [config.namespace] - Conversation namespace
|
|
134
|
+
* @param {string} [config.systemPrompt] - Override system prompt
|
|
135
|
+
* @param {Function} [config.onResult] - Callback for results
|
|
136
|
+
* @param {boolean} [config.enabled=true] - Whether trigger is active
|
|
137
|
+
*/
|
|
138
|
+
register(name, config) {
|
|
139
|
+
if (this._triggers.size >= this._config.maxActiveTriggers) {
|
|
140
|
+
logger.wuWarn(`[wu-ai] Max triggers (${this._config.maxActiveTriggers}) reached. Cannot register '${name}'.`);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Unregister existing trigger with same name
|
|
145
|
+
if (this._triggers.has(name)) {
|
|
146
|
+
this.unregister(name);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const trigger = new Trigger(name, config);
|
|
150
|
+
this._triggers.set(name, trigger);
|
|
151
|
+
|
|
152
|
+
// Subscribe to matching events
|
|
153
|
+
const handler = (eventData) => this._handleEvent(name, eventData);
|
|
154
|
+
const unsub = this._eventBus.on(trigger.pattern, handler);
|
|
155
|
+
this._listeners.set(name, unsub);
|
|
156
|
+
|
|
157
|
+
logger.wuDebug(`[wu-ai] Trigger registered: '${name}' → pattern '${trigger.pattern}' (${trigger.priority})`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Unregister a trigger.
|
|
162
|
+
*/
|
|
163
|
+
unregister(name) {
|
|
164
|
+
const trigger = this._triggers.get(name);
|
|
165
|
+
if (trigger) {
|
|
166
|
+
// Clear any pending debounce
|
|
167
|
+
if (trigger._debounceTimer) {
|
|
168
|
+
clearTimeout(trigger._debounceTimer);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Remove event listener
|
|
173
|
+
const unsub = this._listeners.get(name);
|
|
174
|
+
if (typeof unsub === 'function') {
|
|
175
|
+
unsub();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this._triggers.delete(name);
|
|
179
|
+
this._listeners.delete(name);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Enable/disable a trigger.
|
|
184
|
+
*/
|
|
185
|
+
setEnabled(name, enabled) {
|
|
186
|
+
const trigger = this._triggers.get(name);
|
|
187
|
+
if (trigger) trigger.enabled = enabled;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Enable/disable all triggers.
|
|
192
|
+
*/
|
|
193
|
+
setAllEnabled(enabled) {
|
|
194
|
+
this._config.enabled = enabled;
|
|
195
|
+
for (const trigger of this._triggers.values()) {
|
|
196
|
+
trigger.enabled = enabled;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Fire a trigger manually (bypasses event matching).
|
|
202
|
+
*/
|
|
203
|
+
async fire(name, eventData = {}) {
|
|
204
|
+
const trigger = this._triggers.get(name);
|
|
205
|
+
if (!trigger) {
|
|
206
|
+
logger.wuWarn(`[wu-ai] Trigger '${name}' not found`);
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
return this._executeTrigger(trigger, eventData);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Get registered trigger names.
|
|
214
|
+
*/
|
|
215
|
+
getNames() {
|
|
216
|
+
return [...this._triggers.keys()];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Get trigger info.
|
|
221
|
+
*/
|
|
222
|
+
getTrigger(name) {
|
|
223
|
+
const t = this._triggers.get(name);
|
|
224
|
+
if (!t) return null;
|
|
225
|
+
return {
|
|
226
|
+
name: t.name,
|
|
227
|
+
pattern: t.pattern,
|
|
228
|
+
priority: t.priority,
|
|
229
|
+
namespace: t.namespace,
|
|
230
|
+
enabled: t.enabled,
|
|
231
|
+
fireCount: t._fireCount,
|
|
232
|
+
lastFired: t._lastFired,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Destroy all triggers and clean up.
|
|
238
|
+
*/
|
|
239
|
+
destroy() {
|
|
240
|
+
for (const name of [...this._triggers.keys()]) {
|
|
241
|
+
this.unregister(name);
|
|
242
|
+
}
|
|
243
|
+
if (this._batchTimer) {
|
|
244
|
+
clearTimeout(this._batchTimer);
|
|
245
|
+
this._batchTimer = null;
|
|
246
|
+
}
|
|
247
|
+
this._batchQueue = [];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ── Private: Event Handling ──
|
|
251
|
+
|
|
252
|
+
async _handleEvent(triggerName, eventData) {
|
|
253
|
+
if (!this._config.enabled) return;
|
|
254
|
+
|
|
255
|
+
const trigger = this._triggers.get(triggerName);
|
|
256
|
+
if (!trigger || !trigger.enabled) return;
|
|
257
|
+
|
|
258
|
+
// Check condition
|
|
259
|
+
const conditionMet = await trigger.checkCondition(eventData);
|
|
260
|
+
if (!conditionMet) {
|
|
261
|
+
this._stats.totalSkipped++;
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Priority routing
|
|
266
|
+
if (trigger.priority === 'high') {
|
|
267
|
+
// High priority: fire immediately (with debounce)
|
|
268
|
+
this._debouncedFire(trigger, eventData);
|
|
269
|
+
} else if (trigger.priority === 'low') {
|
|
270
|
+
// Low priority: batch
|
|
271
|
+
this._batchQueue.push({ trigger, eventData });
|
|
272
|
+
this._scheduleBatch();
|
|
273
|
+
} else {
|
|
274
|
+
// Medium: debounce
|
|
275
|
+
this._debouncedFire(trigger, eventData);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
_debouncedFire(trigger, eventData) {
|
|
280
|
+
// Store pending event (latest wins for debounce)
|
|
281
|
+
trigger._pendingEvent = eventData;
|
|
282
|
+
|
|
283
|
+
if (trigger._debounceTimer) {
|
|
284
|
+
clearTimeout(trigger._debounceTimer);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (trigger.debounceMs <= 0) {
|
|
288
|
+
// No debounce
|
|
289
|
+
this._executeTrigger(trigger, eventData);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
trigger._debounceTimer = setTimeout(() => {
|
|
294
|
+
trigger._debounceTimer = null;
|
|
295
|
+
const pending = trigger._pendingEvent;
|
|
296
|
+
trigger._pendingEvent = null;
|
|
297
|
+
if (pending) {
|
|
298
|
+
this._executeTrigger(trigger, pending);
|
|
299
|
+
}
|
|
300
|
+
}, trigger.debounceMs);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
_scheduleBatch() {
|
|
304
|
+
if (this._batchTimer) return;
|
|
305
|
+
|
|
306
|
+
this._batchTimer = setTimeout(() => {
|
|
307
|
+
this._batchTimer = null;
|
|
308
|
+
this._processBatch();
|
|
309
|
+
}, this._config.batchIntervalMs);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async _processBatch() {
|
|
313
|
+
const batch = [...this._batchQueue];
|
|
314
|
+
this._batchQueue = [];
|
|
315
|
+
|
|
316
|
+
// Deduplicate: keep last event per trigger
|
|
317
|
+
const byTrigger = new Map();
|
|
318
|
+
for (const { trigger, eventData } of batch) {
|
|
319
|
+
byTrigger.set(trigger.name, { trigger, eventData });
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
for (const { trigger, eventData } of byTrigger.values()) {
|
|
323
|
+
await this._executeTrigger(trigger, eventData);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async _executeTrigger(trigger, eventData) {
|
|
328
|
+
try {
|
|
329
|
+
const prompt = trigger.buildPrompt(eventData);
|
|
330
|
+
if (!prompt) {
|
|
331
|
+
this._stats.totalSkipped++;
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
logger.wuDebug(`[wu-ai] Trigger '${trigger.name}' firing with prompt: ${prompt.slice(0, 100)}...`);
|
|
336
|
+
|
|
337
|
+
const result = await this._conversation.send(prompt, {
|
|
338
|
+
namespace: trigger.namespace,
|
|
339
|
+
systemPrompt: trigger.systemPrompt,
|
|
340
|
+
temperature: trigger.temperature,
|
|
341
|
+
maxTokens: trigger.maxTokens,
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
trigger._fireCount++;
|
|
345
|
+
trigger._lastFired = Date.now();
|
|
346
|
+
this._stats.totalFired++;
|
|
347
|
+
|
|
348
|
+
// Emit trigger result event
|
|
349
|
+
this._eventBus.emit('ai:trigger:result', {
|
|
350
|
+
trigger: trigger.name,
|
|
351
|
+
pattern: trigger.pattern,
|
|
352
|
+
result,
|
|
353
|
+
}, { appName: 'wu-ai' });
|
|
354
|
+
|
|
355
|
+
// Call onResult callback if provided
|
|
356
|
+
if (trigger.onResult) {
|
|
357
|
+
try {
|
|
358
|
+
await trigger.onResult(result, eventData);
|
|
359
|
+
} catch (err) {
|
|
360
|
+
logger.wuDebug(`[wu-ai] Trigger '${trigger.name}' onResult error: ${err.message}`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return result;
|
|
365
|
+
} catch (err) {
|
|
366
|
+
this._stats.totalErrors++;
|
|
367
|
+
logger.wuWarn(`[wu-ai] Trigger '${trigger.name}' error: ${err.message}`);
|
|
368
|
+
|
|
369
|
+
this._eventBus.emit('ai:trigger:error', {
|
|
370
|
+
trigger: trigger.name,
|
|
371
|
+
error: err.message,
|
|
372
|
+
}, { appName: 'wu-ai' });
|
|
373
|
+
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
getStats() {
|
|
379
|
+
const triggers = {};
|
|
380
|
+
for (const [name, t] of this._triggers) {
|
|
381
|
+
triggers[name] = {
|
|
382
|
+
pattern: t.pattern,
|
|
383
|
+
priority: t.priority,
|
|
384
|
+
enabled: t.enabled,
|
|
385
|
+
fireCount: t._fireCount,
|
|
386
|
+
lastFired: t._lastFired,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
...this._stats,
|
|
391
|
+
triggerCount: this._triggers.size,
|
|
392
|
+
batchQueueSize: this._batchQueue.length,
|
|
393
|
+
triggers,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
}
|