asterism 0.12.0 → 0.14.0

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.
@@ -241,7 +241,7 @@ module Asterism
241
241
 
242
242
  # The receiving side. Expose it (or a subclass) with caller: :from:
243
243
  #
244
- # Asterism.expose("inbox", inbox, methods: Asterism::Transfer::Inbox::METHODS, caller: :from)
244
+ # net.expose("inbox", inbox, methods: Asterism::Transfer::Inbox::METHODS, caller: :from)
245
245
  #
246
246
  # and call tick from the update loop. The hooks to override:
247
247
  #
@@ -626,8 +626,9 @@ module Asterism
626
626
 
627
627
  # The sending side of one file. Made with the receiver's path
628
628
  # ("<node>/<app>/<object>"), the kind, the local file and the meta the
629
- # kind wants; call step from the update loop until done?. Then ok? and
630
- # answer (commit's answer: the kind's), or reason.
629
+ # kind wants, and net: the Asterism::Connection the calls go out on;
630
+ # call step from the update loop (which polls net) until done?. Then
631
+ # ok? and answer (commit's answer: the kind's), or reason.
631
632
  #
632
633
  # The receiver's position is the truth: every answer says where it
633
634
  # stands and the next piece goes from there. A call that fails (no
@@ -649,8 +650,9 @@ module Asterism
649
650
  attr_accessor :logger
650
651
  attr_reader :path, :size, :sha256, :answer, :reason, :stats, :pos, :id, :kind, :piece, :window
651
652
 
