Skip to content

Agent Mesh

ci: add flutter workflow and flutter application skeleton for sam-node FFI

Antonio Ojeaa37102f52ea1
diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml
index 88727a61..8748f4cb 100644
--- a/.github/workflows/test.yaml
+++ b/.github/workflows/test.yaml
@@ -54,12 +54,3 @@ jobs:
- name: Install protoc
run: sudo apt-get update && sudo apt-get install -y protobuf-compiler
- run: make verify
-
- build-mobile:
- runs-on: ubuntu-latest
- steps:
- - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6
- with:
- go-version: ${{ env.GO_VERSION }}
- - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
- - run: make mobile-ffi-host
diff --git a/.github/workflows/test_flutter.yaml b/.github/workflows/test_flutter.yaml
new file mode 100644
index 00000000..91fd5a20
--- /dev/null
+++ b/.github/workflows/test_flutter.yaml
@@ -0,0 +1,50 @@
+# Copyright 2026 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+name: Test Flutter App
+
+on:
+ push:
+ branches:
+ - 'main'
+ tags:
+ - 'v*'
+ pull_request:
+ branches: [ main ]
+
+env:
+ GO_VERSION: "1.26"
+
+jobs:
+ build-ffi:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6
+ with:
+ go-version: ${{ env.GO_VERSION }}
+ - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
+ - run: make mobile-ffi-host
+
+ analyze-flutter:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
+ - uses: subosito/flutter-action@f0e3869966141470404a3952d9a30b9538350811 # v2
+ with:
+ channel: 'stable'
+ - name: Install dependencies
+ run: flutter pub get
+ working-directory: mobile/sam-node-app
+ - name: Analyze code
+ run: flutter analyze
+ working-directory: mobile/sam-node-app
diff --git a/mobile/sam-node-app/lib/main.dart b/mobile/sam-node-app/lib/main.dart
new file mode 100644
index 00000000..7f1f7d8d
--- /dev/null
+++ b/mobile/sam-node-app/lib/main.dart
@@ -0,0 +1,174 @@
+import 'package:flutter/material.dart';
+import 'package:path_provider/path_provider.dart';
+import 'sam_ffi.dart';
+
+void main() {
+ runApp(const MyApp());
+}
+
+class MyApp extends StatelessWidget {
+ const MyApp({super.key});
+
+ @override
+ Widget build(BuildContext context) {
+ return MaterialApp(
+ title: 'SAM Node Control',
+ theme: ThemeData(
+ primarySwatch: Colors.blue,
+ useMaterial3: true,
+ ),
+ home: const NodeControlPage(),
+ );
+ }
+}
+
+class NodeControlPage extends StatefulWidget {
+ const NodeControlPage({super.key});
+
+ @override
+ State<NodeControlPage> createState() => _NodeControlPageState();
+}
+
+class _NodeControlPageState extends State<NodeControlPage> {
+ final _hubController = TextEditingController(text: 'https://bananas.sam-mesh.dev');
+ final _jwtController = TextEditingController();
+ final _tokenController = TextEditingController(text: 'secret-token');
+
+ late SamNodeLib _samLib;
+ bool _running = false;
+ String _status = 'Disconnected';
+ String _nodeID = '';
+
+ @override
+ void initState() {
+ super.initState();
+ _samLib = SamNodeLib();
+ }
+
+ Future<void> _enroll() async {
+ final appDir = await getApplicationDocumentsDirectory();
+ final dataDir = '${appDir.path}/sam_data';
+ final err = _samLib.enroll(
+ dataDir,
+ _hubController.text,
+ _jwtController.text,
+ true, // allowLoopback
+ );
+
+ setState(() {
+ if (err != null) {
+ _status = 'Enrollment failed: $err';
+ } else {
+ _status = 'Enrollment successful! You can now start the node.';
+ }
+ });
+ }
+
+ Future<void> _start() async {
+ final appDir = await getApplicationDocumentsDirectory();
+ final dataDir = '${appDir.path}/sam_data';
+
+ final err = _samLib.start({
+ 'dataDir': dataDir,
+ 'hubURL': _hubController.text,
+ 'meshID': 'public-mesh',
+ 'bindAddr': '127.0.0.1:5005', // sidecar port inside phone
+ 'apiToken': _tokenController.text,
+ 'allowLoopback': true,
+ 'enableRelay': false,
+ });
+
+ if (err != null) {
+ setState(() {
+ _status = 'Start failed: $err';
+ });
+ return;
+ }
+
+ setState(() {
+ _running = true;
+ _status = 'Running';
+ _nodeID = _samLib.getNodeID() ?? 'unknown';
+ });
+ }
+
+ void _stop() {
+ final err = _samLib.stop();
+ setState(() {
+ if (err != null) {
+ _status = 'Stop failed: $err';
+ } else {
+ _running = false;
+ _status = 'Stopped';
+ _nodeID = '';
+ }
+ });
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ appBar: AppBar(title: const Text('SAM Node Mobile')),
+ body: SingleChildScrollView(
+ padding: const EdgeInsets.all(16.0),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ TextField(
+ controller: _hubController,
+ decoration: const InputDecoration(labelText: 'Hub URL / Address'),
+ ),
+ TextField(
+ controller: _jwtController,
+ decoration: const InputDecoration(labelText: 'Enrollment JWT'),
+ ),
+ TextField(
+ controller: _tokenController,
+ decoration: const InputDecoration(labelText: 'Local API Token'),
+ ),
+ const SizedBox(height: 20),
+ ElevatedButton(
+ onPressed: _enroll,
+ child: const Text('Enroll Node'),
+ ),
+ const SizedBox(height: 10),
+ Row(
+ children: [
+ Expanded(
+ child: ElevatedButton(
+ onPressed: _running ? null : _start,
+ child: const Text('Start Node'),
+ ),
+ ),
+ const SizedBox(width: 10),
+ Expanded(
+ child: ElevatedButton(
+ onPressed: _running ? _stop : null,
+ style: ElevatedButton.styleFrom(
+ backgroundColor: Colors.red.shade100,
+ ),
+ child: const Text('Stop Node'),
+ ),
+ ),
+ ],
+ ),
+ const SizedBox(height: 30),
+ Card(
+ child: Padding(
+ padding: const EdgeInsets.all(16.0),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text('Status: $_status', style: const TextStyle(fontWeight: FontWeight.bold)),
+ const SizedBox(height: 10),
+ Text('Node ID: $_nodeID', style: const TextStyle(fontFamily: 'monospace')),
+ ],
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+}
diff --git a/mobile/sam-node-app/lib/sam_ffi.dart b/mobile/sam-node-app/lib/sam_ffi.dart
new file mode 100644
index 00000000..b16fd084
--- /dev/null
+++ b/mobile/sam-node-app/lib/sam_ffi.dart
@@ -0,0 +1,99 @@
+import 'dart:convert';
+import 'dart:ffi' as ffi;
+import 'dart:io';
+import 'package:ffi/ffi.dart';
+
+// FFI signatures
+typedef StartNodeC = ffi.Pointer<Utf8> Function(ffi.Pointer<Utf8> configJSON);
+typedef StartNodeDart = ffi.Pointer<Utf8> Function(ffi.Pointer<Utf8> configJSON);
+
+typedef StopNodeC = ffi.Pointer<Utf8> Function();
+typedef StopNodeDart = ffi.Pointer<Utf8> Function();
+
+typedef GetNodeIDC = ffi.Pointer<Utf8> Function();
+typedef GetNodeIDDart = ffi.Pointer<Utf8> Function();
+
+typedef EnrollNodeC = ffi.Pointer<Utf8> Function(
+ ffi.Pointer<Utf8> dataDir,
+ ffi.Pointer<Utf8> hubURL,
+ ffi.Pointer<Utf8> jwt,
+ ffi.Int8 allowLoopback);
+typedef EnrollNodeDart = ffi.Pointer<Utf8> Function(
+ ffi.Pointer<Utf8> dataDir,
+ ffi.Pointer<Utf8> hubURL,
+ ffi.Pointer<Utf8> jwt,
+ int allowLoopback);
+
+typedef FreeStringC = ffi.Void Function(ffi.Pointer<Utf8> str);
+typedef FreeStringDart = void Function(ffi.Pointer<Utf8> str);
+
+class SamNodeLib {
+ late ffi.DynamicLibrary _dylib;
+ late StartNodeDart _startNode;
+ late StopNodeDart _stopNode;
+ late GetNodeIDDart _getNodeID;
+ late EnrollNodeDart _enrollNode;
+ late FreeStringDart _freeString;
+
+ SamNodeLib() {
+ if (Platform.isAndroid) {
+ _dylib = ffi.DynamicLibrary.open('libsam.so');
+ } else if (Platform.isIOS || Platform.isMacOS) {
+ _dylib = ffi.DynamicLibrary.process();
+ } else {
+ _dylib = ffi.DynamicLibrary.open('libsam.so'); // fallback
+ }
+
+ _startNode = _dylib.lookupFunction<StartNodeC, StartNodeDart>('StartNode');
+ _stopNode = _dylib.lookupFunction<StopNodeC, StopNodeDart>('StopNode');
+ _getNodeID = _dylib.lookupFunction<GetNodeIDC, GetNodeIDDart>('GetNodeID');
+ _enrollNode = _dylib.lookupFunction<EnrollNodeC, EnrollNodeDart>('EnrollNode');
+ _freeString = _dylib.lookupFunction<FreeStringC, FreeStringDart>('FreeString');
+ }
+
+ String? start(Map<String, dynamic> config) {
+ final configJSON = jsonEncode(config);
+ final cConfig = configJSON.toNativeUtf8();
+ final cErr = _startNode(cConfig);
+ calloc.free(cConfig);
+
+ if (cErr.address == 0) return null;
+ final goErr = cErr.toDartString();
+ _freeString(cErr);
+ return goErr;
+ }
+
+ String? stop() {
+ final cErr = _stopNode();
+ if (cErr.address == 0) return null;
+ final goErr = cErr.toDartString();
+ _freeString(cErr);
+ return goErr;
+ }
+
+ String? getNodeID() {
+ final cID = _getNodeID();
+ if (cID.address == 0) return null;
+ final goID = cID.toDartString();
+ _freeString(cID);
+ return goID;
+ }
+
+ String? enroll(String dataDir, String hubURL, String jwt, bool allowLoopback) {
+ final cDataDir = dataDir.toNativeUtf8();
+ final cHubURL = hubURL.toNativeUtf8();
+ final cJWT = jwt.toNativeUtf8();
+ final cAllowLoopback = allowLoopback ? 1 : 0;
+
+ final cErr = _enrollNode(cDataDir, cHubURL, cJWT, cAllowLoopback);
+
+ calloc.free(cDataDir);
+ calloc.free(cHubURL);
+ calloc.free(cJWT);
+
+ if (cErr.address == 0) return null;
+ final goErr = cErr.toDartString();
+ _freeString(cErr);
+ return goErr;
+ }
+}
diff --git a/mobile/sam-node-app/pubspec.yaml b/mobile/sam-node-app/pubspec.yaml
new file mode 100644
index 00000000..09a62754
--- /dev/null
+++ b/mobile/sam-node-app/pubspec.yaml
@@ -0,0 +1,22 @@
+name: sam_node_app
+description: "A PoC Flutter app for starting/stopping sam-node via FFI."
+publish_to: 'none'
+
+version: 1.0.0+1
+
+environment:
+ sdk: '>=3.0.0 <4.0.0'
+
+dependencies:
+ flutter:
+ sdk: flutter
+ ffi: ^2.1.0
+ path_provider: ^2.1.1
+
+dev_dependencies:
+ flutter_test:
+ sdk: flutter
+ flutter_lints: ^3.0.0
+
+flutter:
+ uses-material-design: true