@tsplc/tmp-debug 0.0.0-stage → 0.6.2

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.
Files changed (78) hide show
  1. package/README.md +38 -2
  2. package/dist/api.js +744 -0
  3. package/dist/api.js.map +12 -0
  4. package/dist/constant.js +33 -0
  5. package/dist/constant.js.map +10 -0
  6. package/dist/explain-file.js +468 -0
  7. package/dist/explain-file.js.map +11 -0
  8. package/dist/explain.js +1310 -0
  9. package/dist/explain.js.map +19 -0
  10. package/dist/format.js +214 -0
  11. package/dist/format.js.map +12 -0
  12. package/dist/index.js +4066 -0
  13. package/dist/index.js.map +44 -0
  14. package/dist/manifest.js +240 -0
  15. package/dist/manifest.js.map +10 -0
  16. package/dist/paths.js +242 -0
  17. package/dist/paths.js.map +10 -0
  18. package/package.json +72 -4
  19. package/src/adapter.ts +77 -0
  20. package/src/api.ts +446 -0
  21. package/src/app-server.ts +151 -0
  22. package/src/build-files.ts +46 -0
  23. package/src/constant.ts +41 -0
  24. package/src/debugger.ts +237 -0
  25. package/src/errors.ts +13 -0
  26. package/src/explain-expressions.ts +484 -0
  27. package/src/explain-file.ts +325 -0
  28. package/src/explain-frames.ts +155 -0
  29. package/src/explain-loops.ts +151 -0
  30. package/src/explain-operations.ts +232 -0
  31. package/src/explain-tables.ts +345 -0
  32. package/src/explain-value.ts +203 -0
  33. package/src/explain-verdicts.ts +548 -0
  34. package/src/explain.ts +82 -0
  35. package/src/hardware.ts +159 -0
  36. package/src/http.ts +218 -0
  37. package/src/index.ts +44 -0
  38. package/src/jobs.ts +139 -0
  39. package/src/login-value.ts +165 -0
  40. package/src/login.ts +315 -0
  41. package/src/manifest.ts +328 -0
  42. package/src/opcua.ts +80 -0
  43. package/src/paths.ts +363 -0
  44. package/src/requests.ts +99 -0
  45. package/src/server-cursor.ts +22 -0
  46. package/src/shown.ts +229 -0
  47. package/src/telemetry.ts +125 -0
  48. package/src/timeline.ts +148 -0
  49. package/src/trace.ts +62 -0
  50. package/src/ui/bits.ts +48 -0
  51. package/src/ui/client.ts +87 -0
  52. package/src/ui/dom.ts +61 -0
  53. package/src/ui/explain-read.ts +105 -0
  54. package/src/ui/format.ts +261 -0
  55. package/src/ui/hardware.ts +425 -0
  56. package/src/ui/header.ts +207 -0
  57. package/src/ui/health.ts +64 -0
  58. package/src/ui/index.ts +27 -0
  59. package/src/ui/jobs.ts +263 -0
  60. package/src/ui/links.ts +59 -0
  61. package/src/ui/live-value.ts +85 -0
  62. package/src/ui/main.ts +76 -0
  63. package/src/ui/overview.ts +178 -0
  64. package/src/ui/page.ts +286 -0
  65. package/src/ui/pages.ts +64 -0
  66. package/src/ui/recent-window.ts +49 -0
  67. package/src/ui/recent.ts +355 -0
  68. package/src/ui/request-trace.ts +38 -0
  69. package/src/ui/requests.ts +80 -0
  70. package/src/ui/routes.ts +98 -0
  71. package/src/ui/store.ts +295 -0
  72. package/src/ui/success-cache.ts +11 -0
  73. package/src/ui/tree.ts +162 -0
  74. package/src/ui/variables.ts +597 -0
  75. package/src/ui/why-watch.ts +11 -0
  76. package/src/ui/why.ts +694 -0
  77. package/src/values.ts +170 -0
  78. package/src/watch.ts +32 -0