652
- def initialize(to, kind, path, meta: {}, name: nil, piece: PIECE_MAX, window: WINDOW_MAX,
653
+ def initialize(to, kind, path, net:, meta: {}, name: nil, piece: PIECE_MAX, window: WINDOW_MAX,
653
654
  timeout_ms: CALL_TIMEOUT_MS, tries: TRIES, sha256: nil)
655
+ @net = net
654
656
  @to = to
655
657
  @kind = kind
656
658
  @path = path
@@ -781,7 +783,7 @@ module Asterism
781
783
  end
782
784
 
783
785
  def proxy
784
- ::Asterism[@to, timeout_ms: @timeout_ms].async
786
+ @net[@to, timeout_ms: @timeout_ms].async
785
787
  end
786
788
 
787
789
  def call(method, *args)
@@ -0,0 +1,99 @@
1
+ # The command table of the command line (lib/asterism/cli.rb): the nouns
2
+ # and their verbs, the commands without a noun, the older names, and what
3
+ # each command does on the network (it reads, or it calls a method).
4
+ #
5
+ # Plain Ruby with nothing required, so a program that does not load the
6
+ # Zenoh binding (a web server in front of a process that does) can read the
7
+ # table: `require "asterism/cli/commands"`.
8
+ module Asterism
9
+ class CLI
10
+ # The nouns and their verbs: verb => [its arguments, what it does].
11
+ NOUNS = {
12
+ "node" => {
13
+ "list" => ["", "the nodes, a name a line"],
14
+ "info" => ["NODE", "one node: its profile, applications (with their statuses) and objects"]
15
+ },
16
+ "app" => {
17
+ "list" => ["", "the applications, node/app a line"],
18
+ "info" => ["NODE/APP", "one application: its status, topics, types and objects"]
19
+ },
20
+ "object" => {
21
+ "list" => ["[PATTERN]", "the objects, node/app/object a line (PATTERN: * and ** for whole parts)"],
22
+ "info" => ["PATH", "one object (node/app/object): its description and its methods"],
23
+ "find" => ["METHOD", "the objects that have the method (* as a wildcard)"],
24
+ "get" => ["PATH [KEY]", "the next value the application puts on PATH/KEY (its own keys; KEY: ** " \
25
+ "by default; --timeout 5.0)"],
26
+ "echo" => ["PATH [KEY]", "the values it puts there as they come (the text: what changed; --once: " \
27
+ "the first; --raw: each whole)"]
28
+ },
29
+ "topic" => {
30
+ "list" => ["[NODE/APP]", "the topics the applications put values on, node/app/name a line, with their " \
31
+ "types (NODE/APP: * for a part; */* by default); a shared place's " \
32
+ "(node/@shared/name, @shared/name) with the application that declares it"],
33
+ "info" => ["PATH", "one topic (node/app/name): its type, encoding, unit and text, and the definition " \
34
+ "of its type when the application defines it; for a shared key (node/@shared/name, " \
35
+ "@shared/name) each application that declares it, with its own"]
36
+ },
37
+ "status" => {
38
+ "get" => ["NODE/APP", "an application's status, now"],
39
+ "echo" => ["NODE/APP", "its status, then each change as it comes (--once: now, once)"],
40
+ "hz" => ["NODE/APP", "how often it changes: the rate, the intervals' min / max / std dev"]
41
+ }
42
+ }.freeze
43
+
44
+ # The commands without a noun: name => [its arguments, what it does].
45
+ COMMANDS = {
46
+ "call" => ["PATH METHOD [ARG...]", "calls a method; each ARG is JSON, else a Ruby literal, else text"],
47
+ "ping" => ["PATH", "round trips: PATH node/app/object (its meta) or node (its profile); " \
48
+ "--call METHOD [ARG...] times a call instead"],
49
+ "watch" => ["", "nodes, applications and objects coming and going, and status changes"],
50
+ "tree" => ["", "nodes > applications (with their status) > objects (--methods: their methods)"],
51
+ "mcp" => ["", "an MCP server on stdio (needs the mcp gem): the listings as tools, and the methods " \
52
+ "--allow names"],
53
+ "completion" => ["bash|zsh", "the shell's Tab completion: source <(asterism completion bash)"],
54
+ "version" => ["", "the gem's version"]
55
+ }.freeze
56
+
57
+ # The older names, kept with their output.
58
+ ALIASES = {
59
+ "nodes" => "the nodes, their applications (with their statuses) and profiles, in one listing",
60
+ "objects" => "[PATTERN] the objects with their descriptions and methods (object info of each)",
61
+ "echo" => "ping"
62
+ }.freeze
63
+
64
+ # The options that take a value (what __complete skips over).
65
+ VALUE_OPTIONS = %w[--router --credentials --peer --node --app -o --output --timeout --settle --duration --count
66
+ --interval --call --kwargs --allow --period --window].freeze
67
+
68
+ # The options that say where the network is and who this command line
69
+ # is on it. A command line given a connection (CLI.new(net:)) has none.
70
+ CONNECTION_OPTIONS = %w[--router --credentials --peer --node --app].freeze
71
+
72
+ # The commands that act here rather than on the network: an MCP server
73
+ # on stdio, a script for the shell. A command line given a connection
74
+ # does not run them.
75
+ LOCAL = %w[mcp completion].freeze
76
+
77
+ # What a command does on the network. Every command only reads (the
78
+ # tokens, metas, profiles, statuses, topics, the values the applications put),
79
+ # except call, and ping with --call: those run a method of an object.
80
+ #
81
+ # :call path:, method: say which
82
+ # :read it only reads
83
+ # :local it acts here (LOCAL)
84
+ #
85
+ # key: "node list", "call", "ping", ...; call: ping's --call METHOD.
86
+ def self.kind(key, call: nil)
87
+ return :local if LOCAL.include?(key)
88
+ return :call if key == "call"
89
+ return :call if %w[ping echo].include?(key) && call
90
+ :read
91
+ end
92
+
93
+ # What a command line is about to do, once its words are read (before
94
+ # it connects): the command, its kind (CLI.kind), the object and the
95
+ # method it calls (kind :call), its positional arguments and options.
96
+ # What CLI.new(permit:) is asked about.
97
+ Plan = Struct.new(:key, :kind, :path, :method, :args, :options, keyword_init: true)
98
+ end
99
+ end
@@ -81,7 +81,8 @@ module Asterism
81
81
  i = args.size
82
82
  case [key, i]
83
83
  when ["node info", 0] then nodes
84
- when ["app info", 0], ["status get", 0], ["status echo", 0], ["status hz", 0] then staged(cur, apps, 2)
84
+ when ["app info", 0], ["status get", 0], ["status echo", 0], ["status hz", 0], ["topic list", 0],
85
+ ["topic info", 0] then staged(cur, apps, 2)
85
86
  when ["object list", 0], ["object info", 0], ["object get", 0], ["object echo", 0], ["objects", 0], ["call", 0]
86
87
  staged(cur, objects, 3)
87
88
  when ["ping", 0], ["echo", 0] then (cur.include?("/") ? [] : nodes) + staged(cur, objects, 3)
@@ -9,10 +9,15 @@
9
9
  # and objects (always; reads)
10
10
  # asterism_objects the objects and their methods (meta) (always; reads)
11
11
  # asterism_status the applications' statuses (always; reads)
12
- # asterism_values the latest value of each key the applications put
13
- # on (asterism/<node>/<app>/<object>/<more>), as this
14
- # server received them since it started (always;
15
- # reads; lib/asterism/cli/values.rb)
12
+ # asterism_topics the topics the applications declare (their types,
13
+ # shared places included,
14
+ # encodings, units) and their own types' definitions
15
+ # (@profile?pubs;types) (always; reads)
16
+ # asterism_values the latest value of each topic the applications put
17
+ # on (asterism/<node>/<app>/<name>), as this server
18
+ # received them since it started, with their
19
+ # encodings and sequence numbers, pictures as images
20
+ # when asked (always; reads; lib/asterism/cli/values.rb)
16
21
  # <node>.<app>.<object>.<method>
17
22
  # one per method that an --allow pattern names, made
18
23
  # from the object's meta: the arguments' names the
@@ -34,8 +39,10 @@ module Asterism
34
39
  These tools look at an Asterism network: Ruby programs and boards that expose objects to each other over
35
40
  Zenoh. Paths are <node>/<app>/<object>. asterism_nodes lists the nodes and their applications (with the
36
41
  status each application tells about itself, a JSON object of its own), asterism_objects the objects and
37
- their methods, asterism_status the statuses, asterism_values the latest values the applications put on
38
- their own keys (a game table's state). The other tools call one method each, on the object their
42
+ their methods, asterism_status the statuses, asterism_topics the topics the applications put values on
43
+ (with their types), asterism_values the latest values put on them (a game table's state) and in the
44
+ shared places (<node>/@shared/..., @shared/...; each value with its sender, from). The other
45
+ tools call one method each, on the object their
39
46
  name says (<node>.<app>.<object>.<method>); only the methods this server was started to allow are there.
40
47
  A call's answer is {"value": ...}; an error says its kind (raised, not_exposed, arguments, ...).
41
48
  TEXT
@@ -48,7 +55,7 @@ module Asterism
48
55
  @call_tools = {} # tool name => [path, method, arity, meta entry]
49
56
  @signature = nil
50
57
  @lock = Mutex.new
51
- @values = Values::Store.new(alive: ->(app) { !::Asterism.apps(app).empty? })
58
+ @values = Values::Store.new(alive: ->(app) { !@net.apps(app).empty? })
52
59
  end
53
60
 
54
61
  attr_reader :values
@@ -68,12 +75,23 @@ module Asterism
68
75
  @refresher&.kill
69
76
  end
70
77
 
71
- # From now on, the puts on the applications' own keys (asterism/**
72
- # does not match @profile and @status), and the applications that
73
- # leave (their values are marked).
78
+ # The key expressions remember_values subscribes to: the
79
+ # applications' own keys (asterism/** does not match @profile,
80
+ # @status nor @shared), the nodes' shared places and the network's
81
+ # (docs/protocol.md 17).
82
+ VALUE_KEYS = ["#{Values::PREFIX}**", "#{Values::PREFIX}*/#{Values::SHARED}/**",
83
+ "#{Values::PREFIX}#{Values::SHARED}/**"].freeze
84
+
85
+ # From now on, the puts on the applications' own keys and in the
86
+ # shared places, and the applications that leave (their values are
87
+ # marked).
74
88
  def remember_values
75
- @net.session.subscribe("#{Values::PREFIX}**", depth: 1024) { |s| @values.put(s.key, s.payload) }
76
- @net.session.liveliness_watch("#{Values::PREFIX}*/*", depth: 1024) do |key, alive|
89
+ VALUE_KEYS.each do |k|
90
+ @net.zenoh.subscribe(k, depth: 1024) do |s|
91
+ @values.put(s.key, s.payload, encoding: s.encoding, attachment: s.attachment)
92
+ end
93
+ end
94
+ @net.zenoh.liveliness_watch("#{Values::PREFIX}*/*", depth: 1024) do |key, alive|
77
95
  @values.left(key.delete_prefix(Values::PREFIX)) unless alive
78
96
  end
79
97
  end
@@ -173,44 +191,92 @@ module Asterism
173
191
  end
174
192
  end
175
193
  end,
194
+ ::MCP::Tool.define(
195
+ name: "asterism_topics",
196
+ description: "The topics the applications put values on (asterism/<node>/<app>/<name>), as they " \
197
+ "declare them: {\"<node>/<app>\": {\"topics\": [{\"name\", \"type\", \"encoding\", " \
198
+ "\"unit\", \"desc\", \"ts\"}], \"types\": {...}}}. type: a built-in type (number, " \
199
+ "vector3 [x, y, z], pose [x, y, z, qx, qy, qz, qw], image, state, log, text) or the name of " \
200
+ "an application's own type, whose definition is in types ({\"f\": [[field, type], ...], " \
201
+ "\"d\": what it is}); no type: not declared. encoding: absent for MessagePack, else how " \
202
+ "the bytes are (image/jpeg). ts: the sender's time comes with each value. shared: the topic " \
203
+ "is on a shared place, its name the key below asterism/ (\"node\": " \
204
+ "<node>/@shared/<name>, \"global\": @shared/<name>); several applications may declare " \
205
+ "one shared key, each with its own declaration and types (read a value's type from the " \
206
+ "application that sent it). An application that declares nothing is not listed. The " \
207
+ "values are asterism_values's.",
208
+ input_schema: { properties: { node: { type: "string" }, app: { type: "string" } } },
209
+ annotations: { read_only_hint: true }
210
+ ) do |server_context:, node: nil, app: nil|
211
+ bridge.answer do
212
+ list = sv.apps("#{node || '*'}/#{app || '*'}")
213
+ sv.topics(list).reject { |_, h| h["topics"].empty? && h["types"].empty? }
214
+ end
215
+ end,
176
216
  ::MCP::Tool.define(
177
217
  name: "asterism_values",
178
218
  description: VALUES_DOC,
179
219
  input_schema: {
180
220
  properties: {
181
221
  pattern: { type: "string",
182
- description: "keys as node/app/object/more, with * (one part) and ** (any parts); " \
183
- "default ** (every key). A key always has a part after the object: " \
184
- "games/games/othello1/** or games/*/*/state, not games/games/othello1" },
222
+ description: "keys as node/app/<topic name> (a name may have several parts), with * " \
223
+ "(one part) and ** (any parts); default ** (every application's key). " \
224
+ "The whole name: games/games/othello1/** or games/*/*/state, not " \
225
+ "games/games/othello1 for the topic othello1/state. The shared places: " \
226
+ "node/@shared/<name> (*/@shared/spectate/** every node's) and " \
227
+ "@shared/<name> (the network's); * and ** never match @shared itself" },
185
228
  wait: { type: "number",
186
229
  description: "seconds (at most #{Values::Store::MAX_WAIT}) to wait for the first put " \
187
- "that matches when none has come yet; default 0 (answer now)" }
230
+ "that matches when none has come yet; default 0 (answer now)" },
231
+ images: { type: "boolean",
232
+ description: "true: the values that are pictures (image/png, image/jpeg, image/gif, " \
233
+ "image/webp) come along as images, at most #{Values::Store::MAX_IMAGES}; " \
234
+ "default false (their size only)" }
188
235
  }
189
236
  },
190
237
  annotations: { read_only_hint: true }
191
- ) do |server_context:, pattern: "**", wait: 0|
192
- bridge.answer { bridge.values.answer(pattern, wait: wait) }
238
+ ) do |server_context:, pattern: "**", wait: 0, images: false|
239
+ bridge.values_answer(pattern, wait: wait, images: images)
193
240
  rescue ArgumentError => e
