Repository navigation
Expand file tree
/
Copy pathEdgeCompiler.ts
More file actions
1361 lines (1203 loc) · 48.4 KB
/
Copy pathEdgeCompiler.ts
File metadata and controls
1361 lines (1203 loc) · 48.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* HoloScript → Edge (ARM64 / Local-LLM) Compiler
*
* Emits a self-contained Python deployment bundle for any Ollama-capable edge
* device: Jetson Orin, Raspberry Pi 5, The Unit, or any Linux ARM64 / x64 node.
*
* Trait-driven output:
* @LocalInference → Ollama /api/chat inference loop (always included)
* @EdgeNode → board task claiming + local-first dispatch config
* @SystemMonitor → generic CPU/RAM/disk health poller
* @JetsonGPU → CUDA power-mode config + NVIDIA-specific sections
* @TegraMonitor → tegrastats reader (Jetson-only extension)
* @TensorRTInference → TensorRT quantized-model engine loader
* ros2_actuation / @ROS2Node → ROS2 bridge node
*
* Output: JSON manifest with inline file contents. Extract and write to device
* with the CLI (`holoscript edge deploy`) or the mcp-server compile_to_edge tool.
*/
import { CompilerBase } from './CompilerBase';
import { ANSCapabilityPath, type ANSCapabilityPathValue } from '@holoscript/core-types/ans';
import type { HoloComposition, HoloObjectTrait } from '../parser/HoloCompositionTypes';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface EdgeCompilerOptions {
/** Ollama base URL on the target device (default: http://localhost:11434) */
ollamaUrl?: string;
/** Primary inference model (default: qwen3:4b) */
model?: string;
/** Target platform (default: linux-arm64) */
platform?: 'linux-arm64' | 'linux-x64' | 'windows-x64';
/** Remote deploy path on the device (default: /opt/holoscript) */
remotePath?: string;
/** systemd service user (default: root) */
serviceUser?: string;
/**
* Edge runtime the generated unit runs:
* 'python' → standalone Python agent.py (thin Ollama loop) — DEFAULT (back-compat).
* 'agentrunner' → the canonical TS AgentRunner (`index.js run`) with the full gate
* stack (artifact-grounding W.107.b, reflect self-eval, CAEL hash-chain,
* content-hashed/signed hardware receipts, native on_task cognitive
* verbs). Use for sovereign nodes (jetson-orin) that must not regress
* those gates onto the thinner Python agent.
*/
runtime?: 'python' | 'agentrunner';
}
export interface EdgeBundleFile {
path: string;
content: string;
executable?: boolean;
}
export interface EdgeBundle {
name: string;
target: 'edge';
platform: string;
files: EdgeBundleFile[];
config: {
ollamaUrl: string;
model: string;
hasLocalInference: boolean;
hasEdgeNode: boolean;
hasSystemMonitor: boolean;
hasJetsonGPU: boolean;
hasTegraMonitor: boolean;
hasTensorRT: boolean;
hasROS2: boolean;
/** Isaac ROS 2 (NVIDIA acceleration layer — NITROS transport, CuVSLAM, etc.) */
hasIsaacROS2: boolean;
/** Which edge runtime the generated systemd unit runs. */
runtime: 'python' | 'agentrunner';
};
deployInstructions: string;
/** Set when a deprecated runtime mode was used (e.g. the legacy gate-less 'python'). */
deprecation?: string;
}
// ---------------------------------------------------------------------------
// EdgeCompiler
// ---------------------------------------------------------------------------
export class EdgeCompiler extends CompilerBase {
protected readonly compilerName = 'EdgeCompiler';
protected override getRequiredCapability(): ANSCapabilityPathValue {
return ANSCapabilityPath.EDGE;
}
private opts: Required<EdgeCompilerOptions>;
constructor(options: EdgeCompilerOptions = {}) {
super();
this.opts = {
ollamaUrl: options.ollamaUrl ?? 'http://localhost:11434',
model: options.model ?? 'qwen3:4b',
platform: options.platform ?? 'linux-arm64',
remotePath: options.remotePath ?? '/opt/holoscript',
serviceUser: options.serviceUser ?? 'root',
// DEFAULT = the canonical TS AgentRunner (full gate stack). 'python' is the
// deprecated gate-less legacy agent (retirement tracked; see /executioner).
runtime: options.runtime ?? 'agentrunner',
};
}
// ── Trait detection ─────────────────────────────────────────────────────
private collectTraitNames(composition: HoloComposition): Set<string> {
const names = new Set<string>();
const addTrait = (t: HoloObjectTrait | string) => {
const n = typeof t === 'string' ? t : t.name;
names.add(n.toLowerCase().replace(/@/g, ''));
};
// Composition-level traits
composition.traits?.forEach(addTrait);
// Object-level traits
composition.objects?.forEach((obj) => obj.traits?.forEach(addTrait));
// Template-level traits
composition.templates?.forEach((tpl) =>
tpl.properties?.forEach((p) => {
if ('traits' in p) (p as { traits?: HoloObjectTrait[] }).traits?.forEach(addTrait);
})
);
return names;
}
private detect(names: Set<string>, ...keys: string[]): boolean {
return keys.some((k) => names.has(k));
}
// ── Compile ─────────────────────────────────────────────────────────────
compile(composition: HoloComposition, agentToken: string, outputPath?: string): string {
this.validateCompilerAccess(agentToken, outputPath);
const traits = this.collectTraitNames(composition);
const name = composition.name ?? 'holoscript-edge-agent';
const ollamaUrl = this.opts.ollamaUrl;
const model = this.opts.model;
const remotePath = this.opts.remotePath;
const platform = this.opts.platform;
const serviceUser = this.opts.serviceUser;
const hasLocalInference = this.detect(
traits,
'localinference',
'local_inference',
'local-inference'
);
const hasEdgeNode = this.detect(
traits,
'edgenode',
'edge_node',
'edge-node',
'sovereign_agent'
);
const hasSystemMonitor = this.detect(
traits,
'systemmonitor',
'system_monitor',
'system-monitor'
);
const hasJetsonGPU = this.detect(traits, 'jetsongpu', 'jetson_gpu', 'jetson-gpu', 'jetson');
const hasTegraMonitor = this.detect(traits, 'tegramonitor', 'tegra_monitor', 'tegrastats');
const hasTensorRT = this.detect(traits, 'tensorrtinference', 'tensorrt_inference', 'tensorrt');
const hasROS2 = this.detect(traits, 'ros2node', 'ros2_actuation', 'ros2-node', 'ros2actuation');
// Isaac ROS 2: NVIDIA hardware-accelerated ROS2 layer (NITROS transport,
// CuVSLAM, Isaac Manipulator). Closes CG-108 for Jetson + JetPack 6+.
const hasIsaacROS2 = this.detect(
traits,
'isaac_ros2',
'isaac-ros2',
'isaac_ros',
'isaacros2',
'isaacros',
'nova_carter',
'novacarter',
'isaac_manipulator',
'isaacmanipulator'
);
const isAgentRunner = this.opts.runtime === 'agentrunner';
const files: EdgeBundleFile[] = isAgentRunner
? [
// Canonical TS AgentRunner runtime — the gate stack (artifact-grounding,
// reflect), CAEL hash-chain, content-hashed/signed hardware receipts, and
// native on_task cognitive-verb consumption all ship in `index.js run`.
// No agent.py: the TS package is the runtime, not a generated Python loop.
{
path: 'holoscript_agent.service',
content: this.genAgentRunnerSystemd(name, ollamaUrl, model, remotePath, serviceUser),
executable: false,
},
{
path: 'setup.sh',
content: this.genAgentRunnerSetup(name, model, remotePath, serviceUser, hasJetsonGPU),
executable: true,
},
{
path: 'manifest.json',
content: JSON.stringify(
{
name,
target: 'edge',
platform,
runtime: 'agentrunner',
ollamaUrl,
model,
generatedBy: 'EdgeCompiler',
hasIsaacROS2,
},
null,
2
),
executable: false,
},
]
: [
{
path: 'agent.py',
content: this.genAgent(name, ollamaUrl, model, hasEdgeNode, hasTensorRT),
executable: true,
},
{
path: 'monitor.py',
content: this.genMonitor(name, hasJetsonGPU, hasTegraMonitor),
executable: true,
},
{
path: 'setup.sh',
content: this.genSetup(
name,
model,
remotePath,
platform,
serviceUser,
hasJetsonGPU,
hasROS2,
hasIsaacROS2
),
executable: true,
},
{
path: 'holoscript_agent.service',
content: this.genSystemd(name, ollamaUrl, model, remotePath, serviceUser, hasROS2),
executable: false,
},
{
path: 'manifest.json',
content: JSON.stringify(
{
name,
target: 'edge',
platform,
runtime: 'python',
ollamaUrl,
model,
generatedBy: 'EdgeCompiler',
hasIsaacROS2,
},
null,
2
),
executable: false,
},
];
if (hasROS2) {
// Colcon-buildable ament_python package — replaces standalone ros2_bridge.py.
// Build with: cd ros2_ws && colcon build --symlink-install
const pkgName = name.replace(/[^a-z0-9_]/gi, '_').toLowerCase() + '_bridge';
files.push({
path: `ros2_ws/src/${pkgName}/package.xml`,
content: this.genColconPackageXml(pkgName, hasIsaacROS2),
});
files.push({
path: `ros2_ws/src/${pkgName}/setup.py`,
content: this.genColconSetupPy(pkgName),
});
files.push({
path: `ros2_ws/src/${pkgName}/setup.cfg`,
content: `[develop]\nscript_dir=$base/lib/${pkgName}\n[install]\ninstall_scripts=$base/lib/${pkgName}\n`,
});
files.push({ path: `ros2_ws/src/${pkgName}/${pkgName}/__init__.py`, content: '' });
files.push({
path: `ros2_ws/src/${pkgName}/${pkgName}/bridge.py`,
content: this.genROS2Bridge(name, pkgName),
executable: true,
});
}
if (hasIsaacROS2) {
const pkgName = name.replace(/[^a-z0-9_]/gi, '_').toLowerCase() + '_bridge';
files.push({
path: `ros2_ws/src/${pkgName}/${pkgName}/isaac_bridge.py`,
content: this.genIsaacBridge(name, pkgName),
executable: true,
});
}
if (hasTensorRT && !isAgentRunner) {
// tensorrt_loader.py is imported by the Python agent.py — irrelevant under the
// TS AgentRunner runtime (which loads its model via Ollama/the provider layer).
files.push({
path: 'tensorrt_loader.py',
content: this.genTensorRTLoader(model),
executable: false,
});
}
const bundle: EdgeBundle = {
name,
target: 'edge',
platform,
files,
config: {
ollamaUrl,
model,
hasLocalInference,
hasEdgeNode,
hasSystemMonitor,
hasJetsonGPU,
hasTegraMonitor,
hasTensorRT,
hasROS2,
hasIsaacROS2,
runtime: this.opts.runtime,
},
deployInstructions: this.genDeployInstructions(
name,
ollamaUrl,
remotePath,
platform,
hasROS2
),
...(isAgentRunner
? {}
: {
deprecation:
"The 'python' edge runtime is a GATE-LESS legacy agent (no artifact-grounding, " +
'reflect, CAEL hash-chain, or signed hardware receipts). Prefer runtime:"agentrunner" ' +
'(the canonical TS AgentRunner). Scheduled for retirement.',
}),
};
return JSON.stringify(bundle, null, 2);
}
// ── File generators ─────────────────────────────────────────────────────
private genAgent(
name: string,
ollamaUrl: string,
model: string,
hasEdgeNode: boolean,
hasTensorRT: boolean
): string {
const boardSection = hasEdgeNode
? `
BOARD_URL = os.getenv("HOLOSCRIPT_BOARD_URL", "https://mcp-orchestrator-production-45f9.up.railway.app")
AGENT_TOKEN = os.getenv("HOLOMESH_API_KEY", "")
def claim_task() -> dict | None:
"""Claim a board task tagged for this node."""
try:
r = requests.get(f"{BOARD_URL}/api/holomesh/team/board", headers={"Authorization": f"Bearer {AGENT_TOKEN}"}, timeout=10)
r.raise_for_status()
tasks = r.json().get("tasks", [])
claimable = [t for t in tasks if t.get("status") == "open" and "edge-node" in t.get("tags", [])]
if not claimable:
return None
task = claimable[0]
requests.patch(f"{BOARD_URL}/api/holomesh/team/board/{task['id']}/claim", headers={"Authorization": f"Bearer {AGENT_TOKEN}"}, timeout=10)
return task
except Exception as e:
print(f"[board] claim error: {e}")
return None
`
: '';
const tensorrtImport = hasTensorRT ? 'from tensorrt_loader import load_trt_engine\n' : '';
return `#!/usr/bin/env python3
"""
HoloScript Edge Agent — ${name}
Generated by EdgeCompiler. Target: ${ollamaUrl}
"""
import json, os, sys, time, requests
${tensorrtImport}
OLLAMA_URL = os.getenv("OLLAMA_URL", "${ollamaUrl}")
MODEL = os.getenv("HOLOSCRIPT_MODEL", "${model}")
TICK_INTERVAL = int(os.getenv("TICK_INTERVAL_S", "10"))
${boardSection}
HOLOSCRIPT_TOOLS = [
{
"type": "function",
"function": {
"name": "write_file",
"description": "Write content to a file on the edge device",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "File path"},
"content": {"type": "string", "description": "File content"},
},
"required": ["path", "content"],
},
},
},
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read a file from the edge device",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
},
"required": ["path"],
},
},
},
{
"type": "function",
"function": {
"name": "bash",
"description": "Run a shell command",
"parameters": {
"type": "object",
"properties": {
"command": {"type": "string"},
},
"required": ["command"],
},
},
},
]
def chat(messages: list, tools: list | None = None) -> dict:
"""Call Ollama /api/chat (native endpoint — correct tool_calls parsing)."""
payload = {"model": MODEL, "messages": messages, "stream": False}
if tools:
payload["tools"] = tools
resp = requests.post(f"{OLLAMA_URL}/api/chat", json=payload, timeout=120)
resp.raise_for_status()
return resp.json()
def dispatch_tool(name: str, args: dict) -> str:
"""Execute a tool call and return the result string."""
import subprocess
from pathlib import Path
if name == "write_file":
path, content = args.get("path", ""), args.get("content", "")
Path(path).parent.mkdir(parents=True, exist_ok=True)
Path(path).write_text(content)
return f"wrote {len(content)} bytes to {path}"
if name == "read_file":
try:
return Path(args["path"]).read_text()
except FileNotFoundError:
return f"[error] file not found: {args['path']}"
if name == "bash":
result = subprocess.run(args["command"], shell=True, capture_output=True, text=True, timeout=30)
return (result.stdout + result.stderr).strip() or "(no output)"
return f"[error] unknown tool: {name}"
def run_task(prompt: str) -> str:
"""Run an agentic task to completion with tool-use loop."""
messages = [{"role": "user", "content": prompt}]
for _ in range(10): # max 10 rounds
resp = chat(messages, tools=HOLOSCRIPT_TOOLS)
msg = resp.get("message", {})
messages.append(msg)
tool_calls = msg.get("tool_calls") or []
if not tool_calls:
return msg.get("content", "")
for tc in tool_calls:
fn = tc.get("function", {})
result = dispatch_tool(fn.get("name", ""), fn.get("arguments", {}))
messages.append({"role": "tool", "content": result})
return "[agent] max rounds reached"
def _start_monitor():
"""Start the health monitor HTTP server as a daemon thread."""
try:
import sys as _sys, os as _os
_sys.path.insert(0, _os.path.dirname(_os.path.abspath(__file__)))
from monitor import main as _monitor_main
import threading as _threading
_threading.Thread(target=_monitor_main, daemon=True).start()
print(f"[${name}] monitor thread started on :9090")
except Exception as _e:
print(f"[${name}] monitor start failed (non-fatal): {_e}")
def main():
_start_monitor()
print(f"[${name}] HoloScript edge agent online — {OLLAMA_URL} model={MODEL}")
${
hasEdgeNode
? `
while True:
task = claim_task()
if task:
print(f"[board] claimed task {task['id']}: {task.get('title', '')}")
result = run_task(task.get("description", task.get("title", "")))
print(f"[board] done: {result[:200]}")
else:
time.sleep(TICK_INTERVAL)
`
: `
prompt = " ".join(sys.argv[1:]) or "Write a valid HoloScript .holo scene to agent-out/scene.holo"
print(run_task(prompt))
`
}
if __name__ == "__main__":
main()
`;
}
private genMonitor(name: string, hasJetsonGPU: boolean, hasTegraMonitor: boolean): string {
const jetsonSection =
hasJetsonGPU || hasTegraMonitor
? `
def jetson_stats() -> dict:
"""Read NVIDIA Jetson hardware stats via tegrastats snapshot."""
import subprocess, re
try:
raw = subprocess.run(
["tegrastats", "--interval", "1", "--stop"],
capture_output=True, text=True, timeout=5
).stdout.strip()
# Parse RAM: "RAM 1234/8096MB"
ram_match = re.search(r"RAM (\\d+)/(\\d+)MB", raw)
# Parse CPU: "CPU [35%@1510,18%@1510,...]"
cpu_match = re.search(r"CPU \\[([^\\]]+)\\]", raw)
# Parse power: "POM_5V_IN 3500/3500"
pow_match = re.search(r"POM_5V_IN (\\d+)/(\\d+)", raw)
return {
"ram_used_mb": int(ram_match.group(1)) if ram_match else None,
"ram_total_mb": int(ram_match.group(2)) if ram_match else None,
"cpu_cores": cpu_match.group(1) if cpu_match else None,
"power_mw": int(pow_match.group(1)) if pow_match else None,
"raw": raw,
}
except Exception as e:
return {"error": str(e)}
`
: '';
return `#!/usr/bin/env python3
"""
HoloScript Edge Monitor — ${name}
Polls system health and exposes a /health HTTP endpoint.
"""
import json, os, platform, time, threading
from http.server import HTTPServer, BaseHTTPRequestHandler
${hasJetsonGPU || hasTegraMonitor ? 'import subprocess, re' : ''}
MONITOR_PORT = int(os.getenv("MONITOR_PORT", "9090"))
POLL_INTERVAL_S = int(os.getenv("POLL_INTERVAL_S", "5"))
_stats: dict = {}
_lock = threading.Lock()
${jetsonSection}
def generic_stats() -> dict:
"""Read generic Linux system stats."""
stats: dict = {"platform": platform.machine(), "node": platform.node(), "timestamp": time.time()}
try:
with open("/proc/loadavg") as f:
parts = f.read().split()
stats["load_1m"], stats["load_5m"], stats["load_15m"] = parts[0], parts[1], parts[2]
except OSError:
pass
try:
with open("/proc/meminfo") as f:
lines = {l.split(":")[0]: int(l.split(":")[1].strip().split()[0]) for l in f if ":" in l}
stats["mem_total_kb"] = lines.get("MemTotal")
stats["mem_avail_kb"] = lines.get("MemAvailable")
except OSError:
pass
return stats
def poll_loop():
while True:
data = generic_stats()
${hasJetsonGPU || hasTegraMonitor ? ' data["jetson"] = jetson_stats()' : ''}
with _lock:
_stats.update(data)
time.sleep(POLL_INTERVAL_S)
class HealthHandler(BaseHTTPRequestHandler):
def do_GET(self):
with _lock:
body = json.dumps(_stats, indent=2).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(body)
def log_message(self, *_): # silence access log
pass
def main():
threading.Thread(target=poll_loop, daemon=True).start()
server = HTTPServer(("0.0.0.0", MONITOR_PORT), HealthHandler)
print(f"[monitor] listening on :{MONITOR_PORT}")
server.serve_forever()
if __name__ == "__main__":
main()
`;
}
private genColconPackageXml(pkgName: string, hasIsaacROS2: boolean): string {
const isaacDeps = hasIsaacROS2
? ` <depend>isaac_ros_common</depend>\n <depend>isaac_ros_nitros</depend>\n`
: '';
return `<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>${pkgName}</name>
<version>0.1.0</version>
<description>HoloScript ROS2 bridge — generated by EdgeCompiler</description>
<maintainer email="agent@holoscript.net">holoscript-agent</maintainer>
<license>Apache-2.0</license>
<buildtool_depend>ament_python</buildtool_depend>
<depend>rclpy</depend>
<depend>std_msgs</depend>
<depend>geometry_msgs</depend>
<depend>sensor_msgs</depend>
<depend>control_msgs</depend>
<depend>action_msgs</depend>
${isaacDeps}
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
`;
}
private genColconSetupPy(pkgName: string): string {
return `from setuptools import find_packages, setup
package_name = '${pkgName}'
setup(
name=package_name,
version='0.1.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages', ['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='holoscript-agent',
maintainer_email='agent@holoscript.net',
description='HoloScript ROS2 bridge — generated by EdgeCompiler',
license='Apache-2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'bridge = ${pkgName}.bridge:main',
'isaac_bridge = ${pkgName}.isaac_bridge:main',
],
},
)
`;
}
private genROS2Bridge(name: string, pkgName: string): string {
const nodeVar = name.replace(/[^a-z0-9_]/gi, '_').toLowerCase();
return `#!/usr/bin/env python3
"""
HoloScript ROS2 Bridge — ${name}
Colcon-buildable ament_python node. Build: cd ros2_ws && colcon build --symlink-install
Run: ros2 run ${pkgName} bridge
Publishes typed ROS2 messages instead of raw strings:
/holoscript/cmd_vel geometry_msgs/Twist — velocity commands
/holoscript/joint_commands sensor_msgs/JointState — joint position targets
/holoscript/commands std_msgs/String — raw HoloScript action JSON
Subscribes:
/holoscript/feedback std_msgs/String — task result feedback
/holoscript/joint_states sensor_msgs/JointState — live joint state
"""
import json, os, threading
import rclpy
from rclpy.node import Node
from rclpy.action import ActionClient
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from sensor_msgs.msg import JointState
from control_msgs.action import FollowJointTrajectory
from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
import builtin_interfaces.msg
TOPIC_PREFIX = os.getenv('ROS2_TOPIC_PREFIX', '/holoscript')
NODE_NAME = os.getenv('ROS2_NODE_NAME', '${nodeVar}_bridge')
class HoloScriptBridgeNode(Node):
"""Typed ROS2 bridge for HoloScript edge agent commands."""
def __init__(self):
super().__init__(NODE_NAME)
# Publishers
self.pub_cmd_vel = self.create_publisher(Twist, f'{TOPIC_PREFIX}/cmd_vel', 10)
self.pub_joint_cmd = self.create_publisher(JointState, f'{TOPIC_PREFIX}/joint_commands', 10)
self.pub_raw = self.create_publisher(String, f'{TOPIC_PREFIX}/commands', 10)
# Subscribers
self.sub_feedback = self.create_subscription(
String, f'{TOPIC_PREFIX}/feedback', self._on_feedback, 10)
self.sub_joint_state = self.create_subscription(
JointState, f'{TOPIC_PREFIX}/joint_states', self._on_joint_state, 10)
# Optional joint trajectory action client (ros2_control)
self._joint_traj_client = ActionClient(
self, FollowJointTrajectory, '/joint_trajectory_controller/follow_joint_trajectory')
self._last_joint_state: JointState | None = None
self.get_logger().info(f'HoloScript ROS2 bridge ready — topics on {TOPIC_PREFIX}')
def _on_feedback(self, msg: String) -> None:
self.get_logger().info(f'[feedback] {msg.data[:200]}')
def _on_joint_state(self, msg: JointState) -> None:
self._last_joint_state = msg
# ── Command dispatch ────────────────────────────────────────────────────
def send_velocity(self, linear_x: float = 0.0, linear_y: float = 0.0,
angular_z: float = 0.0) -> None:
"""Publish a Twist velocity command."""
msg = Twist()
msg.linear.x = float(linear_x)
msg.linear.y = float(linear_y)
msg.angular.z = float(angular_z)
self.pub_cmd_vel.publish(msg)
def send_joint_positions(self, joint_names: list[str], positions: list[float]) -> None:
"""Publish joint position targets via JointState."""
msg = JointState()
msg.header.stamp = self.get_clock().now().to_msg()
msg.name = joint_names
msg.position = [float(p) for p in positions]
self.pub_joint_cmd.publish(msg)
def send_trajectory(self, joint_names: list[str],
waypoints: list[dict]) -> None:
"""Send a joint trajectory to the ros2_control action server."""
goal = FollowJointTrajectory.Goal()
traj = JointTrajectory()
traj.joint_names = joint_names
for wp in waypoints:
pt = JointTrajectoryPoint()
pt.positions = [float(p) for p in wp.get('positions', [])]
pt.velocities = [float(v) for v in wp.get('velocities', [0.0] * len(joint_names))]
sec = int(wp.get('time_from_start', 1.0))
pt.time_from_start = builtin_interfaces.msg.Duration(sec=sec, nanosec=0)
traj.points.append(pt)
goal.trajectory = traj
if self._joint_traj_client.wait_for_server(timeout_sec=1.0):
self._joint_traj_client.send_goal_async(goal)
else:
self.get_logger().warn('joint_trajectory_controller not available — publishing JointState instead')
self.send_joint_positions(joint_names, waypoints[0].get('positions', []))
def dispatch_holoscript_action(self, action: dict) -> None:
"""Route a HoloScript action dict to the correct ROS2 message."""
kind = action.get('type', action.get('kind', ''))
if kind in ('move', 'velocity', 'cmd_vel'):
self.send_velocity(
linear_x=action.get('linear_x', action.get('x', 0.0)),
linear_y=action.get('linear_y', action.get('y', 0.0)),
angular_z=action.get('angular_z', action.get('yaw', 0.0)),
)
elif kind in ('joint', 'joint_position', 'actuate'):
self.send_joint_positions(
action.get('joints', action.get('joint_names', [])),
action.get('positions', action.get('values', [])),
)
elif kind in ('trajectory', 'joint_trajectory'):
self.send_trajectory(
action.get('joints', []),
action.get('waypoints', []),
)
else:
# Fall back to raw JSON string for unknown action types.
m = String()
m.data = json.dumps(action)
self.pub_raw.publish(m)
_bridge_node: HoloScriptBridgeNode | None = None
_bridge_thread: threading.Thread | None = None
def get_bridge() -> HoloScriptBridgeNode:
"""Return the singleton bridge node, starting it if needed."""
global _bridge_node, _bridge_thread
if _bridge_node is None:
rclpy.init()
_bridge_node = HoloScriptBridgeNode()
_bridge_thread = threading.Thread(target=rclpy.spin, args=(_bridge_node,), daemon=True)
_bridge_thread.start()
return _bridge_node
def main() -> None:
rclpy.init()
node = HoloScriptBridgeNode()
try:
rclpy.spin(node)
except KeyboardInterrupt:
pass
finally:
node.destroy_node()
rclpy.try_shutdown()
if __name__ == '__main__':
main()
`;
}
private genIsaacBridge(name: string, pkgName: string): string {
const nodeVar = name.replace(/[^a-z0-9_]/gi, '_').toLowerCase();
return `#!/usr/bin/env python3
"""
HoloScript Isaac ROS 2 Bridge — ${name}
NVIDIA Isaac ROS 2 integration layer — NITROS transport, CuVSLAM, Isaac Manipulator.
Requirements:
- JetPack 6.0+ (Ubuntu 22.04 / JetPack 6.2.1 verified on Orin Nano Super)
- Isaac ROS 2 Common: https://nvidia-isaac-ros.github.io/repositories_and_packages/isaac_ros_common/
- isaac_ros_nitros (hardware-accelerated zero-copy transport)
- Optional: isaac_ros_visual_slam (CuVSLAM 3D tracking)
- Optional: isaac_ros_manipulator (cuMotion planner)
Build:
source /opt/ros/humble/setup.bash
source ~/ros2_ws/install/setup.bash
cd ~/ros2_ws && colcon build --symlink-install --packages-select ${pkgName}
ros2 run ${pkgName} isaac_bridge
"""
import json, os, threading
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from geometry_msgs.msg import PoseStamped, Twist
from sensor_msgs.msg import Image, CameraInfo, PointCloud2
TOPIC_PREFIX = os.getenv('ROS2_TOPIC_PREFIX', '/holoscript')
ISAAC_NODE_NAME = os.getenv('ISAAC_NODE_NAME', '${nodeVar}_isaac')
class HoloScriptIsaacBridgeNode(Node):
"""Isaac ROS 2 bridge — subscribes to NITROS-accelerated perception topics
and exposes them to HoloScript agents as structured events.
NITROS (NVIDIA Isaac Transport for ROS) passes GPU tensor buffers
between nodes without CPU copies. HoloScript receives the parsed
semantic output, not the raw tensors.
"""
def __init__(self):
super().__init__(ISAAC_NODE_NAME)
# ── Perception inputs (Isaac ROS 2 outputs → HoloScript) ───────────
# CuVSLAM visual odometry pose (isaac_ros_visual_slam)
self.sub_slam_pose = self.create_subscription(
PoseStamped,
'/visual_slam/tracking/odometry',
self._on_slam_pose, 10,
)
# Raw image from camera pipeline (isaac_ros_image_pipeline)
self.sub_image = self.create_subscription(
Image, '/image_rect', self._on_image, 10)
# Depth / point cloud (isaac_ros_depth_segmentation / nvblox)
self.sub_depth = self.create_subscription(
PointCloud2, '/nvblox_node/static_map_pc', self._on_depth, 10)
# ── HoloScript command relay ────────────────────────────────────────
self.sub_hs_commands = self.create_subscription(
String, f'{TOPIC_PREFIX}/commands', self._on_hs_command, 10)
# ── HoloScript perception events (out) ─────────────────────────────
self.pub_perception = self.create_publisher(
String, f'{TOPIC_PREFIX}/perception', 10)
self.pub_cmd_vel = self.create_publisher(
Twist, f'{TOPIC_PREFIX}/cmd_vel', 10)
# cuMotion planner availability check
self._has_cumotion = False
self._check_cumotion()
self.get_logger().info(
f'[{ISAAC_NODE_NAME}] Isaac ROS 2 bridge ready — '
f'CuVSLAM=listening, cuMotion={self._has_cumotion}'
)
# ── Perception handlers ─────────────────────────────────────────────────
def _on_slam_pose(self, msg: PoseStamped) -> None:
"""Forward CuVSLAM pose as a HoloScript perception event."""
p = msg.pose.position
q = msg.pose.orientation
event = {
'type': 'slam_pose',
'frame': msg.header.frame_id,
'stamp': msg.header.stamp.sec + msg.header.stamp.nanosec * 1e-9,
'position': {'x': p.x, 'y': p.y, 'z': p.z},
'orientation': {'x': q.x, 'y': q.y, 'z': q.z, 'w': q.w},
}
m = String()
m.data = json.dumps(event)
self.pub_perception.publish(m)
def _on_image(self, msg: Image) -> None:
"""Log image arrival for HoloScript frame-rate monitoring (no copy)."""
self.get_logger().debug(
f'[image] {msg.width}x{msg.height} encoding={msg.encoding}')
def _on_depth(self, msg: PointCloud2) -> None:
"""Forward point cloud metadata as a HoloScript event (no tensor copy)."""
event = {
'type': 'depth_map',
'stamp': msg.header.stamp.sec + msg.header.stamp.nanosec * 1e-9,
'point_count': msg.width * msg.height,
'frame': msg.header.frame_id,
}
m = String()
m.data = json.dumps(event)
self.pub_perception.publish(m)
def _on_hs_command(self, msg: String) -> None:
"""Route HoloScript commands that need Isaac acceleration."""
try:
action = json.loads(msg.data)
except json.JSONDecodeError:
return
kind = action.get('type', '')
if kind == 'navigate_to' and self._has_cumotion:
self._plan_with_cumotion(action)
elif kind in ('move', 'velocity'):
twist = Twist()
twist.linear.x = float(action.get('x', 0.0))
twist.angular.z = float(action.get('yaw', 0.0))
self.pub_cmd_vel.publish(twist)
def _check_cumotion(self) -> None:
"""Check if cuMotion service is available (non-blocking)."""
try:
from rclpy.client import Client # noqa: F401
# cuMotion exposes /compute_motion_plan (MoveGroupActionFeedback shape)
# We don't import the specific type to avoid hard dependency.
self._has_cumotion = self.get_service_names_and_types().__len__() > 0
except Exception:
self._has_cumotion = False