@@ -0,0 +1,328 @@
1
+ import { z } from "zod";
2
+
3
+ /**
4
+ * What a build tells the debugger: every TypeScript identity it can show, and the target symbol each one reads.
5
+ * TypeScript paths (`bench.instruments[10].phase`) are the identity; `target` strings are opaque to
6
+ * everything except the target's adapter (for CODESYS, IEC symbol paths such as `Bench.instruments…`).
7
+ * Written by the compiler beside its emitted ST and shipped with each release, as `debugManifestFile`.
8
+ */
9
+ export const debugManifestFile = "debug.json";
10
+
11
+ /** A location in the project's own sources: `file` indexes `sources`; lines and columns are 1-based. */
12
+ export const sourceRange = z.object({
13
+ file: z.number().int(),
14
+ line: z.number().int(),
15
+ column: z.number().int(),
16
+ endLine: z.number().int(),
17
+ endColumn: z.number().int(),
18
+ });
19
+
20
+ /** Any JSON value. */
21
+ export const json: z.ZodType<unknown> = z.lazy(() =>
22
+ z.union([
23
+ z.string(),
24
+ z.number(),
25
+ z.boolean(),
26
+ z.null(),
27
+ z.array(json),
28
+ z.record(z.string(), json),
29
+ ]),
30
+ );
31
+
32
+ export const debugField = z.object({
33
+ name: z.string(),
34
+ target: z.string(),
35
+ type: z.string(),
36
+ declaration: sourceRange.optional(),
37
+ /** Compiler-made storage, such as an async job's `step`; hidden unless asked for. */
38
+ generated: z.literal(true).optional(),
39
+ });
40
+
41
+ /** Keys of `types` are opaque ids; scalars use their IEC name, such as `BOOL`. */
42
+ export const debugType = z.discriminatedUnion("kind", [
43
+ z.object({
44
+ kind: z.literal("scalar"),
45
+ iec: z.string(),
46
+ /** A STRING's or WSTRING's capacity in characters. */
47
+ capacity: z.number().int().optional(),
48
+ }),
49
+ z.object({
50
+ kind: z.literal("enum"),
51
+ name: z.string(),
52
+ members: z.record(z.string(), z.number().int()),
53
+ /** For a plain TypeScript program's string-literal union, the string each member stands for. */
54
+ texts: z.record(z.string(), z.string()).optional(),
55
+ }),
56
+ z.object({
57
+ kind: z.literal("record"),
58
+ name: z.string(),
59
+ declaration: sourceRange.optional(),
60
+ fields: z.array(debugField),
61
+ }),
62
+ z.object({
63
+ kind: z.literal("array"),
64
+ element: z.string(),
65
+ length: z.number().int(),
66
+ /** The first index; TypeScript paths use the target's own indexes, `lower` to `lower + length - 1`. */
67
+ lower: z.number().int(),
68
+ /** For `Record<Enum, T>`, the enum whose member values are the indexes. */
69
+ index: z.string().optional(),
70
+ }),
71
+ ]);
72
+
73
+ export const debugRoot = z.object({
74
+ path: z.string(),
75
+ target: z.string(),
76
+ type: z.string(),
77
+ declaration: sourceRange.optional(),
78
+ /** Readable at runtime through the target's adapter; for CODESYS, all stored state is. */
79
+ readable: z.boolean(),
80
+ generated: z.literal(true).optional(),
81
+ });
82
+
83
+ /** A field the compiler folded away: it has no runtime storage, but its value is known. */
84
+ export const debugConstant = z.object({
85
+ path: z.string(),
86
+ type: z.string(),
87
+ /** Integers and durations as `encodeConstant` writes them. */
88
+ value: json,
89
+ declaration: sourceRange.optional(),
90
+ });
91
+
92
+ export const debugIo = z.object({
93
+ path: z.string(),
94
+ target: z.string(),
95
+ direction: z.enum(["input", "output"]),
96
+ type: z.string(),
97
+ device: z.string().optional(),
98
+ channel: z.string().optional(),
99
+ address: z.string().optional(),
100
+ inverted: z.literal(true).optional(),
101
+ shared: z.literal(true).optional(),
102
+ });
103
+
104
+ export const debugChannel = z.object({
105
+ id: z.string(),
106
+ name: z.string(),
107
+ direction: z.enum(["input", "output"]),
108
+ type: z.string(),
109
+ offsetBits: z.number().int(),
110
+ bits: z.number().int(),
111
+ address: z.string().optional(),
112
+ unit: z.string().optional(),
113
+ description: z.string().optional(),
114
+ });
115
+
116
+ export const debugDevice = z.object({
117
+ id: z.string(),
118
+ product: z.string().optional(),
119
+ vendor: z.string().optional(),
120
+ productCode: z.string().optional(),
121
+ network: z
122
+ .object({ address: z.string(), rpiMs: z.number().optional() })
123
+ .optional(),
124
+ connection: z.string().optional(),
125
+ assemblies: z.array(
126
+ z.object({
127
+ direction: z.enum(["input", "output"]),
128
+ bytes: z.number().int(),
129
+ address: z.string().optional(),
130
+ /** A byte array over the whole assembly, mapped or not. */
131
+ raw: z.string().optional(),
132
+ }),
133
+ ),
134
+ /**
135
+ * Target paths of the driver's own state, such as a generated adapter's `eState`; absent means
136
+ * the device's health is unknown.
137
+ */
138
+ health: z
139
+ .object({
140
+ state: z.string().optional(),
141
+ states: z.record(z.string(), z.number().int()).optional(),
142
+ diagnostic: z.string().optional(),
143
+ message: z.string().optional(),
144
+ })
145
+ .optional(),
146
+ /** Status symbols by label: hand-configured ones, and a generated Modbus server's error. */
147
+ status: z.record(z.string(), z.string()).optional(),
148
+ channels: z.array(debugChannel),
149
+ });
150
+
151
+ /** A specialized function: one per method per instance, so `reads` resolve to concrete paths. */
152
+ export const debugFunction = z.object({
153
+ id: z.number().int(),
154
+ name: z.string(),
155
+ declaration: sourceRange,
156
+ /** The TypeScript path of `this`, for a method specialized to one instance. */
157
+ instance: z.string().optional(),
158
+ task: z.string().optional(),
159
+ target: z.string(),
160
+ /** Member reads in the body whose runtime path is static in this specialization. */
161
+ reads: z.array(z.object({ range: sourceRange, path: z.string() })),
162
+ });
163
+
164
+ /** The fields of a job's state, by name, with the IEC type the generated job block declares. */
165
+ export const jobStateFields = {
166
+ step: "UINT",
167
+ cancelling: "BOOL",
168
+ busy: "BOOL",
169
+ done: "BOOL",
170
+ error: "UDINT",
171
+ } as const;
172
+
173
+ /**
174
+ * The fields the compiler adds to a block for the debugger, by key, with their IEC types: copies
175
+ * of a job's `busy` and `step` for its trace, when it last started, and a service call's job and
176
+ * request counter.
177
+ */
178
+ export const instrumentationFields = {
179
+ busy: jobStateFields.busy,
180
+ step: jobStateFields.step,
181
+ startedAt: "UDINT",
182
+ job: "UDINT",
183
+ sequence: "UDINT",
184
+ } as const;
185
+
186
+ export const debugJob = z.object({
187
+ id: z.number().int(),
188
+ name: z.string(),
189
+ declaration: sourceRange,
190
+ instance: z.string().optional(),
191
+ /** The job's state, with the fields of `jobStateFields`. */
192
+ target: z.string(),
193
+ steps: z.array(
194
+ z.object({
195
+ step: z.number().int(),
196
+ kind: z.enum(["await", "return", "throw", "statement"]),
197
+ range: sourceRange,
198
+ text: z.string(),
199
+ }),
200
+ ),
201
+ /**
202
+ * The PLC clock in milliseconds when the job last started: the clock of telemetry records'
203
+ * `time` and a batch's `now`.
204
+ */
205
+ startedAt: z.string().optional(),
206
+ /**
207
+ * Service calls the job awaits, its nested jobs' included; `sequence` is the request counter sent
208
+ * as `x-tsplc-request`, with `job`'s id when a nested job makes the call.
209
+ */
210
+ requests: z.array(
211
+ z.object({
212
+ service: z.string(),
213
+ target: z.string(),
214
+ sequence: z.string(),
215
+ job: z.number().int().optional(),
216
+ }),
217
+ ),
218
+ /** Made by the compiler, such as a service call's own job; hidden unless asked for. */
219
+ generated: z.literal(true).optional(),
220
+ });
221
+
222
+ /**
223
+ * Where `debug.trace` records: an enum state field changing, a decision function returning a
224
+ * different value, or a job starting or finishing. Trace records name their site by `id`.
225
+ */
226
+ export const debugEventSite = z.object({
227
+ id: z.number().int(),
228
+ kind: z.enum(["state", "decision", "job"]),
229
+ /** For `kind: "job"`: the job, and whether it starts or finishes at this site. */
230
+ job: z.number().int().optional(),
231
+ phase: z.enum(["started", "finished"]).optional(),
232
+ /** The TypeScript path written, or of the job. */
233
+ path: z.string(),
234
+ function: z.number().int().optional(),
235
+ type: z.string(),
236
+ range: sourceRange,
237
+ text: z.string(),
238
+ task: z.string(),
239
+ /**
240
+ * Guard values captured in the same scan, at the write: each `if` condition leaf enclosing it.
241
+ * `stable` is false when the body between the guard and the write can change the leaf.
242
+ */
243
+ operands: z.array(
244
+ z.object({
245
+ text: z.string(),
246
+ range: sourceRange,
247
+ type: z.string(),
248
+ path: z.string().optional(),
249
+ stable: z.boolean(),
250
+ }),
251
+ ),
252
+ });
253
+
254
+ /** A writer slot's value before any site writes it; site ids count from 1. */
255
+ export const neverWrittenSlot = 0;
256
+
257
+ /** The IEC type of a writer slot, which holds the id of the site that last wrote its output. */
258
+ export const writerSlotType = "UINT";
259
+
260
+ /** The statement that last wrote each output, per scan. */
261
+ export const debugWriter = z.object({
262
+ output: z.string(),
263
+ target: z.string(),
264
+ /** Every statement that can write `output` is probed, so the slot names its last writer exactly. */
265
+ exact: z.boolean().optional(),
266
+ sites: z.array(
267
+ z.object({
268
+ id: z.number().int(),
269
+ range: sourceRange,
270
+ function: z.number().int(),
271
+ /** The `explain.json` assignments this site is, one per instance that runs it. */
272
+ assignments: z.array(z.number().int()).optional(),
273
+ }),
274
+ ),
275
+ });
276
+
277
+ /** The IEC type of a task's scan clock. */
278
+ export const scanClockType = "DINT";
279
+
280
+ export const debugTask = z.object({
281
+ name: z.string(),
282
+ intervalMs: z.number(),
283
+ priority: z.number().int(),
284
+ /** The PLC clock in milliseconds, of type `scanClockType`, as the task's first program read it this scan. */
285
+ clock: z.string().optional(),
286
+ });
287
+
288
+ export const debugManifest = z.object({
289
+ kind: z.literal("tsplc-debug"),
290
+ version: z.literal(1),
291
+ build: z.object({
292
+ id: z.string(),
293
+ /** A variable holding `id`, to tell which build the controller runs. */
294
+ target: z.string(),
295
+ compiler: z.string(),
296
+ }),
297
+ /** Project-relative paths. */
298
+ sources: z.array(z.string()),
299
+ types: z.record(z.string(), debugType),
300
+ roots: z.array(debugRoot),
301
+ constants: z.array(debugConstant),
302
+ io: z.array(debugIo),
303
+ devices: z.array(debugDevice),
304
+ functions: z.array(debugFunction),
305
+ jobs: z.array(debugJob),
306
+ events: z.array(debugEventSite),
307
+ writers: z.array(debugWriter),
308
+ tasks: z.array(debugTask),
309
+ /** Job error codes by number: 1 cancelled, 2 timeout… */
310
+ errors: z.record(z.string(), z.string()),
311
+ /** The digest of the build's `explain.json`; absent when the build has no explanations. */
312
+ explain: z.object({ sha256: z.string() }).optional(),
313
+ });
314
+
315
+ export type SourceRange = z.infer<typeof sourceRange>;
316
+ export type DebugType = z.infer<typeof debugType>;
317
+ export type DebugField = z.infer<typeof debugField>;
318
+ export type DebugRoot = z.infer<typeof debugRoot>;
319
+ export type DebugConstant = z.infer<typeof debugConstant>;
320
+ export type DebugIo = z.infer<typeof debugIo>;
321
+ export type DebugDevice = z.infer<typeof debugDevice>;
322
+ export type DebugChannel = z.infer<typeof debugChannel>;
323
+ export type DebugFunction = z.infer<typeof debugFunction>;
324
+ export type DebugJob = z.infer<typeof debugJob>;
325
+ export type DebugEventSite = z.infer<typeof debugEventSite>;
326
+ export type DebugWriter = z.infer<typeof debugWriter>;
327
+ export type DebugTask = z.infer<typeof debugTask>;
328
+ export type DebugManifest = z.infer<typeof debugManifest>;
package/src/opcua.ts ADDED
@@ -0,0 +1,80 @@
1
+ import type { Environment, ResolvedTsplcConfig } from "@tsplc/config";
2
+ import {
3
+ createMonitor,
4
+ type MonitorOptions,
5
+ monitorConnection,
6
+ monitorPassword,
7
+ type Reading,
8
+ } from "@tsplc/opcua";
9
+ import type { TargetAdapter, TargetRead, TargetSample } from "./adapter";
10
+
11
+ /** The config's `monitor` connection, with the user's password from `passwordEnv` in `env`. */
12
+ export const opcUaConnection = (
13
+ monitor: NonNullable<ResolvedTsplcConfig["monitor"]>,
14
+ options: { root: string; env: Environment },
15
+ ): MonitorOptions =>
16
+ monitorConnection(monitor, {
17
+ root: options.root,
18
+ password: monitorPassword(monitor, options.env),
19
+ });
20
+
21
+ /**
22
+ * Reads a CODESYS controller over OPC UA, where targets are IEC symbol paths. It reads only
23
+ * what the build publishes, so it suits a local virtual PLC.
24
+ */
25
+ export const createOpcUaAdapter = (options: MonitorOptions): TargetAdapter => {
26
+ const monitor = createMonitor(options);
27
+ const controller = new AbortController();
28
+ const running = monitor.run(controller.signal);
29
+ const sample = ({ value, quality, status }: Reading): TargetSample => ({
30
+ value,
31
+ quality,
32
+ status,
33
+ receivedAt: new Date().toISOString(),
34
+ });
35
+ const names = (targets: readonly TargetRead[]) =>
36
+ targets.map(({ target }) => target);
37
+
38
+ return {
39
+ method: "OPC UA",
40
+ endpoint: options.endpoint,
41
+ samplingMs: monitor.samplingMs,
42
+ connection: monitor.status,
43
+ onConnection: monitor.onStatus,
44
+ read: async (targets) =>
45
+ new Map(
46
+ Object.entries(await monitor.read(names(targets))).map(
47
+ ([target, reading]) => [target, sample(reading)],
48
+ ),
49
+ ),
50
+ subscribe: (targets, listener, intervalMs = monitor.samplingMs) => {
51
+ const latest = new Map<string, { reading: Reading; at: number }>();
52
+ const report = (target: string, reading: Reading) => {
53
+ latest.set(target, { reading, at: Date.now() });
54
+ listener(target, sample(reading));
55
+ };
56
+ const stop = monitor.watch(names(targets), report);
57
+ // OPC UA reports a value only when it changes. While the session holds, an unchanged value
58
+ // is still current, so it is reported again each interval.
59
+ const repeat = setInterval(() => {
60
+ if (monitor.status().state !== "connected") {
61
+ return;
62
+ }
63
+ const now = Date.now();
64
+ for (const [target, { reading, at }] of latest) {
65
+ if (now - at >= intervalMs) {
66
+ report(target, reading);
67
+ }
68
+ }
69
+ }, intervalMs);
70
+ return () => {
71
+ clearInterval(repeat);
72
+ stop();
73
+ };
74
+ },
75
+ close: async () => {
76
+ controller.abort();
77
+ await running;
78
+ },
79
+ };
80
+ };