194
241
  bridge.error(e.message)
195
242
  end
196
243
  ]
197
244
  end
198
245
 
199
- VALUES_DOC = "The latest value each application put on its own keys (asterism/<node>/<app>/<object>/<more>: " \
200
- "a game table's state, a log), as this server received them: {\"since\", \"values\": " \
201
- "[{\"key\", \"value\", \"t\", \"age_s\", \"app\", \"bytes\", \"format\"}]}. " \
246
+ VALUES_DOC = "The latest value each application put on its topics (asterism/<node>/<app>/<name>: " \
247
+ "a game table's state, a log, a camera's pictures) and in the shared places (asterism/<node>/" \
248
+ "@shared/<name>, asterism/@shared/<name>: data no single application owns, such as a game " \
249
+ "host's spectate views), as this server received them: {\"since\", " \
250
+ "\"values\": [{\"key\", \"value\", \"t\", \"age_s\", \"app\", \"bytes\", \"format\", " \
251
+ "\"from\", \"seq\", \"run_id\", \"sent\", \"lost\"}]}. " \
252
+ "from: a shared value's sender (<node>/<app>); a shared key has one entry for each sender, " \
253
+ "with that sender's numbers and app. " \
202
254
  "A put comes only when the value changes, and this server remembers only what it received " \
