tokensafe-ui 0.1.1 → 0.1.3
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/dist/chunk-CXJYXG22.js +566 -0
- package/dist/chunk-DC5DUO6P.js +727 -0
- package/dist/chunk-KAQKTPPN.js +1104 -0
- package/dist/chunk-R6O2VMCZ.js +205 -0
- package/dist/index.cjs +0 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +46 -0
- package/dist/mcp.cjs +1188 -0
- package/dist/mcp.d.cts +5 -0
- package/dist/mcp.d.ts +5 -0
- package/dist/mcp.js +36 -0
- package/dist/observer.cjs +767 -0
- package/dist/observer.d.ts +1 -0
- package/dist/observer.js +19 -0
- package/dist/oumv.cjs +1621 -0
- package/dist/oumv.d.ts +1 -0
- package/dist/oumv.js +130 -0
- package/dist/patcher.cjs +241 -0
- package/dist/patcher.d.ts +1 -0
- package/dist/patcher.js +14 -0
- package/dist/testing.cjs +207 -0
- package/dist/testing.d.ts +1 -0
- package/dist/testing.js +12 -0
- package/dist/vite.cjs +493 -0
- package/dist/vite.d.ts +2 -0
- package/dist/vite.js +261 -0
- package/package.json +6 -1
- package/src/index.ts +0 -1
- package/src/mcp.ts +0 -10
- package/tsconfig.json +0 -11
- package/tsup.config.ts +0 -44
- /package/{src/observer.ts → dist/observer.d.cts} +0 -0
- /package/{src/oumv.ts → dist/oumv.d.cts} +0 -0
- /package/{src/patcher.ts → dist/patcher.d.cts} +0 -0
- /package/{src/testing.ts → dist/testing.d.cts} +0 -0
- /package/{src/vite.ts → dist/vite.d.cts} +0 -0
|
@@ -0,0 +1,727 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from 'node:module';
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from 'node:url';
|
|
3
|
+
import { dirname as __dirnameFunc } from 'node:path';
|
|
4
|
+
const require = __createRequire(import.meta.url);
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __dirnameFunc(__filename);
|
|
7
|
+
import {
|
|
8
|
+
activeInstances
|
|
9
|
+
} from "./chunk-KAQKTPPN.js";
|
|
10
|
+
|
|
11
|
+
// ../observer/dist/index.js
|
|
12
|
+
import fs from "fs";
|
|
13
|
+
import path from "path";
|
|
14
|
+
import * as parser from "@babel/parser";
|
|
15
|
+
import _traverse from "@babel/traverse";
|
|
16
|
+
import * as t from "@babel/types";
|
|
17
|
+
function getSemanticSnapshot(root, options = {}) {
|
|
18
|
+
const format = options.format || "yaml";
|
|
19
|
+
const maxElements = options.maxElements || 50;
|
|
20
|
+
const rootAgentId = root.getAttribute("data-agent-id") || "root";
|
|
21
|
+
const componentId = rootAgentId.split(":")[0] || "app";
|
|
22
|
+
const instance = activeInstances.get(componentId);
|
|
23
|
+
const stateSnapshot = {};
|
|
24
|
+
if (instance) {
|
|
25
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
26
|
+
stateSnapshot[k] = sig.peek();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const elements = [];
|
|
30
|
+
collectSemanticElements(root, elements, maxElements);
|
|
31
|
+
const snapshot = {
|
|
32
|
+
view: componentId,
|
|
33
|
+
state: stateSnapshot,
|
|
34
|
+
elements
|
|
35
|
+
};
|
|
36
|
+
if (format === "json") {
|
|
37
|
+
return snapshot;
|
|
38
|
+
}
|
|
39
|
+
if (format === "sexp") {
|
|
40
|
+
return formatAsSExp(snapshot);
|
|
41
|
+
}
|
|
42
|
+
return formatAsYaml(snapshot);
|
|
43
|
+
}
|
|
44
|
+
function collectSemanticElements(el, elements, max) {
|
|
45
|
+
if (elements.length >= max) return;
|
|
46
|
+
const agentId = el.getAttribute("data-agent-id");
|
|
47
|
+
const role = el.getAttribute("role") || inferRole(el);
|
|
48
|
+
const isInteractive = isInteractiveElement(el, role);
|
|
49
|
+
if (agentId || isInteractive) {
|
|
50
|
+
const semEl = {
|
|
51
|
+
id: agentId || `${el.tagName.toLowerCase()}#${elements.length}`,
|
|
52
|
+
tag: el.tagName.toLowerCase(),
|
|
53
|
+
role
|
|
54
|
+
};
|
|
55
|
+
const text = getDirectText(el).trim();
|
|
56
|
+
if (text) {
|
|
57
|
+
semEl.text = text;
|
|
58
|
+
}
|
|
59
|
+
if (el instanceof HTMLInputElement) {
|
|
60
|
+
if (el.type === "checkbox" || el.type === "radio") {
|
|
61
|
+
semEl.state = { checked: el.checked };
|
|
62
|
+
} else {
|
|
63
|
+
semEl.value = el.value;
|
|
64
|
+
}
|
|
65
|
+
} else if (el instanceof HTMLSelectElement || el instanceof HTMLTextAreaElement) {
|
|
66
|
+
semEl.value = el.value;
|
|
67
|
+
}
|
|
68
|
+
if (el.disabled) {
|
|
69
|
+
semEl.state = { ...semEl.state || {}, disabled: true };
|
|
70
|
+
}
|
|
71
|
+
const action = el.getAttribute("data-agent-action");
|
|
72
|
+
if (action) {
|
|
73
|
+
semEl.action = action;
|
|
74
|
+
}
|
|
75
|
+
elements.push(semEl);
|
|
76
|
+
}
|
|
77
|
+
for (const child of Array.from(el.children)) {
|
|
78
|
+
collectSemanticElements(child, elements, max);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function inferRole(el) {
|
|
82
|
+
const tag = el.tagName.toLowerCase();
|
|
83
|
+
switch (tag) {
|
|
84
|
+
case "button":
|
|
85
|
+
return "button";
|
|
86
|
+
case "input":
|
|
87
|
+
return el.type || "input";
|
|
88
|
+
case "select":
|
|
89
|
+
return "combobox";
|
|
90
|
+
case "textarea":
|
|
91
|
+
return "textbox";
|
|
92
|
+
case "a":
|
|
93
|
+
return "link";
|
|
94
|
+
case "h1":
|
|
95
|
+
case "h2":
|
|
96
|
+
case "h3":
|
|
97
|
+
case "h4":
|
|
98
|
+
case "h5":
|
|
99
|
+
case "h6":
|
|
100
|
+
return "heading";
|
|
101
|
+
case "form":
|
|
102
|
+
return "form";
|
|
103
|
+
case "table":
|
|
104
|
+
return "table";
|
|
105
|
+
default:
|
|
106
|
+
return "generic";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function isInteractiveElement(el, role) {
|
|
110
|
+
if (["button", "input", "combobox", "textbox", "link", "checkbox", "radio"].includes(role)) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return el.hasAttribute("onclick") || el.hasAttribute("data-agent-action");
|
|
114
|
+
}
|
|
115
|
+
function getDirectText(el) {
|
|
116
|
+
let text = "";
|
|
117
|
+
for (const node of Array.from(el.childNodes)) {
|
|
118
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
119
|
+
text += node.textContent || "";
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return text || el.textContent || "";
|
|
123
|
+
}
|
|
124
|
+
function formatAsYaml(snapshot) {
|
|
125
|
+
const lines = [];
|
|
126
|
+
lines.push(`view: ${snapshot.view}`);
|
|
127
|
+
if (Object.keys(snapshot.state).length > 0) {
|
|
128
|
+
lines.push(`state: ${JSON.stringify(snapshot.state)}`);
|
|
129
|
+
}
|
|
130
|
+
lines.push("elements:");
|
|
131
|
+
for (const el of snapshot.elements) {
|
|
132
|
+
let line = ` - ${el.tag}#${el.id}`;
|
|
133
|
+
const meta = [];
|
|
134
|
+
if (el.role && el.role !== "generic" && el.role !== el.tag) {
|
|
135
|
+
meta.push(`role: ${el.role}`);
|
|
136
|
+
}
|
|
137
|
+
if (el.action) {
|
|
138
|
+
meta.push(`action: ${el.action}`);
|
|
139
|
+
}
|
|
140
|
+
if (el.state) {
|
|
141
|
+
meta.push(`state: ${JSON.stringify(el.state)}`);
|
|
142
|
+
}
|
|
143
|
+
if (el.value !== void 0) {
|
|
144
|
+
meta.push(`val: ${JSON.stringify(el.value)}`);
|
|
145
|
+
}
|
|
146
|
+
if (meta.length > 0) {
|
|
147
|
+
line += ` [${meta.join(", ")}]`;
|
|
148
|
+
}
|
|
149
|
+
if (el.text) {
|
|
150
|
+
line += `: ${JSON.stringify(el.text)}`;
|
|
151
|
+
}
|
|
152
|
+
lines.push(line);
|
|
153
|
+
}
|
|
154
|
+
return lines.join("\n");
|
|
155
|
+
}
|
|
156
|
+
function formatAsSExp(snapshot) {
|
|
157
|
+
const stateStr = Object.entries(snapshot.state).map(([k, v]) => `(${k} ${JSON.stringify(v)})`).join(" ");
|
|
158
|
+
const elementsStr = snapshot.elements.map((el) => {
|
|
159
|
+
const parts = [`${el.tag}#${el.id}`];
|
|
160
|
+
if (el.action) parts.push(`:act ${el.action}`);
|
|
161
|
+
if (el.text) parts.push(`"${el.text}"`);
|
|
162
|
+
return `(${parts.join(" ")})`;
|
|
163
|
+
}).join(" ");
|
|
164
|
+
return `(view ${snapshot.view} (state ${stateStr}) (elements ${elementsStr}))`;
|
|
165
|
+
}
|
|
166
|
+
var traverse = _traverse.default || _traverse;
|
|
167
|
+
function observe(root, options = {}) {
|
|
168
|
+
const level = options.level || "L1";
|
|
169
|
+
const format = options.format || "yaml";
|
|
170
|
+
switch (level) {
|
|
171
|
+
case "L0":
|
|
172
|
+
return formatPayload(buildL0Pulse(root), format);
|
|
173
|
+
case "L1":
|
|
174
|
+
return buildL1Surface(root, options, format);
|
|
175
|
+
case "L2":
|
|
176
|
+
return formatPayload(buildL2Contract(root, options), format);
|
|
177
|
+
case "L3":
|
|
178
|
+
return formatPayload(buildL3Source(root, options), format);
|
|
179
|
+
case "L4":
|
|
180
|
+
return formatPayload(buildL4Traces(root, options), format);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function buildL0Pulse(root) {
|
|
184
|
+
const mounted = Array.from(activeInstances.keys());
|
|
185
|
+
let errorCount = 0;
|
|
186
|
+
for (const instance of activeInstances.values()) {
|
|
187
|
+
errorCount += instance.runtimeLog?.filter((e) => e.type === "error").length || 0;
|
|
188
|
+
}
|
|
189
|
+
const interactiveCount = root.querySelectorAll(
|
|
190
|
+
'button, input, select, textarea, a, [role="button"], [data-agent-action]'
|
|
191
|
+
).length;
|
|
192
|
+
return {
|
|
193
|
+
level: "L0",
|
|
194
|
+
route: typeof window !== "undefined" ? window.location.pathname : "/",
|
|
195
|
+
mounted,
|
|
196
|
+
elements: interactiveCount,
|
|
197
|
+
errors: errorCount
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function buildL1Surface(root, options, format) {
|
|
201
|
+
const rootAgentId = root.getAttribute("data-agent-id") || "root";
|
|
202
|
+
const componentId = options.componentId || rootAgentId.split(":")[0] || "app";
|
|
203
|
+
const instance = activeInstances.get(componentId);
|
|
204
|
+
const stateSnapshot = {};
|
|
205
|
+
if (instance) {
|
|
206
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
207
|
+
stateSnapshot[k] = sig.peek();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const elements = [];
|
|
211
|
+
const allNodes = root.querySelectorAll(
|
|
212
|
+
'button, input, select, textarea, a, h1, h2, h3, [role="button"], [data-agent-action], [data-agent-id]'
|
|
213
|
+
);
|
|
214
|
+
allNodes.forEach((node, idx) => {
|
|
215
|
+
const el = node;
|
|
216
|
+
const agentId = el.getAttribute("data-agent-id");
|
|
217
|
+
const action = el.getAttribute("data-agent-action");
|
|
218
|
+
const tag = el.tagName.toLowerCase();
|
|
219
|
+
const role = el.getAttribute("role") || tag;
|
|
220
|
+
if (!agentId && !action && ["div", "span", "ul", "li"].includes(tag)) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const item = {
|
|
224
|
+
id: agentId || `${tag}#${idx}`,
|
|
225
|
+
tag,
|
|
226
|
+
role
|
|
227
|
+
};
|
|
228
|
+
const text = (el.textContent || "").trim();
|
|
229
|
+
if (text && text.length <= 40) {
|
|
230
|
+
item.text = text;
|
|
231
|
+
}
|
|
232
|
+
if (el instanceof HTMLInputElement) {
|
|
233
|
+
if (el.type === "checkbox" || el.type === "radio") {
|
|
234
|
+
item.state = { checked: el.checked };
|
|
235
|
+
} else if (el.value) {
|
|
236
|
+
item.value = el.value;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (el.disabled) {
|
|
240
|
+
item.state = { ...item.state || {}, disabled: true };
|
|
241
|
+
}
|
|
242
|
+
if (action) {
|
|
243
|
+
item.action = action;
|
|
244
|
+
}
|
|
245
|
+
elements.push(item);
|
|
246
|
+
});
|
|
247
|
+
const snapshot = {
|
|
248
|
+
view: componentId,
|
|
249
|
+
state: stateSnapshot,
|
|
250
|
+
elements
|
|
251
|
+
};
|
|
252
|
+
if (format === "json") return snapshot;
|
|
253
|
+
if (format === "sexp") {
|
|
254
|
+
const elementsStr = elements.map((e) => `(${e.tag}#${e.id}${e.action ? ` :act ${e.action}` : ""}${e.text ? ` "${e.text}"` : ""})`).join(" ");
|
|
255
|
+
return `(L1 view:${componentId} (elements ${elementsStr}))`;
|
|
256
|
+
}
|
|
257
|
+
const lines = [`level: L1`, `view: ${componentId}`];
|
|
258
|
+
if (Object.keys(stateSnapshot).length > 0) {
|
|
259
|
+
lines.push(`state: ${JSON.stringify(stateSnapshot)}`);
|
|
260
|
+
}
|
|
261
|
+
lines.push("elements:");
|
|
262
|
+
for (const el of elements) {
|
|
263
|
+
let line = ` - ${el.tag}#${el.id}`;
|
|
264
|
+
const meta = [];
|
|
265
|
+
if (el.action) meta.push(`action: ${el.action}`);
|
|
266
|
+
if (el.state) meta.push(`state: ${JSON.stringify(el.state)}`);
|
|
267
|
+
if (el.value !== void 0) meta.push(`val: ${JSON.stringify(el.value)}`);
|
|
268
|
+
if (meta.length > 0) line += ` [${meta.join(", ")}]`;
|
|
269
|
+
if (el.text) line += `: ${JSON.stringify(el.text)}`;
|
|
270
|
+
lines.push(line);
|
|
271
|
+
}
|
|
272
|
+
return lines.join("\n");
|
|
273
|
+
}
|
|
274
|
+
function buildL2Contract(root, options) {
|
|
275
|
+
const rootAgentId = root.getAttribute("data-agent-id") || "root";
|
|
276
|
+
const componentId = options.componentId || rootAgentId.split(":")[0] || "app";
|
|
277
|
+
const instance = activeInstances.get(componentId);
|
|
278
|
+
const stateDetails = {};
|
|
279
|
+
const actions = [];
|
|
280
|
+
if (instance) {
|
|
281
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
282
|
+
stateDetails[k] = { current: sig.peek() };
|
|
283
|
+
}
|
|
284
|
+
actions.push(...Object.keys(instance.config.actions));
|
|
285
|
+
}
|
|
286
|
+
const elements = [];
|
|
287
|
+
root.querySelectorAll("[data-agent-id]").forEach((el) => {
|
|
288
|
+
const id = el.getAttribute("data-agent-id");
|
|
289
|
+
if (id) elements.push(id);
|
|
290
|
+
});
|
|
291
|
+
return {
|
|
292
|
+
level: "L2",
|
|
293
|
+
componentId,
|
|
294
|
+
state: stateDetails,
|
|
295
|
+
actions,
|
|
296
|
+
elements
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function buildL3Source(root, options) {
|
|
300
|
+
const rootAgentId = root.getAttribute("data-agent-id") || "root";
|
|
301
|
+
const componentId = options.componentId || rootAgentId.split(":")[0] || "app";
|
|
302
|
+
const instance = activeInstances.get(componentId);
|
|
303
|
+
let source = options.sourceCode;
|
|
304
|
+
let resolvedFilePath = options.filePath || instance?.config?.filePath;
|
|
305
|
+
if (!source && resolvedFilePath && typeof fs !== "undefined" && fs.existsSync) {
|
|
306
|
+
try {
|
|
307
|
+
if (fs.existsSync(resolvedFilePath)) {
|
|
308
|
+
source = fs.readFileSync(resolvedFilePath, "utf-8");
|
|
309
|
+
}
|
|
310
|
+
} catch {
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (!source && typeof process !== "undefined") {
|
|
314
|
+
const rootDir = options.projectRoot || process.cwd();
|
|
315
|
+
const found = searchComponentFile(rootDir, componentId);
|
|
316
|
+
if (found) {
|
|
317
|
+
resolvedFilePath = found.filePath;
|
|
318
|
+
source = found.source;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (source) {
|
|
322
|
+
try {
|
|
323
|
+
return extractIsolatedSourceAST(source, componentId, resolvedFilePath);
|
|
324
|
+
} catch {
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return buildSyntheticSourceSnapshot(componentId, instance, resolvedFilePath);
|
|
328
|
+
}
|
|
329
|
+
function searchComponentFile(dir, componentId) {
|
|
330
|
+
try {
|
|
331
|
+
if (!fs.existsSync(dir)) return null;
|
|
332
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
333
|
+
for (const entry of entries) {
|
|
334
|
+
if (entry.name === "node_modules" || entry.name === "dist" || entry.name === ".git") {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const fullPath = path.join(dir, entry.name);
|
|
338
|
+
if (entry.isDirectory()) {
|
|
339
|
+
const found = searchComponentFile(fullPath, componentId);
|
|
340
|
+
if (found) return found;
|
|
341
|
+
} else if (entry.isFile() && (entry.name.endsWith(".tsx") || entry.name.endsWith(".ts") || entry.name.endsWith(".jsx") || entry.name.endsWith(".js"))) {
|
|
342
|
+
const content = fs.readFileSync(fullPath, "utf-8");
|
|
343
|
+
if (content.includes(`id: '${componentId}'`) || content.includes(`id: "${componentId}"`)) {
|
|
344
|
+
return { filePath: fullPath, source: content };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
} catch {
|
|
349
|
+
}
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
function extractIsolatedSourceAST(fullSource, componentId, filePath) {
|
|
353
|
+
const ast = parser.parse(fullSource, {
|
|
354
|
+
sourceType: "module",
|
|
355
|
+
plugins: ["typescript", "jsx"]
|
|
356
|
+
});
|
|
357
|
+
let startLine = 1;
|
|
358
|
+
let endLine = fullSource.split("\n").length;
|
|
359
|
+
let found = false;
|
|
360
|
+
const agentIdLineMap = {};
|
|
361
|
+
traverse(ast, {
|
|
362
|
+
CallExpression(p) {
|
|
363
|
+
const callee = p.node.callee;
|
|
364
|
+
const isDefine = t.isIdentifier(callee) && callee.name === "defineComponent" || t.isMemberExpression(callee) && t.isIdentifier(callee.property) && callee.property.name === "defineComponent";
|
|
365
|
+
if (!isDefine || p.node.arguments.length === 0) return;
|
|
366
|
+
const configArg = p.node.arguments[0];
|
|
367
|
+
if (!t.isObjectExpression(configArg)) return;
|
|
368
|
+
const idProp = configArg.properties.find(
|
|
369
|
+
(prop) => t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "id" && t.isStringLiteral(prop.value) && prop.value.value === componentId
|
|
370
|
+
);
|
|
371
|
+
if (idProp) {
|
|
372
|
+
found = true;
|
|
373
|
+
const parentDecl = p.findParent(
|
|
374
|
+
(parent) => parent.isExportNamedDeclaration() || parent.isVariableDeclaration() || parent.isDeclaration()
|
|
375
|
+
);
|
|
376
|
+
const targetNode = parentDecl ? parentDecl.node : p.node;
|
|
377
|
+
if (targetNode.loc) {
|
|
378
|
+
startLine = targetNode.loc.start.line;
|
|
379
|
+
endLine = targetNode.loc.end.line;
|
|
380
|
+
}
|
|
381
|
+
p.traverse({
|
|
382
|
+
JSXAttribute(attrPath) {
|
|
383
|
+
if (attrPath.node.name.name === "data-agent-id") {
|
|
384
|
+
const val = attrPath.node.value;
|
|
385
|
+
if (t.isStringLiteral(val) && attrPath.node.loc) {
|
|
386
|
+
agentIdLineMap[val.value] = attrPath.node.loc.start.line;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
ObjectProperty(objPropPath) {
|
|
391
|
+
const key = objPropPath.node.key;
|
|
392
|
+
const isAgentIdKey = t.isStringLiteral(key) && key.value === "data-agent-id" || t.isIdentifier(key) && key.name === "data-agent-id";
|
|
393
|
+
if (isAgentIdKey && t.isStringLiteral(objPropPath.node.value) && objPropPath.node.loc) {
|
|
394
|
+
agentIdLineMap[objPropPath.node.value.value] = objPropPath.node.loc.start.line;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
const allLines = fullSource.split("\n");
|
|
402
|
+
const isolatedLines = found ? allLines.slice(startLine - 1, endLine) : allLines;
|
|
403
|
+
const formattedLines = isolatedLines.map(
|
|
404
|
+
(l, i) => `${String(startLine + i).padStart(2, "0")} | ${l}`
|
|
405
|
+
);
|
|
406
|
+
return {
|
|
407
|
+
level: "L3",
|
|
408
|
+
componentId,
|
|
409
|
+
filePath,
|
|
410
|
+
startLine,
|
|
411
|
+
endLine,
|
|
412
|
+
lines: formattedLines,
|
|
413
|
+
isolatedSource: isolatedLines.join("\n"),
|
|
414
|
+
agentIdLineMap
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
function buildSyntheticSourceSnapshot(componentId, instance, filePath) {
|
|
418
|
+
const stateKeys = Object.keys(instance?.signals || {});
|
|
419
|
+
const actionKeys = Object.keys(instance?.config?.actions || {});
|
|
420
|
+
const effectKeys = Object.keys(instance?.config?.effects || {});
|
|
421
|
+
const syntheticLines = [
|
|
422
|
+
`// [TokenSafe Isolated Component Spec: ${componentId}]`,
|
|
423
|
+
`export const ${componentId.replace(/[^a-zA-Z0-9]/g, "_")} = defineComponent({`,
|
|
424
|
+
` id: '${componentId}',`,
|
|
425
|
+
` state: {`,
|
|
426
|
+
...stateKeys.map((k) => ` ${k}: /* ${JSON.stringify(instance?.signals?.[k]?.peek())} */,`),
|
|
427
|
+
` },`,
|
|
428
|
+
` actions: {`,
|
|
429
|
+
...actionKeys.map((a) => ` ${a}: (state, payload) => ({ ...state }),`),
|
|
430
|
+
` },`,
|
|
431
|
+
...effectKeys.length > 0 ? [
|
|
432
|
+
` effects: {`,
|
|
433
|
+
...effectKeys.map((e) => ` ${e}: async ({ dispatch }) => {},`),
|
|
434
|
+
` },`
|
|
435
|
+
] : [],
|
|
436
|
+
` render({ state, emit }) {`,
|
|
437
|
+
` return h('div', { 'data-agent-id': '${componentId}:root' });`,
|
|
438
|
+
` }`,
|
|
439
|
+
`});`
|
|
440
|
+
];
|
|
441
|
+
const agentIdLineMap = {
|
|
442
|
+
[`${componentId}:root`]: syntheticLines.length - 2
|
|
443
|
+
};
|
|
444
|
+
return {
|
|
445
|
+
level: "L3",
|
|
446
|
+
componentId,
|
|
447
|
+
filePath,
|
|
448
|
+
startLine: 1,
|
|
449
|
+
endLine: syntheticLines.length,
|
|
450
|
+
lines: syntheticLines.map((l, i) => `${String(i + 1).padStart(2, "0")} | ${l}`),
|
|
451
|
+
isolatedSource: syntheticLines.join("\n"),
|
|
452
|
+
agentIdLineMap
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
function buildL4Traces(root, options) {
|
|
456
|
+
const rootAgentId = root.getAttribute("data-agent-id") || "root";
|
|
457
|
+
const componentId = options.componentId || rootAgentId.split(":")[0] || "app";
|
|
458
|
+
const instance = activeInstances.get(componentId);
|
|
459
|
+
const events = (instance?.runtimeLog || []).map((e) => {
|
|
460
|
+
let details = "";
|
|
461
|
+
if (e.type === "action_start") details = `Action '${e.actionName}' started`;
|
|
462
|
+
else if (e.type === "action_end") details = `Action '${e.actionName}' completed in ${e.durationMs}ms`;
|
|
463
|
+
else if (e.type === "signal_mutation")
|
|
464
|
+
details = `Signal '${e.signalKey}' changed: ${JSON.stringify(e.oldValue)} -> ${JSON.stringify(e.newValue)}`;
|
|
465
|
+
else if (e.type === "error") details = `Error: ${e.error}`;
|
|
466
|
+
return {
|
|
467
|
+
timestamp: e.timestamp,
|
|
468
|
+
type: e.type,
|
|
469
|
+
details
|
|
470
|
+
};
|
|
471
|
+
});
|
|
472
|
+
return {
|
|
473
|
+
level: "L4",
|
|
474
|
+
componentId,
|
|
475
|
+
events
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function formatPayload(payload, format) {
|
|
479
|
+
if (format === "json") return payload;
|
|
480
|
+
const lines = [];
|
|
481
|
+
lines.push(`level: ${payload.level}`);
|
|
482
|
+
if (payload.componentId) lines.push(`component: ${payload.componentId}`);
|
|
483
|
+
if (payload.route) lines.push(`route: ${payload.route}`);
|
|
484
|
+
if (payload.mounted) lines.push(`mounted: [${payload.mounted.join(", ")}]`);
|
|
485
|
+
if (payload.elements !== void 0) {
|
|
486
|
+
if (Array.isArray(payload.elements)) {
|
|
487
|
+
lines.push("elements:");
|
|
488
|
+
for (const el of payload.elements) lines.push(` - ${el}`);
|
|
489
|
+
} else {
|
|
490
|
+
lines.push(`elements_count: ${payload.elements}`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
if (payload.errors !== void 0) lines.push(`errors_count: ${payload.errors}`);
|
|
494
|
+
if (payload.actions) lines.push(`actions: [${payload.actions.join(", ")}]`);
|
|
495
|
+
if (payload.state) lines.push(`state: ${JSON.stringify(payload.state)}`);
|
|
496
|
+
if (payload.events) {
|
|
497
|
+
lines.push("traces:");
|
|
498
|
+
for (const ev of payload.events) {
|
|
499
|
+
lines.push(` - [${ev.type}] ${ev.details}`);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
if (payload.lines) {
|
|
503
|
+
lines.push("source:");
|
|
504
|
+
for (const l of payload.lines) {
|
|
505
|
+
lines.push(` ${l}`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return lines.join("\n");
|
|
509
|
+
}
|
|
510
|
+
function buildDependencyGraph() {
|
|
511
|
+
const components = {};
|
|
512
|
+
for (const [id, instance] of activeInstances.entries()) {
|
|
513
|
+
const stateKeys = Object.keys(instance.signals || {});
|
|
514
|
+
const actions = Object.keys(instance.config.actions || {});
|
|
515
|
+
const effects = Object.keys(instance.config.effects || {});
|
|
516
|
+
const signalMap = {};
|
|
517
|
+
for (const [key, sig] of Object.entries(instance.signals || {})) {
|
|
518
|
+
const subs = Array.from(sig.getSubscribers?.() || []).map((s) => ({
|
|
519
|
+
id: s.id,
|
|
520
|
+
name: s.name,
|
|
521
|
+
kind: s.kind
|
|
522
|
+
}));
|
|
523
|
+
signalMap[key] = {
|
|
524
|
+
value: sig.peek?.(),
|
|
525
|
+
subscribers: subs
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
components[id] = {
|
|
529
|
+
id,
|
|
530
|
+
stateKeys,
|
|
531
|
+
actions,
|
|
532
|
+
effects,
|
|
533
|
+
dependencies: [],
|
|
534
|
+
dependents: [],
|
|
535
|
+
signals: signalMap
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
for (const [id, instance] of activeInstances.entries()) {
|
|
539
|
+
const childComponentIds = /* @__PURE__ */ new Set();
|
|
540
|
+
findChildComponents(instance.vnode, childComponentIds);
|
|
541
|
+
for (const childId of childComponentIds) {
|
|
542
|
+
if (childId !== id && components[childId]) {
|
|
543
|
+
if (!components[id].dependencies.includes(childId)) {
|
|
544
|
+
components[id].dependencies.push(childId);
|
|
545
|
+
}
|
|
546
|
+
if (!components[childId].dependents.includes(id)) {
|
|
547
|
+
components[childId].dependents.push(id);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
for (const [id, comp] of Object.entries(components)) {
|
|
553
|
+
for (const [stateKey, sigData] of Object.entries(comp.signals)) {
|
|
554
|
+
for (const sub of sigData.subscribers) {
|
|
555
|
+
for (const otherId of Object.keys(components)) {
|
|
556
|
+
if (otherId !== id && (sub.name?.startsWith(`${otherId}:`) || sub.id?.startsWith(`${otherId}:`))) {
|
|
557
|
+
if (!components[otherId].dependencies.includes(id)) {
|
|
558
|
+
components[otherId].dependencies.push(id);
|
|
559
|
+
}
|
|
560
|
+
if (!components[id].dependents.includes(otherId)) {
|
|
561
|
+
components[id].dependents.push(otherId);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
components,
|
|
570
|
+
analyzeImpact(target) {
|
|
571
|
+
const directlyAffected = /* @__PURE__ */ new Set();
|
|
572
|
+
const indirectlyAffected = /* @__PURE__ */ new Set();
|
|
573
|
+
const paths = [];
|
|
574
|
+
let seedComponents = [];
|
|
575
|
+
let sourceLabel = target;
|
|
576
|
+
if (components[target]) {
|
|
577
|
+
seedComponents = [target];
|
|
578
|
+
directlyAffected.add(target);
|
|
579
|
+
} else {
|
|
580
|
+
let targetComp = "";
|
|
581
|
+
let targetKey = target;
|
|
582
|
+
if (target.includes(".")) {
|
|
583
|
+
const parts = target.split(".");
|
|
584
|
+
targetComp = parts[0];
|
|
585
|
+
targetKey = parts.slice(1).join(".");
|
|
586
|
+
}
|
|
587
|
+
for (const [id, comp] of Object.entries(components)) {
|
|
588
|
+
if ((!targetComp || targetComp === id) && comp.stateKeys.includes(targetKey)) {
|
|
589
|
+
seedComponents.push(id);
|
|
590
|
+
directlyAffected.add(id);
|
|
591
|
+
sourceLabel = `${id}.${targetKey}`;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
for (const seed of seedComponents) {
|
|
596
|
+
for (const dep of components[seed]?.dependents || []) {
|
|
597
|
+
directlyAffected.add(dep);
|
|
598
|
+
paths.push(`${sourceLabel} -> ${dep}`);
|
|
599
|
+
const queue = [
|
|
600
|
+
{ node: dep, path: [sourceLabel, dep], depth: 1 }
|
|
601
|
+
];
|
|
602
|
+
const visited = /* @__PURE__ */ new Set([seed, dep]);
|
|
603
|
+
while (queue.length > 0) {
|
|
604
|
+
const current = queue.shift();
|
|
605
|
+
for (const nextDep of components[current.node]?.dependents || []) {
|
|
606
|
+
if (!visited.has(nextDep)) {
|
|
607
|
+
visited.add(nextDep);
|
|
608
|
+
indirectlyAffected.add(nextDep);
|
|
609
|
+
const nextPath = [...current.path, nextDep];
|
|
610
|
+
paths.push(nextPath.join(" -> "));
|
|
611
|
+
queue.push({ node: nextDep, path: nextPath, depth: current.depth + 1 });
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
const allAffected = /* @__PURE__ */ new Set([...directlyAffected, ...indirectlyAffected]);
|
|
618
|
+
const safeComponents = Object.keys(components).filter((id) => !allAffected.has(id));
|
|
619
|
+
const directList = Array.from(directlyAffected);
|
|
620
|
+
const indirectList = Array.from(indirectlyAffected);
|
|
621
|
+
const blastDepth = paths.reduce((max, p) => Math.max(max, p.split(" -> ").length), 1);
|
|
622
|
+
let recommendation = `Target '${target}' is not bound to any active components. Safe to introduce or edit.`;
|
|
623
|
+
if (directList.length > 0) {
|
|
624
|
+
recommendation = `Editing '${target}' directly impacts: [${directList.join(", ")}]`;
|
|
625
|
+
if (indirectList.length > 0) {
|
|
626
|
+
recommendation += ` and cascades to: [${indirectList.join(", ")}] (blast depth: ${blastDepth}). Verify contracts before commit.`;
|
|
627
|
+
} else {
|
|
628
|
+
recommendation += `. Verify contract before commit.`;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return {
|
|
632
|
+
target,
|
|
633
|
+
directlyAffected: directList,
|
|
634
|
+
indirectlyAffected: indirectList,
|
|
635
|
+
safeComponents,
|
|
636
|
+
blastRadiusDepth: blastDepth,
|
|
637
|
+
propagationPaths: paths,
|
|
638
|
+
recommendation
|
|
639
|
+
};
|
|
640
|
+
},
|
|
641
|
+
traceCausality(target) {
|
|
642
|
+
let compId = "";
|
|
643
|
+
let stateKey = "";
|
|
644
|
+
if (target.includes(".")) {
|
|
645
|
+
const parts = target.split(".");
|
|
646
|
+
compId = parts[0];
|
|
647
|
+
stateKey = parts.slice(1).join(".");
|
|
648
|
+
} else {
|
|
649
|
+
for (const [id, comp] of Object.entries(components)) {
|
|
650
|
+
if (comp.stateKeys.includes(target)) {
|
|
651
|
+
compId = id;
|
|
652
|
+
stateKey = target;
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if (!compId || !components[compId]) {
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
const instance = activeInstances.get(compId);
|
|
661
|
+
const node = components[compId];
|
|
662
|
+
const sigData = node.signals[stateKey];
|
|
663
|
+
if (!instance || !sigData) {
|
|
664
|
+
return null;
|
|
665
|
+
}
|
|
666
|
+
const lastMutation = [...instance.runtimeLog].reverse().find((e) => e.type === "signal_mutation" && e.signalKey === stateKey);
|
|
667
|
+
const subSummary = sigData.subscribers.map((s) => s.name || s.id).join(", ");
|
|
668
|
+
const mutationDesc = lastMutation ? `Last mutated by action '${lastMutation.actionName}' (${JSON.stringify(lastMutation.oldValue)} -> ${JSON.stringify(lastMutation.newValue)}).` : "Initial default value (unmodified since mount).";
|
|
669
|
+
const explanation = `State '${compId}.${stateKey}' = ${JSON.stringify(sigData.value)}. ${mutationDesc} Active subscribers: [${subSummary || "none"}].`;
|
|
670
|
+
return {
|
|
671
|
+
target,
|
|
672
|
+
componentId: compId,
|
|
673
|
+
stateKey,
|
|
674
|
+
value: sigData.value,
|
|
675
|
+
subscribers: sigData.subscribers,
|
|
676
|
+
lastMutation,
|
|
677
|
+
explanation
|
|
678
|
+
};
|
|
679
|
+
},
|
|
680
|
+
toYaml() {
|
|
681
|
+
const lines = ["dependency_graph:"];
|
|
682
|
+
for (const [id, node] of Object.entries(components)) {
|
|
683
|
+
lines.push(` - component: ${id}`);
|
|
684
|
+
lines.push(` state: [${node.stateKeys.join(", ")}]`);
|
|
685
|
+
lines.push(` actions: [${node.actions.join(", ")}]`);
|
|
686
|
+
if (node.effects.length > 0) {
|
|
687
|
+
lines.push(` effects: [${node.effects.join(", ")}]`);
|
|
688
|
+
}
|
|
689
|
+
if (node.dependencies.length > 0) {
|
|
690
|
+
lines.push(` dependencies: [${node.dependencies.join(", ")}]`);
|
|
691
|
+
}
|
|
692
|
+
if (node.dependents.length > 0) {
|
|
693
|
+
lines.push(` dependents: [${node.dependents.join(", ")}]`);
|
|
694
|
+
}
|
|
695
|
+
if (Object.keys(node.signals).length > 0) {
|
|
696
|
+
lines.push(` signals:`);
|
|
697
|
+
for (const [k, s] of Object.entries(node.signals)) {
|
|
698
|
+
const subs = s.subscribers.map((sub) => sub.name || sub.id).join(", ");
|
|
699
|
+
lines.push(` ${k}: { value: ${JSON.stringify(s.value)}, subscribers: [${subs}] }`);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return lines.join("\n");
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
function findChildComponents(node, set) {
|
|
708
|
+
if (!node || typeof node !== "object") return;
|
|
709
|
+
const agentId = node.agentId || node.props?.["data-agent-id"];
|
|
710
|
+
if (agentId) {
|
|
711
|
+
const compId = agentId.split(":")[0];
|
|
712
|
+
if (compId) set.add(compId);
|
|
713
|
+
}
|
|
714
|
+
if (Array.isArray(node.children)) {
|
|
715
|
+
for (const child of node.children) {
|
|
716
|
+
findChildComponents(child, set);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
var OBSERVER_VERSION = "0.2.0";
|
|
721
|
+
|
|
722
|
+
export {
|
|
723
|
+
getSemanticSnapshot,
|
|
724
|
+
observe,
|
|
725
|
+
buildDependencyGraph,
|
|
726
|
+
OBSERVER_VERSION
|
|
727
|
+
};
|