203
255
  "since it started (since): a key that has not been put since then is not listed. Give wait " \
204
256
  "(seconds) to wait for the first put that matches when none has come yet (a quiet table, " \
205
257
  "just after the start). age_s: seconds since it came. app: alive; gone (the application is " \
206
258
  "not on the network now: this is its last value, not the present); restarted (it left and " \
207
- "came back after this value). value: MessagePack or JSON as it is, text as a string; a " \
259
+ "came back after this value). format: msgpack (the value as it is), or the encoding the " \
260
+ "value came with (image/jpeg: the value is its size; JSON and text encodings as they are). " \
261
+ "seq: the value's sequence number, run_id: the sender's connection (another one: it " \
262
+ "connected again and numbers from 0), sent: the sender's time (when it sends it), lost: how " \
263
+ "many values of the key this server missed since it started. value: a " \
208
264
  "value whose JSON is longer than #{Values::Store::VALUE_CUT} bytes is cut (truncated: " \
209
265
  "true, value: the start of its JSON text). It keeps at most #{Values::Store::MAX_KEYS} keys " \
210
266
  "(those received longest ago are forgotten first) and lists at most " \
211
267
  "#{Values::Store::MAX_ANSWER} (more: how many it left out). Nothing came: the note says so " \
212
268
  "(not put since the start, or the router does not let this server read it). The statuses " \
213
- "(@status) are asterism_status's."
269
+ "(@status) are asterism_status's, what each topic is asterism_topics's. images: true hands the " \
270
+ "pictures over as images (the entry's image: its place among them)."
271
+
272
+ # asterism_values's answer: its JSON, then the pictures asked for as
273
+ # MCP images.
274
+ def values_answer(pattern, wait:, images:)
275
+ out, pics = @values.answer_images(pattern, wait: wait, images: images ? Values::Store::MAX_IMAGES : 0)
276
+ content = [{ type: "text", text: JSON.generate(json_safe(out)) }]
277
+ pics.each { |fmt, bytes| content << { type: "image", data: [bytes].pack("m0"), mimeType: fmt } }
278
+ ::MCP::Tool::Response.new(content)
279
+ end
214
280
 
215
281
  def objects(pattern)
216
282
  paths = @survey.objects(pattern)
@@ -289,7 +355,7 @@ module Asterism
289
355
  unless @lock.synchronize { @call_tools.any? { |_, t| t[0] == path && t[1] == method } }
290
356
  return error("#{path}.#{method} is not callable here (not allowed, or gone)")
291
357
  end
292
- v = ::Asterism[path, timeout: @survey.timeout].asterism_call(method, *pos, **kw)
358
+ v = @net[path, timeout: @survey.timeout].asterism_call(method, *pos, **kw)
293
359
  text({ "value" => v })
294
360
  rescue ::Asterism::RemoteError => e
295
361
  error({ "error" => { "kind" => e.remote_kind, "class" => e.remote_class, "message" => e.remote_message } })